ARM: MX3: modularize 'mx31lite' code
[safe/jmp/linux-2.6] / arch / arm / mach-mx3 / mx31moboard.c
1 /*
2  *  Copyright (C) 2008 Valentin Longchamp, EPFL Mobots group
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/fsl_devices.h>
22 #include <linux/gpio.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/leds.h>
26 #include <linux/memory.h>
27 #include <linux/mtd/physmap.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/mfd/mc13783.h>
32 #include <linux/spi/spi.h>
33 #include <linux/types.h>
34
35 #include <asm/mach-types.h>
36 #include <asm/mach/arch.h>
37 #include <asm/mach/time.h>
38 #include <asm/mach/map.h>
39 #include <mach/board-mx31moboard.h>
40 #include <mach/common.h>
41 #include <mach/hardware.h>
42 #include <mach/imx-uart.h>
43 #include <mach/iomux-mx3.h>
44 #include <mach/ipu.h>
45 #include <mach/i2c.h>
46 #include <mach/mmc.h>
47 #include <mach/mx3_camera.h>
48 #include <mach/spi.h>
49
50 #include "devices.h"
51
52 static unsigned int moboard_pins[] = {
53         /* UART0 */
54         MX31_PIN_TXD1__TXD1, MX31_PIN_RXD1__RXD1,
55         MX31_PIN_CTS1__GPIO2_7,
56         /* UART4 */
57         MX31_PIN_PC_RST__CTS5, MX31_PIN_PC_VS2__RTS5,
58         MX31_PIN_PC_BVD2__TXD5, MX31_PIN_PC_BVD1__RXD5,
59         /* I2C0 */
60         MX31_PIN_I2C_DAT__I2C1_SDA, MX31_PIN_I2C_CLK__I2C1_SCL,
61         /* I2C1 */
62         MX31_PIN_DCD_DTE1__I2C2_SDA, MX31_PIN_RI_DTE1__I2C2_SCL,
63         /* SDHC1 */
64         MX31_PIN_SD1_DATA3__SD1_DATA3, MX31_PIN_SD1_DATA2__SD1_DATA2,
65         MX31_PIN_SD1_DATA1__SD1_DATA1, MX31_PIN_SD1_DATA0__SD1_DATA0,
66         MX31_PIN_SD1_CLK__SD1_CLK, MX31_PIN_SD1_CMD__SD1_CMD,
67         MX31_PIN_ATA_CS0__GPIO3_26, MX31_PIN_ATA_CS1__GPIO3_27,
68         /* USB reset */
69         MX31_PIN_GPIO1_0__GPIO1_0,
70         /* USB OTG */
71         MX31_PIN_USBOTG_DATA0__USBOTG_DATA0,
72         MX31_PIN_USBOTG_DATA1__USBOTG_DATA1,
73         MX31_PIN_USBOTG_DATA2__USBOTG_DATA2,
74         MX31_PIN_USBOTG_DATA3__USBOTG_DATA3,
75         MX31_PIN_USBOTG_DATA4__USBOTG_DATA4,
76         MX31_PIN_USBOTG_DATA5__USBOTG_DATA5,
77         MX31_PIN_USBOTG_DATA6__USBOTG_DATA6,
78         MX31_PIN_USBOTG_DATA7__USBOTG_DATA7,
79         MX31_PIN_USBOTG_CLK__USBOTG_CLK, MX31_PIN_USBOTG_DIR__USBOTG_DIR,
80         MX31_PIN_USBOTG_NXT__USBOTG_NXT, MX31_PIN_USBOTG_STP__USBOTG_STP,
81         MX31_PIN_USB_OC__GPIO1_30,
82         /* LEDs */
83         MX31_PIN_SVEN0__GPIO2_0, MX31_PIN_STX0__GPIO2_1,
84         MX31_PIN_SRX0__GPIO2_2, MX31_PIN_SIMPD0__GPIO2_3,
85         /* SEL */
86         MX31_PIN_DTR_DCE1__GPIO2_8, MX31_PIN_DSR_DCE1__GPIO2_9,
87         MX31_PIN_RI_DCE1__GPIO2_10, MX31_PIN_DCD_DCE1__GPIO2_11,
88         /* SPI1 */
89         MX31_PIN_CSPI2_MOSI__MOSI, MX31_PIN_CSPI2_MISO__MISO,
90         MX31_PIN_CSPI2_SCLK__SCLK, MX31_PIN_CSPI2_SPI_RDY__SPI_RDY,
91         MX31_PIN_CSPI2_SS0__SS0, MX31_PIN_CSPI2_SS2__SS2,
92         /* Atlas IRQ */
93         MX31_PIN_GPIO1_3__GPIO1_3,
94         /* SPI2 */
95         MX31_PIN_CSPI3_MOSI__MOSI, MX31_PIN_CSPI3_MISO__MISO,
96         MX31_PIN_CSPI3_SCLK__SCLK, MX31_PIN_CSPI3_SPI_RDY__SPI_RDY,
97         MX31_PIN_CSPI2_SS1__CSPI3_SS1,
98 };
99
100 static struct physmap_flash_data mx31moboard_flash_data = {
101         .width          = 2,
102 };
103
104 static struct resource mx31moboard_flash_resource = {
105         .start  = 0xa0000000,
106         .end    = 0xa1ffffff,
107         .flags  = IORESOURCE_MEM,
108 };
109
110 static struct platform_device mx31moboard_flash = {
111         .name   = "physmap-flash",
112         .id     = 0,
113         .dev    = {
114                 .platform_data  = &mx31moboard_flash_data,
115         },
116         .resource = &mx31moboard_flash_resource,
117         .num_resources = 1,
118 };
119
120 static int moboard_uart0_init(struct platform_device *pdev)
121 {
122         gpio_request(IOMUX_TO_GPIO(MX31_PIN_CTS1), "uart0-cts-hack");
123         gpio_direction_output(IOMUX_TO_GPIO(MX31_PIN_CTS1), 0);
124         return 0;
125 }
126
127 static struct imxuart_platform_data uart0_pdata = {
128         .init = moboard_uart0_init,
129 };
130
131 static struct imxuart_platform_data uart4_pdata = {
132         .flags = IMXUART_HAVE_RTSCTS,
133 };
134
135 static struct imxi2c_platform_data moboard_i2c0_pdata = {
136         .bitrate = 400000,
137 };
138
139 static struct imxi2c_platform_data moboard_i2c1_pdata = {
140         .bitrate = 100000,
141 };
142
143 static int moboard_spi1_cs[] = {
144         MXC_SPI_CS(0),
145         MXC_SPI_CS(2),
146 };
147
148 static struct spi_imx_master moboard_spi1_master = {
149         .chipselect     = moboard_spi1_cs,
150         .num_chipselect = ARRAY_SIZE(moboard_spi1_cs),
151 };
152
153 static struct regulator_consumer_supply sdhc_consumers[] = {
154         {
155                 .dev    = &mxcsdhc_device0.dev,
156                 .supply = "sdhc0_vcc",
157         },
158         {
159                 .dev    = &mxcsdhc_device1.dev,
160                 .supply = "sdhc1_vcc",
161         },
162 };
163
164 static struct regulator_init_data sdhc_vreg_data = {
165         .constraints = {
166                 .min_uV = 2700000,
167                 .max_uV = 3000000,
168                 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE |
169                         REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
170                 .valid_modes_mask = REGULATOR_MODE_NORMAL |
171                         REGULATOR_MODE_FAST,
172                 .always_on = 0,
173                 .boot_on = 1,
174         },
175         .num_consumer_supplies = ARRAY_SIZE(sdhc_consumers),
176         .consumer_supplies = sdhc_consumers,
177 };
178
179 static struct regulator_consumer_supply cam_consumers[] = {
180         {
181                 .dev    = &mx3_camera.dev,
182                 .supply = "cam_vcc",
183         },
184 };
185
186 static struct regulator_init_data cam_vreg_data = {
187         .constraints = {
188                 .min_uV = 2700000,
189                 .max_uV = 3000000,
190                 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE |
191                         REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
192                 .valid_modes_mask = REGULATOR_MODE_NORMAL |
193                         REGULATOR_MODE_FAST,
194                 .always_on = 0,
195                 .boot_on = 1,
196         },
197         .num_consumer_supplies = ARRAY_SIZE(cam_consumers),
198         .consumer_supplies = cam_consumers,
199 };
200
201 static struct mc13783_regulator_init_data moboard_regulators[] = {
202         {
203                 .id = MC13783_REGU_VMMC1,
204                 .init_data = &sdhc_vreg_data,
205         },
206         {
207                 .id = MC13783_REGU_VCAM,
208                 .init_data = &cam_vreg_data,
209         },
210 };
211
212 static struct mc13783_platform_data moboard_pmic = {
213         .regulators = moboard_regulators,
214         .num_regulators = ARRAY_SIZE(moboard_regulators),
215         .flags = MC13783_USE_REGULATOR | MC13783_USE_RTC,
216 };
217
218 static struct spi_board_info moboard_spi_board_info[] __initdata = {
219         {
220                 .modalias = "mc13783",
221                 .irq = IOMUX_TO_IRQ(MX31_PIN_GPIO1_3),
222                 .max_speed_hz = 300000,
223                 .bus_num = 1,
224                 .chip_select = 0,
225                 .platform_data = &moboard_pmic,
226                 .mode = SPI_CS_HIGH,
227         },
228         {
229                 .modalias = "spidev",
230                 .max_speed_hz = 300000,
231                 .bus_num = 1,
232                 .chip_select = 1, /* according spi1_cs[] ! */
233         },
234 };
235
236 static int moboard_spi2_cs[] = {
237         MXC_SPI_CS(1),
238 };
239
240 static struct spi_imx_master moboard_spi2_master = {
241         .chipselect     = moboard_spi2_cs,
242         .num_chipselect = ARRAY_SIZE(moboard_spi2_cs),
243 };
244
245 #define SDHC1_CD IOMUX_TO_GPIO(MX31_PIN_ATA_CS0)
246 #define SDHC1_WP IOMUX_TO_GPIO(MX31_PIN_ATA_CS1)
247
248 static int moboard_sdhc1_get_ro(struct device *dev)
249 {
250         return !gpio_get_value(SDHC1_WP);
251 }
252
253 static int moboard_sdhc1_init(struct device *dev, irq_handler_t detect_irq,
254                 void *data)
255 {
256         int ret;
257
258         ret = gpio_request(SDHC1_CD, "sdhc-detect");
259         if (ret)
260                 return ret;
261
262         gpio_direction_input(SDHC1_CD);
263
264         ret = gpio_request(SDHC1_WP, "sdhc-wp");
265         if (ret)
266                 goto err_gpio_free;
267         gpio_direction_input(SDHC1_WP);
268
269         ret = request_irq(gpio_to_irq(SDHC1_CD), detect_irq,
270                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
271                 "sdhc1-card-detect", data);
272         if (ret)
273                 goto err_gpio_free_2;
274
275         return 0;
276
277 err_gpio_free_2:
278         gpio_free(SDHC1_WP);
279 err_gpio_free:
280         gpio_free(SDHC1_CD);
281
282         return ret;
283 }
284
285 static void moboard_sdhc1_exit(struct device *dev, void *data)
286 {
287         free_irq(gpio_to_irq(SDHC1_CD), data);
288         gpio_free(SDHC1_WP);
289         gpio_free(SDHC1_CD);
290 }
291
292 static struct imxmmc_platform_data sdhc1_pdata = {
293         .get_ro = moboard_sdhc1_get_ro,
294         .init   = moboard_sdhc1_init,
295         .exit   = moboard_sdhc1_exit,
296 };
297
298 /*
299  * this pin is dedicated for all mx31moboard systems, so we do it here
300  */
301 #define USB_RESET_B     IOMUX_TO_GPIO(MX31_PIN_GPIO1_0)
302
303 static void usb_xcvr_reset(void)
304 {
305         gpio_request(USB_RESET_B, "usb-reset");
306         gpio_direction_output(USB_RESET_B, 0);
307         mdelay(1);
308         gpio_set_value(USB_RESET_B, 1);
309 }
310
311 #define USB_PAD_CFG (PAD_CTL_DRV_MAX | PAD_CTL_SRE_FAST | PAD_CTL_HYS_CMOS | \
312                         PAD_CTL_ODE_CMOS | PAD_CTL_100K_PU)
313
314 #define OTG_EN_B IOMUX_TO_GPIO(MX31_PIN_USB_OC)
315
316 static void moboard_usbotg_init(void)
317 {
318         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA0, USB_PAD_CFG);
319         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA1, USB_PAD_CFG);
320         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA2, USB_PAD_CFG);
321         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA3, USB_PAD_CFG);
322         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA4, USB_PAD_CFG);
323         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA5, USB_PAD_CFG);
324         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA6, USB_PAD_CFG);
325         mxc_iomux_set_pad(MX31_PIN_USBOTG_DATA7, USB_PAD_CFG);
326         mxc_iomux_set_pad(MX31_PIN_USBOTG_CLK, USB_PAD_CFG);
327         mxc_iomux_set_pad(MX31_PIN_USBOTG_DIR, USB_PAD_CFG);
328         mxc_iomux_set_pad(MX31_PIN_USBOTG_NXT, USB_PAD_CFG);
329         mxc_iomux_set_pad(MX31_PIN_USBOTG_STP, USB_PAD_CFG);
330
331         gpio_request(OTG_EN_B, "usb-udc-en");
332         gpio_direction_output(OTG_EN_B, 0);
333 }
334
335 static struct fsl_usb2_platform_data usb_pdata = {
336         .operating_mode = FSL_USB2_DR_DEVICE,
337         .phy_mode       = FSL_USB2_PHY_ULPI,
338 };
339
340 static struct gpio_led mx31moboard_leds[] = {
341         {
342                 .name   = "coreboard-led-0:red:running",
343                 .default_trigger = "heartbeat",
344                 .gpio   = IOMUX_TO_GPIO(MX31_PIN_SVEN0),
345         }, {
346                 .name   = "coreboard-led-1:red",
347                 .gpio   = IOMUX_TO_GPIO(MX31_PIN_STX0),
348         }, {
349                 .name   = "coreboard-led-2:red",
350                 .gpio   = IOMUX_TO_GPIO(MX31_PIN_SRX0),
351         }, {
352                 .name   = "coreboard-led-3:red",
353                 .gpio   = IOMUX_TO_GPIO(MX31_PIN_SIMPD0),
354         },
355 };
356
357 static struct gpio_led_platform_data mx31moboard_led_pdata = {
358         .num_leds       = ARRAY_SIZE(mx31moboard_leds),
359         .leds           = mx31moboard_leds,
360 };
361
362 static struct platform_device mx31moboard_leds_device = {
363         .name   = "leds-gpio",
364         .id     = -1,
365         .dev    = {
366                 .platform_data = &mx31moboard_led_pdata,
367         },
368 };
369
370 #define SEL0 IOMUX_TO_GPIO(MX31_PIN_DTR_DCE1)
371 #define SEL1 IOMUX_TO_GPIO(MX31_PIN_DSR_DCE1)
372 #define SEL2 IOMUX_TO_GPIO(MX31_PIN_RI_DCE1)
373 #define SEL3 IOMUX_TO_GPIO(MX31_PIN_DCD_DCE1)
374
375 static void mx31moboard_init_sel_gpios(void)
376 {
377         if (!gpio_request(SEL0, "sel0")) {
378                 gpio_direction_input(SEL0);
379                 gpio_export(SEL0, true);
380         }
381
382         if (!gpio_request(SEL1, "sel1")) {
383                 gpio_direction_input(SEL1);
384                 gpio_export(SEL1, true);
385         }
386
387         if (!gpio_request(SEL2, "sel2")) {
388                 gpio_direction_input(SEL2);
389                 gpio_export(SEL2, true);
390         }
391
392         if (!gpio_request(SEL3, "sel3")) {
393                 gpio_direction_input(SEL3);
394                 gpio_export(SEL3, true);
395         }
396 }
397
398 static struct ipu_platform_data mx3_ipu_data = {
399         .irq_base = MXC_IPU_IRQ_START,
400 };
401
402 static struct platform_device *devices[] __initdata = {
403         &mx31moboard_flash,
404         &mx31moboard_leds_device,
405 };
406
407 static struct mx3_camera_pdata camera_pdata = {
408         .dma_dev        = &mx3_ipu.dev,
409         .flags          = MX3_CAMERA_DATAWIDTH_8 | MX3_CAMERA_DATAWIDTH_10,
410         .mclk_10khz     = 4800,
411 };
412
413 #define CAMERA_BUF_SIZE (4*1024*1024)
414
415 static int __init mx31moboard_cam_alloc_dma(const size_t buf_size)
416 {
417         dma_addr_t dma_handle;
418         void *buf;
419         int dma;
420
421         if (buf_size < 2 * 1024 * 1024)
422                 return -EINVAL;
423
424         buf = dma_alloc_coherent(NULL, buf_size, &dma_handle, GFP_KERNEL);
425         if (!buf) {
426                 pr_err("%s: cannot allocate camera buffer-memory\n", __func__);
427                 return -ENOMEM;
428         }
429
430         memset(buf, 0, buf_size);
431
432         dma = dma_declare_coherent_memory(&mx3_camera.dev,
433                                         dma_handle, dma_handle, buf_size,
434                                         DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE);
435
436         /* The way we call dma_declare_coherent_memory only a malloc can fail */
437         return dma & DMA_MEMORY_MAP ? 0 : -ENOMEM;
438 }
439
440 static int mx31moboard_baseboard;
441 core_param(mx31moboard_baseboard, mx31moboard_baseboard, int, 0444);
442
443 /*
444  * Board specific initialization.
445  */
446 static void __init mxc_board_init(void)
447 {
448         mxc_iomux_setup_multiple_pins(moboard_pins, ARRAY_SIZE(moboard_pins),
449                 "moboard");
450
451         platform_add_devices(devices, ARRAY_SIZE(devices));
452
453         mxc_register_device(&mxc_uart_device0, &uart0_pdata);
454
455         mxc_register_device(&mxc_uart_device4, &uart4_pdata);
456
457         mx31moboard_init_sel_gpios();
458
459         mxc_register_device(&mxc_i2c_device0, &moboard_i2c0_pdata);
460         mxc_register_device(&mxc_i2c_device1, &moboard_i2c1_pdata);
461
462         mxc_register_device(&mxc_spi_device1, &moboard_spi1_master);
463         mxc_register_device(&mxc_spi_device2, &moboard_spi2_master);
464
465         gpio_request(IOMUX_TO_GPIO(MX31_PIN_GPIO1_3), "pmic-irq");
466         gpio_direction_input(IOMUX_TO_GPIO(MX31_PIN_GPIO1_3));
467         spi_register_board_info(moboard_spi_board_info,
468                 ARRAY_SIZE(moboard_spi_board_info));
469
470         mxc_register_device(&mxcsdhc_device0, &sdhc1_pdata);
471
472         mxc_register_device(&mx3_ipu, &mx3_ipu_data);
473         if (!mx31moboard_cam_alloc_dma(CAMERA_BUF_SIZE))
474                 mxc_register_device(&mx3_camera, &camera_pdata);
475
476         usb_xcvr_reset();
477
478         moboard_usbotg_init();
479         mxc_register_device(&mxc_otg_udc_device, &usb_pdata);
480
481         switch (mx31moboard_baseboard) {
482         case MX31NOBOARD:
483                 break;
484         case MX31DEVBOARD:
485                 mx31moboard_devboard_init();
486                 break;
487         case MX31MARXBOT:
488                 mx31moboard_marxbot_init();
489                 break;
490         default:
491                 printk(KERN_ERR "Illegal mx31moboard_baseboard type %d\n",
492                         mx31moboard_baseboard);
493         }
494 }
495
496 static void __init mx31moboard_timer_init(void)
497 {
498         mx31_clocks_init(26000000);
499 }
500
501 struct sys_timer mx31moboard_timer = {
502         .init   = mx31moboard_timer_init,
503 };
504
505 MACHINE_START(MX31MOBOARD, "EPFL Mobots mx31moboard")
506         /* Maintainer: Valentin Longchamp, EPFL Mobots group */
507         .phys_io        = AIPS1_BASE_ADDR,
508         .io_pg_offst    = ((AIPS1_BASE_ADDR_VIRT) >> 18) & 0xfffc,
509         .boot_params    = PHYS_OFFSET + 0x100,
510         .map_io         = mx31_map_io,
511         .init_irq       = mx31_init_irq,
512         .init_machine   = mxc_board_init,
513         .timer          = &mx31moboard_timer,
514 MACHINE_END
515