xref: /openbmc/u-boot/drivers/mtd/spi/sf-uclass.c (revision c6832a96)
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 /*
15  * TODO(sjg@chromium.org): This is an old-style function. We should remove
16  * it when all SPI flash drivers use dm
17  */
18 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
19 				  unsigned int max_hz, unsigned int spi_mode)
20 {
21 	struct udevice *dev;
22 
23 	if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev))
24 		return NULL;
25 
26 	return dev->uclass_priv;
27 }
28 
29 void spi_flash_free(struct spi_flash *flash)
30 {
31 	spi_flash_remove(flash->spi->dev);
32 }
33 
34 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
35 			   unsigned int max_hz, unsigned int spi_mode,
36 			   struct udevice **devp)
37 {
38 	struct spi_slave *slave;
39 	struct udevice *bus;
40 	char name[20], *str;
41 	int ret;
42 
43 	snprintf(name, sizeof(name), "%d:%d", busnum, cs);
44 	str = strdup(name);
45 	ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
46 				  "spi_flash_std", str, &bus, &slave);
47 	if (ret)
48 		return ret;
49 
50 	*devp = slave->dev;
51 	return 0;
52 }
53 
54 int spi_flash_remove(struct udevice *dev)
55 {
56 	return device_remove(dev);
57 }
58 
59 UCLASS_DRIVER(spi_flash) = {
60 	.id		= UCLASS_SPI_FLASH,
61 	.name		= "spi_flash",
62 	.per_device_auto_alloc_size = sizeof(struct spi_flash),
63 };
64