wl1271_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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_raw_write32(wl, HW_PART0_START_ADDR, p->mem.start);
  163. wl1271_raw_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size);
  164. wl1271_raw_write32(wl, HW_PART1_START_ADDR, p->reg.start);
  165. wl1271_raw_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size);
  166. wl1271_raw_write32(wl, HW_PART2_START_ADDR, p->mem2.start);
  167. wl1271_raw_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size);
  168. wl1271_raw_write32(wl, HW_PART3_START_ADDR, p->mem3.start);
  169. return 0;
  170. }
  171. #define WL1271_BUSY_WORD_TIMEOUT 1000
  172. /* FIXME: Check busy words, removed due to SPI bug */
  173. #if 0
  174. static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
  175. {
  176. struct spi_transfer t[1];
  177. struct spi_message m;
  178. u32 *busy_buf;
  179. int num_busy_bytes = 0;
  180. wl1271_info("spi read BUSY!");
  181. /*
  182. * Look for the non-busy word in the read buffer, and if found,
  183. * read in the remaining data into the buffer.
  184. */
  185. busy_buf = (u32 *)buf;
  186. for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
  187. num_busy_bytes += sizeof(u32);
  188. if (*busy_buf & 0x1) {
  189. spi_message_init(&m);
  190. memset(t, 0, sizeof(t));
  191. memmove(buf, busy_buf, len - num_busy_bytes);
  192. t[0].rx_buf = buf + (len - num_busy_bytes);
  193. t[0].len = num_busy_bytes;
  194. spi_message_add_tail(&t[0], &m);
  195. spi_sync(wl->spi, &m);
  196. return;
  197. }
  198. }
  199. /*
  200. * Read further busy words from SPI until a non-busy word is
  201. * encountered, then read the data itself into the buffer.
  202. */
  203. wl1271_info("spi read BUSY-polling needed!");
  204. num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
  205. busy_buf = wl->buffer_busyword;
  206. while (num_busy_bytes) {
  207. num_busy_bytes--;
  208. spi_message_init(&m);
  209. memset(t, 0, sizeof(t));
  210. t[0].rx_buf = busy_buf;
  211. t[0].len = sizeof(u32);
  212. spi_message_add_tail(&t[0], &m);
  213. spi_sync(wl->spi, &m);
  214. if (*busy_buf & 0x1) {
  215. spi_message_init(&m);
  216. memset(t, 0, sizeof(t));
  217. t[0].rx_buf = buf;
  218. t[0].len = len;
  219. spi_message_add_tail(&t[0], &m);
  220. spi_sync(wl->spi, &m);
  221. return;
  222. }
  223. }
  224. /* The SPI bus is unresponsive, the read failed. */
  225. memset(buf, 0, len);
  226. wl1271_error("SPI read busy-word timeout!\n");
  227. }
  228. #endif
  229. void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
  230. size_t len, bool fixed)
  231. {
  232. struct spi_transfer t[3];
  233. struct spi_message m;
  234. u32 *busy_buf;
  235. u32 *cmd;
  236. cmd = &wl->buffer_cmd;
  237. busy_buf = wl->buffer_busyword;
  238. *cmd = 0;
  239. *cmd |= WSPI_CMD_READ;
  240. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  241. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  242. if (fixed)
  243. *cmd |= WSPI_CMD_FIXED;
  244. spi_message_init(&m);
  245. memset(t, 0, sizeof(t));
  246. t[0].tx_buf = cmd;
  247. t[0].len = 4;
  248. spi_message_add_tail(&t[0], &m);
  249. /* Busy and non busy words read */
  250. t[1].rx_buf = busy_buf;
  251. t[1].len = WL1271_BUSY_WORD_LEN;
  252. spi_message_add_tail(&t[1], &m);
  253. t[2].rx_buf = buf;
  254. t[2].len = len;
  255. spi_message_add_tail(&t[2], &m);
  256. spi_sync(wl->spi, &m);
  257. /* FIXME: Check busy words, removed due to SPI bug */
  258. /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
  259. wl1271_spi_read_busy(wl, buf, len); */
  260. wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  261. wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  262. }
  263. void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
  264. size_t len, bool fixed)
  265. {
  266. struct spi_transfer t[2];
  267. struct spi_message m;
  268. u32 *cmd;
  269. cmd = &wl->buffer_cmd;
  270. *cmd = 0;
  271. *cmd |= WSPI_CMD_WRITE;
  272. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  273. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  274. if (fixed)
  275. *cmd |= WSPI_CMD_FIXED;
  276. spi_message_init(&m);
  277. memset(t, 0, sizeof(t));
  278. t[0].tx_buf = cmd;
  279. t[0].len = sizeof(*cmd);
  280. spi_message_add_tail(&t[0], &m);
  281. t[1].tx_buf = buf;
  282. t[1].len = len;
  283. spi_message_add_tail(&t[1], &m);
  284. spi_sync(wl->spi, &m);
  285. wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  286. wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  287. }
  288. void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf, size_t len,
  289. bool fixed)
  290. {
  291. int physical;
  292. physical = wl1271_translate_addr(wl, addr);
  293. wl1271_spi_raw_read(wl, physical, buf, len, fixed);
  294. }
  295. void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf, size_t len,
  296. bool fixed)
  297. {
  298. int physical;
  299. physical = wl1271_translate_addr(wl, addr);
  300. wl1271_spi_raw_write(wl, physical, buf, len, fixed);
  301. }
  302. u32 wl1271_spi_read32(struct wl1271 *wl, int addr)
  303. {
  304. return wl1271_raw_read32(wl, wl1271_translate_addr(wl, addr));
  305. }
  306. void wl1271_spi_write32(struct wl1271 *wl, int addr, u32 val)
  307. {
  308. wl1271_raw_write32(wl, wl1271_translate_addr(wl, addr), val);
  309. }
  310. void wl1271_top_reg_write(struct wl1271 *wl, int addr, u16 val)
  311. {
  312. /* write address >> 1 + 0x30000 to OCP_POR_CTR */
  313. addr = (addr >> 1) + 0x30000;
  314. wl1271_spi_write32(wl, OCP_POR_CTR, addr);
  315. /* write value to OCP_POR_WDATA */
  316. wl1271_spi_write32(wl, OCP_DATA_WRITE, val);
  317. /* write 1 to OCP_CMD */
  318. wl1271_spi_write32(wl, OCP_CMD, OCP_CMD_WRITE);
  319. }
  320. u16 wl1271_top_reg_read(struct wl1271 *wl, int addr)
  321. {
  322. u32 val;
  323. int timeout = OCP_CMD_LOOP;
  324. /* write address >> 1 + 0x30000 to OCP_POR_CTR */
  325. addr = (addr >> 1) + 0x30000;
  326. wl1271_spi_write32(wl, OCP_POR_CTR, addr);
  327. /* write 2 to OCP_CMD */
  328. wl1271_spi_write32(wl, OCP_CMD, OCP_CMD_READ);
  329. /* poll for data ready */
  330. do {
  331. val = wl1271_spi_read32(wl, OCP_DATA_READ);
  332. timeout--;
  333. } while (!(val & OCP_READY_MASK) && timeout);
  334. if (!timeout) {
  335. wl1271_warning("Top register access timed out.");
  336. return 0xffff;
  337. }
  338. /* check data status and return if OK */
  339. if ((val & OCP_STATUS_MASK) == OCP_STATUS_OK)
  340. return val & 0xffff;
  341. else {
  342. wl1271_warning("Top register access returned error.");
  343. return 0xffff;
  344. }
  345. }