reset-uniphier.c 12 KB

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