common.c 8.3 KB

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