Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Marek Vasut <marex@denx.de> |
| 4 | * |
| 5 | * Altera SoCFPGA EMAC extras |
| 6 | */ |
| 7 | |
Chee Hong Ang | c0649b5 | 2020-12-24 18:21:05 +0800 | [diff] [blame] | 8 | #include <asm/arch/secure_reg_helper.h> |
| 9 | #include <asm/arch/system_manager.h> |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <dm.h> |
| 12 | #include <clk.h> |
| 13 | #include <phy.h> |
| 14 | #include <regmap.h> |
| 15 | #include <reset.h> |
| 16 | #include <syscon.h> |
| 17 | #include "designware.h" |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 20 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 21 | struct dwmac_socfpga_plat { |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 22 | struct dw_eth_pdata dw_eth_pdata; |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 23 | void *phy_intf; |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 24 | u32 reg_shift; |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 27 | static int dwmac_socfpga_of_to_plat(struct udevice *dev) |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 28 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 29 | struct dwmac_socfpga_plat *pdata = dev_get_plat(dev); |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 30 | struct regmap *regmap; |
| 31 | struct ofnode_phandle_args args; |
| 32 | void *range; |
| 33 | int ret; |
| 34 | |
| 35 | ret = dev_read_phandle_with_args(dev, "altr,sysmgr-syscon", NULL, |
| 36 | 2, 0, &args); |
| 37 | if (ret) { |
| 38 | dev_err(dev, "Failed to get syscon: %d\n", ret); |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | if (args.args_count != 2) { |
| 43 | dev_err(dev, "Invalid number of syscon args\n"); |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | |
| 47 | regmap = syscon_node_to_regmap(args.node); |
| 48 | if (IS_ERR(regmap)) { |
| 49 | ret = PTR_ERR(regmap); |
| 50 | dev_err(dev, "Failed to get regmap: %d\n", ret); |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | range = regmap_get_range(regmap, 0); |
| 55 | if (!range) { |
| 56 | dev_err(dev, "Failed to get regmap range\n"); |
| 57 | return -ENOMEM; |
| 58 | } |
| 59 | |
| 60 | pdata->phy_intf = range + args.args[0]; |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 61 | pdata->reg_shift = args.args[1]; |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 62 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 63 | return designware_eth_of_to_plat(dev); |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Chee Hong Ang | c0649b5 | 2020-12-24 18:21:05 +0800 | [diff] [blame] | 66 | static int dwmac_socfpga_do_setphy(struct udevice *dev, u32 modereg) |
| 67 | { |
| 68 | struct dwmac_socfpga_plat *pdata = dev_get_plat(dev); |
| 69 | u32 modemask = SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << pdata->reg_shift; |
| 70 | |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 71 | #if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_SPL_ATF) |
Chee Hong Ang | c0649b5 | 2020-12-24 18:21:05 +0800 | [diff] [blame] | 72 | u32 index = ((u64)pdata->phy_intf - socfpga_get_sysmgr_addr() - |
| 73 | SYSMGR_SOC64_EMAC0) >> 2; |
| 74 | |
| 75 | u32 id = SOCFPGA_SECURE_REG_SYSMGR_SOC64_EMAC0 + index; |
| 76 | |
| 77 | int ret = socfpga_secure_reg_update32(id, |
| 78 | modemask, |
| 79 | modereg << pdata->reg_shift); |
| 80 | if (ret) { |
| 81 | dev_err(dev, "Failed to set PHY register via SMC call\n"); |
| 82 | return ret; |
| 83 | } |
| 84 | #else |
| 85 | clrsetbits_le32(pdata->phy_intf, modemask, |
| 86 | modereg << pdata->reg_shift); |
| 87 | #endif |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 92 | static int dwmac_socfpga_probe(struct udevice *dev) |
| 93 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 94 | struct dwmac_socfpga_plat *pdata = dev_get_plat(dev); |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 95 | struct eth_pdata *edata = &pdata->dw_eth_pdata.eth_pdata; |
| 96 | struct reset_ctl_bulk reset_bulk; |
| 97 | int ret; |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 98 | u32 modereg; |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 99 | |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 100 | switch (edata->phy_interface) { |
| 101 | case PHY_INTERFACE_MODE_MII: |
| 102 | case PHY_INTERFACE_MODE_GMII: |
| 103 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII; |
| 104 | break; |
| 105 | case PHY_INTERFACE_MODE_RMII: |
| 106 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII; |
| 107 | break; |
| 108 | case PHY_INTERFACE_MODE_RGMII: |
| 109 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII; |
| 110 | break; |
| 111 | default: |
| 112 | dev_err(dev, "Unsupported PHY mode\n"); |
| 113 | return -EINVAL; |
| 114 | } |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 115 | |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 116 | ret = reset_get_bulk(dev, &reset_bulk); |
| 117 | if (ret) { |
| 118 | dev_err(dev, "Failed to get reset: %d\n", ret); |
| 119 | return ret; |
| 120 | } |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 121 | |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 122 | reset_assert_bulk(&reset_bulk); |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 123 | |
Chee Hong Ang | c0649b5 | 2020-12-24 18:21:05 +0800 | [diff] [blame] | 124 | ret = dwmac_socfpga_do_setphy(dev, modereg); |
| 125 | if (ret) |
| 126 | return ret; |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 127 | |
Simon Goldschmidt | b50afc8 | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 128 | reset_release_bulk(&reset_bulk); |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 129 | |
| 130 | return designware_eth_probe(dev); |
| 131 | } |
| 132 | |
| 133 | static const struct udevice_id dwmac_socfpga_ids[] = { |
| 134 | { .compatible = "altr,socfpga-stmmac" }, |
| 135 | { } |
| 136 | }; |
| 137 | |
| 138 | U_BOOT_DRIVER(dwmac_socfpga) = { |
| 139 | .name = "dwmac_socfpga", |
| 140 | .id = UCLASS_ETH, |
| 141 | .of_match = dwmac_socfpga_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 142 | .of_to_plat = dwmac_socfpga_of_to_plat, |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 143 | .probe = dwmac_socfpga_probe, |
| 144 | .ops = &designware_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 145 | .priv_auto = sizeof(struct dw_eth_dev), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 146 | .plat_auto = sizeof(struct dwmac_socfpga_plat), |
Marek Vasut | 0d9a4a0 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 147 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 148 | }; |