1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Adaptrum Anarion DWMAC glue layer 4 * 5 * Copyright (C) 2017, Adaptrum, Inc. 6 * (Written by Alexandru Gagniuc <alex.g at adaptrum.com> for Adaptrum, Inc.) 7 */ 8 9 #include <linux/io.h> 10 #include <linux/of.h> 11 #include <linux/of_net.h> 12 #include <linux/stmmac.h> 13 14 #include "stmmac.h" 15 #include "stmmac_platform.h" 16 17 #define GMAC_RESET_CONTROL_REG 0 18 #define GMAC_SW_CONFIG_REG 4 19 #define GMAC_CONFIG_INTF_SEL_MASK (0x7 << 0) 20 #define GMAC_CONFIG_INTF_RGMII (0x1 << 0) 21 22 struct anarion_gmac { 23 uintptr_t ctl_block; 24 uint32_t phy_intf_sel; 25 }; 26 27 static uint32_t gmac_read_reg(struct anarion_gmac *gmac, uint8_t reg) 28 { 29 return readl((void *)(gmac->ctl_block + reg)); 30 }; 31 32 static void gmac_write_reg(struct anarion_gmac *gmac, uint8_t reg, uint32_t val) 33 { 34 writel(val, (void *)(gmac->ctl_block + reg)); 35 } 36 37 static int anarion_gmac_init(struct platform_device *pdev, void *priv) 38 { 39 uint32_t sw_config; 40 struct anarion_gmac *gmac = priv; 41 42 /* Reset logic, configure interface mode, then release reset. SIMPLE! */ 43 gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1); 44 45 sw_config = gmac_read_reg(gmac, GMAC_SW_CONFIG_REG); 46 sw_config &= ~GMAC_CONFIG_INTF_SEL_MASK; 47 sw_config |= (gmac->phy_intf_sel & GMAC_CONFIG_INTF_SEL_MASK); 48 gmac_write_reg(gmac, GMAC_SW_CONFIG_REG, sw_config); 49 50 gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 0); 51 52 return 0; 53 } 54 55 static void anarion_gmac_exit(struct platform_device *pdev, void *priv) 56 { 57 struct anarion_gmac *gmac = priv; 58 59 gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1); 60 } 61 62 static struct anarion_gmac *anarion_config_dt(struct platform_device *pdev) 63 { 64 int phy_mode; 65 void __iomem *ctl_block; 66 struct anarion_gmac *gmac; 67 68 ctl_block = devm_platform_ioremap_resource(pdev, 1); 69 if (IS_ERR(ctl_block)) { 70 dev_err(&pdev->dev, "Cannot get reset region (%ld)!\n", 71 PTR_ERR(ctl_block)); 72 return ctl_block; 73 } 74 75 gmac = devm_kzalloc(&pdev->dev, sizeof(*gmac), GFP_KERNEL); 76 if (!gmac) 77 return ERR_PTR(-ENOMEM); 78 79 gmac->ctl_block = (uintptr_t)ctl_block; 80 81 phy_mode = of_get_phy_mode(pdev->dev.of_node); 82 switch (phy_mode) { 83 case PHY_INTERFACE_MODE_RGMII: /* Fall through */ 84 case PHY_INTERFACE_MODE_RGMII_ID /* Fall through */: 85 case PHY_INTERFACE_MODE_RGMII_RXID: /* Fall through */ 86 case PHY_INTERFACE_MODE_RGMII_TXID: 87 gmac->phy_intf_sel = GMAC_CONFIG_INTF_RGMII; 88 break; 89 default: 90 dev_err(&pdev->dev, "Unsupported phy-mode (%d)\n", 91 phy_mode); 92 return ERR_PTR(-ENOTSUPP); 93 } 94 95 return gmac; 96 } 97 98 static int anarion_dwmac_probe(struct platform_device *pdev) 99 { 100 int ret; 101 struct anarion_gmac *gmac; 102 struct plat_stmmacenet_data *plat_dat; 103 struct stmmac_resources stmmac_res; 104 105 ret = stmmac_get_platform_resources(pdev, &stmmac_res); 106 if (ret) 107 return ret; 108 109 gmac = anarion_config_dt(pdev); 110 if (IS_ERR(gmac)) 111 return PTR_ERR(gmac); 112 113 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); 114 if (IS_ERR(plat_dat)) 115 return PTR_ERR(plat_dat); 116 117 plat_dat->init = anarion_gmac_init; 118 plat_dat->exit = anarion_gmac_exit; 119 anarion_gmac_init(pdev, gmac); 120 plat_dat->bsp_priv = gmac; 121 122 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); 123 if (ret) { 124 stmmac_remove_config_dt(pdev, plat_dat); 125 return ret; 126 } 127 128 return 0; 129 } 130 131 static const struct of_device_id anarion_dwmac_match[] = { 132 { .compatible = "adaptrum,anarion-gmac" }, 133 { } 134 }; 135 MODULE_DEVICE_TABLE(of, anarion_dwmac_match); 136 137 static struct platform_driver anarion_dwmac_driver = { 138 .probe = anarion_dwmac_probe, 139 .remove = stmmac_pltfr_remove, 140 .driver = { 141 .name = "anarion-dwmac", 142 .pm = &stmmac_pltfr_pm_ops, 143 .of_match_table = anarion_dwmac_match, 144 }, 145 }; 146 module_platform_driver(anarion_dwmac_driver); 147 148 MODULE_DESCRIPTION("Adaptrum Anarion DWMAC specific glue layer"); 149 MODULE_AUTHOR("Alexandru Gagniuc <mr.nuke.me@gmail.com>"); 150 MODULE_LICENSE("GPL v2"); 151