m25p80.c 29 KB

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