blob: 6c8ddbd93618b913e9c2eec08032f671b049f438 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mugunthan V Ne4310562016-04-28 15:36:07 +05302/*
3 * CPSW common - libs used across TI ethernet devices.
4 *
5 * Copyright (C) 2016, Texas Instruments, Incorporated
Mugunthan V Ne4310562016-04-28 15:36:07 +05306 */
7
8#include <common.h>
9#include <dm.h>
Alex Kiernan9925f1d2018-04-01 09:22:38 +000010#include <environment.h>
Mugunthan V Ne4310562016-04-28 15:36:07 +053011#include <fdt_support.h>
12#include <asm/io.h>
13#include <cpsw.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17#define CTRL_MAC_REG(offset, id) ((offset) + 0x8 * (id))
18
19static 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;
Simon Glasse160f7d2017-01-17 16:52:55 -070023 int node = dev_of_offset(dev);
Mugunthan V Ne4310562016-04-28 15:36:07 +053024 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) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090032 pr_err("Syscon offset not found\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +053033 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) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090039 pr_err("Not able to get syscon address to get mac efuse address\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +053040 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
59static 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;
Simon Glasse160f7d2017-01-17 16:52:55 -070063 int node = dev_of_offset(dev);
Mugunthan V Ne4310562016-04-28 15:36:07 +053064 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) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090072 pr_err("Syscon offset not found\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +053073 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) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090079 pr_err("Not able to get syscon address to get mac efuse address\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +053080 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
99int 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
Simon Glass911f3ae2017-05-18 20:08:57 -0600107 if (device_is_compatible(dev, "ti,am3517-emac"))
Mugunthan V Ne4310562016-04-28 15:36:07 +0530108 return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
109
Simon Glass911f3ae2017-05-18 20:08:57 -0600110 if (device_is_compatible(dev, "ti,dm816-emac"))
Mugunthan V Ne4310562016-04-28 15:36:07 +0530111 return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
112
Lokesh Vutlae9ced142017-02-01 09:20:49 +0530113 if (of_machine_is_compatible("ti,am43"))
Mugunthan V Ne4310562016-04-28 15:36:07 +0530114 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}