blob: ac12cfe9b86bd989777943915c5e1f34f94d7a00 [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
Faiz Abbasa58d2222019-03-18 13:54:34 +053019static void davinci_emac_3517_get_macid(u32 addr, u8 *mac_addr)
Mugunthan V Ne4310562016-04-28 15:36:07 +053020{
Mugunthan V Ne4310562016-04-28 15:36:07 +053021 /* try reading mac address from efuse */
Faiz Abbasa58d2222019-03-18 13:54:34 +053022 u32 macid_lsb = readl(addr);
23 u32 macid_msb = readl(addr + 4);
Mugunthan V Ne4310562016-04-28 15:36:07 +053024
25 mac_addr[0] = (macid_msb >> 16) & 0xff;
26 mac_addr[1] = (macid_msb >> 8) & 0xff;
27 mac_addr[2] = macid_msb & 0xff;
28 mac_addr[3] = (macid_lsb >> 16) & 0xff;
29 mac_addr[4] = (macid_lsb >> 8) & 0xff;
30 mac_addr[5] = macid_lsb & 0xff;
Mugunthan V Ne4310562016-04-28 15:36:07 +053031}
32
Faiz Abbasa58d2222019-03-18 13:54:34 +053033static void cpsw_am33xx_cm_get_macid(u32 addr, u8 *mac_addr)
Mugunthan V Ne4310562016-04-28 15:36:07 +053034{
Mugunthan V Ne4310562016-04-28 15:36:07 +053035 /* try reading mac address from efuse */
Faiz Abbasa58d2222019-03-18 13:54:34 +053036 u32 macid_lo = readl(addr);
37 u32 macid_hi = readl(addr + 4);
Mugunthan V Ne4310562016-04-28 15:36:07 +053038
39 mac_addr[5] = (macid_lo >> 8) & 0xff;
40 mac_addr[4] = macid_lo & 0xff;
41 mac_addr[3] = (macid_hi >> 24) & 0xff;
42 mac_addr[2] = (macid_hi >> 16) & 0xff;
43 mac_addr[1] = (macid_hi >> 8) & 0xff;
44 mac_addr[0] = macid_hi & 0xff;
Mugunthan V Ne4310562016-04-28 15:36:07 +053045}
46
Faiz Abbasa58d2222019-03-18 13:54:34 +053047void ti_cm_get_macid(struct udevice *dev, struct cpsw_platform_data *data,
48 u8 *mac_addr)
Mugunthan V Ne4310562016-04-28 15:36:07 +053049{
Faiz Abbasa58d2222019-03-18 13:54:34 +053050 if (!strcmp(data->macid_sel_compat, "cpsw,am33xx"))
51 cpsw_am33xx_cm_get_macid(data->syscon_addr, mac_addr);
52 else if (!strcmp(data->macid_sel_compat, "davinci,emac"))
53 davinci_emac_3517_get_macid(data->syscon_addr, mac_addr);
54}
Mugunthan V Ne4310562016-04-28 15:36:07 +053055
Faiz Abbasa58d2222019-03-18 13:54:34 +053056int ti_cm_get_macid_addr(struct udevice *dev, int slave,
57 struct cpsw_platform_data *data)
58{
59 void *fdt = (void *)gd->fdt_blob;
60 int node = dev_of_offset(dev);
61 fdt32_t gmii = 0;
62 int syscon;
63 u16 offset;
Mugunthan V Ne4310562016-04-28 15:36:07 +053064
Faiz Abbasa58d2222019-03-18 13:54:34 +053065 if (of_machine_is_compatible("ti,dm8148")) {
66 offset = 0x630;
67 data->macid_sel_compat = "cpsw,am33xx";
68 } else if (of_machine_is_compatible("ti,am33xx")) {
69 offset = 0x630;
70 data->macid_sel_compat = "cpsw,am33xx";
71 } else if (device_is_compatible(dev, "ti,am3517-emac")) {
72 offset = 0x110;
73 data->macid_sel_compat = "davinci,emac";
74 } else if (device_is_compatible(dev, "ti,dm816-emac")) {
75 offset = 0x30;
76 data->macid_sel_compat = "cpsw,am33xx";
77 } else if (of_machine_is_compatible("ti,am43")) {
78 offset = 0x630;
79 data->macid_sel_compat = "cpsw,am33xx";
80 } else if (of_machine_is_compatible("ti,dra7")) {
81 offset = 0x514;
82 data->macid_sel_compat = "davinci,emac";
83 } else {
84 dev_err(dev, "incompatible machine/device type for reading mac address\n");
85 return -ENOENT;
86 }
Mugunthan V Ne4310562016-04-28 15:36:07 +053087
Faiz Abbasa58d2222019-03-18 13:54:34 +053088 syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
89 if (syscon < 0) {
90 pr_err("Syscon offset not found\n");
91 return -ENOENT;
92 }
Mugunthan V Ne4310562016-04-28 15:36:07 +053093
Faiz Abbasa58d2222019-03-18 13:54:34 +053094 data->syscon_addr = (u32)map_physmem(fdt_translate_address(fdt, syscon,
95 &gmii),
96 sizeof(u32), MAP_NOCACHE);
97 if (data->syscon_addr == FDT_ADDR_T_NONE) {
98 pr_err("Not able to get syscon address to get mac efuse address\n");
99 return -ENOENT;
100 }
Mugunthan V Ne4310562016-04-28 15:36:07 +0530101
Faiz Abbasa58d2222019-03-18 13:54:34 +0530102 data->syscon_addr += CTRL_MAC_REG(offset, slave);
Mugunthan V Ne4310562016-04-28 15:36:07 +0530103
Faiz Abbasa58d2222019-03-18 13:54:34 +0530104 return 0;
105
Mugunthan V Ne4310562016-04-28 15:36:07 +0530106}