ssbi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. int ret = 0;
  224. const char *type;
  225. ssbi = kzalloc(sizeof(struct ssbi), GFP_KERNEL);
  226. if (!ssbi) {
  227. pr_err("can not allocate ssbi_data\n");
  228. return -ENOMEM;
  229. }
  230. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  231. if (!mem_res) {
  232. pr_err("missing mem resource\n");
  233. ret = -EINVAL;
  234. goto err_get_mem_res;
  235. }
  236. ssbi->base = ioremap(mem_res->start, resource_size(mem_res));
  237. if (!ssbi->base) {
  238. pr_err("ioremap of 0x%p failed\n", (void *)mem_res->start);
  239. ret = -EINVAL;
  240. goto err_ioremap;
  241. }
  242. platform_set_drvdata(pdev, ssbi);
  243. type = of_get_property(np, "qcom,controller-type", NULL);
  244. if (type == NULL) {
  245. pr_err("Missing qcom,controller-type property\n");
  246. ret = -EINVAL;
  247. goto err_ssbi_controller;
  248. }
  249. dev_info(&pdev->dev, "SSBI controller type: '%s'\n", type);
  250. if (strcmp(type, "ssbi") == 0)
  251. ssbi->controller_type = MSM_SBI_CTRL_SSBI;
  252. else if (strcmp(type, "ssbi2") == 0)
  253. ssbi->controller_type = MSM_SBI_CTRL_SSBI2;
  254. else if (strcmp(type, "pmic-arbiter") == 0)
  255. ssbi->controller_type = MSM_SBI_CTRL_PMIC_ARBITER;
  256. else {
  257. pr_err("Unknown qcom,controller-type\n");
  258. ret = -EINVAL;
  259. goto err_ssbi_controller;
  260. }
  261. if (ssbi->controller_type == MSM_SBI_CTRL_PMIC_ARBITER) {
  262. ssbi->read = ssbi_pa_read_bytes;
  263. ssbi->write = ssbi_pa_write_bytes;
  264. } else {
  265. ssbi->read = ssbi_read_bytes;
  266. ssbi->write = ssbi_write_bytes;
  267. }
  268. spin_lock_init(&ssbi->lock);
  269. ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
  270. if (ret)
  271. goto err_ssbi_controller;
  272. return 0;
  273. err_ssbi_controller:
  274. platform_set_drvdata(pdev, NULL);
  275. iounmap(ssbi->base);
  276. err_ioremap:
  277. err_get_mem_res:
  278. kfree(ssbi);
  279. return ret;
  280. }
  281. static int ssbi_remove(struct platform_device *pdev)
  282. {
  283. struct ssbi *ssbi = platform_get_drvdata(pdev);
  284. platform_set_drvdata(pdev, NULL);
  285. iounmap(ssbi->base);
  286. kfree(ssbi);
  287. return 0;
  288. }
  289. static struct of_device_id ssbi_match_table[] = {
  290. { .compatible = "qcom,ssbi" },
  291. {}
  292. };
  293. static struct platform_driver ssbi_driver = {
  294. .probe = ssbi_probe,
  295. .remove = ssbi_remove,
  296. .driver = {
  297. .name = "ssbi",
  298. .owner = THIS_MODULE,
  299. .of_match_table = ssbi_match_table,
  300. },
  301. };
  302. static int __init ssbi_init(void)
  303. {
  304. return platform_driver_register(&ssbi_driver);
  305. }
  306. module_init(ssbi_init);
  307. static void __exit ssbi_exit(void)
  308. {
  309. platform_driver_unregister(&ssbi_driver);
  310. }
  311. module_exit(ssbi_exit)
  312. MODULE_LICENSE("GPL v2");
  313. MODULE_VERSION("1.0");
  314. MODULE_ALIAS("platform:ssbi");
  315. MODULE_AUTHOR("Dima Zavin <dima@android.com>");