fc8300_spi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <mach/sec_debug.h>
  23. #include "fci_types.h"
  24. #include "fc8300_regs.h"
  25. #include "fci_oal.h"
  26. #define SPI_LEN 0x00 /* or 0x10 */
  27. #define SPI_REG 0x20
  28. #define SPI_THR 0x30
  29. #define SPI_READ 0x40
  30. #define SPI_WRITE 0x00
  31. #define SPI_AINC 0x80
  32. #define DRIVER_NAME "fc8300_spi"
  33. struct spi_device *fc8300_spi;
  34. static u8 tx_data[10];
  35. static u8 wdata_buf[32] __cacheline_aligned;
  36. static u8 rdata_buf[65536] __cacheline_aligned;
  37. static DEFINE_MUTEX(fci_spi_lock);
  38. static int __devinit fc8300_spi_probe(struct spi_device *spi)
  39. {
  40. s32 ret;
  41. ISDB_PR_ERR("fc8300_spi_probe\n");
  42. //prevent issue CID 27494
  43. if(spi == NULL)
  44. {
  45. ISDB_PR_ERR("fc8300_spi_probe spi == NULL \n");
  46. return -1;
  47. }
  48. spi->max_speed_hz = 48000000; //52000000
  49. spi->bits_per_word = 8;
  50. spi->mode = SPI_MODE_0;
  51. ret = spi_setup(spi);
  52. if (ret < 0)
  53. {
  54. ISDB_PR_ERR("fc8300_spi_probe ret =%d \n",ret);
  55. return ret;
  56. }
  57. fc8300_spi = spi;
  58. //prevent issue CID 27516
  59. return ret;
  60. }
  61. static int fc8300_spi_remove(struct spi_device *spi)
  62. {
  63. ISDB_PR_ERR("fc8300_spi_remove\n");
  64. return 0;
  65. }
  66. static const struct of_device_id tmm_spi_match_table[] = {
  67. { .compatible = "isdbt_spi_comp",
  68. },
  69. {}
  70. };
  71. static struct spi_driver fc8300_spi_driver = {
  72. .driver = {
  73. .name = DRIVER_NAME,
  74. .owner = THIS_MODULE,
  75. .of_match_table = tmm_spi_match_table,
  76. },
  77. .probe = fc8300_spi_probe,
  78. .remove = __devexit_p(fc8300_spi_remove),
  79. };
  80. static int fc8300_spi_write_then_read(struct spi_device *spi
  81. , u8 *txbuf, u16 tx_length, u8 *rxbuf, u16 rx_length)
  82. {
  83. int res = 0;
  84. struct spi_message message;
  85. struct spi_transfer x;
  86. if (spi == NULL) {
  87. ISDB_PR_ERR("[ERROR] FC8300_SPI Handle Fail...........\n");
  88. return BBM_NOK;
  89. }
  90. spi_message_init(&message);
  91. memset(&x, 0, sizeof x);
  92. spi_message_add_tail(&x, &message);
  93. memcpy(&wdata_buf[0], txbuf, tx_length);
  94. x.tx_buf = &wdata_buf[0];
  95. x.rx_buf = &rdata_buf[0];
  96. x.len = tx_length + rx_length;
  97. x.cs_change = 0;
  98. x.bits_per_word = 8;
  99. res = spi_sync(spi, &message);
  100. memcpy(rxbuf, x.rx_buf + tx_length, rx_length);
  101. return res;
  102. }
  103. static s32 spi_bulkread(HANDLE handle, u8 devid,
  104. u16 addr, u8 command, u8 *data, u16 length)
  105. {
  106. int res;
  107. tx_data[0] = addr & 0xff;
  108. tx_data[1] = (addr >> 8) & 0xff;
  109. tx_data[2] = command | devid;
  110. tx_data[3] = length & 0xff;
  111. res = fc8300_spi_write_then_read(fc8300_spi
  112. , &tx_data[0], 4, data, length);
  113. if (res) {
  114. ISDB_PR_ERR("fc8300_spi_bulkread fail : %d\n", res);
  115. return BBM_NOK;
  116. }
  117. return res;
  118. }
  119. static s32 spi_bulkwrite(HANDLE handle, u8 devid,
  120. u16 addr, u8 command, u8 *data, u16 length)
  121. {
  122. int i;
  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. for (i = 0 ; i < length ; i++)
  129. {
  130. if( (4+i) < 10) //Fix PLM P140206-02359
  131. {
  132. tx_data[4+i] = data[i];
  133. }
  134. else
  135. ISDB_PR_ERR("Error spi_bulkwrite tx_data length= %d\n",length);
  136. }
  137. res = fc8300_spi_write_then_read(fc8300_spi
  138. , &tx_data[0], length+4, NULL, 0);
  139. if (res) {
  140. ISDB_PR_ERR("fc8300_spi_bulkwrite fail : %d\n", res);
  141. return BBM_NOK;
  142. }
  143. return res;
  144. }
  145. static s32 spi_dataread(HANDLE handle, u8 devid,
  146. u16 addr, u8 command, u8 *data, u32 length)
  147. {
  148. int res;
  149. tx_data[0] = addr & 0xff;
  150. tx_data[1] = (addr >> 8) & 0xff;
  151. tx_data[2] = command | devid;
  152. tx_data[3] = length & 0xff;
  153. res = fc8300_spi_write_then_read(fc8300_spi
  154. , &tx_data[0], 4, data, length);
  155. if (res) {
  156. ISDB_PR_ERR("fc8300 spi_dataread fail : %d\n", res);
  157. return BBM_NOK;
  158. }
  159. return res;
  160. }
  161. s32 fc8300_spi_init(HANDLE handle, u16 param1, u16 param2)
  162. {
  163. int res = 0;
  164. ISDB_PR_ERR("fc8300_spi_init : %d\n", res);
  165. res = spi_register_driver(&fc8300_spi_driver);
  166. if (res) {
  167. ISDB_PR_ERR("fc8300_spi register fail : %d\n", res);
  168. return BBM_NOK;
  169. }
  170. return res;
  171. }
  172. s32 fc8300_spi_byteread(HANDLE handle, DEVICEID devid, u16 addr, u8 *data)
  173. {
  174. s32 res;
  175. u8 command = SPI_READ;
  176. mutex_lock(&fci_spi_lock);
  177. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  178. data, 1);
  179. mutex_unlock(&fci_spi_lock);
  180. return res;
  181. }
  182. s32 fc8300_spi_wordread(HANDLE handle, DEVICEID devid, u16 addr, u16 *data)
  183. {
  184. s32 res;
  185. u8 command = SPI_READ | SPI_AINC;
  186. mutex_lock(&fci_spi_lock);
  187. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  188. (u8 *)data, 2);
  189. mutex_unlock(&fci_spi_lock);
  190. return res;
  191. }
  192. s32 fc8300_spi_longread(HANDLE handle, DEVICEID devid, u16 addr, u32 *data)
  193. {
  194. s32 res;
  195. u8 command = SPI_READ | SPI_AINC;
  196. mutex_lock(&fci_spi_lock);
  197. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  198. (u8 *)data, 4);
  199. mutex_unlock(&fci_spi_lock);
  200. return res;
  201. }
  202. s32 fc8300_spi_bulkread(HANDLE handle, DEVICEID devid,
  203. u16 addr, u8 *data, u16 length)
  204. {
  205. s32 res;
  206. u8 command = SPI_READ | SPI_AINC;
  207. mutex_lock(&fci_spi_lock);
  208. res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
  209. data, length);
  210. mutex_unlock(&fci_spi_lock);
  211. return res;
  212. }
  213. s32 fc8300_spi_bytewrite(HANDLE handle, DEVICEID devid, u16 addr, u8 data)
  214. {
  215. s32 res;
  216. u8 command = SPI_WRITE;
  217. mutex_lock(&fci_spi_lock);
  218. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  219. (u8 *)&data, 1);
  220. mutex_unlock(&fci_spi_lock);
  221. return res;
  222. }
  223. s32 fc8300_spi_wordwrite(HANDLE handle, DEVICEID devid, u16 addr, u16 data)
  224. {
  225. s32 res;
  226. #ifdef BBM_ES
  227. u8 command = SPI_WRITE;
  228. if ((addr & 0xff00) != 0x0f00)
  229. command |= SPI_AINC;
  230. #else
  231. u8 command = SPI_WRITE | SPI_AINC;
  232. #endif
  233. mutex_lock(&fci_spi_lock);
  234. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  235. (u8 *)&data, 2);
  236. mutex_unlock(&fci_spi_lock);
  237. return res;
  238. }
  239. s32 fc8300_spi_longwrite(HANDLE handle, DEVICEID devid, u16 addr, u32 data)
  240. {
  241. s32 res;
  242. u8 command = SPI_WRITE | SPI_AINC;
  243. mutex_lock(&fci_spi_lock);
  244. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  245. (u8 *) &data, 4);
  246. mutex_unlock(&fci_spi_lock);
  247. return res;
  248. }
  249. s32 fc8300_spi_bulkwrite(HANDLE handle, DEVICEID devid,
  250. u16 addr, u8 *data, u16 length)
  251. {
  252. s32 res;
  253. u8 command = SPI_WRITE | SPI_AINC;
  254. mutex_lock(&fci_spi_lock);
  255. res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
  256. data, length);
  257. mutex_unlock(&fci_spi_lock);
  258. return res;
  259. }
  260. s32 fc8300_spi_dataread(HANDLE handle, DEVICEID devid,
  261. u16 addr, u8 *data, u32 length)
  262. {
  263. s32 res = 0;
  264. #ifdef SPI_DATAREAD_REGMODE
  265. u8 command = SPI_READ | SPI_REG;
  266. u32 read_len = 0;
  267. u32 i, m, r;
  268. mutex_lock(&fci_spi_lock);
  269. if (length > SPI_DMA_MAX_SIZE) {
  270. m = length / SPI_DMA_MAX_SIZE;
  271. r = length % SPI_DMA_MAX_SIZE;
  272. for (i = 0; i < m ; i++) {
  273. res |= spi_dataread(handle, (u8) (devid & 0x000f), addr
  274. , command, &data[read_len], SPI_DMA_MAX_SIZE);
  275. read_len += SPI_DMA_MAX_SIZE;
  276. }
  277. } else {
  278. res = spi_dataread(handle, (u8) (devid & 0x000f), addr, command,
  279. data, length);
  280. }
  281. mutex_unlock(&fci_spi_lock);
  282. #else
  283. u8 command = SPI_READ | SPI_THR;
  284. mutex_lock(&fci_spi_lock);
  285. res = spi_dataread(handle, (u8) (devid & 0x000f), addr, command,
  286. data, length);
  287. mutex_unlock(&fci_spi_lock);
  288. ISDB_PR_DBG("fc8300_spi_dataread res = %d, length : %d \n", res, length);
  289. #endif
  290. return res;
  291. }
  292. s32 fc8300_spi_deinit(HANDLE handle)
  293. {
  294. ISDB_PR_ERR("fc8300_spi_deinit\n");
  295. return BBM_OK;
  296. }