hc
2023-11-06 9df731a176aab8e03b984b681b1bea01ccff6644
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * (C) Copyright 2021 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
#include <common.h>
#include <adc.h>
#include <asm/io.h>
#include <dm/ofnode.h>
#include <asm/arch/resource_img.h>
 
#define is_digit(c)        ((c) >= '0' && (c) <= '9')
#define is_abcd(c)        ((c) >= 'a' && (c) <= 'd')
#define is_equal(c)        ((c) == '=')
#define KEY_WORDS_ADC_CTRL    "#_"
#define KEY_WORDS_ADC_CH    "_ch"
#define KEY_WORDS_GPIO        "#gpio"
#define MAX_ADC_CH_NR        10
#define MAX_GPIO_NR        10
 
static fdt_addr_t gpio_base_addr[MAX_GPIO_NR];
static uint32_t gpio_record[MAX_GPIO_NR];
static int adc_record[MAX_ADC_CH_NR];
 
#ifdef CONFIG_ROCKCHIP_GPIO_V2
#define GPIO_SWPORT_DDR        0x08
#define GPIO_EXT_PORT        0x70
#define WMSK_SETBIT(n)        (n << 16 | n)
#define WMSK_CLRBIT(n)        (n << 16)
#define REG_PLUS4(off, n)    (off + (n >= BIT(16) ? 4 : 0))
#define BIT_SUB16(n)        (n >= BIT(16) ? (n >> 16) : n)
static int gpio_read(fdt_addr_t gpio_addr, int gpio_bank, int gpio_pin)
{
   uint32_t off, bit;
 
   bit = gpio_bank * 8 + gpio_pin;
   off = REG_PLUS4(GPIO_SWPORT_DDR, bit);
   bit = BIT_SUB16(bit);
   writel(WMSK_CLRBIT(bit), gpio_addr + off);
 
   return readl(gpio_addr + GPIO_EXT_PORT);
}
 
#else
#define GPIO_SWPORT_DDR        0x04
#define GPIO_EXT_PORT        0x50
static int gpio_read(fdt_addr_t gpio_addr, int gpio_bank, int gpio_pin)
{
   uint32_t val;
 
   val = readl(gpio_addr + GPIO_SWPORT_DDR);
   val &= ~(1 << (gpio_bank * 8 + gpio_pin));
   writel(val, gpio_addr + GPIO_SWPORT_DDR);
 
   return readl(gpio_addr + GPIO_EXT_PORT);
}
#endif
 
static int gpio_parse_base_address(fdt_addr_t *gpio_base_addr)
{
   static int initialized;
   ofnode parent, node;
   int idx = 0;
 
   if (initialized)
       return 0;
 
   parent = ofnode_path("/pinctrl");
   if (!ofnode_valid(parent)) {
       debug("   - No pinctrl node\n");
       return -EINVAL;
   }
 
   ofnode_for_each_subnode(node, parent) {
       if (!ofnode_get_property(node, "gpio-controller", NULL)) {
           debug("   - No gpio controller node\n");
           continue;
       }
       gpio_base_addr[idx] = ofnode_get_addr(node);
       debug("   - gpio%d: 0x%x\n", idx, (uint32_t)gpio_base_addr[idx]);
       idx++;
   }
 
   if (idx == 0) {
       debug("   - parse gpio address failed\n");
       return -EINVAL;
   }
 
   initialized = 1;
 
   return 0;
}
 
static void hwid_init_data(void)
{
   memset(adc_record, 0, sizeof(adc_record));
   memset(gpio_record, 0, sizeof(gpio_record));
   memset(gpio_base_addr, 0, sizeof(gpio_base_addr));
}
 
/*
 * How to use ?
 *
 * 1. Pack dtbs into resource.img, dtb file name:
 *    - End with: ".dtb"
 *    - Pattern: ...#_[controller]_ch[channel]=[value]...dtb
 *        @controller: adc controller name in dts, eg. "saradc", ...;
 *        @channel: adc channel;
 *        @value: adc value;
 *    - Eg: ...#_saradc_ch1=223#_saradc_ch2=650....dtb
 *
 * 2. U-Boot dtsi about adc node:
 *    - Add "u-boot,dm-pre-reloc;"
 *    - Set node "okay"
 */
