m25p80.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. static int m25p80_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  454. {
  455. struct m25p *flash = mtd_to_m25p(mtd);
  456. uint32_t offset = ofs;
  457. uint8_t status_old, status_new;
  458. int res = 0;
  459. mutex_lock(&flash->lock);
  460. /* Wait until finished previous command */
  461. if (wait_till_ready(flash)) {
  462. res = 1;
  463. goto err;
  464. }
  465. status_old = read_sr(flash);
  466. if (offset < flash->mtd.size-(flash->mtd.size/2))
  467. status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
  468. else if (offset < flash->mtd.size-(flash->mtd.size/4))
  469. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  470. else if (offset < flash->mtd.size-(flash->mtd.size/8))
  471. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  472. else if (offset < flash->mtd.size-(flash->mtd.size/16))
  473. status_new = (status_old & ~(SR_BP0|SR_BP1)) | SR_BP2;
  474. else if (offset < flash->mtd.size-(flash->mtd.size/32))
  475. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  476. else if (offset < flash->mtd.size-(flash->mtd.size/64))
  477. status_new = (status_old & ~(SR_BP2|SR_BP0)) | SR_BP1;
  478. else
  479. status_new = (status_old & ~(SR_BP2|SR_BP1)) | SR_BP0;
  480. /* Only modify protection if it will not unlock other areas */
  481. if ((status_new&(SR_BP2|SR_BP1|SR_BP0)) >
  482. (status_old&(SR_BP2|SR_BP1|SR_BP0))) {
  483. write_enable(flash);
  484. if (write_sr(flash, status_new) < 0) {
  485. res = 1;
  486. goto err;
  487. }
  488. }
  489. err: mutex_unlock(&flash->lock);
  490. return res;
  491. }
  492. static int m25p80_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  493. {
  494. struct m25p *flash = mtd_to_m25p(mtd);
  495. uint32_t offset = ofs;
  496. uint8_t status_old, status_new;
  497. int res = 0;
  498. mutex_lock(&flash->lock);
  499. /* Wait until finished previous command */
  500. if (wait_till_ready(flash)) {
  501. res = 1;
  502. goto err;
  503. }
  504. status_old = read_sr(flash);
  505. if (offset+len > flash->mtd.size-(flash->mtd.size/64))
  506. status_new = status_old & ~(SR_BP2|SR_BP1|SR_BP0);
  507. else if (offset+len > flash->mtd.size-(flash->mtd.size/32))
  508. status_new = (status_old & ~(SR_BP2|SR_BP1)) | SR_BP0;
  509. else if (offset+len > flash->mtd.size-(flash->mtd.size/16))
  510. status_new = (status_old & ~(SR_BP2|SR_BP0)) | SR_BP1;
  511. else if (offset+len > flash->mtd.size-(flash->mtd.size/8))
  512. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  513. else if (offset+len > flash->mtd.size-(flash->mtd.size/4))
  514. status_new = (status_old & ~(SR_BP0|SR_BP1)) | SR_BP2;
  515. else if (offset+len > flash->mtd.size-(flash->mtd.size/2))
  516. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  517. else
  518. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  519. /* Only modify protection if it will not lock other areas */
  520. if ((status_new&(SR_BP2|SR_BP1|SR_BP0)) <
  521. (status_old&(SR_BP2|SR_BP1|SR_BP0))) {
  522. write_enable(flash);
  523. if (write_sr(flash, status_new) < 0) {
  524. res = 1;
  525. goto err;
  526. }
  527. }
  528. err: mutex_unlock(&flash->lock);
  529. return res;
  530. }
  531. /****************************************************************************/
  532. /*
  533. * SPI device driver setup and teardown
  534. */
  535. struct flash_info {
  536. /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  537. * a high byte of zero plus three data bytes: the manufacturer id,
  538. * then a two byte device id.
  539. */
  540. u32 jedec_id;
  541. u16 ext_id;
  542. /* The size listed here is what works with OPCODE_SE, which isn't
  543. * necessarily called a "sector" by the vendor.
  544. */
  545. unsigned sector_size;
  546. u16 n_sectors;
  547. u16 page_size;
  548. u16 addr_width;
  549. u16 flags;
  550. #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
  551. #define M25P_NO_ERASE 0x02 /* No erase command needed */
  552. };
  553. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  554. ((kernel_ulong_t)&(struct flash_info) { \
  555. .jedec_id = (_jedec_id), \
  556. .ext_id = (_ext_id), \
  557. .sector_size = (_sector_size), \
  558. .n_sectors = (_n_sectors), \
  559. .page_size = 256, \
  560. .flags = (_flags), \
  561. })
  562. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width) \
  563. ((kernel_ulong_t)&(struct flash_info) { \
  564. .sector_size = (_sector_size), \
  565. .n_sectors = (_n_sectors), \
  566. .page_size = (_page_size), \
  567. .addr_width = (_addr_width), \
  568. .flags = M25P_NO_ERASE, \
  569. })
  570. /* NOTE: double check command sets and memory organization when you add
  571. * more flash chips. This current list focusses on newer chips, which
  572. * have been converging on command sets which including JEDEC ID.
  573. */
  574. static const struct spi_device_id m25p_ids[] = {
  575. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  576. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  577. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  578. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  579. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  580. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  581. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  582. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  583. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  584. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  585. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  586. /* EON -- en25xxx */
  587. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  588. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  589. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  590. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  591. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  592. /* Everspin */
  593. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2) },
  594. /* GigaDevice */
  595. { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
  596. { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
  597. /* Intel/Numonyx -- xxxs33b */
  598. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  599. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  600. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  601. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, 0) },
  602. /* Macronix */
  603. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  604. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  605. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  606. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  607. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  608. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  609. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  610. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  611. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  612. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  613. /* Micron */
  614. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, 0) },
  615. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, 0) },
  616. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K) },
  617. /* Spansion -- single (large) sector size only, at least
  618. * for the chips listed here (without boot sectors).
  619. */
  620. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, 0) },
  621. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  622. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  623. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, 0) },
  624. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, 0) },
  625. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  626. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  627. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  628. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  629. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  630. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  631. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  632. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  633. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  634. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  635. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  636. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  637. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  638. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
  639. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
  640. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
  641. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
  642. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
  643. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
  644. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
  645. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
  646. /* ST Microelectronics -- newer production may have feature updates */
  647. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  648. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  649. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  650. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  651. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  652. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  653. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  654. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  655. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  656. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, 0) },
  657. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  658. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  659. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  660. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  661. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  662. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  663. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  664. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  665. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  666. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  667. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  668. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  669. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  670. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  671. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  672. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  673. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  674. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  675. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  676. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  677. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  678. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  679. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  680. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  681. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  682. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  683. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  684. { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  685. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  686. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  687. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  688. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  689. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  690. /* Catalyst / On Semiconductor -- non-JEDEC */
  691. { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
  692. { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
  693. { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
  694. { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
  695. { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
  696. { },
  697. };
  698. MODULE_DEVICE_TABLE(spi, m25p_ids);
  699. static const struct spi_device_id *jedec_probe(struct spi_device *spi)
  700. {
  701. int tmp;
  702. u8 code = OPCODE_RDID;
  703. u8 id[5];
  704. u32 jedec;
  705. u16 ext_jedec;
  706. struct flash_info *info;
  707. /* JEDEC also defines an optional "extended device information"
  708. * string for after vendor-specific data, after the three bytes
  709. * we use here. Supporting some chips might require using it.
  710. */
  711. tmp = spi_write_then_read(spi, &code, 1, id, 5);
  712. if (tmp < 0) {
  713. pr_debug("%s: error %d reading JEDEC ID\n",
  714. dev_name(&spi->dev), tmp);
  715. return ERR_PTR(tmp);
  716. }
  717. jedec = id[0];
  718. jedec = jedec << 8;
  719. jedec |= id[1];
  720. jedec = jedec << 8;
  721. jedec |= id[2];
  722. ext_jedec = id[3] << 8 | id[4];
  723. for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
  724. info = (void *)m25p_ids[tmp].driver_data;
  725. if (info->jedec_id == jedec) {
  726. if (info->ext_id != 0 && info->ext_id != ext_jedec)
  727. continue;
  728. return &m25p_ids[tmp];
  729. }
  730. }
  731. dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
  732. return ERR_PTR(-ENODEV);
  733. }
  734. /*
  735. * board specific setup should have ensured the SPI clock used here
  736. * matches what the READ command supports, at least until this driver
  737. * understands FAST_READ (for clocks over 25 MHz).
  738. */
  739. static int m25p_probe(struct spi_device *spi)
  740. {
  741. const struct spi_device_id *id = spi_get_device_id(spi);
  742. struct flash_platform_data *data;
  743. struct m25p *flash;
  744. struct flash_info *info;
  745. unsigned i;
  746. struct mtd_part_parser_data ppdata;
  747. struct device_node __maybe_unused *np = spi->dev.of_node;
  748. #ifdef CONFIG_MTD_OF_PARTS
  749. if (!of_device_is_available(np))
  750. return -ENODEV;
  751. #endif
  752. /* Platform data helps sort out which chip type we have, as
  753. * well as how this board partitions it. If we don't have
  754. * a chip ID, try the JEDEC id commands; they'll work for most
  755. * newer chips, even if we don't recognize the particular chip.
  756. */
  757. data = spi->dev.platform_data;
  758. if (data && data->type) {
  759. const struct spi_device_id *plat_id;
  760. for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
  761. plat_id = &m25p_ids[i];
  762. if (strcmp(data->type, plat_id->name))
  763. continue;
  764. break;
  765. }
  766. if (i < ARRAY_SIZE(m25p_ids) - 1)
  767. id = plat_id;
  768. else
  769. dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
  770. }
  771. info = (void *)id->driver_data;
  772. if (info->jedec_id) {
  773. const struct spi_device_id *jid;
  774. jid = jedec_probe(spi);
  775. if (IS_ERR(jid)) {
  776. return PTR_ERR(jid);
  777. } else if (jid != id) {
  778. /*
  779. * JEDEC knows better, so overwrite platform ID. We
  780. * can't trust partitions any longer, but we'll let
  781. * mtd apply them anyway, since some partitions may be
  782. * marked read-only, and we don't want to lose that
  783. * information, even if it's not 100% accurate.
  784. */
  785. dev_warn(&spi->dev, "found %s, expected %s\n",
  786. jid->name, id->name);
  787. id = jid;
  788. info = (void *)jid->driver_data;
  789. }
  790. }
  791. flash = kzalloc(sizeof *flash, GFP_KERNEL);
  792. if (!flash)
  793. return -ENOMEM;
  794. flash->command = kmalloc(MAX_CMD_SIZE + (flash->fast_read ? 1 : 0),
  795. GFP_KERNEL);
  796. if (!flash->command) {
  797. kfree(flash);
  798. return -ENOMEM;
  799. }
  800. flash->spi = spi;
  801. mutex_init(&flash->lock);
  802. dev_set_drvdata(&spi->dev, flash);
  803. /*
  804. * Atmel, SST and Intel/Numonyx serial flash tend to power
  805. * up with the software protection bits set
  806. */
  807. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
  808. JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
  809. JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
  810. write_enable(flash);
  811. write_sr(flash, 0);
  812. }
  813. if (data && data->name)
  814. flash->mtd.name = data->name;
  815. else
  816. flash->mtd.name = dev_name(&spi->dev);
  817. flash->mtd.type = MTD_NORFLASH;
  818. flash->mtd.writesize = 1;
  819. flash->mtd.flags = MTD_CAP_NORFLASH;
  820. flash->mtd.size = info->sector_size * info->n_sectors;
  821. flash->mtd._erase = m25p80_erase;
  822. flash->mtd._read = m25p80_read;
  823. /* flash protection support for STmicro chips */
  824. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
  825. flash->mtd._lock = m25p80_lock;
  826. flash->mtd._unlock = m25p80_unlock;
  827. }
  828. /* sst flash chips use AAI word program */
  829. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_SST)
  830. flash->mtd._write = sst_write;
  831. else
  832. flash->mtd._write = m25p80_write;
  833. /* prefer "small sector" erase if possible */
  834. if (info->flags & SECT_4K) {
  835. flash->erase_opcode = OPCODE_BE_4K;
  836. flash->mtd.erasesize = 4096;
  837. } else {
  838. flash->erase_opcode = OPCODE_SE;
  839. flash->mtd.erasesize = info->sector_size;
  840. }
  841. if (info->flags & M25P_NO_ERASE)
  842. flash->mtd.flags |= MTD_NO_ERASE;
  843. ppdata.of_node = spi->dev.of_node;
  844. flash->mtd.dev.parent = &spi->dev;
  845. flash->page_size = info->page_size;
  846. flash->mtd.writebufsize = flash->page_size;
  847. flash->fast_read = false;
  848. #ifdef CONFIG_OF
  849. if (np && of_property_read_bool(np, "m25p,fast-read"))
  850. flash->fast_read = true;
  851. #endif
  852. #ifdef CONFIG_M25PXX_USE_FAST_READ
  853. flash->fast_read = true;
  854. #endif
  855. if (info->addr_width)
  856. flash->addr_width = info->addr_width;
  857. else {
  858. /* enable 4-byte addressing if the device exceeds 16MiB */
  859. if (flash->mtd.size > 0x1000000) {
  860. flash->addr_width = 4;
  861. set_4byte(flash, info->jedec_id, 1);
  862. } else
  863. flash->addr_width = 3;
  864. }
  865. dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
  866. (long long)flash->mtd.size >> 10);
  867. pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
  868. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  869. flash->mtd.name,
  870. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  871. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  872. flash->mtd.numeraseregions);
  873. if (flash->mtd.numeraseregions)
  874. for (i = 0; i < flash->mtd.numeraseregions; i++)
  875. pr_debug("mtd.eraseregions[%d] = { .offset = 0x%llx, "
  876. ".erasesize = 0x%.8x (%uKiB), "
  877. ".numblocks = %d }\n",
  878. i, (long long)flash->mtd.eraseregions[i].offset,
  879. flash->mtd.eraseregions[i].erasesize,
  880. flash->mtd.eraseregions[i].erasesize / 1024,
  881. flash->mtd.eraseregions[i].numblocks);
  882. /* partitions should match sector boundaries; and it may be good to
  883. * use readonly partitions for writeprotected sectors (BP2..BP0).
  884. */
  885. return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,
  886. data ? data->parts : NULL,
  887. data ? data->nr_parts : 0);
  888. }
  889. static int m25p_remove(struct spi_device *spi)
  890. {
  891. struct m25p *flash = dev_get_drvdata(&spi->dev);
  892. int status;
  893. /* Clean up MTD stuff. */
  894. status = mtd_device_unregister(&flash->mtd);
  895. if (status == 0) {
  896. kfree(flash->command);
  897. kfree(flash);
  898. }
  899. return 0;
  900. }
  901. static struct spi_driver m25p80_driver = {
  902. .driver = {
  903. .name = "m25p80",
  904. .owner = THIS_MODULE,
  905. },
  906. .id_table = m25p_ids,
  907. .probe = m25p_probe,
  908. .remove = m25p_remove,
  909. /* REVISIT: many of these chips have deep power-down modes, which
  910. * should clearly be entered on suspend() to minimize power use.
  911. * And also when they're otherwise idle...
  912. */
  913. };
  914. module_spi_driver(m25p80_driver);
  915. MODULE_LICENSE("GPL");
  916. MODULE_AUTHOR("Mike Lavender");
  917. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");