1e5a49c1eSTony Lindgren /*
2e5a49c1eSTony Lindgren  * This program is free software; you can redistribute it and/or modify
3e5a49c1eSTony Lindgren  * it under the terms of the GNU General Public License as published by
4e5a49c1eSTony Lindgren  * the Free Software Foundation; either version 2 of the License, or
5e5a49c1eSTony Lindgren  * (at your option) any later version.
6e5a49c1eSTony Lindgren  *
7e5a49c1eSTony Lindgren  * This program is distributed in the hope that it will be useful,
8e5a49c1eSTony Lindgren  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9e5a49c1eSTony Lindgren  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10e5a49c1eSTony Lindgren  * GNU General Public License for more details.
11e5a49c1eSTony Lindgren  */
12e5a49c1eSTony Lindgren 
13e5a49c1eSTony Lindgren #include <linux/kernel.h>
14e5a49c1eSTony Lindgren #include <linux/module.h>
15e5a49c1eSTony Lindgren #include <linux/of.h>
16e5a49c1eSTony Lindgren #include <linux/of_device.h>
17e5a49c1eSTony Lindgren #include <linux/regmap.h>
18e5a49c1eSTony Lindgren #include <linux/mfd/syscon.h>
19e5a49c1eSTony Lindgren 
20f86b4ae6SLad, Prabhakar #include "cpsw.h"
21f86b4ae6SLad, Prabhakar 
22b6745f6eSMugunthan V N #define CTRL_MAC_LO_REG(offset, id) ((offset) + 0x8 * (id))
23b6745f6eSMugunthan V N #define CTRL_MAC_HI_REG(offset, id) ((offset) + 0x8 * (id) + 0x4)
24e5a49c1eSTony Lindgren 
25b6745f6eSMugunthan V N static int davinci_emac_3517_get_macid(struct device *dev, u16 offset,
26b6745f6eSMugunthan V N 				       int slave, u8 *mac_addr)
27b6745f6eSMugunthan V N {
28b6745f6eSMugunthan V N 	u32 macid_lsb;
29b6745f6eSMugunthan V N 	u32 macid_msb;
30b6745f6eSMugunthan V N 	struct regmap *syscon;
31b6745f6eSMugunthan V N 
32b6745f6eSMugunthan V N 	syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
33b6745f6eSMugunthan V N 	if (IS_ERR(syscon)) {
34b6745f6eSMugunthan V N 		if (PTR_ERR(syscon) == -ENODEV)
35b6745f6eSMugunthan V N 			return 0;
36b6745f6eSMugunthan V N 		return PTR_ERR(syscon);
37b6745f6eSMugunthan V N 	}
38b6745f6eSMugunthan V N 
39b6745f6eSMugunthan V N 	regmap_read(syscon, CTRL_MAC_LO_REG(offset, slave), &macid_lsb);
40b6745f6eSMugunthan V N 	regmap_read(syscon, CTRL_MAC_HI_REG(offset, slave), &macid_msb);
41b6745f6eSMugunthan V N 
42b6745f6eSMugunthan V N 	mac_addr[0] = (macid_msb >> 16) & 0xff;
43b6745f6eSMugunthan V N 	mac_addr[1] = (macid_msb >> 8)  & 0xff;
44b6745f6eSMugunthan V N 	mac_addr[2] = macid_msb & 0xff;
45b6745f6eSMugunthan V N 	mac_addr[3] = (macid_lsb >> 16) & 0xff;
46b6745f6eSMugunthan V N 	mac_addr[4] = (macid_lsb >> 8)  & 0xff;
47b6745f6eSMugunthan V N 	mac_addr[5] = macid_lsb & 0xff;
48b6745f6eSMugunthan V N 
49b6745f6eSMugunthan V N 	return 0;
50b6745f6eSMugunthan V N }
51b6745f6eSMugunthan V N 
52b6745f6eSMugunthan V N static int cpsw_am33xx_cm_get_macid(struct device *dev, u16 offset, int slave,
53e5a49c1eSTony Lindgren 				    u8 *mac_addr)
54e5a49c1eSTony Lindgren {
55e5a49c1eSTony Lindgren 	u32 macid_lo;
56e5a49c1eSTony Lindgren 	u32 macid_hi;
57e5a49c1eSTony Lindgren 	struct regmap *syscon;
58e5a49c1eSTony Lindgren 
59e5a49c1eSTony Lindgren 	syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
60e5a49c1eSTony Lindgren 	if (IS_ERR(syscon)) {
61e5a49c1eSTony Lindgren 		if (PTR_ERR(syscon) == -ENODEV)
62e5a49c1eSTony Lindgren 			return 0;
63e5a49c1eSTony Lindgren 		return PTR_ERR(syscon);
64e5a49c1eSTony Lindgren 	}
65e5a49c1eSTony Lindgren 
66b6745f6eSMugunthan V N 	regmap_read(syscon, CTRL_MAC_LO_REG(offset, slave), &macid_lo);
67b6745f6eSMugunthan V N 	regmap_read(syscon, CTRL_MAC_HI_REG(offset, slave), &macid_hi);
68e5a49c1eSTony Lindgren 
69e5a49c1eSTony Lindgren 	mac_addr[5] = (macid_lo >> 8) & 0xff;
70e5a49c1eSTony Lindgren 	mac_addr[4] = macid_lo & 0xff;
71e5a49c1eSTony Lindgren 	mac_addr[3] = (macid_hi >> 24) & 0xff;
72e5a49c1eSTony Lindgren 	mac_addr[2] = (macid_hi >> 16) & 0xff;
73e5a49c1eSTony Lindgren 	mac_addr[1] = (macid_hi >> 8) & 0xff;
74e5a49c1eSTony Lindgren 	mac_addr[0] = macid_hi & 0xff;
75e5a49c1eSTony Lindgren 
76e5a49c1eSTony Lindgren 	return 0;
77e5a49c1eSTony Lindgren }
78b6745f6eSMugunthan V N 
79b6745f6eSMugunthan V N int ti_cm_get_macid(struct device *dev, int slave, u8 *mac_addr)
80b6745f6eSMugunthan V N {
81b6745f6eSMugunthan V N 	if (of_machine_is_compatible("ti,am33xx"))
82b6745f6eSMugunthan V N 		return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
83b6745f6eSMugunthan V N 
84b6745f6eSMugunthan V N 	if (of_device_is_compatible(dev->of_node, "ti,am3517-emac"))
85b6745f6eSMugunthan V N 		return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
86b6745f6eSMugunthan V N 
87b6745f6eSMugunthan V N 	if (of_device_is_compatible(dev->of_node, "ti,dm816-emac"))
88b6745f6eSMugunthan V N 		return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
89b6745f6eSMugunthan V N 
90b6745f6eSMugunthan V N 	dev_err(dev, "incompatible machine/device type for reading mac address\n");
91b6745f6eSMugunthan V N 	return -ENOENT;
92b6745f6eSMugunthan V N }
93b6745f6eSMugunthan V N EXPORT_SYMBOL_GPL(ti_cm_get_macid);
94e5a49c1eSTony Lindgren 
95e5a49c1eSTony Lindgren MODULE_LICENSE("GPL");
96