wl1251_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "wl1251.h"
  27. #include "wl12xx_80211.h"
  28. #include "reg.h"
  29. #include "wl1251_spi.h"
  30. static int wl12xx_translate_reg_addr(struct wl12xx *wl, int addr)
  31. {
  32. /* If the address is lower than REGISTERS_BASE, it means that this is
  33. * a chip-specific register address, so look it up in the registers
  34. * table */
  35. if (addr < REGISTERS_BASE) {
  36. /* Make sure we don't go over the table */
  37. if (addr >= ACX_REG_TABLE_LEN) {
  38. wl12xx_error("address out of range (%d)", addr);
  39. return -EINVAL;
  40. }
  41. addr = wl->chip.acx_reg_table[addr];
  42. }
  43. return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
  44. }
  45. static int wl12xx_translate_mem_addr(struct wl12xx *wl, int addr)
  46. {
  47. return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
  48. }
  49. void wl12xx_spi_reset(struct wl12xx *wl)
  50. {
  51. u8 *cmd;
  52. struct spi_transfer t;
  53. struct spi_message m;
  54. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  55. if (!cmd) {
  56. wl12xx_error("could not allocate cmd for spi reset");
  57. return;
  58. }
  59. memset(&t, 0, sizeof(t));
  60. spi_message_init(&m);
  61. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  62. t.tx_buf = cmd;
  63. t.len = WSPI_INIT_CMD_LEN;
  64. spi_message_add_tail(&t, &m);
  65. spi_sync(wl->spi, &m);
  66. wl12xx_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  67. }
  68. void wl12xx_spi_init(struct wl12xx *wl)
  69. {
  70. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  71. struct spi_transfer t;
  72. struct spi_message m;
  73. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  74. if (!cmd) {
  75. wl12xx_error("could not allocate cmd for spi init");
  76. return;
  77. }
  78. memset(crc, 0, sizeof(crc));
  79. memset(&t, 0, sizeof(t));
  80. spi_message_init(&m);
  81. /*
  82. * Set WSPI_INIT_COMMAND
  83. * the data is being send from the MSB to LSB
  84. */
  85. cmd[2] = 0xff;
  86. cmd[3] = 0xff;
  87. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  88. cmd[0] = 0;
  89. cmd[7] = 0;
  90. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  91. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  92. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  93. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  94. else
  95. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  96. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  97. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  98. crc[0] = cmd[1];
  99. crc[1] = cmd[0];
  100. crc[2] = cmd[7];
  101. crc[3] = cmd[6];
  102. crc[4] = cmd[5];
  103. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  104. cmd[4] |= WSPI_INIT_CMD_END;
  105. t.tx_buf = cmd;
  106. t.len = WSPI_INIT_CMD_LEN;
  107. spi_message_add_tail(&t, &m);
  108. spi_sync(wl->spi, &m);
  109. wl12xx_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  110. }
  111. /* Set the SPI partitions to access the chip addresses
  112. *
  113. * There are two VIRTUAL (SPI) partitions (the memory partition and the
  114. * registers partition), which are mapped to two different areas of the
  115. * PHYSICAL (hardware) memory. This function also makes other checks to
  116. * ensure that the partitions are not overlapping. In the diagram below, the
  117. * memory partition comes before the register partition, but the opposite is
  118. * also supported.
  119. *
  120. * PHYSICAL address
  121. * space
  122. *
  123. * | |
  124. * ...+----+--> mem_start
  125. * VIRTUAL address ... | |
  126. * space ... | | [PART_0]
  127. * ... | |
  128. * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
  129. * | | ... | |
  130. * |MEM | ... | |
  131. * | | ... | |
  132. * part_size <--+----+... | | {unused area)
  133. * | | ... | |
  134. * |REG | ... | |
  135. * part_size | | ... | |
  136. * + <--+----+... ...+----+--> reg_start
  137. * reg_size ... | |
  138. * ... | | [PART_1]
  139. * ... | |
  140. * ...+----+--> reg_start + reg_size
  141. * | |
  142. *
  143. */
  144. int wl12xx_set_partition(struct wl12xx *wl,
  145. u32 mem_start, u32 mem_size,
  146. u32 reg_start, u32 reg_size)
  147. {
  148. struct wl12xx_partition *partition;
  149. struct spi_transfer t;
  150. struct spi_message m;
  151. size_t len, cmd_len;
  152. u32 *cmd;
  153. int addr;
  154. cmd_len = sizeof(u32) + 2 * sizeof(struct wl12xx_partition);
  155. cmd = kzalloc(cmd_len, GFP_KERNEL);
  156. if (!cmd)
  157. return -ENOMEM;
  158. spi_message_init(&m);
  159. memset(&t, 0, sizeof(t));
  160. partition = (struct wl12xx_partition *) (cmd + 1);
  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 = cmd;
  213. t.len = cmd_len;
  214. spi_message_add_tail(&t, &m);
  215. spi_sync(wl->spi, &m);
  216. kfree(cmd);
  217. return 0;
  218. }
  219. void wl12xx_spi_read(struct wl12xx *wl, int addr, void *buf,
  220. size_t len, bool fixed)
  221. {
  222. struct spi_transfer t[3];
  223. struct spi_message m;
  224. u8 *busy_buf;
  225. u32 *cmd;
  226. cmd = &wl->buffer_cmd;
  227. busy_buf = wl->buffer_busyword;
  228. *cmd = 0;
  229. *cmd |= WSPI_CMD_READ;
  230. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  231. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  232. if (fixed)
  233. *cmd |= WSPI_CMD_FIXED;
  234. spi_message_init(&m);
  235. memset(t, 0, sizeof(t));
  236. t[0].tx_buf = cmd;
  237. t[0].len = 4;
  238. spi_message_add_tail(&t[0], &m);
  239. /* Busy and non busy words read */
  240. t[1].rx_buf = busy_buf;
  241. t[1].len = WL12XX_BUSY_WORD_LEN;
  242. spi_message_add_tail(&t[1], &m);
  243. t[2].rx_buf = buf;
  244. t[2].len = len;
  245. spi_message_add_tail(&t[2], &m);
  246. spi_sync(wl->spi, &m);
  247. /* FIXME: check busy words */
  248. wl12xx_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  249. wl12xx_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  250. }
  251. void wl12xx_spi_write(struct wl12xx *wl, int addr, void *buf,
  252. size_t len, bool fixed)
  253. {
  254. struct spi_transfer t[2];
  255. struct spi_message m;
  256. u32 *cmd;
  257. cmd = &wl->buffer_cmd;
  258. *cmd = 0;
  259. *cmd |= WSPI_CMD_WRITE;
  260. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  261. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  262. if (fixed)
  263. *cmd |= WSPI_CMD_FIXED;
  264. spi_message_init(&m);
  265. memset(t, 0, sizeof(t));
  266. t[0].tx_buf = cmd;
  267. t[0].len = sizeof(*cmd);
  268. spi_message_add_tail(&t[0], &m);
  269. t[1].tx_buf = buf;
  270. t[1].len = len;
  271. spi_message_add_tail(&t[1], &m);
  272. spi_sync(wl->spi, &m);
  273. wl12xx_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  274. wl12xx_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  275. }
  276. void wl12xx_spi_mem_read(struct wl12xx *wl, int addr, void *buf,
  277. size_t len)
  278. {
  279. int physical;
  280. physical = wl12xx_translate_mem_addr(wl, addr);
  281. wl12xx_spi_read(wl, physical, buf, len, false);
  282. }
  283. void wl12xx_spi_mem_write(struct wl12xx *wl, int addr, void *buf,
  284. size_t len)
  285. {
  286. int physical;
  287. physical = wl12xx_translate_mem_addr(wl, addr);
  288. wl12xx_spi_write(wl, physical, buf, len, false);
  289. }
  290. void wl12xx_spi_reg_read(struct wl12xx *wl, int addr, void *buf, size_t len,
  291. bool fixed)
  292. {
  293. int physical;
  294. physical = wl12xx_translate_reg_addr(wl, addr);
  295. wl12xx_spi_read(wl, physical, buf, len, fixed);
  296. }
  297. void wl12xx_spi_reg_write(struct wl12xx *wl, int addr, void *buf, size_t len,
  298. bool fixed)
  299. {
  300. int physical;
  301. physical = wl12xx_translate_reg_addr(wl, addr);
  302. wl12xx_spi_write(wl, physical, buf, len, fixed);
  303. }
  304. u32 wl12xx_mem_read32(struct wl12xx *wl, int addr)
  305. {
  306. return wl12xx_read32(wl, wl12xx_translate_mem_addr(wl, addr));
  307. }
  308. void wl12xx_mem_write32(struct wl12xx *wl, int addr, u32 val)
  309. {
  310. wl12xx_write32(wl, wl12xx_translate_mem_addr(wl, addr), val);
  311. }
  312. u32 wl12xx_reg_read32(struct wl12xx *wl, int addr)
  313. {
  314. return wl12xx_read32(wl, wl12xx_translate_reg_addr(wl, addr));
  315. }
  316. void wl12xx_reg_write32(struct wl12xx *wl, int addr, u32 val)
  317. {
  318. wl12xx_write32(wl, wl12xx_translate_reg_addr(wl, addr), val);
  319. }