static int hwid_adc_find_dtb(const char *file_name)
{
   char *cell_name, *adc_tail, *adc_head, *p;
   int prefix_len, chn_len, len;
   int found = 0, margin = 30;
   int ret, channel;
   char dev_name[32];
   char adc_val[10];
   ulong dtb_adc;
   u32 raw_adc;
 
   debug("[HW-ADC]: %s\n", file_name);
 
   chn_len = strlen(KEY_WORDS_ADC_CH);
   prefix_len = strlen(KEY_WORDS_ADC_CTRL);
   cell_name = strstr(file_name, KEY_WORDS_ADC_CTRL);
   while (cell_name) {
       /* Parse adc controller name */
       adc_tail = strstr(cell_name, KEY_WORDS_ADC_CH);
       adc_head = cell_name + prefix_len;
       len = adc_tail - adc_head;
       strlcpy(dev_name, adc_head, len + 1);
 
       /* Parse adc channel */
       p = adc_tail + chn_len;
       if (is_digit(*p) && is_equal(*(p + 1))) {
           channel = *p - '0';
       } else {
           debug("   - invalid format: %s\n", cell_name);
           return 0;
       }
 
       /*
        * Read raw adc value
        *
        * It doesn't need to read adc value every loop, reading once
        * is enough. We use adc_record[] to save what we have read, zero
        * means not read before.
        */
       if (adc_record[channel] == 0) {
           ret = adc_channel_single_shot(dev_name, channel, &raw_adc);
           if (ret) {
               debug("   - failed to read adc, ret=%d\n", ret);
               return 0;
           }
           adc_record[channel] = raw_adc;
       }
 
       /* Parse dtb adc value */
       p = adc_tail + chn_len + 2;    /* 2: channel and '=' */
       while (*p && is_digit(*p)) {
           len++;
           p++;
       }
       strlcpy(adc_val, adc_tail + chn_len + 2, len + 1);
       dtb_adc = simple_strtoul(adc_val, NULL, 10);
       found = (abs(dtb_adc - adc_record[channel]) <= margin) ? 1 : 0;
       debug("   - dev=%s, channel=%d, dtb_adc=%ld, read=%d, found=%d\n",
             dev_name, channel, dtb_adc, adc_record[channel], found);
       if (!found)
           break;
       cell_name = strstr(p, KEY_WORDS_ADC_CTRL);
 
   }
 
   return found;
}
 
/*
 * How to use ?
 *
 * 1. Pack dtbs into resource.img, dtb file name:
 *    - End with: ".dtb"
 *    - Pattern: ...#gpio[pin]=[level]...dtb
 *         @pin: gpio name, eg. 0a2 means GPIO0A2
 *         @level: gpio level, 0 or 1
 *    - Eg: ...#gpio0a6=1#gpio1c2=0....dtb
 *
 * 2. U-Boot dtsi about gpio node:
 *    - Add "u-boot,dm-pre-reloc;" for [all] gpio node;
 *    - Set all gpio node "disabled" (We just want their property);
 */
static int hwid_gpio_find_dtb(const char *file_name)
{
   uint8_t port, pin, bank, lvl, val;
   char *cell_name, *p;
   int ret, prefix_len;
   int found = 0;
   u32 bit;
 
   debug("[HW-GPIO]: %s\n", file_name);
 
   if (gpio_base_addr[0] == 0) {
       ret = gpio_parse_base_address(gpio_base_addr);
       if (ret) {
           debug("[HW-GPIO]: Can't parse gpio base, ret=%d\n", ret);
           return 0;
       }
   }
 
   prefix_len = strlen(KEY_WORDS_GPIO);
   cell_name = strstr(file_name, KEY_WORDS_GPIO);
   while (cell_name) {
       p = cell_name + prefix_len;
 
       /* Invalid format ? */
       if (!(is_digit(*(p + 0)) && is_abcd(*(p + 1)) &&
             is_digit(*(p + 2)) && is_equal(*(p + 3)) &&
             is_digit(*(p + 4)))) {
           debug("   - invalid format: %s\n", cell_name);
           return 0;
       }
 
       /* Read gpio value */
       port = *(p + 0) - '0';
       bank = *(p + 1) - 'a';
       pin  = *(p + 2) - '0';
       lvl  = *(p + 4) - '0';
 
       /*
        * It doesn't need to read gpio value every loop, reading once
        * is enough. We use gpio_record[] to save what we have read, zero
        * means not read before.
        */
       if (gpio_record[port] == 0) {
           if (!gpio_base_addr[port]) {
               debug("   - can't find gpio%d base\n", port);
               return 0;
           }
           gpio_record[port] =
               gpio_read(gpio_base_addr[port], bank, pin);
       }
 
       /* Verify result */
       bit = bank * 8 + pin;
       val = gpio_record[port] & (1 << bit) ? 1 : 0;
       found = (val == !!lvl) ? 1 : 0;
       debug("   - gpio%d%c%d=%d, read=%d, found=%d\n",
             port, bank + 'a', pin, lvl, val, found);
       if (!found)
           break;
       cell_name = strstr(p, KEY_WORDS_GPIO);
   }
 
   return found;
}
 
struct resource_file *resource_read_hwid_dtb(void)
{
   struct resource_file *file;
   struct list_head *node;
 
   hwid_init_data();
 
   list_for_each(node, &entry_head) {
       file = list_entry(node, struct resource_file, link);
       if (!strstr(file->name, DTB_SUFFIX))
           continue;
 
       if (strstr(file->name, KEY_WORDS_ADC_CTRL) &&
           strstr(file->name, KEY_WORDS_ADC_CH) &&
           hwid_adc_find_dtb(file->name)) {
           return file;
       } else if (strstr(file->name, KEY_WORDS_GPIO) &&
              hwid_gpio_find_dtb(file->name)) {
           return file;
       }
   }
 
   return NULL;
}