writeprotect.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2010 Google Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "flash.h"
  23. #include "flashchips.h"
  24. #include "chipdrivers.h"
  25. #include "spi.h"
  26. #include "writeprotect.h"
  27. /*
  28. * The following procedures rely on look-up tables to match the user-specified
  29. * range with the chip's supported ranges. This turned out to be the most
  30. * elegant approach since diferent flash chips use different levels of
  31. * granularity and methods to determine protected ranges. In other words,
  32. * be stupid and simple since clever arithmetic will not work for many chips.
  33. */
  34. struct wp_range {
  35. unsigned int start; /* starting address */
  36. unsigned int len; /* len */
  37. };
  38. enum bit_state {
  39. OFF = 0,
  40. ON = 1,
  41. X = -1 /* don't care. Must be bigger than max # of bp. */
  42. };
  43. /*
  44. * Generic write-protection schema for 25-series SPI flash chips. This assumes
  45. * there is a status register that contains one or more consecutive bits which
  46. * determine which address range is protected.
  47. */
  48. struct status_register_layout {
  49. int bp0_pos; /* position of BP0 */
  50. int bp_bits; /* number of block protect bits */
  51. int srp_pos; /* position of status register protect enable bit */
  52. };
  53. struct generic_range {
  54. struct generic_modifier_bits m;
  55. unsigned int bp; /* block protect bitfield */
  56. struct wp_range range;
  57. };
  58. struct generic_wp {
  59. struct status_register_layout sr1; /* status register 1 */
  60. struct generic_range *ranges;
  61. /*
  62. * Some chips store modifier bits in one or more special control
  63. * registers instead of the status register like many older SPI NOR
  64. * flash chips did. get_modifier_bits() and set_modifier_bits() will do
  65. * any chip-specific operations necessary to get/set these bit values.
  66. */
  67. int (*get_modifier_bits)(const struct flashctx *flash,
  68. struct generic_modifier_bits *m);
  69. int (*set_modifier_bits)(const struct flashctx *flash,
  70. struct generic_modifier_bits *m);
  71. };
  72. /*
  73. * The following ranges and functions are useful for representing Winbond-
  74. * style writeprotect schema in which there are typically 5 bits of
  75. * relevant information stored in status register 1:
  76. * sec: This bit indicates the units (sectors vs. blocks)
  77. * tb: The top-bottom bit indicates if the affected range is at the top of
  78. * the flash memory's address space or at the bottom.
  79. * bp[2:0]: The number of affected sectors/blocks.
  80. */
  81. struct w25q_range {
  82. enum bit_state sec; /* if 1, bp[2:0] describe sectors */
  83. enum bit_state tb; /* top/bottom select */
  84. int bp; /* block protect bitfield */
  85. struct wp_range range;
  86. };
  87. /*
  88. * Mask to extract write-protect enable and range bits
  89. * Status register 1:
  90. * SRP0: bit 7
  91. * range(BP2-BP0): bit 4-2
  92. * Status register 2:
  93. * SRP1: bit 1
  94. */
  95. #define MASK_WP_AREA (0x9C)
  96. #define MASK_WP2_AREA (0x01)
  97. struct w25q_range en25f40_ranges[] = {
  98. { X, X, 0, {0, 0} }, /* none */
  99. { 0, 0, 0x1, {0x000000, 504 * 1024} },
  100. { 0, 0, 0x2, {0x000000, 496 * 1024} },
  101. { 0, 0, 0x3, {0x000000, 480 * 1024} },
  102. { 0, 0, 0x4, {0x000000, 448 * 1024} },
  103. { 0, 0, 0x5, {0x000000, 384 * 1024} },
  104. { 0, 0, 0x6, {0x000000, 256 * 1024} },
  105. { 0, 0, 0x7, {0x000000, 512 * 1024} },
  106. };
  107. struct w25q_range en25q40_ranges[] = {
  108. { 0, 0, 0, {0, 0} }, /* none */
  109. { 0, 0, 0x1, {0x000000, 504 * 1024} },
  110. { 0, 0, 0x2, {0x000000, 496 * 1024} },
  111. { 0, 0, 0x3, {0x000000, 480 * 1024} },
  112. { 0, 1, 0x0, {0x000000, 448 * 1024} },
  113. { 0, 1, 0x1, {0x000000, 384 * 1024} },
  114. { 0, 1, 0x2, {0x000000, 256 * 1024} },
  115. { 0, 1, 0x3, {0x000000, 512 * 1024} },
  116. };
  117. struct w25q_range en25q80_ranges[] = {
  118. { 0, 0, 0, {0, 0} }, /* none */
  119. { 0, 0, 0x1, {0x000000, 1016 * 1024} },
  120. { 0, 0, 0x2, {0x000000, 1008 * 1024} },
  121. { 0, 0, 0x3, {0x000000, 992 * 1024} },
  122. { 0, 0, 0x4, {0x000000, 960 * 1024} },
  123. { 0, 0, 0x5, {0x000000, 896 * 1024} },
  124. { 0, 0, 0x6, {0x000000, 768 * 1024} },
  125. { 0, 0, 0x7, {0x000000, 1024 * 1024} },
  126. };
  127. struct w25q_range en25q32_ranges[] = {
  128. { 0, 0, 0, {0, 0} }, /* none */
  129. { 0, 0, 0x1, {0x000000, 4032 * 1024} },
  130. { 0, 0, 0x2, {0x000000, 3968 * 1024} },
  131. { 0, 0, 0x3, {0x000000, 3840 * 1024} },
  132. { 0, 0, 0x4, {0x000000, 3584 * 1024} },
  133. { 0, 0, 0x5, {0x000000, 3072 * 1024} },
  134. { 0, 0, 0x6, {0x000000, 2048 * 1024} },
  135. { 0, 0, 0x7, {0x000000, 4096 * 1024} },
  136. { 0, 1, 0, {0, 0} }, /* none */
  137. { 0, 1, 0x1, {0x010000, 4032 * 1024} },
  138. { 0, 1, 0x2, {0x020000, 3968 * 1024} },
  139. { 0, 1, 0x3, {0x040000, 3840 * 1024} },
  140. { 0, 1, 0x4, {0x080000, 3584 * 1024} },
  141. { 0, 1, 0x5, {0x100000, 3072 * 1024} },
  142. { 0, 1, 0x6, {0x200000, 2048 * 1024} },
  143. { 0, 1, 0x7, {0x000000, 4096 * 1024} },
  144. };
  145. struct w25q_range en25q64_ranges[] = {
  146. { 0, 0, 0, {0, 0} }, /* none */
  147. { 0, 0, 0x1, {0x000000, 8128 * 1024} },
  148. { 0, 0, 0x2, {0x000000, 8064 * 1024} },
  149. { 0, 0, 0x3, {0x000000, 7936 * 1024} },
  150. { 0, 0, 0x4, {0x000000, 7680 * 1024} },
  151. { 0, 0, 0x5, {0x000000, 7168 * 1024} },
  152. { 0, 0, 0x6, {0x000000, 6144 * 1024} },
  153. { 0, 0, 0x7, {0x000000, 8192 * 1024} },
  154. { 0, 1, 0, {0, 0} }, /* none */
  155. { 0, 1, 0x1, {0x010000, 8128 * 1024} },
  156. { 0, 1, 0x2, {0x020000, 8064 * 1024} },
  157. { 0, 1, 0x3, {0x040000, 7936 * 1024} },
  158. { 0, 1, 0x4, {0x080000, 7680 * 1024} },
  159. { 0, 1, 0x5, {0x100000, 7168 * 1024} },
  160. { 0, 1, 0x6, {0x200000, 6144 * 1024} },
  161. { 0, 1, 0x7, {0x000000, 8192 * 1024} },
  162. };
  163. struct w25q_range en25q128_ranges[] = {
  164. { 0, 0, 0, {0, 0} }, /* none */
  165. { 0, 0, 0x1, {0x000000, 16320 * 1024} },
  166. { 0, 0, 0x2, {0x000000, 16256 * 1024} },
  167. { 0, 0, 0x3, {0x000000, 16128 * 1024} },
  168. { 0, 0, 0x4, {0x000000, 15872 * 1024} },
  169. { 0, 0, 0x5, {0x000000, 15360 * 1024} },
  170. { 0, 0, 0x6, {0x000000, 14336 * 1024} },
  171. { 0, 0, 0x7, {0x000000, 16384 * 1024} },
  172. { 0, 1, 0, {0, 0} }, /* none */
  173. { 0, 1, 0x1, {0x010000, 16320 * 1024} },
  174. { 0, 1, 0x2, {0x020000, 16256 * 1024} },
  175. { 0, 1, 0x3, {0x040000, 16128 * 1024} },
  176. { 0, 1, 0x4, {0x080000, 15872 * 1024} },
  177. { 0, 1, 0x5, {0x100000, 15360 * 1024} },
  178. { 0, 1, 0x6, {0x200000, 14336 * 1024} },
  179. { 0, 1, 0x7, {0x000000, 16384 * 1024} },
  180. };
  181. struct w25q_range en25s64_ranges[] = {
  182. { 0, 0, 0, {0, 0} }, /* none */
  183. { 0, 0, 0x1, {0x000000, 8064 * 1024} },
  184. { 0, 0, 0x2, {0x000000, 7936 * 1024} },
  185. { 0, 0, 0x3, {0x000000, 7680 * 1024} },
  186. { 0, 0, 0x4, {0x000000, 7168 * 1024} },
  187. { 0, 0, 0x5, {0x000000, 6144 * 1024} },
  188. { 0, 0, 0x6, {0x000000, 4096 * 1024} },
  189. { 0, 0, 0x7, {0x000000, 8192 * 1024} },
  190. { 0, 1, 0, {0, 0} }, /* none */
  191. { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
  192. { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
  193. { 0, 1, 0x3, {0x780000, 512 * 1024} },
  194. { 0, 1, 0x4, {0x700000, 1024 * 1024} },
  195. { 0, 1, 0x5, {0x600000, 2048 * 1024} },
  196. { 0, 1, 0x6, {0x400000, 4096 * 1024} },
  197. { 0, 1, 0x7, {0x000000, 8192 * 1024} },
  198. };
  199. /* mx25l1005 ranges also work for the mx25l1005c */
  200. static struct w25q_range mx25l1005_ranges[] = {
  201. { X, X, 0, {0, 0} }, /* none */
  202. { X, X, 0x1, {0x010000, 64 * 1024} },
  203. { X, X, 0x2, {0x000000, 128 * 1024} },
  204. { X, X, 0x3, {0x000000, 128 * 1024} },
  205. };
  206. static struct w25q_range mx25l2005_ranges[] = {
  207. { X, X, 0, {0, 0} }, /* none */
  208. { X, X, 0x1, {0x030000, 64 * 1024} },
  209. { X, X, 0x2, {0x020000, 128 * 1024} },
  210. { X, X, 0x3, {0x000000, 256 * 1024} },
  211. };
  212. static struct w25q_range mx25l4005_ranges[] = {
  213. { X, X, 0, {0, 0} }, /* none */
  214. { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
  215. { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
  216. { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
  217. { X, X, 0x4, {0x000000, 512 * 1024} },
  218. { X, X, 0x5, {0x000000, 512 * 1024} },
  219. { X, X, 0x6, {0x000000, 512 * 1024} },
  220. { X, X, 0x7, {0x000000, 512 * 1024} },
  221. };
  222. static struct w25q_range mx25l8005_ranges[] = {
  223. { X, X, 0, {0, 0} }, /* none */
  224. { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
  225. { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
  226. { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
  227. { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
  228. { X, X, 0x5, {0x000000, 1024 * 1024} },
  229. { X, X, 0x6, {0x000000, 1024 * 1024} },
  230. { X, X, 0x7, {0x000000, 1024 * 1024} },
  231. };
  232. #if 0
  233. /* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
  234. static struct w25q_range mx25l1605_ranges[] = {
  235. { X, X, 0, {0, 0} }, /* none */
  236. { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
  237. { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
  238. { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
  239. { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
  240. { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
  241. { X, X, 0x6, {0x000000, 2048 * 1024} },
  242. { X, X, 0x7, {0x000000, 2048 * 1024} },
  243. };
  244. #endif
  245. #if 0
  246. /* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
  247. static struct w25q_range mx25l6405_ranges[] = {
  248. { X, 0, 0, {0, 0} }, /* none */
  249. { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
  250. { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
  251. { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
  252. { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
  253. { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
  254. { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
  255. { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
  256. { X, 1, 0x0, {0x000000, 8192 * 1024} },
  257. { X, 1, 0x1, {0x000000, 8192 * 1024} },
  258. { X, 1, 0x2, {0x000000, 8192 * 1024} },
  259. { X, 1, 0x3, {0x000000, 8192 * 1024} },
  260. { X, 1, 0x4, {0x000000, 8192 * 1024} },
  261. { X, 1, 0x5, {0x000000, 8192 * 1024} },
  262. { X, 1, 0x6, {0x000000, 8192 * 1024} },
  263. { X, 1, 0x7, {0x000000, 8192 * 1024} },
  264. };
  265. #endif
  266. static struct w25q_range mx25l1605d_ranges[] = {
  267. { X, 0, 0, {0, 0} }, /* none */
  268. { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
  269. { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
  270. { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
  271. { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
  272. { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
  273. { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
  274. { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
  275. { X, 1, 0x0, {0x000000, 2048 * 1024} },
  276. { X, 1, 0x1, {0x000000, 2048 * 1024} },
  277. { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
  278. { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
  279. { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
  280. { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
  281. { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
  282. { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
  283. };
  284. /* FIXME: Is there an mx25l3205 (without a trailing letter)? */
  285. static struct w25q_range mx25l3205d_ranges[] = {
  286. { X, 0, 0, {0, 0} }, /* none */
  287. { X, 0, 0x1, {0x3f0000, 64 * 1024} },
  288. { X, 0, 0x2, {0x3e0000, 128 * 1024} },
  289. { X, 0, 0x3, {0x3c0000, 256 * 1024} },
  290. { X, 0, 0x4, {0x380000, 512 * 1024} },
  291. { X, 0, 0x5, {0x300000, 1024 * 1024} },
  292. { X, 0, 0x6, {0x200000, 2048 * 1024} },
  293. { X, 0, 0x7, {0x000000, 4096 * 1024} },
  294. { X, 1, 0x0, {0x000000, 4096 * 1024} },
  295. { X, 1, 0x1, {0x000000, 2048 * 1024} },
  296. { X, 1, 0x2, {0x000000, 3072 * 1024} },
  297. { X, 1, 0x3, {0x000000, 3584 * 1024} },
  298. { X, 1, 0x4, {0x000000, 3840 * 1024} },
  299. { X, 1, 0x5, {0x000000, 3968 * 1024} },
  300. { X, 1, 0x6, {0x000000, 4032 * 1024} },
  301. { X, 1, 0x7, {0x000000, 4096 * 1024} },
  302. };
  303. static struct w25q_range mx25u3235e_ranges[] = {
  304. { X, 0, 0, {0, 0} }, /* none */
  305. { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
  306. { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
  307. { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
  308. { 0, 0, 0x4, {0x380000, 512 * 1024} },
  309. { 0, 0, 0x5, {0x300000, 1024 * 1024} },
  310. { 0, 0, 0x6, {0x200000, 2048 * 1024} },
  311. { 0, 0, 0x7, {0x000000, 4096 * 1024} },
  312. { 0, 1, 0x0, {0x000000, 4096 * 1024} },
  313. { 0, 1, 0x1, {0x000000, 2048 * 1024} },
  314. { 0, 1, 0x2, {0x000000, 3072 * 1024} },
  315. { 0, 1, 0x3, {0x000000, 3584 * 1024} },
  316. { 0, 1, 0x4, {0x000000, 3840 * 1024} },
  317. { 0, 1, 0x5, {0x000000, 3968 * 1024} },
  318. { 0, 1, 0x6, {0x000000, 4032 * 1024} },
  319. { 0, 1, 0x7, {0x000000, 4096 * 1024} },
  320. };
  321. static struct w25q_range mx25u6435e_ranges[] = {
  322. { X, 0, 0, {0, 0} }, /* none */
  323. { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
  324. { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
  325. { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
  326. { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
  327. { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
  328. { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
  329. { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
  330. { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
  331. { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
  332. { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
  333. { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
  334. { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
  335. { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
  336. { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
  337. { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
  338. };
  339. static struct w25q_range n25q064_ranges[] = {
  340. /*
  341. * Note: For N25Q064, sec (usually in bit position 6) is called BP3
  342. * (block protect bit 3). It is only useful when all blocks are to
  343. * be write-protected.
  344. */
  345. { 0, 0, 0, {0, 0} }, /* none */
  346. { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
  347. { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
  348. { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
  349. { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
  350. { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
  351. { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
  352. { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
  353. { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
  354. { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
  355. { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
  356. { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
  357. { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
  358. { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
  359. { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
  360. { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
  361. { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
  362. { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
  363. { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
  364. { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
  365. { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
  366. { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
  367. { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
  368. };
  369. static struct w25q_range w25q16_ranges[] = {
  370. { X, X, 0, {0, 0} }, /* none */
  371. { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
  372. { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
  373. { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
  374. { 0, 0, 0x4, {0x180000, 512 * 1024} },
  375. { 0, 0, 0x5, {0x100000, 1024 * 1024} },
  376. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  377. { 0, 1, 0x2, {0x000000, 128 * 1024} },
  378. { 0, 1, 0x3, {0x000000, 256 * 1024} },
  379. { 0, 1, 0x4, {0x000000, 512 * 1024} },
  380. { 0, 1, 0x5, {0x000000, 1024 * 1024} },
  381. { X, X, 0x6, {0x000000, 2048 * 1024} },
  382. { X, X, 0x7, {0x000000, 2048 * 1024} },
  383. { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
  384. { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
  385. { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
  386. { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
  387. { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
  388. { 1, 1, 0x1, {0x000000, 4 * 1024} },
  389. { 1, 1, 0x2, {0x000000, 8 * 1024} },
  390. { 1, 1, 0x3, {0x000000, 16 * 1024} },
  391. { 1, 1, 0x4, {0x000000, 32 * 1024} },
  392. { 1, 1, 0x5, {0x000000, 32 * 1024} },
  393. };
  394. static struct w25q_range w25q32_ranges[] = {
  395. { X, X, 0, {0, 0} }, /* none */
  396. { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
  397. { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
  398. { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
  399. { 0, 0, 0x4, {0x380000, 512 * 1024} },
  400. { 0, 0, 0x5, {0x300000, 1024 * 1024} },
  401. { 0, 0, 0x6, {0x200000, 2048 * 1024} },
  402. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  403. { 0, 1, 0x2, {0x000000, 128 * 1024} },
  404. { 0, 1, 0x3, {0x000000, 256 * 1024} },
  405. { 0, 1, 0x4, {0x000000, 512 * 1024} },
  406. { 0, 1, 0x5, {0x000000, 1024 * 1024} },
  407. { 0, 1, 0x6, {0x000000, 2048 * 1024} },
  408. { X, X, 0x7, {0x000000, 4096 * 1024} },
  409. { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
  410. { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
  411. { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
  412. { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
  413. { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
  414. { 1, 1, 0x1, {0x000000, 4 * 1024} },
  415. { 1, 1, 0x2, {0x000000, 8 * 1024} },
  416. { 1, 1, 0x3, {0x000000, 16 * 1024} },
  417. { 1, 1, 0x4, {0x000000, 32 * 1024} },
  418. { 1, 1, 0x5, {0x000000, 32 * 1024} },
  419. };
  420. static struct w25q_range w25q80_ranges[] = {
  421. { X, X, 0, {0, 0} }, /* none */
  422. { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
  423. { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
  424. { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
  425. { 0, 0, 0x4, {0x080000, 512 * 1024} },
  426. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  427. { 0, 1, 0x2, {0x000000, 128 * 1024} },
  428. { 0, 1, 0x3, {0x000000, 256 * 1024} },
  429. { 0, 1, 0x4, {0x000000, 512 * 1024} },
  430. { X, X, 0x6, {0x000000, 1024 * 1024} },
  431. { X, X, 0x7, {0x000000, 1024 * 1024} },
  432. { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
  433. { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
  434. { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
  435. { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
  436. { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
  437. { 1, 1, 0x1, {0x000000, 4 * 1024} },
  438. { 1, 1, 0x2, {0x000000, 8 * 1024} },
  439. { 1, 1, 0x3, {0x000000, 16 * 1024} },
  440. { 1, 1, 0x4, {0x000000, 32 * 1024} },
  441. { 1, 1, 0x5, {0x000000, 32 * 1024} },
  442. };
  443. static struct w25q_range w25q64_ranges[] = {
  444. { X, X, 0, {0, 0} }, /* none */
  445. { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
  446. { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
  447. { 0, 0, 0x3, {0x780000, 512 * 1024} },
  448. { 0, 0, 0x4, {0x700000, 1024 * 1024} },
  449. { 0, 0, 0x5, {0x600000, 2048 * 1024} },
  450. { 0, 0, 0x6, {0x400000, 4096 * 1024} },
  451. { 0, 1, 0x1, {0x000000, 128 * 1024} },
  452. { 0, 1, 0x2, {0x000000, 256 * 1024} },
  453. { 0, 1, 0x3, {0x000000, 512 * 1024} },
  454. { 0, 1, 0x4, {0x000000, 1024 * 1024} },
  455. { 0, 1, 0x5, {0x000000, 2048 * 1024} },
  456. { 0, 1, 0x6, {0x000000, 4096 * 1024} },
  457. { X, X, 0x7, {0x000000, 8192 * 1024} },
  458. { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
  459. { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
  460. { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
  461. { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
  462. { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
  463. { 1, 1, 0x1, {0x000000, 4 * 1024} },
  464. { 1, 1, 0x2, {0x000000, 8 * 1024} },
  465. { 1, 1, 0x3, {0x000000, 16 * 1024} },
  466. { 1, 1, 0x4, {0x000000, 32 * 1024} },
  467. { 1, 1, 0x5, {0x000000, 32 * 1024} },
  468. };
  469. static struct w25q_range w25rq128_cmp0_ranges[] = {
  470. { X, X, 0, {0, 0} }, /* NONE */
  471. { 0, 0, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
  472. { 0, 0, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
  473. { 0, 0, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
  474. { 0, 0, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
  475. { 0, 0, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
  476. { 0, 0, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
  477. { 0, 1, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
  478. { 0, 1, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
  479. { 0, 1, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
  480. { 0, 1, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
  481. { 0, 1, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
  482. { 0, 1, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
  483. { X, X, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
  484. { 1, 0, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
  485. { 1, 0, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
  486. { 1, 0, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
  487. { 1, 0, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
  488. { 1, 0, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
  489. { 1, 1, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
  490. { 1, 1, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
  491. { 1, 1, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
  492. { 1, 1, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
  493. { 1, 1, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
  494. };
  495. static struct w25q_range w25rq128_cmp1_ranges[] = {
  496. { X, X, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
  497. { 0, 0, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
  498. { 0, 0, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
  499. { 0, 0, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
  500. { 0, 0, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
  501. { 0, 0, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
  502. { 0, 0, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
  503. { 0, 1, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
  504. { 0, 1, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
  505. { 0, 1, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
  506. { 0, 1, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
  507. { 0, 1, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
  508. { 0, 1, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
  509. { X, X, 0x7, {0x000000, 0} }, /* NONE */
  510. { 1, 0, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
  511. { 1, 0, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
  512. { 1, 0, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
  513. { 1, 0, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
  514. { 1, 0, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
  515. { 1, 1, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
  516. { 1, 1, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
  517. { 1, 1, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
  518. { 1, 1, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
  519. { 1, 1, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
  520. };
  521. struct w25q_range w25x10_ranges[] = {
  522. { X, X, 0, {0, 0} }, /* none */
  523. { 0, 0, 0x1, {0x010000, 64 * 1024} },
  524. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  525. { X, X, 0x2, {0x000000, 128 * 1024} },
  526. { X, X, 0x3, {0x000000, 128 * 1024} },
  527. };
  528. struct w25q_range w25x20_ranges[] = {
  529. { X, X, 0, {0, 0} }, /* none */
  530. { 0, 0, 0x1, {0x030000, 64 * 1024} },
  531. { 0, 0, 0x2, {0x020000, 128 * 1024} },
  532. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  533. { 0, 1, 0x2, {0x000000, 128 * 1024} },
  534. { 0, X, 0x3, {0x000000, 256 * 1024} },
  535. };
  536. struct w25q_range w25x40_ranges[] = {
  537. { X, X, 0, {0, 0} }, /* none */
  538. { 0, 0, 0x1, {0x070000, 64 * 1024} },
  539. { 0, 0, 0x2, {0x060000, 128 * 1024} },
  540. { 0, 0, 0x3, {0x040000, 256 * 1024} },
  541. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  542. { 0, 1, 0x2, {0x000000, 128 * 1024} },
  543. { 0, 1, 0x3, {0x000000, 256 * 1024} },
  544. { 0, X, 0x4, {0x000000, 512 * 1024} },
  545. { 0, X, 0x5, {0x000000, 512 * 1024} },
  546. { 0, X, 0x6, {0x000000, 512 * 1024} },
  547. { 0, X, 0x7, {0x000000, 512 * 1024} },
  548. };
  549. struct w25q_range w25x80_ranges[] = {
  550. { X, X, 0, {0, 0} }, /* none */
  551. { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
  552. { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
  553. { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
  554. { 0, 0, 0x4, {0x080000, 512 * 1024} },
  555. { 0, 1, 0x1, {0x000000, 64 * 1024} },
  556. { 0, 1, 0x2, {0x000000, 128 * 1024} },
  557. { 0, 1, 0x3, {0x000000, 256 * 1024} },
  558. { 0, 1, 0x4, {0x000000, 512 * 1024} },
  559. { 0, X, 0x5, {0x000000, 1024 * 1024} },
  560. { 0, X, 0x6, {0x000000, 1024 * 1024} },
  561. { 0, X, 0x7, {0x000000, 1024 * 1024} },
  562. };
  563. static struct w25q_range gd25q64_ranges[] = {
  564. { X, X, 0, {0, 0} }, /* none */
  565. { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
  566. { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
  567. { 0, 0, 0x3, {0x780000, 512 * 1024} },
  568. { 0, 0, 0x4, {0x700000, 1024 * 1024} },
  569. { 0, 0, 0x5, {0x600000, 2048 * 1024} },
  570. { 0, 0, 0x6, {0x400000, 4096 * 1024} },
  571. { 0, 1, 0x1, {0x000000, 128 * 1024} },
  572. { 0, 1, 0x2, {0x000000, 256 * 1024} },
  573. { 0, 1, 0x3, {0x000000, 512 * 1024} },
  574. { 0, 1, 0x4, {0x000000, 1024 * 1024} },
  575. { 0, 1, 0x5, {0x000000, 2048 * 1024} },
  576. { 0, 1, 0x6, {0x000000, 4096 * 1024} },
  577. { X, X, 0x7, {0x000000, 8192 * 1024} },
  578. { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
  579. { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
  580. { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
  581. { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
  582. { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
  583. { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
  584. { 1, 1, 0x1, {0x000000, 4 * 1024} },
  585. { 1, 1, 0x2, {0x000000, 8 * 1024} },
  586. { 1, 1, 0x3, {0x000000, 16 * 1024} },
  587. { 1, 1, 0x4, {0x000000, 32 * 1024} },
  588. { 1, 1, 0x5, {0x000000, 32 * 1024} },
  589. { 1, 1, 0x6, {0x000000, 32 * 1024} },
  590. };
  591. static struct w25q_range a25l040_ranges[] = {
  592. { X, X, 0x0, {0, 0} }, /* none */
  593. { X, X, 0x1, {0x70000, 64 * 1024} },
  594. { X, X, 0x2, {0x60000, 128 * 1024} },
  595. { X, X, 0x3, {0x40000, 256 * 1024} },
  596. { X, X, 0x4, {0x00000, 512 * 1024} },
  597. { X, X, 0x5, {0x00000, 512 * 1024} },
  598. { X, X, 0x6, {0x00000, 512 * 1024} },
  599. { X, X, 0x7, {0x00000, 512 * 1024} },
  600. };
  601. static uint8_t do_read_status(const struct flashctx *flash)
  602. {
  603. if (flash->read_status)
  604. return flash->read_status(flash);
  605. else
  606. return spi_read_status_register(flash);
  607. }
  608. static int do_write_status(const struct flashctx *flash, int status)
  609. {
  610. if (flash->write_status)
  611. return flash->write_status(flash, status);
  612. else
  613. return spi_write_status_register(flash, status);
  614. }
  615. /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
  616. static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
  617. {
  618. static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
  619. unsigned char readarr[2];
  620. int ret;
  621. /* Read Status Register */
  622. ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
  623. if (ret) {
  624. /*
  625. * FIXME: make this a benign failure for now in case we are
  626. * unable to execute the opcode
  627. */
  628. msg_cdbg("RDSR2 failed!\n");
  629. readarr[0] = 0x00;
  630. }
  631. return readarr[0];
  632. }
  633. /* Given a flash chip, this function returns its range table. */
  634. static int w25_range_table(const struct flashctx *flash,
  635. struct w25q_range **w25q_ranges,
  636. int *num_entries)
  637. {
  638. *w25q_ranges = 0;
  639. *num_entries = 0;
  640. switch (flash->manufacture_id) {
  641. case WINBOND_NEX_ID:
  642. switch(flash->model_id) {
  643. case WINBOND_NEX_W25X10:
  644. *w25q_ranges = w25x10_ranges;
  645. *num_entries = ARRAY_SIZE(w25x10_ranges);
  646. break;
  647. case WINBOND_NEX_W25X20:
  648. *w25q_ranges = w25x20_ranges;
  649. *num_entries = ARRAY_SIZE(w25x20_ranges);
  650. break;
  651. case WINBOND_NEX_W25X40:
  652. *w25q_ranges = w25x40_ranges;
  653. *num_entries = ARRAY_SIZE(w25x40_ranges);
  654. break;
  655. case WINBOND_NEX_W25X80:
  656. *w25q_ranges = w25x80_ranges;
  657. *num_entries = ARRAY_SIZE(w25x80_ranges);
  658. break;
  659. case WINBOND_NEX_W25Q80:
  660. *w25q_ranges = w25q80_ranges;
  661. *num_entries = ARRAY_SIZE(w25q80_ranges);
  662. break;
  663. case WINBOND_NEX_W25Q16:
  664. *w25q_ranges = w25q16_ranges;
  665. *num_entries = ARRAY_SIZE(w25q16_ranges);
  666. break;
  667. case WINBOND_NEX_W25Q32:
  668. case WINBOND_NEX_W25Q32DW:
  669. *w25q_ranges = w25q32_ranges;
  670. *num_entries = ARRAY_SIZE(w25q32_ranges);
  671. break;
  672. case WINBOND_NEX_W25Q64:
  673. case WINBOND_NEX_W25Q64DW:
  674. *w25q_ranges = w25q64_ranges;
  675. *num_entries = ARRAY_SIZE(w25q64_ranges);
  676. break;
  677. case WINBOND_NEX_W25Q128:
  678. case WINBOND_NEX_W25Q128FW:
  679. if (w25q_read_status_register_2(flash) & (1 << 6)) {
  680. /* CMP == 1 */
  681. *w25q_ranges = w25rq128_cmp1_ranges;
  682. *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
  683. } else {
  684. /* CMP == 0 */
  685. *w25q_ranges = w25rq128_cmp0_ranges;
  686. *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
  687. }
  688. break;
  689. default:
  690. msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
  691. ", aborting\n", __func__, __LINE__,
  692. flash->model_id);
  693. return -1;
  694. }
  695. break;
  696. case EON_ID_NOPREFIX:
  697. switch (flash->model_id) {
  698. case EON_EN25F40:
  699. *w25q_ranges = en25f40_ranges;
  700. *num_entries = ARRAY_SIZE(en25f40_ranges);
  701. break;
  702. case EON_EN25Q40:
  703. *w25q_ranges = en25q40_ranges;
  704. *num_entries = ARRAY_SIZE(en25q40_ranges);
  705. break;
  706. case EON_EN25Q80:
  707. *w25q_ranges = en25q80_ranges;
  708. *num_entries = ARRAY_SIZE(en25q80_ranges);
  709. break;
  710. case EON_EN25Q32:
  711. *w25q_ranges = en25q32_ranges;
  712. *num_entries = ARRAY_SIZE(en25q32_ranges);
  713. break;
  714. case EON_EN25Q64:
  715. *w25q_ranges = en25q64_ranges;
  716. *num_entries = ARRAY_SIZE(en25q64_ranges);
  717. break;
  718. case EON_EN25Q128:
  719. *w25q_ranges = en25q128_ranges;
  720. *num_entries = ARRAY_SIZE(en25q128_ranges);
  721. break;
  722. case EON_EN25S64:
  723. *w25q_ranges = en25s64_ranges;
  724. *num_entries = ARRAY_SIZE(en25s64_ranges);
  725. break;
  726. default:
  727. msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
  728. ", aborting\n", __func__, __LINE__,
  729. flash->model_id);
  730. return -1;
  731. }
  732. break;
  733. case MACRONIX_ID:
  734. switch (flash->model_id) {
  735. case MACRONIX_MX25L1005:
  736. *w25q_ranges = mx25l1005_ranges;
  737. *num_entries = ARRAY_SIZE(mx25l1005_ranges);
  738. break;
  739. case MACRONIX_MX25L2005:
  740. *w25q_ranges = mx25l2005_ranges;
  741. *num_entries = ARRAY_SIZE(mx25l2005_ranges);
  742. break;
  743. case MACRONIX_MX25L4005:
  744. *w25q_ranges = mx25l4005_ranges;
  745. *num_entries = ARRAY_SIZE(mx25l4005_ranges);
  746. break;
  747. case MACRONIX_MX25L8005:
  748. *w25q_ranges = mx25l8005_ranges;
  749. *num_entries = ARRAY_SIZE(mx25l8005_ranges);
  750. break;
  751. case MACRONIX_MX25L1605:
  752. /* FIXME: MX25L1605 and MX25L1605D have different write
  753. * protection capabilities, but share IDs */
  754. *w25q_ranges = mx25l1605d_ranges;
  755. *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
  756. break;
  757. case MACRONIX_MX25L3205:
  758. *w25q_ranges = mx25l3205d_ranges;
  759. *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
  760. break;
  761. case MACRONIX_MX25U3235E:
  762. *w25q_ranges = mx25u3235e_ranges;
  763. *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
  764. break;
  765. case MACRONIX_MX25U6435E:
  766. *w25q_ranges = mx25u6435e_ranges;
  767. *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
  768. break;
  769. default:
  770. msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
  771. ", aborting\n", __func__, __LINE__,
  772. flash->model_id);
  773. return -1;
  774. }
  775. break;
  776. case ST_ID:
  777. switch(flash->model_id) {
  778. case ST_N25Q064__1E:
  779. case ST_N25Q064__3E:
  780. *w25q_ranges = n25q064_ranges;
  781. *num_entries = ARRAY_SIZE(n25q064_ranges);
  782. break;
  783. default:
  784. msg_cerr("%s() %d: Micron flash chip mismatch"
  785. " (0x%04x), aborting\n", __func__, __LINE__,
  786. flash->model_id);
  787. return -1;
  788. }
  789. break;
  790. case GIGADEVICE_ID:
  791. switch(flash->model_id) {
  792. case GIGADEVICE_GD25LQ32:
  793. *w25q_ranges = w25q32_ranges;
  794. *num_entries = ARRAY_SIZE(w25q32_ranges);
  795. break;
  796. case GIGADEVICE_GD25Q64:
  797. case GIGADEVICE_GD25LQ64:
  798. *w25q_ranges = gd25q64_ranges;
  799. *num_entries = ARRAY_SIZE(gd25q64_ranges);
  800. break;
  801. /* TODO(shawnn): add support for other GD parts */
  802. default:
  803. msg_cerr("%s() %d: GigaDevice flash chip mismatch"
  804. " (0x%04x), aborting\n", __func__, __LINE__,
  805. flash->model_id);
  806. return -1;
  807. }
  808. break;
  809. case AMIC_ID_NOPREFIX:
  810. switch(flash->model_id) {
  811. case AMIC_A25L040:
  812. *w25q_ranges = a25l040_ranges;
  813. *num_entries = ARRAY_SIZE(a25l040_ranges);
  814. break;
  815. default:
  816. msg_cerr("%s() %d: AMIC flash chip mismatch"
  817. " (0x%04x), aborting\n", __func__, __LINE__,
  818. flash->model_id);
  819. return -1;
  820. }
  821. break;
  822. default:
  823. msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
  824. __func__, flash->manufacture_id);
  825. return -1;
  826. }
  827. return 0;
  828. }
  829. int w25_range_to_status(const struct flashctx *flash,
  830. unsigned int start, unsigned int len,
  831. struct w25q_status *status)
  832. {
  833. struct w25q_range *w25q_ranges;
  834. int i, range_found = 0;
  835. int num_entries;
  836. if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
  837. for (i = 0; i < num_entries; i++) {
  838. struct wp_range *r = &w25q_ranges[i].range;
  839. msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
  840. start, len, r->start, r->len);
  841. if ((start == r->start) && (len == r->len)) {
  842. status->bp0 = w25q_ranges[i].bp & 1;
  843. status->bp1 = w25q_ranges[i].bp >> 1;
  844. status->bp2 = w25q_ranges[i].bp >> 2;
  845. status->tb = w25q_ranges[i].tb;
  846. status->sec = w25q_ranges[i].sec;
  847. range_found = 1;
  848. break;
  849. }
  850. }
  851. if (!range_found) {
  852. msg_cerr("matching range not found\n");
  853. return -1;
  854. }
  855. return 0;
  856. }
  857. int w25_status_to_range(const struct flashctx *flash,
  858. const struct w25q_status *status,
  859. unsigned int *start, unsigned int *len)
  860. {
  861. struct w25q_range *w25q_ranges;
  862. int i, status_found = 0;
  863. int num_entries;
  864. if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
  865. for (i = 0; i < num_entries; i++) {
  866. int bp;
  867. int table_bp, table_tb, table_sec;
  868. bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
  869. msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
  870. bp, w25q_ranges[i].bp,
  871. status->tb, w25q_ranges[i].tb,
  872. status->sec, w25q_ranges[i].sec);
  873. table_bp = w25q_ranges[i].bp;
  874. table_tb = w25q_ranges[i].tb;
  875. table_sec = w25q_ranges[i].sec;
  876. if ((bp == table_bp || table_bp == X) &&
  877. (status->tb == table_tb || table_tb == X) &&
  878. (status->sec == table_sec || table_sec == X)) {
  879. *start = w25q_ranges[i].range.start;
  880. *len = w25q_ranges[i].range.len;
  881. status_found = 1;
  882. break;
  883. }
  884. }
  885. if (!status_found) {
  886. msg_cerr("matching status not found\n");
  887. return -1;
  888. }
  889. return 0;
  890. }
  891. /* Given a [start, len], this function calls w25_range_to_status() to convert
  892. * it to flash-chip-specific range bits, then sets into status register.
  893. */
  894. static int w25_set_range(const struct flashctx *flash,
  895. unsigned int start, unsigned int len)
  896. {
  897. struct w25q_status status;
  898. int tmp = 0;
  899. int expected = 0;
  900. memset(&status, 0, sizeof(status));
  901. tmp = do_read_status(flash);
  902. memcpy(&status, &tmp, 1);
  903. msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
  904. if (w25_range_to_status(flash, start, len, &status)) return -1;
  905. msg_cdbg("status.busy: %x\n", status.busy);
  906. msg_cdbg("status.wel: %x\n", status.wel);
  907. msg_cdbg("status.bp0: %x\n", status.bp0);
  908. msg_cdbg("status.bp1: %x\n", status.bp1);
  909. msg_cdbg("status.bp2: %x\n", status.bp2);
  910. msg_cdbg("status.tb: %x\n", status.tb);
  911. msg_cdbg("status.sec: %x\n", status.sec);
  912. msg_cdbg("status.srp0: %x\n", status.srp0);
  913. memcpy(&expected, &status, sizeof(status));
  914. do_write_status(flash, expected);
  915. tmp = do_read_status(flash);
  916. msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
  917. if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
  918. return 0;
  919. } else {
  920. msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
  921. expected, tmp);
  922. return 1;
  923. }
  924. }
  925. /* Print out the current status register value with human-readable text. */
  926. static int w25_wp_status(const struct flashctx *flash)
  927. {
  928. struct w25q_status status;
  929. int tmp;
  930. unsigned int start, len;
  931. int ret = 0;
  932. memset(&status, 0, sizeof(status));
  933. tmp = do_read_status(flash);
  934. memcpy(&status, &tmp, 1);
  935. msg_cinfo("WP: status: 0x%02x\n", tmp);
  936. msg_cinfo("WP: status.srp0: %x\n", status.srp0);
  937. msg_cinfo("WP: write protect is %s.\n",
  938. status.srp0 ? "enabled" : "disabled");
  939. msg_cinfo("WP: write protect range: ");
  940. if (w25_status_to_range(flash, &status, &start, &len)) {
  941. msg_cinfo("(cannot resolve the range)\n");
  942. ret = -1;
  943. } else {
  944. msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
  945. }
  946. return ret;
  947. }
  948. /* Set/clear the SRP0 bit in the status register. */
  949. static int w25_set_srp0(const struct flashctx *flash, int enable)
  950. {
  951. struct w25q_status status;
  952. int tmp = 0;
  953. int expected = 0;
  954. memset(&status, 0, sizeof(status));
  955. tmp = do_read_status(flash);
  956. /* FIXME: this is NOT endian-free copy. */
  957. memcpy(&status, &tmp, 1);
  958. msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
  959. status.srp0 = enable ? 1 : 0;
  960. memcpy(&expected, &status, sizeof(status));
  961. do_write_status(flash, expected);
  962. tmp = do_read_status(flash);
  963. msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
  964. if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
  965. return 1;
  966. return 0;
  967. }
  968. static int w25_enable_writeprotect(const struct flashctx *flash,
  969. enum wp_mode wp_mode)
  970. {
  971. int ret;
  972. switch (wp_mode) {
  973. case WP_MODE_HARDWARE:
  974. ret = w25_set_srp0(flash, 1);
  975. break;
  976. default:
  977. msg_cerr("%s(): unsupported write-protect mode\n", __func__);
  978. return 1;
  979. }
  980. if (ret)
  981. msg_cerr("%s(): error=%d.\n", __func__, ret);
  982. return ret;
  983. }
  984. static int w25_disable_writeprotect(const struct flashctx *flash)
  985. {
  986. int ret;
  987. ret = w25_set_srp0(flash, 0);
  988. if (ret)
  989. msg_cerr("%s(): error=%d.\n", __func__, ret);
  990. return ret;
  991. }
  992. static int w25_list_ranges(const struct flashctx *flash)
  993. {
  994. struct w25q_range *w25q_ranges;
  995. int i, num_entries;
  996. if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
  997. for (i = 0; i < num_entries; i++) {
  998. msg_cinfo("start: 0x%06x, length: 0x%06x\n",
  999. w25q_ranges[i].range.start,
  1000. w25q_ranges[i].range.len);
  1001. }
  1002. return 0;
  1003. }
  1004. static int w25q_wp_status(const struct flashctx *flash)
  1005. {
  1006. struct w25q_status sr1;
  1007. struct w25q_status_2 sr2;
  1008. uint8_t tmp[2];
  1009. unsigned int start, len;
  1010. int ret = 0;
  1011. memset(&sr1, 0, sizeof(sr1));
  1012. tmp[0] = do_read_status(flash);
  1013. memcpy(&sr1, &tmp[0], 1);
  1014. memset(&sr2, 0, sizeof(sr2));
  1015. tmp[1] = w25q_read_status_register_2(flash);
  1016. memcpy(&sr2, &tmp[1], 1);
  1017. msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
  1018. msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
  1019. msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
  1020. msg_cinfo("WP: write protect is %s.\n",
  1021. (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
  1022. msg_cinfo("WP: write protect range: ");
  1023. if (w25_status_to_range(flash, &sr1, &start, &len)) {
  1024. msg_cinfo("(cannot resolve the range)\n");
  1025. ret = -1;
  1026. } else {
  1027. msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
  1028. }
  1029. return ret;
  1030. }
  1031. /*
  1032. * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
  1033. * de-asserted after the first byte, then it acts like a JEDEC-standard
  1034. * WRSR command. if /CS is asserted, then the next data byte is written
  1035. * into status register 2.
  1036. */
  1037. #define W25Q_WRSR_OUTSIZE 0x03
  1038. static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
  1039. {
  1040. int result;
  1041. struct spi_command cmds[] = {
  1042. {
  1043. /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
  1044. .writecnt = JEDEC_WREN_OUTSIZE,
  1045. .writearr = (const unsigned char[]){ JEDEC_WREN },
  1046. .readcnt = 0,
  1047. .readarr = NULL,
  1048. }, {
  1049. .writecnt = W25Q_WRSR_OUTSIZE,
  1050. .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
  1051. .readcnt = 0,
  1052. .readarr = NULL,
  1053. }, {
  1054. .writecnt = 0,
  1055. .writearr = NULL,
  1056. .readcnt = 0,
  1057. .readarr = NULL,
  1058. }};
  1059. result = spi_send_multicommand(flash, cmds);
  1060. if (result) {
  1061. msg_cerr("%s failed during command execution\n",
  1062. __func__);
  1063. }
  1064. /* WRSR performs a self-timed erase before the changes take effect. */
  1065. programmer_delay(100 * 1000);
  1066. return result;
  1067. }
  1068. /*
  1069. * Set/clear the SRP1 bit in status register 2.
  1070. * FIXME: make this more generic if other chips use the same SR2 layout
  1071. */
  1072. static int w25q_set_srp1(const struct flashctx *flash, int enable)
  1073. {
  1074. struct w25q_status sr1;
  1075. struct w25q_status_2 sr2;
  1076. uint8_t tmp, expected;
  1077. tmp = do_read_status(flash);
  1078. memcpy(&sr1, &tmp, 1);
  1079. tmp = w25q_read_status_register_2(flash);
  1080. memcpy(&sr2, &tmp, 1);
  1081. msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
  1082. sr2.srp1 = enable ? 1 : 0;
  1083. memcpy(&expected, &sr2, 1);
  1084. w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
  1085. tmp = w25q_read_status_register_2(flash);
  1086. msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
  1087. if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
  1088. return 1;
  1089. return 0;
  1090. }
  1091. enum wp_mode get_wp_mode(const char *mode_str)
  1092. {
  1093. enum wp_mode wp_mode = WP_MODE_UNKNOWN;
  1094. if (!strcasecmp(mode_str, "hardware"))
  1095. wp_mode = WP_MODE_HARDWARE;
  1096. else if (!strcasecmp(mode_str, "power_cycle"))
  1097. wp_mode = WP_MODE_POWER_CYCLE;
  1098. else if (!strcasecmp(mode_str, "permanent"))
  1099. wp_mode = WP_MODE_PERMANENT;
  1100. return wp_mode;
  1101. }
  1102. static int w25q_disable_writeprotect(const struct flashctx *flash,
  1103. enum wp_mode wp_mode)
  1104. {
  1105. int ret = 1;
  1106. struct w25q_status_2 sr2;
  1107. uint8_t tmp;
  1108. switch (wp_mode) {
  1109. case WP_MODE_HARDWARE:
  1110. ret = w25_set_srp0(flash, 0);
  1111. break;
  1112. case WP_MODE_POWER_CYCLE:
  1113. tmp = w25q_read_status_register_2(flash);
  1114. memcpy(&sr2, &tmp, 1);
  1115. if (sr2.srp1) {
  1116. msg_cerr("%s(): must disconnect power to disable "
  1117. "write-protection\n", __func__);
  1118. } else {
  1119. ret = 0;
  1120. }
  1121. break;
  1122. case WP_MODE_PERMANENT:
  1123. msg_cerr("%s(): cannot disable permanent write-protection\n",
  1124. __func__);
  1125. break;
  1126. default:
  1127. msg_cerr("%s(): invalid mode specified\n", __func__);
  1128. break;
  1129. }
  1130. if (ret)
  1131. msg_cerr("%s(): error=%d.\n", __func__, ret);
  1132. return ret;
  1133. }
  1134. static int w25q_disable_writeprotect_default(const struct flashctx *flash)
  1135. {
  1136. return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
  1137. }
  1138. static int w25q_enable_writeprotect(const struct flashctx *flash,
  1139. enum wp_mode wp_mode)
  1140. {
  1141. int ret = 1;
  1142. struct w25q_status sr1;
  1143. struct w25q_status_2 sr2;
  1144. uint8_t tmp;
  1145. switch (wp_mode) {
  1146. case WP_MODE_HARDWARE:
  1147. if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
  1148. msg_cerr("%s(): cannot disable power cycle WP mode\n",
  1149. __func__);
  1150. break;
  1151. }
  1152. tmp = do_read_status(flash);
  1153. memcpy(&sr1, &tmp, 1);
  1154. if (sr1.srp0)
  1155. ret = 0;
  1156. else
  1157. ret = w25_set_srp0(flash, 1);
  1158. break;
  1159. case WP_MODE_POWER_CYCLE:
  1160. if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
  1161. msg_cerr("%s(): cannot disable hardware WP mode\n",
  1162. __func__);
  1163. break;
  1164. }
  1165. tmp = w25q_read_status_register_2(flash);
  1166. memcpy(&sr2, &tmp, 1);
  1167. if (sr2.srp1)
  1168. ret = 0;
  1169. else
  1170. ret = w25q_set_srp1(flash, 1);
  1171. break;
  1172. case WP_MODE_PERMANENT:
  1173. tmp = do_read_status(flash);
  1174. memcpy(&sr1, &tmp, 1);
  1175. if (sr1.srp0 == 0) {
  1176. ret = w25_set_srp0(flash, 1);
  1177. if (ret) {
  1178. msg_perr("%s(): cannot enable SRP0 for "
  1179. "permanent WP\n", __func__);
  1180. break;
  1181. }
  1182. }
  1183. tmp = w25q_read_status_register_2(flash);
  1184. memcpy(&sr2, &tmp, 1);
  1185. if (sr2.srp1 == 0) {
  1186. ret = w25q_set_srp1(flash, 1);
  1187. if (ret) {
  1188. msg_perr("%s(): cannot enable SRP1 for "
  1189. "permanent WP\n", __func__);
  1190. break;
  1191. }
  1192. }
  1193. break;
  1194. default:
  1195. msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
  1196. break;
  1197. }
  1198. if (ret)
  1199. msg_cerr("%s(): error=%d.\n", __func__, ret);
  1200. return ret;
  1201. }
  1202. /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
  1203. uint8_t mx25l_read_config_register(const struct flashctx *flash)
  1204. {
  1205. static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
  1206. unsigned char readarr[2]; /* leave room for dummy byte */
  1207. int ret;
  1208. ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
  1209. if (ret) {
  1210. msg_cerr("RDCR failed!\n");
  1211. readarr[0] = 0x00;
  1212. }
  1213. return readarr[0];
  1214. }
  1215. /* W25P, W25X, and many flash chips from various vendors */
  1216. struct wp wp_w25 = {
  1217. .list_ranges = w25_list_ranges,
  1218. .set_range = w25_set_range,
  1219. .enable = w25_enable_writeprotect,
  1220. .disable = w25_disable_writeprotect,
  1221. .wp_status = w25_wp_status,
  1222. };
  1223. /* W25Q series has features such as a second status register and SFDP */
  1224. struct wp wp_w25q = {
  1225. .list_ranges = w25_list_ranges,
  1226. .set_range = w25_set_range,
  1227. .enable = w25q_enable_writeprotect,
  1228. /*
  1229. * By default, disable hardware write-protection. We may change
  1230. * this later if we want to add fine-grained write-protect disable
  1231. * as a command-line option.
  1232. */
  1233. .disable = w25q_disable_writeprotect_default,
  1234. .wp_status = w25q_wp_status,
  1235. };
  1236. struct generic_range gd25q32_cmp0_ranges[] = {
  1237. /* none, bp4 and bp3 => don't care */
  1238. { { }, 0x00, {0, 0} },
  1239. { { }, 0x08, {0, 0} },
  1240. { { }, 0x10, {0, 0} },
  1241. { { }, 0x18, {0, 0} },
  1242. { { }, 0x01, {0x3f0000, 64 * 1024} },
  1243. { { }, 0x02, {0x3e0000, 128 * 1024} },
  1244. { { }, 0x03, {0x3c0000, 256 * 1024} },
  1245. { { }, 0x04, {0x380000, 512 * 1024} },
  1246. { { }, 0x05, {0x300000, 1024 * 1024} },
  1247. { { }, 0x06, {0x200000, 2048 * 1024} },
  1248. { { }, 0x09, {0x000000, 64 * 1024} },
  1249. { { }, 0x0a, {0x000000, 128 * 1024} },
  1250. { { }, 0x0b, {0x000000, 256 * 1024} },
  1251. { { }, 0x0c, {0x000000, 512 * 1024} },
  1252. { { }, 0x0d, {0x000000, 1024 * 1024} },
  1253. { { }, 0x0e, {0x000000, 2048 * 1024} },
  1254. /* all, bp4 and bp3 => don't care */
  1255. { { }, 0x07, {0x000000, 4096 * 1024} },
  1256. { { }, 0x0f, {0x000000, 4096 * 1024} },
  1257. { { }, 0x17, {0x000000, 4096 * 1024} },
  1258. { { }, 0x1f, {0x000000, 4096 * 1024} },
  1259. { { }, 0x11, {0x3ff000, 4 * 1024} },
  1260. { { }, 0x12, {0x3fe000, 8 * 1024} },
  1261. { { }, 0x13, {0x3fc000, 16 * 1024} },
  1262. { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
  1263. { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
  1264. { { }, 0x16, {0x3f8000, 32 * 1024} },
  1265. { { }, 0x19, {0x000000, 4 * 1024} },
  1266. { { }, 0x1a, {0x000000, 8 * 1024} },
  1267. { { }, 0x1b, {0x000000, 16 * 1024} },
  1268. { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
  1269. { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
  1270. { { }, 0x1e, {0x000000, 32 * 1024} },
  1271. };
  1272. struct generic_range gd25q32_cmp1_ranges[] = {
  1273. /* none, bp4 and bp3 => don't care */
  1274. { { }, 0x00, {0, 0} },
  1275. { { }, 0x08, {0, 0} },
  1276. { { }, 0x10, {0, 0} },
  1277. { { }, 0x18, {0, 0} },
  1278. { { }, 0x01, {0x000000, 4032 * 1024} },
  1279. { { }, 0x02, {0x000000, 3968 * 1024} },
  1280. { { }, 0x03, {0x000000, 3840 * 1024} },
  1281. { { }, 0x04, {0x000000, 3584 * 1024} },
  1282. { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
  1283. { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
  1284. { { }, 0x09, {0x010000, 4032 * 1024} },
  1285. { { }, 0x0a, {0x020000, 3968 * 1024} },
  1286. { { }, 0x0b, {0x040000, 3840 * 1024} },
  1287. { { }, 0x0c, {0x080000, 3584 * 1024} },
  1288. { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
  1289. { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
  1290. /* all, bp4 and bp3 => don't care */
  1291. { { }, 0x07, {0x000000, 4096 * 1024} },
  1292. { { }, 0x0f, {0x000000, 4096 * 1024} },
  1293. { { }, 0x17, {0x000000, 4096 * 1024} },
  1294. { { }, 0x1f, {0x000000, 4096 * 1024} },
  1295. { { }, 0x11, {0x000000, 4092 * 1024} },
  1296. { { }, 0x12, {0x000000, 4088 * 1024} },
  1297. { { }, 0x13, {0x000000, 4080 * 1024} },
  1298. { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
  1299. { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
  1300. { { }, 0x16, {0x000000, 4064 * 1024} },
  1301. { { }, 0x19, {0x001000, 4092 * 1024} },
  1302. { { }, 0x1a, {0x002000, 4088 * 1024} },
  1303. { { }, 0x1b, {0x040000, 4080 * 1024} },
  1304. { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
  1305. { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
  1306. { { }, 0x1e, {0x080000, 4064 * 1024} },
  1307. };
  1308. static struct generic_wp gd25q32_wp = {
  1309. /* TODO: map second status register */
  1310. .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
  1311. };
  1312. struct generic_range gd25q128_cmp0_ranges[] = {
  1313. /* none, bp4 and bp3 => don't care, others = 0 */
  1314. { { .tb = 0 }, 0x00, {0, 0} },
  1315. { { .tb = 0 }, 0x08, {0, 0} },
  1316. { { .tb = 0 }, 0x10, {0, 0} },
  1317. { { .tb = 0 }, 0x18, {0, 0} },
  1318. { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
  1319. { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
  1320. { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
  1321. { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
  1322. { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
  1323. { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
  1324. { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
  1325. { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
  1326. { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
  1327. { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
  1328. { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
  1329. { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
  1330. /* all, bp4 and bp3 => don't care, others = 1 */
  1331. { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
  1332. { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
  1333. { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
  1334. { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
  1335. { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
  1336. { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
  1337. { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
  1338. { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
  1339. { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
  1340. { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
  1341. { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
  1342. { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
  1343. { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
  1344. { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
  1345. { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
  1346. };
  1347. struct generic_range gd25q128_cmp1_ranges[] = {
  1348. /* none, bp4 and bp3 => don't care, others = 0 */
  1349. { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
  1350. { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
  1351. { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
  1352. { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
  1353. { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
  1354. { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
  1355. { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
  1356. { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
  1357. { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
  1358. { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
  1359. { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
  1360. { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
  1361. { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
  1362. { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
  1363. { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
  1364. { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
  1365. /* none, bp4 and bp3 => don't care, others = 1 */
  1366. { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
  1367. { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
  1368. { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
  1369. { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
  1370. { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
  1371. { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
  1372. { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
  1373. { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
  1374. { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
  1375. { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
  1376. { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
  1377. { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
  1378. { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
  1379. { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
  1380. { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
  1381. { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
  1382. };
  1383. static struct generic_wp gd25q128_wp = {
  1384. /* TODO: map second and third status registers */
  1385. .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
  1386. };
  1387. #if 0
  1388. /* FIXME: MX25L6405D has same ID as MX25L6406 */
  1389. static struct w25q_range mx25l6405d_ranges[] = {
  1390. { X, 0, 0, {0, 0} }, /* none */
  1391. { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
  1392. { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
  1393. { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
  1394. { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
  1395. { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
  1396. { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
  1397. { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
  1398. { X, 1, 0x0, {0x000000, 8192 * 1024} },
  1399. { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
  1400. { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
  1401. { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
  1402. { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
  1403. { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
  1404. { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
  1405. { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
  1406. };
  1407. #endif
  1408. /* FIXME: MX25L6406 has same ID as MX25L6405D */
  1409. struct generic_range mx25l6406e_ranges[] = {
  1410. { { }, 0, {0, 0} }, /* none */
  1411. { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
  1412. { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
  1413. { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
  1414. { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
  1415. { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
  1416. { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
  1417. { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
  1418. { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
  1419. { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
  1420. { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
  1421. { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
  1422. { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
  1423. { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
  1424. { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
  1425. { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
  1426. };
  1427. static struct generic_wp mx25l6406e_wp = {
  1428. .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
  1429. .ranges = &mx25l6406e_ranges[0],
  1430. };
  1431. struct generic_range mx25l6495f_tb0_ranges[] = {
  1432. { { }, 0, {0, 0} }, /* none */
  1433. { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
  1434. { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
  1435. { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
  1436. { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
  1437. { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
  1438. { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
  1439. { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
  1440. { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
  1441. { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
  1442. { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
  1443. { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
  1444. { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
  1445. { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
  1446. { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
  1447. { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
  1448. };
  1449. struct generic_range mx25l6495f_tb1_ranges[] = {
  1450. { { }, 0, {0, 0} }, /* none */
  1451. { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
  1452. { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
  1453. { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
  1454. { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
  1455. { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
  1456. { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
  1457. { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
  1458. { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
  1459. { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
  1460. { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
  1461. { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
  1462. { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
  1463. { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
  1464. { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
  1465. { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
  1466. };
  1467. static struct generic_wp mx25l6495f_wp = {
  1468. .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
  1469. };
  1470. struct generic_range s25fs128s_ranges[] = {
  1471. { { .tb = 1 }, 0, {0, 0} }, /* none */
  1472. { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
  1473. { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
  1474. { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
  1475. { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
  1476. { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
  1477. { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
  1478. { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
  1479. { { .tb = 0 }, 0, {0, 0} }, /* none */
  1480. { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
  1481. { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
  1482. { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
  1483. { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
  1484. { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
  1485. { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
  1486. { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
  1487. };
  1488. static struct generic_wp s25fs128s_wp = {
  1489. .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
  1490. .get_modifier_bits = s25f_get_modifier_bits,
  1491. .set_modifier_bits = s25f_set_modifier_bits,
  1492. };
  1493. struct generic_range s25fl256s_ranges[] = {
  1494. { { .tb = 1 }, 0, {0, 0} }, /* none */
  1495. { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
  1496. { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
  1497. { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
  1498. { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
  1499. { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
  1500. { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
  1501. { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
  1502. { { .tb = 0 }, 0, {0, 0} }, /* none */
  1503. { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
  1504. { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
  1505. { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
  1506. { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
  1507. { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
  1508. { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
  1509. { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
  1510. };
  1511. static struct generic_wp s25fl256s_wp = {
  1512. .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
  1513. .get_modifier_bits = s25f_get_modifier_bits,
  1514. .set_modifier_bits = s25f_set_modifier_bits,
  1515. };
  1516. /* Given a flash chip, this function returns its writeprotect info. */
  1517. static int generic_range_table(const struct flashctx *flash,
  1518. struct generic_wp **wp,
  1519. int *num_entries)
  1520. {
  1521. *wp = NULL;
  1522. *num_entries = 0;
  1523. switch (flash->manufacture_id) {
  1524. case GIGADEVICE_ID:
  1525. switch(flash->model_id) {
  1526. case GIGADEVICE_GD25Q32: {
  1527. uint8_t sr1 = w25q_read_status_register_2(flash);
  1528. *wp = &gd25q32_wp;
  1529. if (!(sr1 & (1 << 6))) { /* CMP == 0 */
  1530. (*wp)->ranges = &gd25q32_cmp0_ranges[0];
  1531. *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
  1532. } else { /* CMP == 1 */
  1533. (*wp)->ranges = &gd25q32_cmp1_ranges[0];
  1534. *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
  1535. }
  1536. break;
  1537. }
  1538. case GIGADEVICE_GD25Q128:
  1539. case GIGADEVICE_GD25LQ128C: {
  1540. uint8_t sr1 = w25q_read_status_register_2(flash);
  1541. *wp = &gd25q128_wp;
  1542. if (!(sr1 & (1 << 6))) { /* CMP == 0 */
  1543. (*wp)->ranges = &gd25q128_cmp0_ranges[0];
  1544. *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
  1545. } else { /* CMP == 1 */
  1546. (*wp)->ranges = &gd25q128_cmp1_ranges[0];
  1547. *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
  1548. }
  1549. break;
  1550. }
  1551. default:
  1552. msg_cerr("%s() %d: GigaDevice flash chip mismatch"
  1553. " (0x%04x), aborting\n", __func__, __LINE__,
  1554. flash->model_id);
  1555. return -1;
  1556. }
  1557. break;
  1558. case MACRONIX_ID:
  1559. switch (flash->model_id) {
  1560. case MACRONIX_MX25L6405:
  1561. /* FIXME: MX25L64* chips have mixed capabilities and
  1562. share IDs */
  1563. *wp = &mx25l6406e_wp;
  1564. *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
  1565. break;
  1566. case MACRONIX_MX25L6495F: {
  1567. uint8_t cr = mx25l_read_config_register(flash);
  1568. *wp = &mx25l6495f_wp;
  1569. if (!(cr & (1 << 3))) { /* T/B == 0 */
  1570. (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
  1571. *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
  1572. } else { /* T/B == 1 */
  1573. (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
  1574. *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
  1575. }
  1576. break;
  1577. }
  1578. default:
  1579. msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
  1580. ", aborting\n", __func__, __LINE__,
  1581. flash->model_id);
  1582. return -1;
  1583. }
  1584. break;
  1585. case SPANSION_ID:
  1586. switch (flash->model_id) {
  1587. case SPANSION_S25FS128S_L:
  1588. case SPANSION_S25FS128S_S: {
  1589. *wp = &s25fs128s_wp;
  1590. (*wp)->ranges = s25fs128s_ranges;
  1591. *num_entries = ARRAY_SIZE(s25fs128s_ranges);
  1592. break;
  1593. }
  1594. case SPANSION_S25FL256S_UL:
  1595. case SPANSION_S25FL256S_US: {
  1596. *wp = &s25fl256s_wp;
  1597. (*wp)->ranges = s25fl256s_ranges;
  1598. *num_entries = ARRAY_SIZE(s25fl256s_ranges);
  1599. break;
  1600. }
  1601. default:
  1602. msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
  1603. ", aborting\n", __func__, __LINE__, flash->model_id);
  1604. return -1;
  1605. }
  1606. break;
  1607. default:
  1608. msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
  1609. __func__, flash->manufacture_id);
  1610. return -1;
  1611. }
  1612. return 0;
  1613. }
  1614. /* Given a [start, len], this function finds a block protect bit combination
  1615. * (if possible) and sets the corresponding bits in "status". Remaining bits
  1616. * are preserved. */
  1617. static int generic_range_to_status(const struct flashctx *flash,
  1618. unsigned int start, unsigned int len,
  1619. uint8_t *status)
  1620. {
  1621. struct generic_wp *wp;
  1622. struct generic_range *r;
  1623. int i, range_found = 0, num_entries;
  1624. uint8_t bp_mask;
  1625. if (generic_range_table(flash, &wp, &num_entries))
  1626. return -1;
  1627. bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
  1628. ((1 << wp->sr1.bp0_pos) - 1);
  1629. for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
  1630. msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
  1631. start, len, r->range.start, r->range.len);
  1632. if ((start == r->range.start) && (len == r->range.len)) {
  1633. *status &= ~(bp_mask);
  1634. *status |= r->bp << (wp->sr1.bp0_pos);
  1635. if (wp->set_modifier_bits) {
  1636. if (wp->set_modifier_bits(flash, &r->m) < 0) {
  1637. msg_cerr("error setting modifier "
  1638. "bits for range.\n");
  1639. return -1;
  1640. }
  1641. }
  1642. range_found = 1;
  1643. break;
  1644. }
  1645. }
  1646. if (!range_found) {
  1647. msg_cerr("matching range not found\n");
  1648. return -1;
  1649. }
  1650. return 0;
  1651. }
  1652. static int generic_status_to_range(const struct flashctx *flash,
  1653. const uint8_t sr1, unsigned int *start, unsigned int *len)
  1654. {
  1655. struct generic_wp *wp;
  1656. struct generic_range *r;
  1657. int num_entries, i, status_found = 0;
  1658. uint8_t sr1_bp;
  1659. struct generic_modifier_bits m;
  1660. if (generic_range_table(flash, &wp, &num_entries))
  1661. return -1;
  1662. /* modifier bits may be compared more than once, so get them here */
  1663. if (wp->get_modifier_bits) {
  1664. if (wp->get_modifier_bits(flash, &m) < 0)
  1665. return -1;
  1666. }
  1667. sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
  1668. for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
  1669. if (wp->get_modifier_bits) {
  1670. if (memcmp(&m, &r->m, sizeof(m)))
  1671. continue;
  1672. }
  1673. msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
  1674. if (sr1_bp == r->bp) {
  1675. *start = r->range.start;
  1676. *len = r->range.len;
  1677. status_found = 1;
  1678. break;
  1679. }
  1680. }
  1681. if (!status_found) {
  1682. msg_cerr("matching status not found\n");
  1683. return -1;
  1684. }
  1685. return 0;
  1686. }
  1687. /* Given a [start, len], this function calls generic_range_to_status() to
  1688. * convert it to flash-chip-specific range bits, then sets into status register.
  1689. */
  1690. static int generic_set_range(const struct flashctx *flash,
  1691. unsigned int start, unsigned int len)
  1692. {
  1693. uint8_t status, expected;
  1694. status = do_read_status(flash);
  1695. msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
  1696. expected = status; /* preserve non-bp bits */
  1697. if (generic_range_to_status(flash, start, len, &expected))
  1698. return -1;
  1699. do_write_status(flash, expected);
  1700. status = do_read_status(flash);
  1701. msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
  1702. if (status != expected) {
  1703. msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
  1704. expected, status);
  1705. return 1;
  1706. }
  1707. return 0;
  1708. }
  1709. /* Set/clear the status regsiter write protect bit in SR1. */
  1710. static int generic_set_srp0(const struct flashctx *flash, int enable)
  1711. {
  1712. uint8_t status, expected;
  1713. struct generic_wp *wp;
  1714. int num_entries;
  1715. if (generic_range_table(flash, &wp, &num_entries))
  1716. return -1;
  1717. expected = do_read_status(flash);
  1718. msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
  1719. if (enable)
  1720. expected |= 1 << wp->sr1.srp_pos;
  1721. else
  1722. expected &= ~(1 << wp->sr1.srp_pos);
  1723. do_write_status(flash, expected);
  1724. status = do_read_status(flash);
  1725. msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
  1726. if (status != expected)
  1727. return -1;
  1728. return 0;
  1729. }
  1730. static int generic_enable_writeprotect(const struct flashctx *flash,
  1731. enum wp_mode wp_mode)
  1732. {
  1733. int ret;
  1734. switch (wp_mode) {
  1735. case WP_MODE_HARDWARE:
  1736. ret = generic_set_srp0(flash, 1);
  1737. break;
  1738. default:
  1739. msg_cerr("%s(): unsupported write-protect mode\n", __func__);
  1740. return 1;
  1741. }
  1742. if (ret)
  1743. msg_cerr("%s(): error=%d.\n", __func__, ret);
  1744. return ret;
  1745. }
  1746. static int generic_disable_writeprotect(const struct flashctx *flash)
  1747. {
  1748. int ret;
  1749. ret = generic_set_srp0(flash, 0);
  1750. if (ret)
  1751. msg_cerr("%s(): error=%d.\n", __func__, ret);
  1752. return ret;
  1753. }
  1754. static int generic_list_ranges(const struct flashctx *flash)
  1755. {
  1756. struct generic_wp *wp;
  1757. struct generic_range *r;
  1758. int i, num_entries;
  1759. if (generic_range_table(flash, &wp, &num_entries))
  1760. return -1;
  1761. r = &wp->ranges[0];
  1762. for (i = 0; i < num_entries; i++) {
  1763. msg_cinfo("start: 0x%06x, length: 0x%06x\n",
  1764. r->range.start, r->range.len);
  1765. r++;
  1766. }
  1767. return 0;
  1768. }
  1769. static int generic_wp_status(const struct flashctx *flash)
  1770. {
  1771. uint8_t sr1;
  1772. unsigned int start, len;
  1773. int ret = 0;
  1774. struct generic_wp *wp;
  1775. int num_entries, wp_en;
  1776. if (generic_range_table(flash, &wp, &num_entries))
  1777. return -1;
  1778. sr1 = do_read_status(flash);
  1779. wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
  1780. msg_cinfo("WP: status: 0x%04x\n", sr1);
  1781. msg_cinfo("WP: status.srp0: %x\n", wp_en);
  1782. /* FIXME: SRP1 is not really generic, but we probably should print
  1783. * it anyway to have consistent output. #legacycruft */
  1784. msg_cinfo("WP: status.srp1: %x\n", 0);
  1785. msg_cinfo("WP: write protect is %s.\n",
  1786. wp_en ? "enabled" : "disabled");
  1787. msg_cinfo("WP: write protect range: ");
  1788. if (generic_status_to_range(flash, sr1, &start, &len)) {
  1789. msg_cinfo("(cannot resolve the range)\n");
  1790. ret = -1;
  1791. } else {
  1792. msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
  1793. }
  1794. return ret;
  1795. }
  1796. struct wp wp_generic = {
  1797. .list_ranges = generic_list_ranges,
  1798. .set_range = generic_set_range,
  1799. .enable = generic_enable_writeprotect,
  1800. .disable = generic_disable_writeprotect,
  1801. .wp_status = generic_wp_status,
  1802. };