add 2.6.26 specific files
[librecmc/librecmc.git] / target / linux / adm5120 / files-2.6.26 / arch / mips / adm5120 / common / platform.c
1 /*
2  *  ADM5120 generic platform devices
3  *
4  *  Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio.h>
19
20 #include <asm/bootinfo.h>
21
22 #include <asm/mach-adm5120/adm5120_defs.h>
23 #include <asm/mach-adm5120/adm5120_info.h>
24 #include <asm/mach-adm5120/adm5120_irq.h>
25 #include <asm/mach-adm5120/adm5120_switch.h>
26 #include <asm/mach-adm5120/adm5120_nand.h>
27 #include <asm/mach-adm5120/adm5120_platform.h>
28
29 #if 1
30 /*
31  * TODO:remove global adm5120_eth* variables when the switch driver will be
32  *      converted into a real platform driver
33  */
34 unsigned int adm5120_eth_num_ports = 6;
35 EXPORT_SYMBOL_GPL(adm5120_eth_num_ports);
36
37 unsigned char adm5120_eth_macs[6][6] = {
38         {'\00', 'A', 'D', 'M', '\x51', '\x20' },
39         {'\00', 'A', 'D', 'M', '\x51', '\x21' },
40         {'\00', 'A', 'D', 'M', '\x51', '\x22' },
41         {'\00', 'A', 'D', 'M', '\x51', '\x23' },
42         {'\00', 'A', 'D', 'M', '\x51', '\x24' },
43         {'\00', 'A', 'D', 'M', '\x51', '\x25' }
44 };
45 EXPORT_SYMBOL_GPL(adm5120_eth_macs);
46
47 unsigned char adm5120_eth_vlans[6] = {
48         0x41, 0x42, 0x44, 0x48, 0x50, 0x60
49 };
50 EXPORT_SYMBOL_GPL(adm5120_eth_vlans);
51 #endif
52
53 /*
54  * Built-in ethernet switch
55  */
56 struct resource adm5120_switch_resources[] = {
57         [0] = {
58                 .start  = ADM5120_SWITCH_BASE,
59                 .end    = ADM5120_SWITCH_BASE+ADM5120_SWITCH_SIZE-1,
60                 .flags  = IORESOURCE_MEM,
61         },
62         [1] = {
63                 .start  = ADM5120_IRQ_SWITCH,
64                 .end    = ADM5120_IRQ_SWITCH,
65                 .flags  = IORESOURCE_IRQ,
66         },
67 };
68
69 struct adm5120_switch_platform_data adm5120_switch_data;
70 struct platform_device adm5120_switch_device = {
71         .name           = "adm5120-switch",
72         .id             = -1,
73         .num_resources  = ARRAY_SIZE(adm5120_switch_resources),
74         .resource       = adm5120_switch_resources,
75         .dev.platform_data = &adm5120_switch_data,
76 };
77
78 void __init adm5120_add_device_switch(unsigned num_ports, u8 *vlan_map)
79 {
80         if (num_ports > 0)
81                 adm5120_eth_num_ports = num_ports;
82
83         if (vlan_map)
84                 memcpy(adm5120_eth_vlans, vlan_map, sizeof(adm5120_eth_vlans));
85
86         platform_device_register(&adm5120_switch_device);
87 }
88
89 /*
90  * USB Host Controller
91  */
92 struct resource adm5120_hcd_resources[] = {
93         [0] = {
94                 .start  = ADM5120_USBC_BASE,
95                 .end    = ADM5120_USBC_BASE+ADM5120_USBC_SIZE-1,
96                 .flags  = IORESOURCE_MEM,
97         },
98         [1] = {
99                 .start  = ADM5120_IRQ_USBC,
100                 .end    = ADM5120_IRQ_USBC,
101                 .flags  = IORESOURCE_IRQ,
102         },
103 };
104
105 static u64 adm5120_hcd_dma_mask = DMA_BIT_MASK(24);
106 struct platform_device adm5120_hcd_device = {
107         .name           = "adm5120-hcd",
108         .id             = -1,
109         .num_resources  = ARRAY_SIZE(adm5120_hcd_resources),
110         .resource       = adm5120_hcd_resources,
111         .dev = {
112                 .dma_mask               = &adm5120_hcd_dma_mask,
113                 .coherent_dma_mask      = DMA_BIT_MASK(24),
114         }
115 };
116
117 void __init adm5120_add_device_usb(void)
118 {
119         platform_device_register(&adm5120_hcd_device);
120 }
121
122 /*
123  * NOR flash devices
124  */
125 struct adm5120_flash_platform_data adm5120_flash0_data;
126 struct platform_device adm5120_flash0_device =  {
127         .name                   = "adm5120-flash",
128         .id                     = 0,
129         .dev.platform_data      = &adm5120_flash0_data,
130 };
131
132 struct adm5120_flash_platform_data adm5120_flash1_data;
133 struct platform_device adm5120_flash1_device =  {
134         .name                   = "adm5120-flash",
135         .id                     = 1,
136         .dev.platform_data      = &adm5120_flash1_data,
137 };
138
139 void __init adm5120_add_device_flash(unsigned id)
140 {
141         struct platform_device *pdev;
142
143         switch (id) {
144         case 0:
145                 pdev = &adm5120_flash0_device;
146                 break;
147         case 1:
148                 pdev = &adm5120_flash1_device;
149                 break;
150         default:
151                 pdev = NULL;
152                 break;
153         }
154
155         if (pdev)
156                 platform_device_register(pdev);
157 }
158
159 /*
160  * built-in UARTs
161  */
162 static void adm5120_uart_set_mctrl(struct amba_device *dev, void __iomem *base,
163                 unsigned int mctrl)
164 {
165 }
166
167 struct amba_pl010_data adm5120_uart0_data = {
168         .set_mctrl = adm5120_uart_set_mctrl
169 };
170
171 struct amba_device adm5120_uart0_device = {
172         .dev            = {
173                 .bus_id = "APB:UART0",
174                 .platform_data = &adm5120_uart0_data,
175         },
176         .res            = {
177                 .start  = ADM5120_UART0_BASE,
178                 .end    = ADM5120_UART0_BASE + ADM5120_UART_SIZE - 1,
179                 .flags  = IORESOURCE_MEM,
180         },
181         .irq            = { ADM5120_IRQ_UART0, -1 },
182         .periphid       = 0x0041010,
183 };
184
185 struct amba_pl010_data adm5120_uart1_data = {
186         .set_mctrl = adm5120_uart_set_mctrl
187 };
188
189 struct amba_device adm5120_uart1_device = {
190         .dev            = {
191                 .bus_id = "APB:UART1",
192                 .platform_data = &adm5120_uart1_data,
193         },
194         .res            = {
195                 .start  = ADM5120_UART1_BASE,
196                 .end    = ADM5120_UART1_BASE + ADM5120_UART_SIZE - 1,
197                 .flags  = IORESOURCE_MEM,
198         },
199         .irq            = { ADM5120_IRQ_UART1, -1 },
200         .periphid       = 0x0041010,
201 };
202
203 void __init adm5120_add_device_uart(unsigned id)
204 {
205         struct amba_device *dev;
206
207         switch (id) {
208         case 0:
209                 dev = &adm5120_uart0_device;
210                 break;
211         case 1:
212                 dev = &adm5120_uart1_device;
213                 break;
214         default:
215                 dev = NULL;
216                 break;
217         }
218
219         if (dev)
220                 amba_device_register(dev, &iomem_resource);
221 }
222
223 /*
224  * GPIO buttons
225  */
226 #define ADM5120_BUTTON_INTERVAL 20
227 struct gpio_buttons_platform_data adm5120_gpio_buttons_data = {
228         .poll_interval  = ADM5120_BUTTON_INTERVAL,
229 };
230
231 struct platform_device adm5120_gpio_buttons_device = {
232         .name           = "gpio-buttons",
233         .id             = -1,
234         .dev.platform_data = &adm5120_gpio_buttons_data,
235 };
236
237 void __init adm5120_add_device_gpio_buttons(unsigned nbuttons,
238                                     struct gpio_button *buttons)
239 {
240         struct gpio_button *p;
241
242         p = kmalloc(nbuttons * sizeof(*p), GFP_KERNEL);
243         if (!p)
244                 return;
245
246         memcpy(p, buttons, nbuttons * sizeof(*p));
247         adm5120_gpio_buttons_data.nbuttons = nbuttons;
248         adm5120_gpio_buttons_data.buttons = p;
249
250         platform_device_register(&adm5120_gpio_buttons_device);
251 }
252
253 /*
254  * GPIO LEDS
255  */
256 struct gpio_led_platform_data adm5120_gpio_leds_data;
257 struct platform_device adm5120_gpio_leds_device = {
258         .name           = "leds-gpio",
259         .id             = -1,
260         .dev.platform_data = &adm5120_gpio_leds_data,
261 };
262
263 void __init adm5120_add_device_gpio_leds(unsigned num_leds,
264                                     struct gpio_led *leds)
265 {
266         struct gpio_led *p;
267
268         p = kmalloc(num_leds * sizeof(*p), GFP_KERNEL);
269         if (!p)
270                 return;
271
272         memcpy(p, leds, num_leds * sizeof(*p));
273         adm5120_gpio_leds_data.num_leds = num_leds;
274         adm5120_gpio_leds_data.leds = p;
275
276         platform_device_register(&adm5120_gpio_leds_device);
277 }
278
279 /*
280  * GPIO device
281  */
282 static struct resource adm5120_gpio_resource[] __initdata = {
283         {
284                 .start  = 0x3fffff,
285         },
286 };
287
288 void __init adm5120_add_device_gpio(u32 disable_mask)
289 {
290         if (adm5120_package_pqfp())
291                 disable_mask |= 0xf0;
292
293         adm5120_gpio_resource[0].start &= ~disable_mask;
294         platform_device_register_simple("GPIODEV", -1,
295                         adm5120_gpio_resource,
296                         ARRAY_SIZE(adm5120_gpio_resource));
297 }
298
299 /*
300  * NAND flash
301  */
302 struct resource adm5120_nand_resources[] = {
303         [0] = {
304                 .start  = ADM5120_NAND_BASE,
305                 .end    = ADM5120_NAND_BASE + ADM5120_NAND_SIZE-1,
306                 .flags  = IORESOURCE_MEM,
307         },
308 };
309
310 static int adm5120_nand_ready(struct mtd_info *mtd)
311 {
312         return ((adm5120_nand_get_status() & ADM5120_NAND_STATUS_READY) != 0);
313 }
314
315 static void adm5120_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
316                                         unsigned int ctrl)
317 {
318         if (ctrl & NAND_CTRL_CHANGE) {
319                 adm5120_nand_set_cle(ctrl & NAND_CLE);
320                 adm5120_nand_set_ale(ctrl & NAND_ALE);
321                 adm5120_nand_set_cen(ctrl & NAND_NCE);
322         }
323
324         if (cmd != NAND_CMD_NONE)
325                 NAND_WRITE_REG(NAND_REG_DATA, cmd);
326 }
327
328 void __init adm5120_add_device_nand(struct platform_nand_data *pdata)
329 {
330         struct platform_device *pdev;
331         int err;
332
333         pdev = platform_device_alloc("gen_nand", -1);
334         if (!pdev)
335                 goto err_out;
336
337         err = platform_device_add_resources(pdev, adm5120_nand_resources,
338                                         ARRAY_SIZE(adm5120_nand_resources));
339         if (err)
340                 goto err_put;
341
342         err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
343         if (err)
344                 goto err_put;
345
346         pdata = pdev->dev.platform_data;
347         pdata->ctrl.dev_ready = adm5120_nand_ready;
348         pdata->ctrl.cmd_ctrl = adm5120_nand_cmd_ctrl;
349
350         err = platform_device_add(pdev);
351         if (err)
352                 goto err_put;
353
354         return;
355
356 err_put:
357         platform_device_put(pdev);
358 err_out:
359         return;
360 }