common.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <asm/system_info.h>
  28. #include <mach/hardware.h>
  29. #include <mach/platform.h>
  30. #include "common.h"
  31. /*
  32. * Returns the unique ID for the device
  33. */
  34. void lpc32xx_get_uid(u32 devid[4])
  35. {
  36. int i;
  37. for (i = 0; i < 4; i++)
  38. devid[i] = __raw_readl(LPC32XX_CLKPWR_DEVID(i << 2));
  39. }
  40. /*
  41. * Returns SYSCLK source
  42. * 0 = PLL397, 1 = main oscillator
  43. */
  44. int clk_is_sysclk_mainosc(void)
  45. {
  46. if ((__raw_readl(LPC32XX_CLKPWR_SYSCLK_CTRL) &
  47. LPC32XX_CLKPWR_SYSCTRL_SYSCLKMUX) == 0)
  48. return 1;
  49. return 0;
  50. }
  51. /*
  52. * System reset via the watchdog timer
  53. */
  54. static void lpc32xx_watchdog_reset(void)
  55. {
  56. /* Make sure WDT clocks are enabled */
  57. __raw_writel(LPC32XX_CLKPWR_PWMCLK_WDOG_EN,
  58. LPC32XX_CLKPWR_TIMER_CLK_CTRL);
  59. /* Instant assert of RESETOUT_N with pulse length 1mS */
  60. __raw_writel(13000, io_p2v(LPC32XX_WDTIM_BASE + 0x18));
  61. __raw_writel(0x70, io_p2v(LPC32XX_WDTIM_BASE + 0xC));
  62. }
  63. /*
  64. * Detects and returns IRAM size for the device variation
  65. */
  66. #define LPC32XX_IRAM_BANK_SIZE SZ_128K
  67. static u32 iram_size;
  68. u32 lpc32xx_return_iram_size(void)
  69. {
  70. if (iram_size == 0) {
  71. u32 savedval1, savedval2;
  72. void __iomem *iramptr1, *iramptr2;
  73. iramptr1 = io_p2v(LPC32XX_IRAM_BASE);
  74. iramptr2 = io_p2v(LPC32XX_IRAM_BASE + LPC32XX_IRAM_BANK_SIZE);
  75. savedval1 = __raw_readl(iramptr1);
  76. savedval2 = __raw_readl(iramptr2);
  77. if (savedval1 == savedval2) {
  78. __raw_writel(savedval2 + 1, iramptr2);
  79. if (__raw_readl(iramptr1) == savedval2 + 1)
  80. iram_size = LPC32XX_IRAM_BANK_SIZE;
  81. else
  82. iram_size = LPC32XX_IRAM_BANK_SIZE * 2;
  83. __raw_writel(savedval2, iramptr2);
  84. } else
  85. iram_size = LPC32XX_IRAM_BANK_SIZE * 2;
  86. }
  87. return iram_size;
  88. }
  89. EXPORT_SYMBOL_GPL(lpc32xx_return_iram_size);
  90. /*
  91. * Computes PLL rate from PLL register and input clock
  92. */
  93. u32 clk_check_pll_setup(u32 ifreq, struct clk_pll_setup *pllsetup)
  94. {
  95. u32 ilfreq, p, m, n, fcco, fref, cfreq;
  96. int mode;
  97. /*
  98. * PLL requirements
  99. * ifreq must be >= 1MHz and <= 20MHz
  100. * FCCO must be >= 156MHz and <= 320MHz
  101. * FREF must be >= 1MHz and <= 27MHz
  102. * Assume the passed input data is not valid
  103. */
  104. ilfreq = ifreq;
  105. m = pllsetup->pll_m;
  106. n = pllsetup->pll_n;
  107. p = pllsetup->pll_p;
  108. mode = (pllsetup->cco_bypass_b15 << 2) |
  109. (pllsetup->direct_output_b14 << 1) |
  110. pllsetup->fdbk_div_ctrl_b13;
  111. switch (mode) {
  112. case 0x0: /* Non-integer mode */
  113. cfreq = (m * ilfreq) / (2 * p * n);
  114. fcco = (m * ilfreq) / n;
  115. fref = ilfreq / n;
  116. break;
  117. case 0x1: /* integer mode */
  118. cfreq = (m * ilfreq) / n;
  119. fcco = (m * ilfreq) / (n * 2 * p);
  120. fref = ilfreq / n;
  121. break;
  122. case 0x2:
  123. case 0x3: /* Direct mode */
  124. cfreq = (m * ilfreq) / n;
  125. fcco = cfreq;
  126. fref = ilfreq / n;
  127. break;
  128. case 0x4:
  129. case 0x5: /* Bypass mode */
  130. cfreq = ilfreq / (2 * p);
  131. fcco = 156000000;
  132. fref = 1000000;
  133. break;
  134. case 0x6:
  135. case 0x7: /* Direct bypass mode */
  136. default:
  137. cfreq = ilfreq;
  138. fcco = 156000000;
  139. fref = 1000000;
  140. break;
  141. }
  142. if (fcco < 156000000 || fcco > 320000000)
  143. cfreq = 0;
  144. if (fref < 1000000 || fref > 27000000)
  145. cfreq = 0;
  146. return (u32) cfreq;
  147. }
  148. u32 clk_get_pclk_div(void)
  149. {
  150. return 1 + ((__raw_readl(LPC32XX_CLKPWR_HCLK_DIV) >> 2) & 0x1F);
  151. }
  152. static struct map_desc lpc32xx_io_desc[] __initdata = {
  153. {
  154. .virtual = (unsigned long)IO_ADDRESS(LPC32XX_AHB0_START),
  155. .pfn = __phys_to_pfn(LPC32XX_AHB0_START),
  156. .length = LPC32XX_AHB0_SIZE,
  157. .type = MT_DEVICE
  158. },
  159. {
  160. .virtual = (unsigned long)IO_ADDRESS(LPC32XX_AHB1_START),
  161. .pfn = __phys_to_pfn(LPC32XX_AHB1_START),
  162. .length = LPC32XX_AHB1_SIZE,
  163. .type = MT_DEVICE
  164. },
  165. {
  166. .virtual = (unsigned long)IO_ADDRESS(LPC32XX_FABAPB_START),
  167. .pfn = __phys_to_pfn(LPC32XX_FABAPB_START),
  168. .length = LPC32XX_FABAPB_SIZE,
  169. .type = MT_DEVICE
  170. },
  171. {
  172. .virtual = (unsigned long)IO_ADDRESS(LPC32XX_IRAM_BASE),
  173. .pfn = __phys_to_pfn(LPC32XX_IRAM_BASE),
  174. .length = (LPC32XX_IRAM_BANK_SIZE * 2),
  175. .type = MT_DEVICE
  176. },
  177. };
  178. void __init lpc32xx_map_io(void)
  179. {
  180. iotable_init(lpc32xx_io_desc, ARRAY_SIZE(lpc32xx_io_desc));
  181. }
  182. void lpc23xx_restart(enum reboot_mode mode, const char *cmd)
  183. {
  184. switch (mode) {
  185. case REBOOT_SOFT:
  186. case REBOOT_HARD:
  187. lpc32xx_watchdog_reset();
  188. break;
  189. default:
  190. /* Do nothing */
  191. break;
  192. }
  193. /* Wait for watchdog to reset system */
  194. while (1)
  195. ;
  196. }
  197. static int __init lpc32xx_check_uid(void)
  198. {
  199. u32 uid[4];
  200. lpc32xx_get_uid(uid);
  201. printk(KERN_INFO "LPC32XX unique ID: %08x%08x%08x%08x\n",
  202. uid[3], uid[2], uid[1], uid[0]);
  203. if (!system_serial_low && !system_serial_high) {
  204. system_serial_low = uid[0];
  205. system_serial_high = uid[1];
  206. }
  207. return 1;
  208. }
  209. arch_initcall(lpc32xx_check_uid);