Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1 | /* |
| 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 Kiernan | 9c21549 | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 11 | #include <environment.h> |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 12 | #include <fdt_support.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <cpsw.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | #define CTRL_MAC_REG(offset, id) ((offset) + 0x8 * (id)) |
| 19 | |
| 20 | static 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 Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 24 | int node = dev_of_offset(dev); |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 25 | 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 Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 33 | pr_err("Syscon offset not found\n"); |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 34 | 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 Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 40 | pr_err("Not able to get syscon address to get mac efuse address\n"); |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 41 | 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 | |
| 60 | static 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 Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 64 | int node = dev_of_offset(dev); |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 65 | 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 Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 73 | pr_err("Syscon offset not found\n"); |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 74 | 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 Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 80 | pr_err("Not able to get syscon address to get mac efuse address\n"); |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 81 | 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 | |
| 100 | int 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 Glass | 54cbcc8 | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 108 | if (device_is_compatible(dev, "ti,am3517-emac")) |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 109 | return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr); |
| 110 | |
Simon Glass | 54cbcc8 | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 111 | if (device_is_compatible(dev, "ti,dm816-emac")) |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 112 | return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr); |
| 113 | |
Lokesh Vutla | ed75d36 | 2017-02-01 09:20:49 +0530 | [diff] [blame] | 114 | if (of_machine_is_compatible("ti,am43")) |
Mugunthan V N | 9e55635 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 115 | 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 | } |