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/module.h>
18 #include <linux/of_net.h>
19 #include <linux/of_mdio.h>
20 #include <linux/phy.h>
21 #include <linux/iopoll.h>
22 #include <linux/acpi.h>
26 #include "emac-sgmii.h"
28 /* EMAC base register offsets */
29 #define EMAC_MDIO_CTRL 0x001414
30 #define EMAC_PHY_STS 0x001418
31 #define EMAC_MDIO_EX_CTRL 0x001440
34 #define MDIO_MODE BIT(30)
35 #define MDIO_PR BIT(29)
36 #define MDIO_AP_EN BIT(28)
37 #define MDIO_BUSY BIT(27)
38 #define MDIO_CLK_SEL_BMSK 0x7000000
39 #define MDIO_CLK_SEL_SHFT 24
40 #define MDIO_START BIT(23)
41 #define SUP_PREAMBLE BIT(22)
42 #define MDIO_RD_NWR BIT(21)
43 #define MDIO_REG_ADDR_BMSK 0x1f0000
44 #define MDIO_REG_ADDR_SHFT 16
45 #define MDIO_DATA_BMSK 0xffff
46 #define MDIO_DATA_SHFT 0
49 #define PHY_ADDR_BMSK 0x1f0000
50 #define PHY_ADDR_SHFT 16
52 #define MDIO_CLK_25_4 0
53 #define MDIO_CLK_25_28 7
55 #define MDIO_WAIT_TIMES 1000
57 #define EMAC_LINK_SPEED_DEFAULT (\
58 EMAC_LINK_SPEED_10_HALF |\
59 EMAC_LINK_SPEED_10_FULL |\
60 EMAC_LINK_SPEED_100_HALF |\
61 EMAC_LINK_SPEED_100_FULL |\
62 EMAC_LINK_SPEED_1GB_FULL)
65 * emac_phy_mdio_autopoll_disable() - disable mdio autopoll
66 * @adpt: the emac adapter
68 * The autopoll feature takes over the MDIO bus. In order for
69 * the PHY driver to be able to talk to the PHY over the MDIO
70 * bus, we need to temporarily disable the autopoll feature.
72 static int emac_phy_mdio_autopoll_disable(struct emac_adapter
*adpt
)
76 /* disable autopoll */
77 emac_reg_update32(adpt
->base
+ EMAC_MDIO_CTRL
, MDIO_AP_EN
, 0);
79 /* wait for any mdio polling to complete */
80 if (!readl_poll_timeout(adpt
->base
+ EMAC_MDIO_CTRL
, val
,
81 !(val
& MDIO_BUSY
), 100, MDIO_WAIT_TIMES
* 100))
84 /* failed to disable; ensure it is enabled before returning */
85 emac_reg_update32(adpt
->base
+ EMAC_MDIO_CTRL
, 0, MDIO_AP_EN
);
91 * emac_phy_mdio_autopoll_disable() - disable mdio autopoll
92 * @adpt: the emac adapter
94 * The EMAC has the ability to poll the external PHY on the MDIO
95 * bus for link state changes. This eliminates the need for the
96 * driver to poll the phy. If if the link state does change,
97 * the EMAC issues an interrupt on behalf of the PHY.
99 static void emac_phy_mdio_autopoll_enable(struct emac_adapter
*adpt
)
101 emac_reg_update32(adpt
->base
+ EMAC_MDIO_CTRL
, 0, MDIO_AP_EN
);
104 static int emac_mdio_read(struct mii_bus
*bus
, int addr
, int regnum
)
106 struct emac_adapter
*adpt
= bus
->priv
;
110 ret
= emac_phy_mdio_autopoll_disable(adpt
);
114 emac_reg_update32(adpt
->base
+ EMAC_PHY_STS
, PHY_ADDR_BMSK
,
115 (addr
<< PHY_ADDR_SHFT
));
118 ((MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHFT
) & MDIO_CLK_SEL_BMSK
) |
119 ((regnum
<< MDIO_REG_ADDR_SHFT
) & MDIO_REG_ADDR_BMSK
) |
120 MDIO_START
| MDIO_RD_NWR
;
122 writel(reg
, adpt
->base
+ EMAC_MDIO_CTRL
);
124 if (readl_poll_timeout(adpt
->base
+ EMAC_MDIO_CTRL
, reg
,
125 !(reg
& (MDIO_START
| MDIO_BUSY
)),
126 100, MDIO_WAIT_TIMES
* 100))
129 ret
= (reg
>> MDIO_DATA_SHFT
) & MDIO_DATA_BMSK
;
131 emac_phy_mdio_autopoll_enable(adpt
);
136 static int emac_mdio_write(struct mii_bus
*bus
, int addr
, int regnum
, u16 val
)
138 struct emac_adapter
*adpt
= bus
->priv
;
142 ret
= emac_phy_mdio_autopoll_disable(adpt
);
146 emac_reg_update32(adpt
->base
+ EMAC_PHY_STS
, PHY_ADDR_BMSK
,
147 (addr
<< PHY_ADDR_SHFT
));
150 ((MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHFT
) & MDIO_CLK_SEL_BMSK
) |
151 ((regnum
<< MDIO_REG_ADDR_SHFT
) & MDIO_REG_ADDR_BMSK
) |
152 ((val
<< MDIO_DATA_SHFT
) & MDIO_DATA_BMSK
) |
155 writel(reg
, adpt
->base
+ EMAC_MDIO_CTRL
);
157 if (readl_poll_timeout(adpt
->base
+ EMAC_MDIO_CTRL
, reg
,
158 !(reg
& (MDIO_START
| MDIO_BUSY
)), 100,
159 MDIO_WAIT_TIMES
* 100))
162 emac_phy_mdio_autopoll_enable(adpt
);
167 /* Configure the MDIO bus and connect the external PHY */
168 int emac_phy_config(struct platform_device
*pdev
, struct emac_adapter
*adpt
)
170 struct device_node
*np
= pdev
->dev
.of_node
;
171 struct mii_bus
*mii_bus
;
174 /* Create the mii_bus object for talking to the MDIO bus */
175 adpt
->mii_bus
= mii_bus
= devm_mdiobus_alloc(&pdev
->dev
);
179 mii_bus
->name
= "emac-mdio";
180 snprintf(mii_bus
->id
, MII_BUS_ID_SIZE
, "%s", pdev
->name
);
181 mii_bus
->read
= emac_mdio_read
;
182 mii_bus
->write
= emac_mdio_write
;
183 mii_bus
->parent
= &pdev
->dev
;
184 mii_bus
->priv
= adpt
;
186 if (has_acpi_companion(&pdev
->dev
)) {
189 ret
= mdiobus_register(mii_bus
);
191 dev_err(&pdev
->dev
, "could not register mdio bus\n");
194 ret
= device_property_read_u32(&pdev
->dev
, "phy-channel",
197 /* If we can't read a valid phy address, then assume
198 * that there is only one phy on this mdio bus.
200 adpt
->phydev
= phy_find_first(mii_bus
);
202 adpt
->phydev
= mdiobus_get_phy(mii_bus
, phy_addr
);
205 struct device_node
*phy_np
;
207 ret
= of_mdiobus_register(mii_bus
, np
);
209 dev_err(&pdev
->dev
, "could not register mdio bus\n");
213 phy_np
= of_parse_phandle(np
, "phy-handle", 0);
214 adpt
->phydev
= of_phy_find_device(phy_np
);
219 dev_err(&pdev
->dev
, "could not find external phy\n");
220 mdiobus_unregister(mii_bus
);
224 if (adpt
->phydev
->drv
)
225 phy_attached_print(adpt
->phydev
, NULL
);