m25p80.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
  3. *
  4. * Author: Mike Lavender, mike@steroidmicros.com
  5. *
  6. * Copyright (c) 2005, Intec Automation Inc.
  7. *
  8. * Some parts are based on lart.c by Abraham Van Der Merwe
  9. *
  10. * Cleaned up and generalized based on mtd_dataflash.c
  11. *
  12. * This code is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mutex.h>
  22. #include <linux/math64.h>
  23. #include <linux/sched.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/flash.h>
  28. #define FLASH_PAGESIZE 256
  29. /* Flash opcodes. */
  30. #define OPCODE_WREN 0x06 /* Write enable */
  31. #define OPCODE_RDSR 0x05 /* Read status register */
  32. #define OPCODE_WRSR 0x01 /* Write status register 1 byte */
  33. #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
  34. #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
  35. #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
  36. #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
  37. #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
  38. #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
  39. #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
  40. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  41. /* Used for SST flashes only. */
  42. #define OPCODE_BP 0x02 /* Byte program */
  43. #define OPCODE_WRDI 0x04 /* Write disable */
  44. #define OPCODE_AAI_WP 0xad /* Auto address increment word program */
  45. /* Status Register bits. */
  46. #define SR_WIP 1 /* Write in progress */
  47. #define SR_WEL 2 /* Write enable latch */
  48. /* meaning of other SR_* bits may differ between vendors */
  49. #define SR_BP0 4 /* Block protect 0 */
  50. #define SR_BP1 8 /* Block protect 1 */
  51. #define SR_BP2 0x10 /* Block protect 2 */
  52. #define SR_SRWD 0x80 /* SR write protect */
  53. /* Define max times to check status register before we give up. */
  54. #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  55. #define CMD_SIZE 4
  56. #ifdef CONFIG_M25PXX_USE_FAST_READ
  57. #define OPCODE_READ OPCODE_FAST_READ
  58. #define FAST_READ_DUMMY_BYTE 1
  59. #else
  60. #define OPCODE_READ OPCODE_NORM_READ
  61. #define FAST_READ_DUMMY_BYTE 0
  62. #endif
  63. /****************************************************************************/
  64. struct m25p {
  65. struct spi_device *spi;
  66. struct mutex lock;
  67. struct mtd_info mtd;
  68. unsigned partitioned:1;
  69. u8 erase_opcode;
  70. u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
  71. };
  72. static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
  73. {
  74. return container_of(mtd, struct m25p, mtd);
  75. }
  76. /****************************************************************************/
  77. /*
  78. * Internal helper functions
  79. */
  80. /*
  81. * Read the status register, returning its value in the location
  82. * Return the status register value.
  83. * Returns negative if error occurred.
  84. */
  85. static int read_sr(struct m25p *flash)
  86. {
  87. ssize_t retval;
  88. u8 code = OPCODE_RDSR;
  89. u8 val;
  90. retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
  91. if (retval < 0) {
  92. dev_err(&flash->spi->dev, "error %d reading SR\n",
  93. (int) retval);
  94. return retval;
  95. }
  96. return val;
  97. }
  98. /*
  99. * Write status register 1 byte
  100. * Returns negative if error occurred.
  101. */
  102. static int write_sr(struct m25p *flash, u8 val)
  103. {
  104. flash->command[0] = OPCODE_WRSR;
  105. flash->command[1] = val;
  106. return spi_write(flash->spi, flash->command, 2);
  107. }
  108. /*
  109. * Set write enable latch with Write Enable command.
  110. * Returns negative if error occurred.
  111. */
  112. static inline int write_enable(struct m25p *flash)
  113. {
  114. u8 code = OPCODE_WREN;
  115. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  116. }
  117. /*
  118. * Send write disble instruction to the chip.
  119. */
  120. static inline int write_disable(struct m25p *flash)
  121. {
  122. u8 code = OPCODE_WRDI;
  123. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  124. }
  125. /*
  126. * Service routine to read status register until ready, or timeout occurs.
  127. * Returns non-zero if error.
  128. */
  129. static int wait_till_ready(struct m25p *flash)
  130. {
  131. unsigned long deadline;
  132. int sr;
  133. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  134. do {
  135. if ((sr = read_sr(flash)) < 0)
  136. break;
  137. else if (!(sr & SR_WIP))
  138. return 0;
  139. cond_resched();
  140. } while (!time_after_eq(jiffies, deadline));
  141. return 1;
  142. }
  143. /*
  144. * Erase the whole flash memory
  145. *
  146. * Returns 0 if successful, non-zero otherwise.
  147. */
  148. static int erase_chip(struct m25p *flash)
  149. {
  150. DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
  151. dev_name(&flash->spi->dev), __func__,
  152. (long long)(flash->mtd.size >> 10));
  153. /* Wait until finished previous write command. */
  154. if (wait_till_ready(flash))
  155. return 1;
  156. /* Send write enable, then erase commands. */
  157. write_enable(flash);
  158. /* Set up command buffer. */
  159. flash->command[0] = OPCODE_CHIP_ERASE;
  160. spi_write(flash->spi, flash->command, 1);
  161. return 0;
  162. }
  163. /*
  164. * Erase one sector of flash memory at offset ``offset'' which is any
  165. * address within the sector which should be erased.
  166. *
  167. * Returns 0 if successful, non-zero otherwise.
  168. */
  169. static int erase_sector(struct m25p *flash, u32 offset)
  170. {
  171. DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
  172. dev_name(&flash->spi->dev), __func__,
  173. flash->mtd.erasesize / 1024, offset);
  174. /* Wait until finished previous write command. */
  175. if (wait_till_ready(flash))
  176. return 1;
  177. /* Send write enable, then erase commands. */
  178. write_enable(flash);
  179. /* Set up command buffer. */
  180. flash->command[0] = flash->erase_opcode;
  181. flash->command[1] = offset >> 16;
  182. flash->command[2] = offset >> 8;
  183. flash->command[3] = offset;
  184. spi_write(flash->spi, flash->command, CMD_SIZE);
  185. return 0;
  186. }
  187. /****************************************************************************/
  188. /*
  189. * MTD implementation
  190. */
  191. /*
  192. * Erase an address range on the flash chip. The address range may extend
  193. * one or more erase sectors. Return an error is there is a problem erasing.
  194. */
  195. static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
  196. {
  197. struct m25p *flash = mtd_to_m25p(mtd);
  198. u32 addr,len;
  199. uint32_t rem;
  200. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
  201. dev_name(&flash->spi->dev), __func__, "at",
  202. (long long)instr->addr, (long long)instr->len);
  203. /* sanity checks */
  204. if (instr->addr + instr->len > flash->mtd.size)
  205. return -EINVAL;
  206. div_u64_rem(instr->len, mtd->erasesize, &rem);
  207. if (rem)
  208. return -EINVAL;
  209. addr = instr->addr;
  210. len = instr->len;
  211. mutex_lock(&flash->lock);
  212. /* whole-chip erase? */
  213. if (len == flash->mtd.size) {
  214. if (erase_chip(flash)) {
  215. instr->state = MTD_ERASE_FAILED;
  216. mutex_unlock(&flash->lock);
  217. return -EIO;
  218. }
  219. /* REVISIT in some cases we could speed up erasing large regions
  220. * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
  221. * to use "small sector erase", but that's not always optimal.
  222. */
  223. /* "sector"-at-a-time erase */
  224. } else {
  225. while (len) {
  226. if (erase_sector(flash, addr)) {
  227. instr->state = MTD_ERASE_FAILED;
  228. mutex_unlock(&flash->lock);
  229. return -EIO;
  230. }
  231. addr += mtd->erasesize;
  232. len -= mtd->erasesize;
  233. }
  234. }
  235. mutex_unlock(&flash->lock);
  236. instr->state = MTD_ERASE_DONE;
  237. mtd_erase_callback(instr);
  238. return 0;
  239. }
  240. /*
  241. * Read an address range from the flash chip. The address range
  242. * may be any size provided it is within the physical boundaries.
  243. */
  244. static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
  245. size_t *retlen, u_char *buf)
  246. {
  247. struct m25p *flash = mtd_to_m25p(mtd);
  248. struct spi_transfer t[2];
  249. struct spi_message m;
  250. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
  251. dev_name(&flash->spi->dev), __func__, "from",
  252. (u32)from, len);
  253. /* sanity checks */
  254. if (!len)
  255. return 0;
  256. if (from + len > flash->mtd.size)
  257. return -EINVAL;
  258. spi_message_init(&m);
  259. memset(t, 0, (sizeof t));
  260. /* NOTE:
  261. * OPCODE_FAST_READ (if available) is faster.
  262. * Should add 1 byte DUMMY_BYTE.
  263. */
  264. t[0].tx_buf = flash->command;
  265. t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
  266. spi_message_add_tail(&t[0], &m);
  267. t[1].rx_buf = buf;
  268. t[1].len = len;
  269. spi_message_add_tail(&t[1], &m);
  270. /* Byte count starts at zero. */
  271. if (retlen)
  272. *retlen = 0;
  273. mutex_lock(&flash->lock);
  274. /* Wait till previous write/erase is done. */
  275. if (wait_till_ready(flash)) {
  276. /* REVISIT status return?? */
  277. mutex_unlock(&flash->lock);
  278. return 1;
  279. }
  280. /* FIXME switch to OPCODE_FAST_READ. It's required for higher
  281. * clocks; and at this writing, every chip this driver handles
  282. * supports that opcode.
  283. */
  284. /* Set up the write data buffer. */
  285. flash->command[0] = OPCODE_READ;
  286. flash->command[1] = from >> 16;
  287. flash->command[2] = from >> 8;
  288. flash->command[3] = from;
  289. spi_sync(flash->spi, &m);
  290. *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
  291. mutex_unlock(&flash->lock);
  292. return 0;
  293. }
  294. /*
  295. * Write an address range to the flash chip. Data must be written in
  296. * FLASH_PAGESIZE chunks. The address range may be any size provided
  297. * it is within the physical boundaries.
  298. */
  299. static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
  300. size_t *retlen, const u_char *buf)
  301. {
  302. struct m25p *flash = mtd_to_m25p(mtd);
  303. u32 page_offset, page_size;
  304. struct spi_transfer t[2];
  305. struct spi_message m;
  306. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
  307. dev_name(&flash->spi->dev), __func__, "to",
  308. (u32)to, len);
  309. if (retlen)
  310. *retlen = 0;
  311. /* sanity checks */
  312. if (!len)
  313. return(0);
  314. if (to + len > flash->mtd.size)
  315. return -EINVAL;
  316. spi_message_init(&m);
  317. memset(t, 0, (sizeof t));
  318. t[0].tx_buf = flash->command;
  319. t[0].len = CMD_SIZE;
  320. spi_message_add_tail(&t[0], &m);
  321. t[1].tx_buf = buf;
  322. spi_message_add_tail(&t[1], &m);
  323. mutex_lock(&flash->lock);
  324. /* Wait until finished previous write command. */
  325. if (wait_till_ready(flash)) {
  326. mutex_unlock(&flash->lock);
  327. return 1;
  328. }
  329. write_enable(flash);
  330. /* Set up the opcode in the write buffer. */
  331. flash->command[0] = OPCODE_PP;
  332. flash->command[1] = to >> 16;
  333. flash->command[2] = to >> 8;
  334. flash->command[3] = to;
  335. /* what page do we start with? */
  336. page_offset = to % FLASH_PAGESIZE;
  337. /* do all the bytes fit onto one page? */
  338. if (page_offset + len <= FLASH_PAGESIZE) {
  339. t[1].len = len;
  340. spi_sync(flash->spi, &m);
  341. *retlen = m.actual_length - CMD_SIZE;
  342. } else {
  343. u32 i;
  344. /* the size of data remaining on the first page */
  345. page_size = FLASH_PAGESIZE - page_offset;
  346. t[1].len = page_size;
  347. spi_sync(flash->spi, &m);
  348. *retlen = m.actual_length - CMD_SIZE;
  349. /* write everything in PAGESIZE chunks */
  350. for (i = page_size; i < len; i += page_size) {
  351. page_size = len - i;
  352. if (page_size > FLASH_PAGESIZE)
  353. page_size = FLASH_PAGESIZE;
  354. /* write the next page to flash */
  355. flash->command[1] = (to + i) >> 16;
  356. flash->command[2] = (to + i) >> 8;
  357. flash->command[3] = (to + i);
  358. t[1].tx_buf = buf + i;
  359. t[1].len = page_size;
  360. wait_till_ready(flash);
  361. write_enable(flash);
  362. spi_sync(flash->spi, &m);
  363. if (retlen)
  364. *retlen += m.actual_length - CMD_SIZE;
  365. }
  366. }
  367. mutex_unlock(&flash->lock);
  368. return 0;
  369. }
  370. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  371. size_t *retlen, const u_char *buf)
  372. {
  373. struct m25p *flash = mtd_to_m25p(mtd);
  374. struct spi_transfer t[2];
  375. struct spi_message m;
  376. size_t actual;
  377. int cmd_sz, ret;
  378. if (retlen)
  379. *retlen = 0;
  380. /* sanity checks */
  381. if (!len)
  382. return 0;
  383. if (to + len > flash->mtd.size)
  384. return -EINVAL;
  385. spi_message_init(&m);
  386. memset(t, 0, (sizeof t));
  387. t[0].tx_buf = flash->command;
  388. t[0].len = CMD_SIZE;
  389. spi_message_add_tail(&t[0], &m);
  390. t[1].tx_buf = buf;
  391. spi_message_add_tail(&t[1], &m);
  392. mutex_lock(&flash->lock);
  393. /* Wait until finished previous write command. */
  394. ret = wait_till_ready(flash);
  395. if (ret)
  396. goto time_out;
  397. write_enable(flash);
  398. actual = to % 2;
  399. /* Start write from odd address. */
  400. if (actual) {
  401. flash->command[0] = OPCODE_BP;
  402. flash->command[1] = to >> 16;
  403. flash->command[2] = to >> 8;
  404. flash->command[3] = to;
  405. /* write one byte. */
  406. t[1].len = 1;
  407. spi_sync(flash->spi, &m);
  408. ret = wait_till_ready(flash);
  409. if (ret)
  410. goto time_out;
  411. *retlen += m.actual_length - CMD_SIZE;
  412. }
  413. to += actual;
  414. flash->command[0] = OPCODE_AAI_WP;
  415. flash->command[1] = to >> 16;
  416. flash->command[2] = to >> 8;
  417. flash->command[3] = to;
  418. /* Write out most of the data here. */
  419. cmd_sz = CMD_SIZE;
  420. for (; actual < len - 1; actual += 2) {
  421. t[0].len = cmd_sz;
  422. /* write two bytes. */
  423. t[1].len = 2;
  424. t[1].tx_buf = buf + actual;
  425. spi_sync(flash->spi, &m);
  426. ret = wait_till_ready(flash);
  427. if (ret)
  428. goto time_out;
  429. *retlen += m.actual_length - cmd_sz;
  430. cmd_sz = 1;
  431. to += 2;
  432. }
  433. write_disable(flash);
  434. ret = wait_till_ready(flash);
  435. if (ret)
  436. goto time_out;
  437. /* Write out trailing byte if it exists. */
  438. if (actual != len) {
  439. write_enable(flash);
  440. flash->command[0] = OPCODE_BP;
  441. flash->command[1] = to >> 16;
  442. flash->command[2] = to >> 8;
  443. flash->command[3] = to;
  444. t[0].len = CMD_SIZE;
  445. t[1].len = 1;
  446. t[1].tx_buf = buf + actual;
  447. spi_sync(flash->spi, &m);
  448. ret = wait_till_ready(flash);
  449. if (ret)
  450. goto time_out;
  451. *retlen += m.actual_length - CMD_SIZE;
  452. write_disable(flash);
  453. }
  454. time_out:
  455. mutex_unlock(&flash->lock);
  456. return ret;
  457. }
  458. /****************************************************************************/
  459. /*
  460. * SPI device driver setup and teardown
  461. */
  462. struct flash_info {
  463. char *name;
  464. /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  465. * a high byte of zero plus three data bytes: the manufacturer id,
  466. * then a two byte device id.
  467. */
  468. u32 jedec_id;
  469. u16 ext_id;
  470. /* The size listed here is what works with OPCODE_SE, which isn't
  471. * necessarily called a "sector" by the vendor.
  472. */
  473. unsigned sector_size;
  474. u16 n_sectors;
  475. u16 flags;
  476. #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
  477. };
  478. /* NOTE: double check command sets and memory organization when you add
  479. * more flash chips. This current list focusses on newer chips, which
  480. * have been converging on command sets which including JEDEC ID.
  481. */
  482. static struct flash_info __devinitdata m25p_data [] = {
  483. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  484. { "at25fs010", 0x1f6601, 0, 32 * 1024, 4, SECT_4K, },
  485. { "at25fs040", 0x1f6604, 0, 64 * 1024, 8, SECT_4K, },
  486. { "at25df041a", 0x1f4401, 0, 64 * 1024, 8, SECT_4K, },
  487. { "at25df641", 0x1f4800, 0, 64 * 1024, 128, SECT_4K, },
  488. { "at26f004", 0x1f0400, 0, 64 * 1024, 8, SECT_4K, },
  489. { "at26df081a", 0x1f4501, 0, 64 * 1024, 16, SECT_4K, },
  490. { "at26df161a", 0x1f4601, 0, 64 * 1024, 32, SECT_4K, },
  491. { "at26df321", 0x1f4701, 0, 64 * 1024, 64, SECT_4K, },
  492. /* Macronix */
  493. { "mx25l3205d", 0xc22016, 0, 64 * 1024, 64, },
  494. { "mx25l6405d", 0xc22017, 0, 64 * 1024, 128, },
  495. { "mx25l12805d", 0xc22018, 0, 64 * 1024, 256, },
  496. { "mx25l12855e", 0xc22618, 0, 64 * 1024, 256, },
  497. /* Spansion -- single (large) sector size only, at least
  498. * for the chips listed here (without boot sectors).
  499. */
  500. { "s25sl004a", 0x010212, 0, 64 * 1024, 8, },
  501. { "s25sl008a", 0x010213, 0, 64 * 1024, 16, },
  502. { "s25sl016a", 0x010214, 0, 64 * 1024, 32, },
  503. { "s25sl032a", 0x010215, 0, 64 * 1024, 64, },
  504. { "s25sl064a", 0x010216, 0, 64 * 1024, 128, },
  505. { "s25sl12800", 0x012018, 0x0300, 256 * 1024, 64, },
  506. { "s25sl12801", 0x012018, 0x0301, 64 * 1024, 256, },
  507. { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, },
  508. { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, },
  509. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  510. { "sst25vf040b", 0xbf258d, 0, 64 * 1024, 8, SECT_4K, },
  511. { "sst25vf080b", 0xbf258e, 0, 64 * 1024, 16, SECT_4K, },
  512. { "sst25vf016b", 0xbf2541, 0, 64 * 1024, 32, SECT_4K, },
  513. { "sst25vf032b", 0xbf254a, 0, 64 * 1024, 64, SECT_4K, },
  514. { "sst25wf512", 0xbf2501, 0, 64 * 1024, 1, SECT_4K, },
  515. { "sst25wf010", 0xbf2502, 0, 64 * 1024, 2, SECT_4K, },
  516. { "sst25wf020", 0xbf2503, 0, 64 * 1024, 4, SECT_4K, },
  517. { "sst25wf040", 0xbf2504, 0, 64 * 1024, 8, SECT_4K, },
  518. /* ST Microelectronics -- newer production may have feature updates */
  519. { "m25p05", 0x202010, 0, 32 * 1024, 2, },
  520. { "m25p10", 0x202011, 0, 32 * 1024, 4, },
  521. { "m25p20", 0x202012, 0, 64 * 1024, 4, },
  522. { "m25p40", 0x202013, 0, 64 * 1024, 8, },
  523. { "m25p80", 0, 0, 64 * 1024, 16, },
  524. { "m25p16", 0x202015, 0, 64 * 1024, 32, },
  525. { "m25p32", 0x202016, 0, 64 * 1024, 64, },
  526. { "m25p64", 0x202017, 0, 64 * 1024, 128, },
  527. { "m25p128", 0x202018, 0, 256 * 1024, 64, },
  528. { "m45pe10", 0x204011, 0, 64 * 1024, 2, },
  529. { "m45pe80", 0x204014, 0, 64 * 1024, 16, },
  530. { "m45pe16", 0x204015, 0, 64 * 1024, 32, },
  531. { "m25pe80", 0x208014, 0, 64 * 1024, 16, },
  532. { "m25pe16", 0x208015, 0, 64 * 1024, 32, SECT_4K, },
  533. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  534. { "w25x10", 0xef3011, 0, 64 * 1024, 2, SECT_4K, },
  535. { "w25x20", 0xef3012, 0, 64 * 1024, 4, SECT_4K, },
  536. { "w25x40", 0xef3013, 0, 64 * 1024, 8, SECT_4K, },
  537. { "w25x80", 0xef3014, 0, 64 * 1024, 16, SECT_4K, },
  538. { "w25x16", 0xef3015, 0, 64 * 1024, 32, SECT_4K, },
  539. { "w25x32", 0xef3016, 0, 64 * 1024, 64, SECT_4K, },
  540. { "w25x64", 0xef3017, 0, 64 * 1024, 128, SECT_4K, },
  541. };
  542. static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
  543. {
  544. int tmp;
  545. u8 code = OPCODE_RDID;
  546. u8 id[5];
  547. u32 jedec;
  548. u16 ext_jedec;
  549. struct flash_info *info;
  550. /* JEDEC also defines an optional "extended device information"
  551. * string for after vendor-specific data, after the three bytes
  552. * we use here. Supporting some chips might require using it.
  553. */
  554. tmp = spi_write_then_read(spi, &code, 1, id, 5);
  555. if (tmp < 0) {
  556. DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
  557. dev_name(&spi->dev), tmp);
  558. return NULL;
  559. }
  560. jedec = id[0];
  561. jedec = jedec << 8;
  562. jedec |= id[1];
  563. jedec = jedec << 8;
  564. jedec |= id[2];
  565. ext_jedec = id[3] << 8 | id[4];
  566. for (tmp = 0, info = m25p_data;
  567. tmp < ARRAY_SIZE(m25p_data);
  568. tmp++, info++) {
  569. if (info->jedec_id == jedec) {
  570. if (info->ext_id != 0 && info->ext_id != ext_jedec)
  571. continue;
  572. return info;
  573. }
  574. }
  575. dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
  576. return NULL;
  577. }
  578. /*
  579. * board specific setup should have ensured the SPI clock used here
  580. * matches what the READ command supports, at least until this driver
  581. * understands FAST_READ (for clocks over 25 MHz).
  582. */
  583. static int __devinit m25p_probe(struct spi_device *spi)
  584. {
  585. struct flash_platform_data *data;
  586. struct m25p *flash;
  587. struct flash_info *info;
  588. unsigned i;
  589. /* Platform data helps sort out which chip type we have, as
  590. * well as how this board partitions it. If we don't have
  591. * a chip ID, try the JEDEC id commands; they'll work for most
  592. * newer chips, even if we don't recognize the particular chip.
  593. */
  594. data = spi->dev.platform_data;
  595. if (data && data->type) {
  596. for (i = 0, info = m25p_data;
  597. i < ARRAY_SIZE(m25p_data);
  598. i++, info++) {
  599. if (strcmp(data->type, info->name) == 0)
  600. break;
  601. }
  602. /* unrecognized chip? */
  603. if (i == ARRAY_SIZE(m25p_data)) {
  604. DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
  605. dev_name(&spi->dev), data->type);
  606. info = NULL;
  607. /* recognized; is that chip really what's there? */
  608. } else if (info->jedec_id) {
  609. struct flash_info *chip = jedec_probe(spi);
  610. if (!chip || chip != info) {
  611. dev_warn(&spi->dev, "found %s, expected %s\n",
  612. chip ? chip->name : "UNKNOWN",
  613. info->name);
  614. info = NULL;
  615. }
  616. }
  617. } else
  618. info = jedec_probe(spi);
  619. if (!info)
  620. return -ENODEV;
  621. flash = kzalloc(sizeof *flash, GFP_KERNEL);
  622. if (!flash)
  623. return -ENOMEM;
  624. flash->spi = spi;
  625. mutex_init(&flash->lock);
  626. dev_set_drvdata(&spi->dev, flash);
  627. /*
  628. * Atmel serial flash tend to power up
  629. * with the software protection bits set
  630. */
  631. if (info->jedec_id >> 16 == 0x1f) {
  632. write_enable(flash);
  633. write_sr(flash, 0);
  634. }
  635. if (data && data->name)
  636. flash->mtd.name = data->name;
  637. else
  638. flash->mtd.name = dev_name(&spi->dev);
  639. flash->mtd.type = MTD_NORFLASH;
  640. flash->mtd.writesize = 1;
  641. flash->mtd.flags = MTD_CAP_NORFLASH;
  642. flash->mtd.size = info->sector_size * info->n_sectors;
  643. flash->mtd.erase = m25p80_erase;
  644. flash->mtd.read = m25p80_read;
  645. /* sst flash chips use AAI word program */
  646. if (info->jedec_id >> 16 == 0xbf)
  647. flash->mtd.write = sst_write;
  648. else
  649. flash->mtd.write = m25p80_write;
  650. /* prefer "small sector" erase if possible */
  651. if (info->flags & SECT_4K) {
  652. flash->erase_opcode = OPCODE_BE_4K;
  653. flash->mtd.erasesize = 4096;
  654. } else {
  655. flash->erase_opcode = OPCODE_SE;
  656. flash->mtd.erasesize = info->sector_size;
  657. }
  658. flash->mtd.dev.parent = &spi->dev;
  659. dev_info(&spi->dev, "%s (%lld Kbytes)\n", info->name,
  660. (long long)flash->mtd.size >> 10);
  661. DEBUG(MTD_DEBUG_LEVEL2,
  662. "mtd .name = %s, .size = 0x%llx (%lldMiB) "
  663. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  664. flash->mtd.name,
  665. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  666. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  667. flash->mtd.numeraseregions);
  668. if (flash->mtd.numeraseregions)
  669. for (i = 0; i < flash->mtd.numeraseregions; i++)
  670. DEBUG(MTD_DEBUG_LEVEL2,
  671. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  672. ".erasesize = 0x%.8x (%uKiB), "
  673. ".numblocks = %d }\n",
  674. i, (long long)flash->mtd.eraseregions[i].offset,
  675. flash->mtd.eraseregions[i].erasesize,
  676. flash->mtd.eraseregions[i].erasesize / 1024,
  677. flash->mtd.eraseregions[i].numblocks);
  678. /* partitions should match sector boundaries; and it may be good to
  679. * use readonly partitions for writeprotected sectors (BP2..BP0).
  680. */
  681. if (mtd_has_partitions()) {
  682. struct mtd_partition *parts = NULL;
  683. int nr_parts = 0;
  684. if (mtd_has_cmdlinepart()) {
  685. static const char *part_probes[]
  686. = { "cmdlinepart", NULL, };
  687. nr_parts = parse_mtd_partitions(&flash->mtd,
  688. part_probes, &parts, 0);
  689. }
  690. if (nr_parts <= 0 && data && data->parts) {
  691. parts = data->parts;
  692. nr_parts = data->nr_parts;
  693. }
  694. if (nr_parts > 0) {
  695. for (i = 0; i < nr_parts; i++) {
  696. DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
  697. "{.name = %s, .offset = 0x%llx, "
  698. ".size = 0x%llx (%lldKiB) }\n",
  699. i, parts[i].name,
  700. (long long)parts[i].offset,
  701. (long long)parts[i].size,
  702. (long long)(parts[i].size >> 10));
  703. }
  704. flash->partitioned = 1;
  705. return add_mtd_partitions(&flash->mtd, parts, nr_parts);
  706. }
  707. } else if (data && data->nr_parts)
  708. dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
  709. data->nr_parts, data->name);
  710. return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
  711. }
  712. static int __devexit m25p_remove(struct spi_device *spi)
  713. {
  714. struct m25p *flash = dev_get_drvdata(&spi->dev);
  715. int status;
  716. /* Clean up MTD stuff. */
  717. if (mtd_has_partitions() && flash->partitioned)
  718. status = del_mtd_partitions(&flash->mtd);
  719. else
  720. status = del_mtd_device(&flash->mtd);
  721. if (status == 0)
  722. kfree(flash);
  723. return 0;
  724. }
  725. static struct spi_driver m25p80_driver = {
  726. .driver = {
  727. .name = "m25p80",
  728. .bus = &spi_bus_type,
  729. .owner = THIS_MODULE,
  730. },
  731. .probe = m25p_probe,
  732. .remove = __devexit_p(m25p_remove),
  733. /* REVISIT: many of these chips have deep power-down modes, which
  734. * should clearly be entered on suspend() to minimize power use.
  735. * And also when they're otherwise idle...
  736. */
  737. };
  738. static int __init m25p80_init(void)
  739. {
  740. return spi_register_driver(&m25p80_driver);
  741. }
  742. static void __exit m25p80_exit(void)
  743. {
  744. spi_unregister_driver(&m25p80_driver);
  745. }
  746. module_init(m25p80_init);
  747. module_exit(m25p80_exit);
  748. MODULE_LICENSE("GPL");
  749. MODULE_AUTHOR("Mike Lavender");
  750. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");