xref: /openbmc/u-boot/drivers/mtd/spi/sf-uclass.c (revision a5e1bcdeebebabdc5d013fbd488f87a4e62ff411)
14c2dbefdSSimon Glass /*
24c2dbefdSSimon Glass  * Copyright (c) 2014 Google, Inc
34c2dbefdSSimon Glass  *
44c2dbefdSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
54c2dbefdSSimon Glass  */
64c2dbefdSSimon Glass 
74c2dbefdSSimon Glass #include <common.h>
84c2dbefdSSimon Glass #include <dm.h>
94c2dbefdSSimon Glass #include <spi.h>
104c2dbefdSSimon Glass #include <spi_flash.h>
114c2dbefdSSimon Glass #include <dm/device-internal.h>
124c2dbefdSSimon Glass #include "sf_internal.h"
134c2dbefdSSimon Glass 
148d987abcSSimon Glass int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
158d987abcSSimon Glass {
168d987abcSSimon Glass 	return sf_get_ops(dev)->read(dev, offset, len, buf);
178d987abcSSimon Glass }
188d987abcSSimon Glass 
198d987abcSSimon Glass int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
208d987abcSSimon Glass 		       const void *buf)
218d987abcSSimon Glass {
228d987abcSSimon Glass 	return sf_get_ops(dev)->write(dev, offset, len, buf);
238d987abcSSimon Glass }
248d987abcSSimon Glass 
258d987abcSSimon Glass int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
268d987abcSSimon Glass {
278d987abcSSimon Glass 	return sf_get_ops(dev)->erase(dev, offset, len);
288d987abcSSimon Glass }
298d987abcSSimon Glass 
304c2dbefdSSimon Glass /*
314c2dbefdSSimon Glass  * TODO(sjg@chromium.org): This is an old-style function. We should remove
324c2dbefdSSimon Glass  * it when all SPI flash drivers use dm
334c2dbefdSSimon Glass  */
344c2dbefdSSimon Glass struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
354c2dbefdSSimon Glass 				  unsigned int max_hz, unsigned int spi_mode)
364c2dbefdSSimon Glass {
374c2dbefdSSimon Glass 	struct udevice *dev;
384c2dbefdSSimon Glass 
394c2dbefdSSimon Glass 	if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev))
404c2dbefdSSimon Glass 		return NULL;
414c2dbefdSSimon Glass 
42e564f054SSimon Glass 	return dev_get_uclass_priv(dev);
434c2dbefdSSimon Glass }
444c2dbefdSSimon Glass 
454c2dbefdSSimon Glass void spi_flash_free(struct spi_flash *flash)
464c2dbefdSSimon Glass {
474c2dbefdSSimon Glass 	spi_flash_remove(flash->spi->dev);
484c2dbefdSSimon Glass }
494c2dbefdSSimon Glass 
504c2dbefdSSimon Glass int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
514c2dbefdSSimon Glass 			   unsigned int max_hz, unsigned int spi_mode,
524c2dbefdSSimon Glass 			   struct udevice **devp)
534c2dbefdSSimon Glass {
544c2dbefdSSimon Glass 	struct spi_slave *slave;
554c2dbefdSSimon Glass 	struct udevice *bus;
56*a5e1bcdeSHaikun.Wang@freescale.com 	char name[30], *str;
574c2dbefdSSimon Glass 	int ret;
584c2dbefdSSimon Glass 
59*a5e1bcdeSHaikun.Wang@freescale.com 	snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs);
604c2dbefdSSimon Glass 	str = strdup(name);
614c2dbefdSSimon Glass 	ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
624c2dbefdSSimon Glass 				  "spi_flash_std", str, &bus, &slave);
634c2dbefdSSimon Glass 	if (ret)
644c2dbefdSSimon Glass 		return ret;
654c2dbefdSSimon Glass 
664c2dbefdSSimon Glass 	*devp = slave->dev;
674c2dbefdSSimon Glass 	return 0;
684c2dbefdSSimon Glass }
694c2dbefdSSimon Glass 
704c2dbefdSSimon Glass int spi_flash_remove(struct udevice *dev)
714c2dbefdSSimon Glass {
724c2dbefdSSimon Glass 	return device_remove(dev);
734c2dbefdSSimon Glass }
744c2dbefdSSimon Glass 
754c2dbefdSSimon Glass UCLASS_DRIVER(spi_flash) = {
764c2dbefdSSimon Glass 	.id		= UCLASS_SPI_FLASH,
774c2dbefdSSimon Glass 	.name		= "spi_flash",
784c2dbefdSSimon Glass 	.per_device_auto_alloc_size = sizeof(struct spi_flash),
794c2dbefdSSimon Glass };
80