generic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * arch/arm/mach-imx/generic.c
  3. *
  4. * author: Sascha Hauer
  5. * Created: april 20th, 2004
  6. * Copyright: Synertronixx GmbH
  7. *
  8. * Common code for i.MX machines
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/platform_device.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/string.h>
  30. #include <asm/errno.h>
  31. #include <mach/imxfb.h>
  32. #include <mach/hardware.h>
  33. #include <mach/imx-regs.h>
  34. #include <asm/mach/map.h>
  35. #include <mach/mmc.h>
  36. #include <mach/gpio.h>
  37. unsigned long imx_gpio_alloc_map[(GPIO_PORT_MAX + 1) * 32 / BITS_PER_LONG];
  38. void imx_gpio_mode(int gpio_mode)
  39. {
  40. unsigned int pin = gpio_mode & GPIO_PIN_MASK;
  41. unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
  42. unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
  43. unsigned int tmp;
  44. /* Pullup enable */
  45. if(gpio_mode & GPIO_PUEN)
  46. PUEN(port) |= (1<<pin);
  47. else
  48. PUEN(port) &= ~(1<<pin);
  49. /* Data direction */
  50. if(gpio_mode & GPIO_OUT)
  51. DDIR(port) |= 1<<pin;
  52. else
  53. DDIR(port) &= ~(1<<pin);
  54. /* Primary / alternate function */
  55. if(gpio_mode & GPIO_AF)
  56. GPR(port) |= (1<<pin);
  57. else
  58. GPR(port) &= ~(1<<pin);
  59. /* use as gpio? */
  60. if(gpio_mode & GPIO_GIUS)
  61. GIUS(port) |= (1<<pin);
  62. else
  63. GIUS(port) &= ~(1<<pin);
  64. /* Output / input configuration */
  65. /* FIXME: I'm not very sure about OCR and ICONF, someone
  66. * should have a look over it
  67. */
  68. if(pin<16) {
  69. tmp = OCR1(port);
  70. tmp &= ~( 3<<(pin*2));
  71. tmp |= (ocr << (pin*2));
  72. OCR1(port) = tmp;
  73. ICONFA1(port) &= ~( 3<<(pin*2));
  74. ICONFA1(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2);
  75. ICONFB1(port) &= ~( 3<<(pin*2));
  76. ICONFB1(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2);
  77. } else {
  78. tmp = OCR2(port);
  79. tmp &= ~( 3<<((pin-16)*2));
  80. tmp |= (ocr << ((pin-16)*2));
  81. OCR2(port) = tmp;
  82. ICONFA2(port) &= ~( 3<<((pin-16)*2));
  83. ICONFA2(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << ((pin-16) * 2);
  84. ICONFB2(port) &= ~( 3<<((pin-16)*2));
  85. ICONFB2(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << ((pin-16) * 2);
  86. }
  87. }
  88. EXPORT_SYMBOL(imx_gpio_mode);
  89. int imx_gpio_request(unsigned gpio, const char *label)
  90. {
  91. if(gpio >= (GPIO_PORT_MAX + 1) * 32) {
  92. printk(KERN_ERR "imx_gpio: Attempt to request nonexistent GPIO %d for \"%s\"\n",
  93. gpio, label ? label : "?");
  94. return -EINVAL;
  95. }
  96. if(test_and_set_bit(gpio, imx_gpio_alloc_map)) {
  97. printk(KERN_ERR "imx_gpio: GPIO %d already used. Allocation for \"%s\" failed\n",
  98. gpio, label ? label : "?");
  99. return -EBUSY;
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(imx_gpio_request);
  104. void imx_gpio_free(unsigned gpio)
  105. {
  106. if(gpio >= (GPIO_PORT_MAX + 1) * 32)
  107. return;
  108. clear_bit(gpio, imx_gpio_alloc_map);
  109. }
  110. EXPORT_SYMBOL(imx_gpio_free);
  111. int imx_gpio_direction_input(unsigned gpio)
  112. {
  113. imx_gpio_mode(gpio | GPIO_IN | GPIO_GIUS | GPIO_DR);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(imx_gpio_direction_input);
  117. int imx_gpio_direction_output(unsigned gpio, int value)
  118. {
  119. imx_gpio_set_value(gpio, value);
  120. imx_gpio_mode(gpio | GPIO_OUT | GPIO_GIUS | GPIO_DR);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(imx_gpio_direction_output);
  124. int imx_gpio_setup_multiple_pins(const int *pin_list, unsigned count,
  125. int alloc_mode, const char *label)
  126. {
  127. const int *p = pin_list;
  128. int i;
  129. unsigned gpio;
  130. unsigned mode;
  131. for (i = 0; i < count; i++) {
  132. gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
  133. mode = *p & ~(GPIO_PIN_MASK | GPIO_PORT_MASK);
  134. if (gpio >= (GPIO_PORT_MAX + 1) * 32)
  135. goto setup_error;
  136. if (alloc_mode & IMX_GPIO_ALLOC_MODE_RELEASE)
  137. imx_gpio_free(gpio);
  138. else if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_NO_ALLOC))
  139. if (imx_gpio_request(gpio, label))
  140. if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
  141. goto setup_error;
  142. if (!(alloc_mode & (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY |
  143. IMX_GPIO_ALLOC_MODE_RELEASE)))
  144. imx_gpio_mode(gpio | mode);
  145. p++;
  146. }
  147. return 0;
  148. setup_error:
  149. if(alloc_mode & (IMX_GPIO_ALLOC_MODE_NO_ALLOC |
  150. IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
  151. return -EINVAL;
  152. while (p != pin_list) {
  153. p--;
  154. gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
  155. imx_gpio_free(gpio);
  156. }
  157. return -EINVAL;
  158. }
  159. EXPORT_SYMBOL(imx_gpio_setup_multiple_pins);
  160. void __imx_gpio_set_value(unsigned gpio, int value)
  161. {
  162. imx_gpio_set_value_inline(gpio, value);
  163. }
  164. EXPORT_SYMBOL(__imx_gpio_set_value);
  165. int imx_gpio_to_irq(unsigned gpio)
  166. {
  167. return IRQ_GPIOA(0) + gpio;
  168. }
  169. EXPORT_SYMBOL(imx_gpio_to_irq);
  170. int imx_irq_to_gpio(unsigned irq)
  171. {
  172. if (irq < IRQ_GPIOA(0))
  173. return -EINVAL;
  174. return irq - IRQ_GPIOA(0);
  175. }
  176. EXPORT_SYMBOL(imx_irq_to_gpio);
  177. static struct resource imx_mmc_resources[] = {
  178. [0] = {
  179. .start = 0x00214000,
  180. .end = 0x002140FF,
  181. .flags = IORESOURCE_MEM,
  182. },
  183. [1] = {
  184. .start = (SDHC_INT),
  185. .end = (SDHC_INT),
  186. .flags = IORESOURCE_IRQ,
  187. },
  188. };
  189. static u64 imxmmmc_dmamask = 0xffffffffUL;
  190. static struct platform_device imx_mmc_device = {
  191. .name = "imx-mmc",
  192. .id = 0,
  193. .dev = {
  194. .dma_mask = &imxmmmc_dmamask,
  195. .coherent_dma_mask = 0xffffffff,
  196. },
  197. .num_resources = ARRAY_SIZE(imx_mmc_resources),
  198. .resource = imx_mmc_resources,
  199. };
  200. void __init imx_set_mmc_info(struct imxmmc_platform_data *info)
  201. {
  202. imx_mmc_device.dev.platform_data = info;
  203. }
  204. static struct imx_fb_platform_data imx_fb_info;
  205. void __init set_imx_fb_info(struct imx_fb_platform_data *hard_imx_fb_info)
  206. {
  207. memcpy(&imx_fb_info,hard_imx_fb_info,sizeof(struct imx_fb_platform_data));
  208. }
  209. static struct resource imxfb_resources[] = {
  210. [0] = {
  211. .start = 0x00205000,
  212. .end = 0x002050FF,
  213. .flags = IORESOURCE_MEM,
  214. },
  215. [1] = {
  216. .start = LCDC_INT,
  217. .end = LCDC_INT,
  218. .flags = IORESOURCE_IRQ,
  219. },
  220. };
  221. static u64 fb_dma_mask = ~(u64)0;
  222. static struct platform_device imxfb_device = {
  223. .name = "imx-fb",
  224. .id = 0,
  225. .dev = {
  226. .platform_data = &imx_fb_info,
  227. .dma_mask = &fb_dma_mask,
  228. .coherent_dma_mask = 0xffffffff,
  229. },
  230. .num_resources = ARRAY_SIZE(imxfb_resources),
  231. .resource = imxfb_resources,
  232. };
  233. static struct platform_device *devices[] __initdata = {
  234. &imx_mmc_device,
  235. &imxfb_device,
  236. };
  237. static struct map_desc imx_io_desc[] __initdata = {
  238. {
  239. .virtual = IMX_IO_BASE,
  240. .pfn = __phys_to_pfn(IMX_IO_PHYS),
  241. .length = IMX_IO_SIZE,
  242. .type = MT_DEVICE
  243. }
  244. };
  245. void __init
  246. imx_map_io(void)
  247. {
  248. iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc));
  249. }
  250. static int __init imx_init(void)
  251. {
  252. return platform_add_devices(devices, ARRAY_SIZE(devices));
  253. }
  254. subsys_initcall(imx_init);