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" |
| 17 | |
| 18 | #include <asm/arch/system_manager.h> |
| 19 | |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 20 | struct dwmac_socfpga_platdata { |
| 21 | struct dw_eth_pdata dw_eth_pdata; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 22 | void *phy_intf; |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 23 | u32 reg_shift; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static int dwmac_socfpga_ofdata_to_platdata(struct udevice *dev) |
| 27 | { |
| 28 | struct dwmac_socfpga_platdata *pdata = dev_get_platdata(dev); |
| 29 | struct regmap *regmap; |
| 30 | struct ofnode_phandle_args args; |
| 31 | void *range; |
| 32 | int ret; |
| 33 | |
| 34 | ret = dev_read_phandle_with_args(dev, "altr,sysmgr-syscon", NULL, |
| 35 | 2, 0, &args); |
| 36 | if (ret) { |
| 37 | dev_err(dev, "Failed to get syscon: %d\n", ret); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | if (args.args_count != 2) { |
| 42 | dev_err(dev, "Invalid number of syscon args\n"); |
| 43 | return -EINVAL; |
| 44 | } |
| 45 | |
| 46 | regmap = syscon_node_to_regmap(args.node); |
| 47 | if (IS_ERR(regmap)) { |
| 48 | ret = PTR_ERR(regmap); |
| 49 | dev_err(dev, "Failed to get regmap: %d\n", ret); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | range = regmap_get_range(regmap, 0); |
| 54 | if (!range) { |
| 55 | dev_err(dev, "Failed to get regmap range\n"); |
| 56 | return -ENOMEM; |
| 57 | } |
| 58 | |
| 59 | pdata->phy_intf = range + args.args[0]; |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 60 | pdata->reg_shift = args.args[1]; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 61 | |
| 62 | return designware_eth_ofdata_to_platdata(dev); |
| 63 | } |
| 64 | |
| 65 | static int dwmac_socfpga_probe(struct udevice *dev) |
| 66 | { |
| 67 | struct dwmac_socfpga_platdata *pdata = dev_get_platdata(dev); |
| 68 | struct eth_pdata *edata = &pdata->dw_eth_pdata.eth_pdata; |
| 69 | struct reset_ctl_bulk reset_bulk; |
| 70 | int ret; |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 71 | u32 modereg; |
| 72 | u32 modemask; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 73 | |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 74 | switch (edata->phy_interface) { |
| 75 | case PHY_INTERFACE_MODE_MII: |
| 76 | case PHY_INTERFACE_MODE_GMII: |
| 77 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII; |
| 78 | break; |
| 79 | case PHY_INTERFACE_MODE_RMII: |
| 80 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII; |
| 81 | break; |
| 82 | case PHY_INTERFACE_MODE_RGMII: |
| 83 | modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII; |
| 84 | break; |
| 85 | default: |
| 86 | dev_err(dev, "Unsupported PHY mode\n"); |
| 87 | return -EINVAL; |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Simon Goldschmidt | 4f1267c | 2019-01-13 19:58:40 +0100 | [diff] [blame] | 90 | ret = reset_get_bulk(dev, &reset_bulk); |
| 91 | if (ret) { |
| 92 | dev_err(dev, "Failed to get reset: %d\n", ret); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | reset_assert_bulk(&reset_bulk); |
| 97 | |
| 98 | modemask = SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << pdata->reg_shift; |
| 99 | clrsetbits_le32(pdata->phy_intf, modemask, |
| 100 | modereg << pdata->reg_shift); |
| 101 | |
| 102 | reset_release_bulk(&reset_bulk); |
| 103 | |
Marek Vasut | 215a065 | 2018-08-13 19:32:14 +0200 | [diff] [blame] | 104 | return designware_eth_probe(dev); |
| 105 | } |
| 106 | |
| 107 | static const struct udevice_id dwmac_socfpga_ids[] = { |
| 108 | { .compatible = "altr,socfpga-stmmac" }, |
| 109 | { } |
| 110 | }; |
| 111 | |
| 112 | U_BOOT_DRIVER(dwmac_socfpga) = { |
| 113 | .name = "dwmac_socfpga", |
| 114 | .id = UCLASS_ETH, |
| 115 | .of_match = dwmac_socfpga_ids, |
| 116 | .ofdata_to_platdata = dwmac_socfpga_ofdata_to_platdata, |
| 117 | .probe = dwmac_socfpga_probe, |
| 118 | .ops = &designware_eth_ops, |
| 119 | .priv_auto_alloc_size = sizeof(struct dw_eth_dev), |
| 120 | .platdata_auto_alloc_size = sizeof(struct dwmac_socfpga_platdata), |
| 121 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 122 | }; |