fc8300_spi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*****************************************************************************
  2. Copyright(c) 2013 FCI Inc. All Rights Reserved
  3. File name : fc8300_spi.c
  4. Description : source of SPI interface
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. History :
  17. ----------------------------------------------------------------------
  18. *******************************************************************************/
  19. #include <linux/spi/spi.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include "fci_types.h"
  23. #include "fc8300_regs.h"
  24. #include "fci_oal.h"
  25. #define SPI_LEN 0x00 /* or 0x10 */
  26. #define SPI_REG 0x20
  27. #define SPI_THR 0x30
  28. #define SPI_READ 0x40
  29. #define SPI_WRITE 0x00
  30. #define SPI_AINC 0x80
  31. #define DRIVER_NAME "fc8300_spi"
  32. struct spi_device *fc8300_spi;
  33. static u8 tx_data[10];
  34. static u8 wdata_buf[32] __cacheline_aligned;
  35. static u8 rdata_buf[65536] __cacheline_aligned;
  36. static DEFINE_MUTEX(fci_spi_lock);
  37. static int fc8300_spi_probe(struct spi_device *spi)
  38. {
  39. s32 ret;
  40. print_log(0, "fc8300_spi_probe\n");
  41. spi->max_speed_hz = 52000000;
  42. spi->bits_per_word = 8;
  43. spi->mode = SPI_MODE_0;
  44. ret = spi_setup(spi);
  45. if (ret < 0)
  46. return ret;
  47. fc8300_spi = spi;
  48. return ret;
  49. }
  50. static int fc8300_spi_remove(struct spi_device *spi)
  51. {
  52. return 0;
  53. }
  54. static struct spi_driver fc8300_spi_driver = {
  55. .driver = {
  56. .name = DRIVER_NAME,
  57. .owner = THIS_MODULE,
  58. },
  59. .probe = fc8300_spi_probe,
  60. .remove = fc8300_spi_remove,
  61. };
  62. static int fc8300_spi_write_then_read(struct spi_device *spi
  63. , u8 *txbuf, u16 tx_length, u8 *rxbuf, u16 rx_length)
  64. {
  65. int res = 0;
  66. struct spi_message message;
  67. struct spi_transfer x;
  68. if (spi == NULL) {
  69. print_log(0, "[ERROR] FC8300_SPI Handle Fail...........\n");
  70. return BBM_NOK;
  71. }
  72. spi_message_init(&message);
  73. memset(&x, 0, sizeof x);
  74. spi_message_add_tail(&x, &message);
  75. memcpy(&wdata_buf[0], txbuf, tx_length);
  76. x.tx_buf = &wdata_buf[0];
  77. x.rx_buf = &rdata_buf[0];
  78. x.len = tx_length + rx_length;
  79. x.cs_change = 0;
  80. x.bits_per_word = 8;
  81. res = spi_sync(spi, &message);
  82. memcpy(rxbuf, x.rx_buf + tx_length, rx_length);
  83. return res;
  84. }
  85. static s32 spi_bulkread(HANDLE handle, u8 devid,
  86. u16 addr, u8 command, u8 *data, u16 length)
  87. {
  88. int res;
  89. tx_data[0] = addr & 0xff;
  90. tx_data[1] = (addr >> 8) & 0xff;
  91. tx_data[2] = command | devid;
  92. tx_data[3] = length & 0xff;
  93. res = fc8300_spi_write_then_read(fc8300_spi
  94. , &tx_data[0], 4, data, length);
  95. if (res) {
  96. print_log(0, "fc8300_spi_bulkread fail : %d\n", res);
  97. return BBM_NOK;
  98. }
  99. return res;
  100. }
  101. static s32 spi_bulkwrite(HANDLE handle, u8 devid,
  102. u16 addr, u8 command, u8 *data, u16 length)
  103. {
  104. int i;
  105. int res;
  106. tx_data[0] = addr & 0xff;
  107. tx_data[1] = (addr >> 8) & 0xff;
  108. tx_data[2] = command | devid;
  109. tx_data[3] = length & 0xff;
  110. for (i = 0 ; i < length ; i++)
  111. tx_data[4+i] = data[i];
  112. res = fc8300_spi_write_then_read(fc8300_spi
  113. , &tx_data[0], length+4, NULL, 0);
  114. if (res) {
  115. print_log(0, "fc8300_spi_bulkwrite fail : %d\n", res);
  116. return BBM_NOK;
  117. }
  118. return res;
  119. }
  120. static s32 spi_dataread(HANDLE handle, u8 devid,
  121. u16 addr, u8 command, u8 *data, u32 length)
  122. {
  123. int res;
  124. tx_data[0] = addr & 0xff;
  125. tx_data[1] = (addr >> 8) & 0xff;
  126. tx_data[2] = command | devid;
  127. tx_data[3] = length & 0xff;
  128. res = fc8300_spi_write_then_read(fc8300_spi
  129. , &tx_data[0], 4, data, length);
  130. if (res) {
  131. print_log(0, "fc8300_spi_dataread fail : %d\n", res);
  132. return BBM_NOK;
  133. }
  134. return res;
  135. }
  136. s32 fc8300_spi_init(HANDLE handle, u16 param1, u16 param2)
  137. {
  138. int res = 0;
  139. print_log(0, "fc8300_spi_init : %d\n", res);
  140. res = spi_register_driver(&fc8300_spi_driver);
  141. if (res) {
  142. print_log(0, "fc8300_spi register fail : %d\n", res);
  143. return BBM_NOK;
  144. }
  145. return res;
  146. }
  147. s32 fc8300_spi_byteread(HANDLE handle, DEVICEID devid, u16 addr, u8 *data)
  148. {
  149. s32 res;
  150. u8 command = SPI_READ;
  151. mutex_lock(&fci_spi_lock);
  152. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  153. data, 1);
  154. mutex_unlock(&fci_spi_lock);
  155. return res;
  156. }
  157. s32 fc8300_spi_wordread(HANDLE handle, DEVICEID devid, u16 addr, u16 *data)
  158. {
  159. s32 res;
  160. u8 command = SPI_READ | SPI_AINC;
  161. mutex_lock(&fci_spi_lock);
  162. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  163. (u8 *)data, 2);
  164. mutex_unlock(&fci_spi_lock);
  165. return res;
  166. }
  167. s32 fc8300_spi_longread(HANDLE handle, DEVICEID devid, u16 addr, u32 *data)
  168. {
  169. s32 res;
  170. u8 command = SPI_READ | SPI_AINC;
  171. mutex_lock(&fci_spi_lock);
  172. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  173. (u8 *)data, 4);
  174. mutex_unlock(&fci_spi_lock);
  175. return res;
  176. }
  177. s32 fc8300_spi_bulkread(HANDLE handle, DEVICEID devid,
  178. u16 addr, u8 *data, u16 length)
  179. {
  180. s32 res;
  181. u8 command = SPI_READ | SPI_AINC;
  182. mutex_lock(&fci_spi_lock);
  183. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  184. data, length);
  185. mutex_unlock(&fci_spi_lock);
  186. return res;
  187. }
  188. s32 fc8300_spi_bytewrite(HANDLE handle, DEVICEID devid, u16 addr, u8 data)
  189. {
  190. s32 res;
  191. u8 command = SPI_WRITE;
  192. mutex_lock(&fci_spi_lock);
  193. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  194. (u8 *)&data, 1);
  195. mutex_unlock(&fci_spi_lock);
  196. return res;
  197. }
  198. s32 fc8300_spi_wordwrite(HANDLE handle, DEVICEID devid, u16 addr, u16 data)
  199. {
  200. s32 res;
  201. #ifdef BBM_ES
  202. u8 command = SPI_WRITE;
  203. if ((addr & 0xff00) != 0x0f00)
  204. command |= SPI_AINC;
  205. #else
  206. u8 command = SPI_WRITE | SPI_AINC;
  207. #endif
  208. mutex_lock(&fci_spi_lock);
  209. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  210. (u8 *)&data, 2);
  211. mutex_unlock(&fci_spi_lock);
  212. return res;
  213. }
  214. s32 fc8300_spi_longwrite(HANDLE handle, DEVICEID devid, u16 addr, u32 data)
  215. {
  216. s32 res;
  217. u8 command = SPI_WRITE | SPI_AINC;
  218. mutex_lock(&fci_spi_lock);
  219. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  220. (u8 *) &data, 4);
  221. mutex_unlock(&fci_spi_lock);
  222. return res;
  223. }
  224. s32 fc8300_spi_bulkwrite(HANDLE handle, DEVICEID devid,
  225. u16 addr, u8 *data, u16 length)
  226. {
  227. s32 res;
  228. u8 command = SPI_WRITE | SPI_AINC;
  229. mutex_lock(&fci_spi_lock);
  230. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  231. data, length);
  232. mutex_unlock(&fci_spi_lock);
  233. return res;
  234. }
  235. s32 fc8300_spi_dataread(HANDLE handle, DEVICEID devid,
  236. u16 addr, u8 *data, u32 length)
  237. {
  238. s32 res = 0;
  239. #ifdef SPI_DATAREAD_REGMODE
  240. u8 command = SPI_READ | SPI_REG;
  241. u32 read_len = 0;
  242. u32 i, m, r;
  243. mutex_lock(&fci_spi_lock);
  244. if (length > SPI_DMA_MAX_SIZE) {
  245. m = length / SPI_DMA_MAX_SIZE;
  246. r = length % SPI_DMA_MAX_SIZE;
  247. for (i = 0; i < m ; i++) {
  248. res |= spi_dataread(handle, (u8) (devid & 0x000f), addr
  249. , command, &data[read_len], SPI_DMA_MAX_SIZE);
  250. read_len += SPI_DMA_MAX_SIZE;
  251. }
  252. } else {
  253. res = spi_dataread(handle, (u8) (devid & 0x000f), addr, command,
  254. data, length);
  255. }
  256. mutex_unlock(&fci_spi_lock);
  257. #else
  258. u8 command = SPI_READ | SPI_THR;
  259. mutex_lock(&fci_spi_lock);
  260. res = spi_dataread(handle, (u8) (devid & 0x000f), addr, command,
  261. data, length);
  262. mutex_unlock(&fci_spi_lock);
  263. return res;
  264. #endif
  265. }
  266. s32 fc8300_spi_deinit(HANDLE handle)
  267. {
  268. return BBM_OK;
  269. }