fc8300_spi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <linux/kernel.h>
  23. #include "fci_types.h"
  24. #include "fc8300_regs.h"
  25. #include "fci_oal.h"
  26. //#include "isdbt_port_fc8300.h"
  27. #define SPI_LEN 0x00 /* or 0x10 */
  28. #define SPI_REG 0x20
  29. #define SPI_THR 0x30
  30. #define SPI_READ 0x40
  31. #define SPI_WRITE 0x00
  32. #define SPI_AINC 0x80
  33. //#define DRIVER_NAME "isdbt"
  34. struct spi_device *fc8300_spi;
  35. static u8 tx_data[10];
  36. static u8 wdata_buf[32] __cacheline_aligned;
  37. static u8 rdata_buf[65536] __cacheline_aligned;
  38. static DEFINE_MUTEX(fci_spi_lock);
  39. void fc8300_set_port_if(unsigned long interface)
  40. {
  41. fc8300_spi = (struct spi_device *)interface;
  42. print_log(0, "fc8300_spi : 0x%x\n", fc8300_spi);
  43. }
  44. static int fc8300_spi_write_then_read(struct spi_device *spi
  45. , u8 *txbuf, u16 tx_length, u8 *rxbuf, u16 rx_length)
  46. {
  47. int res = 0;
  48. struct spi_message message;
  49. struct spi_transfer x;
  50. if (spi == NULL) {
  51. print_log(0, "[ERROR] FC8300_SPI Handle Fail...........\n");
  52. return BBM_NOK;
  53. }
  54. spi_message_init(&message);
  55. memset(&x, 0, sizeof x);
  56. spi_message_add_tail(&x, &message);
  57. memcpy(&wdata_buf[0], txbuf, tx_length);
  58. x.tx_buf = &wdata_buf[0];
  59. x.rx_buf = &rdata_buf[0];
  60. x.len = tx_length + rx_length;
  61. x.cs_change = 0;
  62. x.bits_per_word = 8;
  63. res = spi_sync(spi, &message);
  64. memcpy(rxbuf, x.rx_buf + tx_length, rx_length);
  65. return res;
  66. }
  67. static s32 spi_bulkread(HANDLE handle, u8 devid,
  68. u16 addr, u8 command, u8 *data, u16 length)
  69. {
  70. int res;
  71. tx_data[0] = addr & 0xff;
  72. tx_data[1] = (addr >> 8) & 0xff;
  73. tx_data[2] = command | devid;
  74. tx_data[3] = length & 0xff;
  75. res = fc8300_spi_write_then_read(fc8300_spi
  76. , &tx_data[0], 4, data, length);
  77. if (res) {
  78. print_log(0, "fc8300_spi_bulkread fail : %d\n", res);
  79. return BBM_NOK;
  80. }
  81. return res;
  82. }
  83. static s32 spi_bulkwrite(HANDLE handle, u8 devid,
  84. u16 addr, u8 command, u8 *data, u16 length)
  85. {
  86. int i;
  87. int res;
  88. tx_data[0] = addr & 0xff;
  89. tx_data[1] = (addr >> 8) & 0xff;
  90. tx_data[2] = command | devid;
  91. tx_data[3] = length & 0xff;
  92. for (i = 0 ; i < length ; i++)
  93. tx_data[4+i] = data[i];
  94. res = fc8300_spi_write_then_read(fc8300_spi
  95. , &tx_data[0], length+4, NULL, 0);
  96. if (res) {
  97. print_log(0, "fc8300_spi_bulkwrite fail : %d\n", res);
  98. return BBM_NOK;
  99. }
  100. return res;
  101. }
  102. static s32 spi_dataread(HANDLE handle, u8 devid,
  103. u16 addr, u8 command, u8 *data, u32 length)
  104. {
  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. res = fc8300_spi_write_then_read(fc8300_spi
  111. , &tx_data[0], 4, data, length);
  112. if (res) {
  113. print_log(0, "fc8300_spi_dataread fail : %d\n", res);
  114. return BBM_NOK;
  115. }
  116. return res;
  117. }
  118. s32 fc8300_spi_init(HANDLE handle, u16 param1, u16 param2)
  119. {
  120. print_log(0, "fc8300_spi_init\n");
  121. if(fc8300_spi == NULL) {
  122. print_log(0, "fc8300_spi register fail\n");
  123. return BBM_NOK;
  124. }
  125. return 0;
  126. }
  127. s32 fc8300_spi_byteread(HANDLE handle, DEVICEID devid, u16 addr, u8 *data)
  128. {
  129. s32 res;
  130. u8 command = SPI_READ;
  131. mutex_lock(&fci_spi_lock);
  132. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  133. data, 1);
  134. mutex_unlock(&fci_spi_lock);
  135. return res;
  136. }
  137. s32 fc8300_spi_wordread(HANDLE handle, DEVICEID devid, u16 addr, u16 *data)
  138. {
  139. s32 res;
  140. u8 command = SPI_READ | SPI_AINC;
  141. mutex_lock(&fci_spi_lock);
  142. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  143. (u8 *)data, 2);
  144. mutex_unlock(&fci_spi_lock);
  145. return res;
  146. }
  147. s32 fc8300_spi_longread(HANDLE handle, DEVICEID devid, u16 addr, u32 *data)
  148. {
  149. s32 res;
  150. u8 command = SPI_READ | SPI_AINC;
  151. mutex_lock(&fci_spi_lock);
  152. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  153. (u8 *)data, 4);
  154. mutex_unlock(&fci_spi_lock);
  155. return res;
  156. }
  157. s32 fc8300_spi_bulkread(HANDLE handle, DEVICEID devid,
  158. u16 addr, u8 *data, u16 length)
  159. {
  160. s32 res;
  161. u8 command = SPI_READ | SPI_AINC;
  162. mutex_lock(&fci_spi_lock);
  163. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  164. data, length);
  165. mutex_unlock(&fci_spi_lock);
  166. return res;
  167. }
  168. s32 fc8300_spi_bytewrite(HANDLE handle, DEVICEID devid, u16 addr, u8 data)
  169. {
  170. s32 res;
  171. u8 command = SPI_WRITE;
  172. mutex_lock(&fci_spi_lock);
  173. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  174. (u8 *)&data, 1);
  175. mutex_unlock(&fci_spi_lock);
  176. return res;
  177. }
  178. s32 fc8300_spi_wordwrite(HANDLE handle, DEVICEID devid, u16 addr, u16 data)
  179. {
  180. s32 res;
  181. u8 command = SPI_WRITE | SPI_AINC;
  182. mutex_lock(&fci_spi_lock);
  183. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  184. (u8 *)&data, 2);
  185. mutex_unlock(&fci_spi_lock);
  186. return res;
  187. }
  188. s32 fc8300_spi_longwrite(HANDLE handle, DEVICEID devid, u16 addr, u32 data)
  189. {
  190. s32 res;
  191. u8 command = SPI_WRITE | SPI_AINC;
  192. mutex_lock(&fci_spi_lock);
  193. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  194. (u8 *) &data, 4);
  195. mutex_unlock(&fci_spi_lock);
  196. return res;
  197. }
  198. s32 fc8300_spi_bulkwrite(HANDLE handle, DEVICEID devid,
  199. u16 addr, u8 *data, u16 length)
  200. {
  201. s32 res;
  202. u8 command = SPI_WRITE | SPI_AINC;
  203. mutex_lock(&fci_spi_lock);
  204. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  205. data, length);
  206. mutex_unlock(&fci_spi_lock);
  207. return res;
  208. }
  209. s32 fc8300_spi_dataread(HANDLE handle, DEVICEID devid,
  210. u16 addr, u8 *data, u32 length)
  211. {
  212. s32 res;
  213. u8 command = SPI_READ | SPI_THR;
  214. mutex_lock(&fci_spi_lock);
  215. res = spi_dataread(handle, (u8) (devid & 0x000f), addr, command,
  216. data, length);
  217. mutex_unlock(&fci_spi_lock);
  218. return res;
  219. }
  220. s32 fc8300_spi_deinit(HANDLE handle)
  221. {
  222. return BBM_OK;
  223. }