2 * AMD 10Gb Ethernet driver
4 * This file is available to you under your choice of the following two
9 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
11 * This file is free software; you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or (at
14 * your option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * This file incorporates work covered by the following copyright and
26 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
27 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28 * Inc. unless otherwise expressly agreed to in writing between Synopsys
31 * The Software IS NOT an item of Licensed Software or Licensed Product
32 * under any End User Software License Agreement or Agreement for Licensed
33 * Product with Synopsys or any supplement thereto. Permission is hereby
34 * granted, free of charge, to any person obtaining a copy of this software
35 * annotated with this license and the Software, to deal in the Software
36 * without restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38 * of the Software, and to permit persons to whom the Software is furnished
39 * to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included
42 * in all copies or substantial portions of the Software.
44 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
57 * License 2: Modified BSD
59 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
60 * All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions are met:
64 * * Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * * Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * * Neither the name of Advanced Micro Devices, Inc. nor the
70 * names of its contributors may be used to endorse or promote products
71 * derived from this software without specific prior written permission.
73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84 * This file incorporates work covered by the following copyright and
86 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
87 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88 * Inc. unless otherwise expressly agreed to in writing between Synopsys
91 * The Software IS NOT an item of Licensed Software or Licensed Product
92 * under any End User Software License Agreement or Agreement for Licensed
93 * Product with Synopsys or any supplement thereto. Permission is hereby
94 * granted, free of charge, to any person obtaining a copy of this software
95 * annotated with this license and the Software, to deal in the Software
96 * without restriction, including without limitation the rights to use,
97 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98 * of the Software, and to permit persons to whom the Software is furnished
99 * to do so, subject to the following conditions:
101 * The above copyright notice and this permission notice shall be included
102 * in all copies or substantial portions of the Software.
104 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 * THE POSSIBILITY OF SUCH DAMAGE.
117 #include <linux/module.h>
118 #include <linux/device.h>
119 #include <linux/platform_device.h>
120 #include <linux/spinlock.h>
121 #include <linux/netdevice.h>
122 #include <linux/etherdevice.h>
123 #include <linux/io.h>
124 #include <linux/of.h>
125 #include <linux/of_net.h>
126 #include <linux/of_address.h>
127 #include <linux/of_platform.h>
128 #include <linux/of_device.h>
129 #include <linux/clk.h>
130 #include <linux/property.h>
131 #include <linux/acpi.h>
132 #include <linux/mdio.h>
135 #include "xgbe-common.h"
138 static const struct acpi_device_id xgbe_acpi_match[];
140 static struct xgbe_version_data *xgbe_acpi_vdata(struct xgbe_prv_data *pdata)
142 const struct acpi_device_id *id;
144 id = acpi_match_device(xgbe_acpi_match, pdata->dev);
146 return id ? (struct xgbe_version_data *)id->driver_data : NULL;
149 static int xgbe_acpi_support(struct xgbe_prv_data *pdata)
151 struct device *dev = pdata->dev;
155 /* Obtain the system clock setting */
156 ret = device_property_read_u32(dev, XGBE_ACPI_DMA_FREQ, &property);
158 dev_err(dev, "unable to obtain %s property\n",
162 pdata->sysclk_rate = property;
164 /* Obtain the PTP clock setting */
165 ret = device_property_read_u32(dev, XGBE_ACPI_PTP_FREQ, &property);
167 dev_err(dev, "unable to obtain %s property\n",
171 pdata->ptpclk_rate = property;
175 #else /* CONFIG_ACPI */
176 static struct xgbe_version_data *xgbe_acpi_vdata(struct xgbe_prv_data *pdata)
181 static int xgbe_acpi_support(struct xgbe_prv_data *pdata)
185 #endif /* CONFIG_ACPI */
188 static const struct of_device_id xgbe_of_match[];
190 static struct xgbe_version_data *xgbe_of_vdata(struct xgbe_prv_data *pdata)
192 const struct of_device_id *id;
194 id = of_match_device(xgbe_of_match, pdata->dev);
196 return id ? (struct xgbe_version_data *)id->data : NULL;
199 static int xgbe_of_support(struct xgbe_prv_data *pdata)
201 struct device *dev = pdata->dev;
203 /* Obtain the system clock setting */
204 pdata->sysclk = devm_clk_get(dev, XGBE_DMA_CLOCK);
205 if (IS_ERR(pdata->sysclk)) {
206 dev_err(dev, "dma devm_clk_get failed\n");
207 return PTR_ERR(pdata->sysclk);
209 pdata->sysclk_rate = clk_get_rate(pdata->sysclk);
211 /* Obtain the PTP clock setting */
212 pdata->ptpclk = devm_clk_get(dev, XGBE_PTP_CLOCK);
213 if (IS_ERR(pdata->ptpclk)) {
214 dev_err(dev, "ptp devm_clk_get failed\n");
215 return PTR_ERR(pdata->ptpclk);
217 pdata->ptpclk_rate = clk_get_rate(pdata->ptpclk);
222 static struct platform_device *xgbe_of_get_phy_pdev(struct xgbe_prv_data *pdata)
224 struct device *dev = pdata->dev;
225 struct device_node *phy_node;
226 struct platform_device *phy_pdev;
228 phy_node = of_parse_phandle(dev->of_node, "phy-handle", 0);
230 /* Old style device tree:
231 * The XGBE and PHY resources are separate
233 phy_pdev = of_find_device_by_node(phy_node);
234 of_node_put(phy_node);
236 /* New style device tree:
237 * The XGBE and PHY resources are grouped together with
238 * the PHY resources listed last
241 phy_pdev = pdata->platdev;
246 #else /* CONFIG_OF */
247 static struct xgbe_version_data *xgbe_of_vdata(struct xgbe_prv_data *pdata)
252 static int xgbe_of_support(struct xgbe_prv_data *pdata)
257 static struct platform_device *xgbe_of_get_phy_pdev(struct xgbe_prv_data *pdata)
261 #endif /* CONFIG_OF */
263 static unsigned int xgbe_resource_count(struct platform_device *pdev,
269 for (i = 0, count = 0; i < pdev->num_resources; i++) {
270 struct resource *res = &pdev->resource[i];
272 if (type == resource_type(res))
279 static struct platform_device *xgbe_get_phy_pdev(struct xgbe_prv_data *pdata)
281 struct platform_device *phy_pdev;
283 if (pdata->use_acpi) {
284 get_device(pdata->dev);
285 phy_pdev = pdata->platdev;
287 phy_pdev = xgbe_of_get_phy_pdev(pdata);
293 static struct xgbe_version_data *xgbe_get_vdata(struct xgbe_prv_data *pdata)
295 return pdata->use_acpi ? xgbe_acpi_vdata(pdata)
296 : xgbe_of_vdata(pdata);
299 static int xgbe_platform_probe(struct platform_device *pdev)
301 struct xgbe_prv_data *pdata;
302 struct device *dev = &pdev->dev;
303 struct platform_device *phy_pdev;
304 struct resource *res;
305 const char *phy_mode;
306 unsigned int phy_memnum, phy_irqnum;
307 unsigned int dma_irqnum, dma_irqend;
308 enum dev_dma_attr attr;
311 pdata = xgbe_alloc_pdata(dev);
313 ret = PTR_ERR(pdata);
317 pdata->platdev = pdev;
318 pdata->adev = ACPI_COMPANION(dev);
319 platform_set_drvdata(pdev, pdata);
321 /* Check if we should use ACPI or DT */
322 pdata->use_acpi = dev->of_node ? 0 : 1;
324 /* Get the version data */
325 pdata->vdata = xgbe_get_vdata(pdata);
327 phy_pdev = xgbe_get_phy_pdev(pdata);
329 dev_err(dev, "unable to obtain phy device\n");
333 pdata->phy_platdev = phy_pdev;
334 pdata->phy_dev = &phy_pdev->dev;
336 if (pdev == phy_pdev) {
337 /* New style device tree or ACPI:
338 * The XGBE and PHY resources are grouped together with
339 * the PHY resources listed last
341 phy_memnum = xgbe_resource_count(pdev, IORESOURCE_MEM) - 3;
342 phy_irqnum = xgbe_resource_count(pdev, IORESOURCE_IRQ) - 1;
344 dma_irqend = phy_irqnum;
346 /* Old style device tree:
347 * The XGBE and PHY resources are separate
352 dma_irqend = xgbe_resource_count(pdev, IORESOURCE_IRQ);
355 /* Obtain the mmio areas for the device */
356 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
357 pdata->xgmac_regs = devm_ioremap_resource(dev, res);
358 if (IS_ERR(pdata->xgmac_regs)) {
359 dev_err(dev, "xgmac ioremap failed\n");
360 ret = PTR_ERR(pdata->xgmac_regs);
363 if (netif_msg_probe(pdata))
364 dev_dbg(dev, "xgmac_regs = %p\n", pdata->xgmac_regs);
366 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
367 pdata->xpcs_regs = devm_ioremap_resource(dev, res);
368 if (IS_ERR(pdata->xpcs_regs)) {
369 dev_err(dev, "xpcs ioremap failed\n");
370 ret = PTR_ERR(pdata->xpcs_regs);
373 if (netif_msg_probe(pdata))
374 dev_dbg(dev, "xpcs_regs = %p\n", pdata->xpcs_regs);
376 res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
377 pdata->rxtx_regs = devm_ioremap_resource(dev, res);
378 if (IS_ERR(pdata->rxtx_regs)) {
379 dev_err(dev, "rxtx ioremap failed\n");
380 ret = PTR_ERR(pdata->rxtx_regs);
383 if (netif_msg_probe(pdata))
384 dev_dbg(dev, "rxtx_regs = %p\n", pdata->rxtx_regs);
386 res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
387 pdata->sir0_regs = devm_ioremap_resource(dev, res);
388 if (IS_ERR(pdata->sir0_regs)) {
389 dev_err(dev, "sir0 ioremap failed\n");
390 ret = PTR_ERR(pdata->sir0_regs);
393 if (netif_msg_probe(pdata))
394 dev_dbg(dev, "sir0_regs = %p\n", pdata->sir0_regs);
396 res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
397 pdata->sir1_regs = devm_ioremap_resource(dev, res);
398 if (IS_ERR(pdata->sir1_regs)) {
399 dev_err(dev, "sir1 ioremap failed\n");
400 ret = PTR_ERR(pdata->sir1_regs);
403 if (netif_msg_probe(pdata))
404 dev_dbg(dev, "sir1_regs = %p\n", pdata->sir1_regs);
406 /* Retrieve the MAC address */
407 ret = device_property_read_u8_array(dev, XGBE_MAC_ADDR_PROPERTY,
409 sizeof(pdata->mac_addr));
410 if (ret || !is_valid_ether_addr(pdata->mac_addr)) {
411 dev_err(dev, "invalid %s property\n", XGBE_MAC_ADDR_PROPERTY);
417 /* Retrieve the PHY mode - it must be "xgmii" */
418 ret = device_property_read_string(dev, XGBE_PHY_MODE_PROPERTY,
420 if (ret || strcmp(phy_mode, phy_modes(PHY_INTERFACE_MODE_XGMII))) {
421 dev_err(dev, "invalid %s property\n", XGBE_PHY_MODE_PROPERTY);
426 pdata->phy_mode = PHY_INTERFACE_MODE_XGMII;
428 /* Check for per channel interrupt support */
429 if (device_property_present(dev, XGBE_DMA_IRQS_PROPERTY)) {
430 pdata->per_channel_irq = 1;
431 pdata->channel_irq_mode = XGBE_IRQ_MODE_EDGE;
434 /* Obtain device settings unique to ACPI/OF */
436 ret = xgbe_acpi_support(pdata);
438 ret = xgbe_of_support(pdata);
442 /* Set the DMA coherency values */
443 attr = device_get_dma_attr(dev);
444 if (attr == DEV_DMA_NOT_SUPPORTED) {
445 dev_err(dev, "DMA is not supported");
449 pdata->coherent = (attr == DEV_DMA_COHERENT);
450 if (pdata->coherent) {
451 pdata->arcr = XGBE_DMA_OS_ARCR;
452 pdata->awcr = XGBE_DMA_OS_AWCR;
454 pdata->arcr = XGBE_DMA_SYS_ARCR;
455 pdata->awcr = XGBE_DMA_SYS_AWCR;
458 /* Set the maximum fifo amounts */
459 pdata->tx_max_fifo_size = pdata->vdata->tx_max_fifo_size;
460 pdata->rx_max_fifo_size = pdata->vdata->rx_max_fifo_size;
462 /* Set the hardware channel and queue counts */
463 xgbe_set_counts(pdata);
465 /* Always have XGMAC and XPCS (auto-negotiation) interrupts */
466 pdata->irq_count = 2;
468 /* Get the device interrupt */
469 ret = platform_get_irq(pdev, 0);
471 dev_err(dev, "platform_get_irq 0 failed\n");
474 pdata->dev_irq = ret;
476 /* Get the per channel DMA interrupts */
477 if (pdata->per_channel_irq) {
478 unsigned int i, max = ARRAY_SIZE(pdata->channel_irq);
480 for (i = 0; (i < max) && (dma_irqnum < dma_irqend); i++) {
481 ret = platform_get_irq(pdata->platdev, dma_irqnum++);
483 netdev_err(pdata->netdev,
484 "platform_get_irq %u failed\n",
489 pdata->channel_irq[i] = ret;
492 pdata->channel_irq_count = max;
494 pdata->irq_count += max;
497 /* Get the auto-negotiation interrupt */
498 ret = platform_get_irq(phy_pdev, phy_irqnum++);
500 dev_err(dev, "platform_get_irq phy 0 failed\n");
505 /* Configure the netdev resource */
506 ret = xgbe_config_netdev(pdata);
510 netdev_notice(pdata->netdev, "net device enabled\n");
515 platform_device_put(phy_pdev);
518 xgbe_free_pdata(pdata);
521 dev_notice(dev, "net device not enabled\n");
526 static int xgbe_platform_remove(struct platform_device *pdev)
528 struct xgbe_prv_data *pdata = platform_get_drvdata(pdev);
530 xgbe_deconfig_netdev(pdata);
532 platform_device_put(pdata->phy_platdev);
534 xgbe_free_pdata(pdata);
539 #ifdef CONFIG_PM_SLEEP
540 static int xgbe_platform_suspend(struct device *dev)
542 struct xgbe_prv_data *pdata = dev_get_drvdata(dev);
543 struct net_device *netdev = pdata->netdev;
546 DBGPR("-->xgbe_suspend\n");
548 if (netif_running(netdev))
549 ret = xgbe_powerdown(netdev, XGMAC_DRIVER_CONTEXT);
551 pdata->lpm_ctrl = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
552 pdata->lpm_ctrl |= MDIO_CTRL1_LPOWER;
553 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
555 DBGPR("<--xgbe_suspend\n");
560 static int xgbe_platform_resume(struct device *dev)
562 struct xgbe_prv_data *pdata = dev_get_drvdata(dev);
563 struct net_device *netdev = pdata->netdev;
566 DBGPR("-->xgbe_resume\n");
568 pdata->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
569 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
571 if (netif_running(netdev)) {
572 ret = xgbe_powerup(netdev, XGMAC_DRIVER_CONTEXT);
574 /* Schedule a restart in case the link or phy state changed
575 * while we were powered down.
577 schedule_work(&pdata->restart_work);
580 DBGPR("<--xgbe_resume\n");
584 #endif /* CONFIG_PM_SLEEP */
586 static const struct xgbe_version_data xgbe_v1 = {
587 .init_function_ptrs_phy_impl = xgbe_init_function_ptrs_phy_v1,
588 .xpcs_access = XGBE_XPCS_ACCESS_V1,
589 .tx_max_fifo_size = 81920,
590 .rx_max_fifo_size = 81920,
591 .tx_tstamp_workaround = 1,
595 static const struct acpi_device_id xgbe_acpi_match[] = {
597 .driver_data = (kernel_ulong_t)&xgbe_v1 },
601 MODULE_DEVICE_TABLE(acpi, xgbe_acpi_match);
605 static const struct of_device_id xgbe_of_match[] = {
606 { .compatible = "amd,xgbe-seattle-v1a",
611 MODULE_DEVICE_TABLE(of, xgbe_of_match);
614 static SIMPLE_DEV_PM_OPS(xgbe_platform_pm_ops,
615 xgbe_platform_suspend, xgbe_platform_resume);
617 static struct platform_driver xgbe_driver = {
619 .name = XGBE_DRV_NAME,
621 .acpi_match_table = xgbe_acpi_match,
624 .of_match_table = xgbe_of_match,
626 .pm = &xgbe_platform_pm_ops,
628 .probe = xgbe_platform_probe,
629 .remove = xgbe_platform_remove,
632 int xgbe_platform_init(void)
634 return platform_driver_register(&xgbe_driver);
637 void xgbe_platform_exit(void)
639 platform_driver_unregister(&xgbe_driver);