1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Adaptrum Anarion DWMAC glue layer
5 * Copyright (C) 2017, Adaptrum, Inc.
6 * (Written by Alexandru Gagniuc <alex.g at adaptrum.com> for Adaptrum, Inc.)
11 #include <linux/of_net.h>
12 #include <linux/stmmac.h>
15 #include "stmmac_platform.h"
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)
24 uint32_t phy_intf_sel;
27 static uint32_t gmac_read_reg(struct anarion_gmac *gmac, uint8_t reg)
29 return readl((void *)(gmac->ctl_block + reg));
32 static void gmac_write_reg(struct anarion_gmac *gmac, uint8_t reg, uint32_t val)
34 writel(val, (void *)(gmac->ctl_block + reg));
37 static int anarion_gmac_init(struct platform_device *pdev, void *priv)
40 struct anarion_gmac *gmac = priv;
42 /* Reset logic, configure interface mode, then release reset. SIMPLE! */
43 gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1);
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);
50 gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 0);
55 static void anarion_gmac_exit(struct platform_device *pdev, void *priv)
57 struct anarion_gmac *gmac = priv;
59 gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1);
62 static struct anarion_gmac *anarion_config_dt(struct platform_device *pdev)
64 struct anarion_gmac *gmac;
65 phy_interface_t phy_mode;
66 void __iomem *ctl_block;
69 ctl_block = devm_platform_ioremap_resource(pdev, 1);
70 if (IS_ERR(ctl_block)) {
71 dev_err(&pdev->dev, "Cannot get reset region (%ld)!\n",
76 gmac = devm_kzalloc(&pdev->dev, sizeof(*gmac), GFP_KERNEL);
78 return ERR_PTR(-ENOMEM);
80 gmac->ctl_block = (uintptr_t)ctl_block;
82 err = of_get_phy_mode(pdev->dev.of_node, &phy_mode);
87 case PHY_INTERFACE_MODE_RGMII:
89 case PHY_INTERFACE_MODE_RGMII_ID:
90 case PHY_INTERFACE_MODE_RGMII_RXID:
91 case PHY_INTERFACE_MODE_RGMII_TXID:
92 gmac->phy_intf_sel = GMAC_CONFIG_INTF_RGMII;
95 dev_err(&pdev->dev, "Unsupported phy-mode (%d)\n",
97 return ERR_PTR(-ENOTSUPP);
103 static int anarion_dwmac_probe(struct platform_device *pdev)
106 struct anarion_gmac *gmac;
107 struct plat_stmmacenet_data *plat_dat;
108 struct stmmac_resources stmmac_res;
110 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
114 gmac = anarion_config_dt(pdev);
116 return PTR_ERR(gmac);
118 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
119 if (IS_ERR(plat_dat))
120 return PTR_ERR(plat_dat);
122 plat_dat->init = anarion_gmac_init;
123 plat_dat->exit = anarion_gmac_exit;
124 anarion_gmac_init(pdev, gmac);
125 plat_dat->bsp_priv = gmac;
127 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
129 stmmac_remove_config_dt(pdev, plat_dat);
136 static const struct of_device_id anarion_dwmac_match[] = {
137 { .compatible = "adaptrum,anarion-gmac" },
140 MODULE_DEVICE_TABLE(of, anarion_dwmac_match);
142 static struct platform_driver anarion_dwmac_driver = {
143 .probe = anarion_dwmac_probe,
144 .remove = stmmac_pltfr_remove,
146 .name = "anarion-dwmac",
147 .pm = &stmmac_pltfr_pm_ops,
148 .of_match_table = anarion_dwmac_match,
151 module_platform_driver(anarion_dwmac_driver);
153 MODULE_DESCRIPTION("Adaptrum Anarion DWMAC specific glue layer");
154 MODULE_AUTHOR("Alexandru Gagniuc <mr.nuke.me@gmail.com>");
155 MODULE_LICENSE("GPL v2");