mpc52xx_common.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/kernel.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/of_platform.h>
  16. #include <asm/io.h>
  17. #include <asm/prom.h>
  18. #include <asm/mpc52xx.h>
  19. /* MPC5200 device tree match tables */
  20. static struct of_device_id mpc52xx_xlb_ids[] __initdata = {
  21. { .compatible = "fsl,mpc5200-xlb", },
  22. { .compatible = "mpc5200-xlb", },
  23. {}
  24. };
  25. static struct of_device_id mpc52xx_bus_ids[] __initdata = {
  26. { .compatible = "fsl,mpc5200-immr", },
  27. { .compatible = "fsl,mpc5200b-immr", },
  28. { .compatible = "fsl,lpb", },
  29. /* depreciated matches; shouldn't be used in new device trees */
  30. { .type = "builtin", .compatible = "mpc5200", }, /* efika */
  31. { .type = "soc", .compatible = "mpc5200", }, /* lite5200 */
  32. {}
  33. };
  34. /*
  35. * This variable is mapped in mpc52xx_map_wdt() and used in mpc52xx_restart().
  36. * Permanent mapping is required because mpc52xx_restart() can be called
  37. * from interrupt context while node mapping (which calls ioremap())
  38. * cannot be used at such point.
  39. */
  40. static spinlock_t mpc52xx_lock = SPIN_LOCK_UNLOCKED;
  41. static struct mpc52xx_gpt __iomem *mpc52xx_wdt;
  42. static struct mpc52xx_cdm __iomem *mpc52xx_cdm;
  43. /**
  44. * mpc52xx_find_ipb_freq - Find the IPB bus frequency for a device
  45. * @node: device node
  46. *
  47. * Returns IPB bus frequency, or 0 if the bus frequency cannot be found.
  48. */
  49. unsigned int
  50. mpc52xx_find_ipb_freq(struct device_node *node)
  51. {
  52. struct device_node *np;
  53. const unsigned int *p_ipb_freq = NULL;
  54. of_node_get(node);
  55. while (node) {
  56. p_ipb_freq = of_get_property(node, "bus-frequency", NULL);
  57. if (p_ipb_freq)
  58. break;
  59. np = of_get_parent(node);
  60. of_node_put(node);
  61. node = np;
  62. }
  63. if (node)
  64. of_node_put(node);
  65. return p_ipb_freq ? *p_ipb_freq : 0;
  66. }
  67. EXPORT_SYMBOL(mpc52xx_find_ipb_freq);
  68. /*
  69. * Configure the XLB arbiter settings to match what Linux expects.
  70. */
  71. void __init
  72. mpc5200_setup_xlb_arbiter(void)
  73. {
  74. struct device_node *np;
  75. struct mpc52xx_xlb __iomem *xlb;
  76. np = of_find_matching_node(NULL, mpc52xx_xlb_ids);
  77. xlb = of_iomap(np, 0);
  78. of_node_put(np);
  79. if (!xlb) {
  80. printk(KERN_ERR __FILE__ ": "
  81. "Error mapping XLB in mpc52xx_setup_cpu(). "
  82. "Expect some abnormal behavior\n");
  83. return;
  84. }
  85. /* Configure the XLB Arbiter priorities */
  86. out_be32(&xlb->master_pri_enable, 0xff);
  87. out_be32(&xlb->master_priority, 0x11111111);
  88. /*
  89. * Disable XLB pipelining
  90. * (cfr errate 292. We could do this only just before ATA PIO
  91. * transaction and re-enable it afterwards ...)
  92. * Not needed on MPC5200B.
  93. */
  94. if ((mfspr(SPRN_SVR) & MPC5200_SVR_MASK) == MPC5200_SVR)
  95. out_be32(&xlb->config, in_be32(&xlb->config) | MPC52xx_XLB_CFG_PLDIS);
  96. iounmap(xlb);
  97. }
  98. /**
  99. * mpc52xx_declare_of_platform_devices: register internal devices and children
  100. * of the localplus bus to the of_platform
  101. * bus.
  102. */
  103. void __init
  104. mpc52xx_declare_of_platform_devices(void)
  105. {
  106. /* Find every child of the SOC node and add it to of_platform */
  107. if (of_platform_bus_probe(NULL, mpc52xx_bus_ids, NULL))
  108. printk(KERN_ERR __FILE__ ": "
  109. "Error while probing of_platform bus\n");
  110. }
  111. /*
  112. * match tables used by mpc52xx_map_common_devices()
  113. */
  114. static struct of_device_id mpc52xx_gpt_ids[] __initdata = {
  115. { .compatible = "fsl,mpc5200-gpt", },
  116. { .compatible = "mpc5200-gpt", }, /* old */
  117. {}
  118. };
  119. static struct of_device_id mpc52xx_cdm_ids[] __initdata = {
  120. { .compatible = "fsl,mpc5200-cdm", },
  121. { .compatible = "mpc5200-cdm", }, /* old */
  122. {}
  123. };
  124. /**
  125. * mpc52xx_map_common_devices: iomap devices required by common code
  126. */
  127. void __init
  128. mpc52xx_map_common_devices(void)
  129. {
  130. struct device_node *np;
  131. /* mpc52xx_wdt is mapped here and used in mpc52xx_restart,
  132. * possibly from a interrupt context. wdt is only implement
  133. * on a gpt0, so check has-wdt property before mapping.
  134. */
  135. for_each_matching_node(np, mpc52xx_gpt_ids) {
  136. if (of_get_property(np, "fsl,has-wdt", NULL) ||
  137. of_get_property(np, "has-wdt", NULL)) {
  138. mpc52xx_wdt = of_iomap(np, 0);
  139. of_node_put(np);
  140. break;
  141. }
  142. }
  143. /* Clock Distribution Module, used by PSC clock setting function */
  144. np = of_find_matching_node(NULL, mpc52xx_cdm_ids);
  145. mpc52xx_cdm = 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_restart: ppc_md->restart hook for mpc5200 using the watchdog timer
  183. */
  184. void
  185. mpc52xx_restart(char *cmd)
  186. {
  187. local_irq_disable();
  188. /* Turn on the watchdog and wait for it to expire.
  189. * It effectively does a reset. */
  190. if (mpc52xx_wdt) {
  191. out_be32(&mpc52xx_wdt->mode, 0x00000000);
  192. out_be32(&mpc52xx_wdt->count, 0x000000ff);
  193. out_be32(&mpc52xx_wdt->mode, 0x00009004);
  194. } else
  195. printk(KERN_ERR __FILE__ ": "
  196. "mpc52xx_restart: Can't access wdt. "
  197. "Restart impossible, system halted.\n");
  198. while (1);
  199. }