mtd_dataflash.c 25 KB

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