reset-k230.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022-2024 Canaan Bright Sight Co., Ltd
  4. * Copyright (C) 2024-2025 Junhui Liu <junhui.liu@pigmoral.tech>
  5. *
  6. * The reset management module in the K230 SoC provides reset time control
  7. * registers. For RST_TYPE_CPU0, RST_TYPE_CPU1 and RST_TYPE_SW_DONE, the period
  8. * during which reset is applied or removed while the clock is stopped can be
  9. * set up to 15 * 0.25 = 3.75 µs. For RST_TYPE_HW_DONE, that period can be set
  10. * up to 255 * 0.25 = 63.75 µs. For RST_TYPE_FLUSH, the reset bit is
  11. * automatically cleared by hardware when flush completes.
  12. *
  13. * Although this driver does not configure the reset time registers, delays have
  14. * been added to the assert, deassert, and reset operations to cover the maximum
  15. * reset time. Some reset types include done bits whose toggle does not
  16. * unambiguously signal whether hardware reset removal or clock-stop period
  17. * expiration occurred first. Delays are therefore retained for types with done
  18. * bits to ensure safe timing.
  19. *
  20. * Reference: K230 Technical Reference Manual V0.3.1
  21. * https://kendryte-download.canaan-creative.com/developer/k230/HDK/K230%E7%A1%AC%E4%BB%B6%E6%96%87%E6%A1%A3/K230_Technical_Reference_Manual_V0.3.1_20241118.pdf
  22. */
  23. #include <linux/cleanup.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/iopoll.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/reset-controller.h>
  30. #include <linux/spinlock.h>
  31. #include <dt-bindings/reset/canaan,k230-rst.h>
  32. /**
  33. * enum k230_rst_type - K230 reset types
  34. * @RST_TYPE_CPU0: Reset type for CPU0
  35. * Automatically clears, has write enable and done bit, active high
  36. * @RST_TYPE_CPU1: Reset type for CPU1
  37. * Manually clears, has write enable and done bit, active high
  38. * @RST_TYPE_FLUSH: Reset type for CPU L2 cache flush
  39. * Automatically clears, has write enable, no done bit, active high
  40. * @RST_TYPE_HW_DONE: Reset type for hardware auto clear
  41. * Automatically clears, no write enable, has done bit, active high
  42. * @RST_TYPE_SW_DONE: Reset type for software manual clear
  43. * Manually clears, no write enable and done bit,
  44. * active high if ID is RST_SPI2AXI, otherwise active low
  45. */
  46. enum k230_rst_type {
  47. RST_TYPE_CPU0,
  48. RST_TYPE_CPU1,
  49. RST_TYPE_FLUSH,
  50. RST_TYPE_HW_DONE,
  51. RST_TYPE_SW_DONE,
  52. };
  53. struct k230_rst_map {
  54. u32 offset;
  55. enum k230_rst_type type;
  56. u32 done;
  57. u32 reset;
  58. };
  59. struct k230_rst {
  60. struct reset_controller_dev rcdev;
  61. void __iomem *base;
  62. /* protect register read-modify-write */
  63. spinlock_t lock;
  64. };
  65. static const struct k230_rst_map k230_resets[] = {
  66. [RST_CPU0] = { 0x4, RST_TYPE_CPU0, BIT(12), BIT(0) },
  67. [RST_CPU1] = { 0xc, RST_TYPE_CPU1, BIT(12), BIT(0) },
  68. [RST_CPU0_FLUSH] = { 0x4, RST_TYPE_FLUSH, 0, BIT(4) },
  69. [RST_CPU1_FLUSH] = { 0xc, RST_TYPE_FLUSH, 0, BIT(4) },
  70. [RST_AI] = { 0x14, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  71. [RST_VPU] = { 0x1c, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  72. [RST_HISYS] = { 0x2c, RST_TYPE_HW_DONE, BIT(4), BIT(0) },
  73. [RST_HISYS_AHB] = { 0x2c, RST_TYPE_HW_DONE, BIT(5), BIT(1) },
  74. [RST_SDIO0] = { 0x34, RST_TYPE_HW_DONE, BIT(28), BIT(0) },
  75. [RST_SDIO1] = { 0x34, RST_TYPE_HW_DONE, BIT(29), BIT(1) },
  76. [RST_SDIO_AXI] = { 0x34, RST_TYPE_HW_DONE, BIT(30), BIT(2) },
  77. [RST_USB0] = { 0x3c, RST_TYPE_HW_DONE, BIT(28), BIT(0) },
  78. [RST_USB1] = { 0x3c, RST_TYPE_HW_DONE, BIT(29), BIT(1) },
  79. [RST_USB0_AHB] = { 0x3c, RST_TYPE_HW_DONE, BIT(30), BIT(0) },
  80. [RST_USB1_AHB] = { 0x3c, RST_TYPE_HW_DONE, BIT(31), BIT(1) },
  81. [RST_SPI0] = { 0x44, RST_TYPE_HW_DONE, BIT(28), BIT(0) },
  82. [RST_SPI1] = { 0x44, RST_TYPE_HW_DONE, BIT(29), BIT(1) },
  83. [RST_SPI2] = { 0x44, RST_TYPE_HW_DONE, BIT(30), BIT(2) },
  84. [RST_SEC] = { 0x4c, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  85. [RST_PDMA] = { 0x54, RST_TYPE_HW_DONE, BIT(28), BIT(0) },
  86. [RST_SDMA] = { 0x54, RST_TYPE_HW_DONE, BIT(29), BIT(1) },
  87. [RST_DECOMPRESS] = { 0x5c, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  88. [RST_SRAM] = { 0x64, RST_TYPE_HW_DONE, BIT(28), BIT(0) },
  89. [RST_SHRM_AXIM] = { 0x64, RST_TYPE_HW_DONE, BIT(30), BIT(2) },
  90. [RST_SHRM_AXIS] = { 0x64, RST_TYPE_HW_DONE, BIT(31), BIT(3) },
  91. [RST_NONAI2D] = { 0x6c, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  92. [RST_MCTL] = { 0x74, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  93. [RST_ISP] = { 0x80, RST_TYPE_HW_DONE, BIT(29), BIT(6) },
  94. [RST_ISP_DW] = { 0x80, RST_TYPE_HW_DONE, BIT(28), BIT(5) },
  95. [RST_DPU] = { 0x88, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  96. [RST_DISP] = { 0x90, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  97. [RST_GPU] = { 0x98, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  98. [RST_AUDIO] = { 0xa4, RST_TYPE_HW_DONE, BIT(31), BIT(0) },
  99. [RST_TIMER0] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(0) },
  100. [RST_TIMER1] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(1) },
  101. [RST_TIMER2] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(2) },
  102. [RST_TIMER3] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(3) },
  103. [RST_TIMER4] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(4) },
  104. [RST_TIMER5] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(5) },
  105. [RST_TIMER_APB] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(6) },
  106. [RST_HDI] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(7) },
  107. [RST_WDT0] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(12) },
  108. [RST_WDT1] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(13) },
  109. [RST_WDT0_APB] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(14) },
  110. [RST_WDT1_APB] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(15) },
  111. [RST_TS_APB] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(16) },
  112. [RST_MAILBOX] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(17) },
  113. [RST_STC] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(18) },
  114. [RST_PMU] = { 0x20, RST_TYPE_SW_DONE, 0, BIT(19) },
  115. [RST_LOSYS_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(0) },
  116. [RST_UART0] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(1) },
  117. [RST_UART1] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(2) },
  118. [RST_UART2] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(3) },
  119. [RST_UART3] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(4) },
  120. [RST_UART4] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(5) },
  121. [RST_I2C0] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(6) },
  122. [RST_I2C1] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(7) },
  123. [RST_I2C2] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(8) },
  124. [RST_I2C3] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(9) },
  125. [RST_I2C4] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(10) },
  126. [RST_JAMLINK0_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(11) },
  127. [RST_JAMLINK1_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(12) },
  128. [RST_JAMLINK2_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(13) },
  129. [RST_JAMLINK3_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(14) },
  130. [RST_CODEC_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(17) },
  131. [RST_GPIO_DB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(18) },
  132. [RST_GPIO_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(19) },
  133. [RST_ADC] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(20) },
  134. [RST_ADC_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(21) },
  135. [RST_PWM_APB] = { 0x24, RST_TYPE_SW_DONE, 0, BIT(22) },
  136. [RST_SHRM_APB] = { 0x64, RST_TYPE_SW_DONE, 0, BIT(1) },
  137. [RST_CSI0] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(0) },
  138. [RST_CSI1] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(1) },
  139. [RST_CSI2] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(2) },
  140. [RST_CSI_DPHY] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(3) },
  141. [RST_ISP_AHB] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(4) },
  142. [RST_M0] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(7) },
  143. [RST_M1] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(8) },
  144. [RST_M2] = { 0x80, RST_TYPE_SW_DONE, 0, BIT(9) },
  145. [RST_SPI2AXI] = { 0xa8, RST_TYPE_SW_DONE, 0, BIT(0) }
  146. };
  147. static inline struct k230_rst *to_k230_rst(struct reset_controller_dev *rcdev)
  148. {
  149. return container_of(rcdev, struct k230_rst, rcdev);
  150. }
  151. static void k230_rst_clear_done(struct k230_rst *rstc, unsigned long id,
  152. bool write_en)
  153. {
  154. const struct k230_rst_map *rmap = &k230_resets[id];
  155. u32 reg;
  156. guard(spinlock_irqsave)(&rstc->lock);
  157. reg = readl(rstc->base + rmap->offset);
  158. reg |= rmap->done; /* write 1 to clear */
  159. if (write_en)
  160. reg |= rmap->done << 16;
  161. writel(reg, rstc->base + rmap->offset);
  162. }
  163. static int k230_rst_wait_and_clear_done(struct k230_rst *rstc, unsigned long id,
  164. bool write_en)
  165. {
  166. const struct k230_rst_map *rmap = &k230_resets[id];
  167. u32 reg;
  168. int ret;
  169. ret = readl_poll_timeout(rstc->base + rmap->offset, reg,
  170. reg & rmap->done, 10, 1000);
  171. if (ret) {
  172. dev_err(rstc->rcdev.dev, "Wait for reset done timeout\n");
  173. return ret;
  174. }
  175. k230_rst_clear_done(rstc, id, write_en);
  176. return 0;
  177. }
  178. static void k230_rst_update(struct k230_rst *rstc, unsigned long id,
  179. bool assert, bool write_en, bool active_low)
  180. {
  181. const struct k230_rst_map *rmap = &k230_resets[id];
  182. u32 reg;
  183. guard(spinlock_irqsave)(&rstc->lock);
  184. reg = readl(rstc->base + rmap->offset);
  185. if (assert ^ active_low)
  186. reg |= rmap->reset;
  187. else
  188. reg &= ~rmap->reset;
  189. if (write_en)
  190. reg |= rmap->reset << 16;
  191. writel(reg, rstc->base + rmap->offset);
  192. }
  193. static int k230_rst_assert(struct reset_controller_dev *rcdev, unsigned long id)
  194. {
  195. struct k230_rst *rstc = to_k230_rst(rcdev);
  196. switch (k230_resets[id].type) {
  197. case RST_TYPE_CPU1:
  198. k230_rst_update(rstc, id, true, true, false);
  199. break;
  200. case RST_TYPE_SW_DONE:
  201. k230_rst_update(rstc, id, true, false,
  202. id == RST_SPI2AXI ? false : true);
  203. break;
  204. case RST_TYPE_CPU0:
  205. case RST_TYPE_FLUSH:
  206. case RST_TYPE_HW_DONE:
  207. return -EOPNOTSUPP;
  208. }
  209. /*
  210. * The time period when reset is applied but the clock is stopped for
  211. * RST_TYPE_CPU1 and RST_TYPE_SW_DONE can be set up to 3.75us. Delay
  212. * 10us to ensure proper reset timing.
  213. */
  214. udelay(10);
  215. return 0;
  216. }
  217. static int k230_rst_deassert(struct reset_controller_dev *rcdev,
  218. unsigned long id)
  219. {
  220. struct k230_rst *rstc = to_k230_rst(rcdev);
  221. int ret = 0;
  222. switch (k230_resets[id].type) {
  223. case RST_TYPE_CPU1:
  224. k230_rst_update(rstc, id, false, true, false);
  225. ret = k230_rst_wait_and_clear_done(rstc, id, true);
  226. break;
  227. case RST_TYPE_SW_DONE:
  228. k230_rst_update(rstc, id, false, false,
  229. id == RST_SPI2AXI ? false : true);
  230. break;
  231. case RST_TYPE_CPU0:
  232. case RST_TYPE_FLUSH:
  233. case RST_TYPE_HW_DONE:
  234. return -EOPNOTSUPP;
  235. }
  236. /*
  237. * The time period when reset is removed but the clock is stopped for
  238. * RST_TYPE_CPU1 and RST_TYPE_SW_DONE can be set up to 3.75us. Delay
  239. * 10us to ensure proper reset timing.
  240. */
  241. udelay(10);
  242. return ret;
  243. }
  244. static int k230_rst_reset(struct reset_controller_dev *rcdev, unsigned long id)
  245. {
  246. struct k230_rst *rstc = to_k230_rst(rcdev);
  247. const struct k230_rst_map *rmap = &k230_resets[id];
  248. u32 reg;
  249. int ret = 0;
  250. switch (rmap->type) {
  251. case RST_TYPE_CPU0:
  252. k230_rst_clear_done(rstc, id, true);
  253. k230_rst_update(rstc, id, true, true, false);
  254. ret = k230_rst_wait_and_clear_done(rstc, id, true);
  255. /*
  256. * The time period when reset is applied and removed but the
  257. * clock is stopped for RST_TYPE_CPU0 can be set up to 7.5us.
  258. * Delay 10us to ensure proper reset timing.
  259. */
  260. udelay(10);
  261. break;
  262. case RST_TYPE_FLUSH:
  263. k230_rst_update(rstc, id, true, true, false);
  264. /* Wait flush request bit auto cleared by hardware */
  265. ret = readl_poll_timeout(rstc->base + rmap->offset, reg,
  266. !(reg & rmap->reset), 10, 1000);
  267. if (ret)
  268. dev_err(rcdev->dev, "Wait for flush done timeout\n");
  269. break;
  270. case RST_TYPE_HW_DONE:
  271. k230_rst_clear_done(rstc, id, false);
  272. k230_rst_update(rstc, id, true, false, false);
  273. ret = k230_rst_wait_and_clear_done(rstc, id, false);
  274. /*
  275. * The time period when reset is applied and removed but the
  276. * clock is stopped for RST_TYPE_HW_DONE can be set up to
  277. * 127.5us. Delay 200us to ensure proper reset timing.
  278. */
  279. fsleep(200);
  280. break;
  281. case RST_TYPE_CPU1:
  282. case RST_TYPE_SW_DONE:
  283. k230_rst_assert(rcdev, id);
  284. ret = k230_rst_deassert(rcdev, id);
  285. break;
  286. }
  287. return ret;
  288. }
  289. static const struct reset_control_ops k230_rst_ops = {
  290. .reset = k230_rst_reset,
  291. .assert = k230_rst_assert,
  292. .deassert = k230_rst_deassert,
  293. };
  294. static int k230_rst_probe(struct platform_device *pdev)
  295. {
  296. struct device *dev = &pdev->dev;
  297. struct k230_rst *rstc;
  298. rstc = devm_kzalloc(dev, sizeof(*rstc), GFP_KERNEL);
  299. if (!rstc)
  300. return -ENOMEM;
  301. rstc->base = devm_platform_ioremap_resource(pdev, 0);
  302. if (IS_ERR(rstc->base))
  303. return PTR_ERR(rstc->base);
  304. spin_lock_init(&rstc->lock);
  305. rstc->rcdev.dev = dev;
  306. rstc->rcdev.owner = THIS_MODULE;
  307. rstc->rcdev.ops = &k230_rst_ops;
  308. rstc->rcdev.nr_resets = ARRAY_SIZE(k230_resets);
  309. rstc->rcdev.of_node = dev->of_node;
  310. return devm_reset_controller_register(dev, &rstc->rcdev);
  311. }
  312. static const struct of_device_id k230_rst_match[] = {
  313. { .compatible = "canaan,k230-rst", },
  314. { /* sentinel */ }
  315. };
  316. MODULE_DEVICE_TABLE(of, k230_rst_match);
  317. static struct platform_driver k230_rst_driver = {
  318. .probe = k230_rst_probe,
  319. .driver = {
  320. .name = "k230-rst",
  321. .of_match_table = k230_rst_match,
  322. }
  323. };
  324. module_platform_driver(k230_rst_driver);
  325. MODULE_AUTHOR("Junhui Liu <junhui.liu@pigmoral.tech>");
  326. MODULE_DESCRIPTION("Canaan K230 reset driver");
  327. MODULE_LICENSE("GPL");