wl1251_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * This file is part of wl1251
  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 "reg.h"
  28. #include "wl1251_spi.h"
  29. static int wl1251_translate_reg_addr(struct wl1251 *wl, int addr)
  30. {
  31. /* If the address is lower than REGISTERS_BASE, it means that this is
  32. * a chip-specific register address, so look it up in the registers
  33. * table */
  34. if (addr < REGISTERS_BASE) {
  35. /* Make sure we don't go over the table */
  36. if (addr >= ACX_REG_TABLE_LEN) {
  37. wl1251_error("address out of range (%d)", addr);
  38. return -EINVAL;
  39. }
  40. addr = wl->chip.acx_reg_table[addr];
  41. }
  42. return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
  43. }
  44. static int wl1251_translate_mem_addr(struct wl1251 *wl, int addr)
  45. {
  46. return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
  47. }
  48. void wl1251_spi_reset(struct wl1251 *wl)
  49. {
  50. u8 *cmd;
  51. struct spi_transfer t;
  52. struct spi_message m;
  53. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  54. if (!cmd) {
  55. wl1251_error("could not allocate cmd for spi reset");
  56. return;
  57. }
  58. memset(&t, 0, sizeof(t));
  59. spi_message_init(&m);
  60. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  61. t.tx_buf = cmd;
  62. t.len = WSPI_INIT_CMD_LEN;
  63. spi_message_add_tail(&t, &m);
  64. spi_sync(wl->spi, &m);
  65. wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  66. }
  67. void wl1251_spi_init(struct wl1251 *wl)
  68. {
  69. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  70. struct spi_transfer t;
  71. struct spi_message m;
  72. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  73. if (!cmd) {
  74. wl1251_error("could not allocate cmd for spi init");
  75. return;
  76. }
  77. memset(crc, 0, sizeof(crc));
  78. memset(&t, 0, sizeof(t));
  79. spi_message_init(&m);
  80. /*
  81. * Set WSPI_INIT_COMMAND
  82. * the data is being send from the MSB to LSB
  83. */
  84. cmd[2] = 0xff;
  85. cmd[3] = 0xff;
  86. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  87. cmd[0] = 0;
  88. cmd[7] = 0;
  89. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  90. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  91. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  92. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  93. else
  94. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  95. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  96. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  97. crc[0] = cmd[1];
  98. crc[1] = cmd[0];
  99. crc[2] = cmd[7];
  100. crc[3] = cmd[6];
  101. crc[4] = cmd[5];
  102. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  103. cmd[4] |= WSPI_INIT_CMD_END;
  104. t.tx_buf = cmd;
  105. t.len = WSPI_INIT_CMD_LEN;
  106. spi_message_add_tail(&t, &m);
  107. spi_sync(wl->spi, &m);
  108. wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  109. }
  110. /* Set the SPI partitions to access the chip addresses
  111. *
  112. * There are two VIRTUAL (SPI) partitions (the memory partition and the
  113. * registers partition), which are mapped to two different areas of the
  114. * PHYSICAL (hardware) memory. This function also makes other checks to
  115. * ensure that the partitions are not overlapping. In the diagram below, the
  116. * memory partition comes before the register partition, but the opposite is
  117. * also supported.
  118. *
  119. * PHYSICAL address
  120. * space
  121. *
  122. * | |
  123. * ...+----+--> mem_start
  124. * VIRTUAL address ... | |
  125. * space ... | | [PART_0]
  126. * ... | |
  127. * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
  128. * | | ... | |
  129. * |MEM | ... | |
  130. * | | ... | |
  131. * part_size <--+----+... | | {unused area)
  132. * | | ... | |
  133. * |REG | ... | |
  134. * part_size | | ... | |
  135. * + <--+----+... ...+----+--> reg_start
  136. * reg_size ... | |
  137. * ... | | [PART_1]
  138. * ... | |
  139. * ...+----+--> reg_start + reg_size
  140. * | |
  141. *
  142. */
  143. int wl1251_set_partition(struct wl1251 *wl,
  144. u32 mem_start, u32 mem_size,
  145. u32 reg_start, u32 reg_size)
  146. {
  147. struct wl1251_partition *partition;
  148. struct spi_transfer t;
  149. struct spi_message m;
  150. size_t len, cmd_len;
  151. u32 *cmd;
  152. int addr;
  153. cmd_len = sizeof(u32) + 2 * sizeof(struct wl1251_partition);
  154. cmd = kzalloc(cmd_len, GFP_KERNEL);
  155. if (!cmd)
  156. return -ENOMEM;
  157. spi_message_init(&m);
  158. memset(&t, 0, sizeof(t));
  159. partition = (struct wl1251_partition *) (cmd + 1);
  160. addr = HW_ACCESS_PART0_SIZE_ADDR;
  161. len = 2 * sizeof(struct wl1251_partition);
  162. *cmd |= WSPI_CMD_WRITE;
  163. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  164. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  165. wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  166. mem_start, mem_size);
  167. wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  168. reg_start, reg_size);
  169. /* Make sure that the two partitions together don't exceed the
  170. * address range */
  171. if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
  172. wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
  173. " address range. Truncating partition[0].");
  174. mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
  175. wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  176. mem_start, mem_size);
  177. wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  178. reg_start, reg_size);
  179. }
  180. if ((mem_start < reg_start) &&
  181. ((mem_start + mem_size) > reg_start)) {
  182. /* Guarantee that the memory partition doesn't overlap the
  183. * registers partition */
  184. wl1251_debug(DEBUG_SPI, "End of partition[0] is "
  185. "overlapping partition[1]. Adjusted.");
  186. mem_size = reg_start - mem_start;
  187. wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  188. mem_start, mem_size);
  189. wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  190. reg_start, reg_size);
  191. } else if ((reg_start < mem_start) &&
  192. ((reg_start + reg_size) > mem_start)) {
  193. /* Guarantee that the register partition doesn't overlap the
  194. * memory partition */
  195. wl1251_debug(DEBUG_SPI, "End of partition[1] is"
  196. " overlapping partition[0]. Adjusted.");
  197. reg_size = mem_start - reg_start;
  198. wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  199. mem_start, mem_size);
  200. wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  201. reg_start, reg_size);
  202. }
  203. partition[0].start = mem_start;
  204. partition[0].size = mem_size;
  205. partition[1].start = reg_start;
  206. partition[1].size = reg_size;
  207. wl->physical_mem_addr = mem_start;
  208. wl->physical_reg_addr = reg_start;
  209. wl->virtual_mem_addr = 0;
  210. wl->virtual_reg_addr = mem_size;
  211. t.tx_buf = cmd;
  212. t.len = cmd_len;
  213. spi_message_add_tail(&t, &m);
  214. spi_sync(wl->spi, &m);
  215. kfree(cmd);
  216. return 0;
  217. }
  218. void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  219. size_t len, bool fixed)
  220. {
  221. struct spi_transfer t[3];
  222. struct spi_message m;
  223. u8 *busy_buf;
  224. u32 *cmd;
  225. cmd = &wl->buffer_cmd;
  226. busy_buf = wl->buffer_busyword;
  227. *cmd = 0;
  228. *cmd |= WSPI_CMD_READ;
  229. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  230. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  231. if (fixed)
  232. *cmd |= WSPI_CMD_FIXED;
  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 = WL1251_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. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  248. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  249. }
  250. void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  251. size_t len, bool fixed)
  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. if (fixed)
  262. *cmd |= WSPI_CMD_FIXED;
  263. spi_message_init(&m);
  264. memset(t, 0, sizeof(t));
  265. t[0].tx_buf = cmd;
  266. t[0].len = sizeof(*cmd);
  267. spi_message_add_tail(&t[0], &m);
  268. t[1].tx_buf = buf;
  269. t[1].len = len;
  270. spi_message_add_tail(&t[1], &m);
  271. spi_sync(wl->spi, &m);
  272. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  273. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  274. }
  275. void wl1251_spi_mem_read(struct wl1251 *wl, int addr, void *buf,
  276. size_t len)
  277. {
  278. int physical;
  279. physical = wl1251_translate_mem_addr(wl, addr);
  280. wl1251_spi_read(wl, physical, buf, len, false);
  281. }
  282. void wl1251_spi_mem_write(struct wl1251 *wl, int addr, void *buf,
  283. size_t len)
  284. {
  285. int physical;
  286. physical = wl1251_translate_mem_addr(wl, addr);
  287. wl1251_spi_write(wl, physical, buf, len, false);
  288. }
  289. void wl1251_spi_reg_read(struct wl1251 *wl, int addr, void *buf, size_t len,
  290. bool fixed)
  291. {
  292. int physical;
  293. physical = wl1251_translate_reg_addr(wl, addr);
  294. wl1251_spi_read(wl, physical, buf, len, fixed);
  295. }
  296. void wl1251_spi_reg_write(struct wl1251 *wl, int addr, void *buf, size_t len,
  297. bool fixed)
  298. {
  299. int physical;
  300. physical = wl1251_translate_reg_addr(wl, addr);
  301. wl1251_spi_write(wl, physical, buf, len, fixed);
  302. }
  303. u32 wl1251_mem_read32(struct wl1251 *wl, int addr)
  304. {
  305. return wl1251_read32(wl, wl1251_translate_mem_addr(wl, addr));
  306. }
  307. void wl1251_mem_write32(struct wl1251 *wl, int addr, u32 val)
  308. {
  309. wl1251_write32(wl, wl1251_translate_mem_addr(wl, addr), val);
  310. }
  311. u32 wl1251_reg_read32(struct wl1251 *wl, int addr)
  312. {
  313. return wl1251_read32(wl, wl1251_translate_reg_addr(wl, addr));
  314. }
  315. void wl1251_reg_write32(struct wl1251 *wl, int addr, u32 val)
  316. {
  317. wl1251_write32(wl, wl1251_translate_reg_addr(wl, addr), val);
  318. }