m25p80.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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 6
  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. div_u64_rem(instr->len, mtd->erasesize, &rem);
  240. if (rem)
  241. return -EINVAL;
  242. addr = instr->addr;
  243. len = instr->len;
  244. mutex_lock(&flash->lock);
  245. /* whole-chip erase? */
  246. if (len == flash->mtd.size) {
  247. if (erase_chip(flash)) {
  248. instr->state = MTD_ERASE_FAILED;
  249. mutex_unlock(&flash->lock);
  250. return -EIO;
  251. }
  252. /* REVISIT in some cases we could speed up erasing large regions
  253. * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
  254. * to use "small sector erase", but that's not always optimal.
  255. */
  256. /* "sector"-at-a-time erase */
  257. } else {
  258. while (len) {
  259. if (erase_sector(flash, addr)) {
  260. instr->state = MTD_ERASE_FAILED;
  261. mutex_unlock(&flash->lock);
  262. return -EIO;
  263. }
  264. addr += mtd->erasesize;
  265. len -= mtd->erasesize;
  266. }
  267. }
  268. mutex_unlock(&flash->lock);
  269. instr->state = MTD_ERASE_DONE;
  270. mtd_erase_callback(instr);
  271. return 0;
  272. }
  273. /*
  274. * Read an address range from the flash chip. The address range
  275. * may be any size provided it is within the physical boundaries.
  276. */
  277. static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
  278. size_t *retlen, u_char *buf)
  279. {
  280. struct m25p *flash = mtd_to_m25p(mtd);
  281. struct spi_transfer t[2];
  282. struct spi_message m;
  283. pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  284. __func__, (u32)from, len);
  285. spi_message_init(&m);
  286. memset(t, 0, (sizeof t));
  287. /* NOTE:
  288. * OPCODE_FAST_READ (if available) is faster.
  289. * Should add 1 byte DUMMY_BYTE.
  290. */
  291. t[0].tx_buf = flash->command;
  292. t[0].len = m25p_cmdsz(flash) + FAST_READ_DUMMY_BYTE;
  293. spi_message_add_tail(&t[0], &m);
  294. t[1].rx_buf = buf;
  295. t[1].len = len;
  296. spi_message_add_tail(&t[1], &m);
  297. mutex_lock(&flash->lock);
  298. /* Wait till previous write/erase is done. */
  299. if (wait_till_ready(flash)) {
  300. /* REVISIT status return?? */
  301. mutex_unlock(&flash->lock);
  302. return 1;
  303. }
  304. /* FIXME switch to OPCODE_FAST_READ. It's required for higher
  305. * clocks; and at this writing, every chip this driver handles
  306. * supports that opcode.
  307. */
  308. /* Set up the write data buffer. */
  309. flash->command[0] = OPCODE_READ;
  310. m25p_addr2cmd(flash, from, flash->command);
  311. spi_sync(flash->spi, &m);
  312. *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
  313. mutex_unlock(&flash->lock);
  314. return 0;
  315. }
  316. /*
  317. * Write an address range to the flash chip. Data must be written in
  318. * FLASH_PAGESIZE chunks. The address range may be any size provided
  319. * it is within the physical boundaries.
  320. */
  321. static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
  322. size_t *retlen, const u_char *buf)
  323. {
  324. struct m25p *flash = mtd_to_m25p(mtd);
  325. u32 page_offset, page_size;
  326. struct spi_transfer t[2];
  327. struct spi_message m;
  328. pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  329. __func__, (u32)to, len);
  330. spi_message_init(&m);
  331. memset(t, 0, (sizeof t));
  332. t[0].tx_buf = flash->command;
  333. t[0].len = m25p_cmdsz(flash);
  334. spi_message_add_tail(&t[0], &m);
  335. t[1].tx_buf = buf;
  336. spi_message_add_tail(&t[1], &m);
  337. mutex_lock(&flash->lock);
  338. /* Wait until finished previous write command. */
  339. if (wait_till_ready(flash)) {
  340. mutex_unlock(&flash->lock);
  341. return 1;
  342. }
  343. write_enable(flash);
  344. /* Set up the opcode in the write buffer. */
  345. flash->command[0] = OPCODE_PP;
  346. m25p_addr2cmd(flash, to, flash->command);
  347. page_offset = to & (flash->page_size - 1);
  348. /* do all the bytes fit onto one page? */
  349. if (page_offset + len <= flash->page_size) {
  350. t[1].len = len;
  351. spi_sync(flash->spi, &m);
  352. *retlen = m.actual_length - m25p_cmdsz(flash);
  353. } else {
  354. u32 i;
  355. /* the size of data remaining on the first page */
  356. page_size = flash->page_size - page_offset;
  357. t[1].len = page_size;
  358. spi_sync(flash->spi, &m);
  359. *retlen = m.actual_length - m25p_cmdsz(flash);
  360. /* write everything in flash->page_size chunks */
  361. for (i = page_size; i < len; i += page_size) {
  362. page_size = len - i;
  363. if (page_size > flash->page_size)
  364. page_size = flash->page_size;
  365. /* write the next page to flash */
  366. m25p_addr2cmd(flash, to + i, flash->command);
  367. t[1].tx_buf = buf + i;
  368. t[1].len = page_size;
  369. wait_till_ready(flash);
  370. write_enable(flash);
  371. spi_sync(flash->spi, &m);
  372. *retlen += m.actual_length - m25p_cmdsz(flash);
  373. }
  374. }
  375. mutex_unlock(&flash->lock);
  376. return 0;
  377. }
  378. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  379. size_t *retlen, const u_char *buf)
  380. {
  381. struct m25p *flash = mtd_to_m25p(mtd);
  382. struct spi_transfer t[2];
  383. struct spi_message m;
  384. size_t actual;
  385. int cmd_sz, ret;
  386. pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  387. __func__, (u32)to, len);
  388. spi_message_init(&m);
  389. memset(t, 0, (sizeof t));
  390. t[0].tx_buf = flash->command;
  391. t[0].len = m25p_cmdsz(flash);
  392. spi_message_add_tail(&t[0], &m);
  393. t[1].tx_buf = buf;
  394. spi_message_add_tail(&t[1], &m);
  395. mutex_lock(&flash->lock);
  396. /* Wait until finished previous write command. */
  397. ret = wait_till_ready(flash);
  398. if (ret)
  399. goto time_out;
  400. write_enable(flash);
  401. actual = to % 2;
  402. /* Start write from odd address. */
  403. if (actual) {
  404. flash->command[0] = OPCODE_BP;
  405. m25p_addr2cmd(flash, to, flash->command);
  406. /* write one byte. */
  407. t[1].len = 1;
  408. spi_sync(flash->spi, &m);
  409. ret = wait_till_ready(flash);
  410. if (ret)
  411. goto time_out;
  412. *retlen += m.actual_length - m25p_cmdsz(flash);
  413. }
  414. to += actual;
  415. flash->command[0] = OPCODE_AAI_WP;
  416. m25p_addr2cmd(flash, to, flash->command);
  417. /* Write out most of the data here. */
  418. cmd_sz = m25p_cmdsz(flash);
  419. for (; actual < len - 1; actual += 2) {
  420. t[0].len = cmd_sz;
  421. /* write two bytes. */
  422. t[1].len = 2;
  423. t[1].tx_buf = buf + actual;
  424. spi_sync(flash->spi, &m);
  425. ret = wait_till_ready(flash);
  426. if (ret)
  427. goto time_out;
  428. *retlen += m.actual_length - cmd_sz;
  429. cmd_sz = 1;
  430. to += 2;
  431. }
  432. write_disable(flash);
  433. ret = wait_till_ready(flash);
  434. if (ret)
  435. goto time_out;
  436. /* Write out trailing byte if it exists. */
  437. if (actual != len) {
  438. write_enable(flash);
  439. flash->command[0] = OPCODE_BP;
  440. m25p_addr2cmd(flash, to, flash->command);
  441. t[0].len = m25p_cmdsz(flash);
  442. t[1].len = 1;
  443. t[1].tx_buf = buf + actual;
  444. spi_sync(flash->spi, &m);
  445. ret = wait_till_ready(flash);
  446. if (ret)
  447. goto time_out;
  448. *retlen += m.actual_length - m25p_cmdsz(flash);
  449. write_disable(flash);
  450. }
  451. time_out:
  452. mutex_unlock(&flash->lock);
  453. return ret;
  454. }
  455. /****************************************************************************/
  456. /*
  457. * SPI device driver setup and teardown
  458. */
  459. struct flash_info {
  460. /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  461. * a high byte of zero plus three data bytes: the manufacturer id,
  462. * then a two byte device id.
  463. */
  464. u32 jedec_id;
  465. u16 ext_id;
  466. /* The size listed here is what works with OPCODE_SE, which isn't
  467. * necessarily called a "sector" by the vendor.
  468. */
  469. unsigned sector_size;
  470. u16 n_sectors;
  471. u16 page_size;
  472. u16 addr_width;
  473. u16 flags;
  474. #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
  475. #define M25P_NO_ERASE 0x02 /* No erase command needed */
  476. };
  477. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  478. ((kernel_ulong_t)&(struct flash_info) { \
  479. .jedec_id = (_jedec_id), \
  480. .ext_id = (_ext_id), \
  481. .sector_size = (_sector_size), \
  482. .n_sectors = (_n_sectors), \
  483. .page_size = 256, \
  484. .flags = (_flags), \
  485. })
  486. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width) \
  487. ((kernel_ulong_t)&(struct flash_info) { \
  488. .sector_size = (_sector_size), \
  489. .n_sectors = (_n_sectors), \
  490. .page_size = (_page_size), \
  491. .addr_width = (_addr_width), \
  492. .flags = M25P_NO_ERASE, \
  493. })
  494. /* NOTE: double check command sets and memory organization when you add
  495. * more flash chips. This current list focusses on newer chips, which
  496. * have been converging on command sets which including JEDEC ID.
  497. */
  498. static const struct spi_device_id m25p_ids[] = {
  499. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  500. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  501. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  502. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  503. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  504. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  505. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  506. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  507. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  508. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  509. /* EON -- en25xxx */
  510. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  511. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  512. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  513. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  514. /* Intel/Numonyx -- xxxs33b */
  515. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  516. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  517. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  518. /* Macronix */
  519. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  520. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  521. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  522. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  523. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  524. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  525. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  526. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  527. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  528. /* Spansion -- single (large) sector size only, at least
  529. * for the chips listed here (without boot sectors).
  530. */
  531. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  532. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  533. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  534. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  535. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SECT_4K) },
  536. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  537. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  538. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, 0) },
  539. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, 0) },
  540. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  541. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  542. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  543. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  544. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  545. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  546. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  547. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  548. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
  549. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
  550. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
  551. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
  552. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
  553. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
  554. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
  555. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
  556. /* ST Microelectronics -- newer production may have feature updates */
  557. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  558. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  559. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  560. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  561. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  562. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  563. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  564. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  565. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  566. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  567. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  568. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  569. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  570. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  571. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  572. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  573. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  574. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  575. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  576. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  577. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  578. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  579. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  580. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  581. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  582. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  583. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  584. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  585. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  586. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  587. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  588. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  589. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  590. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  591. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  592. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  593. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  594. /* Catalyst / On Semiconductor -- non-JEDEC */
  595. { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
  596. { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
  597. { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
  598. { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
  599. { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
  600. { },
  601. };
  602. MODULE_DEVICE_TABLE(spi, m25p_ids);
  603. static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
  604. {
  605. int tmp;
  606. u8 code = OPCODE_RDID;
  607. u8 id[5];
  608. u32 jedec;
  609. u16 ext_jedec;
  610. struct flash_info *info;
  611. /* JEDEC also defines an optional "extended device information"
  612. * string for after vendor-specific data, after the three bytes
  613. * we use here. Supporting some chips might require using it.
  614. */
  615. tmp = spi_write_then_read(spi, &code, 1, id, 5);
  616. if (tmp < 0) {
  617. pr_debug("%s: error %d reading JEDEC ID\n",
  618. dev_name(&spi->dev), tmp);
  619. return ERR_PTR(tmp);
  620. }
  621. jedec = id[0];
  622. jedec = jedec << 8;
  623. jedec |= id[1];
  624. jedec = jedec << 8;
  625. jedec |= id[2];
  626. ext_jedec = id[3] << 8 | id[4];
  627. for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
  628. info = (void *)m25p_ids[tmp].driver_data;
  629. if (info->jedec_id == jedec) {
  630. if (info->ext_id != 0 && info->ext_id != ext_jedec)
  631. continue;
  632. return &m25p_ids[tmp];
  633. }
  634. }
  635. dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
  636. return ERR_PTR(-ENODEV);
  637. }
  638. /*
  639. * board specific setup should have ensured the SPI clock used here
  640. * matches what the READ command supports, at least until this driver
  641. * understands FAST_READ (for clocks over 25 MHz).
  642. */
  643. static int __devinit m25p_probe(struct spi_device *spi)
  644. {
  645. const struct spi_device_id *id = spi_get_device_id(spi);
  646. struct flash_platform_data *data;
  647. struct m25p *flash;
  648. struct flash_info *info;
  649. unsigned i;
  650. struct mtd_part_parser_data ppdata;
  651. #ifdef CONFIG_MTD_OF_PARTS
  652. if (!of_device_is_available(spi->dev.of_node))
  653. return -ENODEV;
  654. #endif
  655. /* Platform data helps sort out which chip type we have, as
  656. * well as how this board partitions it. If we don't have
  657. * a chip ID, try the JEDEC id commands; they'll work for most
  658. * newer chips, even if we don't recognize the particular chip.
  659. */
  660. data = spi->dev.platform_data;
  661. if (data && data->type) {
  662. const struct spi_device_id *plat_id;
  663. for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
  664. plat_id = &m25p_ids[i];
  665. if (strcmp(data->type, plat_id->name))
  666. continue;
  667. break;
  668. }
  669. if (i < ARRAY_SIZE(m25p_ids) - 1)
  670. id = plat_id;
  671. else
  672. dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
  673. }
  674. info = (void *)id->driver_data;
  675. if (info->jedec_id) {
  676. const struct spi_device_id *jid;
  677. jid = jedec_probe(spi);
  678. if (IS_ERR(jid)) {
  679. return PTR_ERR(jid);
  680. } else if (jid != id) {
  681. /*
  682. * JEDEC knows better, so overwrite platform ID. We
  683. * can't trust partitions any longer, but we'll let
  684. * mtd apply them anyway, since some partitions may be
  685. * marked read-only, and we don't want to lose that
  686. * information, even if it's not 100% accurate.
  687. */
  688. dev_warn(&spi->dev, "found %s, expected %s\n",
  689. jid->name, id->name);
  690. id = jid;
  691. info = (void *)jid->driver_data;
  692. }
  693. }
  694. flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
  695. if (!flash)
  696. return -ENOMEM;
  697. flash->command = devm_kzalloc(&spi->dev, MAX_CMD_SIZE, GFP_KERNEL);
  698. if (!flash->command)
  699. return -ENOMEM;
  700. flash->spi = spi;
  701. mutex_init(&flash->lock);
  702. dev_set_drvdata(&spi->dev, flash);
  703. /*
  704. * Atmel, SST and Intel/Numonyx serial flash tend to power
  705. * up with the software protection bits set
  706. */
  707. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
  708. JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
  709. JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
  710. write_enable(flash);
  711. write_sr(flash, 0);
  712. }
  713. if (data && data->name)
  714. flash->mtd.name = data->name;
  715. else
  716. flash->mtd.name = dev_name(&spi->dev);
  717. flash->mtd.type = MTD_NORFLASH;
  718. flash->mtd.writesize = 1;
  719. flash->mtd.flags = MTD_CAP_NORFLASH;
  720. flash->mtd.size = info->sector_size * info->n_sectors;
  721. flash->mtd._erase = m25p80_erase;
  722. flash->mtd._read = m25p80_read;
  723. /* sst flash chips use AAI word program */
  724. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_SST)
  725. flash->mtd._write = sst_write;
  726. else
  727. flash->mtd._write = m25p80_write;
  728. /* prefer "small sector" erase if possible */
  729. if (info->flags & SECT_4K) {
  730. flash->erase_opcode = OPCODE_BE_4K;
  731. flash->mtd.erasesize = 4096;
  732. } else {
  733. flash->erase_opcode = OPCODE_SE;
  734. flash->mtd.erasesize = info->sector_size;
  735. }
  736. if (info->flags & M25P_NO_ERASE)
  737. flash->mtd.flags |= MTD_NO_ERASE;
  738. ppdata.of_node = spi->dev.of_node;
  739. flash->mtd.dev.parent = &spi->dev;
  740. flash->page_size = info->page_size;
  741. flash->mtd.writebufsize = flash->page_size;
  742. if (info->addr_width)
  743. flash->addr_width = info->addr_width;
  744. else {
  745. /* enable 4-byte addressing if the device exceeds 16MiB */
  746. if (flash->mtd.size > 0x1000000) {
  747. flash->addr_width = 4;
  748. set_4byte(flash, info->jedec_id, 1);
  749. } else
  750. flash->addr_width = 3;
  751. }
  752. dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
  753. (long long)flash->mtd.size >> 10);
  754. pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
  755. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  756. flash->mtd.name,
  757. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  758. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  759. flash->mtd.numeraseregions);
  760. if (flash->mtd.numeraseregions)
  761. for (i = 0; i < flash->mtd.numeraseregions; i++)
  762. pr_debug("mtd.eraseregions[%d] = { .offset = 0x%llx, "
  763. ".erasesize = 0x%.8x (%uKiB), "
  764. ".numblocks = %d }\n",
  765. i, (long long)flash->mtd.eraseregions[i].offset,
  766. flash->mtd.eraseregions[i].erasesize,
  767. flash->mtd.eraseregions[i].erasesize / 1024,
  768. flash->mtd.eraseregions[i].numblocks);
  769. /* partitions should match sector boundaries; and it may be good to
  770. * use readonly partitions for writeprotected sectors (BP2..BP0).
  771. */
  772. return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,
  773. data ? data->parts : NULL,
  774. data ? data->nr_parts : 0);
  775. }
  776. static int __devexit m25p_remove(struct spi_device *spi)
  777. {
  778. struct m25p *flash = dev_get_drvdata(&spi->dev);
  779. /* Clean up MTD stuff. */
  780. mtd_device_unregister(&flash->mtd);
  781. return 0;
  782. }
  783. static struct spi_driver m25p80_driver = {
  784. .driver = {
  785. .name = "m25p80",
  786. .owner = THIS_MODULE,
  787. },
  788. .id_table = m25p_ids,
  789. .probe = m25p_probe,
  790. .remove = __devexit_p(m25p_remove),
  791. /* REVISIT: many of these chips have deep power-down modes, which
  792. * should clearly be entered on suspend() to minimize power use.
  793. * And also when they're otherwise idle...
  794. */
  795. };
  796. module_spi_driver(m25p80_driver);
  797. MODULE_LICENSE("GPL");
  798. MODULE_AUTHOR("Mike Lavender");
  799. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");