spi_flash_acts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (c) 2018 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <drivers/flash.h>
  7. #include <drivers/spi.h>
  8. #include <logging/log.h>
  9. #include <soc.h>
  10. #include <board_cfg.h>
  11. #include "spi_flash.h"
  12. #include <linker/linker-defs.h>
  13. #include <drivers/gpio.h>
  14. LOG_MODULE_REGISTER(spi_flash_acts, CONFIG_FLASH_LOG_LEVEL);
  15. #ifdef CONFIG_SPI_NOR_FLASH_DRV_EXT
  16. extern int spinor_enter_4byte_address_mode(struct spinor_info *sni);
  17. #endif
  18. #if defined(CONFIG_SPI_FLASH_1_GPIO_CS_EN) && (CONFIG_SPI_FLASH_1_GPIO_CS_EN == 1)
  19. static const struct device *spi_gpio_cs_dev;
  20. #endif
  21. #if (CONFIG_SPI_FLASH_SYNC_MULTI_DEV == 1)
  22. static struct k_sem spi_flash_sync = Z_SEM_INITIALIZER(spi_flash_sync, 1, 1);
  23. #endif
  24. #ifdef SPINOR_RESET_FUN_ADDR
  25. typedef void (*spi_reset_func)(struct spi_info *si);
  26. __ramfunc void spi_flash_reset(struct spi_info *si)
  27. {
  28. spi_reset_func func = (spi_reset_func)(SPINOR_RESET_FUN_ADDR);
  29. func(si);
  30. }
  31. #else
  32. __ramfunc void spi_flash_reset(struct spi_info *si)
  33. {
  34. p_spinor_api->continuous_read_reset((struct spinor_info *)si);
  35. }
  36. #endif
  37. __ramfunc void spi_flash_acts_prepare(struct spi_info *si)
  38. {
  39. /* wait for spi ready */
  40. while(!(sys_read32(SPI_STA(si->base)) & SPI_STA_READY));
  41. spi_flash_reset(si);
  42. }
  43. __ramfunc void spi_flash_lock_acquire(void)
  44. {
  45. #ifndef CONFIG_NOR_CODE_IN_RAM
  46. #if defined(CONFIG_SPI_FLASH_SYNC_MULTI_DEV) && (CONFIG_SPI_FLASH_SYNC_MULTI_DEV == 1)
  47. if(!k_is_in_isr()){
  48. k_sem_take(&spi_flash_sync, K_FOREVER);
  49. }
  50. #endif
  51. #endif
  52. }
  53. __ramfunc void spi_flash_lock_release(void)
  54. {
  55. #ifndef CONFIG_NOR_CODE_IN_RAM
  56. #if defined(CONFIG_SPI_FLASH_SYNC_MULTI_DEV) && (CONFIG_SPI_FLASH_SYNC_MULTI_DEV == 1)
  57. if(!k_is_in_isr()){
  58. k_sem_give(&spi_flash_sync);
  59. }
  60. #endif
  61. #endif
  62. }
  63. __ramfunc int spi_flash_acts_read(const struct device *dev, off_t offset, void *data, size_t len)
  64. {
  65. struct spinor_info *sni = DEV_DATA(dev);
  66. int ret = 0;
  67. size_t tmplen;
  68. spi_flash_lock_acquire();
  69. tmplen = len;
  70. while(tmplen > 0) {
  71. if(tmplen < 0x8000)
  72. len = tmplen;
  73. else
  74. len = 0x8000;
  75. #ifdef CONFIG_SPI_NOR_FLASH_4B_ADDRESS
  76. ret = spinor_4b_addr_op_api.read(sni, offset, data, len);
  77. #else
  78. #if defined(CONFIG_SPI_FLASH_NO_IRQ_LOCK) && (CONFIG_SPI_FLASH_NO_IRQ_LOCK == 1)
  79. ret = p_spinor_api->read(sni, offset, data, len);
  80. #else
  81. uint32_t key = irq_lock();
  82. ret = p_spinor_api->read(sni, offset, data, len);
  83. irq_unlock(key);
  84. #endif
  85. #endif
  86. offset += len;
  87. data = (void *)((unsigned int )data + len);
  88. tmplen -= len;
  89. }
  90. spi_flash_lock_release();
  91. return ret;
  92. }
  93. __ramfunc int spi_flash_acts_write(const struct device *dev, off_t offset, const void *data, size_t len)
  94. {
  95. struct spinor_info *sni = DEV_DATA(dev);
  96. int ret;
  97. spi_flash_lock_acquire();
  98. #ifdef CONFIG_SPI_NOR_FLASH_4B_ADDRESS
  99. ret = spinor_4b_addr_op_api.write(sni, offset, data, len);
  100. #else
  101. #if defined(CONFIG_SPI_FLASH_NO_IRQ_LOCK) && (CONFIG_SPI_FLASH_NO_IRQ_LOCK == 1)
  102. uint32_t flag = sni->spi.flag;
  103. uint32_t nor_flag = sni->flag;
  104. sni->flag |= SPINOR_FLAG_UNLOCK_IRQ_WAIT_READY; //unlock wait ready
  105. sni->spi.flag &= ~SPI_FLAG_NO_IRQ_LOCK; //lock
  106. ret = p_spinor_api->write(sni, offset, data, len);
  107. sni->spi.flag = flag;
  108. sni->flag = nor_flag;
  109. #else
  110. uint32_t key = irq_lock();
  111. ret = p_spinor_api->write(sni, offset, data, len);
  112. irq_unlock(key);
  113. #endif
  114. #endif
  115. spi_flash_lock_release();
  116. return ret ;
  117. }
  118. __ramfunc int spi_flash_acts_erase(const struct device *dev, off_t offset, size_t size)
  119. {
  120. struct spinor_info *sni = DEV_DATA(dev);
  121. int ret;
  122. spi_flash_lock_acquire();
  123. #ifdef CONFIG_SPI_NOR_FLASH_4B_ADDRESS
  124. ret = spinor_4b_addr_op_api.erase(sni, offset, size);
  125. #else
  126. #if defined(CONFIG_SPI_FLASH_NO_IRQ_LOCK) && (CONFIG_SPI_FLASH_NO_IRQ_LOCK == 1)
  127. uint32_t flag = sni->spi.flag;
  128. uint32_t nor_flag = sni->flag;
  129. sni->flag |= SPINOR_FLAG_UNLOCK_IRQ_WAIT_READY; //unlock wait ready
  130. sni->spi.flag &= ~SPI_FLAG_NO_IRQ_LOCK; //lock
  131. ret = p_spinor_api->erase(sni, offset, size);
  132. sni->spi.flag = flag;
  133. sni->flag = nor_flag;
  134. #else
  135. uint32_t key = irq_lock();
  136. ret = p_spinor_api->erase(sni, offset, size);
  137. irq_unlock(key);
  138. #endif
  139. #endif
  140. spi_flash_lock_release();
  141. return ret ;
  142. }
  143. static inline void xspi_delay(void)
  144. {
  145. volatile int i = 100000;
  146. while (i--)
  147. ;
  148. }
  149. __ramfunc void xspi_nor_enable_status_qe(struct spinor_info *sni)
  150. {
  151. uint16_t status;
  152. /* MACRONIX's spinor has different QE bit */
  153. if (XSPI_NOR_MANU_ID_MACRONIX == (sni->chipid & 0xff)) {
  154. status = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  155. if (!(status & 0x40)) {
  156. /* set QE bit to disable HOLD/WP pin function */
  157. status |= 0x40;
  158. p_spinor_api->write_status(sni, XSPI_NOR_CMD_WRITE_STATUS,
  159. (u8_t *)&status, 1);
  160. }
  161. return;
  162. }
  163. /* check QE bit */
  164. status = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  165. if (!(status & 0x2)) {
  166. /* set QE bit to disable HOLD/WP pin function, for WinBond */
  167. status |= 0x2;
  168. p_spinor_api->write_status(sni, XSPI_NOR_CMD_WRITE_STATUS2,
  169. (u8_t *)&status, 1);
  170. /* check QE bit again */
  171. status = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  172. if (!(status & 0x2)) {
  173. /* oh, let's try old write status cmd, for GigaDevice/Berg */
  174. status = ((status | 0x2) << 8) |
  175. p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  176. p_spinor_api->write_status(sni, XSPI_NOR_CMD_WRITE_STATUS,
  177. (u8_t *)&status, 2);
  178. }
  179. }
  180. xspi_delay();
  181. }
  182. static inline void xspi_setup_bus_width(struct spinor_info *sni, u8_t bus_width)
  183. {
  184. struct acts_spi_reg *spi= (struct acts_spi_reg *)sni->spi.base;
  185. spi->ctrl = (spi->ctrl & ~(0x3 << 10)) | (((bus_width & 0x7) / 2 + 1) << 10);
  186. xspi_delay();
  187. }
  188. static inline void xspi_setup_delaychain(struct spinor_info *sni, u8_t delay)
  189. {
  190. struct acts_spi_reg *spi= (struct acts_spi_reg *)sni->spi.base;
  191. spi->delaychain = (spi->delaychain & ~(0x3F << 0)) | (delay << 0);
  192. xspi_delay();
  193. }
  194. #if IS_ENABLED(CONFIG_SPINOR_TEST_DELAYCHAIN)
  195. extern int nor_test_delaychain(struct device *dev);
  196. #endif
  197. #if IS_ENABLED(CONFIG_NOR_ACTS_DQ_MODE_ENABLE)
  198. extern void nor_dual_quad_read_mode_try(struct spinor_info *sni);
  199. #endif
  200. __ramfunc int spi_flash_acts_init(const struct device *dev)
  201. {
  202. struct spinor_info *sni = DEV_DATA(dev);
  203. uint32_t key;
  204. uint8_t status, status2, status3;
  205. sni->spi.prepare_hook = spi_flash_acts_prepare;
  206. key = irq_lock();
  207. sni->chipid = p_spinor_api->read_chipid(sni);
  208. printk("read spi nor chipid:0x%x\n", sni->chipid);
  209. status = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  210. status2 = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  211. status3 = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS3);
  212. printk("spinor status: {0x%02x 0x%02x 0x%02x}\n", status, status2, status3);
  213. #if IS_ENABLED(CONFIG_NOR_ACTS_DQ_MODE_ENABLE)
  214. nor_dual_quad_read_mode_try(sni);
  215. printk("bus width : %d, and cache read use ", sni->spi.bus_width);
  216. #else
  217. if(sni->spi.bus_width == 4) {
  218. printk("nor is 4 line mode\n");
  219. sni->spi.flag |= SPI_FLAG_SPI_4XIO;
  220. xspi_nor_enable_status_qe(sni);
  221. /* enable 4x mode */
  222. xspi_setup_bus_width(sni, 4);
  223. } else if(sni->spi.bus_width == 2) {
  224. printk("nor is 2 line mode\n");
  225. /* enable 2x mode */
  226. xspi_setup_bus_width(sni, 2);
  227. } else {
  228. sni->spi.bus_width = 1;
  229. printk("nor is 1 line mode\n");
  230. /* enable 1x mode */
  231. xspi_setup_bus_width(sni, 1);
  232. }
  233. #endif
  234. /* setup SPI clock rate */
  235. clk_set_rate(CLOCK_ID_SPI0, MHZ(CONFIG_SPI_FLASH_FREQ_MHZ));
  236. /* configure delay chain */
  237. xspi_setup_delaychain(sni, sni->spi.delay_chain);
  238. /* check delay chain workable */
  239. sni->chipid = p_spinor_api->read_chipid(sni);
  240. printk("read again spi nor chipid:0x%x\n", sni->chipid);
  241. #ifdef CONFIG_SPI_NOR_FLASH_4B_ADDRESS
  242. spinor_enter_4byte_address_mode(sni);
  243. #endif
  244. #if defined(CONFIG_SPI_FLASH_1_GPIO_CS_EN) && (CONFIG_SPI_FLASH_1_GPIO_CS_EN == 1)
  245. spi_gpio_cs_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(CONFIG_SPI_FLASH_1_GPIO_CS_PIN));
  246. if (!spi_gpio_cs_dev) {
  247. printk("failed to get gpio:%d device", CONFIG_SPI_FLASH_1_GPIO_CS_PIN);
  248. irq_unlock(key);
  249. return -1;
  250. }
  251. gpio_pin_configure(spi_gpio_cs_dev, CONFIG_SPI_FLASH_1_GPIO_CS_PIN % 32, GPIO_OUTPUT);
  252. gpio_pin_set(spi_gpio_cs_dev, CONFIG_SPI_FLASH_1_GPIO_CS_PIN % 32, 1);
  253. printk("use GPIO:%d as spi cs pin", CONFIG_SPI_FLASH_1_GPIO_CS_PIN);
  254. #endif
  255. status = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  256. status2 = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  257. status3 = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS3);
  258. printk("spinor status: {0x%02x 0x%02x 0x%02x}\n", status, status2, status3);
  259. #if IS_ENABLED(CONFIG_SPINOR_TEST_DELAYCHAIN)
  260. nor_test_delaychain(dev);
  261. #endif
  262. irq_unlock(key);
  263. return 0;
  264. }
  265. #if defined(CONFIG_SPI_FLASH_1_GPIO_CS_EN) && (CONFIG_SPI_FLASH_1_GPIO_CS_EN == 1)
  266. static void spi_flash_acts_cs_gpio(struct spi_info *si, int value)
  267. {
  268. if (spi_gpio_cs_dev) {
  269. gpio_pin_set(spi_gpio_cs_dev, CONFIG_SPI_FLASH_1_GPIO_CS_PIN % 32, value ? true : false);
  270. k_busy_wait(1);
  271. }
  272. }
  273. #endif
  274. #define SPI0_BASE 0x40028000UL
  275. static struct spinor_info system_spi_nor = {
  276. .spi = {
  277. .base = SPI0_BASE,
  278. .bus_width = 1,
  279. .delay_chain = 0x20,
  280. .flag = 0,
  281. #if 0
  282. .dma_base= 0x4001C600, //DMA5
  283. #endif
  284. },
  285. };
  286. #ifndef CONFIG_BOARD_NANDBOOT
  287. __sleepfunc void sys_norflash_power_ctrl(uint32_t is_powerdown)
  288. {
  289. if (is_powerdown){
  290. p_spinor_api->write_status(&system_spi_nor, 0xB9, NULL, 0);
  291. } else {
  292. p_spinor_api->write_status(&system_spi_nor, 0xAB, NULL, 0);
  293. }
  294. }
  295. #endif
  296. void sys_norflash_exit_4b(void)
  297. {
  298. printk("spinor exit 4-byte address mode\n");
  299. p_spinor_api->write_status(&system_spi_nor, 0xE9, NULL, 0);
  300. }
  301. #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)
  302. static void spi_flash_acts_pages_layout(
  303. const struct device *dev,
  304. const struct flash_pages_layout **layout,
  305. size_t *layout_size)
  306. {
  307. *layout = &(DEV_CFG(dev)->pages_layout);
  308. *layout_size = 1;
  309. }
  310. #endif /* IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT) */
  311. #if IS_ENABLED(CONFIG_NOR_ACTS_DATA_PROTECTION_ENABLE)
  312. extern int nor_write_protection(const struct device *dev, bool enable);
  313. #endif
  314. __ramfunc int spi_flash_acts_write_protection(const struct device *dev, bool enable)
  315. {
  316. #if IS_ENABLED(CONFIG_NOR_ACTS_DATA_PROTECTION_ENABLE)
  317. spi_flash_lock_acquire();
  318. nor_write_protection(dev, enable);
  319. spi_flash_lock_release();
  320. #else
  321. struct spinor_info *sni = DEV_DATA(dev);
  322. uint8_t status1, status2, sta_en;
  323. uint8_t sta[2];
  324. uint32_t key;
  325. if (sni->chipid == 0x1560c8){//3085 nor
  326. key = irq_lock();
  327. status1 = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  328. status2 = p_spinor_api->read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  329. sta_en = (status1 >> 2) & 0x1f; //bp4-bp0 bit6-bit2
  330. if(enable){
  331. if(!sta_en){ // if disable
  332. #define PROTECT_16KB 0x1b
  333. #define PROTECT_64KB 0x9
  334. #define PROTECT_128KB 0xa
  335. #define PROTECT_256KB 0xb
  336. #define PROTECT_512KB 0xc
  337. #define PROTECT_1MB 0xd
  338. sta[0] = status1 | (PROTECT_16KB << 2); //bit6-bit2 = bp4-bp0 =
  339. sta[1] = status2 & (~0x40);
  340. p_spinor_api->write_status(sni, XSPI_NOR_CMD_WRITE_STATUS, sta, 2);
  341. printk("enable status1-2: {0x%02x 0x%02x}\n", sta[0], sta[1]);
  342. }
  343. }else{
  344. if(sta_en){ // if enable
  345. sta[0] = status1 & 0x83 ; //bit6-bit2 = bp4-bp0 = 00000 = disable protect
  346. sta[1] = status2 & (~0x40);
  347. p_spinor_api->write_status(sni, XSPI_NOR_CMD_WRITE_STATUS, sta, 2);
  348. printk("disable status1-2: {0x%02x 0x%02x}\n", sta[0], sta[1]);
  349. }
  350. }
  351. irq_unlock(key);
  352. }
  353. #endif
  354. return 0;
  355. }
  356. static struct flash_driver_api spi_flash_nor_api = {
  357. .read = spi_flash_acts_read,
  358. .write = spi_flash_acts_write,
  359. .erase = spi_flash_acts_erase,
  360. .write_protection = spi_flash_acts_write_protection,
  361. #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)
  362. .page_layout = spi_flash_acts_pages_layout,
  363. #endif
  364. };
  365. /* system XIP spinor */
  366. static struct spinor_info spi_flash_acts_data = {
  367. .spi = {
  368. .base = SPI0_REG_BASE,
  369. .bus_width = CONFIG_SPI_FLASH_BUS_WIDTH,
  370. .delay_chain = CONFIG_SPI_FLASH_DELAY_CHAIN,
  371. #if (CONFIG_SPI_FLASH_1 == 0) && (CONFIG_SPI_FLASH_2 == 0)
  372. #if (CONFIG_DMA_SPINOR_RESEVER_CHAN < CONFIG_DMA_0_PCHAN_NUM)
  373. .dma_base= (DMA_REG_BASE + 0x100 + (CONFIG_DMA_SPINOR_RESEVER_CHAN * 0x100)),
  374. #endif
  375. #endif
  376. #if defined(CONFIG_SPI_FLASH_NO_IRQ_LOCK) && (CONFIG_SPI_FLASH_NO_IRQ_LOCK == 1)
  377. .flag = SPI_FLAG_NO_IRQ_LOCK,
  378. #else
  379. .flag = 0,
  380. #endif
  381. },
  382. .flag = 0,
  383. };
  384. static const struct spi_flash_acts_config spi_acts_config = {
  385. #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)
  386. .pages_layout = {
  387. .pages_count = CONFIG_SPI_FLASH_CHIP_SIZE/0x1000,
  388. .pages_size = 0x1000,
  389. },
  390. #endif
  391. .chip_size = CONFIG_SPI_FLASH_CHIP_SIZE,
  392. .page_size = 0x1000,
  393. };
  394. #if IS_ENABLED(CONFIG_SPI_FLASH_0)
  395. DEVICE_DEFINE(spi_flash_acts, CONFIG_SPI_FLASH_NAME, &spi_flash_acts_init, NULL,
  396. &spi_flash_acts_data, &spi_acts_config, PRE_KERNEL_1,
  397. CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &spi_flash_nor_api);
  398. #endif
  399. #if (CONFIG_SPI_FLASH_1 == 1)
  400. /* system XIP spinor */
  401. static struct spinor_info spi_flash_1_acts_data = {
  402. .spi = {
  403. .base = SPI0_REG_BASE,
  404. .bus_width = CONFIG_SPI_FLASH_1_BUS_WIDTH,
  405. .delay_chain = CONFIG_SPI_FLASH_1_DELAY_CHAIN,
  406. #if (CONFIG_DMA_SPINOR_RESEVER_CHAN < CONFIG_DMA_0_PCHAN_NUM)
  407. .dma_base= (DMA_REG_BASE + 0x100 + (CONFIG_DMA_SPINOR_RESEVER_CHAN * 0x100)),
  408. #endif
  409. #if defined(CONFIG_SPI_FLASH_NO_IRQ_LOCK) && (CONFIG_SPI_FLASH_NO_IRQ_LOCK == 1)
  410. .flag = SPI_FLAG_NO_IRQ_LOCK,
  411. #else
  412. .flag = 0,
  413. #endif
  414. #if defined(CONFIG_SPI_FLASH_1_GPIO_CS_EN) && (CONFIG_SPI_FLASH_1_GPIO_CS_EN == 1)
  415. .set_cs = spi_flash_acts_cs_gpio,
  416. #endif
  417. },
  418. .flag = 0,
  419. };
  420. static const struct spi_flash_acts_config spi_flash_1_acts_config = {
  421. #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)
  422. .pages_layout = {
  423. .pages_count = CONFIG_SPI_FLASH_1_CHIP_SIZE/0x1000,
  424. .pages_size = 0x1000,
  425. },
  426. #endif
  427. .chip_size = CONFIG_SPI_FLASH_1_CHIP_SIZE,
  428. .page_size = 0x1000,
  429. };
  430. DEVICE_DEFINE(spi_flash_1_acts, CONFIG_SPI_FLASH_1_NAME, &spi_flash_acts_init, NULL,
  431. &spi_flash_1_acts_data, &spi_flash_1_acts_config, PRE_KERNEL_1,
  432. CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &spi_flash_nor_api);
  433. #endif
  434. #if IS_ENABLED(CONFIG_SPI_FLASH_2)
  435. static K_MUTEX_DEFINE(flash_2_mutex);
  436. static int spi_flash_2_acts_read(const struct device *dev, off_t offset, void *data, size_t len)
  437. {
  438. struct spinor_info *sni = DEV_DATA(dev);
  439. int ret = 0;
  440. size_t tmplen;
  441. k_mutex_lock(&flash_2_mutex, K_FOREVER);
  442. tmplen = len;
  443. while(tmplen > 0) {
  444. if(tmplen < 0x8000)
  445. len = tmplen;
  446. else
  447. len = 0x8000;
  448. ret = spinor_4b_addr_op_api.read(sni, offset, data, len);
  449. offset += len;
  450. data = (void *)((unsigned int )data + len);
  451. tmplen -= len;
  452. }
  453. k_mutex_unlock(&flash_2_mutex);
  454. return ret;
  455. }
  456. static int spi_flash_2_acts_write(const struct device *dev, off_t offset, const void *data, size_t len)
  457. {
  458. struct spinor_info *sni = DEV_DATA(dev);
  459. int ret;
  460. k_mutex_lock(&flash_2_mutex, K_FOREVER);
  461. ret = spinor_4b_addr_op_api.write(sni, offset, data, len);
  462. k_mutex_unlock(&flash_2_mutex);
  463. return ret ;
  464. }
  465. static int spi_flash_2_acts_erase(const struct device *dev, off_t offset, size_t size)
  466. {
  467. struct spinor_info *sni = DEV_DATA(dev);
  468. int ret;
  469. k_mutex_lock(&flash_2_mutex, K_FOREVER);
  470. ret = spinor_4b_addr_op_api.erase(sni, offset, size);
  471. k_mutex_unlock(&flash_2_mutex);
  472. return ret ;
  473. }
  474. static int spi_flash_2_pwoer(bool on)
  475. {
  476. #if IS_ENABLED(CONFIG_SPI_FLASH_2_USE_GPIO_POWER)
  477. int ret;
  478. int gpio_value = CONFIG_SPI_FLASH_2_GPIO_POWER_LEVEL;
  479. const struct device *power_gpio_dev;
  480. uint8_t power_gpio = CONFIG_SPI_FLASH_2_POWER_GPIO % 32;
  481. power_gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(CONFIG_SPI_FLASH_2_POWER_GPIO));
  482. if (!power_gpio_dev) {
  483. LOG_ERR("Failed to bind nor power GPIO(%d:%s)", power_gpio, CONFIG_GPIO_PIN2NAME(CONFIG_SPI_FLASH_2_POWER_GPIO));
  484. return -1;
  485. }
  486. ret = gpio_pin_configure(power_gpio_dev, power_gpio, GPIO_OUTPUT);
  487. if (ret) {
  488. LOG_ERR("Failed to config output GPIO:%d", power_gpio);
  489. return ret;
  490. }
  491. if (on) {
  492. /* power on nor */
  493. gpio_pin_set(power_gpio_dev, power_gpio, gpio_value);
  494. } else {
  495. /* power off nor */
  496. gpio_pin_set(power_gpio_dev, power_gpio, !gpio_value);
  497. }
  498. #endif
  499. return 0;
  500. }
  501. static int spi_flash_2_acts_init(const struct device *dev)
  502. {
  503. struct spinor_info *sni = DEV_DATA(dev);
  504. uint8_t status, status2, status3;
  505. printk("spi3 flash init\n");
  506. spi_flash_2_pwoer(true);
  507. /* enable spi3 controller clock */
  508. acts_clock_peripheral_enable(CLOCK_ID_SPI3);
  509. /* reset spi3 controller */
  510. acts_reset_peripheral(RESET_ID_SPI3);
  511. /* setup SPI3 clock rate */
  512. clk_set_rate(CLOCK_ID_SPI3, MHZ(CONFIG_SPI_FLASH_2_FREQ_MHZ));
  513. sni->chipid = spinor_4b_addr_op_api.read_chipid(sni);
  514. printk("read spi3 nor chipid:0x%x\n", sni->chipid);
  515. status = spinor_4b_addr_op_api.read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  516. status2 = spinor_4b_addr_op_api.read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  517. status3 = spinor_4b_addr_op_api.read_status(sni, XSPI_NOR_CMD_READ_STATUS3);
  518. printk("spi3 nor status: {0x%02x 0x%02x 0x%02x}\n", status, status2, status3);
  519. if(sni->spi.bus_width == 4) {
  520. printk("data nor is 4 line mode\n");
  521. sni->spi.flag |= SPI_FLAG_SPI_4XIO;
  522. /* check QE bit */
  523. if (!(status2 & 0x2)) {
  524. /* set QE bit to disable HOLD/WP pin function, for WinBond */
  525. status2 |= 0x2;
  526. spinor_4b_addr_op_api.write_status(sni, XSPI_NOR_CMD_WRITE_STATUS2,
  527. (u8_t *)&status2, 1);
  528. }
  529. } else if(sni->spi.bus_width == 2) {
  530. printk("data nor is 2 line mode\n");
  531. } else {
  532. sni->spi.bus_width = 1;
  533. printk("data nor is 1 line mode\n");
  534. }
  535. /* check delay chain workable */
  536. sni->chipid = spinor_4b_addr_op_api.read_chipid(sni);
  537. printk("read again spi3 nor chipid:0x%x\n", sni->chipid);
  538. #if (CONFIG_SPI_FLASH_2_CHIP_SIZE >= 0x20000000)
  539. spinor_enter_4byte_address_mode(sni);
  540. #endif
  541. status = spinor_4b_addr_op_api.read_status(sni, XSPI_NOR_CMD_READ_STATUS);
  542. status2 = spinor_4b_addr_op_api.read_status(sni, XSPI_NOR_CMD_READ_STATUS2);
  543. status3 = spinor_4b_addr_op_api.read_status(sni, XSPI_NOR_CMD_READ_STATUS3);
  544. printk("spi3 nor status: {0x%02x 0x%02x 0x%02x}\n", status, status2, status3);
  545. #if IS_ENABLED(CONFIG_SPINOR_TEST_DELAYCHAIN)
  546. nor_test_delaychain(dev);
  547. #endif
  548. return 0;
  549. }
  550. static struct flash_driver_api spi_flash_2_nor_api = {
  551. .read = spi_flash_2_acts_read,
  552. .write = spi_flash_2_acts_write,
  553. .erase = spi_flash_2_acts_erase,
  554. #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)
  555. .page_layout = spi_flash_acts_pages_layout,
  556. #endif
  557. };
  558. static struct spinor_info spi_flash_2_acts_data = {
  559. .spi = {
  560. .base = SPI3_REG_BASE,
  561. .bus_width = CONFIG_SPI_FLASH_2_BUS_WIDTH,
  562. .delay_chain = CONFIG_SPI_FLASH_2_DELAY_CHAIN,
  563. #if (CONFIG_DMA_SPINOR_RESEVER_CHAN < CONFIG_DMA_0_PCHAN_NUM)
  564. .dma_base= (DMA_REG_BASE + 0x100 + (CONFIG_DMA_SPINOR_RESEVER_CHAN * 0x100)),
  565. #endif
  566. .flag = SPI_FLAG_NO_IRQ_LOCK,
  567. },
  568. .flag = 0,
  569. };
  570. static const struct spi_flash_acts_config spi_flash_2_acts_config = {
  571. #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)
  572. .pages_layout = {
  573. .pages_count = CONFIG_SPI_FLASH_2_CHIP_SIZE/0x1000,
  574. .pages_size = 0x1000,
  575. },
  576. #endif
  577. .chip_size = CONFIG_SPI_FLASH_2_CHIP_SIZE,
  578. .page_size = 0x1000,
  579. };
  580. DEVICE_DEFINE(spi_flash_2_acts, CONFIG_SPI_FLASH_2_NAME, &spi_flash_2_acts_init, NULL,
  581. &spi_flash_2_acts_data, &spi_flash_2_acts_config, POST_KERNEL,
  582. CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, &spi_flash_2_nor_api);
  583. #endif