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