m25p80.c 26 KB

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