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