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