compile and register SPI controller device
[oweals/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_spi.h>
31 #include <board_bcm963xx.h>
32
33 #define PFX     "board_bcm963xx: "
34
35 static struct bcm963xx_nvram nvram;
36 static unsigned int mac_addr_used = 0;
37 static struct board_info board;
38
39 /*
40  * known 6338 boards
41  */
42
43 #ifdef CONFIG_BCM63XX_CPU_6338
44 static struct board_info __initdata board_96338gw = {
45         .name                           = "96338GW",
46         .expected_cpu_id                = 0x6338,
47
48         .has_enet0                      = 1,
49         .enet0 = {
50                 .has_phy                = 1,
51                 .use_internal_phy       = 1,
52         },
53
54         .has_ohci0                      = 1,
55 };
56 #endif
57
58 /*
59  * known 6348 boards
60  */
61 #ifdef CONFIG_BCM63XX_CPU_6348
62 static struct board_info __initdata board_96348r = {
63         .name                           = "96348R",
64         .expected_cpu_id                = 0x6348,
65
66         .has_enet0                      = 1,
67         .has_pci                        = 1,
68
69         .enet0 = {
70                 .has_phy                = 1,
71                 .use_internal_phy       = 1,
72         },
73 };
74
75 static struct board_info __initdata board_96348gw_10 = { 
76         .name                           = "96348GW-10",
77         .expected_cpu_id                = 0x6348,
78         
79         .has_enet0                      = 1,
80         .has_enet1                      = 1,
81         .has_pci                        = 1, 
82         
83         .enet0 = {
84                 .has_phy                = 1,
85                 .use_internal_phy       = 1,
86         },
87         .enet1 = {
88                 .force_speed_100        = 1,
89                 .force_duplex_full      = 1,
90         },
91         
92         .has_ohci0                      = 1,
93         .has_pccard                     = 1,
94         .has_ehci0                      = 1,
95 }; 
96
97 static struct board_info __initdata board_96348gw_11 = {
98         .name                           = "96348GW-11",
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
110         .enet1 = {
111                 .force_speed_100        = 1,
112                 .force_duplex_full      = 1,
113         },
114
115
116         .has_ohci0 = 1,
117         .has_pccard = 1,
118         .has_ehci0 = 1,
119 };
120
121 static struct board_info __initdata board_96348gw = {
122         .name                           = "96348GW",
123         .expected_cpu_id                = 0x6348,
124
125         .has_enet0                      = 1,
126         .has_enet1                      = 1,
127         .has_pci                        = 1,
128
129         .enet0 = {
130                 .has_phy                = 1,
131                 .use_internal_phy       = 1,
132         },
133         .enet1 = {
134                 .force_speed_100        = 1,
135                 .force_duplex_full      = 1,
136         },
137
138         .has_ohci0 = 1,
139 };
140
141 static struct board_info __initdata board_FAST2404 = {
142         .name                           = "F@ST2404",
143         .expected_cpu_id                = 0x6348,
144
145         .has_enet0                      = 1,
146         .has_enet1                      = 1,
147         .has_pci                        = 1,
148
149         .enet0 = {
150                 .has_phy                = 1,
151                 .use_internal_phy       = 1,
152         },
153
154         .enet1 = {
155                 .force_speed_100        = 1,
156                 .force_duplex_full      = 1,
157         },
158
159
160         .has_ohci0 = 1,
161         .has_pccard = 1,
162         .has_ehci0 = 1,
163 };
164
165 static struct board_info __initdata board_DV201AMR = {
166         .name                           = "DV201AMR",
167         .expected_cpu_id                = 0x6348,
168
169         .has_enet0                      = 1,
170         .has_enet1                      = 1,
171         .has_pci                        = 1,
172
173         .enet0 = {
174                 .has_phy                = 1,
175                 .use_internal_phy       = 1,
176         },
177
178         .enet1 = {
179                 .force_speed_100        = 1,
180                 .force_duplex_full      = 1,
181         },
182
183
184         .has_ohci0 = 1,
185         .has_pccard = 1,
186         .has_ehci0 = 1,
187 };
188
189 static struct board_info __initdata board_96348gw_a = {
190         .name                           = "96348GW-A",
191         .expected_cpu_id                = 0x6348,
192
193         .has_enet0                      = 1,
194         .has_enet1                      = 1,
195         .has_pci                        = 1,
196
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         .has_ohci0 = 1,
207 };
208
209
210 #endif
211
212 /*
213  * known 6358 boards
214  */
215 #ifdef CONFIG_BCM63XX_CPU_6358
216 static struct board_info __initdata board_96358vw = {
217         .name                           = "96358VW",
218         .expected_cpu_id                = 0x6358,
219
220         .has_enet0                      = 1,
221         .has_enet1                      = 1,
222         .has_pci                        = 1,
223
224         .enet0 = {
225                 .has_phy                = 1,
226                 .use_internal_phy       = 1,
227         },
228
229         .enet1 = {
230                 .force_speed_100        = 1,
231                 .force_duplex_full      = 1,
232         },
233
234
235         .has_ohci0 = 1,
236         .has_pccard = 1,
237         .has_ehci0 = 1,
238 };
239
240 static struct board_info __initdata board_96358vw2 = {
241         .name                           = "96358VW2",
242         .expected_cpu_id                = 0x6358,
243
244         .has_enet0                      = 1,
245         .has_enet1                      = 1,
246         .has_pci                        = 1,
247
248         .enet0 = {
249                 .has_phy                = 1,
250                 .use_internal_phy       = 1,
251         },
252
253         .enet1 = {
254                 .force_speed_100        = 1,
255                 .force_duplex_full      = 1,
256         },
257
258
259         .has_ohci0 = 1,
260         .has_pccard = 1,
261         .has_ehci0 = 1,
262 };
263 #endif
264
265 /*
266  * all boards
267  */
268 static const struct board_info __initdata *bcm963xx_boards[] = {
269 #ifdef CONFIG_BCM63XX_CPU_6338
270         &board_96338gw,
271 #endif
272 #ifdef CONFIG_BCM63XX_CPU_6348
273         &board_96348r,
274         &board_96348gw,
275         &board_96348gw_10,
276         &board_96348gw_11,
277         &board_FAST2404,
278         &board_DV201AMR,
279         &board_96348gw_a,
280 #endif
281
282 #ifdef CONFIG_BCM63XX_CPU_6358
283         &board_96358vw,
284         &board_96358vw2,
285 #endif
286 };
287
288 /*
289  * early init callback, read nvram data from flash and checksum it
290  */
291 void __init board_prom_init(void)
292 {
293         unsigned int check_len, i;
294         u8 *boot_addr, *cfe, *p;
295         char cfe_version[32];
296         u32 val;
297
298         /* read base address of boot chip select (0) */
299         val = bcm_mpi_readl(MPI_CSBASE_REG(0));
300         val &= MPI_CSBASE_BASE_MASK;
301         boot_addr = (u8 *)KSEG1ADDR(val);
302
303         /* dump cfe version */
304         cfe = boot_addr + BCM963XX_CFE_VERSION_OFFSET;
305         if (!memcmp(cfe, "cfe-v", 5))
306                 snprintf(cfe_version, sizeof(cfe_version), "%u.%u.%u-%u.%u",
307                          cfe[5], cfe[6], cfe[7], cfe[8], cfe[9]);
308         else
309                 strcpy(cfe_version, "unknown");
310         printk(KERN_INFO PFX "CFE version: %s\n", cfe_version);
311
312         /* extract nvram data */
313         memcpy(&nvram, boot_addr + BCM963XX_NVRAM_OFFSET, sizeof(nvram));
314
315         /* check checksum before using data */
316         if (nvram.version <= 4)
317                 check_len = offsetof(struct bcm963xx_nvram, checksum_old);
318         else
319                 check_len = sizeof(nvram);
320         val = 0;
321         p = (u8 *)&nvram;
322         while (check_len--)
323                 val += *p;
324         if (val) {
325                 printk(KERN_ERR PFX "invalid nvram checksum\n");
326                 return;
327         }
328
329         /* find board by name */
330         for (i = 0; i < ARRAY_SIZE(bcm963xx_boards); i++) {
331                 if (strncmp(nvram.name, bcm963xx_boards[i]->name,
332                             sizeof(nvram.name)))
333                         continue;
334                 /* copy, board desc array is marked initdata */
335                 memcpy(&board, bcm963xx_boards[i], sizeof(board));
336                 break;
337         }
338
339         /* bail out if board is not found, will complain later */
340         if (!board.name[0]) {
341                 char name[17];
342                 memcpy(name, nvram.name, 16);
343                 name[16] = 0;
344                 printk(KERN_ERR PFX "unknown bcm963xx board: %s\n",
345                        name);
346                 return;
347         }
348
349         /* setup pin multiplexing depending on board enabled device,
350          * this has to be done this early since PCI init is done
351          * inside arch_initcall */
352         val = 0;
353
354         if (board.has_pci) {
355                 bcm63xx_pci_enabled = 1;
356                 if (BCMCPU_IS_6348())
357                         val |= GPIO_MODE_6348_G2_PCI;
358         }
359
360         if (board.has_pccard) {
361                 if (BCMCPU_IS_6348())
362                         val |= GPIO_MODE_6348_G1_MII_PCCARD;
363         }
364
365         if (board.has_enet0 && !board.enet0.use_internal_phy) {
366                 if (BCMCPU_IS_6348())
367                         val |= GPIO_MODE_6348_G3_EXT_MII |
368                                 GPIO_MODE_6348_G0_EXT_MII;
369         }
370
371         if (board.has_enet1 && !board.enet1.use_internal_phy) {
372                 if (BCMCPU_IS_6348())
373                         val |= GPIO_MODE_6348_G3_EXT_MII |
374                                 GPIO_MODE_6348_G0_EXT_MII;
375         }
376
377         bcm_gpio_writel(val, GPIO_MODE_REG);
378 }
379
380 /*
381  * second stage init callback, good time to panic if we couldn't
382  * identify on which board we're running since early printk is working
383  */
384 void __init board_setup(void)
385 {
386         if (!board.name[0])
387                 panic("unable to detect bcm963xx board");
388         printk(KERN_INFO PFX "board name: %s\n", board.name);
389
390         /* make sure we're running on expected cpu */
391         if (bcm63xx_get_cpu_id() != board.expected_cpu_id)
392                 panic("unexpected CPU for bcm963xx board");
393 }
394
395 /*
396  * return board name for /proc/cpuinfo
397  */
398 const char *board_get_name(void)
399 {
400         return board.name;
401 }
402
403 /*
404  * register & return a new board mac address
405  */
406 static int board_get_mac_address(u8 *mac)
407 {
408         u8 *p;
409         int count;
410
411         if (mac_addr_used >= nvram.mac_addr_count) {
412                 printk(KERN_ERR PFX "not enough mac address\n");
413                 return -ENODEV;
414         }
415
416         memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
417         p = mac + ETH_ALEN - 1;
418         count = mac_addr_used;
419
420         while (count--) {
421                 do {
422                         (*p)++;
423                         if (*p != 0)
424                                 break;
425                         p--;
426                 } while (p != mac);
427         }
428
429         if (p == mac) {
430                 printk(KERN_ERR PFX "unable to fetch mac address\n");
431                 return -ENODEV;
432         }
433
434         mac_addr_used++;
435         return 0;
436 }
437
438 static struct resource mtd_resources[] = {
439         {
440                 .start          = 0,    /* filled at runtime */
441                 .end            = 0,    /* filled at runtime */
442                 .flags          = IORESOURCE_MEM,
443         }
444 };
445
446 static struct platform_device mtd_dev = {
447         .name                   = "bcm963xx-flash",
448         .resource               = mtd_resources,
449         .num_resources          = ARRAY_SIZE(mtd_resources),
450 };
451
452 /*
453  * Register a sane SPROMv2 to make the on-board
454  * bcm4318 WLAN work
455  */
456 static struct ssb_sprom bcm63xx_sprom = {
457         .revision               = 0x02,
458         .board_rev              = 0x17,
459         .country_code           = 0x0,
460         .ant_available_bg       = 0x3,
461         .pa0b0                  = 0x15ae,
462         .pa0b1                  = 0xfa85,
463         .pa0b2                  = 0xfe8d,
464         .pa1b0                  = 0xffff,
465         .pa1b1                  = 0xffff,
466         .pa1b2                  = 0xffff,
467         .gpio0                  = 0xff,
468         .gpio1                  = 0xff,
469         .gpio2                  = 0xff,
470         .gpio3                  = 0xff,
471         .maxpwr_bg              = 0x004c,
472         .itssi_bg               = 0x00,
473         .boardflags_lo          = 0x2848,
474         .boardflags_hi          = 0x0000,
475 };
476
477 /*
478  * third stage init callback, register all board devices.
479  */
480 int __init board_register_devices(void)
481 {
482         u32 val;
483
484         bcm63xx_uart_register();
485         bcm63xx_wdt_register();
486         bcm63xx_spi_register();
487
488         if (board.has_pccard)
489                 bcm63xx_pcmcia_register();
490
491         if (board.has_enet0 &&
492             !board_get_mac_address(board.enet0.mac_addr))
493                 bcm63xx_enet_register(0, &board.enet0);
494
495         if (board.has_enet1 &&
496             !board_get_mac_address(board.enet1.mac_addr))
497                 bcm63xx_enet_register(1, &board.enet1);
498
499         if (board.has_ohci0)
500                 bcm63xx_ohci_register();
501
502         if (board.has_ehci0)
503                 bcm63xx_ehci_register();
504
505         /* Generate MAC address for WLAN and
506          * register our SPROM */
507         if (!board_get_mac_address(bcm63xx_sprom.il0mac)) {
508                 memcpy(bcm63xx_sprom.et0mac, bcm63xx_sprom.il0mac, ETH_ALEN);
509                 memcpy(bcm63xx_sprom.et1mac, bcm63xx_sprom.il0mac, ETH_ALEN);
510                 if (ssb_arch_set_fallback_sprom(&bcm63xx_sprom) < 0)
511                         printk(KERN_ERR "failed to register fallback SPROM\n");
512         }
513
514         /* read base address of boot chip select (0) */
515         val = bcm_mpi_readl(MPI_CSBASE_REG(0));
516         val &= MPI_CSBASE_BASE_MASK;
517         mtd_resources[0].start = val;
518         mtd_resources[0].end = 0x1FFFFFFF;
519
520         platform_device_register(&mtd_dev);
521
522         return 0;
523 }
524