m25p80.c 26 KB

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