[brcm63xx] a couple of more fixes to get 6345 booting up to the console handover
[openwrt.git] / target / linux / brcm63xx / files / arch / mips / bcm63xx / boards / board_bcm963xx.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7  */
8
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/platform_device.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
15 #include <linux/mtd/physmap.h>
16 #include <linux/ssb/ssb.h>
17 #include <asm/addrspace.h>
18 #include <bcm63xx_board.h>
19 #include <bcm63xx_cpu.h>
20 #include <bcm63xx_regs.h>
21 #include <bcm63xx_io.h>
22 #include <bcm63xx_board.h>
23 #include <bcm63xx_dev_pci.h>
24 #include <bcm63xx_dev_uart.h>
25 #include <bcm63xx_dev_wdt.h>
26 #include <bcm63xx_dev_enet.h>
27 #include <bcm63xx_dev_pcmcia.h>
28 #include <bcm63xx_dev_usb_ohci.h>
29 #include <bcm63xx_dev_usb_ehci.h>
30 #include <bcm63xx_dev_usb_udc.h>
31 #include <bcm63xx_dev_spi.h>
32 #include <board_bcm963xx.h>
33
34 #define PFX     "board_bcm963xx: "
35
36 static struct bcm963xx_nvram nvram;
37 static unsigned int mac_addr_used = 0;
38 static struct board_info board;
39
40 /*
41  * known 6338 boards
42  */
43
44 #ifdef CONFIG_BCM63XX_CPU_6338
45 static struct board_info __initdata board_96338gw = {
46         .name                           = "96338GW",
47         .expected_cpu_id                = 0x6338,
48         
49         .has_enet0                      = 1,
50         .enet0 = {
51                 .force_speed_100        = 1,
52                 .force_duplex_full      = 1,
53         },
54
55         .has_ohci0                      = 1,
56 };
57
58 static struct board_info __initdata board_96338w = {
59         .name                           = "96338W",
60         .expected_cpu_id                = 0x6338,
61         
62         .has_enet0                      = 1,
63         .enet0 = {
64                 .force_speed_100        = 1,
65                 .force_duplex_full      = 1,
66         }
67 };
68 #endif
69
70 /*
71  * known 6345 boards
72  */
73 #ifdef CONFIG_BCM63XX_CPU_6345
74 static struct board_info __initdata board_96345gw2 = {
75         .name                           = "96345GW2",
76         .expected_cpu_id                = 0x6345,
77 };
78 #endif
79
80 /*
81  * known 6348 boards
82  */
83 #ifdef CONFIG_BCM63XX_CPU_6348
84 static struct board_info __initdata board_96348r = {
85         .name                           = "96348R",
86         .expected_cpu_id                = 0x6348,
87
88         .has_enet0                      = 1,
89         .has_pci                        = 1,
90
91         .enet0 = {
92                 .has_phy                = 1,
93                 .use_internal_phy       = 1,
94         },
95 };
96
97 static struct board_info __initdata board_96348gw_10 = { 
98         .name                           = "96348GW-10",
99         .expected_cpu_id                = 0x6348,
100         
101         .has_enet0                      = 1,
102         .has_enet1                      = 1,
103         .has_pci                        = 1, 
104         
105         .enet0 = {
106                 .has_phy                = 1,
107                 .use_internal_phy       = 1,
108         },
109         .enet1 = {
110                 .force_speed_100        = 1,
111                 .force_duplex_full      = 1,
112         },
113         
114         .has_ohci0                      = 1,
115         .has_pccard                     = 1,
116         .has_ehci0                      = 1,
117 }; 
118
119 static struct board_info __initdata board_96348gw_11 = {
120         .name                           = "96348GW-11",
121         .expected_cpu_id                = 0x6348,
122
123         .has_enet0                      = 1,
124         .has_enet1                      = 1,
125         .has_pci                        = 1,
126
127         .enet0 = {
128                 .has_phy                = 1,
129                 .use_internal_phy       = 1,
130         },
131
132         .enet1 = {
133                 .force_speed_100        = 1,
134                 .force_duplex_full      = 1,
135         },
136
137
138         .has_ohci0 = 1,
139         .has_pccard = 1,
140         .has_ehci0 = 1,
141 };
142
143 static struct board_info __initdata board_96348gw = {
144         .name                           = "96348GW",
145         .expected_cpu_id                = 0x6348,
146
147         .has_enet0                      = 1,
148         .has_enet1                      = 1,
149         .has_pci                        = 1,
150
151         .enet0 = {
152                 .has_phy                = 1,
153                 .use_internal_phy       = 1,
154         },
155         .enet1 = {
156                 .force_speed_100        = 1,
157                 .force_duplex_full      = 1,
158         },
159
160         .has_ohci0 = 1,
161 };
162
163 static struct board_info __initdata board_FAST2404 = {
164         .name                           = "F@ST2404",
165         .expected_cpu_id                = 0x6348,
166
167         .has_enet0                      = 1,
168         .has_enet1                      = 1,
169         .has_pci                        = 1,
170
171         .enet0 = {
172                 .has_phy                = 1,
173                 .use_internal_phy       = 1,
174         },
175
176         .enet1 = {
177                 .force_speed_100        = 1,
178                 .force_duplex_full      = 1,
179         },
180
181
182         .has_ohci0 = 1,
183         .has_pccard = 1,
184         .has_ehci0 = 1,
185 };
186
187 static struct board_info __initdata board_DV201AMR = {
188         .name                           = "DV201AMR",
189         .expected_cpu_id                = 0x6348,
190
191         .has_pci                        = 1,
192         .has_ohci0                      = 1,
193         .has_udc0                       = 1,
194
195         .has_enet0                      = 1,
196         .has_enet1                      = 1,
197         .enet0 = {
198                 .has_phy                = 1,
199                 .use_internal_phy       = 1,
200         },
201         .enet1 = {
202                 .force_speed_100        = 1,
203                 .force_duplex_full      = 1,
204         },
205 };
206
207 static struct board_info __initdata board_96348gw_a = {
208         .name                           = "96348GW-A",
209         .expected_cpu_id                = 0x6348,
210
211         .has_enet0                      = 1,
212         .has_enet1                      = 1,
213         .has_pci                        = 1,
214
215         .enet0 = {
216                 .has_phy                = 1,
217                 .use_internal_phy       = 1,
218         },
219         .enet1 = {
220                 .force_speed_100        = 1,
221                 .force_duplex_full      = 1,
222         },
223
224         .has_ohci0 = 1,
225 };
226
227
228 #endif
229
230 /*
231  * known 6358 boards
232  */
233 #ifdef CONFIG_BCM63XX_CPU_6358
234 static struct board_info __initdata board_96358vw = {
235         .name                           = "96358VW",
236         .expected_cpu_id                = 0x6358,
237
238         .has_enet0                      = 1,
239         .has_enet1                      = 1,
240         .has_pci                        = 1,
241
242         .enet0 = {
243                 .has_phy                = 1,
244                 .use_internal_phy       = 1,
245         },
246
247         .enet1 = {
248                 .force_speed_100        = 1,
249                 .force_duplex_full      = 1,
250         },
251
252
253         .has_ohci0 = 1,
254         .has_pccard = 1,
255         .has_ehci0 = 1,
256 };
257
258 static struct board_info __initdata board_96358vw2 = {
259         .name                           = "96358VW2",
260         .expected_cpu_id                = 0x6358,
261
262         .has_enet0                      = 1,
263         .has_enet1                      = 1,
264         .has_pci                        = 1,
265
266         .enet0 = {
267                 .has_phy                = 1,
268                 .use_internal_phy       = 1,
269         },
270
271         .enet1 = {
272                 .force_speed_100        = 1,
273                 .force_duplex_full      = 1,
274         },
275
276
277         .has_ohci0 = 1,
278         .has_pccard = 1,
279         .has_ehci0 = 1,
280 };
281
282 static struct board_info __initdata board_AGPFS0 = {
283         .name                           = "AGPF-S0",
284         .expected_cpu_id                = 0x6358,
285
286         .has_enet0                      = 1,
287         .has_enet1                      = 1,
288         .has_pci                        = 1,
289
290         .enet0 = {
291                 .has_phy                = 1,
292                 .use_internal_phy       = 1,
293         },
294
295         .enet1 = {
296                 .force_speed_100        = 1,
297                 .force_duplex_full      = 1,
298         },
299
300         .has_ohci0 = 1,
301         .has_ehci0 = 1,
302 };
303 #endif
304
305 /*
306  * all boards
307  */
308 static const struct board_info __initdata *bcm963xx_boards[] = {
309 #ifdef CONFIG_BCM63XX_CPU_6338
310         &board_96338gw,
311         &board_96338w,
312 #endif
313 #ifdef CONFIG_BCM63XX_CPU_6345
314         &board_96345gw2,
315 #endif
316 #ifdef CONFIG_BCM63XX_CPU_6348
317         &board_96348r,
318         &board_96348gw,
319         &board_96348gw_10,
320         &board_96348gw_11,
321         &board_FAST2404,
322         &board_DV201AMR,
323         &board_96348gw_a,
324 #endif
325
326 #ifdef CONFIG_BCM63XX_CPU_6358
327         &board_96358vw,
328         &board_96358vw2,
329         &board_AGPFS0,
330 #endif
331 };
332
333 /*
334  * early init callback, read nvram data from flash and checksum it
335  */
336 void __init board_prom_init(void)
337 {
338         unsigned int check_len, i;
339         u8 *boot_addr, *cfe, *p;
340         char cfe_version[32];
341         u32 val;
342
343         /* read base address of boot chip select (0) 
344          * 6338/6345 does not have MPI but boots from standard
345          * MIPS Flash address */
346         if (BCMCPU_IS_6345())
347                 val = 0x1fc00000;
348         else {
349                 val = bcm_mpi_readl(MPI_CSBASE_REG(0));
350                 val &= MPI_CSBASE_BASE_MASK;
351         }
352         boot_addr = (u8 *)KSEG1ADDR(val);
353
354         /* dump cfe version */
355         cfe = boot_addr + BCM963XX_CFE_VERSION_OFFSET;
356         if (!memcmp(cfe, "cfe-v", 5))
357                 snprintf(cfe_version, sizeof(cfe_version), "%u.%u.%u-%u.%u",
358                          cfe[5], cfe[6], cfe[7], cfe[8], cfe[9]);
359         else
360                 strcpy(cfe_version, "unknown");
361         printk(KERN_INFO PFX "CFE version: %s\n", cfe_version);
362
363         /* extract nvram data */
364         memcpy(&nvram, boot_addr + BCM963XX_NVRAM_OFFSET, sizeof(nvram));
365
366         /* check checksum before using data */
367         if (nvram.version <= 4)
368                 check_len = offsetof(struct bcm963xx_nvram, checksum_old);
369         else
370                 check_len = sizeof(nvram);
371         val = 0;
372         p = (u8 *)&nvram;
373         while (check_len--)
374                 val += *p;
375         if (val) {
376                 printk(KERN_ERR PFX "invalid nvram checksum\n");
377                 return;
378         }
379
380         /* find board by name */
381         for (i = 0; i < ARRAY_SIZE(bcm963xx_boards); i++) {
382                 if (strncmp(nvram.name, bcm963xx_boards[i]->name,
383                             sizeof(nvram.name)))
384                         continue;
385                 /* copy, board desc array is marked initdata */
386                 memcpy(&board, bcm963xx_boards[i], sizeof(board));
387                 break;
388         }
389
390         /* bail out if board is not found, will complain later */
391         if (!board.name[0]) {
392                 char name[17];
393                 memcpy(name, nvram.name, 16);
394                 name[16] = 0;
395                 printk(KERN_ERR PFX "unknown bcm963xx board: %s\n",
396                        name);
397                 return;
398         }
399
400         /* setup pin multiplexing depending on board enabled device,
401          * this has to be done this early since PCI init is done
402          * inside arch_initcall */
403         val = 0;
404 #ifdef CONFIG_PCI
405         if (board.has_pci) {
406                 bcm63xx_pci_enabled = 1;
407                 if (BCMCPU_IS_6348())
408                         val |= GPIO_MODE_6348_G2_PCI;
409         }
410 #endif
411         if (board.has_pccard) {
412                 if (BCMCPU_IS_6348())
413                         val |= GPIO_MODE_6348_G1_MII_PCCARD;
414         }
415
416         if (board.has_enet0 && !board.enet0.use_internal_phy) {
417                 if (BCMCPU_IS_6348())
418                         val |= GPIO_MODE_6348_G3_EXT_MII |
419                                 GPIO_MODE_6348_G0_EXT_MII;
420         }
421
422         if (board.has_enet1 && !board.enet1.use_internal_phy) {
423                 if (BCMCPU_IS_6348())
424                         val |= GPIO_MODE_6348_G3_EXT_MII |
425                                 GPIO_MODE_6348_G0_EXT_MII;
426         }
427
428         bcm_gpio_writel(val, GPIO_MODE_REG);
429 }
430
431 /*
432  * second stage init callback, good time to panic if we couldn't
433  * identify on which board we're running since early printk is working
434  */
435 void __init board_setup(void)
436 {
437         if (!board.name[0])
438                 panic("unable to detect bcm963xx board");
439         printk(KERN_INFO PFX "board name: %s\n", board.name);
440
441         /* make sure we're running on expected cpu */
442         if (bcm63xx_get_cpu_id() != board.expected_cpu_id)
443                 panic("unexpected CPU for bcm963xx board");
444 }
445
446 /*
447  * return board name for /proc/cpuinfo
448  */
449 const char *board_get_name(void)
450 {
451         return board.name;
452 }
453
454 /*
455  * register & return a new board mac address
456  */
457 static int board_get_mac_address(u8 *mac)
458 {
459         u8 *p;
460         int count;
461
462         if (mac_addr_used >= nvram.mac_addr_count) {
463                 printk(KERN_ERR PFX "not enough mac address\n");
464                 return -ENODEV;
465         }
466
467         memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
468         p = mac + ETH_ALEN - 1;
469         count = mac_addr_used;
470
471         while (count--) {
472                 do {
473                         (*p)++;
474                         if (*p != 0)
475                                 break;
476                         p--;
477                 } while (p != mac);
478         }
479
480         if (p == mac) {
481                 printk(KERN_ERR PFX "unable to fetch mac address\n");
482                 return -ENODEV;
483         }
484
485         mac_addr_used++;
486         return 0;
487 }
488
489 static struct resource mtd_resources[] = {
490         {
491                 .start          = 0,    /* filled at runtime */
492                 .end            = 0,    /* filled at runtime */
493                 .flags          = IORESOURCE_MEM,
494         }
495 };
496
497 static struct platform_device mtd_dev = {
498         .name                   = "bcm963xx-flash",
499         .resource               = mtd_resources,
500         .num_resources          = ARRAY_SIZE(mtd_resources),
501 };
502
503 /*
504  * Register a sane SPROMv2 to make the on-board
505  * bcm4318 WLAN work
506  */
507 static struct ssb_sprom bcm63xx_sprom = {
508         .revision               = 0x02,
509         .board_rev              = 0x17,
510         .country_code           = 0x0,
511         .ant_available_bg       = 0x3,
512         .pa0b0                  = 0x15ae,
513         .pa0b1                  = 0xfa85,
514         .pa0b2                  = 0xfe8d,
515         .pa1b0                  = 0xffff,
516         .pa1b1                  = 0xffff,
517         .pa1b2                  = 0xffff,
518         .gpio0                  = 0xff,
519         .gpio1                  = 0xff,
520         .gpio2                  = 0xff,
521         .gpio3                  = 0xff,
522         .maxpwr_bg              = 0x004c,
523         .itssi_bg               = 0x00,
524         .boardflags_lo          = 0x2848,
525         .boardflags_hi          = 0x0000,
526 };
527
528 static struct resource gpiodev_resource = {
529         .start                  = 0xFFFFFFFF,
530 };
531
532 /*
533  * third stage init callback, register all board devices.
534  */
535 int __init board_register_devices(void)
536 {
537         u32 val;
538
539         bcm63xx_uart_register();
540         bcm63xx_wdt_register();
541         bcm63xx_spi_register();
542
543         if (board.has_pccard)
544                 bcm63xx_pcmcia_register();
545
546         if (board.has_enet0 &&
547             !board_get_mac_address(board.enet0.mac_addr))
548                 bcm63xx_enet_register(0, &board.enet0);
549
550         if (board.has_enet1 &&
551             !board_get_mac_address(board.enet1.mac_addr))
552                 bcm63xx_enet_register(1, &board.enet1);
553
554         if (board.has_ohci0)
555                 bcm63xx_ohci_register();
556
557         if (board.has_ehci0)
558                 bcm63xx_ehci_register();
559
560         if (board.has_udc0)
561                 bcm63xx_udc_register();
562         /* Generate MAC address for WLAN and
563          * register our SPROM */
564 #ifdef CONFIG_PCI
565         if (!board_get_mac_address(bcm63xx_sprom.il0mac)) {
566                 memcpy(bcm63xx_sprom.et0mac, bcm63xx_sprom.il0mac, ETH_ALEN);
567                 memcpy(bcm63xx_sprom.et1mac, bcm63xx_sprom.il0mac, ETH_ALEN);
568                 if (ssb_arch_set_fallback_sprom(&bcm63xx_sprom) < 0)
569                         printk(KERN_ERR "failed to register fallback SPROM\n");
570         }
571 #endif
572
573         /* read base address of boot chip select (0) */
574         if (BCMCPU_IS_6345())
575                 val = 0x1fc0000;
576         else {
577                 val = bcm_mpi_readl(MPI_CSBASE_REG(0));
578                 val &= MPI_CSBASE_BASE_MASK;
579         }
580         mtd_resources[0].start = val;
581         mtd_resources[0].end = 0x1FFFFFFF;
582
583         platform_device_register(&mtd_dev);
584
585         /* Register GPIODEV */
586         platform_device_register_simple("GPIODEV", 0, &gpiodev_resource, 1);
587
588         return 0;
589 }
590