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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
#include <common.h>
#include <boot_rkimg.h>
#include <dm.h>
#include <malloc.h>
#include <of_live.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#include <dm/uclass-internal.h>
#include <asm/arch/hotkey.h>
#include <asm/arch/resource_img.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
#ifdef CONFIG_USING_KERNEL_DTB_V2
static int dm_rm_kernel_dev(void)
{
   struct udevice *dev, *rec[10];
   u32 uclass[] = { UCLASS_CRYPTO };
   int i, j, k;
 
   for (i = 0, j = 0; i < ARRAY_SIZE(uclass); i++) {
       for (uclass_find_first_device(uclass[i], &dev); dev;
            uclass_find_next_device(&dev)) {
           if (dev->flags & DM_FLAG_KNRL_DTB)
               rec[j++] = dev;
       }
 
       for (k = 0; k < j; k++) {
           device_remove(rec[k], DM_REMOVE_NORMAL);
           device_unbind(rec[k]);
       }
   }
 
   return 0;
}
 
static int dm_rm_u_boot_dev(void)
{
   struct udevice *dev, *rec[10];
   u32 uclass[] = { UCLASS_ETH };
   int del = 0;
   int i, j, k;
 
   for (i = 0, j = 0; i < ARRAY_SIZE(uclass); i++) {
       for (uclass_find_first_device(uclass[i], &dev); dev;
            uclass_find_next_device(&dev)) {
           if (dev->flags & DM_FLAG_KNRL_DTB)
               del = 1;
           else
               rec[j++] = dev;
       }
 
       /* remove u-boot dev if there is someone from kernel */
       if (del) {
           for (k = 0; k < j; k++) {
               device_remove(rec[k], DM_REMOVE_NORMAL);
               device_unbind(rec[k]);
           }
       }
   }
 
   return 0;
}
 
#else
/* Here, only fixup cru phandle, pmucru is not included */
static int phandles_fixup_cru(const void *fdt)
{
   const char *props[] = { "clocks", "assigned-clocks", "resets"};
   struct udevice *dev;
   struct uclass *uc;
   const char *comp;
   u32 id, nclocks;
   u32 *clocks;
   int phandle, ncells;
   int off, offset;
   int ret, length;
   int i, j;
   int first_phandle = -1;
 
   phandle = -ENODATA;
   ncells = -ENODATA;
 
   /* fdt points to kernel dtb, getting cru phandle and "#clock-cells" */
   for (offset = fdt_next_node(fdt, 0, NULL);
        offset >= 0;
        offset = fdt_next_node(fdt, offset, NULL)) {
       comp = fdt_getprop(fdt, offset, "compatible", NULL);
       if (!comp)
           continue;
 
       /* Actually, this is not a good method to get cru node */
       off = strlen(comp) - strlen("-cru");
       if (off > 0 && !strncmp(comp + off, "-cru", 4)) {
           phandle = fdt_get_phandle(fdt, offset);
           ncells = fdtdec_get_int(fdt, offset,
                       "#clock-cells", -ENODATA);
           break;
       }
   }
 
   if (phandle == -ENODATA || ncells == -ENODATA)
       return 0;
 
   debug("%s: target cru: clock-cells:%d, phandle:0x%x\n",
         __func__, ncells, fdt32_to_cpu(phandle));
 
   /* Try to fixup all cru phandle from U-Boot dtb nodes */
   for (id = 0; id < UCLASS_COUNT; id++) {
       ret = uclass_get(id, &uc);
       if (ret)
           continue;
 
       if (list_empty(&uc->dev_head))
           continue;
 
       list_for_each_entry(dev, &uc->dev_head, uclass_node) {
           /* Only U-Boot node go further */
           if (!dev_read_bool(dev, "u-boot,dm-pre-reloc") &&
               !dev_read_bool(dev, "u-boot,dm-spl"))
               continue;
 
           for (i = 0; i < ARRAY_SIZE(props); i++) {
               if (!dev_read_prop(dev, props[i], &length))
                   continue;
 
               clocks = malloc(length);
               if (!clocks)
                   return -ENOMEM;
 
               /* Read "props[]" which contains cru phandle */
               nclocks = length / sizeof(u32);
               if (dev_read_u32_array(dev, props[i],
                              clocks, nclocks)) {
                   free(clocks);
                   continue;
               }
 
               /* Fixup with kernel cru phandle */
               for (j = 0; j < nclocks; j += (ncells + 1)) {
                   /*
                    * Check: update pmucru phandle with cru
                    * phandle by mistake.
                    */
                   if (first_phandle == -1)
                       first_phandle = clocks[j];
 
                   if (clocks[j] != first_phandle) {
                       debug("WARN: %s: first cru phandle=%d, this=%d\n",
                             dev_read_name(dev),
                             first_phandle, clocks[j]);
                       continue;
                   }
 
                   clocks[j] = phandle;
               }
 
               /*
                * Override live dt nodes but not fdt nodes,
                * because all U-Boot nodes has been imported
                * to live dt nodes, should use "dev_xxx()".
                */
               dev_write_u32_array(dev, props[i],
                           clocks, nclocks);
               free(clocks);
           }
       }
   }
 
   return 0;
}
 
