reset-uniphier.c 12 KB

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