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