Merge git://git.kernel.org/pub/scm/linux/kernel/git/joern/logfs
[pandora-kernel.git] / arch / arm / mach-orion5x / dns323-setup.c
1 /*
2  * arch/arm/mach-orion5x/dns323-setup.c
3  *
4  * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <linux/pci.h>
18 #include <linux/irq.h>
19 #include <linux/mtd/physmap.h>
20 #include <linux/mv643xx_eth.h>
21 #include <linux/leds.h>
22 #include <linux/gpio_keys.h>
23 #include <linux/input.h>
24 #include <linux/i2c.h>
25 #include <linux/ata_platform.h>
26 #include <asm/mach-types.h>
27 #include <asm/gpio.h>
28 #include <asm/mach/arch.h>
29 #include <asm/mach/pci.h>
30 #include <mach/orion5x.h>
31 #include "common.h"
32 #include "mpp.h"
33
34 #define DNS323_GPIO_LED_RIGHT_AMBER     1
35 #define DNS323_GPIO_LED_LEFT_AMBER      2
36 #define DNS323_GPIO_SYSTEM_UP           3
37 #define DNS323_GPIO_LED_POWER           5
38 #define DNS323_GPIO_OVERTEMP            6
39 #define DNS323_GPIO_RTC                 7
40 #define DNS323_GPIO_POWER_OFF           8
41 #define DNS323_GPIO_KEY_POWER           9
42 #define DNS323_GPIO_KEY_RESET           10
43
44 /****************************************************************************
45  * PCI setup
46  */
47
48 static int __init dns323_pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
49 {
50         int irq;
51
52         /*
53          * Check for devices with hard-wired IRQs.
54          */
55         irq = orion5x_pci_map_irq(dev, slot, pin);
56         if (irq != -1)
57                 return irq;
58
59         return -1;
60 }
61
62 static struct hw_pci dns323_pci __initdata = {
63         .nr_controllers = 2,
64         .swizzle        = pci_std_swizzle,
65         .setup          = orion5x_pci_sys_setup,
66         .scan           = orion5x_pci_sys_scan_bus,
67         .map_irq        = dns323_pci_map_irq,
68 };
69
70 static int __init dns323_dev_id(void)
71 {
72         u32 dev, rev;
73
74         orion5x_pcie_id(&dev, &rev);
75
76         return dev;
77 }
78
79 static int __init dns323_pci_init(void)
80 {
81         /* The 5182 doesn't really use its PCI bus, and initialising PCI
82          * gets in the way of initialising the SATA controller.
83          */
84         if (machine_is_dns323() && dns323_dev_id() != MV88F5182_DEV_ID)
85                 pci_common_init(&dns323_pci);
86
87         return 0;
88 }
89
90 subsys_initcall(dns323_pci_init);
91
92 /****************************************************************************
93  * 8MiB NOR flash (Spansion S29GL064M90TFIR4)
94  *
95  * Layout as used by D-Link:
96  *  0x00000000-0x00010000 : "MTD1"
97  *  0x00010000-0x00020000 : "MTD2"
98  *  0x00020000-0x001a0000 : "Linux Kernel"
99  *  0x001a0000-0x007d0000 : "File System"
100  *  0x007d0000-0x00800000 : "u-boot"
101  */
102
103 #define DNS323_NOR_BOOT_BASE 0xf4000000
104 #define DNS323_NOR_BOOT_SIZE SZ_8M
105
106 static struct mtd_partition dns323_partitions[] = {
107         {
108                 .name   = "MTD1",
109                 .size   = 0x00010000,
110                 .offset = 0,
111         }, {
112                 .name   = "MTD2",
113                 .size   = 0x00010000,
114                 .offset = 0x00010000,
115         }, {
116                 .name   = "Linux Kernel",
117                 .size   = 0x00180000,
118                 .offset = 0x00020000,
119         }, {
120                 .name   = "File System",
121                 .size   = 0x00630000,
122                 .offset = 0x001A0000,
123         }, {
124                 .name   = "u-boot",
125                 .size   = 0x00030000,
126                 .offset = 0x007d0000,
127         },
128 };
129
130 static struct physmap_flash_data dns323_nor_flash_data = {
131         .width          = 1,
132         .parts          = dns323_partitions,
133         .nr_parts       = ARRAY_SIZE(dns323_partitions)
134 };
135
136 static struct resource dns323_nor_flash_resource = {
137         .flags          = IORESOURCE_MEM,
138         .start          = DNS323_NOR_BOOT_BASE,
139         .end            = DNS323_NOR_BOOT_BASE + DNS323_NOR_BOOT_SIZE - 1,
140 };
141
142 static struct platform_device dns323_nor_flash = {
143         .name           = "physmap-flash",
144         .id             = 0,
145         .dev            = {
146                 .platform_data  = &dns323_nor_flash_data,
147         },
148         .resource       = &dns323_nor_flash_resource,
149         .num_resources  = 1,
150 };
151
152 /****************************************************************************
153  * Ethernet
154  */
155
156 static struct mv643xx_eth_platform_data dns323_eth_data = {
157         .phy_addr = MV643XX_ETH_PHY_ADDR(8),
158 };
159
160 /* dns323_parse_hex_*() taken from tsx09-common.c; should a common copy of these
161  * functions be kept somewhere?
162  */
163 static int __init dns323_parse_hex_nibble(char n)
164 {
165         if (n >= '0' && n <= '9')
166                 return n - '0';
167
168         if (n >= 'A' && n <= 'F')
169                 return n - 'A' + 10;
170
171         if (n >= 'a' && n <= 'f')
172                 return n - 'a' + 10;
173
174         return -1;
175 }
176
177 static int __init dns323_parse_hex_byte(const char *b)
178 {
179         int hi;
180         int lo;
181
182         hi = dns323_parse_hex_nibble(b[0]);
183         lo = dns323_parse_hex_nibble(b[1]);
184
185         if (hi < 0 || lo < 0)
186                 return -1;
187
188         return (hi << 4) | lo;
189 }
190
191 static int __init dns323_read_mac_addr(void)
192 {
193         u_int8_t addr[6];
194         int i;
195         char *mac_page;
196
197         /* MAC address is stored as a regular ol' string in /dev/mtdblock4
198          * (0x007d0000-0x00800000) starting at offset 196480 (0x2ff80).
199          */
200         mac_page = ioremap(DNS323_NOR_BOOT_BASE + 0x7d0000 + 196480, 1024);
201         if (!mac_page)
202                 return -ENOMEM;
203
204         /* Sanity check the string we're looking at */
205         for (i = 0; i < 5; i++) {
206                 if (*(mac_page + (i * 3) + 2) != ':') {
207                         goto error_fail;
208                 }
209         }
210
211         for (i = 0; i < 6; i++) {
212                 int byte;
213
214                 byte = dns323_parse_hex_byte(mac_page + (i * 3));
215                 if (byte < 0) {
216                         goto error_fail;
217                 }
218
219                 addr[i] = byte;
220         }
221
222         iounmap(mac_page);
223         printk("DNS323: Found ethernet MAC address: ");
224         for (i = 0; i < 6; i++)
225                 printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
226
227         memcpy(dns323_eth_data.mac_addr, addr, 6);
228
229         return 0;
230
231 error_fail:
232         iounmap(mac_page);
233         return -EINVAL;
234 }
235
236 /****************************************************************************
237  * GPIO LEDs (simple - doesn't use hardware blinking support)
238  */
239
240 static struct gpio_led dns323_leds[] = {
241         {
242                 .name = "power:blue",
243                 .gpio = DNS323_GPIO_LED_POWER,
244                 .default_state = LEDS_GPIO_DEFSTATE_ON,
245         }, {
246                 .name = "right:amber",
247                 .gpio = DNS323_GPIO_LED_RIGHT_AMBER,
248                 .active_low = 1,
249         }, {
250                 .name = "left:amber",
251                 .gpio = DNS323_GPIO_LED_LEFT_AMBER,
252                 .active_low = 1,
253         },
254 };
255
256 static struct gpio_led_platform_data dns323_led_data = {
257         .num_leds       = ARRAY_SIZE(dns323_leds),
258         .leds           = dns323_leds,
259 };
260
261 static struct platform_device dns323_gpio_leds = {
262         .name           = "leds-gpio",
263         .id             = -1,
264         .dev            = {
265                 .platform_data  = &dns323_led_data,
266         },
267 };
268
269 /****************************************************************************
270  * GPIO Attached Keys
271  */
272
273 static struct gpio_keys_button dns323_buttons[] = {
274         {
275                 .code           = KEY_RESTART,
276                 .gpio           = DNS323_GPIO_KEY_RESET,
277                 .desc           = "Reset Button",
278                 .active_low     = 1,
279         }, {
280                 .code           = KEY_POWER,
281                 .gpio           = DNS323_GPIO_KEY_POWER,
282                 .desc           = "Power Button",
283                 .active_low     = 1,
284         },
285 };
286
287 static struct gpio_keys_platform_data dns323_button_data = {
288         .buttons        = dns323_buttons,
289         .nbuttons       = ARRAY_SIZE(dns323_buttons),
290 };
291
292 static struct platform_device dns323_button_device = {
293         .name           = "gpio-keys",
294         .id             = -1,
295         .num_resources  = 0,
296         .dev            = {
297                 .platform_data  = &dns323_button_data,
298         },
299 };
300
301 /*****************************************************************************
302  * SATA
303  */
304 static struct mv_sata_platform_data dns323_sata_data = {
305        .n_ports        = 2,
306 };
307
308 /****************************************************************************
309  * General Setup
310  */
311 static struct orion5x_mpp_mode dns323_mv88f5181_mpp_modes[] __initdata = {
312         {  0, MPP_PCIE_RST_OUTn },
313         {  1, MPP_GPIO },               /* right amber LED (sata ch0) */
314         {  2, MPP_GPIO },               /* left amber LED (sata ch1) */
315         {  3, MPP_UNUSED },
316         {  4, MPP_GPIO },               /* power button LED */
317         {  5, MPP_GPIO },               /* power button LED */
318         {  6, MPP_GPIO },               /* GMT G751-2f overtemp */
319         {  7, MPP_GPIO },               /* M41T80 nIRQ/OUT/SQW */
320         {  8, MPP_GPIO },               /* triggers power off */
321         {  9, MPP_GPIO },               /* power button switch */
322         { 10, MPP_GPIO },               /* reset button switch */
323         { 11, MPP_UNUSED },
324         { 12, MPP_UNUSED },
325         { 13, MPP_UNUSED },
326         { 14, MPP_UNUSED },
327         { 15, MPP_UNUSED },
328         { 16, MPP_UNUSED },
329         { 17, MPP_UNUSED },
330         { 18, MPP_UNUSED },
331         { 19, MPP_UNUSED },
332         { -1 },
333 };
334
335 static struct orion5x_mpp_mode dns323_mv88f5182_mpp_modes[] __initdata = {
336         {  0, MPP_UNUSED },
337         {  1, MPP_GPIO },               /* right amber LED (sata ch0) */
338         {  2, MPP_GPIO },               /* left amber LED (sata ch1) */
339         {  3, MPP_GPIO },               /* system up flag */
340         {  4, MPP_GPIO },               /* power button LED */
341         {  5, MPP_GPIO },               /* power button LED */
342         {  6, MPP_GPIO },               /* GMT G751-2f overtemp */
343         {  7, MPP_GPIO },               /* M41T80 nIRQ/OUT/SQW */
344         {  8, MPP_GPIO },               /* triggers power off */
345         {  9, MPP_GPIO },               /* power button switch */
346         { 10, MPP_GPIO },               /* reset button switch */
347         { 11, MPP_UNUSED },
348         { 12, MPP_SATA_LED },
349         { 13, MPP_SATA_LED },
350         { 14, MPP_SATA_LED },
351         { 15, MPP_SATA_LED },
352         { 16, MPP_UNUSED },
353         { 17, MPP_UNUSED },
354         { 18, MPP_UNUSED },
355         { 19, MPP_UNUSED },
356         { -1 },
357 };
358
359 /*
360  * On the DNS-323 the following devices are attached via I2C:
361  *
362  *  i2c addr | chip        | description
363  *  0x3e     | GMT G760Af  | fan speed PWM controller
364  *  0x48     | GMT G751-2f | temp. sensor and therm. watchdog (LM75 compatible)
365  *  0x68     | ST M41T80   | RTC w/ alarm
366  */
367 static struct i2c_board_info __initdata dns323_i2c_devices[] = {
368         {
369                 I2C_BOARD_INFO("g760a", 0x3e),
370         }, {
371                 I2C_BOARD_INFO("lm75", 0x48),
372         }, {
373                 I2C_BOARD_INFO("m41t80", 0x68),
374         },
375 };
376
377 /* DNS-323 rev. A specific power off method */
378 static void dns323a_power_off(void)
379 {
380         pr_info("%s: triggering power-off...\n", __func__);
381         gpio_set_value(DNS323_GPIO_POWER_OFF, 1);
382 }
383
384 /* DNS-323 rev B specific power off method */
385 static void dns323b_power_off(void)
386 {
387         pr_info("%s: triggering power-off...\n", __func__);
388         /* Pin has to be changed to 1 and back to 0 to do actual power off. */
389         gpio_set_value(DNS323_GPIO_POWER_OFF, 1);
390         mdelay(100);
391         gpio_set_value(DNS323_GPIO_POWER_OFF, 0);
392 }
393
394 static void __init dns323_init(void)
395 {
396         /* Setup basic Orion functions. Need to be called early. */
397         orion5x_init();
398
399         /* Just to be tricky, the 5182 has a completely different
400          * set of MPP modes to the 5181.
401          */
402         if (dns323_dev_id() == MV88F5182_DEV_ID)
403                 orion5x_mpp_conf(dns323_mv88f5182_mpp_modes);
404         else {
405                 orion5x_mpp_conf(dns323_mv88f5181_mpp_modes);
406                 writel(0, MPP_DEV_CTRL);                /* DEV_D[31:16] */
407         }
408
409         /* setup flash mapping
410          * CS3 holds a 8 MB Spansion S29GL064M90TFIR4
411          */
412         orion5x_setup_dev_boot_win(DNS323_NOR_BOOT_BASE, DNS323_NOR_BOOT_SIZE);
413         platform_device_register(&dns323_nor_flash);
414
415         platform_device_register(&dns323_gpio_leds);
416
417         platform_device_register(&dns323_button_device);
418
419         i2c_register_board_info(0, dns323_i2c_devices,
420                                 ARRAY_SIZE(dns323_i2c_devices));
421
422         /*
423          * Configure peripherals.
424          */
425         if (dns323_read_mac_addr() < 0)
426                 printk("DNS323: Failed to read MAC address\n");
427
428         orion5x_ehci0_init();
429         orion5x_eth_init(&dns323_eth_data);
430         orion5x_i2c_init();
431         orion5x_uart0_init();
432
433         /* The 5182 has its SATA controller on-chip, and needs its own little
434          * init routine.
435          */
436         if (dns323_dev_id() == MV88F5182_DEV_ID)
437                 orion5x_sata_init(&dns323_sata_data);
438
439         /* The 5182 has flag to indicate the system is up. Without this flag
440          * set, power LED will flash and cannot be controlled via leds-gpio.
441          */
442         if (dns323_dev_id() == MV88F5182_DEV_ID)
443                 gpio_set_value(DNS323_GPIO_SYSTEM_UP, 1);
444
445         /* Register dns323 specific power-off method */
446         if (gpio_request(DNS323_GPIO_POWER_OFF, "POWEROFF") != 0 ||
447             gpio_direction_output(DNS323_GPIO_POWER_OFF, 0) != 0)
448                 pr_err("DNS323: failed to setup power-off GPIO\n");
449         if (dns323_dev_id() == MV88F5182_DEV_ID)
450                 pm_power_off = dns323b_power_off;
451         else
452                 pm_power_off = dns323a_power_off;
453 }
454
455 /* Warning: D-Link uses a wrong mach-type (=526) in their bootloader */
456 MACHINE_START(DNS323, "D-Link DNS-323")
457         /* Maintainer: Herbert Valerio Riedel <hvr@gnu.org> */
458         .phys_io        = ORION5X_REGS_PHYS_BASE,
459         .io_pg_offst    = ((ORION5X_REGS_VIRT_BASE) >> 18) & 0xFFFC,
460         .boot_params    = 0x00000100,
461         .init_machine   = dns323_init,
462         .map_io         = orion5x_map_io,
463         .init_irq       = orion5x_init_irq,
464         .timer          = &orion5x_timer,
465         .fixup          = tag_fixup_mem32,
466 MACHINE_END