1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/of.h>
6 #include <linux/of_device.h>
7 #include <linux/regmap.h>
8 #include <linux/mfd/syscon.h>
9 
10 #include "cpsw.h"
11 
12 #define CTRL_MAC_LO_REG(offset, id) ((offset) + 0x8 * (id))
13 #define CTRL_MAC_HI_REG(offset, id) ((offset) + 0x8 * (id) + 0x4)
14 
15 static int davinci_emac_3517_get_macid(struct device *dev, u16 offset,
16 				       int slave, u8 *mac_addr)
17 {
18 	u32 macid_lsb;
19 	u32 macid_msb;
20 	struct regmap *syscon;
21 
22 	syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
23 	if (IS_ERR(syscon)) {
24 		if (PTR_ERR(syscon) == -ENODEV)
25 			return 0;
26 		return PTR_ERR(syscon);
27 	}
28 
29 	regmap_read(syscon, CTRL_MAC_LO_REG(offset, slave), &macid_lsb);
30 	regmap_read(syscon, CTRL_MAC_HI_REG(offset, slave), &macid_msb);
31 
32 	mac_addr[0] = (macid_msb >> 16) & 0xff;
33 	mac_addr[1] = (macid_msb >> 8)  & 0xff;
34 	mac_addr[2] = macid_msb & 0xff;
35 	mac_addr[3] = (macid_lsb >> 16) & 0xff;
36 	mac_addr[4] = (macid_lsb >> 8)  & 0xff;
37 	mac_addr[5] = macid_lsb & 0xff;
38 
39 	return 0;
40 }
41 
42 static int cpsw_am33xx_cm_get_macid(struct device *dev, u16 offset, int slave,
43 				    u8 *mac_addr)
44 {
45 	u32 macid_lo;
46 	u32 macid_hi;
47 	struct regmap *syscon;
48 
49 	syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
50 	if (IS_ERR(syscon)) {
51 		if (PTR_ERR(syscon) == -ENODEV)
52 			return 0;
53 		return PTR_ERR(syscon);
54 	}
55 
56 	regmap_read(syscon, CTRL_MAC_LO_REG(offset, slave), &macid_lo);
57 	regmap_read(syscon, CTRL_MAC_HI_REG(offset, slave), &macid_hi);
58 
59 	mac_addr[5] = (macid_lo >> 8) & 0xff;
60 	mac_addr[4] = macid_lo & 0xff;
61 	mac_addr[3] = (macid_hi >> 24) & 0xff;
62 	mac_addr[2] = (macid_hi >> 16) & 0xff;
63 	mac_addr[1] = (macid_hi >> 8) & 0xff;
64 	mac_addr[0] = macid_hi & 0xff;
65 
66 	return 0;
67 }
68 
69 int ti_cm_get_macid(struct device *dev, int slave, u8 *mac_addr)
70 {
71 	if (of_machine_is_compatible("ti,dm8148"))
72 		return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
73 
74 	if (of_machine_is_compatible("ti,am33xx"))
75 		return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
76 
77 	if (of_device_is_compatible(dev->of_node, "ti,am3517-emac"))
78 		return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
79 
80 	if (of_device_is_compatible(dev->of_node, "ti,dm816-emac"))
81 		return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
82 
83 	if (of_machine_is_compatible("ti,am43"))
84 		return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
85 
86 	if (of_machine_is_compatible("ti,dra7"))
87 		return davinci_emac_3517_get_macid(dev, 0x514, slave, mac_addr);
88 
89 	dev_info(dev, "incompatible machine/device type for reading mac address\n");
90 	return -ENOENT;
91 }
92 EXPORT_SYMBOL_GPL(ti_cm_get_macid);
93 
94 MODULE_LICENSE("GPL");
95