diff --git a/cpu/esp_common/periph/flash.c b/cpu/esp_common/periph/flash.c index 16b5d2de90dc3..a0f1d06599da9 100644 --- a/cpu/esp_common/periph/flash.c +++ b/cpu/esp_common/periph/flash.c @@ -83,7 +83,6 @@ extern uint32_t spi_flash_get_id(void); /* forward declaration of mtd functions */ static int _flash_init(mtd_dev_t *dev); static int _flash_read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size); -static int _flash_write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size); static int _flash_write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset, uint32_t size); static int _flash_erase(mtd_dev_t *dev, uint32_t addr, uint32_t size); @@ -130,7 +129,6 @@ void spi_flash_drive_init(void) _flash_driver.init = &_flash_init; _flash_driver.read = &_flash_read; - _flash_driver.write = &_flash_write; _flash_driver.write_page = &_flash_write_page; _flash_driver.erase = &_flash_erase; _flash_driver.power = &_flash_power; @@ -311,24 +309,6 @@ static int _flash_read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size) return (spi_flash_read(_flash_beg + addr, buff, size) == ESP_OK) ? 0 : -EIO; } -static int _flash_write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) -{ - DEBUG("%s dev=%p addr=%08"PRIx32" size=%"PRIu32" buf=%p\n", - __func__, dev, addr, size, buff); - - CHECK_PARAM_RET(dev == &_flash_dev, -ENODEV); - CHECK_PARAM_RET(buff != NULL, -ENOTSUP); - - /* size must be within the flash address space */ - CHECK_PARAM_RET(_flash_beg + addr + size <= _flash_end, -EOVERFLOW); - - /* addr + size must be within a page */ - CHECK_PARAM_RET(size <= _flashchip->page_size, -EOVERFLOW); - CHECK_PARAM_RET((addr % _flashchip->page_size) + size <= _flashchip->page_size, -EOVERFLOW); - - return (spi_flash_write(_flash_beg + addr, buff, size) == ESP_OK) ? 0 : -EIO; -} - static int _flash_write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset, uint32_t size) { diff --git a/cpu/native/mtd/mtd_native.c b/cpu/native/mtd/mtd_native.c index 31cd75fe69acf..672cdad3a5bbd 100644 --- a/cpu/native/mtd/mtd_native.c +++ b/cpu/native/mtd/mtd_native.c @@ -78,35 +78,6 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size) return (nread == size) ? 0 : -EIO; } -static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) -{ - mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev; - size_t mtd_size = dev->sector_count * dev->pages_per_sector * dev->page_size; - - DEBUG("mtd_native: write from 0x%" PRIx32 " count %" PRIu32 "\n", addr, size); - - if (addr + size > mtd_size) { - return -EOVERFLOW; - } - if (((addr % dev->page_size) + size) > dev->page_size) { - return -EOVERFLOW; - } - - FILE *f = real_fopen(_dev->fname, "r+"); - if (!f) { - return -EIO; - } - real_fseek(f, addr, SEEK_SET); - for (size_t i = 0; i < size; i++) { - uint8_t c = real_fgetc(f); - real_fseek(f, -1, SEEK_CUR); - real_fputc(c & ((uint8_t*)buff)[i], f); - } - real_fclose(f); - - return 0; -} - static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset, uint32_t size) { @@ -181,7 +152,6 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power) const mtd_desc_t native_flash_driver = { .read = _read, .power = _power, - .write = _write, .write_page = _write_page, .erase = _erase, .init = _init, diff --git a/drivers/at25xxx/mtd/mtd.c b/drivers/at25xxx/mtd/mtd.c index 4f96cb3470de9..8fda94b428022 100644 --- a/drivers/at25xxx/mtd/mtd.c +++ b/drivers/at25xxx/mtd/mtd.c @@ -51,13 +51,6 @@ static int mtd_at25xxx_read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t return at25xxx_read(mtd_at25xxx_->at25xxx_eeprom, addr, buff, size); } -static int mtd_at25xxx_write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) -{ - DEBUG("[mtd_at25xxx] write: addr:%" PRIu32 " size:%" PRIu32 "\n", addr, size); - mtd_at25xxx_t *mtd_at25xxx_ = (mtd_at25xxx_t*)dev; - return at25xxx_write(mtd_at25xxx_->at25xxx_eeprom, addr, buff, size); -} - static int mtd_at25xxx_write_page(mtd_dev_t *dev, const void *src, uint32_t page, uint32_t offset, uint32_t size) { @@ -87,7 +80,6 @@ static int mtd_at25xxx_power(mtd_dev_t *dev, enum mtd_power_state power) const mtd_desc_t mtd_at25xxx_driver = { .init = mtd_at25xxx_init, .read = mtd_at25xxx_read, - .write = mtd_at25xxx_write, .write_page = mtd_at25xxx_write_page, .erase = mtd_at25xxx_erase, .power = mtd_at25xxx_power, diff --git a/drivers/include/mtd.h b/drivers/include/mtd.h index d4e02abcddff6..8aab06fcb09f3 100644 --- a/drivers/include/mtd.h +++ b/drivers/include/mtd.h @@ -196,25 +196,6 @@ struct mtd_desc { uint32_t offset, uint32_t size); - /** - * @brief Write to the Memory Technology Device (MTD) - * - * @p addr + @p size must be inside a page boundary. @p addr can be anywhere - * but the buffer cannot overlap two pages. - * - * @param[in] dev Pointer to the selected driver - * @param[in] buff Pointer to the data to be written - * @param[in] addr Starting address - * @param[in] size Number of bytes - * - * @return 0 on success - * @return < 0 value on error - */ - int (*write)(mtd_dev_t *dev, - const void *buff, - uint32_t addr, - uint32_t size); - /** * @brief Write to the Memory Technology Device (MTD) using * pagewise addressing. diff --git a/drivers/mtd/mtd.c b/drivers/mtd/mtd.c index ac4570bdc94c9..dd26a3443ccd4 100644 --- a/drivers/mtd/mtd.c +++ b/drivers/mtd/mtd.c @@ -165,14 +165,6 @@ int mtd_write(mtd_dev_t *mtd, const void *src, uint32_t addr, uint32_t count) return -ENODEV; } - if (out_of_bounds(mtd, 0, addr, count)) { - return -EOVERFLOW; - } - - if (mtd->driver->write) { - return mtd->driver->write(mtd, src, addr, count); - } - /* page size is always a power of two */ const uint32_t page_shift = bitarithm_msb(mtd->page_size); const uint32_t page_mask = mtd->page_size - 1; @@ -292,15 +284,6 @@ int mtd_write_page_raw(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t return -EOVERFLOW; } - if (mtd->driver->write_page == NULL) { - /* TODO: remove when all backends implement write_page */ - if (mtd->driver->write) { - return mtd->driver->write(mtd, src, mtd->page_size * page + offset, count); - } else { - return -ENOTSUP; - } - } - /* Implementation assumes page size is <= INT_MAX and a power of two. */ /* We didn't find hardware yet where this is not true. */ assert(mtd->page_size <= INT_MAX); diff --git a/drivers/mtd_mapper/mtd_mapper.c b/drivers/mtd_mapper/mtd_mapper.c index 6cf67418d9a19..68fa5bd0caa89 100644 --- a/drivers/mtd_mapper/mtd_mapper.c +++ b/drivers/mtd_mapper/mtd_mapper.c @@ -102,21 +102,6 @@ static int _init(mtd_dev_t *mtd) return res; } -static int _write(mtd_dev_t *mtd, const void *src, uint32_t addr, - uint32_t count) -{ - mtd_mapper_region_t *region = container_of(mtd, mtd_mapper_region_t, mtd); - - if (addr + count > _region_size(region)) { - return -EOVERFLOW; - } - - _lock(region); - int res = mtd_write(region->parent->mtd, src, addr + _byte_offset(region), count); - _unlock(region); - return res; -} - static int _write_page(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t offset, uint32_t count) { @@ -199,7 +184,6 @@ const mtd_desc_t mtd_mapper_driver = { .init = _init, .read = _read, .read_page = _read_page, - .write = _write, .write_page = _write_page, .erase = _erase, .erase_sector = _erase_sector, diff --git a/drivers/mtd_sdcard/mtd_sdcard.c b/drivers/mtd_sdcard/mtd_sdcard.c index 39136723dd1df..9a57baac4b817 100644 --- a/drivers/mtd_sdcard/mtd_sdcard.c +++ b/drivers/mtd_sdcard/mtd_sdcard.c @@ -198,25 +198,10 @@ static int mtd_sdcard_read(mtd_dev_t *dev, void *buff, uint32_t addr, return -EOVERFLOW; } -static int mtd_sdcard_write(mtd_dev_t *dev, const void *buff, uint32_t addr, - uint32_t size) -{ - int res = mtd_sdcard_write_page(dev, buff, addr / SD_HC_BLOCK_SIZE, - addr % SD_HC_BLOCK_SIZE, size); - if (res < 0) { - return res; - } - if (res == (int)size) { - return 0; - } - return -EOVERFLOW; -} - const mtd_desc_t mtd_sdcard_driver = { .init = mtd_sdcard_init, .read = mtd_sdcard_read, .read_page = mtd_sdcard_read_page, - .write = mtd_sdcard_write, .write_page = mtd_sdcard_write_page, .erase_sector = mtd_sdcard_erase_sector, .power = mtd_sdcard_power, diff --git a/drivers/mtd_spi_nor/mtd_spi_nor.c b/drivers/mtd_spi_nor/mtd_spi_nor.c index e0b1ff6c01961..b405b1f5075b5 100644 --- a/drivers/mtd_spi_nor/mtd_spi_nor.c +++ b/drivers/mtd_spi_nor/mtd_spi_nor.c @@ -612,45 +612,6 @@ static int mtd_spi_nor_read(mtd_dev_t *mtd, void *dest, uint32_t addr, uint32_t return 0; } -static int mtd_spi_nor_write(mtd_dev_t *mtd, const void *src, uint32_t addr, uint32_t size) -{ - uint32_t total_size = mtd->page_size * mtd->pages_per_sector * mtd->sector_count; - - DEBUG("mtd_spi_nor_write: %p, %p, 0x%" PRIx32 ", 0x%" PRIx32 "\n", - (void *)mtd, src, addr, size); - if (size == 0) { - return 0; - } - const mtd_spi_nor_t *dev = (mtd_spi_nor_t *)mtd; - if (size > mtd->page_size) { - DEBUG("mtd_spi_nor_write: ERR: page program >1 page (%" PRIu32 ")!\n", mtd->page_size); - return -EOVERFLOW; - } - if (dev->page_addr_mask && - ((addr & dev->page_addr_mask) != ((addr + size - 1) & dev->page_addr_mask))) { - DEBUG("mtd_spi_nor_write: ERR: page program spans page boundary!\n"); - return -EOVERFLOW; - } - if (addr + size > total_size) { - return -EOVERFLOW; - } - - mtd_spi_acquire(dev); - - /* write enable */ - mtd_spi_cmd(dev, dev->params->opcode->wren); - - /* Page program */ - mtd_spi_cmd_addr_write(dev, dev->params->opcode->page_program, addr, src, size); - - /* waiting for the command to complete before returning */ - wait_for_write_complete(dev, 0); - - mtd_spi_release(dev); - - return 0; -} - static int mtd_spi_nor_write_page(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t offset, uint32_t size) { @@ -759,7 +720,6 @@ static int mtd_spi_nor_erase(mtd_dev_t *mtd, uint32_t addr, uint32_t size) const mtd_desc_t mtd_spi_nor_driver = { .init = mtd_spi_nor_init, .read = mtd_spi_nor_read, - .write = mtd_spi_nor_write, .write_page = mtd_spi_nor_write_page, .erase = mtd_spi_nor_erase, .power = mtd_spi_nor_power, diff --git a/tests/mtd_mapper/main.c b/tests/mtd_mapper/main.c index e1717fe41524e..12394b54e153b 100644 --- a/tests/mtd_mapper/main.c +++ b/tests/mtd_mapper/main.c @@ -91,22 +91,6 @@ static int _read_page(mtd_dev_t *dev, void *buff, uint32_t page, uint32_t offset return size; } -static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, - uint32_t size) -{ - (void)dev; - - if (addr + size > sizeof(_dummy_memory)) { - return -EOVERFLOW; - } - if (size > PAGE_SIZE) { - return -EOVERFLOW; - } - memcpy(_dummy_memory + addr, buff, size); - - return 0; -} - static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset, uint32_t size) { uint32_t addr = page * dev->page_size + offset; @@ -168,7 +152,6 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power) static const mtd_desc_t driver = { .init = _init, .read = _read, - .write = _write, .erase = _erase, .power = _power, .read_page = _read_page, diff --git a/tests/pkg_littlefs/main.c b/tests/pkg_littlefs/main.c index 1586148ba578d..7ba888e555620 100644 --- a/tests/pkg_littlefs/main.c +++ b/tests/pkg_littlefs/main.c @@ -62,19 +62,14 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size) return 0; } -static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) +static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, + uint32_t offset, uint32_t size) { - (void)dev; + uint32_t addr = page * dev->page_size + offset; - if (addr + size > sizeof(dummy_memory)) { - return -EOVERFLOW; - } - if (size > PAGE_SIZE) { - return -EOVERFLOW; - } memcpy(dummy_memory + addr, buff, size); - return 0; + return size; } static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size) @@ -105,7 +100,7 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power) static const mtd_desc_t driver = { .init = _init, .read = _read, - .write = _write, + .write_page = _write_page, .erase = _erase, .power = _power, }; diff --git a/tests/pkg_littlefs2/main.c b/tests/pkg_littlefs2/main.c index df33d559fb83c..715508a176c33 100644 --- a/tests/pkg_littlefs2/main.c +++ b/tests/pkg_littlefs2/main.c @@ -62,19 +62,14 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size) return 0; } -static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) +static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, + uint32_t offset, uint32_t size) { - (void)dev; + uint32_t addr = page * dev->page_size + offset; - if (addr + size > sizeof(dummy_memory)) { - return -EOVERFLOW; - } - if (size > PAGE_SIZE) { - return -EOVERFLOW; - } memcpy(dummy_memory + addr, buff, size); - return 0; + return size; } static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size) @@ -105,7 +100,7 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power) static const mtd_desc_t driver = { .init = _init, .read = _read, - .write = _write, + .write_page = _write_page, .erase = _erase, .power = _power, }; diff --git a/tests/pkg_spiffs/main.c b/tests/pkg_spiffs/main.c index e66a383f9fa04..e5a7f7c5e062f 100644 --- a/tests/pkg_spiffs/main.c +++ b/tests/pkg_spiffs/main.c @@ -59,19 +59,14 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size) return 0; } -static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) +static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, + uint32_t offset, uint32_t size) { - (void)dev; + uint32_t addr = page * dev->page_size + offset; - if (addr + size > sizeof(dummy_memory)) { - return -EOVERFLOW; - } - if (size > PAGE_SIZE) { - return -EOVERFLOW; - } memcpy(dummy_memory + addr, buff, size); - return 0; + return size; } static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size) @@ -102,7 +97,7 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power) static const mtd_desc_t driver = { .init = _init, .read = _read, - .write = _write, + .write_page = _write_page, .erase = _erase, .power = _power, }; diff --git a/tests/unittests/tests-mtd/tests-mtd.c b/tests/unittests/tests-mtd/tests-mtd.c index 23174f2c43e9f..3442635e1a8c3 100644 --- a/tests/unittests/tests-mtd/tests-mtd.c +++ b/tests/unittests/tests-mtd/tests-mtd.c @@ -65,19 +65,14 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size) return 0; } -static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size) +static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, + uint32_t offset, uint32_t size) { - (void)dev; + uint32_t addr = page * dev->page_size + offset; - if (addr + size > sizeof(dummy_memory)) { - return -EOVERFLOW; - } - if (((addr % PAGE_SIZE) + size) > PAGE_SIZE) { - return -EOVERFLOW; - } memcpy(dummy_memory + addr, buff, size); - return 0; + return size; } static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size) @@ -108,7 +103,7 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power) static const mtd_desc_t driver = { .init = _init, .read = _read, - .write = _write, + .write_page = _write_page, .erase = _erase, .power = _power, }; @@ -222,7 +217,7 @@ static void test_mtd_write_read(void) uint8_t buf_page[page_size + 1]; memset(buf_page, 1, sizeof(buf_page)); ret = mtd_write(dev, buf_page, 0, sizeof(buf_page)); - TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret); + TEST_ASSERT_EQUAL_INT(0, ret); /* Read more than one page */ ret = mtd_erase(dev, 0, dev->page_size * dev->pages_per_sector); @@ -239,9 +234,9 @@ static void test_mtd_write_read(void) /* pages overlap write */ ret = mtd_write(dev, buf, dev->page_size - (sizeof(buf) / 2), sizeof(buf)); - TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret); + TEST_ASSERT_EQUAL_INT(0, ret); ret = mtd_write(dev, buf_page, 1, sizeof(buf_page) - 1); - TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret); + TEST_ASSERT_EQUAL_INT(0, ret); } #ifdef MTD_0