spi-nor.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. /*
  2. * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
  3. * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
  4. *
  5. * Copyright (C) 2005, Intec Automation Inc.
  6. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  7. *
  8. * This code is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/mutex.h>
  17. #include <linux/math64.h>
  18. #include <linux/sizes.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/spi/flash.h>
  22. #include <linux/mtd/spi-nor.h>
  23. /* Define max times to check status register before we give up. */
  24. /*
  25. * For everything but full-chip erase; probably could be much smaller, but kept
  26. * around for safety for now
  27. */
  28. #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
  29. /*
  30. * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
  31. * for larger flash
  32. */
  33. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  34. #define SPI_NOR_MAX_ID_LEN 6
  35. #define SPI_NOR_MAX_ADDR_WIDTH 4
  36. struct flash_info {
  37. char *name;
  38. /*
  39. * This array stores the ID bytes.
  40. * The first three bytes are the JEDIC ID.
  41. * JEDEC ID zero means "no ID" (mostly older chips).
  42. */
  43. u8 id[SPI_NOR_MAX_ID_LEN];
  44. u8 id_len;
  45. /* The size listed here is what works with SPINOR_OP_SE, which isn't
  46. * necessarily called a "sector" by the vendor.
  47. */
  48. unsigned sector_size;
  49. u16 n_sectors;
  50. u16 page_size;
  51. u16 addr_width;
  52. u16 flags;
  53. #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
  54. #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
  55. #define SST_WRITE BIT(2) /* use SST byte programming */
  56. #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
  57. #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
  58. #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
  59. #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
  60. #define USE_FSR BIT(7) /* use flag status register */
  61. #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
  62. #define SPI_NOR_HAS_TB BIT(9) /*
  63. * Flash SR has Top/Bottom (TB) protect
  64. * bit. Must be used with
  65. * SPI_NOR_HAS_LOCK.
  66. */
  67. };
  68. #define JEDEC_MFR(info) ((info)->id[0])
  69. static const struct flash_info *spi_nor_match_id(const char *name);
  70. /*
  71. * Read the status register, returning its value in the location
  72. * Return the status register value.
  73. * Returns negative if error occurred.
  74. */
  75. static int read_sr(struct spi_nor *nor)
  76. {
  77. int ret;
  78. u8 val;
  79. ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
  80. if (ret < 0) {
  81. pr_err("error %d reading SR\n", (int) ret);
  82. return ret;
  83. }
  84. return val;
  85. }
  86. /*
  87. * Read the flag 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_fsr(struct spi_nor *nor)
  92. {
  93. int ret;
  94. u8 val;
  95. ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
  96. if (ret < 0) {
  97. pr_err("error %d reading FSR\n", ret);
  98. return ret;
  99. }
  100. return val;
  101. }
  102. /*
  103. * Read configuration register, returning its value in the
  104. * location. Return the configuration register value.
  105. * Returns negative if error occured.
  106. */
  107. static int read_cr(struct spi_nor *nor)
  108. {
  109. int ret;
  110. u8 val;
  111. ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
  112. if (ret < 0) {
  113. dev_err(nor->dev, "error %d reading CR\n", ret);
  114. return ret;
  115. }
  116. return val;
  117. }
  118. /*
  119. * Dummy Cycle calculation for different type of read.
  120. * It can be used to support more commands with
  121. * different dummy cycle requirements.
  122. */
  123. static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
  124. {
  125. switch (nor->flash_read) {
  126. case SPI_NOR_FAST:
  127. case SPI_NOR_DUAL:
  128. case SPI_NOR_QUAD:
  129. return 8;
  130. case SPI_NOR_NORMAL:
  131. return 0;
  132. }
  133. return 0;
  134. }
  135. /*
  136. * Write status register 1 byte
  137. * Returns negative if error occurred.
  138. */
  139. static inline int write_sr(struct spi_nor *nor, u8 val)
  140. {
  141. nor->cmd_buf[0] = val;
  142. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
  143. }
  144. /*
  145. * Set write enable latch with Write Enable command.
  146. * Returns negative if error occurred.
  147. */
  148. static inline int write_enable(struct spi_nor *nor)
  149. {
  150. return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
  151. }
  152. /*
  153. * Send write disble instruction to the chip.
  154. */
  155. static inline int write_disable(struct spi_nor *nor)
  156. {
  157. return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
  158. }
  159. static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  160. {
  161. return mtd->priv;
  162. }
  163. /* Enable/disable 4-byte addressing mode. */
  164. static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
  165. int enable)
  166. {
  167. int status;
  168. bool need_wren = false;
  169. u8 cmd;
  170. switch (JEDEC_MFR(info)) {
  171. case SNOR_MFR_MICRON:
  172. /* Some Micron need WREN command; all will accept it */
  173. need_wren = true;
  174. case SNOR_MFR_MACRONIX:
  175. case SNOR_MFR_WINBOND:
  176. if (need_wren)
  177. write_enable(nor);
  178. cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  179. status = nor->write_reg(nor, cmd, NULL, 0);
  180. if (need_wren)
  181. write_disable(nor);
  182. return status;
  183. default:
  184. /* Spansion style */
  185. nor->cmd_buf[0] = enable << 7;
  186. return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
  187. }
  188. }
  189. static inline int spi_nor_sr_ready(struct spi_nor *nor)
  190. {
  191. int sr = read_sr(nor);
  192. if (sr < 0)
  193. return sr;
  194. else
  195. return !(sr & SR_WIP);
  196. }
  197. static inline int spi_nor_fsr_ready(struct spi_nor *nor)
  198. {
  199. int fsr = read_fsr(nor);
  200. if (fsr < 0)
  201. return fsr;
  202. else
  203. return fsr & FSR_READY;
  204. }
  205. static int spi_nor_ready(struct spi_nor *nor)
  206. {
  207. int sr, fsr;
  208. sr = spi_nor_sr_ready(nor);
  209. if (sr < 0)
  210. return sr;
  211. fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
  212. if (fsr < 0)
  213. return fsr;
  214. return sr && fsr;
  215. }
  216. /*
  217. * Service routine to read status register until ready, or timeout occurs.
  218. * Returns non-zero if error.
  219. */
  220. static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
  221. unsigned long timeout_jiffies)
  222. {
  223. unsigned long deadline;
  224. int timeout = 0, ret;
  225. deadline = jiffies + timeout_jiffies;
  226. while (!timeout) {
  227. if (time_after_eq(jiffies, deadline))
  228. timeout = 1;
  229. ret = spi_nor_ready(nor);
  230. if (ret < 0)
  231. return ret;
  232. if (ret)
  233. return 0;
  234. cond_resched();
  235. }
  236. dev_err(nor->dev, "flash operation timed out\n");
  237. return -ETIMEDOUT;
  238. }
  239. static int spi_nor_wait_till_ready(struct spi_nor *nor)
  240. {
  241. return spi_nor_wait_till_ready_with_timeout(nor,
  242. DEFAULT_READY_WAIT_JIFFIES);
  243. }
  244. /*
  245. * Erase the whole flash memory
  246. *
  247. * Returns 0 if successful, non-zero otherwise.
  248. */
  249. static int erase_chip(struct spi_nor *nor)
  250. {
  251. dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
  252. return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
  253. }
  254. static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  255. {
  256. int ret = 0;
  257. mutex_lock(&nor->lock);
  258. if (nor->prepare) {
  259. ret = nor->prepare(nor, ops);
  260. if (ret) {
  261. dev_err(nor->dev, "failed in the preparation.\n");
  262. mutex_unlock(&nor->lock);
  263. return ret;
  264. }
  265. }
  266. return ret;
  267. }
  268. static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  269. {
  270. if (nor->unprepare)
  271. nor->unprepare(nor, ops);
  272. mutex_unlock(&nor->lock);
  273. }
  274. /*
  275. * Initiate the erasure of a single sector
  276. */
  277. static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  278. {
  279. u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  280. int i;
  281. if (nor->erase)
  282. return nor->erase(nor, addr);
  283. /*
  284. * Default implementation, if driver doesn't have a specialized HW
  285. * control
  286. */
  287. for (i = nor->addr_width - 1; i >= 0; i--) {
  288. buf[i] = addr & 0xff;
  289. addr >>= 8;
  290. }
  291. return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  292. }
  293. /*
  294. * Erase an address range on the nor chip. The address range may extend
  295. * one or more erase sectors. Return an error is there is a problem erasing.
  296. */
  297. static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  298. {
  299. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  300. u32 addr, len;
  301. uint32_t rem;
  302. int ret;
  303. dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  304. (long long)instr->len);
  305. div_u64_rem(instr->len, mtd->erasesize, &rem);
  306. if (rem)
  307. return -EINVAL;
  308. addr = instr->addr;
  309. len = instr->len;
  310. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
  311. if (ret)
  312. return ret;
  313. /* whole-chip erase? */
  314. if (len == mtd->size) {
  315. unsigned long timeout;
  316. write_enable(nor);
  317. if (erase_chip(nor)) {
  318. ret = -EIO;
  319. goto erase_err;
  320. }
  321. /*
  322. * Scale the timeout linearly with the size of the flash, with
  323. * a minimum calibrated to an old 2MB flash. We could try to
  324. * pull these from CFI/SFDP, but these values should be good
  325. * enough for now.
  326. */
  327. timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
  328. CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
  329. (unsigned long)(mtd->size / SZ_2M));
  330. ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
  331. if (ret)
  332. goto erase_err;
  333. /* REVISIT in some cases we could speed up erasing large regions
  334. * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
  335. * to use "small sector erase", but that's not always optimal.
  336. */
  337. /* "sector"-at-a-time erase */
  338. } else {
  339. while (len) {
  340. write_enable(nor);
  341. ret = spi_nor_erase_sector(nor, addr);
  342. if (ret)
  343. goto erase_err;
  344. addr += mtd->erasesize;
  345. len -= mtd->erasesize;
  346. ret = spi_nor_wait_till_ready(nor);
  347. if (ret)
  348. goto erase_err;
  349. }
  350. }
  351. write_disable(nor);
  352. erase_err:
  353. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  354. instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
  355. mtd_erase_callback(instr);
  356. return ret;
  357. }
  358. static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
  359. uint64_t *len)
  360. {
  361. struct mtd_info *mtd = &nor->mtd;
  362. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  363. int shift = ffs(mask) - 1;
  364. int pow;
  365. if (!(sr & mask)) {
  366. /* No protection */
  367. *ofs = 0;
  368. *len = 0;
  369. } else {
  370. pow = ((sr & mask) ^ mask) >> shift;
  371. *len = mtd->size >> pow;
  372. if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
  373. *ofs = 0;
  374. else
  375. *ofs = mtd->size - *len;
  376. }
  377. }
  378. /*
  379. * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
  380. * @locked is false); 0 otherwise
  381. */
  382. static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  383. u8 sr, bool locked)
  384. {
  385. loff_t lock_offs;
  386. uint64_t lock_len;
  387. if (!len)
  388. return 1;
  389. stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
  390. if (locked)
  391. /* Requested range is a sub-range of locked range */
  392. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  393. else
  394. /* Requested range does not overlap with locked range */
  395. return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
  396. }
  397. static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  398. u8 sr)
  399. {
  400. return stm_check_lock_status_sr(nor, ofs, len, sr, true);
  401. }
  402. static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  403. u8 sr)
  404. {
  405. return stm_check_lock_status_sr(nor, ofs, len, sr, false);
  406. }
  407. /*
  408. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  409. * Supports the block protection bits BP{0,1,2} in the status register
  410. * (SR). Does not support these features found in newer SR bitfields:
  411. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  412. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  413. *
  414. * Support for the following is provided conditionally for some flash:
  415. * - TB: top/bottom protect
  416. *
  417. * Sample table portion for 8MB flash (Winbond w25q64fw):
  418. *
  419. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  420. * --------------------------------------------------------------------------
  421. * X | X | 0 | 0 | 0 | NONE | NONE
  422. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  423. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  424. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  425. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  426. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  427. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  428. * X | X | 1 | 1 | 1 | 8 MB | ALL
  429. * ------|-------|-------|-------|-------|---------------|-------------------
  430. * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
  431. * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
  432. * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
  433. * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
  434. * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
  435. * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
  436. *
  437. * Returns negative on errors, 0 on success.
  438. */
  439. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  440. {
  441. struct mtd_info *mtd = &nor->mtd;
  442. int status_old, status_new;
  443. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  444. u8 shift = ffs(mask) - 1, pow, val;
  445. loff_t lock_len;
  446. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  447. bool use_top;
  448. int ret;
  449. status_old = read_sr(nor);
  450. if (status_old < 0)
  451. return status_old;
  452. /* If nothing in our range is unlocked, we don't need to do anything */
  453. if (stm_is_locked_sr(nor, ofs, len, status_old))
  454. return 0;
  455. /* If anything below us is unlocked, we can't use 'bottom' protection */
  456. if (!stm_is_locked_sr(nor, 0, ofs, status_old))
  457. can_be_bottom = false;
  458. /* If anything above us is unlocked, we can't use 'top' protection */
  459. if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
  460. status_old))
  461. can_be_top = false;
  462. if (!can_be_bottom && !can_be_top)
  463. return -EINVAL;
  464. /* Prefer top, if both are valid */
  465. use_top = can_be_top;
  466. /* lock_len: length of region that should end up locked */
  467. if (use_top)
  468. lock_len = mtd->size - ofs;
  469. else
  470. lock_len = ofs + len;
  471. /*
  472. * Need smallest pow such that:
  473. *
  474. * 1 / (2^pow) <= (len / size)
  475. *
  476. * so (assuming power-of-2 size) we do:
  477. *
  478. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  479. */
  480. pow = ilog2(mtd->size) - ilog2(lock_len);
  481. val = mask - (pow << shift);
  482. if (val & ~mask)
  483. return -EINVAL;
  484. /* Don't "lock" with no region! */
  485. if (!(val & mask))
  486. return -EINVAL;
  487. status_new = (status_old & ~mask & ~SR_TB) | val;
  488. /* Disallow further writes if WP pin is asserted */
  489. status_new |= SR_SRWD;
  490. if (!use_top)
  491. status_new |= SR_TB;
  492. /* Don't bother if they're the same */
  493. if (status_new == status_old)
  494. return 0;
  495. /* Only modify protection if it will not unlock other areas */
  496. if ((status_new & mask) < (status_old & mask))
  497. return -EINVAL;
  498. write_enable(nor);
  499. ret = write_sr(nor, status_new);
  500. if (ret)
  501. return ret;
  502. return spi_nor_wait_till_ready(nor);
  503. }
  504. /*
  505. * Unlock a region of the flash. See stm_lock() for more info
  506. *
  507. * Returns negative on errors, 0 on success.
  508. */
  509. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  510. {
  511. struct mtd_info *mtd = &nor->mtd;
  512. int status_old, status_new;
  513. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  514. u8 shift = ffs(mask) - 1, pow, val;
  515. loff_t lock_len;
  516. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  517. bool use_top;
  518. int ret;
  519. status_old = read_sr(nor);
  520. if (status_old < 0)
  521. return status_old;
  522. /* If nothing in our range is locked, we don't need to do anything */
  523. if (stm_is_unlocked_sr(nor, ofs, len, status_old))
  524. return 0;
  525. /* If anything below us is locked, we can't use 'top' protection */
  526. if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
  527. can_be_top = false;
  528. /* If anything above us is locked, we can't use 'bottom' protection */
  529. if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
  530. status_old))
  531. can_be_bottom = false;
  532. if (!can_be_bottom && !can_be_top)
  533. return -EINVAL;
  534. /* Prefer top, if both are valid */
  535. use_top = can_be_top;
  536. /* lock_len: length of region that should remain locked */
  537. if (use_top)
  538. lock_len = mtd->size - (ofs + len);
  539. else
  540. lock_len = ofs;
  541. /*
  542. * Need largest pow such that:
  543. *
  544. * 1 / (2^pow) >= (len / size)
  545. *
  546. * so (assuming power-of-2 size) we do:
  547. *
  548. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  549. */
  550. pow = ilog2(mtd->size) - order_base_2(lock_len);
  551. if (lock_len == 0) {
  552. val = 0; /* fully unlocked */
  553. } else {
  554. val = mask - (pow << shift);
  555. /* Some power-of-two sizes are not supported */
  556. if (val & ~mask)
  557. return -EINVAL;
  558. }
  559. status_new = (status_old & ~mask & ~SR_TB) | val;
  560. /* Don't protect status register if we're fully unlocked */
  561. if (lock_len == 0)
  562. status_new &= ~SR_SRWD;
  563. if (!use_top)
  564. status_new |= SR_TB;
  565. /* Don't bother if they're the same */
  566. if (status_new == status_old)
  567. return 0;
  568. /* Only modify protection if it will not lock other areas */
  569. if ((status_new & mask) > (status_old & mask))
  570. return -EINVAL;
  571. write_enable(nor);
  572. ret = write_sr(nor, status_new);
  573. if (ret)
  574. return ret;
  575. return spi_nor_wait_till_ready(nor);
  576. }
  577. /*
  578. * Check if a region of the flash is (completely) locked. See stm_lock() for
  579. * more info.
  580. *
  581. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  582. * negative on errors.
  583. */
  584. static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
  585. {
  586. int status;
  587. status = read_sr(nor);
  588. if (status < 0)
  589. return status;
  590. return stm_is_locked_sr(nor, ofs, len, status);
  591. }
  592. static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  593. {
  594. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  595. int ret;
  596. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
  597. if (ret)
  598. return ret;
  599. ret = nor->flash_lock(nor, ofs, len);
  600. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
  601. return ret;
  602. }
  603. static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  604. {
  605. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  606. int ret;
  607. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  608. if (ret)
  609. return ret;
  610. ret = nor->flash_unlock(nor, ofs, len);
  611. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  612. return ret;
  613. }
  614. static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  615. {
  616. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  617. int ret;
  618. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  619. if (ret)
  620. return ret;
  621. ret = nor->flash_is_locked(nor, ofs, len);
  622. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  623. return ret;
  624. }
  625. /* Used when the "_ext_id" is two bytes at most */
  626. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  627. .id = { \
  628. ((_jedec_id) >> 16) & 0xff, \
  629. ((_jedec_id) >> 8) & 0xff, \
  630. (_jedec_id) & 0xff, \
  631. ((_ext_id) >> 8) & 0xff, \
  632. (_ext_id) & 0xff, \
  633. }, \
  634. .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
  635. .sector_size = (_sector_size), \
  636. .n_sectors = (_n_sectors), \
  637. .page_size = 256, \
  638. .flags = (_flags),
  639. #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  640. .id = { \
  641. ((_jedec_id) >> 16) & 0xff, \
  642. ((_jedec_id) >> 8) & 0xff, \
  643. (_jedec_id) & 0xff, \
  644. ((_ext_id) >> 16) & 0xff, \
  645. ((_ext_id) >> 8) & 0xff, \
  646. (_ext_id) & 0xff, \
  647. }, \
  648. .id_len = 6, \
  649. .sector_size = (_sector_size), \
  650. .n_sectors = (_n_sectors), \
  651. .page_size = 256, \
  652. .flags = (_flags),
  653. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  654. .sector_size = (_sector_size), \
  655. .n_sectors = (_n_sectors), \
  656. .page_size = (_page_size), \
  657. .addr_width = (_addr_width), \
  658. .flags = (_flags),
  659. /* NOTE: double check command sets and memory organization when you add
  660. * more nor chips. This current list focusses on newer chips, which
  661. * have been converging on command sets which including JEDEC ID.
  662. *
  663. * All newly added entries should describe *hardware* and should use SECT_4K
  664. * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
  665. * scenarios excluding small sectors there is config option that can be
  666. * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
  667. * For historical (and compatibility) reasons (before we got above config) some
  668. * old entries may be missing 4K flag.
  669. */
  670. static const struct flash_info spi_nor_ids[] = {
  671. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  672. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  673. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  674. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  675. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  676. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  677. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  678. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  679. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  680. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  681. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  682. /* EON -- en25xxx */
  683. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  684. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  685. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  686. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  687. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  688. { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  689. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  690. { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
  691. /* ESMT */
  692. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
  693. /* Everspin */
  694. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  695. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  696. /* Fujitsu */
  697. { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
  698. /* GigaDevice */
  699. {
  700. "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
  701. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  702. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  703. },
  704. {
  705. "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
  706. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  707. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  708. },
  709. {
  710. "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
  711. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  712. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  713. },
  714. {
  715. "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
  716. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  717. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  718. },
  719. /* Intel/Numonyx -- xxxs33b */
  720. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  721. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  722. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  723. /* ISSI */
  724. { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
  725. /* Macronix */
  726. { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
  727. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  728. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  729. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  730. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  731. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
  732. { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  733. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
  734. { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
  735. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  736. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  737. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  738. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  739. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
  740. { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
  741. /* Micron */
  742. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  743. { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  744. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  745. { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  746. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  747. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  748. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  749. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  750. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  751. { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  752. { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  753. /* PMC */
  754. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  755. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  756. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  757. /* Spansion -- single (large) sector size only, at least
  758. * for the chips listed here (without boot sectors).
  759. */
  760. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  761. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  762. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  763. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  764. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  765. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  766. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  767. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  768. { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  769. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  770. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  771. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  772. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  773. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  774. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  775. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  776. { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  777. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  778. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  779. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  780. { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  781. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  782. { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  783. { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
  784. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  785. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  786. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  787. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  788. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  789. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  790. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  791. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  792. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  793. { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
  794. { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
  795. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  796. { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  797. /* ST Microelectronics -- newer production may have feature updates */
  798. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  799. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  800. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  801. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  802. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  803. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  804. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  805. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  806. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  807. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  808. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  809. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  810. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  811. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  812. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  813. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  814. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  815. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  816. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  817. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  818. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  819. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  820. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  821. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  822. { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
  823. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  824. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  825. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  826. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  827. { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
  828. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  829. { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
  830. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  831. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  832. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  833. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  834. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  835. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  836. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  837. {
  838. "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
  839. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  840. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  841. },
  842. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  843. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  844. {
  845. "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
  846. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  847. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  848. },
  849. {
  850. "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
  851. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  852. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  853. },
  854. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  855. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  856. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  857. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  858. /* Catalyst / On Semiconductor -- non-JEDEC */
  859. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  860. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  861. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  862. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  863. { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  864. { },
  865. };
  866. static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
  867. {
  868. int tmp;
  869. u8 id[SPI_NOR_MAX_ID_LEN];
  870. const struct flash_info *info;
  871. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  872. if (tmp < 0) {
  873. dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
  874. return ERR_PTR(tmp);
  875. }
  876. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  877. info = &spi_nor_ids[tmp];
  878. if (info->id_len) {
  879. if (!memcmp(info->id, id, info->id_len))
  880. return &spi_nor_ids[tmp];
  881. }
  882. }
  883. dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  884. id[0], id[1], id[2]);
  885. return ERR_PTR(-ENODEV);
  886. }
  887. static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  888. size_t *retlen, u_char *buf)
  889. {
  890. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  891. int ret;
  892. dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  893. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
  894. if (ret)
  895. return ret;
  896. while (len) {
  897. ret = nor->read(nor, from, len, buf);
  898. if (ret == 0) {
  899. /* We shouldn't see 0-length reads */
  900. ret = -EIO;
  901. goto read_err;
  902. }
  903. if (ret < 0)
  904. goto read_err;
  905. WARN_ON(ret > len);
  906. *retlen += ret;
  907. buf += ret;
  908. from += ret;
  909. len -= ret;
  910. }
  911. ret = 0;
  912. read_err:
  913. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
  914. return ret;
  915. }
  916. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  917. size_t *retlen, const u_char *buf)
  918. {
  919. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  920. size_t actual;
  921. int ret;
  922. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  923. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  924. if (ret)
  925. return ret;
  926. write_enable(nor);
  927. nor->sst_write_second = false;
  928. actual = to % 2;
  929. /* Start write from odd address. */
  930. if (actual) {
  931. nor->program_opcode = SPINOR_OP_BP;
  932. /* write one byte. */
  933. ret = nor->write(nor, to, 1, buf);
  934. if (ret < 0)
  935. goto sst_write_err;
  936. WARN(ret != 1, "While writing 1 byte written %i bytes\n",
  937. (int)ret);
  938. ret = spi_nor_wait_till_ready(nor);
  939. if (ret)
  940. goto sst_write_err;
  941. }
  942. to += actual;
  943. /* Write out most of the data here. */
  944. for (; actual < len - 1; actual += 2) {
  945. nor->program_opcode = SPINOR_OP_AAI_WP;
  946. /* write two bytes. */
  947. ret = nor->write(nor, to, 2, buf + actual);
  948. if (ret < 0)
  949. goto sst_write_err;
  950. WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
  951. (int)ret);
  952. ret = spi_nor_wait_till_ready(nor);
  953. if (ret)
  954. goto sst_write_err;
  955. to += 2;
  956. nor->sst_write_second = true;
  957. }
  958. nor->sst_write_second = false;
  959. write_disable(nor);
  960. ret = spi_nor_wait_till_ready(nor);
  961. if (ret)
  962. goto sst_write_err;
  963. /* Write out trailing byte if it exists. */
  964. if (actual != len) {
  965. write_enable(nor);
  966. nor->program_opcode = SPINOR_OP_BP;
  967. ret = nor->write(nor, to, 1, buf + actual);
  968. if (ret < 0)
  969. goto sst_write_err;
  970. WARN(ret != 1, "While writing 1 byte written %i bytes\n",
  971. (int)ret);
  972. ret = spi_nor_wait_till_ready(nor);
  973. if (ret)
  974. goto sst_write_err;
  975. write_disable(nor);
  976. actual += 1;
  977. }
  978. sst_write_err:
  979. *retlen += actual;
  980. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  981. return ret;
  982. }
  983. /*
  984. * Write an address range to the nor chip. Data must be written in
  985. * FLASH_PAGESIZE chunks. The address range may be any size provided
  986. * it is within the physical boundaries.
  987. */
  988. static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  989. size_t *retlen, const u_char *buf)
  990. {
  991. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  992. size_t page_offset, page_remain, i;
  993. ssize_t ret;
  994. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  995. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  996. if (ret)
  997. return ret;
  998. for (i = 0; i < len; ) {
  999. ssize_t written;
  1000. page_offset = (to + i) & (nor->page_size - 1);
  1001. WARN_ONCE(page_offset,
  1002. "Writing at offset %zu into a NOR page. Writing partial pages may decrease reliability and increase wear of NOR flash.",
  1003. page_offset);
  1004. /* the size of data remaining on the first page */
  1005. page_remain = min_t(size_t,
  1006. nor->page_size - page_offset, len - i);
  1007. write_enable(nor);
  1008. ret = nor->write(nor, to + i, page_remain, buf + i);
  1009. if (ret < 0)
  1010. goto write_err;
  1011. written = ret;
  1012. ret = spi_nor_wait_till_ready(nor);
  1013. if (ret)
  1014. goto write_err;
  1015. *retlen += written;
  1016. i += written;
  1017. if (written != page_remain) {
  1018. dev_err(nor->dev,
  1019. "While writing %zu bytes written %zd bytes\n",
  1020. page_remain, written);
  1021. ret = -EIO;
  1022. goto write_err;
  1023. }
  1024. }
  1025. write_err:
  1026. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1027. return ret;
  1028. }
  1029. static int macronix_quad_enable(struct spi_nor *nor)
  1030. {
  1031. int ret, val;
  1032. val = read_sr(nor);
  1033. if (val < 0)
  1034. return val;
  1035. write_enable(nor);
  1036. write_sr(nor, val | SR_QUAD_EN_MX);
  1037. if (spi_nor_wait_till_ready(nor))
  1038. return 1;
  1039. ret = read_sr(nor);
  1040. if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
  1041. dev_err(nor->dev, "Macronix Quad bit not set\n");
  1042. return -EINVAL;
  1043. }
  1044. return 0;
  1045. }
  1046. /*
  1047. * Write status Register and configuration register with 2 bytes
  1048. * The first byte will be written to the status register, while the
  1049. * second byte will be written to the configuration register.
  1050. * Return negative if error occured.
  1051. */
  1052. static int write_sr_cr(struct spi_nor *nor, u16 val)
  1053. {
  1054. nor->cmd_buf[0] = val & 0xff;
  1055. nor->cmd_buf[1] = (val >> 8);
  1056. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
  1057. }
  1058. static int spansion_quad_enable(struct spi_nor *nor)
  1059. {
  1060. int ret;
  1061. int quad_en = CR_QUAD_EN_SPAN << 8;
  1062. write_enable(nor);
  1063. ret = write_sr_cr(nor, quad_en);
  1064. if (ret < 0) {
  1065. dev_err(nor->dev,
  1066. "error while writing configuration register\n");
  1067. return -EINVAL;
  1068. }
  1069. ret = spi_nor_wait_till_ready(nor);
  1070. if (ret) {
  1071. dev_err(nor->dev,
  1072. "timeout while writing configuration register\n");
  1073. return ret;
  1074. }
  1075. /* read back and check it */
  1076. ret = read_cr(nor);
  1077. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  1078. dev_err(nor->dev, "Spansion Quad bit not set\n");
  1079. return -EINVAL;
  1080. }
  1081. return 0;
  1082. }
  1083. static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
  1084. {
  1085. int status;
  1086. switch (JEDEC_MFR(info)) {
  1087. case SNOR_MFR_MACRONIX:
  1088. status = macronix_quad_enable(nor);
  1089. if (status) {
  1090. dev_err(nor->dev, "Macronix quad-read not enabled\n");
  1091. return -EINVAL;
  1092. }
  1093. return status;
  1094. case SNOR_MFR_MICRON:
  1095. return 0;
  1096. default:
  1097. status = spansion_quad_enable(nor);
  1098. if (status) {
  1099. dev_err(nor->dev, "Spansion quad-read not enabled\n");
  1100. return -EINVAL;
  1101. }
  1102. return status;
  1103. }
  1104. }
  1105. static int spi_nor_check(struct spi_nor *nor)
  1106. {
  1107. if (!nor->dev || !nor->read || !nor->write ||
  1108. !nor->read_reg || !nor->write_reg) {
  1109. pr_err("spi-nor: please fill all the necessary fields!\n");
  1110. return -EINVAL;
  1111. }
  1112. return 0;
  1113. }
  1114. int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  1115. {
  1116. const struct flash_info *info = NULL;
  1117. struct device *dev = nor->dev;
  1118. struct mtd_info *mtd = &nor->mtd;
  1119. struct device_node *np = spi_nor_get_flash_node(nor);
  1120. int ret;
  1121. int i;
  1122. ret = spi_nor_check(nor);
  1123. if (ret)
  1124. return ret;
  1125. if (name)
  1126. info = spi_nor_match_id(name);
  1127. /* Try to auto-detect if chip name wasn't specified or not found */
  1128. if (!info)
  1129. info = spi_nor_read_id(nor);
  1130. if (IS_ERR_OR_NULL(info))
  1131. return -ENOENT;
  1132. /*
  1133. * If caller has specified name of flash model that can normally be
  1134. * detected using JEDEC, let's verify it.
  1135. */
  1136. if (name && info->id_len) {
  1137. const struct flash_info *jinfo;
  1138. jinfo = spi_nor_read_id(nor);
  1139. if (IS_ERR(jinfo)) {
  1140. return PTR_ERR(jinfo);
  1141. } else if (jinfo != info) {
  1142. /*
  1143. * JEDEC knows better, so overwrite platform ID. We
  1144. * can't trust partitions any longer, but we'll let
  1145. * mtd apply them anyway, since some partitions may be
  1146. * marked read-only, and we don't want to lose that
  1147. * information, even if it's not 100% accurate.
  1148. */
  1149. dev_warn(dev, "found %s, expected %s\n",
  1150. jinfo->name, info->name);
  1151. info = jinfo;
  1152. }
  1153. }
  1154. mutex_init(&nor->lock);
  1155. /*
  1156. * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
  1157. * with the software protection bits set
  1158. */
  1159. if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
  1160. JEDEC_MFR(info) == SNOR_MFR_INTEL ||
  1161. JEDEC_MFR(info) == SNOR_MFR_SST ||
  1162. info->flags & SPI_NOR_HAS_LOCK) {
  1163. write_enable(nor);
  1164. write_sr(nor, 0);
  1165. spi_nor_wait_till_ready(nor);
  1166. }
  1167. if (!mtd->name)
  1168. mtd->name = dev_name(dev);
  1169. mtd->priv = nor;
  1170. mtd->type = MTD_NORFLASH;
  1171. mtd->writesize = 1;
  1172. mtd->flags = MTD_CAP_NORFLASH;
  1173. mtd->size = info->sector_size * info->n_sectors;
  1174. mtd->_erase = spi_nor_erase;
  1175. mtd->_read = spi_nor_read;
  1176. /* NOR protection support for STmicro/Micron chips and similar */
  1177. if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
  1178. info->flags & SPI_NOR_HAS_LOCK) {
  1179. nor->flash_lock = stm_lock;
  1180. nor->flash_unlock = stm_unlock;
  1181. nor->flash_is_locked = stm_is_locked;
  1182. }
  1183. if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
  1184. mtd->_lock = spi_nor_lock;
  1185. mtd->_unlock = spi_nor_unlock;
  1186. mtd->_is_locked = spi_nor_is_locked;
  1187. }
  1188. /* sst nor chips use AAI word program */
  1189. if (info->flags & SST_WRITE)
  1190. mtd->_write = sst_write;
  1191. else
  1192. mtd->_write = spi_nor_write;
  1193. if (info->flags & USE_FSR)
  1194. nor->flags |= SNOR_F_USE_FSR;
  1195. if (info->flags & SPI_NOR_HAS_TB)
  1196. nor->flags |= SNOR_F_HAS_SR_TB;
  1197. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  1198. /* prefer "small sector" erase if possible */
  1199. if (info->flags & SECT_4K) {
  1200. nor->erase_opcode = SPINOR_OP_BE_4K;
  1201. mtd->erasesize = 4096;
  1202. } else if (info->flags & SECT_4K_PMC) {
  1203. nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
  1204. mtd->erasesize = 4096;
  1205. } else
  1206. #endif
  1207. {
  1208. nor->erase_opcode = SPINOR_OP_SE;
  1209. mtd->erasesize = info->sector_size;
  1210. }
  1211. if (info->flags & SPI_NOR_NO_ERASE)
  1212. mtd->flags |= MTD_NO_ERASE;
  1213. mtd->dev.parent = dev;
  1214. nor->page_size = info->page_size;
  1215. mtd->writebufsize = nor->page_size;
  1216. if (np) {
  1217. /* If we were instantiated by DT, use it */
  1218. if (of_property_read_bool(np, "m25p,fast-read"))
  1219. nor->flash_read = SPI_NOR_FAST;
  1220. else
  1221. nor->flash_read = SPI_NOR_NORMAL;
  1222. } else {
  1223. /* If we weren't instantiated by DT, default to fast-read */
  1224. nor->flash_read = SPI_NOR_FAST;
  1225. }
  1226. /* Some devices cannot do fast-read, no matter what DT tells us */
  1227. if (info->flags & SPI_NOR_NO_FR)
  1228. nor->flash_read = SPI_NOR_NORMAL;
  1229. /* Quad/Dual-read mode takes precedence over fast/normal */
  1230. if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
  1231. ret = set_quad_mode(nor, info);
  1232. if (ret) {
  1233. dev_err(dev, "quad mode not supported\n");
  1234. return ret;
  1235. }
  1236. nor->flash_read = SPI_NOR_QUAD;
  1237. } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
  1238. nor->flash_read = SPI_NOR_DUAL;
  1239. }
  1240. /* Default commands */
  1241. switch (nor->flash_read) {
  1242. case SPI_NOR_QUAD:
  1243. nor->read_opcode = SPINOR_OP_READ_1_1_4;
  1244. break;
  1245. case SPI_NOR_DUAL:
  1246. nor->read_opcode = SPINOR_OP_READ_1_1_2;
  1247. break;
  1248. case SPI_NOR_FAST:
  1249. nor->read_opcode = SPINOR_OP_READ_FAST;
  1250. break;
  1251. case SPI_NOR_NORMAL:
  1252. nor->read_opcode = SPINOR_OP_READ;
  1253. break;
  1254. default:
  1255. dev_err(dev, "No Read opcode defined\n");
  1256. return -EINVAL;
  1257. }
  1258. nor->program_opcode = SPINOR_OP_PP;
  1259. if (info->addr_width)
  1260. nor->addr_width = info->addr_width;
  1261. else if (mtd->size > 0x1000000) {
  1262. /* enable 4-byte addressing if the device exceeds 16MiB */
  1263. nor->addr_width = 4;
  1264. if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
  1265. /* Dedicated 4-byte command set */
  1266. switch (nor->flash_read) {
  1267. case SPI_NOR_QUAD:
  1268. nor->read_opcode = SPINOR_OP_READ4_1_1_4;
  1269. break;
  1270. case SPI_NOR_DUAL:
  1271. nor->read_opcode = SPINOR_OP_READ4_1_1_2;
  1272. break;
  1273. case SPI_NOR_FAST:
  1274. nor->read_opcode = SPINOR_OP_READ4_FAST;
  1275. break;
  1276. case SPI_NOR_NORMAL:
  1277. nor->read_opcode = SPINOR_OP_READ4;
  1278. break;
  1279. }
  1280. nor->program_opcode = SPINOR_OP_PP_4B;
  1281. /* No small sector erase for 4-byte command set */
  1282. nor->erase_opcode = SPINOR_OP_SE_4B;
  1283. mtd->erasesize = info->sector_size;
  1284. } else
  1285. set_4byte(nor, info, 1);
  1286. } else {
  1287. nor->addr_width = 3;
  1288. }
  1289. if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  1290. dev_err(dev, "address width is too large: %u\n",
  1291. nor->addr_width);
  1292. return -EINVAL;
  1293. }
  1294. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  1295. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  1296. (long long)mtd->size >> 10);
  1297. dev_dbg(dev,
  1298. "mtd .name = %s, .size = 0x%llx (%lldMiB), "
  1299. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  1300. mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
  1301. mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
  1302. if (mtd->numeraseregions)
  1303. for (i = 0; i < mtd->numeraseregions; i++)
  1304. dev_dbg(dev,
  1305. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  1306. ".erasesize = 0x%.8x (%uKiB), "
  1307. ".numblocks = %d }\n",
  1308. i, (long long)mtd->eraseregions[i].offset,
  1309. mtd->eraseregions[i].erasesize,
  1310. mtd->eraseregions[i].erasesize / 1024,
  1311. mtd->eraseregions[i].numblocks);
  1312. return 0;
  1313. }
  1314. EXPORT_SYMBOL_GPL(spi_nor_scan);
  1315. static const struct flash_info *spi_nor_match_id(const char *name)
  1316. {
  1317. const struct flash_info *id = spi_nor_ids;
  1318. while (id->name) {
  1319. if (!strcmp(name, id->name))
  1320. return id;
  1321. id++;
  1322. }
  1323. return NULL;
  1324. }
  1325. MODULE_LICENSE("GPL");
  1326. MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
  1327. MODULE_AUTHOR("Mike Lavender");
  1328. MODULE_DESCRIPTION("framework for SPI NOR");