Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

1358 wiersze
30 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains NMC1000 SPI protocol bus APIs implementation.
  6. *
  7. * Copyright (c) 2015 - 2017 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. #include "common/include/nm_common.h"
  42. #ifdef CONF_WINC_USE_SPI
  43. #define USE_OLD_SPI_SW
  44. #include "bus_wrapper/include/nm_bus_wrapper.h"
  45. #include "nmspi.h"
  46. #define NMI_PERIPH_REG_BASE 0x1000
  47. #define NMI_INTR_REG_BASE (NMI_PERIPH_REG_BASE + 0xa00)
  48. #define NMI_CHIPID (NMI_PERIPH_REG_BASE)
  49. #define NMI_PIN_MUX_0 (NMI_PERIPH_REG_BASE + 0x408)
  50. #define NMI_INTR_ENABLE (NMI_INTR_REG_BASE)
  51. #define NMI_SPI_REG_BASE 0xe800
  52. #define NMI_SPI_CTL (NMI_SPI_REG_BASE)
  53. #define NMI_SPI_MASTER_DMA_ADDR (NMI_SPI_REG_BASE + 0x4)
  54. #define NMI_SPI_MASTER_DMA_COUNT (NMI_SPI_REG_BASE + 0x8)
  55. #define NMI_SPI_SLAVE_DMA_ADDR (NMI_SPI_REG_BASE + 0xc)
  56. #define NMI_SPI_SLAVE_DMA_COUNT (NMI_SPI_REG_BASE + 0x10)
  57. #define NMI_SPI_TX_MODE (NMI_SPI_REG_BASE + 0x20)
  58. #define NMI_SPI_PROTOCOL_CONFIG (NMI_SPI_REG_BASE + 0x24)
  59. #define NMI_SPI_INTR_CTL (NMI_SPI_REG_BASE + 0x2c)
  60. #define NMI_SPI_MISC_CTRL (NMI_SPI_REG_BASE + 0x48)
  61. #define NMI_SPI_PROTOCOL_OFFSET (NMI_SPI_PROTOCOL_CONFIG - NMI_SPI_REG_BASE)
  62. #define SPI_BASE NMI_SPI_REG_BASE
  63. #define CMD_DMA_WRITE 0xc1
  64. #define CMD_DMA_READ 0xc2
  65. #define CMD_INTERNAL_WRITE 0xc3
  66. #define CMD_INTERNAL_READ 0xc4
  67. #define CMD_TERMINATE 0xc5
  68. #define CMD_REPEAT 0xc6
  69. #define CMD_DMA_EXT_WRITE 0xc7
  70. #define CMD_DMA_EXT_READ 0xc8
  71. #define CMD_SINGLE_WRITE 0xc9
  72. #define CMD_SINGLE_READ 0xca
  73. #define CMD_RESET 0xcf
  74. #define N_OK 1
  75. #define N_FAIL 0
  76. #define N_RESET -1
  77. #define N_RETRY -2
  78. #define SPI_RESP_RETRY_COUNT (10)
  79. #define SPI_RETRY_COUNT (10)
  80. #define DATA_PKT_SZ_256 256
  81. #define DATA_PKT_SZ_512 512
  82. #define DATA_PKT_SZ_1K 1024
  83. #define DATA_PKT_SZ_4K (4 * 1024)
  84. #define DATA_PKT_SZ_8K (8 * 1024)
  85. #define DATA_PKT_SZ DATA_PKT_SZ_8K
  86. static uint8 gu8Crc_off = 0;
  87. static sint8 nmi_spi_read(uint8 *b, uint16 sz)
  88. {
  89. tstrNmSpiRw spi;
  90. spi.pu8InBuf = NULL;
  91. spi.pu8OutBuf = b;
  92. spi.u16Sz = sz;
  93. return nm_bus_ioctl(NM_BUS_IOCTL_RW, &spi);
  94. }
  95. static sint8 nmi_spi_write(uint8 *b, uint16 sz)
  96. {
  97. tstrNmSpiRw spi;
  98. spi.pu8InBuf = b;
  99. spi.pu8OutBuf = NULL;
  100. spi.u16Sz = sz;
  101. return nm_bus_ioctl(NM_BUS_IOCTL_RW, &spi);
  102. }
  103. #ifndef USE_OLD_SPI_SW
  104. static sint8 nmi_spi_rw(uint8 *bin, uint8 *bout, uint16 sz)
  105. {
  106. tstrNmSpiRw spi;
  107. spi.pu8InBuf = bin;
  108. spi.pu8OutBuf = bout;
  109. spi.u16Sz = sz;
  110. return nm_bus_ioctl(NM_BUS_IOCTL_RW, &spi);
  111. }
  112. #endif
  113. /********************************************
  114. Crc7
  115. ********************************************/
  116. static const uint8 crc7_syndrome_table[256]
  117. = {0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, 0x19, 0x10, 0x0b,
  118. 0x02, 0x3d, 0x34, 0x2f, 0x26, 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e, 0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f,
  119. 0x04, 0x0d, 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45, 0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14, 0x63,
  120. 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c, 0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b, 0x2c, 0x25, 0x3e, 0x37,
  121. 0x08, 0x01, 0x1a, 0x13, 0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42, 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03,
  122. 0x0a, 0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69, 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21, 0x4f, 0x46,
  123. 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70, 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38, 0x41, 0x48, 0x53, 0x5a, 0x65,
  124. 0x6c, 0x77, 0x7e, 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36, 0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67,
  125. 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, 0x3b, 0x32, 0x29,
  126. 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55, 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f,
  127. 0x14, 0x1d, 0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a, 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52, 0x3c,
  128. 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03, 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b, 0x17, 0x1e, 0x05, 0x0c,
  129. 0x33, 0x3a, 0x21, 0x28, 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60, 0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38,
  130. 0x31, 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79};
  131. static uint8 crc7_byte(uint8 crc, uint8 data)
  132. {
  133. return crc7_syndrome_table[(crc << 1) ^ data];
  134. }
  135. static uint8 crc7(uint8 crc, const uint8 *buffer, uint32 len)
  136. {
  137. while (len--)
  138. crc = crc7_byte(crc, *buffer++);
  139. return crc;
  140. }
  141. /********************************************
  142. Spi protocol Function
  143. ********************************************/
  144. #define CMD_DMA_WRITE 0xc1
  145. #define CMD_DMA_READ 0xc2
  146. #define CMD_INTERNAL_WRITE 0xc3
  147. #define CMD_INTERNAL_READ 0xc4
  148. #define CMD_TERMINATE 0xc5
  149. #define CMD_REPEAT 0xc6
  150. #define CMD_DMA_EXT_WRITE 0xc7
  151. #define CMD_DMA_EXT_READ 0xc8
  152. #define CMD_SINGLE_WRITE 0xc9
  153. #define CMD_SINGLE_READ 0xca
  154. #define CMD_RESET 0xcf
  155. #define DATA_PKT_SZ_256 256
  156. #define DATA_PKT_SZ_512 512
  157. #define DATA_PKT_SZ_1K 1024
  158. #define DATA_PKT_SZ_4K (4 * 1024)
  159. #define DATA_PKT_SZ_8K (8 * 1024)
  160. #define DATA_PKT_SZ DATA_PKT_SZ_8K
  161. static sint8 spi_cmd(uint8 cmd, uint32 adr, uint32 u32data, uint32 sz, uint8 clockless)
  162. {
  163. uint8 bc[9];
  164. uint8 len = 5;
  165. sint8 result = N_OK;
  166. bc[0] = cmd;
  167. switch (cmd) {
  168. case CMD_SINGLE_READ: /* single word (4 bytes) read */
  169. bc[1] = (uint8)(adr >> 16);
  170. bc[2] = (uint8)(adr >> 8);
  171. bc[3] = (uint8)adr;
  172. len = 5;
  173. break;
  174. case CMD_INTERNAL_READ: /* internal register read */
  175. bc[1] = (uint8)(adr >> 8);
  176. if (clockless)
  177. bc[1] |= (1 << 7);
  178. bc[2] = (uint8)adr;
  179. bc[3] = 0x00;
  180. len = 5;
  181. break;
  182. case CMD_TERMINATE: /* termination */
  183. bc[1] = 0x00;
  184. bc[2] = 0x00;
  185. bc[3] = 0x00;
  186. len = 5;
  187. break;
  188. case CMD_REPEAT: /* repeat */
  189. bc[1] = 0x00;
  190. bc[2] = 0x00;
  191. bc[3] = 0x00;
  192. len = 5;
  193. break;
  194. case CMD_RESET: /* reset */
  195. bc[1] = 0xff;
  196. bc[2] = 0xff;
  197. bc[3] = 0xff;
  198. len = 5;
  199. break;
  200. case CMD_DMA_WRITE: /* dma write */
  201. case CMD_DMA_READ: /* dma read */
  202. bc[1] = (uint8)(adr >> 16);
  203. bc[2] = (uint8)(adr >> 8);
  204. bc[3] = (uint8)adr;
  205. bc[4] = (uint8)(sz >> 8);
  206. bc[5] = (uint8)(sz);
  207. len = 7;
  208. break;
  209. case CMD_DMA_EXT_WRITE: /* dma extended write */
  210. case CMD_DMA_EXT_READ: /* dma extended read */
  211. bc[1] = (uint8)(adr >> 16);
  212. bc[2] = (uint8)(adr >> 8);
  213. bc[3] = (uint8)adr;
  214. bc[4] = (uint8)(sz >> 16);
  215. bc[5] = (uint8)(sz >> 8);
  216. bc[6] = (uint8)(sz);
  217. len = 8;
  218. break;
  219. case CMD_INTERNAL_WRITE: /* internal register write */
  220. bc[1] = (uint8)(adr >> 8);
  221. if (clockless)
  222. bc[1] |= (1 << 7);
  223. bc[2] = (uint8)(adr);
  224. bc[3] = (uint8)(u32data >> 24);
  225. bc[4] = (uint8)(u32data >> 16);
  226. bc[5] = (uint8)(u32data >> 8);
  227. bc[6] = (uint8)(u32data);
  228. len = 8;
  229. break;
  230. case CMD_SINGLE_WRITE: /* single word write */
  231. bc[1] = (uint8)(adr >> 16);
  232. bc[2] = (uint8)(adr >> 8);
  233. bc[3] = (uint8)(adr);
  234. bc[4] = (uint8)(u32data >> 24);
  235. bc[5] = (uint8)(u32data >> 16);
  236. bc[6] = (uint8)(u32data >> 8);
  237. bc[7] = (uint8)(u32data);
  238. len = 9;
  239. break;
  240. default:
  241. result = N_FAIL;
  242. break;
  243. }
  244. if (result) {
  245. if (!gu8Crc_off)
  246. bc[len - 1] = (crc7(0x7f, (const uint8 *)&bc[0], len - 1)) << 1;
  247. else
  248. len -= 1;
  249. if (M2M_SUCCESS != nmi_spi_write(bc, len)) {
  250. M2M_ERR("[nmi spi]: Failed cmd write, bus error...\n");
  251. result = N_FAIL;
  252. }
  253. }
  254. return result;
  255. }
  256. static sint8 spi_data_rsp(uint8 cmd)
  257. {
  258. uint8 len;
  259. uint8 rsp[3];
  260. sint8 result = N_OK;
  261. if (!gu8Crc_off)
  262. len = 2;
  263. else
  264. len = 3;
  265. if (M2M_SUCCESS != nmi_spi_read(&rsp[0], len)) {
  266. M2M_ERR("[nmi spi]: Failed bus error...\n");
  267. result = N_FAIL;
  268. goto _fail_;
  269. }
  270. if ((rsp[len - 1] != 0) || (rsp[len - 2] != 0xC3)) {
  271. M2M_ERR("[nmi spi]: Failed data response read, %x %x %x\n", rsp[0], rsp[1], rsp[2]);
  272. result = N_FAIL;
  273. goto _fail_;
  274. }
  275. _fail_:
  276. return result;
  277. }
  278. static sint8 spi_cmd_rsp(uint8 cmd)
  279. {
  280. uint8 rsp;
  281. sint8 result = N_OK;
  282. sint8 s8RetryCnt;
  283. /**
  284. Command/Control response
  285. **/
  286. if ((cmd == CMD_RESET) || (cmd == CMD_TERMINATE) || (cmd == CMD_REPEAT)) {
  287. if (M2M_SUCCESS != nmi_spi_read(&rsp, 1)) {
  288. result = N_FAIL;
  289. goto _fail_;
  290. }
  291. }
  292. /* wait for response */
  293. s8RetryCnt = SPI_RESP_RETRY_COUNT;
  294. do {
  295. if (M2M_SUCCESS != nmi_spi_read(&rsp, 1)) {
  296. M2M_ERR("[nmi spi]: Failed cmd response read, bus error...\n");
  297. result = N_FAIL;
  298. goto _fail_;
  299. }
  300. } while ((rsp != cmd) && (s8RetryCnt-- > 0));
  301. /**
  302. State response
  303. **/
  304. /* wait for response */
  305. s8RetryCnt = SPI_RESP_RETRY_COUNT;
  306. do {
  307. if (M2M_SUCCESS != nmi_spi_read(&rsp, 1)) {
  308. M2M_ERR("[nmi spi]: Failed cmd response read, bus error...\n");
  309. result = N_FAIL;
  310. goto _fail_;
  311. }
  312. } while ((rsp != 0x00) && (s8RetryCnt-- > 0));
  313. _fail_:
  314. return result;
  315. }
  316. #ifndef USE_OLD_SPI_SW
  317. static int spi_cmd_complete(uint8_t cmd, uint32_t adr, uint8_t *b, uint32_t sz, uint8_t clockless)
  318. {
  319. uint8_t wb[32], rb[32];
  320. uint8_t wix, rix;
  321. uint32_t len2;
  322. uint8_t rsp;
  323. int len = 0;
  324. int result = N_OK;
  325. wb[0] = cmd;
  326. switch (cmd) {
  327. case CMD_SINGLE_READ: /* single word (4 bytes) read */
  328. wb[1] = (uint8_t)(adr >> 16);
  329. wb[2] = (uint8_t)(adr >> 8);
  330. wb[3] = (uint8_t)adr;
  331. len = 5;
  332. break;
  333. case CMD_INTERNAL_READ: /* internal register read */
  334. wb[1] = (uint8_t)(adr >> 8);
  335. if (clockless == 1)
  336. wb[1] |= (1 << 7);
  337. wb[2] = (uint8_t)adr;
  338. wb[3] = 0x00;
  339. len = 5;
  340. break;
  341. case CMD_TERMINATE: /* termination */
  342. wb[1] = 0x00;
  343. wb[2] = 0x00;
  344. wb[3] = 0x00;
  345. len = 5;
  346. break;
  347. case CMD_REPEAT: /* repeat */
  348. wb[1] = 0x00;
  349. wb[2] = 0x00;
  350. wb[3] = 0x00;
  351. len = 5;
  352. break;
  353. case CMD_RESET: /* reset */
  354. wb[1] = 0xff;
  355. wb[2] = 0xff;
  356. wb[3] = 0xff;
  357. len = 5;
  358. break;
  359. case CMD_DMA_WRITE: /* dma write */
  360. case CMD_DMA_READ: /* dma read */
  361. wb[1] = (uint8_t)(adr >> 16);
  362. wb[2] = (uint8_t)(adr >> 8);
  363. wb[3] = (uint8_t)adr;
  364. wb[4] = (uint8_t)(sz >> 8);
  365. wb[5] = (uint8_t)(sz);
  366. len = 7;
  367. break;
  368. case CMD_DMA_EXT_WRITE: /* dma extended write */
  369. case CMD_DMA_EXT_READ: /* dma extended read */
  370. wb[1] = (uint8_t)(adr >> 16);
  371. wb[2] = (uint8_t)(adr >> 8);
  372. wb[3] = (uint8_t)adr;
  373. wb[4] = (uint8_t)(sz >> 16);
  374. wb[5] = (uint8_t)(sz >> 8);
  375. wb[6] = (uint8_t)(sz);
  376. len = 8;
  377. break;
  378. case CMD_INTERNAL_WRITE: /* internal register write */
  379. wb[1] = (uint8_t)(adr >> 8);
  380. if (clockless == 1)
  381. wb[1] |= (1 << 7);
  382. wb[2] = (uint8_t)(adr);
  383. wb[3] = b[3];
  384. wb[4] = b[2];
  385. wb[5] = b[1];
  386. wb[6] = b[0];
  387. len = 8;
  388. break;
  389. case CMD_SINGLE_WRITE: /* single word write */
  390. wb[1] = (uint8_t)(adr >> 16);
  391. wb[2] = (uint8_t)(adr >> 8);
  392. wb[3] = (uint8_t)(adr);
  393. wb[4] = b[3];
  394. wb[5] = b[2];
  395. wb[6] = b[1];
  396. wb[7] = b[0];
  397. len = 9;
  398. break;
  399. default:
  400. result = N_FAIL;
  401. break;
  402. }
  403. if (result != N_OK) {
  404. return result;
  405. }
  406. if (!gu8Crc_off) {
  407. wb[len - 1] = (crc7(0x7f, (const uint8_t *)&wb[0], len - 1)) << 1;
  408. } else {
  409. len -= 1;
  410. }
  411. #define NUM_SKIP_BYTES (1)
  412. #define NUM_RSP_BYTES (2)
  413. #define NUM_DATA_HDR_BYTES (1)
  414. #define NUM_DATA_BYTES (4)
  415. #define NUM_CRC_BYTES (2)
  416. #define NUM_DUMMY_BYTES (3)
  417. if ((cmd == CMD_RESET) || (cmd == CMD_TERMINATE) || (cmd == CMD_REPEAT)) {
  418. len2 = len + (NUM_SKIP_BYTES + NUM_RSP_BYTES + NUM_DUMMY_BYTES);
  419. } else if ((cmd == CMD_INTERNAL_READ) || (cmd == CMD_SINGLE_READ)) {
  420. if (!gu8Crc_off) {
  421. len2 = len + (NUM_RSP_BYTES + NUM_DATA_HDR_BYTES + NUM_DATA_BYTES + NUM_CRC_BYTES + NUM_DUMMY_BYTES);
  422. } else {
  423. len2 = len + (NUM_RSP_BYTES + NUM_DATA_HDR_BYTES + NUM_DATA_BYTES + NUM_DUMMY_BYTES);
  424. }
  425. } else {
  426. len2 = len + (NUM_RSP_BYTES + NUM_DUMMY_BYTES);
  427. }
  428. #undef NUM_DUMMY_BYTES
  429. if (len2 > (sizeof(wb) / sizeof(wb[0]))) {
  430. M2M_ERR("[nmi spi]: spi buffer size too small (%d) (%d)\n", len2, (sizeof(wb) / sizeof(wb[0])));
  431. result = N_FAIL;
  432. return result;
  433. }
  434. /* zero spi write buffers. */
  435. for (wix = len; wix < len2; wix++) {
  436. wb[wix] = 0;
  437. }
  438. rix = len;
  439. if (nmi_spi_rw(wb, rb, len2) != M2M_SUCCESS) {
  440. M2M_ERR("[nmi spi]: Failed cmd write, bus error...\n");
  441. result = N_FAIL;
  442. return result;
  443. }
  444. #if 0
  445. {
  446. int jj;
  447. printk("--- cnd = %x, len=%d, len2=%d\n", cmd, len, len2);
  448. for(jj=0; jj<sizeof(wb)/sizeof(wb[0]); jj++) {
  449. if(jj >= len2) break;
  450. if(((jj+1)%16) != 0) {
  451. if((jj%16) == 0) {
  452. printk("wb[%02x]: %02x ", jj, wb[jj]);
  453. } else {
  454. printk("%02x ", wb[jj]);
  455. }
  456. } else {
  457. printk("%02x\n", wb[jj]);
  458. }
  459. }
  460. printk("\n");
  461. for(jj=0; jj<sizeof(rb)/sizeof(rb[0]); jj++) {
  462. if(jj >= len2) break;
  463. if(((jj+1)%16) != 0) {
  464. if((jj%16) == 0) {
  465. printk("rb[%02x]: %02x ", jj, rb[jj]);
  466. } else {
  467. printk("%02x ", rb[jj]);
  468. }
  469. } else {
  470. printk("%02x\n", rb[jj]);
  471. }
  472. }
  473. printk("\n");
  474. }
  475. #endif
  476. /**
  477. Command/Control response
  478. **/
  479. if ((cmd == CMD_RESET) || (cmd == CMD_TERMINATE) || (cmd == CMD_REPEAT)) {
  480. rix++; /* skip 1 byte */
  481. }
  482. rsp = rb[rix++];
  483. if (rsp != cmd) {
  484. M2M_ERR("[nmi spi]: Failed cmd response, cmd (%02x), resp (%02x)\n", cmd, rsp);
  485. result = N_FAIL;
  486. return result;
  487. }
  488. /**
  489. State response
  490. **/
  491. rsp = rb[rix++];
  492. if (rsp != 0x00) {
  493. M2M_ERR("[nmi spi]: Failed cmd state response state (%02x)\n", rsp);
  494. result = N_FAIL;
  495. return result;
  496. }
  497. if ((cmd == CMD_INTERNAL_READ) || (cmd == CMD_SINGLE_READ) || (cmd == CMD_DMA_READ) || (cmd == CMD_DMA_EXT_READ)) {
  498. int retry;
  499. // uint16_t crc1, crc2;
  500. uint8_t crc[2];
  501. /**
  502. Data Respnose header
  503. **/
  504. retry = SPI_RESP_RETRY_COUNT;
  505. do {
  506. /* ensure there is room in buffer later to read data and crc */
  507. if (rix < len2) {
  508. rsp = rb[rix++];
  509. } else {
  510. retry = 0;
  511. break;
  512. }
  513. if (((rsp >> 4) & 0xf) == 0xf)
  514. break;
  515. } while (retry--);
  516. if (retry <= 0) {
  517. M2M_ERR("[nmi spi]: Error, data read response (%02x)\n", rsp);
  518. result = N_RESET;
  519. return result;
  520. }
  521. if ((cmd == CMD_INTERNAL_READ) || (cmd == CMD_SINGLE_READ)) {
  522. /**
  523. Read bytes
  524. **/
  525. if ((rix + 3) < len2) {
  526. b[0] = rb[rix++];
  527. b[1] = rb[rix++];
  528. b[2] = rb[rix++];
  529. b[3] = rb[rix++];
  530. } else {
  531. M2M_ERR("[nmi spi]: buffer overrun when reading data.\n");
  532. result = N_FAIL;
  533. return result;
  534. }
  535. if (!gu8Crc_off) {
  536. /**
  537. Read Crc
  538. **/
  539. if ((rix + 1) < len2) {
  540. crc[0] = rb[rix++];
  541. crc[1] = rb[rix++];
  542. } else {
  543. M2M_ERR("[nmi spi]: buffer overrun when reading crc.\n");
  544. result = N_FAIL;
  545. return result;
  546. }
  547. }
  548. } else if ((cmd == CMD_DMA_READ) || (cmd == CMD_DMA_EXT_READ)) {
  549. int ix;
  550. /* some data may be read in response to dummy bytes. */
  551. for (ix = 0; (rix < len2) && (ix < sz);) {
  552. b[ix++] = rb[rix++];
  553. }
  554. #if 0
  555. if(ix) M2M_INFO("ttt %d %d\n", sz, ix);
  556. #endif
  557. sz -= ix;
  558. if (sz > 0) {
  559. int nbytes;
  560. if (sz <= (DATA_PKT_SZ - ix)) {
  561. nbytes = sz;
  562. } else {
  563. nbytes = DATA_PKT_SZ - ix;
  564. }
  565. /**
  566. Read bytes
  567. **/
  568. if (nmi_spi_read(&b[ix], nbytes) != M2M_SUCCESS) {
  569. M2M_ERR("[nmi spi]: Failed data block read, bus error...\n");
  570. result = N_FAIL;
  571. goto _error_;
  572. }
  573. /**
  574. Read Crc
  575. **/
  576. if (!gu8Crc_off) {
  577. if (nmi_spi_read(crc, 2) != M2M_SUCCESS) {
  578. M2M_ERR("[nmi spi]: Failed data block crc read, bus error...\n");
  579. result = N_FAIL;
  580. goto _error_;
  581. }
  582. }
  583. ix += nbytes;
  584. sz -= nbytes;
  585. }
  586. /* if any data in left unread, then read the rest using normal DMA code.*/
  587. while (sz > 0) {
  588. int nbytes;
  589. if (sz <= DATA_PKT_SZ) {
  590. nbytes = sz;
  591. } else {
  592. nbytes = DATA_PKT_SZ;
  593. }
  594. /**
  595. read data response only on the next DMA cycles not
  596. the first DMA since data response header is already
  597. handled above for the first DMA.
  598. **/
  599. /**
  600. Data Respnose header
  601. **/
  602. retry = SPI_RESP_RETRY_COUNT;
  603. do {
  604. if (nmi_spi_read(&rsp, 1) != M2M_SUCCESS) {
  605. M2M_ERR("[nmi spi]: Failed data response read, bus error...\n");
  606. result = N_FAIL;
  607. break;
  608. }
  609. if (((rsp >> 4) & 0xf) == 0xf)
  610. break;
  611. } while (retry--);
  612. if (result == N_FAIL)
  613. break;
  614. /**
  615. Read bytes
  616. **/
  617. if (nmi_spi_read(&b[ix], nbytes) != M2M_SUCCESS) {
  618. M2M_ERR("[nmi spi]: Failed data block read, bus error...\n");
  619. result = N_FAIL;
  620. break;
  621. }
  622. /**
  623. Read Crc
  624. **/
  625. if (!gu8Crc_off) {
  626. if (nmi_spi_read(crc, 2) != M2M_SUCCESS) {
  627. M2M_ERR("[nmi spi]: Failed data block crc read, bus error...\n");
  628. result = N_FAIL;
  629. break;
  630. }
  631. }
  632. ix += nbytes;
  633. sz -= nbytes;
  634. }
  635. }
  636. }
  637. _error_:
  638. return result;
  639. }
  640. #endif
  641. static sint8 spi_data_read(uint8 *b, uint16 sz, uint8 clockless)
  642. {
  643. sint16 retry, ix, nbytes;
  644. sint8 result = N_OK;
  645. uint8 crc[2];
  646. uint8 rsp;
  647. /**
  648. Data
  649. **/
  650. ix = 0;
  651. do {
  652. if (sz <= DATA_PKT_SZ)
  653. nbytes = sz;
  654. else
  655. nbytes = DATA_PKT_SZ;
  656. /**
  657. Data Respnose header
  658. **/
  659. retry = SPI_RESP_RETRY_COUNT;
  660. do {
  661. if (M2M_SUCCESS != nmi_spi_read(&rsp, 1)) {
  662. M2M_ERR("[nmi spi]: Failed data response read, bus error...\n");
  663. result = N_FAIL;
  664. break;
  665. }
  666. if (((rsp >> 4) & 0xf) == 0xf)
  667. break;
  668. } while (retry--);
  669. if (result == N_FAIL)
  670. break;
  671. if (retry <= 0) {
  672. M2M_ERR("[nmi spi]: Failed data response read...(%02x)\n", rsp);
  673. result = N_FAIL;
  674. break;
  675. }
  676. /**
  677. Read bytes
  678. **/
  679. if (M2M_SUCCESS != nmi_spi_read(&b[ix], nbytes)) {
  680. M2M_ERR("[nmi spi]: Failed data block read, bus error...\n");
  681. result = N_FAIL;
  682. break;
  683. }
  684. if (!clockless) {
  685. /**
  686. Read Crc
  687. **/
  688. if (!gu8Crc_off) {
  689. if (M2M_SUCCESS != nmi_spi_read(crc, 2)) {
  690. M2M_ERR("[nmi spi]: Failed data block crc read, bus error...\n");
  691. result = N_FAIL;
  692. break;
  693. }
  694. }
  695. }
  696. ix += nbytes;
  697. sz -= nbytes;
  698. } while (sz);
  699. return result;
  700. }
  701. static sint8 spi_data_write(uint8 *b, uint16 sz)
  702. {
  703. sint16 ix;
  704. uint16 nbytes;
  705. sint8 result = 1;
  706. uint8 cmd, order, crc[2] = {0};
  707. // uint8 rsp;
  708. /**
  709. Data
  710. **/
  711. ix = 0;
  712. do {
  713. if (sz <= DATA_PKT_SZ)
  714. nbytes = sz;
  715. else
  716. nbytes = DATA_PKT_SZ;
  717. /**
  718. Write command
  719. **/
  720. cmd = 0xf0;
  721. if (ix == 0) {
  722. if (sz <= DATA_PKT_SZ)
  723. order = 0x3;
  724. else
  725. order = 0x1;
  726. } else {
  727. if (sz <= DATA_PKT_SZ)
  728. order = 0x3;
  729. else
  730. order = 0x2;
  731. }
  732. cmd |= order;
  733. if (M2M_SUCCESS != nmi_spi_write(&cmd, 1)) {
  734. M2M_ERR("[nmi spi]: Failed data block cmd write, bus error...\n");
  735. result = N_FAIL;
  736. break;
  737. }
  738. /**
  739. Write data
  740. **/
  741. if (M2M_SUCCESS != nmi_spi_write(&b[ix], nbytes)) {
  742. M2M_ERR("[nmi spi]: Failed data block write, bus error...\n");
  743. result = N_FAIL;
  744. break;
  745. }
  746. /**
  747. Write Crc
  748. **/
  749. if (!gu8Crc_off) {
  750. if (M2M_SUCCESS != nmi_spi_write(crc, 2)) {
  751. M2M_ERR("[nmi spi]: Failed data block crc write, bus error...\n");
  752. result = N_FAIL;
  753. break;
  754. }
  755. }
  756. ix += nbytes;
  757. sz -= nbytes;
  758. } while (sz);
  759. return result;
  760. }
  761. /********************************************
  762. Spi Internal Read/Write Function
  763. ********************************************/
  764. /********************************************
  765. Spi interfaces
  766. ********************************************/
  767. static sint8 spi_write_reg(uint32 addr, uint32 u32data)
  768. {
  769. uint8 retry = SPI_RETRY_COUNT;
  770. sint8 result = N_OK;
  771. uint8 cmd = CMD_SINGLE_WRITE;
  772. uint8 clockless = 0;
  773. _RETRY_:
  774. if (addr <= 0x30) {
  775. /**
  776. NMC1000 clockless registers.
  777. **/
  778. cmd = CMD_INTERNAL_WRITE;
  779. clockless = 1;
  780. } else {
  781. cmd = CMD_SINGLE_WRITE;
  782. clockless = 0;
  783. }
  784. #if defined USE_OLD_SPI_SW
  785. result = spi_cmd(cmd, addr, u32data, 4, clockless);
  786. if (result != N_OK) {
  787. M2M_ERR("[nmi spi]: Failed cmd, write reg (%08x)...\n", (unsigned int)addr);
  788. goto _FAIL_;
  789. }
  790. result = spi_cmd_rsp(cmd);
  791. if (result != N_OK) {
  792. M2M_ERR("[nmi spi]: Failed cmd response, write reg (%08x)...\n", (unsigned int)addr);
  793. goto _FAIL_;
  794. }
  795. #else
  796. result = spi_cmd_complete(cmd, addr, (uint8 *)&u32data, 4, clockless);
  797. if (result != N_OK) {
  798. M2M_ERR("[nmi spi]: Failed cmd, write reg (%08x)...\n", addr);
  799. goto _FAIL_;
  800. }
  801. #endif
  802. _FAIL_:
  803. if (result != N_OK) {
  804. nm_bsp_sleep(1);
  805. spi_cmd(CMD_RESET, 0, 0, 0, 0);
  806. spi_cmd_rsp(CMD_RESET);
  807. M2M_ERR("Reset and retry %d %x %x\n", retry, addr, u32data);
  808. nm_bsp_sleep(1);
  809. retry--;
  810. if (retry)
  811. goto _RETRY_;
  812. }
  813. return result;
  814. }
  815. static sint8 nm_spi_write(uint32 addr, uint8 *buf, uint16 size)
  816. {
  817. sint8 result;
  818. uint8 retry = SPI_RETRY_COUNT;
  819. uint8 cmd = CMD_DMA_EXT_WRITE;
  820. _RETRY_:
  821. /**
  822. Command
  823. **/
  824. #if defined USE_OLD_SPI_SW
  825. // Workaround hardware problem with single byte transfers over SPI bus
  826. if (size == 1)
  827. size = 2;
  828. result = spi_cmd(cmd, addr, 0, size, 0);
  829. if (result != N_OK) {
  830. M2M_ERR("[nmi spi]: Failed cmd, write block (%08x)...\n", (unsigned int)addr);
  831. goto _FAIL_;
  832. }
  833. result = spi_cmd_rsp(cmd);
  834. if (result != N_OK) {
  835. M2M_ERR("[nmi spi ]: Failed cmd response, write block (%08x)...\n", (unsigned int)addr);
  836. goto _FAIL_;
  837. }
  838. #else
  839. result = spi_cmd_complete(cmd, addr, NULL, size, 0);
  840. if (result != N_OK) {
  841. M2M_ERR("[nmi spi]: Failed cmd, write block (%08x)...\n", addr);
  842. goto _FAIL_;
  843. }
  844. #endif
  845. /**
  846. Data
  847. **/
  848. result = spi_data_write(buf, size);
  849. if (result != N_OK) {
  850. M2M_ERR("[nmi spi]: Failed block data write...\n");
  851. goto _FAIL_;
  852. }
  853. /**
  854. Data RESP
  855. **/
  856. result = spi_data_rsp(cmd);
  857. if (result != N_OK) {
  858. M2M_ERR("[nmi spi]: Failed block data write...\n");
  859. goto _FAIL_;
  860. }
  861. _FAIL_:
  862. if (result != N_OK) {
  863. nm_bsp_sleep(1);
  864. spi_cmd(CMD_RESET, 0, 0, 0, 0);
  865. spi_cmd_rsp(CMD_RESET);
  866. M2M_ERR("Reset and retry %d %x %d\n", retry, addr, size);
  867. nm_bsp_sleep(1);
  868. retry--;
  869. if (retry)
  870. goto _RETRY_;
  871. }
  872. return result;
  873. }
  874. static sint8 spi_read_reg(uint32 addr, uint32 *u32data)
  875. {
  876. uint8 retry = SPI_RETRY_COUNT;
  877. sint8 result = N_OK;
  878. uint8 cmd = CMD_SINGLE_READ;
  879. uint8 tmp[4];
  880. uint8 clockless = 0;
  881. _RETRY_:
  882. if (addr <= 0xff) {
  883. /**
  884. NMC1000 clockless registers.
  885. **/
  886. cmd = CMD_INTERNAL_READ;
  887. clockless = 1;
  888. } else {
  889. cmd = CMD_SINGLE_READ;
  890. clockless = 0;
  891. }
  892. #if defined USE_OLD_SPI_SW
  893. result = spi_cmd(cmd, addr, 0, 4, clockless);
  894. if (result != N_OK) {
  895. M2M_ERR("[nmi spi]: Failed cmd, read reg (%08x)...\n", (unsigned int)addr);
  896. goto _FAIL_;
  897. }
  898. result = spi_cmd_rsp(cmd);
  899. if (result != N_OK) {
  900. M2M_ERR("[nmi spi]: Failed cmd response, read reg (%08x)...\n", (unsigned int)addr);
  901. goto _FAIL_;
  902. }
  903. /* to avoid endianess issues */
  904. result = spi_data_read(&tmp[0], 4, clockless);
  905. if (result != N_OK) {
  906. M2M_ERR("[nmi spi]: Failed data read...\n");
  907. goto _FAIL_;
  908. }
  909. #else
  910. result = spi_cmd_complete(cmd, addr, (uint8 *)&tmp[0], 4, clockless);
  911. if (result != N_OK) {
  912. M2M_ERR("[nmi spi]: Failed cmd, read reg (%08x)...\n", addr);
  913. goto _FAIL_;
  914. }
  915. #endif
  916. *u32data = tmp[0] | ((uint32)tmp[1] << 8) | ((uint32)tmp[2] << 16) | ((uint32)tmp[3] << 24);
  917. _FAIL_:
  918. if (result != N_OK) {
  919. nm_bsp_sleep(1);
  920. spi_cmd(CMD_RESET, 0, 0, 0, 0);
  921. spi_cmd_rsp(CMD_RESET);
  922. M2M_ERR("Reset and retry %d %x\n", retry, addr);
  923. nm_bsp_sleep(1);
  924. retry--;
  925. if (retry)
  926. goto _RETRY_;
  927. }
  928. return result;
  929. }
  930. static sint8 nm_spi_read(uint32 addr, uint8 *buf, uint16 size)
  931. {
  932. uint8 cmd = CMD_DMA_EXT_READ;
  933. sint8 result;
  934. uint8 retry = SPI_RETRY_COUNT;
  935. #if defined USE_OLD_SPI_SW
  936. uint8 tmp[2];
  937. uint8 single_byte_workaround = 0;
  938. #endif
  939. _RETRY_:
  940. /**
  941. Command
  942. **/
  943. #if defined USE_OLD_SPI_SW
  944. if (size == 1) {
  945. // Workaround hardware problem with single byte transfers over SPI bus
  946. size = 2;
  947. single_byte_workaround = 1;
  948. }
  949. result = spi_cmd(cmd, addr, 0, size, 0);
  950. if (result != N_OK) {
  951. M2M_ERR("[nmi spi]: Failed cmd, read block (%08x)...\n", (unsigned int)addr);
  952. goto _FAIL_;
  953. }
  954. result = spi_cmd_rsp(cmd);
  955. if (result != N_OK) {
  956. M2M_ERR("[nmi spi]: Failed cmd response, read block (%08x)...\n", (unsigned int)addr);
  957. goto _FAIL_;
  958. }
  959. /**
  960. Data
  961. **/
  962. if (single_byte_workaround) {
  963. result = spi_data_read(tmp, size, 0);
  964. buf[0] = tmp[0];
  965. } else
  966. result = spi_data_read(buf, size, 0);
  967. if (result != N_OK) {
  968. M2M_ERR("[nmi spi]: Failed block data read...\n");
  969. goto _FAIL_;
  970. }
  971. #else
  972. result = spi_cmd_complete(cmd, addr, buf, size, 0);
  973. if (result != N_OK) {
  974. M2M_ERR("[nmi spi]: Failed cmd, read block (%08x)...\n", addr);
  975. goto _FAIL_;
  976. }
  977. #endif
  978. _FAIL_:
  979. if (result != N_OK) {
  980. nm_bsp_sleep(1);
  981. spi_cmd(CMD_RESET, 0, 0, 0, 0);
  982. spi_cmd_rsp(CMD_RESET);
  983. M2M_ERR("Reset and retry %d %x %d\n", retry, addr, size);
  984. nm_bsp_sleep(1);
  985. retry--;
  986. if (retry)
  987. goto _RETRY_;
  988. }
  989. return result;
  990. }
  991. /********************************************
  992. Bus interfaces
  993. ********************************************/
  994. static void spi_init_pkt_sz(void)
  995. {
  996. uint32 val32;
  997. /* Make sure SPI max. packet size fits the defined DATA_PKT_SZ. */
  998. val32 = nm_spi_read_reg(SPI_BASE + 0x24);
  999. val32 &= ~(0x7 << 4);
  1000. switch (DATA_PKT_SZ) {
  1001. case 256:
  1002. val32 |= (0 << 4);
  1003. break;
  1004. case 512:
  1005. val32 |= (1 << 4);
  1006. break;
  1007. case 1024:
  1008. val32 |= (2 << 4);
  1009. break;
  1010. case 2048:
  1011. val32 |= (3 << 4);
  1012. break;
  1013. case 4096:
  1014. val32 |= (4 << 4);
  1015. break;
  1016. case 8192:
  1017. val32 |= (5 << 4);
  1018. break;
  1019. }
  1020. nm_spi_write_reg(SPI_BASE + 0x24, val32);
  1021. }
  1022. sint8 nm_spi_reset(void)
  1023. {
  1024. spi_cmd(CMD_RESET, 0, 0, 0, 0);
  1025. spi_cmd_rsp(CMD_RESET);
  1026. return M2M_SUCCESS;
  1027. }
  1028. /*
  1029. * @fn nm_spi_init
  1030. * @brief Initialize the SPI
  1031. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  1032. * @author M. Abdelmawla
  1033. * @date 11 July 2012
  1034. * @version 1.0
  1035. */
  1036. sint8 nm_spi_init(void)
  1037. {
  1038. uint32 chipid;
  1039. uint32 reg = 0;
  1040. /**
  1041. configure protocol
  1042. **/
  1043. gu8Crc_off = 0;
  1044. // TODO: We can remove the CRC trials if there is a definite way to reset
  1045. // the SPI to it's initial value.
  1046. if (!spi_read_reg(NMI_SPI_PROTOCOL_CONFIG, &reg)) {
  1047. /* Read failed. Try with CRC off. This might happen when module
  1048. is removed but chip isn't reset*/
  1049. gu8Crc_off = 1;
  1050. M2M_ERR("[nmi spi]: Failed internal read protocol with CRC on, retyring with CRC off...\n");
  1051. if (!spi_read_reg(NMI_SPI_PROTOCOL_CONFIG, &reg)) {
  1052. // Reaad failed with both CRC on and off, something went bad
  1053. M2M_ERR("[nmi spi]: Failed internal read protocol...\n");
  1054. return 0;
  1055. }
  1056. }
  1057. if (gu8Crc_off == 0) {
  1058. reg &= ~0xc; /* disable crc checking */
  1059. reg &= ~0x70;
  1060. reg |= (0x5 << 4);
  1061. if (!spi_write_reg(NMI_SPI_PROTOCOL_CONFIG, reg)) {
  1062. M2M_ERR("[nmi spi]: Failed internal write protocol reg...\n");
  1063. return 0;
  1064. }
  1065. gu8Crc_off = 1;
  1066. }
  1067. /**
  1068. make sure can read back chip id correctly
  1069. **/
  1070. if (!spi_read_reg(0x1000, &chipid)) {
  1071. M2M_ERR("[nmi spi]: Fail cmd read chip id...\n");
  1072. return M2M_ERR_BUS_FAIL;
  1073. }
  1074. M2M_DBG("[nmi spi]: chipid (%08x)\n", (unsigned int)chipid);
  1075. spi_init_pkt_sz();
  1076. return M2M_SUCCESS;
  1077. }
  1078. /*
  1079. * @fn nm_spi_init
  1080. * @brief DeInitialize the SPI
  1081. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  1082. * @author Samer Sarhan
  1083. * @date 27 Feb 2015
  1084. * @version 1.0
  1085. */
  1086. sint8 nm_spi_deinit(void)
  1087. {
  1088. gu8Crc_off = 0;
  1089. return M2M_SUCCESS;
  1090. }
  1091. /*
  1092. * @fn nm_spi_read_reg
  1093. * @brief Read register
  1094. * @param [in] u32Addr
  1095. * Register address
  1096. * @return Register value
  1097. * @author M. Abdelmawla
  1098. * @date 11 July 2012
  1099. * @version 1.0
  1100. */
  1101. uint32 nm_spi_read_reg(uint32 u32Addr)
  1102. {
  1103. uint32 u32Val;
  1104. spi_read_reg(u32Addr, &u32Val);
  1105. return u32Val;
  1106. }
  1107. /*
  1108. * @fn nm_spi_read_reg_with_ret
  1109. * @brief Read register with error code return
  1110. * @param [in] u32Addr
  1111. * Register address
  1112. * @param [out] pu32RetVal
  1113. * Pointer to u32 variable used to return the read value
  1114. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  1115. * @author M. Abdelmawla
  1116. * @date 11 July 2012
  1117. * @version 1.0
  1118. */
  1119. sint8 nm_spi_read_reg_with_ret(uint32 u32Addr, uint32 *pu32RetVal)
  1120. {
  1121. sint8 s8Ret;
  1122. s8Ret = spi_read_reg(u32Addr, pu32RetVal);
  1123. if (N_OK == s8Ret)
  1124. s8Ret = M2M_SUCCESS;
  1125. else
  1126. s8Ret = M2M_ERR_BUS_FAIL;
  1127. return s8Ret;
  1128. }
  1129. /*
  1130. * @fn nm_spi_write_reg
  1131. * @brief write register
  1132. * @param [in] u32Addr
  1133. * Register address
  1134. * @param [in] u32Val
  1135. * Value to be written to the register
  1136. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  1137. * @author M. Abdelmawla
  1138. * @date 11 July 2012
  1139. * @version 1.0
  1140. */
  1141. sint8 nm_spi_write_reg(uint32 u32Addr, uint32 u32Val)
  1142. {
  1143. sint8 s8Ret;
  1144. s8Ret = spi_write_reg(u32Addr, u32Val);
  1145. if (N_OK == s8Ret)
  1146. s8Ret = M2M_SUCCESS;
  1147. else
  1148. s8Ret = M2M_ERR_BUS_FAIL;
  1149. return s8Ret;
  1150. }
  1151. /*
  1152. * @fn nm_spi_read_block
  1153. * @brief Read block of data
  1154. * @param [in] u32Addr
  1155. * Start address
  1156. * @param [out] puBuf
  1157. * Pointer to a buffer used to return the read data
  1158. * @param [in] u16Sz
  1159. * Number of bytes to read. The buffer size must be >= u16Sz
  1160. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  1161. * @author M. Abdelmawla
  1162. * @date 11 July 2012
  1163. * @version 1.0
  1164. */
  1165. sint8 nm_spi_read_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
  1166. {
  1167. sint8 s8Ret;
  1168. s8Ret = nm_spi_read(u32Addr, puBuf, u16Sz);
  1169. if (N_OK == s8Ret)
  1170. s8Ret = M2M_SUCCESS;
  1171. else
  1172. s8Ret = M2M_ERR_BUS_FAIL;
  1173. return s8Ret;
  1174. }
  1175. /*
  1176. * @fn nm_spi_write_block
  1177. * @brief Write block of data
  1178. * @param [in] u32Addr
  1179. * Start address
  1180. * @param [in] puBuf
  1181. * Pointer to the buffer holding the data to be written
  1182. * @param [in] u16Sz
  1183. * Number of bytes to write. The buffer size must be >= u16Sz
  1184. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  1185. * @author M. Abdelmawla
  1186. * @date 11 July 2012
  1187. * @version 1.0
  1188. */
  1189. sint8 nm_spi_write_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
  1190. {
  1191. sint8 s8Ret;
  1192. s8Ret = nm_spi_write(u32Addr, puBuf, u16Sz);
  1193. if (N_OK == s8Ret)
  1194. s8Ret = M2M_SUCCESS;
  1195. else
  1196. s8Ret = M2M_ERR_BUS_FAIL;
  1197. return s8Ret;
  1198. }
  1199. #endif