wl1271_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/crc7.h>
  26. #include <linux/spi/spi.h>
  27. #include "wl1271.h"
  28. #include "wl12xx_80211.h"
  29. #include "wl1271_spi.h"
  30. static int wl1271_translate_addr(struct wl1271 *wl, int addr)
  31. {
  32. /*
  33. * To translate, first check to which window of addresses the
  34. * particular address belongs. Then subtract the starting address
  35. * of that window from the address. Then, add offset of the
  36. * translated region.
  37. *
  38. * The translated regions occur next to each other in physical device
  39. * memory, so just add the sizes of the preceeding address regions to
  40. * get the offset to the new region.
  41. *
  42. * Currently, only the two first regions are addressed, and the
  43. * assumption is that all addresses will fall into either of those
  44. * two.
  45. */
  46. if ((addr >= wl->part.reg.start) &&
  47. (addr < wl->part.reg.start + wl->part.reg.size))
  48. return addr - wl->part.reg.start + wl->part.mem.size;
  49. else
  50. return addr - wl->part.mem.start;
  51. }
  52. void wl1271_spi_reset(struct wl1271 *wl)
  53. {
  54. u8 *cmd;
  55. struct spi_transfer t;
  56. struct spi_message m;
  57. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  58. if (!cmd) {
  59. wl1271_error("could not allocate cmd for spi reset");
  60. return;
  61. }
  62. memset(&t, 0, sizeof(t));
  63. spi_message_init(&m);
  64. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  65. t.tx_buf = cmd;
  66. t.len = WSPI_INIT_CMD_LEN;
  67. spi_message_add_tail(&t, &m);
  68. spi_sync(wl->spi, &m);
  69. wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  70. }
  71. void wl1271_spi_init(struct wl1271 *wl)
  72. {
  73. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  74. struct spi_transfer t;
  75. struct spi_message m;
  76. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  77. if (!cmd) {
  78. wl1271_error("could not allocate cmd for spi init");
  79. return;
  80. }
  81. memset(crc, 0, sizeof(crc));
  82. memset(&t, 0, sizeof(t));
  83. spi_message_init(&m);
  84. /*
  85. * Set WSPI_INIT_COMMAND
  86. * the data is being send from the MSB to LSB
  87. */
  88. cmd[2] = 0xff;
  89. cmd[3] = 0xff;
  90. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  91. cmd[0] = 0;
  92. cmd[7] = 0;
  93. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  94. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  95. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  96. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  97. else
  98. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  99. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  100. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  101. crc[0] = cmd[1];
  102. crc[1] = cmd[0];
  103. crc[2] = cmd[7];
  104. crc[3] = cmd[6];
  105. crc[4] = cmd[5];
  106. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  107. cmd[4] |= WSPI_INIT_CMD_END;
  108. t.tx_buf = cmd;
  109. t.len = WSPI_INIT_CMD_LEN;
  110. spi_message_add_tail(&t, &m);
  111. spi_sync(wl->spi, &m);
  112. wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  113. }
  114. /* Set the SPI partitions to access the chip addresses
  115. *
  116. * To simplify driver code, a fixed (virtual) memory map is defined for
  117. * register and memory addresses. Because in the chipset, in different stages
  118. * of operation, those addresses will move around, an address translation
  119. * mechanism is required.
  120. *
  121. * There are four partitions (three memory and one register partition),
  122. * which are mapped to two different areas of the hardware memory.
  123. *
  124. * Virtual address
  125. * space
  126. *
  127. * | |
  128. * ...+----+--> mem.start
  129. * Physical address ... | |
  130. * space ... | | [PART_0]
  131. * ... | |
  132. * 00000000 <--+----+... ...+----+--> mem.start + mem.size
  133. * | | ... | |
  134. * |MEM | ... | |
  135. * | | ... | |
  136. * mem.size <--+----+... | | {unused area)
  137. * | | ... | |
  138. * |REG | ... | |
  139. * mem.size | | ... | |
  140. * + <--+----+... ...+----+--> reg.start
  141. * reg.size | | ... | |
  142. * |MEM2| ... | | [PART_1]
  143. * | | ... | |
  144. * ...+----+--> reg.start + reg.size
  145. * | |
  146. *
  147. */
  148. int wl1271_set_partition(struct wl1271 *wl,
  149. struct wl1271_partition_set *p)
  150. {
  151. /* copy partition info */
  152. memcpy(&wl->part, p, sizeof(*p));
  153. wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  154. p->mem.start, p->mem.size);
  155. wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  156. p->reg.start, p->reg.size);
  157. wl1271_debug(DEBUG_SPI, "mem2_start %08X mem2_size %08X",
  158. p->mem2.start, p->mem2.size);
  159. wl1271_debug(DEBUG_SPI, "mem3_start %08X mem3_size %08X",
  160. p->mem3.start, p->mem3.size);
  161. /* write partition info to the chipset */
  162. wl1271_write32(wl, HW_PART0_START_ADDR, p->mem.start);
  163. wl1271_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size);
  164. wl1271_write32(wl, HW_PART1_START_ADDR, p->reg.start);
  165. wl1271_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size);
  166. wl1271_write32(wl, HW_PART2_START_ADDR, p->mem2.start);
  167. wl1271_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size);
  168. wl1271_write32(wl, HW_PART3_START_ADDR, p->mem3.start);
  169. return 0;
  170. }
  171. #define WL1271_BUSY_WORD_TIMEOUT 1000
  172. void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
  173. {
  174. struct spi_transfer t[1];
  175. struct spi_message m;
  176. u32 *busy_buf;
  177. int num_busy_bytes = 0;
  178. wl1271_info("spi read BUSY!");
  179. /*
  180. * Look for the non-busy word in the read buffer, and if found,
  181. * read in the remaining data into the buffer.
  182. */
  183. busy_buf = (u32 *)buf;
  184. for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
  185. num_busy_bytes += sizeof(u32);
  186. if (*busy_buf & 0x1) {
  187. spi_message_init(&m);
  188. memset(t, 0, sizeof(t));
  189. memmove(buf, busy_buf, len - num_busy_bytes);
  190. t[0].rx_buf = buf + (len - num_busy_bytes);
  191. t[0].len = num_busy_bytes;
  192. spi_message_add_tail(&t[0], &m);
  193. spi_sync(wl->spi, &m);
  194. return;
  195. }
  196. }
  197. /*
  198. * Read further busy words from SPI until a non-busy word is
  199. * encountered, then read the data itself into the buffer.
  200. */
  201. wl1271_info("spi read BUSY-polling needed!");
  202. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  203. busy_buf = wl->buffer_busyword;
  204. while (num_busy_bytes) {
  205. num_busy_bytes--;
  206. spi_message_init(&m);
  207. memset(t, 0, sizeof(t));
  208. t[0].rx_buf = busy_buf;
  209. t[0].len = sizeof(u32);
  210. spi_message_add_tail(&t[0], &m);
  211. spi_sync(wl->spi, &m);
  212. if (*busy_buf & 0x1) {
  213. spi_message_init(&m);
  214. memset(t, 0, sizeof(t));
  215. t[0].rx_buf = buf;
  216. t[0].len = len;
  217. spi_message_add_tail(&t[0], &m);
  218. spi_sync(wl->spi, &m);
  219. return;
  220. }
  221. }
  222. /* The SPI bus is unresponsive, the read failed. */
  223. memset(buf, 0, len);
  224. wl1271_error("SPI read busy-word timeout!\n");
  225. }
  226. void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf,
  227. size_t len, bool fixed)
  228. {
  229. struct spi_transfer t[3];
  230. struct spi_message m;
  231. u32 *busy_buf;
  232. u32 *cmd;
  233. cmd = &wl->buffer_cmd;
  234. busy_buf = wl->buffer_busyword;
  235. *cmd = 0;
  236. *cmd |= WSPI_CMD_READ;
  237. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  238. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  239. if (fixed)
  240. *cmd |= WSPI_CMD_FIXED;
  241. spi_message_init(&m);
  242. memset(t, 0, sizeof(t));
  243. t[0].tx_buf = cmd;
  244. t[0].len = 4;
  245. spi_message_add_tail(&t[0], &m);
  246. /* Busy and non busy words read */
  247. t[1].rx_buf = busy_buf;
  248. t[1].len = WL1271_BUSY_WORD_LEN;
  249. spi_message_add_tail(&t[1], &m);
  250. t[2].rx_buf = buf;
  251. t[2].len = len;
  252. spi_message_add_tail(&t[2], &m);
  253. spi_sync(wl->spi, &m);
  254. /* Check busy words */
  255. if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
  256. wl1271_spi_read_busy(wl, buf, len);
  257. wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  258. wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  259. }
  260. void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf,
  261. size_t len, bool fixed)
  262. {
  263. struct spi_transfer t[2];
  264. struct spi_message m;
  265. u32 *cmd;
  266. cmd = &wl->buffer_cmd;
  267. *cmd = 0;
  268. *cmd |= WSPI_CMD_WRITE;
  269. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  270. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  271. if (fixed)
  272. *cmd |= WSPI_CMD_FIXED;
  273. spi_message_init(&m);
  274. memset(t, 0, sizeof(t));
  275. t[0].tx_buf = cmd;
  276. t[0].len = sizeof(*cmd);
  277. spi_message_add_tail(&t[0], &m);
  278. t[1].tx_buf = buf;
  279. t[1].len = len;
  280. spi_message_add_tail(&t[1], &m);
  281. spi_sync(wl->spi, &m);
  282. wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  283. wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  284. }
  285. void wl1271_spi_mem_read(struct wl1271 *wl, int addr, void *buf,
  286. size_t len)
  287. {
  288. int physical;
  289. physical = wl1271_translate_addr(wl, addr);
  290. wl1271_spi_read(wl, physical, buf, len, false);
  291. }
  292. void wl1271_spi_mem_write(struct wl1271 *wl, int addr, void *buf,
  293. size_t len)
  294. {
  295. int physical;
  296. physical = wl1271_translate_addr(wl, addr);
  297. wl1271_spi_write(wl, physical, buf, len, false);
  298. }
  299. void wl1271_spi_reg_read(struct wl1271 *wl, int addr, void *buf, size_t len,
  300. bool fixed)
  301. {
  302. int physical;
  303. physical = wl1271_translate_addr(wl, addr);
  304. wl1271_spi_read(wl, physical, buf, len, fixed);
  305. }
  306. void wl1271_spi_reg_write(struct wl1271 *wl, int addr, void *buf, size_t len,
  307. bool fixed)
  308. {
  309. int physical;
  310. physical = wl1271_translate_addr(wl, addr);
  311. wl1271_spi_write(wl, physical, buf, len, fixed);
  312. }
  313. u32 wl1271_mem_read32(struct wl1271 *wl, int addr)
  314. {
  315. return wl1271_read32(wl, wl1271_translate_addr(wl, addr));
  316. }
  317. void wl1271_mem_write32(struct wl1271 *wl, int addr, u32 val)
  318. {
  319. wl1271_write32(wl, wl1271_translate_addr(wl, addr), val);
  320. }
  321. u32 wl1271_reg_read32(struct wl1271 *wl, int addr)
  322. {
  323. return wl1271_read32(wl, wl1271_translate_addr(wl, addr));
  324. }
  325. void wl1271_reg_write32(struct wl1271 *wl, int addr, u32 val)
  326. {
  327. wl1271_write32(wl, wl1271_translate_addr(wl, addr), val);
  328. }