xref: /aosp_15_r20/external/coreboot/util/intelp2m/parser/template.go (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1package parser
2
3import (
4	"fmt"
5	"strings"
6	"unicode"
7)
8
9type template func(string, *string, *string, *uint32, *uint32) int
10
11// extractPadFuncFromComment
12// line   : string from file with pad config map
13// return : pad function string
14func extractPadFuncFromComment(line string) string {
15	if !strings.Contains(line, "/*") && !strings.Contains(line, "*/") {
16		return ""
17	}
18
19	fields := strings.Fields(line)
20	for i, field := range fields {
21		if field == "/*" && len(fields) >= i+2 {
22			return fields[i+1]
23		}
24	}
25	return ""
26}
27
28// tokenCheck
29func tokenCheck(c rune) bool {
30	return c != '_' && c != '#' && !unicode.IsLetter(c) && !unicode.IsNumber(c)
31}
32
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
41func useInteltoolLogTemplate(line string, function *string,
42	id *string, dw0 *uint32, dw1 *uint32) int {
43
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]
58		}
59		// clear RO Interrupt Select (INTSEL)
60		*dw1 &= 0xffffff00
61	}
62	return 0
63}
64
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
73func useGpioHTemplate(line string, function *string,
74	id *string, dw0 *uint32, dw1 *uint32) int {
75
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
85			}
86
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
90			}
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
96		}
97	}
98	return -1
99}
100
101// useYourTemplate
102func useYourTemplate(line string, function *string,
103	id *string, dw0 *uint32, dw1 *uint32) int {
104
105	// ADD YOUR TEMPLATE HERE
106	*function = ""
107	*id = ""
108	*dw0 = 0
109	*dw1 = 0
110
111	fmt.Printf("ADD YOUR TEMPLATE!\n")
112	return -1
113}
114
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
122func 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
130	}
131	return -1
132}
133