reset-uniphier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) 2016 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/reset-controller.h>
  22. struct uniphier_reset_data {
  23. unsigned int id;
  24. unsigned int reg;
  25. unsigned int bit;
  26. unsigned int flags;
  27. #define UNIPHIER_RESET_ACTIVE_LOW BIT(0)
  28. };
  29. #define UNIPHIER_RESET_ID_END (unsigned int)(-1)
  30. #define UNIPHIER_RESET_END \
  31. { .id = UNIPHIER_RESET_ID_END }
  32. #define UNIPHIER_RESET(_id, _reg, _bit) \
  33. { \
  34. .id = (_id), \
  35. .reg = (_reg), \
  36. .bit = (_bit), \
  37. }
  38. #define UNIPHIER_RESETX(_id, _reg, _bit) \
  39. { \
  40. .id = (_id), \
  41. .reg = (_reg), \
  42. .bit = (_bit), \
  43. .flags = UNIPHIER_RESET_ACTIVE_LOW, \
  44. }
  45. /* System reset data */
  46. #define UNIPHIER_SLD3_SYS_RESET_NAND(id) \
  47. UNIPHIER_RESETX((id), 0x2004, 2)
  48. #define UNIPHIER_LD11_SYS_RESET_NAND(id) \
  49. UNIPHIER_RESETX((id), 0x200c, 0)
  50. #define UNIPHIER_LD11_SYS_RESET_EMMC(id) \
  51. UNIPHIER_RESETX((id), 0x200c, 2)
  52. #define UNIPHIER_SLD3_SYS_RESET_STDMAC(id) \
  53. UNIPHIER_RESETX((id), 0x2000, 10)
  54. #define UNIPHIER_LD11_SYS_RESET_STDMAC(id) \
  55. UNIPHIER_RESETX((id), 0x200c, 8)
  56. #define UNIPHIER_PRO4_SYS_RESET_GIO(id) \
  57. UNIPHIER_RESETX((id), 0x2000, 6)
  58. #define UNIPHIER_LD20_SYS_RESET_GIO(id) \
  59. UNIPHIER_RESETX((id), 0x200c, 5)
  60. #define UNIPHIER_PRO4_SYS_RESET_USB3(id, ch) \
  61. UNIPHIER_RESETX((id), 0x2000 + 0x4 * (ch), 17)
  62. static const struct uniphier_reset_data uniphier_sld3_sys_reset_data[] = {
  63. UNIPHIER_SLD3_SYS_RESET_NAND(2),
  64. UNIPHIER_SLD3_SYS_RESET_STDMAC(8), /* Ether, HSC, MIO */
  65. UNIPHIER_RESET_END,
  66. };
  67. static const struct uniphier_reset_data uniphier_pro4_sys_reset_data[] = {
  68. UNIPHIER_SLD3_SYS_RESET_NAND(2),
  69. UNIPHIER_SLD3_SYS_RESET_STDMAC(8), /* HSC, MIO, RLE */
  70. UNIPHIER_PRO4_SYS_RESET_GIO(12), /* Ether, SATA, USB3 */
  71. UNIPHIER_PRO4_SYS_RESET_USB3(14, 0),
  72. UNIPHIER_PRO4_SYS_RESET_USB3(15, 1),
  73. UNIPHIER_RESET_END,
  74. };
  75. static const struct uniphier_reset_data uniphier_pro5_sys_reset_data[] = {
  76. UNIPHIER_SLD3_SYS_RESET_NAND(2),
  77. UNIPHIER_SLD3_SYS_RESET_STDMAC(8), /* HSC */
  78. UNIPHIER_PRO4_SYS_RESET_GIO(12), /* PCIe, USB3 */
  79. UNIPHIER_PRO4_SYS_RESET_USB3(14, 0),
  80. UNIPHIER_PRO4_SYS_RESET_USB3(15, 1),
  81. UNIPHIER_RESET_END,
  82. };
  83. static const struct uniphier_reset_data uniphier_pxs2_sys_reset_data[] = {
  84. UNIPHIER_SLD3_SYS_RESET_NAND(2),
  85. UNIPHIER_SLD3_SYS_RESET_STDMAC(8), /* HSC, RLE */
  86. UNIPHIER_PRO4_SYS_RESET_USB3(14, 0),
  87. UNIPHIER_PRO4_SYS_RESET_USB3(15, 1),
  88. UNIPHIER_RESETX(16, 0x2014, 4), /* USB30-PHY0 */
  89. UNIPHIER_RESETX(17, 0x2014, 0), /* USB30-PHY1 */
  90. UNIPHIER_RESETX(18, 0x2014, 2), /* USB30-PHY2 */
  91. UNIPHIER_RESETX(20, 0x2014, 5), /* USB31-PHY0 */
  92. UNIPHIER_RESETX(21, 0x2014, 1), /* USB31-PHY1 */
  93. UNIPHIER_RESETX(28, 0x2014, 12), /* SATA */
  94. UNIPHIER_RESET(29, 0x2014, 8), /* SATA-PHY (active high) */
  95. UNIPHIER_RESET_END,
  96. };
  97. static const struct uniphier_reset_data uniphier_ld11_sys_reset_data[] = {
  98. UNIPHIER_LD11_SYS_RESET_NAND(2),
  99. UNIPHIER_LD11_SYS_RESET_EMMC(4),
  100. UNIPHIER_LD11_SYS_RESET_STDMAC(8), /* HSC, MIO */
  101. UNIPHIER_RESET_END,
  102. };
  103. static const struct uniphier_reset_data uniphier_ld20_sys_reset_data[] = {
  104. UNIPHIER_LD11_SYS_RESET_NAND(2),
  105. UNIPHIER_LD11_SYS_RESET_EMMC(4),
  106. UNIPHIER_LD11_SYS_RESET_STDMAC(8), /* HSC */
  107. UNIPHIER_LD20_SYS_RESET_GIO(12), /* PCIe, USB3 */
  108. UNIPHIER_RESETX(16, 0x200c, 12), /* USB30-PHY0 */
  109. UNIPHIER_RESETX(17, 0x200c, 13), /* USB30-PHY1 */
  110. UNIPHIER_RESETX(18, 0x200c, 14), /* USB30-PHY2 */
  111. UNIPHIER_RESETX(19, 0x200c, 15), /* USB30-PHY3 */
  112. UNIPHIER_RESET_END,
  113. };
  114. /* Media I/O reset data */
  115. #define UNIPHIER_MIO_RESET_SD(id, ch) \
  116. UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 0)
  117. #define UNIPHIER_MIO_RESET_SD_BRIDGE(id, ch) \
  118. UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 26)
  119. #define UNIPHIER_MIO_RESET_EMMC_HW_RESET(id, ch) \
  120. UNIPHIER_RESETX((id), 0x80 + 0x200 * (ch), 0)
  121. #define UNIPHIER_MIO_RESET_USB2(id, ch) \
  122. UNIPHIER_RESETX((id), 0x114 + 0x200 * (ch), 0)
  123. #define UNIPHIER_MIO_RESET_USB2_BRIDGE(id, ch) \
  124. UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 24)
  125. #define UNIPHIER_MIO_RESET_DMAC(id) \
  126. UNIPHIER_RESETX((id), 0x110, 17)
  127. static const struct uniphier_reset_data uniphier_sld3_mio_reset_data[] = {
  128. UNIPHIER_MIO_RESET_SD(0, 0),
  129. UNIPHIER_MIO_RESET_SD(1, 1),
  130. UNIPHIER_MIO_RESET_SD(2, 2),
  131. UNIPHIER_MIO_RESET_SD_BRIDGE(3, 0),
  132. UNIPHIER_MIO_RESET_SD_BRIDGE(4, 1),
  133. UNIPHIER_MIO_RESET_SD_BRIDGE(5, 2),
  134. UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
  135. UNIPHIER_MIO_RESET_DMAC(7),
  136. UNIPHIER_MIO_RESET_USB2(8, 0),
  137. UNIPHIER_MIO_RESET_USB2(9, 1),
  138. UNIPHIER_MIO_RESET_USB2(10, 2),
  139. UNIPHIER_MIO_RESET_USB2(11, 3),
  140. UNIPHIER_MIO_RESET_USB2_BRIDGE(12, 0),
  141. UNIPHIER_MIO_RESET_USB2_BRIDGE(13, 1),
  142. UNIPHIER_MIO_RESET_USB2_BRIDGE(14, 2),
  143. UNIPHIER_MIO_RESET_USB2_BRIDGE(15, 3),
  144. UNIPHIER_RESET_END,
  145. };
  146. static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
  147. UNIPHIER_MIO_RESET_SD(0, 0),
  148. UNIPHIER_MIO_RESET_SD(1, 1),
  149. UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
  150. UNIPHIER_RESET_END,
  151. };
  152. /* Peripheral reset data */
  153. #define UNIPHIER_PERI_RESET_UART(id, ch) \
  154. UNIPHIER_RESETX((id), 0x114, 19 + (ch))
  155. #define UNIPHIER_PERI_RESET_I2C(id, ch) \
  156. UNIPHIER_RESETX((id), 0x114, 5 + (ch))
  157. #define UNIPHIER_PERI_RESET_FI2C(id, ch) \
  158. UNIPHIER_RESETX((id), 0x114, 24 + (ch))
  159. static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
  160. UNIPHIER_PERI_RESET_UART(0, 0),
  161. UNIPHIER_PERI_RESET_UART(1, 1),
  162. UNIPHIER_PERI_RESET_UART(2, 2),
  163. UNIPHIER_PERI_RESET_UART(3, 3),
  164. UNIPHIER_PERI_RESET_I2C(4, 0),
  165. UNIPHIER_PERI_RESET_I2C(5, 1),
  166. UNIPHIER_PERI_RESET_I2C(6, 2),
  167. UNIPHIER_PERI_RESET_I2C(7, 3),
  168. UNIPHIER_PERI_RESET_I2C(8, 4),
  169. UNIPHIER_RESET_END,
  170. };
  171. static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
  172. UNIPHIER_PERI_RESET_UART(0, 0),
  173. UNIPHIER_PERI_RESET_UART(1, 1),
  174. UNIPHIER_PERI_RESET_UART(2, 2),
  175. UNIPHIER_PERI_RESET_UART(3, 3),
  176. UNIPHIER_PERI_RESET_FI2C(4, 0),
  177. UNIPHIER_PERI_RESET_FI2C(5, 1),
  178. UNIPHIER_PERI_RESET_FI2C(6, 2),
  179. UNIPHIER_PERI_RESET_FI2C(7, 3),
  180. UNIPHIER_PERI_RESET_FI2C(8, 4),
  181. UNIPHIER_PERI_RESET_FI2C(9, 5),
  182. UNIPHIER_PERI_RESET_FI2C(10, 6),
  183. UNIPHIER_RESET_END,
  184. };
  185. /* core implementaton */
  186. struct uniphier_reset_priv {
  187. struct reset_controller_dev rcdev;
  188. struct device *dev;
  189. struct regmap *regmap;
  190. const struct uniphier_reset_data *data;
  191. };
  192. #define to_uniphier_reset_priv(_rcdev) \
  193. container_of(_rcdev, struct uniphier_reset_priv, rcdev)
  194. static int uniphier_reset_update(struct reset_controller_dev *rcdev,
  195. unsigned long id, int assert)
  196. {
  197. struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
  198. const struct uniphier_reset_data *p;
  199. for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
  200. unsigned int mask, val;
  201. if (p->id != id)
  202. continue;
  203. mask = BIT(p->bit);
  204. if (assert)
  205. val = mask;
  206. else
  207. val = ~mask;
  208. if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
  209. val = ~val;
  210. return regmap_write_bits(priv->regmap, p->reg, mask, val);
  211. }
  212. dev_err(priv->dev, "reset_id=%lu was not handled\n", id);
  213. return -EINVAL;
  214. }
  215. static int uniphier_reset_assert(struct reset_controller_dev *rcdev,
  216. unsigned long id)
  217. {
  218. return uniphier_reset_update(rcdev, id, 1);
  219. }
  220. static int uniphier_reset_deassert(struct reset_controller_dev *rcdev,
  221. unsigned long id)
  222. {
  223. return uniphier_reset_update(rcdev, id, 0);
  224. }
  225. static int uniphier_reset_status(struct reset_controller_dev *rcdev,
  226. unsigned long id)
  227. {
  228. struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
  229. const struct uniphier_reset_data *p;
  230. for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
  231. unsigned int val;
  232. int ret, asserted;
  233. if (p->id != id)
  234. continue;
  235. ret = regmap_read(priv->regmap, p->reg, &val);
  236. if (ret)
  237. return ret;
  238. asserted = !!(val & BIT(p->bit));
  239. if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
  240. asserted = !asserted;
  241. return asserted;
  242. }
  243. dev_err(priv->dev, "reset_id=%lu was not found\n", id);
  244. return -EINVAL;
  245. }
  246. static const struct reset_control_ops uniphier_reset_ops = {
  247. .assert = uniphier_reset_assert,
  248. .deassert = uniphier_reset_deassert,
  249. .status = uniphier_reset_status,
  250. };
  251. static int uniphier_reset_probe(struct platform_device *pdev)
  252. {
  253. struct device *dev = &pdev->dev;
  254. struct uniphier_reset_priv *priv;
  255. const struct uniphier_reset_data *p, *data;
  256. struct regmap *regmap;
  257. struct device_node *parent;
  258. unsigned int nr_resets = 0;
  259. data = of_device_get_match_data(dev);
  260. if (WARN_ON(!data))
  261. return -EINVAL;
  262. parent = of_get_parent(dev->of_node); /* parent should be syscon node */
  263. regmap = syscon_node_to_regmap(parent);
  264. of_node_put(parent);
  265. if (IS_ERR(regmap)) {
  266. dev_err(dev, "failed to get regmap (error %ld)\n",
  267. PTR_ERR(regmap));
  268. return PTR_ERR(regmap);
  269. }
  270. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  271. if (!priv)
  272. return -ENOMEM;
  273. for (p = data; p->id != UNIPHIER_RESET_ID_END; p++)
  274. nr_resets = max(nr_resets, p->id + 1);
  275. priv->rcdev.ops = &uniphier_reset_ops;
  276. priv->rcdev.owner = dev->driver->owner;
  277. priv->rcdev.of_node = dev->of_node;
  278. priv->rcdev.nr_resets = nr_resets;
  279. priv->dev = dev;
  280. priv->regmap = regmap;
  281. priv->data = data;
  282. return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
  283. }
  284. static const struct of_device_id uniphier_reset_match[] = {
  285. /* System reset */
  286. {
  287. .compatible = "socionext,uniphier-sld3-reset",
  288. .data = uniphier_sld3_sys_reset_data,
  289. },
  290. {
  291. .compatible = "socionext,uniphier-ld4-reset",
  292. .data = uniphier_sld3_sys_reset_data,
  293. },
  294. {
  295. .compatible = "socionext,uniphier-pro4-reset",
  296. .data = uniphier_pro4_sys_reset_data,
  297. },
  298. {
  299. .compatible = "socionext,uniphier-sld8-reset",
  300. .data = uniphier_sld3_sys_reset_data,
  301. },
  302. {
  303. .compatible = "socionext,uniphier-pro5-reset",
  304. .data = uniphier_pro5_sys_reset_data,
  305. },
  306. {
  307. .compatible = "socionext,uniphier-pxs2-reset",
  308. .data = uniphier_pxs2_sys_reset_data,
  309. },
  310. {
  311. .compatible = "socionext,uniphier-ld11-reset",
  312. .data = uniphier_ld11_sys_reset_data,
  313. },
  314. {
  315. .compatible = "socionext,uniphier-ld20-reset",
  316. .data = uniphier_ld20_sys_reset_data,
  317. },
  318. /* Media I/O reset, SD reset */
  319. {
  320. .compatible = "socionext,uniphier-sld3-mio-reset",
  321. .data = uniphier_sld3_mio_reset_data,
  322. },
  323. {
  324. .compatible = "socionext,uniphier-ld4-mio-reset",
  325. .data = uniphier_sld3_mio_reset_data,
  326. },
  327. {
  328. .compatible = "socionext,uniphier-pro4-mio-reset",
  329. .data = uniphier_sld3_mio_reset_data,
  330. },
  331. {
  332. .compatible = "socionext,uniphier-sld8-mio-reset",
  333. .data = uniphier_sld3_mio_reset_data,
  334. },
  335. {
  336. .compatible = "socionext,uniphier-pro5-sd-reset",
  337. .data = uniphier_pro5_sd_reset_data,
  338. },
  339. {
  340. .compatible = "socionext,uniphier-pxs2-sd-reset",
  341. .data = uniphier_pro5_sd_reset_data,
  342. },
  343. {
  344. .compatible = "socionext,uniphier-ld11-mio-reset",
  345. .data = uniphier_sld3_mio_reset_data,
  346. },
  347. {
  348. .compatible = "socionext,uniphier-ld11-sd-reset",
  349. .data = uniphier_pro5_sd_reset_data,
  350. },
  351. {
  352. .compatible = "socionext,uniphier-ld20-sd-reset",
  353. .data = uniphier_pro5_sd_reset_data,
  354. },
  355. /* Peripheral reset */
  356. {
  357. .compatible = "socionext,uniphier-ld4-peri-reset",
  358. .data = uniphier_ld4_peri_reset_data,
  359. },
  360. {
  361. .compatible = "socionext,uniphier-pro4-peri-reset",
  362. .data = uniphier_pro4_peri_reset_data,
  363. },
  364. {
  365. .compatible = "socionext,uniphier-sld8-peri-reset",
  366. .data = uniphier_ld4_peri_reset_data,
  367. },
  368. {
  369. .compatible = "socionext,uniphier-pro5-peri-reset",
  370. .data = uniphier_pro4_peri_reset_data,
  371. },
  372. {
  373. .compatible = "socionext,uniphier-pxs2-peri-reset",
  374. .data = uniphier_pro4_peri_reset_data,
  375. },
  376. {
  377. .compatible = "socionext,uniphier-ld11-peri-reset",
  378. .data = uniphier_pro4_peri_reset_data,
  379. },
  380. {
  381. .compatible = "socionext,uniphier-ld20-peri-reset",
  382. .data = uniphier_pro4_peri_reset_data,
  383. },
  384. { /* sentinel */ }
  385. };
  386. MODULE_DEVICE_TABLE(of, uniphier_reset_match);
  387. static struct platform_driver uniphier_reset_driver = {
  388. .probe = uniphier_reset_probe,
  389. .driver = {
  390. .name = "uniphier-reset",
  391. .of_match_table = uniphier_reset_match,
  392. },
  393. };
  394. module_platform_driver(uniphier_reset_driver);
  395. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  396. MODULE_DESCRIPTION("UniPhier Reset Controller Driver");
  397. MODULE_LICENSE("GPL");