blob: 88db0cb7bf1985aba46e37bff1ca04522c61f82b [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
18static inline u32 bcma_cc_write32_masked(struct bcma_drv_cc *cc, u16 offset,
19 u32 mask, u32 value)
20{
21 value &= mask;
22 value |= bcma_cc_read32(cc, offset) & ~mask;
23 bcma_cc_write32(cc, offset, value);
24
25 return value;
26}
27
Hauke Mehrtens69516182013-03-27 17:23:11 +010028u32 bcma_chipco_get_alp_clock(struct bcma_drv_cc *cc)
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010029{
30 if (cc->capabilities & BCMA_CC_CAP_PMU)
Rafał Miłecki5b5ac412012-12-07 12:56:56 +010031 return bcma_pmu_get_alp_clock(cc);
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010032
33 return 20000000;
34}
Hauke Mehrtens69516182013-03-27 17:23:11 +010035EXPORT_SYMBOL_GPL(bcma_chipco_get_alp_clock);
Hauke Mehrtens56fd5f02012-12-05 18:45:59 +010036
Hauke Mehrtensf6354c82012-12-05 18:46:00 +010037static u32 bcma_chipco_watchdog_get_max_timer(struct bcma_drv_cc *cc)
38{
39 struct bcma_bus *bus = cc->core->bus;
40 u32 nb;
41
42 if (cc->capabilities & BCMA_CC_CAP_PMU) {
43 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
44 nb = 32;
45 else if (cc->core->id.rev < 26)
46 nb = 16;
47 else
48 nb = (cc->core->id.rev >= 37) ? 32 : 24;
49 } else {
50 nb = 28;
51 }
52 if (nb == 32)
53 return 0xffffffff;
54 else
55 return (1 << nb) - 1;
56}
57
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010058static u32 bcma_chipco_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt,
59 u32 ticks)
60{
61 struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
62
63 return bcma_chipco_watchdog_timer_set(cc, ticks);
64}
65
66static u32 bcma_chipco_watchdog_timer_set_ms_wdt(struct bcm47xx_wdt *wdt,
67 u32 ms)
68{
69 struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
70 u32 ticks;
71
72 ticks = bcma_chipco_watchdog_timer_set(cc, cc->ticks_per_ms * ms);
73 return ticks / cc->ticks_per_ms;
74}
75
76static int bcma_chipco_watchdog_ticks_per_ms(struct bcma_drv_cc *cc)
77{
78 struct bcma_bus *bus = cc->core->bus;
79
80 if (cc->capabilities & BCMA_CC_CAP_PMU) {
81 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
82 /* 4706 CC and PMU watchdogs are clocked at 1/4 of ALP clock */
Rafał Miłecki5b5ac412012-12-07 12:56:56 +010083 return bcma_chipco_get_alp_clock(cc) / 4000;
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010084 else
85 /* based on 32KHz ILP clock */
86 return 32;
87 } else {
Rafał Miłecki5b5ac412012-12-07 12:56:56 +010088 return bcma_chipco_get_alp_clock(cc) / 1000;
Hauke Mehrtensa22a3112012-12-05 18:46:01 +010089 }
90}
Hauke Mehrtensf6354c82012-12-05 18:46:00 +010091
Hauke Mehrtensa4855f392012-12-05 18:46:02 +010092int bcma_chipco_watchdog_register(struct bcma_drv_cc *cc)
93{
94 struct bcm47xx_wdt wdt = {};
95 struct platform_device *pdev;
96
97 wdt.driver_data = cc;
98 wdt.timer_set = bcma_chipco_watchdog_timer_set_wdt;
99 wdt.timer_set_ms = bcma_chipco_watchdog_timer_set_ms_wdt;
100 wdt.max_timer_ms = bcma_chipco_watchdog_get_max_timer(cc) / cc->ticks_per_ms;
101
102 pdev = platform_device_register_data(NULL, "bcm47xx-wdt",
103 cc->core->bus->num, &wdt,
104 sizeof(wdt));
105 if (IS_ERR(pdev))
106 return PTR_ERR(pdev);
107
108 cc->watchdog = pdev;
109
110 return 0;
111}
112
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200113void bcma_core_chipcommon_early_init(struct bcma_drv_cc *cc)
114{
115 if (cc->early_setup_done)
116 return;
117
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000118 spin_lock_init(&cc->gpio_lock);
119
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200120 if (cc->core->id.rev >= 11)
121 cc->status = bcma_cc_read32(cc, BCMA_CC_CHIPSTAT);
122 cc->capabilities = bcma_cc_read32(cc, BCMA_CC_CAP);
123 if (cc->core->id.rev >= 35)
124 cc->capabilities_ext = bcma_cc_read32(cc, BCMA_CC_CAP_EXT);
125
126 if (cc->capabilities & BCMA_CC_CAP_PMU)
127 bcma_pmu_early_init(cc);
128
129 cc->early_setup_done = true;
130}
131
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200132void bcma_core_chipcommon_init(struct bcma_drv_cc *cc)
133{
Rafał Miłecki18dfa492011-07-14 21:49:19 +0200134 u32 leddc_on = 10;
135 u32 leddc_off = 90;
136
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200137 if (cc->setup_done)
138 return;
139
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200140 bcma_core_chipcommon_early_init(cc);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200141
Rafał Miłecki1073e4e2011-05-11 02:08:09 +0200142 if (cc->core->id.rev >= 20) {
143 bcma_cc_write32(cc, BCMA_CC_GPIOPULLUP, 0);
144 bcma_cc_write32(cc, BCMA_CC_GPIOPULLDOWN, 0);
145 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200146
147 if (cc->capabilities & BCMA_CC_CAP_PMU)
148 bcma_pmu_init(cc);
149 if (cc->capabilities & BCMA_CC_CAP_PCTL)
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200150 bcma_err(cc->core->bus, "Power control not implemented!\n");
Rafał Miłecki18dfa492011-07-14 21:49:19 +0200151
152 if (cc->core->id.rev >= 16) {
153 if (cc->core->bus->sprom.leddc_on_time &&
154 cc->core->bus->sprom.leddc_off_time) {
155 leddc_on = cc->core->bus->sprom.leddc_on_time;
156 leddc_off = cc->core->bus->sprom.leddc_off_time;
157 }
158 bcma_cc_write32(cc, BCMA_CC_GPIOTIMER,
159 ((leddc_on << BCMA_CC_GPIOTIMER_ONTIME_SHIFT) |
160 (leddc_off << BCMA_CC_GPIOTIMER_OFFTIME_SHIFT)));
161 }
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100162 cc->ticks_per_ms = bcma_chipco_watchdog_ticks_per_ms(cc);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200163
164 cc->setup_done = true;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200165}
166
167/* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100168u32 bcma_chipco_watchdog_timer_set(struct bcma_drv_cc *cc, u32 ticks)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200169{
Hauke Mehrtensf6354c82012-12-05 18:46:00 +0100170 u32 maxt;
171 enum bcma_clkmode clkmode;
172
173 maxt = bcma_chipco_watchdog_get_max_timer(cc);
174 if (cc->capabilities & BCMA_CC_CAP_PMU) {
175 if (ticks == 1)
176 ticks = 2;
177 else if (ticks > maxt)
178 ticks = maxt;
179 bcma_cc_write32(cc, BCMA_CC_PMU_WATCHDOG, ticks);
180 } else {
181 clkmode = ticks ? BCMA_CLKMODE_FAST : BCMA_CLKMODE_DYNAMIC;
182 bcma_core_set_clockmode(cc->core, clkmode);
183 if (ticks > maxt)
184 ticks = maxt;
185 /* instant NMI */
186 bcma_cc_write32(cc, BCMA_CC_WATCHDOG, ticks);
187 }
Hauke Mehrtensa22a3112012-12-05 18:46:01 +0100188 return ticks;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200189}
190
191void bcma_chipco_irq_mask(struct bcma_drv_cc *cc, u32 mask, u32 value)
192{
193 bcma_cc_write32_masked(cc, BCMA_CC_IRQMASK, mask, value);
194}
195
196u32 bcma_chipco_irq_status(struct bcma_drv_cc *cc, u32 mask)
197{
198 return bcma_cc_read32(cc, BCMA_CC_IRQSTAT) & mask;
199}
200
201u32 bcma_chipco_gpio_in(struct bcma_drv_cc *cc, u32 mask)
202{
203 return bcma_cc_read32(cc, BCMA_CC_GPIOIN) & mask;
204}
205
206u32 bcma_chipco_gpio_out(struct bcma_drv_cc *cc, u32 mask, u32 value)
207{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000208 unsigned long flags;
209 u32 res;
210
211 spin_lock_irqsave(&cc->gpio_lock, flags);
212 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUT, mask, value);
213 spin_unlock_irqrestore(&cc->gpio_lock, flags);
214
215 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200216}
217
218u32 bcma_chipco_gpio_outen(struct bcma_drv_cc *cc, u32 mask, u32 value)
219{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000220 unsigned long flags;
221 u32 res;
222
223 spin_lock_irqsave(&cc->gpio_lock, flags);
224 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUTEN, mask, value);
225 spin_unlock_irqrestore(&cc->gpio_lock, flags);
226
227 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200228}
229
Hauke Mehrtens3e8bb502012-11-20 22:24:29 +0000230/*
231 * If the bit is set to 0, chipcommon controlls this GPIO,
232 * if the bit is set to 1, it is used by some part of the chip and not our code.
233 */
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200234u32 bcma_chipco_gpio_control(struct bcma_drv_cc *cc, u32 mask, u32 value)
235{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000236 unsigned long flags;
237 u32 res;
238
239 spin_lock_irqsave(&cc->gpio_lock, flags);
240 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOCTL, mask, value);
241 spin_unlock_irqrestore(&cc->gpio_lock, flags);
242
243 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200244}
245EXPORT_SYMBOL_GPL(bcma_chipco_gpio_control);
246
247u32 bcma_chipco_gpio_intmask(struct bcma_drv_cc *cc, u32 mask, u32 value)
248{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000249 unsigned long flags;
250 u32 res;
251
252 spin_lock_irqsave(&cc->gpio_lock, flags);
253 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOIRQ, mask, value);
254 spin_unlock_irqrestore(&cc->gpio_lock, flags);
255
256 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200257}
258
259u32 bcma_chipco_gpio_polarity(struct bcma_drv_cc *cc, u32 mask, u32 value)
260{
Hauke Mehrtensef85fb22012-11-20 22:24:27 +0000261 unsigned long flags;
262 u32 res;
263
264 spin_lock_irqsave(&cc->gpio_lock, flags);
265 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPOL, mask, value);
266 spin_unlock_irqrestore(&cc->gpio_lock, flags);
267
268 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200269}
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200270
Hauke Mehrtensea3488f2012-11-20 22:24:28 +0000271u32 bcma_chipco_gpio_pullup(struct bcma_drv_cc *cc, u32 mask, u32 value)
272{
273 unsigned long flags;
274 u32 res;
275
276 if (cc->core->id.rev < 20)
277 return 0;
278
279 spin_lock_irqsave(&cc->gpio_lock, flags);
280 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLUP, mask, value);
281 spin_unlock_irqrestore(&cc->gpio_lock, flags);
282
283 return res;
284}
285
286u32 bcma_chipco_gpio_pulldown(struct bcma_drv_cc *cc, u32 mask, u32 value)
287{
288 unsigned long flags;
289 u32 res;
290
291 if (cc->core->id.rev < 20)
292 return 0;
293
294 spin_lock_irqsave(&cc->gpio_lock, flags);
295 res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLDOWN, mask, value);
296 spin_unlock_irqrestore(&cc->gpio_lock, flags);
297
298 return res;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200299}
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200300
301#ifdef CONFIG_BCMA_DRIVER_MIPS
302void bcma_chipco_serial_init(struct bcma_drv_cc *cc)
303{
304 unsigned int irq;
305 u32 baud_base;
306 u32 i;
307 unsigned int ccrev = cc->core->id.rev;
308 struct bcma_serial_port *ports = cc->serial_ports;
309
310 if (ccrev >= 11 && ccrev != 15) {
Rafał Miłecki5b5ac412012-12-07 12:56:56 +0100311 baud_base = bcma_chipco_get_alp_clock(cc);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200312 if (ccrev >= 21) {
313 /* Turn off UART clock before switching clocksource. */
314 bcma_cc_write32(cc, BCMA_CC_CORECTL,
315 bcma_cc_read32(cc, BCMA_CC_CORECTL)
316 & ~BCMA_CC_CORECTL_UARTCLKEN);
317 }
318 /* Set the override bit so we don't divide it */
319 bcma_cc_write32(cc, BCMA_CC_CORECTL,
320 bcma_cc_read32(cc, BCMA_CC_CORECTL)
321 | BCMA_CC_CORECTL_UARTCLK0);
322 if (ccrev >= 21) {
323 /* Re-enable the UART clock. */
324 bcma_cc_write32(cc, BCMA_CC_CORECTL,
325 bcma_cc_read32(cc, BCMA_CC_CORECTL)
326 | BCMA_CC_CORECTL_UARTCLKEN);
327 }
328 } else {
Rafał Miłecki9a89c3a2012-07-09 19:34:59 +0200329 bcma_err(cc->core->bus, "serial not supported on this device ccrev: 0x%x\n", ccrev);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200330 return;
331 }
332
Nathan Hintze2aa19f2013-01-10 17:54:09 +0100333 irq = bcma_core_irq(cc->core);
Hauke Mehrtense3afe0e2011-07-23 01:20:10 +0200334
335 /* Determine the registers of the UARTs */
336 cc->nr_serial_ports = (cc->capabilities & BCMA_CC_CAP_NRUART);
337 for (i = 0; i < cc->nr_serial_ports; i++) {
338 ports[i].regs = cc->core->io_addr + BCMA_CC_UART0_DATA +
339 (i * 256);
340 ports[i].irq = irq;
341 ports[i].baud_base = baud_base;
342 ports[i].reg_shift = 0;
343 }
344}
345#endif /* CONFIG_BCMA_DRIVER_MIPS */