]> Pileus Git - ~andy/linux/blob - drivers/bcma/scan.c
Merge branch 'delete-mca' of git://git.kernel.org/pub/scm/linux/kernel/git/paulg...
[~andy/linux] / drivers / bcma / scan.c
1 /*
2  * Broadcom specific AMBA
3  * Bus scanning
4  *
5  * Licensed under the GNU/GPL. See COPYING for details.
6  */
7
8 #include "scan.h"
9 #include "bcma_private.h"
10
11 #include <linux/bcma/bcma.h>
12 #include <linux/bcma/bcma_regs.h>
13 #include <linux/pci.h>
14 #include <linux/io.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/slab.h>
17
18 struct bcma_device_id_name {
19         u16 id;
20         const char *name;
21 };
22 struct bcma_device_id_name bcma_device_names[] = {
23         { BCMA_CORE_OOB_ROUTER, "OOB Router" },
24         { BCMA_CORE_INVALID, "Invalid" },
25         { BCMA_CORE_CHIPCOMMON, "ChipCommon" },
26         { BCMA_CORE_ILINE20, "ILine 20" },
27         { BCMA_CORE_SRAM, "SRAM" },
28         { BCMA_CORE_SDRAM, "SDRAM" },
29         { BCMA_CORE_PCI, "PCI" },
30         { BCMA_CORE_MIPS, "MIPS" },
31         { BCMA_CORE_ETHERNET, "Fast Ethernet" },
32         { BCMA_CORE_V90, "V90" },
33         { BCMA_CORE_USB11_HOSTDEV, "USB 1.1 Hostdev" },
34         { BCMA_CORE_ADSL, "ADSL" },
35         { BCMA_CORE_ILINE100, "ILine 100" },
36         { BCMA_CORE_IPSEC, "IPSEC" },
37         { BCMA_CORE_UTOPIA, "UTOPIA" },
38         { BCMA_CORE_PCMCIA, "PCMCIA" },
39         { BCMA_CORE_INTERNAL_MEM, "Internal Memory" },
40         { BCMA_CORE_MEMC_SDRAM, "MEMC SDRAM" },
41         { BCMA_CORE_OFDM, "OFDM" },
42         { BCMA_CORE_EXTIF, "EXTIF" },
43         { BCMA_CORE_80211, "IEEE 802.11" },
44         { BCMA_CORE_PHY_A, "PHY A" },
45         { BCMA_CORE_PHY_B, "PHY B" },
46         { BCMA_CORE_PHY_G, "PHY G" },
47         { BCMA_CORE_MIPS_3302, "MIPS 3302" },
48         { BCMA_CORE_USB11_HOST, "USB 1.1 Host" },
49         { BCMA_CORE_USB11_DEV, "USB 1.1 Device" },
50         { BCMA_CORE_USB20_HOST, "USB 2.0 Host" },
51         { BCMA_CORE_USB20_DEV, "USB 2.0 Device" },
52         { BCMA_CORE_SDIO_HOST, "SDIO Host" },
53         { BCMA_CORE_ROBOSWITCH, "Roboswitch" },
54         { BCMA_CORE_PARA_ATA, "PATA" },
55         { BCMA_CORE_SATA_XORDMA, "SATA XOR-DMA" },
56         { BCMA_CORE_ETHERNET_GBIT, "GBit Ethernet" },
57         { BCMA_CORE_PCIE, "PCIe" },
58         { BCMA_CORE_PHY_N, "PHY N" },
59         { BCMA_CORE_SRAM_CTL, "SRAM Controller" },
60         { BCMA_CORE_MINI_MACPHY, "Mini MACPHY" },
61         { BCMA_CORE_ARM_1176, "ARM 1176" },
62         { BCMA_CORE_ARM_7TDMI, "ARM 7TDMI" },
63         { BCMA_CORE_PHY_LP, "PHY LP" },
64         { BCMA_CORE_PMU, "PMU" },
65         { BCMA_CORE_PHY_SSN, "PHY SSN" },
66         { BCMA_CORE_SDIO_DEV, "SDIO Device" },
67         { BCMA_CORE_ARM_CM3, "ARM CM3" },
68         { BCMA_CORE_PHY_HT, "PHY HT" },
69         { BCMA_CORE_MIPS_74K, "MIPS 74K" },
70         { BCMA_CORE_MAC_GBIT, "GBit MAC" },
71         { BCMA_CORE_DDR12_MEM_CTL, "DDR1/DDR2 Memory Controller" },
72         { BCMA_CORE_PCIE_RC, "PCIe Root Complex" },
73         { BCMA_CORE_OCP_OCP_BRIDGE, "OCP to OCP Bridge" },
74         { BCMA_CORE_SHARED_COMMON, "Common Shared" },
75         { BCMA_CORE_OCP_AHB_BRIDGE, "OCP to AHB Bridge" },
76         { BCMA_CORE_SPI_HOST, "SPI Host" },
77         { BCMA_CORE_I2S, "I2S" },
78         { BCMA_CORE_SDR_DDR1_MEM_CTL, "SDR/DDR1 Memory Controller" },
79         { BCMA_CORE_SHIM, "SHIM" },
80         { BCMA_CORE_DEFAULT, "Default" },
81 };
82 const char *bcma_device_name(struct bcma_device_id *id)
83 {
84         int i;
85
86         if (id->manuf == BCMA_MANUF_BCM) {
87                 for (i = 0; i < ARRAY_SIZE(bcma_device_names); i++) {
88                         if (bcma_device_names[i].id == id->id)
89                                 return bcma_device_names[i].name;
90                 }
91         }
92         return "UNKNOWN";
93 }
94
95 static u32 bcma_scan_read32(struct bcma_bus *bus, u8 current_coreidx,
96                        u16 offset)
97 {
98         return readl(bus->mmio + offset);
99 }
100
101 static void bcma_scan_switch_core(struct bcma_bus *bus, u32 addr)
102 {
103         if (bus->hosttype == BCMA_HOSTTYPE_PCI)
104                 pci_write_config_dword(bus->host_pci, BCMA_PCI_BAR0_WIN,
105                                        addr);
106 }
107
108 static u32 bcma_erom_get_ent(struct bcma_bus *bus, u32 **eromptr)
109 {
110         u32 ent = readl(*eromptr);
111         (*eromptr)++;
112         return ent;
113 }
114
115 static void bcma_erom_push_ent(u32 **eromptr)
116 {
117         (*eromptr)--;
118 }
119
120 static s32 bcma_erom_get_ci(struct bcma_bus *bus, u32 **eromptr)
121 {
122         u32 ent = bcma_erom_get_ent(bus, eromptr);
123         if (!(ent & SCAN_ER_VALID))
124                 return -ENOENT;
125         if ((ent & SCAN_ER_TAG) != SCAN_ER_TAG_CI)
126                 return -ENOENT;
127         return ent;
128 }
129
130 static bool bcma_erom_is_end(struct bcma_bus *bus, u32 **eromptr)
131 {
132         u32 ent = bcma_erom_get_ent(bus, eromptr);
133         bcma_erom_push_ent(eromptr);
134         return (ent == (SCAN_ER_TAG_END | SCAN_ER_VALID));
135 }
136
137 static bool bcma_erom_is_bridge(struct bcma_bus *bus, u32 **eromptr)
138 {
139         u32 ent = bcma_erom_get_ent(bus, eromptr);
140         bcma_erom_push_ent(eromptr);
141         return (((ent & SCAN_ER_VALID)) &&
142                 ((ent & SCAN_ER_TAGX) == SCAN_ER_TAG_ADDR) &&
143                 ((ent & SCAN_ADDR_TYPE) == SCAN_ADDR_TYPE_BRIDGE));
144 }
145
146 static void bcma_erom_skip_component(struct bcma_bus *bus, u32 **eromptr)
147 {
148         u32 ent;
149         while (1) {
150                 ent = bcma_erom_get_ent(bus, eromptr);
151                 if ((ent & SCAN_ER_VALID) &&
152                     ((ent & SCAN_ER_TAG) == SCAN_ER_TAG_CI))
153                         break;
154                 if (ent == (SCAN_ER_TAG_END | SCAN_ER_VALID))
155                         break;
156         }
157         bcma_erom_push_ent(eromptr);
158 }
159
160 static s32 bcma_erom_get_mst_port(struct bcma_bus *bus, u32 **eromptr)
161 {
162         u32 ent = bcma_erom_get_ent(bus, eromptr);
163         if (!(ent & SCAN_ER_VALID))
164                 return -ENOENT;
165         if ((ent & SCAN_ER_TAG) != SCAN_ER_TAG_MP)
166                 return -ENOENT;
167         return ent;
168 }
169
170 static s32 bcma_erom_get_addr_desc(struct bcma_bus *bus, u32 **eromptr,
171                                   u32 type, u8 port)
172 {
173         u32 addrl, addrh, sizel, sizeh = 0;
174         u32 size;
175
176         u32 ent = bcma_erom_get_ent(bus, eromptr);
177         if ((!(ent & SCAN_ER_VALID)) ||
178             ((ent & SCAN_ER_TAGX) != SCAN_ER_TAG_ADDR) ||
179             ((ent & SCAN_ADDR_TYPE) != type) ||
180             (((ent & SCAN_ADDR_PORT) >> SCAN_ADDR_PORT_SHIFT) != port)) {
181                 bcma_erom_push_ent(eromptr);
182                 return -EINVAL;
183         }
184
185         addrl = ent & SCAN_ADDR_ADDR;
186         if (ent & SCAN_ADDR_AG32)
187                 addrh = bcma_erom_get_ent(bus, eromptr);
188         else
189                 addrh = 0;
190
191         if ((ent & SCAN_ADDR_SZ) == SCAN_ADDR_SZ_SZD) {
192                 size = bcma_erom_get_ent(bus, eromptr);
193                 sizel = size & SCAN_SIZE_SZ;
194                 if (size & SCAN_SIZE_SG32)
195                         sizeh = bcma_erom_get_ent(bus, eromptr);
196         } else
197                 sizel = SCAN_ADDR_SZ_BASE <<
198                                 ((ent & SCAN_ADDR_SZ) >> SCAN_ADDR_SZ_SHIFT);
199
200         return addrl;
201 }
202
203 static struct bcma_device *bcma_find_core_by_index(struct bcma_bus *bus,
204                                                    u16 index)
205 {
206         struct bcma_device *core;
207
208         list_for_each_entry(core, &bus->cores, list) {
209                 if (core->core_index == index)
210                         return core;
211         }
212         return NULL;
213 }
214
215 static struct bcma_device *bcma_find_core_reverse(struct bcma_bus *bus, u16 coreid)
216 {
217         struct bcma_device *core;
218
219         list_for_each_entry_reverse(core, &bus->cores, list) {
220                 if (core->id.id == coreid)
221                         return core;
222         }
223         return NULL;
224 }
225
226 static int bcma_get_next_core(struct bcma_bus *bus, u32 __iomem **eromptr,
227                               struct bcma_device_id *match, int core_num,
228                               struct bcma_device *core)
229 {
230         s32 tmp;
231         u8 i, j;
232         s32 cia, cib;
233         u8 ports[2], wrappers[2];
234
235         /* get CIs */
236         cia = bcma_erom_get_ci(bus, eromptr);
237         if (cia < 0) {
238                 bcma_erom_push_ent(eromptr);
239                 if (bcma_erom_is_end(bus, eromptr))
240                         return -ESPIPE;
241                 return -EILSEQ;
242         }
243         cib = bcma_erom_get_ci(bus, eromptr);
244         if (cib < 0)
245                 return -EILSEQ;
246
247         /* parse CIs */
248         core->id.class = (cia & SCAN_CIA_CLASS) >> SCAN_CIA_CLASS_SHIFT;
249         core->id.id = (cia & SCAN_CIA_ID) >> SCAN_CIA_ID_SHIFT;
250         core->id.manuf = (cia & SCAN_CIA_MANUF) >> SCAN_CIA_MANUF_SHIFT;
251         ports[0] = (cib & SCAN_CIB_NMP) >> SCAN_CIB_NMP_SHIFT;
252         ports[1] = (cib & SCAN_CIB_NSP) >> SCAN_CIB_NSP_SHIFT;
253         wrappers[0] = (cib & SCAN_CIB_NMW) >> SCAN_CIB_NMW_SHIFT;
254         wrappers[1] = (cib & SCAN_CIB_NSW) >> SCAN_CIB_NSW_SHIFT;
255         core->id.rev = (cib & SCAN_CIB_REV) >> SCAN_CIB_REV_SHIFT;
256
257         if (((core->id.manuf == BCMA_MANUF_ARM) &&
258              (core->id.id == 0xFFF)) ||
259             (ports[1] == 0)) {
260                 bcma_erom_skip_component(bus, eromptr);
261                 return -ENXIO;
262         }
263
264         /* check if component is a core at all */
265         if (wrappers[0] + wrappers[1] == 0) {
266                 /* we could save addrl of the router
267                 if (cid == BCMA_CORE_OOB_ROUTER)
268                  */
269                 bcma_erom_skip_component(bus, eromptr);
270                 return -ENXIO;
271         }
272
273         if (bcma_erom_is_bridge(bus, eromptr)) {
274                 bcma_erom_skip_component(bus, eromptr);
275                 return -ENXIO;
276         }
277
278         if (bcma_find_core_by_index(bus, core_num)) {
279                 bcma_erom_skip_component(bus, eromptr);
280                 return -ENODEV;
281         }
282
283         if (match && ((match->manuf != BCMA_ANY_MANUF &&
284               match->manuf != core->id.manuf) ||
285              (match->id != BCMA_ANY_ID && match->id != core->id.id) ||
286              (match->rev != BCMA_ANY_REV && match->rev != core->id.rev) ||
287              (match->class != BCMA_ANY_CLASS && match->class != core->id.class)
288             )) {
289                 bcma_erom_skip_component(bus, eromptr);
290                 return -ENODEV;
291         }
292
293         /* get & parse master ports */
294         for (i = 0; i < ports[0]; i++) {
295                 s32 mst_port_d = bcma_erom_get_mst_port(bus, eromptr);
296                 if (mst_port_d < 0)
297                         return -EILSEQ;
298         }
299
300         /* First Slave Address Descriptor should be port 0:
301          * the main register space for the core
302          */
303         tmp = bcma_erom_get_addr_desc(bus, eromptr, SCAN_ADDR_TYPE_SLAVE, 0);
304         if (tmp <= 0) {
305                 /* Try again to see if it is a bridge */
306                 tmp = bcma_erom_get_addr_desc(bus, eromptr,
307                                               SCAN_ADDR_TYPE_BRIDGE, 0);
308                 if (tmp <= 0) {
309                         return -EILSEQ;
310                 } else {
311                         pr_info("Bridge found\n");
312                         return -ENXIO;
313                 }
314         }
315         core->addr = tmp;
316
317         /* get & parse slave ports */
318         for (i = 0; i < ports[1]; i++) {
319                 for (j = 0; ; j++) {
320                         tmp = bcma_erom_get_addr_desc(bus, eromptr,
321                                 SCAN_ADDR_TYPE_SLAVE, i);
322                         if (tmp < 0) {
323                                 /* no more entries for port _i_ */
324                                 /* pr_debug("erom: slave port %d "
325                                  * "has %d descriptors\n", i, j); */
326                                 break;
327                         } else {
328                                 if (i == 0 && j == 0)
329                                         core->addr1 = tmp;
330                         }
331                 }
332         }
333
334         /* get & parse master wrappers */
335         for (i = 0; i < wrappers[0]; i++) {
336                 for (j = 0; ; j++) {
337                         tmp = bcma_erom_get_addr_desc(bus, eromptr,
338                                 SCAN_ADDR_TYPE_MWRAP, i);
339                         if (tmp < 0) {
340                                 /* no more entries for port _i_ */
341                                 /* pr_debug("erom: master wrapper %d "
342                                  * "has %d descriptors\n", i, j); */
343                                 break;
344                         } else {
345                                 if (i == 0 && j == 0)
346                                         core->wrap = tmp;
347                         }
348                 }
349         }
350
351         /* get & parse slave wrappers */
352         for (i = 0; i < wrappers[1]; i++) {
353                 u8 hack = (ports[1] == 1) ? 0 : 1;
354                 for (j = 0; ; j++) {
355                         tmp = bcma_erom_get_addr_desc(bus, eromptr,
356                                 SCAN_ADDR_TYPE_SWRAP, i + hack);
357                         if (tmp < 0) {
358                                 /* no more entries for port _i_ */
359                                 /* pr_debug("erom: master wrapper %d "
360                                  * has %d descriptors\n", i, j); */
361                                 break;
362                         } else {
363                                 if (wrappers[0] == 0 && !i && !j)
364                                         core->wrap = tmp;
365                         }
366                 }
367         }
368         if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
369                 core->io_addr = ioremap_nocache(core->addr, BCMA_CORE_SIZE);
370                 if (!core->io_addr)
371                         return -ENOMEM;
372                 core->io_wrap = ioremap_nocache(core->wrap, BCMA_CORE_SIZE);
373                 if (!core->io_wrap) {
374                         iounmap(core->io_addr);
375                         return -ENOMEM;
376                 }
377         }
378         return 0;
379 }
380
381 void bcma_init_bus(struct bcma_bus *bus)
382 {
383         s32 tmp;
384         struct bcma_chipinfo *chipinfo = &(bus->chipinfo);
385
386         if (bus->init_done)
387                 return;
388
389         INIT_LIST_HEAD(&bus->cores);
390         bus->nr_cores = 0;
391
392         bcma_scan_switch_core(bus, BCMA_ADDR_BASE);
393
394         tmp = bcma_scan_read32(bus, 0, BCMA_CC_ID);
395         chipinfo->id = (tmp & BCMA_CC_ID_ID) >> BCMA_CC_ID_ID_SHIFT;
396         chipinfo->rev = (tmp & BCMA_CC_ID_REV) >> BCMA_CC_ID_REV_SHIFT;
397         chipinfo->pkg = (tmp & BCMA_CC_ID_PKG) >> BCMA_CC_ID_PKG_SHIFT;
398         pr_info("Found chip with id 0x%04X, rev 0x%02X and package 0x%02X\n",
399                 chipinfo->id, chipinfo->rev, chipinfo->pkg);
400
401         bus->init_done = true;
402 }
403
404 int bcma_bus_scan(struct bcma_bus *bus)
405 {
406         u32 erombase;
407         u32 __iomem *eromptr, *eromend;
408
409         int err, core_num = 0;
410
411         bcma_init_bus(bus);
412
413         erombase = bcma_scan_read32(bus, 0, BCMA_CC_EROM);
414         if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
415                 eromptr = ioremap_nocache(erombase, BCMA_CORE_SIZE);
416                 if (!eromptr)
417                         return -ENOMEM;
418         } else {
419                 eromptr = bus->mmio;
420         }
421
422         eromend = eromptr + BCMA_CORE_SIZE / sizeof(u32);
423
424         bcma_scan_switch_core(bus, erombase);
425
426         while (eromptr < eromend) {
427                 struct bcma_device *other_core;
428                 struct bcma_device *core = kzalloc(sizeof(*core), GFP_KERNEL);
429                 if (!core)
430                         return -ENOMEM;
431                 INIT_LIST_HEAD(&core->list);
432                 core->bus = bus;
433
434                 err = bcma_get_next_core(bus, &eromptr, NULL, core_num, core);
435                 if (err < 0) {
436                         kfree(core);
437                         if (err == -ENODEV) {
438                                 core_num++;
439                                 continue;
440                         } else if (err == -ENXIO) {
441                                 continue;
442                         } else if (err == -ESPIPE) {
443                                 break;
444                         }
445                         return err;
446                 }
447
448                 core->core_index = core_num++;
449                 bus->nr_cores++;
450                 other_core = bcma_find_core_reverse(bus, core->id.id);
451                 core->core_unit = (other_core == NULL) ? 0 : other_core->core_unit + 1;
452
453                 pr_info("Core %d found: %s "
454                         "(manuf 0x%03X, id 0x%03X, rev 0x%02X, class 0x%X)\n",
455                         core->core_index, bcma_device_name(&core->id),
456                         core->id.manuf, core->id.id, core->id.rev,
457                         core->id.class);
458
459                 list_add(&core->list, &bus->cores);
460         }
461
462         if (bus->hosttype == BCMA_HOSTTYPE_SOC)
463                 iounmap(eromptr);
464
465         return 0;
466 }
467
468 int __init bcma_bus_scan_early(struct bcma_bus *bus,
469                                struct bcma_device_id *match,
470                                struct bcma_device *core)
471 {
472         u32 erombase;
473         u32 __iomem *eromptr, *eromend;
474
475         int err = -ENODEV;
476         int core_num = 0;
477
478         erombase = bcma_scan_read32(bus, 0, BCMA_CC_EROM);
479         if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
480                 eromptr = ioremap_nocache(erombase, BCMA_CORE_SIZE);
481                 if (!eromptr)
482                         return -ENOMEM;
483         } else {
484                 eromptr = bus->mmio;
485         }
486
487         eromend = eromptr + BCMA_CORE_SIZE / sizeof(u32);
488
489         bcma_scan_switch_core(bus, erombase);
490
491         while (eromptr < eromend) {
492                 memset(core, 0, sizeof(*core));
493                 INIT_LIST_HEAD(&core->list);
494                 core->bus = bus;
495
496                 err = bcma_get_next_core(bus, &eromptr, match, core_num, core);
497                 if (err == -ENODEV) {
498                         core_num++;
499                         continue;
500                 } else if (err == -ENXIO)
501                         continue;
502                 else if (err == -ESPIPE)
503                         break;
504                 else if (err < 0)
505                         return err;
506
507                 core->core_index = core_num++;
508                 bus->nr_cores++;
509                 pr_info("Core %d found: %s "
510                         "(manuf 0x%03X, id 0x%03X, rev 0x%02X, class 0x%X)\n",
511                         core->core_index, bcma_device_name(&core->id),
512                         core->id.manuf, core->id.id, core->id.rev,
513                         core->id.class);
514
515                 list_add(&core->list, &bus->cores);
516                 err = 0;
517                 break;
518         }
519
520         if (bus->hosttype == BCMA_HOSTTYPE_SOC)
521                 iounmap(eromptr);
522
523         return err;
524 }