wl1271_spi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_reg_addr(struct wl1271 *wl, int addr)
  31. {
  32. return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
  33. }
  34. static int wl1271_translate_mem_addr(struct wl1271 *wl, int addr)
  35. {
  36. return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
  37. }
  38. void wl1271_spi_reset(struct wl1271 *wl)
  39. {
  40. u8 *cmd;
  41. struct spi_transfer t;
  42. struct spi_message m;
  43. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  44. if (!cmd) {
  45. wl1271_error("could not allocate cmd for spi reset");
  46. return;
  47. }
  48. memset(&t, 0, sizeof(t));
  49. spi_message_init(&m);
  50. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  51. t.tx_buf = cmd;
  52. t.len = WSPI_INIT_CMD_LEN;
  53. spi_message_add_tail(&t, &m);
  54. spi_sync(wl->spi, &m);
  55. wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  56. }
  57. void wl1271_spi_init(struct wl1271 *wl)
  58. {
  59. u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
  60. struct spi_transfer t;
  61. struct spi_message m;
  62. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  63. if (!cmd) {
  64. wl1271_error("could not allocate cmd for spi init");
  65. return;
  66. }
  67. memset(crc, 0, sizeof(crc));
  68. memset(&t, 0, sizeof(t));
  69. spi_message_init(&m);
  70. /*
  71. * Set WSPI_INIT_COMMAND
  72. * the data is being send from the MSB to LSB
  73. */
  74. cmd[2] = 0xff;
  75. cmd[3] = 0xff;
  76. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  77. cmd[0] = 0;
  78. cmd[7] = 0;
  79. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  80. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  81. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  82. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  83. else
  84. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  85. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  86. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  87. crc[0] = cmd[1];
  88. crc[1] = cmd[0];
  89. crc[2] = cmd[7];
  90. crc[3] = cmd[6];
  91. crc[4] = cmd[5];
  92. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  93. cmd[4] |= WSPI_INIT_CMD_END;
  94. t.tx_buf = cmd;
  95. t.len = WSPI_INIT_CMD_LEN;
  96. spi_message_add_tail(&t, &m);
  97. spi_sync(wl->spi, &m);
  98. wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  99. }
  100. /* Set the SPI partitions to access the chip addresses
  101. *
  102. * There are two VIRTUAL (SPI) partitions (the memory partition and the
  103. * registers partition), which are mapped to two different areas of the
  104. * PHYSICAL (hardware) memory. This function also makes other checks to
  105. * ensure that the partitions are not overlapping. In the diagram below, the
  106. * memory partition comes before the register partition, but the opposite is
  107. * also supported.
  108. *
  109. * PHYSICAL address
  110. * space
  111. *
  112. * | |
  113. * ...+----+--> mem_start
  114. * VIRTUAL address ... | |
  115. * space ... | | [PART_0]
  116. * ... | |
  117. * 0x00000000 <--+----+... ...+----+--> mem_start + mem_size
  118. * | | ... | |
  119. * |MEM | ... | |
  120. * | | ... | |
  121. * part_size <--+----+... | | {unused area)
  122. * | | ... | |
  123. * |REG | ... | |
  124. * part_size | | ... | |
  125. * + <--+----+... ...+----+--> reg_start
  126. * reg_size ... | |
  127. * ... | | [PART_1]
  128. * ... | |
  129. * ...+----+--> reg_start + reg_size
  130. * | |
  131. *
  132. */
  133. int wl1271_set_partition(struct wl1271 *wl,
  134. u32 mem_start, u32 mem_size,
  135. u32 reg_start, u32 reg_size)
  136. {
  137. struct wl1271_partition *partition;
  138. struct spi_transfer t;
  139. struct spi_message m;
  140. size_t len, cmd_len;
  141. u32 *cmd;
  142. int addr;
  143. cmd_len = sizeof(u32) + 2 * sizeof(struct wl1271_partition);
  144. cmd = kzalloc(cmd_len, GFP_KERNEL);
  145. if (!cmd)
  146. return -ENOMEM;
  147. spi_message_init(&m);
  148. memset(&t, 0, sizeof(t));
  149. partition = (struct wl1271_partition *) (cmd + 1);
  150. addr = HW_ACCESS_PART0_SIZE_ADDR;
  151. len = 2 * sizeof(struct wl1271_partition);
  152. *cmd |= WSPI_CMD_WRITE;
  153. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  154. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  155. wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  156. mem_start, mem_size);
  157. wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  158. reg_start, reg_size);
  159. /* Make sure that the two partitions together don't exceed the
  160. * address range */
  161. if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
  162. wl1271_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
  163. " address range. Truncating partition[0].");
  164. mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
  165. wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  166. mem_start, mem_size);
  167. wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  168. reg_start, reg_size);
  169. }
  170. if ((mem_start < reg_start) &&
  171. ((mem_start + mem_size) > reg_start)) {
  172. /* Guarantee that the memory partition doesn't overlap the
  173. * registers partition */
  174. wl1271_debug(DEBUG_SPI, "End of partition[0] is "
  175. "overlapping partition[1]. Adjusted.");
  176. mem_size = reg_start - mem_start;
  177. wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  178. mem_start, mem_size);
  179. wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  180. reg_start, reg_size);
  181. } else if ((reg_start < mem_start) &&
  182. ((reg_start + reg_size) > mem_start)) {
  183. /* Guarantee that the register partition doesn't overlap the
  184. * memory partition */
  185. wl1271_debug(DEBUG_SPI, "End of partition[1] is"
  186. " overlapping partition[0]. Adjusted.");
  187. reg_size = mem_start - reg_start;
  188. wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
  189. mem_start, mem_size);
  190. wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
  191. reg_start, reg_size);
  192. }
  193. partition[0].start = mem_start;
  194. partition[0].size = mem_size;
  195. partition[1].start = reg_start;
  196. partition[1].size = reg_size;
  197. wl->physical_mem_addr = mem_start;
  198. wl->physical_reg_addr = reg_start;
  199. wl->virtual_mem_addr = 0;
  200. wl->virtual_reg_addr = mem_size;
  201. t.tx_buf = cmd;
  202. t.len = cmd_len;
  203. spi_message_add_tail(&t, &m);
  204. spi_sync(wl->spi, &m);
  205. kfree(cmd);
  206. return 0;
  207. }
  208. void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf,
  209. size_t len, bool fixed)
  210. {
  211. struct spi_transfer t[3];
  212. struct spi_message m;
  213. u8 *busy_buf;
  214. u32 *cmd;
  215. cmd = &wl->buffer_cmd;
  216. busy_buf = wl->buffer_busyword;
  217. *cmd = 0;
  218. *cmd |= WSPI_CMD_READ;
  219. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  220. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  221. if (fixed)
  222. *cmd |= WSPI_CMD_FIXED;
  223. spi_message_init(&m);
  224. memset(t, 0, sizeof(t));
  225. t[0].tx_buf = cmd;
  226. t[0].len = 4;
  227. spi_message_add_tail(&t[0], &m);
  228. /* Busy and non busy words read */
  229. t[1].rx_buf = busy_buf;
  230. t[1].len = WL1271_BUSY_WORD_LEN;
  231. spi_message_add_tail(&t[1], &m);
  232. t[2].rx_buf = buf;
  233. t[2].len = len;
  234. spi_message_add_tail(&t[2], &m);
  235. spi_sync(wl->spi, &m);
  236. /* FIXME: check busy words */
  237. wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  238. wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  239. }
  240. void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf,
  241. size_t len, bool fixed)
  242. {
  243. struct spi_transfer t[2];
  244. struct spi_message m;
  245. u32 *cmd;
  246. cmd = &wl->buffer_cmd;
  247. *cmd = 0;
  248. *cmd |= WSPI_CMD_WRITE;
  249. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  250. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  251. if (fixed)
  252. *cmd |= WSPI_CMD_FIXED;
  253. spi_message_init(&m);
  254. memset(t, 0, sizeof(t));
  255. t[0].tx_buf = cmd;
  256. t[0].len = sizeof(*cmd);
  257. spi_message_add_tail(&t[0], &m);
  258. t[1].tx_buf = buf;
  259. t[1].len = len;
  260. spi_message_add_tail(&t[1], &m);
  261. spi_sync(wl->spi, &m);
  262. wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  263. wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  264. }
  265. void wl1271_spi_mem_read(struct wl1271 *wl, int addr, void *buf,
  266. size_t len)
  267. {
  268. int physical;
  269. physical = wl1271_translate_mem_addr(wl, addr);
  270. wl1271_spi_read(wl, physical, buf, len, false);
  271. }
  272. void wl1271_spi_mem_write(struct wl1271 *wl, int addr, void *buf,
  273. size_t len)
  274. {
  275. int physical;
  276. physical = wl1271_translate_mem_addr(wl, addr);
  277. wl1271_spi_write(wl, physical, buf, len, false);
  278. }
  279. void wl1271_spi_reg_read(struct wl1271 *wl, int addr, void *buf, size_t len,
  280. bool fixed)
  281. {
  282. int physical;
  283. physical = wl1271_translate_reg_addr(wl, addr);
  284. wl1271_spi_read(wl, physical, buf, len, fixed);
  285. }
  286. void wl1271_spi_reg_write(struct wl1271 *wl, int addr, void *buf, size_t len,
  287. bool fixed)
  288. {
  289. int physical;
  290. physical = wl1271_translate_reg_addr(wl, addr);
  291. wl1271_spi_write(wl, physical, buf, len, fixed);
  292. }
  293. u32 wl1271_mem_read32(struct wl1271 *wl, int addr)
  294. {
  295. return wl1271_read32(wl, wl1271_translate_mem_addr(wl, addr));
  296. }
  297. void wl1271_mem_write32(struct wl1271 *wl, int addr, u32 val)
  298. {
  299. wl1271_write32(wl, wl1271_translate_mem_addr(wl, addr), val);
  300. }
  301. u32 wl1271_reg_read32(struct wl1271 *wl, int addr)
  302. {
  303. return wl1271_read32(wl, wl1271_translate_reg_addr(wl, addr));
  304. }
  305. void wl1271_reg_write32(struct wl1271 *wl, int addr, u32 val)
  306. {
  307. wl1271_write32(wl, wl1271_translate_reg_addr(wl, addr), val);
  308. }