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