sf_dataflash.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. *
  3. * Atmel DataFlash probing
  4. *
  5. * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
  6. * Haikun Wang (haikun.wang@freescale.com)
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <fdtdec.h>
  14. #include <spi.h>
  15. #include <spi_flash.h>
  16. #include <div64.h>
  17. #include <linux/err.h>
  18. #include <linux/math64.h>
  19. #include "sf_internal.h"
  20. /* reads can bypass the buffers */
  21. #define OP_READ_CONTINUOUS 0xE8
  22. #define OP_READ_PAGE 0xD2
  23. /* group B requests can run even while status reports "busy" */
  24. #define OP_READ_STATUS 0xD7 /* group B */
  25. /* move data between host and buffer */
  26. #define OP_READ_BUFFER1 0xD4 /* group B */
  27. #define OP_READ_BUFFER2 0xD6 /* group B */
  28. #define OP_WRITE_BUFFER1 0x84 /* group B */
  29. #define OP_WRITE_BUFFER2 0x87 /* group B */
  30. /* erasing flash */
  31. #define OP_ERASE_PAGE 0x81
  32. #define OP_ERASE_BLOCK 0x50
  33. /* move data between buffer and flash */
  34. #define OP_TRANSFER_BUF1 0x53
  35. #define OP_TRANSFER_BUF2 0x55
  36. #define OP_MREAD_BUFFER1 0xD4
  37. #define OP_MREAD_BUFFER2 0xD6
  38. #define OP_MWERASE_BUFFER1 0x83
  39. #define OP_MWERASE_BUFFER2 0x86
  40. #define OP_MWRITE_BUFFER1 0x88 /* sector must be pre-erased */
  41. #define OP_MWRITE_BUFFER2 0x89 /* sector must be pre-erased */
  42. /* write to buffer, then write-erase to flash */
  43. #define OP_PROGRAM_VIA_BUF1 0x82
  44. #define OP_PROGRAM_VIA_BUF2 0x85
  45. /* compare buffer to flash */
  46. #define OP_COMPARE_BUF1 0x60
  47. #define OP_COMPARE_BUF2 0x61
  48. /* read flash to buffer, then write-erase to flash */
  49. #define OP_REWRITE_VIA_BUF1 0x58
  50. #define OP_REWRITE_VIA_BUF2 0x59
  51. /*
  52. * newer chips report JEDEC manufacturer and device IDs; chip
  53. * serial number and OTP bits; and per-sector writeprotect.
  54. */
  55. #define OP_READ_ID 0x9F
  56. #define OP_READ_SECURITY 0x77
  57. #define OP_WRITE_SECURITY_REVC 0x9A
  58. #define OP_WRITE_SECURITY 0x9B /* revision D */
  59. struct dataflash {
  60. uint8_t command[16];
  61. unsigned short page_offset; /* offset in flash address */
  62. };
  63. /*
  64. * Return the status of the DataFlash device.
  65. */
  66. static inline int dataflash_status(struct spi_slave *spi)
  67. {
  68. int ret;
  69. u8 status;
  70. /*
  71. * NOTE: at45db321c over 25 MHz wants to write
  72. * a dummy byte after the opcode...
  73. */
  74. ret = spi_flash_cmd(spi, OP_READ_STATUS, &status, 1);
  75. return ret ? -EIO : status;
  76. }
  77. /*
  78. * Poll the DataFlash device until it is READY.
  79. * This usually takes 5-20 msec or so; more for sector erase.
  80. * ready: return > 0
  81. */
  82. static int dataflash_waitready(struct spi_slave *spi)
  83. {
  84. int status;
  85. int timeout = 2 * CONFIG_SYS_HZ;
  86. int timebase;
  87. timebase = get_timer(0);
  88. do {
  89. status = dataflash_status(spi);
  90. if (status < 0)
  91. status = 0;
  92. if (status & (1 << 7)) /* RDY/nBSY */
  93. return status;
  94. mdelay(3);
  95. } while (get_timer(timebase) < timeout);
  96. return -ETIME;
  97. }
  98. /*
  99. * Erase pages of flash.
  100. */
  101. static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
  102. {
  103. struct dataflash *dataflash;
  104. struct spi_flash *spi_flash;
  105. struct spi_slave *spi;
  106. unsigned blocksize;
  107. uint8_t *command;
  108. uint32_t rem;
  109. int status;
  110. dataflash = dev_get_priv(dev);
  111. spi_flash = dev_get_uclass_priv(dev);
  112. spi = spi_flash->spi;
  113. blocksize = spi_flash->page_size << 3;
  114. memset(dataflash->command, 0 , sizeof(dataflash->command));
  115. command = dataflash->command;
  116. debug("%s: erase addr=0x%x len 0x%x\n", dev->name, offset, len);
  117. div_u64_rem(len, spi_flash->page_size, &rem);
  118. if (rem)
  119. return -EINVAL;
  120. div_u64_rem(offset, spi_flash->page_size, &rem);
  121. if (rem)
  122. return -EINVAL;
  123. status = spi_claim_bus(spi);
  124. if (status) {
  125. debug("SPI 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("SPI 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("SPI 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("SPI 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, u8 *id)
  417. {
  418. int tmp;
  419. uint32_t jedec;
  420. struct flash_info *info;
  421. int status;
  422. /*
  423. * JEDEC also defines an optional "extended device information"
  424. * string for after vendor-specific data, after the three bytes
  425. * we use here. Supporting some chips might require using it.
  426. *
  427. * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
  428. * That's not an error; only rev C and newer chips handle it, and
  429. * only Atmel sells these chips.
  430. */
  431. if (id[0] != 0x1f)
  432. return NULL;
  433. jedec = id[0];
  434. jedec = jedec << 8;
  435. jedec |= id[1];
  436. jedec = jedec << 8;
  437. jedec |= id[2];
  438. for (tmp = 0, info = dataflash_data;
  439. tmp < ARRAY_SIZE(dataflash_data);
  440. tmp++, info++) {
  441. if (info->jedec_id == jedec) {
  442. if (info->flags & SUP_POW2PS) {
  443. status = dataflash_status(spi);
  444. if (status < 0) {
  445. debug("SPI DataFlash: status error %d\n",
  446. status);
  447. return NULL;
  448. }
  449. if (status & 0x1) {
  450. if (info->flags & IS_POW2PS)
  451. return info;
  452. } else {
  453. if (!(info->flags & IS_POW2PS))
  454. return info;
  455. }
  456. } else {
  457. return info;
  458. }
  459. }
  460. }
  461. /*
  462. * Treat other chips as errors ... we won't know the right page
  463. * size (it might be binary) even when we can tell which density
  464. * class is involved (legacy chip id scheme).
  465. */
  466. printf("SPI DataFlash: Unsupported flash IDs: ");
  467. printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
  468. id[0], jedec, id[3] << 8 | id[4]);
  469. return NULL;
  470. }
  471. /*
  472. * Detect and initialize DataFlash device, using JEDEC IDs on newer chips
  473. * or else the ID code embedded in the status bits:
  474. *
  475. * Device Density ID code #Pages PageSize Offset
  476. * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
  477. * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1024 264 9
  478. * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
  479. * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
  480. * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
  481. * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
  482. * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
  483. * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
  484. */
  485. static int spi_dataflash_probe(struct udevice *dev)
  486. {
  487. struct spi_slave *spi = dev_get_parent_priv(dev);
  488. struct spi_flash *spi_flash;
  489. struct flash_info *info;
  490. u8 idcode[5];
  491. int ret, status = 0;
  492. spi_flash = dev_get_uclass_priv(dev);
  493. spi_flash->dev = dev;
  494. ret = spi_claim_bus(spi);
  495. if (ret)
  496. return ret;
  497. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  498. if (ret) {
  499. printf("SPI DataFlash: Failed to get idcodes\n");
  500. goto err_read_cmd;
  501. }
  502. /*
  503. * Try to detect dataflash by JEDEC ID.
  504. * If it succeeds we know we have either a C or D part.
  505. * D will support power of 2 pagesize option.
  506. * Both support the security register, though with different
  507. * write procedures.
  508. */
  509. info = jedec_probe(spi, idcode);
  510. if (info != NULL)
  511. add_dataflash(dev, info->name, info->nr_pages,
  512. info->pagesize, info->pageoffset,
  513. (info->flags & SUP_POW2PS) ? 'd' : 'c');
  514. else {
  515. /*
  516. * Older chips support only legacy commands, identifing
  517. * capacity using bits in the status byte.
  518. */
  519. status = dataflash_status(spi);
  520. if (status <= 0 || status == 0xff) {
  521. printf("SPI DataFlash: read status error %d\n", status);
  522. if (status == 0 || status == 0xff)
  523. status = -ENODEV;
  524. goto err_read_cmd;
  525. }
  526. /*
  527. * if there's a device there, assume it's dataflash.
  528. * board setup should have set spi->max_speed_max to
  529. * match f(car) for continuous reads, mode 0 or 3.
  530. */
  531. switch (status & 0x3c) {
  532. case 0x0c: /* 0 0 1 1 x x */
  533. status = add_dataflash(dev, "AT45DB011B",
  534. 512, 264, 9, 0);
  535. break;
  536. case 0x14: /* 0 1 0 1 x x */
  537. status = add_dataflash(dev, "AT45DB021B",
  538. 1024, 264, 9, 0);
  539. break;
  540. case 0x1c: /* 0 1 1 1 x x */
  541. status = add_dataflash(dev, "AT45DB041x",
  542. 2048, 264, 9, 0);
  543. break;
  544. case 0x24: /* 1 0 0 1 x x */
  545. status = add_dataflash(dev, "AT45DB081B",
  546. 4096, 264, 9, 0);
  547. break;
  548. case 0x2c: /* 1 0 1 1 x x */
  549. status = add_dataflash(dev, "AT45DB161x",
  550. 4096, 528, 10, 0);
  551. break;
  552. case 0x34: /* 1 1 0 1 x x */
  553. status = add_dataflash(dev, "AT45DB321x",
  554. 8192, 528, 10, 0);
  555. break;
  556. case 0x38: /* 1 1 1 x x x */
  557. case 0x3c:
  558. status = add_dataflash(dev, "AT45DB642x",
  559. 8192, 1056, 11, 0);
  560. break;
  561. /* obsolete AT45DB1282 not (yet?) supported */
  562. default:
  563. dev_info(&spi->dev, "unsupported device (%x)\n",
  564. status & 0x3c);
  565. status = -ENODEV;
  566. goto err_read_cmd;
  567. }
  568. }
  569. /* Assign spi data */
  570. spi_flash->spi = spi;
  571. spi_flash->memory_map = spi->memory_map;
  572. spi_flash->dual_flash = spi->option;
  573. spi_release_bus(spi);
  574. return 0;
  575. err_read_cmd:
  576. spi_release_bus(spi);
  577. return status;
  578. }
  579. static const struct dm_spi_flash_ops spi_dataflash_ops = {
  580. .read = spi_dataflash_read,
  581. .write = spi_dataflash_write,
  582. .erase = spi_dataflash_erase,
  583. };
  584. static const struct udevice_id spi_dataflash_ids[] = {
  585. { .compatible = "atmel,at45", },
  586. { .compatible = "atmel,dataflash", },
  587. { }
  588. };
  589. U_BOOT_DRIVER(spi_dataflash) = {
  590. .name = "spi_dataflash",
  591. .id = UCLASS_SPI_FLASH,
  592. .of_match = spi_dataflash_ids,
  593. .probe = spi_dataflash_probe,
  594. .priv_auto_alloc_size = sizeof(struct dataflash),
  595. .ops = &spi_dataflash_ops,
  596. };