xref: /linux/drivers/net/dsa/lan9303_mdio.c (revision 7aacf86b75bc5523d20fd9127104384fce51ce9c)
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 	mutex_lock(&sw_dev->device->bus->mdio_lock);
44 	lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
45 	lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
46 	mutex_unlock(&sw_dev->device->bus->mdio_lock);
47 
48 	return 0;
49 }
50 
51 static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
52 {
53 	return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
54 }
55 
56 static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
57 {
58 	struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
59 
60 	mutex_lock(&sw_dev->device->bus->mdio_lock);
61 	*val = lan9303_mdio_real_read(sw_dev->device, reg);
62 	*val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
63 	mutex_unlock(&sw_dev->device->bus->mdio_lock);
64 
65 	return 0;
66 }
67 
68 static const struct regmap_config lan9303_mdio_regmap_config = {
69 	.reg_bits = 8,
70 	.val_bits = 32,
71 	.reg_stride = 1,
72 	.can_multi_write = true,
73 	.max_register = 0x0ff, /* address bits 0..1 are not used */
74 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
75 
76 	.volatile_table = &lan9303_register_set,
77 	.wr_table = &lan9303_register_set,
78 	.rd_table = &lan9303_register_set,
79 
80 	.reg_read = lan9303_mdio_read,
81 	.reg_write = lan9303_mdio_write,
82 
83 	.cache_type = REGCACHE_NONE,
84 };
85 
86 static int lan9303_mdio_probe(struct mdio_device *mdiodev)
87 {
88 	struct lan9303_mdio *sw_dev;
89 	int ret;
90 
91 	sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
92 			      GFP_KERNEL);
93 	if (!sw_dev)
94 		return -ENOMEM;
95 
96 	sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
97 						&lan9303_mdio_regmap_config);
98 	if (IS_ERR(sw_dev->chip.regmap)) {
99 		ret = PTR_ERR(sw_dev->chip.regmap);
100 		dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
101 		return ret;
102 	}
103 
104 	/* link forward and backward */
105 	sw_dev->device = mdiodev;
106 	dev_set_drvdata(&mdiodev->dev, sw_dev);
107 	sw_dev->chip.dev = &mdiodev->dev;
108 
109 	ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
110 	if (ret != 0)
111 		return ret;
112 
113 	dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
114 
115 	return 0;
116 }
117 
118 static void lan9303_mdio_remove(struct mdio_device *mdiodev)
119 {
120 	struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
121 
122 	if (!sw_dev)
123 		return;
124 
125 	lan9303_remove(&sw_dev->chip);
126 }
127 
128 /*-------------------------------------------------------------------------*/
129 
130 static const struct of_device_id lan9303_mdio_of_match[] = {
131 	{ .compatible = "smsc,lan9303-mdio" },
132 	{ /* sentinel */ },
133 };
134 MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
135 
136 static struct mdio_driver lan9303_mdio_driver = {
137 	.mdiodrv.driver = {
138 		.name = "LAN9303_MDIO",
139 		.of_match_table = of_match_ptr(lan9303_mdio_of_match),
140 	},
141 	.probe  = lan9303_mdio_probe,
142 	.remove = lan9303_mdio_remove,
143 };
144 mdio_module_driver(lan9303_mdio_driver);
145 
146 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
147 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
148 MODULE_LICENSE("GPL v2");
149