common.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * arch/arm/mach-lpc32xx/common.c
  3. *
  4. * Author: Kevin Wells <kevin.wells@nxp.com>
  5. *
  6. * Copyright (C) 2010 NXP Semiconductors
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/err.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-pnx.h>
  25. #include <linux/io.h>
  26. #include <asm/mach/map.h>
  27. #include <mach/hardware.h>
  28. #include <mach/platform.h>
  29. #include "common.h"
  30. /*
  31. * Watchdog timer
  32. */
  33. static struct resource watchdog_resources[] = {
  34. [0] = {
  35. .start = LPC32XX_WDTIM_BASE,
  36. .end = LPC32XX_WDTIM_BASE + SZ_4K - 1,
  37. .flags = IORESOURCE_MEM,
  38. },
  39. };
  40. struct platform_device lpc32xx_watchdog_device = {
  41. .name = "pnx4008-watchdog",
  42. .id = -1,
  43. .num_resources = ARRAY_SIZE(watchdog_resources),
  44. .resource = watchdog_resources,
  45. };
  46. /*
  47. * I2C busses
  48. */
  49. static struct resource i2c0_resources[] = {
  50. [0] = {
  51. .start = LPC32XX_I2C1_BASE,
  52. .end = LPC32XX_I2C1_BASE + 0x100 - 1,
  53. .flags = IORESOURCE_MEM,
  54. },
  55. [1] = {
  56. .start = IRQ_LPC32XX_I2C_1,
  57. .end = IRQ_LPC32XX_I2C_1,
  58. .flags = IORESOURCE_IRQ,
  59. },
  60. };
  61. static struct resource i2c1_resources[] = {
  62. [0] = {
  63. .start = LPC32XX_I2C2_BASE,
  64. .end = LPC32XX_I2C2_BASE + 0x100 - 1,
  65. .flags = IORESOURCE_MEM,
  66. },
  67. [1] = {
  68. .start = IRQ_LPC32XX_I2C_2,
  69. .end = IRQ_LPC32XX_I2C_2,
  70. .flags = IORESOURCE_IRQ,
  71. },
  72. };
  73. static struct resource i2c2_resources[] = {
  74. [0] = {
  75. .start = LPC32XX_OTG_I2C_BASE,
  76. .end = LPC32XX_OTG_I2C_BASE + 0x100 - 1,
  77. .flags = IORESOURCE_MEM,
  78. },
  79. [1] = {
  80. .start = IRQ_LPC32XX_USB_I2C,
  81. .end = IRQ_LPC32XX_USB_I2C,
  82. .flags = IORESOURCE_IRQ,
  83. },
  84. };
  85. struct platform_device lpc32xx_i2c0_device = {
  86. .name = "pnx-i2c.0",
  87. .id = 0,
  88. .num_resources = ARRAY_SIZE(i2c0_resources),
  89. .resource = i2c0_resources,
  90. };
  91. struct platform_device lpc32xx_i2c1_device = {
  92. .name = "pnx-i2c.1",
  93. .id = 1,
  94. .num_resources = ARRAY_SIZE(i2c1_resources),
  95. .resource = i2c1_resources,
  96. };
  97. struct platform_device lpc32xx_i2c2_device = {
  98. .name = "pnx-i2c.2",
  99. .id = 2,
  100. .num_resources = ARRAY_SIZE(i2c2_resources),
  101. .resource = i2c2_resources,
  102. };
  103. /* TSC (Touch Screen Controller) */
  104. static struct resource lpc32xx_tsc_resources[] = {
  105. {
  106. .start = LPC32XX_ADC_BASE,
  107. .end = LPC32XX_ADC_BASE + SZ_4K - 1,
  108. .flags = IORESOURCE_MEM,
  109. }, {
  110. .start = IRQ_LPC32XX_TS_IRQ,
  111. .end = IRQ_LPC32XX_TS_IRQ,
  112. .flags = IORESOURCE_IRQ,
  113. },
  114. };
  115. struct platform_device lpc32xx_tsc_device = {
  116. .name = "ts-lpc32xx",
  117. .id = -1,
  118. .num_resources = ARRAY_SIZE(lpc32xx_tsc_resources),
  119. .resource = lpc32xx_tsc_resources,
  120. };
  121. /* RTC */
  122. static struct resource lpc32xx_rtc_resources[] = {
  123. {
  124. .start = LPC32XX_RTC_BASE,
  125. .end = LPC32XX_RTC_BASE + SZ_4K - 1,
  126. .flags = IORESOURCE_MEM,
  127. },{
  128. .start = IRQ_LPC32XX_RTC,
  129. .end = IRQ_LPC32XX_RTC,
  130. .flags = IORESOURCE_IRQ,
  131. },
  132. };
  133. struct platform_device lpc32xx_rtc_device = {
  134. .name = "rtc-lpc32xx",
  135. .id = -1,
  136. .num_resources = ARRAY_SIZE(lpc32xx_rtc_resources),
  137. .resource = lpc32xx_rtc_resources,
  138. };
  139. /*
  140. * ADC support
  141. */
  142. static struct resource adc_resources[] = {
  143. {
  144. .start = LPC32XX_ADC_BASE,
  145. .end = LPC32XX_ADC_BASE + SZ_4K - 1,
  146. .flags = IORESOURCE_MEM,
  147. }, {
  148. .start = IRQ_LPC32XX_TS_IRQ,
  149. .end = IRQ_LPC32XX_TS_IRQ,
  150. .flags = IORESOURCE_IRQ,
  151. },
  152. };
  153. struct platform_device lpc32xx_adc_device = {
  154. .name = "lpc32xx-adc",
  155. .id = -1,
  156. .num_resources = ARRAY_SIZE(adc_resources),
  157. .resource = adc_resources,
  158. };
  159. /*
  160. * USB support
  161. */
  162. /* The dmamask must be set for OHCI to work */
  163. static u64 ohci_dmamask = ~(u32) 0;
  164. static struct resource ohci_resources[] = {
  165. {
  166. .start = IO_ADDRESS(LPC32XX_USB_BASE),
  167. .end = IO_ADDRESS(LPC32XX_USB_BASE + 0x100 - 1),
  168. .flags = IORESOURCE_MEM,
  169. }, {
  170. .start = IRQ_LPC32XX_USB_HOST,
  171. .flags = IORESOURCE_IRQ,
  172. },
  173. };
  174. struct platform_device lpc32xx_ohci_device = {
  175. .name = "usb-ohci",
  176. .id = -1,
  177. .dev = {
  178. .dma_mask = &ohci_dmamask,
  179. .coherent_dma_mask = 0xFFFFFFFF,
  180. },
  181. .num_resources = ARRAY_SIZE(ohci_resources),
  182. .resource = ohci_resources,
  183. };
  184. /*
  185. * Network Support
  186. */
  187. static struct resource net_resources[] = {
  188. [0] = DEFINE_RES_MEM(LPC32XX_ETHERNET_BASE, SZ_4K),
  189. [1] = DEFINE_RES_MEM(LPC32XX_IRAM_BASE, SZ_128K),
  190. [2] = DEFINE_RES_IRQ(IRQ_LPC32XX_ETHERNET),
  191. };
  192. static u64 lpc32xx_mac_dma_mask = 0xffffffffUL;
  193. struct platform_device lpc32xx_net_device = {
  194. .name = "lpc-eth",
  195. .id = 0,
  196. .dev = {
  197. .dma_mask = &lpc32xx_mac_dma_mask,
  198. .coherent_dma_mask = 0xffffffffUL,
  199. },
  200. .num_resources = ARRAY_SIZE(net_resources),
  201. .resource = net_resources,
  202. };
  203. /*
  204. * Returns the unique ID for the device
  205. */
  206. void lpc32xx_get_uid(u32 devid[4])
  207. {
  208. int i;
  209. for (i = 0; i < 4; i++)
  210. devid[i] = __raw_readl(LPC32XX_CLKPWR_DEVID(i << 2));
  211. }
  212. /*
  213. * Returns SYSCLK source
  214. * 0 = PLL397, 1 = main oscillator
  215. */
  216. int clk_is_sysclk_mainosc(void)
  217. {
  218. if ((__raw_readl(LPC32XX_CLKPWR_SYSCLK_CTRL) &
  219. LPC32XX_CLKPWR_SYSCTRL_SYSCLKMUX) == 0)
  220. return 1;
  221. return 0;
  222. }
  223. /*
  224. * System reset via the watchdog timer
  225. */
  226. static void lpc32xx_watchdog_reset(void)
  227. {
  228. /* Make sure WDT clocks are enabled */
  229. __raw_writel(LPC32XX_CLKPWR_PWMCLK_WDOG_EN,
  230. LPC32XX_CLKPWR_TIMER_CLK_CTRL);
  231. /* Instant assert of RESETOUT_N with pulse length 1mS */
  232. __raw_writel(13000, io_p2v(LPC32XX_WDTIM_BASE + 0x18));
  233. __raw_writel(0x70, io_p2v(LPC32XX_WDTIM_BASE + 0xC));
  234. }
  235. /*
  236. * Detects and returns IRAM size for the device variation
  237. */
  238. #define LPC32XX_IRAM_BANK_SIZE SZ_128K
  239. static u32 iram_size;
  240. u32 lpc32xx_return_iram_size(void)
  241. {
  242. if (iram_size == 0) {
  243. u32 savedval1, savedval2;
  244. void __iomem *iramptr1, *iramptr2;
  245. iramptr1 = io_p2v(LPC32XX_IRAM_BASE);
  246. iramptr2 = io_p2v(LPC32XX_IRAM_BASE + LPC32XX_IRAM_BANK_SIZE);
  247. savedval1 = __raw_readl(iramptr1);
  248. savedval2 = __raw_readl(iramptr2);
  249. if (savedval1 == savedval2) {
  250. __raw_writel(savedval2 + 1, iramptr2);
  251. if (__raw_readl(iramptr1) == savedval2 + 1)
  252. iram_size = LPC32XX_IRAM_BANK_SIZE;
  253. else
  254. iram_size = LPC32XX_IRAM_BANK_SIZE * 2;
  255. __raw_writel(savedval2, iramptr2);
  256. } else
  257. iram_size = LPC32XX_IRAM_BANK_SIZE * 2;
  258. }
  259. return iram_size;
  260. }
  261. /*
  262. * Computes PLL rate from PLL register and input clock
  263. */
  264. u32 clk_check_pll_setup(u32 ifreq, struct clk_pll_setup *pllsetup)
  265. {
  266. u32 ilfreq, p, m, n, fcco, fref, cfreq;
  267. int mode;
  268. /*
  269. * PLL requirements
  270. * ifreq must be >= 1MHz and <= 20MHz
  271. * FCCO must be >= 156MHz and <= 320MHz
  272. * FREF must be >= 1MHz and <= 27MHz
  273. * Assume the passed input data is not valid
  274. */
  275. ilfreq = ifreq;
  276. m = pllsetup->pll_m;
  277. n = pllsetup->pll_n;
  278. p = pllsetup->pll_p;
  279. mode = (pllsetup->cco_bypass_b15 << 2) |
  280. (pllsetup->direct_output_b14 << 1) |
  281. pllsetup->fdbk_div_ctrl_b13;
  282. switch (mode) {
  283. case 0x0: /* Non-integer mode */
  284. cfreq = (m * ilfreq) / (2 * p * n);
  285. fcco = (m * ilfreq) / n;
  286. fref = ilfreq / n;
  287. break;
  288. case 0x1: /* integer mode */
  289. cfreq = (m * ilfreq) / n;
  290. fcco = (m * ilfreq) / (n * 2 * p);
  291. fref = ilfreq / n;
  292. break;
  293. case 0x2:
  294. case 0x3: /* Direct mode */
  295. cfreq = (m * ilfreq) / n;
  296. fcco = cfreq;
  297. fref = ilfreq / n;
  298. break;
  299. case 0x4:
  300. case 0x5: /* Bypass mode */
  301. cfreq = ilfreq / (2 * p);
  302. fcco = 156000000;
  303. fref = 1000000;
  304. break;
  305. case 0x6:
  306. case 0x7: /* Direct bypass mode */
  307. default:
  308. cfreq = ilfreq;
  309. fcco = 156000000;
  310. fref = 1000000;
  311. break;
  312. }
  313. if (fcco < 156000000 || fcco > 320000000)
  314. cfreq = 0;
  315. if (fref < 1000000 || fref > 27000000)
  316. cfreq = 0;
  317. return (u32) cfreq;
  318. }
  319. u32 clk_get_pclk_div(void)
  320. {
  321. return 1 + ((__raw_readl(LPC32XX_CLKPWR_HCLK_DIV) >> 2) & 0x1F);
  322. }
  323. static struct map_desc lpc32xx_io_desc[] __initdata = {
  324. {
  325. .virtual = IO_ADDRESS(LPC32XX_AHB0_START),
  326. .pfn = __phys_to_pfn(LPC32XX_AHB0_START),
  327. .length = LPC32XX_AHB0_SIZE,
  328. .type = MT_DEVICE
  329. },
  330. {
  331. .virtual = IO_ADDRESS(LPC32XX_AHB1_START),
  332. .pfn = __phys_to_pfn(LPC32XX_AHB1_START),
  333. .length = LPC32XX_AHB1_SIZE,
  334. .type = MT_DEVICE
  335. },
  336. {
  337. .virtual = IO_ADDRESS(LPC32XX_FABAPB_START),
  338. .pfn = __phys_to_pfn(LPC32XX_FABAPB_START),
  339. .length = LPC32XX_FABAPB_SIZE,
  340. .type = MT_DEVICE
  341. },
  342. {
  343. .virtual = IO_ADDRESS(LPC32XX_IRAM_BASE),
  344. .pfn = __phys_to_pfn(LPC32XX_IRAM_BASE),
  345. .length = (LPC32XX_IRAM_BANK_SIZE * 2),
  346. .type = MT_DEVICE
  347. },
  348. };
  349. void __init lpc32xx_map_io(void)
  350. {
  351. iotable_init(lpc32xx_io_desc, ARRAY_SIZE(lpc32xx_io_desc));
  352. }
  353. void lpc23xx_restart(char mode, const char *cmd)
  354. {
  355. switch (mode) {
  356. case 's':
  357. case 'h':
  358. lpc32xx_watchdog_reset();
  359. break;
  360. default:
  361. /* Do nothing */
  362. break;
  363. }
  364. /* Wait for watchdog to reset system */
  365. while (1)
  366. ;
  367. }