1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/of_mdio.h>
9 #define USB_MARVELL_VID 0x1286
11 static const struct usb_device_id mvusb_mdio_table[] = {
12 { USB_DEVICE(USB_MARVELL_VID, 0x1fa4) },
16 MODULE_DEVICE_TABLE(usb, mvusb_mdio_table);
26 struct usb_device *udev;
32 static int mvusb_mdio_read(struct mii_bus *mdio, int dev, int reg)
34 struct mvusb_mdio *mvusb = mdio->priv;
37 if (dev & MII_ADDR_C45)
40 mvusb->buf[MVUSB_CMD_ADDR] = cpu_to_le16(0xa400 | (dev << 5) | reg);
42 err = usb_bulk_msg(mvusb->udev, usb_sndbulkpipe(mvusb->udev, 2),
43 mvusb->buf, 6, &alen, 100);
47 err = usb_bulk_msg(mvusb->udev, usb_rcvbulkpipe(mvusb->udev, 6),
48 &mvusb->buf[MVUSB_CMD_VAL], 2, &alen, 100);
52 return le16_to_cpu(mvusb->buf[MVUSB_CMD_VAL]);
55 static int mvusb_mdio_write(struct mii_bus *mdio, int dev, int reg, u16 val)
57 struct mvusb_mdio *mvusb = mdio->priv;
60 if (dev & MII_ADDR_C45)
63 mvusb->buf[MVUSB_CMD_ADDR] = cpu_to_le16(0x8000 | (dev << 5) | reg);
64 mvusb->buf[MVUSB_CMD_VAL] = cpu_to_le16(val);
66 return usb_bulk_msg(mvusb->udev, usb_sndbulkpipe(mvusb->udev, 2),
67 mvusb->buf, 8, &alen, 100);
70 static int mvusb_mdio_probe(struct usb_interface *interface,
71 const struct usb_device_id *id)
73 struct device *dev = &interface->dev;
74 struct mvusb_mdio *mvusb;
78 mdio = devm_mdiobus_alloc_size(dev, sizeof(*mvusb));
84 mvusb->udev = usb_get_dev(interface_to_usbdev(interface));
86 /* Reversed from USB PCAPs, no idea what these mean. */
87 mvusb->buf[MVUSB_CMD_PREAMBLE0] = cpu_to_le16(0xe800);
88 mvusb->buf[MVUSB_CMD_PREAMBLE1] = cpu_to_le16(0x0001);
90 snprintf(mdio->id, MII_BUS_ID_SIZE, "mvusb-%s", dev_name(dev));
91 mdio->name = mdio->id;
93 mdio->read = mvusb_mdio_read;
94 mdio->write = mvusb_mdio_write;
96 usb_set_intfdata(interface, mvusb);
97 ret = of_mdiobus_register(mdio, dev->of_node);
104 usb_put_dev(mvusb->udev);
108 static void mvusb_mdio_disconnect(struct usb_interface *interface)
110 struct mvusb_mdio *mvusb = usb_get_intfdata(interface);
111 struct usb_device *udev = mvusb->udev;
113 mdiobus_unregister(mvusb->mdio);
114 usb_set_intfdata(interface, NULL);
118 static struct usb_driver mvusb_mdio_driver = {
119 .name = "mvusb_mdio",
120 .id_table = mvusb_mdio_table,
121 .probe = mvusb_mdio_probe,
122 .disconnect = mvusb_mdio_disconnect,
125 module_usb_driver(mvusb_mdio_driver);
127 MODULE_AUTHOR("Tobias Waldekranz <tobias@waldekranz.com>");
128 MODULE_DESCRIPTION("Marvell USB MDIO Adapter");
129 MODULE_LICENSE("GPL");