wl1251_spi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 void wl1251_spi_reset(struct wl1251 *wl)
  30. {
  31. u8 *cmd;
  32. struct spi_transfer t;
  33. struct spi_message m;
  34. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  35. if (!cmd) {
  36. wl1251_error("could not allocate cmd for spi reset");
  37. return;
  38. }
  39. memset(&t, 0, sizeof(t));
  40. spi_message_init(&m);
  41. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  42. t.tx_buf = cmd;
  43. t.len = WSPI_INIT_CMD_LEN;
  44. spi_message_add_tail(&t, &m);
  45. spi_sync(wl->spi, &m);
  46. wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  47. }
  48. static void wl1251_spi_init(struct wl1251 *wl)
  49. {
  50. u8 crc[WSPI_INIT_CMD_CRC_LEN], *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 init");
  56. return;
  57. }
  58. memset(crc, 0, sizeof(crc));
  59. memset(&t, 0, sizeof(t));
  60. spi_message_init(&m);
  61. /*
  62. * Set WSPI_INIT_COMMAND
  63. * the data is being send from the MSB to LSB
  64. */
  65. cmd[2] = 0xff;
  66. cmd[3] = 0xff;
  67. cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  68. cmd[0] = 0;
  69. cmd[7] = 0;
  70. cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  71. cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  72. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  73. cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  74. else
  75. cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  76. cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  77. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  78. crc[0] = cmd[1];
  79. crc[1] = cmd[0];
  80. crc[2] = cmd[7];
  81. crc[3] = cmd[6];
  82. crc[4] = cmd[5];
  83. cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
  84. cmd[4] |= WSPI_INIT_CMD_END;
  85. t.tx_buf = cmd;
  86. t.len = WSPI_INIT_CMD_LEN;
  87. spi_message_add_tail(&t, &m);
  88. spi_sync(wl->spi, &m);
  89. wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  90. }
  91. static void wl1251_spi_reset_wake(struct wl1251 *wl)
  92. {
  93. wl1251_spi_reset(wl);
  94. wl1251_spi_init(wl);
  95. }
  96. static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  97. size_t len)
  98. {
  99. struct spi_transfer t[3];
  100. struct spi_message m;
  101. u8 *busy_buf;
  102. u32 *cmd;
  103. cmd = &wl->buffer_cmd;
  104. busy_buf = wl->buffer_busyword;
  105. *cmd = 0;
  106. *cmd |= WSPI_CMD_READ;
  107. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  108. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  109. spi_message_init(&m);
  110. memset(t, 0, sizeof(t));
  111. t[0].tx_buf = cmd;
  112. t[0].len = 4;
  113. spi_message_add_tail(&t[0], &m);
  114. /* Busy and non busy words read */
  115. t[1].rx_buf = busy_buf;
  116. t[1].len = WL1251_BUSY_WORD_LEN;
  117. spi_message_add_tail(&t[1], &m);
  118. t[2].rx_buf = buf;
  119. t[2].len = len;
  120. spi_message_add_tail(&t[2], &m);
  121. spi_sync(wl->spi, &m);
  122. /* FIXME: check busy words */
  123. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  124. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  125. }
  126. static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  127. size_t len)
  128. {
  129. struct spi_transfer t[2];
  130. struct spi_message m;
  131. u32 *cmd;
  132. cmd = &wl->buffer_cmd;
  133. *cmd = 0;
  134. *cmd |= WSPI_CMD_WRITE;
  135. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  136. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  137. spi_message_init(&m);
  138. memset(t, 0, sizeof(t));
  139. t[0].tx_buf = cmd;
  140. t[0].len = sizeof(*cmd);
  141. spi_message_add_tail(&t[0], &m);
  142. t[1].tx_buf = buf;
  143. t[1].len = len;
  144. spi_message_add_tail(&t[1], &m);
  145. spi_sync(wl->spi, &m);
  146. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  147. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  148. }
  149. const struct wl1251_if_operations wl1251_spi_ops = {
  150. .read = wl1251_spi_read,
  151. .write = wl1251_spi_write,
  152. .reset = wl1251_spi_reset_wake,
  153. };