spi-nor.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  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/slab.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/spi/flash.h>
  23. #include <linux/mtd/spi-nor.h>
  24. /* Define max times to check status register before we give up. */
  25. /*
  26. * For everything but full-chip erase; probably could be much smaller, but kept
  27. * around for safety for now
  28. */
  29. #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
  30. /*
  31. * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
  32. * for larger flash
  33. */
  34. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  35. #define SPI_NOR_MAX_ID_LEN 6
  36. #define SPI_NOR_MAX_ADDR_WIDTH 4
  37. struct flash_info {
  38. char *name;
  39. /*
  40. * This array stores the ID bytes.
  41. * The first three bytes are the JEDIC ID.
  42. * JEDEC ID zero means "no ID" (mostly older chips).
  43. */
  44. u8 id[SPI_NOR_MAX_ID_LEN];
  45. u8 id_len;
  46. /* The size listed here is what works with SPINOR_OP_SE, which isn't
  47. * necessarily called a "sector" by the vendor.
  48. */
  49. unsigned sector_size;
  50. u16 n_sectors;
  51. u16 page_size;
  52. u16 addr_width;
  53. u16 flags;
  54. #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
  55. #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
  56. #define SST_WRITE BIT(2) /* use SST byte programming */
  57. #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
  58. #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
  59. #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
  60. #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
  61. #define USE_FSR BIT(7) /* use flag status register */
  62. #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
  63. #define SPI_NOR_HAS_TB BIT(9) /*
  64. * Flash SR has Top/Bottom (TB) protect
  65. * bit. Must be used with
  66. * SPI_NOR_HAS_LOCK.
  67. */
  68. #define SPI_S3AN BIT(10) /*
  69. * Xilinx Spartan 3AN In-System Flash
  70. * (MFR cannot be used for probing
  71. * because it has the same value as
  72. * ATMEL flashes)
  73. */
  74. #define SPI_NOR_4B_OPCODES BIT(11) /*
  75. * Use dedicated 4byte address op codes
  76. * to support memory size above 128Mib.
  77. */
  78. #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
  79. #define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
  80. #define USE_CLSR BIT(14) /* use CLSR command */
  81. int (*quad_enable)(struct spi_nor *nor);
  82. };
  83. #define JEDEC_MFR(info) ((info)->id[0])
  84. static const struct flash_info *spi_nor_match_id(const char *name);
  85. /*
  86. * Read the status register, returning its value in the location
  87. * Return the status register value.
  88. * Returns negative if error occurred.
  89. */
  90. static int read_sr(struct spi_nor *nor)
  91. {
  92. int ret;
  93. u8 val;
  94. ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
  95. if (ret < 0) {
  96. pr_err("error %d reading SR\n", (int) ret);
  97. return ret;
  98. }
  99. return val;
  100. }
  101. /*
  102. * Read the flag status register, returning its value in the location
  103. * Return the status register value.
  104. * Returns negative if error occurred.
  105. */
  106. static int read_fsr(struct spi_nor *nor)
  107. {
  108. int ret;
  109. u8 val;
  110. ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
  111. if (ret < 0) {
  112. pr_err("error %d reading FSR\n", ret);
  113. return ret;
  114. }
  115. return val;
  116. }
  117. /*
  118. * Read configuration register, returning its value in the
  119. * location. Return the configuration register value.
  120. * Returns negative if error occurred.
  121. */
  122. static int read_cr(struct spi_nor *nor)
  123. {
  124. int ret;
  125. u8 val;
  126. ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
  127. if (ret < 0) {
  128. dev_err(nor->dev, "error %d reading CR\n", ret);
  129. return ret;
  130. }
  131. return val;
  132. }
  133. /*
  134. * Write status register 1 byte
  135. * Returns negative if error occurred.
  136. */
  137. static inline int write_sr(struct spi_nor *nor, u8 val)
  138. {
  139. nor->cmd_buf[0] = val;
  140. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
  141. }
  142. /*
  143. * Set write enable latch with Write Enable command.
  144. * Returns negative if error occurred.
  145. */
  146. static inline int write_enable(struct spi_nor *nor)
  147. {
  148. return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
  149. }
  150. /*
  151. * Send write disable instruction to the chip.
  152. */
  153. static inline int write_disable(struct spi_nor *nor)
  154. {
  155. return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
  156. }
  157. static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  158. {
  159. return mtd->priv;
  160. }
  161. static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
  162. {
  163. size_t i;
  164. for (i = 0; i < size; i++)
  165. if (table[i][0] == opcode)
  166. return table[i][1];
  167. /* No conversion found, keep input op code. */
  168. return opcode;
  169. }
  170. static inline u8 spi_nor_convert_3to4_read(u8 opcode)
  171. {
  172. static const u8 spi_nor_3to4_read[][2] = {
  173. { SPINOR_OP_READ, SPINOR_OP_READ_4B },
  174. { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
  175. { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
  176. { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
  177. { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
  178. { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
  179. { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
  180. { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
  181. { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
  182. };
  183. return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
  184. ARRAY_SIZE(spi_nor_3to4_read));
  185. }
  186. static inline u8 spi_nor_convert_3to4_program(u8 opcode)
  187. {
  188. static const u8 spi_nor_3to4_program[][2] = {
  189. { SPINOR_OP_PP, SPINOR_OP_PP_4B },
  190. { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
  191. { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
  192. };
  193. return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
  194. ARRAY_SIZE(spi_nor_3to4_program));
  195. }
  196. static inline u8 spi_nor_convert_3to4_erase(u8 opcode)
  197. {
  198. static const u8 spi_nor_3to4_erase[][2] = {
  199. { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
  200. { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
  201. { SPINOR_OP_SE, SPINOR_OP_SE_4B },
  202. };
  203. return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
  204. ARRAY_SIZE(spi_nor_3to4_erase));
  205. }
  206. static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
  207. const struct flash_info *info)
  208. {
  209. /* Do some manufacturer fixups first */
  210. switch (JEDEC_MFR(info)) {
  211. case SNOR_MFR_SPANSION:
  212. /* No small sector erase for 4-byte command set */
  213. nor->erase_opcode = SPINOR_OP_SE;
  214. nor->mtd.erasesize = info->sector_size;
  215. break;
  216. default:
  217. break;
  218. }
  219. nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
  220. nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
  221. nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
  222. }
  223. /* Enable/disable 4-byte addressing mode. */
  224. static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
  225. int enable)
  226. {
  227. int status;
  228. bool need_wren = false;
  229. u8 cmd;
  230. switch (JEDEC_MFR(info)) {
  231. case SNOR_MFR_MICRON:
  232. /* Some Micron need WREN command; all will accept it */
  233. need_wren = true;
  234. case SNOR_MFR_MACRONIX:
  235. case SNOR_MFR_WINBOND:
  236. if (need_wren)
  237. write_enable(nor);
  238. cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  239. status = nor->write_reg(nor, cmd, NULL, 0);
  240. if (need_wren)
  241. write_disable(nor);
  242. return status;
  243. default:
  244. /* Spansion style */
  245. nor->cmd_buf[0] = enable << 7;
  246. return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
  247. }
  248. }
  249. static int s3an_sr_ready(struct spi_nor *nor)
  250. {
  251. int ret;
  252. u8 val;
  253. ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
  254. if (ret < 0) {
  255. dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
  256. return ret;
  257. }
  258. return !!(val & XSR_RDY);
  259. }
  260. static inline int spi_nor_sr_ready(struct spi_nor *nor)
  261. {
  262. int sr = read_sr(nor);
  263. if (sr < 0)
  264. return sr;
  265. if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
  266. if (sr & SR_E_ERR)
  267. dev_err(nor->dev, "Erase Error occurred\n");
  268. else
  269. dev_err(nor->dev, "Programming Error occurred\n");
  270. nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
  271. return -EIO;
  272. }
  273. return !(sr & SR_WIP);
  274. }
  275. static inline int spi_nor_fsr_ready(struct spi_nor *nor)
  276. {
  277. int fsr = read_fsr(nor);
  278. if (fsr < 0)
  279. return fsr;
  280. if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
  281. if (fsr & FSR_E_ERR)
  282. dev_err(nor->dev, "Erase operation failed.\n");
  283. else
  284. dev_err(nor->dev, "Program operation failed.\n");
  285. if (fsr & FSR_PT_ERR)
  286. dev_err(nor->dev,
  287. "Attempted to modify a protected sector.\n");
  288. nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
  289. return -EIO;
  290. }
  291. return fsr & FSR_READY;
  292. }
  293. static int spi_nor_ready(struct spi_nor *nor)
  294. {
  295. int sr, fsr;
  296. if (nor->flags & SNOR_F_READY_XSR_RDY)
  297. sr = s3an_sr_ready(nor);
  298. else
  299. sr = spi_nor_sr_ready(nor);
  300. if (sr < 0)
  301. return sr;
  302. fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
  303. if (fsr < 0)
  304. return fsr;
  305. return sr && fsr;
  306. }
  307. /*
  308. * Service routine to read status register until ready, or timeout occurs.
  309. * Returns non-zero if error.
  310. */
  311. static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
  312. unsigned long timeout_jiffies)
  313. {
  314. unsigned long deadline;
  315. int timeout = 0, ret;
  316. deadline = jiffies + timeout_jiffies;
  317. while (!timeout) {
  318. if (time_after_eq(jiffies, deadline))
  319. timeout = 1;
  320. ret = spi_nor_ready(nor);
  321. if (ret < 0)
  322. return ret;
  323. if (ret)
  324. return 0;
  325. cond_resched();
  326. }
  327. dev_err(nor->dev, "flash operation timed out\n");
  328. return -ETIMEDOUT;
  329. }
  330. static int spi_nor_wait_till_ready(struct spi_nor *nor)
  331. {
  332. return spi_nor_wait_till_ready_with_timeout(nor,
  333. DEFAULT_READY_WAIT_JIFFIES);
  334. }
  335. /*
  336. * Erase the whole flash memory
  337. *
  338. * Returns 0 if successful, non-zero otherwise.
  339. */
  340. static int erase_chip(struct spi_nor *nor)
  341. {
  342. dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
  343. return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
  344. }
  345. static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  346. {
  347. int ret = 0;
  348. mutex_lock(&nor->lock);
  349. if (nor->prepare) {
  350. ret = nor->prepare(nor, ops);
  351. if (ret) {
  352. dev_err(nor->dev, "failed in the preparation.\n");
  353. mutex_unlock(&nor->lock);
  354. return ret;
  355. }
  356. }
  357. return ret;
  358. }
  359. static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  360. {
  361. if (nor->unprepare)
  362. nor->unprepare(nor, ops);
  363. mutex_unlock(&nor->lock);
  364. }
  365. /*
  366. * This code converts an address to the Default Address Mode, that has non
  367. * power of two page sizes. We must support this mode because it is the default
  368. * mode supported by Xilinx tools, it can access the whole flash area and
  369. * changing over to the Power-of-two mode is irreversible and corrupts the
  370. * original data.
  371. * Addr can safely be unsigned int, the biggest S3AN device is smaller than
  372. * 4 MiB.
  373. */
  374. static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
  375. {
  376. unsigned int offset;
  377. unsigned int page;
  378. offset = addr % nor->page_size;
  379. page = addr / nor->page_size;
  380. page <<= (nor->page_size > 512) ? 10 : 9;
  381. return page | offset;
  382. }
  383. /*
  384. * Initiate the erasure of a single sector
  385. */
  386. static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  387. {
  388. u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  389. int i;
  390. if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  391. addr = spi_nor_s3an_addr_convert(nor, addr);
  392. if (nor->erase)
  393. return nor->erase(nor, addr);
  394. /*
  395. * Default implementation, if driver doesn't have a specialized HW
  396. * control
  397. */
  398. for (i = nor->addr_width - 1; i >= 0; i--) {
  399. buf[i] = addr & 0xff;
  400. addr >>= 8;
  401. }
  402. return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  403. }
  404. /*
  405. * Erase an address range on the nor chip. The address range may extend
  406. * one or more erase sectors. Return an error is there is a problem erasing.
  407. */
  408. static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  409. {
  410. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  411. u32 addr, len;
  412. uint32_t rem;
  413. int ret;
  414. dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  415. (long long)instr->len);
  416. div_u64_rem(instr->len, mtd->erasesize, &rem);
  417. if (rem)
  418. return -EINVAL;
  419. addr = instr->addr;
  420. len = instr->len;
  421. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
  422. if (ret)
  423. return ret;
  424. /* whole-chip erase? */
  425. if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
  426. unsigned long timeout;
  427. write_enable(nor);
  428. if (erase_chip(nor)) {
  429. ret = -EIO;
  430. goto erase_err;
  431. }
  432. /*
  433. * Scale the timeout linearly with the size of the flash, with
  434. * a minimum calibrated to an old 2MB flash. We could try to
  435. * pull these from CFI/SFDP, but these values should be good
  436. * enough for now.
  437. */
  438. timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
  439. CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
  440. (unsigned long)(mtd->size / SZ_2M));
  441. ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
  442. if (ret)
  443. goto erase_err;
  444. /* REVISIT in some cases we could speed up erasing large regions
  445. * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
  446. * to use "small sector erase", but that's not always optimal.
  447. */
  448. /* "sector"-at-a-time erase */
  449. } else {
  450. while (len) {
  451. write_enable(nor);
  452. ret = spi_nor_erase_sector(nor, addr);
  453. if (ret)
  454. goto erase_err;
  455. addr += mtd->erasesize;
  456. len -= mtd->erasesize;
  457. ret = spi_nor_wait_till_ready(nor);
  458. if (ret)
  459. goto erase_err;
  460. }
  461. }
  462. write_disable(nor);
  463. erase_err:
  464. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  465. instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
  466. mtd_erase_callback(instr);
  467. return ret;
  468. }
  469. /* Write status register and ensure bits in mask match written values */
  470. static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
  471. {
  472. int ret;
  473. write_enable(nor);
  474. ret = write_sr(nor, status_new);
  475. if (ret)
  476. return ret;
  477. ret = spi_nor_wait_till_ready(nor);
  478. if (ret)
  479. return ret;
  480. ret = read_sr(nor);
  481. if (ret < 0)
  482. return ret;
  483. return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
  484. }
  485. static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
  486. uint64_t *len)
  487. {
  488. struct mtd_info *mtd = &nor->mtd;
  489. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  490. int shift = ffs(mask) - 1;
  491. int pow;
  492. if (!(sr & mask)) {
  493. /* No protection */
  494. *ofs = 0;
  495. *len = 0;
  496. } else {
  497. pow = ((sr & mask) ^ mask) >> shift;
  498. *len = mtd->size >> pow;
  499. if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
  500. *ofs = 0;
  501. else
  502. *ofs = mtd->size - *len;
  503. }
  504. }
  505. /*
  506. * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
  507. * @locked is false); 0 otherwise
  508. */
  509. static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  510. u8 sr, bool locked)
  511. {
  512. loff_t lock_offs;
  513. uint64_t lock_len;
  514. if (!len)
  515. return 1;
  516. stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
  517. if (locked)
  518. /* Requested range is a sub-range of locked range */
  519. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  520. else
  521. /* Requested range does not overlap with locked range */
  522. return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
  523. }
  524. static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  525. u8 sr)
  526. {
  527. return stm_check_lock_status_sr(nor, ofs, len, sr, true);
  528. }
  529. static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  530. u8 sr)
  531. {
  532. return stm_check_lock_status_sr(nor, ofs, len, sr, false);
  533. }
  534. /*
  535. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  536. * Supports the block protection bits BP{0,1,2} in the status register
  537. * (SR). Does not support these features found in newer SR bitfields:
  538. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  539. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  540. *
  541. * Support for the following is provided conditionally for some flash:
  542. * - TB: top/bottom protect
  543. *
  544. * Sample table portion for 8MB flash (Winbond w25q64fw):
  545. *
  546. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  547. * --------------------------------------------------------------------------
  548. * X | X | 0 | 0 | 0 | NONE | NONE
  549. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  550. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  551. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  552. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  553. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  554. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  555. * X | X | 1 | 1 | 1 | 8 MB | ALL
  556. * ------|-------|-------|-------|-------|---------------|-------------------
  557. * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
  558. * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
  559. * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
  560. * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
  561. * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
  562. * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
  563. *
  564. * Returns negative on errors, 0 on success.
  565. */
  566. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  567. {
  568. struct mtd_info *mtd = &nor->mtd;
  569. int status_old, status_new;
  570. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  571. u8 shift = ffs(mask) - 1, pow, val;
  572. loff_t lock_len;
  573. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  574. bool use_top;
  575. status_old = read_sr(nor);
  576. if (status_old < 0)
  577. return status_old;
  578. /* If nothing in our range is unlocked, we don't need to do anything */
  579. if (stm_is_locked_sr(nor, ofs, len, status_old))
  580. return 0;
  581. /* If anything below us is unlocked, we can't use 'bottom' protection */
  582. if (!stm_is_locked_sr(nor, 0, ofs, status_old))
  583. can_be_bottom = false;
  584. /* If anything above us is unlocked, we can't use 'top' protection */
  585. if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
  586. status_old))
  587. can_be_top = false;
  588. if (!can_be_bottom && !can_be_top)
  589. return -EINVAL;
  590. /* Prefer top, if both are valid */
  591. use_top = can_be_top;
  592. /* lock_len: length of region that should end up locked */
  593. if (use_top)
  594. lock_len = mtd->size - ofs;
  595. else
  596. lock_len = ofs + len;
  597. /*
  598. * Need smallest pow such that:
  599. *
  600. * 1 / (2^pow) <= (len / size)
  601. *
  602. * so (assuming power-of-2 size) we do:
  603. *
  604. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  605. */
  606. pow = ilog2(mtd->size) - ilog2(lock_len);
  607. val = mask - (pow << shift);
  608. if (val & ~mask)
  609. return -EINVAL;
  610. /* Don't "lock" with no region! */
  611. if (!(val & mask))
  612. return -EINVAL;
  613. status_new = (status_old & ~mask & ~SR_TB) | val;
  614. /* Disallow further writes if WP pin is asserted */
  615. status_new |= SR_SRWD;
  616. if (!use_top)
  617. status_new |= SR_TB;
  618. /* Don't bother if they're the same */
  619. if (status_new == status_old)
  620. return 0;
  621. /* Only modify protection if it will not unlock other areas */
  622. if ((status_new & mask) < (status_old & mask))
  623. return -EINVAL;
  624. return write_sr_and_check(nor, status_new, mask);
  625. }
  626. /*
  627. * Unlock a region of the flash. See stm_lock() for more info
  628. *
  629. * Returns negative on errors, 0 on success.
  630. */
  631. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  632. {
  633. struct mtd_info *mtd = &nor->mtd;
  634. int status_old, status_new;
  635. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  636. u8 shift = ffs(mask) - 1, pow, val;
  637. loff_t lock_len;
  638. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  639. bool use_top;
  640. status_old = read_sr(nor);
  641. if (status_old < 0)
  642. return status_old;
  643. /* If nothing in our range is locked, we don't need to do anything */
  644. if (stm_is_unlocked_sr(nor, ofs, len, status_old))
  645. return 0;
  646. /* If anything below us is locked, we can't use 'top' protection */
  647. if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
  648. can_be_top = false;
  649. /* If anything above us is locked, we can't use 'bottom' protection */
  650. if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
  651. status_old))
  652. can_be_bottom = false;
  653. if (!can_be_bottom && !can_be_top)
  654. return -EINVAL;
  655. /* Prefer top, if both are valid */
  656. use_top = can_be_top;
  657. /* lock_len: length of region that should remain locked */
  658. if (use_top)
  659. lock_len = mtd->size - (ofs + len);
  660. else
  661. lock_len = ofs;
  662. /*
  663. * Need largest pow such that:
  664. *
  665. * 1 / (2^pow) >= (len / size)
  666. *
  667. * so (assuming power-of-2 size) we do:
  668. *
  669. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  670. */
  671. pow = ilog2(mtd->size) - order_base_2(lock_len);
  672. if (lock_len == 0) {
  673. val = 0; /* fully unlocked */
  674. } else {
  675. val = mask - (pow << shift);
  676. /* Some power-of-two sizes are not supported */
  677. if (val & ~mask)
  678. return -EINVAL;
  679. }
  680. status_new = (status_old & ~mask & ~SR_TB) | val;
  681. /* Don't protect status register if we're fully unlocked */
  682. if (lock_len == 0)
  683. status_new &= ~SR_SRWD;
  684. if (!use_top)
  685. status_new |= SR_TB;
  686. /* Don't bother if they're the same */
  687. if (status_new == status_old)
  688. return 0;
  689. /* Only modify protection if it will not lock other areas */
  690. if ((status_new & mask) > (status_old & mask))
  691. return -EINVAL;
  692. return write_sr_and_check(nor, status_new, mask);
  693. }
  694. /*
  695. * Check if a region of the flash is (completely) locked. See stm_lock() for
  696. * more info.
  697. *
  698. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  699. * negative on errors.
  700. */
  701. static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
  702. {
  703. int status;
  704. status = read_sr(nor);
  705. if (status < 0)
  706. return status;
  707. return stm_is_locked_sr(nor, ofs, len, status);
  708. }
  709. static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  710. {
  711. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  712. int ret;
  713. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
  714. if (ret)
  715. return ret;
  716. ret = nor->flash_lock(nor, ofs, len);
  717. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
  718. return ret;
  719. }
  720. static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  721. {
  722. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  723. int ret;
  724. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  725. if (ret)
  726. return ret;
  727. ret = nor->flash_unlock(nor, ofs, len);
  728. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  729. return ret;
  730. }
  731. static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  732. {
  733. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  734. int ret;
  735. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  736. if (ret)
  737. return ret;
  738. ret = nor->flash_is_locked(nor, ofs, len);
  739. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  740. return ret;
  741. }
  742. static int macronix_quad_enable(struct spi_nor *nor);
  743. /* Used when the "_ext_id" is two bytes at most */
  744. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  745. .id = { \
  746. ((_jedec_id) >> 16) & 0xff, \
  747. ((_jedec_id) >> 8) & 0xff, \
  748. (_jedec_id) & 0xff, \
  749. ((_ext_id) >> 8) & 0xff, \
  750. (_ext_id) & 0xff, \
  751. }, \
  752. .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
  753. .sector_size = (_sector_size), \
  754. .n_sectors = (_n_sectors), \
  755. .page_size = 256, \
  756. .flags = (_flags),
  757. #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  758. .id = { \
  759. ((_jedec_id) >> 16) & 0xff, \
  760. ((_jedec_id) >> 8) & 0xff, \
  761. (_jedec_id) & 0xff, \
  762. ((_ext_id) >> 16) & 0xff, \
  763. ((_ext_id) >> 8) & 0xff, \
  764. (_ext_id) & 0xff, \
  765. }, \
  766. .id_len = 6, \
  767. .sector_size = (_sector_size), \
  768. .n_sectors = (_n_sectors), \
  769. .page_size = 256, \
  770. .flags = (_flags),
  771. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  772. .sector_size = (_sector_size), \
  773. .n_sectors = (_n_sectors), \
  774. .page_size = (_page_size), \
  775. .addr_width = (_addr_width), \
  776. .flags = (_flags),
  777. #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
  778. .id = { \
  779. ((_jedec_id) >> 16) & 0xff, \
  780. ((_jedec_id) >> 8) & 0xff, \
  781. (_jedec_id) & 0xff \
  782. }, \
  783. .id_len = 3, \
  784. .sector_size = (8*_page_size), \
  785. .n_sectors = (_n_sectors), \
  786. .page_size = _page_size, \
  787. .addr_width = 3, \
  788. .flags = SPI_NOR_NO_FR | SPI_S3AN,
  789. /* NOTE: double check command sets and memory organization when you add
  790. * more nor chips. This current list focusses on newer chips, which
  791. * have been converging on command sets which including JEDEC ID.
  792. *
  793. * All newly added entries should describe *hardware* and should use SECT_4K
  794. * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
  795. * scenarios excluding small sectors there is config option that can be
  796. * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
  797. * For historical (and compatibility) reasons (before we got above config) some
  798. * old entries may be missing 4K flag.
  799. */
  800. static const struct flash_info spi_nor_ids[] = {
  801. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  802. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  803. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  804. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  805. { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  806. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  807. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  808. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  809. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  810. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  811. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  812. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  813. /* EON -- en25xxx */
  814. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  815. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  816. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  817. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  818. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  819. { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  820. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  821. { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
  822. /* ESMT */
  823. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
  824. { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
  825. { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_HAS_LOCK) },
  826. /* Everspin */
  827. { "mr25h128", CAT25_INFO( 16 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  828. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  829. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  830. { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  831. /* Fujitsu */
  832. { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
  833. /* GigaDevice */
  834. {
  835. "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
  836. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  837. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  838. },
  839. {
  840. "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
  841. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  842. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  843. },
  844. {
  845. "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
  846. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  847. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  848. },
  849. {
  850. "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
  851. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  852. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  853. },
  854. {
  855. "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
  856. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  857. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  858. },
  859. {
  860. "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
  861. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  862. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  863. },
  864. {
  865. "gd25q256", INFO(0xc84019, 0, 64 * 1024, 512,
  866. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  867. SPI_NOR_4B_OPCODES | SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  868. .quad_enable = macronix_quad_enable,
  869. },
  870. /* Intel/Numonyx -- xxxs33b */
  871. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  872. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  873. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  874. /* ISSI */
  875. { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
  876. { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
  877. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  878. { "is25lp080d", INFO(0x9d6014, 0, 64 * 1024, 16,
  879. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  880. { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
  881. SECT_4K | SPI_NOR_DUAL_READ) },
  882. /* Macronix */
  883. { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
  884. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  885. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  886. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  887. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  888. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
  889. { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  890. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
  891. { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
  892. { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K) },
  893. { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K) },
  894. { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
  895. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  896. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  897. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  898. { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
  899. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  900. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  901. { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  902. { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  903. { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
  904. /* Micron */
  905. { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
  906. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  907. { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  908. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  909. { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  910. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  911. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  912. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  913. { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  914. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  915. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  916. { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
  917. { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
  918. /* PMC */
  919. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  920. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  921. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  922. /* Spansion/Cypress -- single (large) sector size only, at least
  923. * for the chips listed here (without boot sectors).
  924. */
  925. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  926. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  927. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
  928. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  929. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  930. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  931. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  932. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  933. { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  934. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  935. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  936. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  937. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  938. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  939. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  940. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  941. { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  942. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  943. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  944. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  945. { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  946. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  947. { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  948. { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
  949. { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
  950. { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  951. { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  952. { "s25fl256l", INFO(0x016019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  953. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  954. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  955. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  956. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  957. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  958. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  959. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  960. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  961. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  962. { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
  963. { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
  964. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  965. { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  966. { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  967. /* ST Microelectronics -- newer production may have feature updates */
  968. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  969. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  970. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  971. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  972. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  973. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  974. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  975. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  976. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  977. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  978. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  979. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  980. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  981. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  982. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  983. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  984. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  985. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  986. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  987. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  988. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  989. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  990. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  991. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  992. { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
  993. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  994. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  995. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  996. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  997. { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
  998. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  999. { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
  1000. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  1001. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  1002. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  1003. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  1004. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  1005. {
  1006. "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
  1007. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1008. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1009. },
  1010. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  1011. { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
  1012. { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
  1013. { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
  1014. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  1015. {
  1016. "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
  1017. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1018. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1019. },
  1020. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  1021. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  1022. {
  1023. "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
  1024. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1025. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1026. },
  1027. {
  1028. "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
  1029. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1030. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1031. },
  1032. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  1033. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  1034. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  1035. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1036. { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
  1037. SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
  1038. /* Catalyst / On Semiconductor -- non-JEDEC */
  1039. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1040. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1041. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1042. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1043. { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1044. /* Xilinx S3AN Internal Flash */
  1045. { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
  1046. { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
  1047. { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
  1048. { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
  1049. { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
  1050. { },
  1051. };
  1052. static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
  1053. {
  1054. int tmp;
  1055. u8 id[SPI_NOR_MAX_ID_LEN];
  1056. const struct flash_info *info;
  1057. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  1058. if (tmp < 0) {
  1059. dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
  1060. return ERR_PTR(tmp);
  1061. }
  1062. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  1063. info = &spi_nor_ids[tmp];
  1064. if (info->id_len) {
  1065. if (!memcmp(info->id, id, info->id_len))
  1066. return &spi_nor_ids[tmp];
  1067. }
  1068. }
  1069. dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  1070. id[0], id[1], id[2]);
  1071. return ERR_PTR(-ENODEV);
  1072. }
  1073. static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  1074. size_t *retlen, u_char *buf)
  1075. {
  1076. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1077. int ret;
  1078. dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  1079. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
  1080. if (ret)
  1081. return ret;
  1082. while (len) {
  1083. loff_t addr = from;
  1084. if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  1085. addr = spi_nor_s3an_addr_convert(nor, addr);
  1086. ret = nor->read(nor, addr, len, buf);
  1087. if (ret == 0) {
  1088. /* We shouldn't see 0-length reads */
  1089. ret = -EIO;
  1090. goto read_err;
  1091. }
  1092. if (ret < 0)
  1093. goto read_err;
  1094. WARN_ON(ret > len);
  1095. *retlen += ret;
  1096. buf += ret;
  1097. from += ret;
  1098. len -= ret;
  1099. }
  1100. ret = 0;
  1101. read_err:
  1102. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
  1103. return ret;
  1104. }
  1105. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  1106. size_t *retlen, const u_char *buf)
  1107. {
  1108. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1109. size_t actual;
  1110. int ret;
  1111. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1112. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  1113. if (ret)
  1114. return ret;
  1115. write_enable(nor);
  1116. nor->sst_write_second = false;
  1117. actual = to % 2;
  1118. /* Start write from odd address. */
  1119. if (actual) {
  1120. nor->program_opcode = SPINOR_OP_BP;
  1121. /* write one byte. */
  1122. ret = nor->write(nor, to, 1, buf);
  1123. if (ret < 0)
  1124. goto sst_write_err;
  1125. WARN(ret != 1, "While writing 1 byte written %i bytes\n",
  1126. (int)ret);
  1127. ret = spi_nor_wait_till_ready(nor);
  1128. if (ret)
  1129. goto sst_write_err;
  1130. }
  1131. to += actual;
  1132. /* Write out most of the data here. */
  1133. for (; actual < len - 1; actual += 2) {
  1134. nor->program_opcode = SPINOR_OP_AAI_WP;
  1135. /* write two bytes. */
  1136. ret = nor->write(nor, to, 2, buf + actual);
  1137. if (ret < 0)
  1138. goto sst_write_err;
  1139. WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
  1140. (int)ret);
  1141. ret = spi_nor_wait_till_ready(nor);
  1142. if (ret)
  1143. goto sst_write_err;
  1144. to += 2;
  1145. nor->sst_write_second = true;
  1146. }
  1147. nor->sst_write_second = false;
  1148. write_disable(nor);
  1149. ret = spi_nor_wait_till_ready(nor);
  1150. if (ret)
  1151. goto sst_write_err;
  1152. /* Write out trailing byte if it exists. */
  1153. if (actual != len) {
  1154. write_enable(nor);
  1155. nor->program_opcode = SPINOR_OP_BP;
  1156. ret = nor->write(nor, to, 1, buf + actual);
  1157. if (ret < 0)
  1158. goto sst_write_err;
  1159. WARN(ret != 1, "While writing 1 byte written %i bytes\n",
  1160. (int)ret);
  1161. ret = spi_nor_wait_till_ready(nor);
  1162. if (ret)
  1163. goto sst_write_err;
  1164. write_disable(nor);
  1165. actual += 1;
  1166. }
  1167. sst_write_err:
  1168. *retlen += actual;
  1169. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1170. return ret;
  1171. }
  1172. /*
  1173. * Write an address range to the nor chip. Data must be written in
  1174. * FLASH_PAGESIZE chunks. The address range may be any size provided
  1175. * it is within the physical boundaries.
  1176. */
  1177. static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  1178. size_t *retlen, const u_char *buf)
  1179. {
  1180. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1181. size_t page_offset, page_remain, i;
  1182. ssize_t ret;
  1183. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1184. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  1185. if (ret)
  1186. return ret;
  1187. for (i = 0; i < len; ) {
  1188. ssize_t written;
  1189. loff_t addr = to + i;
  1190. /*
  1191. * If page_size is a power of two, the offset can be quickly
  1192. * calculated with an AND operation. On the other cases we
  1193. * need to do a modulus operation (more expensive).
  1194. * Power of two numbers have only one bit set and we can use
  1195. * the instruction hweight32 to detect if we need to do a
  1196. * modulus (do_div()) or not.
  1197. */
  1198. if (hweight32(nor->page_size) == 1) {
  1199. page_offset = addr & (nor->page_size - 1);
  1200. } else {
  1201. uint64_t aux = addr;
  1202. page_offset = do_div(aux, nor->page_size);
  1203. }
  1204. /* the size of data remaining on the first page */
  1205. page_remain = min_t(size_t,
  1206. nor->page_size - page_offset, len - i);
  1207. if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  1208. addr = spi_nor_s3an_addr_convert(nor, addr);
  1209. write_enable(nor);
  1210. ret = nor->write(nor, addr, page_remain, buf + i);
  1211. if (ret < 0)
  1212. goto write_err;
  1213. written = ret;
  1214. ret = spi_nor_wait_till_ready(nor);
  1215. if (ret)
  1216. goto write_err;
  1217. *retlen += written;
  1218. i += written;
  1219. if (written != page_remain) {
  1220. dev_err(nor->dev,
  1221. "While writing %zu bytes written %zd bytes\n",
  1222. page_remain, written);
  1223. ret = -EIO;
  1224. goto write_err;
  1225. }
  1226. }
  1227. write_err:
  1228. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1229. return ret;
  1230. }
  1231. /**
  1232. * macronix_quad_enable() - set QE bit in Status Register.
  1233. * @nor: pointer to a 'struct spi_nor'
  1234. *
  1235. * Set the Quad Enable (QE) bit in the Status Register.
  1236. *
  1237. * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
  1238. *
  1239. * Return: 0 on success, -errno otherwise.
  1240. */
  1241. static int macronix_quad_enable(struct spi_nor *nor)
  1242. {
  1243. int ret, val;
  1244. val = read_sr(nor);
  1245. if (val < 0)
  1246. return val;
  1247. if (val & SR_QUAD_EN_MX)
  1248. return 0;
  1249. write_enable(nor);
  1250. write_sr(nor, val | SR_QUAD_EN_MX);
  1251. ret = spi_nor_wait_till_ready(nor);
  1252. if (ret)
  1253. return ret;
  1254. ret = read_sr(nor);
  1255. if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
  1256. dev_err(nor->dev, "Macronix Quad bit not set\n");
  1257. return -EINVAL;
  1258. }
  1259. return 0;
  1260. }
  1261. /*
  1262. * Write status Register and configuration register with 2 bytes
  1263. * The first byte will be written to the status register, while the
  1264. * second byte will be written to the configuration register.
  1265. * Return negative if error occurred.
  1266. */
  1267. static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
  1268. {
  1269. int ret;
  1270. write_enable(nor);
  1271. ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
  1272. if (ret < 0) {
  1273. dev_err(nor->dev,
  1274. "error while writing configuration register\n");
  1275. return -EINVAL;
  1276. }
  1277. ret = spi_nor_wait_till_ready(nor);
  1278. if (ret) {
  1279. dev_err(nor->dev,
  1280. "timeout while writing configuration register\n");
  1281. return ret;
  1282. }
  1283. return 0;
  1284. }
  1285. /**
  1286. * spansion_quad_enable() - set QE bit in Configuraiton Register.
  1287. * @nor: pointer to a 'struct spi_nor'
  1288. *
  1289. * Set the Quad Enable (QE) bit in the Configuration Register.
  1290. * This function is kept for legacy purpose because it has been used for a
  1291. * long time without anybody complaining but it should be considered as
  1292. * deprecated and maybe buggy.
  1293. * First, this function doesn't care about the previous values of the Status
  1294. * and Configuration Registers when it sets the QE bit (bit 1) in the
  1295. * Configuration Register: all other bits are cleared, which may have unwanted
  1296. * side effects like removing some block protections.
  1297. * Secondly, it uses the Read Configuration Register (35h) instruction though
  1298. * some very old and few memories don't support this instruction. If a pull-up
  1299. * resistor is present on the MISO/IO1 line, we might still be able to pass the
  1300. * "read back" test because the QSPI memory doesn't recognize the command,
  1301. * so leaves the MISO/IO1 line state unchanged, hence read_cr() returns 0xFF.
  1302. *
  1303. * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
  1304. * memories.
  1305. *
  1306. * Return: 0 on success, -errno otherwise.
  1307. */
  1308. static int spansion_quad_enable(struct spi_nor *nor)
  1309. {
  1310. u8 sr_cr[2] = {0, CR_QUAD_EN_SPAN};
  1311. int ret;
  1312. ret = write_sr_cr(nor, sr_cr);
  1313. if (ret)
  1314. return ret;
  1315. /* read back and check it */
  1316. ret = read_cr(nor);
  1317. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  1318. dev_err(nor->dev, "Spansion Quad bit not set\n");
  1319. return -EINVAL;
  1320. }
  1321. return 0;
  1322. }
  1323. /**
  1324. * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
  1325. * @nor: pointer to a 'struct spi_nor'
  1326. *
  1327. * Set the Quad Enable (QE) bit in the Configuration Register.
  1328. * This function should be used with QSPI memories not supporting the Read
  1329. * Configuration Register (35h) instruction.
  1330. *
  1331. * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
  1332. * memories.
  1333. *
  1334. * Return: 0 on success, -errno otherwise.
  1335. */
  1336. static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
  1337. {
  1338. u8 sr_cr[2];
  1339. int ret;
  1340. /* Keep the current value of the Status Register. */
  1341. ret = read_sr(nor);
  1342. if (ret < 0) {
  1343. dev_err(nor->dev, "error while reading status register\n");
  1344. return -EINVAL;
  1345. }
  1346. sr_cr[0] = ret;
  1347. sr_cr[1] = CR_QUAD_EN_SPAN;
  1348. return write_sr_cr(nor, sr_cr);
  1349. }
  1350. /**
  1351. * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
  1352. * @nor: pointer to a 'struct spi_nor'
  1353. *
  1354. * Set the Quad Enable (QE) bit in the Configuration Register.
  1355. * This function should be used with QSPI memories supporting the Read
  1356. * Configuration Register (35h) instruction.
  1357. *
  1358. * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
  1359. * memories.
  1360. *
  1361. * Return: 0 on success, -errno otherwise.
  1362. */
  1363. static int spansion_read_cr_quad_enable(struct spi_nor *nor)
  1364. {
  1365. struct device *dev = nor->dev;
  1366. u8 sr_cr[2];
  1367. int ret;
  1368. /* Check current Quad Enable bit value. */
  1369. ret = read_cr(nor);
  1370. if (ret < 0) {
  1371. dev_err(dev, "error while reading configuration register\n");
  1372. return -EINVAL;
  1373. }
  1374. if (ret & CR_QUAD_EN_SPAN)
  1375. return 0;
  1376. sr_cr[1] = ret | CR_QUAD_EN_SPAN;
  1377. /* Keep the current value of the Status Register. */
  1378. ret = read_sr(nor);
  1379. if (ret < 0) {
  1380. dev_err(dev, "error while reading status register\n");
  1381. return -EINVAL;
  1382. }
  1383. sr_cr[0] = ret;
  1384. ret = write_sr_cr(nor, sr_cr);
  1385. if (ret)
  1386. return ret;
  1387. /* Read back and check it. */
  1388. ret = read_cr(nor);
  1389. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  1390. dev_err(nor->dev, "Spansion Quad bit not set\n");
  1391. return -EINVAL;
  1392. }
  1393. return 0;
  1394. }
  1395. /**
  1396. * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
  1397. * @nor: pointer to a 'struct spi_nor'
  1398. *
  1399. * Set the Quad Enable (QE) bit in the Status Register 2.
  1400. *
  1401. * This is one of the procedures to set the QE bit described in the SFDP
  1402. * (JESD216 rev B) specification but no manufacturer using this procedure has
  1403. * been identified yet, hence the name of the function.
  1404. *
  1405. * Return: 0 on success, -errno otherwise.
  1406. */
  1407. static int sr2_bit7_quad_enable(struct spi_nor *nor)
  1408. {
  1409. u8 sr2;
  1410. int ret;
  1411. /* Check current Quad Enable bit value. */
  1412. ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
  1413. if (ret)
  1414. return ret;
  1415. if (sr2 & SR2_QUAD_EN_BIT7)
  1416. return 0;
  1417. /* Update the Quad Enable bit. */
  1418. sr2 |= SR2_QUAD_EN_BIT7;
  1419. write_enable(nor);
  1420. ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
  1421. if (ret < 0) {
  1422. dev_err(nor->dev, "error while writing status register 2\n");
  1423. return -EINVAL;
  1424. }
  1425. ret = spi_nor_wait_till_ready(nor);
  1426. if (ret < 0) {
  1427. dev_err(nor->dev, "timeout while writing status register 2\n");
  1428. return ret;
  1429. }
  1430. /* Read back and check it. */
  1431. ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
  1432. if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
  1433. dev_err(nor->dev, "SR2 Quad bit not set\n");
  1434. return -EINVAL;
  1435. }
  1436. return 0;
  1437. }
  1438. static int spi_nor_check(struct spi_nor *nor)
  1439. {
  1440. if (!nor->dev || !nor->read || !nor->write ||
  1441. !nor->read_reg || !nor->write_reg) {
  1442. pr_err("spi-nor: please fill all the necessary fields!\n");
  1443. return -EINVAL;
  1444. }
  1445. return 0;
  1446. }
  1447. static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
  1448. {
  1449. int ret;
  1450. u8 val;
  1451. ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
  1452. if (ret < 0) {
  1453. dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
  1454. return ret;
  1455. }
  1456. nor->erase_opcode = SPINOR_OP_XSE;
  1457. nor->program_opcode = SPINOR_OP_XPP;
  1458. nor->read_opcode = SPINOR_OP_READ;
  1459. nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
  1460. /*
  1461. * This flashes have a page size of 264 or 528 bytes (known as
  1462. * Default addressing mode). It can be changed to a more standard
  1463. * Power of two mode where the page size is 256/512. This comes
  1464. * with a price: there is 3% less of space, the data is corrupted
  1465. * and the page size cannot be changed back to default addressing
  1466. * mode.
  1467. *
  1468. * The current addressing mode can be read from the XRDSR register
  1469. * and should not be changed, because is a destructive operation.
  1470. */
  1471. if (val & XSR_PAGESIZE) {
  1472. /* Flash in Power of 2 mode */
  1473. nor->page_size = (nor->page_size == 264) ? 256 : 512;
  1474. nor->mtd.writebufsize = nor->page_size;
  1475. nor->mtd.size = 8 * nor->page_size * info->n_sectors;
  1476. nor->mtd.erasesize = 8 * nor->page_size;
  1477. } else {
  1478. /* Flash in Default addressing mode */
  1479. nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
  1480. }
  1481. return 0;
  1482. }
  1483. struct spi_nor_read_command {
  1484. u8 num_mode_clocks;
  1485. u8 num_wait_states;
  1486. u8 opcode;
  1487. enum spi_nor_protocol proto;
  1488. };
  1489. struct spi_nor_pp_command {
  1490. u8 opcode;
  1491. enum spi_nor_protocol proto;
  1492. };
  1493. enum spi_nor_read_command_index {
  1494. SNOR_CMD_READ,
  1495. SNOR_CMD_READ_FAST,
  1496. SNOR_CMD_READ_1_1_1_DTR,
  1497. /* Dual SPI */
  1498. SNOR_CMD_READ_1_1_2,
  1499. SNOR_CMD_READ_1_2_2,
  1500. SNOR_CMD_READ_2_2_2,
  1501. SNOR_CMD_READ_1_2_2_DTR,
  1502. /* Quad SPI */
  1503. SNOR_CMD_READ_1_1_4,
  1504. SNOR_CMD_READ_1_4_4,
  1505. SNOR_CMD_READ_4_4_4,
  1506. SNOR_CMD_READ_1_4_4_DTR,
  1507. /* Octo SPI */
  1508. SNOR_CMD_READ_1_1_8,
  1509. SNOR_CMD_READ_1_8_8,
  1510. SNOR_CMD_READ_8_8_8,
  1511. SNOR_CMD_READ_1_8_8_DTR,
  1512. SNOR_CMD_READ_MAX
  1513. };
  1514. enum spi_nor_pp_command_index {
  1515. SNOR_CMD_PP,
  1516. /* Quad SPI */
  1517. SNOR_CMD_PP_1_1_4,
  1518. SNOR_CMD_PP_1_4_4,
  1519. SNOR_CMD_PP_4_4_4,
  1520. /* Octo SPI */
  1521. SNOR_CMD_PP_1_1_8,
  1522. SNOR_CMD_PP_1_8_8,
  1523. SNOR_CMD_PP_8_8_8,
  1524. SNOR_CMD_PP_MAX
  1525. };
  1526. struct spi_nor_flash_parameter {
  1527. u64 size;
  1528. u32 page_size;
  1529. struct spi_nor_hwcaps hwcaps;
  1530. struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
  1531. struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
  1532. int (*quad_enable)(struct spi_nor *nor);
  1533. };
  1534. static void
  1535. spi_nor_set_read_settings(struct spi_nor_read_command *read,
  1536. u8 num_mode_clocks,
  1537. u8 num_wait_states,
  1538. u8 opcode,
  1539. enum spi_nor_protocol proto)
  1540. {
  1541. read->num_mode_clocks = num_mode_clocks;
  1542. read->num_wait_states = num_wait_states;
  1543. read->opcode = opcode;
  1544. read->proto = proto;
  1545. }
  1546. static void
  1547. spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
  1548. u8 opcode,
  1549. enum spi_nor_protocol proto)
  1550. {
  1551. pp->opcode = opcode;
  1552. pp->proto = proto;
  1553. }
  1554. /*
  1555. * Serial Flash Discoverable Parameters (SFDP) parsing.
  1556. */
  1557. /**
  1558. * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
  1559. * @nor: pointer to a 'struct spi_nor'
  1560. * @addr: offset in the SFDP area to start reading data from
  1561. * @len: number of bytes to read
  1562. * @buf: buffer where the SFDP data are copied into (dma-safe memory)
  1563. *
  1564. * Whatever the actual numbers of bytes for address and dummy cycles are
  1565. * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
  1566. * followed by a 3-byte address and 8 dummy clock cycles.
  1567. *
  1568. * Return: 0 on success, -errno otherwise.
  1569. */
  1570. static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
  1571. size_t len, void *buf)
  1572. {
  1573. u8 addr_width, read_opcode, read_dummy;
  1574. int ret;
  1575. read_opcode = nor->read_opcode;
  1576. addr_width = nor->addr_width;
  1577. read_dummy = nor->read_dummy;
  1578. nor->read_opcode = SPINOR_OP_RDSFDP;
  1579. nor->addr_width = 3;
  1580. nor->read_dummy = 8;
  1581. while (len) {
  1582. ret = nor->read(nor, addr, len, (u8 *)buf);
  1583. if (!ret || ret > len) {
  1584. ret = -EIO;
  1585. goto read_err;
  1586. }
  1587. if (ret < 0)
  1588. goto read_err;
  1589. buf += ret;
  1590. addr += ret;
  1591. len -= ret;
  1592. }
  1593. ret = 0;
  1594. read_err:
  1595. nor->read_opcode = read_opcode;
  1596. nor->addr_width = addr_width;
  1597. nor->read_dummy = read_dummy;
  1598. return ret;
  1599. }
  1600. /**
  1601. * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
  1602. * @nor: pointer to a 'struct spi_nor'
  1603. * @addr: offset in the SFDP area to start reading data from
  1604. * @len: number of bytes to read
  1605. * @buf: buffer where the SFDP data are copied into
  1606. *
  1607. * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
  1608. * guaranteed to be dma-safe.
  1609. *
  1610. * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
  1611. * otherwise.
  1612. */
  1613. static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
  1614. size_t len, void *buf)
  1615. {
  1616. void *dma_safe_buf;
  1617. int ret;
  1618. dma_safe_buf = kmalloc(len, GFP_KERNEL);
  1619. if (!dma_safe_buf)
  1620. return -ENOMEM;
  1621. ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
  1622. memcpy(buf, dma_safe_buf, len);
  1623. kfree(dma_safe_buf);
  1624. return ret;
  1625. }
  1626. struct sfdp_parameter_header {
  1627. u8 id_lsb;
  1628. u8 minor;
  1629. u8 major;
  1630. u8 length; /* in double words */
  1631. u8 parameter_table_pointer[3]; /* byte address */
  1632. u8 id_msb;
  1633. };
  1634. #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
  1635. #define SFDP_PARAM_HEADER_PTP(p) \
  1636. (((p)->parameter_table_pointer[2] << 16) | \
  1637. ((p)->parameter_table_pointer[1] << 8) | \
  1638. ((p)->parameter_table_pointer[0] << 0))
  1639. #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
  1640. #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
  1641. #define SFDP_SIGNATURE 0x50444653U
  1642. #define SFDP_JESD216_MAJOR 1
  1643. #define SFDP_JESD216_MINOR 0
  1644. #define SFDP_JESD216A_MINOR 5
  1645. #define SFDP_JESD216B_MINOR 6
  1646. struct sfdp_header {
  1647. u32 signature; /* Ox50444653U <=> "SFDP" */
  1648. u8 minor;
  1649. u8 major;
  1650. u8 nph; /* 0-base number of parameter headers */
  1651. u8 unused;
  1652. /* Basic Flash Parameter Table. */
  1653. struct sfdp_parameter_header bfpt_header;
  1654. };
  1655. /* Basic Flash Parameter Table */
  1656. /*
  1657. * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
  1658. * They are indexed from 1 but C arrays are indexed from 0.
  1659. */
  1660. #define BFPT_DWORD(i) ((i) - 1)
  1661. #define BFPT_DWORD_MAX 16
  1662. /* The first version of JESB216 defined only 9 DWORDs. */
  1663. #define BFPT_DWORD_MAX_JESD216 9
  1664. /* 1st DWORD. */
  1665. #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
  1666. #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
  1667. #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
  1668. #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
  1669. #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
  1670. #define BFPT_DWORD1_DTR BIT(19)
  1671. #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
  1672. #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
  1673. #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
  1674. /* 5th DWORD. */
  1675. #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
  1676. #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
  1677. /* 11th DWORD. */
  1678. #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
  1679. #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
  1680. /* 15th DWORD. */
  1681. /*
  1682. * (from JESD216 rev B)
  1683. * Quad Enable Requirements (QER):
  1684. * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
  1685. * reads based on instruction. DQ3/HOLD# functions are hold during
  1686. * instruction phase.
  1687. * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
  1688. * two data bytes where bit 1 of the second byte is one.
  1689. * [...]
  1690. * Writing only one byte to the status register has the side-effect of
  1691. * clearing status register 2, including the QE bit. The 100b code is
  1692. * used if writing one byte to the status register does not modify
  1693. * status register 2.
  1694. * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
  1695. * one data byte where bit 6 is one.
  1696. * [...]
  1697. * - 011b: QE is bit 7 of status register 2. It is set via Write status
  1698. * register 2 instruction 3Eh with one data byte where bit 7 is one.
  1699. * [...]
  1700. * The status register 2 is read using instruction 3Fh.
  1701. * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
  1702. * two data bytes where bit 1 of the second byte is one.
  1703. * [...]
  1704. * In contrast to the 001b code, writing one byte to the status
  1705. * register does not modify status register 2.
  1706. * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
  1707. * Read Status instruction 05h. Status register2 is read using
  1708. * instruction 35h. QE is set via Writ Status instruction 01h with
  1709. * two data bytes where bit 1 of the second byte is one.
  1710. * [...]
  1711. */
  1712. #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
  1713. #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
  1714. #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
  1715. #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
  1716. #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
  1717. #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
  1718. #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
  1719. struct sfdp_bfpt {
  1720. u32 dwords[BFPT_DWORD_MAX];
  1721. };
  1722. /* Fast Read settings. */
  1723. static inline void
  1724. spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
  1725. u16 half,
  1726. enum spi_nor_protocol proto)
  1727. {
  1728. read->num_mode_clocks = (half >> 5) & 0x07;
  1729. read->num_wait_states = (half >> 0) & 0x1f;
  1730. read->opcode = (half >> 8) & 0xff;
  1731. read->proto = proto;
  1732. }
  1733. struct sfdp_bfpt_read {
  1734. /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
  1735. u32 hwcaps;
  1736. /*
  1737. * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
  1738. * whether the Fast Read x-y-z command is supported.
  1739. */
  1740. u32 supported_dword;
  1741. u32 supported_bit;
  1742. /*
  1743. * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
  1744. * encodes the op code, the number of mode clocks and the number of wait
  1745. * states to be used by Fast Read x-y-z command.
  1746. */
  1747. u32 settings_dword;
  1748. u32 settings_shift;
  1749. /* The SPI protocol for this Fast Read x-y-z command. */
  1750. enum spi_nor_protocol proto;
  1751. };
  1752. static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
  1753. /* Fast Read 1-1-2 */
  1754. {
  1755. SNOR_HWCAPS_READ_1_1_2,
  1756. BFPT_DWORD(1), BIT(16), /* Supported bit */
  1757. BFPT_DWORD(4), 0, /* Settings */
  1758. SNOR_PROTO_1_1_2,
  1759. },
  1760. /* Fast Read 1-2-2 */
  1761. {
  1762. SNOR_HWCAPS_READ_1_2_2,
  1763. BFPT_DWORD(1), BIT(20), /* Supported bit */
  1764. BFPT_DWORD(4), 16, /* Settings */
  1765. SNOR_PROTO_1_2_2,
  1766. },
  1767. /* Fast Read 2-2-2 */
  1768. {
  1769. SNOR_HWCAPS_READ_2_2_2,
  1770. BFPT_DWORD(5), BIT(0), /* Supported bit */
  1771. BFPT_DWORD(6), 16, /* Settings */
  1772. SNOR_PROTO_2_2_2,
  1773. },
  1774. /* Fast Read 1-1-4 */
  1775. {
  1776. SNOR_HWCAPS_READ_1_1_4,
  1777. BFPT_DWORD(1), BIT(22), /* Supported bit */
  1778. BFPT_DWORD(3), 16, /* Settings */
  1779. SNOR_PROTO_1_1_4,
  1780. },
  1781. /* Fast Read 1-4-4 */
  1782. {
  1783. SNOR_HWCAPS_READ_1_4_4,
  1784. BFPT_DWORD(1), BIT(21), /* Supported bit */
  1785. BFPT_DWORD(3), 0, /* Settings */
  1786. SNOR_PROTO_1_4_4,
  1787. },
  1788. /* Fast Read 4-4-4 */
  1789. {
  1790. SNOR_HWCAPS_READ_4_4_4,
  1791. BFPT_DWORD(5), BIT(4), /* Supported bit */
  1792. BFPT_DWORD(7), 16, /* Settings */
  1793. SNOR_PROTO_4_4_4,
  1794. },
  1795. };
  1796. struct sfdp_bfpt_erase {
  1797. /*
  1798. * The half-word at offset <shift> in DWORD <dwoard> encodes the
  1799. * op code and erase sector size to be used by Sector Erase commands.
  1800. */
  1801. u32 dword;
  1802. u32 shift;
  1803. };
  1804. static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
  1805. /* Erase Type 1 in DWORD8 bits[15:0] */
  1806. {BFPT_DWORD(8), 0},
  1807. /* Erase Type 2 in DWORD8 bits[31:16] */
  1808. {BFPT_DWORD(8), 16},
  1809. /* Erase Type 3 in DWORD9 bits[15:0] */
  1810. {BFPT_DWORD(9), 0},
  1811. /* Erase Type 4 in DWORD9 bits[31:16] */
  1812. {BFPT_DWORD(9), 16},
  1813. };
  1814. static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
  1815. /**
  1816. * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
  1817. * @nor: pointer to a 'struct spi_nor'
  1818. * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
  1819. * the Basic Flash Parameter Table length and version
  1820. * @params: pointer to the 'struct spi_nor_flash_parameter' to be
  1821. * filled
  1822. *
  1823. * The Basic Flash Parameter Table is the main and only mandatory table as
  1824. * defined by the SFDP (JESD216) specification.
  1825. * It provides us with the total size (memory density) of the data array and
  1826. * the number of address bytes for Fast Read, Page Program and Sector Erase
  1827. * commands.
  1828. * For Fast READ commands, it also gives the number of mode clock cycles and
  1829. * wait states (regrouped in the number of dummy clock cycles) for each
  1830. * supported instruction op code.
  1831. * For Page Program, the page size is now available since JESD216 rev A, however
  1832. * the supported instruction op codes are still not provided.
  1833. * For Sector Erase commands, this table stores the supported instruction op
  1834. * codes and the associated sector sizes.
  1835. * Finally, the Quad Enable Requirements (QER) are also available since JESD216
  1836. * rev A. The QER bits encode the manufacturer dependent procedure to be
  1837. * executed to set the Quad Enable (QE) bit in some internal register of the
  1838. * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
  1839. * sending any Quad SPI command to the memory. Actually, setting the QE bit
  1840. * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
  1841. * and IO3 hence enabling 4 (Quad) I/O lines.
  1842. *
  1843. * Return: 0 on success, -errno otherwise.
  1844. */
  1845. static int spi_nor_parse_bfpt(struct spi_nor *nor,
  1846. const struct sfdp_parameter_header *bfpt_header,
  1847. struct spi_nor_flash_parameter *params)
  1848. {
  1849. struct mtd_info *mtd = &nor->mtd;
  1850. struct sfdp_bfpt bfpt;
  1851. size_t len;
  1852. int i, cmd, err;
  1853. u32 addr;
  1854. u16 half;
  1855. /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
  1856. if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
  1857. return -EINVAL;
  1858. /* Read the Basic Flash Parameter Table. */
  1859. len = min_t(size_t, sizeof(bfpt),
  1860. bfpt_header->length * sizeof(u32));
  1861. addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
  1862. memset(&bfpt, 0, sizeof(bfpt));
  1863. err = spi_nor_read_sfdp_dma_unsafe(nor, addr, len, &bfpt);
  1864. if (err < 0)
  1865. return err;
  1866. /* Fix endianness of the BFPT DWORDs. */
  1867. for (i = 0; i < BFPT_DWORD_MAX; i++)
  1868. bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
  1869. /* Number of address bytes. */
  1870. switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
  1871. case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
  1872. nor->addr_width = 3;
  1873. break;
  1874. case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
  1875. nor->addr_width = 4;
  1876. break;
  1877. default:
  1878. break;
  1879. }
  1880. /* Flash Memory Density (in bits). */
  1881. params->size = bfpt.dwords[BFPT_DWORD(2)];
  1882. if (params->size & BIT(31)) {
  1883. params->size &= ~BIT(31);
  1884. /*
  1885. * Prevent overflows on params->size. Anyway, a NOR of 2^64
  1886. * bits is unlikely to exist so this error probably means
  1887. * the BFPT we are reading is corrupted/wrong.
  1888. */
  1889. if (params->size > 63)
  1890. return -EINVAL;
  1891. params->size = 1ULL << params->size;
  1892. } else {
  1893. params->size++;
  1894. }
  1895. params->size >>= 3; /* Convert to bytes. */
  1896. /* Fast Read settings. */
  1897. for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
  1898. const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
  1899. struct spi_nor_read_command *read;
  1900. if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
  1901. params->hwcaps.mask &= ~rd->hwcaps;
  1902. continue;
  1903. }
  1904. params->hwcaps.mask |= rd->hwcaps;
  1905. cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
  1906. read = &params->reads[cmd];
  1907. half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
  1908. spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
  1909. }
  1910. /* Sector Erase settings. */
  1911. for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
  1912. const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
  1913. u32 erasesize;
  1914. u8 opcode;
  1915. half = bfpt.dwords[er->dword] >> er->shift;
  1916. erasesize = half & 0xff;
  1917. /* erasesize == 0 means this Erase Type is not supported. */
  1918. if (!erasesize)
  1919. continue;
  1920. erasesize = 1U << erasesize;
  1921. opcode = (half >> 8) & 0xff;
  1922. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  1923. if (erasesize == SZ_4K) {
  1924. nor->erase_opcode = opcode;
  1925. mtd->erasesize = erasesize;
  1926. break;
  1927. }
  1928. #endif
  1929. if (!mtd->erasesize || mtd->erasesize < erasesize) {
  1930. nor->erase_opcode = opcode;
  1931. mtd->erasesize = erasesize;
  1932. }
  1933. }
  1934. /* Stop here if not JESD216 rev A or later. */
  1935. if (bfpt_header->length < BFPT_DWORD_MAX)
  1936. return 0;
  1937. /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
  1938. params->page_size = bfpt.dwords[BFPT_DWORD(11)];
  1939. params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
  1940. params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
  1941. params->page_size = 1U << params->page_size;
  1942. /* Quad Enable Requirements. */
  1943. switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
  1944. case BFPT_DWORD15_QER_NONE:
  1945. params->quad_enable = NULL;
  1946. break;
  1947. case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
  1948. case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
  1949. params->quad_enable = spansion_no_read_cr_quad_enable;
  1950. break;
  1951. case BFPT_DWORD15_QER_SR1_BIT6:
  1952. params->quad_enable = macronix_quad_enable;
  1953. break;
  1954. case BFPT_DWORD15_QER_SR2_BIT7:
  1955. params->quad_enable = sr2_bit7_quad_enable;
  1956. break;
  1957. case BFPT_DWORD15_QER_SR2_BIT1:
  1958. params->quad_enable = spansion_read_cr_quad_enable;
  1959. break;
  1960. default:
  1961. return -EINVAL;
  1962. }
  1963. return 0;
  1964. }
  1965. /**
  1966. * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
  1967. * @nor: pointer to a 'struct spi_nor'
  1968. * @params: pointer to the 'struct spi_nor_flash_parameter' to be
  1969. * filled
  1970. *
  1971. * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
  1972. * specification. This is a standard which tends to supported by almost all
  1973. * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
  1974. * runtime the main parameters needed to perform basic SPI flash operations such
  1975. * as Fast Read, Page Program or Sector Erase commands.
  1976. *
  1977. * Return: 0 on success, -errno otherwise.
  1978. */
  1979. static int spi_nor_parse_sfdp(struct spi_nor *nor,
  1980. struct spi_nor_flash_parameter *params)
  1981. {
  1982. const struct sfdp_parameter_header *param_header, *bfpt_header;
  1983. struct sfdp_parameter_header *param_headers = NULL;
  1984. struct sfdp_header header;
  1985. struct device *dev = nor->dev;
  1986. size_t psize;
  1987. int i, err;
  1988. /* Get the SFDP header. */
  1989. err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
  1990. if (err < 0)
  1991. return err;
  1992. /* Check the SFDP header version. */
  1993. if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
  1994. header.major != SFDP_JESD216_MAJOR)
  1995. return -EINVAL;
  1996. /*
  1997. * Verify that the first and only mandatory parameter header is a
  1998. * Basic Flash Parameter Table header as specified in JESD216.
  1999. */
  2000. bfpt_header = &header.bfpt_header;
  2001. if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
  2002. bfpt_header->major != SFDP_JESD216_MAJOR)
  2003. return -EINVAL;
  2004. /*
  2005. * Allocate memory then read all parameter headers with a single
  2006. * Read SFDP command. These parameter headers will actually be parsed
  2007. * twice: a first time to get the latest revision of the basic flash
  2008. * parameter table, then a second time to handle the supported optional
  2009. * tables.
  2010. * Hence we read the parameter headers once for all to reduce the
  2011. * processing time. Also we use kmalloc() instead of devm_kmalloc()
  2012. * because we don't need to keep these parameter headers: the allocated
  2013. * memory is always released with kfree() before exiting this function.
  2014. */
  2015. if (header.nph) {
  2016. psize = header.nph * sizeof(*param_headers);
  2017. param_headers = kmalloc(psize, GFP_KERNEL);
  2018. if (!param_headers)
  2019. return -ENOMEM;
  2020. err = spi_nor_read_sfdp(nor, sizeof(header),
  2021. psize, param_headers);
  2022. if (err < 0) {
  2023. dev_err(dev, "failed to read SFDP parameter headers\n");
  2024. goto exit;
  2025. }
  2026. }
  2027. /*
  2028. * Check other parameter headers to get the latest revision of
  2029. * the basic flash parameter table.
  2030. */
  2031. for (i = 0; i < header.nph; i++) {
  2032. param_header = &param_headers[i];
  2033. if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
  2034. param_header->major == SFDP_JESD216_MAJOR &&
  2035. (param_header->minor > bfpt_header->minor ||
  2036. (param_header->minor == bfpt_header->minor &&
  2037. param_header->length > bfpt_header->length)))
  2038. bfpt_header = param_header;
  2039. }
  2040. err = spi_nor_parse_bfpt(nor, bfpt_header, params);
  2041. if (err)
  2042. goto exit;
  2043. /* Parse other parameter headers. */
  2044. for (i = 0; i < header.nph; i++) {
  2045. param_header = &param_headers[i];
  2046. switch (SFDP_PARAM_HEADER_ID(param_header)) {
  2047. case SFDP_SECTOR_MAP_ID:
  2048. dev_info(dev, "non-uniform erase sector maps are not supported yet.\n");
  2049. break;
  2050. default:
  2051. break;
  2052. }
  2053. if (err)
  2054. goto exit;
  2055. }
  2056. exit:
  2057. kfree(param_headers);
  2058. return err;
  2059. }
  2060. static int spi_nor_init_params(struct spi_nor *nor,
  2061. const struct flash_info *info,
  2062. struct spi_nor_flash_parameter *params)
  2063. {
  2064. /* Set legacy flash parameters as default. */
  2065. memset(params, 0, sizeof(*params));
  2066. /* Set SPI NOR sizes. */
  2067. params->size = info->sector_size * info->n_sectors;
  2068. params->page_size = info->page_size;
  2069. /* (Fast) Read settings. */
  2070. params->hwcaps.mask |= SNOR_HWCAPS_READ;
  2071. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
  2072. 0, 0, SPINOR_OP_READ,
  2073. SNOR_PROTO_1_1_1);
  2074. if (!(info->flags & SPI_NOR_NO_FR)) {
  2075. params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  2076. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
  2077. 0, 8, SPINOR_OP_READ_FAST,
  2078. SNOR_PROTO_1_1_1);
  2079. }
  2080. if (info->flags & SPI_NOR_DUAL_READ) {
  2081. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  2082. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
  2083. 0, 8, SPINOR_OP_READ_1_1_2,
  2084. SNOR_PROTO_1_1_2);
  2085. }
  2086. if (info->flags & SPI_NOR_QUAD_READ) {
  2087. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  2088. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
  2089. 0, 8, SPINOR_OP_READ_1_1_4,
  2090. SNOR_PROTO_1_1_4);
  2091. }
  2092. /* Page Program settings. */
  2093. params->hwcaps.mask |= SNOR_HWCAPS_PP;
  2094. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
  2095. SPINOR_OP_PP, SNOR_PROTO_1_1_1);
  2096. /* Select the procedure to set the Quad Enable bit. */
  2097. if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
  2098. SNOR_HWCAPS_PP_QUAD)) {
  2099. switch (JEDEC_MFR(info)) {
  2100. case SNOR_MFR_MACRONIX:
  2101. params->quad_enable = macronix_quad_enable;
  2102. break;
  2103. case SNOR_MFR_MICRON:
  2104. break;
  2105. default:
  2106. /* Kept only for backward compatibility purpose. */
  2107. params->quad_enable = spansion_quad_enable;
  2108. break;
  2109. }
  2110. /*
  2111. * Some manufacturer like GigaDevice may use different
  2112. * bit to set QE on different memories, so the MFR can't
  2113. * indicate the quad_enable method for this case, we need
  2114. * set it in flash info list.
  2115. */
  2116. if (info->quad_enable)
  2117. params->quad_enable = info->quad_enable;
  2118. }
  2119. /* Override the parameters with data read from SFDP tables. */
  2120. nor->addr_width = 0;
  2121. nor->mtd.erasesize = 0;
  2122. if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
  2123. !(info->flags & SPI_NOR_SKIP_SFDP)) {
  2124. struct spi_nor_flash_parameter sfdp_params;
  2125. memcpy(&sfdp_params, params, sizeof(sfdp_params));
  2126. if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
  2127. nor->addr_width = 0;
  2128. nor->mtd.erasesize = 0;
  2129. } else {
  2130. memcpy(params, &sfdp_params, sizeof(*params));
  2131. }
  2132. }
  2133. return 0;
  2134. }
  2135. static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
  2136. {
  2137. size_t i;
  2138. for (i = 0; i < size; i++)
  2139. if (table[i][0] == (int)hwcaps)
  2140. return table[i][1];
  2141. return -EINVAL;
  2142. }
  2143. static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
  2144. {
  2145. static const int hwcaps_read2cmd[][2] = {
  2146. { SNOR_HWCAPS_READ, SNOR_CMD_READ },
  2147. { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
  2148. { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
  2149. { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
  2150. { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
  2151. { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
  2152. { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
  2153. { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
  2154. { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
  2155. { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
  2156. { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
  2157. { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
  2158. { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
  2159. { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
  2160. { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
  2161. };
  2162. return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
  2163. ARRAY_SIZE(hwcaps_read2cmd));
  2164. }
  2165. static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
  2166. {
  2167. static const int hwcaps_pp2cmd[][2] = {
  2168. { SNOR_HWCAPS_PP, SNOR_CMD_PP },
  2169. { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
  2170. { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
  2171. { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
  2172. { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
  2173. { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
  2174. { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
  2175. };
  2176. return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
  2177. ARRAY_SIZE(hwcaps_pp2cmd));
  2178. }
  2179. static int spi_nor_select_read(struct spi_nor *nor,
  2180. const struct spi_nor_flash_parameter *params,
  2181. u32 shared_hwcaps)
  2182. {
  2183. int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
  2184. const struct spi_nor_read_command *read;
  2185. if (best_match < 0)
  2186. return -EINVAL;
  2187. cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
  2188. if (cmd < 0)
  2189. return -EINVAL;
  2190. read = &params->reads[cmd];
  2191. nor->read_opcode = read->opcode;
  2192. nor->read_proto = read->proto;
  2193. /*
  2194. * In the spi-nor framework, we don't need to make the difference
  2195. * between mode clock cycles and wait state clock cycles.
  2196. * Indeed, the value of the mode clock cycles is used by a QSPI
  2197. * flash memory to know whether it should enter or leave its 0-4-4
  2198. * (Continuous Read / XIP) mode.
  2199. * eXecution In Place is out of the scope of the mtd sub-system.
  2200. * Hence we choose to merge both mode and wait state clock cycles
  2201. * into the so called dummy clock cycles.
  2202. */
  2203. nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
  2204. return 0;
  2205. }
  2206. static int spi_nor_select_pp(struct spi_nor *nor,
  2207. const struct spi_nor_flash_parameter *params,
  2208. u32 shared_hwcaps)
  2209. {
  2210. int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
  2211. const struct spi_nor_pp_command *pp;
  2212. if (best_match < 0)
  2213. return -EINVAL;
  2214. cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
  2215. if (cmd < 0)
  2216. return -EINVAL;
  2217. pp = &params->page_programs[cmd];
  2218. nor->program_opcode = pp->opcode;
  2219. nor->write_proto = pp->proto;
  2220. return 0;
  2221. }
  2222. static int spi_nor_select_erase(struct spi_nor *nor,
  2223. const struct flash_info *info)
  2224. {
  2225. struct mtd_info *mtd = &nor->mtd;
  2226. /* Do nothing if already configured from SFDP. */
  2227. if (mtd->erasesize)
  2228. return 0;
  2229. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  2230. /* prefer "small sector" erase if possible */
  2231. if (info->flags & SECT_4K) {
  2232. nor->erase_opcode = SPINOR_OP_BE_4K;
  2233. mtd->erasesize = 4096;
  2234. } else if (info->flags & SECT_4K_PMC) {
  2235. nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
  2236. mtd->erasesize = 4096;
  2237. } else
  2238. #endif
  2239. {
  2240. nor->erase_opcode = SPINOR_OP_SE;
  2241. mtd->erasesize = info->sector_size;
  2242. }
  2243. return 0;
  2244. }
  2245. static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
  2246. const struct spi_nor_flash_parameter *params,
  2247. const struct spi_nor_hwcaps *hwcaps)
  2248. {
  2249. u32 ignored_mask, shared_mask;
  2250. bool enable_quad_io;
  2251. int err;
  2252. /*
  2253. * Keep only the hardware capabilities supported by both the SPI
  2254. * controller and the SPI flash memory.
  2255. */
  2256. shared_mask = hwcaps->mask & params->hwcaps.mask;
  2257. /* SPI n-n-n protocols are not supported yet. */
  2258. ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
  2259. SNOR_HWCAPS_READ_4_4_4 |
  2260. SNOR_HWCAPS_READ_8_8_8 |
  2261. SNOR_HWCAPS_PP_4_4_4 |
  2262. SNOR_HWCAPS_PP_8_8_8);
  2263. if (shared_mask & ignored_mask) {
  2264. dev_dbg(nor->dev,
  2265. "SPI n-n-n protocols are not supported yet.\n");
  2266. shared_mask &= ~ignored_mask;
  2267. }
  2268. /* Select the (Fast) Read command. */
  2269. err = spi_nor_select_read(nor, params, shared_mask);
  2270. if (err) {
  2271. dev_err(nor->dev,
  2272. "can't select read settings supported by both the SPI controller and memory.\n");
  2273. return err;
  2274. }
  2275. /* Select the Page Program command. */
  2276. err = spi_nor_select_pp(nor, params, shared_mask);
  2277. if (err) {
  2278. dev_err(nor->dev,
  2279. "can't select write settings supported by both the SPI controller and memory.\n");
  2280. return err;
  2281. }
  2282. /* Select the Sector Erase command. */
  2283. err = spi_nor_select_erase(nor, info);
  2284. if (err) {
  2285. dev_err(nor->dev,
  2286. "can't select erase settings supported by both the SPI controller and memory.\n");
  2287. return err;
  2288. }
  2289. /* Enable Quad I/O if needed. */
  2290. enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
  2291. spi_nor_get_protocol_width(nor->write_proto) == 4);
  2292. if (enable_quad_io && params->quad_enable)
  2293. nor->quad_enable = params->quad_enable;
  2294. else
  2295. nor->quad_enable = NULL;
  2296. return 0;
  2297. }
  2298. static int spi_nor_init(struct spi_nor *nor)
  2299. {
  2300. int err;
  2301. /*
  2302. * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
  2303. * with the software protection bits set
  2304. */
  2305. if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
  2306. JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
  2307. JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
  2308. nor->info->flags & SPI_NOR_HAS_LOCK) {
  2309. write_enable(nor);
  2310. write_sr(nor, 0);
  2311. spi_nor_wait_till_ready(nor);
  2312. }
  2313. if (nor->quad_enable) {
  2314. err = nor->quad_enable(nor);
  2315. if (err) {
  2316. dev_err(nor->dev, "quad mode not supported\n");
  2317. return err;
  2318. }
  2319. }
  2320. if ((nor->addr_width == 4) &&
  2321. (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
  2322. !(nor->info->flags & SPI_NOR_4B_OPCODES))
  2323. set_4byte(nor, nor->info, 1);
  2324. return 0;
  2325. }
  2326. /* mtd resume handler */
  2327. static void spi_nor_resume(struct mtd_info *mtd)
  2328. {
  2329. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  2330. struct device *dev = nor->dev;
  2331. int ret;
  2332. /* re-initialize the nor chip */
  2333. ret = spi_nor_init(nor);
  2334. if (ret)
  2335. dev_err(dev, "resume() failed\n");
  2336. }
  2337. void spi_nor_restore(struct spi_nor *nor)
  2338. {
  2339. /* restore the addressing mode */
  2340. if ((nor->addr_width == 4) &&
  2341. (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
  2342. !(nor->info->flags & SPI_NOR_4B_OPCODES))
  2343. set_4byte(nor, nor->info, 0);
  2344. }
  2345. EXPORT_SYMBOL_GPL(spi_nor_restore);
  2346. int spi_nor_scan(struct spi_nor *nor, const char *name,
  2347. const struct spi_nor_hwcaps *hwcaps)
  2348. {
  2349. struct spi_nor_flash_parameter params;
  2350. const struct flash_info *info = NULL;
  2351. struct device *dev = nor->dev;
  2352. struct mtd_info *mtd = &nor->mtd;
  2353. struct device_node *np = spi_nor_get_flash_node(nor);
  2354. int ret;
  2355. int i;
  2356. ret = spi_nor_check(nor);
  2357. if (ret)
  2358. return ret;
  2359. /* Reset SPI protocol for all commands. */
  2360. nor->reg_proto = SNOR_PROTO_1_1_1;
  2361. nor->read_proto = SNOR_PROTO_1_1_1;
  2362. nor->write_proto = SNOR_PROTO_1_1_1;
  2363. if (name)
  2364. info = spi_nor_match_id(name);
  2365. /* Try to auto-detect if chip name wasn't specified or not found */
  2366. if (!info)
  2367. info = spi_nor_read_id(nor);
  2368. if (IS_ERR_OR_NULL(info))
  2369. return -ENOENT;
  2370. /*
  2371. * If caller has specified name of flash model that can normally be
  2372. * detected using JEDEC, let's verify it.
  2373. */
  2374. if (name && info->id_len) {
  2375. const struct flash_info *jinfo;
  2376. jinfo = spi_nor_read_id(nor);
  2377. if (IS_ERR(jinfo)) {
  2378. return PTR_ERR(jinfo);
  2379. } else if (jinfo != info) {
  2380. /*
  2381. * JEDEC knows better, so overwrite platform ID. We
  2382. * can't trust partitions any longer, but we'll let
  2383. * mtd apply them anyway, since some partitions may be
  2384. * marked read-only, and we don't want to lose that
  2385. * information, even if it's not 100% accurate.
  2386. */
  2387. dev_warn(dev, "found %s, expected %s\n",
  2388. jinfo->name, info->name);
  2389. info = jinfo;
  2390. }
  2391. }
  2392. mutex_init(&nor->lock);
  2393. /*
  2394. * Make sure the XSR_RDY flag is set before calling
  2395. * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
  2396. * with Atmel spi-nor
  2397. */
  2398. if (info->flags & SPI_S3AN)
  2399. nor->flags |= SNOR_F_READY_XSR_RDY;
  2400. /* Parse the Serial Flash Discoverable Parameters table. */
  2401. ret = spi_nor_init_params(nor, info, &params);
  2402. if (ret)
  2403. return ret;
  2404. if (!mtd->name)
  2405. mtd->name = dev_name(dev);
  2406. mtd->priv = nor;
  2407. mtd->type = MTD_NORFLASH;
  2408. mtd->writesize = 1;
  2409. mtd->flags = MTD_CAP_NORFLASH;
  2410. mtd->size = params.size;
  2411. mtd->_erase = spi_nor_erase;
  2412. mtd->_read = spi_nor_read;
  2413. mtd->_resume = spi_nor_resume;
  2414. /* NOR protection support for STmicro/Micron chips and similar */
  2415. if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
  2416. info->flags & SPI_NOR_HAS_LOCK) {
  2417. nor->flash_lock = stm_lock;
  2418. nor->flash_unlock = stm_unlock;
  2419. nor->flash_is_locked = stm_is_locked;
  2420. }
  2421. if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
  2422. mtd->_lock = spi_nor_lock;
  2423. mtd->_unlock = spi_nor_unlock;
  2424. mtd->_is_locked = spi_nor_is_locked;
  2425. }
  2426. /* sst nor chips use AAI word program */
  2427. if (info->flags & SST_WRITE)
  2428. mtd->_write = sst_write;
  2429. else
  2430. mtd->_write = spi_nor_write;
  2431. if (info->flags & USE_FSR)
  2432. nor->flags |= SNOR_F_USE_FSR;
  2433. if (info->flags & SPI_NOR_HAS_TB)
  2434. nor->flags |= SNOR_F_HAS_SR_TB;
  2435. if (info->flags & NO_CHIP_ERASE)
  2436. nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
  2437. if (info->flags & USE_CLSR)
  2438. nor->flags |= SNOR_F_USE_CLSR;
  2439. if (info->flags & SPI_NOR_NO_ERASE)
  2440. mtd->flags |= MTD_NO_ERASE;
  2441. mtd->dev.parent = dev;
  2442. nor->page_size = params.page_size;
  2443. mtd->writebufsize = nor->page_size;
  2444. if (np) {
  2445. /* If we were instantiated by DT, use it */
  2446. if (of_property_read_bool(np, "m25p,fast-read"))
  2447. params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  2448. else
  2449. params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
  2450. } else {
  2451. /* If we weren't instantiated by DT, default to fast-read */
  2452. params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  2453. }
  2454. /* Some devices cannot do fast-read, no matter what DT tells us */
  2455. if (info->flags & SPI_NOR_NO_FR)
  2456. params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
  2457. /*
  2458. * Configure the SPI memory:
  2459. * - select op codes for (Fast) Read, Page Program and Sector Erase.
  2460. * - set the number of dummy cycles (mode cycles + wait states).
  2461. * - set the SPI protocols for register and memory accesses.
  2462. * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
  2463. */
  2464. ret = spi_nor_setup(nor, info, &params, hwcaps);
  2465. if (ret)
  2466. return ret;
  2467. if (nor->addr_width) {
  2468. /* already configured from SFDP */
  2469. } else if (info->addr_width) {
  2470. nor->addr_width = info->addr_width;
  2471. } else if (mtd->size > 0x1000000) {
  2472. /* enable 4-byte addressing if the device exceeds 16MiB */
  2473. nor->addr_width = 4;
  2474. if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
  2475. info->flags & SPI_NOR_4B_OPCODES)
  2476. spi_nor_set_4byte_opcodes(nor, info);
  2477. } else {
  2478. nor->addr_width = 3;
  2479. }
  2480. if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  2481. dev_err(dev, "address width is too large: %u\n",
  2482. nor->addr_width);
  2483. return -EINVAL;
  2484. }
  2485. if (info->flags & SPI_S3AN) {
  2486. ret = s3an_nor_scan(info, nor);
  2487. if (ret)
  2488. return ret;
  2489. }
  2490. /* Send all the required SPI flash commands to initialize device */
  2491. nor->info = info;
  2492. ret = spi_nor_init(nor);
  2493. if (ret)
  2494. return ret;
  2495. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  2496. (long long)mtd->size >> 10);
  2497. dev_dbg(dev,
  2498. "mtd .name = %s, .size = 0x%llx (%lldMiB), "
  2499. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  2500. mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
  2501. mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
  2502. if (mtd->numeraseregions)
  2503. for (i = 0; i < mtd->numeraseregions; i++)
  2504. dev_dbg(dev,
  2505. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  2506. ".erasesize = 0x%.8x (%uKiB), "
  2507. ".numblocks = %d }\n",
  2508. i, (long long)mtd->eraseregions[i].offset,
  2509. mtd->eraseregions[i].erasesize,
  2510. mtd->eraseregions[i].erasesize / 1024,
  2511. mtd->eraseregions[i].numblocks);
  2512. return 0;
  2513. }
  2514. EXPORT_SYMBOL_GPL(spi_nor_scan);
  2515. static const struct flash_info *spi_nor_match_id(const char *name)
  2516. {
  2517. const struct flash_info *id = spi_nor_ids;
  2518. while (id->name) {
  2519. if (!strcmp(name, id->name))
  2520. return id;
  2521. id++;
  2522. }
  2523. return NULL;
  2524. }
  2525. MODULE_LICENSE("GPL");
  2526. MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
  2527. MODULE_AUTHOR("Mike Lavender");
  2528. MODULE_DESCRIPTION("framework for SPI NOR");