Skip to content

Commit 4ff79d0

Browse files
committed
feat(spi_nand_flash): add support for Micron MT29F2G chips
1 parent ddfc93b commit 4ff79d0

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

spi_nand_flash/src/dhara_glue.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ int dhara_nand_is_bad(const struct dhara_nand *n, dhara_block_t b)
6868

6969
ESP_GOTO_ON_ERROR(read_page_and_wait(dev, first_block_page, NULL), fail, TAG, "");
7070

71+
uint16_t column_addr = dev->page_size;
72+
column_addr += 4096 * (b % 2);
7173
// Read the first 2 bytes on the OOB of the first page in the block. This should be 0xFFFF for a good block
72-
ESP_GOTO_ON_ERROR(spi_nand_read(dev->config.device_handle, (uint8_t *) &bad_block_indicator, dev->page_size, 2),
74+
ESP_GOTO_ON_ERROR(spi_nand_read(dev->config.device_handle, (uint8_t *) &bad_block_indicator, column_addr, 2),
7375
fail, TAG, "");
7476

7577
ESP_LOGD(TAG, "is_bad, block=%"PRIu32", page=%"PRIu32",indicator = %04x", b, first_block_page, bad_block_indicator);
@@ -138,13 +140,15 @@ int dhara_nand_prog(const struct dhara_nand *n, dhara_page_t p, const uint8_t *d
138140
esp_err_t ret;
139141
uint8_t status;
140142
uint16_t used_marker = 0;
143+
uint32_t column_addr = 0;
144+
column_addr += 4096 * ((p / (1 << dev->dhara_nand.log2_ppb)) % 2);
141145

142146
ESP_GOTO_ON_ERROR(read_page_and_wait(dev, p, NULL), fail, TAG, "");
143147
ESP_GOTO_ON_ERROR(spi_nand_write_enable(dev->config.device_handle), fail, TAG, "");
144-
ESP_GOTO_ON_ERROR(spi_nand_program_load(dev->config.device_handle, data, 0, dev->page_size),
148+
ESP_GOTO_ON_ERROR(spi_nand_program_load(dev->config.device_handle, data, column_addr, dev->page_size),
145149
fail, TAG, "");
146150
ESP_GOTO_ON_ERROR(spi_nand_program_load(dev->config.device_handle, (uint8_t *)&used_marker,
147-
dev->page_size + 2, 2),
151+
column_addr + dev->page_size + 2, 2),
148152
fail, TAG, "");
149153
ESP_GOTO_ON_ERROR(program_execute_and_wait(dev, p, &status), fail, TAG, "");
150154

@@ -167,8 +171,11 @@ int dhara_nand_is_free(const struct dhara_nand *n, dhara_page_t p)
167171
uint16_t used_marker;
168172

169173
ESP_GOTO_ON_ERROR(read_page_and_wait(dev, p, NULL), fail, TAG, "");
174+
uint16_t column_addr = dev->page_size + 2;
175+
column_addr += 4096 * ((p / (1 << dev->dhara_nand.log2_ppb)) % 2);
176+
170177
ESP_GOTO_ON_ERROR(spi_nand_read(dev->config.device_handle, (uint8_t *)&used_marker,
171-
dev->page_size + 2, 2),
178+
column_addr, 2),
172179
fail, TAG, "");
173180

174181
ESP_LOGD(TAG, "is free, page=%"PRIu32", used_marker=%04x,", p, used_marker);
@@ -200,7 +207,10 @@ int dhara_nand_read(const struct dhara_nand *n, dhara_page_t p, size_t offset, s
200207
return -1;
201208
}
202209

203-
ESP_GOTO_ON_ERROR(spi_nand_read(dev->config.device_handle, data, offset, length), fail, TAG, "");
210+
uint16_t column_addr = offset;
211+
column_addr += 4096 * ((p / (1 << dev->dhara_nand.log2_ppb)) % 2);
212+
213+
ESP_GOTO_ON_ERROR(spi_nand_read(dev->config.device_handle, data, column_addr, length), fail, TAG, "");
204214

205215
return 0;
206216
fail:

spi_nand_flash/src/nand.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ static esp_err_t spi_nand_micron_init(spi_nand_flash_device_t *dev)
148148
dev->dhara_nand.log2_ppb = 6; // 64 pages per block
149149
dev->dhara_nand.log2_page_size = 12; // 4096 bytes per page
150150
break;
151+
case MICRON_DI_24:
152+
dev->dhara_nand.num_blocks = 2048;
153+
dev->dhara_nand.log2_ppb = 6; // 64 pages per block
154+
dev->dhara_nand.log2_page_size = 11; // 2048 bytes per page
155+
break;
151156
default:
152157
return ESP_ERR_INVALID_RESPONSE;
153158
}
@@ -276,7 +281,7 @@ esp_err_t spi_nand_erase_chip(spi_nand_flash_device_t *handle)
276281

277282
esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, dhara_sector_t sector_id)
278283
{
279-
dhara_error_t err;
284+
dhara_error_t err = 0;
280285
esp_err_t ret = ESP_OK;
281286

282287
xSemaphoreTake(handle->mutex, portMAX_DELAY);
@@ -296,7 +301,7 @@ esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *b
296301

297302
esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, dhara_sector_t sector_id)
298303
{
299-
dhara_error_t err;
304+
dhara_error_t err = 0;
300305
esp_err_t ret = ESP_OK;
301306

302307
xSemaphoreTake(handle->mutex, portMAX_DELAY);
@@ -311,7 +316,7 @@ esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uin
311316

312317
esp_err_t spi_nand_flash_sync(spi_nand_flash_device_t *handle)
313318
{
314-
dhara_error_t err;
319+
dhara_error_t err = 0;
315320
esp_err_t ret = ESP_OK;
316321

317322
xSemaphoreTake(handle->mutex, portMAX_DELAY);

spi_nand_flash/src/nand_flash_devices.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@
4242
#define WINBOND_DI_BC21 0xBC21
4343

4444
#define MICRON_DI_34 0x34
45+
#define MICRON_DI_24 0x24 // MT29F2G

0 commit comments

Comments
 (0)