mtd_dataflash.c 25 KB

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