m25p80.c 27 KB

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