sfud_port.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "timer.h"
  32. #include "FreeRTOS.h"
  33. #include "spi.h"
  34. #include "errno.h"
  35. #include "os_adapt.h"
  36. typedef struct {
  37. const char *spi_name;
  38. struct spi_configuration cfg;
  39. }sfud_spi_cfg_t;
  40. static const sfud_spi_cfg_t spi_cfg_tab[] = {
  41. {
  42. .spi_name = "dw_spi0",
  43. .cfg = {
  44. .mode = SPI_MODE_0,
  45. .data_width = 8,
  46. .max_hz = 80000000,
  47. .qspi_max_hz = 50000000,
  48. },
  49. },
  50. {
  51. .spi_name = "dw_spi2",
  52. .cfg = {
  53. .mode = SPI_MODE_0,
  54. .data_width = 8,
  55. .max_hz = 35000000,
  56. .qspi_max_hz = 35000000,
  57. },
  58. },
  59. {
  60. .spi_name = "ec_spi1",
  61. .cfg = {
  62. .mode = SPI_MODE_0,
  63. .data_width = 8,
  64. .max_hz = 35000000,
  65. .qspi_max_hz = 35000000,
  66. },
  67. },
  68. };
  69. static char log_buf[256];
  70. void sfud_log_debug(const char *file, const long line, const char *format, ...);
  71. #ifdef SFUD_USING_QSPI
  72. sfud_err enter_qspi_mode(const sfud_spi *spi);
  73. sfud_err exit_qspi_mode(const sfud_spi *spi);
  74. #endif
  75. /**
  76. * SPI write data then read data
  77. */
  78. static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  79. size_t read_size) {
  80. sfud_err result = SFUD_SUCCESS;
  81. struct spi_slave *slave;
  82. configASSERT(spi);
  83. if (write_size) {
  84. configASSERT(write_buf);
  85. }
  86. if (read_size) {
  87. configASSERT(read_buf);
  88. }
  89. slave = spi->user_data;
  90. configASSERT(slave);
  91. if (write_size && read_size) {
  92. if (spi_send_then_recv(slave, write_buf, write_size, read_buf, read_size) != ENOERR) {
  93. result = SFUD_ERR_TIMEOUT;
  94. }
  95. } else if (write_size) {
  96. if (spi_send(slave, write_buf, write_size) < 0) {
  97. result = SFUD_ERR_TIMEOUT;
  98. }
  99. } else {
  100. if (spi_recv(slave, read_buf, read_size) < 0) {
  101. result = SFUD_ERR_TIMEOUT;
  102. }
  103. }
  104. return result;
  105. }
  106. #ifdef SFUD_USING_QSPI
  107. /**
  108. * read flash data by QSPI
  109. */
  110. static sfud_err qspi_read(const sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  111. uint8_t *read_buf, size_t read_size) {
  112. sfud_err result = SFUD_SUCCESS;
  113. struct spi_slave *slave;
  114. struct qspi_message qspi_message = {0};
  115. uint32_t align_size, left_size;
  116. uint32_t retries = 0;
  117. configASSERT(spi);
  118. configASSERT(qspi_read_cmd_format);
  119. configASSERT(read_buf);
  120. slave = spi->user_data;
  121. configASSERT(slave);
  122. configASSERT(slave != NULL);
  123. xSemaphoreTake(slave->xMutex, portMAX_DELAY);
  124. retry:
  125. align_size = read_size & ~(ARCH_DMA_MINALIGN - 1);
  126. left_size = read_size & (ARCH_DMA_MINALIGN - 1);
  127. if (align_size) {
  128. enter_qspi_mode(spi);
  129. /* initial message */
  130. qspi_message.message.recv_buf = read_buf;
  131. qspi_message.message.length = align_size;
  132. qspi_message.message.cs_take = 1;
  133. qspi_message.message.cs_release = 1;
  134. qspi_message.instruction.content = qspi_read_cmd_format->instruction;
  135. qspi_message.instruction.qspi_lines = qspi_read_cmd_format->instruction_lines;
  136. qspi_message.address.content = addr;
  137. qspi_message.address.size = qspi_read_cmd_format->address_size;
  138. qspi_message.address.qspi_lines = qspi_read_cmd_format->address_lines;
  139. qspi_message.dummy_cycles = qspi_read_cmd_format->dummy_cycles;
  140. qspi_message.qspi_data_lines = qspi_read_cmd_format->data_lines;
  141. /* transfer message */
  142. result = slave->qspi_read(slave, &qspi_message);
  143. exit_qspi_mode(spi);
  144. if (result) {
  145. if (retries++ < 3) {
  146. goto retry;
  147. }
  148. result = SFUD_ERR_READ;
  149. printf("%s(), read retry failed.\n", __func__);
  150. goto __exit;
  151. }
  152. }
  153. if (left_size) {
  154. uint8_t cmd_data[5], cmd_size;
  155. int i;
  156. cmd_data[0] = SFUD_CMD_READ_DATA;
  157. cmd_size = spi->flash->addr_in_4_byte ? 5 : 4;
  158. #if DEVICE_TYPE_SELECT == SPI_NAND_FLASH
  159. addr += (align_size << 8); //2byte addr + 1dummy byte.
  160. #else //#elif DEVICE_TYPE_SELECT == SPI_NOR_FLASH
  161. addr += align_size;
  162. #endif
  163. for (i = 1; i < cmd_size; i++)
  164. cmd_data[i] = (addr >> ((cmd_size - (i + 1)) * 8)) & 0xFF;
  165. result = spi->wr(spi, cmd_data, cmd_size, read_buf + align_size, left_size);
  166. }
  167. __exit:
  168. xSemaphoreGive(slave->xMutex);
  169. return result;
  170. }
  171. #endif /* SFUD_USING_QSPI */
  172. static void spi_lock(const sfud_spi *spi)
  173. {
  174. struct spi_slave *slave;
  175. configASSERT(spi);
  176. slave = spi->user_data;
  177. configASSERT(slave);
  178. xSemaphoreTake(slave->xSfudMutex, portMAX_DELAY);
  179. }
  180. static void spi_unlock(const sfud_spi *spi)
  181. {
  182. struct spi_slave *slave;
  183. configASSERT(spi);
  184. slave = spi->user_data;
  185. configASSERT(slave);
  186. xSemaphoreGive(slave->xSfudMutex);
  187. }
  188. static void retry_delay_100us(void) {
  189. /* 100 microsecond delay */
  190. #if 1
  191. int time = get_timer(0);
  192. while (get_timer(0) - time < 100) {
  193. taskYIELD();
  194. }
  195. #else
  196. vTaskDelay((configTICK_RATE_HZ * 1 + 9999) / 10000);
  197. #endif
  198. }
  199. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  200. int i;
  201. sfud_err result = SFUD_SUCCESS;
  202. struct spi_slave *slave;
  203. struct spi_configuration *config;
  204. /**
  205. * add your port spi bus and device object initialize code like this:
  206. * 1. rcc initialize
  207. * 2. gpio initialize
  208. * 3. spi device initialize
  209. * 4. flash->spi and flash->retry item initialize
  210. * flash->spi.wr = spi_write_read; //Required
  211. * flash->spi.qspi_read = qspi_read; //Required when QSPI mode enable
  212. * flash->spi.lock = spi_lock;
  213. * flash->spi.unlock = spi_unlock;
  214. * flash->spi.user_data = &spix;
  215. * flash->retry.delay = null;
  216. * flash->retry.times = 10000; //Required
  217. */
  218. config = NULL;
  219. for (i=0; i<(sizeof(spi_cfg_tab)/sizeof(spi_cfg_tab[0])); i++) {
  220. if (strcmp(spi_cfg_tab[i].spi_name, flash->spi.name) == 0) {
  221. config = &(spi_cfg_tab[i].cfg);
  222. break;
  223. }
  224. }
  225. if (config == NULL) {
  226. printf("not find %s default config!\n", flash->spi.name);
  227. return SFUD_ERR_NOT_FOUND;
  228. }
  229. slave = spi_open(flash->spi.name);
  230. if (!slave) {
  231. printf("%s open fail.\n", flash->spi.name);
  232. return SFUD_ERR_NOT_FOUND;
  233. }
  234. slave->xSfudMutex = xSemaphoreCreateMutex();
  235. spi_configure(slave, config);
  236. flash->spi.wr = spi_write_read;
  237. #ifdef SFUD_USING_QSPI
  238. flash->spi.qspi_read = qspi_read;
  239. #endif
  240. flash->spi.lock = spi_lock;
  241. flash->spi.unlock = spi_unlock;
  242. flash->spi.user_data = slave;
  243. /* 100 microsecond delay */
  244. flash->retry.delay = retry_delay_100us;
  245. /* 60 seconds timeout */
  246. flash->retry.times = 60 * 10000;
  247. flash->spi.flash = flash;
  248. return result;
  249. }
  250. /**
  251. * This function is print debug info.
  252. *
  253. * @param file the file which has call this function
  254. * @param line the line number which has call this function
  255. * @param format output format
  256. * @param ... args
  257. */
  258. void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  259. va_list args;
  260. /* args point to the first variable parameter */
  261. va_start(args, format);
  262. printf("[SFUD](%s:%ld) ", file, line);
  263. /* must use vprintf to print */
  264. vsnprintf(log_buf, sizeof(log_buf), format, args);
  265. printf("%s\n", log_buf);
  266. va_end(args);
  267. }
  268. /**
  269. * This function is print routine info.
  270. *
  271. * @param format output format
  272. * @param ... args
  273. */
  274. void sfud_log_info(const char *format, ...) {
  275. va_list args;
  276. /* args point to the first variable parameter */
  277. va_start(args, format);
  278. printf("[SFUD]");
  279. /* must use vprintf to print */
  280. vsnprintf(log_buf, sizeof(log_buf), format, args);
  281. printf("%s\n", log_buf);
  282. va_end(args);
  283. }