[5/5] mtd: spi: Switch to MTD uclass (absolute UCLASS_SPI_FLASH)

Message ID 20200709111709.68904-6-jagan@amarulasolutions.com
State New
Headers show
Series
  • mtd: Implement MTD UCLASS (use SPINOR)
Related show

Commit Message

Jagan Teki July 9, 2020, 11:17 a.m. UTC
Why MTD UCLASS switch?

From a nutshell, all the memory-related flash devices like
Parallel NOR, Raw NAND, SPI NOR/NAND stacks should be part
of the MTD layer.

The same theory applies to incase of MTD UCLASS in U-Boot.

Unfortunately, no flash drivers are using the MTD UCLASS in the
fully functional driver model. There are several reasons behind
it like
- MTD code always copies/sync from Linux to support the latest
  flash features.
- So no special interest to touch since flash code is much bigger
  than usual and a lot of features are tested with existing code.
- No real taker to implement it.

So, this patch is trying to move further to use a common flash
interface layer called MTD in the driver model way.

As initial support it supporting spinor flash device drivers would
be written as UCLASS_MTD.

What happened to UCLASS_SPI_FLASH, why it is absolute now?

Due to the effect of Linux MTD/SPI_NOR sync the existing spi_flash
the structure has been affected such a way that it was dead and
typedefs to spi_nor.

What happened with this patch then?

With this patch, the UCLASS_SPI_FLASH will be replaced with
UCLASS_MTD by using dm-driven mtd_ops.

absolute link:
sf.c => sf uclass => sf driver => spi-nor-core
- flash operations via dm_spi_flash_ops

updated link:
sf.c => mtd uclass => mtd driver => spi-nor-core
- flash operations via mtd_ops (dm variant)

Future work?

- cleanup of spi_flash with spinor
- renaming of CONFIG option changes
- implement common mtd probe
- implement mtd flash interface types so that the respective
  commands like sf, mtd can make use of dm-mtd in the proper way.

Signed-off-by: Jagan Teki <jagan@amarulasolutions.com>
---
 arch/arm/mach-rockchip/spl-boot-order.c |   4 +-
 arch/x86/cpu/apollolake/spl.c           |   8 +-
 arch/x86/lib/fsp2/fsp_init.c            |   2 +-
 arch/x86/lib/fsp2/fsp_support.c         |   7 +-
 arch/x86/lib/mrccache.c                 |   8 +-
 board/atmel/common/mac-spi-nor.c        |   4 +-
 drivers/mtd/spi/Kconfig                 |   2 +
 drivers/mtd/spi/sandbox.c               |   2 +-
 drivers/mtd/spi/sf-uclass.c             | 106 ++++++------------------
 drivers/mtd/spi/sf_dataflash.c          |  25 +++---
 include/dm/uclass-id.h                  |   1 -
 include/spi_flash.h                     |  53 +-----------
 test/dm/sf.c                            |  12 +--
 13 files changed, 71 insertions(+), 163 deletions(-)

Comments

