blob: a9e2d8c097254de1d5f537ad36742f5b96ac4ad6 [file] [log] [blame]
Marek Vasut0d9a4a02018-08-13 19:32:14 +02001// 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 Angc0649b52020-12-24 18:21:05 +08008#include <asm/arch/secure_reg_helper.h>
9#include <asm/arch/system_manager.h>
Marek Vasut0d9a4a02018-08-13 19:32:14 +020010#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 Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Marek Vasut0d9a4a02018-08-13 19:32:14 +020020
Simon Glassb75b15b2020-12-03 16:55:23 -070021struct dwmac_socfpga_plat {
Marek Vasut0d9a4a02018-08-13 19:32:14 +020022 struct dw_eth_pdata dw_eth_pdata;
Marek Vasut0d9a4a02018-08-13 19:32:14 +020023 void *phy_intf;
Simon Goldschmidtb50afc82019-01-13 19:58:40 +010024 u32 reg_shift;
Marek Vasut0d9a4a02018-08-13 19:32:14 +020025};
26
Simon Glassaad29ae2020-12-03 16:55:21 -070027static int dwmac_socfpga_of_to_plat(struct udevice *dev)
Marek Vasut0d9a4a02018-08-13 19:32:14 +020028{
Simon Glassb75b15b2020-12-03 16:55:23 -070029 struct dwmac_socfpga_plat *pdata = dev_get_plat(dev);
Marek Vasut0d9a4a02018-08-13 19:32:14 +020030 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 Goldschmidtb50afc82019-01-13 19:58:40 +010061 pdata->reg_shift = args.args[1];
Marek Vasut0d9a4a02018-08-13 19:32:14 +020062
Simon Glassaad29ae2020-12-03 16:55:21 -070063 return designware_eth_of_to_plat(dev);
Marek Vasut0d9a4a02018-08-13 19:32:14 +020064}
65
Chee Hong Angc0649b52020-12-24 18:21:05 +080066static 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 Glass7ec24132024-09-29 19:49:48 -060071#if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_SPL_ATF)
Chee Hong Angc0649b52020-12-24 18:21:05 +080072 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 Vasut0d9a4a02018-08-13 19:32:14 +020092static int dwmac_socfpga_probe(struct udevice *dev)
93{
Simon Glassb75b15b2020-12-03 16:55:23 -070094 struct dwmac_socfpga_plat *pdata = dev_get_plat(dev);
Marek Vasut0d9a4a02018-08-13 19:32:14 +020095 struct eth_pdata *edata = &pdata->dw_eth_pdata.eth_pdata;
96 struct reset_ctl_bulk reset_bulk;
97 int ret;
Simon Goldschmidtb50afc82019-01-13 19:58:40 +010098 u32 modereg;
Marek Vasut0d9a4a02018-08-13 19:32:14 +020099
Simon Goldschmidtb50afc82019-01-13 19:58:40 +0100100 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 Vasut0d9a4a02018-08-13 19:32:14 +0200115
Simon Goldschmidtb50afc82019-01-13 19:58:40 +0100116 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 Vasut0d9a4a02018-08-13 19:32:14 +0200121
Simon Goldschmidtb50afc82019-01-13 19:58:40 +0100122 reset_assert_bulk(&reset_bulk);
Marek Vasut0d9a4a02018-08-13 19:32:14 +0200123
Chee Hong Angc0649b52020-12-24 18:21:05 +0800124 ret = dwmac_socfpga_do_setphy(dev, modereg);
125 if (ret)
126 return ret;
Marek Vasut0d9a4a02018-08-13 19:32:14 +0200127
Simon Goldschmidtb50afc82019-01-13 19:58:40 +0100128 reset_release_bulk(&reset_bulk);
Marek Vasut0d9a4a02018-08-13 19:32:14 +0200129
130 return designware_eth_probe(dev);
131}
132
133static const struct udevice_id dwmac_socfpga_ids[] = {
134 { .compatible = "altr,socfpga-stmmac" },
135 { }
136};
137
138U_BOOT_DRIVER(dwmac_socfpga) = {
139 .name = "dwmac_socfpga",
140 .id = UCLASS_ETH,
141 .of_match = dwmac_socfpga_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700142 .of_to_plat = dwmac_socfpga_of_to_plat,
Marek Vasut0d9a4a02018-08-13 19:32:14 +0200143 .probe = dwmac_socfpga_probe,
144 .ops = &designware_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700145 .priv_auto = sizeof(struct dw_eth_dev),
Simon Glassb75b15b2020-12-03 16:55:23 -0700146 .plat_auto = sizeof(struct dwmac_socfpga_plat),
Marek Vasut0d9a4a02018-08-13 19:32:14 +0200147 .flags = DM_FLAG_ALLOC_PRIV_DMA,
148};