sf_dataflash.c 18 KB

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