hwio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Low-level device IO routines for ST-Ericsson CW1200 drivers
  3. *
  4. * Copyright (c) 2010, ST-Ericsson
  5. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  6. *
  7. * Based on:
  8. * ST-Ericsson UMAC CW1200 driver, which is
  9. * Copyright (c) 2010, ST-Ericsson
  10. * Author: Ajitpal Singh <ajitpal.singh@lockless.no>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/types.h>
  17. #include "cw1200.h"
  18. #include "hwio.h"
  19. #include "hwbus.h"
  20. /* Sdio addr is 4*spi_addr */
  21. #define SPI_REG_ADDR_TO_SDIO(spi_reg_addr) ((spi_reg_addr) << 2)
  22. #define SDIO_ADDR17BIT(buf_id, mpf, rfu, reg_id_ofs) \
  23. ((((buf_id) & 0x1F) << 7) \
  24. | (((mpf) & 1) << 6) \
  25. | (((rfu) & 1) << 5) \
  26. | (((reg_id_ofs) & 0x1F) << 0))
  27. #define MAX_RETRY 3
  28. static int __cw1200_reg_read(struct cw1200_common *priv, u16 addr,
  29. void *buf, size_t buf_len, int buf_id)
  30. {
  31. u16 addr_sdio;
  32. u32 sdio_reg_addr_17bit;
  33. /* Check if buffer is aligned to 4 byte boundary */
  34. if (WARN_ON(((unsigned long)buf & 3) && (buf_len > 4))) {
  35. pr_err("buffer is not aligned.\n");
  36. return -EINVAL;
  37. }
  38. /* Convert to SDIO Register Address */
  39. addr_sdio = SPI_REG_ADDR_TO_SDIO(addr);
  40. sdio_reg_addr_17bit = SDIO_ADDR17BIT(buf_id, 0, 0, addr_sdio);
  41. return priv->hwbus_ops->hwbus_memcpy_fromio(priv->hwbus_priv,
  42. sdio_reg_addr_17bit,
  43. buf, buf_len);
  44. }
  45. static int __cw1200_reg_write(struct cw1200_common *priv, u16 addr,
  46. const void *buf, size_t buf_len, int buf_id)
  47. {
  48. u16 addr_sdio;
  49. u32 sdio_reg_addr_17bit;
  50. /* Convert to SDIO Register Address */
  51. addr_sdio = SPI_REG_ADDR_TO_SDIO(addr);
  52. sdio_reg_addr_17bit = SDIO_ADDR17BIT(buf_id, 0, 0, addr_sdio);
  53. return priv->hwbus_ops->hwbus_memcpy_toio(priv->hwbus_priv,
  54. sdio_reg_addr_17bit,
  55. buf, buf_len);
  56. }
  57. static inline int __cw1200_reg_read_32(struct cw1200_common *priv,
  58. u16 addr, u32 *val)
  59. {
  60. __le32 tmp;
  61. int i = __cw1200_reg_read(priv, addr, &tmp, sizeof(tmp), 0);
  62. *val = le32_to_cpu(tmp);
  63. return i;
  64. }
  65. static inline int __cw1200_reg_write_32(struct cw1200_common *priv,
  66. u16 addr, u32 val)
  67. {
  68. __le32 tmp = cpu_to_le32(val);
  69. return __cw1200_reg_write(priv, addr, &tmp, sizeof(tmp), 0);
  70. }
  71. static inline int __cw1200_reg_read_16(struct cw1200_common *priv,
  72. u16 addr, u16 *val)
  73. {
  74. __le16 tmp;
  75. int i = __cw1200_reg_read(priv, addr, &tmp, sizeof(tmp), 0);
  76. *val = le16_to_cpu(tmp);
  77. return i;
  78. }
  79. static inline int __cw1200_reg_write_16(struct cw1200_common *priv,
  80. u16 addr, u16 val)
  81. {
  82. __le16 tmp = cpu_to_le16(val);
  83. return __cw1200_reg_write(priv, addr, &tmp, sizeof(tmp), 0);
  84. }
  85. int cw1200_reg_read(struct cw1200_common *priv, u16 addr, void *buf,
  86. size_t buf_len)
  87. {
  88. int ret;
  89. priv->hwbus_ops->lock(priv->hwbus_priv);
  90. ret = __cw1200_reg_read(priv, addr, buf, buf_len, 0);
  91. priv->hwbus_ops->unlock(priv->hwbus_priv);
  92. return ret;
  93. }
  94. int cw1200_reg_write(struct cw1200_common *priv, u16 addr, const void *buf,
  95. size_t buf_len)
  96. {
  97. int ret;
  98. priv->hwbus_ops->lock(priv->hwbus_priv);
  99. ret = __cw1200_reg_write(priv, addr, buf, buf_len, 0);
  100. priv->hwbus_ops->unlock(priv->hwbus_priv);
  101. return ret;
  102. }
  103. int cw1200_data_read(struct cw1200_common *priv, void *buf, size_t buf_len)
  104. {
  105. int ret, retry = 1;
  106. int buf_id_rx = priv->buf_id_rx;
  107. priv->hwbus_ops->lock(priv->hwbus_priv);
  108. while (retry <= MAX_RETRY) {
  109. ret = __cw1200_reg_read(priv,
  110. ST90TDS_IN_OUT_QUEUE_REG_ID, buf,
  111. buf_len, buf_id_rx + 1);
  112. if (!ret) {
  113. buf_id_rx = (buf_id_rx + 1) & 3;
  114. priv->buf_id_rx = buf_id_rx;
  115. break;
  116. } else {
  117. retry++;
  118. mdelay(1);
  119. pr_err("error :[%d]\n", ret);
  120. }
  121. }
  122. priv->hwbus_ops->unlock(priv->hwbus_priv);
  123. return ret;
  124. }
  125. int cw1200_data_write(struct cw1200_common *priv, const void *buf,
  126. size_t buf_len)
  127. {
  128. int ret, retry = 1;
  129. int buf_id_tx = priv->buf_id_tx;
  130. priv->hwbus_ops->lock(priv->hwbus_priv);
  131. while (retry <= MAX_RETRY) {
  132. ret = __cw1200_reg_write(priv,
  133. ST90TDS_IN_OUT_QUEUE_REG_ID, buf,
  134. buf_len, buf_id_tx);
  135. if (!ret) {
  136. buf_id_tx = (buf_id_tx + 1) & 31;
  137. priv->buf_id_tx = buf_id_tx;
  138. break;
  139. } else {
  140. retry++;
  141. mdelay(1);
  142. pr_err("error :[%d]\n", ret);
  143. }
  144. }
  145. priv->hwbus_ops->unlock(priv->hwbus_priv);
  146. return ret;
  147. }
  148. int cw1200_indirect_read(struct cw1200_common *priv, u32 addr, void *buf,
  149. size_t buf_len, u32 prefetch, u16 port_addr)
  150. {
  151. u32 val32 = 0;
  152. int i, ret;
  153. if ((buf_len / 2) >= 0x1000) {
  154. pr_err("Can't read more than 0xfff words.\n");
  155. return -EINVAL;
  156. }
  157. priv->hwbus_ops->lock(priv->hwbus_priv);
  158. /* Write address */
  159. ret = __cw1200_reg_write_32(priv, ST90TDS_SRAM_BASE_ADDR_REG_ID, addr);
  160. if (ret < 0) {
  161. pr_err("Can't write address register.\n");
  162. goto out;
  163. }
  164. /* Read CONFIG Register Value - We will read 32 bits */
  165. ret = __cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
  166. if (ret < 0) {
  167. pr_err("Can't read config register.\n");
  168. goto out;
  169. }
  170. /* Set PREFETCH bit */
  171. ret = __cw1200_reg_write_32(priv, ST90TDS_CONFIG_REG_ID,
  172. val32 | prefetch);
  173. if (ret < 0) {
  174. pr_err("Can't write prefetch bit.\n");
  175. goto out;
  176. }
  177. /* Check for PRE-FETCH bit to be cleared */
  178. for (i = 0; i < 20; i++) {
  179. ret = __cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
  180. if (ret < 0) {
  181. pr_err("Can't check prefetch bit.\n");
  182. goto out;
  183. }
  184. if (!(val32 & prefetch))
  185. break;
  186. mdelay(i);
  187. }
  188. if (val32 & prefetch) {
  189. pr_err("Prefetch bit is not cleared.\n");
  190. goto out;
  191. }
  192. /* Read data port */
  193. ret = __cw1200_reg_read(priv, port_addr, buf, buf_len, 0);
  194. if (ret < 0) {
  195. pr_err("Can't read data port.\n");
  196. goto out;
  197. }
  198. out:
  199. priv->hwbus_ops->unlock(priv->hwbus_priv);
  200. return ret;
  201. }
  202. int cw1200_apb_write(struct cw1200_common *priv, u32 addr, const void *buf,
  203. size_t buf_len)
  204. {
  205. int ret;
  206. if ((buf_len / 2) >= 0x1000) {
  207. pr_err("Can't write more than 0xfff words.\n");
  208. return -EINVAL;
  209. }
  210. priv->hwbus_ops->lock(priv->hwbus_priv);
  211. /* Write address */
  212. ret = __cw1200_reg_write_32(priv, ST90TDS_SRAM_BASE_ADDR_REG_ID, addr);
  213. if (ret < 0) {
  214. pr_err("Can't write address register.\n");
  215. goto out;
  216. }
  217. /* Write data port */
  218. ret = __cw1200_reg_write(priv, ST90TDS_SRAM_DPORT_REG_ID,
  219. buf, buf_len, 0);
  220. if (ret < 0) {
  221. pr_err("Can't write data port.\n");
  222. goto out;
  223. }
  224. out:
  225. priv->hwbus_ops->unlock(priv->hwbus_priv);
  226. return ret;
  227. }
  228. int __cw1200_irq_enable(struct cw1200_common *priv, int enable)
  229. {
  230. u32 val32;
  231. u16 val16;
  232. int ret;
  233. if (HIF_8601_SILICON == priv->hw_type) {
  234. ret = __cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
  235. if (ret < 0) {
  236. pr_err("Can't read config register.\n");
  237. return ret;
  238. }
  239. if (enable)
  240. val32 |= ST90TDS_CONF_IRQ_RDY_ENABLE;
  241. else
  242. val32 &= ~ST90TDS_CONF_IRQ_RDY_ENABLE;
  243. ret = __cw1200_reg_write_32(priv, ST90TDS_CONFIG_REG_ID, val32);
  244. if (ret < 0) {
  245. pr_err("Can't write config register.\n");
  246. return ret;
  247. }
  248. } else {
  249. ret = __cw1200_reg_read_16(priv, ST90TDS_CONFIG_REG_ID, &val16);
  250. if (ret < 0) {
  251. pr_err("Can't read control register.\n");
  252. return ret;
  253. }
  254. if (enable)
  255. val16 |= ST90TDS_CONT_IRQ_RDY_ENABLE;
  256. else
  257. val16 &= ~ST90TDS_CONT_IRQ_RDY_ENABLE;
  258. ret = __cw1200_reg_write_16(priv, ST90TDS_CONFIG_REG_ID, val16);
  259. if (ret < 0) {
  260. pr_err("Can't write control register.\n");
  261. return ret;
  262. }
  263. }
  264. return 0;
  265. }