spmi-pmic-arb-debug.c 9.7 KB

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