Debian Codegraph @

util/intelp2m/platforms/common/macro.go

raw symbols SCIP
1 package common
2
3 import (
4 "fmt"
5 "strconv"
6
7 "review.coreboot.org/coreboot.git/util/intelp2m/config/p2m"
8 "review.coreboot.org/coreboot.git/util/intelp2m/logs"
9 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/register"
10 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/register/bits"
11 )
12
13 const (
14 Driver bool = true
15 Acpi bool = false
16 )
17
18 type FieldsIf interface {
19 DecodeDW0(*Macro) *Macro
20 DecodeDW1(*Macro) *Macro
21 GenerateMacro(*Macro) *Macro
22 }
23
24 type PlatformIf interface {
25 RemapResetSource(*Macro)
26 Pull(*Macro)
27 AddGpiMacro(*Macro)
28 AddGpoMacro(*Macro)
29 AddNativeFunctionMacro(*Macro)
30 AddNoConnMacro(*Macro)
31 GetRegisterDW0() *register.DW0
32 GetRegisterDW1() *register.DW1
33 }
34
35 type Macro struct {
36 line string
37 id string
38 ownership bool
39 Platform PlatformIf
40 Fields FieldsIf
41 }
42
43 func CreateFrom(id string, ownership bool, pi PlatformIf, fi FieldsIf) Macro {
44 return Macro{
45 id: id,
46 ownership: ownership,
47 Platform: pi,
48 Fields: fi,
49 }
50 }
51
52 func (m *Macro) String() string {
53 return m.line
54 }
55
56 func (m *Macro) GetPadId() string {
57 return m.id
58 }
59
60 func (m Macro) IsOwnershipDriver() bool {
61 return m.ownership
62 }
63
64 func (m *Macro) SetOwnershipAcpi() {
65 m.ownership = Acpi
66 }
67
68 func (m *Macro) SetOwnershipDriver() {
69 m.ownership = Driver
70 }
71
72 func (m *Macro) Add(str string) *Macro {
73 m.line += str
74 return m
75 }
76
77 func (m *Macro) Set(str string) *Macro {
78 m.line = str
79 return m
80 }
81
82 func (m *Macro) Clear() *Macro {
83 m.line = ""
84 return m
85 }
86
87 // Id() adds Pad Id to the macro string
88 func (m *Macro) Id() *Macro {
89 return m.Add(m.id)
90 }
91
92 // Separator() adds separator ", " to macro if needed
93 func (m *Macro) Separator() *Macro {
94 line := m.line
95 c := line[len(line)-1]
96 if c != '(' && c != '_' {
97 m.Add(", ")
98 }
99 return m
100 }
101
102 // or - Set " | " if its needed
103 func (m *Macro) Or() *Macro {
104 if str := m.line; str[len(str)-1] == ')' {
105 m.Add(" | ")
106 }
107 return m
108 }
109
110 // Rstsrc() adds PADRSTCFG parameter
111 func (m *Macro) Rstsrc() *Macro {
112 dw0 := m.Platform.GetRegisterDW0()
113 resetsrc := map[uint32]string{
114 0b00: "PWROK",
115 0b01: "DEEP",
116 0b10: "PLTRST",
117 0b11: "RSMRST",
118 }
119 source, exist := resetsrc[dw0.GetResetConfig()]
120 if !exist {
121 logs.Errorf("%s: ResetConfig error: map does not contain %d",
122 m.id, dw0.GetResetConfig())
123 source = "ERROR"
124 }
125 return m.Separator().Add(source)
126 }
127
128 // Pull() adds Pad Termination TERM parameter
129 func (m *Macro) Pull() *Macro {
130 m.Platform.Pull(m)
131 return m
132 }
133
134 // Val() adds pad GPO value to macro string
135 func (m *Macro) Val() *Macro {
136 dw0 := m.Platform.GetRegisterDW0()
137 return m.Separator().Add(strconv.Itoa(int(dw0.GetGPIOTXState())))
138 }
139
140 // Trig() adds Pad GPO value to macro string
141 func (m *Macro) Trig() *Macro {
142 dw0 := m.Platform.GetRegisterDW0()
143 trig := map[uint32]string{
144 0b00: "LEVEL",
145 0b01: "EDGE_SINGLE",
146 0b10: "OFF",
147 0b11: "EDGE_BOTH",
148 }
149 level, exist := trig[dw0.GetRXLevelEdgeConfiguration()]
150 if !exist {
151 logs.Errorf("%s: RXLevelEdgeConfig error: map does not contain %d",
152 m.id, dw0.GetRXLevelEdgeConfiguration())
153 level = "ERROR"
154 }
155 return m.Separator().Add(level)
156 }
157
158 // Invert() adds Pad Polarity Inversion Stage (RXINV) to macro string
159 func (m *Macro) Invert() *Macro {
160 if dw0 := m.Platform.GetRegisterDW0(); dw0.GetRxInvert() != 0 {
161 return m.Separator().Add("INVERT")
162 }
163 return m.Separator().Add("NONE")
164 }
165
166 // Bufdis() adds input/output buffer state
167 func (m *Macro) Bufdis() *Macro {
168 dw0 := m.Platform.GetRegisterDW0()
169 states := map[uint32]string{
170 0b00: "NO_DISABLE", // both buffers are enabled
171 0b01: "TX_DISABLE", // output buffer is disabled
172 0b10: "RX_DISABLE", // input buffer is disabled
173 0b11: "TX_RX_DISABLE", // both buffers are disabled
174 }
175 state, exist := states[dw0.GetGPIORxTxDisableStatus()]
176 if !exist {
177 logs.Errorf("%s: GPIORxTxDisableStatus error: map does not contain %d",
178 m.id, dw0.GetGPIORxTxDisableStatus())
179 state = "ERROR"
180 }
181 return m.Separator().Add(state)
182 }
183
184 // Own() adds macro to set the host software ownership
185 func (m *Macro) Own() *Macro {
186 if m.IsOwnershipDriver() {
187 return m.Separator().Add("DRIVER")
188 }
189 return m.Separator().Add("ACPI")
190 }
191
192 // Padfn() adds pad native function (PMODE)
193 func (m *Macro) Padfn() *Macro {
194 dw0 := m.Platform.GetRegisterDW0()
195 if number := dw0.GetPadMode(); number != 0 {
196 return m.Separator().Add(fmt.Sprintf("NF%d", number))
197 }
198 // GPIO used only for PAD_FUNC(x) macro
199 return m.Add("GPIO")
200 }
201
202 // IOSstate() adds a line to the macro that defines IO Standby State
203 func (m *Macro) IOSstate() *Macro {
204 states := map[uint32]string{
205 bits.IOStateTxLASTRxE: "TxLASTRxE",
206 bits.IOStateTx0RxDCRx0: "Tx0RxDCRx0",
207 bits.IOStateTx0RxDCRx1: "Tx0RxDCRx1",
208 bits.IOStateTx1RxDCRx0: "Tx1RxDCRx0",
209 bits.IOStateTx1RxDCRx1: "Tx1RxDCRx1",
210 bits.IOStateTx0RxE: "Tx0RxE",
211 bits.IOStateTx1RxE: "Tx1RxE",
212 bits.IOStateHIZCRx0: "HIZCRx0",
213 bits.IOStateHIZCRx1: "HIZCRx1",
214 bits.IOStateTxDRxE: "TxDRxE",
215 bits.IOStateStandbyIgnore: "IGNORE",
216 }
217 dw1 := m.Platform.GetRegisterDW1()
218 state, exist := states[dw1.GetIOStandbyState()]
219 if !exist {
220 logs.Errorf("%s: IOStandbyState error: map does not contain %d",
221 m.id, dw1.GetIOStandbyState())
222 state = "ERROR"
223 }
224 return m.Separator().Add(state)
225 }
226
227 // IOTerm() add a line to the macro that defines IO Standby Termination
228 func (m *Macro) IOTerm() *Macro {
229 dw1 := m.Platform.GetRegisterDW1()
230 terminations := map[uint32]string{
231 bits.IOTermSAME: "SAME",
232 bits.IOTermDISPUPD: "DISPUPD",
233 bits.IOTermENPD: "ENPD",
234 bits.IOTermENPU: "ENPU",
235 }
236 termination, exist := terminations[dw1.GetIOStandbyTermination()]
237 if !exist {
238 logs.Errorf("%s: IOStandbyTermination error: map does not contain %d",
239 m.id, dw1.GetIOStandbyTermination())
240 termination = "ERROR"
241 }
242 return m.Separator().Add(termination)
243 }
244
245 // Check created macro
246 func (m *Macro) Check() *Macro {
247 dw0 := m.Platform.GetRegisterDW0()
248 if !dw0.MaskCheck() {
249 return m.GenerateFields()
250 }
251 return m
252 }
253
254 func (m *Macro) DecodeIgnoredFieldsDW0() *Macro {
255 if p2m.Config.Field == p2m.FspFlds {
256 logs.Infof("%s: decoding of extracted fields is not applied for fsp", m.id)
257 return m
258 } else if m.Fields == nil {
259 logs.Errorf("%s: field collection is not set in the macro structure", m.id)
260 return m
261 }
262
263 dw0 := m.Platform.GetRegisterDW0()
264 if ignored := dw0.IgnoredFieldsGet(); ignored != 0 {
265 saved := dw0.Value
266 dw0.Value = ignored
267 m.Add("/* DW0: ").Fields.DecodeDW0(m).Add(" - IGNORED */\n")
268 dw0.Value = saved
269 }
270
271 return m
272 }
273
274 func (m *Macro) DecodeIgnoredFieldsDW1() *Macro {
275 if p2m.Config.Field == p2m.FspFlds {
276 logs.Infof("%s: decoding of extracted fields is not applied for fsp", m.id)
277 return m
278 } else if m.Fields == nil {
279 logs.Errorf("%s: field collection is not set in the macro structure", m.id)
280 return m
281 }
282
283 dw1 := m.Platform.GetRegisterDW1()
284 if ignored := dw1.IgnoredFieldsGet(); ignored != 0 {
285 saved := dw1.Value
286 dw1.Value = ignored
287 m.Add("/* DW1: ").Fields.DecodeDW1(m).Add(" - IGNORED */\n")
288 dw1.Value = saved
289 }
290
291 return m
292 }
293
294 // GenerateFields() generates bitfield macros
295 func (m *Macro) GenerateFields() *Macro {
296 if m.Fields == nil {
297 logs.Errorf("%s: field collection is not set in the macro structure", m.id)
298 return m
299 }
300
301 dw0 := m.Platform.GetRegisterDW0()
302 dw1 := m.Platform.GetRegisterDW1()
303
304 // Get mask of ignored bit fields.
305 dw0Ignored := dw0.IgnoredFieldsGet()
306 dw1Ignored := dw1.IgnoredFieldsGet()
307
308 if p2m.Config.GenLevel != 4 {
309 m.Clear()
310 }
311 if p2m.Config.GenLevel >= 3 {
312 // Add string of reference m as a comment
313 reference := m.line
314 m.Clear()
315 /* DW0 : PAD_TRIG(OFF) | PAD_BUF(RX_DISABLE) | 1 - IGNORED */
316 m.DecodeIgnoredFieldsDW0()
317 m.DecodeIgnoredFieldsDW1()
318 if p2m.Config.GenLevel >= 4 {
319 /* PAD_CFG_NF(GPP_B23, 20K_PD, PLTRST, NF2), */
320 m.Add("/* ").Add(reference).Add(" */\n")
321 }
322 }
323 if p2m.Config.IgnoredFields {
324 // Consider bit fields that should be ignored when regenerating
325 // advansed ms
326 tempVal := dw0.Value & ^dw0Ignored
327 dw0.Value = tempVal
328
329 tempVal = dw1.Value & ^dw1Ignored
330 dw1.Value = tempVal
331 }
332
333 return m.Fields.GenerateMacro(m)
334 }
335
336 // Bidirection() generates macro for bi-directional GPIO port
337 func (m *Macro) Bidirection() {
338 dw1 := m.Platform.GetRegisterDW1()
339 ios := dw1.GetIOStandbyState() != 0 || dw1.GetIOStandbyTermination() != 0
340 m.Set("PAD_CFG_GPIO_BIDIRECT")
341 if ios {
342 m.Add("_IOS")
343 }
344 // PAD_CFG_GPIO_BIDIRECT(pad, val, pull, rst, trig, own)
345 m.Add("(").Id().Val().Pull().Rstsrc().Trig()
346 if ios {
347 // PAD_CFG_GPIO_BIDIRECT_IOS(pad, val, pull, rst, trig, iosstate, iosterm, own)
348 m.IOSstate().IOTerm()
349 }
350 m.Own().Add("),")
351 }
352
353 // Generate() generates string of macro
354 func (m *Macro) Generate() string {
355 m.Platform.RemapResetSource(m)
356 if dw0 := m.Platform.GetRegisterDW0(); dw0.GetPadMode() == 0 {
357 const txDisable uint32 = 0b01
358 const rxDisable uint32 = 0b10
359 switch m.Set("PAD_CFG"); dw0.GetGPIORxTxDisableStatus() {
360 case txDisable:
361 m.Platform.AddGpiMacro(m) // GPI
362
363 case rxDisable:
364 m.Platform.AddGpoMacro(m) // GPO
365
366 case rxDisable | txDisable:
367 m.Platform.AddNoConnMacro(m) // NC
368
369 default:
370 m.Bidirection()
371 }
372 } else {
373 m.Platform.AddNativeFunctionMacro(m)
374 }
375
376 if p2m.Config.Field != p2m.NoFlds {
377 // clear control mask to generate field collection macro
378 return m.GenerateFields().line
379 }
380
381 if !p2m.Config.AutoCheck {
382 body := m.line
383 if p2m.Config.GenLevel >= 3 {
384 if m.Fields == nil {
385 logs.Errorf("%s: field collection is not set in the macro structure", m.id)
386 return body
387 }
388 m.Clear()
389 m.DecodeIgnoredFieldsDW0()
390 m.DecodeIgnoredFieldsDW1()
391 comment := m.line
392 if p2m.Config.GenLevel >= 4 {
393 comment += m.Clear().Add("/* ").Fields.GenerateMacro(m).Add(" */\n").line
394 }
395 return comment + body
396 }
397 return body
398 }
399
400 return m.Check().line
401 }