fc8150_spi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*****************************************************************************
  2. Copyright(c) 2012 FCI Inc. All Rights Reserved
  3. File name : fc8150_spi.c
  4. Description : fc8150 host interface
  5. *******************************************************************************/
  6. #include <linux/spi/spi.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include "fci_types.h"
  10. #include "fc8150_regs.h"
  11. #include "fci_oal.h"
  12. #define SPI_BMODE 0x00
  13. #define SPI_WMODE 0x10
  14. #define SPI_LMODE 0x20
  15. #define SPI_READ 0x40
  16. #define SPI_WRITE 0x00
  17. #define SPI_AINC 0x80
  18. #define CHIPID (0 << 3)
  19. #define DRIVER_NAME "fc8150_spi"
  20. struct spi_device *fc8150_spi;
  21. static u8 tx_data[10];
  22. static u8 rdata_buf[8192];
  23. static u8 wdata_buf[8192];
  24. static DEFINE_MUTEX(lock);
  25. static int __devinit fc8150_spi_probe(struct spi_device *spi)
  26. {
  27. s32 ret;
  28. PRINTF(0, "fc8150_spi_probe\n");
  29. spi->max_speed_hz = 24000000;
  30. spi->bits_per_word = 8;
  31. spi->mode = SPI_MODE_0;
  32. ret = spi_setup(spi);
  33. if (ret < 0)
  34. return ret;
  35. fc8150_spi = spi;
  36. return ret;
  37. }
  38. static int fc8150_spi_remove(struct spi_device *spi)
  39. {
  40. return 0;
  41. }
  42. static struct spi_driver fc8150_spi_driver = {
  43. .driver = {
  44. .name = DRIVER_NAME,
  45. .owner = THIS_MODULE,
  46. },
  47. .probe = fc8150_spi_probe,
  48. .remove = __devexit_p(fc8150_spi_remove),
  49. };
  50. static int fc8150_spi_write_then_read(struct spi_device *spi
  51. , u8 *txbuf, u16 tx_length, u8 *rxbuf, u16 rx_length)
  52. {
  53. int res = 0;
  54. struct spi_message message;
  55. struct spi_transfer x;
  56. spi_message_init(&message);
  57. memset(&x, 0, sizeof x);
  58. spi_message_add_tail(&x, &message);
  59. memcpy(&wdata_buf[0], txbuf, tx_length);
  60. x.tx_buf = &wdata_buf[0];
  61. x.rx_buf = &rdata_buf[0];
  62. x.len = tx_length + rx_length;
  63. x.cs_change = 0;
  64. x.bits_per_word = 8;
  65. res = spi_sync(spi, &message);
  66. memcpy(rxbuf, x.rx_buf + tx_length, rx_length);
  67. return res;
  68. }
  69. static int spi_bulkread(HANDLE hDevice, u16 addr
  70. , u8 command, u8 *data, u16 length)
  71. {
  72. int res;
  73. tx_data[0] = addr & 0xff;
  74. tx_data[1] = (addr >> 8) & 0xff;
  75. tx_data[2] = (command & 0xf0) | CHIPID | ((length >> 16) & 0x07);
  76. tx_data[3] = (length >> 8) & 0xff;
  77. tx_data[4] = length & 0xff;
  78. res = fc8150_spi_write_then_read(fc8150_spi, &tx_data[0]
  79. , 5, data, length);
  80. if (res) {
  81. PRINTF(0, "fc8150_spi_bulkread fail : %d\n", res);
  82. return BBM_NOK;
  83. }
  84. return BBM_OK;
  85. }
  86. static int spi_bulkwrite(HANDLE hDevice, u16 addr
  87. , u8 command, u8 *data, u16 length)
  88. {
  89. int i;
  90. int res;
  91. tx_data[0] = addr & 0xff;
  92. tx_data[1] = (addr >> 8) & 0xff;
  93. tx_data[2] = (command & 0xf0) | CHIPID | ((length >> 16) & 0x07);
  94. tx_data[3] = (length >> 8) & 0xff;
  95. tx_data[4] = length & 0xff;
  96. for (i = 0; i < length; i++)
  97. tx_data[5+i] = data[i];
  98. res = fc8150_spi_write_then_read(fc8150_spi
  99. , &tx_data[0], length+5, NULL, 0);
  100. if (res) {
  101. PRINTF(0, "fc8150_spi_bulkwrite fail : %d\n", res);
  102. return BBM_NOK;
  103. }
  104. return BBM_OK;
  105. }
  106. static int spi_dataread(HANDLE hDevice, u16 addr
  107. , u8 command, u8 *data, u32 length)
  108. {
  109. int res;
  110. tx_data[0] = addr & 0xff;
  111. tx_data[1] = (addr >> 8) & 0xff;
  112. tx_data[2] = (command & 0xf0) | CHIPID | ((length >> 16) & 0x07);
  113. tx_data[3] = (length >> 8) & 0xff;
  114. tx_data[4] = length & 0xff;
  115. res = fc8150_spi_write_then_read(fc8150_spi
  116. , &tx_data[0], 5, data, length);
  117. if (res) {
  118. PRINTF(0, "fc8150_spi_dataread fail : %d\n", res);
  119. return BBM_NOK;
  120. }
  121. return BBM_OK;
  122. }
  123. int fc8150_spi_init(HANDLE hDevice, u16 param1, u16 param2)
  124. {
  125. int res = 0;
  126. PRINTF(0, "fc8150_spi_init : %d\n", res);
  127. res = spi_register_driver(&fc8150_spi_driver);
  128. if (res) {
  129. PRINTF(0, "fc8150_spi register fail : %d\n", res);
  130. return BBM_NOK;
  131. }
  132. return res;
  133. }
  134. int fc8150_spi_byteread(HANDLE hDevice, u16 addr, u8 *data)
  135. {
  136. int res;
  137. u8 command = SPI_READ;
  138. mutex_lock(&lock);
  139. res = spi_bulkread(hDevice, addr, command, data, 1);
  140. mutex_unlock(&lock);
  141. return res;
  142. }
  143. int fc8150_spi_wordread(HANDLE hDevice, u16 addr, u16 *data)
  144. {
  145. int res;
  146. u8 command = SPI_READ | SPI_AINC;
  147. mutex_lock(&lock);
  148. res = spi_bulkread(hDevice, addr, command, (u8 *)data, 2);
  149. mutex_unlock(&lock);
  150. return res;
  151. }
  152. int fc8150_spi_longread(HANDLE hDevice, u16 addr, u32 *data)
  153. {
  154. int res;
  155. u8 command = SPI_READ | SPI_AINC;
  156. mutex_lock(&lock);
  157. res = spi_bulkread(hDevice, addr, command, (u8 *)data, 4);
  158. mutex_unlock(&lock);
  159. return res;
  160. }
  161. int fc8150_spi_bulkread(HANDLE hDevice, u16 addr, u8 *data, u16 length)
  162. {
  163. int res;
  164. u8 command = SPI_READ | SPI_AINC;
  165. mutex_lock(&lock);
  166. res = spi_bulkread(hDevice, addr, command, data, length);
  167. mutex_unlock(&lock);
  168. return res;
  169. }
  170. int fc8150_spi_bytewrite(HANDLE hDevice, u16 addr, u8 data)
  171. {
  172. int res;
  173. u8 command = SPI_WRITE;
  174. mutex_lock(&lock);
  175. res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 1);
  176. mutex_unlock(&lock);
  177. return res;
  178. }
  179. int fc8150_spi_wordwrite(HANDLE hDevice, u16 addr, u16 data)
  180. {
  181. int res;
  182. u8 command = SPI_WRITE | SPI_AINC;
  183. mutex_lock(&lock);
  184. res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 2);
  185. mutex_unlock(&lock);
  186. return res;
  187. }
  188. int fc8150_spi_longwrite(HANDLE hDevice, u16 addr, u32 data)
  189. {
  190. int res;
  191. u8 command = SPI_WRITE | SPI_AINC;
  192. mutex_lock(&lock);
  193. res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 4);
  194. mutex_unlock(&lock);
  195. return res;
  196. }
  197. int fc8150_spi_bulkwrite(HANDLE hDevice, u16 addr, u8 *data, u16 length)
  198. {
  199. int res;
  200. u8 command = SPI_WRITE | SPI_AINC;
  201. mutex_lock(&lock);
  202. res = spi_bulkwrite(hDevice, addr, command, data, length);
  203. mutex_unlock(&lock);
  204. return res;
  205. }
  206. int fc8150_spi_dataread(HANDLE hDevice, u16 addr, u8 *data, u32 length)
  207. {
  208. int res;
  209. u8 command = SPI_READ;
  210. mutex_lock(&lock);
  211. res = spi_dataread(hDevice, addr, command, data, length);
  212. mutex_unlock(&lock);
  213. return res;
  214. }
  215. int fc8150_spi_deinit(HANDLE hDevice)
  216. {
  217. return BBM_OK;
  218. }