/src/barebox/drivers/eeprom/at25.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * at25.c -- support most SPI EEPROMs, such as Atmel AT25 models |
4 | | * |
5 | | * Copyright (C) 2011 Hubert Feurstein <h.feurstein@gmail.com> |
6 | | * |
7 | | * based on linux driver by: |
8 | | * Copyright (C) 2006 David Brownell |
9 | | */ |
10 | | |
11 | | #include <common.h> |
12 | | #include <init.h> |
13 | | #include <clock.h> |
14 | | #include <driver.h> |
15 | | #include <errno.h> |
16 | | #include <xfuncs.h> |
17 | | #include <malloc.h> |
18 | | #include <spi/spi.h> |
19 | | #include <spi/eeprom.h> |
20 | | |
21 | | /* |
22 | | * NOTE: this is an *EEPROM* driver. The vagaries of product naming |
23 | | * mean that some AT25 products are EEPROMs, and others are FLASH. |
24 | | * Handle FLASH chips with the drivers/mtd/devices/m25p80.c driver, |
25 | | * not this one! |
26 | | */ |
27 | | |
28 | | struct at25_data { |
29 | | struct cdev cdev; |
30 | | struct spi_device *spi; |
31 | | struct spi_eeprom chip; |
32 | | unsigned addrlen; |
33 | | }; |
34 | | |
35 | 0 | #define to_at25_data(cdev) ((struct at25_data *)(cdev)->priv) |
36 | | |
37 | 0 | #define AT25_WREN 0x06 /* latch the write enable */ |
38 | | #define AT25_WRDI 0x04 /* reset the write enable */ |
39 | 0 | #define AT25_RDSR 0x05 /* read status register */ |
40 | | #define AT25_WRSR 0x01 /* write status register */ |
41 | 0 | #define AT25_READ 0x03 /* read byte(s) */ |
42 | 0 | #define AT25_WRITE 0x02 /* write byte(s)/sector */ |
43 | | |
44 | 0 | #define AT25_SR_nRDY 0x01 /* nRDY = write-in-progress */ |
45 | | #define AT25_SR_WEN 0x02 /* write enable (latched) */ |
46 | | #define AT25_SR_BP0 0x04 /* BP for software writeprotect */ |
47 | | #define AT25_SR_BP1 0x08 |
48 | | #define AT25_SR_WPEN 0x80 /* writeprotect enable */ |
49 | | |
50 | | |
51 | | #define EE_MAXADDRLEN 3 /* 24 bit addresses, up to 2 MBytes */ |
52 | | |
53 | | /* Specs often allow 5 msec for a page write, sometimes 20 msec; |
54 | | * it's important to recover from write timeouts. |
55 | | */ |
56 | 0 | #define EE_TIMEOUT (25 * MSECOND) |
57 | 0 | #define DRIVERNAME "at25x" |
58 | | |
59 | | /*-------------------------------------------------------------------------*/ |
60 | | |
61 | 0 | #define io_limit PAGE_SIZE /* bytes */ |
62 | | |
63 | | static ssize_t at25_ee_read(struct cdev *cdev, |
64 | | void *buf, |
65 | | size_t count, |
66 | | loff_t offset, |
67 | | ulong flags) |
68 | 0 | { |
69 | 0 | u8 command[EE_MAXADDRLEN + 1]; |
70 | 0 | u8 *cp; |
71 | 0 | ssize_t status; |
72 | 0 | struct spi_transfer t[2]; |
73 | 0 | struct spi_message m; |
74 | 0 | struct at25_data *at25 = to_at25_data(cdev); |
75 | |
|
76 | 0 | if (unlikely(offset >= at25->chip.size)) |
77 | 0 | return 0; |
78 | 0 | if ((offset + count) > at25->chip.size) |
79 | 0 | count = at25->chip.size - offset; |
80 | 0 | if (unlikely(!count)) |
81 | 0 | return count; |
82 | | |
83 | 0 | cp = command; |
84 | 0 | *cp++ = AT25_READ; |
85 | | |
86 | | /* 8/16/24-bit address is written MSB first */ |
87 | 0 | switch (at25->addrlen) { |
88 | 0 | default: /* case 3 */ |
89 | 0 | *cp++ = offset >> 16; |
90 | 0 | fallthrough; |
91 | 0 | case 2: |
92 | 0 | *cp++ = offset >> 8; |
93 | 0 | fallthrough; |
94 | 0 | case 1: |
95 | 0 | case 0: /* can't happen: for better codegen */ |
96 | 0 | *cp++ = offset >> 0; |
97 | 0 | } |
98 | |
|
99 | 0 | spi_message_init(&m); |
100 | 0 | memset(t, 0, sizeof t); |
101 | |
|
102 | 0 | t[0].tx_buf = command; |
103 | 0 | t[0].len = at25->addrlen + 1; |
104 | 0 | spi_message_add_tail(&t[0], &m); |
105 | |
|
106 | 0 | t[1].rx_buf = buf; |
107 | 0 | t[1].len = count; |
108 | 0 | spi_message_add_tail(&t[1], &m); |
109 | | |
110 | | /* Read it all at once. |
111 | | * |
112 | | * REVISIT that's potentially a problem with large chips, if |
113 | | * other devices on the bus need to be accessed regularly or |
114 | | * this chip is clocked very slowly |
115 | | */ |
116 | 0 | status = spi_sync(at25->spi, &m); |
117 | 0 | dev_dbg(at25->cdev.dev, |
118 | 0 | "read %zd bytes at %llu --> %zd\n", |
119 | 0 | count, offset, status); |
120 | |
|
121 | 0 | return status ? status : count; |
122 | 0 | } |
123 | | |
124 | | static ssize_t at25_ee_write(struct cdev *cdev, |
125 | | const void *buf, |
126 | | size_t count, |
127 | | loff_t off, |
128 | | ulong flags) |
129 | 0 | { |
130 | 0 | ssize_t status = 0; |
131 | 0 | unsigned written = 0; |
132 | 0 | unsigned buf_size; |
133 | 0 | u8 *bounce; |
134 | 0 | struct at25_data *at25 = to_at25_data(cdev); |
135 | |
|
136 | 0 | if (unlikely(off >= at25->chip.size)) |
137 | 0 | return -EFBIG; |
138 | 0 | if ((off + count) > at25->chip.size) |
139 | 0 | count = at25->chip.size - off; |
140 | 0 | if (unlikely(!count)) |
141 | 0 | return count; |
142 | | |
143 | | /* Temp buffer starts with command and address */ |
144 | 0 | buf_size = at25->chip.page_size; |
145 | 0 | if (buf_size > io_limit) |
146 | 0 | buf_size = io_limit; |
147 | 0 | bounce = xmalloc(buf_size + at25->addrlen + 1); |
148 | | |
149 | | /* For write, rollover is within the page ... so we write at |
150 | | * most one page, then manually roll over to the next page. |
151 | | */ |
152 | 0 | bounce[0] = AT25_WRITE; |
153 | |
|
154 | 0 | do { |
155 | 0 | uint64_t start_time; |
156 | 0 | unsigned segment; |
157 | 0 | unsigned offset = (unsigned) off; |
158 | 0 | u8 *cp = bounce + 1; |
159 | 0 | int sr; |
160 | |
|
161 | 0 | *cp = AT25_WREN; |
162 | 0 | status = spi_write(at25->spi, cp, 1); |
163 | 0 | if (status < 0) { |
164 | 0 | dev_dbg(at25->cdev.dev, "WREN --> %d\n", |
165 | 0 | (int) status); |
166 | 0 | break; |
167 | 0 | } |
168 | | |
169 | | /* 8/16/24-bit address is written MSB first */ |
170 | 0 | switch (at25->addrlen) { |
171 | 0 | default: /* case 3 */ |
172 | 0 | *cp++ = offset >> 16; |
173 | 0 | fallthrough; |
174 | 0 | case 2: |
175 | 0 | *cp++ = offset >> 8; |
176 | 0 | fallthrough; |
177 | 0 | case 1: |
178 | 0 | case 0: /* can't happen: for better codegen */ |
179 | 0 | *cp++ = offset >> 0; |
180 | 0 | } |
181 | | |
182 | | /* Write as much of a page as we can */ |
183 | 0 | segment = buf_size - (offset % buf_size); |
184 | 0 | if (segment > count) |
185 | 0 | segment = count; |
186 | 0 | memcpy(cp, buf, segment); |
187 | 0 | status = spi_write(at25->spi, bounce, |
188 | 0 | segment + at25->addrlen + 1); |
189 | 0 | dev_dbg(at25->cdev.dev, |
190 | 0 | "write %u bytes at %u --> %d\n", |
191 | 0 | segment, offset, (int) status); |
192 | 0 | if (status < 0) |
193 | 0 | break; |
194 | | |
195 | | /* REVISIT this should detect (or prevent) failed writes |
196 | | * to readonly sections of the EEPROM... |
197 | | */ |
198 | | |
199 | | /* Wait for non-busy status */ |
200 | 0 | start_time = get_time_ns(); |
201 | |
|
202 | 0 | do { |
203 | 0 | sr = spi_w8r8(at25->spi, AT25_RDSR); |
204 | 0 | if (sr < 0 || (sr & AT25_SR_nRDY)) { |
205 | 0 | dev_dbg(at25->cdev.dev, |
206 | 0 | "rdsr --> %d (%02x)\n", sr, sr); |
207 | 0 | continue; |
208 | 0 | } |
209 | 0 | if (!(sr & AT25_SR_nRDY)) |
210 | 0 | break; |
211 | 0 | } while (!is_timeout(start_time, EE_TIMEOUT)); |
212 | |
|
213 | 0 | if ((sr < 0) || (sr & AT25_SR_nRDY)) { |
214 | 0 | dev_err(at25->cdev.dev, |
215 | 0 | "write %d bytes offset %d, " |
216 | 0 | "timeout after %u msecs\n", |
217 | 0 | segment, offset, |
218 | 0 | (unsigned int)(get_time_ns() - start_time) / |
219 | 0 | 1000000); |
220 | 0 | status = -ETIMEDOUT; |
221 | 0 | break; |
222 | 0 | } |
223 | | |
224 | 0 | off += segment; |
225 | 0 | buf += segment; |
226 | 0 | count -= segment; |
227 | 0 | written += segment; |
228 | |
|
229 | 0 | } while (count > 0); |
230 | | |
231 | 0 | free(bounce); |
232 | 0 | return written ? written : status; |
233 | 0 | } |
234 | | |
235 | | static struct cdev_operations at25_fops = { |
236 | | .read = at25_ee_read, |
237 | | .write = at25_ee_write, |
238 | | }; |
239 | | |
240 | | static int at25_np_to_chip(struct device *dev, |
241 | | struct device_node *np, |
242 | | struct spi_eeprom *chip) |
243 | 0 | { |
244 | 0 | u32 val; |
245 | |
|
246 | 0 | if (!IS_ENABLED(CONFIG_OFDEVICE)) |
247 | 0 | return -ENODEV; |
248 | | |
249 | 0 | memset(chip, 0, sizeof(*chip)); |
250 | 0 | strlcpy(chip->name, np->name, sizeof(chip->name)); |
251 | |
|
252 | 0 | if (of_property_read_u32(np, "size", &val) == 0 || |
253 | 0 | of_property_read_u32(np, "at25,byte-len", &val) == 0) { |
254 | 0 | chip->size = val; |
255 | 0 | } else { |
256 | 0 | dev_err(dev, "Error: missing \"size\" property\n"); |
257 | 0 | return -ENODEV; |
258 | 0 | } |
259 | | |
260 | 0 | if (of_property_read_u32(np, "pagesize", &val) == 0 || |
261 | 0 | of_property_read_u32(np, "at25,page-size", &val) == 0) { |
262 | 0 | chip->page_size = val; |
263 | 0 | } else { |
264 | 0 | dev_err(dev, "Error: missing \"pagesize\" property\n"); |
265 | 0 | return -ENODEV; |
266 | 0 | } |
267 | | |
268 | 0 | if (of_property_read_u32(np, "at25,addr-mode", &val) == 0) { |
269 | 0 | chip->flags = (u16)val; |
270 | 0 | } else { |
271 | 0 | if (of_property_read_u32(np, "address-width", &val)) { |
272 | 0 | dev_err(dev, |
273 | 0 | "Error: missing \"address-width\" property\n"); |
274 | 0 | return -ENODEV; |
275 | 0 | } |
276 | 0 | switch (val) { |
277 | 0 | case 8: |
278 | 0 | chip->flags |= EE_ADDR1; |
279 | 0 | break; |
280 | 0 | case 16: |
281 | 0 | chip->flags |= EE_ADDR2; |
282 | 0 | break; |
283 | 0 | case 24: |
284 | 0 | chip->flags |= EE_ADDR3; |
285 | 0 | break; |
286 | 0 | default: |
287 | 0 | dev_err(dev, |
288 | 0 | "Error: bad \"address-width\" property: %u\n", |
289 | 0 | val); |
290 | 0 | return -ENODEV; |
291 | 0 | } |
292 | 0 | if (of_property_read_bool(np, "read-only")) |
293 | 0 | chip->flags |= EE_READONLY; |
294 | 0 | } |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | | static int at25_probe(struct device *dev) |
299 | 0 | { |
300 | 0 | int err, sr; |
301 | 0 | int addrlen; |
302 | 0 | struct at25_data *at25 = NULL; |
303 | 0 | struct spi_eeprom *chip; |
304 | |
|
305 | 0 | at25 = xzalloc(sizeof(*at25)); |
306 | | |
307 | | /* Chip description */ |
308 | 0 | if (dev->of_node) { |
309 | 0 | err = at25_np_to_chip(dev, dev->of_node, &at25->chip); |
310 | 0 | if (err) |
311 | 0 | goto fail; |
312 | 0 | } else { |
313 | 0 | chip = dev->platform_data; |
314 | 0 | if (!chip) { |
315 | 0 | dev_dbg(dev, "no chip description\n"); |
316 | 0 | err = -ENODEV; |
317 | 0 | goto fail; |
318 | 0 | } |
319 | 0 | at25->chip = *chip; |
320 | 0 | } |
321 | | |
322 | | /* For now we only support 8/16/24 bit addressing */ |
323 | 0 | if (at25->chip.flags & EE_ADDR1) { |
324 | 0 | addrlen = 1; |
325 | 0 | } else if (at25->chip.flags & EE_ADDR2) { |
326 | 0 | addrlen = 2; |
327 | 0 | } else if (at25->chip.flags & EE_ADDR3) { |
328 | 0 | addrlen = 3; |
329 | 0 | } else { |
330 | 0 | dev_dbg(dev, "unsupported address type\n"); |
331 | 0 | err = -EINVAL; |
332 | 0 | goto fail; |
333 | 0 | } |
334 | | |
335 | 0 | at25->addrlen = addrlen; |
336 | 0 | at25->spi = dev->type_data; |
337 | 0 | at25->spi->mode = SPI_MODE_0; |
338 | 0 | at25->spi->bits_per_word = 8; |
339 | 0 | at25->cdev.ops = &at25_fops; |
340 | 0 | at25->cdev.size = at25->chip.size; |
341 | 0 | at25->cdev.dev = dev; |
342 | 0 | at25->cdev.name = at25->chip.name[0] ? at25->chip.name : DRIVERNAME; |
343 | 0 | at25->cdev.priv = at25; |
344 | | |
345 | | /* Ping the chip ... the status register is pretty portable, |
346 | | * unlike probing manufacturer IDs. We do expect that system |
347 | | * firmware didn't write it in the past few milliseconds! |
348 | | */ |
349 | 0 | sr = spi_w8r8(at25->spi, AT25_RDSR); |
350 | 0 | if (sr < 0 || sr & AT25_SR_nRDY) { |
351 | 0 | dev_dbg(dev, "rdsr --> %d (%02x)\n", sr, sr); |
352 | 0 | err = -ENXIO; |
353 | 0 | goto fail; |
354 | 0 | } |
355 | | |
356 | 0 | err = devfs_create(&at25->cdev); |
357 | 0 | if (err) |
358 | 0 | goto fail; |
359 | | |
360 | 0 | dev_dbg(dev, "%s probed\n", at25->cdev.name); |
361 | 0 | of_parse_partitions(&at25->cdev, dev->of_node); |
362 | 0 | of_partitions_register_fixup(&at25->cdev); |
363 | |
|
364 | 0 | return 0; |
365 | | |
366 | 0 | fail: |
367 | 0 | free(at25); |
368 | |
|
369 | 0 | return err; |
370 | 0 | } |
371 | | |
372 | | static __maybe_unused struct of_device_id at25_dt_ids[] = { |
373 | | { |
374 | | .compatible = "atmel,at25", |
375 | | }, { |
376 | | /* sentinel */ |
377 | | } |
378 | | }; |
379 | | MODULE_DEVICE_TABLE(of, at25_dt_ids); |
380 | | |
381 | | static struct driver at25_driver = { |
382 | | .name = DRIVERNAME, |
383 | | .of_compatible = DRV_OF_COMPAT(at25_dt_ids), |
384 | | .probe = at25_probe, |
385 | | }; |
386 | | device_spi_driver(at25_driver); |