Register watchdog driver
[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 <asm/addrspace.h>
17 #include <bcm63xx_board.h>
18 #include <bcm63xx_cpu.h>
19 #include <bcm63xx_regs.h>
20 #include <bcm63xx_io.h>
21 #include <bcm63xx_board.h>
22 #include <bcm63xx_dev_pci.h>
23 #include <bcm63xx_dev_uart.h>
24 #include <bcm63xx_dev_wdt.h>
25 #include <bcm63xx_dev_enet.h>
26 #include <bcm63xx_dev_pcmcia.h>
27 #include <bcm63xx_dev_usb_ohci.h>
28 #include <bcm63xx_dev_usb_ehci.h>
29 #include <board_bcm963xx.h>
30
31 #define PFX     "board_bcm963xx: "
32
33 static struct bcm963xx_nvram nvram;
34 static unsigned int mac_addr_used = 0;
35 static struct board_info board;
36
37 /*
38  * known 6348 boards
39  */
40 #ifdef CONFIG_BCM63XX_CPU_6348
41 static struct board_info __initdata board_96348r = {
42         .name                           = "96348R",
43         .expected_cpu_id                = 0x6348,
44
45         .has_enet0                      = 1,
46         .has_pci                        = 1,
47
48         .enet0 = {
49                 .has_phy                = 1,
50                 .use_internal_phy       = 1,
51         },
52 };
53
54 static struct board_info __initdata board_96348gw_10 = { 
55         .name                           = "96348GW-10",
56         .expected_cpu_id                = 0x6348,
57         
58         .has_enet0                      = 1,
59         .has_enet1                      = 1,
60         .has_pci                        = 1, 
61         
62         .enet0 = {
63                 .has_phy                = 1,
64                 .use_internal_phy       = 1,
65         },
66         .enet1 = {
67                 .force_speed_100        = 1,
68                 .force_duplex_full      = 1,
69         },
70         
71         .has_ohci0                      = 1,
72         .has_pccard                     = 1,
73         .has_ehci0                      = 1,
74 }; 
75
76 static struct board_info __initdata board_96348gw_11 = {
77         .name                           = "96348GW-11",
78         .expected_cpu_id                = 0x6348,
79
80         .has_enet0                      = 1,
81         .has_enet1                      = 1,
82         .has_pci                        = 1,
83
84         .enet0 = {
85                 .has_phy                = 1,
86                 .use_internal_phy       = 1,
87         },
88
89         .enet1 = {
90                 .force_speed_100        = 1,
91                 .force_duplex_full      = 1,
92         },
93
94
95         .has_ohci0 = 1,
96         .has_pccard = 1,
97         .has_ehci0 = 1,
98 };
99
100 static struct board_info __initdata board_96348gw = {
101         .name                           = "96348GW",
102         .expected_cpu_id                = 0x6348,
103
104         .has_enet0                      = 1,
105         .has_enet1                      = 1,
106         .has_pci                        = 1,
107
108         .enet0 = {
109                 .has_phy                = 1,
110                 .use_internal_phy       = 1,
111         },
112         .enet1 = {
113                 .force_speed_100        = 1,
114                 .force_duplex_full      = 1,
115         },
116
117         .has_ohci0 = 1,
118 };
119
120 static struct board_info __initdata board_FAST2404 = {
121         .name                           = "F@ST2404",
122         .expected_cpu_id                = 0x6348,
123
124         .has_enet0                      = 1,
125         .has_enet1                      = 1,
126         .has_pci                        = 1,
127
128         .enet0 = {
129                 .has_phy                = 1,
130                 .use_internal_phy       = 1,
131         },
132
133         .enet1 = {
134                 .force_speed_100        = 1,
135                 .force_duplex_full      = 1,
136         },
137
138
139         .has_ohci0 = 1,
140         .has_pccard = 1,
141         .has_ehci0 = 1,
142 };
143
144 #endif
145
146 /*
147  * known 6358 boards
148  */
149 #ifdef CONFIG_BCM63XX_CPU_6358
150 static struct board_info __initdata board_96358vw = {
151         .name                           = "96358VW",
152         .expected_cpu_id                = 0x6358,
153
154         .has_enet0                      = 1,
155         .has_enet1                      = 1,
156         .has_pci                        = 1,
157
158         .enet0 = {
159                 .has_phy                = 1,
160                 .use_internal_phy       = 1,
161         },
162
163         .enet1 = {
164                 .force_speed_100        = 1,
165                 .force_duplex_full      = 1,
166         },
167
168
169         .has_ohci0 = 1,
170         .has_pccard = 1,
171         .has_ehci0 = 1,
172 };
173
174 static struct board_info __initdata board_96358vw2 = {
175         .name                           = "96358VW2",
176         .expected_cpu_id                = 0x6358,
177
178         .has_enet0                      = 1,
179         .has_enet1                      = 1,
180         .has_pci                        = 1,
181
182         .enet0 = {
183                 .has_phy                = 1,
184                 .use_internal_phy       = 1,
185         },
186
187         .enet1 = {
188                 .force_speed_100        = 1,
189                 .force_duplex_full      = 1,
190         },
191
192
193         .has_ohci0 = 1,
194         .has_pccard = 1,
195         .has_ehci0 = 1,
196 };
197 #endif
198
199 /*
200  * all boards
201  */
202 static const struct board_info __initdata *bcm963xx_boards[] = {
203 #ifdef CONFIG_BCM63XX_CPU_6348
204         &board_96348r,
205         &board_96348gw,
206         &board_96348gw_10,
207         &board_96348gw_11,
208         &board_FAST2404,
209 #endif
210
211 #ifdef CONFIG_BCM63XX_CPU_6358
212         &board_96358vw,
213         &board_96358vw2,
214 #endif
215 };
216
217 /*
218  * early init callback, read nvram data from flash and checksum it
219  */
220 void __init board_prom_init(void)
221 {
222         unsigned int check_len, i;
223         u8 *boot_addr, *cfe, *p;
224         char cfe_version[32];
225         u32 val;
226
227         /* read base address of boot chip select (0) */
228         val = bcm_mpi_readl(MPI_CSBASE_REG(0));
229         val &= MPI_CSBASE_BASE_MASK;
230         boot_addr = (u8 *)KSEG1ADDR(val);
231
232         /* dump cfe version */
233         cfe = boot_addr + BCM963XX_CFE_VERSION_OFFSET;
234         if (!memcmp(cfe, "cfe-v", 5))
235                 snprintf(cfe_version, sizeof(cfe_version), "%u.%u.%u-%u.%u",
236                          cfe[5], cfe[6], cfe[7], cfe[8], cfe[9]);
237         else
238                 strcpy(cfe_version, "unknown");
239         printk(KERN_INFO PFX "CFE version: %s\n", cfe_version);
240
241         /* extract nvram data */
242         memcpy(&nvram, boot_addr + BCM963XX_NVRAM_OFFSET, sizeof(nvram));
243
244         /* check checksum before using data */
245         if (nvram.version <= 4)
246                 check_len = offsetof(struct bcm963xx_nvram, checksum_old);
247         else
248                 check_len = sizeof(nvram);
249         val = 0;
250         p = (u8 *)&nvram;
251         while (check_len--)
252                 val += *p;
253         if (val) {
254                 printk(KERN_ERR PFX "invalid nvram checksum\n");
255                 return;
256         }
257
258         /* find board by name */
259         for (i = 0; i < ARRAY_SIZE(bcm963xx_boards); i++) {
260                 if (strncmp(nvram.name, bcm963xx_boards[i]->name,
261                             sizeof(nvram.name)))
262                         continue;
263                 /* copy, board desc array is marked initdata */
264                 memcpy(&board, bcm963xx_boards[i], sizeof(board));
265                 break;
266         }
267
268         /* bail out if board is not found, will complain later */
269         if (!board.name[0]) {
270                 char name[17];
271                 memcpy(name, nvram.name, 16);
272                 name[16] = 0;
273                 printk(KERN_ERR PFX "unknown bcm963xx board: %s\n",
274                        name);
275                 return;
276         }
277
278         /* setup pin multiplexing depending on board enabled device,
279          * this has to be done this early since PCI init is done
280          * inside arch_initcall */
281         val = 0;
282
283         if (board.has_pci) {
284                 bcm63xx_pci_enabled = 1;
285                 if (BCMCPU_IS_6348())
286                         val |= GPIO_MODE_6348_G2_PCI;
287         }
288
289         if (board.has_pccard) {
290                 if (BCMCPU_IS_6348())
291                         val |= GPIO_MODE_6348_G1_MII_PCCARD;
292         }
293
294         if (board.has_enet0 && !board.enet0.use_internal_phy) {
295                 if (BCMCPU_IS_6348())
296                         val |= GPIO_MODE_6348_G3_EXT_MII |
297                                 GPIO_MODE_6348_G0_EXT_MII;
298         }
299
300         if (board.has_enet1 && !board.enet1.use_internal_phy) {
301                 if (BCMCPU_IS_6348())
302                         val |= GPIO_MODE_6348_G3_EXT_MII |
303                                 GPIO_MODE_6348_G0_EXT_MII;
304         }
305
306         bcm_gpio_writel(val, GPIO_MODE_REG);
307 }
308
309 /*
310  * second stage init callback, good time to panic if we couldn't
311  * identify on which board we're running since early printk is working
312  */
313 void __init board_setup(void)
314 {
315         if (!board.name[0])
316                 panic("unable to detect bcm963xx board");
317         printk(KERN_INFO PFX "board name: %s\n", board.name);
318
319         /* make sure we're running on expected cpu */
320         if (bcm63xx_get_cpu_id() != board.expected_cpu_id)
321                 panic("unexpected CPU for bcm963xx board");
322 }
323
324 /*
325  * return board name for /proc/cpuinfo
326  */
327 const char *board_get_name(void)
328 {
329         return board.name;
330 }
331
332 /*
333  * register & return a new board mac address
334  */
335 static int board_get_mac_address(u8 *mac)
336 {
337         u8 *p;
338         int count;
339
340         if (mac_addr_used >= nvram.mac_addr_count) {
341                 printk(KERN_ERR PFX "not enough mac address\n");
342                 return -ENODEV;
343         }
344
345         memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
346         p = mac + ETH_ALEN - 1;
347         count = mac_addr_used;
348
349         while (count--) {
350                 do {
351                         (*p)++;
352                         if (*p != 0)
353                                 break;
354                         p--;
355                 } while (p != mac);
356         }
357
358         if (p == mac) {
359                 printk(KERN_ERR PFX "unable to fetch mac address\n");
360                 return -ENODEV;
361         }
362
363         mac_addr_used++;
364         return 0;
365 }
366
367 static struct resource mtd_resources[] = {
368         {
369                 .start          = 0,    /* filled at runtime */
370                 .end            = 0,    /* filled at runtime */
371                 .flags          = IORESOURCE_MEM,
372         }
373 };
374
375 static struct platform_device mtd_dev = {
376         .name                   = "bcm963xx-flash",
377         .resource               = mtd_resources,
378         .num_resources          = ARRAY_SIZE(mtd_resources),
379 };
380
381 /*
382  * third stage init callback, register all board devices.
383  */
384 int __init board_register_devices(void)
385 {
386         u32 val;
387
388         bcm63xx_uart_register();
389         bcm63xx_wdt_register();
390
391         if (board.has_pccard)
392                 bcm63xx_pcmcia_register();
393
394         if (board.has_enet0 &&
395             !board_get_mac_address(board.enet0.mac_addr))
396                 bcm63xx_enet_register(0, &board.enet0);
397
398         if (board.has_enet1 &&
399             !board_get_mac_address(board.enet1.mac_addr))
400                 bcm63xx_enet_register(1, &board.enet1);
401
402         if (board.has_ohci0)
403                 bcm63xx_ohci_register();
404
405         if (board.has_ehci0)
406                 bcm63xx_ehci_register();
407
408
409         /* read base address of boot chip select (0) */
410         val = bcm_mpi_readl(MPI_CSBASE_REG(0));
411         val &= MPI_CSBASE_BASE_MASK;
412         mtd_resources[0].start = val;
413         mtd_resources[0].end = 0x1FFFFFFF;
414
415         platform_device_register(&mtd_dev);
416
417         return 0;
418 }
419