183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
24c2dbefdSSimon Glass /*
34c2dbefdSSimon Glass * Copyright (c) 2014 Google, Inc
44c2dbefdSSimon Glass */
54c2dbefdSSimon Glass
64c2dbefdSSimon Glass #include <common.h>
74c2dbefdSSimon Glass #include <dm.h>
84c2dbefdSSimon Glass #include <spi.h>
94c2dbefdSSimon Glass #include <spi_flash.h>
104c2dbefdSSimon Glass #include <dm/device-internal.h>
114c2dbefdSSimon Glass #include "sf_internal.h"
124c2dbefdSSimon Glass
132588f2ddSMichal Simek DECLARE_GLOBAL_DATA_PTR;
142588f2ddSMichal Simek
spi_flash_read_dm(struct udevice * dev,u32 offset,size_t len,void * buf)158d987abcSSimon Glass int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
168d987abcSSimon Glass {
175e24a2efSSimon Glass return log_ret(sf_get_ops(dev)->read(dev, offset, len, buf));
188d987abcSSimon Glass }
198d987abcSSimon Glass
spi_flash_write_dm(struct udevice * dev,u32 offset,size_t len,const void * buf)208d987abcSSimon Glass int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
218d987abcSSimon Glass const void *buf)
228d987abcSSimon Glass {
235e24a2efSSimon Glass return log_ret(sf_get_ops(dev)->write(dev, offset, len, buf));
248d987abcSSimon Glass }
258d987abcSSimon Glass
spi_flash_erase_dm(struct udevice * dev,u32 offset,size_t len)268d987abcSSimon Glass int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
278d987abcSSimon Glass {
285e24a2efSSimon Glass return log_ret(sf_get_ops(dev)->erase(dev, offset, len));
298d987abcSSimon Glass }
308d987abcSSimon Glass
spi_flash_ctrl_wlock_dm(struct udevice * dev,u32 offset,size_t len)31*591e1cf0SChin-Ting Kuo int spi_flash_ctrl_wlock_dm(struct udevice *dev, u32 offset, size_t len)
32*591e1cf0SChin-Ting Kuo {
33*591e1cf0SChin-Ting Kuo return log_ret(sf_get_ops(dev)->flash_ctrl_wlock(dev, offset, len));
34*591e1cf0SChin-Ting Kuo }
35*591e1cf0SChin-Ting Kuo
spi_flash_ctrl_wunlock_dm(struct udevice * dev,u32 offset,size_t len)36*591e1cf0SChin-Ting Kuo int spi_flash_ctrl_wunlock_dm(struct udevice *dev, u32 offset, size_t len)
37*591e1cf0SChin-Ting Kuo {
38*591e1cf0SChin-Ting Kuo return log_ret(sf_get_ops(dev)->flash_ctrl_wunlock(dev, offset, len));
39*591e1cf0SChin-Ting Kuo }
40*591e1cf0SChin-Ting Kuo
spl_flash_get_sw_write_prot(struct udevice * dev)41a58986caSSimon Glass int spl_flash_get_sw_write_prot(struct udevice *dev)
42a58986caSSimon Glass {
43a58986caSSimon Glass struct dm_spi_flash_ops *ops = sf_get_ops(dev);
44a58986caSSimon Glass
45a58986caSSimon Glass if (!ops->get_sw_write_prot)
46a58986caSSimon Glass return -ENOSYS;
47a58986caSSimon Glass return log_ret(ops->get_sw_write_prot(dev));
48a58986caSSimon Glass }
49a58986caSSimon Glass
504c2dbefdSSimon Glass /*
514c2dbefdSSimon Glass * TODO(sjg@chromium.org): This is an old-style function. We should remove
524c2dbefdSSimon Glass * it when all SPI flash drivers use dm
534c2dbefdSSimon Glass */
spi_flash_probe(unsigned int bus,unsigned int cs,unsigned int max_hz,unsigned int spi_mode)544c2dbefdSSimon Glass struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
554c2dbefdSSimon Glass unsigned int max_hz, unsigned int spi_mode)
564c2dbefdSSimon Glass {
574c2dbefdSSimon Glass struct udevice *dev;
584c2dbefdSSimon Glass
594c2dbefdSSimon Glass if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev))
604c2dbefdSSimon Glass return NULL;
614c2dbefdSSimon Glass
62e564f054SSimon Glass return dev_get_uclass_priv(dev);
634c2dbefdSSimon Glass }
644c2dbefdSSimon Glass
spi_flash_free(struct spi_flash * flash)654c2dbefdSSimon Glass void spi_flash_free(struct spi_flash *flash)
664c2dbefdSSimon Glass {
67706865afSStefan Roese device_remove(flash->spi->dev, DM_REMOVE_NORMAL);
684c2dbefdSSimon Glass }
694c2dbefdSSimon Glass
spi_flash_probe_bus_cs(unsigned int busnum,unsigned int cs,unsigned int max_hz,unsigned int spi_mode,struct udevice ** devp)704c2dbefdSSimon Glass int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
714c2dbefdSSimon Glass unsigned int max_hz, unsigned int spi_mode,
724c2dbefdSSimon Glass struct udevice **devp)
734c2dbefdSSimon Glass {
744c2dbefdSSimon Glass struct spi_slave *slave;
754c2dbefdSSimon Glass struct udevice *bus;
763c8fb12bSSimon Glass char *str;
774c2dbefdSSimon Glass int ret;
784c2dbefdSSimon Glass
793c8fb12bSSimon Glass #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_USE_TINY_PRINTF)
803c8fb12bSSimon Glass str = "spi_flash";
813c8fb12bSSimon Glass #else
823c8fb12bSSimon Glass char name[30];
833c8fb12bSSimon Glass
84a5e1bcdeSHaikun.Wang@freescale.com snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs);
854c2dbefdSSimon Glass str = strdup(name);
863c8fb12bSSimon Glass #endif
874c2dbefdSSimon Glass ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
884c2dbefdSSimon Glass "spi_flash_std", str, &bus, &slave);
894c2dbefdSSimon Glass if (ret)
904c2dbefdSSimon Glass return ret;
914c2dbefdSSimon Glass
924c2dbefdSSimon Glass *devp = slave->dev;
934c2dbefdSSimon Glass return 0;
944c2dbefdSSimon Glass }
954c2dbefdSSimon Glass
spi_flash_post_bind(struct udevice * dev)962588f2ddSMichal Simek static int spi_flash_post_bind(struct udevice *dev)
972588f2ddSMichal Simek {
982588f2ddSMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC)
992588f2ddSMichal Simek struct dm_spi_flash_ops *ops = sf_get_ops(dev);
1002588f2ddSMichal Simek static int reloc_done;
1012588f2ddSMichal Simek
1022588f2ddSMichal Simek if (!reloc_done) {
1032588f2ddSMichal Simek if (ops->read)
1042588f2ddSMichal Simek ops->read += gd->reloc_off;
1052588f2ddSMichal Simek if (ops->write)
1062588f2ddSMichal Simek ops->write += gd->reloc_off;
1072588f2ddSMichal Simek if (ops->erase)
1082588f2ddSMichal Simek ops->erase += gd->reloc_off;
1092588f2ddSMichal Simek
1102588f2ddSMichal Simek reloc_done++;
1112588f2ddSMichal Simek }
1122588f2ddSMichal Simek #endif
1132588f2ddSMichal Simek return 0;
1142588f2ddSMichal Simek }
1152588f2ddSMichal Simek
1164c2dbefdSSimon Glass UCLASS_DRIVER(spi_flash) = {
1174c2dbefdSSimon Glass .id = UCLASS_SPI_FLASH,
1184c2dbefdSSimon Glass .name = "spi_flash",
1192588f2ddSMichal Simek .post_bind = spi_flash_post_bind,
1204c2dbefdSSimon Glass .per_device_auto_alloc_size = sizeof(struct spi_flash),
1214c2dbefdSSimon Glass };
122