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>
12 #include <linux/of_device.h>
13 #include <linux/of_platform.h>
16 #include "emac-sgmii.h"
18 /* EMAC_SGMII register offsets */
19 #define EMAC_SGMII_PHY_AUTONEG_CFG2 0x0048
20 #define EMAC_SGMII_PHY_SPEED_CFG1 0x0074
21 #define EMAC_SGMII_PHY_IRQ_CMD 0x00ac
22 #define EMAC_SGMII_PHY_INTERRUPT_CLEAR 0x00b0
23 #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
24 #define EMAC_SGMII_PHY_INTERRUPT_STATUS 0x00b8
25 #define EMAC_SGMII_PHY_RX_CHK_STATUS 0x00d4
27 #define FORCE_AN_TX_CFG BIT(5)
28 #define FORCE_AN_RX_CFG BIT(4)
29 #define AN_ENABLE BIT(0)
31 #define DUPLEX_MODE BIT(4)
32 #define SPDMODE_1000 BIT(1)
33 #define SPDMODE_100 BIT(0)
36 #define CDR_ALIGN_DET BIT(6)
38 #define IRQ_GLOBAL_CLEAR BIT(0)
40 #define DECODE_CODE_ERR BIT(7)
41 #define DECODE_DISP_ERR BIT(6)
43 #define SGMII_PHY_IRQ_CLR_WAIT_TIME 10
45 #define SGMII_PHY_INTERRUPT_ERR (DECODE_CODE_ERR | DECODE_DISP_ERR)
46 #define SGMII_ISR_MASK (SGMII_PHY_INTERRUPT_ERR)
48 #define SERDES_START_WAIT_TIMES 100
50 int emac_sgmii_init(struct emac_adapter *adpt)
52 if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->init))
55 return adpt->phy.sgmii_ops->init(adpt);
58 int emac_sgmii_open(struct emac_adapter *adpt)
60 if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->open))
63 return adpt->phy.sgmii_ops->open(adpt);
66 void emac_sgmii_close(struct emac_adapter *adpt)
68 if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->close))
71 adpt->phy.sgmii_ops->close(adpt);
74 int emac_sgmii_link_change(struct emac_adapter *adpt, bool link_state)
76 if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->link_change))
79 return adpt->phy.sgmii_ops->link_change(adpt, link_state);
82 void emac_sgmii_reset(struct emac_adapter *adpt)
84 if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->reset))
87 adpt->phy.sgmii_ops->reset(adpt);
90 /* Initialize the SGMII link between the internal and external PHYs. */
91 static void emac_sgmii_link_init(struct emac_adapter *adpt)
93 struct emac_sgmii *phy = &adpt->phy;
96 /* Always use autonegotiation. It works no matter how the external
99 val = readl(phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
100 val &= ~(FORCE_AN_RX_CFG | FORCE_AN_TX_CFG);
102 writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
105 static int emac_sgmii_irq_clear(struct emac_adapter *adpt, u8 irq_bits)
107 struct emac_sgmii *phy = &adpt->phy;
110 writel_relaxed(irq_bits, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
111 writel_relaxed(IRQ_GLOBAL_CLEAR, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
112 /* Ensure interrupt clear command is written to HW */
115 /* After set the IRQ_GLOBAL_CLEAR bit, the status clearing must
116 * be confirmed before clearing the bits in other registers.
117 * It takes a few cycles for hw to clear the interrupt status.
119 if (readl_poll_timeout_atomic(phy->base +
120 EMAC_SGMII_PHY_INTERRUPT_STATUS,
121 status, !(status & irq_bits), 1,
122 SGMII_PHY_IRQ_CLR_WAIT_TIME)) {
123 net_err_ratelimited("%s: failed to clear SGMII irq: status:0x%x bits:0x%x\n",
124 adpt->netdev->name, status, irq_bits);
128 /* Finalize clearing procedure */
129 writel_relaxed(0, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
130 writel_relaxed(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
132 /* Ensure that clearing procedure finalization is written to HW */
138 /* The number of decode errors that triggers a reset */
139 #define DECODE_ERROR_LIMIT 2
141 static irqreturn_t emac_sgmii_interrupt(int irq, void *data)
143 struct emac_adapter *adpt = data;
144 struct emac_sgmii *phy = &adpt->phy;
147 status = readl(phy->base + EMAC_SGMII_PHY_INTERRUPT_STATUS);
148 status &= SGMII_ISR_MASK;
152 /* If we get a decoding error and CDR is not locked, then try
153 * resetting the internal PHY. The internal PHY uses an embedded
154 * clock with Clock and Data Recovery (CDR) to recover the
157 if (status & SGMII_PHY_INTERRUPT_ERR) {
160 /* The SGMII is capable of recovering from some decode
161 * errors automatically. However, if we get multiple
162 * decode errors in a row, then assume that something
163 * is wrong and reset the interface.
165 count = atomic_inc_return(&phy->decode_error_count);
166 if (count == DECODE_ERROR_LIMIT) {
167 schedule_work(&adpt->work_thread);
168 atomic_set(&phy->decode_error_count, 0);
171 /* We only care about consecutive decode errors. */
172 atomic_set(&phy->decode_error_count, 0);
175 if (emac_sgmii_irq_clear(adpt, status))
176 schedule_work(&adpt->work_thread);
181 static void emac_sgmii_reset_prepare(struct emac_adapter *adpt)
183 struct emac_sgmii *phy = &adpt->phy;
187 val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
188 writel(((val & ~PHY_RESET) | PHY_RESET), phy->base +
189 EMAC_EMAC_WRAPPER_CSR2);
190 /* Ensure phy-reset command is written to HW before the release cmd */
192 val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
193 writel((val & ~PHY_RESET), phy->base + EMAC_EMAC_WRAPPER_CSR2);
194 /* Ensure phy-reset release command is written to HW before initializing
200 static void emac_sgmii_common_reset(struct emac_adapter *adpt)
204 emac_sgmii_reset_prepare(adpt);
205 emac_sgmii_link_init(adpt);
207 ret = emac_sgmii_init(adpt);
209 netdev_err(adpt->netdev,
210 "could not reinitialize internal PHY (error=%i)\n",
214 static int emac_sgmii_common_open(struct emac_adapter *adpt)
216 struct emac_sgmii *sgmii = &adpt->phy;
220 /* Make sure interrupts are cleared and disabled first */
221 ret = emac_sgmii_irq_clear(adpt, 0xff);
224 writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
226 ret = request_irq(sgmii->irq, emac_sgmii_interrupt, 0,
229 netdev_err(adpt->netdev,
230 "could not register handler for internal PHY\n");
238 static void emac_sgmii_common_close(struct emac_adapter *adpt)
240 struct emac_sgmii *sgmii = &adpt->phy;
242 /* Make sure interrupts are disabled */
243 writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
244 free_irq(sgmii->irq, adpt);
247 /* The error interrupts are only valid after the link is up */
248 static int emac_sgmii_common_link_change(struct emac_adapter *adpt, bool linkup)
250 struct emac_sgmii *sgmii = &adpt->phy;
254 /* Clear and enable interrupts */
255 ret = emac_sgmii_irq_clear(adpt, 0xff);
259 writel(SGMII_ISR_MASK,
260 sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
262 /* Disable interrupts */
263 writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
264 synchronize_irq(sgmii->irq);
270 static struct sgmii_ops fsm9900_ops = {
271 .init = emac_sgmii_init_fsm9900,
272 .open = emac_sgmii_common_open,
273 .close = emac_sgmii_common_close,
274 .link_change = emac_sgmii_common_link_change,
275 .reset = emac_sgmii_common_reset,
278 static struct sgmii_ops qdf2432_ops = {
279 .init = emac_sgmii_init_qdf2432,
280 .open = emac_sgmii_common_open,
281 .close = emac_sgmii_common_close,
282 .link_change = emac_sgmii_common_link_change,
283 .reset = emac_sgmii_common_reset,
287 static struct sgmii_ops qdf2400_ops = {
288 .init = emac_sgmii_init_qdf2400,
289 .open = emac_sgmii_common_open,
290 .close = emac_sgmii_common_close,
291 .link_change = emac_sgmii_common_link_change,
292 .reset = emac_sgmii_common_reset,
296 static int emac_sgmii_acpi_match(struct device *dev, void *data)
299 static const struct acpi_device_id match_table[] = {
305 const struct acpi_device_id *id = acpi_match_device(match_table, dev);
306 struct sgmii_ops **ops = data;
309 acpi_handle handle = ACPI_HANDLE(dev);
310 unsigned long long hrv;
313 status = acpi_evaluate_integer(handle, "_HRV", NULL, &hrv);
315 if (status == AE_NOT_FOUND)
316 /* Older versions of the QDF2432 ACPI tables do
317 * not have an _HRV property.
321 /* Something is wrong with the tables */
339 static const struct of_device_id emac_sgmii_dt_match[] = {
341 .compatible = "qcom,fsm9900-emac-sgmii",
342 .data = &fsm9900_ops,
345 .compatible = "qcom,qdf2432-emac-sgmii",
346 .data = &qdf2432_ops,
351 int emac_sgmii_config(struct platform_device *pdev, struct emac_adapter *adpt)
353 struct platform_device *sgmii_pdev = NULL;
354 struct emac_sgmii *phy = &adpt->phy;
355 struct resource *res;
358 if (has_acpi_companion(&pdev->dev)) {
361 dev = device_find_child(&pdev->dev, &phy->sgmii_ops,
362 emac_sgmii_acpi_match);
365 dev_warn(&pdev->dev, "cannot find internal phy node\n");
369 sgmii_pdev = to_platform_device(dev);
371 const struct of_device_id *match;
372 struct device_node *np;
374 np = of_parse_phandle(pdev->dev.of_node, "internal-phy", 0);
376 dev_err(&pdev->dev, "missing internal-phy property\n");
380 sgmii_pdev = of_find_device_by_node(np);
383 dev_err(&pdev->dev, "invalid internal-phy property\n");
387 match = of_match_device(emac_sgmii_dt_match, &sgmii_pdev->dev);
389 dev_err(&pdev->dev, "unrecognized internal phy node\n");
391 goto error_put_device;
394 phy->sgmii_ops = (struct sgmii_ops *)match->data;
397 /* Base address is the first address */
398 res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 0);
401 goto error_put_device;
404 phy->base = ioremap(res->start, resource_size(res));
407 goto error_put_device;
410 /* v2 SGMII has a per-lane digital digital, so parse it if it exists */
411 res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 1);
413 phy->digital = ioremap(res->start, resource_size(res));
416 goto error_unmap_base;
420 ret = emac_sgmii_init(adpt);
424 emac_sgmii_link_init(adpt);
426 ret = platform_get_irq(sgmii_pdev, 0);
430 /* We've remapped the addresses, so we don't need the device any
431 * more. of_find_device_by_node() says we should release it.
433 put_device(&sgmii_pdev->dev);
439 iounmap(phy->digital);
443 put_device(&sgmii_pdev->dev);