1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
3*4882a593Smuzhiyun */
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /* Qualcomm Technologies, Inc. EMAC SGMII Controller driver.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/interrupt.h>
9*4882a593Smuzhiyun #include <linux/iopoll.h>
10*4882a593Smuzhiyun #include <linux/acpi.h>
11*4882a593Smuzhiyun #include <linux/of_device.h>
12*4882a593Smuzhiyun #include "emac.h"
13*4882a593Smuzhiyun #include "emac-mac.h"
14*4882a593Smuzhiyun #include "emac-sgmii.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /* EMAC_SGMII register offsets */
17*4882a593Smuzhiyun #define EMAC_SGMII_PHY_AUTONEG_CFG2 0x0048
18*4882a593Smuzhiyun #define EMAC_SGMII_PHY_SPEED_CFG1 0x0074
19*4882a593Smuzhiyun #define EMAC_SGMII_PHY_IRQ_CMD 0x00ac
20*4882a593Smuzhiyun #define EMAC_SGMII_PHY_INTERRUPT_CLEAR 0x00b0
21*4882a593Smuzhiyun #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
22*4882a593Smuzhiyun #define EMAC_SGMII_PHY_INTERRUPT_STATUS 0x00b8
23*4882a593Smuzhiyun #define EMAC_SGMII_PHY_RX_CHK_STATUS 0x00d4
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define FORCE_AN_TX_CFG BIT(5)
26*4882a593Smuzhiyun #define FORCE_AN_RX_CFG BIT(4)
27*4882a593Smuzhiyun #define AN_ENABLE BIT(0)
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define DUPLEX_MODE BIT(4)
30*4882a593Smuzhiyun #define SPDMODE_1000 BIT(1)
31*4882a593Smuzhiyun #define SPDMODE_100 BIT(0)
32*4882a593Smuzhiyun #define SPDMODE_10 0
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define CDR_ALIGN_DET BIT(6)
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define IRQ_GLOBAL_CLEAR BIT(0)
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define DECODE_CODE_ERR BIT(7)
39*4882a593Smuzhiyun #define DECODE_DISP_ERR BIT(6)
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define SGMII_PHY_IRQ_CLR_WAIT_TIME 10
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define SGMII_PHY_INTERRUPT_ERR (DECODE_CODE_ERR | DECODE_DISP_ERR)
44*4882a593Smuzhiyun #define SGMII_ISR_MASK (SGMII_PHY_INTERRUPT_ERR)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #define SERDES_START_WAIT_TIMES 100
47*4882a593Smuzhiyun
emac_sgmii_init(struct emac_adapter * adpt)48*4882a593Smuzhiyun int emac_sgmii_init(struct emac_adapter *adpt)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->init))
51*4882a593Smuzhiyun return 0;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun return adpt->phy.sgmii_ops->init(adpt);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
emac_sgmii_open(struct emac_adapter * adpt)56*4882a593Smuzhiyun int emac_sgmii_open(struct emac_adapter *adpt)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->open))
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun return adpt->phy.sgmii_ops->open(adpt);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
emac_sgmii_close(struct emac_adapter * adpt)64*4882a593Smuzhiyun void emac_sgmii_close(struct emac_adapter *adpt)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->close))
67*4882a593Smuzhiyun return;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun adpt->phy.sgmii_ops->close(adpt);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
emac_sgmii_link_change(struct emac_adapter * adpt,bool link_state)72*4882a593Smuzhiyun int emac_sgmii_link_change(struct emac_adapter *adpt, bool link_state)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->link_change))
75*4882a593Smuzhiyun return 0;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun return adpt->phy.sgmii_ops->link_change(adpt, link_state);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
emac_sgmii_reset(struct emac_adapter * adpt)80*4882a593Smuzhiyun void emac_sgmii_reset(struct emac_adapter *adpt)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->reset))
83*4882a593Smuzhiyun return;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun adpt->phy.sgmii_ops->reset(adpt);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /* Initialize the SGMII link between the internal and external PHYs. */
emac_sgmii_link_init(struct emac_adapter * adpt)89*4882a593Smuzhiyun static void emac_sgmii_link_init(struct emac_adapter *adpt)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun struct emac_sgmii *phy = &adpt->phy;
92*4882a593Smuzhiyun u32 val;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* Always use autonegotiation. It works no matter how the external
95*4882a593Smuzhiyun * PHY is configured.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun val = readl(phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
98*4882a593Smuzhiyun val &= ~(FORCE_AN_RX_CFG | FORCE_AN_TX_CFG);
99*4882a593Smuzhiyun val |= AN_ENABLE;
100*4882a593Smuzhiyun writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
emac_sgmii_irq_clear(struct emac_adapter * adpt,u8 irq_bits)103*4882a593Smuzhiyun static int emac_sgmii_irq_clear(struct emac_adapter *adpt, u8 irq_bits)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun struct emac_sgmii *phy = &adpt->phy;
106*4882a593Smuzhiyun u8 status;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun writel_relaxed(irq_bits, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
109*4882a593Smuzhiyun writel_relaxed(IRQ_GLOBAL_CLEAR, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
110*4882a593Smuzhiyun /* Ensure interrupt clear command is written to HW */
111*4882a593Smuzhiyun wmb();
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /* After set the IRQ_GLOBAL_CLEAR bit, the status clearing must
114*4882a593Smuzhiyun * be confirmed before clearing the bits in other registers.
115*4882a593Smuzhiyun * It takes a few cycles for hw to clear the interrupt status.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun if (readl_poll_timeout_atomic(phy->base +
118*4882a593Smuzhiyun EMAC_SGMII_PHY_INTERRUPT_STATUS,
119*4882a593Smuzhiyun status, !(status & irq_bits), 1,
120*4882a593Smuzhiyun SGMII_PHY_IRQ_CLR_WAIT_TIME)) {
121*4882a593Smuzhiyun net_err_ratelimited("%s: failed to clear SGMII irq: status:0x%x bits:0x%x\n",
122*4882a593Smuzhiyun adpt->netdev->name, status, irq_bits);
123*4882a593Smuzhiyun return -EIO;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* Finalize clearing procedure */
127*4882a593Smuzhiyun writel_relaxed(0, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
128*4882a593Smuzhiyun writel_relaxed(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /* Ensure that clearing procedure finalization is written to HW */
131*4882a593Smuzhiyun wmb();
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun return 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* The number of decode errors that triggers a reset */
137*4882a593Smuzhiyun #define DECODE_ERROR_LIMIT 2
138*4882a593Smuzhiyun
emac_sgmii_interrupt(int irq,void * data)139*4882a593Smuzhiyun static irqreturn_t emac_sgmii_interrupt(int irq, void *data)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun struct emac_adapter *adpt = data;
142*4882a593Smuzhiyun struct emac_sgmii *phy = &adpt->phy;
143*4882a593Smuzhiyun u8 status;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun status = readl(phy->base + EMAC_SGMII_PHY_INTERRUPT_STATUS);
146*4882a593Smuzhiyun status &= SGMII_ISR_MASK;
147*4882a593Smuzhiyun if (!status)
148*4882a593Smuzhiyun return IRQ_HANDLED;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* If we get a decoding error and CDR is not locked, then try
151*4882a593Smuzhiyun * resetting the internal PHY. The internal PHY uses an embedded
152*4882a593Smuzhiyun * clock with Clock and Data Recovery (CDR) to recover the
153*4882a593Smuzhiyun * clock and data.
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun if (status & SGMII_PHY_INTERRUPT_ERR) {
156*4882a593Smuzhiyun int count;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* The SGMII is capable of recovering from some decode
159*4882a593Smuzhiyun * errors automatically. However, if we get multiple
160*4882a593Smuzhiyun * decode errors in a row, then assume that something
161*4882a593Smuzhiyun * is wrong and reset the interface.
162*4882a593Smuzhiyun */
163*4882a593Smuzhiyun count = atomic_inc_return(&phy->decode_error_count);
164*4882a593Smuzhiyun if (count == DECODE_ERROR_LIMIT) {
165*4882a593Smuzhiyun schedule_work(&adpt->work_thread);
166*4882a593Smuzhiyun atomic_set(&phy->decode_error_count, 0);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun } else {
169*4882a593Smuzhiyun /* We only care about consecutive decode errors. */
170*4882a593Smuzhiyun atomic_set(&phy->decode_error_count, 0);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (emac_sgmii_irq_clear(adpt, status))
174*4882a593Smuzhiyun schedule_work(&adpt->work_thread);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun return IRQ_HANDLED;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
emac_sgmii_reset_prepare(struct emac_adapter * adpt)179*4882a593Smuzhiyun static void emac_sgmii_reset_prepare(struct emac_adapter *adpt)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct emac_sgmii *phy = &adpt->phy;
182*4882a593Smuzhiyun u32 val;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* Reset PHY */
185*4882a593Smuzhiyun val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
186*4882a593Smuzhiyun writel(((val & ~PHY_RESET) | PHY_RESET), phy->base +
187*4882a593Smuzhiyun EMAC_EMAC_WRAPPER_CSR2);
188*4882a593Smuzhiyun /* Ensure phy-reset command is written to HW before the release cmd */
189*4882a593Smuzhiyun msleep(50);
190*4882a593Smuzhiyun val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
191*4882a593Smuzhiyun writel((val & ~PHY_RESET), phy->base + EMAC_EMAC_WRAPPER_CSR2);
192*4882a593Smuzhiyun /* Ensure phy-reset release command is written to HW before initializing
193*4882a593Smuzhiyun * SGMII
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun msleep(50);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
emac_sgmii_common_reset(struct emac_adapter * adpt)198*4882a593Smuzhiyun static void emac_sgmii_common_reset(struct emac_adapter *adpt)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun int ret;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun emac_sgmii_reset_prepare(adpt);
203*4882a593Smuzhiyun emac_sgmii_link_init(adpt);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun ret = emac_sgmii_init(adpt);
206*4882a593Smuzhiyun if (ret)
207*4882a593Smuzhiyun netdev_err(adpt->netdev,
208*4882a593Smuzhiyun "could not reinitialize internal PHY (error=%i)\n",
209*4882a593Smuzhiyun ret);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
emac_sgmii_common_open(struct emac_adapter * adpt)212*4882a593Smuzhiyun static int emac_sgmii_common_open(struct emac_adapter *adpt)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun struct emac_sgmii *sgmii = &adpt->phy;
215*4882a593Smuzhiyun int ret;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (sgmii->irq) {
218*4882a593Smuzhiyun /* Make sure interrupts are cleared and disabled first */
219*4882a593Smuzhiyun ret = emac_sgmii_irq_clear(adpt, 0xff);
220*4882a593Smuzhiyun if (ret)
221*4882a593Smuzhiyun return ret;
222*4882a593Smuzhiyun writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun ret = request_irq(sgmii->irq, emac_sgmii_interrupt, 0,
225*4882a593Smuzhiyun "emac-sgmii", adpt);
226*4882a593Smuzhiyun if (ret) {
227*4882a593Smuzhiyun netdev_err(adpt->netdev,
228*4882a593Smuzhiyun "could not register handler for internal PHY\n");
229*4882a593Smuzhiyun return ret;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun return 0;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
emac_sgmii_common_close(struct emac_adapter * adpt)236*4882a593Smuzhiyun static void emac_sgmii_common_close(struct emac_adapter *adpt)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun struct emac_sgmii *sgmii = &adpt->phy;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /* Make sure interrupts are disabled */
241*4882a593Smuzhiyun writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
242*4882a593Smuzhiyun free_irq(sgmii->irq, adpt);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* The error interrupts are only valid after the link is up */
emac_sgmii_common_link_change(struct emac_adapter * adpt,bool linkup)246*4882a593Smuzhiyun static int emac_sgmii_common_link_change(struct emac_adapter *adpt, bool linkup)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun struct emac_sgmii *sgmii = &adpt->phy;
249*4882a593Smuzhiyun int ret;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (linkup) {
252*4882a593Smuzhiyun /* Clear and enable interrupts */
253*4882a593Smuzhiyun ret = emac_sgmii_irq_clear(adpt, 0xff);
254*4882a593Smuzhiyun if (ret)
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun writel(SGMII_ISR_MASK,
258*4882a593Smuzhiyun sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
259*4882a593Smuzhiyun } else {
260*4882a593Smuzhiyun /* Disable interrupts */
261*4882a593Smuzhiyun writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
262*4882a593Smuzhiyun synchronize_irq(sgmii->irq);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun return 0;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun static struct sgmii_ops fsm9900_ops = {
269*4882a593Smuzhiyun .init = emac_sgmii_init_fsm9900,
270*4882a593Smuzhiyun .open = emac_sgmii_common_open,
271*4882a593Smuzhiyun .close = emac_sgmii_common_close,
272*4882a593Smuzhiyun .link_change = emac_sgmii_common_link_change,
273*4882a593Smuzhiyun .reset = emac_sgmii_common_reset,
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun static struct sgmii_ops qdf2432_ops = {
277*4882a593Smuzhiyun .init = emac_sgmii_init_qdf2432,
278*4882a593Smuzhiyun .open = emac_sgmii_common_open,
279*4882a593Smuzhiyun .close = emac_sgmii_common_close,
280*4882a593Smuzhiyun .link_change = emac_sgmii_common_link_change,
281*4882a593Smuzhiyun .reset = emac_sgmii_common_reset,
282*4882a593Smuzhiyun };
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun #ifdef CONFIG_ACPI
285*4882a593Smuzhiyun static struct sgmii_ops qdf2400_ops = {
286*4882a593Smuzhiyun .init = emac_sgmii_init_qdf2400,
287*4882a593Smuzhiyun .open = emac_sgmii_common_open,
288*4882a593Smuzhiyun .close = emac_sgmii_common_close,
289*4882a593Smuzhiyun .link_change = emac_sgmii_common_link_change,
290*4882a593Smuzhiyun .reset = emac_sgmii_common_reset,
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun #endif
293*4882a593Smuzhiyun
emac_sgmii_acpi_match(struct device * dev,void * data)294*4882a593Smuzhiyun static int emac_sgmii_acpi_match(struct device *dev, void *data)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun #ifdef CONFIG_ACPI
297*4882a593Smuzhiyun static const struct acpi_device_id match_table[] = {
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun .id = "QCOM8071",
300*4882a593Smuzhiyun },
301*4882a593Smuzhiyun {}
302*4882a593Smuzhiyun };
303*4882a593Smuzhiyun const struct acpi_device_id *id = acpi_match_device(match_table, dev);
304*4882a593Smuzhiyun struct sgmii_ops **ops = data;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (id) {
307*4882a593Smuzhiyun acpi_handle handle = ACPI_HANDLE(dev);
308*4882a593Smuzhiyun unsigned long long hrv;
309*4882a593Smuzhiyun acpi_status status;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun status = acpi_evaluate_integer(handle, "_HRV", NULL, &hrv);
312*4882a593Smuzhiyun if (status) {
313*4882a593Smuzhiyun if (status == AE_NOT_FOUND)
314*4882a593Smuzhiyun /* Older versions of the QDF2432 ACPI tables do
315*4882a593Smuzhiyun * not have an _HRV property.
316*4882a593Smuzhiyun */
317*4882a593Smuzhiyun hrv = 1;
318*4882a593Smuzhiyun else
319*4882a593Smuzhiyun /* Something is wrong with the tables */
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun switch (hrv) {
324*4882a593Smuzhiyun case 1:
325*4882a593Smuzhiyun *ops = &qdf2432_ops;
326*4882a593Smuzhiyun return 1;
327*4882a593Smuzhiyun case 2:
328*4882a593Smuzhiyun *ops = &qdf2400_ops;
329*4882a593Smuzhiyun return 1;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun #endif
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun static const struct of_device_id emac_sgmii_dt_match[] = {
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun .compatible = "qcom,fsm9900-emac-sgmii",
340*4882a593Smuzhiyun .data = &fsm9900_ops,
341*4882a593Smuzhiyun },
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun .compatible = "qcom,qdf2432-emac-sgmii",
344*4882a593Smuzhiyun .data = &qdf2432_ops,
345*4882a593Smuzhiyun },
346*4882a593Smuzhiyun {}
347*4882a593Smuzhiyun };
348*4882a593Smuzhiyun
emac_sgmii_config(struct platform_device * pdev,struct emac_adapter * adpt)349*4882a593Smuzhiyun int emac_sgmii_config(struct platform_device *pdev, struct emac_adapter *adpt)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun struct platform_device *sgmii_pdev = NULL;
352*4882a593Smuzhiyun struct emac_sgmii *phy = &adpt->phy;
353*4882a593Smuzhiyun struct resource *res;
354*4882a593Smuzhiyun int ret;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun if (has_acpi_companion(&pdev->dev)) {
357*4882a593Smuzhiyun struct device *dev;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun dev = device_find_child(&pdev->dev, &phy->sgmii_ops,
360*4882a593Smuzhiyun emac_sgmii_acpi_match);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if (!dev) {
363*4882a593Smuzhiyun dev_warn(&pdev->dev, "cannot find internal phy node\n");
364*4882a593Smuzhiyun return 0;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun sgmii_pdev = to_platform_device(dev);
368*4882a593Smuzhiyun } else {
369*4882a593Smuzhiyun const struct of_device_id *match;
370*4882a593Smuzhiyun struct device_node *np;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun np = of_parse_phandle(pdev->dev.of_node, "internal-phy", 0);
373*4882a593Smuzhiyun if (!np) {
374*4882a593Smuzhiyun dev_err(&pdev->dev, "missing internal-phy property\n");
375*4882a593Smuzhiyun return -ENODEV;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun sgmii_pdev = of_find_device_by_node(np);
379*4882a593Smuzhiyun of_node_put(np);
380*4882a593Smuzhiyun if (!sgmii_pdev) {
381*4882a593Smuzhiyun dev_err(&pdev->dev, "invalid internal-phy property\n");
382*4882a593Smuzhiyun return -ENODEV;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun match = of_match_device(emac_sgmii_dt_match, &sgmii_pdev->dev);
386*4882a593Smuzhiyun if (!match) {
387*4882a593Smuzhiyun dev_err(&pdev->dev, "unrecognized internal phy node\n");
388*4882a593Smuzhiyun ret = -ENODEV;
389*4882a593Smuzhiyun goto error_put_device;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun phy->sgmii_ops = (struct sgmii_ops *)match->data;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /* Base address is the first address */
396*4882a593Smuzhiyun res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 0);
397*4882a593Smuzhiyun if (!res) {
398*4882a593Smuzhiyun ret = -EINVAL;
399*4882a593Smuzhiyun goto error_put_device;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun phy->base = ioremap(res->start, resource_size(res));
403*4882a593Smuzhiyun if (!phy->base) {
404*4882a593Smuzhiyun ret = -ENOMEM;
405*4882a593Smuzhiyun goto error_put_device;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /* v2 SGMII has a per-lane digital digital, so parse it if it exists */
409*4882a593Smuzhiyun res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 1);
410*4882a593Smuzhiyun if (res) {
411*4882a593Smuzhiyun phy->digital = ioremap(res->start, resource_size(res));
412*4882a593Smuzhiyun if (!phy->digital) {
413*4882a593Smuzhiyun ret = -ENOMEM;
414*4882a593Smuzhiyun goto error_unmap_base;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun ret = emac_sgmii_init(adpt);
419*4882a593Smuzhiyun if (ret)
420*4882a593Smuzhiyun goto error;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun emac_sgmii_link_init(adpt);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun ret = platform_get_irq(sgmii_pdev, 0);
425*4882a593Smuzhiyun if (ret > 0)
426*4882a593Smuzhiyun phy->irq = ret;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* We've remapped the addresses, so we don't need the device any
429*4882a593Smuzhiyun * more. of_find_device_by_node() says we should release it.
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun put_device(&sgmii_pdev->dev);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun return 0;
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun error:
436*4882a593Smuzhiyun if (phy->digital)
437*4882a593Smuzhiyun iounmap(phy->digital);
438*4882a593Smuzhiyun error_unmap_base:
439*4882a593Smuzhiyun iounmap(phy->base);
440*4882a593Smuzhiyun error_put_device:
441*4882a593Smuzhiyun put_device(&sgmii_pdev->dev);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun return ret;
444*4882a593Smuzhiyun }
445