ssbi.c 7.9 KB

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