d94686dc4c3f59f52fd0fb3c8b8d96f0c4a1597d
[openwrt/svn-archive/archive.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 .has_ohci0 = 1,
117 };
118
119 static struct board_info __initdata board_FAST2404 = {
120 .name = "F@ST2404",
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 #endif
144
145 /*
146 * known 6358 boards
147 */
148 #ifdef CONFIG_BCM63XX_CPU_6358
149 static struct board_info __initdata board_96358vw = {
150 .name = "96358VW",
151 .expected_cpu_id = 0x6358,
152
153 .has_enet0 = 1,
154 .has_enet1 = 1,
155 .has_pci = 1,
156
157 .enet0 = {
158 .has_phy = 1,
159 .use_internal_phy = 1,
160 },
161
162 .enet1 = {
163 .force_speed_100 = 1,
164 .force_duplex_full = 1,
165 },
166
167
168 .has_ohci0 = 1,
169 .has_pccard = 1,
170 .has_ehci0 = 1,
171 };
172
173 static struct board_info __initdata board_96358vw2 = {
174 .name = "96358VW2",
175 .expected_cpu_id = 0x6358,
176
177 .has_enet0 = 1,
178 .has_enet1 = 1,
179 .has_pci = 1,
180
181 .enet0 = {
182 .has_phy = 1,
183 .use_internal_phy = 1,
184 },
185
186 .enet1 = {
187 .force_speed_100 = 1,
188 .force_duplex_full = 1,
189 },
190
191
192 .has_ohci0 = 1,
193 .has_pccard = 1,
194 .has_ehci0 = 1,
195 };
196 #endif
197
198 /*
199 * all boards
200 */
201 static const struct board_info __initdata *bcm963xx_boards[] = {
202 #ifdef CONFIG_BCM63XX_CPU_6348
203 &board_96348r,
204 &board_96348gw,
205 &board_96348gw_10,
206 &board_96348gw_11,
207 &board_FAST2404,
208 #endif
209
210 #ifdef CONFIG_BCM63XX_CPU_6358
211 &board_96358vw,
212 &board_96358vw2,
213 #endif
214 };
215
216 /*
217 * early init callback, read nvram data from flash and checksum it
218 */
219 void __init board_prom_init(void)
220 {
221 unsigned int check_len, i;
222 u8 *boot_addr, *cfe, *p;
223 char cfe_version[32];
224 u32 val;
225
226 /* read base address of boot chip select (0) */
227 val = bcm_mpi_readl(MPI_CSBASE_REG(0));
228 val &= MPI_CSBASE_BASE_MASK;
229 boot_addr = (u8 *)KSEG1ADDR(val);
230
231 /* dump cfe version */
232 cfe = boot_addr + BCM963XX_CFE_VERSION_OFFSET;
233 if (!memcmp(cfe, "cfe-v", 5))
234 snprintf(cfe_version, sizeof(cfe_version), "%u.%u.%u-%u.%u",
235 cfe[5], cfe[6], cfe[7], cfe[8], cfe[9]);
236 else
237 strcpy(cfe_version, "unknown");
238 printk(KERN_INFO PFX "CFE version: %s\n", cfe_version);
239
240 /* extract nvram data */
241 memcpy(&nvram, boot_addr + BCM963XX_NVRAM_OFFSET, sizeof(nvram));
242
243 /* check checksum before using data */
244 if (nvram.version <= 4)
245 check_len = offsetof(struct bcm963xx_nvram, checksum_old);
246 else
247 check_len = sizeof(nvram);
248 val = 0;
249 p = (u8 *)&nvram;
250 while (check_len--)
251 val += *p;
252 if (val) {
253 printk(KERN_ERR PFX "invalid nvram checksum\n");
254 return;
255 }
256
257 /* find board by name */
258 for (i = 0; i < ARRAY_SIZE(bcm963xx_boards); i++) {
259 if (strncmp(nvram.name, bcm963xx_boards[i]->name,
260 sizeof(nvram.name)))
261 continue;
262 /* copy, board desc array is marked initdata */
263 memcpy(&board, bcm963xx_boards[i], sizeof(board));
264 break;
265 }
266
267 /* bail out if board is not found, will complain later */
268 if (!board.name[0]) {
269 char name[17];
270 memcpy(name, nvram.name, 16);
271 name[16] = 0;
272 printk(KERN_ERR PFX "unknown bcm963xx board: %s\n",
273 name);
274 return;
275 }
276
277 /* setup pin multiplexing depending on board enabled device,
278 * this has to be done this early since PCI init is done
279 * inside arch_initcall */
280 val = 0;
281
282 if (board.has_pci) {
283 bcm63xx_pci_enabled = 1;
284 if (BCMCPU_IS_6348())
285 val |= GPIO_MODE_6348_G2_PCI;
286 }
287
288 if (board.has_pccard) {
289 if (BCMCPU_IS_6348())
290 val |= GPIO_MODE_6348_G1_MII_PCCARD;
291 }
292
293 if (board.has_enet0 && !board.enet0.use_internal_phy) {
294 if (BCMCPU_IS_6348())
295 val |= GPIO_MODE_6348_G3_EXT_MII |
296 GPIO_MODE_6348_G0_EXT_MII;
297 }
298
299 if (board.has_enet1 && !board.enet1.use_internal_phy) {
300 if (BCMCPU_IS_6348())
301 val |= GPIO_MODE_6348_G3_EXT_MII |
302 GPIO_MODE_6348_G0_EXT_MII;
303 }
304
305 bcm_gpio_writel(val, GPIO_MODE_REG);
306 }
307
308 /*
309 * second stage init callback, good time to panic if we couldn't
310 * identify on which board we're running since early printk is working
311 */
312 void __init board_setup(void)
313 {
314 if (!board.name[0])
315 panic("unable to detect bcm963xx board");
316 printk(KERN_INFO PFX "board name: %s\n", board.name);
317
318 /* make sure we're running on expected cpu */
319 if (bcm63xx_get_cpu_id() != board.expected_cpu_id)
320 panic("unexpected CPU for bcm963xx board");
321 }
322
323 /*
324 * return board name for /proc/cpuinfo
325 */
326 const char *board_get_name(void)
327 {
328 return board.name;
329 }
330
331 /*
332 * register & return a new board mac address
333 */
334 static int board_get_mac_address(u8 *mac)
335 {
336 u8 *p;
337 int count;
338
339 if (mac_addr_used >= nvram.mac_addr_count) {
340 printk(KERN_ERR PFX "not enough mac address\n");
341 return -ENODEV;
342 }
343
344 memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
345 p = mac + ETH_ALEN - 1;
346 count = mac_addr_used;
347
348 while (count--) {
349 do {
350 (*p)++;
351 if (*p != 0)
352 break;
353 p--;
354 } while (p != mac);
355 }
356
357 if (p == mac) {
358 printk(KERN_ERR PFX "unable to fetch mac address\n");
359 return -ENODEV;
360 }
361
362 mac_addr_used++;
363 return 0;
364 }
365
366 static struct resource mtd_resources[] = {
367 {
368 .start = 0, /* filled at runtime */
369 .end = 0, /* filled at runtime */
370 .flags = IORESOURCE_MEM,
371 }
372 };
373
374 static struct platform_device mtd_dev = {
375 .name = "bcm963xx-flash",
376 .resource = mtd_resources,
377 .num_resources = ARRAY_SIZE(mtd_resources),
378 };
379
380 /*
381 * third stage init callback, register all board devices.
382 */
383 int __init board_register_devices(void)
384 {
385 u32 val;
386
387 bcm63xx_uart_register();
388
389 if (board.has_pccard)
390 bcm63xx_pcmcia_register();
391
392 if (board.has_enet0 &&
393 !board_get_mac_address(board.enet0.mac_addr))
394 bcm63xx_enet_register(0, &board.enet0);
395
396 if (board.has_enet1 &&
397 !board_get_mac_address(board.enet1.mac_addr))
398 bcm63xx_enet_register(1, &board.enet1);
399
400 if (board.has_ohci0)
401 bcm63xx_ohci_register();
402
403 if (board.has_ehci0)
404 bcm63xx_ehci_register();
405
406
407 /* read base address of boot chip select (0) */
408 val = bcm_mpi_readl(MPI_CSBASE_REG(0));
409 val &= MPI_CSBASE_BASE_MASK;
410 mtd_resources[0].start = val;
411 mtd_resources[0].end = 0x1FFFFFFF;
412
413 platform_device_register(&mtd_dev);
414
415 return 0;
416 }
417