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
)
36 printf("SF: Failed to set up slave\n");
41 ret
= spi_claim_bus(spi
);
43 debug("SF: Failed to claim SPI bus: %d\n", ret
);
47 ret
= spi_flash_scan(spi
, flash
);
53 #ifdef CONFIG_SPI_FLASH_MTD
54 ret
= spi_flash_mtd_register(flash
);
62 #ifndef CONFIG_DM_SPI_FLASH
63 struct spi_flash
*spi_flash_probe_tail(struct spi_slave
*bus
)
65 struct spi_flash
*flash
;
67 /* Allocate space if needed (not used by sf-uclass */
68 flash
= calloc(1, sizeof(*flash
));
70 debug("SF: Failed to allocate spi_flash\n");
74 if (spi_flash_probe_slave(bus
, flash
)) {
83 struct spi_flash
*spi_flash_probe(unsigned int busnum
, unsigned int cs
,
84 unsigned int max_hz
, unsigned int spi_mode
)
86 struct spi_slave
*bus
;
88 bus
= spi_setup_slave(busnum
, cs
, max_hz
, spi_mode
);
91 return spi_flash_probe_tail(bus
);
94 #ifdef CONFIG_OF_SPI_FLASH
95 struct spi_flash
*spi_flash_probe_fdt(const void *blob
, int slave_node
,
98 struct spi_slave
*bus
;
100 bus
= spi_setup_slave_fdt(blob
, slave_node
, spi_node
);
103 return spi_flash_probe_tail(bus
);
107 void spi_flash_free(struct spi_flash
*flash
)
109 #ifdef CONFIG_SPI_FLASH_MTD
110 spi_flash_mtd_unregister();
112 spi_free_slave(flash
->spi
);
116 #else /* defined CONFIG_DM_SPI_FLASH */
118 static int spi_flash_std_read(struct udevice
*dev
, u32 offset
, size_t len
,
121 struct spi_flash
*flash
= dev_get_uclass_priv(dev
);
123 return spi_flash_cmd_read_ops(flash
, offset
, len
, buf
);
126 int spi_flash_std_write(struct udevice
*dev
, u32 offset
, size_t len
,
129 struct spi_flash
*flash
= dev_get_uclass_priv(dev
);
131 #if defined(CONFIG_SPI_FLASH_SST)
132 if (flash
->flags
& SNOR_F_SST_WR
) {
133 if (flash
->spi
->op_mode_tx
& SPI_OPM_TX_BP
)
134 return sst_write_bp(flash
, offset
, len
, buf
);
136 return sst_write_wp(flash
, offset
, len
, buf
);
140 return spi_flash_cmd_write_ops(flash
, offset
, len
, buf
);
143 int spi_flash_std_erase(struct udevice
*dev
, u32 offset
, size_t len
)
145 struct spi_flash
*flash
= dev_get_uclass_priv(dev
);
147 return spi_flash_cmd_erase_ops(flash
, offset
, len
);
150 int spi_flash_std_probe(struct udevice
*dev
)
152 struct spi_slave
*slave
= dev_get_parent_priv(dev
);
153 struct dm_spi_slave_platdata
*plat
= dev_get_parent_platdata(dev
);
154 struct spi_flash
*flash
;
156 flash
= dev_get_uclass_priv(dev
);
158 debug("%s: slave=%p, cs=%d\n", __func__
, slave
, plat
->cs
);
159 return spi_flash_probe_slave(slave
, flash
);
162 static const struct dm_spi_flash_ops spi_flash_std_ops
= {
163 .read
= spi_flash_std_read
,
164 .write
= spi_flash_std_write
,
165 .erase
= spi_flash_std_erase
,
168 static const struct udevice_id spi_flash_std_ids
[] = {
169 { .compatible
= "spi-flash" },
173 U_BOOT_DRIVER(spi_flash_std
) = {
174 .name
= "spi_flash_std",
175 .id
= UCLASS_SPI_FLASH
,
176 .of_match
= spi_flash_std_ids
,
177 .probe
= spi_flash_std_probe
,
178 .priv_auto_alloc_size
= sizeof(struct spi_flash
),
179 .ops
= &spi_flash_std_ops
,
182 #endif /* CONFIG_DM_SPI_FLASH */