]> git.sur5r.net Git - u-boot/blob - drivers/net/cpsw-common.c
Merge git://git.denx.de/u-boot-imx
[u-boot] / drivers / net / cpsw-common.c
1 /*
2  * CPSW common - libs used across TI ethernet devices.
3  *
4  * Copyright (C) 2016, Texas Instruments, Incorporated
5  *
6  * SPDX-License-Identifier: GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <environment.h>
12 #include <fdt_support.h>
13 #include <asm/io.h>
14 #include <cpsw.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 #define CTRL_MAC_REG(offset, id) ((offset) + 0x8 * (id))
19
20 static int davinci_emac_3517_get_macid(struct udevice *dev, u16 offset,
21                                        int slave, u8 *mac_addr)
22 {
23         void *fdt = (void *)gd->fdt_blob;
24         int node = dev_of_offset(dev);
25         u32 macid_lsb;
26         u32 macid_msb;
27         fdt32_t gmii = 0;
28         int syscon;
29         u32 addr;
30
31         syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
32         if (syscon < 0) {
33                 pr_err("Syscon offset not found\n");
34                 return -ENOENT;
35         }
36
37         addr = (u32)map_physmem(fdt_translate_address(fdt, syscon, &gmii),
38                                 sizeof(u32), MAP_NOCACHE);
39         if (addr == FDT_ADDR_T_NONE) {
40                 pr_err("Not able to get syscon address to get mac efuse address\n");
41                 return -ENOENT;
42         }
43
44         addr += CTRL_MAC_REG(offset, slave);
45
46         /* try reading mac address from efuse */
47         macid_lsb = readl(addr);
48         macid_msb = readl(addr + 4);
49
50         mac_addr[0] = (macid_msb >> 16) & 0xff;
51         mac_addr[1] = (macid_msb >> 8)  & 0xff;
52         mac_addr[2] = macid_msb & 0xff;
53         mac_addr[3] = (macid_lsb >> 16) & 0xff;
54         mac_addr[4] = (macid_lsb >> 8)  & 0xff;
55         mac_addr[5] = macid_lsb & 0xff;
56
57         return 0;
58 }
59
60 static int cpsw_am33xx_cm_get_macid(struct udevice *dev, u16 offset, int slave,
61                                     u8 *mac_addr)
62 {
63         void *fdt = (void *)gd->fdt_blob;
64         int node = dev_of_offset(dev);
65         u32 macid_lo;
66         u32 macid_hi;
67         fdt32_t gmii = 0;
68         int syscon;
69         u32 addr;
70
71         syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
72         if (syscon < 0) {
73                 pr_err("Syscon offset not found\n");
74                 return -ENOENT;
75         }
76
77         addr = (u32)map_physmem(fdt_translate_address(fdt, syscon, &gmii),
78                                 sizeof(u32), MAP_NOCACHE);
79         if (addr == FDT_ADDR_T_NONE) {
80                 pr_err("Not able to get syscon address to get mac efuse address\n");
81                 return -ENOENT;
82         }
83
84         addr += CTRL_MAC_REG(offset, slave);
85
86         /* try reading mac address from efuse */
87         macid_lo = readl(addr);
88         macid_hi = readl(addr + 4);
89
90         mac_addr[5] = (macid_lo >> 8) & 0xff;
91         mac_addr[4] = macid_lo & 0xff;
92         mac_addr[3] = (macid_hi >> 24) & 0xff;
93         mac_addr[2] = (macid_hi >> 16) & 0xff;
94         mac_addr[1] = (macid_hi >> 8) & 0xff;
95         mac_addr[0] = macid_hi & 0xff;
96
97         return 0;
98 }
99
100 int ti_cm_get_macid(struct udevice *dev, int slave, u8 *mac_addr)
101 {
102         if (of_machine_is_compatible("ti,dm8148"))
103                 return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
104
105         if (of_machine_is_compatible("ti,am33xx"))
106                 return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
107
108         if (device_is_compatible(dev, "ti,am3517-emac"))
109                 return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
110
111         if (device_is_compatible(dev, "ti,dm816-emac"))
112                 return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
113
114         if (of_machine_is_compatible("ti,am43"))
115                 return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
116
117         if (of_machine_is_compatible("ti,dra7"))
118                 return davinci_emac_3517_get_macid(dev, 0x514, slave, mac_addr);
119
120         dev_err(dev, "incompatible machine/device type for reading mac address\n");
121         return -ENOENT;
122 }