m25p80.c 28 KB

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