4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
8 * SPDX-License-Identifier: GPL-2.0+
18 #include <spi_flash.h>
21 #include "sf_internal.h"
24 * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
27 * @flashp: Pointer to place to put flash info, which may be NULL if the
28 * space should be allocated
30 int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
37 printf("SF: Failed to set up slave\n");
42 ret = spi_claim_bus(spi);
44 debug("SF: Failed to claim SPI bus: %d\n", ret);
48 /* Read the ID codes */
49 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
51 printf("SF: Failed to get idcodes\n");
56 printf("SF: Got idcodes\n");
57 print_buffer(0, idcode, 1, sizeof(idcode), 0);
60 ret = spi_flash_scan(spi, idcode, flash);
66 #ifdef CONFIG_SPI_FLASH_MTD
67 ret = spi_flash_mtd_register(flash);
75 #ifndef CONFIG_DM_SPI_FLASH
76 struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
78 struct spi_flash *flash;
80 /* Allocate space if needed (not used by sf-uclass */
81 flash = calloc(1, sizeof(*flash));
83 debug("SF: Failed to allocate spi_flash\n");
87 if (spi_flash_probe_slave(bus, flash)) {
96 struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
97 unsigned int max_hz, unsigned int spi_mode)
99 struct spi_slave *bus;
101 bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
104 return spi_flash_probe_tail(bus);
107 #ifdef CONFIG_OF_SPI_FLASH
108 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
111 struct spi_slave *bus;
113 bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
116 return spi_flash_probe_tail(bus);
120 void spi_flash_free(struct spi_flash *flash)
122 #ifdef CONFIG_SPI_FLASH_MTD
123 spi_flash_mtd_unregister();
125 spi_free_slave(flash->spi);
129 #else /* defined CONFIG_DM_SPI_FLASH */
131 static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
134 struct spi_flash *flash = dev_get_uclass_priv(dev);
136 return spi_flash_cmd_read_ops(flash, offset, len, buf);
139 int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
142 struct spi_flash *flash = dev_get_uclass_priv(dev);
144 #if defined(CONFIG_SPI_FLASH_SST)
145 if (flash->flags & SNOR_F_SST_WR) {
146 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
147 return sst_write_bp(flash, offset, len, buf);
149 return sst_write_wp(flash, offset, len, buf);
153 return spi_flash_cmd_write_ops(flash, offset, len, buf);
156 int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
158 struct spi_flash *flash = dev_get_uclass_priv(dev);
160 return spi_flash_cmd_erase_ops(flash, offset, len);
163 int spi_flash_std_probe(struct udevice *dev)
165 struct spi_slave *slave = dev_get_parent_priv(dev);
166 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
167 struct spi_flash *flash;
169 flash = dev_get_uclass_priv(dev);
171 debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
172 return spi_flash_probe_slave(slave, flash);
175 static const struct dm_spi_flash_ops spi_flash_std_ops = {
176 .read = spi_flash_std_read,
177 .write = spi_flash_std_write,
178 .erase = spi_flash_std_erase,
181 static const struct udevice_id spi_flash_std_ids[] = {
182 { .compatible = "spi-flash" },
186 U_BOOT_DRIVER(spi_flash_std) = {
187 .name = "spi_flash_std",
188 .id = UCLASS_SPI_FLASH,
189 .of_match = spi_flash_std_ids,
190 .probe = spi_flash_std_probe,
191 .priv_auto_alloc_size = sizeof(struct spi_flash),
192 .ops = &spi_flash_std_ops,
195 #endif /* CONFIG_DM_SPI_FLASH */