cbfs: Remove remnants of ext-win-*
[coreboot.git] / util / intelp2m / parser / template.go
blobfc67e73cd70f112959c0c41e7881c15dfe7be4c6
1 package parser
3 import (
4 "fmt"
5 "strings"
6 "unicode"
9 const INTSEL_MASK uint32 = 0xffffff00
11 type template func(string, *string, *string, *uint32, *uint32) int
13 // extractPadFuncFromComment
14 // line : string from file with pad config map
15 // return : pad function string
16 func extractPadFuncFromComment(line string) string {
17 if !strings.Contains(line, "/*") && !strings.Contains(line, "*/") {
18 return ""
21 fields := strings.Fields(line)
22 for i, field := range fields {
23 if field == "/*" && len(fields) >= i+2 {
24 return fields[i+1]
27 return ""
30 // tokenCheck
31 func tokenCheck(c rune) bool {
32 return c != '_' && c != '#' && !unicode.IsLetter(c) && !unicode.IsNumber(c)
35 // useGpioHTemplate
36 // line : string from file with pad config map
37 // *function : the string that means the pad function
38 // *id : pad id string
39 // *dw0 : DW0 register value
40 // *dw1 : DW1 register value
41 // return
42 // error status
43 func UseInteltoolLogTemplate(line string, function *string,
44 id *string, dw0 *uint32, dw1 *uint32) int {
46 var val uint64
47 // 0x0520: 0x0000003c44000600 GPP_B12 SLP_S0#
48 // 0x0438: 0xffffffffffffffff GPP_C7 RESERVED
49 if fields := strings.FieldsFunc(line, tokenCheck); len(fields) >= 4 {
50 fmt.Sscanf(fields[1], "0x%x", &val)
51 *dw0 = uint32(val & 0xffffffff)
52 *dw1 = uint32(val >> 32)
53 *id = fields[2]
54 *function = fields[3]
55 // Sometimes the configuration file contains compound functions such as
56 // SUSWARN#/SUSPWRDNACK. Since the template does not take this into account,
57 // need to collect all parts of the pad function back into a single word
58 for i := 4; i < len(fields); i++ {
59 *function += "/" + fields[i]
61 // clear RO Interrupt Select (INTSEL)
62 *dw1 &= INTSEL_MASK
64 return 0
67 // useGpioHTemplate
68 // line : string from file with pad config map
69 // *function : the string that means the pad function
70 // *id : pad id string
71 // *dw0 : DW0 register value
72 // *dw1 : DW1 register value
73 // return
74 // error status
75 func useGpioHTemplate(line string, function *string,
76 id *string, dw0 *uint32, dw1 *uint32) int {
78 // /* RCIN# */ _PAD_CFG_STRUCT(GPP_A0, 0x44000702, 0x00000000),
79 // _PAD_CFG_STRUCT(GPP_A0, 0x44000702, 0x00000000), /* RCIN# */
80 // _PAD_CFG_STRUCT(GPP_A0, 0x44000702, 0x00000000)
81 fields := strings.FieldsFunc(line, tokenCheck)
82 for i, field := range fields {
83 if field == "_PAD_CFG_STRUCT" {
84 if len(fields) < 4 {
85 /* the number of definitions does not match the format */
86 return -1
89 if !strings.Contains(fields[i+2], "0x") || !strings.Contains(fields[i+3], "0x") {
90 /* definitions inside the macro do not match the pattern */
91 return -1
93 *id = fields[i+1]
94 fmt.Sscanf(fields[i+2], "0x%x", dw0)
95 fmt.Sscanf(fields[i+3], "0x%x", dw1)
96 *function = extractPadFuncFromComment(line)
97 return 0
100 return -1
103 // useYourTemplate
104 func useYourTemplate(line string, function *string,
105 id *string, dw0 *uint32, dw1 *uint32) int {
107 // ADD YOUR TEMPLATE HERE
108 *function = ""
109 *id = ""
110 *dw0 = 0
111 *dw1 = 0
113 fmt.Printf("ADD YOUR TEMPLATE!\n")
114 return -1
117 // registerInfoTemplate
118 // line : (in) string from file with pad config map
119 // *name : (out) register name
120 // *offset : (out) offset name
121 // *value : (out) register value
122 // return
123 // error status
124 func registerInfoTemplate(line string, name *string, offset *uint32, value *uint32) int {
125 // 0x0088: 0x00ffffff (HOSTSW_OWN_GPP_F)
126 // 0x0100: 0x00000000 (GPI_IS_GPP_A)
127 if fields := strings.FieldsFunc(line, tokenCheck); len(fields) == 3 {
128 *name = fields[2]
129 fmt.Sscanf(fields[1], "0x%x", value)
130 fmt.Sscanf(fields[0], "0x%x", offset)
131 return 0
133 return -1