ichspi.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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 "spi.h"
  31. #include "ich_descriptors.h"
  32. #include "chipdrivers.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. /*SUNRISE point*/
  153. /* 32 Bits Hardware Sequencing Flash Status */
  154. #define PCH100_REG_HSFSC 0x04
  155. /*Status bits*/
  156. #define HSFSC_FDONE_OFF 0 /* 0: Flash Cycle Done */
  157. #define HSFSC_FDONE (0x1 << HSFSC_FDONE_OFF)
  158. #define HSFSC_FCERR_OFF 1 /* 1: Flash Cycle Error */
  159. #define HSFSC_FCERR (0x1 << HSFSC_FCERR_OFF)
  160. #define HSFSC_AEL_OFF 2 /* 2: Access Error Log */
  161. #define HSFSC_AEL (0x1 << HSFSC_AEL_OFF)
  162. #define HSFSC_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
  163. #define HSFSC_SCIP (0x1 << HSFSC_SCIP_OFF)
  164. /* 6-10: reserved */
  165. /* 11: Flash Configuration Lock-Down WRSDIS */
  166. #define HSFSC_WRSDIS_OFF 11
  167. #define HSFSC_WRSDIS (0x1 << HSFSC_WRSDIS_OFF)
  168. #define HSFSC_PRR34LCKDN_OFF 12
  169. #define HSFSC_PRR34LCKDN (0x1 << HSFSC_PRR34LCKDN_OFF)
  170. /* 13: Flash Descriptor Override Pin-Strap Status */
  171. #define HSFSC_FDOPSS_OFF 13
  172. #define HSFSC_FDOPSS (0x1 << HSFSC_FDOPSS_OFF)
  173. #define HSFSC_FDV_OFF 14 /* 14: Flash Descriptor Valid */
  174. #define HSFSC_FDV (0x1 << HSFSC_FDV_OFF)
  175. #define HSFSC_FLOCKDN_OFF 15 /* 11: Flash Configuration Lock-Down */
  176. #define HSFSC_FLOCKDN (0x1 << HSFSC_FLOCKDN_OFF)
  177. /*Control bits*/
  178. #define HSFSC_FGO_OFF 0 /* 0: Flash Cycle Go */
  179. #define HSFSC_FGO (0x1 << HSFSC_FGO_OFF)
  180. #define HSFSC_FCYCLE_OFF 1 /* 1-3: FLASH Cycle */
  181. #define HSFSC_FCYCLE (0xf << HSFSC_FCYCLE_OFF)
  182. #define HSFSC_FDBC_OFF 8 /*8-13 : Flash Data Byte Count */
  183. #define HSFSC_FDBC (0x3f << HSFSC_FDBC_OFF)
  184. #define PCH100_REG_FADDR 0x08 /* 32 Bits */
  185. #define PCH100_REG_FDATA0 0x10 /* 64 Bytes */
  186. #define PCH100_REG_FPR0 0x84 /* 32 Bytes Protected Range 0 */
  187. #define PCH100_WP_OFF 31 /* 31: write protection enable */
  188. #define PCH100_RP_OFF 15 /* 15: read protection enable */
  189. /* The minimum erase block size in PCH which is 4k
  190. * 256,
  191. * 4 * 1024,
  192. * 8 * 1024,
  193. * 64 * 1024
  194. */
  195. #define ERASE_BLOCK_SIZE 1
  196. #define HWSEQ_READ 0
  197. #define HWSEQ_WRITE 1
  198. /* ICH SPI configuration lock-down. May be set during chipset enabling. */
  199. static int ichspi_lock = 0;
  200. enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
  201. uint32_t ichspi_bbar = 0;
  202. static void *ich_spibar = NULL;
  203. typedef struct _OPCODE {
  204. uint8_t opcode; //This commands spi opcode
  205. uint8_t spi_type; //This commands spi type
  206. uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
  207. } OPCODE;
  208. /* Suggested opcode definition:
  209. * Preop 1: Write Enable
  210. * Preop 2: Write Status register enable
  211. *
  212. * OP 0: Write address
  213. * OP 1: Read Address
  214. * OP 2: ERASE block
  215. * OP 3: Read Status register
  216. * OP 4: Read ID
  217. * OP 5: Write Status register
  218. * OP 6: chip private (read JEDEC id)
  219. * OP 7: Chip erase
  220. */
  221. typedef struct _OPCODES {
  222. uint8_t preop[2];
  223. OPCODE opcode[8];
  224. } OPCODES;
  225. static OPCODES *curopcodes = NULL;
  226. /* HW access functions */
  227. static uint32_t REGREAD32(int X)
  228. {
  229. return mmio_readl(ich_spibar + X);
  230. }
  231. static uint16_t REGREAD16(int X)
  232. {
  233. return mmio_readw(ich_spibar + X);
  234. }
  235. static uint16_t REGREAD8(int X)
  236. {
  237. return mmio_readb(ich_spibar + X);
  238. }
  239. #define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
  240. #define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
  241. #define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
  242. /* Common SPI functions */
  243. static int find_opcode(OPCODES *op, uint8_t opcode);
  244. static int find_preop(OPCODES *op, uint8_t preop);
  245. static int generate_opcodes(OPCODES * op);
  246. static int program_opcodes(OPCODES *op, int enable_undo);
  247. static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
  248. uint8_t datalength, uint8_t * data);
  249. /* for pairing opcodes with their required preop */
  250. struct preop_opcode_pair {
  251. uint8_t preop;
  252. uint8_t opcode;
  253. };
  254. /* List of opcodes which need preopcodes and matching preopcodes. Unused. */
  255. const struct preop_opcode_pair pops[] = {
  256. {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
  257. {JEDEC_WREN, JEDEC_SE}, /* sector erase */
  258. {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
  259. {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
  260. {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
  261. {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
  262. /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
  263. {JEDEC_WREN, JEDEC_WRSR},
  264. {JEDEC_EWSR, JEDEC_WRSR},
  265. {0,}
  266. };
  267. /* Reasonable default configuration. Needs ad-hoc modifications if we
  268. * encounter unlisted opcodes. Fun.
  269. */
  270. static OPCODES O_ST_M25P = {
  271. {
  272. JEDEC_WREN,
  273. JEDEC_EWSR,
  274. },
  275. {
  276. {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
  277. {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
  278. {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
  279. {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
  280. {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
  281. {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
  282. {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
  283. {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
  284. }
  285. };
  286. /* List of opcodes with their corresponding spi_type
  287. * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
  288. * is needed which is currently not in the chipset OPCODE table
  289. */
  290. static OPCODE POSSIBLE_OPCODES[] = {
  291. {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
  292. {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
  293. {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
  294. {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
  295. {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
  296. {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
  297. {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
  298. {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
  299. {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
  300. {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
  301. {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
  302. };
  303. static OPCODES O_EXISTING = {};
  304. /* pretty printing functions */
  305. static void prettyprint_opcodes(OPCODES *ops)
  306. {
  307. OPCODE oc;
  308. const char *t;
  309. const char *a;
  310. uint8_t i;
  311. static const char *const spi_type[4] = {
  312. "read w/o addr",
  313. "write w/o addr",
  314. "read w/ addr",
  315. "write w/ addr"
  316. };
  317. static const char *const atomic_type[3] = {
  318. "none",
  319. " 0 ",
  320. " 1 "
  321. };
  322. if (ops == NULL)
  323. return;
  324. msg_pdbg2(" OP Type Pre-OP\n");
  325. for (i = 0; i < 8; i++) {
  326. oc = ops->opcode[i];
  327. t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
  328. a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
  329. msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
  330. }
  331. msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
  332. ops->preop[1]);
  333. }
  334. #define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
  335. static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
  336. {
  337. msg_pdbg("HSFS: ");
  338. pprint_reg(HSFS, FDONE, reg_val, ", ");
  339. pprint_reg(HSFS, FCERR, reg_val, ", ");
  340. pprint_reg(HSFS, AEL, reg_val, ", ");
  341. pprint_reg(HSFS, BERASE, reg_val, ", ");
  342. pprint_reg(HSFS, SCIP, reg_val, ", ");
  343. pprint_reg(HSFS, FDOPSS, reg_val, ", ");
  344. pprint_reg(HSFS, FDV, reg_val, ", ");
  345. pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
  346. }
  347. static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
  348. {
  349. msg_pdbg("HSFC: ");
  350. pprint_reg(HSFC, FGO, reg_val, ", ");
  351. pprint_reg(HSFC, FCYCLE, reg_val, ", ");
  352. pprint_reg(HSFC, FDBC, reg_val, ", ");
  353. pprint_reg(HSFC, SME, reg_val, "\n");
  354. }
  355. static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
  356. {
  357. msg_pdbg("SSFS: ");
  358. pprint_reg(SSFS, SCIP, reg_val, ", ");
  359. pprint_reg(SSFS, FDONE, reg_val, ", ");
  360. pprint_reg(SSFS, FCERR, reg_val, ", ");
  361. pprint_reg(SSFS, AEL, reg_val, "\n");
  362. }
  363. static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
  364. {
  365. msg_pdbg("SSFC: ");
  366. pprint_reg(SSFC, SCGO, reg_val, ", ");
  367. pprint_reg(SSFC, ACS, reg_val, ", ");
  368. pprint_reg(SSFC, SPOP, reg_val, ", ");
  369. pprint_reg(SSFC, COP, reg_val, ", ");
  370. pprint_reg(SSFC, DBC, reg_val, ", ");
  371. pprint_reg(SSFC, SME, reg_val, ", ");
  372. pprint_reg(SSFC, SCF, reg_val, "\n");
  373. }
  374. static uint8_t lookup_spi_type(uint8_t opcode)
  375. {
  376. int a;
  377. for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
  378. if (POSSIBLE_OPCODES[a].opcode == opcode)
  379. return POSSIBLE_OPCODES[a].spi_type;
  380. }
  381. return 0xFF;
  382. }
  383. static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
  384. {
  385. uint8_t spi_type;
  386. spi_type = lookup_spi_type(opcode);
  387. if (spi_type > 3) {
  388. /* Try to guess spi type from read/write sizes.
  389. * The following valid writecnt/readcnt combinations exist:
  390. * writecnt = 4, readcnt >= 0
  391. * writecnt = 1, readcnt >= 0
  392. * writecnt >= 4, readcnt = 0
  393. * writecnt >= 1, readcnt = 0
  394. * writecnt >= 1 is guaranteed for all commands.
  395. */
  396. if (readcnt == 0)
  397. /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
  398. * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
  399. * bytes are actual the address, they go to the bus anyhow
  400. */
  401. spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
  402. else if (writecnt == 1) // and readcnt is > 0
  403. spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
  404. else if (writecnt == 4) // and readcnt is > 0
  405. spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
  406. // else we have an invalid case, will be handled below
  407. }
  408. if (spi_type <= 3) {
  409. int oppos=2; // use original JEDEC_BE_D8 offset
  410. curopcodes->opcode[oppos].opcode = opcode;
  411. curopcodes->opcode[oppos].spi_type = spi_type;
  412. program_opcodes(curopcodes, 0);
  413. oppos = find_opcode(curopcodes, opcode);
  414. msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
  415. return oppos;
  416. }
  417. return -1;
  418. }
  419. static int find_opcode(OPCODES *op, uint8_t opcode)
  420. {
  421. int a;
  422. if (op == NULL) {
  423. msg_perr("\n%s: null OPCODES pointer!\n", __func__);
  424. return -1;
  425. }
  426. for (a = 0; a < 8; a++) {
  427. if (op->opcode[a].opcode == opcode)
  428. return a;
  429. }
  430. return -1;
  431. }
  432. static int find_preop(OPCODES *op, uint8_t preop)
  433. {
  434. int a;
  435. if (op == NULL) {
  436. msg_perr("\n%s: null OPCODES pointer!\n", __func__);
  437. return -1;
  438. }
  439. for (a = 0; a < 2; a++) {
  440. if (op->preop[a] == preop)
  441. return a;
  442. }
  443. return -1;
  444. }
  445. /* Create a struct OPCODES based on what we find in the locked down chipset. */
  446. static int generate_opcodes(OPCODES * op)
  447. {
  448. int a;
  449. uint16_t preop, optype;
  450. uint32_t opmenu[2];
  451. if (op == NULL) {
  452. msg_perr("\n%s: null OPCODES pointer!\n", __func__);
  453. return -1;
  454. }
  455. switch (ich_generation) {
  456. case CHIPSET_ICH7:
  457. preop = REGREAD16(ICH7_REG_PREOP);
  458. optype = REGREAD16(ICH7_REG_OPTYPE);
  459. opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
  460. opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
  461. break;
  462. case CHIPSET_ICH8:
  463. default: /* Future version might behave the same */
  464. preop = REGREAD16(ICH9_REG_PREOP);
  465. optype = REGREAD16(ICH9_REG_OPTYPE);
  466. opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
  467. opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
  468. break;
  469. }
  470. op->preop[0] = (uint8_t) preop;
  471. op->preop[1] = (uint8_t) (preop >> 8);
  472. for (a = 0; a < 8; a++) {
  473. op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
  474. optype >>= 2;
  475. }
  476. for (a = 0; a < 4; a++) {
  477. op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
  478. opmenu[0] >>= 8;
  479. }
  480. for (a = 4; a < 8; a++) {
  481. op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
  482. opmenu[1] >>= 8;
  483. }
  484. /* No preopcodes used by default. */
  485. for (a = 0; a < 8; a++)
  486. op->opcode[a].atomic = 0;
  487. return 0;
  488. }
  489. static int program_opcodes(OPCODES *op, int enable_undo)
  490. {
  491. uint8_t a;
  492. uint16_t preop, optype;
  493. uint32_t opmenu[2];
  494. /* Program Prefix Opcodes */
  495. /* 0:7 Prefix Opcode 1 */
  496. preop = (op->preop[0]);
  497. /* 8:16 Prefix Opcode 2 */
  498. preop |= ((uint16_t) op->preop[1]) << 8;
  499. /* Program Opcode Types 0 - 7 */
  500. optype = 0;
  501. for (a = 0; a < 8; a++) {
  502. optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
  503. }
  504. /* Program Allowable Opcodes 0 - 3 */
  505. opmenu[0] = 0;
  506. for (a = 0; a < 4; a++) {
  507. opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
  508. }
  509. /*Program Allowable Opcodes 4 - 7 */
  510. opmenu[1] = 0;
  511. for (a = 4; a < 8; a++) {
  512. opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
  513. }
  514. msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
  515. switch (ich_generation) {
  516. case CHIPSET_ICH7:
  517. /* Register undo only for enable_undo=1, i.e. first call. */
  518. if (enable_undo) {
  519. rmmio_valw(ich_spibar + ICH7_REG_PREOP);
  520. rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
  521. rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
  522. rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
  523. }
  524. mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
  525. mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
  526. mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
  527. mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
  528. break;
  529. case CHIPSET_ICH8:
  530. default: /* Future version might behave the same */
  531. /* Register undo only for enable_undo=1, i.e. first call. */
  532. if (enable_undo) {
  533. rmmio_valw(ich_spibar + ICH9_REG_PREOP);
  534. rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
  535. rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
  536. rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
  537. }
  538. mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
  539. mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
  540. mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
  541. mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
  542. break;
  543. }
  544. return 0;
  545. }
  546. /*
  547. * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
  548. * FIXME: this should also check for
  549. * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
  550. * - at least one erasing opcode (lots.)
  551. * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
  552. * - necessary preops? (EWSR, WREN, ...?)
  553. */
  554. static int ich_missing_opcodes()
  555. {
  556. uint8_t ops[] = {
  557. JEDEC_READ,
  558. JEDEC_RDSR,
  559. 0
  560. };
  561. int i = 0;
  562. while (ops[i] != 0) {
  563. msg_pspew("checking for opcode 0x%02x\n", ops[i]);
  564. if (find_opcode(curopcodes, ops[i]) == -1)
  565. return -1;
  566. i++;
  567. }
  568. return 0;
  569. }
  570. /*
  571. * Try to set BBAR (BIOS Base Address Register), but read back the value in case
  572. * it didn't stick.
  573. */
  574. static void ich_set_bbar(uint32_t min_addr)
  575. {
  576. int bbar_off;
  577. switch (ich_generation) {
  578. case CHIPSET_ICH7:
  579. bbar_off = 0x50;
  580. break;
  581. case CHIPSET_ICH8:
  582. msg_perr("BBAR offset is unknown on ICH8!\n");
  583. return;
  584. case CHIPSET_ICH9:
  585. default: /* Future version might behave the same */
  586. bbar_off = ICH9_REG_BBAR;
  587. break;
  588. }
  589. ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
  590. if (ichspi_bbar) {
  591. msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
  592. ichspi_bbar);
  593. }
  594. min_addr &= BBAR_MASK;
  595. ichspi_bbar |= min_addr;
  596. rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
  597. ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
  598. /* We don't have any option except complaining. And if the write
  599. * failed, the restore will fail as well, so no problem there.
  600. */
  601. if (ichspi_bbar != min_addr)
  602. msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
  603. min_addr, ichspi_bbar);
  604. }
  605. /* Read len bytes from the fdata/spid register into the data array.
  606. *
  607. * Note that using len > spi_programmer->max_data_read will return garbage or
  608. * may even crash.
  609. */
  610. static void ich_read_data(uint8_t *data, int len, int reg0_off)
  611. {
  612. int i;
  613. uint32_t temp32 = 0;
  614. for (i = 0; i < len; i++) {
  615. if ((i % 4) == 0)
  616. temp32 = REGREAD32(reg0_off + i);
  617. data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
  618. }
  619. }
  620. /* Fill len bytes from the data array into the fdata/spid registers.
  621. *
  622. * Note that using len > spi_programmer->max_data_write will trash the registers
  623. * following the data registers.
  624. */
  625. static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
  626. {
  627. uint32_t temp32 = 0;
  628. int i;
  629. if (len <= 0)
  630. return;
  631. for (i = 0; i < len; i++) {
  632. if ((i % 4) == 0)
  633. temp32 = 0;
  634. temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
  635. if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
  636. REGWRITE32(reg0_off + (i - (i % 4)), temp32);
  637. }
  638. i--;
  639. if ((i % 4) != 3) /* Write remaining data to regs. */
  640. REGWRITE32(reg0_off + (i - (i % 4)), temp32);
  641. }
  642. /* This function generates OPCODES from or programs OPCODES to ICH according to
  643. * the chipset's SPI configuration lock.
  644. *
  645. * It should be called before ICH sends any spi command.
  646. */
  647. static int ich_init_opcodes(void)
  648. {
  649. int rc = 0;
  650. OPCODES *curopcodes_done;
  651. if (curopcodes)
  652. return 0;
  653. if (ichspi_lock) {
  654. msg_pdbg("Reading OPCODES... ");
  655. curopcodes_done = &O_EXISTING;
  656. rc = generate_opcodes(curopcodes_done);
  657. } else {
  658. msg_pdbg("Programming OPCODES... ");
  659. curopcodes_done = &O_ST_M25P;
  660. rc = program_opcodes(curopcodes_done, 1);
  661. }
  662. if (rc) {
  663. curopcodes = NULL;
  664. msg_perr("failed\n");
  665. return 1;
  666. } else {
  667. curopcodes = curopcodes_done;
  668. msg_pdbg("done\n");
  669. prettyprint_opcodes(curopcodes);
  670. return 0;
  671. }
  672. }
  673. static int ich7_run_opcode(OPCODE op, uint32_t offset,
  674. uint8_t datalength, uint8_t * data, int maxdata)
  675. {
  676. int write_cmd = 0;
  677. int timeout;
  678. uint32_t temp32;
  679. uint16_t temp16;
  680. uint64_t opmenu;
  681. int opcode_index;
  682. /* Is it a write command? */
  683. if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
  684. || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
  685. write_cmd = 1;
  686. }
  687. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  688. while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
  689. programmer_delay(10);
  690. }
  691. if (!timeout) {
  692. msg_perr("Error: SCIP never cleared!\n");
  693. return 1;
  694. }
  695. /* Program offset in flash into SPIA while preserving reserved bits. */
  696. temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
  697. REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
  698. /* Program data into SPID0 to N */
  699. if (write_cmd && (datalength != 0))
  700. ich_fill_data(data, datalength, ICH7_REG_SPID0);
  701. /* Assemble SPIS */
  702. temp16 = REGREAD16(ICH7_REG_SPIS);
  703. /* keep reserved bits */
  704. temp16 &= SPIS_RESERVED_MASK;
  705. /* clear error status registers */
  706. temp16 |= (SPIS_CDS | SPIS_FCERR);
  707. REGWRITE16(ICH7_REG_SPIS, temp16);
  708. /* Assemble SPIC */
  709. temp16 = 0;
  710. if (datalength != 0) {
  711. temp16 |= SPIC_DS;
  712. temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
  713. }
  714. /* Select opcode */
  715. opmenu = REGREAD32(ICH7_REG_OPMENU);
  716. opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
  717. for (opcode_index = 0; opcode_index < 8; opcode_index++) {
  718. if ((opmenu & 0xff) == op.opcode) {
  719. break;
  720. }
  721. opmenu >>= 8;
  722. }
  723. if (opcode_index == 8) {
  724. msg_pdbg("Opcode %x not found.\n", op.opcode);
  725. return 1;
  726. }
  727. temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
  728. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  729. /* Handle Atomic. Atomic commands include three steps:
  730. - sending the preop (mainly EWSR or WREN)
  731. - sending the main command
  732. - waiting for the busy bit (WIP) to be cleared
  733. This means the timeout must be sufficient for chip erase
  734. of slow high-capacity chips.
  735. */
  736. switch (op.atomic) {
  737. case 2:
  738. /* Select second preop. */
  739. temp16 |= SPIC_SPOP;
  740. /* And fall through. */
  741. case 1:
  742. /* Atomic command (preop+op) */
  743. temp16 |= SPIC_ACS;
  744. timeout = 100 * 1000 * 60; /* 60 seconds */
  745. break;
  746. }
  747. /* Start */
  748. temp16 |= SPIC_SCGO;
  749. /* write it */
  750. REGWRITE16(ICH7_REG_SPIC, temp16);
  751. /* Original timeout is 60 minutes, which is too excessive.
  752. * Reduce to 30 secs for chip full erase (around 10 secs).
  753. * We also exit the loop if the error bit is set.
  754. */
  755. timeout = 100 * 1000 * 30;
  756. while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
  757. --timeout) {
  758. programmer_delay(10);
  759. if (REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR)
  760. break; /* Transaction error */
  761. }
  762. if (!timeout) {
  763. msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
  764. REGREAD16(ICH7_REG_SPIS));
  765. return 1;
  766. }
  767. /* FIXME: make sure we do not needlessly cause transaction errors. */
  768. temp16 = REGREAD16(ICH7_REG_SPIS);
  769. if (temp16 & SPIS_FCERR) {
  770. msg_perr("Transaction error!\n");
  771. /* keep reserved bits */
  772. temp16 &= SPIS_RESERVED_MASK;
  773. REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
  774. return 1;
  775. }
  776. if ((!write_cmd) && (datalength != 0))
  777. ich_read_data(data, datalength, ICH7_REG_SPID0);
  778. return 0;
  779. }
  780. static int ich9_run_opcode(OPCODE op, uint32_t offset,
  781. uint8_t datalength, uint8_t * data)
  782. {
  783. int write_cmd = 0;
  784. int timeout;
  785. uint32_t temp32;
  786. uint64_t opmenu;
  787. int opcode_index;
  788. /* Is it a write command? */
  789. if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
  790. || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
  791. write_cmd = 1;
  792. }
  793. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  794. while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
  795. programmer_delay(10);
  796. }
  797. if (!timeout) {
  798. msg_perr("Error: SCIP never cleared!\n");
  799. return 1;
  800. }
  801. /* Program offset in flash into FADDR while preserve the reserved bits
  802. * and clearing the 25. address bit which is only useable in hwseq. */
  803. temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
  804. REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
  805. /* Program data into FDATA0 to N */
  806. if (write_cmd && (datalength != 0))
  807. ich_fill_data(data, datalength, ICH9_REG_FDATA0);
  808. /* Assemble SSFS + SSFC */
  809. temp32 = REGREAD32(ICH9_REG_SSFS);
  810. /* Keep reserved bits only */
  811. temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
  812. /* Clear cycle done and cycle error status registers */
  813. temp32 |= (SSFS_FDONE | SSFS_FCERR);
  814. REGWRITE32(ICH9_REG_SSFS, temp32);
  815. /* Use 20 MHz */
  816. temp32 |= SSFC_SCF_20MHZ;
  817. /* Set data byte count (DBC) and data cycle bit (DS) */
  818. if (datalength != 0) {
  819. uint32_t datatemp;
  820. temp32 |= SSFC_DS;
  821. datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
  822. SSFC_DBC);
  823. temp32 |= datatemp;
  824. }
  825. /* Select opcode */
  826. opmenu = REGREAD32(ICH9_REG_OPMENU);
  827. opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
  828. for (opcode_index = 0; opcode_index < 8; opcode_index++) {
  829. if ((opmenu & 0xff) == op.opcode) {
  830. break;
  831. }
  832. opmenu >>= 8;
  833. }
  834. if (opcode_index == 8) {
  835. msg_pdbg("Opcode %x not found.\n", op.opcode);
  836. return 1;
  837. }
  838. temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
  839. timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
  840. /* Handle Atomic. Atomic commands include three steps:
  841. - sending the preop (mainly EWSR or WREN)
  842. - sending the main command
  843. - waiting for the busy bit (WIP) to be cleared
  844. This means the timeout must be sufficient for chip erase
  845. of slow high-capacity chips.
  846. */
  847. switch (op.atomic) {
  848. case 2:
  849. /* Select second preop. */
  850. temp32 |= SSFC_SPOP;
  851. /* And fall through. */
  852. case 1:
  853. /* Atomic command (preop+op) */
  854. temp32 |= SSFC_ACS;
  855. timeout = 100 * 1000 * 60; /* 60 seconds */
  856. break;
  857. }
  858. /* Start */
  859. temp32 |= SSFC_SCGO;
  860. /* write it */
  861. REGWRITE32(ICH9_REG_SSFS, temp32);
  862. /* Wait for Cycle Done Status or Flash Cycle Error. */
  863. while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
  864. --timeout) {
  865. programmer_delay(10);
  866. }
  867. if (!timeout) {
  868. msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
  869. REGREAD32(ICH9_REG_SSFS));
  870. return 1;
  871. }
  872. /* FIXME make sure we do not needlessly cause transaction errors. */
  873. temp32 = REGREAD32(ICH9_REG_SSFS);
  874. if (temp32 & SSFS_FCERR) {
  875. msg_perr("Transaction error!\n");
  876. prettyprint_ich9_reg_ssfs(temp32);
  877. prettyprint_ich9_reg_ssfc(temp32);
  878. /* keep reserved bits */
  879. temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
  880. /* Clear the transaction error. */
  881. REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
  882. return 1;
  883. }
  884. if ((!write_cmd) && (datalength != 0))
  885. ich_read_data(data, datalength, ICH9_REG_FDATA0);
  886. return 0;
  887. }
  888. static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
  889. uint8_t datalength, uint8_t * data)
  890. {
  891. /* max_data_read == max_data_write for all Intel/VIA SPI masters */
  892. uint8_t maxlength = spi_programmer->max_data_read;
  893. if (spi_programmer->type == SPI_CONTROLLER_NONE) {
  894. msg_perr("%s: unsupported chipset\n", __func__);
  895. return -1;
  896. }
  897. if (datalength > maxlength) {
  898. msg_perr("%s: Internal command size error for "
  899. "opcode 0x%02x, got datalength=%i, want <=%i\n",
  900. __func__, op.opcode, datalength, maxlength);
  901. return SPI_INVALID_LENGTH;
  902. }
  903. switch (ich_generation) {
  904. case CHIPSET_ICH7:
  905. return ich7_run_opcode(op, offset, datalength, data, maxlength);
  906. case CHIPSET_ICH8:
  907. default: /* Future version might behave the same */
  908. return ich9_run_opcode(op, offset, datalength, data);
  909. }
  910. }
  911. #define DEFAULT_NUM_FD_REGIONS 5
  912. static int num_fd_regions;
  913. const char *const region_names[] = {
  914. "Flash Descriptor", "BIOS", "Management Engine",
  915. "Gigabit Ethernet", "Platform Data"
  916. };
  917. enum fd_access_level {
  918. FD_REGION_LOCKED,
  919. FD_REGION_READ_ONLY,
  920. FD_REGION_WRITE_ONLY,
  921. FD_REGION_READ_WRITE,
  922. };
  923. struct fd_region_permission {
  924. enum fd_access_level level;
  925. const char *name;
  926. } fd_region_permissions[] = {
  927. /* order corresponds to FRAP bitfield */
  928. { FD_REGION_LOCKED, "locked" },
  929. { FD_REGION_READ_ONLY, "read-only" },
  930. { FD_REGION_WRITE_ONLY, "write-only" },
  931. { FD_REGION_READ_WRITE, "read-write" },
  932. };
  933. /* FIXME: Replace usage of access_names with the region_access struct */
  934. const char *const access_names[4] = {
  935. "locked", "read-only", "write-only", "read-write"
  936. };
  937. struct fd_region {
  938. const char *name;
  939. struct fd_region_permission *permission;
  940. uint32_t base;
  941. uint32_t limit;
  942. } fd_regions[] = {
  943. /* order corresponds to flash descriptor */
  944. { .name = "Flash Descriptor" },
  945. { .name = "BIOS" },
  946. { .name = "Management Engine" },
  947. { .name = "Gigabit Ethernet" },
  948. { .name = "Platform Data" },
  949. };
  950. static int check_fd_permissions_hwseq(int op_type, uint32_t addr, int count)
  951. {
  952. int i;
  953. int ret = 0;
  954. /* check flash descriptor permissions (if present) */
  955. for (i = 0; i < num_fd_regions; i++) {
  956. const char *name = fd_regions[i].name;
  957. enum fd_access_level level;
  958. if ((addr + count - 1 < fd_regions[i].base) ||
  959. (addr > fd_regions[i].limit))
  960. continue;
  961. if (!fd_regions[i].permission) {
  962. msg_perr("No permissions set for flash region %s\n",
  963. fd_regions[i].name);
  964. break;
  965. }
  966. level = fd_regions[i].permission->level;
  967. if (op_type == HWSEQ_READ) {
  968. if (level != FD_REGION_READ_ONLY &&
  969. level != FD_REGION_READ_WRITE) {
  970. msg_pspew("%s: Cannot read address 0x%08x in "
  971. "region %s\n", __func__, addr, name);
  972. ret = SPI_ACCESS_DENIED;
  973. }
  974. } else if (op_type == HWSEQ_WRITE) {
  975. if (level != FD_REGION_WRITE_ONLY &&
  976. level != FD_REGION_READ_WRITE) {
  977. msg_pspew("%s: Cannot write to address 0x%08x "
  978. "in region %s\n", __func__, addr, name);
  979. ret = SPI_ACCESS_DENIED;
  980. }
  981. }
  982. break;
  983. }
  984. if (i == num_fd_regions) {
  985. msg_pspew("%s: Address not covered by any descriptor 0x%06x\n",
  986. __func__, addr);
  987. ret = SPI_ACCESS_DENIED;
  988. }
  989. return ret;
  990. }
  991. static int check_fd_permissions(OPCODE *opcode, uint32_t addr, int count)
  992. {
  993. int i;
  994. uint8_t type = opcode->spi_type;
  995. int ret = 0;
  996. /* check flash descriptor permissions (if present) */
  997. for (i = 0; i < num_fd_regions; i++) {
  998. const char *name = fd_regions[i].name;
  999. enum fd_access_level level;
  1000. if ((addr + count - 1 < fd_regions[i].base) ||
  1001. (addr > fd_regions[i].limit))
  1002. continue;
  1003. if (!fd_regions[i].permission) {
  1004. msg_perr("No permissions set for flash region %s\n",
  1005. fd_regions[i].name);
  1006. break;
  1007. }
  1008. level = fd_regions[i].permission->level;
  1009. if (type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) {
  1010. if (level != FD_REGION_READ_ONLY &&
  1011. level != FD_REGION_READ_WRITE) {
  1012. msg_pspew("%s: Cannot read address 0x%08x in "
  1013. "region %s\n", __func__,addr,name);
  1014. ret = SPI_ACCESS_DENIED;
  1015. }
  1016. } else if (type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
  1017. if (level != FD_REGION_WRITE_ONLY &&
  1018. level != FD_REGION_READ_WRITE) {
  1019. msg_pspew("%s: Cannot write to address 0x%08x in"
  1020. "region %s\n", __func__,addr,name);
  1021. ret = SPI_ACCESS_DENIED;
  1022. }
  1023. }
  1024. break;
  1025. }
  1026. return ret;
  1027. }
  1028. static int ich_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  1029. const unsigned char *writearr, unsigned char *readarr)
  1030. {
  1031. int result;
  1032. int opcode_index = -1;
  1033. const unsigned char cmd = *writearr;
  1034. OPCODE *opcode;
  1035. uint32_t addr = 0;
  1036. uint8_t *data;
  1037. int count;
  1038. /* find cmd in opcodes-table */
  1039. opcode_index = find_opcode(curopcodes, cmd);
  1040. if (opcode_index == -1) {
  1041. if (!ichspi_lock)
  1042. opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
  1043. if (opcode_index == -1) {
  1044. msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
  1045. cmd);
  1046. return SPI_INVALID_OPCODE;
  1047. }
  1048. }
  1049. opcode = &(curopcodes->opcode[opcode_index]);
  1050. /* The following valid writecnt/readcnt combinations exist:
  1051. * writecnt = 4, readcnt >= 0
  1052. * writecnt = 1, readcnt >= 0
  1053. * writecnt >= 4, readcnt = 0
  1054. * writecnt >= 1, readcnt = 0
  1055. * writecnt >= 1 is guaranteed for all commands.
  1056. */
  1057. if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
  1058. (writecnt != 4)) {
  1059. msg_perr("%s: Internal command size error for opcode "
  1060. "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
  1061. writecnt);
  1062. return SPI_INVALID_LENGTH;
  1063. }
  1064. if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
  1065. (writecnt != 1)) {
  1066. msg_perr("%s: Internal command size error for opcode "
  1067. "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
  1068. writecnt);
  1069. return SPI_INVALID_LENGTH;
  1070. }
  1071. if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
  1072. (writecnt < 4)) {
  1073. msg_perr("%s: Internal command size error for opcode "
  1074. "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
  1075. writecnt);
  1076. return SPI_INVALID_LENGTH;
  1077. }
  1078. if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
  1079. (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
  1080. (readcnt)) {
  1081. msg_perr("%s: Internal command size error for opcode "
  1082. "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
  1083. readcnt);
  1084. return SPI_INVALID_LENGTH;
  1085. }
  1086. /* Translate read/write array/count.
  1087. * The maximum data length is identical for the maximum read length and
  1088. * for the maximum write length excluding opcode and address. Opcode and
  1089. * address are stored in separate registers, not in the data registers
  1090. * and are thus not counted towards data length. The only exception
  1091. * applies if the opcode definition (un)intentionally classifies said
  1092. * opcode incorrectly as non-address opcode or vice versa. */
  1093. if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
  1094. data = (uint8_t *) (writearr + 1);
  1095. count = writecnt - 1;
  1096. } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
  1097. data = (uint8_t *) (writearr + 4);
  1098. count = writecnt - 4;
  1099. } else {
  1100. data = (uint8_t *) readarr;
  1101. count = readcnt;
  1102. }
  1103. /* if opcode-type requires an address */
  1104. if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
  1105. opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
  1106. addr = (writearr[1] << 16) |
  1107. (writearr[2] << 8) | (writearr[3] << 0);
  1108. if (addr < ichspi_bbar) {
  1109. msg_perr("%s: Address 0x%06x below allowed "
  1110. "range 0x%06x-0xffffff\n", __func__,
  1111. addr, ichspi_bbar);
  1112. return SPI_INVALID_ADDRESS;
  1113. }
  1114. if (num_fd_regions > 0) {
  1115. result = check_fd_permissions(opcode, addr, count);
  1116. if (result)
  1117. return result;
  1118. }
  1119. }
  1120. result = run_opcode(flash, *opcode, addr, count, data);
  1121. if (result) {
  1122. msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
  1123. if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
  1124. (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
  1125. msg_pdbg("at address 0x%06x ", addr);
  1126. }
  1127. msg_pdbg("(payload length was %d).\n", count);
  1128. /* Print out the data array if it contains data to write.
  1129. * Errors are detected before the received data is read back into
  1130. * the array so it won't make sense to print it then. */
  1131. if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
  1132. (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
  1133. int i;
  1134. msg_pspew("The data was:\n");
  1135. for (i = 0; i < count; i++){
  1136. msg_pspew("%3d: 0x%02x\n", i, data[i]);
  1137. }
  1138. }
  1139. }
  1140. return result;
  1141. }
  1142. static struct hwseq_data {
  1143. uint32_t size_comp0;
  1144. uint32_t size_comp1;
  1145. } hwseq_data;
  1146. /* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
  1147. static void ich_hwseq_set_addr(uint32_t addr)
  1148. {
  1149. uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
  1150. REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old);
  1151. }
  1152. /* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
  1153. * of the block containing this address. May return nonsense if the address is
  1154. * not valid. The erase block size for a specific address depends on the flash
  1155. * partition layout as specified by FPB and the partition properties as defined
  1156. * by UVSCC and LVSCC respectively. An alternative to implement this method
  1157. * would be by querying FPB and the respective VSCC register directly.
  1158. */
  1159. static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
  1160. {
  1161. uint8_t enc_berase;
  1162. static const uint32_t dec_berase[4] = {
  1163. 256,
  1164. 4 * 1024,
  1165. 8 * 1024,
  1166. 64 * 1024
  1167. };
  1168. ich_hwseq_set_addr(addr);
  1169. enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >>
  1170. HSFS_BERASE_OFF;
  1171. return dec_berase[enc_berase];
  1172. }
  1173. /* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
  1174. Resets all error flags in HSFS.
  1175. Returns 0 if the cycle completes successfully without errors within
  1176. timeout us, 1 on errors. */
  1177. static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
  1178. unsigned int len)
  1179. {
  1180. uint16_t hsfs;
  1181. uint32_t addr;
  1182. timeout /= 8; /* scale timeout duration to counter */
  1183. while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
  1184. (HSFS_FDONE | HSFS_FCERR)) == 0) &&
  1185. --timeout) {
  1186. programmer_delay(8);
  1187. }
  1188. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1189. if (!timeout) {
  1190. addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
  1191. msg_perr("Timeout error between offset 0x%08x and "
  1192. "0x%08x (= 0x%08x + %d)!\n",
  1193. addr, addr + len - 1, addr, len - 1);
  1194. prettyprint_ich9_reg_hsfs(hsfs);
  1195. prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
  1196. return 1;
  1197. }
  1198. if (hsfs & HSFS_FCERR) {
  1199. addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
  1200. msg_perr("Transaction error between offset 0x%08x and "
  1201. "0x%08x (= 0x%08x + %d)!\n",
  1202. addr, addr + len - 1, addr, len - 1);
  1203. prettyprint_ich9_reg_hsfs(hsfs);
  1204. prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
  1205. return 1;
  1206. }
  1207. return 0;
  1208. }
  1209. int ich_hwseq_probe(struct flashctx *flash)
  1210. {
  1211. uint32_t total_size, boundary;
  1212. uint32_t erase_size_low, size_low, erase_size_high, size_high;
  1213. struct block_eraser *eraser;
  1214. total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
  1215. msg_cdbg("Found %d attached SPI flash chip",
  1216. (hwseq_data.size_comp1 != 0) ? 2 : 1);
  1217. if (hwseq_data.size_comp1 != 0)
  1218. msg_cdbg("s with a combined");
  1219. else
  1220. msg_cdbg(" with a");
  1221. msg_cdbg(" density of %d kB.\n", total_size / 1024);
  1222. flash->total_size = total_size / 1024;
  1223. eraser = &(flash->block_erasers[0]);
  1224. boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
  1225. size_high = total_size - boundary;
  1226. erase_size_high = ich_hwseq_get_erase_block_size(boundary);
  1227. if (boundary == 0) {
  1228. msg_cdbg("There is only one partition containing the whole "
  1229. "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
  1230. eraser->eraseblocks[0].size = erase_size_high;
  1231. eraser->eraseblocks[0].count = size_high / erase_size_high;
  1232. msg_cdbg("There are %d erase blocks with %d B each.\n",
  1233. size_high / erase_size_high, erase_size_high);
  1234. } else {
  1235. msg_cdbg("The flash address space (0x%06x - 0x%06x) is divided "
  1236. "at address 0x%06x in two partitions.\n",
  1237. 0, size_high-1, boundary);
  1238. size_low = total_size - size_high;
  1239. erase_size_low = ich_hwseq_get_erase_block_size(0);
  1240. eraser->eraseblocks[0].size = erase_size_low;
  1241. eraser->eraseblocks[0].count = size_low / erase_size_low;
  1242. msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
  1243. 0, size_low-1);
  1244. msg_cdbg("In that range are %d erase blocks with %d B each.\n",
  1245. size_low / erase_size_low, erase_size_low);
  1246. eraser->eraseblocks[1].size = erase_size_high;
  1247. eraser->eraseblocks[1].count = size_high / erase_size_high;
  1248. msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
  1249. boundary, size_high-1);
  1250. msg_cdbg("In that range are %d erase blocks with %d B each.\n",
  1251. size_high / erase_size_high, erase_size_high);
  1252. }
  1253. return 1;
  1254. }
  1255. int ich_hwseq_block_erase(struct flashctx *flash,
  1256. unsigned int addr,
  1257. unsigned int len)
  1258. {
  1259. uint32_t erase_block;
  1260. uint16_t hsfc;
  1261. uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
  1262. erase_block = ich_hwseq_get_erase_block_size(addr);
  1263. if (len != erase_block) {
  1264. msg_cerr("Erase block size for address 0x%06x is %d B, "
  1265. "but requested erase block size is %d B. "
  1266. "Not erasing anything.\n", addr, erase_block, len);
  1267. return -1;
  1268. }
  1269. /* Although the hardware supports this (it would erase the whole block
  1270. * containing the address) we play safe here. */
  1271. if (addr % erase_block != 0) {
  1272. msg_cerr("Erase address 0x%06x is not aligned to the erase "
  1273. "block boundary (any multiple of %d). "
  1274. "Not erasing anything.\n", addr, erase_block);
  1275. return -1;
  1276. }
  1277. if (addr + len > flash->total_size * 1024) {
  1278. msg_perr("Request to erase some inaccessible memory address(es)"
  1279. " (addr=0x%x, len=%d). "
  1280. "Not erasing anything.\n", addr, len);
  1281. return -1;
  1282. }
  1283. msg_pspew("Erasing %d bytes starting at 0x%06x.\n", len, addr);
  1284. /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
  1285. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1286. hsfc = REGREAD16(ICH9_REG_HSFC);
  1287. hsfc &= ~HSFC_FCYCLE; /* clear operation */
  1288. hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
  1289. hsfc |= HSFC_FGO; /* start */
  1290. msg_pspew("HSFC used for block erasing: ");
  1291. prettyprint_ich9_reg_hsfc(hsfc);
  1292. REGWRITE16(ICH9_REG_HSFC, hsfc);
  1293. if (ich_hwseq_wait_for_cycle_complete(timeout, len))
  1294. return -1;
  1295. return 0;
  1296. }
  1297. int ich_hwseq_read(struct flashctx *flash, uint8_t *buf, unsigned int addr,
  1298. unsigned int len)
  1299. {
  1300. uint16_t hsfc;
  1301. uint16_t timeout = 100 * 60;
  1302. uint8_t block_len;
  1303. if ((addr + len) > (flash->total_size * 1024)) {
  1304. msg_perr("Request to read from an inaccessible memory address "
  1305. "(addr=0x%x, len=%d).\n", addr, len);
  1306. return -1;
  1307. }
  1308. msg_pspew("Reading %d bytes starting at 0x%06x.\n", len, addr);
  1309. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1310. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1311. while (len > 0) {
  1312. block_len = min(len, opaque_programmer->max_data_read);
  1313. ich_hwseq_set_addr(addr);
  1314. hsfc = REGREAD16(ICH9_REG_HSFC);
  1315. hsfc &= ~HSFC_FCYCLE; /* set read operation */
  1316. hsfc &= ~HSFC_FDBC; /* clear byte count */
  1317. /* set byte count */
  1318. hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
  1319. hsfc |= HSFC_FGO; /* start */
  1320. REGWRITE16(ICH9_REG_HSFC, hsfc);
  1321. if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
  1322. return 1;
  1323. ich_read_data(buf, block_len, ICH9_REG_FDATA0);
  1324. addr += block_len;
  1325. buf += block_len;
  1326. len -= block_len;
  1327. }
  1328. return 0;
  1329. }
  1330. static int ich_hwseq_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
  1331. unsigned int len)
  1332. {
  1333. uint16_t hsfc;
  1334. uint16_t timeout = 100 * 60;
  1335. uint8_t block_len;
  1336. if ((addr + len) > (flash->total_size * 1024)) {
  1337. msg_perr("Request to write to an inaccessible memory address "
  1338. "(addr=0x%x, len=%d).\n", addr, len);
  1339. return -1;
  1340. }
  1341. msg_pspew("Writing %d bytes starting at 0x%06x.\n", len, addr);
  1342. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1343. REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
  1344. while (len > 0) {
  1345. ich_hwseq_set_addr(addr);
  1346. block_len = min(len, opaque_programmer->max_data_write);
  1347. ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
  1348. hsfc = REGREAD16(ICH9_REG_HSFC);
  1349. hsfc &= ~HSFC_FCYCLE; /* clear operation */
  1350. hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
  1351. hsfc &= ~HSFC_FDBC; /* clear byte count */
  1352. /* set byte count */
  1353. hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
  1354. hsfc |= HSFC_FGO; /* start */
  1355. REGWRITE16(ICH9_REG_HSFC, hsfc);
  1356. if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
  1357. return -1;
  1358. addr += block_len;
  1359. buf += block_len;
  1360. len -= block_len;
  1361. }
  1362. return 0;
  1363. }
  1364. /* Routines for PCH */
  1365. /* Sets FLA in FADDR to (addr & 0x07FFFFFF) without touching other bits. */
  1366. static void pch_hwseq_set_addr(uint32_t addr)
  1367. {
  1368. uint32_t addr_old = REGREAD32(PCH100_REG_FADDR) & ~0x07FFFFFF;
  1369. REGWRITE32(PCH100_REG_FADDR, (addr & 0x07FFFFFF) | addr_old);
  1370. }
  1371. /* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
  1372. * of the block containing this address. May return nonsense if the address is
  1373. * not valid. The erase block size for a specific address depends on the flash
  1374. * partition layout as specified by FPB and the partition properties as defined
  1375. * by UVSCC and LVSCC respectively. An alternative to implement this method
  1376. * would be by querying FPB and the respective VSCC register directly.
  1377. */
  1378. static uint32_t pch_hwseq_get_erase_block_size(unsigned int addr)
  1379. {
  1380. static const uint32_t dec_berase[4] = {
  1381. 256,
  1382. 4 * 1024,
  1383. 8 * 1024,
  1384. 64 * 1024
  1385. };
  1386. pch_hwseq_set_addr(addr);
  1387. return dec_berase[ERASE_BLOCK_SIZE];
  1388. }
  1389. /* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
  1390. Resets all error flags in HSFS.
  1391. Returns 0 if the cycle completes successfully without errors within
  1392. timeout us, 1 on errors. */
  1393. static int pch_hwseq_wait_for_cycle_complete(unsigned int timeout,
  1394. unsigned int len)
  1395. {
  1396. uint16_t hsfs;
  1397. uint32_t addr;
  1398. timeout /= 8; /* scale timeout duration to counter */
  1399. while ((((hsfs = REGREAD16(PCH100_REG_HSFSC)) &
  1400. (HSFSC_FDONE | HSFSC_FCERR)) == 0) &&
  1401. --timeout) {
  1402. programmer_delay(8);
  1403. }
  1404. REGWRITE16(PCH100_REG_HSFSC, REGREAD16(PCH100_REG_HSFSC));
  1405. if (!timeout) {
  1406. addr = REGREAD32(PCH100_REG_FADDR) & 0x07FFFFFF;
  1407. msg_perr("Timeout error between offset 0x%08x and "
  1408. "0x%08x (= 0x%08x + %d)!\n",
  1409. addr, addr + len - 1, addr, len - 1);
  1410. return 1;
  1411. }
  1412. if (hsfs & HSFSC_FCERR) {
  1413. addr = REGREAD32(PCH100_REG_FADDR) & 0x07FFFFFF;
  1414. msg_perr("Transaction error between offset 0x%08x and "
  1415. "0x%08x (= 0x%08x + %d)\n",
  1416. addr, addr + len - 1, addr, len - 1);
  1417. return 1;
  1418. }
  1419. return 0;
  1420. }
  1421. int pch_hwseq_probe(struct flashctx *flash)
  1422. {
  1423. uint32_t total_size, boundary = 0; /*There are no partitions in flash*/
  1424. uint32_t erase_size_high, size_high;
  1425. struct block_eraser *eraser;
  1426. total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
  1427. msg_cdbg("Found %d attached SPI flash chip",
  1428. (hwseq_data.size_comp1 != 0) ? 2 : 1);
  1429. if (hwseq_data.size_comp1 != 0)
  1430. msg_cdbg("s with a combined");
  1431. else
  1432. msg_cdbg(" with a");
  1433. msg_cdbg(" density of %d kB.\n", total_size / 1024);
  1434. flash->total_size = total_size / 1024;
  1435. eraser = &(flash->block_erasers[0]);
  1436. size_high = total_size - boundary;
  1437. erase_size_high = pch_hwseq_get_erase_block_size(boundary);
  1438. eraser->eraseblocks[0].size = erase_size_high;
  1439. eraser->eraseblocks[0].count = size_high / erase_size_high;
  1440. msg_cdbg("There are %d erase blocks with %d B each.\n",
  1441. size_high / erase_size_high, erase_size_high);
  1442. return 1;
  1443. }
  1444. int pch_hwseq_block_erase(struct flashctx *flash,
  1445. unsigned int addr,
  1446. unsigned int len)
  1447. {
  1448. uint32_t erase_block;
  1449. uint16_t hsfc;
  1450. uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
  1451. int result;
  1452. int op_type;
  1453. erase_block = pch_hwseq_get_erase_block_size(addr);
  1454. if (len != erase_block) {
  1455. msg_cerr("Erase block size for address 0x%06x is %d B, "
  1456. "but requested erase block size is %d B. "
  1457. "Not erasing anything.\n", addr, erase_block, len);
  1458. return -1;
  1459. }
  1460. /* Although the hardware supports this (it would erase the whole block
  1461. * containing the address) we play safe here. */
  1462. if (addr % erase_block != 0) {
  1463. msg_cerr("Erase address 0x%06x is not aligned to the erase "
  1464. "block boundary (any multiple of %d). "
  1465. "Not erasing anything.\n", addr, erase_block);
  1466. return -1;
  1467. }
  1468. if (addr + len > flash->total_size * 1024) {
  1469. msg_perr("Request to erase some inaccessible memory address(es)"
  1470. " (addr=0x%x, len=%d). "
  1471. "Not erasing anything.\n", addr, len);
  1472. return -1;
  1473. }
  1474. /* Check flash region permissions before erasing */
  1475. op_type = HWSEQ_WRITE;
  1476. result = check_fd_permissions_hwseq(op_type, addr, len);
  1477. if (result)
  1478. return result;
  1479. msg_pspew("Erasing %d bytes starting at 0x%06x.\n", len, addr);
  1480. /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
  1481. REGWRITE16(PCH100_REG_HSFSC, REGREAD16(PCH100_REG_HSFSC));
  1482. hsfc = REGREAD16(PCH100_REG_HSFSC + 2);
  1483. hsfc &= ~HSFSC_FCYCLE; /* clear operation */
  1484. hsfc |= (0x3 << HSFSC_FCYCLE_OFF); /* set erase operation */
  1485. hsfc |= HSFSC_FGO; /* start */
  1486. msg_pspew("HSFC used for block erasing: ");
  1487. REGWRITE16(PCH100_REG_HSFSC + 2, hsfc);
  1488. if (pch_hwseq_wait_for_cycle_complete(timeout, len))
  1489. return -1;
  1490. return 0;
  1491. }
  1492. int pch_hwseq_read(struct flashctx *flash, uint8_t *buf, unsigned int addr,
  1493. unsigned int len)
  1494. {
  1495. uint16_t hsfc;
  1496. uint16_t timeout = 100 * 60;
  1497. uint8_t block_len;
  1498. int result = 0, chunk_status = 0;
  1499. int op_type;
  1500. if ((addr + len) > (flash->total_size * 1024)) {
  1501. msg_perr("Request to read from an inaccessible memory address "
  1502. "(addr=0x%x, len=%d).\n", addr, len);
  1503. return -1;
  1504. }
  1505. msg_pspew("Reading %d bytes starting at 0x%06x.\n", len, addr);
  1506. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1507. REGWRITE16(PCH100_REG_HSFSC, REGREAD16(PCH100_REG_HSFSC));
  1508. while (len > 0) {
  1509. block_len = min(len, opaque_programmer->max_data_read);
  1510. /* Check flash region permissions before reading */
  1511. op_type = HWSEQ_READ;
  1512. chunk_status = check_fd_permissions_hwseq(op_type,
  1513. addr, block_len);
  1514. if (chunk_status) {
  1515. if (ignore_error(chunk_status)) {
  1516. /* fill this chunk with 0xff bytes and
  1517. * inform the caller about the error */
  1518. memset(buf, 0xff, block_len);
  1519. result = chunk_status;
  1520. } else {
  1521. return chunk_status;
  1522. }
  1523. } else {
  1524. pch_hwseq_set_addr(addr);
  1525. hsfc = REGREAD16(PCH100_REG_HSFSC + 2);
  1526. hsfc &= ~HSFSC_FCYCLE; /* set read operation */
  1527. hsfc &= ~HSFSC_FDBC; /* clear byte count */
  1528. /* set byte count */
  1529. hsfc |= (((block_len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC);
  1530. hsfc |= HSFSC_FGO; /* start */
  1531. REGWRITE16(PCH100_REG_HSFSC + 2, hsfc);
  1532. if (pch_hwseq_wait_for_cycle_complete(timeout, block_len))
  1533. return 1;
  1534. ich_read_data(buf, block_len, PCH100_REG_FDATA0);
  1535. }
  1536. addr += block_len;
  1537. buf += block_len;
  1538. len -= block_len;
  1539. }
  1540. return result;
  1541. }
  1542. uint8_t pch_hwseq_read_status(const struct flashctx *flash)
  1543. {
  1544. uint16_t hsfc;
  1545. uint16_t timeout = 100 * 60;
  1546. int len = 1;
  1547. uint8_t buf;
  1548. msg_pdbg("Reading Status register\n");
  1549. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1550. REGWRITE16(PCH100_REG_HSFSC, REGREAD16(PCH100_REG_HSFSC));
  1551. hsfc = REGREAD16(PCH100_REG_HSFSC + 2);
  1552. hsfc &= ~HSFSC_FCYCLE; /* set read operation */
  1553. /* read status register */
  1554. hsfc |= (0x8 << HSFSC_FCYCLE_OFF);
  1555. hsfc &= ~HSFSC_FDBC; /* clear byte count */
  1556. /* set byte count */
  1557. hsfc |= (((len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC);
  1558. hsfc |= HSFSC_FGO; /* start */
  1559. REGWRITE16(PCH100_REG_HSFSC + 2, hsfc);
  1560. if (pch_hwseq_wait_for_cycle_complete(timeout, len)) {
  1561. msg_perr("Reading Status register failed\n!!");
  1562. return -1;
  1563. }
  1564. ich_read_data(&buf, len, PCH100_REG_FDATA0);
  1565. return buf;
  1566. }
  1567. int pch_hwseq_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
  1568. unsigned int len)
  1569. {
  1570. uint16_t hsfc;
  1571. uint16_t timeout = 100 * 60;
  1572. uint8_t block_len;
  1573. int result;
  1574. int op_type;
  1575. if ((addr + len) > (flash->total_size * 1024)) {
  1576. msg_perr("Request to write to an inaccessible memory address "
  1577. "(addr=0x%x, len=%d).\n", addr, len);
  1578. return -1;
  1579. }
  1580. msg_pspew("Writing %d bytes starting at 0x%06x.\n", len, addr);
  1581. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1582. REGWRITE16(PCH100_REG_HSFSC, REGREAD16(PCH100_REG_HSFSC));
  1583. while (len > 0) {
  1584. pch_hwseq_set_addr(addr);
  1585. block_len = min(len, opaque_programmer->max_data_write);
  1586. /* Check flash region permissions before writing */
  1587. op_type = HWSEQ_WRITE;
  1588. result = check_fd_permissions_hwseq(op_type, addr, block_len);
  1589. if (result)
  1590. return result;
  1591. ich_fill_data(buf, block_len, PCH100_REG_FDATA0);
  1592. hsfc = REGREAD16(PCH100_REG_HSFSC + 2);
  1593. hsfc &= ~HSFSC_FCYCLE; /* clear operation */
  1594. /* set write operation */
  1595. hsfc |= (0x2 << HSFSC_FCYCLE_OFF);
  1596. hsfc &= ~HSFSC_FDBC; /* clear byte count */
  1597. /* set byte count */
  1598. hsfc |= (((block_len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC);
  1599. hsfc |= HSFSC_FGO; /* start */
  1600. REGWRITE16(PCH100_REG_HSFSC + 2, hsfc);
  1601. if (pch_hwseq_wait_for_cycle_complete(timeout, block_len))
  1602. return -1;
  1603. addr += block_len;
  1604. buf += block_len;
  1605. len -= block_len;
  1606. }
  1607. return 0;
  1608. }
  1609. int pch_hwseq_write_status(const struct flashctx *flash, int status)
  1610. {
  1611. uint16_t hsfc;
  1612. uint16_t timeout = 100 * 60;
  1613. int len = 1;
  1614. uint8_t buf = status;
  1615. msg_pdbg("Writing status register\n");
  1616. /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
  1617. REGWRITE16(PCH100_REG_HSFSC, REGREAD16(PCH100_REG_HSFSC));
  1618. ich_fill_data(&buf, len, PCH100_REG_FDATA0);
  1619. hsfc = REGREAD16(PCH100_REG_HSFSC + 2);
  1620. hsfc &= ~HSFSC_FCYCLE; /* clear operation */
  1621. /* write status register */
  1622. hsfc |= (0x7 << HSFSC_FCYCLE_OFF);
  1623. hsfc &= ~HSFSC_FDBC; /* clear byte count */
  1624. /* set byte count */
  1625. hsfc |= (((len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC);
  1626. hsfc |= HSFSC_FGO; /* start */
  1627. REGWRITE16(PCH100_REG_HSFSC + 2, hsfc);
  1628. if (pch_hwseq_wait_for_cycle_complete(timeout, len)) {
  1629. msg_perr("Writing Status register failed\n!!");
  1630. return -1;
  1631. }
  1632. return 0;
  1633. }
  1634. static int ich_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
  1635. {
  1636. int ret = 0;
  1637. int i;
  1638. int oppos, preoppos;
  1639. for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
  1640. if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
  1641. /* Next command is valid. */
  1642. preoppos = find_preop(curopcodes, cmds->writearr[0]);
  1643. oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
  1644. if ((oppos == -1) && (preoppos != -1)) {
  1645. /* Current command is listed as preopcode in
  1646. * ICH struct OPCODES, but next command is not
  1647. * listed as opcode in that struct.
  1648. * Check for command sanity, then
  1649. * try to reprogram the ICH opcode list.
  1650. */
  1651. if (find_preop(curopcodes,
  1652. (cmds + 1)->writearr[0]) != -1) {
  1653. msg_perr("%s: Two subsequent "
  1654. "preopcodes 0x%02x and 0x%02x, "
  1655. "ignoring the first.\n",
  1656. __func__, cmds->writearr[0],
  1657. (cmds + 1)->writearr[0]);
  1658. continue;
  1659. }
  1660. /* If the chipset is locked down, we'll fail
  1661. * during execution of the next command anyway.
  1662. * No need to bother with fixups.
  1663. */
  1664. if (!ichspi_lock) {
  1665. oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
  1666. if (oppos == -1)
  1667. continue;
  1668. curopcodes->opcode[oppos].atomic = preoppos + 1;
  1669. continue;
  1670. }
  1671. }
  1672. if ((oppos != -1) && (preoppos != -1)) {
  1673. /* Current command is listed as preopcode in
  1674. * ICH struct OPCODES and next command is listed
  1675. * as opcode in that struct. Match them up.
  1676. */
  1677. curopcodes->opcode[oppos].atomic = preoppos + 1;
  1678. continue;
  1679. }
  1680. /* If none of the above if-statements about oppos or
  1681. * preoppos matched, this is a normal opcode.
  1682. */
  1683. }
  1684. ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
  1685. cmds->writearr, cmds->readarr);
  1686. /* Reset the type of all opcodes to non-atomic. */
  1687. for (i = 0; i < 8; i++)
  1688. curopcodes->opcode[i].atomic = 0;
  1689. }
  1690. return ret;
  1691. }
  1692. #define ICH_BMWAG(x) ((x >> 24) & 0xff)
  1693. #define ICH_BMRAG(x) ((x >> 16) & 0xff)
  1694. #define ICH_BRWA(x) ((x >> 8) & 0xff)
  1695. #define ICH_BRRA(x) ((x >> 0) & 0xff)
  1696. static void do_ich9_spi_frap(uint32_t frap, int i)
  1697. {
  1698. int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
  1699. (((ICH_BRRA(frap) >> i) & 1) << 0);
  1700. int offset = ICH9_REG_FREG0 + i * 4;
  1701. uint32_t freg = mmio_readl(ich_spibar + offset);
  1702. msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
  1703. offset, freg, i, fd_regions[i].name);
  1704. fd_regions[i].base = ICH_FREG_BASE(freg);
  1705. fd_regions[i].limit = ICH_FREG_LIMIT(freg) | 0x0fff;
  1706. fd_regions[i].permission = &fd_region_permissions[rwperms];
  1707. if (fd_regions[i].base > fd_regions[i].limit) {
  1708. /* this FREG is disabled */
  1709. msg_pdbg("%s region is unused.\n", region_names[i]);
  1710. return;
  1711. }
  1712. msg_pdbg("0x%08x-0x%08x is %s\n", fd_regions[i].base,
  1713. fd_regions[i].limit, fd_regions[i].permission->name);
  1714. }
  1715. /* In contrast to FRAP and the master section of the descriptor the bits
  1716. * in the PR registers have an inverted meaning. The bits in FRAP
  1717. * indicate read and write access _grant_. Here they indicate read
  1718. * and write _protection_ respectively. If both bits are 0 the address
  1719. * bits are ignored.
  1720. */
  1721. #define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
  1722. ((~((pr) >> PR_WP_OFF) & 1) << 1))
  1723. static void prettyprint_ich9_reg_pr(int i, int chipset)
  1724. {
  1725. uint8_t off;
  1726. switch (chipset) {
  1727. case CHIPSET_100_SERIES_SUNRISE_POINT:
  1728. case CHIPSET_APL:
  1729. off = PCH100_REG_FPR0 + (i * 4);
  1730. break;
  1731. default:
  1732. off = ICH9_REG_PR0 + (i * 4);
  1733. break;
  1734. }
  1735. uint32_t pr = mmio_readl(ich_spibar + off);
  1736. int rwperms = ICH_PR_PERMS(pr);
  1737. msg_pdbg2("0x%02X: 0x%08x (PR%u", off, pr, i);
  1738. if (rwperms != 0x3)
  1739. msg_pdbg2(")\n0x%08x-0x%08x is %s\n", ICH_FREG_BASE(pr),
  1740. ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
  1741. else
  1742. msg_pdbg2(", unused)\n");
  1743. }
  1744. /* Set/Clear the read and write protection enable bits of PR register @i
  1745. * according to @read_prot and @write_prot. */
  1746. static void ich9_set_pr(int i, int read_prot, int write_prot, int chipset)
  1747. {
  1748. void *addr;
  1749. switch (chipset) {
  1750. case CHIPSET_100_SERIES_SUNRISE_POINT:
  1751. case CHIPSET_APL:
  1752. addr = ich_spibar + PCH100_REG_FPR0 + (i * 4);
  1753. break;
  1754. default:
  1755. addr = ich_spibar + ICH9_REG_PR0 + (i * 4);
  1756. break;
  1757. }
  1758. uint32_t old = mmio_readl(addr);
  1759. uint32_t new;
  1760. msg_gspew("PR%u is 0x%08x", i, old);
  1761. new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
  1762. if (read_prot)
  1763. new |= (1 << PR_RP_OFF);
  1764. if (write_prot)
  1765. new |= (1 << PR_WP_OFF);
  1766. if (old == new) {
  1767. msg_gspew(" already.\n");
  1768. return;
  1769. }
  1770. msg_gspew(", trying to set it to 0x%08x ", new);
  1771. rmmio_writel(new, addr);
  1772. msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
  1773. }
  1774. static const struct spi_programmer spi_programmer_ich7 = {
  1775. .type = SPI_CONTROLLER_ICH7,
  1776. .max_data_read = 64,
  1777. .max_data_write = 64,
  1778. .command = ich_spi_send_command,
  1779. .multicommand = ich_spi_send_multicommand,
  1780. .read = default_spi_read,
  1781. .write_256 = default_spi_write_256,
  1782. };
  1783. static const struct spi_programmer spi_programmer_ich9 = {
  1784. .type = SPI_CONTROLLER_ICH9,
  1785. .max_data_read = 64,
  1786. .max_data_write = 64,
  1787. .command = ich_spi_send_command,
  1788. .multicommand = ich_spi_send_multicommand,
  1789. .read = default_spi_read,
  1790. .write_256 = default_spi_write_256,
  1791. };
  1792. static struct opaque_programmer opaque_programmer_pch_hwseq = {
  1793. .max_data_read = 64,
  1794. .max_data_write = 64,
  1795. .probe = pch_hwseq_probe,
  1796. .read = pch_hwseq_read,
  1797. .write = pch_hwseq_write,
  1798. .read_status = pch_hwseq_read_status,
  1799. .write_status = pch_hwseq_write_status,
  1800. .erase = pch_hwseq_block_erase,
  1801. };
  1802. static struct opaque_programmer opaque_programmer_ich_hwseq = {
  1803. .max_data_read = 64,
  1804. .max_data_write = 64,
  1805. .probe = ich_hwseq_probe,
  1806. .read = ich_hwseq_read,
  1807. .write = ich_hwseq_write,
  1808. .erase = ich_hwseq_block_erase,
  1809. };
  1810. int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
  1811. enum ich_chipset ich_gen)
  1812. {
  1813. int i;
  1814. uint8_t old, new;
  1815. uint16_t spibar_offset, tmp2;
  1816. uint32_t tmp;
  1817. char *arg;
  1818. int desc_valid = 0;
  1819. struct ich_descriptors desc = {{ 0 }};
  1820. enum ich_spi_mode {
  1821. ich_auto,
  1822. ich_hwseq,
  1823. ich_swseq
  1824. } ich_spi_mode = ich_auto;
  1825. ich_generation = ich_gen;
  1826. switch (ich_generation) {
  1827. case CHIPSET_BAYTRAIL:
  1828. spibar_offset = 0;
  1829. break;
  1830. case CHIPSET_ICH_UNKNOWN:
  1831. return ERROR_FATAL;
  1832. case CHIPSET_ICH7:
  1833. case CHIPSET_ICH8:
  1834. spibar_offset = 0x3020;
  1835. break;
  1836. case CHIPSET_100_SERIES_SUNRISE_POINT:
  1837. case CHIPSET_APL:
  1838. spibar_offset = 0x0;
  1839. break;
  1840. case CHIPSET_ICH9:
  1841. default: /* Future version might behave the same */
  1842. spibar_offset = 0x3800;
  1843. break;
  1844. }
  1845. /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
  1846. msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
  1847. /* Assign Virtual Address */
  1848. ich_spibar = rcrb + spibar_offset;
  1849. switch (ich_generation) {
  1850. case CHIPSET_ICH7:
  1851. msg_pdbg("0x00: 0x%04x (SPIS)\n",
  1852. mmio_readw(ich_spibar + 0));
  1853. msg_pdbg("0x02: 0x%04x (SPIC)\n",
  1854. mmio_readw(ich_spibar + 2));
  1855. msg_pdbg("0x04: 0x%08x (SPIA)\n",
  1856. mmio_readl(ich_spibar + 4));
  1857. for (i = 0; i < 8; i++) {
  1858. int offs;
  1859. offs = 8 + (i * 8);
  1860. msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
  1861. mmio_readl(ich_spibar + offs), i);
  1862. msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
  1863. mmio_readl(ich_spibar + offs + 4), i);
  1864. }
  1865. ichspi_bbar = mmio_readl(ich_spibar + 0x50);
  1866. msg_pdbg("0x50: 0x%08x (BBAR)\n",
  1867. ichspi_bbar);
  1868. msg_pdbg("0x54: 0x%04x (PREOP)\n",
  1869. mmio_readw(ich_spibar + 0x54));
  1870. msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
  1871. mmio_readw(ich_spibar + 0x56));
  1872. msg_pdbg("0x58: 0x%08x (OPMENU)\n",
  1873. mmio_readl(ich_spibar + 0x58));
  1874. msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
  1875. mmio_readl(ich_spibar + 0x5c));
  1876. for (i = 0; i < 3; i++) {
  1877. int offs;
  1878. offs = 0x60 + (i * 4);
  1879. msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
  1880. mmio_readl(ich_spibar + offs), i);
  1881. }
  1882. if (mmio_readw(ich_spibar) & (1 << 15)) {
  1883. msg_pdbg("WARNING: SPI Configuration Lockdown activated.\n");
  1884. ichspi_lock = 1;
  1885. }
  1886. ich_init_opcodes();
  1887. ich_set_bbar(0);
  1888. register_spi_programmer(&spi_programmer_ich7);
  1889. break;
  1890. case CHIPSET_100_SERIES_SUNRISE_POINT:
  1891. case CHIPSET_APL:
  1892. arg = extract_programmer_param("ich_spi_mode");
  1893. if (arg && !strcmp(arg, "hwseq")) {
  1894. ich_spi_mode = ich_hwseq;
  1895. msg_pspew("user selected hwseq\n");
  1896. } else if (arg && !strcmp(arg, "swseq")) {
  1897. /* Swseq not supported in SP */
  1898. msg_perr("swseq not supported\n");
  1899. free(arg);
  1900. return ERROR_FATAL;
  1901. } else if (arg && !strcmp(arg, "auto")) {
  1902. msg_pspew("user selected auto\n");
  1903. /* default mode in SP */
  1904. ich_spi_mode = ich_hwseq;
  1905. } else if (arg && !strlen(arg)) {
  1906. msg_perr("Missing argument for ich_spi_mode.\n");
  1907. free(arg);
  1908. return ERROR_FATAL;
  1909. } else if (arg) {
  1910. msg_perr("Unknown argument for ich_spi_mode: %s\n",
  1911. arg);
  1912. free(arg);
  1913. return ERROR_FATAL;
  1914. } else {
  1915. /* default mode in SP */
  1916. ich_spi_mode = ich_hwseq;
  1917. }
  1918. free(arg);
  1919. tmp = mmio_readl(ich_spibar + PCH100_REG_HSFSC);
  1920. msg_pdbg("0x04: 0x%04x (HSFSC)\n", tmp);
  1921. if (tmp & HSFSC_FLOCKDN) {
  1922. msg_perr("WARNING: SPI Configuration "
  1923. "Lockdown activated.\n");
  1924. ichspi_lock = 1;
  1925. }
  1926. if (tmp & HSFSC_FDV)
  1927. desc_valid = 1;
  1928. if (!(tmp & HSFSC_FDOPSS) && desc_valid)
  1929. msg_perr("The Flash Descriptor Security Override "
  1930. "Strap-Pin is set. Restrictions implied\n"
  1931. "by the FRAP and FREG registers are NOT in "
  1932. "effect. Please note that Protected\n"
  1933. "Range (PR) restrictions still apply.\n");
  1934. if (desc_valid) {
  1935. num_fd_regions = DEFAULT_NUM_FD_REGIONS;
  1936. }
  1937. tmp = mmio_readl(ich_spibar + PCH100_REG_FADDR);
  1938. msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
  1939. if (desc_valid) {
  1940. tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
  1941. msg_cdbg("0x50: 0x%08x (FRAP)\n", tmp);
  1942. msg_cdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
  1943. msg_cdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
  1944. msg_cdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
  1945. msg_cdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
  1946. /* Decode and print FREGx and FRAP registers */
  1947. for (i = 0; i < num_fd_regions; i++)
  1948. do_ich9_spi_frap(tmp, i);
  1949. }
  1950. /* try to disable PR locks before printing them */
  1951. if (!ichspi_lock)
  1952. for (i = 0; i < num_fd_regions; i++)
  1953. ich9_set_pr(i, 0, 0, ich_generation);
  1954. for (i = 0; i < num_fd_regions; i++)
  1955. prettyprint_ich9_reg_pr(i, ich_generation);
  1956. if (desc_valid) {
  1957. if (read_ich_descriptors_via_fdo(ich_spibar, &desc,
  1958. ich_generation) == ICH_RET_OK)
  1959. prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
  1960. &desc);
  1961. } else {
  1962. msg_perr("Hardware sequencing was requested "
  1963. "but the flash descriptor is not "
  1964. "valid. Aborting.\n");
  1965. return ERROR_FATAL;
  1966. }
  1967. hwseq_data.size_comp0 = getFCBA_component_density(&desc, 0);
  1968. hwseq_data.size_comp1 = getFCBA_component_density(&desc, 1);
  1969. register_opaque_programmer(&opaque_programmer_pch_hwseq);
  1970. break;
  1971. case CHIPSET_ICH8:
  1972. default: /* Future version might behave the same */
  1973. arg = extract_programmer_param("ich_spi_mode");
  1974. if (arg && !strcmp(arg, "hwseq")) {
  1975. ich_spi_mode = ich_hwseq;
  1976. msg_pspew("user selected hwseq\n");
  1977. } else if (arg && !strcmp(arg, "swseq")) {
  1978. ich_spi_mode = ich_swseq;
  1979. msg_pspew("user selected swseq\n");
  1980. } else if (arg && !strcmp(arg, "auto")) {
  1981. msg_pspew("user selected auto\n");
  1982. ich_spi_mode = ich_auto;
  1983. } else if (arg && !strlen(arg)) {
  1984. msg_perr("Missing argument for ich_spi_mode.\n");
  1985. free(arg);
  1986. return ERROR_FATAL;
  1987. } else if (arg) {
  1988. msg_perr("Unknown argument for ich_spi_mode: %s\n",
  1989. arg);
  1990. free(arg);
  1991. return ERROR_FATAL;
  1992. }
  1993. free(arg);
  1994. tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
  1995. msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
  1996. prettyprint_ich9_reg_hsfs(tmp2);
  1997. if (tmp2 & HSFS_FLOCKDN) {
  1998. msg_pdbg("WARNING: SPI Configuration Lockdown activated.\n");
  1999. ichspi_lock = 1;
  2000. }
  2001. if (tmp2 & HSFS_FDV)
  2002. desc_valid = 1;
  2003. if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
  2004. msg_perr("The Flash Descriptor Security Override "
  2005. "Strap-Pin is set. Restrictions implied\n"
  2006. "by the FRAP and FREG registers are NOT in "
  2007. "effect. Please note that Protected\n"
  2008. "Range (PR) restrictions still apply.\n");
  2009. ich_init_opcodes();
  2010. if (desc_valid) {
  2011. num_fd_regions = DEFAULT_NUM_FD_REGIONS;
  2012. tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
  2013. msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
  2014. prettyprint_ich9_reg_hsfc(tmp2);
  2015. }
  2016. tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
  2017. msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
  2018. if (desc_valid) {
  2019. tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
  2020. msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
  2021. msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
  2022. msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
  2023. msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
  2024. msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
  2025. /* Decode and print FREGx and FRAP registers */
  2026. for (i = 0; i < num_fd_regions; i++)
  2027. do_ich9_spi_frap(tmp, i);
  2028. }
  2029. /* try to disable PR locks before printing them */
  2030. if (!ichspi_lock)
  2031. for (i = 0; i < num_fd_regions; i++)
  2032. ich9_set_pr(i, 0, 0, ich_generation);
  2033. for (i = 0; i < num_fd_regions; i++)
  2034. prettyprint_ich9_reg_pr(i, ich_generation);
  2035. tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
  2036. msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
  2037. prettyprint_ich9_reg_ssfs(tmp);
  2038. if (tmp & SSFS_FCERR) {
  2039. msg_pdbg("Clearing SSFS.FCERR\n");
  2040. mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
  2041. }
  2042. msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
  2043. prettyprint_ich9_reg_ssfc(tmp);
  2044. msg_pdbg("0x94: 0x%04x (PREOP)\n",
  2045. mmio_readw(ich_spibar + ICH9_REG_PREOP));
  2046. msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
  2047. mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
  2048. msg_pdbg("0x98: 0x%08x (OPMENU)\n",
  2049. mmio_readl(ich_spibar + ICH9_REG_OPMENU));
  2050. msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
  2051. mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
  2052. if (ich_generation == CHIPSET_ICH8 && desc_valid) {
  2053. tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
  2054. msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
  2055. msg_pdbg("VSCC: ");
  2056. prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
  2057. } else {
  2058. ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
  2059. msg_pdbg("0xA0: 0x%08x (BBAR)\n",
  2060. ichspi_bbar);
  2061. if (desc_valid) {
  2062. tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
  2063. msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
  2064. msg_pdbg("LVSCC: ");
  2065. prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
  2066. tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
  2067. msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
  2068. msg_pdbg("UVSCC: ");
  2069. prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
  2070. tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
  2071. msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
  2072. }
  2073. ich_set_bbar(0);
  2074. }
  2075. msg_pdbg("\n");
  2076. if (desc_valid) {
  2077. if (read_ich_descriptors_via_fdo(ich_spibar, &desc,
  2078. ich_generation) == ICH_RET_OK)
  2079. prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
  2080. &desc);
  2081. /* If the descriptor is valid and indicates multiple
  2082. * flash devices we need to use hwseq to be able to
  2083. * access the second flash device.
  2084. */
  2085. if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
  2086. msg_pinfo("Enabling hardware sequencing due to "
  2087. "multiple flash chips detected.\n");
  2088. ich_spi_mode = ich_hwseq;
  2089. }
  2090. }
  2091. if (ich_spi_mode == ich_auto && ichspi_lock &&
  2092. ich_missing_opcodes()) {
  2093. msg_pinfo("Enabling hardware sequencing because "
  2094. "some important opcode is locked.\n");
  2095. ich_spi_mode = ich_hwseq;
  2096. }
  2097. if (ich_spi_mode == ich_hwseq) {
  2098. if (!desc_valid) {
  2099. msg_perr("Hardware sequencing was requested "
  2100. "but the flash descriptor is not "
  2101. "valid. Aborting.\n");
  2102. return ERROR_FATAL;
  2103. }
  2104. hwseq_data.size_comp0 = getFCBA_component_density(&desc, 0);
  2105. hwseq_data.size_comp1 = getFCBA_component_density(&desc, 1);
  2106. register_opaque_programmer(&opaque_programmer_ich_hwseq);
  2107. } else {
  2108. register_spi_programmer(&spi_programmer_ich9);
  2109. }
  2110. break;
  2111. }
  2112. switch (ich_generation) {
  2113. case CHIPSET_BAYTRAIL:
  2114. break;
  2115. default:
  2116. if (ich_generation == CHIPSET_APL)
  2117. old = mmio_readb((void *)dev + 0xdc);
  2118. else
  2119. old = pci_read_byte(dev, 0xdc);
  2120. msg_pdbg("SPI Read Configuration: ");
  2121. new = (old >> 2) & 0x3;
  2122. switch (new) {
  2123. case 0:
  2124. case 1:
  2125. case 2:
  2126. msg_pdbg("prefetching %sabled, caching %sabled, ",
  2127. (new & 0x2) ? "en" : "dis",
  2128. (new & 0x1) ? "dis" : "en");
  2129. break;
  2130. default:
  2131. msg_pdbg("invalid prefetching/caching settings, ");
  2132. break;
  2133. }
  2134. }
  2135. return 0;
  2136. }
  2137. static const struct spi_programmer spi_programmer_via = {
  2138. .type = SPI_CONTROLLER_VIA,
  2139. .max_data_read = 16,
  2140. .max_data_write = 16,
  2141. .command = ich_spi_send_command,
  2142. .multicommand = ich_spi_send_multicommand,
  2143. .read = default_spi_read,
  2144. .write_256 = default_spi_write_256,
  2145. };
  2146. int via_init_spi(struct pci_dev *dev)
  2147. {
  2148. uint32_t mmio_base;
  2149. int i;
  2150. mmio_base = (pci_read_long(dev, 0xbc)) << 8;
  2151. msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
  2152. ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
  2153. /* Not sure if it speaks all these bus protocols. */
  2154. internal_buses_supported = BUS_LPC | BUS_FWH;
  2155. ich_generation = CHIPSET_ICH7;
  2156. register_spi_programmer(&spi_programmer_via);
  2157. msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
  2158. msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
  2159. msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
  2160. for (i = 0; i < 2; i++) {
  2161. int offs;
  2162. offs = 8 + (i * 8);
  2163. msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
  2164. mmio_readl(ich_spibar + offs), i);
  2165. msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
  2166. mmio_readl(ich_spibar + offs + 4), i);
  2167. }
  2168. ichspi_bbar = mmio_readl(ich_spibar + 0x50);
  2169. msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
  2170. msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
  2171. msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
  2172. msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
  2173. msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
  2174. for (i = 0; i < 3; i++) {
  2175. int offs;
  2176. offs = 0x60 + (i * 4);
  2177. msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
  2178. mmio_readl(ich_spibar + offs), i);
  2179. }
  2180. msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
  2181. mmio_readw(ich_spibar + 0x6c));
  2182. if (mmio_readw(ich_spibar) & (1 << 15)) {
  2183. msg_pdbg("WARNING: SPI Configuration Lockdown activated.\n");
  2184. ichspi_lock = 1;
  2185. }
  2186. ich_set_bbar(0);
  2187. ich_init_opcodes();
  2188. return 0;
  2189. }
  2190. #endif