core.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
  4. * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
  5. *
  6. * Copyright (C) 2005, Intec Automation Inc.
  7. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  8. */
  9. #include <linux/cleanup.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/math64.h>
  15. #include <linux/module.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/spi-nor.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/sched/task_stack.h>
  22. #include <linux/sizes.h>
  23. #include <linux/slab.h>
  24. #include <linux/spi/flash.h>
  25. #include "core.h"
  26. /* Define max times to check status register before we give up. */
  27. /*
  28. * For everything but full-chip erase; probably could be much smaller, but kept
  29. * around for safety for now
  30. */
  31. #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
  32. /*
  33. * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
  34. * for larger flash
  35. */
  36. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  37. #define SPI_NOR_MAX_ADDR_NBYTES 4
  38. #define SPI_NOR_SRST_SLEEP_MIN 200
  39. #define SPI_NOR_SRST_SLEEP_MAX 400
  40. /**
  41. * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
  42. * extension type.
  43. * @nor: pointer to a 'struct spi_nor'
  44. * @op: pointer to the 'struct spi_mem_op' whose properties
  45. * need to be initialized.
  46. *
  47. * Right now, only "repeat" and "invert" are supported.
  48. *
  49. * Return: The opcode extension.
  50. */
  51. static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
  52. const struct spi_mem_op *op)
  53. {
  54. switch (nor->cmd_ext_type) {
  55. case SPI_NOR_EXT_INVERT:
  56. return ~op->cmd.opcode;
  57. case SPI_NOR_EXT_REPEAT:
  58. return op->cmd.opcode;
  59. default:
  60. dev_err(nor->dev, "Unknown command extension type\n");
  61. return 0;
  62. }
  63. }
  64. /**
  65. * spi_nor_spimem_setup_op() - Set up common properties of a spi-mem op.
  66. * @nor: pointer to a 'struct spi_nor'
  67. * @op: pointer to the 'struct spi_mem_op' whose properties
  68. * need to be initialized.
  69. * @proto: the protocol from which the properties need to be set.
  70. */
  71. void spi_nor_spimem_setup_op(const struct spi_nor *nor,
  72. struct spi_mem_op *op,
  73. const enum spi_nor_protocol proto)
  74. {
  75. u8 ext;
  76. op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
  77. if (op->addr.nbytes)
  78. op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
  79. if (op->dummy.nbytes)
  80. op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
  81. if (op->data.nbytes)
  82. op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
  83. if (spi_nor_protocol_is_dtr(proto)) {
  84. /*
  85. * SPIMEM supports mixed DTR modes, but right now we can only
  86. * have all phases either DTR or STR. IOW, SPIMEM can have
  87. * something like 4S-4D-4D, but SPI NOR can't. So, set all 4
  88. * phases to either DTR or STR.
  89. */
  90. op->cmd.dtr = true;
  91. op->addr.dtr = true;
  92. op->dummy.dtr = true;
  93. op->data.dtr = true;
  94. /* 2 bytes per clock cycle in DTR mode. */
  95. op->dummy.nbytes *= 2;
  96. ext = spi_nor_get_cmd_ext(nor, op);
  97. op->cmd.opcode = (op->cmd.opcode << 8) | ext;
  98. op->cmd.nbytes = 2;
  99. }
  100. if (proto == SNOR_PROTO_8_8_8_DTR && nor->flags & SNOR_F_SWAP16)
  101. op->data.swap16 = true;
  102. }
  103. /**
  104. * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
  105. * transfer
  106. * @nor: pointer to 'struct spi_nor'
  107. * @op: pointer to 'struct spi_mem_op' template for transfer
  108. *
  109. * If we have to use the bounce buffer, the data field in @op will be updated.
  110. *
  111. * Return: true if the bounce buffer is needed, false if not
  112. */
  113. static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op)
  114. {
  115. /* op->data.buf.in occupies the same memory as op->data.buf.out */
  116. if (object_is_on_stack(op->data.buf.in) ||
  117. !virt_addr_valid(op->data.buf.in)) {
  118. if (op->data.nbytes > nor->bouncebuf_size)
  119. op->data.nbytes = nor->bouncebuf_size;
  120. op->data.buf.in = nor->bouncebuf;
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. * spi_nor_spimem_exec_op() - execute a memory operation
  127. * @nor: pointer to 'struct spi_nor'
  128. * @op: pointer to 'struct spi_mem_op' template for transfer
  129. *
  130. * Return: 0 on success, -error otherwise.
  131. */
  132. static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op)
  133. {
  134. int error;
  135. error = spi_mem_adjust_op_size(nor->spimem, op);
  136. if (error)
  137. return error;
  138. return spi_mem_exec_op(nor->spimem, op);
  139. }
  140. int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
  141. u8 *buf, size_t len)
  142. {
  143. if (spi_nor_protocol_is_dtr(nor->reg_proto))
  144. return -EOPNOTSUPP;
  145. return nor->controller_ops->read_reg(nor, opcode, buf, len);
  146. }
  147. int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
  148. const u8 *buf, size_t len)
  149. {
  150. if (spi_nor_protocol_is_dtr(nor->reg_proto))
  151. return -EOPNOTSUPP;
  152. return nor->controller_ops->write_reg(nor, opcode, buf, len);
  153. }
  154. static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs)
  155. {
  156. if (spi_nor_protocol_is_dtr(nor->reg_proto))
  157. return -EOPNOTSUPP;
  158. return nor->controller_ops->erase(nor, offs);
  159. }
  160. /**
  161. * spi_nor_spimem_read_data() - read data from flash's memory region via
  162. * spi-mem
  163. * @nor: pointer to 'struct spi_nor'
  164. * @from: offset to read from
  165. * @len: number of bytes to read
  166. * @buf: pointer to dst buffer
  167. *
  168. * Return: number of bytes read successfully, -errno otherwise
  169. */
  170. static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
  171. size_t len, u8 *buf)
  172. {
  173. struct spi_mem_op op =
  174. SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
  175. SPI_MEM_OP_ADDR(nor->addr_nbytes, from, 0),
  176. SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
  177. SPI_MEM_OP_DATA_IN(len, buf, 0));
  178. bool usebouncebuf;
  179. ssize_t nbytes;
  180. int error;
  181. spi_nor_spimem_setup_op(nor, &op, nor->read_proto);
  182. /* convert the dummy cycles to the number of bytes */
  183. op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
  184. if (spi_nor_protocol_is_dtr(nor->read_proto))
  185. op.dummy.nbytes *= 2;
  186. usebouncebuf = spi_nor_spimem_bounce(nor, &op);
  187. if (nor->dirmap.rdesc) {
  188. nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val,
  189. op.data.nbytes, op.data.buf.in);
  190. } else {
  191. error = spi_nor_spimem_exec_op(nor, &op);
  192. if (error)
  193. return error;
  194. nbytes = op.data.nbytes;
  195. }
  196. if (usebouncebuf && nbytes > 0)
  197. memcpy(buf, op.data.buf.in, nbytes);
  198. return nbytes;
  199. }
  200. /**
  201. * spi_nor_read_data() - read data from flash memory
  202. * @nor: pointer to 'struct spi_nor'
  203. * @from: offset to read from
  204. * @len: number of bytes to read
  205. * @buf: pointer to dst buffer
  206. *
  207. * Return: number of bytes read successfully, -errno otherwise
  208. */
  209. ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf)
  210. {
  211. if (nor->spimem)
  212. return spi_nor_spimem_read_data(nor, from, len, buf);
  213. return nor->controller_ops->read(nor, from, len, buf);
  214. }
  215. /**
  216. * spi_nor_spimem_write_data() - write data to flash memory via
  217. * spi-mem
  218. * @nor: pointer to 'struct spi_nor'
  219. * @to: offset to write to
  220. * @len: number of bytes to write
  221. * @buf: pointer to src buffer
  222. *
  223. * Return: number of bytes written successfully, -errno otherwise
  224. */
  225. static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
  226. size_t len, const u8 *buf)
  227. {
  228. struct spi_mem_op op =
  229. SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
  230. SPI_MEM_OP_ADDR(nor->addr_nbytes, to, 0),
  231. SPI_MEM_OP_NO_DUMMY,
  232. SPI_MEM_OP_DATA_OUT(len, buf, 0));
  233. ssize_t nbytes;
  234. int error;
  235. if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
  236. op.addr.nbytes = 0;
  237. spi_nor_spimem_setup_op(nor, &op, nor->write_proto);
  238. if (spi_nor_spimem_bounce(nor, &op))
  239. memcpy(nor->bouncebuf, buf, op.data.nbytes);
  240. if (nor->dirmap.wdesc) {
  241. nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
  242. op.data.nbytes, op.data.buf.out);
  243. } else {
  244. error = spi_nor_spimem_exec_op(nor, &op);
  245. if (error)
  246. return error;
  247. nbytes = op.data.nbytes;
  248. }
  249. return nbytes;
  250. }
  251. /**
  252. * spi_nor_write_data() - write data to flash memory
  253. * @nor: pointer to 'struct spi_nor'
  254. * @to: offset to write to
  255. * @len: number of bytes to write
  256. * @buf: pointer to src buffer
  257. *
  258. * Return: number of bytes written successfully, -errno otherwise
  259. */
  260. ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
  261. const u8 *buf)
  262. {
  263. if (nor->spimem)
  264. return spi_nor_spimem_write_data(nor, to, len, buf);
  265. return nor->controller_ops->write(nor, to, len, buf);
  266. }
  267. /**
  268. * spi_nor_read_any_reg() - read any register from flash memory, nonvolatile or
  269. * volatile.
  270. * @nor: pointer to 'struct spi_nor'.
  271. * @op: SPI memory operation. op->data.buf must be DMA-able.
  272. * @proto: SPI protocol to use for the register operation.
  273. *
  274. * Return: zero on success, -errno otherwise
  275. */
  276. int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op,
  277. enum spi_nor_protocol proto)
  278. {
  279. if (!nor->spimem)
  280. return -EOPNOTSUPP;
  281. spi_nor_spimem_setup_op(nor, op, proto);
  282. return spi_nor_spimem_exec_op(nor, op);
  283. }
  284. /**
  285. * spi_nor_write_any_volatile_reg() - write any volatile register to flash
  286. * memory.
  287. * @nor: pointer to 'struct spi_nor'
  288. * @op: SPI memory operation. op->data.buf must be DMA-able.
  289. * @proto: SPI protocol to use for the register operation.
  290. *
  291. * Writing volatile registers are instant according to some manufacturers
  292. * (Cypress, Micron) and do not need any status polling.
  293. *
  294. * Return: zero on success, -errno otherwise
  295. */
  296. int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op,
  297. enum spi_nor_protocol proto)
  298. {
  299. int ret;
  300. if (!nor->spimem)
  301. return -EOPNOTSUPP;
  302. ret = spi_nor_write_enable(nor);
  303. if (ret)
  304. return ret;
  305. spi_nor_spimem_setup_op(nor, op, proto);
  306. return spi_nor_spimem_exec_op(nor, op);
  307. }
  308. /**
  309. * spi_nor_write_enable() - Set write enable latch with Write Enable command.
  310. * @nor: pointer to 'struct spi_nor'.
  311. *
  312. * Return: 0 on success, -errno otherwise.
  313. */
  314. int spi_nor_write_enable(struct spi_nor *nor)
  315. {
  316. int ret;
  317. if (nor->spimem) {
  318. struct spi_mem_op op = SPI_NOR_WREN_OP;
  319. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  320. ret = spi_mem_exec_op(nor->spimem, &op);
  321. } else {
  322. ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN,
  323. NULL, 0);
  324. }
  325. if (ret)
  326. dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
  327. return ret;
  328. }
  329. /**
  330. * spi_nor_write_disable() - Send Write Disable instruction to the chip.
  331. * @nor: pointer to 'struct spi_nor'.
  332. *
  333. * Return: 0 on success, -errno otherwise.
  334. */
  335. int spi_nor_write_disable(struct spi_nor *nor)
  336. {
  337. int ret;
  338. if (nor->spimem) {
  339. struct spi_mem_op op = SPI_NOR_WRDI_OP;
  340. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  341. ret = spi_mem_exec_op(nor->spimem, &op);
  342. } else {
  343. ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI,
  344. NULL, 0);
  345. }
  346. if (ret)
  347. dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
  348. return ret;
  349. }
  350. /**
  351. * spi_nor_read_id() - Read the JEDEC ID.
  352. * @nor: pointer to 'struct spi_nor'.
  353. * @naddr: number of address bytes to send. Can be zero if the operation
  354. * does not need to send an address.
  355. * @ndummy: number of dummy bytes to send after an opcode or address. Can
  356. * be zero if the operation does not require dummy bytes.
  357. * @id: pointer to a DMA-able buffer where the value of the JEDEC ID
  358. * will be written.
  359. * @proto: the SPI protocol for register operation.
  360. *
  361. * Return: 0 on success, -errno otherwise.
  362. */
  363. int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id,
  364. enum spi_nor_protocol proto)
  365. {
  366. int ret;
  367. if (nor->spimem) {
  368. struct spi_mem_op op =
  369. SPI_NOR_READID_OP(naddr, ndummy, id, SPI_NOR_MAX_ID_LEN);
  370. spi_nor_spimem_setup_op(nor, &op, proto);
  371. ret = spi_mem_exec_op(nor->spimem, &op);
  372. } else {
  373. ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
  374. SPI_NOR_MAX_ID_LEN);
  375. }
  376. return ret;
  377. }
  378. /**
  379. * spi_nor_read_sr() - Read the Status Register.
  380. * @nor: pointer to 'struct spi_nor'.
  381. * @sr: pointer to a DMA-able buffer where the value of the
  382. * Status Register will be written. Should be at least 2 bytes.
  383. *
  384. * Return: 0 on success, -errno otherwise.
  385. */
  386. int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
  387. {
  388. int ret;
  389. if (nor->spimem) {
  390. struct spi_mem_op op = SPI_NOR_RDSR_OP(sr);
  391. if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
  392. op.addr.nbytes = nor->params->rdsr_addr_nbytes;
  393. op.dummy.nbytes = nor->params->rdsr_dummy;
  394. /*
  395. * We don't want to read only one byte in DTR mode. So,
  396. * read 2 and then discard the second byte.
  397. */
  398. op.data.nbytes = 2;
  399. }
  400. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  401. ret = spi_mem_exec_op(nor->spimem, &op);
  402. } else {
  403. ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr,
  404. 1);
  405. }
  406. if (ret)
  407. dev_dbg(nor->dev, "error %d reading SR\n", ret);
  408. return ret;
  409. }
  410. /**
  411. * spi_nor_read_cr() - Read the Configuration Register using the
  412. * SPINOR_OP_RDCR (35h) command.
  413. * @nor: pointer to 'struct spi_nor'
  414. * @cr: pointer to a DMA-able buffer where the value of the
  415. * Configuration Register will be written.
  416. *
  417. * Return: 0 on success, -errno otherwise.
  418. */
  419. int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
  420. {
  421. int ret;
  422. if (nor->spimem) {
  423. struct spi_mem_op op = SPI_NOR_RDCR_OP(cr);
  424. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  425. ret = spi_mem_exec_op(nor->spimem, &op);
  426. } else {
  427. ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr,
  428. 1);
  429. }
  430. if (ret)
  431. dev_dbg(nor->dev, "error %d reading CR\n", ret);
  432. return ret;
  433. }
  434. /**
  435. * spi_nor_set_4byte_addr_mode_en4b_ex4b() - Enter/Exit 4-byte address mode
  436. * using SPINOR_OP_EN4B/SPINOR_OP_EX4B. Typically used by
  437. * Winbond and Macronix.
  438. * @nor: pointer to 'struct spi_nor'.
  439. * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
  440. * address mode.
  441. *
  442. * Return: 0 on success, -errno otherwise.
  443. */
  444. int spi_nor_set_4byte_addr_mode_en4b_ex4b(struct spi_nor *nor, bool enable)
  445. {
  446. int ret;
  447. if (nor->spimem) {
  448. struct spi_mem_op op = SPI_NOR_EN4B_EX4B_OP(enable);
  449. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  450. ret = spi_mem_exec_op(nor->spimem, &op);
  451. } else {
  452. ret = spi_nor_controller_ops_write_reg(nor,
  453. enable ? SPINOR_OP_EN4B :
  454. SPINOR_OP_EX4B,
  455. NULL, 0);
  456. }
  457. if (ret)
  458. dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
  459. return ret;
  460. }
  461. /**
  462. * spi_nor_set_4byte_addr_mode_wren_en4b_ex4b() - Set 4-byte address mode using
  463. * SPINOR_OP_WREN followed by SPINOR_OP_EN4B or SPINOR_OP_EX4B. Typically used
  464. * by ST and Micron flashes.
  465. * @nor: pointer to 'struct spi_nor'.
  466. * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
  467. * address mode.
  468. *
  469. * Return: 0 on success, -errno otherwise.
  470. */
  471. int spi_nor_set_4byte_addr_mode_wren_en4b_ex4b(struct spi_nor *nor, bool enable)
  472. {
  473. int ret;
  474. ret = spi_nor_write_enable(nor);
  475. if (ret)
  476. return ret;
  477. ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable);
  478. if (ret)
  479. return ret;
  480. return spi_nor_write_disable(nor);
  481. }
  482. /**
  483. * spi_nor_set_4byte_addr_mode_brwr() - Set 4-byte address mode using
  484. * SPINOR_OP_BRWR. Typically used by Spansion flashes.
  485. * @nor: pointer to 'struct spi_nor'.
  486. * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
  487. * address mode.
  488. *
  489. * 8-bit volatile bank register used to define A[30:A24] bits. MSB (bit[7]) is
  490. * used to enable/disable 4-byte address mode. When MSB is set to ‘1’, 4-byte
  491. * address mode is active and A[30:24] bits are don’t care. Write instruction is
  492. * SPINOR_OP_BRWR(17h) with 1 byte of data.
  493. *
  494. * Return: 0 on success, -errno otherwise.
  495. */
  496. int spi_nor_set_4byte_addr_mode_brwr(struct spi_nor *nor, bool enable)
  497. {
  498. int ret;
  499. nor->bouncebuf[0] = enable << 7;
  500. if (nor->spimem) {
  501. struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf);
  502. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  503. ret = spi_mem_exec_op(nor->spimem, &op);
  504. } else {
  505. ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR,
  506. nor->bouncebuf, 1);
  507. }
  508. if (ret)
  509. dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
  510. return ret;
  511. }
  512. /**
  513. * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
  514. * for new commands.
  515. * @nor: pointer to 'struct spi_nor'.
  516. *
  517. * Return: 1 if ready, 0 if not ready, -errno on errors.
  518. */
  519. int spi_nor_sr_ready(struct spi_nor *nor)
  520. {
  521. int ret;
  522. ret = spi_nor_read_sr(nor, nor->bouncebuf);
  523. if (ret)
  524. return ret;
  525. return !(nor->bouncebuf[0] & SR_WIP);
  526. }
  527. /**
  528. * spi_nor_use_parallel_locking() - Checks if RWW locking scheme shall be used
  529. * @nor: pointer to 'struct spi_nor'.
  530. *
  531. * Return: true if parallel locking is enabled, false otherwise.
  532. */
  533. static bool spi_nor_use_parallel_locking(struct spi_nor *nor)
  534. {
  535. return nor->flags & SNOR_F_RWW;
  536. }
  537. /* Locking helpers for status read operations */
  538. static int spi_nor_rww_start_rdst(struct spi_nor *nor)
  539. {
  540. struct spi_nor_rww *rww = &nor->rww;
  541. guard(mutex)(&nor->lock);
  542. if (rww->ongoing_io || rww->ongoing_rd)
  543. return -EAGAIN;
  544. rww->ongoing_io = true;
  545. rww->ongoing_rd = true;
  546. return 0;
  547. }
  548. static void spi_nor_rww_end_rdst(struct spi_nor *nor)
  549. {
  550. struct spi_nor_rww *rww = &nor->rww;
  551. guard(mutex)(&nor->lock);
  552. rww->ongoing_io = false;
  553. rww->ongoing_rd = false;
  554. }
  555. static int spi_nor_lock_rdst(struct spi_nor *nor)
  556. {
  557. if (spi_nor_use_parallel_locking(nor))
  558. return spi_nor_rww_start_rdst(nor);
  559. return 0;
  560. }
  561. static void spi_nor_unlock_rdst(struct spi_nor *nor)
  562. {
  563. if (spi_nor_use_parallel_locking(nor)) {
  564. spi_nor_rww_end_rdst(nor);
  565. wake_up(&nor->rww.wait);
  566. }
  567. }
  568. /**
  569. * spi_nor_ready() - Query the flash to see if it is ready for new commands.
  570. * @nor: pointer to 'struct spi_nor'.
  571. *
  572. * Return: 1 if ready, 0 if not ready, -errno on errors.
  573. */
  574. static int spi_nor_ready(struct spi_nor *nor)
  575. {
  576. int ret;
  577. ret = spi_nor_lock_rdst(nor);
  578. if (ret)
  579. return 0;
  580. /* Flashes might override the standard routine. */
  581. if (nor->params->ready)
  582. ret = nor->params->ready(nor);
  583. else
  584. ret = spi_nor_sr_ready(nor);
  585. spi_nor_unlock_rdst(nor);
  586. return ret;
  587. }
  588. /**
  589. * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
  590. * Status Register until ready, or timeout occurs.
  591. * @nor: pointer to "struct spi_nor".
  592. * @timeout_jiffies: jiffies to wait until timeout.
  593. *
  594. * Return: 0 on success, -errno otherwise.
  595. */
  596. static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
  597. unsigned long timeout_jiffies)
  598. {
  599. unsigned long deadline;
  600. int timeout = 0, ret;
  601. deadline = jiffies + timeout_jiffies;
  602. while (!timeout) {
  603. if (time_after_eq(jiffies, deadline))
  604. timeout = 1;
  605. ret = spi_nor_ready(nor);
  606. if (ret < 0)
  607. return ret;
  608. if (ret)
  609. return 0;
  610. cond_resched();
  611. }
  612. dev_dbg(nor->dev, "flash operation timed out\n");
  613. return -ETIMEDOUT;
  614. }
  615. /**
  616. * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
  617. * flash to be ready, or timeout occurs.
  618. * @nor: pointer to "struct spi_nor".
  619. *
  620. * Return: 0 on success, -errno otherwise.
  621. */
  622. int spi_nor_wait_till_ready(struct spi_nor *nor)
  623. {
  624. return spi_nor_wait_till_ready_with_timeout(nor,
  625. DEFAULT_READY_WAIT_JIFFIES);
  626. }
  627. /**
  628. * spi_nor_global_block_unlock() - Unlock Global Block Protection.
  629. * @nor: pointer to 'struct spi_nor'.
  630. *
  631. * Return: 0 on success, -errno otherwise.
  632. */
  633. int spi_nor_global_block_unlock(struct spi_nor *nor)
  634. {
  635. int ret;
  636. ret = spi_nor_write_enable(nor);
  637. if (ret)
  638. return ret;
  639. if (nor->spimem) {
  640. struct spi_mem_op op = SPI_NOR_GBULK_OP;
  641. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  642. ret = spi_mem_exec_op(nor->spimem, &op);
  643. } else {
  644. ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK,
  645. NULL, 0);
  646. }
  647. if (ret) {
  648. dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret);
  649. return ret;
  650. }
  651. return spi_nor_wait_till_ready(nor);
  652. }
  653. /**
  654. * spi_nor_write_sr() - Write the Status Register.
  655. * @nor: pointer to 'struct spi_nor'.
  656. * @sr: pointer to DMA-able buffer to write to the Status Register.
  657. * @len: number of bytes to write to the Status Register.
  658. *
  659. * Return: 0 on success, -errno otherwise.
  660. */
  661. int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
  662. {
  663. int ret;
  664. ret = spi_nor_write_enable(nor);
  665. if (ret)
  666. return ret;
  667. if (nor->spimem) {
  668. struct spi_mem_op op = SPI_NOR_WRSR_OP(sr, len);
  669. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  670. ret = spi_mem_exec_op(nor->spimem, &op);
  671. } else {
  672. ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr,
  673. len);
  674. }
  675. if (ret) {
  676. dev_dbg(nor->dev, "error %d writing SR\n", ret);
  677. return ret;
  678. }
  679. return spi_nor_wait_till_ready(nor);
  680. }
  681. /**
  682. * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
  683. * ensure that the byte written match the received value.
  684. * @nor: pointer to a 'struct spi_nor'.
  685. * @sr1: byte value to be written to the Status Register.
  686. *
  687. * Return: 0 on success, -errno otherwise.
  688. */
  689. static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
  690. {
  691. int ret;
  692. nor->bouncebuf[0] = sr1;
  693. ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
  694. if (ret)
  695. return ret;
  696. ret = spi_nor_read_sr(nor, nor->bouncebuf);
  697. if (ret)
  698. return ret;
  699. if (nor->bouncebuf[0] != sr1) {
  700. dev_dbg(nor->dev, "SR1: read back test failed\n");
  701. return -EIO;
  702. }
  703. return 0;
  704. }
  705. /**
  706. * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
  707. * Status Register 2 in one shot. Ensure that the byte written in the Status
  708. * Register 1 match the received value, and that the 16-bit Write did not
  709. * affect what was already in the Status Register 2.
  710. * @nor: pointer to a 'struct spi_nor'.
  711. * @sr1: byte value to be written to the Status Register 1.
  712. *
  713. * Return: 0 on success, -errno otherwise.
  714. */
  715. static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
  716. {
  717. int ret;
  718. u8 *sr_cr = nor->bouncebuf;
  719. u8 cr_written;
  720. /* Make sure we don't overwrite the contents of Status Register 2. */
  721. if (!(nor->flags & SNOR_F_NO_READ_CR)) {
  722. ret = spi_nor_read_cr(nor, &sr_cr[1]);
  723. if (ret)
  724. return ret;
  725. } else if (spi_nor_get_protocol_width(nor->read_proto) == 4 &&
  726. spi_nor_get_protocol_width(nor->write_proto) == 4 &&
  727. nor->params->quad_enable) {
  728. /*
  729. * If the Status Register 2 Read command (35h) is not
  730. * supported, we should at least be sure we don't
  731. * change the value of the SR2 Quad Enable bit.
  732. *
  733. * When the Quad Enable method is set and the buswidth is 4, we
  734. * can safely assume that the value of the QE bit is one, as a
  735. * consequence of the nor->params->quad_enable() call.
  736. *
  737. * According to the JESD216 revB standard, BFPT DWORDS[15],
  738. * bits 22:20, the 16-bit Write Status (01h) command is
  739. * available just for the cases in which the QE bit is
  740. * described in SR2 at BIT(1).
  741. */
  742. sr_cr[1] = SR2_QUAD_EN_BIT1;
  743. } else {
  744. sr_cr[1] = 0;
  745. }
  746. sr_cr[0] = sr1;
  747. ret = spi_nor_write_sr(nor, sr_cr, 2);
  748. if (ret)
  749. return ret;
  750. ret = spi_nor_read_sr(nor, sr_cr);
  751. if (ret)
  752. return ret;
  753. if (sr1 != sr_cr[0]) {
  754. dev_dbg(nor->dev, "SR: Read back test failed\n");
  755. return -EIO;
  756. }
  757. if (nor->flags & SNOR_F_NO_READ_CR)
  758. return 0;
  759. cr_written = sr_cr[1];
  760. ret = spi_nor_read_cr(nor, &sr_cr[1]);
  761. if (ret)
  762. return ret;
  763. if (cr_written != sr_cr[1]) {
  764. dev_dbg(nor->dev, "CR: read back test failed\n");
  765. return -EIO;
  766. }
  767. return 0;
  768. }
  769. /**
  770. * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
  771. * Configuration Register in one shot. Ensure that the byte written in the
  772. * Configuration Register match the received value, and that the 16-bit Write
  773. * did not affect what was already in the Status Register 1.
  774. * @nor: pointer to a 'struct spi_nor'.
  775. * @cr: byte value to be written to the Configuration Register.
  776. *
  777. * Return: 0 on success, -errno otherwise.
  778. */
  779. int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
  780. {
  781. int ret;
  782. u8 *sr_cr = nor->bouncebuf;
  783. u8 sr_written;
  784. /* Keep the current value of the Status Register 1. */
  785. ret = spi_nor_read_sr(nor, sr_cr);
  786. if (ret)
  787. return ret;
  788. sr_cr[1] = cr;
  789. ret = spi_nor_write_sr(nor, sr_cr, 2);
  790. if (ret)
  791. return ret;
  792. sr_written = sr_cr[0];
  793. ret = spi_nor_read_sr(nor, sr_cr);
  794. if (ret)
  795. return ret;
  796. if (sr_written != sr_cr[0]) {
  797. dev_dbg(nor->dev, "SR: Read back test failed\n");
  798. return -EIO;
  799. }
  800. if (nor->flags & SNOR_F_NO_READ_CR)
  801. return 0;
  802. ret = spi_nor_read_cr(nor, &sr_cr[1]);
  803. if (ret)
  804. return ret;
  805. if (cr != sr_cr[1]) {
  806. dev_dbg(nor->dev, "CR: read back test failed\n");
  807. return -EIO;
  808. }
  809. return 0;
  810. }
  811. /**
  812. * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
  813. * the byte written match the received value without affecting other bits in the
  814. * Status Register 1 and 2.
  815. * @nor: pointer to a 'struct spi_nor'.
  816. * @sr1: byte value to be written to the Status Register.
  817. *
  818. * Return: 0 on success, -errno otherwise.
  819. */
  820. int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
  821. {
  822. if (nor->flags & SNOR_F_HAS_16BIT_SR)
  823. return spi_nor_write_16bit_sr_and_check(nor, sr1);
  824. return spi_nor_write_sr1_and_check(nor, sr1);
  825. }
  826. /**
  827. * spi_nor_write_sr2() - Write the Status Register 2 using the
  828. * SPINOR_OP_WRSR2 (3eh) command.
  829. * @nor: pointer to 'struct spi_nor'.
  830. * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
  831. *
  832. * Return: 0 on success, -errno otherwise.
  833. */
  834. static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
  835. {
  836. int ret;
  837. ret = spi_nor_write_enable(nor);
  838. if (ret)
  839. return ret;
  840. if (nor->spimem) {
  841. struct spi_mem_op op = SPI_NOR_WRSR2_OP(sr2);
  842. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  843. ret = spi_mem_exec_op(nor->spimem, &op);
  844. } else {
  845. ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2,
  846. sr2, 1);
  847. }
  848. if (ret) {
  849. dev_dbg(nor->dev, "error %d writing SR2\n", ret);
  850. return ret;
  851. }
  852. return spi_nor_wait_till_ready(nor);
  853. }
  854. /**
  855. * spi_nor_read_sr2() - Read the Status Register 2 using the
  856. * SPINOR_OP_RDSR2 (3fh) command.
  857. * @nor: pointer to 'struct spi_nor'.
  858. * @sr2: pointer to DMA-able buffer where the value of the
  859. * Status Register 2 will be written.
  860. *
  861. * Return: 0 on success, -errno otherwise.
  862. */
  863. static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
  864. {
  865. int ret;
  866. if (nor->spimem) {
  867. struct spi_mem_op op = SPI_NOR_RDSR2_OP(sr2);
  868. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  869. ret = spi_mem_exec_op(nor->spimem, &op);
  870. } else {
  871. ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2,
  872. 1);
  873. }
  874. if (ret)
  875. dev_dbg(nor->dev, "error %d reading SR2\n", ret);
  876. return ret;
  877. }
  878. /**
  879. * spi_nor_erase_die() - Erase the entire die.
  880. * @nor: pointer to 'struct spi_nor'.
  881. * @addr: address of the die.
  882. * @die_size: size of the die.
  883. *
  884. * Return: 0 on success, -errno otherwise.
  885. */
  886. static int spi_nor_erase_die(struct spi_nor *nor, loff_t addr, size_t die_size)
  887. {
  888. bool multi_die = nor->mtd.size != die_size;
  889. int ret;
  890. dev_dbg(nor->dev, " %lldKiB\n", (long long)(die_size >> 10));
  891. if (nor->spimem) {
  892. struct spi_mem_op op =
  893. SPI_NOR_DIE_ERASE_OP(nor->params->die_erase_opcode,
  894. nor->addr_nbytes, addr, multi_die);
  895. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  896. ret = spi_mem_exec_op(nor->spimem, &op);
  897. } else {
  898. if (multi_die)
  899. return -EOPNOTSUPP;
  900. ret = spi_nor_controller_ops_write_reg(nor,
  901. SPINOR_OP_CHIP_ERASE,
  902. NULL, 0);
  903. }
  904. if (ret)
  905. dev_dbg(nor->dev, "error %d erasing chip\n", ret);
  906. return ret;
  907. }
  908. static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
  909. {
  910. size_t i;
  911. for (i = 0; i < size; i++)
  912. if (table[i][0] == opcode)
  913. return table[i][1];
  914. /* No conversion found, keep input op code. */
  915. return opcode;
  916. }
  917. u8 spi_nor_convert_3to4_read(u8 opcode)
  918. {
  919. static const u8 spi_nor_3to4_read[][2] = {
  920. { SPINOR_OP_READ, SPINOR_OP_READ_4B },
  921. { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
  922. { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
  923. { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
  924. { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
  925. { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
  926. { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
  927. { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
  928. { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
  929. { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
  930. { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
  931. };
  932. return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
  933. ARRAY_SIZE(spi_nor_3to4_read));
  934. }
  935. static u8 spi_nor_convert_3to4_program(u8 opcode)
  936. {
  937. static const u8 spi_nor_3to4_program[][2] = {
  938. { SPINOR_OP_PP, SPINOR_OP_PP_4B },
  939. { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
  940. { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
  941. { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
  942. { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
  943. };
  944. return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
  945. ARRAY_SIZE(spi_nor_3to4_program));
  946. }
  947. static u8 spi_nor_convert_3to4_erase(u8 opcode)
  948. {
  949. static const u8 spi_nor_3to4_erase[][2] = {
  950. { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
  951. { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
  952. { SPINOR_OP_SE, SPINOR_OP_SE_4B },
  953. };
  954. return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
  955. ARRAY_SIZE(spi_nor_3to4_erase));
  956. }
  957. static bool spi_nor_has_uniform_erase(const struct spi_nor *nor)
  958. {
  959. return !!nor->params->erase_map.uniform_region.erase_mask;
  960. }
  961. static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
  962. {
  963. nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
  964. nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
  965. nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
  966. if (!spi_nor_has_uniform_erase(nor)) {
  967. struct spi_nor_erase_map *map = &nor->params->erase_map;
  968. struct spi_nor_erase_type *erase;
  969. int i;
  970. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  971. erase = &map->erase_type[i];
  972. erase->opcode =
  973. spi_nor_convert_3to4_erase(erase->opcode);
  974. }
  975. }
  976. }
  977. static int spi_nor_prep(struct spi_nor *nor)
  978. {
  979. int ret = 0;
  980. if (nor->controller_ops && nor->controller_ops->prepare)
  981. ret = nor->controller_ops->prepare(nor);
  982. return ret;
  983. }
  984. static void spi_nor_unprep(struct spi_nor *nor)
  985. {
  986. if (nor->controller_ops && nor->controller_ops->unprepare)
  987. nor->controller_ops->unprepare(nor);
  988. }
  989. static void spi_nor_offset_to_banks(u64 bank_size, loff_t start, size_t len,
  990. u8 *first, u8 *last)
  991. {
  992. /* This is currently safe, the number of banks being very small */
  993. *first = DIV_ROUND_DOWN_ULL(start, bank_size);
  994. *last = DIV_ROUND_DOWN_ULL(start + len - 1, bank_size);
  995. }
  996. /* Generic helpers for internal locking and serialization */
  997. static bool spi_nor_rww_start_io(struct spi_nor *nor)
  998. {
  999. struct spi_nor_rww *rww = &nor->rww;
  1000. guard(mutex)(&nor->lock);
  1001. if (rww->ongoing_io)
  1002. return false;
  1003. rww->ongoing_io = true;
  1004. return true;
  1005. }
  1006. static void spi_nor_rww_end_io(struct spi_nor *nor)
  1007. {
  1008. guard(mutex)(&nor->lock);
  1009. nor->rww.ongoing_io = false;
  1010. }
  1011. static int spi_nor_lock_device(struct spi_nor *nor)
  1012. {
  1013. if (!spi_nor_use_parallel_locking(nor))
  1014. return 0;
  1015. return wait_event_killable(nor->rww.wait, spi_nor_rww_start_io(nor));
  1016. }
  1017. static void spi_nor_unlock_device(struct spi_nor *nor)
  1018. {
  1019. if (spi_nor_use_parallel_locking(nor)) {
  1020. spi_nor_rww_end_io(nor);
  1021. wake_up(&nor->rww.wait);
  1022. }
  1023. }
  1024. /* Generic helpers for internal locking and serialization */
  1025. static bool spi_nor_rww_start_exclusive(struct spi_nor *nor)
  1026. {
  1027. struct spi_nor_rww *rww = &nor->rww;
  1028. mutex_lock(&nor->lock);
  1029. if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe)
  1030. return false;
  1031. rww->ongoing_io = true;
  1032. rww->ongoing_rd = true;
  1033. rww->ongoing_pe = true;
  1034. return true;
  1035. }
  1036. static void spi_nor_rww_end_exclusive(struct spi_nor *nor)
  1037. {
  1038. struct spi_nor_rww *rww = &nor->rww;
  1039. guard(mutex)(&nor->lock);
  1040. rww->ongoing_io = false;
  1041. rww->ongoing_rd = false;
  1042. rww->ongoing_pe = false;
  1043. }
  1044. int spi_nor_prep_and_lock(struct spi_nor *nor)
  1045. {
  1046. int ret;
  1047. ret = spi_nor_prep(nor);
  1048. if (ret)
  1049. return ret;
  1050. if (!spi_nor_use_parallel_locking(nor))
  1051. mutex_lock(&nor->lock);
  1052. else
  1053. ret = wait_event_killable(nor->rww.wait,
  1054. spi_nor_rww_start_exclusive(nor));
  1055. return ret;
  1056. }
  1057. void spi_nor_unlock_and_unprep(struct spi_nor *nor)
  1058. {
  1059. if (!spi_nor_use_parallel_locking(nor)) {
  1060. mutex_unlock(&nor->lock);
  1061. } else {
  1062. spi_nor_rww_end_exclusive(nor);
  1063. wake_up(&nor->rww.wait);
  1064. }
  1065. spi_nor_unprep(nor);
  1066. }
  1067. /* Internal locking helpers for program and erase operations */
  1068. static bool spi_nor_rww_start_pe(struct spi_nor *nor, loff_t start, size_t len)
  1069. {
  1070. struct spi_nor_rww *rww = &nor->rww;
  1071. unsigned int used_banks = 0;
  1072. u8 first, last;
  1073. int bank;
  1074. guard(mutex)(&nor->lock);
  1075. if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe)
  1076. return false;
  1077. spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
  1078. for (bank = first; bank <= last; bank++) {
  1079. if (rww->used_banks & BIT(bank))
  1080. return false;
  1081. used_banks |= BIT(bank);
  1082. }
  1083. rww->used_banks |= used_banks;
  1084. rww->ongoing_pe = true;
  1085. return true;
  1086. }
  1087. static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len)
  1088. {
  1089. struct spi_nor_rww *rww = &nor->rww;
  1090. u8 first, last;
  1091. int bank;
  1092. guard(mutex)(&nor->lock);
  1093. spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
  1094. for (bank = first; bank <= last; bank++)
  1095. rww->used_banks &= ~BIT(bank);
  1096. rww->ongoing_pe = false;
  1097. }
  1098. static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t len)
  1099. {
  1100. int ret;
  1101. ret = spi_nor_prep(nor);
  1102. if (ret)
  1103. return ret;
  1104. if (!spi_nor_use_parallel_locking(nor))
  1105. mutex_lock(&nor->lock);
  1106. else
  1107. ret = wait_event_killable(nor->rww.wait,
  1108. spi_nor_rww_start_pe(nor, start, len));
  1109. return ret;
  1110. }
  1111. static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len)
  1112. {
  1113. if (!spi_nor_use_parallel_locking(nor)) {
  1114. mutex_unlock(&nor->lock);
  1115. } else {
  1116. spi_nor_rww_end_pe(nor, start, len);
  1117. wake_up(&nor->rww.wait);
  1118. }
  1119. spi_nor_unprep(nor);
  1120. }
  1121. /* Internal locking helpers for read operations */
  1122. static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len)
  1123. {
  1124. struct spi_nor_rww *rww = &nor->rww;
  1125. unsigned int used_banks = 0;
  1126. u8 first, last;
  1127. int bank;
  1128. guard(mutex)(&nor->lock);
  1129. if (rww->ongoing_io || rww->ongoing_rd)
  1130. return false;
  1131. spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
  1132. for (bank = first; bank <= last; bank++) {
  1133. if (rww->used_banks & BIT(bank))
  1134. return false;
  1135. used_banks |= BIT(bank);
  1136. }
  1137. rww->used_banks |= used_banks;
  1138. rww->ongoing_io = true;
  1139. rww->ongoing_rd = true;
  1140. return true;
  1141. }
  1142. static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len)
  1143. {
  1144. struct spi_nor_rww *rww = &nor->rww;
  1145. u8 first, last;
  1146. int bank;
  1147. guard(mutex)(&nor->lock);
  1148. spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
  1149. for (bank = first; bank <= last; bank++)
  1150. nor->rww.used_banks &= ~BIT(bank);
  1151. rww->ongoing_io = false;
  1152. rww->ongoing_rd = false;
  1153. }
  1154. static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t len)
  1155. {
  1156. int ret;
  1157. ret = spi_nor_prep(nor);
  1158. if (ret)
  1159. return ret;
  1160. if (!spi_nor_use_parallel_locking(nor))
  1161. mutex_lock(&nor->lock);
  1162. else
  1163. ret = wait_event_killable(nor->rww.wait,
  1164. spi_nor_rww_start_rd(nor, start, len));
  1165. return ret;
  1166. }
  1167. static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len)
  1168. {
  1169. if (!spi_nor_use_parallel_locking(nor)) {
  1170. mutex_unlock(&nor->lock);
  1171. } else {
  1172. spi_nor_rww_end_rd(nor, start, len);
  1173. wake_up(&nor->rww.wait);
  1174. }
  1175. spi_nor_unprep(nor);
  1176. }
  1177. /*
  1178. * Initiate the erasure of a single sector
  1179. */
  1180. int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  1181. {
  1182. int i;
  1183. if (nor->spimem) {
  1184. struct spi_mem_op op =
  1185. SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode,
  1186. nor->addr_nbytes, addr);
  1187. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  1188. return spi_mem_exec_op(nor->spimem, &op);
  1189. } else if (nor->controller_ops->erase) {
  1190. return spi_nor_controller_ops_erase(nor, addr);
  1191. }
  1192. /*
  1193. * Default implementation, if driver doesn't have a specialized HW
  1194. * control
  1195. */
  1196. for (i = nor->addr_nbytes - 1; i >= 0; i--) {
  1197. nor->bouncebuf[i] = addr & 0xff;
  1198. addr >>= 8;
  1199. }
  1200. return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode,
  1201. nor->bouncebuf, nor->addr_nbytes);
  1202. }
  1203. /**
  1204. * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
  1205. * @erase: pointer to a structure that describes a SPI NOR erase type
  1206. * @dividend: dividend value
  1207. * @remainder: pointer to u32 remainder (will be updated)
  1208. *
  1209. * Return: the result of the division
  1210. */
  1211. static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
  1212. u64 dividend, u32 *remainder)
  1213. {
  1214. /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
  1215. *remainder = (u32)dividend & erase->size_mask;
  1216. return dividend >> erase->size_shift;
  1217. }
  1218. /**
  1219. * spi_nor_find_best_erase_type() - find the best erase type for the given
  1220. * offset in the serial flash memory and the
  1221. * number of bytes to erase. The region in
  1222. * which the address fits is expected to be
  1223. * provided.
  1224. * @map: the erase map of the SPI NOR
  1225. * @region: pointer to a structure that describes a SPI NOR erase region
  1226. * @addr: offset in the serial flash memory
  1227. * @len: number of bytes to erase
  1228. *
  1229. * Return: a pointer to the best fitted erase type, NULL otherwise.
  1230. */
  1231. static const struct spi_nor_erase_type *
  1232. spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
  1233. const struct spi_nor_erase_region *region,
  1234. u64 addr, u32 len)
  1235. {
  1236. const struct spi_nor_erase_type *erase;
  1237. u32 rem;
  1238. int i;
  1239. /*
  1240. * Erase types are ordered by size, with the smallest erase type at
  1241. * index 0.
  1242. */
  1243. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  1244. /* Does the erase region support the tested erase type? */
  1245. if (!(region->erase_mask & BIT(i)))
  1246. continue;
  1247. erase = &map->erase_type[i];
  1248. if (!erase->size)
  1249. continue;
  1250. /* Alignment is not mandatory for overlaid regions */
  1251. if (region->overlaid && region->size <= len)
  1252. return erase;
  1253. /* Don't erase more than what the user has asked for. */
  1254. if (erase->size > len)
  1255. continue;
  1256. spi_nor_div_by_erase_size(erase, addr, &rem);
  1257. if (!rem)
  1258. return erase;
  1259. }
  1260. return NULL;
  1261. }
  1262. /**
  1263. * spi_nor_init_erase_cmd() - initialize an erase command
  1264. * @region: pointer to a structure that describes a SPI NOR erase region
  1265. * @erase: pointer to a structure that describes a SPI NOR erase type
  1266. *
  1267. * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
  1268. * otherwise.
  1269. */
  1270. static struct spi_nor_erase_command *
  1271. spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
  1272. const struct spi_nor_erase_type *erase)
  1273. {
  1274. struct spi_nor_erase_command *cmd;
  1275. cmd = kmalloc_obj(*cmd);
  1276. if (!cmd)
  1277. return ERR_PTR(-ENOMEM);
  1278. INIT_LIST_HEAD(&cmd->list);
  1279. cmd->opcode = erase->opcode;
  1280. cmd->count = 1;
  1281. if (region->overlaid)
  1282. cmd->size = region->size;
  1283. else
  1284. cmd->size = erase->size;
  1285. return cmd;
  1286. }
  1287. /**
  1288. * spi_nor_destroy_erase_cmd_list() - destroy erase command list
  1289. * @erase_list: list of erase commands
  1290. */
  1291. static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
  1292. {
  1293. struct spi_nor_erase_command *cmd, *next;
  1294. list_for_each_entry_safe(cmd, next, erase_list, list) {
  1295. list_del(&cmd->list);
  1296. kfree(cmd);
  1297. }
  1298. }
  1299. /**
  1300. * spi_nor_init_erase_cmd_list() - initialize erase command list
  1301. * @nor: pointer to a 'struct spi_nor'
  1302. * @erase_list: list of erase commands to be executed once we validate that the
  1303. * erase can be performed
  1304. * @addr: offset in the serial flash memory
  1305. * @len: number of bytes to erase
  1306. *
  1307. * Builds the list of best fitted erase commands and verifies if the erase can
  1308. * be performed.
  1309. *
  1310. * Return: 0 on success, -errno otherwise.
  1311. */
  1312. static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
  1313. struct list_head *erase_list,
  1314. u64 addr, u32 len)
  1315. {
  1316. const struct spi_nor_erase_map *map = &nor->params->erase_map;
  1317. const struct spi_nor_erase_type *erase, *prev_erase = NULL;
  1318. struct spi_nor_erase_region *region;
  1319. struct spi_nor_erase_command *cmd = NULL;
  1320. u64 region_end;
  1321. unsigned int i;
  1322. int ret = -EINVAL;
  1323. for (i = 0; i < map->n_regions && len; i++) {
  1324. region = &map->regions[i];
  1325. region_end = region->offset + region->size;
  1326. while (len && addr >= region->offset && addr < region_end) {
  1327. erase = spi_nor_find_best_erase_type(map, region, addr,
  1328. len);
  1329. if (!erase)
  1330. goto destroy_erase_cmd_list;
  1331. if (prev_erase != erase || erase->size != cmd->size ||
  1332. region->overlaid) {
  1333. cmd = spi_nor_init_erase_cmd(region, erase);
  1334. if (IS_ERR(cmd)) {
  1335. ret = PTR_ERR(cmd);
  1336. goto destroy_erase_cmd_list;
  1337. }
  1338. list_add_tail(&cmd->list, erase_list);
  1339. } else {
  1340. cmd->count++;
  1341. }
  1342. len -= cmd->size;
  1343. addr += cmd->size;
  1344. prev_erase = erase;
  1345. }
  1346. }
  1347. return 0;
  1348. destroy_erase_cmd_list:
  1349. spi_nor_destroy_erase_cmd_list(erase_list);
  1350. return ret;
  1351. }
  1352. /**
  1353. * spi_nor_erase_multi_sectors() - perform a non-uniform erase
  1354. * @nor: pointer to a 'struct spi_nor'
  1355. * @addr: offset in the serial flash memory
  1356. * @len: number of bytes to erase
  1357. *
  1358. * Build a list of best fitted erase commands and execute it once we validate
  1359. * that the erase can be performed.
  1360. *
  1361. * Return: 0 on success, -errno otherwise.
  1362. */
  1363. static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
  1364. {
  1365. LIST_HEAD(erase_list);
  1366. struct spi_nor_erase_command *cmd, *next;
  1367. int ret;
  1368. ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
  1369. if (ret)
  1370. return ret;
  1371. list_for_each_entry_safe(cmd, next, &erase_list, list) {
  1372. nor->erase_opcode = cmd->opcode;
  1373. while (cmd->count) {
  1374. dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n",
  1375. cmd->size, cmd->opcode, cmd->count);
  1376. ret = spi_nor_lock_device(nor);
  1377. if (ret)
  1378. goto destroy_erase_cmd_list;
  1379. ret = spi_nor_write_enable(nor);
  1380. if (ret) {
  1381. spi_nor_unlock_device(nor);
  1382. goto destroy_erase_cmd_list;
  1383. }
  1384. ret = spi_nor_erase_sector(nor, addr);
  1385. spi_nor_unlock_device(nor);
  1386. if (ret)
  1387. goto destroy_erase_cmd_list;
  1388. ret = spi_nor_wait_till_ready(nor);
  1389. if (ret)
  1390. goto destroy_erase_cmd_list;
  1391. addr += cmd->size;
  1392. cmd->count--;
  1393. }
  1394. list_del(&cmd->list);
  1395. kfree(cmd);
  1396. }
  1397. return 0;
  1398. destroy_erase_cmd_list:
  1399. spi_nor_destroy_erase_cmd_list(&erase_list);
  1400. return ret;
  1401. }
  1402. static int spi_nor_erase_dice(struct spi_nor *nor, loff_t addr,
  1403. size_t len, size_t die_size)
  1404. {
  1405. unsigned long timeout;
  1406. int ret;
  1407. /*
  1408. * Scale the timeout linearly with the size of the flash, with
  1409. * a minimum calibrated to an old 2MB flash. We could try to
  1410. * pull these from CFI/SFDP, but these values should be good
  1411. * enough for now.
  1412. */
  1413. timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
  1414. CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
  1415. (unsigned long)(nor->mtd.size / SZ_2M));
  1416. do {
  1417. ret = spi_nor_lock_device(nor);
  1418. if (ret)
  1419. return ret;
  1420. ret = spi_nor_write_enable(nor);
  1421. if (ret) {
  1422. spi_nor_unlock_device(nor);
  1423. return ret;
  1424. }
  1425. ret = spi_nor_erase_die(nor, addr, die_size);
  1426. spi_nor_unlock_device(nor);
  1427. if (ret)
  1428. return ret;
  1429. ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
  1430. if (ret)
  1431. return ret;
  1432. addr += die_size;
  1433. len -= die_size;
  1434. } while (len);
  1435. return 0;
  1436. }
  1437. /*
  1438. * Erase an address range on the nor chip. The address range may extend
  1439. * one or more erase sectors. Return an error if there is a problem erasing.
  1440. */
  1441. static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  1442. {
  1443. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1444. u8 n_dice = nor->params->n_dice;
  1445. bool multi_die_erase = false;
  1446. u32 addr, len, rem;
  1447. size_t die_size;
  1448. int ret;
  1449. dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  1450. (long long)instr->len);
  1451. if (spi_nor_has_uniform_erase(nor)) {
  1452. div_u64_rem(instr->len, mtd->erasesize, &rem);
  1453. if (rem)
  1454. return -EINVAL;
  1455. }
  1456. addr = instr->addr;
  1457. len = instr->len;
  1458. if (n_dice) {
  1459. die_size = div_u64(mtd->size, n_dice);
  1460. if (!(len & (die_size - 1)) && !(addr & (die_size - 1)))
  1461. multi_die_erase = true;
  1462. } else {
  1463. die_size = mtd->size;
  1464. }
  1465. ret = spi_nor_prep_and_lock_pe(nor, instr->addr, instr->len);
  1466. if (ret)
  1467. return ret;
  1468. /* chip (die) erase? */
  1469. if ((len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) ||
  1470. multi_die_erase) {
  1471. ret = spi_nor_erase_dice(nor, addr, len, die_size);
  1472. if (ret)
  1473. goto erase_err;
  1474. /* REVISIT in some cases we could speed up erasing large regions
  1475. * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
  1476. * to use "small sector erase", but that's not always optimal.
  1477. */
  1478. /* "sector"-at-a-time erase */
  1479. } else if (spi_nor_has_uniform_erase(nor)) {
  1480. while (len) {
  1481. ret = spi_nor_lock_device(nor);
  1482. if (ret)
  1483. goto erase_err;
  1484. ret = spi_nor_write_enable(nor);
  1485. if (ret) {
  1486. spi_nor_unlock_device(nor);
  1487. goto erase_err;
  1488. }
  1489. ret = spi_nor_erase_sector(nor, addr);
  1490. spi_nor_unlock_device(nor);
  1491. if (ret)
  1492. goto erase_err;
  1493. ret = spi_nor_wait_till_ready(nor);
  1494. if (ret)
  1495. goto erase_err;
  1496. addr += mtd->erasesize;
  1497. len -= mtd->erasesize;
  1498. }
  1499. /* erase multiple sectors */
  1500. } else {
  1501. ret = spi_nor_erase_multi_sectors(nor, addr, len);
  1502. if (ret)
  1503. goto erase_err;
  1504. }
  1505. ret = spi_nor_write_disable(nor);
  1506. erase_err:
  1507. spi_nor_unlock_and_unprep_pe(nor, instr->addr, instr->len);
  1508. return ret;
  1509. }
  1510. /**
  1511. * spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status
  1512. * Register 1.
  1513. * @nor: pointer to a 'struct spi_nor'
  1514. *
  1515. * Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories.
  1516. *
  1517. * Return: 0 on success, -errno otherwise.
  1518. */
  1519. int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor)
  1520. {
  1521. int ret;
  1522. ret = spi_nor_read_sr(nor, nor->bouncebuf);
  1523. if (ret)
  1524. return ret;
  1525. if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)
  1526. return 0;
  1527. nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6;
  1528. return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]);
  1529. }
  1530. /**
  1531. * spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status
  1532. * Register 2.
  1533. * @nor: pointer to a 'struct spi_nor'.
  1534. *
  1535. * Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories.
  1536. *
  1537. * Return: 0 on success, -errno otherwise.
  1538. */
  1539. int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)
  1540. {
  1541. int ret;
  1542. if (nor->flags & SNOR_F_NO_READ_CR)
  1543. return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1);
  1544. ret = spi_nor_read_cr(nor, nor->bouncebuf);
  1545. if (ret)
  1546. return ret;
  1547. if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)
  1548. return 0;
  1549. nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1;
  1550. return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]);
  1551. }
  1552. /**
  1553. * spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2.
  1554. * @nor: pointer to a 'struct spi_nor'
  1555. *
  1556. * Set the Quad Enable (QE) bit in the Status Register 2.
  1557. *
  1558. * This is one of the procedures to set the QE bit described in the SFDP
  1559. * (JESD216 rev B) specification but no manufacturer using this procedure has
  1560. * been identified yet, hence the name of the function.
  1561. *
  1562. * Return: 0 on success, -errno otherwise.
  1563. */
  1564. int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor)
  1565. {
  1566. u8 *sr2 = nor->bouncebuf;
  1567. int ret;
  1568. u8 sr2_written;
  1569. /* Check current Quad Enable bit value. */
  1570. ret = spi_nor_read_sr2(nor, sr2);
  1571. if (ret)
  1572. return ret;
  1573. if (*sr2 & SR2_QUAD_EN_BIT7)
  1574. return 0;
  1575. /* Update the Quad Enable bit. */
  1576. *sr2 |= SR2_QUAD_EN_BIT7;
  1577. ret = spi_nor_write_sr2(nor, sr2);
  1578. if (ret)
  1579. return ret;
  1580. sr2_written = *sr2;
  1581. /* Read back and check it. */
  1582. ret = spi_nor_read_sr2(nor, sr2);
  1583. if (ret)
  1584. return ret;
  1585. if (*sr2 != sr2_written) {
  1586. dev_dbg(nor->dev, "SR2: Read back test failed\n");
  1587. return -EIO;
  1588. }
  1589. return 0;
  1590. }
  1591. static const struct spi_nor_manufacturer *manufacturers[] = {
  1592. &spi_nor_atmel,
  1593. &spi_nor_eon,
  1594. &spi_nor_esmt,
  1595. &spi_nor_everspin,
  1596. &spi_nor_gigadevice,
  1597. &spi_nor_intel,
  1598. &spi_nor_issi,
  1599. &spi_nor_macronix,
  1600. &spi_nor_micron,
  1601. &spi_nor_st,
  1602. &spi_nor_spansion,
  1603. &spi_nor_sst,
  1604. &spi_nor_winbond,
  1605. &spi_nor_xmc,
  1606. };
  1607. static const struct flash_info spi_nor_generic_flash = {
  1608. .name = "spi-nor-generic",
  1609. };
  1610. static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
  1611. const u8 *id)
  1612. {
  1613. const struct flash_info *part;
  1614. unsigned int i, j;
  1615. for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
  1616. for (j = 0; j < manufacturers[i]->nparts; j++) {
  1617. part = &manufacturers[i]->parts[j];
  1618. if (part->id &&
  1619. !memcmp(part->id->bytes, id, part->id->len)) {
  1620. nor->manufacturer = manufacturers[i];
  1621. return part;
  1622. }
  1623. }
  1624. }
  1625. return NULL;
  1626. }
  1627. static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
  1628. {
  1629. const struct flash_info *info;
  1630. u8 *id = nor->bouncebuf;
  1631. int ret;
  1632. ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto);
  1633. if (ret) {
  1634. dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret);
  1635. return ERR_PTR(ret);
  1636. }
  1637. /* Cache the complete flash ID. */
  1638. nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL);
  1639. if (!nor->id)
  1640. return ERR_PTR(-ENOMEM);
  1641. info = spi_nor_match_id(nor, id);
  1642. /* Fallback to a generic flash described only by its SFDP data. */
  1643. if (!info) {
  1644. ret = spi_nor_check_sfdp_signature(nor);
  1645. if (!ret)
  1646. info = &spi_nor_generic_flash;
  1647. }
  1648. if (!info) {
  1649. dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
  1650. SPI_NOR_MAX_ID_LEN, id);
  1651. return ERR_PTR(-ENODEV);
  1652. }
  1653. return info;
  1654. }
  1655. /*
  1656. * On Octal DTR capable flashes, reads cannot start or end at an odd
  1657. * address in Octal DTR mode. Extra bytes need to be read at the start
  1658. * or end to make sure both the start address and length remain even.
  1659. */
  1660. static int spi_nor_octal_dtr_read(struct spi_nor *nor, loff_t from, size_t len,
  1661. u_char *buf)
  1662. {
  1663. u_char *tmp_buf;
  1664. size_t tmp_len;
  1665. loff_t start, end;
  1666. int ret, bytes_read;
  1667. if (IS_ALIGNED(from, 2) && IS_ALIGNED(len, 2))
  1668. return spi_nor_read_data(nor, from, len, buf);
  1669. else if (IS_ALIGNED(from, 2) && len > PAGE_SIZE)
  1670. return spi_nor_read_data(nor, from, round_down(len, PAGE_SIZE),
  1671. buf);
  1672. tmp_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1673. if (!tmp_buf)
  1674. return -ENOMEM;
  1675. start = round_down(from, 2);
  1676. end = round_up(from + len, 2);
  1677. /*
  1678. * Avoid allocating too much memory. The requested read length might be
  1679. * quite large. Allocating a buffer just as large (slightly bigger, in
  1680. * fact) would put unnecessary memory pressure on the system.
  1681. *
  1682. * For example if the read is from 3 to 1M, then this will read from 2
  1683. * to 4098. The reads from 4098 to 1M will then not need a temporary
  1684. * buffer so they can proceed as normal.
  1685. */
  1686. tmp_len = min_t(size_t, end - start, PAGE_SIZE);
  1687. ret = spi_nor_read_data(nor, start, tmp_len, tmp_buf);
  1688. if (ret == 0) {
  1689. ret = -EIO;
  1690. goto out;
  1691. }
  1692. if (ret < 0)
  1693. goto out;
  1694. /*
  1695. * More bytes are read than actually requested, but that number can't be
  1696. * reported to the calling function or it will confuse its calculations.
  1697. * Calculate how many of the _requested_ bytes were read.
  1698. */
  1699. bytes_read = ret;
  1700. if (from != start)
  1701. ret -= from - start;
  1702. /*
  1703. * Only account for extra bytes at the end if they were actually read.
  1704. * For example, if the total length was truncated because of temporary
  1705. * buffer size limit then the adjustment for the extra bytes at the end
  1706. * is not needed.
  1707. */
  1708. if (start + bytes_read == end)
  1709. ret -= end - (from + len);
  1710. memcpy(buf, tmp_buf + (from - start), ret);
  1711. out:
  1712. kfree(tmp_buf);
  1713. return ret;
  1714. }
  1715. static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  1716. size_t *retlen, u_char *buf)
  1717. {
  1718. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1719. loff_t from_lock = from;
  1720. size_t len_lock = len;
  1721. ssize_t ret;
  1722. dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  1723. ret = spi_nor_prep_and_lock_rd(nor, from_lock, len_lock);
  1724. if (ret)
  1725. return ret;
  1726. while (len) {
  1727. loff_t addr = from;
  1728. if (nor->read_proto == SNOR_PROTO_8_8_8_DTR)
  1729. ret = spi_nor_octal_dtr_read(nor, addr, len, buf);
  1730. else
  1731. ret = spi_nor_read_data(nor, addr, len, buf);
  1732. if (ret == 0) {
  1733. /* We shouldn't see 0-length reads */
  1734. ret = -EIO;
  1735. goto read_err;
  1736. }
  1737. if (ret < 0)
  1738. goto read_err;
  1739. WARN_ON(ret > len);
  1740. *retlen += ret;
  1741. buf += ret;
  1742. from += ret;
  1743. len -= ret;
  1744. }
  1745. ret = 0;
  1746. read_err:
  1747. spi_nor_unlock_and_unprep_rd(nor, from_lock, len_lock);
  1748. return ret;
  1749. }
  1750. /*
  1751. * On Octal DTR capable flashes, writes cannot start or end at an odd address
  1752. * in Octal DTR mode. Extra 0xff bytes need to be appended or prepended to
  1753. * make sure the start address and end address are even. 0xff is used because
  1754. * on NOR flashes a program operation can only flip bits from 1 to 0, not the
  1755. * other way round. 0 to 1 flip needs to happen via erases.
  1756. */
  1757. static int spi_nor_octal_dtr_write(struct spi_nor *nor, loff_t to, size_t len,
  1758. const u8 *buf)
  1759. {
  1760. u8 *tmp_buf;
  1761. size_t bytes_written;
  1762. loff_t start, end;
  1763. int ret;
  1764. if (IS_ALIGNED(to, 2) && IS_ALIGNED(len, 2))
  1765. return spi_nor_write_data(nor, to, len, buf);
  1766. tmp_buf = kmalloc(nor->params->page_size, GFP_KERNEL);
  1767. if (!tmp_buf)
  1768. return -ENOMEM;
  1769. memset(tmp_buf, 0xff, nor->params->page_size);
  1770. start = round_down(to, 2);
  1771. end = round_up(to + len, 2);
  1772. memcpy(tmp_buf + (to - start), buf, len);
  1773. ret = spi_nor_write_data(nor, start, end - start, tmp_buf);
  1774. if (ret == 0) {
  1775. ret = -EIO;
  1776. goto out;
  1777. }
  1778. if (ret < 0)
  1779. goto out;
  1780. /*
  1781. * More bytes are written than actually requested, but that number can't
  1782. * be reported to the calling function or it will confuse its
  1783. * calculations. Calculate how many of the _requested_ bytes were
  1784. * written.
  1785. */
  1786. bytes_written = ret;
  1787. if (to != start)
  1788. ret -= to - start;
  1789. /*
  1790. * Only account for extra bytes at the end if they were actually
  1791. * written. For example, if for some reason the controller could only
  1792. * complete a partial write then the adjustment for the extra bytes at
  1793. * the end is not needed.
  1794. */
  1795. if (start + bytes_written == end)
  1796. ret -= end - (to + len);
  1797. out:
  1798. kfree(tmp_buf);
  1799. return ret;
  1800. }
  1801. /*
  1802. * Write an address range to the nor chip. Data must be written in
  1803. * FLASH_PAGESIZE chunks. The address range may be any size provided
  1804. * it is within the physical boundaries.
  1805. */
  1806. static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  1807. size_t *retlen, const u_char *buf)
  1808. {
  1809. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1810. size_t i;
  1811. ssize_t ret;
  1812. u32 page_size = nor->params->page_size;
  1813. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1814. ret = spi_nor_prep_and_lock_pe(nor, to, len);
  1815. if (ret)
  1816. return ret;
  1817. for (i = 0; i < len; ) {
  1818. ssize_t written;
  1819. loff_t addr = to + i;
  1820. size_t page_offset = addr & (page_size - 1);
  1821. /* the size of data remaining on the first page */
  1822. size_t page_remain = min_t(size_t, page_size - page_offset, len - i);
  1823. ret = spi_nor_lock_device(nor);
  1824. if (ret)
  1825. goto write_err;
  1826. ret = spi_nor_write_enable(nor);
  1827. if (ret) {
  1828. spi_nor_unlock_device(nor);
  1829. goto write_err;
  1830. }
  1831. if (nor->write_proto == SNOR_PROTO_8_8_8_DTR)
  1832. ret = spi_nor_octal_dtr_write(nor, addr, page_remain,
  1833. buf + i);
  1834. else
  1835. ret = spi_nor_write_data(nor, addr, page_remain,
  1836. buf + i);
  1837. spi_nor_unlock_device(nor);
  1838. if (ret < 0)
  1839. goto write_err;
  1840. written = ret;
  1841. ret = spi_nor_wait_till_ready(nor);
  1842. if (ret)
  1843. goto write_err;
  1844. *retlen += written;
  1845. i += written;
  1846. }
  1847. write_err:
  1848. spi_nor_unlock_and_unprep_pe(nor, to, len);
  1849. return ret;
  1850. }
  1851. static int spi_nor_check(struct spi_nor *nor)
  1852. {
  1853. if (!nor->dev ||
  1854. (!nor->spimem && !nor->controller_ops) ||
  1855. (!nor->spimem && nor->controller_ops &&
  1856. (!nor->controller_ops->read ||
  1857. !nor->controller_ops->write ||
  1858. !nor->controller_ops->read_reg ||
  1859. !nor->controller_ops->write_reg))) {
  1860. pr_err("spi-nor: please fill all the necessary fields!\n");
  1861. return -EINVAL;
  1862. }
  1863. if (nor->spimem && nor->controller_ops) {
  1864. dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
  1865. return -EINVAL;
  1866. }
  1867. return 0;
  1868. }
  1869. void
  1870. spi_nor_set_read_settings(struct spi_nor_read_command *read,
  1871. u8 num_mode_clocks,
  1872. u8 num_wait_states,
  1873. u8 opcode,
  1874. enum spi_nor_protocol proto)
  1875. {
  1876. read->num_mode_clocks = num_mode_clocks;
  1877. read->num_wait_states = num_wait_states;
  1878. read->opcode = opcode;
  1879. read->proto = proto;
  1880. }
  1881. void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
  1882. enum spi_nor_protocol proto)
  1883. {
  1884. pp->opcode = opcode;
  1885. pp->proto = proto;
  1886. }
  1887. static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
  1888. {
  1889. size_t i;
  1890. for (i = 0; i < size; i++)
  1891. if (table[i][0] == (int)hwcaps)
  1892. return table[i][1];
  1893. return -EINVAL;
  1894. }
  1895. int spi_nor_hwcaps_read2cmd(u32 hwcaps)
  1896. {
  1897. static const int hwcaps_read2cmd[][2] = {
  1898. { SNOR_HWCAPS_READ, SNOR_CMD_READ },
  1899. { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
  1900. { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
  1901. { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
  1902. { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
  1903. { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
  1904. { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
  1905. { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
  1906. { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
  1907. { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
  1908. { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
  1909. { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
  1910. { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
  1911. { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
  1912. { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
  1913. { SNOR_HWCAPS_READ_8_8_8_DTR, SNOR_CMD_READ_8_8_8_DTR },
  1914. };
  1915. return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
  1916. ARRAY_SIZE(hwcaps_read2cmd));
  1917. }
  1918. int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
  1919. {
  1920. static const int hwcaps_pp2cmd[][2] = {
  1921. { SNOR_HWCAPS_PP, SNOR_CMD_PP },
  1922. { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
  1923. { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
  1924. { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
  1925. { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
  1926. { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
  1927. { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
  1928. { SNOR_HWCAPS_PP_8_8_8_DTR, SNOR_CMD_PP_8_8_8_DTR },
  1929. };
  1930. return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
  1931. ARRAY_SIZE(hwcaps_pp2cmd));
  1932. }
  1933. /**
  1934. * spi_nor_spimem_check_read_pp_op - check if a read or a page program operation is
  1935. * supported by controller
  1936. *@nor: pointer to a 'struct spi_nor'
  1937. *@op: pointer to op template to be checked
  1938. *
  1939. * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
  1940. */
  1941. static int spi_nor_spimem_check_read_pp_op(struct spi_nor *nor,
  1942. struct spi_mem_op *op)
  1943. {
  1944. /*
  1945. * First test with 4 address bytes. The opcode itself might
  1946. * be a 3B addressing opcode but we don't care, because
  1947. * SPI controller implementation should not check the opcode,
  1948. * but just the sequence.
  1949. */
  1950. op->addr.nbytes = 4;
  1951. if (!spi_mem_supports_op(nor->spimem, op)) {
  1952. if (nor->params->size > SZ_16M)
  1953. return -EOPNOTSUPP;
  1954. /* If flash size <= 16MB, 3 address bytes are sufficient */
  1955. op->addr.nbytes = 3;
  1956. if (!spi_mem_supports_op(nor->spimem, op))
  1957. return -EOPNOTSUPP;
  1958. }
  1959. return 0;
  1960. }
  1961. /**
  1962. * spi_nor_spimem_check_readop - check if the read op is supported
  1963. * by controller
  1964. *@nor: pointer to a 'struct spi_nor'
  1965. *@read: pointer to op template to be checked
  1966. *
  1967. * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
  1968. */
  1969. static int spi_nor_spimem_check_readop(struct spi_nor *nor,
  1970. const struct spi_nor_read_command *read)
  1971. {
  1972. struct spi_mem_op op = SPI_NOR_READ_OP(read->opcode);
  1973. spi_nor_spimem_setup_op(nor, &op, read->proto);
  1974. /* convert the dummy cycles to the number of bytes */
  1975. op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
  1976. op.dummy.buswidth / 8;
  1977. if (spi_nor_protocol_is_dtr(nor->read_proto))
  1978. op.dummy.nbytes *= 2;
  1979. return spi_nor_spimem_check_read_pp_op(nor, &op);
  1980. }
  1981. /**
  1982. * spi_nor_spimem_check_pp - check if the page program op is supported
  1983. * by controller
  1984. *@nor: pointer to a 'struct spi_nor'
  1985. *@pp: pointer to op template to be checked
  1986. *
  1987. * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
  1988. */
  1989. static int spi_nor_spimem_check_pp(struct spi_nor *nor,
  1990. const struct spi_nor_pp_command *pp)
  1991. {
  1992. struct spi_mem_op op = SPI_NOR_PP_OP(pp->opcode);
  1993. spi_nor_spimem_setup_op(nor, &op, pp->proto);
  1994. return spi_nor_spimem_check_read_pp_op(nor, &op);
  1995. }
  1996. /**
  1997. * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
  1998. * based on SPI controller capabilities
  1999. * @nor: pointer to a 'struct spi_nor'
  2000. * @hwcaps: pointer to resulting capabilities after adjusting
  2001. * according to controller and flash's capability
  2002. */
  2003. static void
  2004. spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
  2005. {
  2006. struct spi_nor_flash_parameter *params = nor->params;
  2007. unsigned int cap;
  2008. /* X-X-X modes are not supported yet, mask them all. */
  2009. *hwcaps &= ~SNOR_HWCAPS_X_X_X;
  2010. /*
  2011. * If the reset line is broken, we do not want to enter a stateful
  2012. * mode.
  2013. */
  2014. if (nor->flags & SNOR_F_BROKEN_RESET)
  2015. *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
  2016. for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
  2017. int rdidx, ppidx;
  2018. if (!(*hwcaps & BIT(cap)))
  2019. continue;
  2020. rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
  2021. if (rdidx >= 0 &&
  2022. spi_nor_spimem_check_readop(nor, &params->reads[rdidx]))
  2023. *hwcaps &= ~BIT(cap);
  2024. ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
  2025. if (ppidx < 0)
  2026. continue;
  2027. if (spi_nor_spimem_check_pp(nor,
  2028. &params->page_programs[ppidx]))
  2029. *hwcaps &= ~BIT(cap);
  2030. }
  2031. /* Some SPI controllers might not support CR read opcode. */
  2032. if (!(nor->flags & SNOR_F_NO_READ_CR)) {
  2033. struct spi_mem_op op = SPI_NOR_RDCR_OP(nor->bouncebuf);
  2034. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  2035. if (!spi_mem_supports_op(nor->spimem, &op))
  2036. nor->flags |= SNOR_F_NO_READ_CR;
  2037. }
  2038. }
  2039. /**
  2040. * spi_nor_set_erase_type() - set a SPI NOR erase type
  2041. * @erase: pointer to a structure that describes a SPI NOR erase type
  2042. * @size: the size of the sector/block erased by the erase type
  2043. * @opcode: the SPI command op code to erase the sector/block
  2044. */
  2045. void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
  2046. u8 opcode)
  2047. {
  2048. erase->size = size;
  2049. erase->opcode = opcode;
  2050. /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
  2051. erase->size_shift = ffs(erase->size) - 1;
  2052. erase->size_mask = (1 << erase->size_shift) - 1;
  2053. }
  2054. /**
  2055. * spi_nor_mask_erase_type() - mask out a SPI NOR erase type
  2056. * @erase: pointer to a structure that describes a SPI NOR erase type
  2057. */
  2058. void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase)
  2059. {
  2060. erase->size = 0;
  2061. }
  2062. /**
  2063. * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
  2064. * @map: the erase map of the SPI NOR
  2065. * @erase_mask: bitmask encoding erase types that can erase the entire
  2066. * flash memory
  2067. * @flash_size: the spi nor flash memory size
  2068. */
  2069. void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
  2070. u8 erase_mask, u64 flash_size)
  2071. {
  2072. map->uniform_region.offset = 0;
  2073. map->uniform_region.size = flash_size;
  2074. map->uniform_region.erase_mask = erase_mask;
  2075. map->regions = &map->uniform_region;
  2076. map->n_regions = 1;
  2077. }
  2078. int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
  2079. const struct sfdp_parameter_header *bfpt_header,
  2080. const struct sfdp_bfpt *bfpt)
  2081. {
  2082. int ret;
  2083. if (nor->manufacturer && nor->manufacturer->fixups &&
  2084. nor->manufacturer->fixups->post_bfpt) {
  2085. ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header,
  2086. bfpt);
  2087. if (ret)
  2088. return ret;
  2089. }
  2090. if (nor->info->fixups && nor->info->fixups->post_bfpt)
  2091. return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt);
  2092. return 0;
  2093. }
  2094. static int spi_nor_select_read(struct spi_nor *nor,
  2095. u32 shared_hwcaps)
  2096. {
  2097. int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
  2098. const struct spi_nor_read_command *read;
  2099. if (best_match < 0)
  2100. return -EINVAL;
  2101. cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
  2102. if (cmd < 0)
  2103. return -EINVAL;
  2104. read = &nor->params->reads[cmd];
  2105. nor->read_opcode = read->opcode;
  2106. nor->read_proto = read->proto;
  2107. /*
  2108. * In the SPI NOR framework, we don't need to make the difference
  2109. * between mode clock cycles and wait state clock cycles.
  2110. * Indeed, the value of the mode clock cycles is used by a QSPI
  2111. * flash memory to know whether it should enter or leave its 0-4-4
  2112. * (Continuous Read / XIP) mode.
  2113. * eXecution In Place is out of the scope of the mtd sub-system.
  2114. * Hence we choose to merge both mode and wait state clock cycles
  2115. * into the so called dummy clock cycles.
  2116. */
  2117. nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
  2118. return 0;
  2119. }
  2120. static int spi_nor_select_pp(struct spi_nor *nor,
  2121. u32 shared_hwcaps)
  2122. {
  2123. int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
  2124. const struct spi_nor_pp_command *pp;
  2125. if (best_match < 0)
  2126. return -EINVAL;
  2127. cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
  2128. if (cmd < 0)
  2129. return -EINVAL;
  2130. pp = &nor->params->page_programs[cmd];
  2131. nor->program_opcode = pp->opcode;
  2132. nor->write_proto = pp->proto;
  2133. return 0;
  2134. }
  2135. /**
  2136. * spi_nor_select_uniform_erase() - select optimum uniform erase type
  2137. * @map: the erase map of the SPI NOR
  2138. *
  2139. * Once the optimum uniform sector erase command is found, disable all the
  2140. * other.
  2141. *
  2142. * Return: pointer to erase type on success, NULL otherwise.
  2143. */
  2144. static const struct spi_nor_erase_type *
  2145. spi_nor_select_uniform_erase(struct spi_nor_erase_map *map)
  2146. {
  2147. const struct spi_nor_erase_type *tested_erase, *erase = NULL;
  2148. int i;
  2149. u8 uniform_erase_type = map->uniform_region.erase_mask;
  2150. /*
  2151. * Search for the biggest erase size, except for when compiled
  2152. * to use 4k erases.
  2153. */
  2154. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  2155. if (!(uniform_erase_type & BIT(i)))
  2156. continue;
  2157. tested_erase = &map->erase_type[i];
  2158. /* Skip masked erase types. */
  2159. if (!tested_erase->size)
  2160. continue;
  2161. /*
  2162. * If the current erase size is the 4k one, stop here,
  2163. * we have found the right uniform Sector Erase command.
  2164. */
  2165. if (IS_ENABLED(CONFIG_MTD_SPI_NOR_USE_4K_SECTORS) &&
  2166. tested_erase->size == SZ_4K) {
  2167. erase = tested_erase;
  2168. break;
  2169. }
  2170. /*
  2171. * Otherwise, the current erase size is still a valid candidate.
  2172. * Select the biggest valid candidate.
  2173. */
  2174. if (!erase && tested_erase->size)
  2175. erase = tested_erase;
  2176. /* keep iterating to find the wanted_size */
  2177. }
  2178. if (!erase)
  2179. return NULL;
  2180. /* Disable all other Sector Erase commands. */
  2181. map->uniform_region.erase_mask = BIT(erase - map->erase_type);
  2182. return erase;
  2183. }
  2184. static int spi_nor_select_erase(struct spi_nor *nor)
  2185. {
  2186. struct spi_nor_erase_map *map = &nor->params->erase_map;
  2187. const struct spi_nor_erase_type *erase = NULL;
  2188. struct mtd_info *mtd = &nor->mtd;
  2189. int i;
  2190. /*
  2191. * The previous implementation handling Sector Erase commands assumed
  2192. * that the SPI flash memory has an uniform layout then used only one
  2193. * of the supported erase sizes for all Sector Erase commands.
  2194. * So to be backward compatible, the new implementation also tries to
  2195. * manage the SPI flash memory as uniform with a single erase sector
  2196. * size, when possible.
  2197. */
  2198. if (spi_nor_has_uniform_erase(nor)) {
  2199. erase = spi_nor_select_uniform_erase(map);
  2200. if (!erase)
  2201. return -EINVAL;
  2202. nor->erase_opcode = erase->opcode;
  2203. mtd->erasesize = erase->size;
  2204. return 0;
  2205. }
  2206. /*
  2207. * For non-uniform SPI flash memory, set mtd->erasesize to the
  2208. * maximum erase sector size. No need to set nor->erase_opcode.
  2209. */
  2210. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  2211. if (map->erase_type[i].size) {
  2212. erase = &map->erase_type[i];
  2213. break;
  2214. }
  2215. }
  2216. if (!erase)
  2217. return -EINVAL;
  2218. mtd->erasesize = erase->size;
  2219. return 0;
  2220. }
  2221. static int spi_nor_set_addr_nbytes(struct spi_nor *nor)
  2222. {
  2223. if (nor->params->addr_nbytes) {
  2224. nor->addr_nbytes = nor->params->addr_nbytes;
  2225. } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) {
  2226. /*
  2227. * In 8D-8D-8D mode, one byte takes half a cycle to transfer. So
  2228. * in this protocol an odd addr_nbytes cannot be used because
  2229. * then the address phase would only span a cycle and a half.
  2230. * Half a cycle would be left over. We would then have to start
  2231. * the dummy phase in the middle of a cycle and so too the data
  2232. * phase, and we will end the transaction with half a cycle left
  2233. * over.
  2234. *
  2235. * Force all 8D-8D-8D flashes to use an addr_nbytes of 4 to
  2236. * avoid this situation.
  2237. */
  2238. nor->addr_nbytes = 4;
  2239. } else if (nor->info->addr_nbytes) {
  2240. nor->addr_nbytes = nor->info->addr_nbytes;
  2241. } else {
  2242. nor->addr_nbytes = 3;
  2243. }
  2244. if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) {
  2245. /* enable 4-byte addressing if the device exceeds 16MiB */
  2246. nor->addr_nbytes = 4;
  2247. }
  2248. if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) {
  2249. dev_dbg(nor->dev, "The number of address bytes is too large: %u\n",
  2250. nor->addr_nbytes);
  2251. return -EINVAL;
  2252. }
  2253. /* Set 4byte opcodes when possible. */
  2254. if (nor->addr_nbytes == 4 && nor->flags & SNOR_F_4B_OPCODES &&
  2255. !(nor->flags & SNOR_F_HAS_4BAIT))
  2256. spi_nor_set_4byte_opcodes(nor);
  2257. return 0;
  2258. }
  2259. static int spi_nor_setup(struct spi_nor *nor,
  2260. const struct spi_nor_hwcaps *hwcaps)
  2261. {
  2262. struct spi_nor_flash_parameter *params = nor->params;
  2263. u32 ignored_mask, shared_mask;
  2264. int err;
  2265. /*
  2266. * Keep only the hardware capabilities supported by both the SPI
  2267. * controller and the SPI flash memory.
  2268. */
  2269. shared_mask = hwcaps->mask & params->hwcaps.mask;
  2270. if (nor->spimem) {
  2271. /*
  2272. * When called from spi_nor_probe(), all caps are set and we
  2273. * need to discard some of them based on what the SPI
  2274. * controller actually supports (using spi_mem_supports_op()).
  2275. */
  2276. spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
  2277. } else {
  2278. /*
  2279. * SPI n-n-n protocols are not supported when the SPI
  2280. * controller directly implements the spi_nor interface.
  2281. * Yet another reason to switch to spi-mem.
  2282. */
  2283. ignored_mask = SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR;
  2284. if (shared_mask & ignored_mask) {
  2285. dev_dbg(nor->dev,
  2286. "SPI n-n-n protocols are not supported.\n");
  2287. shared_mask &= ~ignored_mask;
  2288. }
  2289. }
  2290. /* Select the (Fast) Read command. */
  2291. err = spi_nor_select_read(nor, shared_mask);
  2292. if (err) {
  2293. dev_dbg(nor->dev,
  2294. "can't select read settings supported by both the SPI controller and memory.\n");
  2295. return err;
  2296. }
  2297. /* Select the Page Program command. */
  2298. err = spi_nor_select_pp(nor, shared_mask);
  2299. if (err) {
  2300. dev_dbg(nor->dev,
  2301. "can't select write settings supported by both the SPI controller and memory.\n");
  2302. return err;
  2303. }
  2304. /* Select the Sector Erase command. */
  2305. err = spi_nor_select_erase(nor);
  2306. if (err) {
  2307. dev_dbg(nor->dev,
  2308. "can't select erase settings supported by both the SPI controller and memory.\n");
  2309. return err;
  2310. }
  2311. return spi_nor_set_addr_nbytes(nor);
  2312. }
  2313. /**
  2314. * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
  2315. * settings based on MFR register and ->default_init() hook.
  2316. * @nor: pointer to a 'struct spi_nor'.
  2317. */
  2318. static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
  2319. {
  2320. if (nor->manufacturer && nor->manufacturer->fixups &&
  2321. nor->manufacturer->fixups->default_init)
  2322. nor->manufacturer->fixups->default_init(nor);
  2323. if (nor->info->fixups && nor->info->fixups->default_init)
  2324. nor->info->fixups->default_init(nor);
  2325. }
  2326. /**
  2327. * spi_nor_no_sfdp_init_params() - Initialize the flash's parameters and
  2328. * settings based on nor->info->sfdp_flags. This method should be called only by
  2329. * flashes that do not define SFDP tables. If the flash supports SFDP but the
  2330. * information is wrong and the settings from this function can not be retrieved
  2331. * by parsing SFDP, one should instead use the fixup hooks and update the wrong
  2332. * bits.
  2333. * @nor: pointer to a 'struct spi_nor'.
  2334. */
  2335. static void spi_nor_no_sfdp_init_params(struct spi_nor *nor)
  2336. {
  2337. struct spi_nor_flash_parameter *params = nor->params;
  2338. struct spi_nor_erase_map *map = &params->erase_map;
  2339. const struct flash_info *info = nor->info;
  2340. const u8 no_sfdp_flags = info->no_sfdp_flags;
  2341. u8 i, erase_mask;
  2342. if (no_sfdp_flags & SPI_NOR_DUAL_READ) {
  2343. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  2344. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
  2345. 0, 8, SPINOR_OP_READ_1_1_2,
  2346. SNOR_PROTO_1_1_2);
  2347. }
  2348. if (no_sfdp_flags & SPI_NOR_QUAD_READ) {
  2349. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  2350. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
  2351. 0, 8, SPINOR_OP_READ_1_1_4,
  2352. SNOR_PROTO_1_1_4);
  2353. }
  2354. if (no_sfdp_flags & SPI_NOR_OCTAL_READ) {
  2355. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
  2356. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_8],
  2357. 0, 8, SPINOR_OP_READ_1_1_8,
  2358. SNOR_PROTO_1_1_8);
  2359. }
  2360. if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_READ) {
  2361. params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
  2362. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR],
  2363. 0, 20, SPINOR_OP_READ_FAST,
  2364. SNOR_PROTO_8_8_8_DTR);
  2365. }
  2366. if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_PP) {
  2367. params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
  2368. /*
  2369. * Since xSPI Page Program opcode is backward compatible with
  2370. * Legacy SPI, use Legacy SPI opcode there as well.
  2371. */
  2372. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_8_8_8_DTR],
  2373. SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
  2374. }
  2375. /*
  2376. * Sector Erase settings. Sort Erase Types in ascending order, with the
  2377. * smallest erase size starting at BIT(0).
  2378. */
  2379. erase_mask = 0;
  2380. i = 0;
  2381. if (no_sfdp_flags & SECT_4K) {
  2382. erase_mask |= BIT(i);
  2383. spi_nor_set_erase_type(&map->erase_type[i], 4096u,
  2384. SPINOR_OP_BE_4K);
  2385. i++;
  2386. }
  2387. erase_mask |= BIT(i);
  2388. spi_nor_set_erase_type(&map->erase_type[i],
  2389. info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE,
  2390. SPINOR_OP_SE);
  2391. spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
  2392. }
  2393. /**
  2394. * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined
  2395. * in the JESD216 SFDP standard, thus can not be retrieved when parsing SFDP.
  2396. * @nor: pointer to a 'struct spi_nor'
  2397. */
  2398. static void spi_nor_init_flags(struct spi_nor *nor)
  2399. {
  2400. struct device_node *np = spi_nor_get_flash_node(nor);
  2401. const u16 flags = nor->info->flags;
  2402. if (of_property_read_bool(np, "broken-flash-reset"))
  2403. nor->flags |= SNOR_F_BROKEN_RESET;
  2404. if (of_property_read_bool(np, "no-wp"))
  2405. nor->flags |= SNOR_F_NO_WP;
  2406. if (flags & SPI_NOR_SWP_IS_VOLATILE)
  2407. nor->flags |= SNOR_F_SWP_IS_VOLATILE;
  2408. if (flags & SPI_NOR_HAS_LOCK)
  2409. nor->flags |= SNOR_F_HAS_LOCK;
  2410. if (flags & SPI_NOR_HAS_TB) {
  2411. nor->flags |= SNOR_F_HAS_SR_TB;
  2412. if (flags & SPI_NOR_TB_SR_BIT6)
  2413. nor->flags |= SNOR_F_HAS_SR_TB_BIT6;
  2414. }
  2415. if (flags & SPI_NOR_4BIT_BP) {
  2416. nor->flags |= SNOR_F_HAS_4BIT_BP;
  2417. if (flags & SPI_NOR_BP3_SR_BIT6)
  2418. nor->flags |= SNOR_F_HAS_SR_BP3_BIT6;
  2419. }
  2420. if (flags & SPI_NOR_RWW && nor->params->n_banks > 1 &&
  2421. !nor->controller_ops)
  2422. nor->flags |= SNOR_F_RWW;
  2423. }
  2424. /**
  2425. * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not
  2426. * be discovered by SFDP for this particular flash because the SFDP table that
  2427. * indicates this support is not defined in the flash. In case the table for
  2428. * this support is defined but has wrong values, one should instead use a
  2429. * post_sfdp() hook to set the SNOR_F equivalent flag.
  2430. * @nor: pointer to a 'struct spi_nor'
  2431. */
  2432. static void spi_nor_init_fixup_flags(struct spi_nor *nor)
  2433. {
  2434. const u8 fixup_flags = nor->info->fixup_flags;
  2435. if (fixup_flags & SPI_NOR_4B_OPCODES)
  2436. nor->flags |= SNOR_F_4B_OPCODES;
  2437. if (fixup_flags & SPI_NOR_IO_MODE_EN_VOLATILE)
  2438. nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
  2439. }
  2440. /**
  2441. * spi_nor_late_init_params() - Late initialization of default flash parameters.
  2442. * @nor: pointer to a 'struct spi_nor'
  2443. *
  2444. * Used to initialize flash parameters that are not declared in the JESD216
  2445. * SFDP standard, or where SFDP tables are not defined at all.
  2446. * Will replace the spi_nor_manufacturer_init_params() method.
  2447. */
  2448. static int spi_nor_late_init_params(struct spi_nor *nor)
  2449. {
  2450. struct spi_nor_flash_parameter *params = nor->params;
  2451. int ret;
  2452. if (nor->manufacturer && nor->manufacturer->fixups &&
  2453. nor->manufacturer->fixups->late_init) {
  2454. ret = nor->manufacturer->fixups->late_init(nor);
  2455. if (ret)
  2456. return ret;
  2457. }
  2458. /* Needed by some flashes late_init hooks. */
  2459. spi_nor_init_flags(nor);
  2460. if (nor->info->fixups && nor->info->fixups->late_init) {
  2461. ret = nor->info->fixups->late_init(nor);
  2462. if (ret)
  2463. return ret;
  2464. }
  2465. if (!nor->params->die_erase_opcode)
  2466. nor->params->die_erase_opcode = SPINOR_OP_CHIP_ERASE;
  2467. /* Default method kept for backward compatibility. */
  2468. if (!params->set_4byte_addr_mode)
  2469. params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_brwr;
  2470. spi_nor_init_fixup_flags(nor);
  2471. /*
  2472. * NOR protection support. When locking_ops are not provided, we pick
  2473. * the default ones.
  2474. */
  2475. if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
  2476. spi_nor_init_default_locking_ops(nor);
  2477. if (params->n_banks > 1)
  2478. params->bank_size = div_u64(params->size, params->n_banks);
  2479. return 0;
  2480. }
  2481. /**
  2482. * spi_nor_sfdp_init_params_deprecated() - Deprecated way of initializing flash
  2483. * parameters and settings based on JESD216 SFDP standard.
  2484. * @nor: pointer to a 'struct spi_nor'.
  2485. *
  2486. * The method has a roll-back mechanism: in case the SFDP parsing fails, the
  2487. * legacy flash parameters and settings will be restored.
  2488. */
  2489. static void spi_nor_sfdp_init_params_deprecated(struct spi_nor *nor)
  2490. {
  2491. struct spi_nor_flash_parameter sfdp_params;
  2492. memcpy(&sfdp_params, nor->params, sizeof(sfdp_params));
  2493. if (spi_nor_parse_sfdp(nor)) {
  2494. memcpy(nor->params, &sfdp_params, sizeof(*nor->params));
  2495. nor->flags &= ~SNOR_F_4B_OPCODES;
  2496. }
  2497. }
  2498. /**
  2499. * spi_nor_init_params_deprecated() - Deprecated way of initializing flash
  2500. * parameters and settings.
  2501. * @nor: pointer to a 'struct spi_nor'.
  2502. *
  2503. * The method assumes that flash doesn't support SFDP so it initializes flash
  2504. * parameters in spi_nor_no_sfdp_init_params() which later on can be overwritten
  2505. * when parsing SFDP, if supported.
  2506. */
  2507. static void spi_nor_init_params_deprecated(struct spi_nor *nor)
  2508. {
  2509. spi_nor_no_sfdp_init_params(nor);
  2510. spi_nor_manufacturer_init_params(nor);
  2511. if (nor->info->no_sfdp_flags & (SPI_NOR_DUAL_READ |
  2512. SPI_NOR_QUAD_READ |
  2513. SPI_NOR_OCTAL_READ |
  2514. SPI_NOR_OCTAL_DTR_READ))
  2515. spi_nor_sfdp_init_params_deprecated(nor);
  2516. }
  2517. /**
  2518. * spi_nor_init_default_params() - Default initialization of flash parameters
  2519. * and settings. Done for all flashes, regardless is they define SFDP tables
  2520. * or not.
  2521. * @nor: pointer to a 'struct spi_nor'.
  2522. */
  2523. static void spi_nor_init_default_params(struct spi_nor *nor)
  2524. {
  2525. struct spi_nor_flash_parameter *params = nor->params;
  2526. const struct flash_info *info = nor->info;
  2527. struct device_node *np = spi_nor_get_flash_node(nor);
  2528. params->quad_enable = spi_nor_sr2_bit1_quad_enable;
  2529. params->otp.org = info->otp;
  2530. /* Default to 16-bit Write Status (01h) Command */
  2531. nor->flags |= SNOR_F_HAS_16BIT_SR;
  2532. /* Set SPI NOR sizes. */
  2533. params->writesize = 1;
  2534. params->size = info->size;
  2535. params->bank_size = params->size;
  2536. params->page_size = info->page_size ?: SPI_NOR_DEFAULT_PAGE_SIZE;
  2537. params->n_banks = info->n_banks ?: SPI_NOR_DEFAULT_N_BANKS;
  2538. /* Default to Fast Read for non-DT and enable it if requested by DT. */
  2539. if (!np || of_property_read_bool(np, "m25p,fast-read"))
  2540. params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
  2541. /* (Fast) Read settings. */
  2542. params->hwcaps.mask |= SNOR_HWCAPS_READ;
  2543. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
  2544. 0, 0, SPINOR_OP_READ,
  2545. SNOR_PROTO_1_1_1);
  2546. if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
  2547. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
  2548. 0, 8, SPINOR_OP_READ_FAST,
  2549. SNOR_PROTO_1_1_1);
  2550. /* Page Program settings. */
  2551. params->hwcaps.mask |= SNOR_HWCAPS_PP;
  2552. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
  2553. SPINOR_OP_PP, SNOR_PROTO_1_1_1);
  2554. if (info->flags & SPI_NOR_QUAD_PP) {
  2555. params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
  2556. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
  2557. SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
  2558. }
  2559. }
  2560. /**
  2561. * spi_nor_init_params() - Initialize the flash's parameters and settings.
  2562. * @nor: pointer to a 'struct spi_nor'.
  2563. *
  2564. * The flash parameters and settings are initialized based on a sequence of
  2565. * calls that are ordered by priority:
  2566. *
  2567. * 1/ Default flash parameters initialization. The initializations are done
  2568. * based on nor->info data:
  2569. * spi_nor_info_init_params()
  2570. *
  2571. * which can be overwritten by:
  2572. * 2/ Manufacturer flash parameters initialization. The initializations are
  2573. * done based on MFR register, or when the decisions can not be done solely
  2574. * based on MFR, by using specific flash_info tweeks, ->default_init():
  2575. * spi_nor_manufacturer_init_params()
  2576. *
  2577. * which can be overwritten by:
  2578. * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
  2579. * should be more accurate that the above.
  2580. * spi_nor_parse_sfdp() or spi_nor_no_sfdp_init_params()
  2581. *
  2582. * Please note that there is a ->post_bfpt() fixup hook that can overwrite
  2583. * the flash parameters and settings immediately after parsing the Basic
  2584. * Flash Parameter Table.
  2585. * spi_nor_post_sfdp_fixups() is called after the SFDP tables are parsed.
  2586. * It is used to tweak various flash parameters when information provided
  2587. * by the SFDP tables are wrong.
  2588. *
  2589. * which can be overwritten by:
  2590. * 4/ Late flash parameters initialization, used to initialize flash
  2591. * parameters that are not declared in the JESD216 SFDP standard, or where SFDP
  2592. * tables are not defined at all.
  2593. * spi_nor_late_init_params()
  2594. *
  2595. * Return: 0 on success, -errno otherwise.
  2596. */
  2597. static int spi_nor_init_params(struct spi_nor *nor)
  2598. {
  2599. int ret;
  2600. nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL);
  2601. if (!nor->params)
  2602. return -ENOMEM;
  2603. spi_nor_init_default_params(nor);
  2604. if (spi_nor_needs_sfdp(nor)) {
  2605. ret = spi_nor_parse_sfdp(nor);
  2606. if (ret) {
  2607. dev_err(nor->dev, "BFPT parsing failed. Please consider using SPI_NOR_SKIP_SFDP when declaring the flash\n");
  2608. return ret;
  2609. }
  2610. } else if (nor->info->no_sfdp_flags & SPI_NOR_SKIP_SFDP) {
  2611. spi_nor_no_sfdp_init_params(nor);
  2612. } else {
  2613. spi_nor_init_params_deprecated(nor);
  2614. }
  2615. ret = spi_nor_late_init_params(nor);
  2616. if (ret)
  2617. return ret;
  2618. if (WARN_ON(!is_power_of_2(nor->params->page_size)))
  2619. return -EINVAL;
  2620. return 0;
  2621. }
  2622. /** spi_nor_set_octal_dtr() - enable or disable Octal DTR I/O.
  2623. * @nor: pointer to a 'struct spi_nor'
  2624. * @enable: whether to enable or disable Octal DTR
  2625. *
  2626. * Return: 0 on success, -errno otherwise.
  2627. */
  2628. static int spi_nor_set_octal_dtr(struct spi_nor *nor, bool enable)
  2629. {
  2630. int ret;
  2631. if (!nor->params->set_octal_dtr)
  2632. return 0;
  2633. if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
  2634. nor->write_proto == SNOR_PROTO_8_8_8_DTR))
  2635. return 0;
  2636. if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE))
  2637. return 0;
  2638. ret = nor->params->set_octal_dtr(nor, enable);
  2639. if (ret)
  2640. return ret;
  2641. if (enable)
  2642. nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
  2643. else
  2644. nor->reg_proto = SNOR_PROTO_1_1_1;
  2645. return 0;
  2646. }
  2647. /**
  2648. * spi_nor_quad_enable() - enable Quad I/O if needed.
  2649. * @nor: pointer to a 'struct spi_nor'
  2650. *
  2651. * Return: 0 on success, -errno otherwise.
  2652. */
  2653. static int spi_nor_quad_enable(struct spi_nor *nor)
  2654. {
  2655. if (!nor->params->quad_enable)
  2656. return 0;
  2657. if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
  2658. spi_nor_get_protocol_width(nor->write_proto) == 4))
  2659. return 0;
  2660. return nor->params->quad_enable(nor);
  2661. }
  2662. /**
  2663. * spi_nor_set_4byte_addr_mode() - Set address mode.
  2664. * @nor: pointer to a 'struct spi_nor'.
  2665. * @enable: enable/disable 4 byte address mode.
  2666. *
  2667. * Return: 0 on success, -errno otherwise.
  2668. */
  2669. int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
  2670. {
  2671. struct spi_nor_flash_parameter *params = nor->params;
  2672. int ret;
  2673. if (enable) {
  2674. /*
  2675. * If the RESET# pin isn't hooked up properly, or the system
  2676. * otherwise doesn't perform a reset command in the boot
  2677. * sequence, it's impossible to 100% protect against unexpected
  2678. * reboots (e.g., crashes). Warn the user (or hopefully, system
  2679. * designer) that this is bad.
  2680. */
  2681. WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
  2682. "enabling reset hack; may not recover from unexpected reboots\n");
  2683. }
  2684. ret = params->set_4byte_addr_mode(nor, enable);
  2685. if (ret && ret != -EOPNOTSUPP)
  2686. return ret;
  2687. if (enable) {
  2688. params->addr_nbytes = 4;
  2689. params->addr_mode_nbytes = 4;
  2690. } else {
  2691. params->addr_nbytes = 3;
  2692. params->addr_mode_nbytes = 3;
  2693. }
  2694. return 0;
  2695. }
  2696. static int spi_nor_init(struct spi_nor *nor)
  2697. {
  2698. int err;
  2699. err = spi_nor_set_octal_dtr(nor, true);
  2700. if (err) {
  2701. dev_dbg(nor->dev, "octal mode not supported\n");
  2702. return err;
  2703. }
  2704. err = spi_nor_quad_enable(nor);
  2705. if (err) {
  2706. dev_dbg(nor->dev, "quad mode not supported\n");
  2707. return err;
  2708. }
  2709. /*
  2710. * Some SPI NOR flashes are write protected by default after a power-on
  2711. * reset cycle, in order to avoid inadvertent writes during power-up.
  2712. * Backward compatibility imposes to unlock the entire flash memory
  2713. * array at power-up by default. Depending on the kernel configuration
  2714. * (1) do nothing, (2) always unlock the entire flash array or (3)
  2715. * unlock the entire flash array only when the software write
  2716. * protection bits are volatile. The latter is indicated by
  2717. * SNOR_F_SWP_IS_VOLATILE.
  2718. */
  2719. if (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE) ||
  2720. (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE_ON_VOLATILE) &&
  2721. nor->flags & SNOR_F_SWP_IS_VOLATILE))
  2722. spi_nor_try_unlock_all(nor);
  2723. if (nor->addr_nbytes == 4 &&
  2724. nor->read_proto != SNOR_PROTO_8_8_8_DTR &&
  2725. !(nor->flags & SNOR_F_4B_OPCODES))
  2726. return spi_nor_set_4byte_addr_mode(nor, true);
  2727. return 0;
  2728. }
  2729. /**
  2730. * spi_nor_soft_reset() - Perform a software reset
  2731. * @nor: pointer to 'struct spi_nor'
  2732. *
  2733. * Performs a "Soft Reset and Enter Default Protocol Mode" sequence which resets
  2734. * the device to its power-on-reset state. This is useful when the software has
  2735. * made some changes to device (volatile) registers and needs to reset it before
  2736. * shutting down, for example.
  2737. *
  2738. * Not every flash supports this sequence. The same set of opcodes might be used
  2739. * for some other operation on a flash that does not support this. Support for
  2740. * this sequence can be discovered via SFDP in the BFPT table.
  2741. *
  2742. * Return: 0 on success, -errno otherwise.
  2743. */
  2744. static void spi_nor_soft_reset(struct spi_nor *nor)
  2745. {
  2746. struct spi_mem_op op;
  2747. int ret;
  2748. op = (struct spi_mem_op)SPINOR_SRSTEN_OP;
  2749. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  2750. ret = spi_mem_exec_op(nor->spimem, &op);
  2751. if (ret) {
  2752. if (ret != -EOPNOTSUPP)
  2753. dev_warn(nor->dev, "Software reset failed: %d\n", ret);
  2754. return;
  2755. }
  2756. op = (struct spi_mem_op)SPINOR_SRST_OP;
  2757. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  2758. ret = spi_mem_exec_op(nor->spimem, &op);
  2759. if (ret) {
  2760. dev_warn(nor->dev, "Software reset failed: %d\n", ret);
  2761. return;
  2762. }
  2763. /*
  2764. * Software Reset is not instant, and the delay varies from flash to
  2765. * flash. Looking at a few flashes, most range somewhere below 100
  2766. * microseconds. So, sleep for a range of 200-400 us.
  2767. */
  2768. usleep_range(SPI_NOR_SRST_SLEEP_MIN, SPI_NOR_SRST_SLEEP_MAX);
  2769. }
  2770. /* mtd suspend handler */
  2771. static int spi_nor_suspend(struct mtd_info *mtd)
  2772. {
  2773. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  2774. int ret;
  2775. /* Disable octal DTR mode if we enabled it. */
  2776. ret = spi_nor_set_octal_dtr(nor, false);
  2777. if (ret)
  2778. dev_err(nor->dev, "suspend() failed\n");
  2779. return ret;
  2780. }
  2781. /* mtd resume handler */
  2782. static void spi_nor_resume(struct mtd_info *mtd)
  2783. {
  2784. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  2785. struct device *dev = nor->dev;
  2786. int ret;
  2787. /* re-initialize the nor chip */
  2788. ret = spi_nor_init(nor);
  2789. if (ret)
  2790. dev_err(dev, "resume() failed\n");
  2791. }
  2792. static int spi_nor_get_device(struct mtd_info *mtd)
  2793. {
  2794. struct mtd_info *master = mtd_get_master(mtd);
  2795. struct spi_nor *nor = mtd_to_spi_nor(master);
  2796. struct device *dev;
  2797. if (nor->spimem)
  2798. dev = nor->spimem->spi->controller->dev.parent;
  2799. else
  2800. dev = nor->dev;
  2801. if (!try_module_get(dev->driver->owner))
  2802. return -ENODEV;
  2803. return 0;
  2804. }
  2805. static void spi_nor_put_device(struct mtd_info *mtd)
  2806. {
  2807. struct mtd_info *master = mtd_get_master(mtd);
  2808. struct spi_nor *nor = mtd_to_spi_nor(master);
  2809. struct device *dev;
  2810. if (nor->spimem)
  2811. dev = nor->spimem->spi->controller->dev.parent;
  2812. else
  2813. dev = nor->dev;
  2814. module_put(dev->driver->owner);
  2815. }
  2816. static void spi_nor_restore(struct spi_nor *nor)
  2817. {
  2818. int ret;
  2819. /* restore the addressing mode */
  2820. if (nor->addr_nbytes == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
  2821. nor->flags & SNOR_F_BROKEN_RESET) {
  2822. ret = spi_nor_set_4byte_addr_mode(nor, false);
  2823. if (ret)
  2824. /*
  2825. * Do not stop the execution in the hope that the flash
  2826. * will default to the 3-byte address mode after the
  2827. * software reset.
  2828. */
  2829. dev_err(nor->dev, "Failed to exit 4-byte address mode, err = %d\n", ret);
  2830. }
  2831. if (nor->flags & SNOR_F_SOFT_RESET)
  2832. spi_nor_soft_reset(nor);
  2833. }
  2834. static const struct flash_info *spi_nor_match_name(struct spi_nor *nor,
  2835. const char *name)
  2836. {
  2837. unsigned int i, j;
  2838. for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
  2839. for (j = 0; j < manufacturers[i]->nparts; j++) {
  2840. if (manufacturers[i]->parts[j].name &&
  2841. !strcmp(name, manufacturers[i]->parts[j].name)) {
  2842. nor->manufacturer = manufacturers[i];
  2843. return &manufacturers[i]->parts[j];
  2844. }
  2845. }
  2846. }
  2847. return NULL;
  2848. }
  2849. static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
  2850. const char *name)
  2851. {
  2852. const struct flash_info *info = NULL;
  2853. if (name)
  2854. info = spi_nor_match_name(nor, name);
  2855. /*
  2856. * Auto-detect if chip name wasn't specified or not found, or the chip
  2857. * has an ID. If the chip supposedly has an ID, we also do an
  2858. * auto-detection to compare it later.
  2859. */
  2860. if (!info || info->id) {
  2861. const struct flash_info *jinfo;
  2862. jinfo = spi_nor_detect(nor);
  2863. if (IS_ERR(jinfo))
  2864. return jinfo;
  2865. /*
  2866. * If caller has specified name of flash model that can normally
  2867. * be detected using JEDEC, let's verify it.
  2868. */
  2869. if (info && jinfo != info)
  2870. dev_warn(nor->dev, "found %s, expected %s\n",
  2871. jinfo->name, info->name);
  2872. /* If info was set before, JEDEC knows better. */
  2873. info = jinfo;
  2874. }
  2875. return info;
  2876. }
  2877. static u32
  2878. spi_nor_get_region_erasesize(const struct spi_nor_erase_region *region,
  2879. const struct spi_nor_erase_type *erase_type)
  2880. {
  2881. int i;
  2882. if (region->overlaid)
  2883. return region->size;
  2884. for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
  2885. if (region->erase_mask & BIT(i))
  2886. return erase_type[i].size;
  2887. }
  2888. return 0;
  2889. }
  2890. static int spi_nor_set_mtd_eraseregions(struct spi_nor *nor)
  2891. {
  2892. const struct spi_nor_erase_map *map = &nor->params->erase_map;
  2893. const struct spi_nor_erase_region *region = map->regions;
  2894. struct mtd_erase_region_info *mtd_region;
  2895. struct mtd_info *mtd = &nor->mtd;
  2896. u32 erasesize, i;
  2897. mtd_region = devm_kcalloc(nor->dev, map->n_regions, sizeof(*mtd_region),
  2898. GFP_KERNEL);
  2899. if (!mtd_region)
  2900. return -ENOMEM;
  2901. for (i = 0; i < map->n_regions; i++) {
  2902. erasesize = spi_nor_get_region_erasesize(&region[i],
  2903. map->erase_type);
  2904. if (!erasesize)
  2905. return -EINVAL;
  2906. mtd_region[i].erasesize = erasesize;
  2907. mtd_region[i].numblocks = div_u64(region[i].size, erasesize);
  2908. mtd_region[i].offset = region[i].offset;
  2909. }
  2910. mtd->numeraseregions = map->n_regions;
  2911. mtd->eraseregions = mtd_region;
  2912. return 0;
  2913. }
  2914. static int spi_nor_set_mtd_info(struct spi_nor *nor)
  2915. {
  2916. struct mtd_info *mtd = &nor->mtd;
  2917. struct device *dev = nor->dev;
  2918. spi_nor_set_mtd_locking_ops(nor);
  2919. spi_nor_set_mtd_otp_ops(nor);
  2920. mtd->dev.parent = dev;
  2921. if (!mtd->name)
  2922. mtd->name = dev_name(dev);
  2923. mtd->type = MTD_NORFLASH;
  2924. mtd->flags = MTD_CAP_NORFLASH;
  2925. /* Unset BIT_WRITEABLE to enable JFFS2 write buffer for ECC'd NOR */
  2926. if (nor->flags & SNOR_F_ECC)
  2927. mtd->flags &= ~MTD_BIT_WRITEABLE;
  2928. if (nor->info->flags & SPI_NOR_NO_ERASE)
  2929. mtd->flags |= MTD_NO_ERASE;
  2930. else
  2931. mtd->_erase = spi_nor_erase;
  2932. mtd->writesize = nor->params->writesize;
  2933. mtd->writebufsize = nor->params->page_size;
  2934. mtd->size = nor->params->size;
  2935. mtd->_read = spi_nor_read;
  2936. /* Might be already set by some SST flashes. */
  2937. if (!mtd->_write)
  2938. mtd->_write = spi_nor_write;
  2939. mtd->_suspend = spi_nor_suspend;
  2940. mtd->_resume = spi_nor_resume;
  2941. mtd->_get_device = spi_nor_get_device;
  2942. mtd->_put_device = spi_nor_put_device;
  2943. if (!spi_nor_has_uniform_erase(nor))
  2944. return spi_nor_set_mtd_eraseregions(nor);
  2945. return 0;
  2946. }
  2947. static int spi_nor_hw_reset(struct spi_nor *nor)
  2948. {
  2949. struct gpio_desc *reset;
  2950. reset = devm_gpiod_get_optional(nor->dev, "reset", GPIOD_OUT_LOW);
  2951. if (IS_ERR_OR_NULL(reset))
  2952. return PTR_ERR_OR_ZERO(reset);
  2953. /*
  2954. * Experimental delay values by looking at different flash device
  2955. * vendors datasheets.
  2956. */
  2957. usleep_range(1, 5);
  2958. gpiod_set_value_cansleep(reset, 1);
  2959. usleep_range(100, 150);
  2960. gpiod_set_value_cansleep(reset, 0);
  2961. usleep_range(1000, 1200);
  2962. return 0;
  2963. }
  2964. int spi_nor_scan(struct spi_nor *nor, const char *name,
  2965. const struct spi_nor_hwcaps *hwcaps)
  2966. {
  2967. const struct flash_info *info;
  2968. struct device *dev = nor->dev;
  2969. int ret;
  2970. ret = spi_nor_check(nor);
  2971. if (ret)
  2972. return ret;
  2973. /* Reset SPI protocol for all commands. */
  2974. nor->reg_proto = SNOR_PROTO_1_1_1;
  2975. nor->read_proto = SNOR_PROTO_1_1_1;
  2976. nor->write_proto = SNOR_PROTO_1_1_1;
  2977. /*
  2978. * We need the bounce buffer early to read/write registers when going
  2979. * through the spi-mem layer (buffers have to be DMA-able).
  2980. * For spi-mem drivers, we'll reallocate a new buffer if
  2981. * nor->params->page_size turns out to be greater than PAGE_SIZE (which
  2982. * shouldn't happen before long since NOR pages are usually less
  2983. * than 1KB) after spi_nor_scan() returns.
  2984. */
  2985. nor->bouncebuf_size = PAGE_SIZE;
  2986. nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
  2987. GFP_KERNEL);
  2988. if (!nor->bouncebuf)
  2989. return -ENOMEM;
  2990. ret = spi_nor_hw_reset(nor);
  2991. if (ret)
  2992. return ret;
  2993. info = spi_nor_get_flash_info(nor, name);
  2994. if (IS_ERR(info))
  2995. return PTR_ERR(info);
  2996. nor->info = info;
  2997. mutex_init(&nor->lock);
  2998. /* Init flash parameters based on flash_info struct and SFDP */
  2999. ret = spi_nor_init_params(nor);
  3000. if (ret)
  3001. return ret;
  3002. if (spi_nor_use_parallel_locking(nor))
  3003. init_waitqueue_head(&nor->rww.wait);
  3004. /*
  3005. * Configure the SPI memory:
  3006. * - select op codes for (Fast) Read, Page Program and Sector Erase.
  3007. * - set the number of dummy cycles (mode cycles + wait states).
  3008. * - set the SPI protocols for register and memory accesses.
  3009. * - set the number of address bytes.
  3010. */
  3011. ret = spi_nor_setup(nor, hwcaps);
  3012. if (ret)
  3013. return ret;
  3014. /* Send all the required SPI flash commands to initialize device */
  3015. ret = spi_nor_init(nor);
  3016. if (ret)
  3017. return ret;
  3018. /* No mtd_info fields should be used up to this point. */
  3019. ret = spi_nor_set_mtd_info(nor);
  3020. if (ret)
  3021. return ret;
  3022. dev_dbg(dev, "Manufacturer and device ID: %*phN\n",
  3023. SPI_NOR_MAX_ID_LEN, nor->id);
  3024. return 0;
  3025. }
  3026. EXPORT_SYMBOL_GPL(spi_nor_scan);
  3027. static int spi_nor_create_read_dirmap(struct spi_nor *nor)
  3028. {
  3029. struct spi_mem_dirmap_info info = {
  3030. .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
  3031. SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
  3032. SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
  3033. SPI_MEM_OP_DATA_IN(0, NULL, 0)),
  3034. .offset = 0,
  3035. .length = nor->params->size,
  3036. };
  3037. struct spi_mem_op *op = &info.op_tmpl;
  3038. spi_nor_spimem_setup_op(nor, op, nor->read_proto);
  3039. /* convert the dummy cycles to the number of bytes */
  3040. op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
  3041. if (spi_nor_protocol_is_dtr(nor->read_proto))
  3042. op->dummy.nbytes *= 2;
  3043. /*
  3044. * Since spi_nor_spimem_setup_op() only sets buswidth when the number
  3045. * of data bytes is non-zero, the data buswidth won't be set here. So,
  3046. * do it explicitly.
  3047. */
  3048. op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
  3049. nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
  3050. &info);
  3051. return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
  3052. }
  3053. static int spi_nor_create_write_dirmap(struct spi_nor *nor)
  3054. {
  3055. struct spi_mem_dirmap_info info = {
  3056. .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
  3057. SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
  3058. SPI_MEM_OP_NO_DUMMY,
  3059. SPI_MEM_OP_DATA_OUT(0, NULL, 0)),
  3060. .offset = 0,
  3061. .length = nor->params->size,
  3062. };
  3063. struct spi_mem_op *op = &info.op_tmpl;
  3064. if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
  3065. op->addr.nbytes = 0;
  3066. spi_nor_spimem_setup_op(nor, op, nor->write_proto);
  3067. /*
  3068. * Since spi_nor_spimem_setup_op() only sets buswidth when the number
  3069. * of data bytes is non-zero, the data buswidth won't be set here. So,
  3070. * do it explicitly.
  3071. */
  3072. op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
  3073. nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
  3074. &info);
  3075. return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
  3076. }
  3077. static int spi_nor_probe(struct spi_mem *spimem)
  3078. {
  3079. struct spi_device *spi = spimem->spi;
  3080. struct device *dev = &spi->dev;
  3081. struct flash_platform_data *data = dev_get_platdata(dev);
  3082. struct spi_nor *nor;
  3083. /*
  3084. * Enable all caps by default. The core will mask them after
  3085. * checking what's really supported using spi_mem_supports_op().
  3086. */
  3087. const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
  3088. char *flash_name;
  3089. int ret;
  3090. ret = devm_regulator_get_enable(dev, "vcc");
  3091. if (ret)
  3092. return ret;
  3093. nor = devm_kzalloc(dev, sizeof(*nor), GFP_KERNEL);
  3094. if (!nor)
  3095. return -ENOMEM;
  3096. nor->spimem = spimem;
  3097. nor->dev = dev;
  3098. spi_nor_set_flash_node(nor, dev->of_node);
  3099. spi_mem_set_drvdata(spimem, nor);
  3100. if (data && data->name)
  3101. nor->mtd.name = data->name;
  3102. if (!nor->mtd.name)
  3103. nor->mtd.name = spi_mem_get_name(spimem);
  3104. /*
  3105. * For some (historical?) reason many platforms provide two different
  3106. * names in flash_platform_data: "name" and "type". Quite often name is
  3107. * set to "m25p80" and then "type" provides a real chip name.
  3108. * If that's the case, respect "type" and ignore a "name".
  3109. */
  3110. if (data && data->type)
  3111. flash_name = data->type;
  3112. else if (!strcmp(spi->modalias, "spi-nor"))
  3113. flash_name = NULL; /* auto-detect */
  3114. else
  3115. flash_name = spi->modalias;
  3116. ret = spi_nor_scan(nor, flash_name, &hwcaps);
  3117. if (ret)
  3118. return ret;
  3119. spi_nor_debugfs_register(nor);
  3120. /*
  3121. * None of the existing parts have > 512B pages, but let's play safe
  3122. * and add this logic so that if anyone ever adds support for such
  3123. * a NOR we don't end up with buffer overflows.
  3124. */
  3125. if (nor->params->page_size > PAGE_SIZE) {
  3126. nor->bouncebuf_size = nor->params->page_size;
  3127. devm_kfree(dev, nor->bouncebuf);
  3128. nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
  3129. GFP_KERNEL);
  3130. if (!nor->bouncebuf)
  3131. return -ENOMEM;
  3132. }
  3133. ret = spi_nor_create_read_dirmap(nor);
  3134. if (ret)
  3135. return ret;
  3136. ret = spi_nor_create_write_dirmap(nor);
  3137. if (ret)
  3138. return ret;
  3139. return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
  3140. data ? data->nr_parts : 0);
  3141. }
  3142. static int spi_nor_remove(struct spi_mem *spimem)
  3143. {
  3144. struct spi_nor *nor = spi_mem_get_drvdata(spimem);
  3145. spi_nor_restore(nor);
  3146. /* Clean up MTD stuff. */
  3147. return mtd_device_unregister(&nor->mtd);
  3148. }
  3149. static void spi_nor_shutdown(struct spi_mem *spimem)
  3150. {
  3151. struct spi_nor *nor = spi_mem_get_drvdata(spimem);
  3152. spi_nor_restore(nor);
  3153. }
  3154. /*
  3155. * Do NOT add to this array without reading the following:
  3156. *
  3157. * Historically, many flash devices are bound to this driver by their name. But
  3158. * since most of these flash are compatible to some extent, and their
  3159. * differences can often be differentiated by the JEDEC read-ID command, we
  3160. * encourage new users to add support to the spi-nor library, and simply bind
  3161. * against a generic string here (e.g., "jedec,spi-nor").
  3162. *
  3163. * Many flash names are kept here in this list to keep them available
  3164. * as module aliases for existing platforms.
  3165. */
  3166. static const struct spi_device_id spi_nor_dev_ids[] = {
  3167. /*
  3168. * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
  3169. * hack around the fact that the SPI core does not provide uevent
  3170. * matching for .of_match_table
  3171. */
  3172. {"spi-nor"},
  3173. /*
  3174. * Entries not used in DTs that should be safe to drop after replacing
  3175. * them with "spi-nor" in platform data.
  3176. */
  3177. {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
  3178. /*
  3179. * Entries that were used in DTs without "jedec,spi-nor" fallback and
  3180. * should be kept for backward compatibility.
  3181. */
  3182. {"at25df321a"}, {"at25df641"}, {"at26df081a"},
  3183. {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
  3184. {"mx25l25635e"},{"mx66l51235l"},
  3185. {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
  3186. {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
  3187. {"s25fl064k"},
  3188. {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
  3189. {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
  3190. {"m25p64"}, {"m25p128"},
  3191. {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
  3192. {"w25q80bl"}, {"w25q128"}, {"w25q256"},
  3193. /* Flashes that can't be detected using JEDEC */
  3194. {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
  3195. {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
  3196. {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
  3197. /* Everspin MRAMs (non-JEDEC) */
  3198. { "mr25h128" }, /* 128 Kib, 40 MHz */
  3199. { "mr25h256" }, /* 256 Kib, 40 MHz */
  3200. { "mr25h10" }, /* 1 Mib, 40 MHz */
  3201. { "mr25h40" }, /* 4 Mib, 40 MHz */
  3202. { },
  3203. };
  3204. MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
  3205. static const struct of_device_id spi_nor_of_table[] = {
  3206. /*
  3207. * Generic compatibility for SPI NOR that can be identified by the
  3208. * JEDEC READ ID opcode (0x9F). Use this, if possible.
  3209. */
  3210. { .compatible = "jedec,spi-nor" },
  3211. { /* sentinel */ },
  3212. };
  3213. MODULE_DEVICE_TABLE(of, spi_nor_of_table);
  3214. /*
  3215. * REVISIT: many of these chips have deep power-down modes, which
  3216. * should clearly be entered on suspend() to minimize power use.
  3217. * And also when they're otherwise idle...
  3218. */
  3219. static struct spi_mem_driver spi_nor_driver = {
  3220. .spidrv = {
  3221. .driver = {
  3222. .name = "spi-nor",
  3223. .of_match_table = spi_nor_of_table,
  3224. .dev_groups = spi_nor_sysfs_groups,
  3225. },
  3226. .id_table = spi_nor_dev_ids,
  3227. },
  3228. .probe = spi_nor_probe,
  3229. .remove = spi_nor_remove,
  3230. .shutdown = spi_nor_shutdown,
  3231. };
  3232. static int __init spi_nor_module_init(void)
  3233. {
  3234. return spi_mem_driver_register(&spi_nor_driver);
  3235. }
  3236. module_init(spi_nor_module_init);
  3237. static void __exit spi_nor_module_exit(void)
  3238. {
  3239. spi_mem_driver_unregister(&spi_nor_driver);
  3240. spi_nor_debugfs_shutdown();
  3241. }
  3242. module_exit(spi_nor_module_exit);
  3243. MODULE_LICENSE("GPL v2");
  3244. MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
  3245. MODULE_AUTHOR("Mike Lavender");
  3246. MODULE_DESCRIPTION("framework for SPI NOR");