blob: 0e2a64bbc281c1f35c9c84f9db52878bf7fb9560 [file] [log] [blame]
Maxim Polyakov82ec61e2020-04-26 22:12:01 +03001package parser
2
3import (
4 "bufio"
5 "fmt"
6 "strings"
7 "strconv"
Maxim Polyakov593b0f12022-05-11 22:49:14 +03008
9 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common"
10 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/snr"
11 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/lbg"
12 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/apl"
13 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/cnl"
14 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/adl"
Jonathon Hall1af3e3c2023-01-27 18:05:30 -050015 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/jsl"
Maxim Polyakov593b0f12022-05-11 22:49:14 +030016 "review.coreboot.org/coreboot.git/util/intelp2m/config"
Maxim Polyakov82ec61e2020-04-26 22:12:01 +030017)
18
Maxim Polyakov82ec61e2020-04-26 22:12:01 +030019// PlatformSpecific - platform-specific interface
20type PlatformSpecific interface {
21 GenMacro(id string, dw0 uint32, dw1 uint32, ownership uint8) string
22 GroupNameExtract(line string) (bool, string)
23 KeywordCheck(line string) bool
24}
25
26// padInfo - information about pad
27// id : pad id string
28// offset : the offset of the register address relative to the base
29// function : the string that means the pad function
30// dw0 : DW0 register value
31// dw1 : DW1 register value
32// ownership : host software ownership
33type padInfo struct {
34 id string
35 offset uint16
36 function string
37 dw0 uint32
38 dw1 uint32
39 ownership uint8
40}
41
42// generate - wrapper for Fprintf(). Writes text to the file specified
43// in config.OutputGenFile
Maxim Polyakov726282b2020-09-30 16:46:11 +030044func (info *padInfo) generate(lvl int, line string, a ...interface{}) {
Maxim Polyakov82ec61e2020-04-26 22:12:01 +030045 if config.InfoLevelGet() >= lvl {
46 fmt.Fprintf(config.OutputGenFile, line, a...)
47 }
48}
49
50// titleFprint - print GPIO group title to file
51// /* ------- GPIO Group GPP_L ------- */
52func (info *padInfo) titleFprint() {
53 info.generate(0, "\n\t/* %s */\n", info.function)
54}
55
56// reservedFprint - print reserved GPIO to file as comment
57// /* GPP_H17 - RESERVED */
58func (info *padInfo) reservedFprint() {
59 info.generate(2, "\n")
60 // small comment about reserved port
61 info.generate(0, "\t/* %s - %s */\n", info.id, info.function)
62}
63
64// padInfoMacroFprint - print information about current pad to file using
65// special macros:
66// PAD_CFG_NF(GPP_F1, 20K_PU, PLTRST, NF1), /* SATAXPCIE4 */
67// gpio : gpio.c file descriptor
68// macro : string of the generated macro
69func (info *padInfo) padInfoMacroFprint(macro string) {
Maxim Polyakov726282b2020-09-30 16:46:11 +030070 info.generate(2,
71 "\n\t/* %s - %s */\n\t/* DW0: 0x%0.8x, DW1: 0x%0.8x */\n",
72 info.id,
73 info.function,
74 info.dw0,
75 info.dw1)
Maxim Polyakov82ec61e2020-04-26 22:12:01 +030076 info.generate(0, "\t%s", macro)
Maxim Polyakov726282b2020-09-30 16:46:11 +030077 if config.InfoLevelGet() == 1 {
78 info.generate(1, "\t/* %s */", info.function)
Maxim Polyakov82ec61e2020-04-26 22:12:01 +030079 }
80 info.generate(0, "\n")
81}
82
83// ParserData - global data
84// line : string from the configuration file
85// padmap : pad info map
86// RawFmt : flag for generating pads config file with DW0/1 reg raw values
87// Template : structure template type of ConfigFile
88type ParserData struct {
89 platform PlatformSpecific
90 line string
91 padmap []padInfo
92 ownership map[string]uint32
93}
94
95// hostOwnershipGet - get the host software ownership value for the corresponding
96// pad ID
97// id : pad ID string
98// return the host software ownership form the parser struct
99func (parser *ParserData) hostOwnershipGet(id string) uint8 {
100 var ownership uint8 = 0
101 status, group := parser.platform.GroupNameExtract(id)
102 if config.TemplateGet() == config.TempInteltool && status {
103 numder, _ := strconv.Atoi(strings.TrimLeft(id, group))
104 if (parser.ownership[group] & (1 << uint8(numder))) != 0 {
105 ownership = 1
106 }
107 }
108 return ownership
109}
110
111// padInfoExtract - adds a new entry to pad info map
112// return error status
113func (parser *ParserData) padInfoExtract() int {
114 var function, id string
115 var dw0, dw1 uint32
116 var template = map[int]template{
117 config.TempInteltool: useInteltoolLogTemplate,
118 config.TempGpioh : useGpioHTemplate,
119 config.TempSpec : useYourTemplate,
120 }
121 if template[config.TemplateGet()](parser.line, &function, &id, &dw0, &dw1) == 0 {
122 pad := padInfo{id: id,
123 function: function,
124 dw0: dw0,
125 dw1: dw1,
126 ownership: parser.hostOwnershipGet(id)}
127 parser.padmap = append(parser.padmap, pad)
128 return 0
129 }
130 fmt.Printf("This template (%d) does not match!\n", config.TemplateGet())
131 return -1
132}
133
134// communityGroupExtract
135func (parser *ParserData) communityGroupExtract() {
136 pad := padInfo{function: parser.line}
137 parser.padmap = append(parser.padmap, pad)
138}
139
140// PlatformSpecificInterfaceSet - specific interface for the platform selected
141// in the configuration
142func (parser *ParserData) PlatformSpecificInterfaceSet() {
143 var platform = map[uint8]PlatformSpecific {
144 config.SunriseType : snr.PlatformSpecific{},
145 // See platforms/lbg/macro.go
146 config.LewisburgType : lbg.PlatformSpecific{
147 InheritanceTemplate : snr.PlatformSpecific{},
148 },
149 config.ApolloType : apl.PlatformSpecific{},
Matt DeVillier5eeead2d2020-08-09 14:13:56 -0500150 config.CannonType : cnl.PlatformSpecific{
151 InheritanceTemplate : snr.PlatformSpecific{},
152 },
Jonathon Hall1af3e3c2023-01-27 18:05:30 -0500153 config.AlderType : adl.PlatformSpecific{},
154 config.JasperType : jsl.PlatformSpecific{},
Maxim Polyakov82ec61e2020-04-26 22:12:01 +0300155 }
156 parser.platform = platform[config.PlatformGet()]
157}
158
159// PadMapFprint - print pad info map to file
160func (parser *ParserData) PadMapFprint() {
161 for _, pad := range parser.padmap {
162 switch pad.dw0 {
163 case 0:
164 pad.titleFprint()
165 case 0xffffffff:
166 pad.reservedFprint()
167 default:
168 str := parser.platform.GenMacro(pad.id, pad.dw0, pad.dw1, pad.ownership)
169 pad.padInfoMacroFprint(str)
170 }
171 }
172}
173
174// Register - read specific platform registers (32 bits)
175// line : string from file with pad config map
176// nameTemplate : register name femplate to filter parsed lines
177// return
178// valid : true if the dump of the register in intertool.log is set in accordance
179// with the template
180// name : full register name
181// offset : register offset relative to the base address
182// value : register value
183func (parser *ParserData) Register(nameTemplate string) (
184 valid bool, name string, offset uint32, value uint32) {
185 if strings.Contains(parser.line, nameTemplate) &&
186 config.TemplateGet() == config.TempInteltool {
187 if registerInfoTemplate(parser.line, &name, &offset, &value) == 0 {
188 fmt.Printf("\n\t/* %s : 0x%x : 0x%x */\n", name, offset, value)
189 return true, name, offset, value
190 }
191 }
192 return false, "ERROR", 0, 0
193}
194
195// padOwnershipExtract - extract Host Software Pad Ownership from inteltool dump
196// return true if success
197func (parser *ParserData) padOwnershipExtract() bool {
198 var group string
199 status, name, offset, value := parser.Register("HOSTSW_OWN_GPP_")
200 if status {
201 _, group = parser.platform.GroupNameExtract(parser.line)
202 parser.ownership[group] = value
203 fmt.Printf("\n\t/* padOwnershipExtract: [offset 0x%x] %s = 0x%x */\n",
204 offset, name, parser.ownership[group])
205 }
206 return status
207}
208
209// padConfigurationExtract - reads GPIO configuration registers and returns true if the
210// information from the inteltool log was successfully parsed.
211func (parser *ParserData) padConfigurationExtract() bool {
Matt DeVillier5eeead2d2020-08-09 14:13:56 -0500212 // Only for Sunrise or CannonLake, and only for inteltool.log file template
Maxim Polyakov82ec61e2020-04-26 22:12:01 +0300213 if config.TemplateGet() != config.TempInteltool || config.IsPlatformApollo() {
214 return false
215 }
216 return parser.padOwnershipExtract()
217}
218
219// Parse pads groupe information in the inteltool log file
220// ConfigFile : name of inteltool log file
221func (parser *ParserData) Parse() {
222 // Read all lines from inteltool log file
223 fmt.Println("Parse IntelTool Log File...")
224
225 // determine the platform type and set the interface for it
226 parser.PlatformSpecificInterfaceSet()
227
228 // map of thepad ownership registers for the GPIO controller
229 parser.ownership = make(map[string]uint32)
230
231 scanner := bufio.NewScanner(config.InputRegDumpFile)
232 for scanner.Scan() {
233 parser.line = scanner.Text()
Maxim Polyakov0a6f8282020-09-08 10:12:02 +0300234 isIncluded, _ := common.KeywordsCheck(parser.line, "GPIO Community", "GPIO Group");
235 if isIncluded {
Maxim Polyakov82ec61e2020-04-26 22:12:01 +0300236 parser.communityGroupExtract()
237 } else if !parser.padConfigurationExtract() && parser.platform.KeywordCheck(parser.line) {
238 if parser.padInfoExtract() != 0 {
239 fmt.Println("...error!")
240 }
241 }
242 }
243 fmt.Println("...done!")
244}