blob: eecea7a0235494e977a91c6c80b47ba1de993b66 [file] [log] [blame]
Angelo Durgehelload420932019-11-15 23:54:16 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) 2019 Angelo Dureghello <angelo.dureghello@timesys.com>
4 */
5
6#include <common.h>
Simon Glass401d1c42020-10-30 21:38:53 -06007#include <asm/global_data.h>
Angelo Durgehelload420932019-11-15 23:54:16 +01008#include <linux/libfdt.h>
9#include <fdt_support.h>
10
11DECLARE_GLOBAL_DATA_PTR;
12
13#if defined(CONFIG_MCFFEC) || defined(CONFIG_FSLDMAFEC)
14static int fec_get_node(int fec_idx)
15{
16 char fec_alias[5] = {"fec"};
17 const char *path;
18 int node;
19
20 if (fec_idx > 1) {
21 puts("Invalid MII base index");
22 return -ENOENT;
23 }
24
25 fec_alias[3] = fec_idx + '0';
26
27 path = fdt_get_alias(gd->fdt_blob, fec_alias);
28 if (!path) {
29 puts("Invalid MII path");
30 return -ENOENT;
31 }
32
33 node = fdt_path_offset(gd->fdt_blob, path);
34 if (node < 0)
35 return -ENOENT;
36
37 return node;
38}
39
40int fec_get_fdt_prop(int fec_idx, const char *prop, u32 *value)
41{
42 int node;
43 const u32 *val;
44
45 node = fec_get_node(fec_idx);
46 if (node < 0)
47 return node;
48
49 val = fdt_getprop(gd->fdt_blob, node, prop, NULL);
50 if (!val)
51 return -ENOENT;
52
53 *value = fdt32_to_cpu(*val);
54
55 return 0;
56}
57
58int fec_get_base_addr(int fec_idx, u32 *fec_iobase)
59{
60 int node;
61 fdt_size_t size;
62 fdt_addr_t addr;
63
64 node = fec_get_node(fec_idx);
65 if (node < 0)
66 return node;
67
68 addr = fdtdec_get_addr_size(gd->fdt_blob, node, "reg", &size);
69
70 *fec_iobase = (u32)addr;
71
72 return 0;
73}
74
75int fec_get_mii_base(int fec_idx, u32 *mii_base)
76{
77 return fec_get_fdt_prop(fec_idx, "mii-base", mii_base);
78}
79
80#endif //CONFIG_MCFFEC || CONFIG_FSLDMAFEC