m25p80.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. struct mtd_partition *parts = NULL;
  676. int nr_parts = 0;
  677. /* Platform data helps sort out which chip type we have, as
  678. * well as how this board partitions it. If we don't have
  679. * a chip ID, try the JEDEC id commands; they'll work for most
  680. * newer chips, even if we don't recognize the particular chip.
  681. */
  682. data = spi->dev.platform_data;
  683. if (data && data->type) {
  684. const struct spi_device_id *plat_id;
  685. for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
  686. plat_id = &m25p_ids[i];
  687. if (strcmp(data->type, plat_id->name))
  688. continue;
  689. break;
  690. }
  691. if (i < ARRAY_SIZE(m25p_ids) - 1)
  692. id = plat_id;
  693. else
  694. dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
  695. }
  696. info = (void *)id->driver_data;
  697. if (info->jedec_id) {
  698. const struct spi_device_id *jid;
  699. jid = jedec_probe(spi);
  700. if (IS_ERR(jid)) {
  701. return PTR_ERR(jid);
  702. } else if (jid != id) {
  703. /*
  704. * JEDEC knows better, so overwrite platform ID. We
  705. * can't trust partitions any longer, but we'll let
  706. * mtd apply them anyway, since some partitions may be
  707. * marked read-only, and we don't want to lose that
  708. * information, even if it's not 100% accurate.
  709. */
  710. dev_warn(&spi->dev, "found %s, expected %s\n",
  711. jid->name, id->name);
  712. id = jid;
  713. info = (void *)jid->driver_data;
  714. }
  715. }
  716. flash = kzalloc(sizeof *flash, GFP_KERNEL);
  717. if (!flash)
  718. return -ENOMEM;
  719. flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
  720. if (!flash->command) {
  721. kfree(flash);
  722. return -ENOMEM;
  723. }
  724. flash->spi = spi;
  725. mutex_init(&flash->lock);
  726. dev_set_drvdata(&spi->dev, flash);
  727. /*
  728. * Atmel, SST and Intel/Numonyx serial flash tend to power
  729. * up with the software protection bits set
  730. */
  731. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
  732. JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
  733. JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
  734. write_enable(flash);
  735. write_sr(flash, 0);
  736. }
  737. if (data && data->name)
  738. flash->mtd.name = data->name;
  739. else
  740. flash->mtd.name = dev_name(&spi->dev);
  741. flash->mtd.type = MTD_NORFLASH;
  742. flash->mtd.writesize = 1;
  743. flash->mtd.flags = MTD_CAP_NORFLASH;
  744. flash->mtd.size = info->sector_size * info->n_sectors;
  745. flash->mtd.erase = m25p80_erase;
  746. flash->mtd.read = m25p80_read;
  747. /* sst flash chips use AAI word program */
  748. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_SST)
  749. flash->mtd.write = sst_write;
  750. else
  751. flash->mtd.write = m25p80_write;
  752. /* prefer "small sector" erase if possible */
  753. if (info->flags & SECT_4K) {
  754. flash->erase_opcode = OPCODE_BE_4K;
  755. flash->mtd.erasesize = 4096;
  756. } else {
  757. flash->erase_opcode = OPCODE_SE;
  758. flash->mtd.erasesize = info->sector_size;
  759. }
  760. if (info->flags & M25P_NO_ERASE)
  761. flash->mtd.flags |= MTD_NO_ERASE;
  762. flash->mtd.dev.parent = &spi->dev;
  763. flash->page_size = info->page_size;
  764. if (info->addr_width)
  765. flash->addr_width = info->addr_width;
  766. else {
  767. /* enable 4-byte addressing if the device exceeds 16MiB */
  768. if (flash->mtd.size > 0x1000000) {
  769. flash->addr_width = 4;
  770. set_4byte(flash, info->jedec_id, 1);
  771. } else
  772. flash->addr_width = 3;
  773. }
  774. dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
  775. (long long)flash->mtd.size >> 10);
  776. DEBUG(MTD_DEBUG_LEVEL2,
  777. "mtd .name = %s, .size = 0x%llx (%lldMiB) "
  778. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  779. flash->mtd.name,
  780. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  781. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  782. flash->mtd.numeraseregions);
  783. if (flash->mtd.numeraseregions)
  784. for (i = 0; i < flash->mtd.numeraseregions; i++)
  785. DEBUG(MTD_DEBUG_LEVEL2,
  786. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  787. ".erasesize = 0x%.8x (%uKiB), "
  788. ".numblocks = %d }\n",
  789. i, (long long)flash->mtd.eraseregions[i].offset,
  790. flash->mtd.eraseregions[i].erasesize,
  791. flash->mtd.eraseregions[i].erasesize / 1024,
  792. flash->mtd.eraseregions[i].numblocks);
  793. /* partitions should match sector boundaries; and it may be good to
  794. * use readonly partitions for writeprotected sectors (BP2..BP0).
  795. */
  796. if (mtd_has_cmdlinepart()) {
  797. static const char *part_probes[]
  798. = { "cmdlinepart", NULL, };
  799. nr_parts = parse_mtd_partitions(&flash->mtd,
  800. part_probes, &parts, 0);
  801. }
  802. if (nr_parts <= 0 && data && data->parts) {
  803. parts = data->parts;
  804. nr_parts = data->nr_parts;
  805. }
  806. #ifdef CONFIG_MTD_OF_PARTS
  807. if (nr_parts <= 0 && spi->dev.of_node) {
  808. nr_parts = of_mtd_parse_partitions(&spi->dev,
  809. spi->dev.of_node, &parts);
  810. }
  811. #endif
  812. if (nr_parts > 0) {
  813. for (i = 0; i < nr_parts; i++) {
  814. DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
  815. "{.name = %s, .offset = 0x%llx, "
  816. ".size = 0x%llx (%lldKiB) }\n",
  817. i, parts[i].name,
  818. (long long)parts[i].offset,
  819. (long long)parts[i].size,
  820. (long long)(parts[i].size >> 10));
  821. }
  822. flash->partitioned = 1;
  823. }
  824. return mtd_device_register(&flash->mtd, parts, nr_parts) == 1 ?
  825. -ENODEV : 0;
  826. }
  827. static int __devexit m25p_remove(struct spi_device *spi)
  828. {
  829. struct m25p *flash = dev_get_drvdata(&spi->dev);
  830. int status;
  831. /* Clean up MTD stuff. */
  832. status = mtd_device_unregister(&flash->mtd);
  833. if (status == 0) {
  834. kfree(flash->command);
  835. kfree(flash);
  836. }
  837. return 0;
  838. }
  839. static struct spi_driver m25p80_driver = {
  840. .driver = {
  841. .name = "m25p80",
  842. .bus = &spi_bus_type,
  843. .owner = THIS_MODULE,
  844. },
  845. .id_table = m25p_ids,
  846. .probe = m25p_probe,
  847. .remove = __devexit_p(m25p_remove),
  848. /* REVISIT: many of these chips have deep power-down modes, which
  849. * should clearly be entered on suspend() to minimize power use.
  850. * And also when they're otherwise idle...
  851. */
  852. };
  853. static int __init m25p80_init(void)
  854. {
  855. return spi_register_driver(&m25p80_driver);
  856. }
  857. static void __exit m25p80_exit(void)
  858. {
  859. spi_unregister_driver(&m25p80_driver);
  860. }
  861. module_init(m25p80_init);
  862. module_exit(m25p80_exit);
  863. MODULE_LICENSE("GPL");
  864. MODULE_AUTHOR("Mike Lavender");
  865. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");