Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / drivers / bcma / driver_mips.c
1 /*
2  * Broadcom specific AMBA
3  * Broadcom MIPS32 74K core driver
4  *
5  * Copyright 2009, Broadcom Corporation
6  * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
7  * Copyright 2010, Bernhard Loos <bernhardloos@googlemail.com>
8  * Copyright 2011, Hauke Mehrtens <hauke@hauke-m.de>
9  *
10  * Licensed under the GNU/GPL. See COPYING for details.
11  */
12
13 #include "bcma_private.h"
14
15 #include <linux/bcma/bcma.h>
16
17 #include <linux/mtd/physmap.h>
18 #include <linux/platform_device.h>
19 #include <linux/serial.h>
20 #include <linux/serial_core.h>
21 #include <linux/serial_reg.h>
22 #include <linux/time.h>
23
24 enum bcma_boot_dev {
25         BCMA_BOOT_DEV_UNK = 0,
26         BCMA_BOOT_DEV_ROM,
27         BCMA_BOOT_DEV_PARALLEL,
28         BCMA_BOOT_DEV_SERIAL,
29         BCMA_BOOT_DEV_NAND,
30 };
31
32 static const char * const part_probes[] = { "bcm47xxpart", NULL };
33
34 static struct physmap_flash_data bcma_pflash_data = {
35         .part_probe_types       = part_probes,
36 };
37
38 static struct resource bcma_pflash_resource = {
39         .name   = "bcma_pflash",
40         .flags  = IORESOURCE_MEM,
41 };
42
43 struct platform_device bcma_pflash_dev = {
44         .name           = "physmap-flash",
45         .dev            = {
46                 .platform_data  = &bcma_pflash_data,
47         },
48         .resource       = &bcma_pflash_resource,
49         .num_resources  = 1,
50 };
51
52 /* The 47162a0 hangs when reading MIPS DMP registers registers */
53 static inline bool bcma_core_mips_bcm47162a0_quirk(struct bcma_device *dev)
54 {
55         return dev->bus->chipinfo.id == BCMA_CHIP_ID_BCM47162 &&
56                dev->bus->chipinfo.rev == 0 && dev->id.id == BCMA_CORE_MIPS_74K;
57 }
58
59 /* The 5357b0 hangs when reading USB20H DMP registers */
60 static inline bool bcma_core_mips_bcm5357b0_quirk(struct bcma_device *dev)
61 {
62         return (dev->bus->chipinfo.id == BCMA_CHIP_ID_BCM5357 ||
63                 dev->bus->chipinfo.id == BCMA_CHIP_ID_BCM4749) &&
64                dev->bus->chipinfo.pkg == 11 &&
65                dev->id.id == BCMA_CORE_USB20_HOST;
66 }
67
68 static inline u32 mips_read32(struct bcma_drv_mips *mcore,
69                               u16 offset)
70 {
71         return bcma_read32(mcore->core, offset);
72 }
73
74 static inline void mips_write32(struct bcma_drv_mips *mcore,
75                                 u16 offset,
76                                 u32 value)
77 {
78         bcma_write32(mcore->core, offset, value);
79 }
80
81 static const u32 ipsflag_irq_mask[] = {
82         0,
83         BCMA_MIPS_IPSFLAG_IRQ1,
84         BCMA_MIPS_IPSFLAG_IRQ2,
85         BCMA_MIPS_IPSFLAG_IRQ3,
86         BCMA_MIPS_IPSFLAG_IRQ4,
87 };
88
89 static const u32 ipsflag_irq_shift[] = {
90         0,
91         BCMA_MIPS_IPSFLAG_IRQ1_SHIFT,
92         BCMA_MIPS_IPSFLAG_IRQ2_SHIFT,
93         BCMA_MIPS_IPSFLAG_IRQ3_SHIFT,
94         BCMA_MIPS_IPSFLAG_IRQ4_SHIFT,
95 };
96
97 static u32 bcma_core_mips_irqflag(struct bcma_device *dev)
98 {
99         u32 flag;
100
101         if (bcma_core_mips_bcm47162a0_quirk(dev))
102                 return dev->core_index;
103         if (bcma_core_mips_bcm5357b0_quirk(dev))
104                 return dev->core_index;
105         flag = bcma_aread32(dev, BCMA_MIPS_OOBSELOUTA30);
106
107         if (flag)
108                 return flag & 0x1F;
109         else
110                 return 0x3f;
111 }
112
113 /* Get the MIPS IRQ assignment for a specified device.
114  * If unassigned, 0 is returned.
115  * If disabled, 5 is returned.
116  * If not supported, 6 is returned.
117  */
118 unsigned int bcma_core_mips_irq(struct bcma_device *dev)
119 {
120         struct bcma_device *mdev = dev->bus->drv_mips.core;
121         u32 irqflag;
122         unsigned int irq;
123
124         irqflag = bcma_core_mips_irqflag(dev);
125         if (irqflag == 0x3f)
126                 return 6;
127
128         for (irq = 0; irq <= 4; irq++)
129                 if (bcma_read32(mdev, BCMA_MIPS_MIPS74K_INTMASK(irq)) &
130                     (1 << irqflag))
131                         return irq;
132
133         return 5;
134 }
135
136 static void bcma_core_mips_set_irq(struct bcma_device *dev, unsigned int irq)
137 {
138         unsigned int oldirq = bcma_core_mips_irq(dev);
139         struct bcma_bus *bus = dev->bus;
140         struct bcma_device *mdev = bus->drv_mips.core;
141         u32 irqflag;
142
143         irqflag = bcma_core_mips_irqflag(dev);
144         BUG_ON(oldirq == 6);
145
146         dev->irq = irq + 2;
147
148         /* clear the old irq */
149         if (oldirq == 0)
150                 bcma_write32(mdev, BCMA_MIPS_MIPS74K_INTMASK(0),
151                             bcma_read32(mdev, BCMA_MIPS_MIPS74K_INTMASK(0)) &
152                             ~(1 << irqflag));
153         else if (oldirq != 5)
154                 bcma_write32(mdev, BCMA_MIPS_MIPS74K_INTMASK(oldirq), 0);
155
156         /* assign the new one */
157         if (irq == 0) {
158                 bcma_write32(mdev, BCMA_MIPS_MIPS74K_INTMASK(0),
159                             bcma_read32(mdev, BCMA_MIPS_MIPS74K_INTMASK(0)) |
160                             (1 << irqflag));
161         } else {
162                 u32 irqinitmask = bcma_read32(mdev,
163                                               BCMA_MIPS_MIPS74K_INTMASK(irq));
164                 if (irqinitmask) {
165                         struct bcma_device *core;
166
167                         /* backplane irq line is in use, find out who uses
168                          * it and set user to irq 0
169                          */
170                         list_for_each_entry(core, &bus->cores, list) {
171                                 if ((1 << bcma_core_mips_irqflag(core)) ==
172                                     irqinitmask) {
173                                         bcma_core_mips_set_irq(core, 0);
174                                         break;
175                                 }
176                         }
177                 }
178                 bcma_write32(mdev, BCMA_MIPS_MIPS74K_INTMASK(irq),
179                              1 << irqflag);
180         }
181
182         bcma_debug(bus, "set_irq: core 0x%04x, irq %d => %d\n",
183                    dev->id.id, oldirq <= 4 ? oldirq + 2 : 0, irq + 2);
184 }
185
186 static void bcma_core_mips_set_irq_name(struct bcma_bus *bus, unsigned int irq,
187                                         u16 coreid, u8 unit)
188 {
189         struct bcma_device *core;
190
191         core = bcma_find_core_unit(bus, coreid, unit);
192         if (!core) {
193                 bcma_warn(bus,
194                           "Can not find core (id: 0x%x, unit %i) for IRQ configuration.\n",
195                           coreid, unit);
196                 return;
197         }
198
199         bcma_core_mips_set_irq(core, irq);
200 }
201
202 static void bcma_core_mips_print_irq(struct bcma_device *dev, unsigned int irq)
203 {
204         int i;
205         static const char *irq_name[] = {"2(S)", "3", "4", "5", "6", "D", "I"};
206         printk(KERN_DEBUG KBUILD_MODNAME ": core 0x%04x, irq :", dev->id.id);
207         for (i = 0; i <= 6; i++)
208                 printk(" %s%s", irq_name[i], i == irq ? "*" : " ");
209         printk("\n");
210 }
211
212 static void bcma_core_mips_dump_irq(struct bcma_bus *bus)
213 {
214         struct bcma_device *core;
215
216         list_for_each_entry(core, &bus->cores, list) {
217                 bcma_core_mips_print_irq(core, bcma_core_mips_irq(core));
218         }
219 }
220
221 u32 bcma_cpu_clock(struct bcma_drv_mips *mcore)
222 {
223         struct bcma_bus *bus = mcore->core->bus;
224
225         if (bus->drv_cc.capabilities & BCMA_CC_CAP_PMU)
226                 return bcma_pmu_get_cpu_clock(&bus->drv_cc);
227
228         bcma_err(bus, "No PMU available, need this to get the cpu clock\n");
229         return 0;
230 }
231 EXPORT_SYMBOL(bcma_cpu_clock);
232
233 static enum bcma_boot_dev bcma_boot_dev(struct bcma_bus *bus)
234 {
235         struct bcma_drv_cc *cc = &bus->drv_cc;
236         u8 cc_rev = cc->core->id.rev;
237
238         if (cc_rev == 42) {
239                 struct bcma_device *core;
240
241                 core = bcma_find_core(bus, BCMA_CORE_NS_ROM);
242                 if (core) {
243                         switch (bcma_aread32(core, BCMA_IOST) &
244                                 BCMA_NS_ROM_IOST_BOOT_DEV_MASK) {
245                         case BCMA_NS_ROM_IOST_BOOT_DEV_NOR:
246                                 return BCMA_BOOT_DEV_SERIAL;
247                         case BCMA_NS_ROM_IOST_BOOT_DEV_NAND:
248                                 return BCMA_BOOT_DEV_NAND;
249                         case BCMA_NS_ROM_IOST_BOOT_DEV_ROM:
250                         default:
251                                 return BCMA_BOOT_DEV_ROM;
252                         }
253                 }
254         } else {
255                 if (cc_rev == 38) {
256                         if (cc->status & BCMA_CC_CHIPST_5357_NAND_BOOT)
257                                 return BCMA_BOOT_DEV_NAND;
258                         else if (cc->status & BIT(5))
259                                 return BCMA_BOOT_DEV_ROM;
260                 }
261
262                 if ((cc->capabilities & BCMA_CC_CAP_FLASHT) ==
263                     BCMA_CC_FLASHT_PARA)
264                         return BCMA_BOOT_DEV_PARALLEL;
265                 else
266                         return BCMA_BOOT_DEV_SERIAL;
267         }
268
269         return BCMA_BOOT_DEV_SERIAL;
270 }
271
272 static void bcma_core_mips_flash_detect(struct bcma_drv_mips *mcore)
273 {
274         struct bcma_bus *bus = mcore->core->bus;
275         struct bcma_drv_cc *cc = &bus->drv_cc;
276         struct bcma_pflash *pflash = &cc->pflash;
277         enum bcma_boot_dev boot_dev;
278
279         switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
280         case BCMA_CC_FLASHT_STSER:
281         case BCMA_CC_FLASHT_ATSER:
282                 bcma_debug(bus, "Found serial flash\n");
283                 bcma_sflash_init(cc);
284                 break;
285         case BCMA_CC_FLASHT_PARA:
286                 bcma_debug(bus, "Found parallel flash\n");
287                 pflash->present = true;
288                 pflash->window = BCMA_SOC_FLASH2;
289                 pflash->window_size = BCMA_SOC_FLASH2_SZ;
290
291                 if ((bcma_read32(cc->core, BCMA_CC_FLASH_CFG) &
292                      BCMA_CC_FLASH_CFG_DS) == 0)
293                         pflash->buswidth = 1;
294                 else
295                         pflash->buswidth = 2;
296
297                 bcma_pflash_data.width = pflash->buswidth;
298                 bcma_pflash_resource.start = pflash->window;
299                 bcma_pflash_resource.end = pflash->window + pflash->window_size;
300
301                 break;
302         default:
303                 bcma_err(bus, "Flash type not supported\n");
304         }
305
306         if (cc->core->id.rev == 38 ||
307             bus->chipinfo.id == BCMA_CHIP_ID_BCM4706) {
308                 if (cc->capabilities & BCMA_CC_CAP_NFLASH) {
309                         bcma_debug(bus, "Found NAND flash\n");
310                         bcma_nflash_init(cc);
311                 }
312         }
313
314         /* Determine flash type this SoC boots from */
315         boot_dev = bcma_boot_dev(bus);
316         switch (boot_dev) {
317         case BCMA_BOOT_DEV_PARALLEL:
318         case BCMA_BOOT_DEV_SERIAL:
319                 /* TODO: Init NVRAM using BCMA_SOC_FLASH2 window */
320                 break;
321         case BCMA_BOOT_DEV_NAND:
322                 /* TODO: Init NVRAM using BCMA_SOC_FLASH1 window */
323                 break;
324         default:
325                 break;
326         }
327 }
328
329 void bcma_core_mips_early_init(struct bcma_drv_mips *mcore)
330 {
331         struct bcma_bus *bus = mcore->core->bus;
332
333         if (mcore->early_setup_done)
334                 return;
335
336         bcma_chipco_serial_init(&bus->drv_cc);
337         bcma_core_mips_flash_detect(mcore);
338
339         mcore->early_setup_done = true;
340 }
341
342 static void bcma_fix_i2s_irqflag(struct bcma_bus *bus)
343 {
344         struct bcma_device *cpu, *pcie, *i2s;
345
346         /* Fixup the interrupts in 4716/4748 for i2s core (2010 Broadcom SDK)
347          * (IRQ flags > 7 are ignored when setting the interrupt masks)
348          */
349         if (bus->chipinfo.id != BCMA_CHIP_ID_BCM4716 &&
350             bus->chipinfo.id != BCMA_CHIP_ID_BCM4748)
351                 return;
352
353         cpu = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
354         pcie = bcma_find_core(bus, BCMA_CORE_PCIE);
355         i2s = bcma_find_core(bus, BCMA_CORE_I2S);
356         if (cpu && pcie && i2s &&
357             bcma_aread32(cpu, BCMA_MIPS_OOBSELINA74) == 0x08060504 &&
358             bcma_aread32(pcie, BCMA_MIPS_OOBSELINA74) == 0x08060504 &&
359             bcma_aread32(i2s, BCMA_MIPS_OOBSELOUTA30) == 0x88) {
360                 bcma_awrite32(cpu, BCMA_MIPS_OOBSELINA74, 0x07060504);
361                 bcma_awrite32(pcie, BCMA_MIPS_OOBSELINA74, 0x07060504);
362                 bcma_awrite32(i2s, BCMA_MIPS_OOBSELOUTA30, 0x87);
363                 bcma_debug(bus,
364                            "Moved i2s interrupt to oob line 7 instead of 8\n");
365         }
366 }
367
368 void bcma_core_mips_init(struct bcma_drv_mips *mcore)
369 {
370         struct bcma_bus *bus;
371         struct bcma_device *core;
372         bus = mcore->core->bus;
373
374         if (mcore->setup_done)
375                 return;
376
377         bcma_debug(bus, "Initializing MIPS core...\n");
378
379         bcma_core_mips_early_init(mcore);
380
381         bcma_fix_i2s_irqflag(bus);
382
383         switch (bus->chipinfo.id) {
384         case BCMA_CHIP_ID_BCM4716:
385         case BCMA_CHIP_ID_BCM4748:
386                 bcma_core_mips_set_irq_name(bus, 1, BCMA_CORE_80211, 0);
387                 bcma_core_mips_set_irq_name(bus, 2, BCMA_CORE_MAC_GBIT, 0);
388                 bcma_core_mips_set_irq_name(bus, 3, BCMA_CORE_USB20_HOST, 0);
389                 bcma_core_mips_set_irq_name(bus, 4, BCMA_CORE_PCIE, 0);
390                 bcma_core_mips_set_irq_name(bus, 0, BCMA_CORE_CHIPCOMMON, 0);
391                 bcma_core_mips_set_irq_name(bus, 0, BCMA_CORE_I2S, 0);
392                 break;
393         case BCMA_CHIP_ID_BCM5356:
394         case BCMA_CHIP_ID_BCM47162:
395         case BCMA_CHIP_ID_BCM53572:
396                 bcma_core_mips_set_irq_name(bus, 1, BCMA_CORE_80211, 0);
397                 bcma_core_mips_set_irq_name(bus, 2, BCMA_CORE_MAC_GBIT, 0);
398                 bcma_core_mips_set_irq_name(bus, 0, BCMA_CORE_CHIPCOMMON, 0);
399                 break;
400         case BCMA_CHIP_ID_BCM5357:
401         case BCMA_CHIP_ID_BCM4749:
402                 bcma_core_mips_set_irq_name(bus, 1, BCMA_CORE_80211, 0);
403                 bcma_core_mips_set_irq_name(bus, 2, BCMA_CORE_MAC_GBIT, 0);
404                 bcma_core_mips_set_irq_name(bus, 3, BCMA_CORE_USB20_HOST, 0);
405                 bcma_core_mips_set_irq_name(bus, 0, BCMA_CORE_CHIPCOMMON, 0);
406                 bcma_core_mips_set_irq_name(bus, 0, BCMA_CORE_I2S, 0);
407                 break;
408         case BCMA_CHIP_ID_BCM4706:
409                 bcma_core_mips_set_irq_name(bus, 1, BCMA_CORE_PCIE, 0);
410                 bcma_core_mips_set_irq_name(bus, 2, BCMA_CORE_4706_MAC_GBIT,
411                                             0);
412                 bcma_core_mips_set_irq_name(bus, 3, BCMA_CORE_PCIE, 1);
413                 bcma_core_mips_set_irq_name(bus, 4, BCMA_CORE_USB20_HOST, 0);
414                 bcma_core_mips_set_irq_name(bus, 0, BCMA_CORE_4706_CHIPCOMMON,
415                                             0);
416                 break;
417         default:
418                 list_for_each_entry(core, &bus->cores, list) {
419                         core->irq = bcma_core_irq(core, 0);
420                 }
421                 bcma_err(bus,
422                          "Unknown device (0x%x) found, can not configure IRQs\n",
423                          bus->chipinfo.id);
424         }
425         bcma_debug(bus, "IRQ reconfiguration done\n");
426         bcma_core_mips_dump_irq(bus);
427
428         mcore->setup_done = true;
429 }