blob: bdb73d97da637757eb6e23cb21debdff4bd03909 [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>
Hauke Mehrtensa4855f392012-12-05 18:46:02 +010015#include <linux/platform_device.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020016#include <linux/bcma/bcma.h>
17
Rafał Miłecki4c81aca2016-01-22 18:02:54 +010018static void bcma_chipco_serial_init(struct bcma_drv_cc *cc);
19
Rafał Miłecki8369ae32011-05-09 18:56:46 +020020static inline u32 bcma_cc_write32_masked(struct bcma_drv_cc *cc, u16 offset,
21 u32 mask, u32 value)
22{
23 value &= mask;
24 value |= bcma_cc_read32(cc, offset) & ~mask;
25 bcma_cc_write32(cc, offset, value);
26
27 return value;
28}
29
Hauke Mehrtens69516182013-03-27 17:23:11 +010030u32 bcma_chipco_get_alp_clock(struct bcma_drv_cc *cc)
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010031{
32 if (cc->capabilities & BCMA_CC_CAP_PMU)
Rafał Miłecki5b5ac412012-12-07 12:56:56 +010033 return bcma_pmu_get_alp_clock(cc);
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010034
35 return 20000000;
36}
Hauke Mehrtens69516182013-03-27 17:23:11 +010037EXPORT_SYMBOL_GPL(bcma_chipco_get_alp_clock);
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010038
Hauke Mehrtensf6354c82012-12-05 18:46:00 +010039static u32 bcma_chipco_watchdog_get_max_timer(struct bcma_drv_cc *cc)
40{
41 struct bcma_bus *bus = cc->core->bus;
42 u32 nb;
43
44 if (cc->capabilities & BCMA_CC_CAP_PMU) {
45 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
46 nb = 32;
47 else if (cc->core->id.rev < 26)
48 nb = 16;
49 else
50 nb = (cc->core->id.rev >= 37) ? 32 : 24;
51 } else {
52 nb = 28;
53 }
54 if (nb == 32)
55 return 0xffffffff;
56 else
57 return (1 << nb) - 1;
58}
59
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010060static u32 bcma_chipco_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt,
61 u32 ticks)
62{
63 struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
64
65 return bcma_chipco_watchdog_timer_set(cc, ticks);
66}
67
68static u32 bcma_chipco_watchdog_timer_set_ms_wdt(struct bcm47xx_wdt *wdt,
69 u32 ms)
70{
71 struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
72 u32 ticks;
73
74 ticks = bcma_chipco_watchdog_timer_set(cc, cc->ticks_per_ms * ms);
75 return ticks / cc->ticks_per_ms;
76}
77
78static int bcma_chipco_watchdog_ticks_per_ms(struct bcma_drv_cc *cc)
79{
80 struct bcma_bus *bus = cc->core->bus;
81
82 if (cc->capabilities & BCMA_CC_CAP_PMU) {
83 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
Oscar Forner Martinezd0f66df2014-12-27 19:24:28 +000084 /* 4706 CC and PMU watchdogs are clocked at 1/4 of ALP
85 * clock
86 */
Rafał Miłecki5b5ac412012-12-07 12:56:56 +010087 return bcma_chipco_get_alp_clock(cc) / 4000;
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010088 else
89 /* based on 32KHz ILP clock */
90 return 32;
91 } else {
Rafał Miłecki5b5ac412012-12-07 12:56:56 +010092 return bcma_chipco_get_alp_clock(cc) / 1000;
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010093 }
94}
Hauke Mehrtensf6354c82012-12-05 18:46:00 +010095
Hauke Mehrtensa4855f392012-12-05 18:46:02 +010096int bcma_chipco_watchdog_register(struct bcma_drv_cc *cc)
97{
98 struct bcm47xx_wdt wdt = {};
99 struct platform_device *pdev;
100
101 wdt.driver_data = cc;
102 wdt.timer_set = bcma_chipco_watchdog_timer_set_wdt;
103 wdt.timer_set_ms = bcma_chipco_watchdog_timer_set_ms_wdt;
Oscar Forner Martinezd0f66df2014-12-27 19:24:28 +0000104 wdt.max_timer_ms =
105 bcma_chipco_watchdog_get_max_timer(cc) / cc->ticks_per_ms;
Hauke Mehrtensa4855f392012-12-05 18:46:02 +0100106
107 pdev = platform_device_register_data(NULL, "bcm47xx-wdt",
108 cc->core->bus->num, &wdt,
109 sizeof(wdt));
110 if (IS_ERR(pdev))
111 return PTR_ERR(pdev);
112
113 cc->watchdog = pdev;
114
115 return 0;
116}
117
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200118void bcma_core_chipcommon_early_init(struct bcma_drv_cc *cc)
119{
Rafał Miłecki4c81aca2016-01-22 18:02:54 +0100120 struct bcma_bus *bus = cc->core->bus;
121
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200122 if (cc->early_setup_done)
123 return;
124
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000125 spin_lock_init(&cc->gpio_lock);
126
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200127 if (cc->core->id.rev >= 11)
128 cc->status = bcma_cc_read32(cc, BCMA_CC_CHIPSTAT);
129 cc->capabilities = bcma_cc_read32(cc, BCMA_CC_CAP);
130 if (cc->core->id.rev >= 35)
131 cc->capabilities_ext = bcma_cc_read32(cc, BCMA_CC_CAP_EXT);
132
133 if (cc->capabilities & BCMA_CC_CAP_PMU)
134 bcma_pmu_early_init(cc);
135
Rafał Miłecki4c81aca2016-01-22 18:02:54 +0100136 if (IS_BUILTIN(CONFIG_BCM47XX) && bus->hosttype == BCMA_HOSTTYPE_SOC)
137 bcma_chipco_serial_init(cc);
138
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200139 cc->early_setup_done = true;
140}
141
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200142void bcma_core_chipcommon_init(struct bcma_drv_cc *cc)
143{
Rafał Miłecki18dfa492011-07-14 21:49:19 +0200144 u32 leddc_on = 10;
145 u32 leddc_off = 90;
146
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200147 if (cc->setup_done)
148 return;
149
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200150 bcma_core_chipcommon_early_init(cc);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200151
Rafał Miłecki1073e4e2011-05-11 02:08:09 +0200152 if (cc->core->id.rev >= 20) {
Rafał Miłecki88f9b652013-06-26 10:02:11 +0200153 u32 pullup = 0, pulldown = 0;
154
155 if (cc->core->bus->chipinfo.id == BCMA_CHIP_ID_BCM43142) {
156 pullup = 0x402e0;
157 pulldown = 0x20500;
158 }
159
160 bcma_cc_write32(cc, BCMA_CC_GPIOPULLUP, pullup);
161 bcma_cc_write32(cc, BCMA_CC_GPIOPULLDOWN, pulldown);
Rafał Miłecki1073e4e2011-05-11 02:08:09 +0200162 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200163
164 if (cc->capabilities & BCMA_CC_CAP_PMU)
165 bcma_pmu_init(cc);
166 if (cc->capabilities & BCMA_CC_CAP_PCTL)
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200167 bcma_err(cc->core->bus, "Power control not implemented!\n");
Rafał Miłecki18dfa492011-07-14 21:49:19 +0200168
169 if (cc->core->id.rev >= 16) {
170 if (cc->core->bus->sprom.leddc_on_time &&
171 cc->core->bus->sprom.leddc_off_time) {
172 leddc_on = cc->core->bus->sprom.leddc_on_time;
173 leddc_off = cc->core->bus->sprom.leddc_off_time;
174 }
175 bcma_cc_write32(cc, BCMA_CC_GPIOTIMER,
176 ((leddc_on << BCMA_CC_GPIOTIMER_ONTIME_SHIFT) |
177 (leddc_off << BCMA_CC_GPIOTIMER_OFFTIME_SHIFT)));
178 }
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100179 cc->ticks_per_ms = bcma_chipco_watchdog_ticks_per_ms(cc);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200180
181 cc->setup_done = true;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200182}
183
184/* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100185u32 bcma_chipco_watchdog_timer_set(struct bcma_drv_cc *cc, u32 ticks)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200186{
Hauke Mehrtensf6354c82012-12-05 18:46:00 +0100187 u32 maxt;
Hauke Mehrtensf6354c82012-12-05 18:46:00 +0100188
189 maxt = bcma_chipco_watchdog_get_max_timer(cc);
190 if (cc->capabilities & BCMA_CC_CAP_PMU) {
191 if (ticks == 1)
192 ticks = 2;
193 else if (ticks > maxt)
194 ticks = maxt;
Rafał Miłeckib3c47af2016-01-19 08:45:26 +0100195 bcma_pmu_write32(cc, BCMA_CC_PMU_WATCHDOG, ticks);
Hauke Mehrtensf6354c82012-12-05 18:46:00 +0100196 } else {
Rafał Miłecki68fcd242015-01-24 00:23:21 +0100197 struct bcma_bus *bus = cc->core->bus;
198
199 if (bus->chipinfo.id != BCMA_CHIP_ID_BCM4707 &&
200 bus->chipinfo.id != BCMA_CHIP_ID_BCM53018)
201 bcma_core_set_clockmode(cc->core,
202 ticks ? BCMA_CLKMODE_FAST : BCMA_CLKMODE_DYNAMIC);
203
Hauke Mehrtensf6354c82012-12-05 18:46:00 +0100204 if (ticks > maxt)
205 ticks = maxt;
206 /* instant NMI */
207 bcma_cc_write32(cc, BCMA_CC_WATCHDOG, ticks);
208 }
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100209 return ticks;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200210}
211
212void bcma_chipco_irq_mask(struct bcma_drv_cc *cc, u32 mask, u32 value)
213{
214 bcma_cc_write32_masked(cc, BCMA_CC_IRQMASK, mask, value);
215}
216
217u32 bcma_chipco_irq_status(struct bcma_drv_cc *cc, u32 mask)
218{
219 return bcma_cc_read32(cc, BCMA_CC_IRQSTAT) & mask;
220}
221
222u32 bcma_chipco_gpio_in(struct bcma_drv_cc *cc, u32 mask)
223{
224 return bcma_cc_read32(cc, BCMA_CC_GPIOIN) & mask;
225}
226
227u32 bcma_chipco_gpio_out(struct bcma_drv_cc *cc, u32 mask, u32 value)
228{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000229 unsigned long flags;
230 u32 res;
231
232 spin_lock_irqsave(&cc->gpio_lock, flags);
233 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUT, mask, value);
234 spin_unlock_irqrestore(&cc->gpio_lock, flags);
235
236 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200237}
Hauke Mehrtensca84a6c2013-03-27 17:23:12 +0100238EXPORT_SYMBOL_GPL(bcma_chipco_gpio_out);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200239
240u32 bcma_chipco_gpio_outen(struct bcma_drv_cc *cc, u32 mask, u32 value)
241{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000242 unsigned long flags;
243 u32 res;
244
245 spin_lock_irqsave(&cc->gpio_lock, flags);
246 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUTEN, mask, value);
247 spin_unlock_irqrestore(&cc->gpio_lock, flags);
248
249 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200250}
Hauke Mehrtensca84a6c2013-03-27 17:23:12 +0100251EXPORT_SYMBOL_GPL(bcma_chipco_gpio_outen);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200252
Hauke Mehrtens3e8bb502012-11-20 22:24:29 +0000253/*
254 * If the bit is set to 0, chipcommon controlls this GPIO,
255 * if the bit is set to 1, it is used by some part of the chip and not our code.
256 */
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200257u32 bcma_chipco_gpio_control(struct bcma_drv_cc *cc, u32 mask, u32 value)
258{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000259 unsigned long flags;
260 u32 res;
261
262 spin_lock_irqsave(&cc->gpio_lock, flags);
263 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOCTL, mask, value);
264 spin_unlock_irqrestore(&cc->gpio_lock, flags);
265
266 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200267}
268EXPORT_SYMBOL_GPL(bcma_chipco_gpio_control);
269
270u32 bcma_chipco_gpio_intmask(struct bcma_drv_cc *cc, u32 mask, u32 value)
271{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000272 unsigned long flags;
273 u32 res;
274
275 spin_lock_irqsave(&cc->gpio_lock, flags);
276 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOIRQ, mask, value);
277 spin_unlock_irqrestore(&cc->gpio_lock, flags);
278
279 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200280}
281
282u32 bcma_chipco_gpio_polarity(struct bcma_drv_cc *cc, u32 mask, u32 value)
283{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000284 unsigned long flags;
285 u32 res;
286
287 spin_lock_irqsave(&cc->gpio_lock, flags);
288 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPOL, mask, value);
289 spin_unlock_irqrestore(&cc->gpio_lock, flags);
290
291 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200292}
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200293
Hauke Mehrtensea3488f2012-11-20 22:24:28 +0000294u32 bcma_chipco_gpio_pullup(struct bcma_drv_cc *cc, u32 mask, u32 value)
295{
296 unsigned long flags;
297 u32 res;
298
299 if (cc->core->id.rev < 20)
300 return 0;
301
302 spin_lock_irqsave(&cc->gpio_lock, flags);
303 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLUP, mask, value);
304 spin_unlock_irqrestore(&cc->gpio_lock, flags);
305
306 return res;
307}
308
309u32 bcma_chipco_gpio_pulldown(struct bcma_drv_cc *cc, u32 mask, u32 value)
310{
311 unsigned long flags;
312 u32 res;
313
314 if (cc->core->id.rev < 20)
315 return 0;
316
317 spin_lock_irqsave(&cc->gpio_lock, flags);
318 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLDOWN, mask, value);
319 spin_unlock_irqrestore(&cc->gpio_lock, flags);
320
321 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200322}
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200323
Rafał Miłecki4c81aca2016-01-22 18:02:54 +0100324static void bcma_chipco_serial_init(struct bcma_drv_cc *cc)
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200325{
Rafał Miłecki4c81aca2016-01-22 18:02:54 +0100326#if IS_BUILTIN(CONFIG_BCM47XX)
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200327 unsigned int irq;
328 u32 baud_base;
329 u32 i;
330 unsigned int ccrev = cc->core->id.rev;
331 struct bcma_serial_port *ports = cc->serial_ports;
332
333 if (ccrev >= 11 && ccrev != 15) {
Rafał Miłecki5b5ac412012-12-07 12:56:56 +0100334 baud_base = bcma_chipco_get_alp_clock(cc);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200335 if (ccrev >= 21) {
336 /* Turn off UART clock before switching clocksource. */
337 bcma_cc_write32(cc, BCMA_CC_CORECTL,
338 bcma_cc_read32(cc, BCMA_CC_CORECTL)
339 & ~BCMA_CC_CORECTL_UARTCLKEN);
340 }
341 /* Set the override bit so we don't divide it */
342 bcma_cc_write32(cc, BCMA_CC_CORECTL,
343 bcma_cc_read32(cc, BCMA_CC_CORECTL)
344 | BCMA_CC_CORECTL_UARTCLK0);
345 if (ccrev >= 21) {
346 /* Re-enable the UART clock. */
347 bcma_cc_write32(cc, BCMA_CC_CORECTL,
348 bcma_cc_read32(cc, BCMA_CC_CORECTL)
349 | BCMA_CC_CORECTL_UARTCLKEN);
350 }
351 } else {
Oscar Forner Martinezd0f66df2014-12-27 19:24:28 +0000352 bcma_err(cc->core->bus, "serial not supported on this device ccrev: 0x%x\n",
353 ccrev);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200354 return;
355 }
356
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100357 irq = bcma_core_irq(cc->core, 0);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200358
359 /* Determine the registers of the UARTs */
360 cc->nr_serial_ports = (cc->capabilities & BCMA_CC_CAP_NRUART);
361 for (i = 0; i < cc->nr_serial_ports; i++) {
362 ports[i].regs = cc->core->io_addr + BCMA_CC_UART0_DATA +
363 (i * 256);
364 ports[i].irq = irq;
365 ports[i].baud_base = baud_base;
366 ports[i].reg_shift = 0;
367 }
Rafał Miłecki4c81aca2016-01-22 18:02:54 +0100368#endif /* CONFIG_BCM47XX */
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200369}