mtd_dataflash.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Atmel AT45xxx DataFlash MTD driver for lightweight SPI framework
  4. *
  5. * Largely derived from at91_dataflash.c:
  6. * Copyright (C) 2003-2005 SAN People (Pty) Ltd
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/mutex.h>
  13. #include <linux/err.h>
  14. #include <linux/math64.h>
  15. #include <linux/of.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/spi/flash.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. /*
  21. * DataFlash is a kind of SPI flash. Most AT45 chips have two buffers in
  22. * each chip, which may be used for double buffered I/O; but this driver
  23. * doesn't (yet) use these for any kind of i/o overlap or prefetching.
  24. *
  25. * Sometimes DataFlash is packaged in MMC-format cards, although the
  26. * MMC stack can't (yet?) distinguish between MMC and DataFlash
  27. * protocols during enumeration.
  28. */
  29. /* reads can bypass the buffers */
  30. #define OP_READ_CONTINUOUS 0xE8
  31. #define OP_READ_PAGE 0xD2
  32. /* group B requests can run even while status reports "busy" */
  33. #define OP_READ_STATUS 0xD7 /* group B */
  34. /* move data between host and buffer */
  35. #define OP_READ_BUFFER1 0xD4 /* group B */
  36. #define OP_READ_BUFFER2 0xD6 /* group B */
  37. #define OP_WRITE_BUFFER1 0x84 /* group B */
  38. #define OP_WRITE_BUFFER2 0x87 /* group B */
  39. /* erasing flash */
  40. #define OP_ERASE_PAGE 0x81
  41. #define OP_ERASE_BLOCK 0x50
  42. /* move data between buffer and flash */
  43. #define OP_TRANSFER_BUF1 0x53
  44. #define OP_TRANSFER_BUF2 0x55
  45. #define OP_MREAD_BUFFER1 0xD4
  46. #define OP_MREAD_BUFFER2 0xD6
  47. #define OP_MWERASE_BUFFER1 0x83
  48. #define OP_MWERASE_BUFFER2 0x86
  49. #define OP_MWRITE_BUFFER1 0x88 /* sector must be pre-erased */
  50. #define OP_MWRITE_BUFFER2 0x89 /* sector must be pre-erased */
  51. /* write to buffer, then write-erase to flash */
  52. #define OP_PROGRAM_VIA_BUF1 0x82
  53. #define OP_PROGRAM_VIA_BUF2 0x85
  54. /* compare buffer to flash */
  55. #define OP_COMPARE_BUF1 0x60
  56. #define OP_COMPARE_BUF2 0x61
  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. /* newer chips report JEDEC manufacturer and device IDs; chip
  61. * serial number and OTP bits; and per-sector writeprotect.
  62. */
  63. #define OP_READ_ID 0x9F
  64. #define OP_READ_SECURITY 0x77
  65. #define OP_WRITE_SECURITY_REVC 0x9A
  66. #define OP_WRITE_SECURITY 0x9B /* revision D */
  67. #define CFI_MFR_ATMEL 0x1F
  68. #define DATAFLASH_SHIFT_EXTID 24
  69. #define DATAFLASH_SHIFT_ID 40
  70. struct dataflash {
  71. u8 command[4];
  72. char name[24];
  73. unsigned short page_offset; /* offset in flash address */
  74. unsigned int page_size; /* of bytes per page */
  75. struct mutex lock;
  76. struct spi_device *spi;
  77. struct mtd_info mtd;
  78. };
  79. #ifdef CONFIG_OF
  80. static const struct of_device_id dataflash_dt_ids[] = {
  81. { .compatible = "atmel,at45", },
  82. { .compatible = "atmel,dataflash", },
  83. { /* sentinel */ }
  84. };
  85. MODULE_DEVICE_TABLE(of, dataflash_dt_ids);
  86. #endif
  87. static const struct spi_device_id dataflash_spi_ids[] = {
  88. { .name = "at45", },
  89. { .name = "dataflash", },
  90. { /* sentinel */ }
  91. };
  92. MODULE_DEVICE_TABLE(spi, dataflash_spi_ids);
  93. /* ......................................................................... */
  94. /*
  95. * Return the status of the DataFlash device.
  96. */
  97. static inline int dataflash_status(struct spi_device *spi)
  98. {
  99. /* NOTE: at45db321c over 25 MHz wants to write
  100. * a dummy byte after the opcode...
  101. */
  102. return spi_w8r8(spi, OP_READ_STATUS);
  103. }
  104. /*
  105. * Poll the DataFlash device until it is READY.
  106. * This usually takes 5-20 msec or so; more for sector erase.
  107. */
  108. static int dataflash_waitready(struct spi_device *spi)
  109. {
  110. int status;
  111. for (;;) {
  112. status = dataflash_status(spi);
  113. if (status < 0) {
  114. dev_dbg(&spi->dev, "status %d?\n", status);
  115. status = 0;
  116. }
  117. if (status & (1 << 7)) /* RDY/nBSY */
  118. return status;
  119. usleep_range(3000, 4000);
  120. }
  121. }
  122. /* ......................................................................... */
  123. /*
  124. * Erase pages of flash.
  125. */
  126. static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
  127. {
  128. struct dataflash *priv = mtd->priv;
  129. struct spi_device *spi = priv->spi;
  130. struct spi_transfer x = { };
  131. struct spi_message msg;
  132. unsigned blocksize = priv->page_size << 3;
  133. u8 *command;
  134. u32 rem;
  135. dev_dbg(&spi->dev, "erase addr=0x%llx len 0x%llx\n",
  136. (long long)instr->addr, (long long)instr->len);
  137. div_u64_rem(instr->len, priv->page_size, &rem);
  138. if (rem)
  139. return -EINVAL;
  140. div_u64_rem(instr->addr, priv->page_size, &rem);
  141. if (rem)
  142. return -EINVAL;
  143. spi_message_init(&msg);
  144. x.tx_buf = command = priv->command;
  145. x.len = 4;
  146. spi_message_add_tail(&x, &msg);
  147. mutex_lock(&priv->lock);
  148. while (instr->len > 0) {
  149. unsigned int pageaddr;
  150. int status;
  151. int do_block;
  152. /* Calculate flash page address; use block erase (for speed) if
  153. * we're at a block boundary and need to erase the whole block.
  154. */
  155. pageaddr = div_u64(instr->addr, priv->page_size);
  156. do_block = (pageaddr & 0x7) == 0 && instr->len >= blocksize;
  157. pageaddr = pageaddr << priv->page_offset;
  158. command[0] = do_block ? OP_ERASE_BLOCK : OP_ERASE_PAGE;
  159. command[1] = (u8)(pageaddr >> 16);
  160. command[2] = (u8)(pageaddr >> 8);
  161. command[3] = 0;
  162. dev_dbg(&spi->dev, "ERASE %s: (%x) %x %x %x [%i]\n",
  163. do_block ? "block" : "page",
  164. command[0], command[1], command[2], command[3],
  165. pageaddr);
  166. status = spi_sync(spi, &msg);
  167. (void) dataflash_waitready(spi);
  168. if (status < 0) {
  169. dev_err(&spi->dev, "erase %x, err %d\n",
  170. pageaddr, status);
  171. /* REVISIT: can retry instr->retries times; or
  172. * giveup and instr->fail_addr = instr->addr;
  173. */
  174. continue;
  175. }
  176. if (do_block) {
  177. instr->addr += blocksize;
  178. instr->len -= blocksize;
  179. } else {
  180. instr->addr += priv->page_size;
  181. instr->len -= priv->page_size;
  182. }
  183. }
  184. mutex_unlock(&priv->lock);
  185. return 0;
  186. }
  187. /*
  188. * Read from the DataFlash device.
  189. * from : Start offset in flash device
  190. * len : Amount to read
  191. * retlen : About of data actually read
  192. * buf : Buffer containing the data
  193. */
  194. static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
  195. size_t *retlen, u_char *buf)
  196. {
  197. struct dataflash *priv = mtd->priv;
  198. struct spi_transfer x[2] = { };
  199. struct spi_message msg;
  200. unsigned int addr;
  201. u8 *command;
  202. int status;
  203. dev_dbg(&priv->spi->dev, "read 0x%x..0x%x\n",
  204. (unsigned int)from, (unsigned int)(from + len));
  205. /* Calculate flash page/byte address */
  206. addr = (((unsigned)from / priv->page_size) << priv->page_offset)
  207. + ((unsigned)from % priv->page_size);
  208. command = priv->command;
  209. dev_dbg(&priv->spi->dev, "READ: (%x) %x %x %x\n",
  210. command[0], command[1], command[2], command[3]);
  211. spi_message_init(&msg);
  212. x[0].tx_buf = command;
  213. x[0].len = 8;
  214. spi_message_add_tail(&x[0], &msg);
  215. x[1].rx_buf = buf;
  216. x[1].len = len;
  217. spi_message_add_tail(&x[1], &msg);
  218. mutex_lock(&priv->lock);
  219. /* Continuous read, max clock = f(car) which may be less than
  220. * the peak rate available. Some chips support commands with
  221. * fewer "don't care" bytes. Both buffers stay unchanged.
  222. */
  223. command[0] = OP_READ_CONTINUOUS;
  224. command[1] = (u8)(addr >> 16);
  225. command[2] = (u8)(addr >> 8);
  226. command[3] = (u8)(addr >> 0);
  227. /* plus 4 "don't care" bytes */
  228. status = spi_sync(priv->spi, &msg);
  229. mutex_unlock(&priv->lock);
  230. if (status >= 0) {
  231. *retlen = msg.actual_length - 8;
  232. status = 0;
  233. } else
  234. dev_dbg(&priv->spi->dev, "read %x..%x --> %d\n",
  235. (unsigned)from, (unsigned)(from + len),
  236. status);
  237. return status;
  238. }
  239. /*
  240. * Write to the DataFlash device.
  241. * to : Start offset in flash device
  242. * len : Amount to write
  243. * retlen : Amount of data actually written
  244. * buf : Buffer containing the data
  245. */
  246. static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
  247. size_t * retlen, const u_char * buf)
  248. {
  249. struct dataflash *priv = mtd->priv;
  250. struct spi_device *spi = priv->spi;
  251. struct spi_transfer x[2] = { };
  252. struct spi_message msg;
  253. unsigned int pageaddr, addr, offset, writelen;
  254. size_t remaining = len;
  255. u_char *writebuf = (u_char *) buf;
  256. int status = -EINVAL;
  257. u8 *command;
  258. dev_dbg(&spi->dev, "write 0x%x..0x%x\n",
  259. (unsigned int)to, (unsigned int)(to + len));
  260. spi_message_init(&msg);
  261. x[0].tx_buf = command = priv->command;
  262. x[0].len = 4;
  263. spi_message_add_tail(&x[0], &msg);
  264. pageaddr = ((unsigned)to / priv->page_size);
  265. offset = ((unsigned)to % priv->page_size);
  266. if (offset + len > priv->page_size)
  267. writelen = priv->page_size - offset;
  268. else
  269. writelen = len;
  270. mutex_lock(&priv->lock);
  271. while (remaining > 0) {
  272. dev_dbg(&spi->dev, "write @ %i:%i len=%i\n",
  273. pageaddr, offset, writelen);
  274. /* REVISIT:
  275. * (a) each page in a sector must be rewritten at least
  276. * once every 10K sibling erase/program operations.
  277. * (b) for pages that are already erased, we could
  278. * use WRITE+MWRITE not PROGRAM for ~30% speedup.
  279. * (c) WRITE to buffer could be done while waiting for
  280. * a previous MWRITE/MWERASE to complete ...
  281. * (d) error handling here seems to be mostly missing.
  282. *
  283. * Two persistent bits per page, plus a per-sector counter,
  284. * could support (a) and (b) ... we might consider using
  285. * the second half of sector zero, which is just one block,
  286. * to track that state. (On AT91, that sector should also
  287. * support boot-from-DataFlash.)
  288. */
  289. addr = pageaddr << priv->page_offset;
  290. /* (1) Maybe transfer partial page to Buffer1 */
  291. if (writelen != priv->page_size) {
  292. command[0] = OP_TRANSFER_BUF1;
  293. command[1] = (addr & 0x00FF0000) >> 16;
  294. command[2] = (addr & 0x0000FF00) >> 8;
  295. command[3] = 0;
  296. dev_dbg(&spi->dev, "TRANSFER: (%x) %x %x %x\n",
  297. command[0], command[1], command[2], command[3]);
  298. status = spi_sync(spi, &msg);
  299. if (status < 0)
  300. dev_dbg(&spi->dev, "xfer %u -> %d\n",
  301. addr, status);
  302. (void) dataflash_waitready(priv->spi);
  303. }
  304. /* (2) Program full page via Buffer1 */
  305. addr += offset;
  306. command[0] = OP_PROGRAM_VIA_BUF1;
  307. command[1] = (addr & 0x00FF0000) >> 16;
  308. command[2] = (addr & 0x0000FF00) >> 8;
  309. command[3] = (addr & 0x000000FF);
  310. dev_dbg(&spi->dev, "PROGRAM: (%x) %x %x %x\n",
  311. command[0], command[1], command[2], command[3]);
  312. x[1].tx_buf = writebuf;
  313. x[1].len = writelen;
  314. spi_message_add_tail(x + 1, &msg);
  315. status = spi_sync(spi, &msg);
  316. spi_transfer_del(x + 1);
  317. if (status < 0)
  318. dev_dbg(&spi->dev, "pgm %u/%u -> %d\n",
  319. addr, writelen, status);
  320. (void) dataflash_waitready(priv->spi);
  321. #ifdef CONFIG_MTD_DATAFLASH_WRITE_VERIFY
  322. /* (3) Compare to Buffer1 */
  323. addr = pageaddr << priv->page_offset;
  324. command[0] = OP_COMPARE_BUF1;
  325. command[1] = (addr & 0x00FF0000) >> 16;
  326. command[2] = (addr & 0x0000FF00) >> 8;
  327. command[3] = 0;
  328. dev_dbg(&spi->dev, "COMPARE: (%x) %x %x %x\n",
  329. command[0], command[1], command[2], command[3]);
  330. status = spi_sync(spi, &msg);
  331. if (status < 0)
  332. dev_dbg(&spi->dev, "compare %u -> %d\n",
  333. addr, status);
  334. status = dataflash_waitready(priv->spi);
  335. /* Check result of the compare operation */
  336. if (status & (1 << 6)) {
  337. dev_err(&spi->dev, "compare page %u, err %d\n",
  338. pageaddr, status);
  339. remaining = 0;
  340. status = -EIO;
  341. break;
  342. } else
  343. status = 0;
  344. #endif /* CONFIG_MTD_DATAFLASH_WRITE_VERIFY */
  345. remaining = remaining - writelen;
  346. pageaddr++;
  347. offset = 0;
  348. writebuf += writelen;
  349. *retlen += writelen;
  350. if (remaining > priv->page_size)
  351. writelen = priv->page_size;
  352. else
  353. writelen = remaining;
  354. }
  355. mutex_unlock(&priv->lock);
  356. return status;
  357. }
  358. /* ......................................................................... */
  359. #ifdef CONFIG_MTD_DATAFLASH_OTP
  360. static int dataflash_get_otp_info(struct mtd_info *mtd, size_t len,
  361. size_t *retlen, struct otp_info *info)
  362. {
  363. /* Report both blocks as identical: bytes 0..64, locked.
  364. * Unless the user block changed from all-ones, we can't
  365. * tell whether it's still writable; so we assume it isn't.
  366. */
  367. info->start = 0;
  368. info->length = 64;
  369. info->locked = 1;
  370. *retlen = sizeof(*info);
  371. return 0;
  372. }
  373. static ssize_t otp_read(struct spi_device *spi, unsigned base,
  374. u8 *buf, loff_t off, size_t len)
  375. {
  376. struct spi_message m;
  377. size_t l;
  378. u8 *scratch;
  379. struct spi_transfer t;
  380. int status;
  381. if (off > 64)
  382. return -EINVAL;
  383. if ((off + len) > 64)
  384. len = 64 - off;
  385. spi_message_init(&m);
  386. l = 4 + base + off + len;
  387. scratch = kzalloc(l, GFP_KERNEL);
  388. if (!scratch)
  389. return -ENOMEM;
  390. /* OUT: OP_READ_SECURITY, 3 don't-care bytes, zeroes
  391. * IN: ignore 4 bytes, data bytes 0..N (max 127)
  392. */
  393. scratch[0] = OP_READ_SECURITY;
  394. memset(&t, 0, sizeof t);
  395. t.tx_buf = scratch;
  396. t.rx_buf = scratch;
  397. t.len = l;
  398. spi_message_add_tail(&t, &m);
  399. dataflash_waitready(spi);
  400. status = spi_sync(spi, &m);
  401. if (status >= 0) {
  402. memcpy(buf, scratch + 4 + base + off, len);
  403. status = len;
  404. }
  405. kfree(scratch);
  406. return status;
  407. }
  408. static int dataflash_read_fact_otp(struct mtd_info *mtd,
  409. loff_t from, size_t len, size_t *retlen, u_char *buf)
  410. {
  411. struct dataflash *priv = mtd->priv;
  412. int status;
  413. /* 64 bytes, from 0..63 ... start at 64 on-chip */
  414. mutex_lock(&priv->lock);
  415. status = otp_read(priv->spi, 64, buf, from, len);
  416. mutex_unlock(&priv->lock);
  417. if (status < 0)
  418. return status;
  419. *retlen = status;
  420. return 0;
  421. }
  422. static int dataflash_read_user_otp(struct mtd_info *mtd,
  423. loff_t from, size_t len, size_t *retlen, u_char *buf)
  424. {
  425. struct dataflash *priv = mtd->priv;
  426. int status;
  427. /* 64 bytes, from 0..63 ... start at 0 on-chip */
  428. mutex_lock(&priv->lock);
  429. status = otp_read(priv->spi, 0, buf, from, len);
  430. mutex_unlock(&priv->lock);
  431. if (status < 0)
  432. return status;
  433. *retlen = status;
  434. return 0;
  435. }
  436. static int dataflash_write_user_otp(struct mtd_info *mtd,
  437. loff_t from, size_t len, size_t *retlen, const u_char *buf)
  438. {
  439. struct spi_message m;
  440. const size_t l = 4 + 64;
  441. u8 *scratch;
  442. struct spi_transfer t;
  443. struct dataflash *priv = mtd->priv;
  444. int status;
  445. if (from >= 64) {
  446. /*
  447. * Attempting to write beyond the end of OTP memory,
  448. * no data can be written.
  449. */
  450. *retlen = 0;
  451. return 0;
  452. }
  453. /* Truncate the write to fit into OTP memory. */
  454. if ((from + len) > 64)
  455. len = 64 - from;
  456. /* OUT: OP_WRITE_SECURITY, 3 zeroes, 64 data-or-zero bytes
  457. * IN: ignore all
  458. */
  459. scratch = kzalloc(l, GFP_KERNEL);
  460. if (!scratch)
  461. return -ENOMEM;
  462. scratch[0] = OP_WRITE_SECURITY;
  463. memcpy(scratch + 4 + from, buf, len);
  464. spi_message_init(&m);
  465. memset(&t, 0, sizeof t);
  466. t.tx_buf = scratch;
  467. t.len = l;
  468. spi_message_add_tail(&t, &m);
  469. /* Write the OTP bits, if they've not yet been written.
  470. * This modifies SRAM buffer1.
  471. */
  472. mutex_lock(&priv->lock);
  473. dataflash_waitready(priv->spi);
  474. status = spi_sync(priv->spi, &m);
  475. mutex_unlock(&priv->lock);
  476. kfree(scratch);
  477. if (status >= 0) {
  478. status = 0;
  479. *retlen = len;
  480. }
  481. return status;
  482. }
  483. static char *otp_setup(struct mtd_info *device, char revision)
  484. {
  485. device->_get_fact_prot_info = dataflash_get_otp_info;
  486. device->_read_fact_prot_reg = dataflash_read_fact_otp;
  487. device->_get_user_prot_info = dataflash_get_otp_info;
  488. device->_read_user_prot_reg = dataflash_read_user_otp;
  489. /* rev c parts (at45db321c and at45db1281 only!) use a
  490. * different write procedure; not (yet?) implemented.
  491. */
  492. if (revision > 'c')
  493. device->_write_user_prot_reg = dataflash_write_user_otp;
  494. return ", OTP";
  495. }
  496. #else
  497. static char *otp_setup(struct mtd_info *device, char revision)
  498. {
  499. return " (OTP)";
  500. }
  501. #endif
  502. /* ......................................................................... */
  503. /*
  504. * Register DataFlash device with MTD subsystem.
  505. */
  506. static int add_dataflash_otp(struct spi_device *spi, char *name, int nr_pages,
  507. int pagesize, int pageoffset, char revision)
  508. {
  509. struct dataflash *priv;
  510. struct mtd_info *device;
  511. struct flash_platform_data *pdata = dev_get_platdata(&spi->dev);
  512. char *otp_tag = "";
  513. int err = 0;
  514. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  515. if (!priv)
  516. return -ENOMEM;
  517. mutex_init(&priv->lock);
  518. priv->spi = spi;
  519. priv->page_size = pagesize;
  520. priv->page_offset = pageoffset;
  521. /* name must be usable with cmdlinepart */
  522. sprintf(priv->name, "spi%d.%d-%s",
  523. spi->controller->bus_num, spi_get_chipselect(spi, 0),
  524. name);
  525. device = &priv->mtd;
  526. device->name = (pdata && pdata->name) ? pdata->name : priv->name;
  527. device->size = nr_pages * pagesize;
  528. device->erasesize = pagesize;
  529. device->writesize = pagesize;
  530. device->type = MTD_DATAFLASH;
  531. device->flags = MTD_WRITEABLE;
  532. device->_erase = dataflash_erase;
  533. device->_read = dataflash_read;
  534. device->_write = dataflash_write;
  535. device->priv = priv;
  536. device->dev.parent = &spi->dev;
  537. mtd_set_of_node(device, spi->dev.of_node);
  538. if (revision >= 'c')
  539. otp_tag = otp_setup(device, revision);
  540. dev_info(&spi->dev, "%s (%lld KBytes) pagesize %d bytes%s\n",
  541. name, (long long)((device->size + 1023) >> 10),
  542. pagesize, otp_tag);
  543. spi_set_drvdata(spi, priv);
  544. err = mtd_device_register(device,
  545. pdata ? pdata->parts : NULL,
  546. pdata ? pdata->nr_parts : 0);
  547. if (!err)
  548. return 0;
  549. kfree(priv);
  550. return err;
  551. }
  552. static inline int add_dataflash(struct spi_device *spi, char *name,
  553. int nr_pages, int pagesize, int pageoffset)
  554. {
  555. return add_dataflash_otp(spi, name, nr_pages, pagesize,
  556. pageoffset, 0);
  557. }
  558. struct flash_info {
  559. char *name;
  560. /* JEDEC id has a high byte of zero plus three data bytes:
  561. * the manufacturer id, then a two byte device id.
  562. */
  563. u64 jedec_id;
  564. /* The size listed here is what works with OP_ERASE_PAGE. */
  565. unsigned nr_pages;
  566. u16 pagesize;
  567. u16 pageoffset;
  568. u16 flags;
  569. #define SUP_EXTID 0x0004 /* supports extended ID data */
  570. #define SUP_POW2PS 0x0002 /* supports 2^N byte pages */
  571. #define IS_POW2PS 0x0001 /* uses 2^N byte pages */
  572. };
  573. static struct flash_info dataflash_data[] = {
  574. /*
  575. * NOTE: chips with SUP_POW2PS (rev D and up) need two entries,
  576. * one with IS_POW2PS and the other without. The entry with the
  577. * non-2^N byte page size can't name exact chip revisions without
  578. * losing backwards compatibility for cmdlinepart.
  579. *
  580. * These newer chips also support 128-byte security registers (with
  581. * 64 bytes one-time-programmable) and software write-protection.
  582. */
  583. { "AT45DB011B", 0x1f2200, 512, 264, 9, SUP_POW2PS},
  584. { "at45db011d", 0x1f2200, 512, 256, 8, SUP_POW2PS | IS_POW2PS},
  585. { "AT45DB021B", 0x1f2300, 1024, 264, 9, SUP_POW2PS},
  586. { "at45db021d", 0x1f2300, 1024, 256, 8, SUP_POW2PS | IS_POW2PS},
  587. { "AT45DB041x", 0x1f2400, 2048, 264, 9, SUP_POW2PS},
  588. { "at45db041d", 0x1f2400, 2048, 256, 8, SUP_POW2PS | IS_POW2PS},
  589. { "AT45DB081B", 0x1f2500, 4096, 264, 9, SUP_POW2PS},
  590. { "at45db081d", 0x1f2500, 4096, 256, 8, SUP_POW2PS | IS_POW2PS},
  591. { "AT45DB161x", 0x1f2600, 4096, 528, 10, SUP_POW2PS},
  592. { "at45db161d", 0x1f2600, 4096, 512, 9, SUP_POW2PS | IS_POW2PS},
  593. { "AT45DB321x", 0x1f2700, 8192, 528, 10, 0}, /* rev C */
  594. { "AT45DB321x", 0x1f2701, 8192, 528, 10, SUP_POW2PS},
  595. { "at45db321d", 0x1f2701, 8192, 512, 9, SUP_POW2PS | IS_POW2PS},
  596. { "AT45DB642x", 0x1f2800, 8192, 1056, 11, SUP_POW2PS},
  597. { "at45db642d", 0x1f2800, 8192, 1024, 10, SUP_POW2PS | IS_POW2PS},
  598. { "AT45DB641E", 0x1f28000100ULL, 32768, 264, 9, SUP_EXTID | SUP_POW2PS},
  599. { "at45db641e", 0x1f28000100ULL, 32768, 256, 8, SUP_EXTID | SUP_POW2PS | IS_POW2PS},
  600. };
  601. static struct flash_info *jedec_lookup(struct spi_device *spi,
  602. u64 jedec, bool use_extid)
  603. {
  604. struct flash_info *info;
  605. int status;
  606. for (info = dataflash_data;
  607. info < dataflash_data + ARRAY_SIZE(dataflash_data);
  608. info++) {
  609. if (use_extid && !(info->flags & SUP_EXTID))
  610. continue;
  611. if (info->jedec_id == jedec) {
  612. dev_dbg(&spi->dev, "OTP, sector protect%s\n",
  613. (info->flags & SUP_POW2PS) ?
  614. ", binary pagesize" : "");
  615. if (info->flags & SUP_POW2PS) {
  616. status = dataflash_status(spi);
  617. if (status < 0) {
  618. dev_dbg(&spi->dev, "status error %d\n",
  619. status);
  620. return ERR_PTR(status);
  621. }
  622. if (status & 0x1) {
  623. if (info->flags & IS_POW2PS)
  624. return info;
  625. } else {
  626. if (!(info->flags & IS_POW2PS))
  627. return info;
  628. }
  629. } else
  630. return info;
  631. }
  632. }
  633. return ERR_PTR(-ENODEV);
  634. }
  635. static struct flash_info *jedec_probe(struct spi_device *spi)
  636. {
  637. int ret;
  638. u8 code = OP_READ_ID;
  639. u64 jedec;
  640. u8 id[sizeof(jedec)] = {0};
  641. const unsigned int id_size = 5;
  642. struct flash_info *info;
  643. /*
  644. * JEDEC also defines an optional "extended device information"
  645. * string for after vendor-specific data, after the three bytes
  646. * we use here. Supporting some chips might require using it.
  647. *
  648. * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
  649. * That's not an error; only rev C and newer chips handle it, and
  650. * only Atmel sells these chips.
  651. */
  652. ret = spi_write_then_read(spi, &code, 1, id, id_size);
  653. if (ret < 0) {
  654. dev_dbg(&spi->dev, "error %d reading JEDEC ID\n", ret);
  655. return ERR_PTR(ret);
  656. }
  657. if (id[0] != CFI_MFR_ATMEL)
  658. return NULL;
  659. jedec = be64_to_cpup((__be64 *)id);
  660. /*
  661. * First, try to match device using extended device
  662. * information
  663. */
  664. info = jedec_lookup(spi, jedec >> DATAFLASH_SHIFT_EXTID, true);
  665. if (!IS_ERR(info))
  666. return info;
  667. /*
  668. * If that fails, make another pass using regular ID
  669. * information
  670. */
  671. info = jedec_lookup(spi, jedec >> DATAFLASH_SHIFT_ID, false);
  672. if (!IS_ERR(info))
  673. return info;
  674. /*
  675. * Treat other chips as errors ... we won't know the right page
  676. * size (it might be binary) even when we can tell which density
  677. * class is involved (legacy chip id scheme).
  678. */
  679. dev_warn(&spi->dev, "JEDEC id %016llx not handled\n", jedec);
  680. return ERR_PTR(-ENODEV);
  681. }
  682. /*
  683. * Detect and initialize DataFlash device, using JEDEC IDs on newer chips
  684. * or else the ID code embedded in the status bits:
  685. *
  686. * Device Density ID code #Pages PageSize Offset
  687. * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
  688. * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1024 264 9
  689. * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
  690. * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
  691. * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
  692. * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
  693. * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
  694. * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
  695. */
  696. static int dataflash_probe(struct spi_device *spi)
  697. {
  698. int status;
  699. struct flash_info *info;
  700. /*
  701. * Try to detect dataflash by JEDEC ID.
  702. * If it succeeds we know we have either a C or D part.
  703. * D will support power of 2 pagesize option.
  704. * Both support the security register, though with different
  705. * write procedures.
  706. */
  707. info = jedec_probe(spi);
  708. if (IS_ERR(info))
  709. return PTR_ERR(info);
  710. if (info != NULL)
  711. return add_dataflash_otp(spi, info->name, info->nr_pages,
  712. info->pagesize, info->pageoffset,
  713. (info->flags & SUP_POW2PS) ? 'd' : 'c');
  714. /*
  715. * Older chips support only legacy commands, identifing
  716. * capacity using bits in the status byte.
  717. */
  718. status = dataflash_status(spi);
  719. if (status <= 0 || status == 0xff) {
  720. dev_dbg(&spi->dev, "status error %d\n", status);
  721. if (status == 0 || status == 0xff)
  722. status = -ENODEV;
  723. return status;
  724. }
  725. /* if there's a device there, assume it's dataflash.
  726. * board setup should have set spi->max_speed_max to
  727. * match f(car) for continuous reads, mode 0 or 3.
  728. */
  729. switch (status & 0x3c) {
  730. case 0x0c: /* 0 0 1 1 x x */
  731. status = add_dataflash(spi, "AT45DB011B", 512, 264, 9);
  732. break;
  733. case 0x14: /* 0 1 0 1 x x */
  734. status = add_dataflash(spi, "AT45DB021B", 1024, 264, 9);
  735. break;
  736. case 0x1c: /* 0 1 1 1 x x */
  737. status = add_dataflash(spi, "AT45DB041x", 2048, 264, 9);
  738. break;
  739. case 0x24: /* 1 0 0 1 x x */
  740. status = add_dataflash(spi, "AT45DB081B", 4096, 264, 9);
  741. break;
  742. case 0x2c: /* 1 0 1 1 x x */
  743. status = add_dataflash(spi, "AT45DB161x", 4096, 528, 10);
  744. break;
  745. case 0x34: /* 1 1 0 1 x x */
  746. status = add_dataflash(spi, "AT45DB321x", 8192, 528, 10);
  747. break;
  748. case 0x38: /* 1 1 1 x x x */
  749. case 0x3c:
  750. status = add_dataflash(spi, "AT45DB642x", 8192, 1056, 11);
  751. break;
  752. /* obsolete AT45DB1282 not (yet?) supported */
  753. default:
  754. dev_info(&spi->dev, "unsupported device (%x)\n",
  755. status & 0x3c);
  756. status = -ENODEV;
  757. }
  758. if (status < 0)
  759. dev_dbg(&spi->dev, "add_dataflash --> %d\n", status);
  760. return status;
  761. }
  762. static void dataflash_remove(struct spi_device *spi)
  763. {
  764. struct dataflash *flash = spi_get_drvdata(spi);
  765. dev_dbg(&spi->dev, "remove\n");
  766. WARN_ON(mtd_device_unregister(&flash->mtd));
  767. kfree(flash);
  768. }
  769. static struct spi_driver dataflash_driver = {
  770. .driver = {
  771. .name = "mtd_dataflash",
  772. .of_match_table = of_match_ptr(dataflash_dt_ids),
  773. },
  774. .probe = dataflash_probe,
  775. .remove = dataflash_remove,
  776. .id_table = dataflash_spi_ids,
  777. /* FIXME: investigate suspend and resume... */
  778. };
  779. module_spi_driver(dataflash_driver);
  780. MODULE_LICENSE("GPL");
  781. MODULE_AUTHOR("Andrew Victor, David Brownell");
  782. MODULE_DESCRIPTION("MTD DataFlash driver");
  783. MODULE_ALIAS("spi:mtd_dataflash");