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