m25p80.c 25 KB

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