Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[pandora-kernel.git] / arch / avr32 / boards / atngw100 / setup.c
1 /*
2  * Board-specific setup code for the ATNGW100 Network Gateway
3  *
4  * Copyright (C) 2005-2006 Atmel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/clk.h>
11 #include <linux/etherdevice.h>
12 #include <linux/gpio.h>
13 #include <linux/irq.h>
14 #include <linux/i2c.h>
15 #include <linux/i2c-gpio.h>
16 #include <linux/init.h>
17 #include <linux/linkage.h>
18 #include <linux/platform_device.h>
19 #include <linux/types.h>
20 #include <linux/leds.h>
21 #include <linux/spi/spi.h>
22 #include <linux/atmel-mci.h>
23
24 #include <asm/io.h>
25 #include <asm/setup.h>
26
27 #include <mach/at32ap700x.h>
28 #include <mach/board.h>
29 #include <mach/init.h>
30 #include <mach/portmux.h>
31
32 /* Oscillator frequencies. These are board-specific */
33 unsigned long at32_board_osc_rates[3] = {
34         [0] = 32768,    /* 32.768 kHz on RTC osc */
35         [1] = 20000000, /* 20 MHz on osc0 */
36         [2] = 12000000, /* 12 MHz on osc1 */
37 };
38
39 /* Initialized by bootloader-specific startup code. */
40 struct tag *bootloader_tags __initdata;
41
42 struct eth_addr {
43         u8 addr[6];
44 };
45 static struct eth_addr __initdata hw_addr[2];
46 static struct eth_platform_data __initdata eth_data[2];
47
48 static struct spi_board_info spi0_board_info[] __initdata = {
49         {
50                 .modalias       = "mtd_dataflash",
51                 .max_speed_hz   = 8000000,
52                 .chip_select    = 0,
53         },
54 };
55
56 static struct mci_platform_data __initdata mci0_data = {
57         .slot[0] = {
58                 .bus_width      = 4,
59                 .detect_pin     = GPIO_PIN_PC(25),
60                 .wp_pin         = GPIO_PIN_PE(0),
61         },
62 };
63
64 /*
65  * The next two functions should go away as the boot loader is
66  * supposed to initialize the macb address registers with a valid
67  * ethernet address. But we need to keep it around for a while until
68  * we can be reasonably sure the boot loader does this.
69  *
70  * The phy_id is ignored as the driver will probe for it.
71  */
72 static int __init parse_tag_ethernet(struct tag *tag)
73 {
74         int i;
75
76         i = tag->u.ethernet.mac_index;
77         if (i < ARRAY_SIZE(hw_addr))
78                 memcpy(hw_addr[i].addr, tag->u.ethernet.hw_address,
79                        sizeof(hw_addr[i].addr));
80
81         return 0;
82 }
83 __tagtable(ATAG_ETHERNET, parse_tag_ethernet);
84
85 static void __init set_hw_addr(struct platform_device *pdev)
86 {
87         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
88         const u8 *addr;
89         void __iomem *regs;
90         struct clk *pclk;
91
92         if (!res)
93                 return;
94         if (pdev->id >= ARRAY_SIZE(hw_addr))
95                 return;
96
97         addr = hw_addr[pdev->id].addr;
98         if (!is_valid_ether_addr(addr))
99                 return;
100
101         /*
102          * Since this is board-specific code, we'll cheat and use the
103          * physical address directly as we happen to know that it's
104          * the same as the virtual address.
105          */
106         regs = (void __iomem __force *)res->start;
107         pclk = clk_get(&pdev->dev, "pclk");
108         if (!pclk)
109                 return;
110
111         clk_enable(pclk);
112         __raw_writel((addr[3] << 24) | (addr[2] << 16)
113                      | (addr[1] << 8) | addr[0], regs + 0x98);
114         __raw_writel((addr[5] << 8) | addr[4], regs + 0x9c);
115         clk_disable(pclk);
116         clk_put(pclk);
117 }
118
119 void __init setup_board(void)
120 {
121         at32_map_usart(1, 0, 0);        /* USART 1: /dev/ttyS0, DB9 */
122         at32_setup_serial_console(0);
123 }
124
125 static const struct gpio_led ngw_leds[] = {
126         { .name = "sys", .gpio = GPIO_PIN_PA(16), .active_low = 1,
127                 .default_trigger = "heartbeat",
128         },
129         { .name = "a", .gpio = GPIO_PIN_PA(19), .active_low = 1, },
130         { .name = "b", .gpio = GPIO_PIN_PE(19), .active_low = 1, },
131 };
132
133 static const struct gpio_led_platform_data ngw_led_data = {
134         .num_leds =     ARRAY_SIZE(ngw_leds),
135         .leds =         (void *) ngw_leds,
136 };
137
138 static struct platform_device ngw_gpio_leds = {
139         .name =         "leds-gpio",
140         .id =           -1,
141         .dev = {
142                 .platform_data = (void *) &ngw_led_data,
143         }
144 };
145
146 static struct i2c_gpio_platform_data i2c_gpio_data = {
147         .sda_pin                = GPIO_PIN_PA(6),
148         .scl_pin                = GPIO_PIN_PA(7),
149         .sda_is_open_drain      = 1,
150         .scl_is_open_drain      = 1,
151         .udelay                 = 2,    /* close to 100 kHz */
152 };
153
154 static struct platform_device i2c_gpio_device = {
155         .name           = "i2c-gpio",
156         .id             = 0,
157         .dev            = {
158                 .platform_data  = &i2c_gpio_data,
159         },
160 };
161
162 static struct i2c_board_info __initdata i2c_info[] = {
163         /* NOTE:  original ATtiny24 firmware is at address 0x0b */
164 };
165
166 static int __init atngw100_init(void)
167 {
168         unsigned        i;
169
170         /*
171          * ATNGW100 uses 16-bit SDRAM interface, so we don't need to
172          * reserve any pins for it.
173          */
174
175         at32_add_device_usart(0);
176
177         set_hw_addr(at32_add_device_eth(0, &eth_data[0]));
178         set_hw_addr(at32_add_device_eth(1, &eth_data[1]));
179
180         at32_add_device_spi(0, spi0_board_info, ARRAY_SIZE(spi0_board_info));
181         at32_add_device_mci(0, &mci0_data);
182         at32_add_device_usba(0, NULL);
183
184         for (i = 0; i < ARRAY_SIZE(ngw_leds); i++) {
185                 at32_select_gpio(ngw_leds[i].gpio,
186                                 AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
187         }
188         platform_device_register(&ngw_gpio_leds);
189
190         /* all these i2c/smbus pins should have external pullups for
191          * open-drain sharing among all I2C devices.  SDA and SCL do;
192          * PB28/EXTINT3 doesn't; it should be SMBALERT# (for PMBus),
193          * but it's not available off-board.
194          */
195         at32_select_periph(GPIO_PIOB_BASE, 1 << 28, 0, AT32_GPIOF_PULLUP);
196         at32_select_gpio(i2c_gpio_data.sda_pin,
197                 AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
198         at32_select_gpio(i2c_gpio_data.scl_pin,
199                 AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
200         platform_device_register(&i2c_gpio_device);
201         i2c_register_board_info(0, i2c_info, ARRAY_SIZE(i2c_info));
202
203         return 0;
204 }
205 postcore_initcall(atngw100_init);
206
207 static int __init atngw100_arch_init(void)
208 {
209         /* PB30 is the otherwise unused jumper on the mainboard, with an
210          * external pullup; the jumper grounds it.  Use it however you
211          * like, including letting U-Boot or Linux tweak boot sequences.
212          */
213         at32_select_gpio(GPIO_PIN_PB(30), 0);
214         gpio_request(GPIO_PIN_PB(30), "j15");
215         gpio_direction_input(GPIO_PIN_PB(30));
216         gpio_export(GPIO_PIN_PB(30), false);
217
218         /* set_irq_type() after the arch_initcall for EIC has run, and
219          * before the I2C subsystem could try using this IRQ.
220          */
221         return set_irq_type(AT32_EXTINT(3), IRQ_TYPE_EDGE_FALLING);
222 }
223 arch_initcall(atngw100_arch_init);