spi-nor.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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/mtd/cfi.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. #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  25. #define SPI_NOR_MAX_ID_LEN 6
  26. struct flash_info {
  27. /*
  28. * This array stores the ID bytes.
  29. * The first three bytes are the JEDIC ID.
  30. * JEDEC ID zero means "no ID" (mostly older chips).
  31. */
  32. u8 id[SPI_NOR_MAX_ID_LEN];
  33. u8 id_len;
  34. /* The size listed here is what works with SPINOR_OP_SE, which isn't
  35. * necessarily called a "sector" by the vendor.
  36. */
  37. unsigned sector_size;
  38. u16 n_sectors;
  39. u16 page_size;
  40. u16 addr_width;
  41. u16 flags;
  42. #define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
  43. #define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
  44. #define SST_WRITE 0x04 /* use SST byte programming */
  45. #define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
  46. #define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
  47. #define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
  48. #define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
  49. #define USE_FSR 0x80 /* use flag status register */
  50. };
  51. #define JEDEC_MFR(info) ((info)->id[0])
  52. static const struct spi_device_id *spi_nor_match_id(const char *name);
  53. /*
  54. * Read the status register, returning its value in the location
  55. * Return the status register value.
  56. * Returns negative if error occurred.
  57. */
  58. static int read_sr(struct spi_nor *nor)
  59. {
  60. int ret;
  61. u8 val;
  62. ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
  63. if (ret < 0) {
  64. pr_err("error %d reading SR\n", (int) ret);
  65. return ret;
  66. }
  67. return val;
  68. }
  69. /*
  70. * Read the flag status register, returning its value in the location
  71. * Return the status register value.
  72. * Returns negative if error occurred.
  73. */
  74. static int read_fsr(struct spi_nor *nor)
  75. {
  76. int ret;
  77. u8 val;
  78. ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
  79. if (ret < 0) {
  80. pr_err("error %d reading FSR\n", ret);
  81. return ret;
  82. }
  83. return val;
  84. }
  85. /*
  86. * Read configuration register, returning its value in the
  87. * location. Return the configuration register value.
  88. * Returns negative if error occured.
  89. */
  90. static int read_cr(struct spi_nor *nor)
  91. {
  92. int ret;
  93. u8 val;
  94. ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
  95. if (ret < 0) {
  96. dev_err(nor->dev, "error %d reading CR\n", ret);
  97. return ret;
  98. }
  99. return val;
  100. }
  101. /*
  102. * Dummy Cycle calculation for different type of read.
  103. * It can be used to support more commands with
  104. * different dummy cycle requirements.
  105. */
  106. static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
  107. {
  108. switch (nor->flash_read) {
  109. case SPI_NOR_FAST:
  110. case SPI_NOR_DUAL:
  111. case SPI_NOR_QUAD:
  112. return 8;
  113. case SPI_NOR_NORMAL:
  114. return 0;
  115. }
  116. return 0;
  117. }
  118. /*
  119. * Write status register 1 byte
  120. * Returns negative if error occurred.
  121. */
  122. static inline int write_sr(struct spi_nor *nor, u8 val)
  123. {
  124. nor->cmd_buf[0] = val;
  125. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  126. }
  127. /*
  128. * Set write enable latch with Write Enable command.
  129. * Returns negative if error occurred.
  130. */
  131. static inline int write_enable(struct spi_nor *nor)
  132. {
  133. return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
  134. }
  135. /*
  136. * Send write disble instruction to the chip.
  137. */
  138. static inline int write_disable(struct spi_nor *nor)
  139. {
  140. return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0, 0);
  141. }
  142. static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  143. {
  144. return mtd->priv;
  145. }
  146. /* Enable/disable 4-byte addressing mode. */
  147. static inline int set_4byte(struct spi_nor *nor, struct flash_info *info,
  148. int enable)
  149. {
  150. int status;
  151. bool need_wren = false;
  152. u8 cmd;
  153. switch (JEDEC_MFR(info)) {
  154. case CFI_MFR_ST: /* Micron, actually */
  155. /* Some Micron need WREN command; all will accept it */
  156. need_wren = true;
  157. case CFI_MFR_MACRONIX:
  158. case 0xEF /* winbond */:
  159. if (need_wren)
  160. write_enable(nor);
  161. cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  162. status = nor->write_reg(nor, cmd, NULL, 0, 0);
  163. if (need_wren)
  164. write_disable(nor);
  165. return status;
  166. default:
  167. /* Spansion style */
  168. nor->cmd_buf[0] = enable << 7;
  169. return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
  170. }
  171. }
  172. static inline int spi_nor_sr_ready(struct spi_nor *nor)
  173. {
  174. int sr = read_sr(nor);
  175. if (sr < 0)
  176. return sr;
  177. else
  178. return !(sr & SR_WIP);
  179. }
  180. static inline int spi_nor_fsr_ready(struct spi_nor *nor)
  181. {
  182. int fsr = read_fsr(nor);
  183. if (fsr < 0)
  184. return fsr;
  185. else
  186. return fsr & FSR_READY;
  187. }
  188. static int spi_nor_ready(struct spi_nor *nor)
  189. {
  190. int sr, fsr;
  191. sr = spi_nor_sr_ready(nor);
  192. if (sr < 0)
  193. return sr;
  194. fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
  195. if (fsr < 0)
  196. return fsr;
  197. return sr && fsr;
  198. }
  199. /*
  200. * Service routine to read status register until ready, or timeout occurs.
  201. * Returns non-zero if error.
  202. */
  203. static int spi_nor_wait_till_ready(struct spi_nor *nor)
  204. {
  205. unsigned long deadline;
  206. int timeout = 0, ret;
  207. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  208. while (!timeout) {
  209. if (time_after_eq(jiffies, deadline))
  210. timeout = 1;
  211. ret = spi_nor_ready(nor);
  212. if (ret < 0)
  213. return ret;
  214. if (ret)
  215. return 0;
  216. cond_resched();
  217. }
  218. dev_err(nor->dev, "flash operation timed out\n");
  219. return -ETIMEDOUT;
  220. }
  221. /*
  222. * Erase the whole flash memory
  223. *
  224. * Returns 0 if successful, non-zero otherwise.
  225. */
  226. static int erase_chip(struct spi_nor *nor)
  227. {
  228. dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd->size >> 10));
  229. return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0, 0);
  230. }
  231. static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  232. {
  233. int ret = 0;
  234. mutex_lock(&nor->lock);
  235. if (nor->prepare) {
  236. ret = nor->prepare(nor, ops);
  237. if (ret) {
  238. dev_err(nor->dev, "failed in the preparation.\n");
  239. mutex_unlock(&nor->lock);
  240. return ret;
  241. }
  242. }
  243. return ret;
  244. }
  245. static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  246. {
  247. if (nor->unprepare)
  248. nor->unprepare(nor, ops);
  249. mutex_unlock(&nor->lock);
  250. }
  251. /*
  252. * Erase an address range on the nor chip. The address range may extend
  253. * one or more erase sectors. Return an error is there is a problem erasing.
  254. */
  255. static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  256. {
  257. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  258. u32 addr, len;
  259. uint32_t rem;
  260. int ret;
  261. dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  262. (long long)instr->len);
  263. div_u64_rem(instr->len, mtd->erasesize, &rem);
  264. if (rem)
  265. return -EINVAL;
  266. addr = instr->addr;
  267. len = instr->len;
  268. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
  269. if (ret)
  270. return ret;
  271. /* whole-chip erase? */
  272. if (len == mtd->size) {
  273. write_enable(nor);
  274. if (erase_chip(nor)) {
  275. ret = -EIO;
  276. goto erase_err;
  277. }
  278. ret = spi_nor_wait_till_ready(nor);
  279. if (ret)
  280. goto erase_err;
  281. /* REVISIT in some cases we could speed up erasing large regions
  282. * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
  283. * to use "small sector erase", but that's not always optimal.
  284. */
  285. /* "sector"-at-a-time erase */
  286. } else {
  287. while (len) {
  288. write_enable(nor);
  289. if (nor->erase(nor, addr)) {
  290. ret = -EIO;
  291. goto erase_err;
  292. }
  293. addr += mtd->erasesize;
  294. len -= mtd->erasesize;
  295. ret = spi_nor_wait_till_ready(nor);
  296. if (ret)
  297. goto erase_err;
  298. }
  299. }
  300. write_disable(nor);
  301. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  302. instr->state = MTD_ERASE_DONE;
  303. mtd_erase_callback(instr);
  304. return ret;
  305. erase_err:
  306. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  307. instr->state = MTD_ERASE_FAILED;
  308. return ret;
  309. }
  310. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  311. {
  312. struct mtd_info *mtd = nor->mtd;
  313. uint32_t offset = ofs;
  314. uint8_t status_old, status_new;
  315. int ret = 0;
  316. status_old = read_sr(nor);
  317. if (offset < mtd->size - (mtd->size / 2))
  318. status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
  319. else if (offset < mtd->size - (mtd->size / 4))
  320. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  321. else if (offset < mtd->size - (mtd->size / 8))
  322. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  323. else if (offset < mtd->size - (mtd->size / 16))
  324. status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
  325. else if (offset < mtd->size - (mtd->size / 32))
  326. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  327. else if (offset < mtd->size - (mtd->size / 64))
  328. status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
  329. else
  330. status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
  331. /* Only modify protection if it will not unlock other areas */
  332. if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
  333. (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
  334. write_enable(nor);
  335. ret = write_sr(nor, status_new);
  336. }
  337. return ret;
  338. }
  339. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  340. {
  341. struct mtd_info *mtd = nor->mtd;
  342. uint32_t offset = ofs;
  343. uint8_t status_old, status_new;
  344. int ret = 0;
  345. status_old = read_sr(nor);
  346. if (offset+len > mtd->size - (mtd->size / 64))
  347. status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
  348. else if (offset+len > mtd->size - (mtd->size / 32))
  349. status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
  350. else if (offset+len > mtd->size - (mtd->size / 16))
  351. status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
  352. else if (offset+len > mtd->size - (mtd->size / 8))
  353. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  354. else if (offset+len > mtd->size - (mtd->size / 4))
  355. status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
  356. else if (offset+len > mtd->size - (mtd->size / 2))
  357. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  358. else
  359. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  360. /* Only modify protection if it will not lock other areas */
  361. if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
  362. (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
  363. write_enable(nor);
  364. ret = write_sr(nor, status_new);
  365. }
  366. return ret;
  367. }
  368. static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  369. {
  370. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  371. int ret;
  372. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
  373. if (ret)
  374. return ret;
  375. ret = nor->flash_lock(nor, ofs, len);
  376. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
  377. return ret;
  378. }
  379. static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  380. {
  381. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  382. int ret;
  383. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  384. if (ret)
  385. return ret;
  386. ret = nor->flash_unlock(nor, ofs, len);
  387. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  388. return ret;
  389. }
  390. /* Used when the "_ext_id" is two bytes at most */
  391. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  392. ((kernel_ulong_t)&(struct flash_info) { \
  393. .id = { \
  394. ((_jedec_id) >> 16) & 0xff, \
  395. ((_jedec_id) >> 8) & 0xff, \
  396. (_jedec_id) & 0xff, \
  397. ((_ext_id) >> 8) & 0xff, \
  398. (_ext_id) & 0xff, \
  399. }, \
  400. .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
  401. .sector_size = (_sector_size), \
  402. .n_sectors = (_n_sectors), \
  403. .page_size = 256, \
  404. .flags = (_flags), \
  405. })
  406. #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  407. ((kernel_ulong_t)&(struct flash_info) { \
  408. .id = { \
  409. ((_jedec_id) >> 16) & 0xff, \
  410. ((_jedec_id) >> 8) & 0xff, \
  411. (_jedec_id) & 0xff, \
  412. ((_ext_id) >> 16) & 0xff, \
  413. ((_ext_id) >> 8) & 0xff, \
  414. (_ext_id) & 0xff, \
  415. }, \
  416. .id_len = 6, \
  417. .sector_size = (_sector_size), \
  418. .n_sectors = (_n_sectors), \
  419. .page_size = 256, \
  420. .flags = (_flags), \
  421. })
  422. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  423. ((kernel_ulong_t)&(struct flash_info) { \
  424. .sector_size = (_sector_size), \
  425. .n_sectors = (_n_sectors), \
  426. .page_size = (_page_size), \
  427. .addr_width = (_addr_width), \
  428. .flags = (_flags), \
  429. })
  430. /* NOTE: double check command sets and memory organization when you add
  431. * more nor chips. This current list focusses on newer chips, which
  432. * have been converging on command sets which including JEDEC ID.
  433. */
  434. static const struct spi_device_id spi_nor_ids[] = {
  435. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  436. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  437. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  438. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  439. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  440. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  441. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  442. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  443. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  444. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  445. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  446. /* EON -- en25xxx */
  447. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  448. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  449. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  450. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  451. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  452. { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  453. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  454. { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, 0) },
  455. /* ESMT */
  456. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
  457. /* Everspin */
  458. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  459. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  460. /* Fujitsu */
  461. { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
  462. /* GigaDevice */
  463. { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
  464. { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
  465. { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
  466. /* Intel/Numonyx -- xxxs33b */
  467. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  468. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  469. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  470. /* Macronix */
  471. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  472. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  473. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  474. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  475. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  476. { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  477. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  478. { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
  479. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  480. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  481. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  482. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  483. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
  484. { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
  485. /* Micron */
  486. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  487. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SPI_NOR_QUAD_READ) },
  488. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  489. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  490. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  491. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  492. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  493. { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  494. /* PMC */
  495. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  496. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  497. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  498. /* Spansion -- single (large) sector size only, at least
  499. * for the chips listed here (without boot sectors).
  500. */
  501. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  502. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  503. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  504. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  505. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  506. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  507. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  508. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  509. { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  510. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  511. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  512. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  513. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  514. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  515. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  516. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  517. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  518. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  519. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  520. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, 0) },
  521. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  522. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  523. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  524. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  525. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  526. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  527. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  528. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  529. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  530. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  531. { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  532. /* ST Microelectronics -- newer production may have feature updates */
  533. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  534. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  535. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  536. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  537. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  538. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  539. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  540. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  541. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  542. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  543. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  544. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  545. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  546. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  547. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  548. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  549. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  550. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  551. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  552. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  553. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  554. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  555. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  556. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  557. { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
  558. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  559. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  560. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  561. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  562. { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
  563. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  564. { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
  565. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  566. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  567. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  568. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  569. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  570. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  571. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  572. { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  573. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  574. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  575. { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
  576. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  577. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  578. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  579. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  580. /* Catalyst / On Semiconductor -- non-JEDEC */
  581. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  582. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  583. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  584. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  585. { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  586. { },
  587. };
  588. static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
  589. {
  590. int tmp;
  591. u8 id[SPI_NOR_MAX_ID_LEN];
  592. struct flash_info *info;
  593. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  594. if (tmp < 0) {
  595. dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
  596. return ERR_PTR(tmp);
  597. }
  598. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  599. info = (void *)spi_nor_ids[tmp].driver_data;
  600. if (info->id_len) {
  601. if (!memcmp(info->id, id, info->id_len))
  602. return &spi_nor_ids[tmp];
  603. }
  604. }
  605. dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
  606. id[0], id[1], id[2]);
  607. return ERR_PTR(-ENODEV);
  608. }
  609. static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  610. size_t *retlen, u_char *buf)
  611. {
  612. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  613. int ret;
  614. dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  615. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
  616. if (ret)
  617. return ret;
  618. ret = nor->read(nor, from, len, retlen, buf);
  619. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
  620. return ret;
  621. }
  622. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  623. size_t *retlen, const u_char *buf)
  624. {
  625. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  626. size_t actual;
  627. int ret;
  628. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  629. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  630. if (ret)
  631. return ret;
  632. write_enable(nor);
  633. nor->sst_write_second = false;
  634. actual = to % 2;
  635. /* Start write from odd address. */
  636. if (actual) {
  637. nor->program_opcode = SPINOR_OP_BP;
  638. /* write one byte. */
  639. nor->write(nor, to, 1, retlen, buf);
  640. ret = spi_nor_wait_till_ready(nor);
  641. if (ret)
  642. goto time_out;
  643. }
  644. to += actual;
  645. /* Write out most of the data here. */
  646. for (; actual < len - 1; actual += 2) {
  647. nor->program_opcode = SPINOR_OP_AAI_WP;
  648. /* write two bytes. */
  649. nor->write(nor, to, 2, retlen, buf + actual);
  650. ret = spi_nor_wait_till_ready(nor);
  651. if (ret)
  652. goto time_out;
  653. to += 2;
  654. nor->sst_write_second = true;
  655. }
  656. nor->sst_write_second = false;
  657. write_disable(nor);
  658. ret = spi_nor_wait_till_ready(nor);
  659. if (ret)
  660. goto time_out;
  661. /* Write out trailing byte if it exists. */
  662. if (actual != len) {
  663. write_enable(nor);
  664. nor->program_opcode = SPINOR_OP_BP;
  665. nor->write(nor, to, 1, retlen, buf + actual);
  666. ret = spi_nor_wait_till_ready(nor);
  667. if (ret)
  668. goto time_out;
  669. write_disable(nor);
  670. }
  671. time_out:
  672. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  673. return ret;
  674. }
  675. /*
  676. * Write an address range to the nor chip. Data must be written in
  677. * FLASH_PAGESIZE chunks. The address range may be any size provided
  678. * it is within the physical boundaries.
  679. */
  680. static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  681. size_t *retlen, const u_char *buf)
  682. {
  683. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  684. u32 page_offset, page_size, i;
  685. int ret;
  686. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  687. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  688. if (ret)
  689. return ret;
  690. write_enable(nor);
  691. page_offset = to & (nor->page_size - 1);
  692. /* do all the bytes fit onto one page? */
  693. if (page_offset + len <= nor->page_size) {
  694. nor->write(nor, to, len, retlen, buf);
  695. } else {
  696. /* the size of data remaining on the first page */
  697. page_size = nor->page_size - page_offset;
  698. nor->write(nor, to, page_size, retlen, buf);
  699. /* write everything in nor->page_size chunks */
  700. for (i = page_size; i < len; i += page_size) {
  701. page_size = len - i;
  702. if (page_size > nor->page_size)
  703. page_size = nor->page_size;
  704. ret = spi_nor_wait_till_ready(nor);
  705. if (ret)
  706. goto write_err;
  707. write_enable(nor);
  708. nor->write(nor, to + i, page_size, retlen, buf + i);
  709. }
  710. }
  711. ret = spi_nor_wait_till_ready(nor);
  712. write_err:
  713. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  714. return ret;
  715. }
  716. static int macronix_quad_enable(struct spi_nor *nor)
  717. {
  718. int ret, val;
  719. val = read_sr(nor);
  720. write_enable(nor);
  721. nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
  722. nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  723. if (spi_nor_wait_till_ready(nor))
  724. return 1;
  725. ret = read_sr(nor);
  726. if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
  727. dev_err(nor->dev, "Macronix Quad bit not set\n");
  728. return -EINVAL;
  729. }
  730. return 0;
  731. }
  732. /*
  733. * Write status Register and configuration register with 2 bytes
  734. * The first byte will be written to the status register, while the
  735. * second byte will be written to the configuration register.
  736. * Return negative if error occured.
  737. */
  738. static int write_sr_cr(struct spi_nor *nor, u16 val)
  739. {
  740. nor->cmd_buf[0] = val & 0xff;
  741. nor->cmd_buf[1] = (val >> 8);
  742. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2, 0);
  743. }
  744. static int spansion_quad_enable(struct spi_nor *nor)
  745. {
  746. int ret;
  747. int quad_en = CR_QUAD_EN_SPAN << 8;
  748. write_enable(nor);
  749. ret = write_sr_cr(nor, quad_en);
  750. if (ret < 0) {
  751. dev_err(nor->dev,
  752. "error while writing configuration register\n");
  753. return -EINVAL;
  754. }
  755. /* read back and check it */
  756. ret = read_cr(nor);
  757. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  758. dev_err(nor->dev, "Spansion Quad bit not set\n");
  759. return -EINVAL;
  760. }
  761. return 0;
  762. }
  763. static int micron_quad_enable(struct spi_nor *nor)
  764. {
  765. int ret;
  766. u8 val;
  767. ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
  768. if (ret < 0) {
  769. dev_err(nor->dev, "error %d reading EVCR\n", ret);
  770. return ret;
  771. }
  772. write_enable(nor);
  773. /* set EVCR, enable quad I/O */
  774. nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
  775. ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1, 0);
  776. if (ret < 0) {
  777. dev_err(nor->dev, "error while writing EVCR register\n");
  778. return ret;
  779. }
  780. ret = spi_nor_wait_till_ready(nor);
  781. if (ret)
  782. return ret;
  783. /* read EVCR and check it */
  784. ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
  785. if (ret < 0) {
  786. dev_err(nor->dev, "error %d reading EVCR\n", ret);
  787. return ret;
  788. }
  789. if (val & EVCR_QUAD_EN_MICRON) {
  790. dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
  791. return -EINVAL;
  792. }
  793. return 0;
  794. }
  795. static int set_quad_mode(struct spi_nor *nor, struct flash_info *info)
  796. {
  797. int status;
  798. switch (JEDEC_MFR(info)) {
  799. case CFI_MFR_MACRONIX:
  800. status = macronix_quad_enable(nor);
  801. if (status) {
  802. dev_err(nor->dev, "Macronix quad-read not enabled\n");
  803. return -EINVAL;
  804. }
  805. return status;
  806. case CFI_MFR_ST:
  807. status = micron_quad_enable(nor);
  808. if (status) {
  809. dev_err(nor->dev, "Micron quad-read not enabled\n");
  810. return -EINVAL;
  811. }
  812. return status;
  813. default:
  814. status = spansion_quad_enable(nor);
  815. if (status) {
  816. dev_err(nor->dev, "Spansion quad-read not enabled\n");
  817. return -EINVAL;
  818. }
  819. return status;
  820. }
  821. }
  822. static int spi_nor_check(struct spi_nor *nor)
  823. {
  824. if (!nor->dev || !nor->read || !nor->write ||
  825. !nor->read_reg || !nor->write_reg || !nor->erase) {
  826. pr_err("spi-nor: please fill all the necessary fields!\n");
  827. return -EINVAL;
  828. }
  829. return 0;
  830. }
  831. int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  832. {
  833. const struct spi_device_id *id = NULL;
  834. struct flash_info *info;
  835. struct device *dev = nor->dev;
  836. struct mtd_info *mtd = nor->mtd;
  837. struct device_node *np = dev->of_node;
  838. int ret;
  839. int i;
  840. ret = spi_nor_check(nor);
  841. if (ret)
  842. return ret;
  843. /* Try to auto-detect if chip name wasn't specified */
  844. if (!name)
  845. id = spi_nor_read_id(nor);
  846. else
  847. id = spi_nor_match_id(name);
  848. if (IS_ERR_OR_NULL(id))
  849. return -ENOENT;
  850. info = (void *)id->driver_data;
  851. /*
  852. * If caller has specified name of flash model that can normally be
  853. * detected using JEDEC, let's verify it.
  854. */
  855. if (name && info->id_len) {
  856. const struct spi_device_id *jid;
  857. jid = spi_nor_read_id(nor);
  858. if (IS_ERR(jid)) {
  859. return PTR_ERR(jid);
  860. } else if (jid != id) {
  861. /*
  862. * JEDEC knows better, so overwrite platform ID. We
  863. * can't trust partitions any longer, but we'll let
  864. * mtd apply them anyway, since some partitions may be
  865. * marked read-only, and we don't want to lose that
  866. * information, even if it's not 100% accurate.
  867. */
  868. dev_warn(dev, "found %s, expected %s\n",
  869. jid->name, id->name);
  870. id = jid;
  871. info = (void *)jid->driver_data;
  872. }
  873. }
  874. mutex_init(&nor->lock);
  875. /*
  876. * Atmel, SST and Intel/Numonyx serial nor tend to power
  877. * up with the software protection bits set
  878. */
  879. if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
  880. JEDEC_MFR(info) == CFI_MFR_INTEL ||
  881. JEDEC_MFR(info) == CFI_MFR_SST) {
  882. write_enable(nor);
  883. write_sr(nor, 0);
  884. }
  885. if (!mtd->name)
  886. mtd->name = dev_name(dev);
  887. mtd->type = MTD_NORFLASH;
  888. mtd->writesize = 1;
  889. mtd->flags = MTD_CAP_NORFLASH;
  890. mtd->size = info->sector_size * info->n_sectors;
  891. mtd->_erase = spi_nor_erase;
  892. mtd->_read = spi_nor_read;
  893. /* nor protection support for STmicro chips */
  894. if (JEDEC_MFR(info) == CFI_MFR_ST) {
  895. nor->flash_lock = stm_lock;
  896. nor->flash_unlock = stm_unlock;
  897. }
  898. if (nor->flash_lock && nor->flash_unlock) {
  899. mtd->_lock = spi_nor_lock;
  900. mtd->_unlock = spi_nor_unlock;
  901. }
  902. /* sst nor chips use AAI word program */
  903. if (info->flags & SST_WRITE)
  904. mtd->_write = sst_write;
  905. else
  906. mtd->_write = spi_nor_write;
  907. if (info->flags & USE_FSR)
  908. nor->flags |= SNOR_F_USE_FSR;
  909. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  910. /* prefer "small sector" erase if possible */
  911. if (info->flags & SECT_4K) {
  912. nor->erase_opcode = SPINOR_OP_BE_4K;
  913. mtd->erasesize = 4096;
  914. } else if (info->flags & SECT_4K_PMC) {
  915. nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
  916. mtd->erasesize = 4096;
  917. } else
  918. #endif
  919. {
  920. nor->erase_opcode = SPINOR_OP_SE;
  921. mtd->erasesize = info->sector_size;
  922. }
  923. if (info->flags & SPI_NOR_NO_ERASE)
  924. mtd->flags |= MTD_NO_ERASE;
  925. mtd->dev.parent = dev;
  926. nor->page_size = info->page_size;
  927. mtd->writebufsize = nor->page_size;
  928. if (np) {
  929. /* If we were instantiated by DT, use it */
  930. if (of_property_read_bool(np, "m25p,fast-read"))
  931. nor->flash_read = SPI_NOR_FAST;
  932. else
  933. nor->flash_read = SPI_NOR_NORMAL;
  934. } else {
  935. /* If we weren't instantiated by DT, default to fast-read */
  936. nor->flash_read = SPI_NOR_FAST;
  937. }
  938. /* Some devices cannot do fast-read, no matter what DT tells us */
  939. if (info->flags & SPI_NOR_NO_FR)
  940. nor->flash_read = SPI_NOR_NORMAL;
  941. /* Quad/Dual-read mode takes precedence over fast/normal */
  942. if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
  943. ret = set_quad_mode(nor, info);
  944. if (ret) {
  945. dev_err(dev, "quad mode not supported\n");
  946. return ret;
  947. }
  948. nor->flash_read = SPI_NOR_QUAD;
  949. } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
  950. nor->flash_read = SPI_NOR_DUAL;
  951. }
  952. /* Default commands */
  953. switch (nor->flash_read) {
  954. case SPI_NOR_QUAD:
  955. nor->read_opcode = SPINOR_OP_READ_1_1_4;
  956. break;
  957. case SPI_NOR_DUAL:
  958. nor->read_opcode = SPINOR_OP_READ_1_1_2;
  959. break;
  960. case SPI_NOR_FAST:
  961. nor->read_opcode = SPINOR_OP_READ_FAST;
  962. break;
  963. case SPI_NOR_NORMAL:
  964. nor->read_opcode = SPINOR_OP_READ;
  965. break;
  966. default:
  967. dev_err(dev, "No Read opcode defined\n");
  968. return -EINVAL;
  969. }
  970. nor->program_opcode = SPINOR_OP_PP;
  971. if (info->addr_width)
  972. nor->addr_width = info->addr_width;
  973. else if (mtd->size > 0x1000000) {
  974. /* enable 4-byte addressing if the device exceeds 16MiB */
  975. nor->addr_width = 4;
  976. if (JEDEC_MFR(info) == CFI_MFR_AMD) {
  977. /* Dedicated 4-byte command set */
  978. switch (nor->flash_read) {
  979. case SPI_NOR_QUAD:
  980. nor->read_opcode = SPINOR_OP_READ4_1_1_4;
  981. break;
  982. case SPI_NOR_DUAL:
  983. nor->read_opcode = SPINOR_OP_READ4_1_1_2;
  984. break;
  985. case SPI_NOR_FAST:
  986. nor->read_opcode = SPINOR_OP_READ4_FAST;
  987. break;
  988. case SPI_NOR_NORMAL:
  989. nor->read_opcode = SPINOR_OP_READ4;
  990. break;
  991. }
  992. nor->program_opcode = SPINOR_OP_PP_4B;
  993. /* No small sector erase for 4-byte command set */
  994. nor->erase_opcode = SPINOR_OP_SE_4B;
  995. mtd->erasesize = info->sector_size;
  996. } else
  997. set_4byte(nor, info, 1);
  998. } else {
  999. nor->addr_width = 3;
  1000. }
  1001. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  1002. dev_info(dev, "%s (%lld Kbytes)\n", id->name,
  1003. (long long)mtd->size >> 10);
  1004. dev_dbg(dev,
  1005. "mtd .name = %s, .size = 0x%llx (%lldMiB), "
  1006. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  1007. mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
  1008. mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
  1009. if (mtd->numeraseregions)
  1010. for (i = 0; i < mtd->numeraseregions; i++)
  1011. dev_dbg(dev,
  1012. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  1013. ".erasesize = 0x%.8x (%uKiB), "
  1014. ".numblocks = %d }\n",
  1015. i, (long long)mtd->eraseregions[i].offset,
  1016. mtd->eraseregions[i].erasesize,
  1017. mtd->eraseregions[i].erasesize / 1024,
  1018. mtd->eraseregions[i].numblocks);
  1019. return 0;
  1020. }
  1021. EXPORT_SYMBOL_GPL(spi_nor_scan);
  1022. static const struct spi_device_id *spi_nor_match_id(const char *name)
  1023. {
  1024. const struct spi_device_id *id = spi_nor_ids;
  1025. while (id->name[0]) {
  1026. if (!strcmp(name, id->name))
  1027. return id;
  1028. id++;
  1029. }
  1030. return NULL;
  1031. }
  1032. MODULE_LICENSE("GPL");
  1033. MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
  1034. MODULE_AUTHOR("Mike Lavender");
  1035. MODULE_DESCRIPTION("framework for SPI NOR");