1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
2812b5ca7SBernd Edlinger /*
3812b5ca7SBernd Edlinger * Driver for the Renesas PHY uPD60620.
4812b5ca7SBernd Edlinger *
5812b5ca7SBernd Edlinger * Copyright (C) 2015 Softing Industrial Automation GmbH
6812b5ca7SBernd Edlinger */
7812b5ca7SBernd Edlinger
8812b5ca7SBernd Edlinger #include <linux/kernel.h>
9812b5ca7SBernd Edlinger #include <linux/module.h>
10812b5ca7SBernd Edlinger #include <linux/phy.h>
11812b5ca7SBernd Edlinger
12812b5ca7SBernd Edlinger #define UPD60620_PHY_ID 0xb8242824
13812b5ca7SBernd Edlinger
14812b5ca7SBernd Edlinger /* Extended Registers and values */
15812b5ca7SBernd Edlinger /* PHY Special Control/Status */
16812b5ca7SBernd Edlinger #define PHY_PHYSCR 0x1F /* PHY.31 */
17812b5ca7SBernd Edlinger #define PHY_PHYSCR_10MB 0x0004 /* PHY speed = 10mb */
18812b5ca7SBernd Edlinger #define PHY_PHYSCR_100MB 0x0008 /* PHY speed = 100mb */
19812b5ca7SBernd Edlinger #define PHY_PHYSCR_DUPLEX 0x0010 /* PHY Duplex */
20812b5ca7SBernd Edlinger
21812b5ca7SBernd Edlinger /* PHY Special Modes */
22812b5ca7SBernd Edlinger #define PHY_SPM 0x12 /* PHY.18 */
23812b5ca7SBernd Edlinger
24812b5ca7SBernd Edlinger /* Init PHY */
25812b5ca7SBernd Edlinger
upd60620_config_init(struct phy_device * phydev)26812b5ca7SBernd Edlinger static int upd60620_config_init(struct phy_device *phydev)
27812b5ca7SBernd Edlinger {
28812b5ca7SBernd Edlinger /* Enable support for passive HUBs (could be a strap option) */
29812b5ca7SBernd Edlinger /* PHYMODE: All speeds, HD in parallel detect */
30812b5ca7SBernd Edlinger return phy_write(phydev, PHY_SPM, 0x0180 | phydev->mdio.addr);
31812b5ca7SBernd Edlinger }
32812b5ca7SBernd Edlinger
33812b5ca7SBernd Edlinger /* Get PHY status from common registers */
34812b5ca7SBernd Edlinger
upd60620_read_status(struct phy_device * phydev)35812b5ca7SBernd Edlinger static int upd60620_read_status(struct phy_device *phydev)
36812b5ca7SBernd Edlinger {
37812b5ca7SBernd Edlinger int phy_state;
38812b5ca7SBernd Edlinger
39812b5ca7SBernd Edlinger /* Read negotiated state */
40812b5ca7SBernd Edlinger phy_state = phy_read(phydev, MII_BMSR);
41812b5ca7SBernd Edlinger if (phy_state < 0)
42812b5ca7SBernd Edlinger return phy_state;
43812b5ca7SBernd Edlinger
44812b5ca7SBernd Edlinger phydev->link = 0;
45c0ec3c27SAndrew Lunn linkmode_zero(phydev->lp_advertising);
46812b5ca7SBernd Edlinger phydev->pause = 0;
47812b5ca7SBernd Edlinger phydev->asym_pause = 0;
48812b5ca7SBernd Edlinger
49812b5ca7SBernd Edlinger if (phy_state & (BMSR_ANEGCOMPLETE | BMSR_LSTATUS)) {
50812b5ca7SBernd Edlinger phy_state = phy_read(phydev, PHY_PHYSCR);
51812b5ca7SBernd Edlinger if (phy_state < 0)
52812b5ca7SBernd Edlinger return phy_state;
53812b5ca7SBernd Edlinger
54812b5ca7SBernd Edlinger if (phy_state & (PHY_PHYSCR_10MB | PHY_PHYSCR_100MB)) {
55812b5ca7SBernd Edlinger phydev->link = 1;
56812b5ca7SBernd Edlinger phydev->speed = SPEED_10;
57812b5ca7SBernd Edlinger phydev->duplex = DUPLEX_HALF;
58812b5ca7SBernd Edlinger
59812b5ca7SBernd Edlinger if (phy_state & PHY_PHYSCR_100MB)
60812b5ca7SBernd Edlinger phydev->speed = SPEED_100;
61812b5ca7SBernd Edlinger if (phy_state & PHY_PHYSCR_DUPLEX)
62812b5ca7SBernd Edlinger phydev->duplex = DUPLEX_FULL;
63812b5ca7SBernd Edlinger
64812b5ca7SBernd Edlinger phy_state = phy_read(phydev, MII_LPA);
65812b5ca7SBernd Edlinger if (phy_state < 0)
66812b5ca7SBernd Edlinger return phy_state;
67812b5ca7SBernd Edlinger
68c0ec3c27SAndrew Lunn mii_lpa_to_linkmode_lpa_t(phydev->lp_advertising,
69c0ec3c27SAndrew Lunn phy_state);
70812b5ca7SBernd Edlinger
71*af006240SRussell King phy_resolve_aneg_pause(phydev);
72812b5ca7SBernd Edlinger }
73812b5ca7SBernd Edlinger }
74812b5ca7SBernd Edlinger return 0;
75812b5ca7SBernd Edlinger }
76812b5ca7SBernd Edlinger
77812b5ca7SBernd Edlinger MODULE_DESCRIPTION("Renesas uPD60620 PHY driver");
78812b5ca7SBernd Edlinger MODULE_AUTHOR("Bernd Edlinger <bernd.edlinger@hotmail.de>");
79812b5ca7SBernd Edlinger MODULE_LICENSE("GPL");
80812b5ca7SBernd Edlinger
81812b5ca7SBernd Edlinger static struct phy_driver upd60620_driver[1] = { {
82812b5ca7SBernd Edlinger .phy_id = UPD60620_PHY_ID,
83812b5ca7SBernd Edlinger .phy_id_mask = 0xfffffffe,
84812b5ca7SBernd Edlinger .name = "Renesas uPD60620",
85dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */
86812b5ca7SBernd Edlinger .flags = 0,
87812b5ca7SBernd Edlinger .config_init = upd60620_config_init,
88812b5ca7SBernd Edlinger .read_status = upd60620_read_status,
89812b5ca7SBernd Edlinger } };
90812b5ca7SBernd Edlinger
91812b5ca7SBernd Edlinger module_phy_driver(upd60620_driver);
92812b5ca7SBernd Edlinger
93812b5ca7SBernd Edlinger static struct mdio_device_id __maybe_unused upd60620_tbl[] = {
94812b5ca7SBernd Edlinger { UPD60620_PHY_ID, 0xfffffffe },
95812b5ca7SBernd Edlinger { }
96812b5ca7SBernd Edlinger };
97812b5ca7SBernd Edlinger
98812b5ca7SBernd Edlinger MODULE_DEVICE_TABLE(mdio, upd60620_tbl);
99