generic.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/hardware.h>
  32. #include <mach/imx-regs.h>
  33. #include <asm/mach/map.h>
  34. #include <mach/mmc.h>
  35. #include <mach/gpio.h>
  36. unsigned long imx_gpio_alloc_map[(GPIO_PORT_MAX + 1) * 32 / BITS_PER_LONG];
  37. void imx_gpio_mode(int gpio_mode)
  38. {
  39. unsigned int pin = gpio_mode & GPIO_PIN_MASK;
  40. unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
  41. unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
  42. unsigned int tmp;
  43. /* Pullup enable */
  44. if(gpio_mode & GPIO_PUEN)
  45. PUEN(port) |= (1<<pin);
  46. else
  47. PUEN(port) &= ~(1<<pin);
  48. /* Data direction */
  49. if(gpio_mode & GPIO_OUT)
  50. DDIR(port) |= 1<<pin;
  51. else
  52. DDIR(port) &= ~(1<<pin);
  53. /* Primary / alternate function */
  54. if(gpio_mode & GPIO_AF)
  55. GPR(port) |= (1<<pin);
  56. else
  57. GPR(port) &= ~(1<<pin);
  58. /* use as gpio? */
  59. if(gpio_mode & GPIO_GIUS)
  60. GIUS(port) |= (1<<pin);
  61. else
  62. GIUS(port) &= ~(1<<pin);
  63. /* Output / input configuration */
  64. /* FIXME: I'm not very sure about OCR and ICONF, someone
  65. * should have a look over it
  66. */
  67. if(pin<16) {
  68. tmp = OCR1(port);
  69. tmp &= ~( 3<<(pin*2));
  70. tmp |= (ocr << (pin*2));
  71. OCR1(port) = tmp;
  72. ICONFA1(port) &= ~( 3<<(pin*2));
  73. ICONFA1(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2);
  74. ICONFB1(port) &= ~( 3<<(pin*2));
  75. ICONFB1(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2);
  76. } else {
  77. tmp = OCR2(port);
  78. tmp &= ~( 3<<((pin-16)*2));
  79. tmp |= (ocr << ((pin-16)*2));
  80. OCR2(port) = tmp;
  81. ICONFA2(port) &= ~( 3<<((pin-16)*2));
  82. ICONFA2(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << ((pin-16) * 2);
  83. ICONFB2(port) &= ~( 3<<((pin-16)*2));
  84. ICONFB2(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << ((pin-16) * 2);
  85. }
  86. }
  87. EXPORT_SYMBOL(imx_gpio_mode);
  88. int imx_gpio_request(unsigned gpio, const char *label)
  89. {
  90. if(gpio >= (GPIO_PORT_MAX + 1) * 32) {
  91. printk(KERN_ERR "imx_gpio: Attempt to request nonexistent GPIO %d for \"%s\"\n",
  92. gpio, label ? label : "?");
  93. return -EINVAL;
  94. }
  95. if(test_and_set_bit(gpio, imx_gpio_alloc_map)) {
  96. printk(KERN_ERR "imx_gpio: GPIO %d already used. Allocation for \"%s\" failed\n",
  97. gpio, label ? label : "?");
  98. return -EBUSY;
  99. }
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(imx_gpio_request);
  103. void imx_gpio_free(unsigned gpio)
  104. {
  105. if(gpio >= (GPIO_PORT_MAX + 1) * 32)
  106. return;
  107. clear_bit(gpio, imx_gpio_alloc_map);
  108. }
  109. EXPORT_SYMBOL(imx_gpio_free);
  110. int imx_gpio_direction_input(unsigned gpio)
  111. {
  112. imx_gpio_mode(gpio | GPIO_IN | GPIO_GIUS | GPIO_DR);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(imx_gpio_direction_input);
  116. int imx_gpio_direction_output(unsigned gpio, int value)
  117. {
  118. imx_gpio_set_value(gpio, value);
  119. imx_gpio_mode(gpio | GPIO_OUT | GPIO_GIUS | GPIO_DR);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(imx_gpio_direction_output);
  123. int imx_gpio_setup_multiple_pins(const int *pin_list, unsigned count,
  124. int alloc_mode, const char *label)
  125. {
  126. const int *p = pin_list;
  127. int i;
  128. unsigned gpio;
  129. unsigned mode;
  130. for (i = 0; i < count; i++) {
  131. gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
  132. mode = *p & ~(GPIO_PIN_MASK | GPIO_PORT_MASK);
  133. if (gpio >= (GPIO_PORT_MAX + 1) * 32)
  134. goto setup_error;
  135. if (alloc_mode & IMX_GPIO_ALLOC_MODE_RELEASE)
  136. imx_gpio_free(gpio);
  137. else if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_NO_ALLOC))
  138. if (imx_gpio_request(gpio, label))
  139. if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
  140. goto setup_error;
  141. if (!(alloc_mode & (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY |
  142. IMX_GPIO_ALLOC_MODE_RELEASE)))
  143. imx_gpio_mode(gpio | mode);
  144. p++;
  145. }
  146. return 0;
  147. setup_error:
  148. if(alloc_mode & (IMX_GPIO_ALLOC_MODE_NO_ALLOC |
  149. IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
  150. return -EINVAL;
  151. while (p != pin_list) {
  152. p--;
  153. gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
  154. imx_gpio_free(gpio);
  155. }
  156. return -EINVAL;
  157. }
  158. EXPORT_SYMBOL(imx_gpio_setup_multiple_pins);
  159. void __imx_gpio_set_value(unsigned gpio, int value)
  160. {
  161. imx_gpio_set_value_inline(gpio, value);
  162. }
  163. EXPORT_SYMBOL(__imx_gpio_set_value);
  164. int imx_gpio_to_irq(unsigned gpio)
  165. {
  166. return IRQ_GPIOA(0) + gpio;
  167. }
  168. EXPORT_SYMBOL(imx_gpio_to_irq);
  169. int imx_irq_to_gpio(unsigned irq)
  170. {
  171. if (irq < IRQ_GPIOA(0))
  172. return -EINVAL;
  173. return irq - IRQ_GPIOA(0);
  174. }
  175. EXPORT_SYMBOL(imx_irq_to_gpio);
  176. static struct resource imx_mmc_resources[] = {
  177. [0] = {
  178. .start = 0x00214000,
  179. .end = 0x002140FF,
  180. .flags = IORESOURCE_MEM,
  181. },
  182. [1] = {
  183. .start = (SDHC_INT),
  184. .end = (SDHC_INT),
  185. .flags = IORESOURCE_IRQ,
  186. },
  187. };
  188. static u64 imxmmmc_dmamask = 0xffffffffUL;
  189. static struct platform_device imx_mmc_device = {
  190. .name = "imx-mmc",
  191. .id = 0,
  192. .dev = {
  193. .dma_mask = &imxmmmc_dmamask,
  194. .coherent_dma_mask = 0xffffffff,
  195. },
  196. .num_resources = ARRAY_SIZE(imx_mmc_resources),
  197. .resource = imx_mmc_resources,
  198. };
  199. void __init imx_set_mmc_info(struct imxmmc_platform_data *info)
  200. {
  201. imx_mmc_device.dev.platform_data = info;
  202. }
  203. static struct platform_device *devices[] __initdata = {
  204. &imx_mmc_device,
  205. };
  206. static struct map_desc imx_io_desc[] __initdata = {
  207. {
  208. .virtual = IMX_IO_BASE,
  209. .pfn = __phys_to_pfn(IMX_IO_PHYS),
  210. .length = IMX_IO_SIZE,
  211. .type = MT_DEVICE
  212. }
  213. };
  214. void __init
  215. imx_map_io(void)
  216. {
  217. iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc));
  218. }
  219. static int __init imx_init(void)
  220. {
  221. return platform_add_devices(devices, ARRAY_SIZE(devices));
  222. }
  223. subsys_initcall(imx_init);