mtd_dataflash.c 24 KB

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