xref: /rk3399_rockchip-uboot/drivers/mtd/spi/sf_probe.c (revision d1ffb5dd4317afb75fe2c8f8c0bfe2b996bb28f7)
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 <blk.h>
13 #include <dm.h>
14 #include <errno.h>
15 #include <malloc.h>
16 #include <spi.h>
17 #include <spi_flash.h>
18 
19 #include "sf_internal.h"
20 
21 /**
22  * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
23  *
24  * @flashp: Pointer to place to put flash info, which may be NULL if the
25  * space should be allocated
26  */
27 static int spi_flash_probe_slave(struct spi_flash *flash)
28 {
29 	struct spi_slave *spi = flash->spi;
30 	int ret;
31 
32 	/* Setup spi_slave */
33 	if (!spi) {
34 		printf("SF: Failed to set up slave\n");
35 		return -ENODEV;
36 	}
37 
38 	/* Claim spi bus */
39 	ret = spi_claim_bus(spi);
40 	if (ret) {
41 		debug("SF: Failed to claim SPI bus: %d\n", ret);
42 		return ret;
43 	}
44 
45 	ret = spi_nor_scan(flash);
46 	if (ret)
47 		goto err_read_id;
48 
49 #ifdef CONFIG_SPI_FLASH_MTD
50 	ret = spi_flash_mtd_register(flash);
51 #endif
52 
53 err_read_id:
54 	spi_release_bus(spi);
55 	return ret;
56 }
57 
58 #ifndef CONFIG_DM_SPI_FLASH
59 struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
60 				  unsigned int max_hz, unsigned int spi_mode)
61 {
62 	struct spi_slave *bus;
63 	struct spi_flash *flash;
64 
65 	bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
66 	if (!bus)
67 		return NULL;
68 
69 	/* Allocate space if needed (not used by sf-uclass */
70 	flash = calloc(1, sizeof(*flash));
71 	if (!flash) {
72 		debug("SF: Failed to allocate spi_flash\n");
73 		return NULL;
74 	}
75 
76 	flash->spi = bus;
77 	if (spi_flash_probe_slave(flash)) {
78 		spi_free_slave(bus);
79 		free(flash);
80 		return NULL;
81 	}
82 
83 	return flash;
84 }
85 
86 void spi_flash_free(struct spi_flash *flash)
87 {
88 #ifdef CONFIG_SPI_FLASH_MTD
89 	spi_flash_mtd_unregister();
90 #endif
91 	spi_free_slave(flash->spi);
92 	free(flash);
93 }
94 
95 #else /* defined CONFIG_DM_SPI_FLASH */
96 
97 static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
98 			      void *buf)
99 {
100 	struct spi_flash *flash = dev_get_uclass_priv(dev);
101 	struct mtd_info *mtd = &flash->mtd;
102 	size_t retlen;
103 
104 	return log_ret(mtd->_read(mtd, offset, len, &retlen, buf));
105 }
106 
107 static int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
108 			       const void *buf)
109 {
110 	struct spi_flash *flash = dev_get_uclass_priv(dev);
111 	struct mtd_info *mtd = &flash->mtd;
112 	size_t retlen;
113 
114 	return mtd->_write(mtd, offset, len, &retlen, buf);
115 }
116 
117 static int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
118 {
119 	struct spi_flash *flash = dev_get_uclass_priv(dev);
120 	struct mtd_info *mtd = &flash->mtd;
121 	struct erase_info instr;
122 
123 	if (offset % mtd->erasesize || len % mtd->erasesize) {
124 		printf("SF: Erase offset/length not multiple of erase size\n");
125 		return -EINVAL;
126 	}
127 
128 	memset(&instr, 0, sizeof(instr));
129 	instr.addr = offset;
130 	instr.len = len;
131 
132 	return mtd->_erase(mtd, &instr);
133 }
134 
135 static int spi_flash_std_get_sw_write_prot(struct udevice *dev)
136 {
137 	struct spi_flash *flash = dev_get_uclass_priv(dev);
138 
139 	return spi_flash_cmd_get_sw_write_prot(flash);
140 }
141 
142 static int spi_flash_std_bind(struct udevice *udev)
143 {
144 	int ret = 0;
145 
146 #ifdef CONFIG_MTD_BLK
147 	struct udevice *bdev;
148 
149 	ret = blk_create_devicef(udev, "mtd_blk", "blk", IF_TYPE_MTD,
150 				 BLK_MTD_SPI_NOR, 512, 0, &bdev);
151 	if (ret)
152 		printf("Cannot create block device\n");
153 #endif
154 	return ret;
155 }
156 
157 static int spi_flash_std_probe(struct udevice *dev)
158 {
159 	struct spi_slave *slave = dev_get_parent_priv(dev);
160 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
161 	struct spi_flash *flash;
162 
163 	flash = dev_get_uclass_priv(dev);
164 	flash->dev = dev;
165 	flash->spi = slave;
166 	debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
167 	return spi_flash_probe_slave(flash);
168 }
169 
170 static int spi_flash_std_remove(struct udevice *dev)
171 {
172 #ifdef CONFIG_SPI_FLASH_MTD
173 	spi_flash_mtd_unregister();
174 #endif
175 	return 0;
176 }
177 
178 static const struct dm_spi_flash_ops spi_flash_std_ops = {
179 	.read = spi_flash_std_read,
180 	.write = spi_flash_std_write,
181 	.erase = spi_flash_std_erase,
182 	.get_sw_write_prot = spi_flash_std_get_sw_write_prot,
183 };
184 
185 static const struct udevice_id spi_flash_std_ids[] = {
186 	{ .compatible = "jedec,spi-nor" },
187 	{ }
188 };
189 
190 U_BOOT_DRIVER(spi_flash_std) = {
191 	.name		= "spi_flash_std",
192 	.id		= UCLASS_SPI_FLASH,
193 	.of_match	= spi_flash_std_ids,
194 	.bind		= spi_flash_std_bind,
195 	.probe		= spi_flash_std_probe,
196 	.remove		= spi_flash_std_remove,
197 	.priv_auto_alloc_size = sizeof(struct spi_flash),
198 	.ops		= &spi_flash_std_ops,
199 };
200 
201 #endif /* CONFIG_DM_SPI_FLASH */
202