spi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. int wl12xx_set_partition(struct wl12xx *wl,
  146. u32 mem_start, u32 mem_size,
  147. u32 reg_start, u32 reg_size)
  148. {
  149. struct wl12xx_partition *partition;
  150. struct spi_transfer t;
  151. struct spi_message m;
  152. size_t len, cmd_len;
  153. u32 *cmd;
  154. int addr;
  155. cmd_len = sizeof(u32) + 2 * sizeof(struct wl12xx_partition);
  156. cmd = kzalloc(cmd_len, GFP_KERNEL);
  157. if (!cmd)
  158. return -ENOMEM;
  159. spi_message_init(&m);
  160. memset(&t, 0, sizeof(t));
  161. partition = (struct wl12xx_partition *) (cmd + 1);
  162. addr = HW_ACCESS_PART0_SIZE_ADDR;
  163. len = 2 * sizeof(struct wl12xx_partition);
  164. *cmd |= WSPI_CMD_WRITE;
  165. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  166. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  167. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  168. mem_start, mem_size);
  169. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  170. reg_start, reg_size);
  171. /* Make sure that the two partitions together don't exceed the
  172. * address range */
  173. if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
  174. wl12xx_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
  175. " address range. Truncating partition[0].");
  176. mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
  177. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  178. mem_start, mem_size);
  179. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  180. reg_start, reg_size);
  181. }
  182. if ((mem_start < reg_start) &&
  183. ((mem_start + mem_size) > reg_start)) {
  184. /* Guarantee that the memory partition doesn't overlap the
  185. * registers partition */
  186. wl12xx_debug(DEBUG_SPI, "End of partition[0] is "
  187. "overlapping partition[1]. Adjusted.");
  188. mem_size = reg_start - mem_start;
  189. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  190. mem_start, mem_size);
  191. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  192. reg_start, reg_size);
  193. } else if ((reg_start < mem_start) &&
  194. ((reg_start + reg_size) > mem_start)) {
  195. /* Guarantee that the register partition doesn't overlap the
  196. * memory partition */
  197. wl12xx_debug(DEBUG_SPI, "End of partition[1] is"
  198. " overlapping partition[0]. Adjusted.");
  199. reg_size = mem_start - reg_start;
  200. wl12xx_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  201. mem_start, mem_size);
  202. wl12xx_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  203. reg_start, reg_size);
  204. }
  205. partition[0].start = mem_start;
  206. partition[0].size = mem_size;
  207. partition[1].start = reg_start;
  208. partition[1].size = reg_size;
  209. wl->physical_mem_addr = mem_start;
  210. wl->physical_reg_addr = reg_start;
  211. wl->virtual_mem_addr = 0;
  212. wl->virtual_reg_addr = mem_size;
  213. t.tx_buf = cmd;
  214. t.len = cmd_len;
  215. spi_message_add_tail(&t, &m);
  216. spi_sync(wl->spi, &m);
  217. kfree(cmd);
  218. return 0;
  219. }
  220. void wl12xx_spi_read(struct wl12xx *wl, int addr, void *buf,
  221. size_t len)
  222. {
  223. struct spi_transfer t[3];
  224. struct spi_message m;
  225. u8 *busy_buf;
  226. u32 *cmd;
  227. cmd = &wl->buffer_cmd;
  228. busy_buf = wl->buffer_busyword;
  229. *cmd = 0;
  230. *cmd |= WSPI_CMD_READ;
  231. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  232. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  233. spi_message_init(&m);
  234. memset(t, 0, sizeof(t));
  235. t[0].tx_buf = cmd;
  236. t[0].len = 4;
  237. spi_message_add_tail(&t[0], &m);
  238. /* Busy and non busy words read */
  239. t[1].rx_buf = busy_buf;
  240. t[1].len = WL12XX_BUSY_WORD_LEN;
  241. spi_message_add_tail(&t[1], &m);
  242. t[2].rx_buf = buf;
  243. t[2].len = len;
  244. spi_message_add_tail(&t[2], &m);
  245. spi_sync(wl->spi, &m);
  246. /* FIXME: check busy words */
  247. wl12xx_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  248. wl12xx_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  249. }
  250. void wl12xx_spi_write(struct wl12xx *wl, int addr, void *buf,
  251. size_t len)
  252. {
  253. struct spi_transfer t[2];
  254. struct spi_message m;
  255. u32 *cmd;
  256. cmd = &wl->buffer_cmd;
  257. *cmd = 0;
  258. *cmd |= WSPI_CMD_WRITE;
  259. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  260. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  261. spi_message_init(&m);
  262. memset(t, 0, sizeof(t));
  263. t[0].tx_buf = cmd;
  264. t[0].len = sizeof(*cmd);
  265. spi_message_add_tail(&t[0], &m);
  266. t[1].tx_buf = buf;
  267. t[1].len = len;
  268. spi_message_add_tail(&t[1], &m);
  269. spi_sync(wl->spi, &m);
  270. wl12xx_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  271. wl12xx_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  272. }
  273. void wl12xx_spi_mem_read(struct wl12xx *wl, int addr, void *buf,
  274. size_t len)
  275. {
  276. int physical;
  277. physical = wl12xx_translate_mem_addr(wl, addr);
  278. wl12xx_spi_read(wl, physical, buf, len);
  279. }
  280. void wl12xx_spi_mem_write(struct wl12xx *wl, int addr, void *buf,
  281. size_t len)
  282. {
  283. int physical;
  284. physical = wl12xx_translate_mem_addr(wl, addr);
  285. wl12xx_spi_write(wl, physical, buf, len);
  286. }
  287. u32 wl12xx_mem_read32(struct wl12xx *wl, int addr)
  288. {
  289. return wl12xx_read32(wl, wl12xx_translate_mem_addr(wl, addr));
  290. }
  291. void wl12xx_mem_write32(struct wl12xx *wl, int addr, u32 val)
  292. {
  293. wl12xx_write32(wl, wl12xx_translate_mem_addr(wl, addr), val);
  294. }
  295. u32 wl12xx_reg_read32(struct wl12xx *wl, int addr)
  296. {
  297. return wl12xx_read32(wl, wl12xx_translate_reg_addr(wl, addr));
  298. }
  299. void wl12xx_reg_write32(struct wl12xx *wl, int addr, u32 val)
  300. {
  301. wl12xx_write32(wl, wl12xx_translate_reg_addr(wl, addr), val);
  302. }