m25p80.c 28 KB

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