1 /* 2 * SPI flash probing 3 * 4 * Copyright (C) 2008 Atmel Corporation 5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik 6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #include <common.h> 12 #include <dm.h> 13 #include <errno.h> 14 #include <fdtdec.h> 15 #include <malloc.h> 16 #include <mapmem.h> 17 #include <spi.h> 18 #include <spi_flash.h> 19 #include <asm/io.h> 20 21 #include "sf_internal.h" 22 23 /** 24 * spi_flash_probe_slave() - Probe for a SPI flash device on a bus 25 * 26 * @spi: Bus to probe 27 * @flashp: Pointer to place to put flash info, which may be NULL if the 28 * space should be allocated 29 */ 30 int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash) 31 { 32 int ret; 33 34 /* Setup spi_slave */ 35 if (!spi) { 36 printf("SF: Failed to set up slave\n"); 37 return -ENODEV; 38 } 39 40 /* Claim spi bus */ 41 ret = spi_claim_bus(spi); 42 if (ret) { 43 debug("SF: Failed to claim SPI bus: %d\n", ret); 44 return ret; 45 } 46 47 ret = spi_flash_scan(spi, flash); 48 if (ret) { 49 ret = -EINVAL; 50 goto err_read_id; 51 } 52 53 #ifdef CONFIG_SPI_FLASH_MTD 54 ret = spi_flash_mtd_register(flash); 55 #endif 56 57 err_read_id: 58 spi_release_bus(spi); 59 return ret; 60 } 61 62 #ifndef CONFIG_DM_SPI_FLASH 63 struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus) 64 { 65 struct spi_flash *flash; 66 67 /* Allocate space if needed (not used by sf-uclass */ 68 flash = calloc(1, sizeof(*flash)); 69 if (!flash) { 70 debug("SF: Failed to allocate spi_flash\n"); 71 return NULL; 72 } 73 74 if (spi_flash_probe_slave(bus, flash)) { 75 spi_free_slave(bus); 76 free(flash); 77 return NULL; 78 } 79 80 return flash; 81 } 82 83 struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs, 84 unsigned int max_hz, unsigned int spi_mode) 85 { 86 struct spi_slave *bus; 87 88 bus = spi_setup_slave(busnum, cs, max_hz, spi_mode); 89 if (!bus) 90 return NULL; 91 return spi_flash_probe_tail(bus); 92 } 93 94 #ifdef CONFIG_OF_SPI_FLASH 95 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, 96 int spi_node) 97 { 98 struct spi_slave *bus; 99 100 bus = spi_setup_slave_fdt(blob, slave_node, spi_node); 101 if (!bus) 102 return NULL; 103 return spi_flash_probe_tail(bus); 104 } 105 #endif 106 107 void spi_flash_free(struct spi_flash *flash) 108 { 109 #ifdef CONFIG_SPI_FLASH_MTD 110 spi_flash_mtd_unregister(); 111 #endif 112 spi_free_slave(flash->spi); 113 free(flash); 114 } 115 116 #else /* defined CONFIG_DM_SPI_FLASH */ 117 118 static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len, 119 void *buf) 120 { 121 struct spi_flash *flash = dev_get_uclass_priv(dev); 122 123 return spi_flash_cmd_read_ops(flash, offset, len, buf); 124 } 125 126 int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len, 127 const void *buf) 128 { 129 struct spi_flash *flash = dev_get_uclass_priv(dev); 130 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); 135 else 136 return sst_write_wp(flash, offset, len, buf); 137 } 138 #endif 139 140 return spi_flash_cmd_write_ops(flash, offset, len, buf); 141 } 142 143 int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len) 144 { 145 struct spi_flash *flash = dev_get_uclass_priv(dev); 146 147 return spi_flash_cmd_erase_ops(flash, offset, len); 148 } 149 150 int spi_flash_std_probe(struct udevice *dev) 151 { 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; 155 156 flash = dev_get_uclass_priv(dev); 157 flash->dev = dev; 158 debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs); 159 return spi_flash_probe_slave(slave, flash); 160 } 161 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, 166 }; 167 168 static const struct udevice_id spi_flash_std_ids[] = { 169 { .compatible = "spi-flash" }, 170 { } 171 }; 172 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, 180 }; 181 182 #endif /* CONFIG_DM_SPI_FLASH */ 183