m25p80.c 28 KB

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