inter.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef _SPINOR_INTER_H_
  2. #define _SPINOR_INTER_H_
  3. #include <sunxi_hal_common.h>
  4. #include <sunxi_hal_spi.h>
  5. #include <sunxi_hal_spinor.h>
  6. #include <hal_log.h>
  7. #define SPINOR_FMT(fmt) "spinor: "fmt
  8. #define SPINOR_ERR(fmt, arg...) hal_log_err(SPINOR_FMT(fmt), ##arg)
  9. #define SPINOR_WARN(fmt, arg...) hal_log_warn(SPINOR_FMT(fmt), ##arg)
  10. #define SPINOR_DEBUG(fmt, arg...) hal_log_debug(SPINOR_FMT(fmt), ##arg)
  11. #define SPINOR_INFO(fmt, arg...) hal_log_info(SPINOR_FMT(fmt), ##arg)
  12. #ifndef BIT
  13. #define BIT(x) (1UL << (x))
  14. #endif
  15. #ifndef MIN
  16. #define MIN(a, b) ((a) > (b) ? (b) : (a))
  17. #endif
  18. #ifndef MAX
  19. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  20. #endif
  21. #ifndef ARRAY_SIZE
  22. #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
  23. #endif
  24. #undef ALIGN
  25. #undef ALIGN_DOWN
  26. #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
  27. #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a))
  28. #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
  29. #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
  30. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  31. #define BITS_PER_BYTE 8
  32. #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
  33. #ifndef BITS_TO_LONGS
  34. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  35. #endif
  36. #define SZ_4K (4 * 1024)
  37. #define SZ_32K (32 * 1024)
  38. #define SZ_64K (64 * 1024)
  39. #define SZ_128K (128 * 1024)
  40. #define SZ_256K (256 * 1024)
  41. #define SZ_512K (512 * 1024)
  42. #define SZ_1M (1 * 1024 * 1024)
  43. #define SZ_2M (2 * 1024 * 1024)
  44. #define SZ_4M (4 * 1024 * 1024)
  45. #define SZ_8M (8 * 1024 * 1024)
  46. #define SZ_12M (12 * 1024 * 1024)
  47. #define SZ_14M (14 * 1024 * 1024)
  48. #define SZ_15M (15 * 1024 * 1024)
  49. #define SZ_15872K (15872 * 1024)
  50. #define SZ_16128K (16128 * 1024)
  51. #define SZ_16M (16 * 1024 * 1024)
  52. #define SZ_32M (32 * 1024 * 1024)
  53. #define SZ_64M (64 * 1024 * 1024)
  54. #define MAX_ID_LEN 3
  55. #define MAX_WAIT_LOOP ((unsigned int)(-1))
  56. #define NOR_DEFAULT_FREQUENCY 50
  57. #define NOR_PAGE_SIZE 256
  58. #define NOR_HALF_BLK_SIZE (SZ_32K)
  59. #define NOR_BLK_SIZE (SZ_64K)
  60. #define FACTORY_ZETTA 0xBA
  61. #define FACTORY_PUYA 0x85
  62. #define FACTORY_MXIC 0xC2
  63. #define FACTORY_GD 0xC8
  64. #define FACTORY_WINBOND 0xEF
  65. #define FACTORY_FM 0xA1
  66. #define FACTORY_ESMT 0x1C
  67. #define FACTORY_XTX 0x0B
  68. #define FACTORY_XMC 0x20
  69. struct nor_info
  70. {
  71. char *model;
  72. unsigned char id[MAX_ID_LEN];
  73. unsigned int total_size;
  74. int flag;
  75. #define SUPPORT_4K_ERASE_BLK BIT(0)
  76. #define SUPPORT_32K_ERASE_BLK BIT(1)
  77. #define SUPPORT_64K_ERASE_BLK BIT(2)
  78. #define SUPPORT_DUAL_READ BIT(3)
  79. #define SUPPORT_QUAD_READ BIT(4)
  80. #define SUPPORT_QUAD_WRITE BIT(5)
  81. #define SUPPORT_INDIVIDUAL_PROTECT BIT(6)
  82. #define SUPPORT_ALL_ERASE_BLK (SUPPORT_4K_ERASE_BLK | \
  83. SUPPORT_32K_ERASE_BLK | \
  84. SUPPORT_64K_ERASE_BLK)
  85. #define SUPPORT_GENERAL (SUPPORT_ALL_ERASE_BLK | \
  86. SUPPORT_QUAD_WRITE | \
  87. SUPPORT_QUAD_READ | \
  88. SUPPORT_DUAL_READ)
  89. #define USE_4K_ERASE BIT(20)
  90. #define USE_IO_PROG_X4 BIT(21)
  91. #define USE_IO_READ_X2 BIT(22)
  92. #define USE_IO_READ_X4 BIT(23)
  93. };
  94. struct nor_spi_master
  95. {
  96. hal_spi_master_port_t port;
  97. hal_spi_master_config_t cfg;
  98. };
  99. struct nor_flash
  100. {
  101. unsigned char cmd_read;
  102. unsigned char cmd_write;
  103. unsigned int r_cmd_slen: 3;
  104. unsigned int w_cmd_slen: 3;
  105. unsigned int total_size;
  106. unsigned int blk_size;
  107. unsigned int page_size;
  108. unsigned int addr_width;
  109. struct nor_spi_master spim;
  110. struct nor_info *info;
  111. struct nor_factory *factory;
  112. hal_sem_t hal_sem;
  113. };
  114. struct nor_factory {
  115. unsigned char factory;
  116. unsigned int idt_cnt;
  117. struct nor_info *idt;
  118. int (*init)(struct nor_flash *nor);
  119. void (*deinit)(struct nor_flash *nor);
  120. int (*init_lock)(struct nor_flash *nor);
  121. void (*deinit_lock)(struct nor_flash *nor);
  122. int (*lock)(struct nor_flash *nor, unsigned int addr, unsigned int len);
  123. int (*unlock)(struct nor_flash *nor, unsigned int addr, unsigned int len);
  124. bool (*islock)(struct nor_flash *nor, unsigned int addr, unsigned int len);
  125. int (*set_quad_mode)(struct nor_flash *nor);
  126. int (*set_4bytes_addr)(struct nor_flash *nor, int enable);
  127. struct nor_factory *next;
  128. };
  129. int nor_register_factory(struct nor_factory *f);
  130. int nor_register_factory_gd(void);
  131. int nor_register_factory_mxic(void);
  132. int nor_register_factory_winbond(void);
  133. int nor_register_factory_xtx(void);
  134. int nor_register_factory_esmt(void);
  135. int nor_register_factory_fm(void);
  136. int nor_register_factory_xmc(void);
  137. int nor_register_factory_puya(void);
  138. int nor_register_factory_zetta(void);
  139. int nor_transfer(int single_len, void *tbuf, int tlen, void *rbuf, int rlen);
  140. int nor_send_cmd(unsigned char cmd);
  141. int nor_read_status(unsigned char *sr);
  142. int nor_write_status(unsigned char *sr, unsigned int len);
  143. int nor_wait_ready(unsigned int ms, unsigned int times);
  144. int nor_write_enable(void);
  145. int nor_init(void);
  146. int nor_deinit(void);
  147. int nor_write(unsigned int addr, char *buf, unsigned int len);
  148. int nor_read(unsigned int addr, char *buf, unsigned int len);
  149. int nor_erase(unsigned int addr, unsigned int size);
  150. struct nor_flash *get_nor_flash(void);
  151. #ifdef CONFIG_DRIVERS_SPINOR_CACHE
  152. int nor_cache_init(struct nor_flash *nor);
  153. void nor_cache_exit(void);
  154. int nor_cache_read(unsigned int addr, char *buf, unsigned int len);
  155. int nor_cache_write(unsigned int addr, char *buf, unsigned int len);
  156. int nor_cache_sync(void);
  157. int nor_cache_erase(unsigned int addr, unsigned int len);
  158. #endif /* CONFIG_DRIVERS_SPINOR_CACHE */
  159. #ifdef CONFIG_DRIVERS_SPINOR_WRITE_LOCK
  160. int nor_wr_lock_init(struct nor_flash *nor);
  161. void nor_wr_lock_deinit(struct nor_flash *nor);
  162. int nor_wr_lock(struct nor_flash *nor, unsigned int addr, unsigned int len);
  163. int nor_wr_unlock(struct nor_flash *nor, unsigned int addr, unsigned int len);
  164. bool nor_wr_islock(struct nor_flash *nor, unsigned int addr, unsigned int len);
  165. int nor_wr_unlock_all(struct nor_flash *nor);
  166. int nor_wr_lock_all(struct nor_flash *nor);
  167. #endif /* CONFIG_DRIVERS_SPINOR_WRITE_LOCK */
  168. #ifdef CONFIG_DRIVERS_SPINOR_WRITE_LOCK
  169. #define is_wrlock_work(nor) (nor->info->flag & SUPPORT_INDIVIDUAL_PROTECT)
  170. #else
  171. #define is_wrlock_work(nor) (false)
  172. #endif
  173. #endif