Marek Vasut | 215a065 | 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 | |
| 8 | #include <common.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <dm.h> |
| 11 | #include <clk.h> |
| 12 | #include <phy.h> |
| 13 | #include <regmap.h> |
| 14 | #include <reset.h> |
| 15 | #include <syscon.h> |
| 16 | #include "designware.h" |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 17 | #include <linux/err.h> |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 18 | |
| 19 | #include <asm/arch/system_manager.h> |
| 20 | |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 21 | struct dwmac_socfpga_platdata { |
| 22 | struct dw_eth_pdata dw_eth_pdata; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 23 | void *phy_intf; |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 24 | u32 reg_shift; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | static int dwmac_socfpga_ofdata_to_platdata(struct udevice *dev) |
| 28 | { |
| 29 | struct dwmac_socfpga_platdata *pdata = dev_get_platdata(dev); |
| 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 | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 61 | pdata->reg_shift = args.args[1]; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 62 | |
| 63 | return designware_eth_ofdata_to_platdata(dev); |
| 64 | } |
| 65 | |
| 66 | static int dwmac_socfpga_probe(struct udevice *dev) |
| 67 | { |
| 68 | struct dwmac_socfpga_platdata *pdata = dev_get_platdata(dev); |
| 69 | struct eth_pdata *edata = &pdata->dw_eth_pdata.eth_pdata; |
| 70 | struct reset_ctl_bulk reset_bulk; |
| 71 | int ret; |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 72 | u32 modereg; |
| 73 | u32 modemask; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 74 | |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 75 | switch (edata->phy_interface) { |
| 76 | case PHY_INTERFACE_MODE_MII: |
| 77 | case PHY_INTERFACE_MODE_GMII: |
| 78 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII; |
| 79 | break; |
| 80 | case PHY_INTERFACE_MODE_RMII: |
| 81 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII; |
| 82 | break; |
| 83 | case PHY_INTERFACE_MODE_RGMII: |
| 84 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII; |
| 85 | break; |
| 86 | default: |
| 87 | dev_err(dev, "Unsupported PHY mode\n"); |
| 88 | return -EINVAL; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 91 | ret = reset_get_bulk(dev, &reset_bulk); |
| 92 | if (ret) { |
| 93 | dev_err(dev, "Failed to get reset: %d\n", ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | reset_assert_bulk(&reset_bulk); |
| 98 | |
| 99 | modemask = SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << pdata->reg_shift; |
| 100 | clrsetbits_le32(pdata->phy_intf, modemask, |
| 101 | modereg << pdata->reg_shift); |
| 102 | |
| 103 | reset_release_bulk(&reset_bulk); |
| 104 | |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 105 | return designware_eth_probe(dev); |
| 106 | } |
| 107 | |
| 108 | static const struct udevice_id dwmac_socfpga_ids[] = { |
| 109 | { .compatible = "altr,socfpga-stmmac" }, |
| 110 | { } |
| 111 | }; |
| 112 | |
| 113 | U_BOOT_DRIVER(dwmac_socfpga) = { |
| 114 | .name = "dwmac_socfpga", |
| 115 | .id = UCLASS_ETH, |
| 116 | .of_match = dwmac_socfpga_ids, |
| 117 | .ofdata_to_platdata = dwmac_socfpga_ofdata_to_platdata, |
| 118 | .probe = dwmac_socfpga_probe, |
| 119 | .ops = &designware_eth_ops, |
| 120 | .priv_auto_alloc_size = sizeof(struct dw_eth_dev), |
| 121 | .platdata_auto_alloc_size = sizeof(struct dwmac_socfpga_platdata), |
| 122 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 123 | }; |