sfud_port.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * This file is part of the Serial Flash Universal Driver Library.
  3. *
  4. * Copyright (c) 2016-2018, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Portable interface for each platform.
  26. * Created on: 2016-04-23
  27. */
  28. #include <sfud.h>
  29. #include <stdarg.h>
  30. #include <string.h>
  31. #include "FreeRTOS.h"
  32. #include "spi.h"
  33. #include "errno.h"
  34. #include "os_adapt.h"
  35. typedef struct {
  36. const char *spi_name;
  37. struct spi_configuration cfg;
  38. }sfud_spi_cfg_t;
  39. static const sfud_spi_cfg_t spi_cfg_tab[] = {
  40. {
  41. .spi_name = "dw_spi0",
  42. .cfg = {
  43. .mode = SPI_MODE_0,
  44. .data_width = 8,
  45. .max_hz = 80000000,
  46. .qspi_max_hz = 50000000,
  47. },
  48. },
  49. {
  50. .spi_name = "dw_spi2",
  51. .cfg = {
  52. .mode = SPI_MODE_0,
  53. .data_width = 8,
  54. .max_hz = 35000000,
  55. .qspi_max_hz = 35000000,
  56. },
  57. },
  58. {
  59. .spi_name = "ec_spi1",
  60. .cfg = {
  61. .mode = SPI_MODE_0,
  62. .data_width = 8,
  63. .max_hz = 35000000,
  64. .qspi_max_hz = 35000000,
  65. },
  66. },
  67. };
  68. static char log_buf[256];
  69. void sfud_log_debug(const char *file, const long line, const char *format, ...);
  70. #ifdef SFUD_USING_QSPI
  71. sfud_err enter_qspi_mode(const sfud_spi *spi);
  72. sfud_err exit_qspi_mode(const sfud_spi *spi);
  73. #endif
  74. /**
  75. * SPI write data then read data
  76. */
  77. static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  78. size_t read_size) {
  79. sfud_err result = SFUD_SUCCESS;
  80. struct spi_slave *slave;
  81. configASSERT(spi);
  82. if (write_size) {
  83. configASSERT(write_buf);
  84. }
  85. if (read_size) {
  86. configASSERT(read_buf);
  87. }
  88. slave = spi->user_data;
  89. configASSERT(slave);
  90. if (write_size && read_size) {
  91. if (spi_send_then_recv(slave, write_buf, write_size, read_buf, read_size) != ENOERR) {
  92. result = SFUD_ERR_TIMEOUT;
  93. }
  94. } else if (write_size) {
  95. if (spi_send(slave, write_buf, write_size) < 0) {
  96. result = SFUD_ERR_TIMEOUT;
  97. }
  98. } else {
  99. if (spi_recv(slave, read_buf, read_size) < 0) {
  100. result = SFUD_ERR_TIMEOUT;
  101. }
  102. }
  103. return result;
  104. }
  105. #ifdef SFUD_USING_QSPI
  106. /**
  107. * read flash data by QSPI
  108. */
  109. static sfud_err qspi_read(const sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  110. uint8_t *read_buf, size_t read_size) {
  111. sfud_err result = SFUD_SUCCESS;
  112. struct spi_slave *slave;
  113. struct qspi_message qspi_message = {0};
  114. uint32_t align_size, left_size;
  115. uint32_t retries = 0;
  116. configASSERT(spi);
  117. configASSERT(qspi_read_cmd_format);
  118. configASSERT(read_buf);
  119. slave = spi->user_data;
  120. configASSERT(slave);
  121. configASSERT(slave != NULL);
  122. xSemaphoreTake(slave->xMutex, portMAX_DELAY);
  123. retry:
  124. align_size = read_size & ~(ARCH_DMA_MINALIGN - 1);
  125. left_size = read_size & (ARCH_DMA_MINALIGN - 1);
  126. if (align_size) {
  127. enter_qspi_mode(spi);
  128. /* initial message */
  129. qspi_message.message.recv_buf = read_buf;
  130. qspi_message.message.length = align_size;
  131. qspi_message.message.cs_take = 1;
  132. qspi_message.message.cs_release = 1;
  133. qspi_message.instruction.content = qspi_read_cmd_format->instruction;
  134. qspi_message.instruction.qspi_lines = qspi_read_cmd_format->instruction_lines;
  135. qspi_message.address.content = addr;
  136. qspi_message.address.size = qspi_read_cmd_format->address_size;
  137. qspi_message.address.qspi_lines = qspi_read_cmd_format->address_lines;
  138. qspi_message.dummy_cycles = qspi_read_cmd_format->dummy_cycles;
  139. qspi_message.qspi_data_lines = qspi_read_cmd_format->data_lines;
  140. /* transfer message */
  141. result = slave->qspi_read(slave, &qspi_message);
  142. exit_qspi_mode(spi);
  143. if (result) {
  144. if (retries++ < 3) {
  145. goto retry;
  146. }
  147. result = SFUD_ERR_READ;
  148. printf("%s(), read retry failed.\n", __func__);
  149. goto __exit;
  150. }
  151. }
  152. if (left_size) {
  153. uint8_t cmd_data[5], cmd_size;
  154. int i;
  155. cmd_data[0] = SFUD_CMD_READ_DATA;
  156. cmd_size = spi->flash->addr_in_4_byte ? 5 : 4;
  157. #if DEVICE_TYPE_SELECT == SPI_NAND_FLASH
  158. addr += (align_size << 8); //2byte addr + 1dummy byte.
  159. #else //#elif DEVICE_TYPE_SELECT == SPI_NOR_FLASH
  160. addr += align_size;
  161. #endif
  162. for (i = 1; i < cmd_size; i++)
  163. cmd_data[i] = (addr >> ((cmd_size - (i + 1)) * 8)) & 0xFF;
  164. result = spi->wr(spi, cmd_data, cmd_size, read_buf + align_size, left_size);
  165. }
  166. __exit:
  167. xSemaphoreGive(slave->xMutex);
  168. return result;
  169. }
  170. #endif /* SFUD_USING_QSPI */
  171. static void spi_lock(const sfud_spi *spi)
  172. {
  173. struct spi_slave *slave;
  174. configASSERT(spi);
  175. slave = spi->user_data;
  176. configASSERT(slave);
  177. xSemaphoreTake(slave->xSfudMutex, portMAX_DELAY);
  178. }
  179. static void spi_unlock(const sfud_spi *spi)
  180. {
  181. struct spi_slave *slave;
  182. configASSERT(spi);
  183. slave = spi->user_data;
  184. configASSERT(slave);
  185. xSemaphoreGive(slave->xSfudMutex);
  186. }
  187. static void retry_delay_100us(void) {
  188. /* 100 microsecond delay */
  189. #if 1
  190. int time = get_timer(0);
  191. while (get_timer(0) - time < 100) {
  192. taskYIELD();
  193. }
  194. #else
  195. vTaskDelay((configTICK_RATE_HZ * 1 + 9999) / 10000);
  196. #endif
  197. }
  198. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  199. int i;
  200. sfud_err result = SFUD_SUCCESS;
  201. struct spi_slave *slave;
  202. struct spi_configuration *config;
  203. /**
  204. * add your port spi bus and device object initialize code like this:
  205. * 1. rcc initialize
  206. * 2. gpio initialize
  207. * 3. spi device initialize
  208. * 4. flash->spi and flash->retry item initialize
  209. * flash->spi.wr = spi_write_read; //Required
  210. * flash->spi.qspi_read = qspi_read; //Required when QSPI mode enable
  211. * flash->spi.lock = spi_lock;
  212. * flash->spi.unlock = spi_unlock;
  213. * flash->spi.user_data = &spix;
  214. * flash->retry.delay = null;
  215. * flash->retry.times = 10000; //Required
  216. */
  217. config = NULL;
  218. for (i=0; i<(sizeof(spi_cfg_tab)/sizeof(spi_cfg_tab[0])); i++) {
  219. if (strcmp(spi_cfg_tab[i].spi_name, flash->spi.name) == 0) {
  220. config = &(spi_cfg_tab[i].cfg);
  221. break;
  222. }
  223. }
  224. if (config == NULL) {
  225. printf("not find %s default config!\n", flash->spi.name);
  226. return SFUD_ERR_NOT_FOUND;
  227. }
  228. slave = spi_open(flash->spi.name);
  229. if (!slave) {
  230. printf("%s open fail.\n", flash->spi.name);
  231. return SFUD_ERR_NOT_FOUND;
  232. }
  233. slave->xSfudMutex = xSemaphoreCreateMutex();
  234. spi_configure(slave, config);
  235. flash->spi.wr = spi_write_read;
  236. #ifdef SFUD_USING_QSPI
  237. flash->spi.qspi_read = qspi_read;
  238. #endif
  239. flash->spi.lock = spi_lock;
  240. flash->spi.unlock = spi_unlock;
  241. flash->spi.user_data = slave;
  242. /* 100 microsecond delay */
  243. flash->retry.delay = retry_delay_100us;
  244. /* 60 seconds timeout */
  245. flash->retry.times = 60 * 10000;
  246. flash->spi.flash = flash;
  247. return result;
  248. }
  249. /**
  250. * This function is print debug info.
  251. *
  252. * @param file the file which has call this function
  253. * @param line the line number which has call this function
  254. * @param format output format
  255. * @param ... args
  256. */
  257. void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  258. va_list args;
  259. /* args point to the first variable parameter */
  260. va_start(args, format);
  261. printf("[SFUD](%s:%ld) ", file, line);
  262. /* must use vprintf to print */
  263. vsnprintf(log_buf, sizeof(log_buf), format, args);
  264. printf("%s\n", log_buf);
  265. va_end(args);
  266. }
  267. /**
  268. * This function is print routine info.
  269. *
  270. * @param format output format
  271. * @param ... args
  272. */
  273. void sfud_log_info(const char *format, ...) {
  274. va_list args;
  275. /* args point to the first variable parameter */
  276. va_start(args, format);
  277. printf("[SFUD]");
  278. /* must use vprintf to print */
  279. vsnprintf(log_buf, sizeof(log_buf), format, args);
  280. printf("%s\n", log_buf);
  281. va_end(args);
  282. }