spi-nor.c 86 KB

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