spmi-pmic-arb-debug.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2018, 2020, The Linux Foundation. All rights reserved. */
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/spmi.h>
  13. /* PMIC Arbiter debug register offsets */
  14. #define PMIC_ARB_DEBUG_CMD0 0x00
  15. #define PMIC_ARB_DEBUG_CMD1 0x04
  16. #define PMIC_ARB_DEBUG_CMD2 0x08
  17. #define PMIC_ARB_DEBUG_CMD3 0x0C
  18. #define PMIC_ARB_DEBUG_STATUS 0x14
  19. #define PMIC_ARB_DEBUG_WDATA(n) (0x18 + 4 * (n))
  20. #define PMIC_ARB_DEBUG_RDATA(n) (0x38 + 4 * (n))
  21. /* Transaction status flag bits */
  22. enum pmic_arb_chnl_status {
  23. PMIC_ARB_STATUS_DONE = BIT(0),
  24. PMIC_ARB_STATUS_FAILURE = BIT(1),
  25. PMIC_ARB_STATUS_DENIED = BIT(2),
  26. PMIC_ARB_STATUS_DROPPED = BIT(3),
  27. };
  28. /* Command Opcodes */
  29. enum pmic_arb_cmd_op_code {
  30. PMIC_ARB_OP_EXT_WRITEL = 0,
  31. PMIC_ARB_OP_EXT_READL = 1,
  32. PMIC_ARB_OP_EXT_WRITE = 2,
  33. PMIC_ARB_OP_RESET = 3,
  34. PMIC_ARB_OP_SLEEP = 4,
  35. PMIC_ARB_OP_SHUTDOWN = 5,
  36. PMIC_ARB_OP_WAKEUP = 6,
  37. PMIC_ARB_OP_AUTHENTICATE = 7,
  38. PMIC_ARB_OP_MSTR_READ = 8,
  39. PMIC_ARB_OP_MSTR_WRITE = 9,
  40. PMIC_ARB_OP_EXT_READ = 13,
  41. PMIC_ARB_OP_WRITE = 14,
  42. PMIC_ARB_OP_READ = 15,
  43. PMIC_ARB_OP_ZERO_WRITE = 16,
  44. };
  45. #define PMIC_ARB_TIMEOUT_US 100
  46. #define PMIC_ARB_MAX_TRANS_BYTES 8
  47. #define PMIC_ARB_MAX_SID 0xF
  48. /**
  49. * spmi_pmic_arb_debug - SPMI PMIC Arbiter debug object
  50. *
  51. * @addr: base address of SPMI PMIC arbiter debug module
  52. * @lock: lock to synchronize accesses.
  53. */
  54. struct spmi_pmic_arb_debug {
  55. void __iomem *addr;
  56. raw_spinlock_t lock;
  57. struct clk *clock;
  58. };
  59. static inline void pmic_arb_debug_write(struct spmi_pmic_arb_debug *pa,
  60. u32 offset, u32 val)
  61. {
  62. writel_relaxed(val, pa->addr + offset);
  63. }
  64. static inline u32 pmic_arb_debug_read(struct spmi_pmic_arb_debug *pa,
  65. u32 offset)
  66. {
  67. return readl_relaxed(pa->addr + offset);
  68. }
  69. /* pa->lock must be held by the caller. */
  70. static int pmic_arb_debug_wait_for_done(struct spmi_controller *ctrl)
  71. {
  72. struct spmi_pmic_arb_debug *pa = spmi_controller_get_drvdata(ctrl);
  73. u32 status = 0;
  74. u32 timeout = PMIC_ARB_TIMEOUT_US;
  75. while (timeout--) {
  76. status = pmic_arb_debug_read(pa, PMIC_ARB_DEBUG_STATUS);
  77. if (status & PMIC_ARB_STATUS_DONE) {
  78. if (status & PMIC_ARB_STATUS_DENIED) {
  79. dev_err(&ctrl->dev, "%s: transaction denied (0x%x)\n",
  80. __func__, status);
  81. return -EPERM;
  82. }
  83. if (status & PMIC_ARB_STATUS_FAILURE) {
  84. dev_err(&ctrl->dev, "%s: transaction failed (0x%x)\n",
  85. __func__, status);
  86. return -EIO;
  87. }
  88. if (status & PMIC_ARB_STATUS_DROPPED) {
  89. dev_err(&ctrl->dev, "%s: transaction dropped (0x%x)\n",
  90. __func__, status);
  91. return -EIO;
  92. }
  93. return 0;
  94. }
  95. udelay(1);
  96. }
  97. dev_err(&ctrl->dev, "%s: timeout, status 0x%x\n", __func__, status);
  98. return -ETIMEDOUT;
  99. }
  100. /* pa->lock must be held by the caller. */
  101. static int pmic_arb_debug_issue_command(struct spmi_controller *ctrl, u8 opc,
  102. u8 sid, u16 addr, size_t len)
  103. {
  104. struct spmi_pmic_arb_debug *pa = spmi_controller_get_drvdata(ctrl);
  105. u16 pid = (addr >> 8) & 0xFF;
  106. u16 offset = addr & 0xFF;
  107. u8 byte_count = len - 1;
  108. if (byte_count >= PMIC_ARB_MAX_TRANS_BYTES) {
  109. dev_err(&ctrl->dev, "pmic-arb supports 1 to %d bytes per transaction, but %zu requested\n",
  110. PMIC_ARB_MAX_TRANS_BYTES, len);
  111. return -EINVAL;
  112. }
  113. if (sid > PMIC_ARB_MAX_SID) {
  114. dev_err(&ctrl->dev, "pmic-arb supports sid 0 to %u, but %u requested\n",
  115. PMIC_ARB_MAX_SID, sid);
  116. return -EINVAL;
  117. }
  118. pmic_arb_debug_write(pa, PMIC_ARB_DEBUG_CMD3, offset);
  119. pmic_arb_debug_write(pa, PMIC_ARB_DEBUG_CMD2, pid);
  120. pmic_arb_debug_write(pa, PMIC_ARB_DEBUG_CMD1, (byte_count << 4) | sid);
  121. /* Start the transaction */
  122. pmic_arb_debug_write(pa, PMIC_ARB_DEBUG_CMD0, opc << 1);
  123. return pmic_arb_debug_wait_for_done(ctrl);
  124. }
  125. /* Non-data command */
  126. static int pmic_arb_debug_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
  127. {
  128. dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
  129. /* Check for valid non-data command */
  130. if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
  131. return -EINVAL;
  132. return -EOPNOTSUPP;
  133. }
  134. static int pmic_arb_debug_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  135. u16 addr, u8 *buf, size_t len)
  136. {
  137. struct spmi_pmic_arb_debug *pa = spmi_controller_get_drvdata(ctrl);
  138. unsigned long flags;
  139. int i, rc;
  140. /* Check the opcode */
  141. if (opc >= 0x60 && opc <= 0x7F)
  142. opc = PMIC_ARB_OP_READ;
  143. else if (opc >= 0x20 && opc <= 0x2F)
  144. opc = PMIC_ARB_OP_EXT_READ;
  145. else if (opc >= 0x38 && opc <= 0x3F)
  146. opc = PMIC_ARB_OP_EXT_READL;
  147. else
  148. return -EINVAL;
  149. rc = clk_prepare_enable(pa->clock);
  150. if (rc) {
  151. pr_err("%s: failed to enable core clock, rc=%d\n",
  152. __func__, rc);
  153. return rc;
  154. }
  155. raw_spin_lock_irqsave(&pa->lock, flags);
  156. rc = pmic_arb_debug_issue_command(ctrl, opc, sid, addr, len);
  157. if (rc)
  158. goto done;
  159. /* Read data from FIFO */
  160. for (i = 0; i < len; i++)
  161. buf[i] = pmic_arb_debug_read(pa, PMIC_ARB_DEBUG_RDATA(i));
  162. done:
  163. raw_spin_unlock_irqrestore(&pa->lock, flags);
  164. clk_disable_unprepare(pa->clock);
  165. return rc;
  166. }
  167. static int pmic_arb_debug_write_cmd(struct spmi_controller *ctrl, u8 opc,
  168. u8 sid, u16 addr, const u8 *buf, size_t len)
  169. {
  170. struct spmi_pmic_arb_debug *pa = spmi_controller_get_drvdata(ctrl);
  171. unsigned long flags;
  172. int i, rc;
  173. if (len > PMIC_ARB_MAX_TRANS_BYTES) {
  174. dev_err(&ctrl->dev, "pmic-arb supports 1 to %d bytes per transaction, but %zu requested\n",
  175. PMIC_ARB_MAX_TRANS_BYTES, len);
  176. return -EINVAL;
  177. }
  178. /* Check the opcode */
  179. if (opc >= 0x40 && opc <= 0x5F)
  180. opc = PMIC_ARB_OP_WRITE;
  181. else if (opc >= 0x00 && opc <= 0x0F)
  182. opc = PMIC_ARB_OP_EXT_WRITE;
  183. else if (opc >= 0x30 && opc <= 0x37)
  184. opc = PMIC_ARB_OP_EXT_WRITEL;
  185. else if (opc >= 0x80)
  186. opc = PMIC_ARB_OP_ZERO_WRITE;
  187. else
  188. return -EINVAL;
  189. rc = clk_prepare_enable(pa->clock);
  190. if (rc) {
  191. pr_err("%s: failed to enable core clock, rc=%d\n",
  192. __func__, rc);
  193. return rc;
  194. }
  195. raw_spin_lock_irqsave(&pa->lock, flags);
  196. /* Write data to FIFO */
  197. for (i = 0; i < len; i++)
  198. pmic_arb_debug_write(pa, PMIC_ARB_DEBUG_WDATA(i), buf[i]);
  199. rc = pmic_arb_debug_issue_command(ctrl, opc, sid, addr, len);
  200. raw_spin_unlock_irqrestore(&pa->lock, flags);
  201. clk_disable_unprepare(pa->clock);
  202. return rc;
  203. }
  204. static int spmi_pmic_arb_debug_probe(struct platform_device *pdev)
  205. {
  206. struct spmi_pmic_arb_debug *pa;
  207. struct spmi_controller *ctrl;
  208. struct resource *res;
  209. int rc;
  210. u32 fuse_val, fuse_bit;
  211. void __iomem *fuse_addr;
  212. bool is_disable_fuse = true;
  213. /* Check if the debug bus is enabled or disabled by a fuse. */
  214. rc = of_property_read_u32(pdev->dev.of_node, "qcom,fuse-disable-bit",
  215. &fuse_bit);
  216. if (rc) {
  217. is_disable_fuse = false;
  218. rc = of_property_read_u32(pdev->dev.of_node,
  219. "qcom,fuse-enable-bit",
  220. &fuse_bit);
  221. }
  222. if (!rc) {
  223. if (fuse_bit > 31) {
  224. dev_err(&pdev->dev, "qcom,fuse-%s-bit supports values 0 to 31, but %u specified\n",
  225. is_disable_fuse ? "disable" : "enable",
  226. fuse_bit);
  227. return -EINVAL;
  228. }
  229. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  230. "fuse");
  231. if (!res) {
  232. dev_err(&pdev->dev, "fuse address not specified\n");
  233. return -EINVAL;
  234. }
  235. fuse_addr = ioremap(res->start, resource_size(res));
  236. if (!fuse_addr)
  237. return -EINVAL;
  238. fuse_val = readl_relaxed(fuse_addr);
  239. iounmap(fuse_addr);
  240. if (!!(fuse_val & BIT(fuse_bit)) == is_disable_fuse) {
  241. dev_err(&pdev->dev, "SPMI PMIC arbiter debug bus disabled by fuse\n");
  242. return -ENODEV;
  243. }
  244. }
  245. ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
  246. if (!ctrl)
  247. return -ENOMEM;
  248. pa = spmi_controller_get_drvdata(ctrl);
  249. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
  250. if (!res) {
  251. dev_err(&pdev->dev, "core address not specified\n");
  252. rc = -EINVAL;
  253. goto err_put_ctrl;
  254. }
  255. pa->addr = devm_ioremap_resource(&ctrl->dev, res);
  256. if (IS_ERR(pa->addr)) {
  257. rc = PTR_ERR(pa->addr);
  258. goto err_put_ctrl;
  259. }
  260. if (of_find_property(pdev->dev.of_node, "clock-names", NULL)) {
  261. pa->clock = devm_clk_get(&pdev->dev, "core_clk");
  262. if (IS_ERR(pa->clock)) {
  263. rc = PTR_ERR(pa->clock);
  264. if (rc != -EPROBE_DEFER)
  265. dev_err(&pdev->dev, "unable to request core clock, rc=%d\n",
  266. rc);
  267. goto err_put_ctrl;
  268. }
  269. }
  270. platform_set_drvdata(pdev, ctrl);
  271. raw_spin_lock_init(&pa->lock);
  272. ctrl->cmd = pmic_arb_debug_cmd;
  273. ctrl->read_cmd = pmic_arb_debug_read_cmd;
  274. ctrl->write_cmd = pmic_arb_debug_write_cmd;
  275. rc = spmi_controller_add(ctrl);
  276. if (rc)
  277. goto err_put_ctrl;
  278. dev_info(&ctrl->dev, "SPMI PMIC arbiter debug bus controller added\n");
  279. return 0;
  280. err_put_ctrl:
  281. spmi_controller_put(ctrl);
  282. return rc;
  283. }
  284. static int spmi_pmic_arb_debug_remove(struct platform_device *pdev)
  285. {
  286. struct spmi_controller *ctrl = platform_get_drvdata(pdev);
  287. spmi_controller_remove(ctrl);
  288. spmi_controller_put(ctrl);
  289. return 0;
  290. }
  291. static const struct of_device_id spmi_pmic_arb_debug_match_table[] = {
  292. { .compatible = "qcom,spmi-pmic-arb-debug", },
  293. {},
  294. };
  295. MODULE_DEVICE_TABLE(of, spmi_pmic_arb_debug_match_table);
  296. static struct platform_driver spmi_pmic_arb_debug_driver = {
  297. .probe = spmi_pmic_arb_debug_probe,
  298. .remove = spmi_pmic_arb_debug_remove,
  299. .driver = {
  300. .name = "spmi_pmic_arb_debug",
  301. .of_match_table = spmi_pmic_arb_debug_match_table,
  302. },
  303. };
  304. module_platform_driver(spmi_pmic_arb_debug_driver);
  305. MODULE_LICENSE("GPL v2");
  306. MODULE_ALIAS("platform:spmi_pmic_arb_debug");