spi-nor.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903
  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/sort.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/spi/flash.h>
  24. #include <linux/mtd/spi-nor.h>
  25. /* Define max times to check status register before we give up. */
  26. /*
  27. * For everything but full-chip erase; probably could be much smaller, but kept
  28. * around for safety for now
  29. */
  30. #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
  31. /*
  32. * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
  33. * for larger flash
  34. */
  35. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  36. #define SPI_NOR_MAX_ID_LEN 6
  37. #define SPI_NOR_MAX_ADDR_WIDTH 4
  38. struct flash_info {
  39. char *name;
  40. /*
  41. * This array stores the ID bytes.
  42. * The first three bytes are the JEDIC ID.
  43. * JEDEC ID zero means "no ID" (mostly older chips).
  44. */
  45. u8 id[SPI_NOR_MAX_ID_LEN];
  46. u8 id_len;
  47. /* The size listed here is what works with SPINOR_OP_SE, which isn't
  48. * necessarily called a "sector" by the vendor.
  49. */
  50. unsigned sector_size;
  51. u16 n_sectors;
  52. u16 page_size;
  53. u16 addr_width;
  54. u16 flags;
  55. #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
  56. #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
  57. #define SST_WRITE BIT(2) /* use SST byte programming */
  58. #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
  59. #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
  60. #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
  61. #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
  62. #define USE_FSR BIT(7) /* use flag status register */
  63. #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
  64. #define SPI_NOR_HAS_TB BIT(9) /*
  65. * Flash SR has Top/Bottom (TB) protect
  66. * bit. Must be used with
  67. * SPI_NOR_HAS_LOCK.
  68. */
  69. #define SPI_S3AN BIT(10) /*
  70. * Xilinx Spartan 3AN In-System Flash
  71. * (MFR cannot be used for probing
  72. * because it has the same value as
  73. * ATMEL flashes)
  74. */
  75. #define SPI_NOR_4B_OPCODES BIT(11) /*
  76. * Use dedicated 4byte address op codes
  77. * to support memory size above 128Mib.
  78. */
  79. #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
  80. #define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
  81. #define USE_CLSR BIT(14) /* use CLSR command */
  82. int (*quad_enable)(struct spi_nor *nor);
  83. };
  84. #define JEDEC_MFR(info) ((info)->id[0])
  85. static const struct flash_info *spi_nor_match_id(const char *name);
  86. /*
  87. * Read the status register, returning its value in the location
  88. * Return the status register value.
  89. * Returns negative if error occurred.
  90. */
  91. static int read_sr(struct spi_nor *nor)
  92. {
  93. int ret;
  94. u8 val;
  95. ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
  96. if (ret < 0) {
  97. pr_err("error %d reading SR\n", (int) ret);
  98. return ret;
  99. }
  100. return val;
  101. }
  102. /*
  103. * Read the flag status register, returning its value in the location
  104. * Return the status register value.
  105. * Returns negative if error occurred.
  106. */
  107. static int read_fsr(struct spi_nor *nor)
  108. {
  109. int ret;
  110. u8 val;
  111. ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
  112. if (ret < 0) {
  113. pr_err("error %d reading FSR\n", ret);
  114. return ret;
  115. }
  116. return val;
  117. }
  118. /*
  119. * Read configuration register, returning its value in the
  120. * location. Return the configuration register value.
  121. * Returns negative if error occurred.
  122. */
  123. static int read_cr(struct spi_nor *nor)
  124. {
  125. int ret;
  126. u8 val;
  127. ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
  128. if (ret < 0) {
  129. dev_err(nor->dev, "error %d reading CR\n", ret);
  130. return ret;
  131. }
  132. return val;
  133. }
  134. /*
  135. * Write status register 1 byte
  136. * Returns negative if error occurred.
  137. */
  138. static inline int write_sr(struct spi_nor *nor, u8 val)
  139. {
  140. nor->cmd_buf[0] = val;
  141. return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
  142. }
  143. /*
  144. * Set write enable latch with Write Enable command.
  145. * Returns negative if error occurred.
  146. */
  147. static inline int write_enable(struct spi_nor *nor)
  148. {
  149. return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
  150. }
  151. /*
  152. * Send write disable instruction to the chip.
  153. */
  154. static inline int write_disable(struct spi_nor *nor)
  155. {
  156. return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
  157. }
  158. static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  159. {
  160. return mtd->priv;
  161. }
  162. static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
  163. {
  164. size_t i;
  165. for (i = 0; i < size; i++)
  166. if (table[i][0] == opcode)
  167. return table[i][1];
  168. /* No conversion found, keep input op code. */
  169. return opcode;
  170. }
  171. static inline u8 spi_nor_convert_3to4_read(u8 opcode)
  172. {
  173. static const u8 spi_nor_3to4_read[][2] = {
  174. { SPINOR_OP_READ, SPINOR_OP_READ_4B },
  175. { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
  176. { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
  177. { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
  178. { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
  179. { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
  180. { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
  181. { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
  182. { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
  183. };
  184. return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
  185. ARRAY_SIZE(spi_nor_3to4_read));
  186. }
  187. static inline u8 spi_nor_convert_3to4_program(u8 opcode)
  188. {
  189. static const u8 spi_nor_3to4_program[][2] = {
  190. { SPINOR_OP_PP, SPINOR_OP_PP_4B },
  191. { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
  192. { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
  193. };
  194. return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
  195. ARRAY_SIZE(spi_nor_3to4_program));
  196. }
  197. static inline u8 spi_nor_convert_3to4_erase(u8 opcode)
  198. {
  199. static const u8 spi_nor_3to4_erase[][2] = {
  200. { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
  201. { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
  202. { SPINOR_OP_SE, SPINOR_OP_SE_4B },
  203. };
  204. return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
  205. ARRAY_SIZE(spi_nor_3to4_erase));
  206. }
  207. static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
  208. const struct flash_info *info)
  209. {
  210. /* Do some manufacturer fixups first */
  211. switch (JEDEC_MFR(info)) {
  212. case SNOR_MFR_SPANSION:
  213. /* No small sector erase for 4-byte command set */
  214. nor->erase_opcode = SPINOR_OP_SE;
  215. nor->mtd.erasesize = info->sector_size;
  216. break;
  217. default:
  218. break;
  219. }
  220. nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
  221. nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
  222. nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
  223. if (!spi_nor_has_uniform_erase(nor)) {
  224. struct spi_nor_erase_map *map = &nor->erase_map;
  225. struct spi_nor_erase_type *erase;
  226. int i;
  227. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  228. erase = &map->erase_type[i];
  229. erase->opcode =
  230. spi_nor_convert_3to4_erase(erase->opcode);
  231. }
  232. }
  233. }
  234. /* Enable/disable 4-byte addressing mode. */
  235. static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
  236. int enable)
  237. {
  238. int status;
  239. bool need_wren = false;
  240. u8 cmd;
  241. switch (JEDEC_MFR(info)) {
  242. case SNOR_MFR_MICRON:
  243. /* Some Micron need WREN command; all will accept it */
  244. need_wren = true;
  245. case SNOR_MFR_MACRONIX:
  246. case SNOR_MFR_WINBOND:
  247. if (need_wren)
  248. write_enable(nor);
  249. cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  250. status = nor->write_reg(nor, cmd, NULL, 0);
  251. if (need_wren)
  252. write_disable(nor);
  253. if (!status && !enable &&
  254. JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
  255. /*
  256. * On Winbond W25Q256FV, leaving 4byte mode causes
  257. * the Extended Address Register to be set to 1, so all
  258. * 3-byte-address reads come from the second 16M.
  259. * We must clear the register to enable normal behavior.
  260. */
  261. write_enable(nor);
  262. nor->cmd_buf[0] = 0;
  263. nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
  264. write_disable(nor);
  265. }
  266. return status;
  267. default:
  268. /* Spansion style */
  269. nor->cmd_buf[0] = enable << 7;
  270. return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
  271. }
  272. }
  273. static int s3an_sr_ready(struct spi_nor *nor)
  274. {
  275. int ret;
  276. u8 val;
  277. ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
  278. if (ret < 0) {
  279. dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
  280. return ret;
  281. }
  282. return !!(val & XSR_RDY);
  283. }
  284. static inline int spi_nor_sr_ready(struct spi_nor *nor)
  285. {
  286. int sr = read_sr(nor);
  287. if (sr < 0)
  288. return sr;
  289. if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
  290. if (sr & SR_E_ERR)
  291. dev_err(nor->dev, "Erase Error occurred\n");
  292. else
  293. dev_err(nor->dev, "Programming Error occurred\n");
  294. nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
  295. return -EIO;
  296. }
  297. return !(sr & SR_WIP);
  298. }
  299. static inline int spi_nor_fsr_ready(struct spi_nor *nor)
  300. {
  301. int fsr = read_fsr(nor);
  302. if (fsr < 0)
  303. return fsr;
  304. if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
  305. if (fsr & FSR_E_ERR)
  306. dev_err(nor->dev, "Erase operation failed.\n");
  307. else
  308. dev_err(nor->dev, "Program operation failed.\n");
  309. if (fsr & FSR_PT_ERR)
  310. dev_err(nor->dev,
  311. "Attempted to modify a protected sector.\n");
  312. nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
  313. return -EIO;
  314. }
  315. return fsr & FSR_READY;
  316. }
  317. static int spi_nor_ready(struct spi_nor *nor)
  318. {
  319. int sr, fsr;
  320. if (nor->flags & SNOR_F_READY_XSR_RDY)
  321. sr = s3an_sr_ready(nor);
  322. else
  323. sr = spi_nor_sr_ready(nor);
  324. if (sr < 0)
  325. return sr;
  326. fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
  327. if (fsr < 0)
  328. return fsr;
  329. return sr && fsr;
  330. }
  331. /*
  332. * Service routine to read status register until ready, or timeout occurs.
  333. * Returns non-zero if error.
  334. */
  335. static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
  336. unsigned long timeout_jiffies)
  337. {
  338. unsigned long deadline;
  339. int timeout = 0, ret;
  340. deadline = jiffies + timeout_jiffies;
  341. while (!timeout) {
  342. if (time_after_eq(jiffies, deadline))
  343. timeout = 1;
  344. ret = spi_nor_ready(nor);
  345. if (ret < 0)
  346. return ret;
  347. if (ret)
  348. return 0;
  349. cond_resched();
  350. }
  351. dev_err(nor->dev, "flash operation timed out\n");
  352. return -ETIMEDOUT;
  353. }
  354. static int spi_nor_wait_till_ready(struct spi_nor *nor)
  355. {
  356. return spi_nor_wait_till_ready_with_timeout(nor,
  357. DEFAULT_READY_WAIT_JIFFIES);
  358. }
  359. /*
  360. * Erase the whole flash memory
  361. *
  362. * Returns 0 if successful, non-zero otherwise.
  363. */
  364. static int erase_chip(struct spi_nor *nor)
  365. {
  366. dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
  367. return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
  368. }
  369. static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  370. {
  371. int ret = 0;
  372. mutex_lock(&nor->lock);
  373. if (nor->prepare) {
  374. ret = nor->prepare(nor, ops);
  375. if (ret) {
  376. dev_err(nor->dev, "failed in the preparation.\n");
  377. mutex_unlock(&nor->lock);
  378. return ret;
  379. }
  380. }
  381. return ret;
  382. }
  383. static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  384. {
  385. if (nor->unprepare)
  386. nor->unprepare(nor, ops);
  387. mutex_unlock(&nor->lock);
  388. }
  389. /*
  390. * This code converts an address to the Default Address Mode, that has non
  391. * power of two page sizes. We must support this mode because it is the default
  392. * mode supported by Xilinx tools, it can access the whole flash area and
  393. * changing over to the Power-of-two mode is irreversible and corrupts the
  394. * original data.
  395. * Addr can safely be unsigned int, the biggest S3AN device is smaller than
  396. * 4 MiB.
  397. */
  398. static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
  399. {
  400. unsigned int offset;
  401. unsigned int page;
  402. offset = addr % nor->page_size;
  403. page = addr / nor->page_size;
  404. page <<= (nor->page_size > 512) ? 10 : 9;
  405. return page | offset;
  406. }
  407. /*
  408. * Initiate the erasure of a single sector
  409. */
  410. static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  411. {
  412. u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  413. int i;
  414. if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  415. addr = spi_nor_s3an_addr_convert(nor, addr);
  416. if (nor->erase)
  417. return nor->erase(nor, addr);
  418. /*
  419. * Default implementation, if driver doesn't have a specialized HW
  420. * control
  421. */
  422. for (i = nor->addr_width - 1; i >= 0; i--) {
  423. buf[i] = addr & 0xff;
  424. addr >>= 8;
  425. }
  426. return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  427. }
  428. /**
  429. * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
  430. * @erase: pointer to a structure that describes a SPI NOR erase type
  431. * @dividend: dividend value
  432. * @remainder: pointer to u32 remainder (will be updated)
  433. *
  434. * Return: the result of the division
  435. */
  436. static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
  437. u64 dividend, u32 *remainder)
  438. {
  439. /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
  440. *remainder = (u32)dividend & erase->size_mask;
  441. return dividend >> erase->size_shift;
  442. }
  443. /**
  444. * spi_nor_find_best_erase_type() - find the best erase type for the given
  445. * offset in the serial flash memory and the
  446. * number of bytes to erase. The region in
  447. * which the address fits is expected to be
  448. * provided.
  449. * @map: the erase map of the SPI NOR
  450. * @region: pointer to a structure that describes a SPI NOR erase region
  451. * @addr: offset in the serial flash memory
  452. * @len: number of bytes to erase
  453. *
  454. * Return: a pointer to the best fitted erase type, NULL otherwise.
  455. */
  456. static const struct spi_nor_erase_type *
  457. spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
  458. const struct spi_nor_erase_region *region,
  459. u64 addr, u32 len)
  460. {
  461. const struct spi_nor_erase_type *erase;
  462. u32 rem;
  463. int i;
  464. u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
  465. /*
  466. * Erase types are ordered by size, with the biggest erase type at
  467. * index 0.
  468. */
  469. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  470. /* Does the erase region support the tested erase type? */
  471. if (!(erase_mask & BIT(i)))
  472. continue;
  473. erase = &map->erase_type[i];
  474. /* Don't erase more than what the user has asked for. */
  475. if (erase->size > len)
  476. continue;
  477. /* Alignment is not mandatory for overlaid regions */
  478. if (region->offset & SNOR_OVERLAID_REGION)
  479. return erase;
  480. spi_nor_div_by_erase_size(erase, addr, &rem);
  481. if (rem)
  482. continue;
  483. else
  484. return erase;
  485. }
  486. return NULL;
  487. }
  488. /**
  489. * spi_nor_region_next() - get the next spi nor region
  490. * @region: pointer to a structure that describes a SPI NOR erase region
  491. *
  492. * Return: the next spi nor region or NULL if last region.
  493. */
  494. static struct spi_nor_erase_region *
  495. spi_nor_region_next(struct spi_nor_erase_region *region)
  496. {
  497. if (spi_nor_region_is_last(region))
  498. return NULL;
  499. region++;
  500. return region;
  501. }
  502. /**
  503. * spi_nor_find_erase_region() - find the region of the serial flash memory in
  504. * which the offset fits
  505. * @map: the erase map of the SPI NOR
  506. * @addr: offset in the serial flash memory
  507. *
  508. * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
  509. * otherwise.
  510. */
  511. static struct spi_nor_erase_region *
  512. spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
  513. {
  514. struct spi_nor_erase_region *region = map->regions;
  515. u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
  516. u64 region_end = region_start + region->size;
  517. while (addr < region_start || addr >= region_end) {
  518. region = spi_nor_region_next(region);
  519. if (!region)
  520. return ERR_PTR(-EINVAL);
  521. region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
  522. region_end = region_start + region->size;
  523. }
  524. return region;
  525. }
  526. /**
  527. * spi_nor_init_erase_cmd() - initialize an erase command
  528. * @region: pointer to a structure that describes a SPI NOR erase region
  529. * @erase: pointer to a structure that describes a SPI NOR erase type
  530. *
  531. * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
  532. * otherwise.
  533. */
  534. static struct spi_nor_erase_command *
  535. spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
  536. const struct spi_nor_erase_type *erase)
  537. {
  538. struct spi_nor_erase_command *cmd;
  539. cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  540. if (!cmd)
  541. return ERR_PTR(-ENOMEM);
  542. INIT_LIST_HEAD(&cmd->list);
  543. cmd->opcode = erase->opcode;
  544. cmd->count = 1;
  545. if (region->offset & SNOR_OVERLAID_REGION)
  546. cmd->size = region->size;
  547. else
  548. cmd->size = erase->size;
  549. return cmd;
  550. }
  551. /**
  552. * spi_nor_destroy_erase_cmd_list() - destroy erase command list
  553. * @erase_list: list of erase commands
  554. */
  555. static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
  556. {
  557. struct spi_nor_erase_command *cmd, *next;
  558. list_for_each_entry_safe(cmd, next, erase_list, list) {
  559. list_del(&cmd->list);
  560. kfree(cmd);
  561. }
  562. }
  563. /**
  564. * spi_nor_init_erase_cmd_list() - initialize erase command list
  565. * @nor: pointer to a 'struct spi_nor'
  566. * @erase_list: list of erase commands to be executed once we validate that the
  567. * erase can be performed
  568. * @addr: offset in the serial flash memory
  569. * @len: number of bytes to erase
  570. *
  571. * Builds the list of best fitted erase commands and verifies if the erase can
  572. * be performed.
  573. *
  574. * Return: 0 on success, -errno otherwise.
  575. */
  576. static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
  577. struct list_head *erase_list,
  578. u64 addr, u32 len)
  579. {
  580. const struct spi_nor_erase_map *map = &nor->erase_map;
  581. const struct spi_nor_erase_type *erase, *prev_erase = NULL;
  582. struct spi_nor_erase_region *region;
  583. struct spi_nor_erase_command *cmd = NULL;
  584. u64 region_end;
  585. int ret = -EINVAL;
  586. region = spi_nor_find_erase_region(map, addr);
  587. if (IS_ERR(region))
  588. return PTR_ERR(region);
  589. region_end = spi_nor_region_end(region);
  590. while (len) {
  591. erase = spi_nor_find_best_erase_type(map, region, addr, len);
  592. if (!erase)
  593. goto destroy_erase_cmd_list;
  594. if (prev_erase != erase ||
  595. region->offset & SNOR_OVERLAID_REGION) {
  596. cmd = spi_nor_init_erase_cmd(region, erase);
  597. if (IS_ERR(cmd)) {
  598. ret = PTR_ERR(cmd);
  599. goto destroy_erase_cmd_list;
  600. }
  601. list_add_tail(&cmd->list, erase_list);
  602. } else {
  603. cmd->count++;
  604. }
  605. addr += cmd->size;
  606. len -= cmd->size;
  607. if (len && addr >= region_end) {
  608. region = spi_nor_region_next(region);
  609. if (!region)
  610. goto destroy_erase_cmd_list;
  611. region_end = spi_nor_region_end(region);
  612. }
  613. prev_erase = erase;
  614. }
  615. return 0;
  616. destroy_erase_cmd_list:
  617. spi_nor_destroy_erase_cmd_list(erase_list);
  618. return ret;
  619. }
  620. /**
  621. * spi_nor_erase_multi_sectors() - perform a non-uniform erase
  622. * @nor: pointer to a 'struct spi_nor'
  623. * @addr: offset in the serial flash memory
  624. * @len: number of bytes to erase
  625. *
  626. * Build a list of best fitted erase commands and execute it once we validate
  627. * that the erase can be performed.
  628. *
  629. * Return: 0 on success, -errno otherwise.
  630. */
  631. static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
  632. {
  633. LIST_HEAD(erase_list);
  634. struct spi_nor_erase_command *cmd, *next;
  635. int ret;
  636. ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
  637. if (ret)
  638. return ret;
  639. list_for_each_entry_safe(cmd, next, &erase_list, list) {
  640. nor->erase_opcode = cmd->opcode;
  641. while (cmd->count) {
  642. write_enable(nor);
  643. ret = spi_nor_erase_sector(nor, addr);
  644. if (ret)
  645. goto destroy_erase_cmd_list;
  646. addr += cmd->size;
  647. cmd->count--;
  648. ret = spi_nor_wait_till_ready(nor);
  649. if (ret)
  650. goto destroy_erase_cmd_list;
  651. }
  652. list_del(&cmd->list);
  653. kfree(cmd);
  654. }
  655. return 0;
  656. destroy_erase_cmd_list:
  657. spi_nor_destroy_erase_cmd_list(&erase_list);
  658. return ret;
  659. }
  660. /*
  661. * Erase an address range on the nor chip. The address range may extend
  662. * one or more erase sectors. Return an error is there is a problem erasing.
  663. */
  664. static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  665. {
  666. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  667. u32 addr, len;
  668. uint32_t rem;
  669. int ret;
  670. dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  671. (long long)instr->len);
  672. if (spi_nor_has_uniform_erase(nor)) {
  673. div_u64_rem(instr->len, mtd->erasesize, &rem);
  674. if (rem)
  675. return -EINVAL;
  676. }
  677. addr = instr->addr;
  678. len = instr->len;
  679. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
  680. if (ret)
  681. return ret;
  682. /* whole-chip erase? */
  683. if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
  684. unsigned long timeout;
  685. write_enable(nor);
  686. if (erase_chip(nor)) {
  687. ret = -EIO;
  688. goto erase_err;
  689. }
  690. /*
  691. * Scale the timeout linearly with the size of the flash, with
  692. * a minimum calibrated to an old 2MB flash. We could try to
  693. * pull these from CFI/SFDP, but these values should be good
  694. * enough for now.
  695. */
  696. timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
  697. CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
  698. (unsigned long)(mtd->size / SZ_2M));
  699. ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
  700. if (ret)
  701. goto erase_err;
  702. /* REVISIT in some cases we could speed up erasing large regions
  703. * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
  704. * to use "small sector erase", but that's not always optimal.
  705. */
  706. /* "sector"-at-a-time erase */
  707. } else if (spi_nor_has_uniform_erase(nor)) {
  708. while (len) {
  709. write_enable(nor);
  710. ret = spi_nor_erase_sector(nor, addr);
  711. if (ret)
  712. goto erase_err;
  713. addr += mtd->erasesize;
  714. len -= mtd->erasesize;
  715. ret = spi_nor_wait_till_ready(nor);
  716. if (ret)
  717. goto erase_err;
  718. }
  719. /* erase multiple sectors */
  720. } else {
  721. ret = spi_nor_erase_multi_sectors(nor, addr, len);
  722. if (ret)
  723. goto erase_err;
  724. }
  725. write_disable(nor);
  726. erase_err:
  727. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  728. return ret;
  729. }
  730. /* Write status register and ensure bits in mask match written values */
  731. static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
  732. {
  733. int ret;
  734. write_enable(nor);
  735. ret = write_sr(nor, status_new);
  736. if (ret)
  737. return ret;
  738. ret = spi_nor_wait_till_ready(nor);
  739. if (ret)
  740. return ret;
  741. ret = read_sr(nor);
  742. if (ret < 0)
  743. return ret;
  744. return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
  745. }
  746. static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
  747. uint64_t *len)
  748. {
  749. struct mtd_info *mtd = &nor->mtd;
  750. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  751. int shift = ffs(mask) - 1;
  752. int pow;
  753. if (!(sr & mask)) {
  754. /* No protection */
  755. *ofs = 0;
  756. *len = 0;
  757. } else {
  758. pow = ((sr & mask) ^ mask) >> shift;
  759. *len = mtd->size >> pow;
  760. if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
  761. *ofs = 0;
  762. else
  763. *ofs = mtd->size - *len;
  764. }
  765. }
  766. /*
  767. * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
  768. * @locked is false); 0 otherwise
  769. */
  770. static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  771. u8 sr, bool locked)
  772. {
  773. loff_t lock_offs;
  774. uint64_t lock_len;
  775. if (!len)
  776. return 1;
  777. stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
  778. if (locked)
  779. /* Requested range is a sub-range of locked range */
  780. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  781. else
  782. /* Requested range does not overlap with locked range */
  783. return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
  784. }
  785. static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  786. u8 sr)
  787. {
  788. return stm_check_lock_status_sr(nor, ofs, len, sr, true);
  789. }
  790. static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  791. u8 sr)
  792. {
  793. return stm_check_lock_status_sr(nor, ofs, len, sr, false);
  794. }
  795. /*
  796. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  797. * Supports the block protection bits BP{0,1,2} in the status register
  798. * (SR). Does not support these features found in newer SR bitfields:
  799. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  800. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  801. *
  802. * Support for the following is provided conditionally for some flash:
  803. * - TB: top/bottom protect
  804. *
  805. * Sample table portion for 8MB flash (Winbond w25q64fw):
  806. *
  807. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  808. * --------------------------------------------------------------------------
  809. * X | X | 0 | 0 | 0 | NONE | NONE
  810. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  811. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  812. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  813. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  814. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  815. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  816. * X | X | 1 | 1 | 1 | 8 MB | ALL
  817. * ------|-------|-------|-------|-------|---------------|-------------------
  818. * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
  819. * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
  820. * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
  821. * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
  822. * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
  823. * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
  824. *
  825. * Returns negative on errors, 0 on success.
  826. */
  827. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  828. {
  829. struct mtd_info *mtd = &nor->mtd;
  830. int status_old, status_new;
  831. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  832. u8 shift = ffs(mask) - 1, pow, val;
  833. loff_t lock_len;
  834. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  835. bool use_top;
  836. status_old = read_sr(nor);
  837. if (status_old < 0)
  838. return status_old;
  839. /* If nothing in our range is unlocked, we don't need to do anything */
  840. if (stm_is_locked_sr(nor, ofs, len, status_old))
  841. return 0;
  842. /* If anything below us is unlocked, we can't use 'bottom' protection */
  843. if (!stm_is_locked_sr(nor, 0, ofs, status_old))
  844. can_be_bottom = false;
  845. /* If anything above us is unlocked, we can't use 'top' protection */
  846. if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
  847. status_old))
  848. can_be_top = false;
  849. if (!can_be_bottom && !can_be_top)
  850. return -EINVAL;
  851. /* Prefer top, if both are valid */
  852. use_top = can_be_top;
  853. /* lock_len: length of region that should end up locked */
  854. if (use_top)
  855. lock_len = mtd->size - ofs;
  856. else
  857. lock_len = ofs + len;
  858. /*
  859. * Need smallest pow such that:
  860. *
  861. * 1 / (2^pow) <= (len / size)
  862. *
  863. * so (assuming power-of-2 size) we do:
  864. *
  865. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  866. */
  867. pow = ilog2(mtd->size) - ilog2(lock_len);
  868. val = mask - (pow << shift);
  869. if (val & ~mask)
  870. return -EINVAL;
  871. /* Don't "lock" with no region! */
  872. if (!(val & mask))
  873. return -EINVAL;
  874. status_new = (status_old & ~mask & ~SR_TB) | val;
  875. /* Disallow further writes if WP pin is asserted */
  876. status_new |= SR_SRWD;
  877. if (!use_top)
  878. status_new |= SR_TB;
  879. /* Don't bother if they're the same */
  880. if (status_new == status_old)
  881. return 0;
  882. /* Only modify protection if it will not unlock other areas */
  883. if ((status_new & mask) < (status_old & mask))
  884. return -EINVAL;
  885. return write_sr_and_check(nor, status_new, mask);
  886. }
  887. /*
  888. * Unlock a region of the flash. See stm_lock() for more info
  889. *
  890. * Returns negative on errors, 0 on success.
  891. */
  892. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  893. {
  894. struct mtd_info *mtd = &nor->mtd;
  895. int status_old, status_new;
  896. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  897. u8 shift = ffs(mask) - 1, pow, val;
  898. loff_t lock_len;
  899. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  900. bool use_top;
  901. status_old = read_sr(nor);
  902. if (status_old < 0)
  903. return status_old;
  904. /* If nothing in our range is locked, we don't need to do anything */
  905. if (stm_is_unlocked_sr(nor, ofs, len, status_old))
  906. return 0;
  907. /* If anything below us is locked, we can't use 'top' protection */
  908. if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
  909. can_be_top = false;
  910. /* If anything above us is locked, we can't use 'bottom' protection */
  911. if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
  912. status_old))
  913. can_be_bottom = false;
  914. if (!can_be_bottom && !can_be_top)
  915. return -EINVAL;
  916. /* Prefer top, if both are valid */
  917. use_top = can_be_top;
  918. /* lock_len: length of region that should remain locked */
  919. if (use_top)
  920. lock_len = mtd->size - (ofs + len);
  921. else
  922. lock_len = ofs;
  923. /*
  924. * Need largest pow such that:
  925. *
  926. * 1 / (2^pow) >= (len / size)
  927. *
  928. * so (assuming power-of-2 size) we do:
  929. *
  930. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  931. */
  932. pow = ilog2(mtd->size) - order_base_2(lock_len);
  933. if (lock_len == 0) {
  934. val = 0; /* fully unlocked */
  935. } else {
  936. val = mask - (pow << shift);
  937. /* Some power-of-two sizes are not supported */
  938. if (val & ~mask)
  939. return -EINVAL;
  940. }
  941. status_new = (status_old & ~mask & ~SR_TB) | val;
  942. /* Don't protect status register if we're fully unlocked */
  943. if (lock_len == 0)
  944. status_new &= ~SR_SRWD;
  945. if (!use_top)
  946. status_new |= SR_TB;
  947. /* Don't bother if they're the same */
  948. if (status_new == status_old)
  949. return 0;
  950. /* Only modify protection if it will not lock other areas */
  951. if ((status_new & mask) > (status_old & mask))
  952. return -EINVAL;
  953. return write_sr_and_check(nor, status_new, mask);
  954. }
  955. /*
  956. * Check if a region of the flash is (completely) locked. See stm_lock() for
  957. * more info.
  958. *
  959. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  960. * negative on errors.
  961. */
  962. static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
  963. {
  964. int status;
  965. status = read_sr(nor);
  966. if (status < 0)
  967. return status;
  968. return stm_is_locked_sr(nor, ofs, len, status);
  969. }
  970. static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  971. {
  972. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  973. int ret;
  974. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
  975. if (ret)
  976. return ret;
  977. ret = nor->flash_lock(nor, ofs, len);
  978. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
  979. return ret;
  980. }
  981. static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  982. {
  983. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  984. int ret;
  985. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  986. if (ret)
  987. return ret;
  988. ret = nor->flash_unlock(nor, ofs, len);
  989. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  990. return ret;
  991. }
  992. static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  993. {
  994. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  995. int ret;
  996. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  997. if (ret)
  998. return ret;
  999. ret = nor->flash_is_locked(nor, ofs, len);
  1000. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  1001. return ret;
  1002. }
  1003. static int macronix_quad_enable(struct spi_nor *nor);
  1004. /* Used when the "_ext_id" is two bytes at most */
  1005. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  1006. .id = { \
  1007. ((_jedec_id) >> 16) & 0xff, \
  1008. ((_jedec_id) >> 8) & 0xff, \
  1009. (_jedec_id) & 0xff, \
  1010. ((_ext_id) >> 8) & 0xff, \
  1011. (_ext_id) & 0xff, \
  1012. }, \
  1013. .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
  1014. .sector_size = (_sector_size), \
  1015. .n_sectors = (_n_sectors), \
  1016. .page_size = 256, \
  1017. .flags = (_flags),
  1018. #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  1019. .id = { \
  1020. ((_jedec_id) >> 16) & 0xff, \
  1021. ((_jedec_id) >> 8) & 0xff, \
  1022. (_jedec_id) & 0xff, \
  1023. ((_ext_id) >> 16) & 0xff, \
  1024. ((_ext_id) >> 8) & 0xff, \
  1025. (_ext_id) & 0xff, \
  1026. }, \
  1027. .id_len = 6, \
  1028. .sector_size = (_sector_size), \
  1029. .n_sectors = (_n_sectors), \
  1030. .page_size = 256, \
  1031. .flags = (_flags),
  1032. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  1033. .sector_size = (_sector_size), \
  1034. .n_sectors = (_n_sectors), \
  1035. .page_size = (_page_size), \
  1036. .addr_width = (_addr_width), \
  1037. .flags = (_flags),
  1038. #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
  1039. .id = { \
  1040. ((_jedec_id) >> 16) & 0xff, \
  1041. ((_jedec_id) >> 8) & 0xff, \
  1042. (_jedec_id) & 0xff \
  1043. }, \
  1044. .id_len = 3, \
  1045. .sector_size = (8*_page_size), \
  1046. .n_sectors = (_n_sectors), \
  1047. .page_size = _page_size, \
  1048. .addr_width = 3, \
  1049. .flags = SPI_NOR_NO_FR | SPI_S3AN,
  1050. /* NOTE: double check command sets and memory organization when you add
  1051. * more nor chips. This current list focusses on newer chips, which
  1052. * have been converging on command sets which including JEDEC ID.
  1053. *
  1054. * All newly added entries should describe *hardware* and should use SECT_4K
  1055. * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
  1056. * scenarios excluding small sectors there is config option that can be
  1057. * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
  1058. * For historical (and compatibility) reasons (before we got above config) some
  1059. * old entries may be missing 4K flag.
  1060. */
  1061. static const struct flash_info spi_nor_ids[] = {
  1062. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  1063. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  1064. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  1065. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  1066. { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  1067. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  1068. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  1069. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  1070. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  1071. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  1072. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  1073. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  1074. /* EON -- en25xxx */
  1075. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  1076. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  1077. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  1078. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  1079. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  1080. { "en25qh32", INFO(0x1c7016, 0, 64 * 1024, 64, 0) },
  1081. { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  1082. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  1083. { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
  1084. /* ESMT */
  1085. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
  1086. { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
  1087. { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_HAS_LOCK) },
  1088. /* Everspin */
  1089. { "mr25h128", CAT25_INFO( 16 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1090. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1091. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1092. { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1093. /* Fujitsu */
  1094. { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
  1095. /* GigaDevice */
  1096. {
  1097. "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
  1098. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1099. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1100. },
  1101. {
  1102. "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
  1103. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1104. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1105. },
  1106. {
  1107. "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
  1108. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1109. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1110. },
  1111. {
  1112. "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
  1113. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1114. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1115. },
  1116. {
  1117. "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
  1118. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1119. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1120. },
  1121. {
  1122. "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
  1123. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1124. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1125. },
  1126. {
  1127. "gd25q256", INFO(0xc84019, 0, 64 * 1024, 512,
  1128. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1129. SPI_NOR_4B_OPCODES | SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1130. .quad_enable = macronix_quad_enable,
  1131. },
  1132. /* Intel/Numonyx -- xxxs33b */
  1133. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  1134. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  1135. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  1136. /* ISSI */
  1137. { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
  1138. { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
  1139. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1140. { "is25lp080d", INFO(0x9d6014, 0, 64 * 1024, 16,
  1141. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1142. { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
  1143. SECT_4K | SPI_NOR_DUAL_READ) },
  1144. { "is25lp256", INFO(0x9d6019, 0, 64 * 1024, 512,
  1145. SECT_4K | SPI_NOR_DUAL_READ) },
  1146. { "is25wp032", INFO(0x9d7016, 0, 64 * 1024, 64,
  1147. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1148. { "is25wp064", INFO(0x9d7017, 0, 64 * 1024, 128,
  1149. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1150. { "is25wp128", INFO(0x9d7018, 0, 64 * 1024, 256,
  1151. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1152. /* Macronix */
  1153. { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
  1154. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  1155. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  1156. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  1157. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  1158. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
  1159. { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  1160. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
  1161. { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
  1162. { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K) },
  1163. { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K) },
  1164. { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
  1165. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  1166. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  1167. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1168. { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
  1169. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  1170. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  1171. { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  1172. { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1173. { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
  1174. /* Micron */
  1175. { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
  1176. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  1177. { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  1178. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  1179. { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  1180. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  1181. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  1182. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1183. { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  1184. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  1185. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  1186. { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
  1187. { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
  1188. { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
  1189. /* PMC */
  1190. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  1191. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  1192. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  1193. /* Spansion/Cypress -- single (large) sector size only, at least
  1194. * for the chips listed here (without boot sectors).
  1195. */
  1196. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1197. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1198. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
  1199. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  1200. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  1201. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  1202. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  1203. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  1204. { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  1205. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  1206. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
  1207. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  1208. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  1209. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  1210. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  1211. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  1212. { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1213. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1214. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1215. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  1216. { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1217. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  1218. { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  1219. { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
  1220. { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
  1221. { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  1222. { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  1223. { "s25fl256l", INFO(0x016019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
  1224. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  1225. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  1226. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  1227. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  1228. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  1229. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  1230. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  1231. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  1232. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  1233. { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
  1234. { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
  1235. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  1236. { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  1237. { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1238. /* ST Microelectronics -- newer production may have feature updates */
  1239. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  1240. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  1241. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  1242. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  1243. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  1244. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  1245. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  1246. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  1247. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  1248. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  1249. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  1250. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  1251. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  1252. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  1253. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  1254. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  1255. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  1256. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  1257. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  1258. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  1259. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  1260. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  1261. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  1262. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  1263. { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
  1264. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  1265. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  1266. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  1267. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  1268. { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
  1269. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  1270. { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
  1271. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  1272. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  1273. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  1274. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  1275. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  1276. {
  1277. "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
  1278. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1279. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1280. },
  1281. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  1282. { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
  1283. { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
  1284. { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
  1285. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  1286. {
  1287. "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
  1288. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1289. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1290. },
  1291. {
  1292. "w25q32jv", INFO(0xef7016, 0, 64 * 1024, 64,
  1293. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1294. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1295. },
  1296. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  1297. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  1298. {
  1299. "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
  1300. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1301. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1302. },
  1303. {
  1304. "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
  1305. SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  1306. SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  1307. },
  1308. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  1309. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  1310. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  1311. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1312. { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
  1313. SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
  1314. /* Catalyst / On Semiconductor -- non-JEDEC */
  1315. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1316. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1317. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1318. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1319. { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1320. /* Xilinx S3AN Internal Flash */
  1321. { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
  1322. { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
  1323. { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
  1324. { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
  1325. { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
  1326. /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
  1327. { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1328. { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1329. { },
  1330. };
  1331. static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
  1332. {
  1333. int tmp;
  1334. u8 id[SPI_NOR_MAX_ID_LEN];
  1335. const struct flash_info *info;
  1336. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  1337. if (tmp < 0) {
  1338. dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
  1339. return ERR_PTR(tmp);
  1340. }
  1341. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  1342. info = &spi_nor_ids[tmp];
  1343. if (info->id_len) {
  1344. if (!memcmp(info->id, id, info->id_len))
  1345. return &spi_nor_ids[tmp];
  1346. }
  1347. }
  1348. dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  1349. id[0], id[1], id[2]);
  1350. return ERR_PTR(-ENODEV);
  1351. }
  1352. static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  1353. size_t *retlen, u_char *buf)
  1354. {
  1355. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1356. int ret;
  1357. dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  1358. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
  1359. if (ret)
  1360. return ret;
  1361. while (len) {
  1362. loff_t addr = from;
  1363. if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  1364. addr = spi_nor_s3an_addr_convert(nor, addr);
  1365. ret = nor->read(nor, addr, len, buf);
  1366. if (ret == 0) {
  1367. /* We shouldn't see 0-length reads */
  1368. ret = -EIO;
  1369. goto read_err;
  1370. }
  1371. if (ret < 0)
  1372. goto read_err;
  1373. WARN_ON(ret > len);
  1374. *retlen += ret;
  1375. buf += ret;
  1376. from += ret;
  1377. len -= ret;
  1378. }
  1379. ret = 0;
  1380. read_err:
  1381. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
  1382. return ret;
  1383. }
  1384. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  1385. size_t *retlen, const u_char *buf)
  1386. {
  1387. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1388. size_t actual;
  1389. int ret;
  1390. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1391. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  1392. if (ret)
  1393. return ret;
  1394. write_enable(nor);
  1395. nor->sst_write_second = false;
  1396. actual = to % 2;
  1397. /* Start write from odd address. */
  1398. if (actual) {
  1399. nor->program_opcode = SPINOR_OP_BP;
  1400. /* write one byte. */
  1401. ret = nor->write(nor, to, 1, buf);
  1402. if (ret < 0)
  1403. goto sst_write_err;
  1404. WARN(ret != 1, "While writing 1 byte written %i bytes\n",
  1405. (int)ret);
  1406. ret = spi_nor_wait_till_ready(nor);
  1407. if (ret)
  1408. goto sst_write_err;
  1409. }
  1410. to += actual;
  1411. /* Write out most of the data here. */
  1412. for (; actual < len - 1; actual += 2) {
  1413. nor->program_opcode = SPINOR_OP_AAI_WP;
  1414. /* write two bytes. */
  1415. ret = nor->write(nor, to, 2, buf + actual);
  1416. if (ret < 0)
  1417. goto sst_write_err;
  1418. WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
  1419. (int)ret);
  1420. ret = spi_nor_wait_till_ready(nor);
  1421. if (ret)
  1422. goto sst_write_err;
  1423. to += 2;
  1424. nor->sst_write_second = true;
  1425. }
  1426. nor->sst_write_second = false;
  1427. write_disable(nor);
  1428. ret = spi_nor_wait_till_ready(nor);
  1429. if (ret)
  1430. goto sst_write_err;
  1431. /* Write out trailing byte if it exists. */
  1432. if (actual != len) {
  1433. write_enable(nor);
  1434. nor->program_opcode = SPINOR_OP_BP;
  1435. ret = nor->write(nor, to, 1, buf + actual);
  1436. if (ret < 0)
  1437. goto sst_write_err;
  1438. WARN(ret != 1, "While writing 1 byte written %i bytes\n",
  1439. (int)ret);
  1440. ret = spi_nor_wait_till_ready(nor);
  1441. if (ret)
  1442. goto sst_write_err;
  1443. write_disable(nor);
  1444. actual += 1;
  1445. }
  1446. sst_write_err:
  1447. *retlen += actual;
  1448. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1449. return ret;
  1450. }
  1451. /*
  1452. * Write an address range to the nor chip. Data must be written in
  1453. * FLASH_PAGESIZE chunks. The address range may be any size provided
  1454. * it is within the physical boundaries.
  1455. */
  1456. static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  1457. size_t *retlen, const u_char *buf)
  1458. {
  1459. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1460. size_t page_offset, page_remain, i;
  1461. ssize_t ret;
  1462. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1463. ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  1464. if (ret)
  1465. return ret;
  1466. for (i = 0; i < len; ) {
  1467. ssize_t written;
  1468. loff_t addr = to + i;
  1469. /*
  1470. * If page_size is a power of two, the offset can be quickly
  1471. * calculated with an AND operation. On the other cases we
  1472. * need to do a modulus operation (more expensive).
  1473. * Power of two numbers have only one bit set and we can use
  1474. * the instruction hweight32 to detect if we need to do a
  1475. * modulus (do_div()) or not.
  1476. */
  1477. if (hweight32(nor->page_size) == 1) {
  1478. page_offset = addr & (nor->page_size - 1);
  1479. } else {
  1480. uint64_t aux = addr;
  1481. page_offset = do_div(aux, nor->page_size);
  1482. }
  1483. /* the size of data remaining on the first page */
  1484. page_remain = min_t(size_t,
  1485. nor->page_size - page_offset, len - i);
  1486. if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  1487. addr = spi_nor_s3an_addr_convert(nor, addr);
  1488. write_enable(nor);
  1489. ret = nor->write(nor, addr, page_remain, buf + i);
  1490. if (ret < 0)
  1491. goto write_err;
  1492. written = ret;
  1493. ret = spi_nor_wait_till_ready(nor);
  1494. if (ret)
  1495. goto write_err;
  1496. *retlen += written;
  1497. i += written;
  1498. }
  1499. write_err:
  1500. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1501. return ret;
  1502. }
  1503. /**
  1504. * macronix_quad_enable() - set QE bit in Status Register.
  1505. * @nor: pointer to a 'struct spi_nor'
  1506. *
  1507. * Set the Quad Enable (QE) bit in the Status Register.
  1508. *
  1509. * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
  1510. *
  1511. * Return: 0 on success, -errno otherwise.
  1512. */
  1513. static int macronix_quad_enable(struct spi_nor *nor)
  1514. {
  1515. int ret, val;
  1516. val = read_sr(nor);
  1517. if (val < 0)
  1518. return val;
  1519. if (val & SR_QUAD_EN_MX)
  1520. return 0;
  1521. write_enable(nor);
  1522. write_sr(nor, val | SR_QUAD_EN_MX);
  1523. ret = spi_nor_wait_till_ready(nor);
  1524. if (ret)
  1525. return ret;
  1526. ret = read_sr(nor);
  1527. if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
  1528. dev_err(nor->dev, "Macronix Quad bit not set\n");
  1529. return -EINVAL;
  1530. }
  1531. return 0;
  1532. }
  1533. /*
  1534. * Write status Register and configuration register with 2 bytes
  1535. * The first byte will be written to the status register, while the
  1536. * second byte will be written to the configuration register.
  1537. * Return negative if error occurred.
  1538. */
  1539. static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
  1540. {
  1541. int ret;
  1542. write_enable(nor);
  1543. ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
  1544. if (ret < 0) {
  1545. dev_err(nor->dev,
  1546. "error while writing configuration register\n");
  1547. return -EINVAL;
  1548. }
  1549. ret = spi_nor_wait_till_ready(nor);
  1550. if (ret) {
  1551. dev_err(nor->dev,
  1552. "timeout while writing configuration register\n");
  1553. return ret;
  1554. }
  1555. return 0;
  1556. }
  1557. /**
  1558. * spansion_quad_enable() - set QE bit in Configuraiton Register.
  1559. * @nor: pointer to a 'struct spi_nor'
  1560. *
  1561. * Set the Quad Enable (QE) bit in the Configuration Register.
  1562. * This function is kept for legacy purpose because it has been used for a
  1563. * long time without anybody complaining but it should be considered as
  1564. * deprecated and maybe buggy.
  1565. * First, this function doesn't care about the previous values of the Status
  1566. * and Configuration Registers when it sets the QE bit (bit 1) in the
  1567. * Configuration Register: all other bits are cleared, which may have unwanted
  1568. * side effects like removing some block protections.
  1569. * Secondly, it uses the Read Configuration Register (35h) instruction though
  1570. * some very old and few memories don't support this instruction. If a pull-up
  1571. * resistor is present on the MISO/IO1 line, we might still be able to pass the
  1572. * "read back" test because the QSPI memory doesn't recognize the command,
  1573. * so leaves the MISO/IO1 line state unchanged, hence read_cr() returns 0xFF.
  1574. *
  1575. * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
  1576. * memories.
  1577. *
  1578. * Return: 0 on success, -errno otherwise.
  1579. */
  1580. static int spansion_quad_enable(struct spi_nor *nor)
  1581. {
  1582. u8 sr_cr[2] = {0, CR_QUAD_EN_SPAN};
  1583. int ret;
  1584. ret = write_sr_cr(nor, sr_cr);
  1585. if (ret)
  1586. return ret;
  1587. /* read back and check it */
  1588. ret = read_cr(nor);
  1589. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  1590. dev_err(nor->dev, "Spansion Quad bit not set\n");
  1591. return -EINVAL;
  1592. }
  1593. return 0;
  1594. }
  1595. /**
  1596. * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
  1597. * @nor: pointer to a 'struct spi_nor'
  1598. *
  1599. * Set the Quad Enable (QE) bit in the Configuration Register.
  1600. * This function should be used with QSPI memories not supporting the Read
  1601. * Configuration Register (35h) instruction.
  1602. *
  1603. * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
  1604. * memories.
  1605. *
  1606. * Return: 0 on success, -errno otherwise.
  1607. */
  1608. static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
  1609. {
  1610. u8 sr_cr[2];
  1611. int ret;
  1612. /* Keep the current value of the Status Register. */
  1613. ret = read_sr(nor);
  1614. if (ret < 0) {
  1615. dev_err(nor->dev, "error while reading status register\n");
  1616. return -EINVAL;
  1617. }
  1618. sr_cr[0] = ret;
  1619. sr_cr[1] = CR_QUAD_EN_SPAN;
  1620. return write_sr_cr(nor, sr_cr);
  1621. }
  1622. /**
  1623. * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
  1624. * @nor: pointer to a 'struct spi_nor'
  1625. *
  1626. * Set the Quad Enable (QE) bit in the Configuration Register.
  1627. * This function should be used with QSPI memories supporting the Read
  1628. * Configuration Register (35h) instruction.
  1629. *
  1630. * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
  1631. * memories.
  1632. *
  1633. * Return: 0 on success, -errno otherwise.
  1634. */
  1635. static int spansion_read_cr_quad_enable(struct spi_nor *nor)
  1636. {
  1637. struct device *dev = nor->dev;
  1638. u8 sr_cr[2];
  1639. int ret;
  1640. /* Check current Quad Enable bit value. */
  1641. ret = read_cr(nor);
  1642. if (ret < 0) {
  1643. dev_err(dev, "error while reading configuration register\n");
  1644. return -EINVAL;
  1645. }
  1646. if (ret & CR_QUAD_EN_SPAN)
  1647. return 0;
  1648. sr_cr[1] = ret | CR_QUAD_EN_SPAN;
  1649. /* Keep the current value of the Status Register. */
  1650. ret = read_sr(nor);
  1651. if (ret < 0) {
  1652. dev_err(dev, "error while reading status register\n");
  1653. return -EINVAL;
  1654. }
  1655. sr_cr[0] = ret;
  1656. ret = write_sr_cr(nor, sr_cr);
  1657. if (ret)
  1658. return ret;
  1659. /* Read back and check it. */
  1660. ret = read_cr(nor);
  1661. if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  1662. dev_err(nor->dev, "Spansion Quad bit not set\n");
  1663. return -EINVAL;
  1664. }
  1665. return 0;
  1666. }
  1667. /**
  1668. * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
  1669. * @nor: pointer to a 'struct spi_nor'
  1670. *
  1671. * Set the Quad Enable (QE) bit in the Status Register 2.
  1672. *
  1673. * This is one of the procedures to set the QE bit described in the SFDP
  1674. * (JESD216 rev B) specification but no manufacturer using this procedure has
  1675. * been identified yet, hence the name of the function.
  1676. *
  1677. * Return: 0 on success, -errno otherwise.
  1678. */
  1679. static int sr2_bit7_quad_enable(struct spi_nor *nor)
  1680. {
  1681. u8 sr2;
  1682. int ret;
  1683. /* Check current Quad Enable bit value. */
  1684. ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
  1685. if (ret)
  1686. return ret;
  1687. if (sr2 & SR2_QUAD_EN_BIT7)
  1688. return 0;
  1689. /* Update the Quad Enable bit. */
  1690. sr2 |= SR2_QUAD_EN_BIT7;
  1691. write_enable(nor);
  1692. ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
  1693. if (ret < 0) {
  1694. dev_err(nor->dev, "error while writing status register 2\n");
  1695. return -EINVAL;
  1696. }
  1697. ret = spi_nor_wait_till_ready(nor);
  1698. if (ret < 0) {
  1699. dev_err(nor->dev, "timeout while writing status register 2\n");
  1700. return ret;
  1701. }
  1702. /* Read back and check it. */
  1703. ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
  1704. if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
  1705. dev_err(nor->dev, "SR2 Quad bit not set\n");
  1706. return -EINVAL;
  1707. }
  1708. return 0;
  1709. }
  1710. static int spi_nor_check(struct spi_nor *nor)
  1711. {
  1712. if (!nor->dev || !nor->read || !nor->write ||
  1713. !nor->read_reg || !nor->write_reg) {
  1714. pr_err("spi-nor: please fill all the necessary fields!\n");
  1715. return -EINVAL;
  1716. }
  1717. return 0;
  1718. }
  1719. static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
  1720. {
  1721. int ret;
  1722. u8 val;
  1723. ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
  1724. if (ret < 0) {
  1725. dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
  1726. return ret;
  1727. }
  1728. nor->erase_opcode = SPINOR_OP_XSE;
  1729. nor->program_opcode = SPINOR_OP_XPP;
  1730. nor->read_opcode = SPINOR_OP_READ;
  1731. nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
  1732. /*
  1733. * This flashes have a page size of 264 or 528 bytes (known as
  1734. * Default addressing mode). It can be changed to a more standard
  1735. * Power of two mode where the page size is 256/512. This comes
  1736. * with a price: there is 3% less of space, the data is corrupted
  1737. * and the page size cannot be changed back to default addressing
  1738. * mode.
  1739. *
  1740. * The current addressing mode can be read from the XRDSR register
  1741. * and should not be changed, because is a destructive operation.
  1742. */
  1743. if (val & XSR_PAGESIZE) {
  1744. /* Flash in Power of 2 mode */
  1745. nor->page_size = (nor->page_size == 264) ? 256 : 512;
  1746. nor->mtd.writebufsize = nor->page_size;
  1747. nor->mtd.size = 8 * nor->page_size * info->n_sectors;
  1748. nor->mtd.erasesize = 8 * nor->page_size;
  1749. } else {
  1750. /* Flash in Default addressing mode */
  1751. nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
  1752. }
  1753. return 0;
  1754. }
  1755. struct spi_nor_read_command {
  1756. u8 num_mode_clocks;
  1757. u8 num_wait_states;
  1758. u8 opcode;
  1759. enum spi_nor_protocol proto;
  1760. };
  1761. struct spi_nor_pp_command {
  1762. u8 opcode;
  1763. enum spi_nor_protocol proto;
  1764. };
  1765. enum spi_nor_read_command_index {
  1766. SNOR_CMD_READ,
  1767. SNOR_CMD_READ_FAST,
  1768. SNOR_CMD_READ_1_1_1_DTR,
  1769. /* Dual SPI */
  1770. SNOR_CMD_READ_1_1_2,
  1771. SNOR_CMD_READ_1_2_2,
  1772. SNOR_CMD_READ_2_2_2,
  1773. SNOR_CMD_READ_1_2_2_DTR,
  1774. /* Quad SPI */
  1775. SNOR_CMD_READ_1_1_4,
  1776. SNOR_CMD_READ_1_4_4,
  1777. SNOR_CMD_READ_4_4_4,
  1778. SNOR_CMD_READ_1_4_4_DTR,
  1779. /* Octo SPI */
  1780. SNOR_CMD_READ_1_1_8,
  1781. SNOR_CMD_READ_1_8_8,
  1782. SNOR_CMD_READ_8_8_8,
  1783. SNOR_CMD_READ_1_8_8_DTR,
  1784. SNOR_CMD_READ_MAX
  1785. };
  1786. enum spi_nor_pp_command_index {
  1787. SNOR_CMD_PP,
  1788. /* Quad SPI */
  1789. SNOR_CMD_PP_1_1_4,
  1790. SNOR_CMD_PP_1_4_4,
  1791. SNOR_CMD_PP_4_4_4,
  1792. /* Octo SPI */
  1793. SNOR_CMD_PP_1_1_8,
  1794. SNOR_CMD_PP_1_8_8,
  1795. SNOR_CMD_PP_8_8_8,
  1796. SNOR_CMD_PP_MAX
  1797. };
  1798. struct spi_nor_flash_parameter {
  1799. u64 size;
  1800. u32 page_size;
  1801. struct spi_nor_hwcaps hwcaps;
  1802. struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
  1803. struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
  1804. int (*quad_enable)(struct spi_nor *nor);
  1805. };
  1806. static void
  1807. spi_nor_set_read_settings(struct spi_nor_read_command *read,
  1808. u8 num_mode_clocks,
  1809. u8 num_wait_states,
  1810. u8 opcode,
  1811. enum spi_nor_protocol proto)
  1812. {
  1813. read->num_mode_clocks = num_mode_clocks;
  1814. read->num_wait_states = num_wait_states;
  1815. read->opcode = opcode;
  1816. read->proto = proto;
  1817. }
  1818. static void
  1819. spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
  1820. u8 opcode,
  1821. enum spi_nor_protocol proto)
  1822. {
  1823. pp->opcode = opcode;
  1824. pp->proto = proto;
  1825. }
  1826. /*
  1827. * Serial Flash Discoverable Parameters (SFDP) parsing.
  1828. */
  1829. /**
  1830. * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
  1831. * addr_width and read_dummy members of the struct spi_nor
  1832. * should be previously
  1833. * set.
  1834. * @nor: pointer to a 'struct spi_nor'
  1835. * @addr: offset in the serial flash memory
  1836. * @len: number of bytes to read
  1837. * @buf: buffer where the data is copied into (dma-safe memory)
  1838. *
  1839. * Return: 0 on success, -errno otherwise.
  1840. */
  1841. static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
  1842. {
  1843. int ret;
  1844. while (len) {
  1845. ret = nor->read(nor, addr, len, buf);
  1846. if (!ret || ret > len)
  1847. return -EIO;
  1848. if (ret < 0)
  1849. return ret;
  1850. buf += ret;
  1851. addr += ret;
  1852. len -= ret;
  1853. }
  1854. return 0;
  1855. }
  1856. /**
  1857. * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
  1858. * @nor: pointer to a 'struct spi_nor'
  1859. * @addr: offset in the SFDP area to start reading data from
  1860. * @len: number of bytes to read
  1861. * @buf: buffer where the SFDP data are copied into (dma-safe memory)
  1862. *
  1863. * Whatever the actual numbers of bytes for address and dummy cycles are
  1864. * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
  1865. * followed by a 3-byte address and 8 dummy clock cycles.
  1866. *
  1867. * Return: 0 on success, -errno otherwise.
  1868. */
  1869. static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
  1870. size_t len, void *buf)
  1871. {
  1872. u8 addr_width, read_opcode, read_dummy;
  1873. int ret;
  1874. read_opcode = nor->read_opcode;
  1875. addr_width = nor->addr_width;
  1876. read_dummy = nor->read_dummy;
  1877. nor->read_opcode = SPINOR_OP_RDSFDP;
  1878. nor->addr_width = 3;
  1879. nor->read_dummy = 8;
  1880. ret = spi_nor_read_raw(nor, addr, len, buf);
  1881. nor->read_opcode = read_opcode;
  1882. nor->addr_width = addr_width;
  1883. nor->read_dummy = read_dummy;
  1884. return ret;
  1885. }
  1886. /**
  1887. * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
  1888. * @nor: pointer to a 'struct spi_nor'
  1889. * @addr: offset in the SFDP area to start reading data from
  1890. * @len: number of bytes to read
  1891. * @buf: buffer where the SFDP data are copied into
  1892. *
  1893. * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
  1894. * guaranteed to be dma-safe.
  1895. *
  1896. * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
  1897. * otherwise.
  1898. */
  1899. static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
  1900. size_t len, void *buf)
  1901. {
  1902. void *dma_safe_buf;
  1903. int ret;
  1904. dma_safe_buf = kmalloc(len, GFP_KERNEL);
  1905. if (!dma_safe_buf)
  1906. return -ENOMEM;
  1907. ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
  1908. memcpy(buf, dma_safe_buf, len);
  1909. kfree(dma_safe_buf);
  1910. return ret;
  1911. }
  1912. struct sfdp_parameter_header {
  1913. u8 id_lsb;
  1914. u8 minor;
  1915. u8 major;
  1916. u8 length; /* in double words */
  1917. u8 parameter_table_pointer[3]; /* byte address */
  1918. u8 id_msb;
  1919. };
  1920. #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
  1921. #define SFDP_PARAM_HEADER_PTP(p) \
  1922. (((p)->parameter_table_pointer[2] << 16) | \
  1923. ((p)->parameter_table_pointer[1] << 8) | \
  1924. ((p)->parameter_table_pointer[0] << 0))
  1925. #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
  1926. #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
  1927. #define SFDP_SIGNATURE 0x50444653U
  1928. #define SFDP_JESD216_MAJOR 1
  1929. #define SFDP_JESD216_MINOR 0
  1930. #define SFDP_JESD216A_MINOR 5
  1931. #define SFDP_JESD216B_MINOR 6
  1932. struct sfdp_header {
  1933. u32 signature; /* Ox50444653U <=> "SFDP" */
  1934. u8 minor;
  1935. u8 major;
  1936. u8 nph; /* 0-base number of parameter headers */
  1937. u8 unused;
  1938. /* Basic Flash Parameter Table. */
  1939. struct sfdp_parameter_header bfpt_header;
  1940. };
  1941. /* Basic Flash Parameter Table */
  1942. /*
  1943. * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
  1944. * They are indexed from 1 but C arrays are indexed from 0.
  1945. */
  1946. #define BFPT_DWORD(i) ((i) - 1)
  1947. #define BFPT_DWORD_MAX 16
  1948. /* The first version of JESB216 defined only 9 DWORDs. */
  1949. #define BFPT_DWORD_MAX_JESD216 9
  1950. /* 1st DWORD. */
  1951. #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
  1952. #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
  1953. #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
  1954. #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
  1955. #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
  1956. #define BFPT_DWORD1_DTR BIT(19)
  1957. #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
  1958. #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
  1959. #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
  1960. /* 5th DWORD. */
  1961. #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
  1962. #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
  1963. /* 11th DWORD. */
  1964. #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
  1965. #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
  1966. /* 15th DWORD. */
  1967. /*
  1968. * (from JESD216 rev B)
  1969. * Quad Enable Requirements (QER):
  1970. * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
  1971. * reads based on instruction. DQ3/HOLD# functions are hold during
  1972. * instruction phase.
  1973. * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
  1974. * two data bytes where bit 1 of the second byte is one.
  1975. * [...]
  1976. * Writing only one byte to the status register has the side-effect of
  1977. * clearing status register 2, including the QE bit. The 100b code is
  1978. * used if writing one byte to the status register does not modify
  1979. * status register 2.
  1980. * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
  1981. * one data byte where bit 6 is one.
  1982. * [...]
  1983. * - 011b: QE is bit 7 of status register 2. It is set via Write status
  1984. * register 2 instruction 3Eh with one data byte where bit 7 is one.
  1985. * [...]
  1986. * The status register 2 is read using instruction 3Fh.
  1987. * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
  1988. * two data bytes where bit 1 of the second byte is one.
  1989. * [...]
  1990. * In contrast to the 001b code, writing one byte to the status
  1991. * register does not modify status register 2.
  1992. * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
  1993. * Read Status instruction 05h. Status register2 is read using
  1994. * instruction 35h. QE is set via Writ Status instruction 01h with
  1995. * two data bytes where bit 1 of the second byte is one.
  1996. * [...]
  1997. */
  1998. #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
  1999. #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
  2000. #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
  2001. #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
  2002. #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
  2003. #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
  2004. #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
  2005. struct sfdp_bfpt {
  2006. u32 dwords[BFPT_DWORD_MAX];
  2007. };
  2008. /* Fast Read settings. */
  2009. static inline void
  2010. spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
  2011. u16 half,
  2012. enum spi_nor_protocol proto)
  2013. {
  2014. read->num_mode_clocks = (half >> 5) & 0x07;
  2015. read->num_wait_states = (half >> 0) & 0x1f;
  2016. read->opcode = (half >> 8) & 0xff;
  2017. read->proto = proto;
  2018. }
  2019. struct sfdp_bfpt_read {
  2020. /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
  2021. u32 hwcaps;
  2022. /*
  2023. * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
  2024. * whether the Fast Read x-y-z command is supported.
  2025. */
  2026. u32 supported_dword;
  2027. u32 supported_bit;
  2028. /*
  2029. * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
  2030. * encodes the op code, the number of mode clocks and the number of wait
  2031. * states to be used by Fast Read x-y-z command.
  2032. */
  2033. u32 settings_dword;
  2034. u32 settings_shift;
  2035. /* The SPI protocol for this Fast Read x-y-z command. */
  2036. enum spi_nor_protocol proto;
  2037. };
  2038. static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
  2039. /* Fast Read 1-1-2 */
  2040. {
  2041. SNOR_HWCAPS_READ_1_1_2,
  2042. BFPT_DWORD(1), BIT(16), /* Supported bit */
  2043. BFPT_DWORD(4), 0, /* Settings */
  2044. SNOR_PROTO_1_1_2,
  2045. },
  2046. /* Fast Read 1-2-2 */
  2047. {
  2048. SNOR_HWCAPS_READ_1_2_2,
  2049. BFPT_DWORD(1), BIT(20), /* Supported bit */
  2050. BFPT_DWORD(4), 16, /* Settings */
  2051. SNOR_PROTO_1_2_2,
  2052. },
  2053. /* Fast Read 2-2-2 */
  2054. {
  2055. SNOR_HWCAPS_READ_2_2_2,
  2056. BFPT_DWORD(5), BIT(0), /* Supported bit */
  2057. BFPT_DWORD(6), 16, /* Settings */
  2058. SNOR_PROTO_2_2_2,
  2059. },
  2060. /* Fast Read 1-1-4 */
  2061. {
  2062. SNOR_HWCAPS_READ_1_1_4,
  2063. BFPT_DWORD(1), BIT(22), /* Supported bit */
  2064. BFPT_DWORD(3), 16, /* Settings */
  2065. SNOR_PROTO_1_1_4,
  2066. },
  2067. /* Fast Read 1-4-4 */
  2068. {
  2069. SNOR_HWCAPS_READ_1_4_4,
  2070. BFPT_DWORD(1), BIT(21), /* Supported bit */
  2071. BFPT_DWORD(3), 0, /* Settings */
  2072. SNOR_PROTO_1_4_4,
  2073. },
  2074. /* Fast Read 4-4-4 */
  2075. {
  2076. SNOR_HWCAPS_READ_4_4_4,
  2077. BFPT_DWORD(5), BIT(4), /* Supported bit */
  2078. BFPT_DWORD(7), 16, /* Settings */
  2079. SNOR_PROTO_4_4_4,
  2080. },
  2081. };
  2082. struct sfdp_bfpt_erase {
  2083. /*
  2084. * The half-word at offset <shift> in DWORD <dwoard> encodes the
  2085. * op code and erase sector size to be used by Sector Erase commands.
  2086. */
  2087. u32 dword;
  2088. u32 shift;
  2089. };
  2090. static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
  2091. /* Erase Type 1 in DWORD8 bits[15:0] */
  2092. {BFPT_DWORD(8), 0},
  2093. /* Erase Type 2 in DWORD8 bits[31:16] */
  2094. {BFPT_DWORD(8), 16},
  2095. /* Erase Type 3 in DWORD9 bits[15:0] */
  2096. {BFPT_DWORD(9), 0},
  2097. /* Erase Type 4 in DWORD9 bits[31:16] */
  2098. {BFPT_DWORD(9), 16},
  2099. };
  2100. static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
  2101. /**
  2102. * spi_nor_set_erase_type() - set a SPI NOR erase type
  2103. * @erase: pointer to a structure that describes a SPI NOR erase type
  2104. * @size: the size of the sector/block erased by the erase type
  2105. * @opcode: the SPI command op code to erase the sector/block
  2106. */
  2107. static void spi_nor_set_erase_type(struct spi_nor_erase_type *erase,
  2108. u32 size, u8 opcode)
  2109. {
  2110. erase->size = size;
  2111. erase->opcode = opcode;
  2112. /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
  2113. erase->size_shift = ffs(erase->size) - 1;
  2114. erase->size_mask = (1 << erase->size_shift) - 1;
  2115. }
  2116. /**
  2117. * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
  2118. * @erase: pointer to a structure that describes a SPI NOR erase type
  2119. * @size: the size of the sector/block erased by the erase type
  2120. * @opcode: the SPI command op code to erase the sector/block
  2121. * @i: erase type index as sorted in the Basic Flash Parameter Table
  2122. *
  2123. * The supported Erase Types will be sorted at init in ascending order, with
  2124. * the smallest Erase Type size being the first member in the erase_type array
  2125. * of the spi_nor_erase_map structure. Save the Erase Type index as sorted in
  2126. * the Basic Flash Parameter Table since it will be used later on to
  2127. * synchronize with the supported Erase Types defined in SFDP optional tables.
  2128. */
  2129. static void
  2130. spi_nor_set_erase_settings_from_bfpt(struct spi_nor_erase_type *erase,
  2131. u32 size, u8 opcode, u8 i)
  2132. {
  2133. erase->idx = i;
  2134. spi_nor_set_erase_type(erase, size, opcode);
  2135. }
  2136. /**
  2137. * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
  2138. * @l: member in the left half of the map's erase_type array
  2139. * @r: member in the right half of the map's erase_type array
  2140. *
  2141. * Comparison function used in the sort() call to sort in ascending order the
  2142. * map's erase types, the smallest erase type size being the first member in the
  2143. * sorted erase_type array.
  2144. *
  2145. * Return: the result of @l->size - @r->size
  2146. */
  2147. static int spi_nor_map_cmp_erase_type(const void *l, const void *r)
  2148. {
  2149. const struct spi_nor_erase_type *left = l, *right = r;
  2150. return left->size - right->size;
  2151. }
  2152. /**
  2153. * spi_nor_sort_erase_mask() - sort erase mask
  2154. * @map: the erase map of the SPI NOR
  2155. * @erase_mask: the erase type mask to be sorted
  2156. *
  2157. * Replicate the sort done for the map's erase types in BFPT: sort the erase
  2158. * mask in ascending order with the smallest erase type size starting from
  2159. * BIT(0) in the sorted erase mask.
  2160. *
  2161. * Return: sorted erase mask.
  2162. */
  2163. static u8 spi_nor_sort_erase_mask(struct spi_nor_erase_map *map, u8 erase_mask)
  2164. {
  2165. struct spi_nor_erase_type *erase_type = map->erase_type;
  2166. int i;
  2167. u8 sorted_erase_mask = 0;
  2168. if (!erase_mask)
  2169. return 0;
  2170. /* Replicate the sort done for the map's erase types. */
  2171. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
  2172. if (erase_type[i].size && erase_mask & BIT(erase_type[i].idx))
  2173. sorted_erase_mask |= BIT(i);
  2174. return sorted_erase_mask;
  2175. }
  2176. /**
  2177. * spi_nor_regions_sort_erase_types() - sort erase types in each region
  2178. * @map: the erase map of the SPI NOR
  2179. *
  2180. * Function assumes that the erase types defined in the erase map are already
  2181. * sorted in ascending order, with the smallest erase type size being the first
  2182. * member in the erase_type array. It replicates the sort done for the map's
  2183. * erase types. Each region's erase bitmask will indicate which erase types are
  2184. * supported from the sorted erase types defined in the erase map.
  2185. * Sort the all region's erase type at init in order to speed up the process of
  2186. * finding the best erase command at runtime.
  2187. */
  2188. static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
  2189. {
  2190. struct spi_nor_erase_region *region = map->regions;
  2191. u8 region_erase_mask, sorted_erase_mask;
  2192. while (region) {
  2193. region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
  2194. sorted_erase_mask = spi_nor_sort_erase_mask(map,
  2195. region_erase_mask);
  2196. /* Overwrite erase mask. */
  2197. region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) |
  2198. sorted_erase_mask;
  2199. region = spi_nor_region_next(region);
  2200. }
  2201. }
  2202. /**
  2203. * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
  2204. * @map: the erase map of the SPI NOR
  2205. * @erase_mask: bitmask encoding erase types that can erase the entire
  2206. * flash memory
  2207. * @flash_size: the spi nor flash memory size
  2208. */
  2209. static void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
  2210. u8 erase_mask, u64 flash_size)
  2211. {
  2212. /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
  2213. map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
  2214. SNOR_LAST_REGION;
  2215. map->uniform_region.size = flash_size;
  2216. map->regions = &map->uniform_region;
  2217. map->uniform_erase_type = erase_mask;
  2218. }
  2219. /**
  2220. * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
  2221. * @nor: pointer to a 'struct spi_nor'
  2222. * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
  2223. * the Basic Flash Parameter Table length and version
  2224. * @params: pointer to the 'struct spi_nor_flash_parameter' to be
  2225. * filled
  2226. *
  2227. * The Basic Flash Parameter Table is the main and only mandatory table as
  2228. * defined by the SFDP (JESD216) specification.
  2229. * It provides us with the total size (memory density) of the data array and
  2230. * the number of address bytes for Fast Read, Page Program and Sector Erase
  2231. * commands.
  2232. * For Fast READ commands, it also gives the number of mode clock cycles and
  2233. * wait states (regrouped in the number of dummy clock cycles) for each
  2234. * supported instruction op code.
  2235. * For Page Program, the page size is now available since JESD216 rev A, however
  2236. * the supported instruction op codes are still not provided.
  2237. * For Sector Erase commands, this table stores the supported instruction op
  2238. * codes and the associated sector sizes.
  2239. * Finally, the Quad Enable Requirements (QER) are also available since JESD216
  2240. * rev A. The QER bits encode the manufacturer dependent procedure to be
  2241. * executed to set the Quad Enable (QE) bit in some internal register of the
  2242. * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
  2243. * sending any Quad SPI command to the memory. Actually, setting the QE bit
  2244. * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
  2245. * and IO3 hence enabling 4 (Quad) I/O lines.
  2246. *
  2247. * Return: 0 on success, -errno otherwise.
  2248. */
  2249. static int spi_nor_parse_bfpt(struct spi_nor *nor,
  2250. const struct sfdp_parameter_header *bfpt_header,
  2251. struct spi_nor_flash_parameter *params)
  2252. {
  2253. struct spi_nor_erase_map *map = &nor->erase_map;
  2254. struct spi_nor_erase_type *erase_type = map->erase_type;
  2255. struct sfdp_bfpt bfpt;
  2256. size_t len;
  2257. int i, cmd, err;
  2258. u32 addr;
  2259. u16 half;
  2260. u8 erase_mask;
  2261. /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
  2262. if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
  2263. return -EINVAL;
  2264. /* Read the Basic Flash Parameter Table. */
  2265. len = min_t(size_t, sizeof(bfpt),
  2266. bfpt_header->length * sizeof(u32));
  2267. addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
  2268. memset(&bfpt, 0, sizeof(bfpt));
  2269. err = spi_nor_read_sfdp_dma_unsafe(nor, addr, len, &bfpt);
  2270. if (err < 0)
  2271. return err;
  2272. /* Fix endianness of the BFPT DWORDs. */
  2273. for (i = 0; i < BFPT_DWORD_MAX; i++)
  2274. bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
  2275. /* Number of address bytes. */
  2276. switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
  2277. case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
  2278. nor->addr_width = 3;
  2279. break;
  2280. case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
  2281. nor->addr_width = 4;
  2282. break;
  2283. default:
  2284. break;
  2285. }
  2286. /* Flash Memory Density (in bits). */
  2287. params->size = bfpt.dwords[BFPT_DWORD(2)];
  2288. if (params->size & BIT(31)) {
  2289. params->size &= ~BIT(31);
  2290. /*
  2291. * Prevent overflows on params->size. Anyway, a NOR of 2^64
  2292. * bits is unlikely to exist so this error probably means
  2293. * the BFPT we are reading is corrupted/wrong.
  2294. */
  2295. if (params->size > 63)
  2296. return -EINVAL;
  2297. params->size = 1ULL << params->size;
  2298. } else {
  2299. params->size++;
  2300. }
  2301. params->size >>= 3; /* Convert to bytes. */
  2302. /* Fast Read settings. */
  2303. for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
  2304. const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
  2305. struct spi_nor_read_command *read;
  2306. if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
  2307. params->hwcaps.mask &= ~rd->hwcaps;
  2308. continue;
  2309. }
  2310. params->hwcaps.mask |= rd->hwcaps;
  2311. cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
  2312. read = &params->reads[cmd];
  2313. half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
  2314. spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
  2315. }
  2316. /*
  2317. * Sector Erase settings. Reinitialize the uniform erase map using the
  2318. * Erase Types defined in the bfpt table.
  2319. */
  2320. erase_mask = 0;
  2321. memset(&nor->erase_map, 0, sizeof(nor->erase_map));
  2322. for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
  2323. const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
  2324. u32 erasesize;
  2325. u8 opcode;
  2326. half = bfpt.dwords[er->dword] >> er->shift;
  2327. erasesize = half & 0xff;
  2328. /* erasesize == 0 means this Erase Type is not supported. */
  2329. if (!erasesize)
  2330. continue;
  2331. erasesize = 1U << erasesize;
  2332. opcode = (half >> 8) & 0xff;
  2333. erase_mask |= BIT(i);
  2334. spi_nor_set_erase_settings_from_bfpt(&erase_type[i], erasesize,
  2335. opcode, i);
  2336. }
  2337. spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
  2338. /*
  2339. * Sort all the map's Erase Types in ascending order with the smallest
  2340. * erase size being the first member in the erase_type array.
  2341. */
  2342. sort(erase_type, SNOR_ERASE_TYPE_MAX, sizeof(erase_type[0]),
  2343. spi_nor_map_cmp_erase_type, NULL);
  2344. /*
  2345. * Sort the erase types in the uniform region in order to update the
  2346. * uniform_erase_type bitmask. The bitmask will be used later on when
  2347. * selecting the uniform erase.
  2348. */
  2349. spi_nor_regions_sort_erase_types(map);
  2350. map->uniform_erase_type = map->uniform_region.offset &
  2351. SNOR_ERASE_TYPE_MASK;
  2352. /* Stop here if not JESD216 rev A or later. */
  2353. if (bfpt_header->length < BFPT_DWORD_MAX)
  2354. return 0;
  2355. /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
  2356. params->page_size = bfpt.dwords[BFPT_DWORD(11)];
  2357. params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
  2358. params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
  2359. params->page_size = 1U << params->page_size;
  2360. /* Quad Enable Requirements. */
  2361. switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
  2362. case BFPT_DWORD15_QER_NONE:
  2363. params->quad_enable = NULL;
  2364. break;
  2365. case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
  2366. case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
  2367. params->quad_enable = spansion_no_read_cr_quad_enable;
  2368. break;
  2369. case BFPT_DWORD15_QER_SR1_BIT6:
  2370. params->quad_enable = macronix_quad_enable;
  2371. break;
  2372. case BFPT_DWORD15_QER_SR2_BIT7:
  2373. params->quad_enable = sr2_bit7_quad_enable;
  2374. break;
  2375. case BFPT_DWORD15_QER_SR2_BIT1:
  2376. params->quad_enable = spansion_read_cr_quad_enable;
  2377. break;
  2378. default:
  2379. return -EINVAL;
  2380. }
  2381. return 0;
  2382. }
  2383. #define SMPT_CMD_ADDRESS_LEN_MASK GENMASK(23, 22)
  2384. #define SMPT_CMD_ADDRESS_LEN_0 (0x0UL << 22)
  2385. #define SMPT_CMD_ADDRESS_LEN_3 (0x1UL << 22)
  2386. #define SMPT_CMD_ADDRESS_LEN_4 (0x2UL << 22)
  2387. #define SMPT_CMD_ADDRESS_LEN_USE_CURRENT (0x3UL << 22)
  2388. #define SMPT_CMD_READ_DUMMY_MASK GENMASK(19, 16)
  2389. #define SMPT_CMD_READ_DUMMY_SHIFT 16
  2390. #define SMPT_CMD_READ_DUMMY(_cmd) \
  2391. (((_cmd) & SMPT_CMD_READ_DUMMY_MASK) >> SMPT_CMD_READ_DUMMY_SHIFT)
  2392. #define SMPT_CMD_READ_DUMMY_IS_VARIABLE 0xfUL
  2393. #define SMPT_CMD_READ_DATA_MASK GENMASK(31, 24)
  2394. #define SMPT_CMD_READ_DATA_SHIFT 24
  2395. #define SMPT_CMD_READ_DATA(_cmd) \
  2396. (((_cmd) & SMPT_CMD_READ_DATA_MASK) >> SMPT_CMD_READ_DATA_SHIFT)
  2397. #define SMPT_CMD_OPCODE_MASK GENMASK(15, 8)
  2398. #define SMPT_CMD_OPCODE_SHIFT 8
  2399. #define SMPT_CMD_OPCODE(_cmd) \
  2400. (((_cmd) & SMPT_CMD_OPCODE_MASK) >> SMPT_CMD_OPCODE_SHIFT)
  2401. #define SMPT_MAP_REGION_COUNT_MASK GENMASK(23, 16)
  2402. #define SMPT_MAP_REGION_COUNT_SHIFT 16
  2403. #define SMPT_MAP_REGION_COUNT(_header) \
  2404. ((((_header) & SMPT_MAP_REGION_COUNT_MASK) >> \
  2405. SMPT_MAP_REGION_COUNT_SHIFT) + 1)
  2406. #define SMPT_MAP_ID_MASK GENMASK(15, 8)
  2407. #define SMPT_MAP_ID_SHIFT 8
  2408. #define SMPT_MAP_ID(_header) \
  2409. (((_header) & SMPT_MAP_ID_MASK) >> SMPT_MAP_ID_SHIFT)
  2410. #define SMPT_MAP_REGION_SIZE_MASK GENMASK(31, 8)
  2411. #define SMPT_MAP_REGION_SIZE_SHIFT 8
  2412. #define SMPT_MAP_REGION_SIZE(_region) \
  2413. (((((_region) & SMPT_MAP_REGION_SIZE_MASK) >> \
  2414. SMPT_MAP_REGION_SIZE_SHIFT) + 1) * 256)
  2415. #define SMPT_MAP_REGION_ERASE_TYPE_MASK GENMASK(3, 0)
  2416. #define SMPT_MAP_REGION_ERASE_TYPE(_region) \
  2417. ((_region) & SMPT_MAP_REGION_ERASE_TYPE_MASK)
  2418. #define SMPT_DESC_TYPE_MAP BIT(1)
  2419. #define SMPT_DESC_END BIT(0)
  2420. /**
  2421. * spi_nor_smpt_addr_width() - return the address width used in the
  2422. * configuration detection command.
  2423. * @nor: pointer to a 'struct spi_nor'
  2424. * @settings: configuration detection command descriptor, dword1
  2425. */
  2426. static u8 spi_nor_smpt_addr_width(const struct spi_nor *nor, const u32 settings)
  2427. {
  2428. switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
  2429. case SMPT_CMD_ADDRESS_LEN_0:
  2430. return 0;
  2431. case SMPT_CMD_ADDRESS_LEN_3:
  2432. return 3;
  2433. case SMPT_CMD_ADDRESS_LEN_4:
  2434. return 4;
  2435. case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
  2436. /* fall through */
  2437. default:
  2438. return nor->addr_width;
  2439. }
  2440. }
  2441. /**
  2442. * spi_nor_smpt_read_dummy() - return the configuration detection command read
  2443. * latency, in clock cycles.
  2444. * @nor: pointer to a 'struct spi_nor'
  2445. * @settings: configuration detection command descriptor, dword1
  2446. *
  2447. * Return: the number of dummy cycles for an SMPT read
  2448. */
  2449. static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
  2450. {
  2451. u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
  2452. if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE)
  2453. return nor->read_dummy;
  2454. return read_dummy;
  2455. }
  2456. /**
  2457. * spi_nor_get_map_in_use() - get the configuration map in use
  2458. * @nor: pointer to a 'struct spi_nor'
  2459. * @smpt: pointer to the sector map parameter table
  2460. * @smpt_len: sector map parameter table length
  2461. *
  2462. * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
  2463. */
  2464. static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
  2465. u8 smpt_len)
  2466. {
  2467. const u32 *ret;
  2468. u8 *buf;
  2469. u32 addr;
  2470. int err;
  2471. u8 i;
  2472. u8 addr_width, read_opcode, read_dummy;
  2473. u8 read_data_mask, map_id;
  2474. /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
  2475. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  2476. if (!buf)
  2477. return ERR_PTR(-ENOMEM);
  2478. addr_width = nor->addr_width;
  2479. read_dummy = nor->read_dummy;
  2480. read_opcode = nor->read_opcode;
  2481. map_id = 0;
  2482. /* Determine if there are any optional Detection Command Descriptors */
  2483. for (i = 0; i < smpt_len; i += 2) {
  2484. if (smpt[i] & SMPT_DESC_TYPE_MAP)
  2485. break;
  2486. read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
  2487. nor->addr_width = spi_nor_smpt_addr_width(nor, smpt[i]);
  2488. nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
  2489. nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
  2490. addr = smpt[i + 1];
  2491. err = spi_nor_read_raw(nor, addr, 1, buf);
  2492. if (err) {
  2493. ret = ERR_PTR(err);
  2494. goto out;
  2495. }
  2496. /*
  2497. * Build an index value that is used to select the Sector Map
  2498. * Configuration that is currently in use.
  2499. */
  2500. map_id = map_id << 1 | !!(*buf & read_data_mask);
  2501. }
  2502. /*
  2503. * If command descriptors are provided, they always precede map
  2504. * descriptors in the table. There is no need to start the iteration
  2505. * over smpt array all over again.
  2506. *
  2507. * Find the matching configuration map.
  2508. */
  2509. ret = ERR_PTR(-EINVAL);
  2510. while (i < smpt_len) {
  2511. if (SMPT_MAP_ID(smpt[i]) == map_id) {
  2512. ret = smpt + i;
  2513. break;
  2514. }
  2515. /*
  2516. * If there are no more configuration map descriptors and no
  2517. * configuration ID matched the configuration identifier, the
  2518. * sector address map is unknown.
  2519. */
  2520. if (smpt[i] & SMPT_DESC_END)
  2521. break;
  2522. /* increment the table index to the next map */
  2523. i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
  2524. }
  2525. /* fall through */
  2526. out:
  2527. kfree(buf);
  2528. nor->addr_width = addr_width;
  2529. nor->read_dummy = read_dummy;
  2530. nor->read_opcode = read_opcode;
  2531. return ret;
  2532. }
  2533. /**
  2534. * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
  2535. * @region: pointer to a structure that describes a SPI NOR erase region
  2536. * @erase: pointer to a structure that describes a SPI NOR erase type
  2537. * @erase_type: erase type bitmask
  2538. */
  2539. static void
  2540. spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
  2541. const struct spi_nor_erase_type *erase,
  2542. const u8 erase_type)
  2543. {
  2544. int i;
  2545. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  2546. if (!(erase_type & BIT(i)))
  2547. continue;
  2548. if (region->size & erase[i].size_mask) {
  2549. spi_nor_region_mark_overlay(region);
  2550. return;
  2551. }
  2552. }
  2553. }
  2554. /**
  2555. * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
  2556. * @nor: pointer to a 'struct spi_nor'
  2557. * @smpt: pointer to the sector map parameter table
  2558. *
  2559. * Return: 0 on success, -errno otherwise.
  2560. */
  2561. static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
  2562. const u32 *smpt)
  2563. {
  2564. struct spi_nor_erase_map *map = &nor->erase_map;
  2565. struct spi_nor_erase_type *erase = map->erase_type;
  2566. struct spi_nor_erase_region *region;
  2567. u64 offset;
  2568. u32 region_count;
  2569. int i, j;
  2570. u8 uniform_erase_type, save_uniform_erase_type;
  2571. u8 erase_type, regions_erase_type;
  2572. region_count = SMPT_MAP_REGION_COUNT(*smpt);
  2573. /*
  2574. * The regions will be freed when the driver detaches from the
  2575. * device.
  2576. */
  2577. region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
  2578. GFP_KERNEL);
  2579. if (!region)
  2580. return -ENOMEM;
  2581. map->regions = region;
  2582. uniform_erase_type = 0xff;
  2583. regions_erase_type = 0;
  2584. offset = 0;
  2585. /* Populate regions. */
  2586. for (i = 0; i < region_count; i++) {
  2587. j = i + 1; /* index for the region dword */
  2588. region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
  2589. erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
  2590. region[i].offset = offset | erase_type;
  2591. spi_nor_region_check_overlay(&region[i], erase, erase_type);
  2592. /*
  2593. * Save the erase types that are supported in all regions and
  2594. * can erase the entire flash memory.
  2595. */
  2596. uniform_erase_type &= erase_type;
  2597. /*
  2598. * regions_erase_type mask will indicate all the erase types
  2599. * supported in this configuration map.
  2600. */
  2601. regions_erase_type |= erase_type;
  2602. offset = (region[i].offset & ~SNOR_ERASE_FLAGS_MASK) +
  2603. region[i].size;
  2604. }
  2605. save_uniform_erase_type = map->uniform_erase_type;
  2606. map->uniform_erase_type = spi_nor_sort_erase_mask(map,
  2607. uniform_erase_type);
  2608. if (!regions_erase_type) {
  2609. /*
  2610. * Roll back to the previous uniform_erase_type mask, SMPT is
  2611. * broken.
  2612. */
  2613. map->uniform_erase_type = save_uniform_erase_type;
  2614. return -EINVAL;
  2615. }
  2616. /*
  2617. * BFPT advertises all the erase types supported by all the possible
  2618. * map configurations. Mask out the erase types that are not supported
  2619. * by the current map configuration.
  2620. */
  2621. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
  2622. if (!(regions_erase_type & BIT(erase[i].idx)))
  2623. spi_nor_set_erase_type(&erase[i], 0, 0xFF);
  2624. spi_nor_region_mark_end(&region[i - 1]);
  2625. return 0;
  2626. }
  2627. /**
  2628. * spi_nor_parse_smpt() - parse Sector Map Parameter Table
  2629. * @nor: pointer to a 'struct spi_nor'
  2630. * @smpt_header: sector map parameter table header
  2631. *
  2632. * This table is optional, but when available, we parse it to identify the
  2633. * location and size of sectors within the main data array of the flash memory
  2634. * device and to identify which Erase Types are supported by each sector.
  2635. *
  2636. * Return: 0 on success, -errno otherwise.
  2637. */
  2638. static int spi_nor_parse_smpt(struct spi_nor *nor,
  2639. const struct sfdp_parameter_header *smpt_header)
  2640. {
  2641. const u32 *sector_map;
  2642. u32 *smpt;
  2643. size_t len;
  2644. u32 addr;
  2645. int i, ret;
  2646. /* Read the Sector Map Parameter Table. */
  2647. len = smpt_header->length * sizeof(*smpt);
  2648. smpt = kzalloc(len, GFP_KERNEL);
  2649. if (!smpt)
  2650. return -ENOMEM;
  2651. addr = SFDP_PARAM_HEADER_PTP(smpt_header);
  2652. ret = spi_nor_read_sfdp(nor, addr, len, smpt);
  2653. if (ret)
  2654. goto out;
  2655. /* Fix endianness of the SMPT DWORDs. */
  2656. for (i = 0; i < smpt_header->length; i++)
  2657. smpt[i] = le32_to_cpu(smpt[i]);
  2658. sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
  2659. if (IS_ERR(sector_map)) {
  2660. ret = PTR_ERR(sector_map);
  2661. goto out;
  2662. }
  2663. ret = spi_nor_init_non_uniform_erase_map(nor, sector_map);
  2664. if (ret)
  2665. goto out;
  2666. spi_nor_regions_sort_erase_types(&nor->erase_map);
  2667. /* fall through */
  2668. out:
  2669. kfree(smpt);
  2670. return ret;
  2671. }
  2672. /**
  2673. * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
  2674. * @nor: pointer to a 'struct spi_nor'
  2675. * @params: pointer to the 'struct spi_nor_flash_parameter' to be
  2676. * filled
  2677. *
  2678. * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
  2679. * specification. This is a standard which tends to supported by almost all
  2680. * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
  2681. * runtime the main parameters needed to perform basic SPI flash operations such
  2682. * as Fast Read, Page Program or Sector Erase commands.
  2683. *
  2684. * Return: 0 on success, -errno otherwise.
  2685. */
  2686. static int spi_nor_parse_sfdp(struct spi_nor *nor,
  2687. struct spi_nor_flash_parameter *params)
  2688. {
  2689. const struct sfdp_parameter_header *param_header, *bfpt_header;
  2690. struct sfdp_parameter_header *param_headers = NULL;
  2691. struct sfdp_header header;
  2692. struct device *dev = nor->dev;
  2693. size_t psize;
  2694. int i, err;
  2695. /* Get the SFDP header. */
  2696. err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
  2697. if (err < 0)
  2698. return err;
  2699. /* Check the SFDP header version. */
  2700. if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
  2701. header.major != SFDP_JESD216_MAJOR)
  2702. return -EINVAL;
  2703. /*
  2704. * Verify that the first and only mandatory parameter header is a
  2705. * Basic Flash Parameter Table header as specified in JESD216.
  2706. */
  2707. bfpt_header = &header.bfpt_header;
  2708. if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
  2709. bfpt_header->major != SFDP_JESD216_MAJOR)
  2710. return -EINVAL;
  2711. /*
  2712. * Allocate memory then read all parameter headers with a single
  2713. * Read SFDP command. These parameter headers will actually be parsed
  2714. * twice: a first time to get the latest revision of the basic flash
  2715. * parameter table, then a second time to handle the supported optional
  2716. * tables.
  2717. * Hence we read the parameter headers once for all to reduce the
  2718. * processing time. Also we use kmalloc() instead of devm_kmalloc()
  2719. * because we don't need to keep these parameter headers: the allocated
  2720. * memory is always released with kfree() before exiting this function.
  2721. */
  2722. if (header.nph) {
  2723. psize = header.nph * sizeof(*param_headers);
  2724. param_headers = kmalloc(psize, GFP_KERNEL);
  2725. if (!param_headers)
  2726. return -ENOMEM;
  2727. err = spi_nor_read_sfdp(nor, sizeof(header),
  2728. psize, param_headers);
  2729. if (err < 0) {
  2730. dev_err(dev, "failed to read SFDP parameter headers\n");
  2731. goto exit;
  2732. }
  2733. }
  2734. /*
  2735. * Check other parameter headers to get the latest revision of
  2736. * the basic flash parameter table.
  2737. */
  2738. for (i = 0; i < header.nph; i++) {
  2739. param_header = &param_headers[i];
  2740. if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
  2741. param_header->major == SFDP_JESD216_MAJOR &&
  2742. (param_header->minor > bfpt_header->minor ||
  2743. (param_header->minor == bfpt_header->minor &&
  2744. param_header->length > bfpt_header->length)))
  2745. bfpt_header = param_header;
  2746. }
  2747. err = spi_nor_parse_bfpt(nor, bfpt_header, params);
  2748. if (err)
  2749. goto exit;
  2750. /* Parse optional parameter tables. */
  2751. for (i = 0; i < header.nph; i++) {
  2752. param_header = &param_headers[i];
  2753. switch (SFDP_PARAM_HEADER_ID(param_header)) {
  2754. case SFDP_SECTOR_MAP_ID:
  2755. err = spi_nor_parse_smpt(nor, param_header);
  2756. break;
  2757. default:
  2758. break;
  2759. }
  2760. if (err) {
  2761. dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
  2762. SFDP_PARAM_HEADER_ID(param_header));
  2763. /*
  2764. * Let's not drop all information we extracted so far
  2765. * if optional table parsers fail. In case of failing,
  2766. * each optional parser is responsible to roll back to
  2767. * the previously known spi_nor data.
  2768. */
  2769. err = 0;
  2770. }
  2771. }
  2772. exit:
  2773. kfree(param_headers);
  2774. return err;
  2775. }
  2776. static int spi_nor_init_params(struct spi_nor *nor,
  2777. const struct flash_info *info,
  2778. struct spi_nor_flash_parameter *params)
  2779. {
  2780. struct spi_nor_erase_map *map = &nor->erase_map;
  2781. u8 i, erase_mask;
  2782. /* Set legacy flash parameters as default. */
  2783. memset(params, 0, sizeof(*params));
  2784. /* Set SPI NOR sizes. */
  2785. params->size = info->sector_size * info->n_sectors;
  2786. params->page_size = info->page_size;
  2787. /* (Fast) Read settings. */
  2788. params->hwcaps.mask |= SNOR_HWCAPS_READ;
  2789. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
  2790. 0, 0, SPINOR_OP_READ,
  2791. SNOR_PROTO_1_1_1);
  2792. if (!(info->flags & SPI_NOR_NO_FR)) {
  2793. params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  2794. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
  2795. 0, 8, SPINOR_OP_READ_FAST,
  2796. SNOR_PROTO_1_1_1);
  2797. }
  2798. if (info->flags & SPI_NOR_DUAL_READ) {
  2799. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  2800. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
  2801. 0, 8, SPINOR_OP_READ_1_1_2,
  2802. SNOR_PROTO_1_1_2);
  2803. }
  2804. if (info->flags & SPI_NOR_QUAD_READ) {
  2805. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  2806. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
  2807. 0, 8, SPINOR_OP_READ_1_1_4,
  2808. SNOR_PROTO_1_1_4);
  2809. }
  2810. /* Page Program settings. */
  2811. params->hwcaps.mask |= SNOR_HWCAPS_PP;
  2812. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
  2813. SPINOR_OP_PP, SNOR_PROTO_1_1_1);
  2814. /*
  2815. * Sector Erase settings. Sort Erase Types in ascending order, with the
  2816. * smallest erase size starting at BIT(0).
  2817. */
  2818. erase_mask = 0;
  2819. i = 0;
  2820. if (info->flags & SECT_4K_PMC) {
  2821. erase_mask |= BIT(i);
  2822. spi_nor_set_erase_type(&map->erase_type[i], 4096u,
  2823. SPINOR_OP_BE_4K_PMC);
  2824. i++;
  2825. } else if (info->flags & SECT_4K) {
  2826. erase_mask |= BIT(i);
  2827. spi_nor_set_erase_type(&map->erase_type[i], 4096u,
  2828. SPINOR_OP_BE_4K);
  2829. i++;
  2830. }
  2831. erase_mask |= BIT(i);
  2832. spi_nor_set_erase_type(&map->erase_type[i], info->sector_size,
  2833. SPINOR_OP_SE);
  2834. spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
  2835. /* Select the procedure to set the Quad Enable bit. */
  2836. if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
  2837. SNOR_HWCAPS_PP_QUAD)) {
  2838. switch (JEDEC_MFR(info)) {
  2839. case SNOR_MFR_MACRONIX:
  2840. params->quad_enable = macronix_quad_enable;
  2841. break;
  2842. case SNOR_MFR_MICRON:
  2843. break;
  2844. default:
  2845. /* Kept only for backward compatibility purpose. */
  2846. params->quad_enable = spansion_quad_enable;
  2847. break;
  2848. }
  2849. /*
  2850. * Some manufacturer like GigaDevice may use different
  2851. * bit to set QE on different memories, so the MFR can't
  2852. * indicate the quad_enable method for this case, we need
  2853. * set it in flash info list.
  2854. */
  2855. if (info->quad_enable)
  2856. params->quad_enable = info->quad_enable;
  2857. }
  2858. if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
  2859. !(info->flags & SPI_NOR_SKIP_SFDP)) {
  2860. struct spi_nor_flash_parameter sfdp_params;
  2861. struct spi_nor_erase_map prev_map;
  2862. memcpy(&sfdp_params, params, sizeof(sfdp_params));
  2863. memcpy(&prev_map, &nor->erase_map, sizeof(prev_map));
  2864. if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
  2865. nor->addr_width = 0;
  2866. /* restore previous erase map */
  2867. memcpy(&nor->erase_map, &prev_map,
  2868. sizeof(nor->erase_map));
  2869. } else {
  2870. memcpy(params, &sfdp_params, sizeof(*params));
  2871. }
  2872. }
  2873. return 0;
  2874. }
  2875. static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
  2876. {
  2877. size_t i;
  2878. for (i = 0; i < size; i++)
  2879. if (table[i][0] == (int)hwcaps)
  2880. return table[i][1];
  2881. return -EINVAL;
  2882. }
  2883. static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
  2884. {
  2885. static const int hwcaps_read2cmd[][2] = {
  2886. { SNOR_HWCAPS_READ, SNOR_CMD_READ },
  2887. { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
  2888. { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
  2889. { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
  2890. { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
  2891. { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
  2892. { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
  2893. { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
  2894. { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
  2895. { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
  2896. { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
  2897. { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
  2898. { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
  2899. { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
  2900. { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
  2901. };
  2902. return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
  2903. ARRAY_SIZE(hwcaps_read2cmd));
  2904. }
  2905. static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
  2906. {
  2907. static const int hwcaps_pp2cmd[][2] = {
  2908. { SNOR_HWCAPS_PP, SNOR_CMD_PP },
  2909. { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
  2910. { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
  2911. { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
  2912. { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
  2913. { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
  2914. { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
  2915. };
  2916. return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
  2917. ARRAY_SIZE(hwcaps_pp2cmd));
  2918. }
  2919. static int spi_nor_select_read(struct spi_nor *nor,
  2920. const struct spi_nor_flash_parameter *params,
  2921. u32 shared_hwcaps)
  2922. {
  2923. int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
  2924. const struct spi_nor_read_command *read;
  2925. if (best_match < 0)
  2926. return -EINVAL;
  2927. cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
  2928. if (cmd < 0)
  2929. return -EINVAL;
  2930. read = &params->reads[cmd];
  2931. nor->read_opcode = read->opcode;
  2932. nor->read_proto = read->proto;
  2933. /*
  2934. * In the spi-nor framework, we don't need to make the difference
  2935. * between mode clock cycles and wait state clock cycles.
  2936. * Indeed, the value of the mode clock cycles is used by a QSPI
  2937. * flash memory to know whether it should enter or leave its 0-4-4
  2938. * (Continuous Read / XIP) mode.
  2939. * eXecution In Place is out of the scope of the mtd sub-system.
  2940. * Hence we choose to merge both mode and wait state clock cycles
  2941. * into the so called dummy clock cycles.
  2942. */
  2943. nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
  2944. return 0;
  2945. }
  2946. static int spi_nor_select_pp(struct spi_nor *nor,
  2947. const struct spi_nor_flash_parameter *params,
  2948. u32 shared_hwcaps)
  2949. {
  2950. int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
  2951. const struct spi_nor_pp_command *pp;
  2952. if (best_match < 0)
  2953. return -EINVAL;
  2954. cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
  2955. if (cmd < 0)
  2956. return -EINVAL;
  2957. pp = &params->page_programs[cmd];
  2958. nor->program_opcode = pp->opcode;
  2959. nor->write_proto = pp->proto;
  2960. return 0;
  2961. }
  2962. /**
  2963. * spi_nor_select_uniform_erase() - select optimum uniform erase type
  2964. * @map: the erase map of the SPI NOR
  2965. * @wanted_size: the erase type size to search for. Contains the value of
  2966. * info->sector_size or of the "small sector" size in case
  2967. * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
  2968. *
  2969. * Once the optimum uniform sector erase command is found, disable all the
  2970. * other.
  2971. *
  2972. * Return: pointer to erase type on success, NULL otherwise.
  2973. */
  2974. static const struct spi_nor_erase_type *
  2975. spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
  2976. const u32 wanted_size)
  2977. {
  2978. const struct spi_nor_erase_type *tested_erase, *erase = NULL;
  2979. int i;
  2980. u8 uniform_erase_type = map->uniform_erase_type;
  2981. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  2982. if (!(uniform_erase_type & BIT(i)))
  2983. continue;
  2984. tested_erase = &map->erase_type[i];
  2985. /*
  2986. * If the current erase size is the one, stop here:
  2987. * we have found the right uniform Sector Erase command.
  2988. */
  2989. if (tested_erase->size == wanted_size) {
  2990. erase = tested_erase;
  2991. break;
  2992. }
  2993. /*
  2994. * Otherwise, the current erase size is still a valid canditate.
  2995. * Select the biggest valid candidate.
  2996. */
  2997. if (!erase && tested_erase->size)
  2998. erase = tested_erase;
  2999. /* keep iterating to find the wanted_size */
  3000. }
  3001. if (!erase)
  3002. return NULL;
  3003. /* Disable all other Sector Erase commands. */
  3004. map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
  3005. map->uniform_erase_type |= BIT(erase - map->erase_type);
  3006. return erase;
  3007. }
  3008. static int spi_nor_select_erase(struct spi_nor *nor, u32 wanted_size)
  3009. {
  3010. struct spi_nor_erase_map *map = &nor->erase_map;
  3011. const struct spi_nor_erase_type *erase = NULL;
  3012. struct mtd_info *mtd = &nor->mtd;
  3013. int i;
  3014. /*
  3015. * The previous implementation handling Sector Erase commands assumed
  3016. * that the SPI flash memory has an uniform layout then used only one
  3017. * of the supported erase sizes for all Sector Erase commands.
  3018. * So to be backward compatible, the new implementation also tries to
  3019. * manage the SPI flash memory as uniform with a single erase sector
  3020. * size, when possible.
  3021. */
  3022. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  3023. /* prefer "small sector" erase if possible */
  3024. wanted_size = 4096u;
  3025. #endif
  3026. if (spi_nor_has_uniform_erase(nor)) {
  3027. erase = spi_nor_select_uniform_erase(map, wanted_size);
  3028. if (!erase)
  3029. return -EINVAL;
  3030. nor->erase_opcode = erase->opcode;
  3031. mtd->erasesize = erase->size;
  3032. return 0;
  3033. }
  3034. /*
  3035. * For non-uniform SPI flash memory, set mtd->erasesize to the
  3036. * maximum erase sector size. No need to set nor->erase_opcode.
  3037. */
  3038. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  3039. if (map->erase_type[i].size) {
  3040. erase = &map->erase_type[i];
  3041. break;
  3042. }
  3043. }
  3044. if (!erase)
  3045. return -EINVAL;
  3046. mtd->erasesize = erase->size;
  3047. return 0;
  3048. }
  3049. static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
  3050. const struct spi_nor_flash_parameter *params,
  3051. const struct spi_nor_hwcaps *hwcaps)
  3052. {
  3053. u32 ignored_mask, shared_mask;
  3054. bool enable_quad_io;
  3055. int err;
  3056. /*
  3057. * Keep only the hardware capabilities supported by both the SPI
  3058. * controller and the SPI flash memory.
  3059. */
  3060. shared_mask = hwcaps->mask & params->hwcaps.mask;
  3061. /* SPI n-n-n protocols are not supported yet. */
  3062. ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
  3063. SNOR_HWCAPS_READ_4_4_4 |
  3064. SNOR_HWCAPS_READ_8_8_8 |
  3065. SNOR_HWCAPS_PP_4_4_4 |
  3066. SNOR_HWCAPS_PP_8_8_8);
  3067. if (shared_mask & ignored_mask) {
  3068. dev_dbg(nor->dev,
  3069. "SPI n-n-n protocols are not supported yet.\n");
  3070. shared_mask &= ~ignored_mask;
  3071. }
  3072. /* Select the (Fast) Read command. */
  3073. err = spi_nor_select_read(nor, params, shared_mask);
  3074. if (err) {
  3075. dev_err(nor->dev,
  3076. "can't select read settings supported by both the SPI controller and memory.\n");
  3077. return err;
  3078. }
  3079. /* Select the Page Program command. */
  3080. err = spi_nor_select_pp(nor, params, shared_mask);
  3081. if (err) {
  3082. dev_err(nor->dev,
  3083. "can't select write settings supported by both the SPI controller and memory.\n");
  3084. return err;
  3085. }
  3086. /* Select the Sector Erase command. */
  3087. err = spi_nor_select_erase(nor, info->sector_size);
  3088. if (err) {
  3089. dev_err(nor->dev,
  3090. "can't select erase settings supported by both the SPI controller and memory.\n");
  3091. return err;
  3092. }
  3093. /* Enable Quad I/O if needed. */
  3094. enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
  3095. spi_nor_get_protocol_width(nor->write_proto) == 4);
  3096. if (enable_quad_io && params->quad_enable)
  3097. nor->quad_enable = params->quad_enable;
  3098. else
  3099. nor->quad_enable = NULL;
  3100. return 0;
  3101. }
  3102. static int spi_nor_init(struct spi_nor *nor)
  3103. {
  3104. int err;
  3105. /*
  3106. * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
  3107. * with the software protection bits set
  3108. */
  3109. if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
  3110. JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
  3111. JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
  3112. nor->info->flags & SPI_NOR_HAS_LOCK) {
  3113. write_enable(nor);
  3114. write_sr(nor, 0);
  3115. spi_nor_wait_till_ready(nor);
  3116. }
  3117. if (nor->quad_enable) {
  3118. err = nor->quad_enable(nor);
  3119. if (err) {
  3120. dev_err(nor->dev, "quad mode not supported\n");
  3121. return err;
  3122. }
  3123. }
  3124. if ((nor->addr_width == 4) &&
  3125. (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
  3126. !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
  3127. /*
  3128. * If the RESET# pin isn't hooked up properly, or the system
  3129. * otherwise doesn't perform a reset command in the boot
  3130. * sequence, it's impossible to 100% protect against unexpected
  3131. * reboots (e.g., crashes). Warn the user (or hopefully, system
  3132. * designer) that this is bad.
  3133. */
  3134. WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
  3135. "enabling reset hack; may not recover from unexpected reboots\n");
  3136. set_4byte(nor, nor->info, 1);
  3137. }
  3138. return 0;
  3139. }
  3140. /* mtd resume handler */
  3141. static void spi_nor_resume(struct mtd_info *mtd)
  3142. {
  3143. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  3144. struct device *dev = nor->dev;
  3145. int ret;
  3146. /* re-initialize the nor chip */
  3147. ret = spi_nor_init(nor);
  3148. if (ret)
  3149. dev_err(dev, "resume() failed\n");
  3150. }
  3151. void spi_nor_restore(struct spi_nor *nor)
  3152. {
  3153. /* restore the addressing mode */
  3154. if ((nor->addr_width == 4) &&
  3155. (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
  3156. !(nor->info->flags & SPI_NOR_4B_OPCODES) &&
  3157. (nor->flags & SNOR_F_BROKEN_RESET))
  3158. set_4byte(nor, nor->info, 0);
  3159. }
  3160. EXPORT_SYMBOL_GPL(spi_nor_restore);
  3161. int spi_nor_scan(struct spi_nor *nor, const char *name,
  3162. const struct spi_nor_hwcaps *hwcaps)
  3163. {
  3164. struct spi_nor_flash_parameter params;
  3165. const struct flash_info *info = NULL;
  3166. struct device *dev = nor->dev;
  3167. struct mtd_info *mtd = &nor->mtd;
  3168. struct device_node *np = spi_nor_get_flash_node(nor);
  3169. int ret;
  3170. int i;
  3171. ret = spi_nor_check(nor);
  3172. if (ret)
  3173. return ret;
  3174. /* Reset SPI protocol for all commands. */
  3175. nor->reg_proto = SNOR_PROTO_1_1_1;
  3176. nor->read_proto = SNOR_PROTO_1_1_1;
  3177. nor->write_proto = SNOR_PROTO_1_1_1;
  3178. if (name)
  3179. info = spi_nor_match_id(name);
  3180. /* Try to auto-detect if chip name wasn't specified or not found */
  3181. if (!info)
  3182. info = spi_nor_read_id(nor);
  3183. if (IS_ERR_OR_NULL(info))
  3184. return -ENOENT;
  3185. /*
  3186. * If caller has specified name of flash model that can normally be
  3187. * detected using JEDEC, let's verify it.
  3188. */
  3189. if (name && info->id_len) {
  3190. const struct flash_info *jinfo;
  3191. jinfo = spi_nor_read_id(nor);
  3192. if (IS_ERR(jinfo)) {
  3193. return PTR_ERR(jinfo);
  3194. } else if (jinfo != info) {
  3195. /*
  3196. * JEDEC knows better, so overwrite platform ID. We
  3197. * can't trust partitions any longer, but we'll let
  3198. * mtd apply them anyway, since some partitions may be
  3199. * marked read-only, and we don't want to lose that
  3200. * information, even if it's not 100% accurate.
  3201. */
  3202. dev_warn(dev, "found %s, expected %s\n",
  3203. jinfo->name, info->name);
  3204. info = jinfo;
  3205. }
  3206. }
  3207. mutex_init(&nor->lock);
  3208. /*
  3209. * Make sure the XSR_RDY flag is set before calling
  3210. * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
  3211. * with Atmel spi-nor
  3212. */
  3213. if (info->flags & SPI_S3AN)
  3214. nor->flags |= SNOR_F_READY_XSR_RDY;
  3215. /* Parse the Serial Flash Discoverable Parameters table. */
  3216. ret = spi_nor_init_params(nor, info, &params);
  3217. if (ret)
  3218. return ret;
  3219. if (!mtd->name)
  3220. mtd->name = dev_name(dev);
  3221. mtd->priv = nor;
  3222. mtd->type = MTD_NORFLASH;
  3223. mtd->writesize = 1;
  3224. mtd->flags = MTD_CAP_NORFLASH;
  3225. mtd->size = params.size;
  3226. mtd->_erase = spi_nor_erase;
  3227. mtd->_read = spi_nor_read;
  3228. mtd->_resume = spi_nor_resume;
  3229. /* NOR protection support for STmicro/Micron chips and similar */
  3230. if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
  3231. info->flags & SPI_NOR_HAS_LOCK) {
  3232. nor->flash_lock = stm_lock;
  3233. nor->flash_unlock = stm_unlock;
  3234. nor->flash_is_locked = stm_is_locked;
  3235. }
  3236. if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
  3237. mtd->_lock = spi_nor_lock;
  3238. mtd->_unlock = spi_nor_unlock;
  3239. mtd->_is_locked = spi_nor_is_locked;
  3240. }
  3241. /* sst nor chips use AAI word program */
  3242. if (info->flags & SST_WRITE)
  3243. mtd->_write = sst_write;
  3244. else
  3245. mtd->_write = spi_nor_write;
  3246. if (info->flags & USE_FSR)
  3247. nor->flags |= SNOR_F_USE_FSR;
  3248. if (info->flags & SPI_NOR_HAS_TB)
  3249. nor->flags |= SNOR_F_HAS_SR_TB;
  3250. if (info->flags & NO_CHIP_ERASE)
  3251. nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
  3252. if (info->flags & USE_CLSR)
  3253. nor->flags |= SNOR_F_USE_CLSR;
  3254. if (info->flags & SPI_NOR_NO_ERASE)
  3255. mtd->flags |= MTD_NO_ERASE;
  3256. mtd->dev.parent = dev;
  3257. nor->page_size = params.page_size;
  3258. mtd->writebufsize = nor->page_size;
  3259. if (np) {
  3260. /* If we were instantiated by DT, use it */
  3261. if (of_property_read_bool(np, "m25p,fast-read"))
  3262. params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  3263. else
  3264. params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
  3265. } else {
  3266. /* If we weren't instantiated by DT, default to fast-read */
  3267. params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  3268. }
  3269. if (of_property_read_bool(np, "broken-flash-reset"))
  3270. nor->flags |= SNOR_F_BROKEN_RESET;
  3271. /* Some devices cannot do fast-read, no matter what DT tells us */
  3272. if (info->flags & SPI_NOR_NO_FR)
  3273. params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
  3274. /*
  3275. * Configure the SPI memory:
  3276. * - select op codes for (Fast) Read, Page Program and Sector Erase.
  3277. * - set the number of dummy cycles (mode cycles + wait states).
  3278. * - set the SPI protocols for register and memory accesses.
  3279. * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
  3280. */
  3281. ret = spi_nor_setup(nor, info, &params, hwcaps);
  3282. if (ret)
  3283. return ret;
  3284. if (nor->addr_width) {
  3285. /* already configured from SFDP */
  3286. } else if (info->addr_width) {
  3287. nor->addr_width = info->addr_width;
  3288. } else if (mtd->size > 0x1000000) {
  3289. /* enable 4-byte addressing if the device exceeds 16MiB */
  3290. nor->addr_width = 4;
  3291. if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
  3292. info->flags & SPI_NOR_4B_OPCODES)
  3293. spi_nor_set_4byte_opcodes(nor, info);
  3294. } else {
  3295. nor->addr_width = 3;
  3296. }
  3297. if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  3298. dev_err(dev, "address width is too large: %u\n",
  3299. nor->addr_width);
  3300. return -EINVAL;
  3301. }
  3302. if (info->flags & SPI_S3AN) {
  3303. ret = s3an_nor_scan(info, nor);
  3304. if (ret)
  3305. return ret;
  3306. }
  3307. /* Send all the required SPI flash commands to initialize device */
  3308. nor->info = info;
  3309. ret = spi_nor_init(nor);
  3310. if (ret)
  3311. return ret;
  3312. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  3313. (long long)mtd->size >> 10);
  3314. dev_dbg(dev,
  3315. "mtd .name = %s, .size = 0x%llx (%lldMiB), "
  3316. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  3317. mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
  3318. mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
  3319. if (mtd->numeraseregions)
  3320. for (i = 0; i < mtd->numeraseregions; i++)
  3321. dev_dbg(dev,
  3322. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  3323. ".erasesize = 0x%.8x (%uKiB), "
  3324. ".numblocks = %d }\n",
  3325. i, (long long)mtd->eraseregions[i].offset,
  3326. mtd->eraseregions[i].erasesize,
  3327. mtd->eraseregions[i].erasesize / 1024,
  3328. mtd->eraseregions[i].numblocks);
  3329. return 0;
  3330. }
  3331. EXPORT_SYMBOL_GPL(spi_nor_scan);
  3332. static const struct flash_info *spi_nor_match_id(const char *name)
  3333. {
  3334. const struct flash_info *id = spi_nor_ids;
  3335. while (id->name) {
  3336. if (!strcmp(name, id->name))
  3337. return id;
  3338. id++;
  3339. }
  3340. return NULL;
  3341. }
  3342. MODULE_LICENSE("GPL");
  3343. MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
  3344. MODULE_AUTHOR("Mike Lavender");
  3345. MODULE_DESCRIPTION("framework for SPI NOR");