blob: 117222697e1f660887b457c8c51a3e3f2e702e41 [file] [log] [blame]
Rafał Miłecki8369ae32011-05-09 18:56:46 +02001/*
2 * Broadcom specific AMBA
3 * ChipCommon core driver
4 *
5 * Copyright 2005, Broadcom Corporation
Michael Büscheb032b92011-07-04 20:50:05 +02006 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +01007 * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
Rafał Miłecki8369ae32011-05-09 18:56:46 +02008 *
9 * Licensed under the GNU/GPL. See COPYING for details.
10 */
11
12#include "bcma_private.h"
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010013#include <linux/bcm47xx_wdt.h>
Paul Gortmaker44a8e372011-07-27 21:21:04 -040014#include <linux/export.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020015#include <linux/bcma/bcma.h>
16
17static inline u32 bcma_cc_write32_masked(struct bcma_drv_cc *cc, u16 offset,
18 u32 mask, u32 value)
19{
20 value &= mask;
21 value |= bcma_cc_read32(cc, offset) & ~mask;
22 bcma_cc_write32(cc, offset, value);
23
24 return value;
25}
26
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010027static u32 bcma_chipco_alp_clock(struct bcma_drv_cc *cc)
28{
29 if (cc->capabilities & BCMA_CC_CAP_PMU)
30 return bcma_pmu_alp_clock(cc);
31
32 return 20000000;
33}
34
Hauke Mehrtensf6354c82012-12-05 18:46:00 +010035static u32 bcma_chipco_watchdog_get_max_timer(struct bcma_drv_cc *cc)
36{
37 struct bcma_bus *bus = cc->core->bus;
38 u32 nb;
39
40 if (cc->capabilities & BCMA_CC_CAP_PMU) {
41 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
42 nb = 32;
43 else if (cc->core->id.rev < 26)
44 nb = 16;
45 else
46 nb = (cc->core->id.rev >= 37) ? 32 : 24;
47 } else {
48 nb = 28;
49 }
50 if (nb == 32)
51 return 0xffffffff;
52 else
53 return (1 << nb) - 1;
54}
55
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010056static u32 bcma_chipco_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt,
57 u32 ticks)
58{
59 struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
60
61 return bcma_chipco_watchdog_timer_set(cc, ticks);
62}
63
64static u32 bcma_chipco_watchdog_timer_set_ms_wdt(struct bcm47xx_wdt *wdt,
65 u32 ms)
66{
67 struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
68 u32 ticks;
69
70 ticks = bcma_chipco_watchdog_timer_set(cc, cc->ticks_per_ms * ms);
71 return ticks / cc->ticks_per_ms;
72}
73
74static int bcma_chipco_watchdog_ticks_per_ms(struct bcma_drv_cc *cc)
75{
76 struct bcma_bus *bus = cc->core->bus;
77
78 if (cc->capabilities & BCMA_CC_CAP_PMU) {
79 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
80 /* 4706 CC and PMU watchdogs are clocked at 1/4 of ALP clock */
81 return bcma_chipco_alp_clock(cc) / 4000;
82 else
83 /* based on 32KHz ILP clock */
84 return 32;
85 } else {
86 return bcma_chipco_alp_clock(cc) / 1000;
87 }
88}
Hauke Mehrtensf6354c82012-12-05 18:46:00 +010089
Hauke Mehrtens49655bb2012-09-29 20:29:49 +020090void bcma_core_chipcommon_early_init(struct bcma_drv_cc *cc)
91{
92 if (cc->early_setup_done)
93 return;
94
95 if (cc->core->id.rev >= 11)
96 cc->status = bcma_cc_read32(cc, BCMA_CC_CHIPSTAT);
97 cc->capabilities = bcma_cc_read32(cc, BCMA_CC_CAP);
98 if (cc->core->id.rev >= 35)
99 cc->capabilities_ext = bcma_cc_read32(cc, BCMA_CC_CAP_EXT);
100
101 if (cc->capabilities & BCMA_CC_CAP_PMU)
102 bcma_pmu_early_init(cc);
103
104 cc->early_setup_done = true;
105}
106
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200107void bcma_core_chipcommon_init(struct bcma_drv_cc *cc)
108{
Rafał Miłecki18dfa492011-07-14 21:49:19 +0200109 u32 leddc_on = 10;
110 u32 leddc_off = 90;
111
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200112 if (cc->setup_done)
113 return;
114
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200115 bcma_core_chipcommon_early_init(cc);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200116
Rafał Miłecki1073e4e2011-05-11 02:08:09 +0200117 if (cc->core->id.rev >= 20) {
118 bcma_cc_write32(cc, BCMA_CC_GPIOPULLUP, 0);
119 bcma_cc_write32(cc, BCMA_CC_GPIOPULLDOWN, 0);
120 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200121
122 if (cc->capabilities & BCMA_CC_CAP_PMU)
123 bcma_pmu_init(cc);
124 if (cc->capabilities & BCMA_CC_CAP_PCTL)
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200125 bcma_err(cc->core->bus, "Power control not implemented!\n");
Rafał Miłecki18dfa492011-07-14 21:49:19 +0200126
127 if (cc->core->id.rev >= 16) {
128 if (cc->core->bus->sprom.leddc_on_time &&
129 cc->core->bus->sprom.leddc_off_time) {
130 leddc_on = cc->core->bus->sprom.leddc_on_time;
131 leddc_off = cc->core->bus->sprom.leddc_off_time;
132 }
133 bcma_cc_write32(cc, BCMA_CC_GPIOTIMER,
134 ((leddc_on << BCMA_CC_GPIOTIMER_ONTIME_SHIFT) |
135 (leddc_off << BCMA_CC_GPIOTIMER_OFFTIME_SHIFT)));
136 }
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100137 cc->ticks_per_ms = bcma_chipco_watchdog_ticks_per_ms(cc);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200138
139 cc->setup_done = true;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200140}
141
142/* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100143u32 bcma_chipco_watchdog_timer_set(struct bcma_drv_cc *cc, u32 ticks)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200144{
Hauke Mehrtensf6354c82012-12-05 18:46:00 +0100145 u32 maxt;
146 enum bcma_clkmode clkmode;
147
148 maxt = bcma_chipco_watchdog_get_max_timer(cc);
149 if (cc->capabilities & BCMA_CC_CAP_PMU) {
150 if (ticks == 1)
151 ticks = 2;
152 else if (ticks > maxt)
153 ticks = maxt;
154 bcma_cc_write32(cc, BCMA_CC_PMU_WATCHDOG, ticks);
155 } else {
156 clkmode = ticks ? BCMA_CLKMODE_FAST : BCMA_CLKMODE_DYNAMIC;
157 bcma_core_set_clockmode(cc->core, clkmode);
158 if (ticks > maxt)
159 ticks = maxt;
160 /* instant NMI */
161 bcma_cc_write32(cc, BCMA_CC_WATCHDOG, ticks);
162 }
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100163 return ticks;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200164}
165
166void bcma_chipco_irq_mask(struct bcma_drv_cc *cc, u32 mask, u32 value)
167{
168 bcma_cc_write32_masked(cc, BCMA_CC_IRQMASK, mask, value);
169}
170
171u32 bcma_chipco_irq_status(struct bcma_drv_cc *cc, u32 mask)
172{
173 return bcma_cc_read32(cc, BCMA_CC_IRQSTAT) & mask;
174}
175
176u32 bcma_chipco_gpio_in(struct bcma_drv_cc *cc, u32 mask)
177{
178 return bcma_cc_read32(cc, BCMA_CC_GPIOIN) & mask;
179}
180
181u32 bcma_chipco_gpio_out(struct bcma_drv_cc *cc, u32 mask, u32 value)
182{
183 return bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUT, mask, value);
184}
185
186u32 bcma_chipco_gpio_outen(struct bcma_drv_cc *cc, u32 mask, u32 value)
187{
188 return bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUTEN, mask, value);
189}
190
191u32 bcma_chipco_gpio_control(struct bcma_drv_cc *cc, u32 mask, u32 value)
192{
193 return bcma_cc_write32_masked(cc, BCMA_CC_GPIOCTL, mask, value);
194}
195EXPORT_SYMBOL_GPL(bcma_chipco_gpio_control);
196
197u32 bcma_chipco_gpio_intmask(struct bcma_drv_cc *cc, u32 mask, u32 value)
198{
199 return bcma_cc_write32_masked(cc, BCMA_CC_GPIOIRQ, mask, value);
200}
201
202u32 bcma_chipco_gpio_polarity(struct bcma_drv_cc *cc, u32 mask, u32 value)
203{
204 return bcma_cc_write32_masked(cc, BCMA_CC_GPIOPOL, mask, value);
205}
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200206
207#ifdef CONFIG_BCMA_DRIVER_MIPS
208void bcma_chipco_serial_init(struct bcma_drv_cc *cc)
209{
210 unsigned int irq;
211 u32 baud_base;
212 u32 i;
213 unsigned int ccrev = cc->core->id.rev;
214 struct bcma_serial_port *ports = cc->serial_ports;
215
216 if (ccrev >= 11 && ccrev != 15) {
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +0100217 baud_base = bcma_chipco_alp_clock(cc);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200218 if (ccrev >= 21) {
219 /* Turn off UART clock before switching clocksource. */
220 bcma_cc_write32(cc, BCMA_CC_CORECTL,
221 bcma_cc_read32(cc, BCMA_CC_CORECTL)
222 & ~BCMA_CC_CORECTL_UARTCLKEN);
223 }
224 /* Set the override bit so we don't divide it */
225 bcma_cc_write32(cc, BCMA_CC_CORECTL,
226 bcma_cc_read32(cc, BCMA_CC_CORECTL)
227 | BCMA_CC_CORECTL_UARTCLK0);
228 if (ccrev >= 21) {
229 /* Re-enable the UART clock. */
230 bcma_cc_write32(cc, BCMA_CC_CORECTL,
231 bcma_cc_read32(cc, BCMA_CC_CORECTL)
232 | BCMA_CC_CORECTL_UARTCLKEN);
233 }
234 } else {
Rafał Miłecki9a89c3a2012-07-09 19:34:59 +0200235 bcma_err(cc->core->bus, "serial not supported on this device ccrev: 0x%x\n", ccrev);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200236 return;
237 }
238
239 irq = bcma_core_mips_irq(cc->core);
240
241 /* Determine the registers of the UARTs */
242 cc->nr_serial_ports = (cc->capabilities & BCMA_CC_CAP_NRUART);
243 for (i = 0; i < cc->nr_serial_ports; i++) {
244 ports[i].regs = cc->core->io_addr + BCMA_CC_UART0_DATA +
245 (i * 256);
246 ports[i].irq = irq;
247 ports[i].baud_base = baud_base;
248 ports[i].reg_shift = 0;
249 }
250}
251#endif /* CONFIG_BCMA_DRIVER_MIPS */