spi-nor.c 81 KB

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