ichspi.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com>
  5. * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
  6. * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com>
  7. * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
  8. * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
  9. * Copyright (C) 2011 Stefan Tauner
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #if defined(__i386__) || defined(__x86_64__)
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "flash.h"
  29. #include "programmer.h"
  30. #include "hwaccess.h"
  31. #include "spi.h"
  32. #include "ich_descriptors.h"
  33. /* ICH9 controller register definition */
  34. #define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
  35. #define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
  36. #define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
  37. #define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
  38. #define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
  39. #define HSFS_AEL_OFF 2 /* 2: Access Error Log */
  40. #define HSFS_AEL (0x1 << HSFS_AEL_OFF)
  41. #define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
  42. #define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
  43. #define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
  44. #define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
  45. /* 6-12: reserved */
  46. #define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
  47. #define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
  48. #define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
  49. #define HSFS_FDV (0x1 << HSFS_FDV_OFF)
  50. #define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
  51. #define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
  52. #define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
  53. #define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
  54. #define HSFC_FGO (0x1 << HSFC_FGO_OFF)
  55. #define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
  56. #define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
  57. /* 3-7: reserved */
  58. #define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
  59. #define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
  60. /* 14: reserved */
  61. #define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
  62. #define HSFC_SME (0x1 << HSFC_SME_OFF)
  63. #define ICH9_REG_FADDR 0x08 /* 32 Bits */
  64. #define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
  65. #define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
  66. #define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
  67. #define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
  68. #define PR_WP_OFF 31 /* 31: write protection enable */
  69. #define PR_RP_OFF 15 /* 15: read protection enable */
  70. #define ICH9_REG_SSFS 0x90 /* 08 Bits */
  71. #define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
  72. #define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
  73. #define SSFS_FDONE_OFF 2 /* Cycle Done Status */
  74. #define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
  75. #define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
  76. #define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
  77. #define SSFS_AEL_OFF 4 /* Access Error Log */
  78. #define SSFS_AEL (0x1 << SSFS_AEL_OFF)
  79. /* The following bits are reserved in SSFS: 1,5-7. */
  80. #define SSFS_RESERVED_MASK 0x000000e2
  81. #define ICH9_REG_SSFC 0x91 /* 24 Bits */
  82. /* We combine SSFS and SSFC to one 32-bit word,
  83. * therefore SSFC bits are off by 8. */
  84. /* 0: reserved */
  85. #define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
  86. #define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
  87. #define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
  88. #define SSFC_ACS (0x1 << SSFC_ACS_OFF)
  89. #define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
  90. #define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
  91. #define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
  92. #define SSFC_COP (0x7 << SSFC_COP_OFF)
  93. /* 7: reserved */
  94. #define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
  95. #define SSFC_DBC (0x3f << SSFC_DBC_OFF)
  96. #define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
  97. #define SSFC_DS (0x1 << SSFC_DS_OFF)
  98. #define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
  99. #define SSFC_SME (0x1 << SSFC_SME_OFF)
  100. #define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
  101. #define SSFC_SCF (0x7 << SSFC_SCF_OFF)
  102. #define SSFC_SCF_20MHZ 0x00000000
  103. #define SSFC_SCF_33MHZ 0x01000000
  104. /* 19-23: reserved */
  105. #define SSFC_RESERVED_MASK 0xf8008100
  106. #define ICH9_REG_PREOP 0x94 /* 16 Bits */
  107. #define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
  108. #define ICH9_REG_OPMENU 0x98 /* 64 Bits */
  109. #define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
  110. #define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
  111. #define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
  112. #define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
  113. #define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
  114. /* The individual fields of the VSCC registers are defined in the file
  115. * ich_descriptors.h. The reason is that the same layout is also used in the
  116. * flash descriptor to define the properties of the different flash chips
  117. * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
  118. * with the information from the descriptor on startup depending on the actual
  119. * chip(s) detected. */
  120. #define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
  121. #define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
  122. #define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
  123. // ICH9R SPI commands
  124. #define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
  125. #define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
  126. #define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
  127. #define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
  128. // ICH7 registers
  129. #define ICH7_REG_SPIS 0x00 /* 16 Bits */
  130. #define SPIS_SCIP 0x0001
  131. #define SPIS_GRANT 0x0002
  132. #define SPIS_CDS 0x0004
  133. #define SPIS_FCERR 0x0008
  134. #define SPIS_RESERVED_MASK 0x7ff0
  135. /* VIA SPI is compatible with ICH7, but maxdata
  136. to transfer is 16 bytes.
  137. DATA byte count on ICH7 is 8:13, on VIA 8:11
  138. bit 12 is port select CS0 CS1
  139. bit 13 is FAST READ enable
  140. bit 7 is used with fast read and one shot controls CS de-assert?
  141. */
  142. #define ICH7_REG_SPIC 0x02 /* 16 Bits */
  143. #define SPIC_SCGO 0x0002
  144. #define SPIC_ACS 0x0004
  145. #define SPIC_SPOP 0x0008
  146. #define SPIC_DS 0x4000
  147. #define ICH7_REG_SPIA 0x04 /* 32 Bits */
  148. #define ICH7_REG_SPID0 0x08 /* 64 Bytes */
  149. #define ICH7_REG_PREOP 0x54 /* 16 Bits */
  150. #define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
  151. #define ICH7_REG_OPMENU 0x58 /* 64 Bits */
  152. /* ICH SPI configuration lock-down. May be set during chipset enabling. */
  153. static int ichspi_lock = 0;
  154. static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
  155. uint32_t ichspi_bbar = 0;
  156. static void *ich_spibar = NULL;
  157. typedef struct _OPCODE {
  158. uint8_t opcode; //This commands spi opcode
  159. uint8_t spi_type; //This commands spi type
  160. uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
  161. } OPCODE;
  162. /* Suggested opcode definition:
  163. * Preop 1: Write Enable
  164. * Preop 2: Write Status register enable
  165. *
  166. * OP 0: Write address
  167. * OP 1: Read Address
  168. * OP 2: ERASE block
  169. * OP 3: Read Status register
  170. * OP 4: Read ID
  171. * OP 5: Write Status register
  172. * OP 6: chip private (read JEDEC id)
  173. * OP 7: Chip erase
  174. */
  175. typedef struct _OPCODES {
  176. uint8_t preop[2];
  177. OPCODE opcode[8];
  178. } OPCODES;
  179. static OPCODES *curopcodes = NULL;
  180. /* HW access functions */
  181. static uint32_t REGREAD32(int X)
  182. {
  183. return mmio_readl(ich_spibar + X);
  184. }
  185. static uint16_t REGREAD16(int X)
  186. {
  187. return mmio_readw(ich_spibar + X);
  188. }
  189. static uint16_t REGREAD8(int X)
  190. {
  191. return mmio_readb(ich_spibar + X);
  192. }
  193. #define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
  194. #define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
  195. #define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
  196. /* Common SPI functions */
  197. static int find_opcode(OPCODES *op, uint8_t opcode);
  198. static int find_preop(OPCODES *op, uint8_t preop);
  199. static int generate_opcodes(OPCODES * op);
  200. static int program_opcodes(OPCODES *op, int enable_undo);
  201. static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
  202. uint8_t datalength, uint8_t * data);
  203. /* for pairing opcodes with their required preop */
  204. struct preop_opcode_pair {
  205. uint8_t preop;
  206. uint8_t opcode;
  207. };
  208. /* List of opcodes which need preopcodes and matching preopcodes. Unused. */
  209. const struct preop_opcode_pair pops[] = {
  210. {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
  211. {JEDEC_WREN, JEDEC_SE}, /* sector erase */
  212. {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
  213. {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
  214. {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
  215. {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
  216. /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
  217. {JEDEC_WREN, JEDEC_WRSR},
  218. {JEDEC_EWSR, JEDEC_WRSR},
  219. {0,}
  220. };
  221. /* Reasonable default configuration. Needs ad-hoc modifications if we
  222. * encounter unlisted opcodes. Fun.
  223. */
  224. static OPCODES O_ST_M25P = {
  225. {
  226. JEDEC_WREN,
  227. JEDEC_EWSR,
  228. },
  229. {
  230. {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
  231. {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
  232. {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
  233. {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
  234. {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
  235. {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
  236. {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
  237. {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
  238. }
  239. };
  240. /* List of opcodes with their corresponding spi_type
  241. * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
  242. * is needed which is currently not in the chipset OPCODE table
  243. */
  244. static OPCODE POSSIBLE_OPCODES[] = {
  245. {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
  246. {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
  247. {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
  248. {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
  249. {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
  250. {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
  251. {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
  252. {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
  253. {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
  254. {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
  255. {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
  256. };
  257. static OPCODES O_EXISTING = {};
  258. /* pretty printing functions */
  259. static void prettyprint_opcodes(OPCODES *ops)
  260. {
  261. OPCODE oc;
  262. const char *t;
  263. const char *a;
  264. uint8_t i;
  265. static const char *const spi_type[4] = {
  266. "read w/o addr",
  267. "write w/o addr",
  268. "read w/ addr",
  269. "write w/ addr"
  270. };
  271. static const char *const atomic_type[3] = {
  272. "none",
  273. " 0 ",
  274. " 1 "
  275. };
  276. if (ops == NULL)
  277. return;
  278. msg_pdbg2(" OP Type Pre-OP\n");
  279. for (i = 0; i < 8; i++) {
  280. oc = ops->opcode[i];
  281. t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
  282. a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
  283. msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
  284. }
  285. msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
  286. ops->preop[1]);
  287. }
  288. #define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
  289. static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
  290. {
  291. msg_pdbg("HSFS: ");
  292. pprint_reg(HSFS, FDONE, reg_val, ", ");
  293. pprint_reg(HSFS, FCERR, reg_val, ", ");
  294. pprint_reg(HSFS, AEL, reg_val, ", ");
  295. pprint_reg(HSFS, BERASE, reg_val, ", ");
  296. pprint_reg(HSFS, SCIP, reg_val, ", ");
  297. pprint_reg(HSFS, FDOPSS, reg_val, ", ");
  298. pprint_reg(HSFS, FDV, reg_val, ", ");
  299. pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
  300. }
  301. static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
  302. {
  303. msg_pdbg("HSFC: ");
  304. pprint_reg(HSFC, FGO, reg_val, ", ");
  305. pprint_reg(HSFC, FCYCLE, reg_val, ", ");
  306. pprint_reg(HSFC, FDBC, reg_val, ", ");
  307. pprint_reg(HSFC, SME, reg_val, "\n");
  308. }
  309. static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
  310. {
  311. msg_pdbg("SSFS: ");
  312. pprint_reg(SSFS, SCIP, reg_val, ", ");
  313. pprint_reg(SSFS, FDONE, reg_val, ", ");
  314. pprint_reg(SSFS, FCERR, reg_val, ", ");
  315. pprint_reg(SSFS, AEL, reg_val, "\n");
  316. }
  317. static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
  318. {
  319. msg_pdbg("SSFC: ");
  320. pprint_reg(SSFC, SCGO, reg_val, ", ");
  321. pprint_reg(SSFC, ACS, reg_val, ", ");
  322. pprint_reg(SSFC, SPOP, reg_val, ", ");
  323. pprint_reg(SSFC, COP, reg_val, ", ");
  324. pprint_reg(SSFC, DBC, reg_val, ", ");
  325. pprint_reg(SSFC, SME, reg_val, ", ");
  326. pprint_reg(SSFC, SCF, reg_val, "\n");
  327. }
  328. static uint8_t lookup_spi_type(uint8_t opcode)
  329. {
  330. int a;
  331. for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
  332. if (POSSIBLE_OPCODES[a].opcode == opcode)
  333. return POSSIBLE_OPCODES[a].spi_type;
  334. }
  335. return 0xFF;
  336. }
  337. static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
  338. {
  339. uint8_t spi_type;
  340. spi_type = lookup_spi_type(opcode);
  341. if (spi_type > 3) {
  342. /* Try to guess spi type from read/write sizes.
  343. * The following valid writecnt/readcnt combinations exist:
  344. * writecnt = 4, readcnt >= 0
  345. * writecnt = 1, readcnt >= 0
  346. * writecnt >= 4, readcnt = 0
  347. * writecnt >= 1, readcnt = 0
  348. * writecnt >= 1 is guaranteed for all commands.
  349. */
  350. if (readcnt == 0)
  351. /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
  352. * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
  353. * bytes are actual the address, they go to the bus anyhow
  354. */
  355. spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
  356. else if (writecnt == 1) // and readcnt is > 0
  357. spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
  358. else if (writecnt == 4) // and readcnt is > 0
  359. spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
  360. else // we have an invalid case
  361. return SPI_INVALID_LENGTH;
  362. }
  363. int oppos = 2; // use original JEDEC_BE_D8 offset
  364. curopcodes->opcode[oppos].opcode = opcode;
  365. curopcodes->opcode[oppos].spi_type = spi_type;
  366. program_opcodes(curopcodes, 0);
  367. oppos = find_opcode(curopcodes, opcode);
  368. msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
  369. return oppos;
  370. }
  371. static int find_opcode(OPCODES *op, uint8_t opcode)
  372. {
  373. int a;
  374. if (op == NULL) {
  375. msg_perr("\n%s: null OPCODES pointer!\n", __func__);
  376. return -1;
  377. }
  378. for (a = 0; a < 8; a++) {
  379. if (op->opcode[a].opcode == opcode)
  380. return a;
  381. }
  382. return -1;
  383. }
  384. static int find_preop(OPCODES *op, uint8_t preop)
  385. {
  386. int a;
  387. if (op == NULL) {
  388. msg_perr("\n%s: null OPCODES pointer!\n", __func__);
  389. return -1;
  390. }
  391. for (a = 0; a < 2; a++) {
  392. if (op->preop[a] == preop)
  393. return a;
  394. }
  395. return -1;
  396. }
  397. /* Create a struct OPCODES based on what we find in the locked down chipset. */
  398. static int generate_opcodes(OPCODES * op)
  399. {
  400. int a;
  401. uint16_t preop, optype;
  402. uint32_t opmenu[2];
  403. if (op == NULL) {
  404. msg_perr("\n%s: null OPCODES pointer!\n", __func__);
  405. return -1;
  406. }
  407. switch (ich_generation) {
  408. case CHIPSET_ICH7:
  409. case CHIPSET_TUNNEL_CREEK:
  410. case CHIPSET_CENTERTON:
  411. preop = REGREAD16(ICH7_REG_PREOP);
  412. optype = REGREAD16(ICH7_REG_OPTYPE);
  413. opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
  414. opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
  415. break;
  416. case CHIPSET_ICH8:
  417. default: /* Future version might behave the same */
  418. preop = REGREAD16(ICH9_REG_PREOP);
  419. optype = REGREAD16(ICH9_REG_OPTYPE);
  420. opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
  421. opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
  422. break;
  423. }
  424. op->preop[0] = (uint8_t) preop;
  425. op->preop[1] = (uint8_t) (preop >> 8);
  426. for (a = 0; a < 8; a++) {
  427. op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
  428. optype >>= 2;
  429. }
  430. for (a = 0; a < 4; a++) {
  431. op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
  432. opmenu[0] >>= 8;
  433. }
  434. for (a = 4; a < 8; a++) {
  435. op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
  436. opmenu[1] >>= 8;
  437. }
  438. /* No preopcodes used by default. */
  439. for (a = 0; a < 8; a++)
  440. op->opcode[a].atomic = 0;
  441. return 0;
  442. }
  443. static int program_opcodes(OPCODES *op, int enable_undo)
  444. {
  445. uint8_t a;
  446. uint16_t preop, optype;
  447. uint32_t opmenu[2];
  448. /* Program Prefix Opcodes */
  449. /* 0:7 Prefix Opcode 1 */
  450. preop = (op->preop[0]);
  451. /* 8:16 Prefix Opcode 2 */
  452. preop |= ((uint16_t) op->preop[1]) << 8;
  453. /* Program Opcode Types 0 - 7 */
  454. optype = 0;
  455. for (a = 0; a < 8; a++) {
  456. optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
  457. }
  458. /* Program Allowable Opcodes 0 - 3 */
  459. opmenu[0] = 0;
  460. for (a = 0; a < 4; a++) {
  461. opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
  462. }
  463. /* Program Allowable Opcodes 4 - 7 */
  464. opmenu[1] = 0;
  465. for (a = 4; a < 8; a++) {
  466. opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
  467. }
  468. msg_pdbg2("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
  469. switch (ich_generation) {
  470. case CHIPSET_ICH7:
  471. case CHIPSET_TUNNEL_CREEK:
  472. case CHIPSET_CENTERTON:
  473. /* Register undo only for enable_undo=1, i.e. first call. */
  474. if (enable_undo) {
  475. rmmio_valw(ich_spibar + ICH7_REG_PREOP);
  476. rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
  477. rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
  478. rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
  479. }
  480. mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
  481. mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
  482. mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
  483. mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
  484. break;
  485. case CHIPSET_ICH8:
  486. default: /* Future version might behave the same */
  487. /* Register undo only for enable_undo=1, i.e. first call. */
  488. if (enable_undo) {
  489. rmmio_valw(ich_spibar + ICH9_REG_PREOP);
  490. rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
  491. rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
  492. rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
  493. }
  494. mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
  495. mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
  496. mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
  497. mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
  498. break;
  499. }
  500. return 0;
  501. }
  502. /*
  503. * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
  504. * FIXME: this should also check for
  505. * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
  506. * - at least one erasing opcode (lots.)
  507. * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
  508. * - necessary preops? (EWSR, WREN, ...?)
  509. */
  510. static int ich_missing_opcodes()
  511. {
  512. uint8_t ops[] = {
  513. JEDEC_READ,
  514. JEDEC_RDSR,
  515. 0
  516. };
  517. int i = 0;
  518. while (ops[i] != 0) {
  519. msg_pspew("checking for opcode 0x%02x\n", ops[i]);
  520. if (find_opcode(curopcodes, ops[i]) == -1)
  521. return -1;
  522. i++;
  523. }
  524. return 0;
  525. }
  526. /*
  527. * Try to set BBAR (BIOS Base Address Register), but read back the value in case
  528. * it didn't stick.
  529. */
  530. static void ich_set_bbar(uint32_t min_addr)
  531. {
  532. int bbar_off;
  533. switch (ich_generation) {
  534. case CHIPSET_ICH7:
  535. case CHIPSET_TUNNEL_CREEK:
  536. case CHIPSET_CENTERTON:
  537. bbar_off = 0x50;
  538. break;
  539. case CHIPSET_ICH8:
  540. case CHIPSET_BAYTRAIL:
  541. msg_pdbg("BBAR offset is unknown!\n");
  542. return;
  543. case CHIPSET_ICH9:
  544. default: /* Future version might behave the same */
  545. bbar_off = ICH9_REG_BBAR;
  546. break;
  547. }
  548. ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
  549. if (ichspi_bbar) {
  550. msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
  551. ichspi_bbar);
  552. }
  553. min_addr &= BBAR_MASK;
  554. ichspi_bbar |= min_addr;
  555. rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
  556. ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
  557. /* We don't have any option except complaining. And if the write
  558. * failed, the restore will fail as well, so no problem there.
  559. */
  560. if (ichspi_bbar != min_addr)
  561. msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
  562. min_addr, ichspi_bbar);
  563. }
  564. /* Read len bytes from the fdata/spid register into the data array.
  565. *
  566. * Note that using len > flash->mst->spi.max_data_read will return garbage or
  567. * may even crash.
  568. */
  569. static void ich_read_data(uint8_t *data, int len, int reg0_off)
  570. {
  571. int i;
  572. uint32_t temp32 = 0;
  573. for (i = 0; i < len; i++) {
  574. if ((i % 4) == 0)
  575. temp32 = REGREAD32(reg0_off + i);
  576. data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
  577. }
  578. }
  579. /* Fill len bytes from the data array into the fdata/spid registers.
  580. *
  581. * Note that using len > flash->mst->spi.max_data_write will trash the registers
  582. * following the data registers.
  583. */
  584. static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
  585. {
  586. uint32_t temp32 = 0;
  587. int i;
  588. if (len <= 0)
  589. return;
  590. for (i = 0; i < len; i++) {
  591. if ((i % 4) == 0)
  592. temp32 = 0;
  593. temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
  594. if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
  595. REGWRITE32(reg0_off + (i - (i % 4)), temp32);
  596. }
  597. i--;
  598. if ((i % 4) != 3) /* Write remaining data to regs. */
  599. REGWRITE32(reg0_off + (i - (i % 4)), temp32);
  600. }
  601. /* This function generates OPCODES from or programs OPCODES to ICH according to
  602. * the chipset's SPI configuration lock.
  603. *
  604. * It should be called before ICH sends any spi command.
  605. */
  606. static int ich_init_opcodes(void)
  607. {
  608. int rc = 0;
  609. OPCODES *curopcodes_done;
  610. if (curopcodes)
  611. return 0;
  612. if (ichspi_lock) {
  613. msg_pdbg("Reading OPCODES... ");
  614. curopcodes_done = &O_EXISTING;
  615. rc = generate_opcodes(curopcodes_done);
  616. } else {
  617. msg_pdbg("Programming OPCODES... ");
  618. curopcodes_done = &O_ST_M25P;
  619. rc = program_opcodes(curopcodes_done, 1);
  620. }
  621. if (rc) {
  622. curopcodes = NULL;
  623. msg_perr("failed\n");
  624. return 1;
  625. } else {
  626. curopcodes = curopcodes_done;
  627. msg_pdbg("done\n");
  628. prettyprint_opcodes(curopcodes);
  629. return 0;
  630. }
  631. }
  632. static int ich7_run_opcode(OPCODE op, uint32_t offset,
  633. uint8_t datalength, uint8_t * data, int maxdata)
  634. {
  635. int write_cmd = 0;
  636. int timeout;
  637. uint32_t temp32;
  638. uint16_t temp16;
  639. uint64_t opmenu;
  640. int opcode_index;
  641. /* Is it a write command? */
  642. if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
  643. || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
  644. write_cmd = 1;
  645. }
  646. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  647. while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
  648. programmer_delay(10);
  649. }
  650. if (!timeout) {
  651. msg_perr("Error: SCIP never cleared!\n");
  652. return 1;
  653. }
  654. /* Program offset in flash into SPIA while preserving reserved bits. */
  655. temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
  656. REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
  657. /* Program data into SPID0 to N */
  658. if (write_cmd && (datalength != 0))
  659. ich_fill_data(data, datalength, ICH7_REG_SPID0);
  660. /* Assemble SPIS */
  661. temp16 = REGREAD16(ICH7_REG_SPIS);
  662. /* keep reserved bits */
  663. temp16 &= SPIS_RESERVED_MASK;
  664. /* clear error status registers */
  665. temp16 |= (SPIS_CDS | SPIS_FCERR);
  666. REGWRITE16(ICH7_REG_SPIS, temp16);
  667. /* Assemble SPIC */
  668. temp16 = 0;
  669. if (datalength != 0) {
  670. temp16 |= SPIC_DS;
  671. temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
  672. }
  673. /* Select opcode */
  674. opmenu = REGREAD32(ICH7_REG_OPMENU);
  675. opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
  676. for (opcode_index = 0; opcode_index < 8; opcode_index++) {
  677. if ((opmenu & 0xff) == op.opcode) {
  678. break;
  679. }
  680. opmenu >>= 8;
  681. }
  682. if (opcode_index == 8) {
  683. msg_pdbg("Opcode %x not found.\n", op.opcode);
  684. return 1;
  685. }
  686. temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
  687. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  688. /* Handle Atomic. Atomic commands include three steps:
  689. - sending the preop (mainly EWSR or WREN)
  690. - sending the main command
  691. - waiting for the busy bit (WIP) to be cleared
  692. This means the timeout must be sufficient for chip erase
  693. of slow high-capacity chips.
  694. */
  695. switch (op.atomic) {
  696. case 2:
  697. /* Select second preop. */
  698. temp16 |= SPIC_SPOP;
  699. /* And fall through. */
  700. case 1:
  701. /* Atomic command (preop+op) */
  702. temp16 |= SPIC_ACS;
  703. timeout = 100 * 1000 * 60; /* 60 seconds */
  704. break;
  705. }
  706. /* Start */
  707. temp16 |= SPIC_SCGO;
  708. /* write it */
  709. REGWRITE16(ICH7_REG_SPIC, temp16);
  710. /* Wait for Cycle Done Status or Flash Cycle Error. */
  711. while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
  712. --timeout) {
  713. programmer_delay(10);
  714. }
  715. if (!timeout) {
  716. msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
  717. REGREAD16(ICH7_REG_SPIS));
  718. return 1;
  719. }
  720. /* FIXME: make sure we do not needlessly cause transaction errors. */
  721. temp16 = REGREAD16(ICH7_REG_SPIS);
  722. if (temp16 & SPIS_FCERR) {
  723. msg_perr("Transaction error!\n");
  724. /* keep reserved bits */
  725. temp16 &= SPIS_RESERVED_MASK;
  726. REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
  727. return 1;
  728. }
  729. if ((!write_cmd) && (datalength != 0))
  730. ich_read_data(data, datalength, ICH7_REG_SPID0);
  731. return 0;
  732. }
  733. static int ich9_run_opcode(OPCODE op, uint32_t offset,
  734. uint8_t datalength, uint8_t * data)
  735. {
  736. int write_cmd = 0;
  737. int timeout;
  738. uint32_t temp32;
  739. uint64_t opmenu;
  740. int opcode_index;
  741. /* Is it a write command? */
  742. if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
  743. || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
  744. write_cmd = 1;
  745. }
  746. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  747. while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
  748. programmer_delay(10);
  749. }
  750. if (!timeout) {
  751. msg_perr("Error: SCIP never cleared!\n");
  752. return 1;
  753. }
  754. /* Program offset in flash into FADDR while preserve the reserved bits
  755. * and clearing the 25. address bit which is only useable in hwseq. */
  756. temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
  757. REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
  758. /* Program data into FDATA0 to N */
  759. if (write_cmd && (datalength != 0))
  760. ich_fill_data(data, datalength, ICH9_REG_FDATA0);
  761. /* Assemble SSFS + SSFC */
  762. temp32 = REGREAD32(ICH9_REG_SSFS);
  763. /* Keep reserved bits only */
  764. temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
  765. /* Clear cycle done and cycle error status registers */
  766. temp32 |= (SSFS_FDONE | SSFS_FCERR);
  767. REGWRITE32(ICH9_REG_SSFS, temp32);
  768. /* Use 20 MHz */
  769. temp32 |= SSFC_SCF_20MHZ;
  770. /* Set data byte count (DBC) and data cycle bit (DS) */
  771. if (datalength != 0) {
  772. uint32_t datatemp;
  773. temp32 |= SSFC_DS;
  774. datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
  775. SSFC_DBC);
  776. temp32 |= datatemp;
  777. }
  778. /* Select opcode */
  779. opmenu = REGREAD32(ICH9_REG_OPMENU);
  780. opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
  781. for (opcode_index = 0; opcode_index < 8; opcode_index++) {
  782. if ((opmenu & 0xff) == op.opcode) {
  783. break;
  784. }
  785. opmenu >>= 8;
  786. }
  787. if (opcode_index == 8) {
  788. msg_pdbg("Opcode %x not found.\n", op.opcode);
  789. return 1;
  790. }
  791. temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
  792. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  793. /* Handle Atomic. Atomic commands include three steps:
  794. - sending the preop (mainly EWSR or WREN)
  795. - sending the main command
  796. - waiting for the busy bit (WIP) to be cleared
  797. This means the timeout must be sufficient for chip erase
  798. of slow high-capacity chips.
  799. */
  800. switch (op.atomic) {
  801. case 2:
  802. /* Select second preop. */
  803. temp32 |= SSFC_SPOP;
  804. /* And fall through. */
  805. case 1:
  806. /* Atomic command (preop+op) */
  807. temp32 |= SSFC_ACS;
  808. timeout = 100 * 1000 * 60; /* 60 seconds */
  809. break;
  810. }
  811. /* Start */
  812. temp32 |= SSFC_SCGO;
  813. /* write it */
  814. REGWRITE32(ICH9_REG_SSFS, temp32);
  815. /* Wait for Cycle Done Status or Flash Cycle Error. */
  816. while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
  817. --timeout) {
  818. programmer_delay(10);
  819. }
  820. if (!timeout) {
  821. msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
  822. REGREAD32(ICH9_REG_SSFS));
  823. return 1;
  824. }
  825. /* FIXME make sure we do not needlessly cause transaction errors. */
  826. temp32 = REGREAD32(ICH9_REG_SSFS);
  827. if (temp32 & SSFS_FCERR) {
  828. msg_perr("Transaction error!\n");
  829. prettyprint_ich9_reg_ssfs(temp32);
  830. prettyprint_ich9_reg_ssfc(temp32);
  831. /* keep reserved bits */
  832. temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
  833. /* Clear the transaction error. */
  834. REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
  835. return 1;
  836. }
  837. if ((!write_cmd) && (datalength != 0))
  838. ich_read_data(data, datalength, ICH9_REG_FDATA0);
  839. return 0;
  840. }
  841. static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
  842. uint8_t datalength, uint8_t * data)
  843. {
  844. /* max_data_read == max_data_write for all Intel/VIA SPI masters */
  845. uint8_t maxlength = flash->mst->spi.max_data_read;
  846. if (ich_generation == CHIPSET_ICH_UNKNOWN) {
  847. msg_perr("%s: unsupported chipset\n", __func__);
  848. return -1;
  849. }
  850. if (datalength > maxlength) {
  851. msg_perr("%s: Internal command size error for "
  852. "opcode 0x%02x, got datalength=%i, want <=%i\n",
  853. __func__, op.opcode, datalength, maxlength);
  854. return SPI_INVALID_LENGTH;
  855. }
  856. switch (ich_generation) {
  857. case CHIPSET_ICH7:
  858. case CHIPSET_TUNNEL_CREEK:
  859. case CHIPSET_CENTERTON:
  860. return ich7_run_opcode(op, offset, datalength, data, maxlength);
  861. case CHIPSET_ICH8:
  862. default: /* Future version might behave the same */
  863. return ich9_run_opcode(op, offset, datalength, data);
  864. }
  865. }
  866. static int ich_spi_send_command(struct flashctx *flash, unsigned int writecnt,
  867. unsigned int readcnt,
  868. const unsigned char *writearr,
  869. unsigned char *readarr)
  870. {
  871. int result;
  872. int opcode_index = -1;
  873. const unsigned char cmd = *writearr;
  874. OPCODE *opcode;
  875. uint32_t addr = 0;
  876. uint8_t *data;
  877. int count;
  878. /* find cmd in opcodes-table */
  879. opcode_index = find_opcode(curopcodes, cmd);
  880. if (opcode_index == -1) {
  881. if (!ichspi_lock)
  882. opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
  883. if (opcode_index == SPI_INVALID_LENGTH) {
  884. msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
  885. return SPI_INVALID_LENGTH;
  886. } else if (opcode_index == -1) {
  887. msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
  888. cmd);
  889. return SPI_INVALID_OPCODE;
  890. }
  891. }
  892. opcode = &(curopcodes->opcode[opcode_index]);
  893. /* The following valid writecnt/readcnt combinations exist:
  894. * writecnt = 4, readcnt >= 0
  895. * writecnt = 1, readcnt >= 0
  896. * writecnt >= 4, readcnt = 0
  897. * writecnt >= 1, readcnt = 0
  898. * writecnt >= 1 is guaranteed for all commands.
  899. */
  900. if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
  901. (writecnt != 4)) {
  902. msg_perr("%s: Internal command size error for opcode "
  903. "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
  904. writecnt);
  905. return SPI_INVALID_LENGTH;
  906. }
  907. if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
  908. (writecnt != 1)) {
  909. msg_perr("%s: Internal command size error for opcode "
  910. "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
  911. writecnt);
  912. return SPI_INVALID_LENGTH;
  913. }
  914. if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
  915. (writecnt < 4)) {
  916. msg_perr("%s: Internal command size error for opcode "
  917. "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
  918. writecnt);
  919. return SPI_INVALID_LENGTH;
  920. }
  921. if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
  922. (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
  923. (readcnt)) {
  924. msg_perr("%s: Internal command size error for opcode "
  925. "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
  926. readcnt);
  927. return SPI_INVALID_LENGTH;
  928. }
  929. /* if opcode-type requires an address */
  930. if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
  931. opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
  932. addr = (writearr[1] << 16) |
  933. (writearr[2] << 8) | (writearr[3] << 0);
  934. if (addr < ichspi_bbar) {
  935. msg_perr("%s: Address 0x%06x below allowed "
  936. "range 0x%06x-0xffffff\n", __func__,
  937. addr, ichspi_bbar);
  938. return SPI_INVALID_ADDRESS;
  939. }
  940. }
  941. /* Translate read/write array/count.
  942. * The maximum data length is identical for the maximum read length and
  943. * for the maximum write length excluding opcode and address. Opcode and
  944. * address are stored in separate registers, not in the data registers
  945. * and are thus not counted towards data length. The only exception
  946. * applies if the opcode definition (un)intentionally classifies said
  947. * opcode incorrectly as non-address opcode or vice versa. */
  948. if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
  949. data = (uint8_t *) (writearr + 1);
  950. count = writecnt - 1;
  951. } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
  952. data = (uint8_t *) (writearr + 4);
  953. count = writecnt - 4;
  954. } else {
  955. data = (uint8_t *) readarr;
  956. count = readcnt;
  957. }
  958. result = run_opcode(flash, *opcode, addr, count, data);
  959. if (result) {
  960. msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
  961. if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
  962. (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
  963. msg_pdbg("at address 0x%06x ", addr);
  964. }
  965. msg_pdbg("(payload length was %d).\n", count);
  966. /* Print out the data array if it contains data to write.
  967. * Errors are detected before the received data is read back into
  968. * the array so it won't make sense to print it then. */
  969. if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
  970. (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
  971. int i;
  972. msg_pspew("The data was:\n");
  973. for (i = 0; i < count; i++){
  974. msg_pspew("%3d: 0x%02x\n", i, data[i]);
  975. }
  976. }
  977. }
  978. return result;
  979. }
  980. static struct hwseq_data {
  981. uint32_t size_comp0;
  982. uint32_t size_comp1;
  983. } hwseq_data;
  984. /* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
  985. static void ich_hwseq_set_addr(uint32_t addr)
  986. {
  987. uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
  988. REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old);
  989. }
  990. /* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
  991. * of the block containing this address. May return nonsense if the address is
  992. * not valid. The erase block size for a specific address depends on the flash
  993. * partition layout as specified by FPB and the partition properties as defined
  994. * by UVSCC and LVSCC respectively. An alternative to implement this method
  995. * would be by querying FPB and the respective VSCC register directly.
  996. */
  997. static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
  998. {
  999. uint8_t enc_berase;
  1000. static const uint32_t dec_berase[4] = {
  1001. 256,
  1002. 4 * 1024,
  1003. 8 * 1024,
  1004. 64 * 1024
  1005. };
  1006. ich_hwseq_set_addr(addr);
  1007. enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >>
  1008. HSFS_BERASE_OFF;
  1009. return dec_berase[enc_berase];
  1010. }
  1011. /* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
  1012. Resets all error flags in HSFS.
  1013. Returns 0 if the cycle completes successfully without errors within
  1014. timeout us, 1 on errors. */
  1015. static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
  1016. unsigned int len)
  1017. {
  1018. uint16_t hsfs;
  1019. uint32_t addr;
  1020. timeout /= 8; /* scale timeout duration to counter */
  1021. while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
  1022. (HSFS_FDONE | HSFS_FCERR)) == 0) &&
  1023. --timeout) {
  1024. programmer_delay(8);
  1025. }
  1026. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1027. if (!timeout) {
  1028. addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
  1029. msg_perr("Timeout error between offset 0x%08x and "
  1030. "0x%08x (= 0x%08x + %d)!\n",
  1031. addr, addr + len - 1, addr, len - 1);
  1032. prettyprint_ich9_reg_hsfs(hsfs);
  1033. prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
  1034. return 1;
  1035. }
  1036. if (hsfs & HSFS_FCERR) {
  1037. addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
  1038. msg_perr("Transaction error between offset 0x%08x and "
  1039. "0x%08x (= 0x%08x + %d)!\n",
  1040. addr, addr + len - 1, addr, len - 1);
  1041. prettyprint_ich9_reg_hsfs(hsfs);
  1042. prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
  1043. return 1;
  1044. }
  1045. return 0;
  1046. }
  1047. static int ich_hwseq_probe(struct flashctx *flash)
  1048. {
  1049. uint32_t total_size, boundary;
  1050. uint32_t erase_size_low, size_low, erase_size_high, size_high;
  1051. struct block_eraser *eraser;
  1052. total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
  1053. msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
  1054. (hwseq_data.size_comp1 != 0) ? 2 : 1);
  1055. if (hwseq_data.size_comp1 != 0)
  1056. msg_cdbg("s with a combined");
  1057. else
  1058. msg_cdbg(" with a");
  1059. msg_cdbg(" density of %d kB.\n", total_size / 1024);
  1060. flash->chip->total_size = total_size / 1024;
  1061. eraser = &(flash->chip->block_erasers[0]);
  1062. boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
  1063. size_high = total_size - boundary;
  1064. erase_size_high = ich_hwseq_get_erase_block_size(boundary);
  1065. if (boundary == 0) {
  1066. msg_cdbg2("There is only one partition containing the whole "
  1067. "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
  1068. eraser->eraseblocks[0].size = erase_size_high;
  1069. eraser->eraseblocks[0].count = size_high / erase_size_high;
  1070. msg_cdbg2("There are %d erase blocks with %d B each.\n",
  1071. size_high / erase_size_high, erase_size_high);
  1072. } else {
  1073. msg_cdbg2("The flash address space (0x%06x - 0x%06x) is divided "
  1074. "at address 0x%06x in two partitions.\n",
  1075. 0, total_size-1, boundary);
  1076. size_low = total_size - size_high;
  1077. erase_size_low = ich_hwseq_get_erase_block_size(0);
  1078. eraser->eraseblocks[0].size = erase_size_low;
  1079. eraser->eraseblocks[0].count = size_low / erase_size_low;
  1080. msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
  1081. 0, size_low-1);
  1082. msg_cdbg("In that range are %d erase blocks with %d B each.\n",
  1083. size_low / erase_size_low, erase_size_low);
  1084. eraser->eraseblocks[1].size = erase_size_high;
  1085. eraser->eraseblocks[1].count = size_high / erase_size_high;
  1086. msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
  1087. boundary, total_size-1);
  1088. msg_cdbg("In that range are %d erase blocks with %d B each.\n",
  1089. size_high / erase_size_high, erase_size_high);
  1090. }
  1091. flash->chip->tested = TEST_OK_PREW;
  1092. return 1;
  1093. }
  1094. static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
  1095. unsigned int len)
  1096. {
  1097. uint32_t erase_block;
  1098. uint16_t hsfc;
  1099. uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
  1100. erase_block = ich_hwseq_get_erase_block_size(addr);
  1101. if (len != erase_block) {
  1102. msg_cerr("Erase block size for address 0x%06x is %d B, "
  1103. "but requested erase block size is %d B. "
  1104. "Not erasing anything.\n", addr, erase_block, len);
  1105. return -1;
  1106. }
  1107. /* Although the hardware supports this (it would erase the whole block
  1108. * containing the address) we play safe here. */
  1109. if (addr % erase_block != 0) {
  1110. msg_cerr("Erase address 0x%06x is not aligned to the erase "
  1111. "block boundary (any multiple of %d). "
  1112. "Not erasing anything.\n", addr, erase_block);
  1113. return -1;
  1114. }
  1115. if (addr + len > flash->chip->total_size * 1024) {
  1116. msg_perr("Request to erase some inaccessible memory address(es)"
  1117. " (addr=0x%x, len=%d). "
  1118. "Not erasing anything.\n", addr, len);
  1119. return -1;
  1120. }
  1121. msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
  1122. ich_hwseq_set_addr(addr);
  1123. /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
  1124. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1125. hsfc = REGREAD16(ICH9_REG_HSFC);
  1126. hsfc &= ~HSFC_FCYCLE; /* clear operation */
  1127. hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
  1128. hsfc |= HSFC_FGO; /* start */
  1129. msg_pdbg("HSFC used for block erasing: ");
  1130. prettyprint_ich9_reg_hsfc(hsfc);
  1131. REGWRITE16(ICH9_REG_HSFC, hsfc);
  1132. if (ich_hwseq_wait_for_cycle_complete(timeout, len))
  1133. return -1;
  1134. return 0;
  1135. }
  1136. static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
  1137. unsigned int addr, unsigned int len)
  1138. {
  1139. uint16_t hsfc;
  1140. uint16_t timeout = 100 * 60;
  1141. uint8_t block_len;
  1142. if (addr + len > flash->chip->total_size * 1024) {
  1143. msg_perr("Request to read from an inaccessible memory address "
  1144. "(addr=0x%x, len=%d).\n", addr, len);
  1145. return -1;
  1146. }
  1147. msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
  1148. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1149. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1150. while (len > 0) {
  1151. /* Obey programmer limit... */
  1152. block_len = min(len, flash->mst->opaque.max_data_read);
  1153. /* as well as flash chip page borders as demanded in the Intel datasheets. */
  1154. block_len = min(block_len, 256 - (addr & 0xFF));
  1155. ich_hwseq_set_addr(addr);
  1156. hsfc = REGREAD16(ICH9_REG_HSFC);
  1157. hsfc &= ~HSFC_FCYCLE; /* set read operation */
  1158. hsfc &= ~HSFC_FDBC; /* clear byte count */
  1159. /* set byte count */
  1160. hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
  1161. hsfc |= HSFC_FGO; /* start */
  1162. REGWRITE16(ICH9_REG_HSFC, hsfc);
  1163. if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
  1164. return 1;
  1165. ich_read_data(buf, block_len, ICH9_REG_FDATA0);
  1166. addr += block_len;
  1167. buf += block_len;
  1168. len -= block_len;
  1169. }
  1170. return 0;
  1171. }
  1172. static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
  1173. {
  1174. uint16_t hsfc;
  1175. uint16_t timeout = 100 * 60;
  1176. uint8_t block_len;
  1177. if (addr + len > flash->chip->total_size * 1024) {
  1178. msg_perr("Request to write to an inaccessible memory address "
  1179. "(addr=0x%x, len=%d).\n", addr, len);
  1180. return -1;
  1181. }
  1182. msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
  1183. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1184. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1185. while (len > 0) {
  1186. ich_hwseq_set_addr(addr);
  1187. /* Obey programmer limit... */
  1188. block_len = min(len, flash->mst->opaque.max_data_write);
  1189. /* as well as flash chip page borders as demanded in the Intel datasheets. */
  1190. block_len = min(block_len, 256 - (addr & 0xFF));
  1191. ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
  1192. hsfc = REGREAD16(ICH9_REG_HSFC);
  1193. hsfc &= ~HSFC_FCYCLE; /* clear operation */
  1194. hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
  1195. hsfc &= ~HSFC_FDBC; /* clear byte count */
  1196. /* set byte count */
  1197. hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
  1198. hsfc |= HSFC_FGO; /* start */
  1199. REGWRITE16(ICH9_REG_HSFC, hsfc);
  1200. if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
  1201. return -1;
  1202. addr += block_len;
  1203. buf += block_len;
  1204. len -= block_len;
  1205. }
  1206. return 0;
  1207. }
  1208. static int ich_spi_send_multicommand(struct flashctx *flash,
  1209. struct spi_command *cmds)
  1210. {
  1211. int ret = 0;
  1212. int i;
  1213. int oppos, preoppos;
  1214. for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
  1215. if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
  1216. /* Next command is valid. */
  1217. preoppos = find_preop(curopcodes, cmds->writearr[0]);
  1218. oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
  1219. if ((oppos == -1) && (preoppos != -1)) {
  1220. /* Current command is listed as preopcode in
  1221. * ICH struct OPCODES, but next command is not
  1222. * listed as opcode in that struct.
  1223. * Check for command sanity, then
  1224. * try to reprogram the ICH opcode list.
  1225. */
  1226. if (find_preop(curopcodes,
  1227. (cmds + 1)->writearr[0]) != -1) {
  1228. msg_perr("%s: Two subsequent "
  1229. "preopcodes 0x%02x and 0x%02x, "
  1230. "ignoring the first.\n",
  1231. __func__, cmds->writearr[0],
  1232. (cmds + 1)->writearr[0]);
  1233. continue;
  1234. }
  1235. /* If the chipset is locked down, we'll fail
  1236. * during execution of the next command anyway.
  1237. * No need to bother with fixups.
  1238. */
  1239. if (!ichspi_lock) {
  1240. oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
  1241. if (oppos == -1)
  1242. continue;
  1243. curopcodes->opcode[oppos].atomic = preoppos + 1;
  1244. continue;
  1245. }
  1246. }
  1247. if ((oppos != -1) && (preoppos != -1)) {
  1248. /* Current command is listed as preopcode in
  1249. * ICH struct OPCODES and next command is listed
  1250. * as opcode in that struct. Match them up.
  1251. */
  1252. curopcodes->opcode[oppos].atomic = preoppos + 1;
  1253. continue;
  1254. }
  1255. /* If none of the above if-statements about oppos or
  1256. * preoppos matched, this is a normal opcode.
  1257. */
  1258. }
  1259. ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
  1260. cmds->writearr, cmds->readarr);
  1261. /* Reset the type of all opcodes to non-atomic. */
  1262. for (i = 0; i < 8; i++)
  1263. curopcodes->opcode[i].atomic = 0;
  1264. }
  1265. return ret;
  1266. }
  1267. #define ICH_BMWAG(x) ((x >> 24) & 0xff)
  1268. #define ICH_BMRAG(x) ((x >> 16) & 0xff)
  1269. #define ICH_BRWA(x) ((x >> 8) & 0xff)
  1270. #define ICH_BRRA(x) ((x >> 0) & 0xff)
  1271. /* returns 0 if region is unused or r/w */
  1272. static int ich9_handle_frap(uint32_t frap, int i)
  1273. {
  1274. static const char *const access_names[4] = {
  1275. "locked", "read-only", "write-only", "read-write"
  1276. };
  1277. static const char *const region_names[5] = {
  1278. "Flash Descriptor", "BIOS", "Management Engine",
  1279. "Gigabit Ethernet", "Platform Data"
  1280. };
  1281. uint32_t base, limit;
  1282. int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
  1283. (((ICH_BRRA(frap) >> i) & 1) << 0);
  1284. int offset = ICH9_REG_FREG0 + i * 4;
  1285. uint32_t freg = mmio_readl(ich_spibar + offset);
  1286. base = ICH_FREG_BASE(freg);
  1287. limit = ICH_FREG_LIMIT(freg);
  1288. if (base > limit || (freg == 0 && i > 0)) {
  1289. /* this FREG is disabled */
  1290. msg_pdbg2("0x%02X: 0x%08x FREG%i: %s region is unused.\n",
  1291. offset, freg, i, region_names[i]);
  1292. return 0;
  1293. }
  1294. msg_pdbg("0x%02X: 0x%08x ", offset, freg);
  1295. if (rwperms == 0x3) {
  1296. msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i,
  1297. region_names[i], base, (limit | 0x0fff),
  1298. access_names[rwperms]);
  1299. return 0;
  1300. }
  1301. msg_pwarn("FREG%i: Warning: %s region (0x%08x-0x%08x) is %s.\n", i,
  1302. region_names[i], base, (limit | 0x0fff),
  1303. access_names[rwperms]);
  1304. return 1;
  1305. }
  1306. /* In contrast to FRAP and the master section of the descriptor the bits
  1307. * in the PR registers have an inverted meaning. The bits in FRAP
  1308. * indicate read and write access _grant_. Here they indicate read
  1309. * and write _protection_ respectively. If both bits are 0 the address
  1310. * bits are ignored.
  1311. */
  1312. #define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
  1313. ((~((pr) >> PR_WP_OFF) & 1) << 1))
  1314. /* returns 0 if range is unused (i.e. r/w) */
  1315. static int ich9_handle_pr(int i)
  1316. {
  1317. static const char *const access_names[3] = {
  1318. "locked", "read-only", "write-only"
  1319. };
  1320. uint8_t off = ICH9_REG_PR0 + (i * 4);
  1321. uint32_t pr = mmio_readl(ich_spibar + off);
  1322. unsigned int rwperms = ICH_PR_PERMS(pr);
  1323. if (rwperms == 0x3) {
  1324. msg_pdbg2("0x%02X: 0x%08x (PR%u is unused)\n", off, pr, i);
  1325. return 0;
  1326. }
  1327. msg_pdbg("0x%02X: 0x%08x ", off, pr);
  1328. msg_pwarn("PR%u: Warning: 0x%08x-0x%08x is %s.\n", i, ICH_FREG_BASE(pr),
  1329. ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
  1330. return 1;
  1331. }
  1332. /* Set/Clear the read and write protection enable bits of PR register @i
  1333. * according to @read_prot and @write_prot. */
  1334. static void ich9_set_pr(int i, int read_prot, int write_prot)
  1335. {
  1336. void *addr = ich_spibar + ICH9_REG_PR0 + (i * 4);
  1337. uint32_t old = mmio_readl(addr);
  1338. uint32_t new;
  1339. msg_gspew("PR%u is 0x%08x", i, old);
  1340. new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
  1341. if (read_prot)
  1342. new |= (1 << PR_RP_OFF);
  1343. if (write_prot)
  1344. new |= (1 << PR_WP_OFF);
  1345. if (old == new) {
  1346. msg_gspew(" already.\n");
  1347. return;
  1348. }
  1349. msg_gspew(", trying to set it to 0x%08x ", new);
  1350. rmmio_writel(new, addr);
  1351. msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
  1352. }
  1353. static const struct spi_master spi_master_ich7 = {
  1354. .type = SPI_CONTROLLER_ICH7,
  1355. .max_data_read = 64,
  1356. .max_data_write = 64,
  1357. .command = ich_spi_send_command,
  1358. .multicommand = ich_spi_send_multicommand,
  1359. .read = default_spi_read,
  1360. .write_256 = default_spi_write_256,
  1361. .write_aai = default_spi_write_aai,
  1362. };
  1363. static const struct spi_master spi_master_ich9 = {
  1364. .type = SPI_CONTROLLER_ICH9,
  1365. .max_data_read = 64,
  1366. .max_data_write = 64,
  1367. .command = ich_spi_send_command,
  1368. .multicommand = ich_spi_send_multicommand,
  1369. .read = default_spi_read,
  1370. .write_256 = default_spi_write_256,
  1371. .write_aai = default_spi_write_aai,
  1372. };
  1373. static const struct opaque_master opaque_master_ich_hwseq = {
  1374. .max_data_read = 64,
  1375. .max_data_write = 64,
  1376. .probe = ich_hwseq_probe,
  1377. .read = ich_hwseq_read,
  1378. .write = ich_hwseq_write,
  1379. .erase = ich_hwseq_block_erase,
  1380. };
  1381. int ich_init_spi(struct pci_dev *dev, void *spibar, enum ich_chipset ich_gen)
  1382. {
  1383. int i;
  1384. uint16_t tmp2;
  1385. uint32_t tmp;
  1386. char *arg;
  1387. int ich_spi_force = 0;
  1388. int ich_spi_rw_restricted = 0;
  1389. int desc_valid = 0;
  1390. struct ich_descriptors desc = {{ 0 }};
  1391. enum ich_spi_mode {
  1392. ich_auto,
  1393. ich_hwseq,
  1394. ich_swseq
  1395. } ich_spi_mode = ich_auto;
  1396. ich_generation = ich_gen;
  1397. ich_spibar = spibar;
  1398. switch (ich_generation) {
  1399. case CHIPSET_ICH7:
  1400. case CHIPSET_TUNNEL_CREEK:
  1401. case CHIPSET_CENTERTON:
  1402. msg_pdbg("0x00: 0x%04x (SPIS)\n",
  1403. mmio_readw(ich_spibar + 0));
  1404. msg_pdbg("0x02: 0x%04x (SPIC)\n",
  1405. mmio_readw(ich_spibar + 2));
  1406. msg_pdbg("0x04: 0x%08x (SPIA)\n",
  1407. mmio_readl(ich_spibar + 4));
  1408. ichspi_bbar = mmio_readl(ich_spibar + 0x50);
  1409. msg_pdbg("0x50: 0x%08x (BBAR)\n",
  1410. ichspi_bbar);
  1411. msg_pdbg("0x54: 0x%04x (PREOP)\n",
  1412. mmio_readw(ich_spibar + 0x54));
  1413. msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
  1414. mmio_readw(ich_spibar + 0x56));
  1415. msg_pdbg("0x58: 0x%08x (OPMENU)\n",
  1416. mmio_readl(ich_spibar + 0x58));
  1417. msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
  1418. mmio_readl(ich_spibar + 0x5c));
  1419. for (i = 0; i < 3; i++) {
  1420. int offs;
  1421. offs = 0x60 + (i * 4);
  1422. msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
  1423. mmio_readl(ich_spibar + offs), i);
  1424. }
  1425. if (mmio_readw(ich_spibar) & (1 << 15)) {
  1426. msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
  1427. ichspi_lock = 1;
  1428. }
  1429. ich_init_opcodes();
  1430. ich_set_bbar(0);
  1431. register_spi_master(&spi_master_ich7);
  1432. break;
  1433. case CHIPSET_ICH8:
  1434. default: /* Future version might behave the same */
  1435. arg = extract_programmer_param("ich_spi_mode");
  1436. if (arg && !strcmp(arg, "hwseq")) {
  1437. ich_spi_mode = ich_hwseq;
  1438. msg_pspew("user selected hwseq\n");
  1439. } else if (arg && !strcmp(arg, "swseq")) {
  1440. ich_spi_mode = ich_swseq;
  1441. msg_pspew("user selected swseq\n");
  1442. } else if (arg && !strcmp(arg, "auto")) {
  1443. msg_pspew("user selected auto\n");
  1444. ich_spi_mode = ich_auto;
  1445. } else if (arg && !strlen(arg)) {
  1446. msg_perr("Missing argument for ich_spi_mode.\n");
  1447. free(arg);
  1448. return ERROR_FATAL;
  1449. } else if (arg) {
  1450. msg_perr("Unknown argument for ich_spi_mode: %s\n",
  1451. arg);
  1452. free(arg);
  1453. return ERROR_FATAL;
  1454. }
  1455. free(arg);
  1456. arg = extract_programmer_param("ich_spi_force");
  1457. if (arg && !strcmp(arg, "yes")) {
  1458. ich_spi_force = 1;
  1459. msg_pspew("ich_spi_force enabled.\n");
  1460. } else if (arg && !strlen(arg)) {
  1461. msg_perr("Missing argument for ich_spi_force.\n");
  1462. free(arg);
  1463. return ERROR_FATAL;
  1464. } else if (arg) {
  1465. msg_perr("Unknown argument for ich_spi_force: \"%s\" "
  1466. "(not \"yes\").\n", arg);
  1467. free(arg);
  1468. return ERROR_FATAL;
  1469. }
  1470. free(arg);
  1471. tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
  1472. msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
  1473. prettyprint_ich9_reg_hsfs(tmp2);
  1474. if (tmp2 & HSFS_FLOCKDN) {
  1475. msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
  1476. ichspi_lock = 1;
  1477. }
  1478. if (tmp2 & HSFS_FDV)
  1479. desc_valid = 1;
  1480. if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
  1481. msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
  1482. "the Master Section of the flash descriptor are NOT in effect. Please note\n"
  1483. "that Protected Range (PR) restrictions still apply.\n");
  1484. ich_init_opcodes();
  1485. if (desc_valid) {
  1486. tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
  1487. msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
  1488. prettyprint_ich9_reg_hsfc(tmp2);
  1489. }
  1490. tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
  1491. msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp);
  1492. if (desc_valid) {
  1493. tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
  1494. msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
  1495. msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
  1496. msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
  1497. msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
  1498. msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
  1499. /* Handle FREGx and FRAP registers */
  1500. for (i = 0; i < 5; i++)
  1501. ich_spi_rw_restricted |= ich9_handle_frap(tmp, i);
  1502. if (ich_spi_rw_restricted)
  1503. msg_pwarn("Not all flash regions are freely accessible by flashrom. This is "
  1504. "most likely\ndue to an active ME. Please see "
  1505. "https://flashrom.org/ME for details.\n");
  1506. }
  1507. /* Handle PR registers */
  1508. for (i = 0; i < 5; i++) {
  1509. /* if not locked down try to disable PR locks first */
  1510. if (!ichspi_lock)
  1511. ich9_set_pr(i, 0, 0);
  1512. ich_spi_rw_restricted |= ich9_handle_pr(i);
  1513. }
  1514. if (ich_spi_rw_restricted) {
  1515. if (!ich_spi_force)
  1516. programmer_may_write = 0;
  1517. msg_pinfo("Writes have been disabled for safety reasons. You can enforce write\n"
  1518. "support with the ich_spi_force programmer option, but you will most likely\n"
  1519. "harm your hardware! If you force flashrom you will get no support if\n"
  1520. "something breaks. On a few mainboards it is possible to enable write\n"
  1521. "access by setting a jumper (see its documentation or the board itself).\n");
  1522. if (ich_spi_force)
  1523. msg_pinfo("Continuing with write support because the user forced us to!\n");
  1524. }
  1525. tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
  1526. msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
  1527. prettyprint_ich9_reg_ssfs(tmp);
  1528. if (tmp & SSFS_FCERR) {
  1529. msg_pdbg("Clearing SSFS.FCERR\n");
  1530. mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
  1531. }
  1532. msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
  1533. prettyprint_ich9_reg_ssfc(tmp);
  1534. msg_pdbg("0x94: 0x%04x (PREOP)\n",
  1535. mmio_readw(ich_spibar + ICH9_REG_PREOP));
  1536. msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
  1537. mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
  1538. msg_pdbg("0x98: 0x%08x (OPMENU)\n",
  1539. mmio_readl(ich_spibar + ICH9_REG_OPMENU));
  1540. msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
  1541. mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
  1542. if (ich_generation == CHIPSET_ICH8 && desc_valid) {
  1543. tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
  1544. msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
  1545. msg_pdbg("VSCC: ");
  1546. prettyprint_ich_reg_vscc(tmp, MSG_DEBUG, true);
  1547. } else {
  1548. if (ich_generation != CHIPSET_BAYTRAIL && desc_valid) {
  1549. ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
  1550. msg_pdbg("0xA0: 0x%08x (BBAR)\n",
  1551. ichspi_bbar);
  1552. ich_set_bbar(0);
  1553. }
  1554. if (desc_valid) {
  1555. tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
  1556. msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
  1557. msg_pdbg("LVSCC: ");
  1558. prettyprint_ich_reg_vscc(tmp, MSG_DEBUG, true);
  1559. tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
  1560. msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
  1561. msg_pdbg("UVSCC: ");
  1562. prettyprint_ich_reg_vscc(tmp, MSG_DEBUG, false);
  1563. tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
  1564. msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
  1565. }
  1566. }
  1567. if (desc_valid) {
  1568. if (read_ich_descriptors_via_fdo(ich_spibar, &desc) == ICH_RET_OK)
  1569. prettyprint_ich_descriptors(ich_gen, &desc);
  1570. /* If the descriptor is valid and indicates multiple
  1571. * flash devices we need to use hwseq to be able to
  1572. * access the second flash device.
  1573. */
  1574. if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
  1575. msg_pinfo("Enabling hardware sequencing due to "
  1576. "multiple flash chips detected.\n");
  1577. ich_spi_mode = ich_hwseq;
  1578. }
  1579. }
  1580. if (ich_spi_mode == ich_auto && ichspi_lock &&
  1581. ich_missing_opcodes()) {
  1582. msg_pinfo("Enabling hardware sequencing because "
  1583. "some important opcode is locked.\n");
  1584. ich_spi_mode = ich_hwseq;
  1585. }
  1586. if (ich_spi_mode == ich_hwseq) {
  1587. if (!desc_valid) {
  1588. msg_perr("Hardware sequencing was requested "
  1589. "but the flash descriptor is not "
  1590. "valid. Aborting.\n");
  1591. return ERROR_FATAL;
  1592. }
  1593. int tmpi = getFCBA_component_density(ich_generation, &desc, 0);
  1594. if (tmpi < 0) {
  1595. msg_perr("Could not determine density of flash component %d.\n", 0);
  1596. return ERROR_FATAL;
  1597. }
  1598. hwseq_data.size_comp0 = tmpi;
  1599. tmpi = getFCBA_component_density(ich_generation, &desc, 1);
  1600. if (tmpi < 0) {
  1601. msg_perr("Could not determine density of flash component %d.\n", 1);
  1602. return ERROR_FATAL;
  1603. }
  1604. hwseq_data.size_comp1 = tmpi;
  1605. register_opaque_master(&opaque_master_ich_hwseq);
  1606. } else {
  1607. register_spi_master(&spi_master_ich9);
  1608. }
  1609. break;
  1610. }
  1611. return 0;
  1612. }
  1613. static const struct spi_master spi_master_via = {
  1614. .type = SPI_CONTROLLER_VIA,
  1615. .max_data_read = 16,
  1616. .max_data_write = 16,
  1617. .command = ich_spi_send_command,
  1618. .multicommand = ich_spi_send_multicommand,
  1619. .read = default_spi_read,
  1620. .write_256 = default_spi_write_256,
  1621. .write_aai = default_spi_write_aai,
  1622. };
  1623. int via_init_spi(struct pci_dev *dev, uint32_t mmio_base)
  1624. {
  1625. int i;
  1626. ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
  1627. if (ich_spibar == ERROR_PTR)
  1628. return ERROR_FATAL;
  1629. /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
  1630. /* Not sure if it speaks all these bus protocols. */
  1631. internal_buses_supported = BUS_LPC | BUS_FWH;
  1632. ich_generation = CHIPSET_ICH7;
  1633. register_spi_master(&spi_master_via);
  1634. msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
  1635. msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
  1636. msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
  1637. for (i = 0; i < 2; i++) {
  1638. int offs;
  1639. offs = 8 + (i * 8);
  1640. msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
  1641. mmio_readl(ich_spibar + offs), i);
  1642. msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
  1643. mmio_readl(ich_spibar + offs + 4), i);
  1644. }
  1645. ichspi_bbar = mmio_readl(ich_spibar + 0x50);
  1646. msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
  1647. msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
  1648. msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
  1649. msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
  1650. msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
  1651. for (i = 0; i < 3; i++) {
  1652. int offs;
  1653. offs = 0x60 + (i * 4);
  1654. msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
  1655. mmio_readl(ich_spibar + offs), i);
  1656. }
  1657. msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
  1658. mmio_readw(ich_spibar + 0x6c));
  1659. if (mmio_readw(ich_spibar) & (1 << 15)) {
  1660. msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
  1661. ichspi_lock = 1;
  1662. }
  1663. ich_set_bbar(0);
  1664. ich_init_opcodes();
  1665. return 0;
  1666. }
  1667. #endif