generic.c 8.4 KB

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