xref: /openbmc/linux/drivers/net/dsa/lan9303_mdio.c (revision 8ee90c5c)
1 /*
2  * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
3  *
4  * Partially based on a patch from
5  * Copyright (c) 2014 Stefan Roese <sr@denx.de>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mdio.h>
20 #include <linux/phy.h>
21 #include <linux/of.h>
22 
23 #include "lan9303.h"
24 
25 /* Generate phy-addr and -reg from the input address */
26 #define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f)
27 #define PHY_REG(x) (((x) >> 1) & 0x1f)
28 
29 struct lan9303_mdio {
30 	struct mdio_device *device;
31 	struct lan9303 chip;
32 };
33 
34 static void lan9303_mdio_real_write(struct mdio_device *mdio, int reg, u16 val)
35 {
36 	mdio->bus->write(mdio->bus, PHY_ADDR(reg), PHY_REG(reg), val);
37 }
38 
39 static int lan9303_mdio_write(void *ctx, uint32_t reg, uint32_t val)
40 {
41 	struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
42 
43 	reg <<= 2; /* reg num to offset */
44 	mutex_lock(&sw_dev->device->bus->mdio_lock);
45 	lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
46 	lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
47 	mutex_unlock(&sw_dev->device->bus->mdio_lock);
48 
49 	return 0;
50 }
51 
52 static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
53 {
54 	return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
55 }
56 
57 static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
58 {
59 	struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
60 
61 	reg <<= 2; /* reg num to offset */
62 	mutex_lock(&sw_dev->device->bus->mdio_lock);
63 	*val = lan9303_mdio_real_read(sw_dev->device, reg);
64 	*val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
65 	mutex_unlock(&sw_dev->device->bus->mdio_lock);
66 
67 	return 0;
68 }
69 
70 int lan9303_mdio_phy_write(struct lan9303 *chip, int phy, int reg, u16 val)
71 {
72 	struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
73 
74 	return mdiobus_write_nested(sw_dev->device->bus, phy, reg, val);
75 }
76 
77 int lan9303_mdio_phy_read(struct lan9303 *chip, int phy,  int reg)
78 {
79 	struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
80 
81 	return mdiobus_read_nested(sw_dev->device->bus, phy, reg);
82 }
83 
84 static const struct lan9303_phy_ops lan9303_mdio_phy_ops = {
85 	.phy_read = lan9303_mdio_phy_read,
86 	.phy_write = lan9303_mdio_phy_write,
87 };
88 
89 static const struct regmap_config lan9303_mdio_regmap_config = {
90 	.reg_bits = 8,
91 	.val_bits = 32,
92 	.reg_stride = 1,
93 	.can_multi_write = true,
94 	.max_register = 0x0ff, /* address bits 0..1 are not used */
95 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
96 
97 	.volatile_table = &lan9303_register_set,
98 	.wr_table = &lan9303_register_set,
99 	.rd_table = &lan9303_register_set,
100 
101 	.reg_read = lan9303_mdio_read,
102 	.reg_write = lan9303_mdio_write,
103 
104 	.cache_type = REGCACHE_NONE,
105 };
106 
107 static int lan9303_mdio_probe(struct mdio_device *mdiodev)
108 {
109 	struct lan9303_mdio *sw_dev;
110 	int ret;
111 
112 	sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
113 			      GFP_KERNEL);
114 	if (!sw_dev)
115 		return -ENOMEM;
116 
117 	sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
118 						&lan9303_mdio_regmap_config);
119 	if (IS_ERR(sw_dev->chip.regmap)) {
120 		ret = PTR_ERR(sw_dev->chip.regmap);
121 		dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
122 		return ret;
123 	}
124 
125 	/* link forward and backward */
126 	sw_dev->device = mdiodev;
127 	dev_set_drvdata(&mdiodev->dev, sw_dev);
128 	sw_dev->chip.dev = &mdiodev->dev;
129 
130 	sw_dev->chip.ops = &lan9303_mdio_phy_ops;
131 
132 	ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
133 	if (ret != 0)
134 		return ret;
135 
136 	dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
137 
138 	return 0;
139 }
140 
141 static void lan9303_mdio_remove(struct mdio_device *mdiodev)
142 {
143 	struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
144 
145 	if (!sw_dev)
146 		return;
147 
148 	lan9303_remove(&sw_dev->chip);
149 }
150 
151 /*-------------------------------------------------------------------------*/
152 
153 static const struct of_device_id lan9303_mdio_of_match[] = {
154 	{ .compatible = "smsc,lan9303-mdio" },
155 	{ /* sentinel */ },
156 };
157 MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
158 
159 static struct mdio_driver lan9303_mdio_driver = {
160 	.mdiodrv.driver = {
161 		.name = "LAN9303_MDIO",
162 		.of_match_table = of_match_ptr(lan9303_mdio_of_match),
163 	},
164 	.probe  = lan9303_mdio_probe,
165 	.remove = lan9303_mdio_remove,
166 };
167 mdio_module_driver(lan9303_mdio_driver);
168 
169 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
170 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
171 MODULE_LICENSE("GPL v2");
172