mpc52xx_common.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. *
  3. * Utility functions for the Freescale MPC52xx.
  4. *
  5. * Copyright (C) 2006 Sylvain Munaut <tnt@246tNt.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. *
  11. */
  12. #undef DEBUG
  13. #include <linux/gpio.h>
  14. #include <linux/kernel.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/export.h>
  19. #include <asm/io.h>
  20. #include <asm/prom.h>
  21. #include <asm/mpc52xx.h>
  22. /* MPC5200 device tree match tables */
  23. static const struct of_device_id mpc52xx_xlb_ids[] __initconst = {
  24. { .compatible = "fsl,mpc5200-xlb", },
  25. { .compatible = "mpc5200-xlb", },
  26. {}
  27. };
  28. static const struct of_device_id mpc52xx_bus_ids[] __initconst = {
  29. { .compatible = "fsl,mpc5200-immr", },
  30. { .compatible = "fsl,mpc5200b-immr", },
  31. { .compatible = "simple-bus", },
  32. /* depreciated matches; shouldn't be used in new device trees */
  33. { .compatible = "fsl,lpb", },
  34. { .type = "builtin", .compatible = "mpc5200", }, /* efika */
  35. { .type = "soc", .compatible = "mpc5200", }, /* lite5200 */
  36. {}
  37. };
  38. /*
  39. * This variable is mapped in mpc52xx_map_wdt() and used in mpc52xx_restart().
  40. * Permanent mapping is required because mpc52xx_restart() can be called
  41. * from interrupt context while node mapping (which calls ioremap())
  42. * cannot be used at such point.
  43. */
  44. static DEFINE_SPINLOCK(mpc52xx_lock);
  45. static struct mpc52xx_gpt __iomem *mpc52xx_wdt;
  46. static struct mpc52xx_cdm __iomem *mpc52xx_cdm;
  47. /*
  48. * Configure the XLB arbiter settings to match what Linux expects.
  49. */
  50. void __init
  51. mpc5200_setup_xlb_arbiter(void)
  52. {
  53. struct device_node *np;
  54. struct mpc52xx_xlb __iomem *xlb;
  55. np = of_find_matching_node(NULL, mpc52xx_xlb_ids);
  56. xlb = of_iomap(np, 0);
  57. of_node_put(np);
  58. if (!xlb) {
  59. printk(KERN_ERR __FILE__ ": "
  60. "Error mapping XLB in mpc52xx_setup_cpu(). "
  61. "Expect some abnormal behavior\n");
  62. return;
  63. }
  64. /* Configure the XLB Arbiter priorities */
  65. out_be32(&xlb->master_pri_enable, 0xff);
  66. out_be32(&xlb->master_priority, 0x11111111);
  67. /*
  68. * Disable XLB pipelining
  69. * (cfr errate 292. We could do this only just before ATA PIO
  70. * transaction and re-enable it afterwards ...)
  71. * Not needed on MPC5200B.
  72. */
  73. if ((mfspr(SPRN_SVR) & MPC5200_SVR_MASK) == MPC5200_SVR)
  74. out_be32(&xlb->config, in_be32(&xlb->config) | MPC52xx_XLB_CFG_PLDIS);
  75. iounmap(xlb);
  76. }
  77. /*
  78. * This variable is mapped in mpc52xx_map_common_devices and
  79. * used in mpc5200_psc_ac97_gpio_reset().
  80. */
  81. static DEFINE_SPINLOCK(gpio_lock);
  82. struct mpc52xx_gpio __iomem *simple_gpio;
  83. struct mpc52xx_gpio_wkup __iomem *wkup_gpio;
  84. /**
  85. * mpc52xx_declare_of_platform_devices: register internal devices and children
  86. * of the localplus bus to the of_platform
  87. * bus.
  88. */
  89. void __init mpc52xx_declare_of_platform_devices(void)
  90. {
  91. /* Find all the 'platform' devices and register them. */
  92. if (of_platform_populate(NULL, mpc52xx_bus_ids, NULL, NULL))
  93. pr_err(__FILE__ ": Error while populating devices from DT\n");
  94. }
  95. /*
  96. * match tables used by mpc52xx_map_common_devices()
  97. */
  98. static const struct of_device_id mpc52xx_gpt_ids[] __initconst = {
  99. { .compatible = "fsl,mpc5200-gpt", },
  100. { .compatible = "mpc5200-gpt", }, /* old */
  101. {}
  102. };
  103. static const struct of_device_id mpc52xx_cdm_ids[] __initconst = {
  104. { .compatible = "fsl,mpc5200-cdm", },
  105. { .compatible = "mpc5200-cdm", }, /* old */
  106. {}
  107. };
  108. static const struct of_device_id mpc52xx_gpio_simple[] __initconst = {
  109. { .compatible = "fsl,mpc5200-gpio", },
  110. {}
  111. };
  112. static const struct of_device_id mpc52xx_gpio_wkup[] __initconst = {
  113. { .compatible = "fsl,mpc5200-gpio-wkup", },
  114. {}
  115. };
  116. /**
  117. * mpc52xx_map_common_devices: iomap devices required by common code
  118. */
  119. void __init
  120. mpc52xx_map_common_devices(void)
  121. {
  122. struct device_node *np;
  123. /* mpc52xx_wdt is mapped here and used in mpc52xx_restart,
  124. * possibly from a interrupt context. wdt is only implement
  125. * on a gpt0, so check has-wdt property before mapping.
  126. */
  127. for_each_matching_node(np, mpc52xx_gpt_ids) {
  128. if (of_get_property(np, "fsl,has-wdt", NULL) ||
  129. of_get_property(np, "has-wdt", NULL)) {
  130. mpc52xx_wdt = of_iomap(np, 0);
  131. of_node_put(np);
  132. break;
  133. }
  134. }
  135. /* Clock Distribution Module, used by PSC clock setting function */
  136. np = of_find_matching_node(NULL, mpc52xx_cdm_ids);
  137. mpc52xx_cdm = of_iomap(np, 0);
  138. of_node_put(np);
  139. /* simple_gpio registers */
  140. np = of_find_matching_node(NULL, mpc52xx_gpio_simple);
  141. simple_gpio = of_iomap(np, 0);
  142. of_node_put(np);
  143. /* wkup_gpio registers */
  144. np = of_find_matching_node(NULL, mpc52xx_gpio_wkup);
  145. wkup_gpio = of_iomap(np, 0);
  146. of_node_put(np);
  147. }
  148. /**
  149. * mpc52xx_set_psc_clkdiv: Set clock divider in the CDM for PSC ports
  150. *
  151. * @psc_id: id of psc port; must be 1,2,3 or 6
  152. * @clkdiv: clock divider value to put into CDM PSC register.
  153. */
  154. int mpc52xx_set_psc_clkdiv(int psc_id, int clkdiv)
  155. {
  156. unsigned long flags;
  157. u16 __iomem *reg;
  158. u32 val;
  159. u32 mask;
  160. u32 mclken_div;
  161. if (!mpc52xx_cdm)
  162. return -ENODEV;
  163. mclken_div = 0x8000 | (clkdiv & 0x1FF);
  164. switch (psc_id) {
  165. case 1: reg = &mpc52xx_cdm->mclken_div_psc1; mask = 0x20; break;
  166. case 2: reg = &mpc52xx_cdm->mclken_div_psc2; mask = 0x40; break;
  167. case 3: reg = &mpc52xx_cdm->mclken_div_psc3; mask = 0x80; break;
  168. case 6: reg = &mpc52xx_cdm->mclken_div_psc6; mask = 0x10; break;
  169. default:
  170. return -ENODEV;
  171. }
  172. /* Set the rate and enable the clock */
  173. spin_lock_irqsave(&mpc52xx_lock, flags);
  174. out_be16(reg, mclken_div);
  175. val = in_be32(&mpc52xx_cdm->clk_enables);
  176. out_be32(&mpc52xx_cdm->clk_enables, val | mask);
  177. spin_unlock_irqrestore(&mpc52xx_lock, flags);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(mpc52xx_set_psc_clkdiv);
  181. /**
  182. * mpc52xx_get_xtal_freq - Get SYS_XTAL_IN frequency for a device
  183. *
  184. * @node: device node
  185. *
  186. * Returns the frequency of the external oscillator clock connected
  187. * to the SYS_XTAL_IN pin, or 0 if it cannot be determined.
  188. */
  189. unsigned int mpc52xx_get_xtal_freq(struct device_node *node)
  190. {
  191. u32 val;
  192. unsigned int freq;
  193. if (!mpc52xx_cdm)
  194. return 0;
  195. freq = mpc5xxx_get_bus_frequency(node);
  196. if (!freq)
  197. return 0;
  198. if (in_8(&mpc52xx_cdm->ipb_clk_sel) & 0x1)
  199. freq *= 2;
  200. val = in_be32(&mpc52xx_cdm->rstcfg);
  201. if (val & (1 << 5))
  202. freq *= 8;
  203. else
  204. freq *= 4;
  205. if (val & (1 << 6))
  206. freq /= 12;
  207. else
  208. freq /= 16;
  209. return freq;
  210. }
  211. EXPORT_SYMBOL(mpc52xx_get_xtal_freq);
  212. /**
  213. * mpc52xx_restart: ppc_md->restart hook for mpc5200 using the watchdog timer
  214. */
  215. void
  216. mpc52xx_restart(char *cmd)
  217. {
  218. local_irq_disable();
  219. /* Turn on the watchdog and wait for it to expire.
  220. * It effectively does a reset. */
  221. if (mpc52xx_wdt) {
  222. out_be32(&mpc52xx_wdt->mode, 0x00000000);
  223. out_be32(&mpc52xx_wdt->count, 0x000000ff);
  224. out_be32(&mpc52xx_wdt->mode, 0x00009004);
  225. } else
  226. printk(KERN_ERR __FILE__ ": "
  227. "mpc52xx_restart: Can't access wdt. "
  228. "Restart impossible, system halted.\n");
  229. while (1);
  230. }
  231. #define PSC1_RESET 0x1
  232. #define PSC1_SYNC 0x4
  233. #define PSC1_SDATA_OUT 0x1
  234. #define PSC2_RESET 0x2
  235. #define PSC2_SYNC (0x4<<4)
  236. #define PSC2_SDATA_OUT (0x1<<4)
  237. #define MPC52xx_GPIO_PSC1_MASK 0x7
  238. #define MPC52xx_GPIO_PSC2_MASK (0x7<<4)
  239. /**
  240. * mpc5200_psc_ac97_gpio_reset: Use gpio pins to reset the ac97 bus
  241. *
  242. * @psc: psc number to reset (only psc 1 and 2 support ac97)
  243. */
  244. int mpc5200_psc_ac97_gpio_reset(int psc_number)
  245. {
  246. unsigned long flags;
  247. u32 gpio;
  248. u32 mux;
  249. int out;
  250. int reset;
  251. int sync;
  252. if ((!simple_gpio) || (!wkup_gpio))
  253. return -ENODEV;
  254. switch (psc_number) {
  255. case 0:
  256. reset = PSC1_RESET; /* AC97_1_RES */
  257. sync = PSC1_SYNC; /* AC97_1_SYNC */
  258. out = PSC1_SDATA_OUT; /* AC97_1_SDATA_OUT */
  259. gpio = MPC52xx_GPIO_PSC1_MASK;
  260. break;
  261. case 1:
  262. reset = PSC2_RESET; /* AC97_2_RES */
  263. sync = PSC2_SYNC; /* AC97_2_SYNC */
  264. out = PSC2_SDATA_OUT; /* AC97_2_SDATA_OUT */
  265. gpio = MPC52xx_GPIO_PSC2_MASK;
  266. break;
  267. default:
  268. pr_err(__FILE__ ": Unable to determine PSC, no ac97 "
  269. "cold-reset will be performed\n");
  270. return -ENODEV;
  271. }
  272. spin_lock_irqsave(&gpio_lock, flags);
  273. /* Reconfiure pin-muxing to gpio */
  274. mux = in_be32(&simple_gpio->port_config);
  275. out_be32(&simple_gpio->port_config, mux & (~gpio));
  276. /* enable gpio pins for output */
  277. setbits8(&wkup_gpio->wkup_gpioe, reset);
  278. setbits32(&simple_gpio->simple_gpioe, sync | out);
  279. setbits8(&wkup_gpio->wkup_ddr, reset);
  280. setbits32(&simple_gpio->simple_ddr, sync | out);
  281. /* Assert cold reset */
  282. clrbits32(&simple_gpio->simple_dvo, sync | out);
  283. clrbits8(&wkup_gpio->wkup_dvo, reset);
  284. /* wait for 1 us */
  285. udelay(1);
  286. /* Deassert reset */
  287. setbits8(&wkup_gpio->wkup_dvo, reset);
  288. /* wait at least 200ns */
  289. /* 7 ~= (200ns * timebase) / ns2sec */
  290. __delay(7);
  291. /* Restore pin-muxing */
  292. out_be32(&simple_gpio->port_config, mux);
  293. spin_unlock_irqrestore(&gpio_lock, flags);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(mpc5200_psc_ac97_gpio_reset);