flash_test_delaychain.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <drivers/flash.h>
  2. #include <drivers/spi.h>
  3. #include <logging/log.h>
  4. #include <soc.h>
  5. #include <board_cfg.h>
  6. #include "spi_flash.h"
  7. #ifdef CONFIG_SOC_LEOPARD
  8. #define DELAY_CHAIN_NUM 64
  9. #else
  10. #define DELAY_CHAIN_NUM 16
  11. #endif
  12. extern unsigned char spi_flash_set_delaytran(const struct device *dev, off_t offset, unsigned char delaytran);
  13. //static inline void test_setup_delaychain(struct spinor_info *sni, u8_t ns)
  14. //{
  15. //struct acts_spi_reg *spi= (struct acts_spi_reg *)sni->spi.base;
  16. //spi->ctrl = (spi->ctrl & ~(0xF << 16)) | (ns << 16);
  17. //volatile int i = 100000;
  18. //while (i--);
  19. //}
  20. #define TEST_READ_WRITE
  21. #ifdef TEST_READ_WRITE
  22. #if defined(CONFIG_SPI_FLASH_GPIO_2CS) && (CONFIG_SPI_FLASH_GPIO_2CS == 1)
  23. extern unsigned int nor_cs0_size;
  24. static u32_t TEST_ADDR = 0x200000;
  25. static u32_t TEST_ADDR_END = 0x400000;
  26. #else
  27. #define TEST_ADDR 0x200000
  28. #define TEST_ADDR_END 0x400000
  29. #endif
  30. #define TEST_SIZE (1024*4)
  31. static u32_t nor_test_buf[TEST_SIZE/4];
  32. static u32_t nor_test_start= 0x200000;
  33. __ramfunc int test_read_write_try(const struct device *dev, u8_t delay_chain)
  34. {
  35. int ret, i;
  36. struct spinor_info *sni = (struct spinor_info *)(dev)->data;
  37. spi_flash_set_delaytran(dev, TEST_ADDR, delay_chain);
  38. //sni->spi.delay_chain = delay_chain;
  39. if(nor_test_start < TEST_ADDR || nor_test_start >= TEST_ADDR_END)
  40. nor_test_start = TEST_ADDR;
  41. ret = flash_erase(dev, nor_test_start, TEST_SIZE);
  42. for(i = 0; i < TEST_SIZE/4; i++)
  43. nor_test_buf[i] = nor_test_start + i;
  44. ret = flash_write(dev, nor_test_start, nor_test_buf, TEST_SIZE);
  45. memset(nor_test_buf, 0 , TEST_SIZE);
  46. ret = flash_read(dev, nor_test_start, nor_test_buf, TEST_SIZE);
  47. if(ret){
  48. //test_setup_delaychain(sni, CONFIG_SPI_FLASH_DELAY_CHAIN);
  49. printk("read write fail =0x%x\n", nor_test_start);
  50. return -1;
  51. }
  52. for(i = 0; i < TEST_SIZE/4; i++){
  53. if( nor_test_buf[i] != nor_test_start + i){
  54. //test_setup_delaychain(sni, CONFIG_SPI_FLASH_DELAY_CHAIN);
  55. printk("of=%x, read 0x%x != 0x%x\n",nor_test_start, nor_test_start + i, nor_test_buf[i]);
  56. nor_test_start += TEST_SIZE;
  57. return -1;
  58. }
  59. }
  60. nor_test_start += TEST_SIZE;
  61. return 0;
  62. }
  63. #endif
  64. __ramfunc u32_t test_delaychain_read_id(const struct device *dev, u8_t delay_chain)
  65. {
  66. u32_t nor_id, mid;
  67. struct spinor_info *sni = (struct spinor_info *)(dev)->data;
  68. //sni->spi.delay_chain = delay_chain;
  69. spi_flash_set_delaytran(dev, TEST_ADDR, delay_chain);
  70. /* configure delay chain */
  71. //test_setup_delaychain(sni, sni->spi.delay_chain);
  72. //k_busy_wait(50);
  73. nor_id = p_spinor_api->read_chipid(sni) & 0xffffff;
  74. mid = nor_id & 0xff;
  75. if ((mid == 0xff) || (mid == 0x00))
  76. return 0;
  77. return nor_id;
  78. }
  79. __ramfunc s32_t test_delaychain_try(const struct device *dev, u8_t *ret_delaychain, u32_t chipid_ref)
  80. {
  81. u32_t i, try_delaychain;
  82. bool match_flag = 0;
  83. //u32_t nor_id_value_check;
  84. u32_t local_irq_save;
  85. local_irq_save = irq_lock();
  86. ret_delaychain[0] = 0;
  87. for (try_delaychain = 1; try_delaychain < DELAY_CHAIN_NUM; try_delaychain++) {
  88. match_flag = 1;
  89. printk("try_delaychain :%d\n", try_delaychain);
  90. soc_udelay(5000);
  91. #ifdef TEST_READ_WRITE
  92. for (i = 0; i < 2; i++) {
  93. if(test_read_write_try(dev, try_delaychain)){
  94. match_flag = 0;
  95. break;
  96. }
  97. }
  98. #else
  99. for (i = 0; i < 64; i++) {
  100. nor_id_value_check = test_delaychain_read_id(dev, try_delaychain);
  101. if (nor_id_value_check != chipid_ref) {
  102. printk("read:0x%x @ %d\n", nor_id_value_check, try_delaychain);
  103. match_flag = 0;
  104. break;
  105. }
  106. }
  107. #endif
  108. ret_delaychain[try_delaychain] = match_flag;
  109. }
  110. //test_delaychain_read_id(dev, CONFIG_SPI_FLASH_DELAY_CHAIN);
  111. irq_unlock(local_irq_save);
  112. return 0;
  113. }
  114. __ramfunc u32_t nor_read_chipid(const struct device *dev)
  115. {
  116. u32_t nor_id;
  117. u32_t local_irq_save;
  118. struct spinor_info *sni = (struct spinor_info *)(dev)->data;
  119. local_irq_save = irq_lock();
  120. nor_id = p_spinor_api->read_chipid(sni) & 0xffffff;
  121. irq_unlock(local_irq_save);
  122. return nor_id;
  123. }
  124. __ramfunc u8_t spinor_test_delaychain(const struct device *test_nor_dev)
  125. {
  126. u8_t ret_delaychain = 0;
  127. u8_t delaychain_flag[DELAY_CHAIN_NUM];
  128. u8_t delaychain_total[DELAY_CHAIN_NUM];
  129. u8_t start = 0, end, middle, i;
  130. u8_t expect_max_count_delay_chain = 0, max_count_delay_chain;
  131. //u32_t freq;
  132. u32_t chipid_ref;
  133. u8_t bak_delaytran;
  134. //struct device *test_nor_dev = device_get_binding(CONFIG_SPI_FLASH_NAME);
  135. struct spinor_info *sni = (struct spinor_info *)(test_nor_dev)->data;
  136. bak_delaytran = spi_flash_set_delaytran(test_nor_dev, TEST_ADDR, sni->spi.delay_chain);
  137. printk("spinor test delaychain start\n");
  138. chipid_ref = nor_read_chipid(test_nor_dev);
  139. printk("delaytran=0x%x, chipid = 0x%x\n", bak_delaytran, chipid_ref);
  140. memset(delaychain_total, 0x0, DELAY_CHAIN_NUM);
  141. //for (freq = 6; freq <= 222; freq += 6) {
  142. expect_max_count_delay_chain++;
  143. // soc_freq_set_cpu_clk(0, freq);
  144. // printk("set cpu freq : %d\n", freq);
  145. if (test_delaychain_try(test_nor_dev, delaychain_flag, chipid_ref) == 0) {
  146. for (i = 0; i < DELAY_CHAIN_NUM; i++)
  147. delaychain_total[i] += delaychain_flag[i];
  148. } else {
  149. printk("test_delaychain_try error!!\n");
  150. goto delay_chain_exit;
  151. }
  152. //}
  153. spi_flash_set_delaytran(test_nor_dev, TEST_ADDR, bak_delaytran);
  154. //sni->spi.delay_chain = bak_delaytran;
  155. printk("delaychain_total : ");
  156. for (i = 0; i < DELAY_CHAIN_NUM; i++)
  157. printk("%d,", delaychain_total[i]);
  158. printk("\n");
  159. max_count_delay_chain = 0;
  160. for (i = 0; i < DELAY_CHAIN_NUM; i++) {
  161. if (delaychain_total[i] > max_count_delay_chain)
  162. max_count_delay_chain = delaychain_total[i];
  163. }
  164. for (i = 0; i < DELAY_CHAIN_NUM; i++) {
  165. if (delaychain_total[i] == max_count_delay_chain) {
  166. start = i;
  167. break;
  168. }
  169. }
  170. end = start;
  171. for (i = start + 1; i < DELAY_CHAIN_NUM; i++) {
  172. if (delaychain_total[i] != max_count_delay_chain)
  173. break;
  174. end = i;
  175. }
  176. if (max_count_delay_chain < expect_max_count_delay_chain) {
  177. printk("test delaychain max count is %d, less then expect %d!!\n",
  178. max_count_delay_chain, expect_max_count_delay_chain);
  179. goto delay_chain_exit;
  180. }
  181. if ((end - start + 1) < 3) {
  182. printk("test delaychain only %d ok!! too less!!\n", end - start + 1);
  183. goto delay_chain_exit;
  184. }
  185. middle = (start + end) / 2;
  186. printk("test delaychain pass, best delaychain is : %d\n\n", middle);
  187. ret_delaychain = middle;
  188. delay_chain_exit:
  189. return ret_delaychain;
  190. }
  191. /*
  192. leopard: vdd < 1000 spi clk 64MHZ else 93MHZ
  193. */
  194. #define NUM_VDD_ITEM 4
  195. const uint16_t g_vdd_volt[NUM_VDD_ITEM] = {1200, 1100, 1000, 950};
  196. //const uint16_t g_vdd_volt[NUM_VDD_ITEM] = {1000, 950};
  197. __ramfunc int nor_test_delaychain(const struct device *dev)
  198. {
  199. int i;
  200. uint16_t vdd;
  201. //printk_dma_switch(0);
  202. printk("-----nor_test_delaychain =0x%x----\n", sys_read32(SPI1_DELAYCHAIN));
  203. sys_write32((sys_read32(SPI1_DELAYCHAIN)&(~0xf))|0x06, SPI1_DELAYCHAIN);
  204. soc_freq_set_cpu_clk(70,70);
  205. sys_write32(0, WD_CTL);
  206. #if 1
  207. for(i = 0; i < NUM_VDD_ITEM; i++){
  208. if(g_vdd_volt[i] == 950){
  209. printk("set spi0/spi1 clk 0.95V\n");
  210. sys_write32((sys_read32(SPI1_DELAYCHAIN)&(~0xf))|0x03, SPI1_DELAYCHAIN);
  211. clk_set_rate(CLOCK_ID_SPI1, MHZ(70) * 2);
  212. clk_set_rate(CLOCK_ID_SPI0, MHZ(64));
  213. }
  214. soc_pmu_set_vdd_voltage(g_vdd_volt[i]);
  215. soc_udelay(1000);
  216. printk("-----%d vdd set %d mv, try delaytran----\n", i, g_vdd_volt[i]);
  217. printk("%d, vdd=%d mv, delaychain : %d\n", i, g_vdd_volt[i], spinor_test_delaychain(dev));
  218. }
  219. #endif
  220. #if defined(CONFIG_SPI_FLASH_GPIO_2CS) && (CONFIG_SPI_FLASH_GPIO_2CS == 1)
  221. for(i = NUM_VDD_ITEM-1; i >= 0; i--){
  222. vdd = g_vdd_volt[i];
  223. soc_pmu_set_vdd_voltage(vdd);
  224. soc_udelay(1000);
  225. if(g_vdd_volt[i] == 1000){
  226. printk("set spi0/spi1 clk 1.0V\n");
  227. sys_write32((sys_read32(SPI1_DELAYCHAIN)&(~0xf))|0x06, SPI1_DELAYCHAIN);
  228. clk_set_rate(CLOCK_ID_SPI1, MHZ(140) * 2);
  229. clk_set_rate(CLOCK_ID_SPI0, MHZ(100));
  230. }
  231. }
  232. printk("\n\n---------------try cs1 nor-----------------\n\n");
  233. TEST_ADDR = 0x200000+nor_cs0_size;
  234. TEST_ADDR_END = 0x400000+nor_cs0_size;
  235. nor_test_start= TEST_ADDR;
  236. for(i = 0; i < NUM_VDD_ITEM; i++){
  237. if(g_vdd_volt[i] == 950){
  238. printk("set spi0/spi1 clk 0.95V\n");
  239. sys_write32((sys_read32(SPI1_DELAYCHAIN)&(~0xf))|0x03, SPI1_DELAYCHAIN);
  240. clk_set_rate(CLOCK_ID_SPI1, MHZ(70) * 2);
  241. clk_set_rate(CLOCK_ID_SPI0, MHZ(64));
  242. }
  243. soc_pmu_set_vdd_voltage(g_vdd_volt[i]);
  244. soc_udelay(1000);
  245. printk("--start cs1---%d vdd set %d mv, try delaytran----\n", i, g_vdd_volt[i]);
  246. printk("--end cs1 %d, vdd=%d mv, best delaychain : %d\n\n", i, g_vdd_volt[i], spinor_test_delaychain(dev));
  247. }
  248. #endif
  249. printk("\n ----nor_test_delaychain-- finshed-----\n");
  250. while(1);
  251. return 0;
  252. }