core.c 96 KB

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