mtd_dataflash.c 25 KB

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