'Krzysztof Kozlowski' via Amarula Linux July 13, 2020, 9 a.m. UTC | #1
On 09/07/20 4:47 pm, Jagan Teki wrote:
[...]
On 09/07/20 4:47 pm, Jagan Teki wrote:
> diff --git a/arch/x86/cpu/apollolake/spl.c b/arch/x86/cpu/apollolake/spl.c
> index e1ee1e0624..9c80440bbb 100644
> --- a/arch/x86/cpu/apollolake/spl.c
> +++ b/arch/x86/cpu/apollolake/spl.c
> @@ -10,6 +10,7 @@
>  #include <image.h>
>  #include <log.h>
>  #include <malloc.h>
> +#include <mtd.h>
>  #include <spi.h>
>  #include <spl.h>
>  #include <spi_flash.h>
> @@ -41,7 +42,7 @@ static int rom_load_image(struct spl_image_info
*spl_image,
>  	debug("Reading from mapped SPI %lx, size %lx", spl_pos, spl_size);
>
>  	if (CONFIG_IS_ENABLED(SPI_FLASH_SUPPORT)) {
> -		ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
> +		ret = uclass_find_first_device(UCLASS_MTD, &dev);

You have to be careful here (and rest of places in this series where
uclass_find_first_device() is used) as the first MTD device need not be
first SPI FLASH device. RAW Nand and parallel flash drivers also
register MTD devices. Eg.: Above change will be wrong, if apollolake has
both SPI FLASH and NAND/Parallel NORs and Parallel NOR binds as first
UCLASS_MTD driver.


>  		if (ret)
>  			return log_msg_ret("spi_flash", ret);
>  		if (!dev)
> @@ -77,7 +78,7 @@ static int spl_fast_spi_load_image(struct
spl_image_info *spl_image,
>  	struct udevice *dev;
>  	int ret;
>
> -	ret = uclass_first_device_err(UCLASS_SPI_FLASH, &dev);
> +	ret = uclass_first_device_err(UCLASS_MTD, &dev);
>  	if (ret)
>  		return ret;
[...]

> diff --git a/drivers/mtd/spi/sf-uclass.c b/drivers/mtd/spi/sf-uclass.c
> index 44cdb3151d..9cab08f49b 100644
> --- a/drivers/mtd/spi/sf-uclass.c
> +++ b/drivers/mtd/spi/sf-uclass.c
> @@ -7,6 +7,7 @@
>  #include <dm.h>
>  #include <log.h>
>  #include <malloc.h>
> +#include <mtd.h>
>  #include <spi.h>
>  #include <spi_flash.h>
>  #include <dm/device-internal.h>
> @@ -14,22 +15,6 @@
>  
>  DECLARE_GLOBAL_DATA_PTR;
>  
> -int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
> -{
> -	return log_ret(sf_get_ops(dev)->read(dev, offset, len, buf));
> -}
> -
> -int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
> -		       const void *buf)
> -{
> -	return log_ret(sf_get_ops(dev)->write(dev, offset, len, buf));
> -}
> -
> -int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
> -{
> -	return log_ret(sf_get_ops(dev)->erase(dev, offset, len));
> -}
> -
>  void spi_flash_free(struct spi_flash *flash)
>  {
>  	device_remove(flash->spi->dev, DM_REMOVE_NORMAL);
> @@ -57,108 +42,78 @@ struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
>  	str = strdup(name);
>  #endif
>  	ret = spi_get_bus_and_cs(bus, cs, max_hz, spi_mode,
> -				 "spi_flash_std", str, &bus_dev, &slave);
> +				  "spi_flash_std", str, &bus_dev, &slave);

Unintended whitespace change?

>  	if (ret)
>  		return NULL;
>  
> -	return dev_get_uclass_priv(slave->dev);
> +	return dev_get_priv(slave->dev);
>  }
>  
> -static int spi_flash_post_bind(struct udevice *dev)
> +static int spi_flash_std_read(struct udevice *dev, loff_t from, size_t len,
> +			      u_char *buf)
>  {
> -#if defined(CONFIG_NEEDS_MANUAL_RELOC)
> -	struct dm_spi_flash_ops *ops = sf_get_ops(dev);
> -	static int reloc_done;
> -
> -	if (!reloc_done) {
> -		if (ops->read)
> -			ops->read += gd->reloc_off;
> -		if (ops->write)
> -			ops->write += gd->reloc_off;
> -		if (ops->erase)
> -			ops->erase += gd->reloc_off;
> -
> -		reloc_done++;
> -	}
> -#endif
> -	return 0;
> -}
> -
> -static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
> -			      void *buf)
> -{
> -	struct spi_flash *flash = dev_get_uclass_priv(dev);
> +	struct spi_flash *flash = dev_get_priv(dev);
>  	struct mtd_info *mtd = &flash->mtd;
>  	size_t retlen;
>  
> -	return log_ret(mtd->_read(mtd, offset, len, &retlen, buf));
> +	return log_ret(mtd->_read(mtd, from, len, &retlen, buf));
>  }
>  
> -static int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
> -			       const void *buf)
> +static int spi_flash_std_write(struct udevice *dev, loff_t to, size_t len,
> +			       const u_char *buf)
>  {
> -	struct spi_flash *flash = dev_get_uclass_priv(dev);
> +	struct spi_flash *flash = dev_get_priv(dev);
>  	struct mtd_info *mtd = &flash->mtd;
>  	size_t retlen;
>  
> -	return mtd->_write(mtd, offset, len, &retlen, buf);
> +	return mtd->_write(mtd, to, len, &retlen, buf);
>  }
>  
> -static int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
> +static int spi_flash_std_erase(struct udevice *dev, struct erase_info *instr)
>  {
> -	struct spi_flash *flash = dev_get_uclass_priv(dev);
> +	struct spi_flash *flash = dev_get_priv(dev);
>  	struct mtd_info *mtd = &flash->mtd;
> -	struct erase_info instr;
>  
> -	if (offset % mtd->erasesize || len % mtd->erasesize) {
> -		printf("SF: Erase offset/length not multiple of erase size\n");
> +	if (instr->addr % mtd->erasesize || instr->len % mtd->erasesize) {
> +		dev_err(dev, "Erase offset/length not multiple of erasesize\n");
>  		return -EINVAL;
>  	}


Should this check be moved to mtd_derase() wrapper?

>  
> -	memset(&instr, 0, sizeof(instr));
> -	instr.addr = offset;
> -	instr.len = len;
> -
> -	return mtd->_erase(mtd, &instr);
> +	return mtd->_erase(mtd, instr);
>  }
>  
>  static int spi_flash_std_probe(struct udevice *dev)
>  {
> +	struct spi_flash *flash = dev_get_priv(dev);
>  	struct spi_slave *slave = dev_get_parent_priv(dev);
> -	struct spi_flash *flash;
>  	int ret;
>  
> -	if (!slave) {
> -		printf("SF: Failed to set up slave\n");
> -		return -ENODEV;
> -	}
> -
> -	flash = dev_get_uclass_priv(dev);
>  	flash->dev = dev;
>  	flash->spi = slave;
>  
> -	/* Claim spi bus */
>  	ret = spi_claim_bus(slave);
>  	if (ret) {
> -		debug("SF: Failed to claim SPI bus: %d\n", ret);
> +		dev_err(dev, "failed to claim bus (ret=%d)\n", ret);
>  		return ret;
>  	}
>  
>  	ret = spi_nor_scan(flash);
> -	if (ret)
> -		goto err_read_id;
> +	if (ret) {
> +		dev_err(dev, "failed to scan spinor (ret=%d)\n", ret);
> +		goto err_claim_bus;
> +	}
>  
> -	if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
> +	if (IS_ENABLED(CONFIG_SPI_FLASH_MTD))
>  		ret = spi_flash_mtd_register(flash);
>  
> -err_read_id:
> +err_claim_bus:
>  	spi_release_bus(slave);
>  	return ret;
>  }
>  
>  static int spi_flash_std_remove(struct udevice *dev)
>  {
> -	if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
> +	if (IS_ENABLED(CONFIG_SPI_FLASH_MTD))
>  		spi_flash_mtd_unregister();
> 
>  	return 0;
> @@ -190,7 +145,7 @@ static int spi_flash_std_bind(struct udevice *dev)
>  	return 0;
>  }
>  
> -static const struct dm_spi_flash_ops spi_flash_std_ops = {
> +static const struct mtd_ops spi_flash_std_ops = {
>  	.read = spi_flash_std_read,
>  	.write = spi_flash_std_write,
>  	.erase = spi_flash_std_erase,
> @@ -203,7 +158,7 @@ static const struct udevice_id spi_flash_std_ids[] = {
>  
>  U_BOOT_DRIVER(spi_flash_std) = {
>  	.name		= "spi_flash_std",
> -	.id		= UCLASS_SPI_FLASH,
> +	.id		= UCLASS_MTD,
>  	.of_match	= spi_flash_std_ids,
>  	.probe		= spi_flash_std_probe,
>  	.bind		= spi_flash_std_bind,
> @@ -211,10 +166,3 @@ U_BOOT_DRIVER(spi_flash_std) = {
>  	.priv_auto_alloc_size = sizeof(struct spi_flash),
>  	.ops		= &spi_flash_std_ops,
>  };
> -
> -UCLASS_DRIVER(spi_flash) = {
> -	.id		= UCLASS_SPI_FLASH,
> -	.name		= "spi_flash",
> -	.post_bind	= spi_flash_post_bind,
> -	.per_device_auto_alloc_size = sizeof(struct spi_flash),
> -};
> diff --git a/drivers/mtd/spi/sf_dataflash.c b/drivers/mtd/spi/sf_dataflash.c
> index 27d847d421..5c73b54cbc 100644
> --- a/drivers/mtd/spi/sf_dataflash.c
> +++ b/drivers/mtd/spi/sf_dataflash.c
> @@ -12,6 +12,7 @@
>  #include <fdtdec.h>
>  #include <flash.h>
>  #include <log.h>
> +#include <mtd.h>
>  #include <spi.h>
>  #include <spi_flash.h>
>  #include <div64.h>
> @@ -117,7 +118,7 @@ static int dataflash_waitready(struct spi_slave *spi)
>  }
>  
>  /* Erase pages of flash */
> -static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
> +static int spi_dataflash_erase(struct udevice *dev, struct erase_info *instr)
>  {
>  	struct dataflash	*dataflash;
>  	struct spi_flash	*spi_flash;
> @@ -125,6 +126,8 @@ static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
>  	unsigned		blocksize;
>  	uint8_t			*command;
>  	uint32_t		rem;
> +	loff_t			offset = instr->addr;
> +	size_t			len = instr->len;
>  	int			status;
>  
>  	dataflash = dev_get_priv(dev);
> @@ -136,7 +139,7 @@ static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
>  	memset(dataflash->command, 0 , sizeof(dataflash->command));
>  	command = dataflash->command;
>  
> -	debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
> +	debug("%s: erase addr=0x%llx len 0x%x\n", dev->name, offset, len);
>  
>  	div_u64_rem(len, spi_flash->page_size, &rem);
>  	if (rem) {
> @@ -146,7 +149,7 @@ static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
>  	}
>  	div_u64_rem(offset, spi_flash->page_size, &rem);
>  	if (rem) {
> -		printf("%s: offset(0x%x) isn't the multiple of page size(0x%x)\n",
> +		printf("%s: offset(0x%llx) isn't the multiple of page size(0x%x)\n",
>  		       dev->name, offset, spi_flash->page_size);
>  		return -EINVAL;
>  	}
> @@ -210,8 +213,8 @@ static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
>   *   len    : Amount to read
>   *   buf    : Buffer containing the data
>   */
> -static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
> -			      void *buf)
> +static int spi_dataflash_read(struct udevice *dev, loff_t offset, size_t len,
> +			      u_char *buf)
>  {
>  	struct dataflash	*dataflash;
>  	struct spi_flash	*spi_flash;
> @@ -227,7 +230,7 @@ static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
>  	memset(dataflash->command, 0 , sizeof(dataflash->command));
>  	command = dataflash->command;
>  
> -	debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
> +	debug("%s: erase addr=0x%llx len 0x%x\n", dev->name, offset, len);
>  	debug("READ: (%x) %x %x %x\n",
>  	      command[0], command[1], command[2], command[3]);
>  
> @@ -266,8 +269,8 @@ static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
>   *   len    : Amount to write
>   *   buf    : Buffer containing the data
>   */
> -int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
> -			const void *buf)
> +int spi_dataflash_write(struct udevice *dev, loff_t offset, size_t len,
> +			const u_char *buf)
>  {
>  	struct dataflash	*dataflash;
>  	struct spi_flash	*spi_flash;
> @@ -285,7 +288,7 @@ int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
>  	memset(dataflash->command, 0 , sizeof(dataflash->command));
>  	command = dataflash->command;
>  
> -	debug("%s: write 0x%x..0x%x\n", dev->name, offset, (offset + len));
> +	debug("%s: write 0x%llx..0x%llx\n", dev->name, offset, (offset + len));
>  
>  	pageaddr = ((unsigned)offset / spi_flash->page_size);
>  	to = ((unsigned)offset % spi_flash->page_size);
> @@ -676,7 +679,7 @@ err_jedec_probe:
>  	return status;
>  }
>  
> -static const struct dm_spi_flash_ops spi_dataflash_ops = {
> +static const struct mtd_ops spi_dataflash_ops = {
>  	.read = spi_dataflash_read,
>  	.write = spi_dataflash_write,
>  	.erase = spi_dataflash_erase,
> @@ -690,7 +693,7 @@ static const struct udevice_id spi_dataflash_ids[] = {
>  
>  U_BOOT_DRIVER(spi_dataflash) = {
>  	.name		= "spi_dataflash",
> -	.id		= UCLASS_SPI_FLASH,
> +	.id		= UCLASS_MTD,
>  	.of_match	= spi_dataflash_ids,
>  	.probe		= spi_dataflash_probe,
>  	.priv_auto_alloc_size = sizeof(struct dataflash),
> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> index 7837d459f1..1f56f7f48a 100644
> --- a/include/dm/uclass-id.h
> +++ b/include/dm/uclass-id.h
> @@ -99,7 +99,6 @@ enum uclass_id {
>  	UCLASS_SMEM,		/* Shared memory interface */
>  	UCLASS_SOUND,		/* Playing simple sounds */
>  	UCLASS_SPI,		/* SPI bus */
> -	UCLASS_SPI_FLASH,	/* SPI flash */
>  	UCLASS_SPI_GENERIC,	/* Generic SPI flash target */
>  	UCLASS_SPMI,		/* System Power Management Interface bus */
>  	UCLASS_SYSCON,		/* System configuration device */
> diff --git a/include/spi_flash.h b/include/spi_flash.h
> index 24759944eb..f4852fc5c1 100644
> --- a/include/spi_flash.h
> +++ b/include/spi_flash.h
> @@ -10,6 +10,7 @@
>  #define _SPI_FLASH_H_
>  
>  #include <dm.h>	/* Because we dereference struct udevice here */
> +#include <mtd.h>
>  #include <linux/types.h>
>  #include <linux/mtd/spi-nor.h>
>  
> @@ -27,70 +28,24 @@
>  # define CONFIG_ENV_SPI_MODE	CONFIG_SF_DEFAULT_MODE
>  #endif
>  
> -struct spi_slave;
> -
> -struct dm_spi_flash_ops {
> -	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
> -	int (*write)(struct udevice *dev, u32 offset, size_t len,
> -		     const void *buf);
> -	int (*erase)(struct udevice *dev, u32 offset, size_t len);
> -};
> -
> -/* Access the serial operations for a device */
> -#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
> -
>  #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
> -/**
> - * spi_flash_read_dm() - Read data from SPI flash
> - *
> - * @dev:	SPI flash device
> - * @offset:	Offset into device in bytes to read from
> - * @len:	Number of bytes to read
> - * @buf:	Buffer to put the data that is read
> - * @return 0 if OK, -ve on error
> - */
> -int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
> -
> -/**
> - * spi_flash_write_dm() - Write data to SPI flash
> - *
> - * @dev:	SPI flash device
> - * @offset:	Offset into device in bytes to write to
> - * @len:	Number of bytes to write
> - * @buf:	Buffer containing bytes to write
> - * @return 0 if OK, -ve on error
> - */
> -int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
> -		       const void *buf);
> -
> -/**
> - * spi_flash_erase_dm() - Erase blocks of the SPI flash
> - *
> - * Note that @len must be a muiltiple of the flash sector size.
> - *
> - * @dev:	SPI flash device
> - * @offset:	Offset into device in bytes to start erasing
> - * @len:	Number of bytes to erase
> - * @return 0 if OK, -ve on error
> - */
> -int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
>  
>  static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
>  				 size_t len, void *buf)
>  {
> -	return spi_flash_read_dm(flash->dev, offset, len, buf);
> +	return mtd_dread(flash->dev, offset, len, buf);
>  }
>  
>  static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
>  				  size_t len, const void *buf)
>  {
> -	return spi_flash_write_dm(flash->dev, offset, len, buf);
> +	return mtd_dwrite(flash->dev, offset, len, buf);
>  }
>  
>  static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
>  				  size_t len)
>  {
> -	return spi_flash_erase_dm(flash->dev, offset, len);
> +	return mtd_derase(flash->dev, offset, len);
>  }
>  
>  struct sandbox_state;
> diff --git a/test/dm/sf.c b/test/dm/sf.c
> index 9e7dead684..a675923dc5 100644
> --- a/test/dm/sf.c
> +++ b/test/dm/sf.c
> @@ -31,23 +31,23 @@ static int dm_test_spi_flash(struct unit_test_state *uts)
>  
>  	src = map_sysmem(0x20000, full_size);
>  	ut_assertok(os_write_file("spi.bin", src, full_size));
> -	ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
> +	ut_assertok(uclass_first_device_err(UCLASS_MTD, &dev));
>  
>  	dst = map_sysmem(0x20000 + full_size, full_size);
> -	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
> +	ut_assertok(mtd_dread(dev, 0, size, dst));
>  	ut_asserteq_mem(src, dst, size);
>  
>  	/* Erase */
> -	ut_assertok(spi_flash_erase_dm(dev, 0, size));
> -	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
> +	ut_assertok(mtd_derase(dev, 0, size));
> +	ut_assertok(mtd_dread(dev, 0, size, dst));
>  	for (i = 0; i < size; i++)
>  		ut_asserteq(dst[i], 0xff);
>  
>  	/* Write some new data */
>  	for (i = 0; i < size; i++)
>  		src[i] = i;
> -	ut_assertok(spi_flash_write_dm(dev, 0, size, src));
> -	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
> +	ut_assertok(mtd_dwrite(dev, 0, size, src));
> +	ut_assertok(mtd_dread(dev, 0, size, dst));
>  	ut_asserteq_mem(src, dst, size);
>  
>  	/* Check mapping */
> -- 2.25.1

Patch

diff --git a/arch/arm/mach-rockchip/spl-boot-order.c b/arch/arm/mach-rockchip/spl-boot-order.c
index 94673f34c9..64a745d189 100644
--- a/arch/arm/mach-rockchip/spl-boot-order.c
+++ b/arch/arm/mach-rockchip/spl-boot-order.c
@@ -62,7 +62,7 @@  static int spl_node_to_boot_device(int node)
 		default:
 			return -ENOSYS;
 		}
-	} else if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node,
+	} else if (!uclass_get_device_by_of_offset(UCLASS_MTD, node,
 		&parent)) {
 		return BOOT_DEVICE_SPI;
 	}
@@ -73,7 +73,7 @@  static int spl_node_to_boot_device(int node)
 	 * extended with awareness of the BLK layer (and matching OF_CONTROL)
 	 * soon.
 	 */
-	if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent))
+	if (!uclass_get_device_by_of_offset(UCLASS_MTD, node, &parent))
 		return BOOT_DEVICE_SPI;
 
 	return -1;
diff --git a/arch/x86/cpu/apollolake/spl.c b/arch/x86/cpu/apollolake/spl.c
index e1ee1e0624..9c80440bbb 100644
--- a/arch/x86/cpu/apollolake/spl.c
+++ b/arch/x86/cpu/apollolake/spl.c
@@ -10,6 +10,7 @@ 
 #include <image.h>
 #include <log.h>
 #include <malloc.h>
+#include <mtd.h>
 #include <spi.h>
 #include <spl.h>
 #include <spi_flash.h>
@@ -41,7 +42,7 @@  static int rom_load_image(struct spl_image_info *spl_image,
 	debug("Reading from mapped SPI %lx, size %lx", spl_pos, spl_size);
 
 	if (CONFIG_IS_ENABLED(SPI_FLASH_SUPPORT)) {
-		ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
+		ret = uclass_find_first_device(UCLASS_MTD, &dev);
 		if (ret)
 			return log_msg_ret("spi_flash", ret);
 		if (!dev)
@@ -77,7 +78,7 @@  static int spl_fast_spi_load_image(struct spl_image_info *spl_image,
 	struct udevice *dev;
 	int ret;
 
-	ret = uclass_first_device_err(UCLASS_SPI_FLASH, &dev);
+	ret = uclass_first_device_err(UCLASS_MTD, &dev);
 	if (ret)
 		return ret;
 
@@ -89,8 +90,7 @@  static int spl_fast_spi_load_image(struct spl_image_info *spl_image,
 	spl_image->name = "U-Boot";
 	spl_pos &= ~0xff000000;
 	debug("Reading from flash %lx, size %lx\n", spl_pos, spl_size);
-	ret = spi_flash_read_dm(dev, spl_pos, spl_size,
-				(void *)spl_image->load_addr);
+	ret = mtd_dread(dev, spl_pos, spl_size, (void *)spl_image->load_addr);
 	cpu_flush_l1d_to_l2();
 	if (ret)
 		return ret;
diff --git a/arch/x86/lib/fsp2/fsp_init.c b/arch/x86/lib/fsp2/fsp_init.c
index 85cae54a0c..61af45d239 100644
--- a/arch/x86/lib/fsp2/fsp_init.c
+++ b/arch/x86/lib/fsp2/fsp_init.c
@@ -124,7 +124,7 @@  int fsp_locate_fsp(enum fsp_type_t type, struct binman_entry *entry,
 		struct udevice *sf;
 
 		/* Just use the SPI driver to get the memory map */
-		ret = uclass_find_first_device(UCLASS_SPI_FLASH, &sf);
+		ret = uclass_find_first_device(UCLASS_MTD, &sf);
 		if (ret)
 			return log_msg_ret("Cannot get SPI flash", ret);
 		ret = dm_spi_get_mmap(sf, &map_base, &map_size, &offset);
diff --git a/arch/x86/lib/fsp2/fsp_support.c b/arch/x86/lib/fsp2/fsp_support.c
index 3f2ca840dc..434bfdbc0e 100644
--- a/arch/x86/lib/fsp2/fsp_support.c
+++ b/arch/x86/lib/fsp2/fsp_support.c
@@ -6,6 +6,7 @@ 
 
 #include <common.h>
 #include <dm.h>
+#include <mtd.h>
 #include <log.h>
 #include <spi_flash.h>
 #include <asm/fsp/fsp_support.h>
@@ -37,10 +38,10 @@  int fsp_get_header(ulong offset, ulong size, bool use_spi_flash,
 	 */
 	debug("offset=%x buf=%x\n", (uint)offset, (uint)buf);
 	if (use_spi_flash) {
-		ret = uclass_first_device_err(UCLASS_SPI_FLASH, &dev);
+		ret = uclass_first_device_err(UCLASS_MTD, &dev);
 		if (ret)
 			return log_msg_ret("Cannot find flash device", ret);
-		ret = spi_flash_read_dm(dev, offset, PROBE_BUF_SIZE, buf);
+		ret = mtd_dread(dev, offset, PROBE_BUF_SIZE, buf);
 		if (ret)
 			return log_msg_ret("Cannot read flash", ret);
 	} else {
@@ -88,7 +89,7 @@  int fsp_get_header(ulong offset, ulong size, bool use_spi_flash,
 	debug("Image base %x\n", (uint)base);
 	debug("Image addr %x\n", (uint)fsp->fsp_mem_init);
 	if (use_spi_flash) {
-		ret = spi_flash_read_dm(dev, offset, size, base);
+		ret = mtd_dread(dev, offset, size, base);
 		if (ret)
 			return log_msg_ret("Could not read FPS-M", ret);
 	} else {
diff --git a/arch/x86/lib/mrccache.c b/arch/x86/lib/mrccache.c
index f181e8100c..30eb5cf669 100644
--- a/arch/x86/lib/mrccache.c
+++ b/arch/x86/lib/mrccache.c
@@ -12,6 +12,7 @@ 
 #include <fdtdec.h>
 #include <log.h>
 #include <malloc.h>
+#include <mtd.h>
 #include <net.h>
 #include <spi.h>
 #include <spi_flash.h>
@@ -167,7 +168,7 @@  static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
 		debug("Erasing the MRC cache region of %x bytes at %x\n",
 		      entry->length, entry->offset);
 
-		ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
+		ret = mtd_derase(sf, entry->offset, entry->length);
 		if (ret) {
 			debug("Failed to erase flash region\n");
 			return ret;
@@ -178,8 +179,7 @@  static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
 	/* Write the data out */
 	offset = (ulong)cache - base_addr + entry->offset;
 	debug("Write MRC cache update to flash at %lx\n", offset);
-	ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
-				 cur);
+	ret = mtd_dwrite(sf, offset, cur->data_size + sizeof(*cur), cur);
 	if (ret) {
 		debug("Failed to write to SPI flash\n");
 		return log_msg_ret("Cannot update mrccache", ret);
@@ -242,7 +242,7 @@  int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
 	 * the device here since it may put it into a strange state where the
 	 * memory map cannot be read.
 	 */
-	ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
+	ret = uclass_find_first_device(UCLASS_MTD, &dev);
 	if (ret || !dev) {
 		/*
 		 * Fall back to searching the device tree since driver model
diff --git a/board/atmel/common/mac-spi-nor.c b/board/atmel/common/mac-spi-nor.c
index ced27b65e6..0b3f1b1340 100644
--- a/board/atmel/common/mac-spi-nor.c
+++ b/board/atmel/common/mac-spi-nor.c
@@ -107,10 +107,10 @@  void at91_spi_nor_set_ethaddr(void)
 	if (env_get(ethaddr_name))
 		return;
 
-	if (uclass_first_device_err(UCLASS_SPI_FLASH, &dev))
+	if (uclass_first_device_err(UCLASS_MTD, &dev))
 		return;
 
-	nor = dev_get_uclass_priv(dev);
+	nor = dev_get_priv(dev);
 	if (!nor)
 		return;
 
diff --git a/drivers/mtd/spi/Kconfig b/drivers/mtd/spi/Kconfig
index 018e8c597e..9de0484b29 100644
--- a/drivers/mtd/spi/Kconfig
+++ b/drivers/mtd/spi/Kconfig
@@ -3,6 +3,8 @@  menu "SPI Flash Support"
 config DM_SPI_FLASH
 	bool "Enable Driver Model for SPI flash"
 	depends on DM && DM_SPI
+	select DM_MTD
+	imply SPL_DM_MTD
 	imply SPI_FLASH
 	help
 	  Enable driver model for SPI flash. This SPI flash interface
diff --git a/drivers/mtd/spi/sandbox.c b/drivers/mtd/spi/sandbox.c
index 8cbe97ee20..0b04f14b16 100644
--- a/drivers/mtd/spi/sandbox.c
+++ b/drivers/mtd/spi/sandbox.c
@@ -8,7 +8,7 @@ 
  * Licensed under the GPL-2 or later.
  */
 
-#define LOG_CATEGORY UCLASS_SPI_FLASH
+#define LOG_CATEGORY UCLASS_MTD
 
 #include <common.h>
 #include <dm.h>
diff --git a/drivers/mtd/spi/sf-uclass.c b/drivers/mtd/spi/sf-uclass.c
index 44cdb3151d..9cab08f49b 100644
--- a/drivers/mtd/spi/sf-uclass.c
+++ b/drivers/mtd/spi/sf-uclass.c
@@ -7,6 +7,7 @@ 
 #include <dm.h>
 #include <log.h>
 #include <malloc.h>
+#include <mtd.h>
 #include <spi.h>
 #include <spi_flash.h>
 #include <dm/device-internal.h>
@@ -14,22 +15,6 @@ 
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
-{
-	return log_ret(sf_get_ops(dev)->read(dev, offset, len, buf));
-}
-
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-		       const void *buf)
-{
-	return log_ret(sf_get_ops(dev)->write(dev, offset, len, buf));
-}
-
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
-{
-	return log_ret(sf_get_ops(dev)->erase(dev, offset, len));
-}
-
 void spi_flash_free(struct spi_flash *flash)
 {
 	device_remove(flash->spi->dev, DM_REMOVE_NORMAL);
@@ -57,108 +42,78 @@  struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 	str = strdup(name);
 #endif
 	ret = spi_get_bus_and_cs(bus, cs, max_hz, spi_mode,
-				 "spi_flash_std", str, &bus_dev, &slave);
+				  "spi_flash_std", str, &bus_dev, &slave);
 	if (ret)
 		return NULL;
 
-	return dev_get_uclass_priv(slave->dev);
+	return dev_get_priv(slave->dev);
 }
 
-static int spi_flash_post_bind(struct udevice *dev)
+static int spi_flash_std_read(struct udevice *dev, loff_t from, size_t len,
+			      u_char *buf)
 {
-#if defined(CONFIG_NEEDS_MANUAL_RELOC)
-	struct dm_spi_flash_ops *ops = sf_get_ops(dev);
-	static int reloc_done;
-
-	if (!reloc_done) {
-		if (ops->read)
-			ops->read += gd->reloc_off;
-		if (ops->write)
-			ops->write += gd->reloc_off;
-		if (ops->erase)
-			ops->erase += gd->reloc_off;
-
-		reloc_done++;
-	}
-#endif
-	return 0;
-}
-
-static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
-			      void *buf)
-{
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
+	struct spi_flash *flash = dev_get_priv(dev);
 	struct mtd_info *mtd = &flash->mtd;
 	size_t retlen;
 
-	return log_ret(mtd->_read(mtd, offset, len, &retlen, buf));
+	return log_ret(mtd->_read(mtd, from, len, &retlen, buf));
 }
 
-static int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
-			       const void *buf)
+static int spi_flash_std_write(struct udevice *dev, loff_t to, size_t len,
+			       const u_char *buf)
 {
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
+	struct spi_flash *flash = dev_get_priv(dev);
 	struct mtd_info *mtd = &flash->mtd;
 	size_t retlen;
 
-	return mtd->_write(mtd, offset, len, &retlen, buf);
+	return mtd->_write(mtd, to, len, &retlen, buf);
 }
 
-static int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
+static int spi_flash_std_erase(struct udevice *dev, struct erase_info *instr)
 {
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
+	struct spi_flash *flash = dev_get_priv(dev);
 	struct mtd_info *mtd = &flash->mtd;
-	struct erase_info instr;
 
-	if (offset % mtd->erasesize || len % mtd->erasesize) {
-		printf("SF: Erase offset/length not multiple of erase size\n");
+	if (instr->addr % mtd->erasesize || instr->len % mtd->erasesize) {
+		dev_err(dev, "Erase offset/length not multiple of erasesize\n");
 		return -EINVAL;
 	}
 
-	memset(&instr, 0, sizeof(instr));
-	instr.addr = offset;
-	instr.len = len;
-
-	return mtd->_erase(mtd, &instr);
+	return mtd->_erase(mtd, instr);
 }
 
 static int spi_flash_std_probe(struct udevice *dev)
 {
+	struct spi_flash *flash = dev_get_priv(dev);
 	struct spi_slave *slave = dev_get_parent_priv(dev);
-	struct spi_flash *flash;
 	int ret;
 
-	if (!slave) {
-		printf("SF: Failed to set up slave\n");
-		return -ENODEV;
-	}
-
-	flash = dev_get_uclass_priv(dev);
 	flash->dev = dev;
 	flash->spi = slave;
 
-	/* Claim spi bus */
 	ret = spi_claim_bus(slave);
 	if (ret) {
-		debug("SF: Failed to claim SPI bus: %d\n", ret);
+		dev_err(dev, "failed to claim bus (ret=%d)\n", ret);
 		return ret;
 	}
 
 	ret = spi_nor_scan(flash);
-	if (ret)
-		goto err_read_id;
+	if (ret) {
+		dev_err(dev, "failed to scan spinor (ret=%d)\n", ret);
+		goto err_claim_bus;
+	}
 
-	if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
+	if (IS_ENABLED(CONFIG_SPI_FLASH_MTD))
 		ret = spi_flash_mtd_register(flash);
 
-err_read_id:
+err_claim_bus:
 	spi_release_bus(slave);
 	return ret;
 }
 
 static int spi_flash_std_remove(struct udevice *dev)
 {
-	if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
+	if (IS_ENABLED(CONFIG_SPI_FLASH_MTD))
 		spi_flash_mtd_unregister();
 
 	return 0;
@@ -190,7 +145,7 @@  static int spi_flash_std_bind(struct udevice *dev)
 	return 0;
 }
 
-static const struct dm_spi_flash_ops spi_flash_std_ops = {
+static const struct mtd_ops spi_flash_std_ops = {
 	.read = spi_flash_std_read,
 	.write = spi_flash_std_write,
 	.erase = spi_flash_std_erase,
@@ -203,7 +158,7 @@  static const struct udevice_id spi_flash_std_ids[] = {
 
 U_BOOT_DRIVER(spi_flash_std) = {
 	.name		= "spi_flash_std",
-	.id		= UCLASS_SPI_FLASH,
+	.id		= UCLASS_MTD,
 	.of_match	= spi_flash_std_ids,
 	.probe		= spi_flash_std_probe,
 	.bind		= spi_flash_std_bind,
@@ -211,10 +166,3 @@  U_BOOT_DRIVER(spi_flash_std) = {
 	.priv_auto_alloc_size = sizeof(struct spi_flash),
 	.ops		= &spi_flash_std_ops,
 };
-
-UCLASS_DRIVER(spi_flash) = {
-	.id		= UCLASS_SPI_FLASH,
-	.name		= "spi_flash",
-	.post_bind	= spi_flash_post_bind,
-	.per_device_auto_alloc_size = sizeof(struct spi_flash),
-};
diff --git a/drivers/mtd/spi/sf_dataflash.c b/drivers/mtd/spi/sf_dataflash.c
index 27d847d421..5c73b54cbc 100644
--- a/drivers/mtd/spi/sf_dataflash.c
+++ b/drivers/mtd/spi/sf_dataflash.c
@@ -12,6 +12,7 @@ 
 #include <fdtdec.h>
 #include <flash.h>
 #include <log.h>
+#include <mtd.h>
 #include <spi.h>
 #include <spi_flash.h>
 #include <div64.h>
@@ -117,7 +118,7 @@  static int dataflash_waitready(struct spi_slave *spi)
 }
 
 /* Erase pages of flash */
-static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
+static int spi_dataflash_erase(struct udevice *dev, struct erase_info *instr)
 {
 	struct dataflash	*dataflash;
 	struct spi_flash	*spi_flash;
@@ -125,6 +126,8 @@  static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
 	unsigned		blocksize;
 	uint8_t			*command;
 	uint32_t		rem;
+	loff_t			offset = instr->addr;
+	size_t			len = instr->len;
 	int			status;
 
 	dataflash = dev_get_priv(dev);
@@ -136,7 +139,7 @@  static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
 	memset(dataflash->command, 0 , sizeof(dataflash->command));
 	command = dataflash->command;
 
-	debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
+	debug("%s: erase addr=0x%llx len 0x%x\n", dev->name, offset, len);
 
 	div_u64_rem(len, spi_flash->page_size, &rem);
 	if (rem) {
@@ -146,7 +149,7 @@  static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
 	}
 	div_u64_rem(offset, spi_flash->page_size, &rem);
 	if (rem) {
-		printf("%s: offset(0x%x) isn't the multiple of page size(0x%x)\n",
+		printf("%s: offset(0x%llx) isn't the multiple of page size(0x%x)\n",
 		       dev->name, offset, spi_flash->page_size);
 		return -EINVAL;
 	}
@@ -210,8 +213,8 @@  static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
  *   len    : Amount to read
  *   buf    : Buffer containing the data
  */
-static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
-			      void *buf)
+static int spi_dataflash_read(struct udevice *dev, loff_t offset, size_t len,
+			      u_char *buf)
 {
 	struct dataflash	*dataflash;
 	struct spi_flash	*spi_flash;
@@ -227,7 +230,7 @@  static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
 	memset(dataflash->command, 0 , sizeof(dataflash->command));
 	command = dataflash->command;
 
-	debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
+	debug("%s: erase addr=0x%llx len 0x%x\n", dev->name, offset, len);
 	debug("READ: (%x) %x %x %x\n",
 	      command[0], command[1], command[2], command[3]);
 
@@ -266,8 +269,8 @@  static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
  *   len    : Amount to write
  *   buf    : Buffer containing the data
  */
-int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
-			const void *buf)
+int spi_dataflash_write(struct udevice *dev, loff_t offset, size_t len,
+			const u_char *buf)
 {
 	struct dataflash	*dataflash;
 	struct spi_flash	*spi_flash;
@@ -285,7 +288,7 @@  int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
 	memset(dataflash->command, 0 , sizeof(dataflash->command));
 	command = dataflash->command;
 
-	debug("%s: write 0x%x..0x%x\n", dev->name, offset, (offset + len));
+	debug("%s: write 0x%llx..0x%llx\n", dev->name, offset, (offset + len));
 
 	pageaddr = ((unsigned)offset / spi_flash->page_size);
 	to = ((unsigned)offset % spi_flash->page_size);
@@ -676,7 +679,7 @@  err_jedec_probe:
 	return status;
 }
 
-static const struct dm_spi_flash_ops spi_dataflash_ops = {
+static const struct mtd_ops spi_dataflash_ops = {
 	.read = spi_dataflash_read,
 	.write = spi_dataflash_write,
 	.erase = spi_dataflash_erase,
@@ -690,7 +693,7 @@  static const struct udevice_id spi_dataflash_ids[] = {
 
 U_BOOT_DRIVER(spi_dataflash) = {
 	.name		= "spi_dataflash",
-	.id		= UCLASS_SPI_FLASH,
+	.id		= UCLASS_MTD,
 	.of_match	= spi_dataflash_ids,
 	.probe		= spi_dataflash_probe,
 	.priv_auto_alloc_size = sizeof(struct dataflash),
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 7837d459f1..1f56f7f48a 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -99,7 +99,6 @@  enum uclass_id {
 	UCLASS_SMEM,		/* Shared memory interface */
 	UCLASS_SOUND,		/* Playing simple sounds */
 	UCLASS_SPI,		/* SPI bus */
-	UCLASS_SPI_FLASH,	/* SPI flash */
 	UCLASS_SPI_GENERIC,	/* Generic SPI flash target */
 	UCLASS_SPMI,		/* System Power Management Interface bus */
 	UCLASS_SYSCON,		/* System configuration device */
diff --git a/include/spi_flash.h b/include/spi_flash.h
index 24759944eb..f4852fc5c1 100644
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -10,6 +10,7 @@ 
 #define _SPI_FLASH_H_
 
 #include <dm.h>	/* Because we dereference struct udevice here */
+#include <mtd.h>
 #include <linux/types.h>
 #include <linux/mtd/spi-nor.h>
 
@@ -27,70 +28,24 @@ 
 # define CONFIG_ENV_SPI_MODE	CONFIG_SF_DEFAULT_MODE
 #endif
 
-struct spi_slave;
-
-struct dm_spi_flash_ops {
-	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
-	int (*write)(struct udevice *dev, u32 offset, size_t len,
-		     const void *buf);
-	int (*erase)(struct udevice *dev, u32 offset, size_t len);
-};
-
-/* Access the serial operations for a device */
-#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
-
 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
-/**
- * spi_flash_read_dm() - Read data from SPI flash
- *
- * @dev:	SPI flash device
- * @offset:	Offset into device in bytes to read from
- * @len:	Number of bytes to read
- * @buf:	Buffer to put the data that is read
- * @return 0 if OK, -ve on error
- */
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
-
-/**
- * spi_flash_write_dm() - Write data to SPI flash
- *
- * @dev:	SPI flash device
- * @offset:	Offset into device in bytes to write to
- * @len:	Number of bytes to write
- * @buf:	Buffer containing bytes to write
- * @return 0 if OK, -ve on error
- */
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-		       const void *buf);
-
-/**
- * spi_flash_erase_dm() - Erase blocks of the SPI flash
- *
- * Note that @len must be a muiltiple of the flash sector size.
- *
- * @dev:	SPI flash device
- * @offset:	Offset into device in bytes to start erasing
- * @len:	Number of bytes to erase
- * @return 0 if OK, -ve on error
- */
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
 
 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
 				 size_t len, void *buf)
 {
-	return spi_flash_read_dm(flash->dev, offset, len, buf);
+	return mtd_dread(flash->dev, offset, len, buf);
 }
 
 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
 				  size_t len, const void *buf)
 {
-	return spi_flash_write_dm(flash->dev, offset, len, buf);
+	return mtd_dwrite(flash->dev, offset, len, buf);
 }
 
 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 				  size_t len)
 {
-	return spi_flash_erase_dm(flash->dev, offset, len);
+	return mtd_derase(flash->dev, offset, len);
 }
 
 struct sandbox_state;
diff --git a/test/dm/sf.c b/test/dm/sf.c
index 9e7dead684..a675923dc5 100644
--- a/test/dm/sf.c
+++ b/test/dm/sf.c
@@ -31,23 +31,23 @@  static int dm_test_spi_flash(struct unit_test_state *uts)
 
 	src = map_sysmem(0x20000, full_size);
 	ut_assertok(os_write_file("spi.bin", src, full_size));
-	ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
+	ut_assertok(uclass_first_device_err(UCLASS_MTD, &dev));
 
 	dst = map_sysmem(0x20000 + full_size, full_size);
-	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
+	ut_assertok(mtd_dread(dev, 0, size, dst));
 	ut_asserteq_mem(src, dst, size);
 
 	/* Erase */
-	ut_assertok(spi_flash_erase_dm(dev, 0, size));
-	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
+	ut_assertok(mtd_derase(dev, 0, size));
+	ut_assertok(mtd_dread(dev, 0, size, dst));
 	for (i = 0; i < size; i++)
 		ut_asserteq(dst[i], 0xff);
 
 	/* Write some new data */
 	for (i = 0; i < size; i++)
 		src[i] = i;
-	ut_assertok(spi_flash_write_dm(dev, 0, size, src));
-	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
+	ut_assertok(mtd_dwrite(dev, 0, size, src));
+	ut_assertok(mtd_dread(dev, 0, size, dst));
 	ut_asserteq_mem(src, dst, size);
 
 	/* Check mapping */