1 /*
2  * Amlogic Meson DWMAC glue layer
3  *
4  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * You should have received a copy of the GNU General Public License
11  * along with this program. If not, see <http://www.gnu.org/licenses/>.
12  */
13 
14 #include <linux/device.h>
15 #include <linux/ethtool.h>
16 #include <linux/io.h>
17 #include <linux/ioport.h>
18 #include <linux/platform_device.h>
19 #include <linux/stmmac.h>
20 
21 #include "stmmac_platform.h"
22 
23 #define ETHMAC_SPEED_100	BIT(1)
24 
25 struct meson_dwmac {
26 	struct device	*dev;
27 	void __iomem	*reg;
28 };
29 
30 static void meson6_dwmac_fix_mac_speed(void *priv, unsigned int speed)
31 {
32 	struct meson_dwmac *dwmac = priv;
33 	unsigned int val;
34 
35 	val = readl(dwmac->reg);
36 
37 	switch (speed) {
38 	case SPEED_10:
39 		val &= ~ETHMAC_SPEED_100;
40 		break;
41 	case SPEED_100:
42 		val |= ETHMAC_SPEED_100;
43 		break;
44 	}
45 
46 	writel(val, dwmac->reg);
47 }
48 
49 static void *meson6_dwmac_setup(struct platform_device *pdev)
50 {
51 	struct meson_dwmac *dwmac;
52 	struct resource *res;
53 
54 	dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
55 	if (!dwmac)
56 		return ERR_PTR(-ENOMEM);
57 
58 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
59 	dwmac->reg = devm_ioremap_resource(&pdev->dev, res);
60 	if (IS_ERR(dwmac->reg))
61 		return ERR_CAST(dwmac->reg);
62 
63 	return dwmac;
64 }
65 
66 const struct stmmac_of_data meson6_dwmac_data = {
67 	.setup		= meson6_dwmac_setup,
68 	.fix_mac_speed	= meson6_dwmac_fix_mac_speed,
69 };
70