summaryrefslogtreecommitdiff
path: root/firmware/target/mips/ingenic_x1000/nand-x1000.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/mips/ingenic_x1000/nand-x1000.c')
-rw-r--r--firmware/target/mips/ingenic_x1000/nand-x1000.c41
1 files changed, 20 insertions, 21 deletions
diff --git a/firmware/target/mips/ingenic_x1000/nand-x1000.c b/firmware/target/mips/ingenic_x1000/nand-x1000.c
index 382fd761b3..c740623d86 100644
--- a/firmware/target/mips/ingenic_x1000/nand-x1000.c
+++ b/firmware/target/mips/ingenic_x1000/nand-x1000.c
@@ -24,7 +24,7 @@
24#include "system.h" 24#include "system.h"
25#include <string.h> 25#include <string.h>
26 26
27const nand_chip supported_nand_chips[] = { 27const struct nand_chip supported_nand_chips[] = {
28#if defined(FIIO_M3K) || defined(SHANLING_Q1) || defined(EROS_QN) 28#if defined(FIIO_M3K) || defined(SHANLING_Q1) || defined(EROS_QN)
29 { 29 {
30 /* ATO25D1GA */ 30 /* ATO25D1GA */
@@ -51,14 +51,13 @@ const nand_chip supported_nand_chips[] = {
51#endif 51#endif
52}; 52};
53 53
54const size_t nr_supported_nand_chips = 54const size_t nr_supported_nand_chips = ARRAYLEN(supported_nand_chips);
55 sizeof(supported_nand_chips) / sizeof(nand_chip);
56 55
57static nand_drv static_nand_drv; 56static struct nand_drv static_nand_drv;
58static uint8_t static_scratch_buf[NAND_DRV_SCRATCHSIZE] CACHEALIGN_ATTR; 57static uint8_t static_scratch_buf[NAND_DRV_SCRATCHSIZE] CACHEALIGN_ATTR;
59static uint8_t static_page_buf[NAND_DRV_MAXPAGESIZE] CACHEALIGN_ATTR; 58static uint8_t static_page_buf[NAND_DRV_MAXPAGESIZE] CACHEALIGN_ATTR;
60 59
61nand_drv* nand_init(void) 60struct nand_drv* nand_init(void)
62{ 61{
63 static bool inited = false; 62 static bool inited = false;
64 if(!inited) { 63 if(!inited) {
@@ -71,19 +70,19 @@ nand_drv* nand_init(void)
71 return &static_nand_drv; 70 return &static_nand_drv;
72} 71}
73 72
74static uint8_t nand_get_reg(nand_drv* drv, uint8_t reg) 73static uint8_t nand_get_reg(struct nand_drv* drv, uint8_t reg)
75{ 74{
76 sfc_exec(NANDCMD_GET_FEATURE, reg, drv->scratch_buf, 1|SFC_READ); 75 sfc_exec(NANDCMD_GET_FEATURE, reg, drv->scratch_buf, 1|SFC_READ);
77 return drv->scratch_buf[0]; 76 return drv->scratch_buf[0];
78} 77}
79 78
80static void nand_set_reg(nand_drv* drv, uint8_t reg, uint8_t val) 79static void nand_set_reg(struct nand_drv* drv, uint8_t reg, uint8_t val)
81{ 80{
82 drv->scratch_buf[0] = val; 81 drv->scratch_buf[0] = val;
83 sfc_exec(NANDCMD_SET_FEATURE, reg, drv->scratch_buf, 1|SFC_WRITE); 82 sfc_exec(NANDCMD_SET_FEATURE, reg, drv->scratch_buf, 1|SFC_WRITE);
84} 83}
85 84
86static void nand_upd_reg(nand_drv* drv, uint8_t reg, uint8_t msk, uint8_t val) 85static void nand_upd_reg(struct nand_drv* drv, uint8_t reg, uint8_t msk, uint8_t val)
87{ 86{
88 uint8_t x = nand_get_reg(drv, reg); 87 uint8_t x = nand_get_reg(drv, reg);
89 x &= ~msk; 88 x &= ~msk;
@@ -91,7 +90,7 @@ static void nand_upd_reg(nand_drv* drv, uint8_t reg, uint8_t msk, uint8_t val)
91 nand_set_reg(drv, reg, x); 90 nand_set_reg(drv, reg, x);
92} 91}
93 92
94static bool identify_chip(nand_drv* drv) 93static bool identify_chip(struct nand_drv* drv)
95{ 94{
96 /* Read ID command has some variations; Linux handles these 3: 95 /* Read ID command has some variations; Linux handles these 3:
97 * - no address or dummy bytes 96 * - no address or dummy bytes
@@ -106,7 +105,7 @@ static bool identify_chip(nand_drv* drv)
106 drv->dev_id2 = drv->scratch_buf[2]; 105 drv->dev_id2 = drv->scratch_buf[2];
107 106
108 for(size_t i = 0; i < nr_supported_nand_chips; ++i) { 107 for(size_t i = 0; i < nr_supported_nand_chips; ++i) {
109 const nand_chip* chip = &supported_nand_chips[i]; 108 const struct nand_chip* chip = &supported_nand_chips[i];
110 if(chip->mf_id != drv->mf_id || chip->dev_id != drv->dev_id) 109 if(chip->mf_id != drv->mf_id || chip->dev_id != drv->dev_id)
111 continue; 110 continue;
112 111
@@ -121,13 +120,13 @@ static bool identify_chip(nand_drv* drv)
121 return false; 120 return false;
122} 121}
123 122
124static void setup_chip_data(nand_drv* drv) 123static void setup_chip_data(struct nand_drv* drv)
125{ 124{
126 drv->ppb = 1 << drv->chip->log2_ppb; 125 drv->ppb = 1 << drv->chip->log2_ppb;
127 drv->fpage_size = drv->chip->page_size + drv->chip->oob_size; 126 drv->fpage_size = drv->chip->page_size + drv->chip->oob_size;
128} 127}
129 128
130static void setup_chip_commands(nand_drv* drv) 129static void setup_chip_commands(struct nand_drv* drv)
131{ 130{
132 /* Select commands appropriate for the chip */ 131 /* Select commands appropriate for the chip */
133 drv->cmd_page_read = NANDCMD_PAGE_READ(drv->chip->row_cycles); 132 drv->cmd_page_read = NANDCMD_PAGE_READ(drv->chip->row_cycles);
@@ -143,7 +142,7 @@ static void setup_chip_commands(nand_drv* drv)
143 } 142 }
144} 143}
145 144
146static void setup_chip_registers(nand_drv* drv) 145static void setup_chip_registers(struct nand_drv* drv)
147{ 146{
148 /* Set chip registers to enter normal operation */ 147 /* Set chip registers to enter normal operation */
149 if(drv->chip->flags & NAND_CHIPFLAG_HAS_QE_BIT) { 148 if(drv->chip->flags & NAND_CHIPFLAG_HAS_QE_BIT) {
@@ -159,7 +158,7 @@ static void setup_chip_registers(nand_drv* drv)
159 nand_set_reg(drv, FREG_PROT, FREG_PROT_UNLOCK); 158 nand_set_reg(drv, FREG_PROT, FREG_PROT_UNLOCK);
160} 159}
161 160
162int nand_open(nand_drv* drv) 161int nand_open(struct nand_drv* drv)
163{ 162{
164 if(drv->refcount > 0) { 163 if(drv->refcount > 0) {
165 drv->refcount++; 164 drv->refcount++;
@@ -193,7 +192,7 @@ int nand_open(nand_drv* drv)
193 return NAND_SUCCESS; 192 return NAND_SUCCESS;
194} 193}
195 194
196void nand_close(nand_drv* drv) 195void nand_close(struct nand_drv* drv)
197{ 196{
198 --drv->refcount; 197 --drv->refcount;
199 if(drv->refcount > 0) 198 if(drv->refcount > 0)
@@ -207,7 +206,7 @@ void nand_close(nand_drv* drv)
207 sfc_close(); 206 sfc_close();
208} 207}
209 208
210static uint8_t nand_wait_busy(nand_drv* drv) 209static uint8_t nand_wait_busy(struct nand_drv* drv)
211{ 210{
212 uint8_t reg; 211 uint8_t reg;
213 do { 212 do {
@@ -216,7 +215,7 @@ static uint8_t nand_wait_busy(nand_drv* drv)
216 return reg; 215 return reg;
217} 216}
218 217
219int nand_block_erase(nand_drv* drv, nand_block_t block) 218int nand_block_erase(struct nand_drv* drv, nand_block_t block)
220{ 219{
221 sfc_exec(NANDCMD_WR_EN, 0, NULL, 0); 220 sfc_exec(NANDCMD_WR_EN, 0, NULL, 0);
222 sfc_exec(drv->cmd_block_erase, block, NULL, 0); 221 sfc_exec(drv->cmd_block_erase, block, NULL, 0);
@@ -228,7 +227,7 @@ int nand_block_erase(nand_drv* drv, nand_block_t block)
228 return NAND_SUCCESS; 227 return NAND_SUCCESS;
229} 228}
230 229
231int nand_page_program(nand_drv* drv, nand_page_t page, const void* buffer) 230int nand_page_program(struct nand_drv* drv, nand_page_t page, const void* buffer)
232{ 231{
233 sfc_exec(NANDCMD_WR_EN, 0, NULL, 0); 232 sfc_exec(NANDCMD_WR_EN, 0, NULL, 0);
234 sfc_exec(drv->cmd_program_load, 0, (void*)buffer, drv->fpage_size|SFC_WRITE); 233 sfc_exec(drv->cmd_program_load, 0, (void*)buffer, drv->fpage_size|SFC_WRITE);
@@ -241,7 +240,7 @@ int nand_page_program(nand_drv* drv, nand_page_t page, const void* buffer)
241 return NAND_SUCCESS; 240 return NAND_SUCCESS;
242} 241}
243 242
244int nand_page_read(nand_drv* drv, nand_page_t page, void* buffer) 243int nand_page_read(struct nand_drv* drv, nand_page_t page, void* buffer)
245{ 244{
246 sfc_exec(drv->cmd_page_read, page, NULL, 0); 245 sfc_exec(drv->cmd_page_read, page, NULL, 0);
247 nand_wait_busy(drv); 246 nand_wait_busy(drv);
@@ -249,7 +248,7 @@ int nand_page_read(nand_drv* drv, nand_page_t page, void* buffer)
249 return NAND_SUCCESS; 248 return NAND_SUCCESS;
250} 249}
251 250
252int nand_read_bytes(nand_drv* drv, uint32_t byte_addr, uint32_t byte_len, void* buffer) 251int nand_read_bytes(struct nand_drv* drv, uint32_t byte_addr, uint32_t byte_len, void* buffer)
253{ 252{
254 if(byte_len == 0) 253 if(byte_len == 0)
255 return NAND_SUCCESS; 254 return NAND_SUCCESS;
@@ -277,7 +276,7 @@ int nand_read_bytes(nand_drv* drv, uint32_t byte_addr, uint32_t byte_len, void*
277 return NAND_SUCCESS; 276 return NAND_SUCCESS;
278} 277}
279 278
280int nand_write_bytes(nand_drv* drv, uint32_t byte_addr, uint32_t byte_len, const void* buffer) 279int nand_write_bytes(struct nand_drv* drv, uint32_t byte_addr, uint32_t byte_len, const void* buffer)
281{ 280{
282 if(byte_len == 0) 281 if(byte_len == 0)
283 return NAND_SUCCESS; 282 return NAND_SUCCESS;