static int phandles_fixup_gpio(const void *fdt, void *ufdt)
{
   struct udevice *dev;
   struct uclass *uc;
   const char *prop = "gpios";
   const char *comp;
   char *gpio_name[10];
   int gpio_off[10];
   int pinctrl;
   int offset;
   int i = 0;
   int n = 0;
 
   pinctrl = fdt_path_offset(fdt, "/pinctrl");
   if (pinctrl < 0)
       return 0;
 
   memset(gpio_name, 0, sizeof(gpio_name));
   for (offset = fdt_first_subnode(fdt, pinctrl);
        offset >= 0;
        offset = fdt_next_subnode(fdt, offset)) {
       /* assume the font nodes are gpio node */
       if (++i >= ARRAY_SIZE(gpio_name))
           break;
 
       comp = fdt_getprop(fdt, offset, "compatible", NULL);
       if (!comp)
           continue;
 
       if (!strcmp(comp, "rockchip,gpio-bank")) {
           gpio_name[n] = (char *)fdt_get_name(fdt, offset, NULL);
           gpio_off[n]  = offset;
           n++;
       }
   }
 
   if (!gpio_name[0])
       return 0;
 
   if (uclass_get(UCLASS_KEY, &uc) || list_empty(&uc->dev_head))
       return 0;
 
   list_for_each_entry(dev, &uc->dev_head, uclass_node) {
       u32 new_phd, phd_old;
       char *name;
       ofnode ofn;
 
       if (!dev_read_bool(dev, "u-boot,dm-pre-reloc") &&
           !dev_read_bool(dev, "u-boot,dm-spl"))
           continue;
 
       if (dev_read_u32_array(dev, prop, &phd_old, 1))
           continue;
 
       ofn = ofnode_get_by_phandle(phd_old);
       if (!ofnode_valid(ofn))
           continue;
 
       name = (char *)ofnode_get_name(ofn);
       if (!name)
           continue;
 
       for (i = 0; i < ARRAY_SIZE(gpio_name); i++) {
           if (gpio_name[i] && !strcmp(name, gpio_name[i])) {
               new_phd = fdt_get_phandle(fdt, gpio_off[i]);
               dev_write_u32_array(dev, prop, &new_phd, 1);
               break;
           }
       }
   }
 
   return 0;
}
#endif
 
__weak int board_mmc_dm_reinit(struct udevice *dev)
{
   return 0;
}
 
static int mmc_dm_reinit(void)
{
   struct udevice *dev;
   struct uclass *uc;
   int ret;
 
   if (uclass_get(UCLASS_MMC, &uc) || list_empty(&uc->dev_head))
       return 0;
 
   list_for_each_entry(dev, &uc->dev_head, uclass_node) {
       ret = board_mmc_dm_reinit(dev);
       if (ret)
           return ret;
   }
 
   return 0;
}
 
/* Check by property: "/compatible" */
static int dtb_check_ok(void *kfdt, void *ufdt)
{
   const char *compat;
   int index;
 
   /* TODO */
   return 1;
 
   for (index = 0;
        compat = fdt_stringlist_get(ufdt, 0, "compatible",
                    index, NULL), compat;
        index++) {
       debug("u-compat: %s\n", compat);
       if (!fdt_node_check_compatible(kfdt, 0, compat))
           return 1;
   }
 
   return 0;
}
 
