add generic led support to atheros target
[librecmc/librecmc.git] / target / linux / atheros / files / arch / mips / atheros / ar5312 / board.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) 2003 Atheros Communications, Inc.,  All Rights Reserved.
7  * Copyright (C) 2006 FON Technology, SL.
8  * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
9  * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
10  */
11
12 /*
13  * Platform devices for Atheros SoCs
14  */
15
16 #include <linux/autoconf.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/mtd/physmap.h>
22 #include <linux/platform_device.h>
23 #include <linux/kernel.h>
24 #include <linux/reboot.h>
25 #include <asm/bootinfo.h>
26 #include <asm/reboot.h>
27 #include <asm/time.h>
28 #include <asm/irq.h>
29 #include <asm/io.h>
30 #include <ar531x.h>
31 #include <linux/leds.h>
32
33 #define NO_PHY 0x1f
34
35 static int is_5312 = 0;
36 static struct platform_device *ar5312_devs[6];
37
38 static struct resource ar5312_eth0_res[] = {
39         {
40                 .name = "eth0_membase",
41                 .flags = IORESOURCE_MEM,
42                 .start = KSEG1ADDR(AR531X_ENET0),
43                 .end = KSEG1ADDR(AR531X_ENET0 + 0x2000),
44         },
45         {
46                 .name = "eth0_irq",
47                 .flags = IORESOURCE_IRQ,
48                 .start = AR5312_IRQ_ENET0_INTRS,
49                 .end = AR5312_IRQ_ENET0_INTRS,
50         },
51 };
52 static struct ar531x_eth ar5312_eth0_data = {
53         .phy = NO_PHY,
54         .mac = 0,
55         .reset_base = AR531X_RESET,
56         .reset_mac = AR531X_RESET_ENET0,
57         .reset_phy = AR531X_RESET_EPHY0,
58         .phy_base = KSEG1ADDR(AR531X_ENET0),
59 };
60
61 static struct resource ar5312_eth1_res[] = {
62         {
63                 .name = "eth1_membase",
64                 .flags = IORESOURCE_MEM,
65                 .start = KSEG1ADDR(AR531X_ENET1),
66                 .end = KSEG1ADDR(AR531X_ENET1 + 0x2000),
67         },
68         {
69                 .name = "eth1_irq",
70                 .flags = IORESOURCE_IRQ,
71                 .start = AR5312_IRQ_ENET1_INTRS,
72                 .end = AR5312_IRQ_ENET1_INTRS,
73         },
74 };
75 static struct ar531x_eth ar5312_eth1_data = {
76         .phy = NO_PHY,
77         .mac = 1,
78         .reset_base = AR531X_RESET,
79         .reset_mac = AR531X_RESET_ENET1,
80         .reset_phy = AR531X_RESET_EPHY1,
81         .phy_base = KSEG1ADDR(AR531X_ENET1),
82 };
83
84 static struct platform_device ar5312_eth[] = {
85         {
86                 .id = 0,
87                 .name = "ar531x-eth",
88                 .dev.platform_data = &ar5312_eth0_data,
89                 .resource = ar5312_eth0_res,
90                 .num_resources = ARRAY_SIZE(ar5312_eth0_res)
91         },
92         {
93                 .id = 1,
94                 .name = "ar531x-eth",
95                 .dev.platform_data = &ar5312_eth1_data,
96                 .resource = ar5312_eth1_res,
97                 .num_resources = ARRAY_SIZE(ar5312_eth1_res)
98         },
99 };
100
101
102 /*
103  * AR2312/3 ethernet uses the PHY of ENET0, but the MAC
104  * of ENET1. Atheros calls it 'twisted' for a reason :)
105  */
106 static struct resource ar231x_eth0_res[] = {
107         {
108                 .name = "eth0_membase",
109                 .flags = IORESOURCE_MEM,
110                 .start = KSEG1ADDR(AR531X_ENET1),
111                 .end = KSEG1ADDR(AR531X_ENET1 + 0x2000),
112         },
113         {
114                 .name = "eth0_irq",
115                 .flags = IORESOURCE_IRQ,
116                 .start = AR5312_IRQ_ENET1_INTRS,
117                 .end = AR5312_IRQ_ENET1_INTRS,
118         },
119 };
120 static struct ar531x_eth ar231x_eth0_data = {
121         .phy = 1,
122         .mac = 1,
123         .reset_base = AR531X_RESET,
124         .reset_mac = AR531X_RESET_ENET1,
125         .reset_phy = AR531X_RESET_EPHY1,
126         .phy_base = KSEG1ADDR(AR531X_ENET0),
127 };
128 static struct platform_device ar231x_eth0 = {
129         .id = 0,
130         .name = "ar531x-eth",
131         .dev.platform_data = &ar231x_eth0_data,
132         .resource = ar231x_eth0_res,
133         .num_resources = ARRAY_SIZE(ar231x_eth0_res)
134 };
135
136
137 static struct platform_device ar5312_wmac[] = {
138         {
139                 .id = 0,
140                 .name = "ar531x-wmac",
141         },
142         {
143                 .id = 1,
144                 .name = "ar531x-wmac",
145         },
146 };
147
148 static struct physmap_flash_data ar5312_flash_data = {
149         .width    = 2,
150 };
151
152 static struct resource ar5312_flash_resource = {
153         .start  = AR531X_FLASH,
154         .end    = AR531X_FLASH + 0x800000 - 1,
155         .flags  = IORESOURCE_MEM,
156 };
157
158 static struct platform_device ar5312_physmap_flash = {
159         .name      = "physmap-flash",
160         .id      = 0,
161         .dev            = {
162                 .platform_data  = &ar5312_flash_data,
163         },
164         .num_resources  = 1,
165         .resource   = &ar5312_flash_resource,
166 };
167
168 #ifdef CONFIG_LEDS_GPIO
169 static struct gpio_led ar5312_leds[] = {
170         { .name = "wlan", .gpio = 0, .active_low = 1, },
171 };
172
173 static const struct gpio_led_platform_data ar5312_led_data = {
174         .num_leds = ARRAY_SIZE(ar5312_leds),
175         .leds = (void *) ar5312_leds,
176 };
177
178 static struct platform_device ar5312_gpio_leds = {
179         .name = "leds-gpio",
180         .id = -1,
181         .dev = {
182                 .platform_data = (void *) &ar5312_led_data,
183         }
184 };
185 #endif
186
187 /*
188  * NB: This mapping size is larger than the actual flash size,
189  * but this shouldn't be a problem here, because the flash
190  * will simply be mapped multiple times.
191  */
192 static char __init *ar5312_flash_limit(void)
193 {
194         u32 ctl;
195         /* 
196          * Configure flash bank 0.
197          * Assume 8M window size. Flash will be aliased if it's smaller
198          */
199         ctl = FLASHCTL_E |
200                 FLASHCTL_AC_8M |
201                 FLASHCTL_RBLE |
202                 (0x01 << FLASHCTL_IDCY_S) |
203                 (0x07 << FLASHCTL_WST1_S) |
204                 (0x07 << FLASHCTL_WST2_S) |
205                 (sysRegRead(AR531X_FLASHCTL0) & FLASHCTL_MW);
206
207         sysRegWrite(AR531X_FLASHCTL0, ctl);
208         
209         /* Disable other flash banks */
210         sysRegWrite(AR531X_FLASHCTL1,
211                 sysRegRead(AR531X_FLASHCTL1) & ~(FLASHCTL_E | FLASHCTL_AC));
212
213         sysRegWrite(AR531X_FLASHCTL2,
214                 sysRegRead(AR531X_FLASHCTL2) & ~(FLASHCTL_E | FLASHCTL_AC));
215
216         return (char *) KSEG1ADDR(AR531X_FLASH + 0x800000);
217 }
218
219 static struct ar531x_config __init *init_wmac(int unit)
220 {
221         struct ar531x_config *config;
222         
223         config = (struct ar531x_config *) kzalloc(sizeof(struct ar531x_config), GFP_KERNEL);
224         config->board = board_config;
225         config->radio = radio_config;
226         config->unit = unit;
227         config->tag = (u_int16_t) ((sysRegRead(AR531X_REV) >> AR531X_REV_WMAC_MIN_S) & AR531X_REV_CHIP);
228
229         return config;
230 }
231                 
232 int __init ar5312_init_devices(void)
233 {
234         struct ar531x_boarddata *bcfg;
235         char *radio, *c;
236         int dev = 0;
237         uint32_t fctl = 0;
238
239         if (!is_5312)
240                 return 0;
241
242         /* Locate board/radio config data */
243         ar531x_find_config(ar5312_flash_limit());
244         bcfg = (struct ar531x_boarddata *) board_config;
245
246         
247         /*
248          * Chip IDs and hardware detection for some Atheros
249          * models are really broken!
250          * 
251          * Atheros uses a disabled WMAC0 and Silicon ID of AR5312
252          * as indication for AR2312, which is otherwise 
253          * indistinguishable from the real AR5312.
254          */
255         if (radio_config) {
256                 radio = radio_config + AR531X_RADIO_MASK_OFF;
257                 if ((*((u32 *) radio) & AR531X_RADIO0_MASK) == 0)
258                         bcfg->config |= BD_ISCASPER;
259         } else
260                 radio = NULL;
261
262         /* AR2313 has CPU minor rev. 10 */
263         if ((current_cpu_data.processor_id & 0xff) == 0x0a)
264                 mips_machtype = MACH_ATHEROS_AR2313;
265         
266         /* AR2312 shares the same Silicon ID as AR5312 */
267         else if (bcfg->config & BD_ISCASPER)
268                 mips_machtype = MACH_ATHEROS_AR2312;
269         
270         /* Everything else is probably AR5312 or compatible */
271         else
272                 mips_machtype = MACH_ATHEROS_AR5312;
273
274         ar5312_eth0_data.board_config = board_config;
275         ar5312_eth1_data.board_config = board_config;
276
277         /* fixup flash width */
278         fctl = sysRegRead(AR531X_FLASHCTL) & FLASHCTL_MW;
279         switch (fctl) {
280                 case FLASHCTL_MWx16:
281                         ar5312_flash_data.width = 2;
282                         break;
283                 case FLASHCTL_MWx8:
284                 default:
285                         ar5312_flash_data.width = 1;
286                         break;
287         }
288
289         ar5312_devs[dev++] = &ar5312_physmap_flash;
290
291         ar5312_leds[0].gpio = bcfg->sysLedGpio;
292         ar5312_devs[dev++] = &ar5312_gpio_leds;
293
294         if (!memcmp(bcfg->enet0Mac, "\xff\xff\xff\xff\xff\xff", 6))
295                 memcpy(bcfg->enet0Mac, bcfg->enet1Mac, 6);
296
297         if (memcmp(bcfg->enet0Mac, bcfg->enet1Mac, 6) == 0) {
298                 /* ENET0 and ENET1 have the same mac.
299                  * Increment the one from ENET1 */
300                 c = bcfg->enet1Mac + 5;
301                 while ((c >= (char *) bcfg->enet1Mac) && !(++(*c)))
302                         c--;
303         }
304
305         switch(mips_machtype) {
306                 case MACH_ATHEROS_AR5312:
307                         ar5312_eth0_data.macaddr = bcfg->enet0Mac;
308                         ar5312_eth1_data.macaddr = bcfg->enet1Mac;
309                         ar5312_devs[dev++] = &ar5312_eth[0];
310                         ar5312_devs[dev++] = &ar5312_eth[1];
311                         break;
312                 case MACH_ATHEROS_AR2312:
313                 case MACH_ATHEROS_AR2313:
314                         ar231x_eth0_data.macaddr = bcfg->enet0Mac;
315                         ar5312_devs[dev++] = &ar231x_eth0;
316                         ar5312_flash_data.width = 1;
317                         break;
318         }
319
320         if (radio) {
321                 if (mips_machtype == MACH_ATHEROS_AR5312) {
322                         if (*((u32 *) radio) & AR531X_RADIO0_MASK) {
323                                 ar5312_wmac[0].dev.platform_data = init_wmac(0);
324                                 ar5312_devs[dev++] = &ar5312_wmac[0];
325                         }
326                 }
327                 if (*((u32 *) radio) & AR531X_RADIO1_MASK) {
328                         ar5312_wmac[1].dev.platform_data = init_wmac(1);
329                         ar5312_devs[dev++] = &ar5312_wmac[1];
330                 }
331         }
332
333         return platform_add_devices(ar5312_devs, dev);
334 }
335
336
337 static void ar5312_halt(void)
338 {
339          while (1);
340 }
341
342 static void ar5312_power_off(void)
343 {
344          ar5312_halt();
345 }
346
347
348 static void ar5312_restart(char *command)
349 {
350         /* reset the system */
351         for(;;) sysRegWrite(AR531X_RESET, AR531X_RESET_SYSTEM);
352 }
353
354
355 /*
356  * This table is indexed by bits 5..4 of the CLOCKCTL1 register
357  * to determine the predevisor value.
358  */
359 static int __initdata CLOCKCTL1_PREDIVIDE_TABLE[4] = {
360         1,
361         2,
362         4,
363         5
364 };
365
366                 
367 static unsigned int __init ar5312_cpu_frequency(void)
368 {
369         unsigned int result;
370         unsigned int predivide_mask, predivide_shift;
371         unsigned int multiplier_mask, multiplier_shift;
372         unsigned int clockCtl1, preDivideSelect, preDivisor, multiplier;
373         unsigned int doubler_mask;
374         unsigned int wisoc_revision;
375
376         /* Trust the bootrom's idea of cpu frequency. */
377         if ((result = sysRegRead(AR5312_SCRATCH)))
378                 return result;
379
380         wisoc_revision = (sysRegRead(AR531X_REV) & AR531X_REV_MAJ) >> AR531X_REV_MAJ_S;
381         if (wisoc_revision == AR531X_REV_MAJ_AR2313) {
382                 predivide_mask = AR2313_CLOCKCTL1_PREDIVIDE_MASK;
383                 predivide_shift = AR2313_CLOCKCTL1_PREDIVIDE_SHIFT;
384                 multiplier_mask = AR2313_CLOCKCTL1_MULTIPLIER_MASK;
385                 multiplier_shift = AR2313_CLOCKCTL1_MULTIPLIER_SHIFT;
386                 doubler_mask = AR2313_CLOCKCTL1_DOUBLER_MASK;
387         } else { /* AR5312 and AR2312 */
388                 predivide_mask = AR5312_CLOCKCTL1_PREDIVIDE_MASK;
389                 predivide_shift = AR5312_CLOCKCTL1_PREDIVIDE_SHIFT;
390                 multiplier_mask = AR5312_CLOCKCTL1_MULTIPLIER_MASK;
391                 multiplier_shift = AR5312_CLOCKCTL1_MULTIPLIER_SHIFT;
392                 doubler_mask = AR5312_CLOCKCTL1_DOUBLER_MASK;
393         }
394         
395         /*
396          * Clocking is derived from a fixed 40MHz input clock.
397          *
398          *  cpuFreq = InputClock * MULT (where MULT is PLL multiplier)
399          *  sysFreq = cpuFreq / 4          (used for APB clock, serial,
400          *                                                         flash, Timer, Watchdog Timer)
401          *
402          *  cntFreq = cpuFreq / 2          (use for CPU count/compare)
403          * 
404          * So, for example, with a PLL multiplier of 5, we have
405          * 
406          *  cpuFreq = 200MHz
407          *  sysFreq = 50MHz
408          *  cntFreq = 100MHz
409          *
410          * We compute the CPU frequency, based on PLL settings.
411          */
412
413         clockCtl1 = sysRegRead(AR5312_CLOCKCTL1);
414         preDivideSelect = (clockCtl1 & predivide_mask) >> predivide_shift;
415         preDivisor = CLOCKCTL1_PREDIVIDE_TABLE[preDivideSelect];
416         multiplier = (clockCtl1 & multiplier_mask) >> multiplier_shift;
417         
418         if (clockCtl1 & doubler_mask) {
419                 multiplier = multiplier << 1;
420         }
421         return (40000000 / preDivisor) * multiplier;
422 }
423
424 static inline int ar5312_sys_frequency(void)
425 {
426         return ar5312_cpu_frequency() / 4;
427 }
428
429 static void __init ar5312_time_init(void)
430 {
431         mips_hpt_frequency = ar5312_cpu_frequency() / 2;
432 }
433
434
435 void __init ar5312_prom_init(void)
436 {
437         u32 memsize, memcfg, bank0AC, bank1AC;
438
439         is_5312 = 1;
440
441         /* Detect memory size */
442         memcfg = sysRegRead(AR531X_MEM_CFG1);
443         bank0AC = (memcfg & MEM_CFG1_AC0) >> MEM_CFG1_AC0_S;
444         bank1AC = (memcfg & MEM_CFG1_AC1) >> MEM_CFG1_AC1_S;
445         memsize = (bank0AC ? (1 << (bank0AC+1)) : 0)
446                 + (bank1AC ? (1 << (bank1AC+1)) : 0);
447         memsize <<= 20;
448         add_memory_region(0, memsize, BOOT_MEM_RAM);
449         
450         /* Initialize it to AR5312 for now. Real detection will be done
451          * in ar5312_init_devices() */
452         mips_machtype = MACH_ATHEROS_AR5312;
453 }
454
455 void __init ar5312_plat_setup(void)
456 {
457         /* Clear any lingering AHB errors */
458         sysRegRead(AR531X_PROCADDR);
459         sysRegRead(AR531X_DMAADDR);
460         sysRegWrite(AR531X_WD_CTRL, AR531X_WD_CTRL_IGNORE_EXPIRATION);
461
462         board_time_init = ar5312_time_init;
463
464         _machine_restart = ar5312_restart;
465         _machine_halt = ar5312_halt;
466         pm_power_off = ar5312_power_off;
467
468         serial_setup(KSEG1ADDR(AR531X_UART0), ar5312_sys_frequency());
469 }
470
471 arch_initcall(ar5312_init_devices);