b53_spi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * B53 register access through SPI
  3. *
  4. * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
  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 <asm/unaligned.h>
  19. #include <linux/delay.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/platform_data/b53.h>
  24. #include "b53_priv.h"
  25. #define B53_SPI_DATA 0xf0
  26. #define B53_SPI_STATUS 0xfe
  27. #define B53_SPI_CMD_SPIF BIT(7)
  28. #define B53_SPI_CMD_RACK BIT(5)
  29. #define B53_SPI_CMD_READ 0x00
  30. #define B53_SPI_CMD_WRITE 0x01
  31. #define B53_SPI_CMD_NORMAL 0x60
  32. #define B53_SPI_CMD_FAST 0x10
  33. #define B53_SPI_PAGE_SELECT 0xff
  34. static inline int b53_spi_read_reg(struct spi_device *spi, u8 reg, u8 *val,
  35. unsigned int len)
  36. {
  37. u8 txbuf[2];
  38. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_READ;
  39. txbuf[1] = reg;
  40. return spi_write_then_read(spi, txbuf, 2, val, len);
  41. }
  42. static inline int b53_spi_clear_status(struct spi_device *spi)
  43. {
  44. unsigned int i;
  45. u8 rxbuf;
  46. int ret;
  47. for (i = 0; i < 10; i++) {
  48. ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
  49. if (ret)
  50. return ret;
  51. if (!(rxbuf & B53_SPI_CMD_SPIF))
  52. break;
  53. mdelay(1);
  54. }
  55. if (i == 10)
  56. return -EIO;
  57. return 0;
  58. }
  59. static inline int b53_spi_set_page(struct spi_device *spi, u8 page)
  60. {
  61. u8 txbuf[3];
  62. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
  63. txbuf[1] = B53_SPI_PAGE_SELECT;
  64. txbuf[2] = page;
  65. return spi_write(spi, txbuf, sizeof(txbuf));
  66. }
  67. static inline int b53_prepare_reg_access(struct spi_device *spi, u8 page)
  68. {
  69. int ret = b53_spi_clear_status(spi);
  70. if (ret)
  71. return ret;
  72. return b53_spi_set_page(spi, page);
  73. }
  74. static int b53_spi_prepare_reg_read(struct spi_device *spi, u8 reg)
  75. {
  76. u8 rxbuf;
  77. int retry_count;
  78. int ret;
  79. ret = b53_spi_read_reg(spi, reg, &rxbuf, 1);
  80. if (ret)
  81. return ret;
  82. for (retry_count = 0; retry_count < 10; retry_count++) {
  83. ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
  84. if (ret)
  85. return ret;
  86. if (rxbuf & B53_SPI_CMD_RACK)
  87. break;
  88. mdelay(1);
  89. }
  90. if (retry_count == 10)
  91. return -EIO;
  92. return 0;
  93. }
  94. static int b53_spi_read(struct b53_device *dev, u8 page, u8 reg, u8 *data,
  95. unsigned int len)
  96. {
  97. struct spi_device *spi = dev->priv;
  98. int ret;
  99. ret = b53_prepare_reg_access(spi, page);
  100. if (ret)
  101. return ret;
  102. ret = b53_spi_prepare_reg_read(spi, reg);
  103. if (ret)
  104. return ret;
  105. return b53_spi_read_reg(spi, B53_SPI_DATA, data, len);
  106. }
  107. static int b53_spi_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
  108. {
  109. return b53_spi_read(dev, page, reg, val, 1);
  110. }
  111. static int b53_spi_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
  112. {
  113. int ret = b53_spi_read(dev, page, reg, (u8 *)val, 2);
  114. if (!ret)
  115. *val = le16_to_cpu(*val);
  116. return ret;
  117. }
  118. static int b53_spi_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
  119. {
  120. int ret = b53_spi_read(dev, page, reg, (u8 *)val, 4);
  121. if (!ret)
  122. *val = le32_to_cpu(*val);
  123. return ret;
  124. }
  125. static int b53_spi_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  126. {
  127. int ret;
  128. *val = 0;
  129. ret = b53_spi_read(dev, page, reg, (u8 *)val, 6);
  130. if (!ret)
  131. *val = le64_to_cpu(*val);
  132. return ret;
  133. }
  134. static int b53_spi_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  135. {
  136. int ret = b53_spi_read(dev, page, reg, (u8 *)val, 8);
  137. if (!ret)
  138. *val = le64_to_cpu(*val);
  139. return ret;
  140. }
  141. static int b53_spi_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
  142. {
  143. struct spi_device *spi = dev->priv;
  144. int ret;
  145. u8 txbuf[3];
  146. ret = b53_prepare_reg_access(spi, page);
  147. if (ret)
  148. return ret;
  149. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
  150. txbuf[1] = reg;
  151. txbuf[2] = value;
  152. return spi_write(spi, txbuf, sizeof(txbuf));
  153. }
  154. static int b53_spi_write16(struct b53_device *dev, u8 page, u8 reg, u16 value)
  155. {
  156. struct spi_device *spi = dev->priv;
  157. int ret;
  158. u8 txbuf[4];
  159. ret = b53_prepare_reg_access(spi, page);
  160. if (ret)
  161. return ret;
  162. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
  163. txbuf[1] = reg;
  164. put_unaligned_le16(value, &txbuf[2]);
  165. return spi_write(spi, txbuf, sizeof(txbuf));
  166. }
  167. static int b53_spi_write32(struct b53_device *dev, u8 page, u8 reg, u32 value)
  168. {
  169. struct spi_device *spi = dev->priv;
  170. int ret;
  171. u8 txbuf[6];
  172. ret = b53_prepare_reg_access(spi, page);
  173. if (ret)
  174. return ret;
  175. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
  176. txbuf[1] = reg;
  177. put_unaligned_le32(value, &txbuf[2]);
  178. return spi_write(spi, txbuf, sizeof(txbuf));
  179. }
  180. static int b53_spi_write48(struct b53_device *dev, u8 page, u8 reg, u64 value)
  181. {
  182. struct spi_device *spi = dev->priv;
  183. int ret;
  184. u8 txbuf[10];
  185. ret = b53_prepare_reg_access(spi, page);
  186. if (ret)
  187. return ret;
  188. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
  189. txbuf[1] = reg;
  190. put_unaligned_le64(value, &txbuf[2]);
  191. return spi_write(spi, txbuf, sizeof(txbuf) - 2);
  192. }
  193. static int b53_spi_write64(struct b53_device *dev, u8 page, u8 reg, u64 value)
  194. {
  195. struct spi_device *spi = dev->priv;
  196. int ret;
  197. u8 txbuf[10];
  198. ret = b53_prepare_reg_access(spi, page);
  199. if (ret)
  200. return ret;
  201. txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
  202. txbuf[1] = reg;
  203. put_unaligned_le64(value, &txbuf[2]);
  204. return spi_write(spi, txbuf, sizeof(txbuf));
  205. }
  206. static const struct b53_io_ops b53_spi_ops = {
  207. .read8 = b53_spi_read8,
  208. .read16 = b53_spi_read16,
  209. .read32 = b53_spi_read32,
  210. .read48 = b53_spi_read48,
  211. .read64 = b53_spi_read64,
  212. .write8 = b53_spi_write8,
  213. .write16 = b53_spi_write16,
  214. .write32 = b53_spi_write32,
  215. .write48 = b53_spi_write48,
  216. .write64 = b53_spi_write64,
  217. };
  218. static int b53_spi_probe(struct spi_device *spi)
  219. {
  220. struct b53_device *dev;
  221. int ret;
  222. dev = b53_switch_alloc(&spi->dev, &b53_spi_ops, spi);
  223. if (!dev)
  224. return -ENOMEM;
  225. if (spi->dev.platform_data)
  226. dev->pdata = spi->dev.platform_data;
  227. ret = b53_switch_register(dev);
  228. if (ret)
  229. return ret;
  230. spi_set_drvdata(spi, dev);
  231. return 0;
  232. }
  233. static int b53_spi_remove(struct spi_device *spi)
  234. {
  235. struct b53_device *dev = spi_get_drvdata(spi);
  236. if (dev)
  237. b53_switch_remove(dev);
  238. return 0;
  239. }
  240. static struct spi_driver b53_spi_driver = {
  241. .driver = {
  242. .name = "b53-switch",
  243. },
  244. .probe = b53_spi_probe,
  245. .remove = b53_spi_remove,
  246. };
  247. module_spi_driver(b53_spi_driver);
  248. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  249. MODULE_DESCRIPTION("B53 SPI access driver");
  250. MODULE_LICENSE("Dual BSD/GPL");