int init_kernel_dtb(void)
{
#ifndef CONFIG_USING_KERNEL_DTB_V2
   void *ufdt_blob = (void *)gd->fdt_blob;
#endif
   ulong fdt_addr = 0;
   int ret = -ENODEV;
 
   printf("DM: v%d\n", IS_ENABLED(CONFIG_USING_KERNEL_DTB_V2) ? 2 : 1);
 
   /*
    * If memory size <= 128MB, we firstly try to get "fdt_addr1_r".
    */
   if (gd->ram_size <= SZ_128M)
       fdt_addr = env_get_ulong("fdt_addr1_r", 16, 0);
 
   if (!fdt_addr)
       fdt_addr = env_get_ulong("fdt_addr_r", 16, 0);
   if (!fdt_addr) {
       printf("No Found FDT Load Address.\n");
       return -ENODEV;
   }
 
#ifdef CONFIG_EMBED_KERNEL_DTB_ALWAYS
   printf("Always embed kernel dtb\n");
   goto dtb_embed;
#endif
   ret = rockchip_read_dtb_file((void *)fdt_addr);
   if (!ret) {
       if (!dtb_check_ok((void *)fdt_addr, (void *)gd->fdt_blob)) {
           ret = -EINVAL;
           printf("Kernel dtb mismatch this platform!\n");
       } else {
           goto dtb_okay;
       }
   }
 
#ifdef CONFIG_EMBED_KERNEL_DTB
#ifdef CONFIG_EMBED_KERNEL_DTB_ALWAYS
dtb_embed:
#endif
   if (gd->fdt_blob_kern) {
       if (!dtb_check_ok((void *)gd->fdt_blob_kern, (void *)gd->fdt_blob)) {
           printf("Embedded kernel dtb mismatch this platform!\n");
           return -EINVAL;
       }
 
       fdt_addr = (ulong)memalign(ARCH_DMA_MINALIGN,
               fdt_totalsize(gd->fdt_blob_kern));
       if (!fdt_addr)
           return -ENOMEM;
 
       /*
        * Alloc another space for this embed kernel dtb.
        * Because "fdt_addr_r" *MUST* be the fdt passed to kernel.
        */
       memcpy((void *)fdt_addr, gd->fdt_blob_kern,
              fdt_totalsize(gd->fdt_blob_kern));
       printf("DTB: %s\n", CONFIG_EMBED_KERNEL_DTB_PATH);
   } else
#endif
   {
       printf("Failed to get kernel dtb, ret=%d\n", ret);
       return -ENOENT;
   }
 
dtb_okay:
   gd->fdt_blob = (void *)fdt_addr;
   hotkey_run(HK_FDT);
 
#ifndef CONFIG_USING_KERNEL_DTB_V2
   /*
    * There is a phandle miss match between U-Boot and kernel dtb node,
    * we fixup it in U-Boot live dt nodes.
    *
    * CRU:     all nodes.
    * GPIO: key nodes.
    */
   phandles_fixup_cru((void *)gd->fdt_blob);
   phandles_fixup_gpio((void *)gd->fdt_blob, (void *)ufdt_blob);
#endif
 
   gd->flags |= GD_FLG_KDTB_READY;
   gd->of_root_f = gd->of_root;
   of_live_build((void *)gd->fdt_blob, (struct device_node **)&gd->of_root);
   dm_scan_fdt((void *)gd->fdt_blob, false);
 
#ifdef CONFIG_USING_KERNEL_DTB_V2
   dm_rm_kernel_dev();
   dm_rm_u_boot_dev();
#endif
   /*
    * There maybe something for the mmc devices to do after kernel dtb
    * dm setup, eg: regain the clock device binding from kernel dtb.
    */
   mmc_dm_reinit();
 
   /* Reserve 'reserved-memory' */
   ret = boot_fdt_add_sysmem_rsv_regions((void *)gd->fdt_blob);
   if (ret)
       return ret;
 
   return 0;
}