m25p80.c 25 KB

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