1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
5 /* Qualcomm Technologies, Inc. EMAC SGMII Controller driver.
8 #include <linux/interrupt.h>
9 #include <linux/iopoll.h>
10 #include <linux/acpi.h>
11 #include <linux/of_device.h>
14 #include "emac-sgmii.h"
16 /* EMAC_SGMII register offsets */
17 #define EMAC_SGMII_PHY_AUTONEG_CFG2 0x0048
18 #define EMAC_SGMII_PHY_SPEED_CFG1 0x0074
19 #define EMAC_SGMII_PHY_IRQ_CMD 0x00ac
20 #define EMAC_SGMII_PHY_INTERRUPT_CLEAR 0x00b0
21 #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
22 #define EMAC_SGMII_PHY_INTERRUPT_STATUS 0x00b8
23 #define EMAC_SGMII_PHY_RX_CHK_STATUS 0x00d4
25 #define FORCE_AN_TX_CFG BIT(5)
26 #define FORCE_AN_RX_CFG BIT(4)
27 #define AN_ENABLE BIT(0)
29 #define DUPLEX_MODE BIT(4)
30 #define SPDMODE_1000 BIT(1)
31 #define SPDMODE_100 BIT(0)
34 #define CDR_ALIGN_DET BIT(6)
36 #define IRQ_GLOBAL_CLEAR BIT(0)
38 #define DECODE_CODE_ERR BIT(7)
39 #define DECODE_DISP_ERR BIT(6)
41 #define SGMII_PHY_IRQ_CLR_WAIT_TIME 10
43 #define SGMII_PHY_INTERRUPT_ERR (DECODE_CODE_ERR | DECODE_DISP_ERR)
44 #define SGMII_ISR_MASK (SGMII_PHY_INTERRUPT_ERR)
46 #define SERDES_START_WAIT_TIMES 100
48 int emac_sgmii_init(struct emac_adapter
*adpt
)
50 if (!(adpt
->phy
.sgmii_ops
&& adpt
->phy
.sgmii_ops
->init
))
53 return adpt
->phy
.sgmii_ops
->init(adpt
);
56 int emac_sgmii_open(struct emac_adapter
*adpt
)
58 if (!(adpt
->phy
.sgmii_ops
&& adpt
->phy
.sgmii_ops
->open
))
61 return adpt
->phy
.sgmii_ops
->open(adpt
);
64 void emac_sgmii_close(struct emac_adapter
*adpt
)
66 if (!(adpt
->phy
.sgmii_ops
&& adpt
->phy
.sgmii_ops
->close
))
69 adpt
->phy
.sgmii_ops
->close(adpt
);
72 int emac_sgmii_link_change(struct emac_adapter
*adpt
, bool link_state
)
74 if (!(adpt
->phy
.sgmii_ops
&& adpt
->phy
.sgmii_ops
->link_change
))
77 return adpt
->phy
.sgmii_ops
->link_change(adpt
, link_state
);
80 void emac_sgmii_reset(struct emac_adapter
*adpt
)
82 if (!(adpt
->phy
.sgmii_ops
&& adpt
->phy
.sgmii_ops
->reset
))
85 adpt
->phy
.sgmii_ops
->reset(adpt
);
88 /* Initialize the SGMII link between the internal and external PHYs. */
89 static void emac_sgmii_link_init(struct emac_adapter
*adpt
)
91 struct emac_sgmii
*phy
= &adpt
->phy
;
94 /* Always use autonegotiation. It works no matter how the external
97 val
= readl(phy
->base
+ EMAC_SGMII_PHY_AUTONEG_CFG2
);
98 val
&= ~(FORCE_AN_RX_CFG
| FORCE_AN_TX_CFG
);
100 writel(val
, phy
->base
+ EMAC_SGMII_PHY_AUTONEG_CFG2
);
103 static int emac_sgmii_irq_clear(struct emac_adapter
*adpt
, u8 irq_bits
)
105 struct emac_sgmii
*phy
= &adpt
->phy
;
108 writel_relaxed(irq_bits
, phy
->base
+ EMAC_SGMII_PHY_INTERRUPT_CLEAR
);
109 writel_relaxed(IRQ_GLOBAL_CLEAR
, phy
->base
+ EMAC_SGMII_PHY_IRQ_CMD
);
110 /* Ensure interrupt clear command is written to HW */
113 /* After set the IRQ_GLOBAL_CLEAR bit, the status clearing must
114 * be confirmed before clearing the bits in other registers.
115 * It takes a few cycles for hw to clear the interrupt status.
117 if (readl_poll_timeout_atomic(phy
->base
+
118 EMAC_SGMII_PHY_INTERRUPT_STATUS
,
119 status
, !(status
& irq_bits
), 1,
120 SGMII_PHY_IRQ_CLR_WAIT_TIME
)) {
121 net_err_ratelimited("%s: failed to clear SGMII irq: status:0x%x bits:0x%x\n",
122 adpt
->netdev
->name
, status
, irq_bits
);
126 /* Finalize clearing procedure */
127 writel_relaxed(0, phy
->base
+ EMAC_SGMII_PHY_IRQ_CMD
);
128 writel_relaxed(0, phy
->base
+ EMAC_SGMII_PHY_INTERRUPT_CLEAR
);
130 /* Ensure that clearing procedure finalization is written to HW */
136 /* The number of decode errors that triggers a reset */
137 #define DECODE_ERROR_LIMIT 2
139 static irqreturn_t
emac_sgmii_interrupt(int irq
, void *data
)
141 struct emac_adapter
*adpt
= data
;
142 struct emac_sgmii
*phy
= &adpt
->phy
;
145 status
= readl(phy
->base
+ EMAC_SGMII_PHY_INTERRUPT_STATUS
);
146 status
&= SGMII_ISR_MASK
;
150 /* If we get a decoding error and CDR is not locked, then try
151 * resetting the internal PHY. The internal PHY uses an embedded
152 * clock with Clock and Data Recovery (CDR) to recover the
155 if (status
& SGMII_PHY_INTERRUPT_ERR
) {
158 /* The SGMII is capable of recovering from some decode
159 * errors automatically. However, if we get multiple
160 * decode errors in a row, then assume that something
161 * is wrong and reset the interface.
163 count
= atomic_inc_return(&phy
->decode_error_count
);
164 if (count
== DECODE_ERROR_LIMIT
) {
165 schedule_work(&adpt
->work_thread
);
166 atomic_set(&phy
->decode_error_count
, 0);
169 /* We only care about consecutive decode errors. */
170 atomic_set(&phy
->decode_error_count
, 0);
173 if (emac_sgmii_irq_clear(adpt
, status
))
174 schedule_work(&adpt
->work_thread
);
179 static void emac_sgmii_reset_prepare(struct emac_adapter
*adpt
)
181 struct emac_sgmii
*phy
= &adpt
->phy
;
185 val
= readl(phy
->base
+ EMAC_EMAC_WRAPPER_CSR2
);
186 writel(((val
& ~PHY_RESET
) | PHY_RESET
), phy
->base
+
187 EMAC_EMAC_WRAPPER_CSR2
);
188 /* Ensure phy-reset command is written to HW before the release cmd */
190 val
= readl(phy
->base
+ EMAC_EMAC_WRAPPER_CSR2
);
191 writel((val
& ~PHY_RESET
), phy
->base
+ EMAC_EMAC_WRAPPER_CSR2
);
192 /* Ensure phy-reset release command is written to HW before initializing
198 static void emac_sgmii_common_reset(struct emac_adapter
*adpt
)
202 emac_sgmii_reset_prepare(adpt
);
203 emac_sgmii_link_init(adpt
);
205 ret
= emac_sgmii_init(adpt
);
207 netdev_err(adpt
->netdev
,
208 "could not reinitialize internal PHY (error=%i)\n",
212 static int emac_sgmii_common_open(struct emac_adapter
*adpt
)
214 struct emac_sgmii
*sgmii
= &adpt
->phy
;
218 /* Make sure interrupts are cleared and disabled first */
219 ret
= emac_sgmii_irq_clear(adpt
, 0xff);
222 writel(0, sgmii
->base
+ EMAC_SGMII_PHY_INTERRUPT_MASK
);
224 ret
= request_irq(sgmii
->irq
, emac_sgmii_interrupt
, 0,
227 netdev_err(adpt
->netdev
,
228 "could not register handler for internal PHY\n");
236 static void emac_sgmii_common_close(struct emac_adapter
*adpt
)
238 struct emac_sgmii
*sgmii
= &adpt
->phy
;
240 /* Make sure interrupts are disabled */
241 writel(0, sgmii
->base
+ EMAC_SGMII_PHY_INTERRUPT_MASK
);
242 free_irq(sgmii
->irq
, adpt
);
245 /* The error interrupts are only valid after the link is up */
246 static int emac_sgmii_common_link_change(struct emac_adapter
*adpt
, bool linkup
)
248 struct emac_sgmii
*sgmii
= &adpt
->phy
;
252 /* Clear and enable interrupts */
253 ret
= emac_sgmii_irq_clear(adpt
, 0xff);
257 writel(SGMII_ISR_MASK
,
258 sgmii
->base
+ EMAC_SGMII_PHY_INTERRUPT_MASK
);
260 /* Disable interrupts */
261 writel(0, sgmii
->base
+ EMAC_SGMII_PHY_INTERRUPT_MASK
);
262 synchronize_irq(sgmii
->irq
);
268 static struct sgmii_ops fsm9900_ops
= {
269 .init
= emac_sgmii_init_fsm9900
,
270 .open
= emac_sgmii_common_open
,
271 .close
= emac_sgmii_common_close
,
272 .link_change
= emac_sgmii_common_link_change
,
273 .reset
= emac_sgmii_common_reset
,
276 static struct sgmii_ops qdf2432_ops
= {
277 .init
= emac_sgmii_init_qdf2432
,
278 .open
= emac_sgmii_common_open
,
279 .close
= emac_sgmii_common_close
,
280 .link_change
= emac_sgmii_common_link_change
,
281 .reset
= emac_sgmii_common_reset
,
285 static struct sgmii_ops qdf2400_ops
= {
286 .init
= emac_sgmii_init_qdf2400
,
287 .open
= emac_sgmii_common_open
,
288 .close
= emac_sgmii_common_close
,
289 .link_change
= emac_sgmii_common_link_change
,
290 .reset
= emac_sgmii_common_reset
,
294 static int emac_sgmii_acpi_match(struct device
*dev
, void *data
)
297 static const struct acpi_device_id match_table
[] = {
303 const struct acpi_device_id
*id
= acpi_match_device(match_table
, dev
);
304 struct sgmii_ops
**ops
= data
;
307 acpi_handle handle
= ACPI_HANDLE(dev
);
308 unsigned long long hrv
;
311 status
= acpi_evaluate_integer(handle
, "_HRV", NULL
, &hrv
);
313 if (status
== AE_NOT_FOUND
)
314 /* Older versions of the QDF2432 ACPI tables do
315 * not have an _HRV property.
319 /* Something is wrong with the tables */
337 static const struct of_device_id emac_sgmii_dt_match
[] = {
339 .compatible
= "qcom,fsm9900-emac-sgmii",
340 .data
= &fsm9900_ops
,
343 .compatible
= "qcom,qdf2432-emac-sgmii",
344 .data
= &qdf2432_ops
,
349 int emac_sgmii_config(struct platform_device
*pdev
, struct emac_adapter
*adpt
)
351 struct platform_device
*sgmii_pdev
= NULL
;
352 struct emac_sgmii
*phy
= &adpt
->phy
;
353 struct resource
*res
;
356 if (has_acpi_companion(&pdev
->dev
)) {
359 dev
= device_find_child(&pdev
->dev
, &phy
->sgmii_ops
,
360 emac_sgmii_acpi_match
);
363 dev_warn(&pdev
->dev
, "cannot find internal phy node\n");
367 sgmii_pdev
= to_platform_device(dev
);
369 const struct of_device_id
*match
;
370 struct device_node
*np
;
372 np
= of_parse_phandle(pdev
->dev
.of_node
, "internal-phy", 0);
374 dev_err(&pdev
->dev
, "missing internal-phy property\n");
378 sgmii_pdev
= of_find_device_by_node(np
);
381 dev_err(&pdev
->dev
, "invalid internal-phy property\n");
385 match
= of_match_device(emac_sgmii_dt_match
, &sgmii_pdev
->dev
);
387 dev_err(&pdev
->dev
, "unrecognized internal phy node\n");
389 goto error_put_device
;
392 phy
->sgmii_ops
= (struct sgmii_ops
*)match
->data
;
395 /* Base address is the first address */
396 res
= platform_get_resource(sgmii_pdev
, IORESOURCE_MEM
, 0);
399 goto error_put_device
;
402 phy
->base
= ioremap(res
->start
, resource_size(res
));
405 goto error_put_device
;
408 /* v2 SGMII has a per-lane digital digital, so parse it if it exists */
409 res
= platform_get_resource(sgmii_pdev
, IORESOURCE_MEM
, 1);
411 phy
->digital
= ioremap(res
->start
, resource_size(res
));
414 goto error_unmap_base
;
418 ret
= emac_sgmii_init(adpt
);
422 emac_sgmii_link_init(adpt
);
424 ret
= platform_get_irq(sgmii_pdev
, 0);
428 /* We've remapped the addresses, so we don't need the device any
429 * more. of_find_device_by_node() says we should release it.
431 put_device(&sgmii_pdev
->dev
);
437 iounmap(phy
->digital
);
441 put_device(&sgmii_pdev
->dev
);