m25p80.c 28 KB

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