1 /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 /* Qualcomm Technologies, Inc. EMAC PHY Controller driver.
16 #include <linux/of_mdio.h>
17 #include <linux/phy.h>
18 #include <linux/iopoll.h>
19 #include <linux/acpi.h>
22 /* EMAC base register offsets */
23 #define EMAC_MDIO_CTRL 0x001414
24 #define EMAC_PHY_STS 0x001418
25 #define EMAC_MDIO_EX_CTRL 0x001440
28 #define MDIO_MODE BIT(30)
29 #define MDIO_PR BIT(29)
30 #define MDIO_AP_EN BIT(28)
31 #define MDIO_BUSY BIT(27)
32 #define MDIO_CLK_SEL_BMSK 0x7000000
33 #define MDIO_CLK_SEL_SHFT 24
34 #define MDIO_START BIT(23)
35 #define SUP_PREAMBLE BIT(22)
36 #define MDIO_RD_NWR BIT(21)
37 #define MDIO_REG_ADDR_BMSK 0x1f0000
38 #define MDIO_REG_ADDR_SHFT 16
39 #define MDIO_DATA_BMSK 0xffff
40 #define MDIO_DATA_SHFT 0
43 #define PHY_ADDR_BMSK 0x1f0000
44 #define PHY_ADDR_SHFT 16
46 #define MDIO_CLK_25_4 0
47 #define MDIO_CLK_25_28 7
49 #define MDIO_WAIT_TIMES 1000
50 #define MDIO_STATUS_DELAY_TIME 1
52 static int emac_mdio_read(struct mii_bus
*bus
, int addr
, int regnum
)
54 struct emac_adapter
*adpt
= bus
->priv
;
57 emac_reg_update32(adpt
->base
+ EMAC_PHY_STS
, PHY_ADDR_BMSK
,
58 (addr
<< PHY_ADDR_SHFT
));
61 ((MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHFT
) & MDIO_CLK_SEL_BMSK
) |
62 ((regnum
<< MDIO_REG_ADDR_SHFT
) & MDIO_REG_ADDR_BMSK
) |
63 MDIO_START
| MDIO_RD_NWR
;
65 writel(reg
, adpt
->base
+ EMAC_MDIO_CTRL
);
67 if (readl_poll_timeout(adpt
->base
+ EMAC_MDIO_CTRL
, reg
,
68 !(reg
& (MDIO_START
| MDIO_BUSY
)),
69 MDIO_STATUS_DELAY_TIME
, MDIO_WAIT_TIMES
* 100))
72 return (reg
>> MDIO_DATA_SHFT
) & MDIO_DATA_BMSK
;
75 static int emac_mdio_write(struct mii_bus
*bus
, int addr
, int regnum
, u16 val
)
77 struct emac_adapter
*adpt
= bus
->priv
;
80 emac_reg_update32(adpt
->base
+ EMAC_PHY_STS
, PHY_ADDR_BMSK
,
81 (addr
<< PHY_ADDR_SHFT
));
84 ((MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHFT
) & MDIO_CLK_SEL_BMSK
) |
85 ((regnum
<< MDIO_REG_ADDR_SHFT
) & MDIO_REG_ADDR_BMSK
) |
86 ((val
<< MDIO_DATA_SHFT
) & MDIO_DATA_BMSK
) |
89 writel(reg
, adpt
->base
+ EMAC_MDIO_CTRL
);
91 if (readl_poll_timeout(adpt
->base
+ EMAC_MDIO_CTRL
, reg
,
92 !(reg
& (MDIO_START
| MDIO_BUSY
)),
93 MDIO_STATUS_DELAY_TIME
, MDIO_WAIT_TIMES
* 100))
99 /* Configure the MDIO bus and connect the external PHY */
100 int emac_phy_config(struct platform_device
*pdev
, struct emac_adapter
*adpt
)
102 struct device_node
*np
= pdev
->dev
.of_node
;
103 struct mii_bus
*mii_bus
;
106 /* Create the mii_bus object for talking to the MDIO bus */
107 adpt
->mii_bus
= mii_bus
= devm_mdiobus_alloc(&pdev
->dev
);
111 mii_bus
->name
= "emac-mdio";
112 snprintf(mii_bus
->id
, MII_BUS_ID_SIZE
, "%s", pdev
->name
);
113 mii_bus
->read
= emac_mdio_read
;
114 mii_bus
->write
= emac_mdio_write
;
115 mii_bus
->parent
= &pdev
->dev
;
116 mii_bus
->priv
= adpt
;
118 if (has_acpi_companion(&pdev
->dev
)) {
121 ret
= mdiobus_register(mii_bus
);
123 dev_err(&pdev
->dev
, "could not register mdio bus\n");
126 ret
= device_property_read_u32(&pdev
->dev
, "phy-channel",
129 /* If we can't read a valid phy address, then assume
130 * that there is only one phy on this mdio bus.
132 adpt
->phydev
= phy_find_first(mii_bus
);
134 adpt
->phydev
= mdiobus_get_phy(mii_bus
, phy_addr
);
136 /* of_phy_find_device() claims a reference to the phydev,
137 * so we do that here manually as well. When the driver
138 * later unloads, it can unilaterally drop the reference
139 * without worrying about ACPI vs DT.
142 get_device(&adpt
->phydev
->mdio
.dev
);
144 struct device_node
*phy_np
;
146 ret
= of_mdiobus_register(mii_bus
, np
);
148 dev_err(&pdev
->dev
, "could not register mdio bus\n");
152 phy_np
= of_parse_phandle(np
, "phy-handle", 0);
153 adpt
->phydev
= of_phy_find_device(phy_np
);
158 dev_err(&pdev
->dev
, "could not find external phy\n");
159 mdiobus_unregister(mii_bus
);