blob: 55fb4bd31ac1df1db0d162b3c18174968abc3d20 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haikun Wang89242852015-06-26 19:30:27 +08002/*
Haikun Wang89242852015-06-26 19:30:27 +08003 * Atmel DataFlash probing
4 *
5 * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
6 * Haikun Wang (haikun.wang@freescale.com)
Jagan Teki42a5f892016-10-30 23:16:30 +05307 */
8
Haikun Wang89242852015-06-26 19:30:27 +08009#include <common.h>
10#include <dm.h>
11#include <errno.h>
12#include <fdtdec.h>
13#include <spi.h>
14#include <spi_flash.h>
15#include <div64.h>
16#include <linux/err.h>
17#include <linux/math64.h>
18
19#include "sf_internal.h"
20
Vignesh R14510412019-02-05 11:29:23 +053021#define CMD_READ_ID 0x9f
Haikun Wang89242852015-06-26 19:30:27 +080022/* reads can bypass the buffers */
23#define OP_READ_CONTINUOUS 0xE8
24#define OP_READ_PAGE 0xD2
25
26/* group B requests can run even while status reports "busy" */
27#define OP_READ_STATUS 0xD7 /* group B */
28
29/* move data between host and buffer */
30#define OP_READ_BUFFER1 0xD4 /* group B */
31#define OP_READ_BUFFER2 0xD6 /* group B */
32#define OP_WRITE_BUFFER1 0x84 /* group B */
33#define OP_WRITE_BUFFER2 0x87 /* group B */
34
35/* erasing flash */
36#define OP_ERASE_PAGE 0x81
37#define OP_ERASE_BLOCK 0x50
38
39/* move data between buffer and flash */
40#define OP_TRANSFER_BUF1 0x53
41#define OP_TRANSFER_BUF2 0x55
42#define OP_MREAD_BUFFER1 0xD4
43#define OP_MREAD_BUFFER2 0xD6
44#define OP_MWERASE_BUFFER1 0x83
45#define OP_MWERASE_BUFFER2 0x86
46#define OP_MWRITE_BUFFER1 0x88 /* sector must be pre-erased */
47#define OP_MWRITE_BUFFER2 0x89 /* sector must be pre-erased */
48
49/* write to buffer, then write-erase to flash */
50#define OP_PROGRAM_VIA_BUF1 0x82
51#define OP_PROGRAM_VIA_BUF2 0x85
52
53/* compare buffer to flash */
54#define OP_COMPARE_BUF1 0x60
55#define OP_COMPARE_BUF2 0x61
56
57/* read flash to buffer, then write-erase to flash */
58#define OP_REWRITE_VIA_BUF1 0x58
59#define OP_REWRITE_VIA_BUF2 0x59
60
61/*
62 * newer chips report JEDEC manufacturer and device IDs; chip
63 * serial number and OTP bits; and per-sector writeprotect.
64 */
65#define OP_READ_ID 0x9F
66#define OP_READ_SECURITY 0x77
67#define OP_WRITE_SECURITY_REVC 0x9A
68#define OP_WRITE_SECURITY 0x9B /* revision D */
69
Haikun Wang89242852015-06-26 19:30:27 +080070struct dataflash {
71 uint8_t command[16];
72 unsigned short page_offset; /* offset in flash address */
73};
74
Jagan Teki42a5f892016-10-30 23:16:30 +053075/* Return the status of the DataFlash device */
Haikun Wang89242852015-06-26 19:30:27 +080076static inline int dataflash_status(struct spi_slave *spi)
77{
78 int ret;
Jagan Teki544a7b42019-07-22 17:22:57 +053079 u8 opcode = OP_READ_STATUS;
Haikun Wang89242852015-06-26 19:30:27 +080080 u8 status;
Jagan Teki544a7b42019-07-22 17:22:57 +053081
Haikun Wang89242852015-06-26 19:30:27 +080082 /*
83 * NOTE: at45db321c over 25 MHz wants to write
84 * a dummy byte after the opcode...
85 */
Jagan Teki544a7b42019-07-22 17:22:57 +053086 ret = spi_write_then_read(spi, &opcode, 1, NULL, &status, 1);
Haikun Wang89242852015-06-26 19:30:27 +080087 return ret ? -EIO : status;
88}
89
90/*
91 * Poll the DataFlash device until it is READY.
92 * This usually takes 5-20 msec or so; more for sector erase.
93 * ready: return > 0
94 */
95static int dataflash_waitready(struct spi_slave *spi)
96{
97 int status;
98 int timeout = 2 * CONFIG_SYS_HZ;
99 int timebase;
100
101 timebase = get_timer(0);
102 do {
103 status = dataflash_status(spi);
104 if (status < 0)
105 status = 0;
106
107 if (status & (1 << 7)) /* RDY/nBSY */
108 return status;
109
110 mdelay(3);
111 } while (get_timer(timebase) < timeout);
112
113 return -ETIME;
114}
115
Jagan Teki42a5f892016-10-30 23:16:30 +0530116/* Erase pages of flash */
Haikun Wang89242852015-06-26 19:30:27 +0800117static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
118{
119 struct dataflash *dataflash;
120 struct spi_flash *spi_flash;
121 struct spi_slave *spi;
122 unsigned blocksize;
123 uint8_t *command;
124 uint32_t rem;
125 int status;
126
127 dataflash = dev_get_priv(dev);
128 spi_flash = dev_get_uclass_priv(dev);
129 spi = spi_flash->spi;
130
131 blocksize = spi_flash->page_size << 3;
132
133 memset(dataflash->command, 0 , sizeof(dataflash->command));
134 command = dataflash->command;
135
136 debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
137
138 div_u64_rem(len, spi_flash->page_size, &rem);
Wenyou.Yang@microchip.com9015bb52017-07-21 13:26:09 +0800139 if (rem) {
140 printf("%s: len(0x%x) isn't the multiple of page size(0x%x)\n",
141 dev->name, len, spi_flash->page_size);
Haikun Wang89242852015-06-26 19:30:27 +0800142 return -EINVAL;
Wenyou.Yang@microchip.com9015bb52017-07-21 13:26:09 +0800143 }
Haikun Wang89242852015-06-26 19:30:27 +0800144 div_u64_rem(offset, spi_flash->page_size, &rem);
Wenyou.Yang@microchip.com9015bb52017-07-21 13:26:09 +0800145 if (rem) {
146 printf("%s: offset(0x%x) isn't the multiple of page size(0x%x)\n",
147 dev->name, offset, spi_flash->page_size);
Haikun Wang89242852015-06-26 19:30:27 +0800148 return -EINVAL;
Wenyou.Yang@microchip.com9015bb52017-07-21 13:26:09 +0800149 }
Haikun Wang89242852015-06-26 19:30:27 +0800150
151 status = spi_claim_bus(spi);
152 if (status) {
Jagan Teki42a5f892016-10-30 23:16:30 +0530153 debug("dataflash: unable to claim SPI bus\n");
Haikun Wang89242852015-06-26 19:30:27 +0800154 return status;
155 }
156
157 while (len > 0) {
158 unsigned int pageaddr;
159 int do_block;
160 /*
161 * Calculate flash page address; use block erase (for speed) if
162 * we're at a block boundary and need to erase the whole block.
163 */
164 pageaddr = div_u64(offset, spi_flash->page_size);
165 do_block = (pageaddr & 0x7) == 0 && len >= blocksize;
166 pageaddr = pageaddr << dataflash->page_offset;
167
168 command[0] = do_block ? OP_ERASE_BLOCK : OP_ERASE_PAGE;
169 command[1] = (uint8_t)(pageaddr >> 16);
170 command[2] = (uint8_t)(pageaddr >> 8);
171 command[3] = 0;
172
173 debug("%s ERASE %s: (%x) %x %x %x [%d]\n",
174 dev->name, do_block ? "block" : "page",
175 command[0], command[1], command[2], command[3],
176 pageaddr);
177
Jagan Teki544a7b42019-07-22 17:22:57 +0530178 status = spi_write_then_read(spi, command, 4, NULL, NULL, 0);
Haikun Wang89242852015-06-26 19:30:27 +0800179 if (status < 0) {
180 debug("%s: erase send command error!\n", dev->name);
181 return -EIO;
182 }
183
184 status = dataflash_waitready(spi);
185 if (status < 0) {
186 debug("%s: erase waitready error!\n", dev->name);
187 return status;
188 }
189
190 if (do_block) {
191 offset += blocksize;
192 len -= blocksize;
193 } else {
194 offset += spi_flash->page_size;
195 len -= spi_flash->page_size;
196 }
197 }
198
199 spi_release_bus(spi);
200
201 return 0;
202}
203
204/*
205 * Read from the DataFlash device.
206 * offset : Start offset in flash device
207 * len : Amount to read
208 * buf : Buffer containing the data
209 */
210static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
211 void *buf)
212{
213 struct dataflash *dataflash;
214 struct spi_flash *spi_flash;
215 struct spi_slave *spi;
216 unsigned int addr;
217 uint8_t *command;
218 int status;
219
220 dataflash = dev_get_priv(dev);
221 spi_flash = dev_get_uclass_priv(dev);
222 spi = spi_flash->spi;
223
224 memset(dataflash->command, 0 , sizeof(dataflash->command));
225 command = dataflash->command;
226
227 debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
228 debug("READ: (%x) %x %x %x\n",
229 command[0], command[1], command[2], command[3]);
230
231 /* Calculate flash page/byte address */
232 addr = (((unsigned)offset / spi_flash->page_size)
233 << dataflash->page_offset)
234 + ((unsigned)offset % spi_flash->page_size);
235
236 status = spi_claim_bus(spi);
237 if (status) {
Jagan Teki42a5f892016-10-30 23:16:30 +0530238 debug("dataflash: unable to claim SPI bus\n");
Haikun Wang89242852015-06-26 19:30:27 +0800239 return status;
240 }
241
242 /*
243 * Continuous read, max clock = f(car) which may be less than
244 * the peak rate available. Some chips support commands with
245 * fewer "don't care" bytes. Both buffers stay unchanged.
246 */
247 command[0] = OP_READ_CONTINUOUS;
248 command[1] = (uint8_t)(addr >> 16);
249 command[2] = (uint8_t)(addr >> 8);
250 command[3] = (uint8_t)(addr >> 0);
251
252 /* plus 4 "don't care" bytes, command len: 4 + 4 "don't care" bytes */
Jagan Teki544a7b42019-07-22 17:22:57 +0530253 status = spi_write_then_read(spi, command, 8, NULL, buf, len);
Haikun Wang89242852015-06-26 19:30:27 +0800254
255 spi_release_bus(spi);
256
257 return status;
258}
259
260/*
261 * Write to the DataFlash device.
262 * offset : Start offset in flash device
263 * len : Amount to write
264 * buf : Buffer containing the data
265 */
266int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
267 const void *buf)
268{
269 struct dataflash *dataflash;
270 struct spi_flash *spi_flash;
271 struct spi_slave *spi;
272 uint8_t *command;
273 unsigned int pageaddr, addr, to, writelen;
274 size_t remaining = len;
275 u_char *writebuf = (u_char *)buf;
276 int status = -EINVAL;
277
278 dataflash = dev_get_priv(dev);
279 spi_flash = dev_get_uclass_priv(dev);
280 spi = spi_flash->spi;
281
282 memset(dataflash->command, 0 , sizeof(dataflash->command));
283 command = dataflash->command;
284
285 debug("%s: write 0x%x..0x%x\n", dev->name, offset, (offset + len));
286
287 pageaddr = ((unsigned)offset / spi_flash->page_size);
288 to = ((unsigned)offset % spi_flash->page_size);
289 if (to + len > spi_flash->page_size)
290 writelen = spi_flash->page_size - to;
291 else
292 writelen = len;
293
294 status = spi_claim_bus(spi);
295 if (status) {
Jagan Teki42a5f892016-10-30 23:16:30 +0530296 debug("dataflash: unable to claim SPI bus\n");
Haikun Wang89242852015-06-26 19:30:27 +0800297 return status;
298 }
299
300 while (remaining > 0) {
301 debug("write @ %d:%d len=%d\n", pageaddr, to, writelen);
302
303 /*
304 * REVISIT:
305 * (a) each page in a sector must be rewritten at least
306 * once every 10K sibling erase/program operations.
307 * (b) for pages that are already erased, we could
308 * use WRITE+MWRITE not PROGRAM for ~30% speedup.
309 * (c) WRITE to buffer could be done while waiting for
310 * a previous MWRITE/MWERASE to complete ...
311 * (d) error handling here seems to be mostly missing.
312 *
313 * Two persistent bits per page, plus a per-sector counter,
314 * could support (a) and (b) ... we might consider using
315 * the second half of sector zero, which is just one block,
316 * to track that state. (On AT91, that sector should also
317 * support boot-from-DataFlash.)
318 */
319
320 addr = pageaddr << dataflash->page_offset;
321
322 /* (1) Maybe transfer partial page to Buffer1 */
323 if (writelen != spi_flash->page_size) {
324 command[0] = OP_TRANSFER_BUF1;
325 command[1] = (addr & 0x00FF0000) >> 16;
326 command[2] = (addr & 0x0000FF00) >> 8;
327 command[3] = 0;
328
329 debug("TRANSFER: (%x) %x %x %x\n",
330 command[0], command[1], command[2], command[3]);
331
Jagan Teki544a7b42019-07-22 17:22:57 +0530332 status = spi_write_then_read(spi, command, 4,
333 NULL, NULL, 0);
Haikun Wang89242852015-06-26 19:30:27 +0800334 if (status < 0) {
335 debug("%s: write(<pagesize) command error!\n",
336 dev->name);
337 return -EIO;
338 }
339
340 status = dataflash_waitready(spi);
341 if (status < 0) {
342 debug("%s: write(<pagesize) waitready error!\n",
343 dev->name);
344 return status;
345 }
346 }
347
348 /* (2) Program full page via Buffer1 */
349 addr += to;
350 command[0] = OP_PROGRAM_VIA_BUF1;
351 command[1] = (addr & 0x00FF0000) >> 16;
352 command[2] = (addr & 0x0000FF00) >> 8;
353 command[3] = (addr & 0x000000FF);
354
355 debug("PROGRAM: (%x) %x %x %x\n",
356 command[0], command[1], command[2], command[3]);
357
Jagan Teki544a7b42019-07-22 17:22:57 +0530358 status = spi_write_then_read(spi, command, 4,
359 writebuf, NULL, writelen);
Haikun Wang89242852015-06-26 19:30:27 +0800360 if (status < 0) {
361 debug("%s: write send command error!\n", dev->name);
362 return -EIO;
363 }
364
365 status = dataflash_waitready(spi);
366 if (status < 0) {
367 debug("%s: write waitready error!\n", dev->name);
368 return status;
369 }
370
371#ifdef CONFIG_SPI_DATAFLASH_WRITE_VERIFY
372 /* (3) Compare to Buffer1 */
373 addr = pageaddr << dataflash->page_offset;
374 command[0] = OP_COMPARE_BUF1;
375 command[1] = (addr & 0x00FF0000) >> 16;
376 command[2] = (addr & 0x0000FF00) >> 8;
377 command[3] = 0;
378
379 debug("COMPARE: (%x) %x %x %x\n",
380 command[0], command[1], command[2], command[3]);
381
Jagan Teki544a7b42019-07-22 17:22:57 +0530382 status = spi_write_then_read(spi, command, 4,
383 writebuf, NULL, writelen);
Haikun Wang89242852015-06-26 19:30:27 +0800384 if (status < 0) {
385 debug("%s: write(compare) send command error!\n",
386 dev->name);
387 return -EIO;
388 }
389
390 status = dataflash_waitready(spi);
391
392 /* Check result of the compare operation */
393 if (status & (1 << 6)) {
Jagan Teki42a5f892016-10-30 23:16:30 +0530394 printf("dataflash: write compare page %u, err %d\n",
Haikun Wang89242852015-06-26 19:30:27 +0800395 pageaddr, status);
396 remaining = 0;
397 status = -EIO;
398 break;
399 } else {
400 status = 0;
401 }
402
403#endif /* CONFIG_SPI_DATAFLASH_WRITE_VERIFY */
404 remaining = remaining - writelen;
405 pageaddr++;
406 to = 0;
407 writebuf += writelen;
408
409 if (remaining > spi_flash->page_size)
410 writelen = spi_flash->page_size;
411 else
412 writelen = remaining;
413 }
414
415 spi_release_bus(spi);
416
417 return 0;
418}
419
420static int add_dataflash(struct udevice *dev, char *name, int nr_pages,
421 int pagesize, int pageoffset, char revision)
422{
423 struct spi_flash *spi_flash;
424 struct dataflash *dataflash;
425
426 dataflash = dev_get_priv(dev);
427 spi_flash = dev_get_uclass_priv(dev);
428
429 dataflash->page_offset = pageoffset;
430
431 spi_flash->name = name;
432 spi_flash->page_size = pagesize;
433 spi_flash->size = nr_pages * pagesize;
434 spi_flash->erase_size = pagesize;
435
436#ifndef CONFIG_SPL_BUILD
437 printf("SPI DataFlash: Detected %s with page size ", spi_flash->name);
438 print_size(spi_flash->page_size, ", erase size ");
439 print_size(spi_flash->erase_size, ", total ");
440 print_size(spi_flash->size, "");
441 printf(", revision %c", revision);
442 puts("\n");
443#endif
444
445 return 0;
446}
447
Vignesh R14510412019-02-05 11:29:23 +0530448struct data_flash_info {
Haikun Wang89242852015-06-26 19:30:27 +0800449 char *name;
450
451 /*
452 * JEDEC id has a high byte of zero plus three data bytes:
453 * the manufacturer id, then a two byte device id.
454 */
455 uint32_t jedec_id;
456
457 /* The size listed here is what works with OP_ERASE_PAGE. */
458 unsigned nr_pages;
459 uint16_t pagesize;
460 uint16_t pageoffset;
461
462 uint16_t flags;
463#define SUP_POW2PS 0x0002 /* supports 2^N byte pages */
464#define IS_POW2PS 0x0001 /* uses 2^N byte pages */
465};
466
Vignesh R14510412019-02-05 11:29:23 +0530467static struct data_flash_info dataflash_data[] = {
Haikun Wang89242852015-06-26 19:30:27 +0800468 /*
469 * NOTE: chips with SUP_POW2PS (rev D and up) need two entries,
470 * one with IS_POW2PS and the other without. The entry with the
471 * non-2^N byte page size can't name exact chip revisions without
472 * losing backwards compatibility for cmdlinepart.
473 *
474 * Those two entries have different name spelling format in order to
475 * show their difference obviously.
476 * The upper case refer to the chip isn't in normal 2^N bytes page-size
477 * mode.
478 * The lower case refer to the chip is in normal 2^N bytes page-size
479 * mode.
480 *
481 * These newer chips also support 128-byte security registers (with
482 * 64 bytes one-time-programmable) and software write-protection.
483 */
484 { "AT45DB011B", 0x1f2200, 512, 264, 9, SUP_POW2PS},
485 { "at45db011d", 0x1f2200, 512, 256, 8, SUP_POW2PS | IS_POW2PS},
486
487 { "AT45DB021B", 0x1f2300, 1024, 264, 9, SUP_POW2PS},
488 { "at45db021d", 0x1f2300, 1024, 256, 8, SUP_POW2PS | IS_POW2PS},
489
490 { "AT45DB041x", 0x1f2400, 2048, 264, 9, SUP_POW2PS},
491 { "at45db041d", 0x1f2400, 2048, 256, 8, SUP_POW2PS | IS_POW2PS},
492
493 { "AT45DB081B", 0x1f2500, 4096, 264, 9, SUP_POW2PS},
494 { "at45db081d", 0x1f2500, 4096, 256, 8, SUP_POW2PS | IS_POW2PS},
495
496 { "AT45DB161x", 0x1f2600, 4096, 528, 10, SUP_POW2PS},
497 { "at45db161d", 0x1f2600, 4096, 512, 9, SUP_POW2PS | IS_POW2PS},
498
499 { "AT45DB321x", 0x1f2700, 8192, 528, 10, 0}, /* rev C */
500
501 { "AT45DB321x", 0x1f2701, 8192, 528, 10, SUP_POW2PS},
502 { "at45db321d", 0x1f2701, 8192, 512, 9, SUP_POW2PS | IS_POW2PS},
503
504 { "AT45DB642x", 0x1f2800, 8192, 1056, 11, SUP_POW2PS},
505 { "at45db642d", 0x1f2800, 8192, 1024, 10, SUP_POW2PS | IS_POW2PS},
506};
507
Vignesh R14510412019-02-05 11:29:23 +0530508static struct data_flash_info *jedec_probe(struct spi_slave *spi)
Haikun Wang89242852015-06-26 19:30:27 +0800509{
510 int tmp;
Jagan Teki7af73ee2016-10-30 23:16:28 +0530511 uint8_t id[5];
Haikun Wang89242852015-06-26 19:30:27 +0800512 uint32_t jedec;
Vignesh R14510412019-02-05 11:29:23 +0530513 struct data_flash_info *info;
Jagan Teki544a7b42019-07-22 17:22:57 +0530514 u8 opcode = CMD_READ_ID;
Haikun Wang89242852015-06-26 19:30:27 +0800515 int status;
516
517 /*
518 * JEDEC also defines an optional "extended device information"
519 * string for after vendor-specific data, after the three bytes
520 * we use here. Supporting some chips might require using it.
521 *
522 * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
523 * That's not an error; only rev C and newer chips handle it, and
524 * only Atmel sells these chips.
525 */
Jagan Teki544a7b42019-07-22 17:22:57 +0530526 tmp = spi_write_then_read(spi, &opcode, 1, NULL, id, sizeof(id));
Jagan Teki7af73ee2016-10-30 23:16:28 +0530527 if (tmp < 0) {
528 printf("dataflash: error %d reading JEDEC ID\n", tmp);
529 return ERR_PTR(tmp);
530 }
Haikun Wang89242852015-06-26 19:30:27 +0800531 if (id[0] != 0x1f)
532 return NULL;
533
534 jedec = id[0];
535 jedec = jedec << 8;
536 jedec |= id[1];
537 jedec = jedec << 8;
538 jedec |= id[2];
539
540 for (tmp = 0, info = dataflash_data;
541 tmp < ARRAY_SIZE(dataflash_data);
542 tmp++, info++) {
543 if (info->jedec_id == jedec) {
544 if (info->flags & SUP_POW2PS) {
545 status = dataflash_status(spi);
546 if (status < 0) {
Jagan Teki42a5f892016-10-30 23:16:30 +0530547 debug("dataflash: status error %d\n",
Haikun Wang89242852015-06-26 19:30:27 +0800548 status);
549 return NULL;
550 }
551 if (status & 0x1) {
552 if (info->flags & IS_POW2PS)
553 return info;
554 } else {
555 if (!(info->flags & IS_POW2PS))
556 return info;
557 }
558 } else {
559 return info;
560 }
561 }
562 }
563
564 /*
565 * Treat other chips as errors ... we won't know the right page
566 * size (it might be binary) even when we can tell which density
567 * class is involved (legacy chip id scheme).
568 */
Jagan Teki42a5f892016-10-30 23:16:30 +0530569 printf("dataflash: JEDEC id %06x not handled\n", jedec);
570 return ERR_PTR(-ENODEV);
Haikun Wang89242852015-06-26 19:30:27 +0800571}
572
573/*
574 * Detect and initialize DataFlash device, using JEDEC IDs on newer chips
575 * or else the ID code embedded in the status bits:
576 *
577 * Device Density ID code #Pages PageSize Offset
578 * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
579 * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1024 264 9
580 * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
581 * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
582 * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
583 * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
584 * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
585 * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
586 */
587static int spi_dataflash_probe(struct udevice *dev)
588{
Simon Glassde44acf2015-09-28 23:32:01 -0600589 struct spi_slave *spi = dev_get_parent_priv(dev);
Haikun Wang89242852015-06-26 19:30:27 +0800590 struct spi_flash *spi_flash;
Vignesh R14510412019-02-05 11:29:23 +0530591 struct data_flash_info *info;
Jagan Teki54111f72016-10-30 23:16:29 +0530592 int status;
Haikun Wang89242852015-06-26 19:30:27 +0800593
594 spi_flash = dev_get_uclass_priv(dev);
Jagan Tekibf7adaf2016-10-30 23:16:27 +0530595 spi_flash->spi = spi;
Haikun Wang89242852015-06-26 19:30:27 +0800596 spi_flash->dev = dev;
597
Jagan Teki54111f72016-10-30 23:16:29 +0530598 status = spi_claim_bus(spi);
599 if (status)
600 return status;
Haikun Wang89242852015-06-26 19:30:27 +0800601
Haikun Wang89242852015-06-26 19:30:27 +0800602 /*
603 * Try to detect dataflash by JEDEC ID.
604 * If it succeeds we know we have either a C or D part.
605 * D will support power of 2 pagesize option.
606 * Both support the security register, though with different
607 * write procedures.
608 */
Jagan Teki7af73ee2016-10-30 23:16:28 +0530609 info = jedec_probe(spi);
610 if (IS_ERR(info))
Jagan Teki54111f72016-10-30 23:16:29 +0530611 goto err_jedec_probe;
612 if (info != NULL) {
613 status = add_dataflash(dev, info->name, info->nr_pages,
614 info->pagesize, info->pageoffset,
615 (info->flags & SUP_POW2PS) ? 'd' : 'c');
616 if (status < 0)
617 goto err_status;
618 }
619
Jagan Teki42a5f892016-10-30 23:16:30 +0530620 /*
Jagan Teki54111f72016-10-30 23:16:29 +0530621 * Older chips support only legacy commands, identifing
622 * capacity using bits in the status byte.
623 */
624 status = dataflash_status(spi);
625 if (status <= 0 || status == 0xff) {
Jagan Teki42a5f892016-10-30 23:16:30 +0530626 printf("dataflash: read status error %d\n", status);
Jagan Teki54111f72016-10-30 23:16:29 +0530627 if (status == 0 || status == 0xff)
Haikun Wang89242852015-06-26 19:30:27 +0800628 status = -ENODEV;
Jagan Teki54111f72016-10-30 23:16:29 +0530629 goto err_jedec_probe;
Haikun Wang89242852015-06-26 19:30:27 +0800630 }
631
Jagan Teki42a5f892016-10-30 23:16:30 +0530632 /*
Jagan Teki54111f72016-10-30 23:16:29 +0530633 * if there's a device there, assume it's dataflash.
634 * board setup should have set spi->max_speed_max to
635 * match f(car) for continuous reads, mode 0 or 3.
636 */
637 switch (status & 0x3c) {
638 case 0x0c: /* 0 0 1 1 x x */
639 status = add_dataflash(dev, "AT45DB011B", 512, 264, 9, 0);
640 break;
641 case 0x14: /* 0 1 0 1 x x */
642 status = add_dataflash(dev, "AT45DB021B", 1024, 264, 9, 0);
643 break;
644 case 0x1c: /* 0 1 1 1 x x */
645 status = add_dataflash(dev, "AT45DB041x", 2048, 264, 9, 0);
646 break;
647 case 0x24: /* 1 0 0 1 x x */
648 status = add_dataflash(dev, "AT45DB081B", 4096, 264, 9, 0);
649 break;
650 case 0x2c: /* 1 0 1 1 x x */
651 status = add_dataflash(dev, "AT45DB161x", 4096, 528, 10, 0);
652 break;
653 case 0x34: /* 1 1 0 1 x x */
654 status = add_dataflash(dev, "AT45DB321x", 8192, 528, 10, 0);
655 break;
656 case 0x38: /* 1 1 1 x x x */
657 case 0x3c:
658 status = add_dataflash(dev, "AT45DB642x", 8192, 1056, 11, 0);
659 break;
660 /* obsolete AT45DB1282 not (yet?) supported */
661 default:
Jagan Teki42a5f892016-10-30 23:16:30 +0530662 printf("dataflash: unsupported device (%x)\n", status & 0x3c);
Jagan Teki54111f72016-10-30 23:16:29 +0530663 status = -ENODEV;
664 goto err_status;
665 }
Haikun Wang89242852015-06-26 19:30:27 +0800666
Jagan Teki54111f72016-10-30 23:16:29 +0530667 return status;
Haikun Wang89242852015-06-26 19:30:27 +0800668
Jagan Teki54111f72016-10-30 23:16:29 +0530669err_status:
670 spi_free_slave(spi);
671err_jedec_probe:
Haikun Wang89242852015-06-26 19:30:27 +0800672 spi_release_bus(spi);
Haikun Wang89242852015-06-26 19:30:27 +0800673 return status;
674}
675
676static const struct dm_spi_flash_ops spi_dataflash_ops = {
677 .read = spi_dataflash_read,
678 .write = spi_dataflash_write,
679 .erase = spi_dataflash_erase,
680};
681
682static const struct udevice_id spi_dataflash_ids[] = {
683 { .compatible = "atmel,at45", },
684 { .compatible = "atmel,dataflash", },
685 { }
686};
687
688U_BOOT_DRIVER(spi_dataflash) = {
689 .name = "spi_dataflash",
690 .id = UCLASS_SPI_FLASH,
691 .of_match = spi_dataflash_ids,
692 .probe = spi_dataflash_probe,
693 .priv_auto_alloc_size = sizeof(struct dataflash),
694 .ops = &spi_dataflash_ops,
695};