mpc52xx_common.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 struct of_device_id mpc52xx_xlb_ids[] __initdata = {
  24. { .compatible = "fsl,mpc5200-xlb", },
  25. { .compatible = "mpc5200-xlb", },
  26. {}
  27. };
  28. static struct of_device_id mpc52xx_bus_ids[] __initdata = {
  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
  90. mpc52xx_declare_of_platform_devices(void)
  91. {
  92. /* Find every child of the SOC node and add it to of_platform */
  93. if (of_platform_bus_probe(NULL, mpc52xx_bus_ids, NULL))
  94. printk(KERN_ERR __FILE__ ": "
  95. "Error while probing of_platform bus\n");
  96. }
  97. /*
  98. * match tables used by mpc52xx_map_common_devices()
  99. */
  100. static struct of_device_id mpc52xx_gpt_ids[] __initdata = {
  101. { .compatible = "fsl,mpc5200-gpt", },
  102. { .compatible = "mpc5200-gpt", }, /* old */
  103. {}
  104. };
  105. static struct of_device_id mpc52xx_cdm_ids[] __initdata = {
  106. { .compatible = "fsl,mpc5200-cdm", },
  107. { .compatible = "mpc5200-cdm", }, /* old */
  108. {}
  109. };
  110. static const struct of_device_id mpc52xx_gpio_simple[] = {
  111. { .compatible = "fsl,mpc5200-gpio", },
  112. {}
  113. };
  114. static const struct of_device_id mpc52xx_gpio_wkup[] = {
  115. { .compatible = "fsl,mpc5200-gpio-wkup", },
  116. {}
  117. };
  118. /**
  119. * mpc52xx_map_common_devices: iomap devices required by common code
  120. */
  121. void __init
  122. mpc52xx_map_common_devices(void)
  123. {
  124. struct device_node *np;
  125. /* mpc52xx_wdt is mapped here and used in mpc52xx_restart,
  126. * possibly from a interrupt context. wdt is only implement
  127. * on a gpt0, so check has-wdt property before mapping.
  128. */
  129. for_each_matching_node(np, mpc52xx_gpt_ids) {
  130. if (of_get_property(np, "fsl,has-wdt", NULL) ||
  131. of_get_property(np, "has-wdt", NULL)) {
  132. mpc52xx_wdt = of_iomap(np, 0);
  133. of_node_put(np);
  134. break;
  135. }
  136. }
  137. /* Clock Distribution Module, used by PSC clock setting function */
  138. np = of_find_matching_node(NULL, mpc52xx_cdm_ids);
  139. mpc52xx_cdm = of_iomap(np, 0);
  140. of_node_put(np);
  141. /* simple_gpio registers */
  142. np = of_find_matching_node(NULL, mpc52xx_gpio_simple);
  143. simple_gpio = of_iomap(np, 0);
  144. of_node_put(np);
  145. /* wkup_gpio registers */
  146. np = of_find_matching_node(NULL, mpc52xx_gpio_wkup);
  147. wkup_gpio = of_iomap(np, 0);
  148. of_node_put(np);
  149. }
  150. /**
  151. * mpc52xx_set_psc_clkdiv: Set clock divider in the CDM for PSC ports
  152. *
  153. * @psc_id: id of psc port; must be 1,2,3 or 6
  154. * @clkdiv: clock divider value to put into CDM PSC register.
  155. */
  156. int mpc52xx_set_psc_clkdiv(int psc_id, int clkdiv)
  157. {
  158. unsigned long flags;
  159. u16 __iomem *reg;
  160. u32 val;
  161. u32 mask;
  162. u32 mclken_div;
  163. if (!mpc52xx_cdm)
  164. return -ENODEV;
  165. mclken_div = 0x8000 | (clkdiv & 0x1FF);
  166. switch (psc_id) {
  167. case 1: reg = &mpc52xx_cdm->mclken_div_psc1; mask = 0x20; break;
  168. case 2: reg = &mpc52xx_cdm->mclken_div_psc2; mask = 0x40; break;
  169. case 3: reg = &mpc52xx_cdm->mclken_div_psc3; mask = 0x80; break;
  170. case 6: reg = &mpc52xx_cdm->mclken_div_psc6; mask = 0x10; break;
  171. default:
  172. return -ENODEV;
  173. }
  174. /* Set the rate and enable the clock */
  175. spin_lock_irqsave(&mpc52xx_lock, flags);
  176. out_be16(reg, mclken_div);
  177. val = in_be32(&mpc52xx_cdm->clk_enables);
  178. out_be32(&mpc52xx_cdm->clk_enables, val | mask);
  179. spin_unlock_irqrestore(&mpc52xx_lock, flags);
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(mpc52xx_set_psc_clkdiv);
  183. /**
  184. * mpc52xx_get_xtal_freq - Get SYS_XTAL_IN frequency for a device
  185. *
  186. * @node: device node
  187. *
  188. * Returns the frequency of the external oscillator clock connected
  189. * to the SYS_XTAL_IN pin, or 0 if it cannot be determined.
  190. */
  191. unsigned int mpc52xx_get_xtal_freq(struct device_node *node)
  192. {
  193. u32 val;
  194. unsigned int freq;
  195. if (!mpc52xx_cdm)
  196. return 0;
  197. freq = mpc5xxx_get_bus_frequency(node);
  198. if (!freq)
  199. return 0;
  200. if (in_8(&mpc52xx_cdm->ipb_clk_sel) & 0x1)
  201. freq *= 2;
  202. val = in_be32(&mpc52xx_cdm->rstcfg);
  203. if (val & (1 << 5))
  204. freq *= 8;
  205. else
  206. freq *= 4;
  207. if (val & (1 << 6))
  208. freq /= 12;
  209. else
  210. freq /= 16;
  211. return freq;
  212. }
  213. EXPORT_SYMBOL(mpc52xx_get_xtal_freq);
  214. /**
  215. * mpc52xx_restart: ppc_md->restart hook for mpc5200 using the watchdog timer
  216. */
  217. void
  218. mpc52xx_restart(char *cmd)
  219. {
  220. local_irq_disable();
  221. /* Turn on the watchdog and wait for it to expire.
  222. * It effectively does a reset. */
  223. if (mpc52xx_wdt) {
  224. out_be32(&mpc52xx_wdt->mode, 0x00000000);
  225. out_be32(&mpc52xx_wdt->count, 0x000000ff);
  226. out_be32(&mpc52xx_wdt->mode, 0x00009004);
  227. } else
  228. printk(KERN_ERR __FILE__ ": "
  229. "mpc52xx_restart: Can't access wdt. "
  230. "Restart impossible, system halted.\n");
  231. while (1);
  232. }
  233. #define PSC1_RESET 0x1
  234. #define PSC1_SYNC 0x4
  235. #define PSC1_SDATA_OUT 0x1
  236. #define PSC2_RESET 0x2
  237. #define PSC2_SYNC (0x4<<4)
  238. #define PSC2_SDATA_OUT (0x1<<4)
  239. #define MPC52xx_GPIO_PSC1_MASK 0x7
  240. #define MPC52xx_GPIO_PSC2_MASK (0x7<<4)
  241. /**
  242. * mpc5200_psc_ac97_gpio_reset: Use gpio pins to reset the ac97 bus
  243. *
  244. * @psc: psc number to reset (only psc 1 and 2 support ac97)
  245. */
  246. int mpc5200_psc_ac97_gpio_reset(int psc_number)
  247. {
  248. unsigned long flags;
  249. u32 gpio;
  250. u32 mux;
  251. int out;
  252. int reset;
  253. int sync;
  254. if ((!simple_gpio) || (!wkup_gpio))
  255. return -ENODEV;
  256. switch (psc_number) {
  257. case 0:
  258. reset = PSC1_RESET; /* AC97_1_RES */
  259. sync = PSC1_SYNC; /* AC97_1_SYNC */
  260. out = PSC1_SDATA_OUT; /* AC97_1_SDATA_OUT */
  261. gpio = MPC52xx_GPIO_PSC1_MASK;
  262. break;
  263. case 1:
  264. reset = PSC2_RESET; /* AC97_2_RES */
  265. sync = PSC2_SYNC; /* AC97_2_SYNC */
  266. out = PSC2_SDATA_OUT; /* AC97_2_SDATA_OUT */
  267. gpio = MPC52xx_GPIO_PSC2_MASK;
  268. break;
  269. default:
  270. pr_err(__FILE__ ": Unable to determine PSC, no ac97 "
  271. "cold-reset will be performed\n");
  272. return -ENODEV;
  273. }
  274. spin_lock_irqsave(&gpio_lock, flags);
  275. /* Reconfiure pin-muxing to gpio */
  276. mux = in_be32(&simple_gpio->port_config);
  277. out_be32(&simple_gpio->port_config, mux & (~gpio));
  278. /* enable gpio pins for output */
  279. setbits8(&wkup_gpio->wkup_gpioe, reset);
  280. setbits32(&simple_gpio->simple_gpioe, sync | out);
  281. setbits8(&wkup_gpio->wkup_ddr, reset);
  282. setbits32(&simple_gpio->simple_ddr, sync | out);
  283. /* Assert cold reset */
  284. clrbits32(&simple_gpio->simple_dvo, sync | out);
  285. clrbits8(&wkup_gpio->wkup_dvo, reset);
  286. /* wait for 1 us */
  287. udelay(1);
  288. /* Deassert reset */
  289. setbits8(&wkup_gpio->wkup_dvo, reset);
  290. /* wait at least 200ns */
  291. /* 7 ~= (200ns * timebase) / ns2sec */
  292. __delay(7);
  293. /* Restore pin-muxing */
  294. out_be32(&simple_gpio->port_config, mux);
  295. spin_unlock_irqrestore(&gpio_lock, flags);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(mpc5200_psc_ac97_gpio_reset);