reset.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/io.h>
  10. #include <linux/ioport.h>
  11. #include <linux/pm.h>
  12. #include <linux/export.h>
  13. #include <linux/delay.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/reset-controller.h>
  17. #include <asm/reboot.h>
  18. #include <lantiq_soc.h>
  19. #include "../prom.h"
  20. #define ltq_rcu_w32(x, y) ltq_w32((x), ltq_rcu_membase + (y))
  21. #define ltq_rcu_r32(x) ltq_r32(ltq_rcu_membase + (x))
  22. /* reset request register */
  23. #define RCU_RST_REQ 0x0010
  24. /* reset status register */
  25. #define RCU_RST_STAT 0x0014
  26. /* vr9 gphy registers */
  27. #define RCU_GFS_ADD0_XRX200 0x0020
  28. #define RCU_GFS_ADD1_XRX200 0x0068
  29. /* reboot bit */
  30. #define RCU_RD_GPHY0_XRX200 BIT(31)
  31. #define RCU_RD_SRST BIT(30)
  32. #define RCU_RD_GPHY1_XRX200 BIT(29)
  33. /* reset cause */
  34. #define RCU_STAT_SHIFT 26
  35. /* boot selection */
  36. #define RCU_BOOT_SEL(x) ((x >> 18) & 0x7)
  37. #define RCU_BOOT_SEL_XRX200(x) (((x >> 17) & 0xf) | ((x >> 8) & 0x10))
  38. /* remapped base addr of the reset control unit */
  39. static void __iomem *ltq_rcu_membase;
  40. static struct device_node *ltq_rcu_np;
  41. /* This function is used by the watchdog driver */
  42. int ltq_reset_cause(void)
  43. {
  44. u32 val = ltq_rcu_r32(RCU_RST_STAT);
  45. return val >> RCU_STAT_SHIFT;
  46. }
  47. EXPORT_SYMBOL_GPL(ltq_reset_cause);
  48. /* allow platform code to find out what source we booted from */
  49. unsigned char ltq_boot_select(void)
  50. {
  51. u32 val = ltq_rcu_r32(RCU_RST_STAT);
  52. if (of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200"))
  53. return RCU_BOOT_SEL_XRX200(val);
  54. return RCU_BOOT_SEL(val);
  55. }
  56. /* reset / boot a gphy */
  57. static struct ltq_xrx200_gphy_reset {
  58. u32 rd;
  59. u32 addr;
  60. } xrx200_gphy[] = {
  61. {RCU_RD_GPHY0_XRX200, RCU_GFS_ADD0_XRX200},
  62. {RCU_RD_GPHY1_XRX200, RCU_GFS_ADD1_XRX200},
  63. };
  64. /* reset and boot a gphy. these phys only exist on xrx200 SoC */
  65. int xrx200_gphy_boot(struct device *dev, unsigned int id, dma_addr_t dev_addr)
  66. {
  67. struct clk *clk;
  68. if (!of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200")) {
  69. dev_err(dev, "this SoC has no GPHY\n");
  70. return -EINVAL;
  71. }
  72. clk = clk_get_sys("1f203000.rcu", "gphy");
  73. if (IS_ERR(clk))
  74. return PTR_ERR(clk);
  75. clk_enable(clk);
  76. if (id > 1) {
  77. dev_err(dev, "%u is an invalid gphy id\n", id);
  78. return -EINVAL;
  79. }
  80. dev_info(dev, "booting GPHY%u firmware at %X\n", id, dev_addr);
  81. ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) | xrx200_gphy[id].rd,
  82. RCU_RST_REQ);
  83. ltq_rcu_w32(dev_addr, xrx200_gphy[id].addr);
  84. ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) & ~xrx200_gphy[id].rd,
  85. RCU_RST_REQ);
  86. return 0;
  87. }
  88. /* reset a io domain for u micro seconds */
  89. void ltq_reset_once(unsigned int module, ulong u)
  90. {
  91. ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) | module, RCU_RST_REQ);
  92. udelay(u);
  93. ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) & ~module, RCU_RST_REQ);
  94. }
  95. static int ltq_assert_device(struct reset_controller_dev *rcdev,
  96. unsigned long id)
  97. {
  98. u32 val;
  99. if (id < 8)
  100. return -1;
  101. val = ltq_rcu_r32(RCU_RST_REQ);
  102. val |= BIT(id);
  103. ltq_rcu_w32(val, RCU_RST_REQ);
  104. return 0;
  105. }
  106. static int ltq_deassert_device(struct reset_controller_dev *rcdev,
  107. unsigned long id)
  108. {
  109. u32 val;
  110. if (id < 8)
  111. return -1;
  112. val = ltq_rcu_r32(RCU_RST_REQ);
  113. val &= ~BIT(id);
  114. ltq_rcu_w32(val, RCU_RST_REQ);
  115. return 0;
  116. }
  117. static int ltq_reset_device(struct reset_controller_dev *rcdev,
  118. unsigned long id)
  119. {
  120. ltq_assert_device(rcdev, id);
  121. return ltq_deassert_device(rcdev, id);
  122. }
  123. static struct reset_control_ops reset_ops = {
  124. .reset = ltq_reset_device,
  125. .assert = ltq_assert_device,
  126. .deassert = ltq_deassert_device,
  127. };
  128. static struct reset_controller_dev reset_dev = {
  129. .ops = &reset_ops,
  130. .owner = THIS_MODULE,
  131. .nr_resets = 32,
  132. .of_reset_n_cells = 1,
  133. };
  134. void ltq_rst_init(void)
  135. {
  136. reset_dev.of_node = of_find_compatible_node(NULL, NULL,
  137. "lantiq,xway-reset");
  138. if (!reset_dev.of_node)
  139. pr_err("Failed to find reset controller node");
  140. else
  141. reset_controller_register(&reset_dev);
  142. }
  143. static void ltq_machine_restart(char *command)
  144. {
  145. u32 val = ltq_rcu_r32(RCU_RST_REQ);
  146. if (of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200"))
  147. val |= RCU_RD_GPHY1_XRX200 | RCU_RD_GPHY0_XRX200;
  148. val |= RCU_RD_SRST;
  149. local_irq_disable();
  150. ltq_rcu_w32(val, RCU_RST_REQ);
  151. unreachable();
  152. }
  153. static void ltq_machine_halt(void)
  154. {
  155. local_irq_disable();
  156. unreachable();
  157. }
  158. static void ltq_machine_power_off(void)
  159. {
  160. local_irq_disable();
  161. unreachable();
  162. }
  163. static int __init mips_reboot_setup(void)
  164. {
  165. struct resource res;
  166. ltq_rcu_np = of_find_compatible_node(NULL, NULL, "lantiq,rcu-xway");
  167. if (!ltq_rcu_np)
  168. ltq_rcu_np = of_find_compatible_node(NULL, NULL,
  169. "lantiq,rcu-xrx200");
  170. /* check if all the reset register range is available */
  171. if (!ltq_rcu_np)
  172. panic("Failed to load reset resources from devicetree");
  173. if (of_address_to_resource(ltq_rcu_np, 0, &res))
  174. panic("Failed to get rcu memory range");
  175. if (request_mem_region(res.start, resource_size(&res), res.name) < 0)
  176. pr_err("Failed to request rcu memory");
  177. ltq_rcu_membase = ioremap_nocache(res.start, resource_size(&res));
  178. if (!ltq_rcu_membase)
  179. panic("Failed to remap core memory");
  180. _machine_restart = ltq_machine_restart;
  181. _machine_halt = ltq_machine_halt;
  182. pm_power_off = ltq_machine_power_off;
  183. return 0;
  184. }
  185. arch_initcall(mips_reboot_setup);