mb/google/skyrim: Enable ACPI tables
[coreboot.git] / util / intelp2m / parser / template.go
blob3248152ceddc89db0daa1b322ef0551429717a89
1 package parser
3 import (
4 "fmt"
5 "strings"
6 "unicode"
9 type template func(string, *string, *string, *uint32, *uint32) int
11 // extractPadFuncFromComment
12 // line : string from file with pad config map
13 // return : pad function string
14 func extractPadFuncFromComment(line string) string {
15 if !strings.Contains(line, "/*") && !strings.Contains(line, "*/") {
16 return ""
19 fields := strings.Fields(line)
20 for i, field := range fields {
21 if field == "/*" && len(fields) >= i+2 {
22 return fields[i+1]
25 return ""
28 // tokenCheck
29 func tokenCheck(c rune) bool {
30 return c != '_' && c != '#' && !unicode.IsLetter(c) && !unicode.IsNumber(c)
33 // useGpioHTemplate
34 // line : string from file with pad config map
35 // *function : the string that means the pad function
36 // *id : pad id string
37 // *dw0 : DW0 register value
38 // *dw1 : DW1 register value
39 // return
40 // error status
41 func useInteltoolLogTemplate(line string, function *string,
42 id *string, dw0 *uint32, dw1 *uint32) int {
44 var val uint64
45 // 0x0520: 0x0000003c44000600 GPP_B12 SLP_S0#
46 // 0x0438: 0xffffffffffffffff GPP_C7 RESERVED
47 if fields := strings.FieldsFunc(line, tokenCheck); len(fields) >= 4 {
48 fmt.Sscanf(fields[1], "0x%x", &val)
49 *dw0 = uint32(val & 0xffffffff)
50 *dw1 = uint32(val >> 32)
51 *id = fields[2]
52 *function = fields[3]
53 // Sometimes the configuration file contains compound functions such as
54 // SUSWARN#/SUSPWRDNACK. Since the template does not take this into account,
55 // need to collect all parts of the pad function back into a single word
56 for i := 4; i < len(fields); i++ {
57 *function += "/" + fields[i]
59 // clear RO Interrupt Select (INTSEL)
60 *dw1 &= 0xffffff00
62 return 0
65 // useGpioHTemplate
66 // line : string from file with pad config map
67 // *function : the string that means the pad function
68 // *id : pad id string
69 // *dw0 : DW0 register value
70 // *dw1 : DW1 register value
71 // return
72 // error status
73 func useGpioHTemplate(line string, function *string,
74 id *string, dw0 *uint32, dw1 *uint32) int {
76 // /* RCIN# */ _PAD_CFG_STRUCT(GPP_A0, 0x44000702, 0x00000000),
77 // _PAD_CFG_STRUCT(GPP_A0, 0x44000702, 0x00000000), /* RCIN# */
78 // _PAD_CFG_STRUCT(GPP_A0, 0x44000702, 0x00000000)
79 fields := strings.FieldsFunc(line, tokenCheck)
80 for i, field := range fields {
81 if field == "_PAD_CFG_STRUCT" {
82 if len(fields) < 4 {
83 /* the number of definitions does not match the format */
84 return -1
87 if !strings.Contains(fields[i+2], "0x") || !strings.Contains(fields[i+3], "0x") {
88 /* definitions inside the macro do not match the pattern */
89 return -1
91 *id = fields[i+1]
92 fmt.Sscanf(fields[i+2], "0x%x", dw0)
93 fmt.Sscanf(fields[i+3], "0x%x", dw1)
94 *function = extractPadFuncFromComment(line)
95 return 0
98 return -1
101 // useYourTemplate
102 func useYourTemplate(line string, function *string,
103 id *string, dw0 *uint32, dw1 *uint32) int {
105 // ADD YOUR TEMPLATE HERE
106 *function = ""
107 *id = ""
108 *dw0 = 0
109 *dw1 = 0
111 fmt.Printf("ADD YOUR TEMPLATE!\n")
112 return -1
115 // registerInfoTemplate
116 // line : (in) string from file with pad config map
117 // *name : (out) register name
118 // *offset : (out) offset name
119 // *value : (out) register value
120 // return
121 // error status
122 func registerInfoTemplate(line string, name *string, offset *uint32, value *uint32) int {
123 // 0x0088: 0x00ffffff (HOSTSW_OWN_GPP_F)
124 // 0x0100: 0x00000000 (GPI_IS_GPP_A)
125 if fields := strings.FieldsFunc(line, tokenCheck); len(fields) == 3 {
126 *name = fields[2]
127 fmt.Sscanf(fields[1], "0x%x", value)
128 fmt.Sscanf(fields[0], "0x%x", offset)
129 return 0
131 return -1