mtd_dataflash.c 24 KB

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