ssbi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2010, Google Inc.
  4. *
  5. * Original authors: Code Aurora Forum
  6. *
  7. * Author: Dima Zavin <[email protected]>
  8. * - Largely rewritten from original to not be an i2c driver.
  9. */
  10. #define pr_fmt(fmt) "%s: " fmt, __func__
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/ssbi.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. /* SSBI 2.0 controller registers */
  22. #define SSBI2_CMD 0x0008
  23. #define SSBI2_RD 0x0010
  24. #define SSBI2_STATUS 0x0014
  25. #define SSBI2_MODE2 0x001C
  26. /* SSBI_CMD fields */
  27. #define SSBI_CMD_RDWRN (1 << 24)
  28. /* SSBI_STATUS fields */
  29. #define SSBI_STATUS_RD_READY (1 << 2)
  30. #define SSBI_STATUS_READY (1 << 1)
  31. #define SSBI_STATUS_MCHN_BUSY (1 << 0)
  32. /* SSBI_MODE2 fields */
  33. #define SSBI_MODE2_REG_ADDR_15_8_SHFT 0x04
  34. #define SSBI_MODE2_REG_ADDR_15_8_MASK (0x7f << SSBI_MODE2_REG_ADDR_15_8_SHFT)
  35. #define SET_SSBI_MODE2_REG_ADDR_15_8(MD, AD) \
  36. (((MD) & 0x0F) | ((((AD) >> 8) << SSBI_MODE2_REG_ADDR_15_8_SHFT) & \
  37. SSBI_MODE2_REG_ADDR_15_8_MASK))
  38. /* SSBI PMIC Arbiter command registers */
  39. #define SSBI_PA_CMD 0x0000
  40. #define SSBI_PA_RD_STATUS 0x0004
  41. /* SSBI_PA_CMD fields */
  42. #define SSBI_PA_CMD_RDWRN (1 << 24)
  43. #define SSBI_PA_CMD_ADDR_MASK 0x7fff /* REG_ADDR_7_0, REG_ADDR_8_14*/
  44. /* SSBI_PA_RD_STATUS fields */
  45. #define SSBI_PA_RD_STATUS_TRANS_DONE (1 << 27)
  46. #define SSBI_PA_RD_STATUS_TRANS_DENIED (1 << 26)
  47. #define SSBI_TIMEOUT_US 100
  48. enum ssbi_controller_type {
  49. MSM_SBI_CTRL_SSBI = 0,
  50. MSM_SBI_CTRL_SSBI2,
  51. MSM_SBI_CTRL_PMIC_ARBITER,
  52. };
  53. struct ssbi {
  54. struct device *slave;
  55. void __iomem *base;
  56. spinlock_t lock;
  57. enum ssbi_controller_type controller_type;
  58. int (*read)(struct ssbi *, u16 addr, u8 *buf, int len);
  59. int (*write)(struct ssbi *, u16 addr, const u8 *buf, int len);
  60. };
  61. static inline u32 ssbi_readl(struct ssbi *ssbi, u32 reg)
  62. {
  63. return readl(ssbi->base + reg);
  64. }
  65. static inline void ssbi_writel(struct ssbi *ssbi, u32 val, u32 reg)
  66. {
  67. writel(val, ssbi->base + reg);
  68. }
  69. /*
  70. * Via private exchange with one of the original authors, the hardware
  71. * should generally finish a transaction in about 5us. The worst
  72. * case, is when using the arbiter and both other CPUs have just
  73. * started trying to use the SSBI bus will result in a time of about
  74. * 20us. It should never take longer than this.
  75. *
  76. * As such, this wait merely spins, with a udelay.
  77. */
  78. static int ssbi_wait_mask(struct ssbi *ssbi, u32 set_mask, u32 clr_mask)
  79. {
  80. u32 timeout = SSBI_TIMEOUT_US;
  81. u32 val;
  82. while (timeout--) {
  83. val = ssbi_readl(ssbi, SSBI2_STATUS);
  84. if (((val & set_mask) == set_mask) && ((val & clr_mask) == 0))
  85. return 0;
  86. udelay(1);
  87. }
  88. return -ETIMEDOUT;
  89. }
  90. static int
  91. ssbi_read_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
  92. {
  93. u32 cmd = SSBI_CMD_RDWRN | ((addr & 0xff) << 16);
  94. int ret = 0;
  95. if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
  96. u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
  97. mode2 = SET_SSBI_MODE2_REG_ADDR_15_8(mode2, addr);
  98. ssbi_writel(ssbi, mode2, SSBI2_MODE2);
  99. }
  100. while (len) {
  101. ret = ssbi_wait_mask(ssbi, SSBI_STATUS_READY, 0);
  102. if (ret)
  103. goto err;
  104. ssbi_writel(ssbi, cmd, SSBI2_CMD);
  105. ret = ssbi_wait_mask(ssbi, SSBI_STATUS_RD_READY, 0);
  106. if (ret)
  107. goto err;
  108. *buf++ = ssbi_readl(ssbi, SSBI2_RD) & 0xff;
  109. len--;
  110. }
  111. err:
  112. return ret;
  113. }
  114. static int
  115. ssbi_write_bytes(struct ssbi *ssbi, u16 addr, const u8 *buf, int len)
  116. {
  117. int ret = 0;
  118. if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
  119. u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
  120. mode2 = SET_SSBI_MODE2_REG_ADDR_15_8(mode2, addr);
  121. ssbi_writel(ssbi, mode2, SSBI2_MODE2);
  122. }
  123. while (len) {
  124. ret = ssbi_wait_mask(ssbi, SSBI_STATUS_READY, 0);
  125. if (ret)
  126. goto err;
  127. ssbi_writel(ssbi, ((addr & 0xff) << 16) | *buf, SSBI2_CMD);
  128. ret = ssbi_wait_mask(ssbi, 0, SSBI_STATUS_MCHN_BUSY);
  129. if (ret)
  130. goto err;
  131. buf++;
  132. len--;
  133. }
  134. err:
  135. return ret;
  136. }
  137. /*
  138. * See ssbi_wait_mask for an explanation of the time and the
  139. * busywait.
  140. */
  141. static inline int
  142. ssbi_pa_transfer(struct ssbi *ssbi, u32 cmd, u8 *data)
  143. {
  144. u32 timeout = SSBI_TIMEOUT_US;
  145. u32 rd_status = 0;
  146. ssbi_writel(ssbi, cmd, SSBI_PA_CMD);
  147. while (timeout--) {
  148. rd_status = ssbi_readl(ssbi, SSBI_PA_RD_STATUS);
  149. if (rd_status & SSBI_PA_RD_STATUS_TRANS_DENIED)
  150. return -EPERM;
  151. if (rd_status & SSBI_PA_RD_STATUS_TRANS_DONE) {
  152. if (data)
  153. *data = rd_status & 0xff;
  154. return 0;
  155. }
  156. udelay(1);
  157. }
  158. return -ETIMEDOUT;
  159. }
  160. static int
  161. ssbi_pa_read_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
  162. {
  163. u32 cmd;
  164. int ret = 0;
  165. cmd = SSBI_PA_CMD_RDWRN | (addr & SSBI_PA_CMD_ADDR_MASK) << 8;
  166. while (len) {
  167. ret = ssbi_pa_transfer(ssbi, cmd, buf);
  168. if (ret)
  169. goto err;
  170. buf++;
  171. len--;
  172. }
  173. err:
  174. return ret;
  175. }
  176. static int
  177. ssbi_pa_write_bytes(struct ssbi *ssbi, u16 addr, const u8 *buf, int len)
  178. {
  179. u32 cmd;
  180. int ret = 0;
  181. while (len) {
  182. cmd = (addr & SSBI_PA_CMD_ADDR_MASK) << 8 | *buf;
  183. ret = ssbi_pa_transfer(ssbi, cmd, NULL);
  184. if (ret)
  185. goto err;
  186. buf++;
  187. len--;
  188. }
  189. err:
  190. return ret;
  191. }
  192. int ssbi_read(struct device *dev, u16 addr, u8 *buf, int len)
  193. {
  194. struct ssbi *ssbi = dev_get_drvdata(dev);
  195. unsigned long flags;
  196. int ret;
  197. spin_lock_irqsave(&ssbi->lock, flags);
  198. ret = ssbi->read(ssbi, addr, buf, len);
  199. spin_unlock_irqrestore(&ssbi->lock, flags);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL_GPL(ssbi_read);
  203. int ssbi_write(struct device *dev, u16 addr, const u8 *buf, int len)
  204. {
  205. struct ssbi *ssbi = dev_get_drvdata(dev);
  206. unsigned long flags;
  207. int ret;
  208. spin_lock_irqsave(&ssbi->lock, flags);
  209. ret = ssbi->write(ssbi, addr, buf, len);
  210. spin_unlock_irqrestore(&ssbi->lock, flags);
  211. return ret;
  212. }
  213. EXPORT_SYMBOL_GPL(ssbi_write);
  214. static int ssbi_probe(struct platform_device *pdev)
  215. {
  216. struct device_node *np = pdev->dev.of_node;
  217. struct resource *mem_res;
  218. struct ssbi *ssbi;
  219. const char *type;
  220. ssbi = devm_kzalloc(&pdev->dev, sizeof(*ssbi), GFP_KERNEL);
  221. if (!ssbi)
  222. return -ENOMEM;
  223. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  224. ssbi->base = devm_ioremap_resource(&pdev->dev, mem_res);
  225. if (IS_ERR(ssbi->base))
  226. return PTR_ERR(ssbi->base);
  227. platform_set_drvdata(pdev, ssbi);
  228. type = of_get_property(np, "qcom,controller-type", NULL);
  229. if (type == NULL) {
  230. dev_err(&pdev->dev, "Missing qcom,controller-type property\n");
  231. return -EINVAL;
  232. }
  233. dev_info(&pdev->dev, "SSBI controller type: '%s'\n", type);
  234. if (strcmp(type, "ssbi") == 0)
  235. ssbi->controller_type = MSM_SBI_CTRL_SSBI;
  236. else if (strcmp(type, "ssbi2") == 0)
  237. ssbi->controller_type = MSM_SBI_CTRL_SSBI2;
  238. else if (strcmp(type, "pmic-arbiter") == 0)
  239. ssbi->controller_type = MSM_SBI_CTRL_PMIC_ARBITER;
  240. else {
  241. dev_err(&pdev->dev, "Unknown qcom,controller-type\n");
  242. return -EINVAL;
  243. }
  244. if (ssbi->controller_type == MSM_SBI_CTRL_PMIC_ARBITER) {
  245. ssbi->read = ssbi_pa_read_bytes;
  246. ssbi->write = ssbi_pa_write_bytes;
  247. } else {
  248. ssbi->read = ssbi_read_bytes;
  249. ssbi->write = ssbi_write_bytes;
  250. }
  251. spin_lock_init(&ssbi->lock);
  252. return devm_of_platform_populate(&pdev->dev);
  253. }
  254. static const struct of_device_id ssbi_match_table[] = {
  255. { .compatible = "qcom,ssbi" },
  256. {}
  257. };
  258. MODULE_DEVICE_TABLE(of, ssbi_match_table);
  259. static struct platform_driver ssbi_driver = {
  260. .probe = ssbi_probe,
  261. .driver = {
  262. .name = "ssbi",
  263. .of_match_table = ssbi_match_table,
  264. },
  265. };
  266. module_platform_driver(ssbi_driver);
  267. MODULE_LICENSE("GPL v2");
  268. MODULE_VERSION("1.0");
  269. MODULE_ALIAS("platform:ssbi");
  270. MODULE_AUTHOR("Dima Zavin <[email protected]>");