mtd_dataflash.c 25 KB

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