1
0

b53_srab.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * B53 register access through Switch Register Access Bridge Registers
  3. *
  4. * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/platform_data/b53.h>
  22. #include "b53_priv.h"
  23. /* command and status register of the SRAB */
  24. #define B53_SRAB_CMDSTAT 0x2c
  25. #define B53_SRAB_CMDSTAT_RST BIT(2)
  26. #define B53_SRAB_CMDSTAT_WRITE BIT(1)
  27. #define B53_SRAB_CMDSTAT_GORDYN BIT(0)
  28. #define B53_SRAB_CMDSTAT_PAGE 24
  29. #define B53_SRAB_CMDSTAT_REG 16
  30. /* high order word of write data to switch registe */
  31. #define B53_SRAB_WD_H 0x30
  32. /* low order word of write data to switch registe */
  33. #define B53_SRAB_WD_L 0x34
  34. /* high order word of read data from switch register */
  35. #define B53_SRAB_RD_H 0x38
  36. /* low order word of read data from switch register */
  37. #define B53_SRAB_RD_L 0x3c
  38. /* command and status register of the SRAB */
  39. #define B53_SRAB_CTRLS 0x40
  40. #define B53_SRAB_CTRLS_RCAREQ BIT(3)
  41. #define B53_SRAB_CTRLS_RCAGNT BIT(4)
  42. #define B53_SRAB_CTRLS_SW_INIT_DONE BIT(6)
  43. /* the register captures interrupt pulses from the switch */
  44. #define B53_SRAB_INTR 0x44
  45. static int b53_srab_request_grant(struct b53_device *dev)
  46. {
  47. u8 __iomem *regs = dev->priv;
  48. u32 ctrls;
  49. int i;
  50. ctrls = readl(regs + B53_SRAB_CTRLS);
  51. ctrls |= B53_SRAB_CTRLS_RCAREQ;
  52. writel(ctrls, regs + B53_SRAB_CTRLS);
  53. for (i = 0; i < 20; i++) {
  54. ctrls = readl(regs + B53_SRAB_CTRLS);
  55. if (ctrls & B53_SRAB_CTRLS_RCAGNT)
  56. break;
  57. usleep_range(10, 100);
  58. }
  59. if (WARN_ON(i == 5))
  60. return -EIO;
  61. return 0;
  62. }
  63. static void b53_srab_release_grant(struct b53_device *dev)
  64. {
  65. u8 __iomem *regs = dev->priv;
  66. u32 ctrls;
  67. ctrls = readl(regs + B53_SRAB_CTRLS);
  68. ctrls &= ~B53_SRAB_CTRLS_RCAREQ;
  69. writel(ctrls, regs + B53_SRAB_CTRLS);
  70. }
  71. static int b53_srab_op(struct b53_device *dev, u8 page, u8 reg, u32 op)
  72. {
  73. int i;
  74. u32 cmdstat;
  75. u8 __iomem *regs = dev->priv;
  76. /* set register address */
  77. cmdstat = (page << B53_SRAB_CMDSTAT_PAGE) |
  78. (reg << B53_SRAB_CMDSTAT_REG) |
  79. B53_SRAB_CMDSTAT_GORDYN |
  80. op;
  81. writel(cmdstat, regs + B53_SRAB_CMDSTAT);
  82. /* check if operation completed */
  83. for (i = 0; i < 5; ++i) {
  84. cmdstat = readl(regs + B53_SRAB_CMDSTAT);
  85. if (!(cmdstat & B53_SRAB_CMDSTAT_GORDYN))
  86. break;
  87. usleep_range(10, 100);
  88. }
  89. if (WARN_ON(i == 5))
  90. return -EIO;
  91. return 0;
  92. }
  93. static int b53_srab_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
  94. {
  95. u8 __iomem *regs = dev->priv;
  96. int ret = 0;
  97. ret = b53_srab_request_grant(dev);
  98. if (ret)
  99. goto err;
  100. ret = b53_srab_op(dev, page, reg, 0);
  101. if (ret)
  102. goto err;
  103. *val = readl(regs + B53_SRAB_RD_L) & 0xff;
  104. err:
  105. b53_srab_release_grant(dev);
  106. return ret;
  107. }
  108. static int b53_srab_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
  109. {
  110. u8 __iomem *regs = dev->priv;
  111. int ret = 0;
  112. ret = b53_srab_request_grant(dev);
  113. if (ret)
  114. goto err;
  115. ret = b53_srab_op(dev, page, reg, 0);
  116. if (ret)
  117. goto err;
  118. *val = readl(regs + B53_SRAB_RD_L) & 0xffff;
  119. err:
  120. b53_srab_release_grant(dev);
  121. return ret;
  122. }
  123. static int b53_srab_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
  124. {
  125. u8 __iomem *regs = dev->priv;
  126. int ret = 0;
  127. ret = b53_srab_request_grant(dev);
  128. if (ret)
  129. goto err;
  130. ret = b53_srab_op(dev, page, reg, 0);
  131. if (ret)
  132. goto err;
  133. *val = readl(regs + B53_SRAB_RD_L);
  134. err:
  135. b53_srab_release_grant(dev);
  136. return ret;
  137. }
  138. static int b53_srab_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  139. {
  140. u8 __iomem *regs = dev->priv;
  141. int ret = 0;
  142. ret = b53_srab_request_grant(dev);
  143. if (ret)
  144. goto err;
  145. ret = b53_srab_op(dev, page, reg, 0);
  146. if (ret)
  147. goto err;
  148. *val = readl(regs + B53_SRAB_RD_L);
  149. *val += ((u64)readl(regs + B53_SRAB_RD_H) & 0xffff) << 32;
  150. err:
  151. b53_srab_release_grant(dev);
  152. return ret;
  153. }
  154. static int b53_srab_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  155. {
  156. u8 __iomem *regs = dev->priv;
  157. int ret = 0;
  158. ret = b53_srab_request_grant(dev);
  159. if (ret)
  160. goto err;
  161. ret = b53_srab_op(dev, page, reg, 0);
  162. if (ret)
  163. goto err;
  164. *val = readl(regs + B53_SRAB_RD_L);
  165. *val += (u64)readl(regs + B53_SRAB_RD_H) << 32;
  166. err:
  167. b53_srab_release_grant(dev);
  168. return ret;
  169. }
  170. static int b53_srab_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
  171. {
  172. u8 __iomem *regs = dev->priv;
  173. int ret = 0;
  174. ret = b53_srab_request_grant(dev);
  175. if (ret)
  176. goto err;
  177. writel(value, regs + B53_SRAB_WD_L);
  178. ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
  179. err:
  180. b53_srab_release_grant(dev);
  181. return ret;
  182. }
  183. static int b53_srab_write16(struct b53_device *dev, u8 page, u8 reg,
  184. u16 value)
  185. {
  186. u8 __iomem *regs = dev->priv;
  187. int ret = 0;
  188. ret = b53_srab_request_grant(dev);
  189. if (ret)
  190. goto err;
  191. writel(value, regs + B53_SRAB_WD_L);
  192. ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
  193. err:
  194. b53_srab_release_grant(dev);
  195. return ret;
  196. }
  197. static int b53_srab_write32(struct b53_device *dev, u8 page, u8 reg,
  198. u32 value)
  199. {
  200. u8 __iomem *regs = dev->priv;
  201. int ret = 0;
  202. ret = b53_srab_request_grant(dev);
  203. if (ret)
  204. goto err;
  205. writel(value, regs + B53_SRAB_WD_L);
  206. ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
  207. err:
  208. b53_srab_release_grant(dev);
  209. return ret;
  210. }
  211. static int b53_srab_write48(struct b53_device *dev, u8 page, u8 reg,
  212. u64 value)
  213. {
  214. u8 __iomem *regs = dev->priv;
  215. int ret = 0;
  216. ret = b53_srab_request_grant(dev);
  217. if (ret)
  218. goto err;
  219. writel((u32)value, regs + B53_SRAB_WD_L);
  220. writel((u16)(value >> 32), regs + B53_SRAB_WD_H);
  221. ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
  222. err:
  223. b53_srab_release_grant(dev);
  224. return ret;
  225. }
  226. static int b53_srab_write64(struct b53_device *dev, u8 page, u8 reg,
  227. u64 value)
  228. {
  229. u8 __iomem *regs = dev->priv;
  230. int ret = 0;
  231. ret = b53_srab_request_grant(dev);
  232. if (ret)
  233. goto err;
  234. writel((u32)value, regs + B53_SRAB_WD_L);
  235. writel((u32)(value >> 32), regs + B53_SRAB_WD_H);
  236. ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
  237. err:
  238. b53_srab_release_grant(dev);
  239. return ret;
  240. }
  241. static struct b53_io_ops b53_srab_ops = {
  242. .read8 = b53_srab_read8,
  243. .read16 = b53_srab_read16,
  244. .read32 = b53_srab_read32,
  245. .read48 = b53_srab_read48,
  246. .read64 = b53_srab_read64,
  247. .write8 = b53_srab_write8,
  248. .write16 = b53_srab_write16,
  249. .write32 = b53_srab_write32,
  250. .write48 = b53_srab_write48,
  251. .write64 = b53_srab_write64,
  252. };
  253. static int b53_srab_probe(struct platform_device *pdev)
  254. {
  255. struct b53_platform_data *pdata = pdev->dev.platform_data;
  256. struct b53_device *dev;
  257. if (!pdata)
  258. return -EINVAL;
  259. dev = b53_switch_alloc(&pdev->dev, &b53_srab_ops, pdata->regs);
  260. if (!dev)
  261. return -ENOMEM;
  262. if (pdata)
  263. dev->pdata = pdata;
  264. platform_set_drvdata(pdev, dev);
  265. return b53_switch_register(dev);
  266. }
  267. static int b53_srab_remove(struct platform_device *pdev)
  268. {
  269. struct b53_device *dev = platform_get_drvdata(pdev);
  270. if (dev)
  271. b53_switch_remove(dev);
  272. return 0;
  273. }
  274. static struct platform_driver b53_srab_driver = {
  275. .probe = b53_srab_probe,
  276. .remove = b53_srab_remove,
  277. .driver = {
  278. .name = "b53-srab-switch",
  279. },
  280. };
  281. module_platform_driver(b53_srab_driver);
  282. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  283. MODULE_DESCRIPTION("B53 Switch Register Access Bridge Registers (SRAB) access driver");
  284. MODULE_LICENSE("Dual BSD/GPL");