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