mtd_dataflash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 use SPI (yet), or distinguish between MMC and DataFlash
  30. * protocols during enumeration.
  31. */
  32. #define CONFIG_DATAFLASH_WRITE_VERIFY
  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 0x9A /* OTP bits */
  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_DATAFLASH_WRITE_VERIFY
  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_DATAFLASH_WRITE_VERIFY */
  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. /*
  368. * Register DataFlash device with MTD subsystem.
  369. */
  370. static int __devinit
  371. add_dataflash(struct spi_device *spi, char *name,
  372. int nr_pages, int pagesize, int pageoffset)
  373. {
  374. struct dataflash *priv;
  375. struct mtd_info *device;
  376. struct flash_platform_data *pdata = spi->dev.platform_data;
  377. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  378. if (!priv)
  379. return -ENOMEM;
  380. mutex_init(&priv->lock);
  381. priv->spi = spi;
  382. priv->page_size = pagesize;
  383. priv->page_offset = pageoffset;
  384. /* name must be usable with cmdlinepart */
  385. sprintf(priv->name, "spi%d.%d-%s",
  386. spi->master->bus_num, spi->chip_select,
  387. name);
  388. device = &priv->mtd;
  389. device->name = (pdata && pdata->name) ? pdata->name : priv->name;
  390. device->size = nr_pages * pagesize;
  391. device->erasesize = pagesize;
  392. device->writesize = pagesize;
  393. device->owner = THIS_MODULE;
  394. device->type = MTD_DATAFLASH;
  395. device->flags = MTD_WRITEABLE;
  396. device->erase = dataflash_erase;
  397. device->read = dataflash_read;
  398. device->write = dataflash_write;
  399. device->priv = priv;
  400. dev_info(&spi->dev, "%s (%d KBytes) pagesize %d bytes\n",
  401. name, DIV_ROUND_UP(device->size, 1024), pagesize);
  402. dev_set_drvdata(&spi->dev, priv);
  403. if (mtd_has_partitions()) {
  404. struct mtd_partition *parts;
  405. int nr_parts = 0;
  406. #ifdef CONFIG_MTD_CMDLINE_PARTS
  407. static const char *part_probes[] = { "cmdlinepart", NULL, };
  408. nr_parts = parse_mtd_partitions(device, part_probes, &parts, 0);
  409. #endif
  410. if (nr_parts <= 0 && pdata && pdata->parts) {
  411. parts = pdata->parts;
  412. nr_parts = pdata->nr_parts;
  413. }
  414. if (nr_parts > 0) {
  415. priv->partitioned = 1;
  416. return add_mtd_partitions(device, parts, nr_parts);
  417. }
  418. } else if (pdata && pdata->nr_parts)
  419. dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
  420. pdata->nr_parts, device->name);
  421. return add_mtd_device(device) == 1 ? -ENODEV : 0;
  422. }
  423. struct flash_info {
  424. char *name;
  425. /* JEDEC id has a high byte of zero plus three data bytes:
  426. * the manufacturer id, then a two byte device id.
  427. */
  428. uint32_t jedec_id;
  429. /* The size listed here is what works with OP_ERASE_PAGE. */
  430. unsigned nr_pages;
  431. uint16_t pagesize;
  432. uint16_t pageoffset;
  433. uint16_t flags;
  434. #define SUP_POW2PS 0x0002 /* supports 2^N byte pages */
  435. #define IS_POW2PS 0x0001 /* uses 2^N byte pages */
  436. };
  437. static struct flash_info __devinitdata dataflash_data [] = {
  438. /*
  439. * NOTE: chips with SUP_POW2PS (rev D and up) need two entries,
  440. * one with IS_POW2PS and the other without. The entry with the
  441. * non-2^N byte page size can't name exact chip revisions without
  442. * losing backwards compatibility for cmdlinepart.
  443. *
  444. * These newer chips also support 128-byte security registers (with
  445. * 64 bytes one-time-programmable) and software write-protection.
  446. */
  447. { "AT45DB011B", 0x1f2200, 512, 264, 9, SUP_POW2PS},
  448. { "at45db011d", 0x1f2200, 512, 256, 8, SUP_POW2PS | IS_POW2PS},
  449. { "AT45DB021B", 0x1f2300, 1024, 264, 9, SUP_POW2PS},
  450. { "at45db021d", 0x1f2300, 1024, 256, 8, SUP_POW2PS | IS_POW2PS},
  451. { "AT45DB041x", 0x1f2400, 2048, 264, 9, SUP_POW2PS},
  452. { "at45db041d", 0x1f2400, 2048, 256, 8, SUP_POW2PS | IS_POW2PS},
  453. { "AT45DB081B", 0x1f2500, 4096, 264, 9, SUP_POW2PS},
  454. { "at45db081d", 0x1f2500, 4096, 256, 8, SUP_POW2PS | IS_POW2PS},
  455. { "AT45DB161x", 0x1f2600, 4096, 528, 10, SUP_POW2PS},
  456. { "at45db161d", 0x1f2600, 4096, 512, 9, SUP_POW2PS | IS_POW2PS},
  457. { "AT45DB321x", 0x1f2700, 8192, 528, 10, 0}, /* rev C */
  458. { "AT45DB321x", 0x1f2701, 8192, 528, 10, SUP_POW2PS},
  459. { "at45db321d", 0x1f2701, 8192, 512, 9, SUP_POW2PS | IS_POW2PS},
  460. { "AT45DB642x", 0x1f2800, 8192, 1056, 11, SUP_POW2PS},
  461. { "at45db642d", 0x1f2800, 8192, 1024, 10, SUP_POW2PS | IS_POW2PS},
  462. };
  463. static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
  464. {
  465. int tmp;
  466. uint8_t code = OP_READ_ID;
  467. uint8_t id[3];
  468. uint32_t jedec;
  469. struct flash_info *info;
  470. int status;
  471. /* JEDEC also defines an optional "extended device information"
  472. * string for after vendor-specific data, after the three bytes
  473. * we use here. Supporting some chips might require using it.
  474. *
  475. * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
  476. * That's not an error; only rev C and newer chips handle it, and
  477. * only Atmel sells these chips.
  478. */
  479. tmp = spi_write_then_read(spi, &code, 1, id, 3);
  480. if (tmp < 0) {
  481. DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
  482. spi->dev.bus_id, tmp);
  483. return ERR_PTR(tmp);
  484. }
  485. if (id[0] != 0x1f)
  486. return NULL;
  487. jedec = id[0];
  488. jedec = jedec << 8;
  489. jedec |= id[1];
  490. jedec = jedec << 8;
  491. jedec |= id[2];
  492. for (tmp = 0, info = dataflash_data;
  493. tmp < ARRAY_SIZE(dataflash_data);
  494. tmp++, info++) {
  495. if (info->jedec_id == jedec) {
  496. DEBUG(MTD_DEBUG_LEVEL1, "%s: OTP, sector protect%s\n",
  497. dev_name(&spi->dev),
  498. (info->flags & SUP_POW2PS)
  499. ? ", binary pagesize" : ""
  500. );
  501. if (info->flags & SUP_POW2PS) {
  502. status = dataflash_status(spi);
  503. if (status < 0) {
  504. DEBUG(MTD_DEBUG_LEVEL1,
  505. "%s: status error %d\n",
  506. dev_name(&spi->dev), status);
  507. return ERR_PTR(status);
  508. }
  509. if (status & 0x1) {
  510. if (info->flags & IS_POW2PS)
  511. return info;
  512. } else {
  513. if (!(info->flags & IS_POW2PS))
  514. return info;
  515. }
  516. }
  517. }
  518. }
  519. /*
  520. * Treat other chips as errors ... we won't know the right page
  521. * size (it might be binary) even when we can tell which density
  522. * class is involved (legacy chip id scheme).
  523. */
  524. dev_warn(&spi->dev, "JEDEC id %06x not handled\n", jedec);
  525. return ERR_PTR(-ENODEV);
  526. }
  527. /*
  528. * Detect and initialize DataFlash device, using JEDEC IDs on newer chips
  529. * or else the ID code embedded in the status bits:
  530. *
  531. * Device Density ID code #Pages PageSize Offset
  532. * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
  533. * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1024 264 9
  534. * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
  535. * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
  536. * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
  537. * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
  538. * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
  539. * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
  540. */
  541. static int __devinit dataflash_probe(struct spi_device *spi)
  542. {
  543. int status;
  544. struct flash_info *info;
  545. /*
  546. * Try to detect dataflash by JEDEC ID.
  547. * If it succeeds we know we have either a C or D part.
  548. * D will support power of 2 pagesize option.
  549. */
  550. info = jedec_probe(spi);
  551. if (IS_ERR(info))
  552. return PTR_ERR(info);
  553. if (info != NULL)
  554. return add_dataflash(spi, info->name, info->nr_pages,
  555. info->pagesize, info->pageoffset);
  556. /*
  557. * Older chips support only legacy commands, identifing
  558. * capacity using bits in the status byte.
  559. */
  560. status = dataflash_status(spi);
  561. if (status <= 0 || status == 0xff) {
  562. DEBUG(MTD_DEBUG_LEVEL1, "%s: status error %d\n",
  563. spi->dev.bus_id, status);
  564. if (status == 0 || status == 0xff)
  565. status = -ENODEV;
  566. return status;
  567. }
  568. /* if there's a device there, assume it's dataflash.
  569. * board setup should have set spi->max_speed_max to
  570. * match f(car) for continuous reads, mode 0 or 3.
  571. */
  572. switch (status & 0x3c) {
  573. case 0x0c: /* 0 0 1 1 x x */
  574. status = add_dataflash(spi, "AT45DB011B", 512, 264, 9);
  575. break;
  576. case 0x14: /* 0 1 0 1 x x */
  577. status = add_dataflash(spi, "AT45DB021B", 1024, 264, 9);
  578. break;
  579. case 0x1c: /* 0 1 1 1 x x */
  580. status = add_dataflash(spi, "AT45DB041x", 2048, 264, 9);
  581. break;
  582. case 0x24: /* 1 0 0 1 x x */
  583. status = add_dataflash(spi, "AT45DB081B", 4096, 264, 9);
  584. break;
  585. case 0x2c: /* 1 0 1 1 x x */
  586. status = add_dataflash(spi, "AT45DB161x", 4096, 528, 10);
  587. break;
  588. case 0x34: /* 1 1 0 1 x x */
  589. status = add_dataflash(spi, "AT45DB321x", 8192, 528, 10);
  590. break;
  591. case 0x38: /* 1 1 1 x x x */
  592. case 0x3c:
  593. status = add_dataflash(spi, "AT45DB642x", 8192, 1056, 11);
  594. break;
  595. /* obsolete AT45DB1282 not (yet?) supported */
  596. default:
  597. DEBUG(MTD_DEBUG_LEVEL1, "%s: unsupported device (%x)\n",
  598. spi->dev.bus_id, status & 0x3c);
  599. status = -ENODEV;
  600. }
  601. if (status < 0)
  602. DEBUG(MTD_DEBUG_LEVEL1, "%s: add_dataflash --> %d\n",
  603. spi->dev.bus_id, status);
  604. return status;
  605. }
  606. static int __devexit dataflash_remove(struct spi_device *spi)
  607. {
  608. struct dataflash *flash = dev_get_drvdata(&spi->dev);
  609. int status;
  610. DEBUG(MTD_DEBUG_LEVEL1, "%s: remove\n", spi->dev.bus_id);
  611. if (mtd_has_partitions() && flash->partitioned)
  612. status = del_mtd_partitions(&flash->mtd);
  613. else
  614. status = del_mtd_device(&flash->mtd);
  615. if (status == 0)
  616. kfree(flash);
  617. return status;
  618. }
  619. static struct spi_driver dataflash_driver = {
  620. .driver = {
  621. .name = "mtd_dataflash",
  622. .bus = &spi_bus_type,
  623. .owner = THIS_MODULE,
  624. },
  625. .probe = dataflash_probe,
  626. .remove = __devexit_p(dataflash_remove),
  627. /* FIXME: investigate suspend and resume... */
  628. };
  629. static int __init dataflash_init(void)
  630. {
  631. return spi_register_driver(&dataflash_driver);
  632. }
  633. module_init(dataflash_init);
  634. static void __exit dataflash_exit(void)
  635. {
  636. spi_unregister_driver(&dataflash_driver);
  637. }
  638. module_exit(dataflash_exit);
  639. MODULE_LICENSE("GPL");
  640. MODULE_AUTHOR("Andrew Victor, David Brownell");
  641. MODULE_DESCRIPTION("MTD DataFlash driver");