reset.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <john@phrozen.org>
  7. * Copyright (C) 2013-2015 Lantiq Beteiligungs-GmbH & Co.KG
  8. */
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/ioport.h>
  12. #include <linux/pm.h>
  13. #include <linux/export.h>
  14. #include <linux/delay.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/reset-controller.h>
  18. #include <asm/reboot.h>
  19. #include <lantiq_soc.h>
  20. #include "../prom.h"
  21. /* reset request register */
  22. #define RCU_RST_REQ 0x0010
  23. /* reset status register */
  24. #define RCU_RST_STAT 0x0014
  25. /* vr9 gphy registers */
  26. #define RCU_GFS_ADD0_XRX200 0x0020
  27. #define RCU_GFS_ADD1_XRX200 0x0068
  28. /* xRX300 gphy registers */
  29. #define RCU_GFS_ADD0_XRX300 0x0020
  30. #define RCU_GFS_ADD1_XRX300 0x0058
  31. #define RCU_GFS_ADD2_XRX300 0x00AC
  32. /* xRX330 gphy registers */
  33. #define RCU_GFS_ADD0_XRX330 0x0020
  34. #define RCU_GFS_ADD1_XRX330 0x0058
  35. #define RCU_GFS_ADD2_XRX330 0x00AC
  36. #define RCU_GFS_ADD3_XRX330 0x0264
  37. /* xbar BE flag */
  38. #define RCU_AHB_ENDIAN 0x004C
  39. #define RCU_VR9_BE_AHB1S 0x00000008
  40. /* reboot bit */
  41. #define RCU_RD_GPHY0_XRX200 BIT(31)
  42. #define RCU_RD_SRST BIT(30)
  43. #define RCU_RD_GPHY1_XRX200 BIT(29)
  44. /* xRX300 bits */
  45. #define RCU_RD_GPHY0_XRX300 BIT(31)
  46. #define RCU_RD_GPHY1_XRX300 BIT(29)
  47. #define RCU_RD_GPHY2_XRX300 BIT(28)
  48. /* xRX330 bits */
  49. #define RCU_RD_GPHY0_XRX330 BIT(31)
  50. #define RCU_RD_GPHY1_XRX330 BIT(29)
  51. #define RCU_RD_GPHY2_XRX330 BIT(28)
  52. #define RCU_RD_GPHY3_XRX330 BIT(10)
  53. /* reset cause */
  54. #define RCU_STAT_SHIFT 26
  55. /* boot selection */
  56. #define RCU_BOOT_SEL(x) ((x >> 18) & 0x7)
  57. #define RCU_BOOT_SEL_XRX200(x) (((x >> 17) & 0xf) | ((x >> 8) & 0x10))
  58. /* dwc2 USB configuration registers */
  59. #define RCU_USB1CFG 0x0018
  60. #define RCU_USB2CFG 0x0034
  61. /* USB DMA endianness bits */
  62. #define RCU_USBCFG_HDSEL_BIT BIT(11)
  63. #define RCU_USBCFG_HOST_END_BIT BIT(10)
  64. #define RCU_USBCFG_SLV_END_BIT BIT(9)
  65. /* USB reset bits */
  66. #define RCU_USBRESET 0x0010
  67. #define USBRESET_BIT BIT(4)
  68. #define RCU_USBRESET2 0x0048
  69. #define USB1RESET_BIT BIT(4)
  70. #define USB2RESET_BIT BIT(5)
  71. #define RCU_CFG1A 0x0038
  72. #define RCU_CFG1B 0x003C
  73. /* USB PMU devices */
  74. #define PMU_AHBM BIT(15)
  75. #define PMU_USB0 BIT(6)
  76. #define PMU_USB1 BIT(27)
  77. /* USB PHY PMU devices */
  78. #define PMU_USB0_P BIT(0)
  79. #define PMU_USB1_P BIT(26)
  80. /* remapped base addr of the reset control unit */
  81. static void __iomem *ltq_rcu_membase;
  82. static struct device_node *ltq_rcu_np;
  83. static DEFINE_SPINLOCK(ltq_rcu_lock);
  84. static void ltq_rcu_w32(uint32_t val, uint32_t reg_off)
  85. {
  86. ltq_w32(val, ltq_rcu_membase + reg_off);
  87. }
  88. static uint32_t ltq_rcu_r32(uint32_t reg_off)
  89. {
  90. return ltq_r32(ltq_rcu_membase + reg_off);
  91. }
  92. static void ltq_rcu_w32_mask(uint32_t clr, uint32_t set, uint32_t reg_off)
  93. {
  94. unsigned long flags;
  95. spin_lock_irqsave(&ltq_rcu_lock, flags);
  96. ltq_rcu_w32((ltq_rcu_r32(reg_off) & ~(clr)) | (set), reg_off);
  97. spin_unlock_irqrestore(&ltq_rcu_lock, flags);
  98. }
  99. /* This function is used by the watchdog driver */
  100. int ltq_reset_cause(void)
  101. {
  102. u32 val = ltq_rcu_r32(RCU_RST_STAT);
  103. return val >> RCU_STAT_SHIFT;
  104. }
  105. EXPORT_SYMBOL_GPL(ltq_reset_cause);
  106. /* allow platform code to find out what source we booted from */
  107. unsigned char ltq_boot_select(void)
  108. {
  109. u32 val = ltq_rcu_r32(RCU_RST_STAT);
  110. if (of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200"))
  111. return RCU_BOOT_SEL_XRX200(val);
  112. return RCU_BOOT_SEL(val);
  113. }
  114. struct ltq_gphy_reset {
  115. u32 rd;
  116. u32 addr;
  117. };
  118. /* reset / boot a gphy */
  119. static struct ltq_gphy_reset xrx200_gphy[] = {
  120. {RCU_RD_GPHY0_XRX200, RCU_GFS_ADD0_XRX200},
  121. {RCU_RD_GPHY1_XRX200, RCU_GFS_ADD1_XRX200},
  122. };
  123. /* reset / boot a gphy */
  124. static struct ltq_gphy_reset xrx300_gphy[] = {
  125. {RCU_RD_GPHY0_XRX300, RCU_GFS_ADD0_XRX300},
  126. {RCU_RD_GPHY1_XRX300, RCU_GFS_ADD1_XRX300},
  127. {RCU_RD_GPHY2_XRX300, RCU_GFS_ADD2_XRX300},
  128. };
  129. /* reset / boot a gphy */
  130. static struct ltq_gphy_reset xrx330_gphy[] = {
  131. {RCU_RD_GPHY0_XRX330, RCU_GFS_ADD0_XRX330},
  132. {RCU_RD_GPHY1_XRX330, RCU_GFS_ADD1_XRX330},
  133. {RCU_RD_GPHY2_XRX330, RCU_GFS_ADD2_XRX330},
  134. {RCU_RD_GPHY3_XRX330, RCU_GFS_ADD3_XRX330},
  135. };
  136. static void xrx200_gphy_boot_addr(struct ltq_gphy_reset *phy_regs,
  137. dma_addr_t dev_addr)
  138. {
  139. ltq_rcu_w32_mask(0, phy_regs->rd, RCU_RST_REQ);
  140. ltq_rcu_w32(dev_addr, phy_regs->addr);
  141. ltq_rcu_w32_mask(phy_regs->rd, 0, RCU_RST_REQ);
  142. }
  143. /* reset and boot a gphy. these phys only exist on xrx200 SoC */
  144. int xrx200_gphy_boot(struct device *dev, unsigned int id, dma_addr_t dev_addr)
  145. {
  146. struct clk *clk;
  147. if (!of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200")) {
  148. dev_err(dev, "this SoC has no GPHY\n");
  149. return -EINVAL;
  150. }
  151. if (of_machine_is_compatible("lantiq,vr9")) {
  152. clk = clk_get_sys("1f203000.rcu", "gphy");
  153. if (IS_ERR(clk))
  154. return PTR_ERR(clk);
  155. clk_enable(clk);
  156. }
  157. dev_info(dev, "booting GPHY%u firmware at %X\n", id, dev_addr);
  158. if (of_machine_is_compatible("lantiq,vr9")) {
  159. if (id >= ARRAY_SIZE(xrx200_gphy)) {
  160. dev_err(dev, "%u is an invalid gphy id\n", id);
  161. return -EINVAL;
  162. }
  163. xrx200_gphy_boot_addr(&xrx200_gphy[id], dev_addr);
  164. } else if (of_machine_is_compatible("lantiq,ar10")) {
  165. if (id >= ARRAY_SIZE(xrx300_gphy)) {
  166. dev_err(dev, "%u is an invalid gphy id\n", id);
  167. return -EINVAL;
  168. }
  169. xrx200_gphy_boot_addr(&xrx300_gphy[id], dev_addr);
  170. } else if (of_machine_is_compatible("lantiq,grx390")) {
  171. if (id >= ARRAY_SIZE(xrx330_gphy)) {
  172. dev_err(dev, "%u is an invalid gphy id\n", id);
  173. return -EINVAL;
  174. }
  175. xrx200_gphy_boot_addr(&xrx330_gphy[id], dev_addr);
  176. }
  177. return 0;
  178. }
  179. /* reset a io domain for u micro seconds */
  180. void ltq_reset_once(unsigned int module, ulong u)
  181. {
  182. ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) | module, RCU_RST_REQ);
  183. udelay(u);
  184. ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) & ~module, RCU_RST_REQ);
  185. }
  186. static int ltq_assert_device(struct reset_controller_dev *rcdev,
  187. unsigned long id)
  188. {
  189. u32 val;
  190. if (id < 8)
  191. return -1;
  192. val = ltq_rcu_r32(RCU_RST_REQ);
  193. val |= BIT(id);
  194. ltq_rcu_w32(val, RCU_RST_REQ);
  195. return 0;
  196. }
  197. static int ltq_deassert_device(struct reset_controller_dev *rcdev,
  198. unsigned long id)
  199. {
  200. u32 val;
  201. if (id < 8)
  202. return -1;
  203. val = ltq_rcu_r32(RCU_RST_REQ);
  204. val &= ~BIT(id);
  205. ltq_rcu_w32(val, RCU_RST_REQ);
  206. return 0;
  207. }
  208. static int ltq_reset_device(struct reset_controller_dev *rcdev,
  209. unsigned long id)
  210. {
  211. ltq_assert_device(rcdev, id);
  212. return ltq_deassert_device(rcdev, id);
  213. }
  214. static const struct reset_control_ops reset_ops = {
  215. .reset = ltq_reset_device,
  216. .assert = ltq_assert_device,
  217. .deassert = ltq_deassert_device,
  218. };
  219. static struct reset_controller_dev reset_dev = {
  220. .ops = &reset_ops,
  221. .owner = THIS_MODULE,
  222. .nr_resets = 32,
  223. .of_reset_n_cells = 1,
  224. };
  225. void ltq_rst_init(void)
  226. {
  227. reset_dev.of_node = of_find_compatible_node(NULL, NULL,
  228. "lantiq,xway-reset");
  229. if (!reset_dev.of_node)
  230. pr_err("Failed to find reset controller node");
  231. else
  232. reset_controller_register(&reset_dev);
  233. }
  234. static void ltq_machine_restart(char *command)
  235. {
  236. u32 val = ltq_rcu_r32(RCU_RST_REQ);
  237. if (of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200"))
  238. val |= RCU_RD_GPHY1_XRX200 | RCU_RD_GPHY0_XRX200;
  239. val |= RCU_RD_SRST;
  240. local_irq_disable();
  241. ltq_rcu_w32(val, RCU_RST_REQ);
  242. unreachable();
  243. }
  244. static void ltq_machine_halt(void)
  245. {
  246. local_irq_disable();
  247. unreachable();
  248. }
  249. static void ltq_machine_power_off(void)
  250. {
  251. local_irq_disable();
  252. unreachable();
  253. }
  254. static void ltq_usb_init(void)
  255. {
  256. /* Power for USB cores 1 & 2 */
  257. ltq_pmu_enable(PMU_AHBM);
  258. ltq_pmu_enable(PMU_USB0);
  259. ltq_pmu_enable(PMU_USB1);
  260. ltq_rcu_w32(ltq_rcu_r32(RCU_CFG1A) | BIT(0), RCU_CFG1A);
  261. ltq_rcu_w32(ltq_rcu_r32(RCU_CFG1B) | BIT(0), RCU_CFG1B);
  262. /* Enable USB PHY power for cores 1 & 2 */
  263. ltq_pmu_enable(PMU_USB0_P);
  264. ltq_pmu_enable(PMU_USB1_P);
  265. /* Configure cores to host mode */
  266. ltq_rcu_w32(ltq_rcu_r32(RCU_USB1CFG) & ~RCU_USBCFG_HDSEL_BIT,
  267. RCU_USB1CFG);
  268. ltq_rcu_w32(ltq_rcu_r32(RCU_USB2CFG) & ~RCU_USBCFG_HDSEL_BIT,
  269. RCU_USB2CFG);
  270. /* Select DMA endianness (Host-endian: big-endian) */
  271. ltq_rcu_w32((ltq_rcu_r32(RCU_USB1CFG) & ~RCU_USBCFG_SLV_END_BIT)
  272. | RCU_USBCFG_HOST_END_BIT, RCU_USB1CFG);
  273. ltq_rcu_w32(ltq_rcu_r32((RCU_USB2CFG) & ~RCU_USBCFG_SLV_END_BIT)
  274. | RCU_USBCFG_HOST_END_BIT, RCU_USB2CFG);
  275. /* Hard reset USB state machines */
  276. ltq_rcu_w32(ltq_rcu_r32(RCU_USBRESET) | USBRESET_BIT, RCU_USBRESET);
  277. udelay(50 * 1000);
  278. ltq_rcu_w32(ltq_rcu_r32(RCU_USBRESET) & ~USBRESET_BIT, RCU_USBRESET);
  279. /* Soft reset USB state machines */
  280. ltq_rcu_w32(ltq_rcu_r32(RCU_USBRESET2)
  281. | USB1RESET_BIT | USB2RESET_BIT, RCU_USBRESET2);
  282. udelay(50 * 1000);
  283. ltq_rcu_w32(ltq_rcu_r32(RCU_USBRESET2)
  284. & ~(USB1RESET_BIT | USB2RESET_BIT), RCU_USBRESET2);
  285. }
  286. static int __init mips_reboot_setup(void)
  287. {
  288. struct resource res;
  289. ltq_rcu_np = of_find_compatible_node(NULL, NULL, "lantiq,rcu-xway");
  290. if (!ltq_rcu_np)
  291. ltq_rcu_np = of_find_compatible_node(NULL, NULL,
  292. "lantiq,rcu-xrx200");
  293. /* check if all the reset register range is available */
  294. if (!ltq_rcu_np)
  295. panic("Failed to load reset resources from devicetree");
  296. if (of_address_to_resource(ltq_rcu_np, 0, &res))
  297. panic("Failed to get rcu memory range");
  298. if (!request_mem_region(res.start, resource_size(&res), res.name))
  299. pr_err("Failed to request rcu memory");
  300. ltq_rcu_membase = ioremap_nocache(res.start, resource_size(&res));
  301. if (!ltq_rcu_membase)
  302. panic("Failed to remap core memory");
  303. if (of_machine_is_compatible("lantiq,ar9") ||
  304. of_machine_is_compatible("lantiq,vr9"))
  305. ltq_usb_init();
  306. if (of_machine_is_compatible("lantiq,vr9"))
  307. ltq_rcu_w32(ltq_rcu_r32(RCU_AHB_ENDIAN) | RCU_VR9_BE_AHB1S,
  308. RCU_AHB_ENDIAN);
  309. _machine_restart = ltq_machine_restart;
  310. _machine_halt = ltq_machine_halt;
  311. pm_power_off = ltq_machine_power_off;
  312. return 0;
  313. }
  314. arch_initcall(mips_reboot_setup);