ssbi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. struct ssbi {
  57. struct device *slave;
  58. void __iomem *base;
  59. spinlock_t lock;
  60. enum ssbi_controller_type controller_type;
  61. int (*read)(struct ssbi *, u16 addr, u8 *buf, int len);
  62. int (*write)(struct ssbi *, u16 addr, u8 *buf, int len);
  63. };
  64. #define to_ssbi(dev) platform_get_drvdata(to_platform_device(dev))
  65. static inline u32 ssbi_readl(struct ssbi *ssbi, u32 reg)
  66. {
  67. return readl(ssbi->base + reg);
  68. }
  69. static inline void ssbi_writel(struct ssbi *ssbi, u32 val, u32 reg)
  70. {
  71. writel(val, ssbi->base + reg);
  72. }
  73. /*
  74. * Via private exchange with one of the original authors, the hardware
  75. * should generally finish a transaction in about 5us. The worst
  76. * case, is when using the arbiter and both other CPUs have just
  77. * started trying to use the SSBI bus will result in a time of about
  78. * 20us. It should never take longer than this.
  79. *
  80. * As such, this wait merely spins, with a udelay.
  81. */
  82. static int ssbi_wait_mask(struct ssbi *ssbi, u32 set_mask, u32 clr_mask)
  83. {
  84. u32 timeout = SSBI_TIMEOUT_US;
  85. u32 val;
  86. while (timeout--) {
  87. val = ssbi_readl(ssbi, SSBI2_STATUS);
  88. if (((val & set_mask) == set_mask) && ((val & clr_mask) == 0))
  89. return 0;
  90. udelay(1);
  91. }
  92. return -ETIMEDOUT;
  93. }
  94. static int
  95. ssbi_read_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
  96. {
  97. u32 cmd = SSBI_CMD_RDWRN | ((addr & 0xff) << 16);
  98. int ret = 0;
  99. if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
  100. u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
  101. mode2 = SET_SSBI_MODE2_REG_ADDR_15_8(mode2, addr);
  102. ssbi_writel(ssbi, mode2, SSBI2_MODE2);
  103. }
  104. while (len) {
  105. ret = ssbi_wait_mask(ssbi, SSBI_STATUS_READY, 0);
  106. if (ret)
  107. goto err;
  108. ssbi_writel(ssbi, cmd, SSBI2_CMD);
  109. ret = ssbi_wait_mask(ssbi, SSBI_STATUS_RD_READY, 0);
  110. if (ret)
  111. goto err;
  112. *buf++ = ssbi_readl(ssbi, SSBI2_RD) & 0xff;
  113. len--;
  114. }
  115. err:
  116. return ret;
  117. }
  118. static int
  119. ssbi_write_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
  120. {
  121. int ret = 0;
  122. if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
  123. u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
  124. mode2 = SET_SSBI_MODE2_REG_ADDR_15_8(mode2, addr);
  125. ssbi_writel(ssbi, mode2, SSBI2_MODE2);
  126. }
  127. while (len) {
  128. ret = ssbi_wait_mask(ssbi, SSBI_STATUS_READY, 0);
  129. if (ret)
  130. goto err;
  131. ssbi_writel(ssbi, ((addr & 0xff) << 16) | *buf, SSBI2_CMD);
  132. ret = ssbi_wait_mask(ssbi, 0, SSBI_STATUS_MCHN_BUSY);
  133. if (ret)
  134. goto err;
  135. buf++;
  136. len--;
  137. }
  138. err:
  139. return ret;
  140. }
  141. /*
  142. * See ssbi_wait_mask for an explanation of the time and the
  143. * busywait.
  144. */
  145. static inline int
  146. ssbi_pa_transfer(struct ssbi *ssbi, u32 cmd, u8 *data)
  147. {
  148. u32 timeout = SSBI_TIMEOUT_US;
  149. u32 rd_status = 0;
  150. ssbi_writel(ssbi, cmd, SSBI_PA_CMD);
  151. while (timeout--) {
  152. rd_status = ssbi_readl(ssbi, SSBI_PA_RD_STATUS);
  153. if (rd_status & SSBI_PA_RD_STATUS_TRANS_DENIED)
  154. return -EPERM;
  155. if (rd_status & SSBI_PA_RD_STATUS_TRANS_DONE) {
  156. if (data)
  157. *data = rd_status & 0xff;
  158. return 0;
  159. }
  160. udelay(1);
  161. }
  162. return -ETIMEDOUT;
  163. }
  164. static int
  165. ssbi_pa_read_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
  166. {
  167. u32 cmd;
  168. int ret = 0;
  169. cmd = SSBI_PA_CMD_RDWRN | (addr & SSBI_PA_CMD_ADDR_MASK) << 8;
  170. while (len) {
  171. ret = ssbi_pa_transfer(ssbi, cmd, buf);
  172. if (ret)
  173. goto err;
  174. buf++;
  175. len--;
  176. }
  177. err:
  178. return ret;
  179. }
  180. static int
  181. ssbi_pa_write_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
  182. {
  183. u32 cmd;
  184. int ret = 0;
  185. while (len) {
  186. cmd = (addr & SSBI_PA_CMD_ADDR_MASK) << 8 | *buf;
  187. ret = ssbi_pa_transfer(ssbi, cmd, NULL);
  188. if (ret)
  189. goto err;
  190. buf++;
  191. len--;
  192. }
  193. err:
  194. return ret;
  195. }
  196. int ssbi_read(struct device *dev, u16 addr, u8 *buf, int len)
  197. {
  198. struct ssbi *ssbi = to_ssbi(dev);
  199. unsigned long flags;
  200. int ret;
  201. spin_lock_irqsave(&ssbi->lock, flags);
  202. ret = ssbi->read(ssbi, addr, buf, len);
  203. spin_unlock_irqrestore(&ssbi->lock, flags);
  204. return ret;
  205. }
  206. EXPORT_SYMBOL_GPL(ssbi_read);
  207. int ssbi_write(struct device *dev, u16 addr, u8 *buf, int len)
  208. {
  209. struct ssbi *ssbi = to_ssbi(dev);
  210. unsigned long flags;
  211. int ret;
  212. spin_lock_irqsave(&ssbi->lock, flags);
  213. ret = ssbi->write(ssbi, addr, buf, len);
  214. spin_unlock_irqrestore(&ssbi->lock, flags);
  215. return ret;
  216. }
  217. EXPORT_SYMBOL_GPL(ssbi_write);
  218. static int ssbi_probe(struct platform_device *pdev)
  219. {
  220. struct device_node *np = pdev->dev.of_node;
  221. struct resource *mem_res;
  222. struct ssbi *ssbi;
  223. const char *type;
  224. ssbi = devm_kzalloc(&pdev->dev, sizeof(*ssbi), GFP_KERNEL);
  225. if (!ssbi)
  226. return -ENOMEM;
  227. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  228. ssbi->base = devm_ioremap_resource(&pdev->dev, mem_res);
  229. if (IS_ERR(ssbi->base))
  230. return PTR_ERR(ssbi->base);
  231. platform_set_drvdata(pdev, ssbi);
  232. type = of_get_property(np, "qcom,controller-type", NULL);
  233. if (type == NULL) {
  234. dev_err(&pdev->dev, "Missing qcom,controller-type property\n");
  235. return -EINVAL;
  236. }
  237. dev_info(&pdev->dev, "SSBI controller type: '%s'\n", type);
  238. if (strcmp(type, "ssbi") == 0)
  239. ssbi->controller_type = MSM_SBI_CTRL_SSBI;
  240. else if (strcmp(type, "ssbi2") == 0)
  241. ssbi->controller_type = MSM_SBI_CTRL_SSBI2;
  242. else if (strcmp(type, "pmic-arbiter") == 0)
  243. ssbi->controller_type = MSM_SBI_CTRL_PMIC_ARBITER;
  244. else {
  245. dev_err(&pdev->dev, "Unknown qcom,controller-type\n");
  246. return -EINVAL;
  247. }
  248. if (ssbi->controller_type == MSM_SBI_CTRL_PMIC_ARBITER) {
  249. ssbi->read = ssbi_pa_read_bytes;
  250. ssbi->write = ssbi_pa_write_bytes;
  251. } else {
  252. ssbi->read = ssbi_read_bytes;
  253. ssbi->write = ssbi_write_bytes;
  254. }
  255. spin_lock_init(&ssbi->lock);
  256. return of_platform_populate(np, NULL, NULL, &pdev->dev);
  257. }
  258. static struct of_device_id ssbi_match_table[] = {
  259. { .compatible = "qcom,ssbi" },
  260. {}
  261. };
  262. MODULE_DEVICE_TABLE(of, ssbi_match_table);
  263. static struct platform_driver ssbi_driver = {
  264. .probe = ssbi_probe,
  265. .driver = {
  266. .name = "ssbi",
  267. .owner = THIS_MODULE,
  268. .of_match_table = ssbi_match_table,
  269. },
  270. };
  271. module_platform_driver(ssbi_driver);
  272. MODULE_LICENSE("GPL v2");
  273. MODULE_VERSION("1.0");
  274. MODULE_ALIAS("platform:ssbi");
  275. MODULE_AUTHOR("Dima Zavin <dima@android.com>");