994559d5489dccf586742532ead48f96454c2e14
[oweals/u-boot.git] / drivers / mtd / spi / sf_probe.c
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         u8 idcode[5];
33         int ret;
34
35         /* Setup spi_slave */
36         if (!spi) {
37                 printf("SF: Failed to set up slave\n");
38                 return -ENODEV;
39         }
40
41         /* Claim spi bus */
42         ret = spi_claim_bus(spi);
43         if (ret) {
44                 debug("SF: Failed to claim SPI bus: %d\n", ret);
45                 return ret;
46         }
47
48         /* Read the ID codes */
49         ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
50         if (ret) {
51                 printf("SF: Failed to get idcodes\n");
52                 goto err_read_id;
53         }
54
55 #ifdef DEBUG
56         printf("SF: Got idcodes\n");
57         print_buffer(0, idcode, 1, sizeof(idcode), 0);
58 #endif
59
60         ret = spi_flash_scan(spi, idcode, flash);
61         if (ret) {
62                 ret = -EINVAL;
63                 goto err_read_id;
64         }
65
66 #ifdef CONFIG_SPI_FLASH_MTD
67         ret = spi_flash_mtd_register(flash);
68 #endif
69
70 err_read_id:
71         spi_release_bus(spi);
72         return ret;
73 }
74
75 #ifndef CONFIG_DM_SPI_FLASH
76 struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
77 {
78         struct spi_flash *flash;
79
80         /* Allocate space if needed (not used by sf-uclass */
81         flash = calloc(1, sizeof(*flash));
82         if (!flash) {
83                 debug("SF: Failed to allocate spi_flash\n");
84                 return NULL;
85         }
86
87         if (spi_flash_probe_slave(bus, flash)) {
88                 spi_free_slave(bus);
89                 free(flash);
90                 return NULL;
91         }
92
93         return flash;
94 }
95
96 struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
97                 unsigned int max_hz, unsigned int spi_mode)
98 {
99         struct spi_slave *bus;
100
101         bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
102         if (!bus)
103                 return NULL;
104         return spi_flash_probe_tail(bus);
105 }
106
107 #ifdef CONFIG_OF_SPI_FLASH
108 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
109                                       int spi_node)
110 {
111         struct spi_slave *bus;
112
113         bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
114         if (!bus)
115                 return NULL;
116         return spi_flash_probe_tail(bus);
117 }
118 #endif
119
120 void spi_flash_free(struct spi_flash *flash)
121 {
122 #ifdef CONFIG_SPI_FLASH_MTD
123         spi_flash_mtd_unregister();
124 #endif
125         spi_free_slave(flash->spi);
126         free(flash);
127 }
128
129 #else /* defined CONFIG_DM_SPI_FLASH */
130
131 static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
132                               void *buf)
133 {
134         struct spi_flash *flash = dev_get_uclass_priv(dev);
135
136         return spi_flash_cmd_read_ops(flash, offset, len, buf);
137 }
138
139 int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
140                         const void *buf)
141 {
142         struct spi_flash *flash = dev_get_uclass_priv(dev);
143
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);
148                 else
149                         return sst_write_wp(flash, offset, len, buf);
150         }
151 #endif
152
153         return spi_flash_cmd_write_ops(flash, offset, len, buf);
154 }
155
156 int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
157 {
158         struct spi_flash *flash = dev_get_uclass_priv(dev);
159
160         return spi_flash_cmd_erase_ops(flash, offset, len);
161 }
162
163 int spi_flash_std_probe(struct udevice *dev)
164 {
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;
168
169         flash = dev_get_uclass_priv(dev);
170         flash->dev = dev;
171         debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
172         return spi_flash_probe_slave(slave, flash);
173 }
174
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,
179 };
180
181 static const struct udevice_id spi_flash_std_ids[] = {
182         { .compatible = "spi-flash" },
183         { }
184 };
185
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,
193 };
194
195 #endif /* CONFIG_DM_SPI_FLASH */