sf_dataflash.c 18 KB

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