generic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <asm/arch/imxfb.h>
  32. #include <asm/hardware.h>
  33. #include <asm/arch/imx-regs.h>
  34. #include <asm/mach/map.h>
  35. #include <asm/arch/mmc.h>
  36. #include <asm/arch/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. /*
  178. * get the system pll clock in Hz
  179. *
  180. * mfi + mfn / (mfd +1)
  181. * f = 2 * f_ref * --------------------
  182. * pd + 1
  183. */
  184. static unsigned int imx_decode_pll(unsigned int pll, u32 f_ref)
  185. {
  186. unsigned long long ll;
  187. unsigned long quot;
  188. u32 mfi = (pll >> 10) & 0xf;
  189. u32 mfn = pll & 0x3ff;
  190. u32 mfd = (pll >> 16) & 0x3ff;
  191. u32 pd = (pll >> 26) & 0xf;
  192. mfi = mfi <= 5 ? 5 : mfi;
  193. ll = 2 * (unsigned long long)f_ref * ( (mfi<<16) + (mfn<<16) / (mfd+1) );
  194. quot = (pd+1) * (1<<16);
  195. ll += quot / 2;
  196. do_div(ll, quot);
  197. return (unsigned int) ll;
  198. }
  199. unsigned int imx_get_system_clk(void)
  200. {
  201. u32 f_ref = (CSCR & CSCR_SYSTEM_SEL) ? 16000000 : (CLK32 * 512);
  202. return imx_decode_pll(SPCTL0, f_ref);
  203. }
  204. EXPORT_SYMBOL(imx_get_system_clk);
  205. unsigned int imx_get_mcu_clk(void)
  206. {
  207. return imx_decode_pll(MPCTL0, CLK32 * 512);
  208. }
  209. EXPORT_SYMBOL(imx_get_mcu_clk);
  210. /*
  211. * get peripheral clock 1 ( UART[12], Timer[12], PWM )
  212. */
  213. unsigned int imx_get_perclk1(void)
  214. {
  215. return imx_get_system_clk() / (((PCDR) & 0xf)+1);
  216. }
  217. EXPORT_SYMBOL(imx_get_perclk1);
  218. /*
  219. * get peripheral clock 2 ( LCD, SD, SPI[12] )
  220. */
  221. unsigned int imx_get_perclk2(void)
  222. {
  223. return imx_get_system_clk() / (((PCDR>>4) & 0xf)+1);
  224. }
  225. EXPORT_SYMBOL(imx_get_perclk2);
  226. /*
  227. * get peripheral clock 3 ( SSI )
  228. */
  229. unsigned int imx_get_perclk3(void)
  230. {
  231. return imx_get_system_clk() / (((PCDR>>16) & 0x7f)+1);
  232. }
  233. EXPORT_SYMBOL(imx_get_perclk3);
  234. /*
  235. * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
  236. */
  237. unsigned int imx_get_hclk(void)
  238. {
  239. return imx_get_system_clk() / (((CSCR>>10) & 0xf)+1);
  240. }
  241. EXPORT_SYMBOL(imx_get_hclk);
  242. static struct resource imx_mmc_resources[] = {
  243. [0] = {
  244. .start = 0x00214000,
  245. .end = 0x002140FF,
  246. .flags = IORESOURCE_MEM,
  247. },
  248. [1] = {
  249. .start = (SDHC_INT),
  250. .end = (SDHC_INT),
  251. .flags = IORESOURCE_IRQ,
  252. },
  253. };
  254. static u64 imxmmmc_dmamask = 0xffffffffUL;
  255. static struct platform_device imx_mmc_device = {
  256. .name = "imx-mmc",
  257. .id = 0,
  258. .dev = {
  259. .dma_mask = &imxmmmc_dmamask,
  260. .coherent_dma_mask = 0xffffffff,
  261. },
  262. .num_resources = ARRAY_SIZE(imx_mmc_resources),
  263. .resource = imx_mmc_resources,
  264. };
  265. void __init imx_set_mmc_info(struct imxmmc_platform_data *info)
  266. {
  267. imx_mmc_device.dev.platform_data = info;
  268. }
  269. static struct imxfb_mach_info imx_fb_info;
  270. void __init set_imx_fb_info(struct imxfb_mach_info *hard_imx_fb_info)
  271. {
  272. memcpy(&imx_fb_info,hard_imx_fb_info,sizeof(struct imxfb_mach_info));
  273. }
  274. EXPORT_SYMBOL(set_imx_fb_info);
  275. static struct resource imxfb_resources[] = {
  276. [0] = {
  277. .start = 0x00205000,
  278. .end = 0x002050FF,
  279. .flags = IORESOURCE_MEM,
  280. },
  281. [1] = {
  282. .start = LCDC_INT,
  283. .end = LCDC_INT,
  284. .flags = IORESOURCE_IRQ,
  285. },
  286. };
  287. static u64 fb_dma_mask = ~(u64)0;
  288. static struct platform_device imxfb_device = {
  289. .name = "imx-fb",
  290. .id = 0,
  291. .dev = {
  292. .platform_data = &imx_fb_info,
  293. .dma_mask = &fb_dma_mask,
  294. .coherent_dma_mask = 0xffffffff,
  295. },
  296. .num_resources = ARRAY_SIZE(imxfb_resources),
  297. .resource = imxfb_resources,
  298. };
  299. static struct platform_device *devices[] __initdata = {
  300. &imx_mmc_device,
  301. &imxfb_device,
  302. };
  303. static struct map_desc imx_io_desc[] __initdata = {
  304. {
  305. .virtual = IMX_IO_BASE,
  306. .pfn = __phys_to_pfn(IMX_IO_PHYS),
  307. .length = IMX_IO_SIZE,
  308. .type = MT_DEVICE
  309. }
  310. };
  311. void __init
  312. imx_map_io(void)
  313. {
  314. iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc));
  315. }
  316. static int __init imx_init(void)
  317. {
  318. return platform_add_devices(devices, ARRAY_SIZE(devices));
  319. }
  320. subsys_initcall(imx_init);