blob: 7bd312a6c045567ffa55b98be42f0f5a8d3145d5 [file] [log] [blame]
Mugunthan V N9e556352016-04-28 15:36:07 +05301/*
2 * CPSW common - libs used across TI ethernet devices.
3 *
4 * Copyright (C) 2016, Texas Instruments, Incorporated
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <dm.h>
Alex Kiernan9c215492018-04-01 09:22:38 +000011#include <environment.h>
Mugunthan V N9e556352016-04-28 15:36:07 +053012#include <fdt_support.h>
13#include <asm/io.h>
14#include <cpsw.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18#define CTRL_MAC_REG(offset, id) ((offset) + 0x8 * (id))
19
20static int davinci_emac_3517_get_macid(struct udevice *dev, u16 offset,
21 int slave, u8 *mac_addr)
22{
23 void *fdt = (void *)gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070024 int node = dev_of_offset(dev);
Mugunthan V N9e556352016-04-28 15:36:07 +053025 u32 macid_lsb;
26 u32 macid_msb;
27 fdt32_t gmii = 0;
28 int syscon;
29 u32 addr;
30
31 syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
32 if (syscon < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090033 pr_err("Syscon offset not found\n");
Mugunthan V N9e556352016-04-28 15:36:07 +053034 return -ENOENT;
35 }
36
37 addr = (u32)map_physmem(fdt_translate_address(fdt, syscon, &gmii),
38 sizeof(u32), MAP_NOCACHE);
39 if (addr == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090040 pr_err("Not able to get syscon address to get mac efuse address\n");
Mugunthan V N9e556352016-04-28 15:36:07 +053041 return -ENOENT;
42 }
43
44 addr += CTRL_MAC_REG(offset, slave);
45
46 /* try reading mac address from efuse */
47 macid_lsb = readl(addr);
48 macid_msb = readl(addr + 4);
49
50 mac_addr[0] = (macid_msb >> 16) & 0xff;
51 mac_addr[1] = (macid_msb >> 8) & 0xff;
52 mac_addr[2] = macid_msb & 0xff;
53 mac_addr[3] = (macid_lsb >> 16) & 0xff;
54 mac_addr[4] = (macid_lsb >> 8) & 0xff;
55 mac_addr[5] = macid_lsb & 0xff;
56
57 return 0;
58}
59
60static int cpsw_am33xx_cm_get_macid(struct udevice *dev, u16 offset, int slave,
61 u8 *mac_addr)
62{
63 void *fdt = (void *)gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070064 int node = dev_of_offset(dev);
Mugunthan V N9e556352016-04-28 15:36:07 +053065 u32 macid_lo;
66 u32 macid_hi;
67 fdt32_t gmii = 0;
68 int syscon;
69 u32 addr;
70
71 syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
72 if (syscon < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090073 pr_err("Syscon offset not found\n");
Mugunthan V N9e556352016-04-28 15:36:07 +053074 return -ENOENT;
75 }
76
77 addr = (u32)map_physmem(fdt_translate_address(fdt, syscon, &gmii),
78 sizeof(u32), MAP_NOCACHE);
79 if (addr == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090080 pr_err("Not able to get syscon address to get mac efuse address\n");
Mugunthan V N9e556352016-04-28 15:36:07 +053081 return -ENOENT;
82 }
83
84 addr += CTRL_MAC_REG(offset, slave);
85
86 /* try reading mac address from efuse */
87 macid_lo = readl(addr);
88 macid_hi = readl(addr + 4);
89
90 mac_addr[5] = (macid_lo >> 8) & 0xff;
91 mac_addr[4] = macid_lo & 0xff;
92 mac_addr[3] = (macid_hi >> 24) & 0xff;
93 mac_addr[2] = (macid_hi >> 16) & 0xff;
94 mac_addr[1] = (macid_hi >> 8) & 0xff;
95 mac_addr[0] = macid_hi & 0xff;
96
97 return 0;
98}
99
100int ti_cm_get_macid(struct udevice *dev, int slave, u8 *mac_addr)
101{
102 if (of_machine_is_compatible("ti,dm8148"))
103 return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
104
105 if (of_machine_is_compatible("ti,am33xx"))
106 return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
107
Simon Glass54cbcc82017-05-18 20:08:57 -0600108 if (device_is_compatible(dev, "ti,am3517-emac"))
Mugunthan V N9e556352016-04-28 15:36:07 +0530109 return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
110
Simon Glass54cbcc82017-05-18 20:08:57 -0600111 if (device_is_compatible(dev, "ti,dm816-emac"))
Mugunthan V N9e556352016-04-28 15:36:07 +0530112 return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
113
Lokesh Vutlaed75d362017-02-01 09:20:49 +0530114 if (of_machine_is_compatible("ti,am43"))
Mugunthan V N9e556352016-04-28 15:36:07 +0530115 return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
116
117 if (of_machine_is_compatible("ti,dra7"))
118 return davinci_emac_3517_get_macid(dev, 0x514, slave, mac_addr);
119
120 dev_err(dev, "incompatible machine/device type for reading mac address\n");
121 return -ENOENT;
122}