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