xref: /openbmc/u-boot/drivers/mtd/spi/sf-uclass.c (revision ed09a554)
1 /*
2  * Copyright (c) 2014 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <spi.h>
10 #include <spi_flash.h>
11 #include <dm/device-internal.h>
12 #include "sf_internal.h"
13 
14 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
15 {
16 	return sf_get_ops(dev)->read(dev, offset, len, buf);
17 }
18 
19 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
20 		       const void *buf)
21 {
22 	return sf_get_ops(dev)->write(dev, offset, len, buf);
23 }
24 
25 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
26 {
27 	return sf_get_ops(dev)->erase(dev, offset, len);
28 }
29 
30 /*
31  * TODO(sjg@chromium.org): This is an old-style function. We should remove
32  * it when all SPI flash drivers use dm
33  */
34 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
35 				  unsigned int max_hz, unsigned int spi_mode)
36 {
37 	struct udevice *dev;
38 
39 	if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev))
40 		return NULL;
41 
42 	return dev_get_uclass_priv(dev);
43 }
44 
45 void spi_flash_free(struct spi_flash *flash)
46 {
47 	spi_flash_remove(flash->spi->dev);
48 }
49 
50 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
51 			   unsigned int max_hz, unsigned int spi_mode,
52 			   struct udevice **devp)
53 {
54 	struct spi_slave *slave;
55 	struct udevice *bus;
56 	char name[20], *str;
57 	int ret;
58 
59 	snprintf(name, sizeof(name), "%d:%d", busnum, cs);
60 	str = strdup(name);
61 	ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
62 				  "spi_flash_std", str, &bus, &slave);
63 	if (ret)
64 		return ret;
65 
66 	*devp = slave->dev;
67 	return 0;
68 }
69 
70 int spi_flash_remove(struct udevice *dev)
71 {
72 	return device_remove(dev);
73 }
74 
75 UCLASS_DRIVER(spi_flash) = {
76 	.id		= UCLASS_SPI_FLASH,
77 	.name		= "spi_flash",
78 	.per_device_auto_alloc_size = sizeof(struct spi_flash),
79 };
80