Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 2 | /* |
| 3 | * CPSW common - libs used across TI ethernet devices. |
| 4 | * |
| 5 | * Copyright (C) 2016, Texas Instruments, Incorporated |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Alex Kiernan | 9925f1d | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 10 | #include <environment.h> |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 11 | #include <fdt_support.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <cpsw.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | #define CTRL_MAC_REG(offset, id) ((offset) + 0x8 * (id)) |
| 18 | |
| 19 | static 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 Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 23 | int node = dev_of_offset(dev); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 24 | 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 Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 32 | pr_err("Syscon offset not found\n"); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 33 | 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 Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 39 | pr_err("Not able to get syscon address to get mac efuse address\n"); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 40 | 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 | |
| 59 | static 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 Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 63 | int node = dev_of_offset(dev); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 64 | 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 Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 72 | pr_err("Syscon offset not found\n"); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 73 | 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 Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 79 | pr_err("Not able to get syscon address to get mac efuse address\n"); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 80 | 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 | |
| 99 | int 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 Glass | 911f3ae | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 107 | if (device_is_compatible(dev, "ti,am3517-emac")) |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 108 | return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr); |
| 109 | |
Simon Glass | 911f3ae | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 110 | if (device_is_compatible(dev, "ti,dm816-emac")) |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 111 | return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr); |
| 112 | |
Lokesh Vutla | e9ced14 | 2017-02-01 09:20:49 +0530 | [diff] [blame] | 113 | if (of_machine_is_compatible("ti,am43")) |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 114 | 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 | } |