spi-nor.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. * All newly added entries should describe *hardware* and should use SECT_4K
  435. * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
  436. * scenarios excluding small sectors there is config option that can be
  437. * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
  438. * For historical (and compatibility) reasons (before we got above config) some
  439. * old entries may be missing 4K flag.
  440. */
  441. static const struct spi_device_id spi_nor_ids[] = {
  442. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  443. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  444. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  445. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  446. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  447. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  448. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  449. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  450. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  451. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  452. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  453. /* EON -- en25xxx */
  454. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  455. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  456. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  457. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  458. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  459. { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  460. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  461. { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
  462. /* ESMT */
  463. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
  464. /* Everspin */
  465. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  466. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  467. /* Fujitsu */
  468. { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
  469. /* GigaDevice */
  470. { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
  471. { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
  472. { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
  473. /* Intel/Numonyx -- xxxs33b */
  474. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  475. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  476. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  477. /* ISSI */
  478. { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
  479. /* Macronix */
  480. { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
  481. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  482. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  483. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  484. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  485. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  486. { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  487. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  488. { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
  489. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  490. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  491. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  492. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  493. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
  494. { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
  495. /* Micron */
  496. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  497. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SPI_NOR_QUAD_READ) },
  498. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  499. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  500. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  501. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  502. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  503. { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  504. /* PMC */
  505. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  506. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  507. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  508. /* Spansion -- single (large) sector size only, at least
  509. * for the chips listed here (without boot sectors).
  510. */
  511. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  512. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  513. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  514. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  515. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  516. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  517. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  518. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  519. { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  520. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  521. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  522. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  523. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  524. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  525. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  526. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  527. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  528. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  529. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  530. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  531. { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  532. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  533. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  534. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  535. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  536. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  537. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  538. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  539. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  540. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  541. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  542. { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  543. /* ST Microelectronics -- newer production may have feature updates */
  544. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  545. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  546. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  547. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  548. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  549. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  550. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  551. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  552. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  553. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  554. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  555. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  556. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  557. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  558. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  559. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  560. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  561. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  562. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  563. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  564. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  565. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  566. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  567. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  568. { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
  569. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  570. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  571. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  572. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  573. { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
  574. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  575. { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
  576. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  577. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  578. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  579. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  580. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  581. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  582. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  583. { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  584. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  585. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  586. { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
  587. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  588. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  589. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  590. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  591. /* Catalyst / On Semiconductor -- non-JEDEC */
  592. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  593. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  594. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  595. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  596. { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  597. { },
  598. };
  599. static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
  600. {
  601. int tmp;
  602. u8 id[SPI_NOR_MAX_ID_LEN];
  603. struct flash_info *info;
  604. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  605. if (tmp < 0) {
  606. dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
  607. return ERR_PTR(tmp);
  608. }
  609. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  610. info = (void *)spi_nor_ids[tmp].driver_data;
  611. if (info->id_len) {
  612. if (!memcmp(info->id, id, info->id_len))
  613. return &spi_nor_ids[tmp];
  614. }
  615. }
  616. dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
  617. id[0], id[1], id[2]);
  618. return ERR_PTR(-ENODEV);
  619. }
  620. static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  621. size_t *retlen, u_char *buf)
  622. {
  623. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  624. int ret;
  625. dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  626. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
  627. if (ret)
  628. return ret;
  629. ret = nor->read(nor, from, len, retlen, buf);
  630. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
  631. return ret;
  632. }
  633. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  634. size_t *retlen, const u_char *buf)
  635. {
  636. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  637. size_t actual;
  638. int ret;
  639. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  640. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  641. if (ret)
  642. return ret;
  643. write_enable(nor);
  644. nor->sst_write_second = false;
  645. actual = to % 2;
  646. /* Start write from odd address. */
  647. if (actual) {
  648. nor->program_opcode = SPINOR_OP_BP;
  649. /* write one byte. */
  650. nor->write(nor, to, 1, retlen, buf);
  651. ret = spi_nor_wait_till_ready(nor);
  652. if (ret)
  653. goto time_out;
  654. }
  655. to += actual;
  656. /* Write out most of the data here. */
  657. for (; actual < len - 1; actual += 2) {
  658. nor->program_opcode = SPINOR_OP_AAI_WP;
  659. /* write two bytes. */
  660. nor->write(nor, to, 2, retlen, buf + actual);
  661. ret = spi_nor_wait_till_ready(nor);
  662. if (ret)
  663. goto time_out;
  664. to += 2;
  665. nor->sst_write_second = true;
  666. }
  667. nor->sst_write_second = false;
  668. write_disable(nor);
  669. ret = spi_nor_wait_till_ready(nor);
  670. if (ret)
  671. goto time_out;
  672. /* Write out trailing byte if it exists. */
  673. if (actual != len) {
  674. write_enable(nor);
  675. nor->program_opcode = SPINOR_OP_BP;
  676. nor->write(nor, to, 1, retlen, buf + actual);
  677. ret = spi_nor_wait_till_ready(nor);
  678. if (ret)
  679. goto time_out;
  680. write_disable(nor);
  681. }
  682. time_out:
  683. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  684. return ret;
  685. }
  686. /*
  687. * Write an address range to the nor chip. Data must be written in
  688. * FLASH_PAGESIZE chunks. The address range may be any size provided
  689. * it is within the physical boundaries.
  690. */
  691. static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  692. size_t *retlen, const u_char *buf)
  693. {
  694. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  695. u32 page_offset, page_size, i;
  696. int ret;
  697. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  698. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  699. if (ret)
  700. return ret;
  701. write_enable(nor);
  702. page_offset = to & (nor->page_size - 1);
  703. /* do all the bytes fit onto one page? */
  704. if (page_offset + len <= nor->page_size) {
  705. nor->write(nor, to, len, retlen, buf);
  706. } else {
  707. /* the size of data remaining on the first page */
  708. page_size = nor->page_size - page_offset;
  709. nor->write(nor, to, page_size, retlen, buf);
  710. /* write everything in nor->page_size chunks */
  711. for (i = page_size; i < len; i += page_size) {
  712. page_size = len - i;
  713. if (page_size > nor->page_size)
  714. page_size = nor->page_size;
  715. ret = spi_nor_wait_till_ready(nor);
  716. if (ret)
  717. goto write_err;
  718. write_enable(nor);
  719. nor->write(nor, to + i, page_size, retlen, buf + i);
  720. }
  721. }
  722. ret = spi_nor_wait_till_ready(nor);
  723. write_err:
  724. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  725. return ret;
  726. }
  727. static int macronix_quad_enable(struct spi_nor *nor)
  728. {
  729. int ret, val;
  730. val = read_sr(nor);
  731. write_enable(nor);
  732. nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
  733. nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  734. if (spi_nor_wait_till_ready(nor))
  735. return 1;
  736. ret = read_sr(nor);
  737. if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
  738. dev_err(nor->dev, "Macronix Quad bit not set\n");
  739. return -EINVAL;
  740. }
  741. return 0;
  742. }
  743. /*
  744. * Write status Register and configuration register with 2 bytes
  745. * The first byte will be written to the status register, while the
  746. * second byte will be written to the configuration register.
  747. * Return negative if error occured.
  748. */
  749. static int write_sr_cr(struct spi_nor *nor, u16 val)
  750. {
  751. nor->cmd_buf[0] = val & 0xff;
  752. nor->cmd_buf[1] = (val >> 8);
  753. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2, 0);
  754. }
  755. static int spansion_quad_enable(struct spi_nor *nor)
  756. {
  757. int ret;
  758. int quad_en = CR_QUAD_EN_SPAN << 8;
  759. write_enable(nor);
  760. ret = write_sr_cr(nor, quad_en);
  761. if (ret < 0) {
  762. dev_err(nor->dev,
  763. "error while writing configuration register\n");
  764. return -EINVAL;
  765. }
  766. /* read back and check it */
  767. ret = read_cr(nor);
  768. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  769. dev_err(nor->dev, "Spansion Quad bit not set\n");
  770. return -EINVAL;
  771. }
  772. return 0;
  773. }
  774. static int micron_quad_enable(struct spi_nor *nor)
  775. {
  776. int ret;
  777. u8 val;
  778. ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
  779. if (ret < 0) {
  780. dev_err(nor->dev, "error %d reading EVCR\n", ret);
  781. return ret;
  782. }
  783. write_enable(nor);
  784. /* set EVCR, enable quad I/O */
  785. nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
  786. ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1, 0);
  787. if (ret < 0) {
  788. dev_err(nor->dev, "error while writing EVCR register\n");
  789. return ret;
  790. }
  791. ret = spi_nor_wait_till_ready(nor);
  792. if (ret)
  793. return ret;
  794. /* read EVCR and check it */
  795. ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
  796. if (ret < 0) {
  797. dev_err(nor->dev, "error %d reading EVCR\n", ret);
  798. return ret;
  799. }
  800. if (val & EVCR_QUAD_EN_MICRON) {
  801. dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
  802. return -EINVAL;
  803. }
  804. return 0;
  805. }
  806. static int set_quad_mode(struct spi_nor *nor, struct flash_info *info)
  807. {
  808. int status;
  809. switch (JEDEC_MFR(info)) {
  810. case CFI_MFR_MACRONIX:
  811. status = macronix_quad_enable(nor);
  812. if (status) {
  813. dev_err(nor->dev, "Macronix quad-read not enabled\n");
  814. return -EINVAL;
  815. }
  816. return status;
  817. case CFI_MFR_ST:
  818. status = micron_quad_enable(nor);
  819. if (status) {
  820. dev_err(nor->dev, "Micron quad-read not enabled\n");
  821. return -EINVAL;
  822. }
  823. return status;
  824. default:
  825. status = spansion_quad_enable(nor);
  826. if (status) {
  827. dev_err(nor->dev, "Spansion quad-read not enabled\n");
  828. return -EINVAL;
  829. }
  830. return status;
  831. }
  832. }
  833. static int spi_nor_check(struct spi_nor *nor)
  834. {
  835. if (!nor->dev || !nor->read || !nor->write ||
  836. !nor->read_reg || !nor->write_reg || !nor->erase) {
  837. pr_err("spi-nor: please fill all the necessary fields!\n");
  838. return -EINVAL;
  839. }
  840. return 0;
  841. }
  842. int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  843. {
  844. const struct spi_device_id *id = NULL;
  845. struct flash_info *info;
  846. struct device *dev = nor->dev;
  847. struct mtd_info *mtd = nor->mtd;
  848. struct device_node *np = dev->of_node;
  849. int ret;
  850. int i;
  851. ret = spi_nor_check(nor);
  852. if (ret)
  853. return ret;
  854. /* Try to auto-detect if chip name wasn't specified */
  855. if (!name)
  856. id = spi_nor_read_id(nor);
  857. else
  858. id = spi_nor_match_id(name);
  859. if (IS_ERR_OR_NULL(id))
  860. return -ENOENT;
  861. info = (void *)id->driver_data;
  862. /*
  863. * If caller has specified name of flash model that can normally be
  864. * detected using JEDEC, let's verify it.
  865. */
  866. if (name && info->id_len) {
  867. const struct spi_device_id *jid;
  868. jid = spi_nor_read_id(nor);
  869. if (IS_ERR(jid)) {
  870. return PTR_ERR(jid);
  871. } else if (jid != id) {
  872. /*
  873. * JEDEC knows better, so overwrite platform ID. We
  874. * can't trust partitions any longer, but we'll let
  875. * mtd apply them anyway, since some partitions may be
  876. * marked read-only, and we don't want to lose that
  877. * information, even if it's not 100% accurate.
  878. */
  879. dev_warn(dev, "found %s, expected %s\n",
  880. jid->name, id->name);
  881. id = jid;
  882. info = (void *)jid->driver_data;
  883. }
  884. }
  885. mutex_init(&nor->lock);
  886. /*
  887. * Atmel, SST and Intel/Numonyx serial nor tend to power
  888. * up with the software protection bits set
  889. */
  890. if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
  891. JEDEC_MFR(info) == CFI_MFR_INTEL ||
  892. JEDEC_MFR(info) == CFI_MFR_SST) {
  893. write_enable(nor);
  894. write_sr(nor, 0);
  895. }
  896. if (!mtd->name)
  897. mtd->name = dev_name(dev);
  898. mtd->type = MTD_NORFLASH;
  899. mtd->writesize = 1;
  900. mtd->flags = MTD_CAP_NORFLASH;
  901. mtd->size = info->sector_size * info->n_sectors;
  902. mtd->_erase = spi_nor_erase;
  903. mtd->_read = spi_nor_read;
  904. /* nor protection support for STmicro chips */
  905. if (JEDEC_MFR(info) == CFI_MFR_ST) {
  906. nor->flash_lock = stm_lock;
  907. nor->flash_unlock = stm_unlock;
  908. }
  909. if (nor->flash_lock && nor->flash_unlock) {
  910. mtd->_lock = spi_nor_lock;
  911. mtd->_unlock = spi_nor_unlock;
  912. }
  913. /* sst nor chips use AAI word program */
  914. if (info->flags & SST_WRITE)
  915. mtd->_write = sst_write;
  916. else
  917. mtd->_write = spi_nor_write;
  918. if (info->flags & USE_FSR)
  919. nor->flags |= SNOR_F_USE_FSR;
  920. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  921. /* prefer "small sector" erase if possible */
  922. if (info->flags & SECT_4K) {
  923. nor->erase_opcode = SPINOR_OP_BE_4K;
  924. mtd->erasesize = 4096;
  925. } else if (info->flags & SECT_4K_PMC) {
  926. nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
  927. mtd->erasesize = 4096;
  928. } else
  929. #endif
  930. {
  931. nor->erase_opcode = SPINOR_OP_SE;
  932. mtd->erasesize = info->sector_size;
  933. }
  934. if (info->flags & SPI_NOR_NO_ERASE)
  935. mtd->flags |= MTD_NO_ERASE;
  936. mtd->dev.parent = dev;
  937. nor->page_size = info->page_size;
  938. mtd->writebufsize = nor->page_size;
  939. if (np) {
  940. /* If we were instantiated by DT, use it */
  941. if (of_property_read_bool(np, "m25p,fast-read"))
  942. nor->flash_read = SPI_NOR_FAST;
  943. else
  944. nor->flash_read = SPI_NOR_NORMAL;
  945. } else {
  946. /* If we weren't instantiated by DT, default to fast-read */
  947. nor->flash_read = SPI_NOR_FAST;
  948. }
  949. /* Some devices cannot do fast-read, no matter what DT tells us */
  950. if (info->flags & SPI_NOR_NO_FR)
  951. nor->flash_read = SPI_NOR_NORMAL;
  952. /* Quad/Dual-read mode takes precedence over fast/normal */
  953. if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
  954. ret = set_quad_mode(nor, info);
  955. if (ret) {
  956. dev_err(dev, "quad mode not supported\n");
  957. return ret;
  958. }
  959. nor->flash_read = SPI_NOR_QUAD;
  960. } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
  961. nor->flash_read = SPI_NOR_DUAL;
  962. }
  963. /* Default commands */
  964. switch (nor->flash_read) {
  965. case SPI_NOR_QUAD:
  966. nor->read_opcode = SPINOR_OP_READ_1_1_4;
  967. break;
  968. case SPI_NOR_DUAL:
  969. nor->read_opcode = SPINOR_OP_READ_1_1_2;
  970. break;
  971. case SPI_NOR_FAST:
  972. nor->read_opcode = SPINOR_OP_READ_FAST;
  973. break;
  974. case SPI_NOR_NORMAL:
  975. nor->read_opcode = SPINOR_OP_READ;
  976. break;
  977. default:
  978. dev_err(dev, "No Read opcode defined\n");
  979. return -EINVAL;
  980. }
  981. nor->program_opcode = SPINOR_OP_PP;
  982. if (info->addr_width)
  983. nor->addr_width = info->addr_width;
  984. else if (mtd->size > 0x1000000) {
  985. /* enable 4-byte addressing if the device exceeds 16MiB */
  986. nor->addr_width = 4;
  987. if (JEDEC_MFR(info) == CFI_MFR_AMD) {
  988. /* Dedicated 4-byte command set */
  989. switch (nor->flash_read) {
  990. case SPI_NOR_QUAD:
  991. nor->read_opcode = SPINOR_OP_READ4_1_1_4;
  992. break;
  993. case SPI_NOR_DUAL:
  994. nor->read_opcode = SPINOR_OP_READ4_1_1_2;
  995. break;
  996. case SPI_NOR_FAST:
  997. nor->read_opcode = SPINOR_OP_READ4_FAST;
  998. break;
  999. case SPI_NOR_NORMAL:
  1000. nor->read_opcode = SPINOR_OP_READ4;
  1001. break;
  1002. }
  1003. nor->program_opcode = SPINOR_OP_PP_4B;
  1004. /* No small sector erase for 4-byte command set */
  1005. nor->erase_opcode = SPINOR_OP_SE_4B;
  1006. mtd->erasesize = info->sector_size;
  1007. } else
  1008. set_4byte(nor, info, 1);
  1009. } else {
  1010. nor->addr_width = 3;
  1011. }
  1012. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  1013. dev_info(dev, "%s (%lld Kbytes)\n", id->name,
  1014. (long long)mtd->size >> 10);
  1015. dev_dbg(dev,
  1016. "mtd .name = %s, .size = 0x%llx (%lldMiB), "
  1017. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  1018. mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
  1019. mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
  1020. if (mtd->numeraseregions)
  1021. for (i = 0; i < mtd->numeraseregions; i++)
  1022. dev_dbg(dev,
  1023. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  1024. ".erasesize = 0x%.8x (%uKiB), "
  1025. ".numblocks = %d }\n",
  1026. i, (long long)mtd->eraseregions[i].offset,
  1027. mtd->eraseregions[i].erasesize,
  1028. mtd->eraseregions[i].erasesize / 1024,
  1029. mtd->eraseregions[i].numblocks);
  1030. return 0;
  1031. }
  1032. EXPORT_SYMBOL_GPL(spi_nor_scan);
  1033. static const struct spi_device_id *spi_nor_match_id(const char *name)
  1034. {
  1035. const struct spi_device_id *id = spi_nor_ids;
  1036. while (id->name[0]) {
  1037. if (!strcmp(name, id->name))
  1038. return id;
  1039. id++;
  1040. }
  1041. return NULL;
  1042. }
  1043. MODULE_LICENSE("GPL");
  1044. MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
  1045. MODULE_AUTHOR("Mike Lavender");
  1046. MODULE_DESCRIPTION("framework for SPI NOR");