spi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * This file is part of wl12xx
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@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/crc7.h>
  25. #include <linux/spi/spi.h>
  26. #include "wl12xx.h"
  27. #include "wl12xx_80211.h"
  28. #include "reg.h"
  29. #include "spi.h"
  30. #include "ps.h"
  31. static int wl12xx_translate_reg_addr(struct wl12xx *wl, int addr)
  32. {
  33. /* If the address is lower than REGISTERS_BASE, it means that this is
  34. * a chip-specific register address, so look it up in the registers
  35. * table */
  36. if (addr < REGISTERS_BASE) {
  37. /* Make sure we don't go over the table */
  38. if (addr >= ACX_REG_TABLE_LEN) {
  39. wl12xx_error("address out of range (%d)", addr);
  40. return -EINVAL;
  41. }
  42. addr = wl->chip.acx_reg_table[addr];
  43. }
  44. return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
  45. }
  46. static int wl12xx_translate_mem_addr(struct wl12xx *wl, int addr)
  47. {
  48. return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
  49. }
  50. void wl12xx_spi_reset(struct wl12xx *wl)
  51. {
  52. u8 *cmd;
  53. struct spi_transfer t;
  54. struct spi_message m;
  55. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  56. if (!cmd) {
  57. wl12xx_error("could not allocate cmd for spi reset");
  58. return;
  59. }
  60. memset(&t, 0, sizeof(t));
  61. spi_message_init(&m);
  62. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  63. t.tx_buf = cmd;
  64. t.len = WSPI_INIT_CMD_LEN;
  65. spi_message_add_tail(&t, &m);
  66. spi_sync(wl->spi, &m);
  67. wl12xx_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  68. }
  69. void wl12xx_spi_init(struct wl12xx *wl)
  70. {
  71. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  72. struct spi_transfer t;
  73. struct spi_message m;
  74. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  75. if (!cmd) {
  76. wl12xx_error("could not allocate cmd for spi init");
  77. return;
  78. }
  79. memset(crc, 0, sizeof(crc));
  80. memset(&t, 0, sizeof(t));
  81. spi_message_init(&m);
  82. /*
  83. * Set WSPI_INIT_COMMAND
  84. * the data is being send from the MSB to LSB
  85. */
  86. cmd[2] = 0xff;
  87. cmd[3] = 0xff;
  88. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  89. cmd[0] = 0;
  90. cmd[7] = 0;
  91. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  92. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  93. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  94. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  95. else
  96. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  97. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  98. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  99. crc[0] = cmd[1];
  100. crc[1] = cmd[0];
  101. crc[2] = cmd[7];
  102. crc[3] = cmd[6];
  103. crc[4] = cmd[5];
  104. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  105. cmd[4] |= WSPI_INIT_CMD_END;
  106. t.tx_buf = cmd;
  107. t.len = WSPI_INIT_CMD_LEN;
  108. spi_message_add_tail(&t, &m);
  109. spi_sync(wl->spi, &m);
  110. wl12xx_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  111. }
  112. /* Set the SPI partitions to access the chip addresses
  113. *
  114. * There are two VIRTUAL (SPI) partitions (the memory partition and the
  115. * registers partition), which are mapped to two different areas of the
  116. * PHYSICAL (hardware) memory. This function also makes other checks to
  117. * ensure that the partitions are not overlapping. In the diagram below, the
  118. * memory partition comes before the register partition, but the opposite is
  119. * also supported.
  120. *
  121. * PHYSICAL address
  122. * space
  123. *
  124. * | |
  125. * ...+----+--> mem_start
  126. * VIRTUAL address ... | |
  127. * space ... | | [PART_0]
  128. * ... | |
  129. * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
  130. * | | ... | |
  131. * |MEM | ... | |
  132. * | | ... | |
  133. * part_size <--+----+... | | {unused area)
  134. * | | ... | |
  135. * |REG | ... | |
  136. * part_size | | ... | |
  137. * + <--+----+... ...+----+--> reg_start
  138. * reg_size ... | |
  139. * ... | | [PART_1]
  140. * ... | |
  141. * ...+----+--> reg_start + reg_size
  142. * | |
  143. *
  144. */
  145. void wl12xx_set_partition(struct wl12xx *wl,
  146. u32 mem_start, u32 mem_size,
  147. u32 reg_start, u32 reg_size)
  148. {
  149. u8 tx_buf[sizeof(u32) + 2 * sizeof(struct wl12xx_partition)];
  150. struct wl12xx_partition *partition;
  151. struct spi_transfer t;
  152. struct spi_message m;
  153. u32 *cmd;
  154. size_t len;
  155. int addr;
  156. spi_message_init(&m);
  157. memset(&t, 0, sizeof(t));
  158. memset(tx_buf, 0, sizeof(tx_buf));
  159. cmd = (u32 *) tx_buf;
  160. partition = (struct wl12xx_partition *) (tx_buf + sizeof(u32));
  161. addr = HW_ACCESS_PART0_SIZE_ADDR;
  162. len = 2 * sizeof(struct wl12xx_partition);
  163. *cmd |= WSPI_CMD_WRITE;
  164. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  165. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  166. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  167. mem_start, mem_size);
  168. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  169. reg_start, reg_size);
  170. /* Make sure that the two partitions together don't exceed the
  171. * address range */
  172. if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
  173. wl12xx_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
  174. " address range. Truncating partition[0].");
  175. mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
  176. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  177. mem_start, mem_size);
  178. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  179. reg_start, reg_size);
  180. }
  181. if ((mem_start < reg_start) &&
  182. ((mem_start + mem_size) > reg_start)) {
  183. /* Guarantee that the memory partition doesn't overlap the
  184. * registers partition */
  185. wl12xx_debug(DEBUG_SPI, "End of partition[0] is "
  186. "overlapping partition[1]. Adjusted.");
  187. mem_size = reg_start - mem_start;
  188. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  189. mem_start, mem_size);
  190. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  191. reg_start, reg_size);
  192. } else if ((reg_start < mem_start) &&
  193. ((reg_start + reg_size) > mem_start)) {
  194. /* Guarantee that the register partition doesn't overlap the
  195. * memory partition */
  196. wl12xx_debug(DEBUG_SPI, "End of partition[1] is"
  197. " overlapping partition[0]. Adjusted.");
  198. reg_size = mem_start - reg_start;
  199. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  200. mem_start, mem_size);
  201. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  202. reg_start, reg_size);
  203. }
  204. partition[0].start = mem_start;
  205. partition[0].size = mem_size;
  206. partition[1].start = reg_start;
  207. partition[1].size = reg_size;
  208. wl->physical_mem_addr = mem_start;
  209. wl->physical_reg_addr = reg_start;
  210. wl->virtual_mem_addr = 0;
  211. wl->virtual_reg_addr = mem_size;
  212. t.tx_buf = tx_buf;
  213. t.len = sizeof(tx_buf);
  214. spi_message_add_tail(&t, &m);
  215. spi_sync(wl->spi, &m);
  216. }
  217. void wl12xx_spi_read(struct wl12xx *wl, int addr, void *buf,
  218. size_t len)
  219. {
  220. struct spi_transfer t[3];
  221. struct spi_message m;
  222. char busy_buf[TNETWIF_READ_OFFSET_BYTES];
  223. u32 cmd;
  224. cmd = 0;
  225. cmd |= WSPI_CMD_READ;
  226. cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  227. cmd |= addr & WSPI_CMD_BYTE_ADDR;
  228. spi_message_init(&m);
  229. memset(t, 0, sizeof(t));
  230. t[0].tx_buf = &cmd;
  231. t[0].len = 4;
  232. spi_message_add_tail(&t[0], &m);
  233. /* Busy and non busy words read */
  234. t[1].rx_buf = busy_buf;
  235. t[1].len = TNETWIF_READ_OFFSET_BYTES;
  236. spi_message_add_tail(&t[1], &m);
  237. t[2].rx_buf = buf;
  238. t[2].len = len;
  239. spi_message_add_tail(&t[2], &m);
  240. spi_sync(wl->spi, &m);
  241. /* FIXME: check busy words */
  242. wl12xx_dump(DEBUG_SPI, "spi_read cmd -> ", &cmd, sizeof(cmd));
  243. wl12xx_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  244. }
  245. void wl12xx_spi_write(struct wl12xx *wl, int addr, void *buf,
  246. size_t len)
  247. {
  248. struct spi_transfer t[2];
  249. struct spi_message m;
  250. u32 cmd;
  251. cmd = 0;
  252. cmd |= WSPI_CMD_WRITE;
  253. cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  254. cmd |= addr & WSPI_CMD_BYTE_ADDR;
  255. spi_message_init(&m);
  256. memset(t, 0, sizeof(t));
  257. t[0].tx_buf = &cmd;
  258. t[0].len = sizeof(cmd);
  259. spi_message_add_tail(&t[0], &m);
  260. t[1].tx_buf = buf;
  261. t[1].len = len;
  262. spi_message_add_tail(&t[1], &m);
  263. spi_sync(wl->spi, &m);
  264. wl12xx_dump(DEBUG_SPI, "spi_write cmd -> ", &cmd, sizeof(cmd));
  265. wl12xx_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  266. }
  267. void wl12xx_spi_mem_read(struct wl12xx *wl, int addr, void *buf,
  268. size_t len)
  269. {
  270. int physical;
  271. physical = wl12xx_translate_mem_addr(wl, addr);
  272. wl12xx_spi_read(wl, physical, buf, len);
  273. }
  274. void wl12xx_spi_mem_write(struct wl12xx *wl, int addr, void *buf,
  275. size_t len)
  276. {
  277. int physical;
  278. physical = wl12xx_translate_mem_addr(wl, addr);
  279. wl12xx_spi_write(wl, physical, buf, len);
  280. }
  281. u32 wl12xx_mem_read32(struct wl12xx *wl, int addr)
  282. {
  283. return wl12xx_read32(wl, wl12xx_translate_mem_addr(wl, addr));
  284. }
  285. void wl12xx_mem_write32(struct wl12xx *wl, int addr, u32 val)
  286. {
  287. wl12xx_write32(wl, wl12xx_translate_mem_addr(wl, addr), val);
  288. }
  289. u32 wl12xx_reg_read32(struct wl12xx *wl, int addr)
  290. {
  291. return wl12xx_read32(wl, wl12xx_translate_reg_addr(wl, addr));
  292. }
  293. void wl12xx_reg_write32(struct wl12xx *wl, int addr, u32 val)
  294. {
  295. wl12xx_write32(wl, wl12xx_translate_reg_addr(wl, addr), val);
  296. }