drv_sfc_gd25qxx_mtd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * File : drv_sfc_gd25qxx_mtd.c
  3. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017Äê4ÔÂ19ÈÕ Urey the first version
  8. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <drivers/mtd_nor.h>
  13. #include "board.h"
  14. #include "drv_clock.h"
  15. #include "drv_gpio.h"
  16. #include "drv_sfc.h"
  17. /* JEDEC Manufacturer's ID */
  18. #define MF_ID (0xC8)
  19. /* JEDEC Device ID: Memory type and Capacity */
  20. #define MTC_GD25Q128 (0x4018)
  21. #define MTC_GD25Q256 (0x4019)
  22. /* RT-Thread MTD device interface */
  23. static rt_base_t mtd_gd25_read_id(struct rt_mtd_nor_device *device)
  24. {
  25. struct sfc_flash *flash = (struct sfc_flash *)device;
  26. return (rt_uint32_t)flash->id;
  27. }
  28. static rt_size_t mtd_gd25_read(struct rt_mtd_nor_device *device, rt_off_t position, rt_uint8_t *data, rt_size_t size)
  29. {
  30. struct sfc_flash *flash = (struct sfc_flash *)device;
  31. return sfc_norflash_read(flash,position,data,size);
  32. }
  33. static rt_size_t mtd_gd25_write(struct rt_mtd_nor_device *device, rt_off_t position, const rt_uint8_t *data, rt_size_t size)
  34. {
  35. struct sfc_flash *flash = (struct sfc_flash *)device;
  36. return sfc_norflash_write(flash,position,data,size);
  37. }
  38. static rt_err_t mtd_gd25_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length)
  39. {
  40. struct sfc_flash *flash = (struct sfc_flash *)device;
  41. sfc_norflash_erase_sector(flash,offset);
  42. return RT_EOK;
  43. }
  44. const static struct rt_mtd_nor_driver_ops mtd_gd25_ops =
  45. {
  46. mtd_gd25_read_id,
  47. mtd_gd25_read,
  48. mtd_gd25_write,
  49. mtd_gd25_erase_block,
  50. };
  51. #ifdef SFC_USE_QUAD
  52. struct sfc_quad_mode flash_quad_mode[] =
  53. {
  54. {
  55. .RDSR_CMD = CMD_RDSR_1,
  56. .WRSR_CMD = CMD_WRSR_1,
  57. .RDSR_DATE = 0x2,//the data is write the spi status register for QE bit
  58. .RD_DATE_SIZE = 1,
  59. .WRSR_DATE = 0x2,//this bit should be the flash QUAD mode enable
  60. .WD_DATE_SIZE = 1,
  61. .cmd_read = CMD_QUAD_READ,//
  62. .sfc_mode = TRAN_SPI_QUAD,
  63. },
  64. {
  65. .RDSR_CMD = CMD_RDSR,
  66. .WRSR_CMD = CMD_WRSR,
  67. .RDSR_DATE = 0x40,//the data is write the spi status register for QE bit
  68. .RD_DATE_SIZE = 1,
  69. .WRSR_DATE = 0x40,//this bit should be the flash QUAD mode enable
  70. .WD_DATE_SIZE = 1,
  71. .cmd_read = CMD_QUAD_IO_FAST_READ,
  72. .sfc_mode = TRAN_SPI_IO_QUAD,
  73. },
  74. {
  75. .RDSR_CMD = CMD_RDSR_1,
  76. .WRSR_CMD = CMD_WRSR,
  77. .RDSR_DATE = 0x20,//the data is write the spi status register for QE bit
  78. .RD_DATE_SIZE = 1,
  79. .WRSR_DATE = 0x200,//this bit should be the flash QUAD mode enable
  80. .WD_DATE_SIZE = 2,
  81. .cmd_read = CMD_QUAD_READ,
  82. .sfc_mode = TRAN_SPI_QUAD,
  83. },
  84. {
  85. .RDSR_CMD = CMD_RDSR,
  86. .WRSR_CMD = CMD_WRSR,
  87. .RDSR_DATE = 0x40,//the data is write the spi status register for QE bit
  88. .RD_DATE_SIZE = 1,
  89. .WRSR_DATE = 0x40,//this bit should be the flash QUAD mode enable
  90. .WD_DATE_SIZE = 1,
  91. .cmd_read = CMD_QUAD_READ,
  92. .sfc_mode = TRAN_SPI_QUAD,
  93. },
  94. };
  95. #endif
  96. static struct sfc_flash _gd25_flash_info =
  97. {
  98. .name = "GD25Q128C",
  99. .id = 0xc84018,
  100. .pagesize = 256,
  101. .sectorsize = ( 4 * 1024),
  102. .chipsize = (16 * 1024 * 1024),
  103. .erasesize = ( 4 * 1024),
  104. .writesize = 256,
  105. .addrsize = DEFAULT_ADDRSIZE,
  106. .quad_mode = &flash_quad_mode[0]
  107. };
  108. static char flashIdStr[128];
  109. extern int rt_hw_gd25qxx_mtd_part_init(const char *mtd_name);
  110. int rt_hw_gd25qxx_init(void)
  111. {
  112. struct sfc_flash *flash = &_gd25_flash_info;
  113. int result;
  114. result = sfc_norflash_probe(flash);
  115. if(result != RT_EOK)
  116. {
  117. rt_kprintf("GD25 init Failed..\n");
  118. return result;
  119. }
  120. if((flash->id >> 16) != MF_ID)
  121. {
  122. rt_kprintf("Manufacturers ID error!\r\n");
  123. rt_kprintf("JEDEC Read-ID Data : %06X\r\n", flash->id);
  124. return -RT_ENOSYS;
  125. }
  126. switch (flash->id & 0xFFFF)
  127. {
  128. case MTC_GD25Q128:
  129. flash->name = "GD25Q128C";
  130. flash->chipsize = (16 * 1024 * 1024);
  131. flash->addrsize = 3;
  132. flash->quad_mode = &flash_quad_mode[0];
  133. break;
  134. case MTC_GD25Q256:
  135. flash->name = "GD25Q256C";
  136. flash->chipsize = (32 * 1024 * 1024);
  137. flash->addrsize = 4;
  138. flash->quad_mode = &flash_quad_mode[3];
  139. /* enable 4-byte addressing if the device exceeds 16MiB */
  140. sfc_norflash_set_addr_width_4byte(flash,1);
  141. break;
  142. default:
  143. rt_kprintf("Memory Capacity error!\r\n");
  144. return -RT_ENOSYS;
  145. break;
  146. }
  147. //format FLASH UUID...
  148. {
  149. int strSize,i;
  150. strSize = rt_snprintf(flashIdStr + 0,sizeof(flashIdStr) - 0,"%06X",flash->id);
  151. for(i=0;i<8;i++)
  152. strSize += rt_snprintf(flashIdStr + strSize,sizeof(flashIdStr) - strSize,"%02X",flash->uid[i]);
  153. flashIdStr[strSize] = '\0';
  154. }
  155. /* Init device interface ... */
  156. flash->mtd.block_size = flash->erasesize;
  157. flash->mtd.block_start = 0;
  158. flash->mtd.block_end = flash->chipsize / flash->erasesize;
  159. flash->mtd.ops = &mtd_gd25_ops;
  160. rt_mtd_nor_register_device("gd25mtd",&flash->mtd);
  161. rt_hw_gd25qxx_mtd_part_init("gd25mtd");
  162. return RT_EOK;
  163. }
  164. INIT_DEVICE_EXPORT(rt_hw_gd25qxx_init);