1*25763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26465859aSSunil Goutham /*
36465859aSSunil Goutham * Copyright (C) 2016 Cavium, Inc.
46465859aSSunil Goutham */
56465859aSSunil Goutham
66465859aSSunil Goutham #include <linux/acpi.h>
76465859aSSunil Goutham #include <linux/module.h>
86465859aSSunil Goutham #include <linux/interrupt.h>
96465859aSSunil Goutham #include <linux/pci.h>
106465859aSSunil Goutham #include <linux/netdevice.h>
116465859aSSunil Goutham #include <linux/etherdevice.h>
126465859aSSunil Goutham #include <linux/phy.h>
136465859aSSunil Goutham #include <linux/of.h>
146465859aSSunil Goutham #include <linux/of_mdio.h>
156465859aSSunil Goutham #include <linux/of_net.h>
166465859aSSunil Goutham
176465859aSSunil Goutham #include "nic.h"
186465859aSSunil Goutham #include "thunder_bgx.h"
196465859aSSunil Goutham
206b9e6547SVadim Lomovtsev #define DRV_NAME "thunder_xcv"
216465859aSSunil Goutham #define DRV_VERSION "1.0"
226465859aSSunil Goutham
236465859aSSunil Goutham /* Register offsets */
246465859aSSunil Goutham #define XCV_RESET 0x00
256465859aSSunil Goutham #define PORT_EN BIT_ULL(63)
266465859aSSunil Goutham #define CLK_RESET BIT_ULL(15)
276465859aSSunil Goutham #define DLL_RESET BIT_ULL(11)
286465859aSSunil Goutham #define COMP_EN BIT_ULL(7)
296465859aSSunil Goutham #define TX_PKT_RESET BIT_ULL(3)
306465859aSSunil Goutham #define TX_DATA_RESET BIT_ULL(2)
316465859aSSunil Goutham #define RX_PKT_RESET BIT_ULL(1)
326465859aSSunil Goutham #define RX_DATA_RESET BIT_ULL(0)
336465859aSSunil Goutham #define XCV_DLL_CTL 0x10
346465859aSSunil Goutham #define CLKRX_BYP BIT_ULL(23)
356465859aSSunil Goutham #define CLKTX_BYP BIT_ULL(15)
366465859aSSunil Goutham #define XCV_COMP_CTL 0x20
376465859aSSunil Goutham #define DRV_BYP BIT_ULL(63)
386465859aSSunil Goutham #define XCV_CTL 0x30
396465859aSSunil Goutham #define XCV_INT 0x40
406465859aSSunil Goutham #define XCV_INT_W1S 0x48
416465859aSSunil Goutham #define XCV_INT_ENA_W1C 0x50
426465859aSSunil Goutham #define XCV_INT_ENA_W1S 0x58
436465859aSSunil Goutham #define XCV_INBND_STATUS 0x80
446465859aSSunil Goutham #define XCV_BATCH_CRD_RET 0x100
456465859aSSunil Goutham
466465859aSSunil Goutham struct xcv {
476465859aSSunil Goutham void __iomem *reg_base;
486465859aSSunil Goutham struct pci_dev *pdev;
496465859aSSunil Goutham };
506465859aSSunil Goutham
516465859aSSunil Goutham static struct xcv *xcv;
526465859aSSunil Goutham
536465859aSSunil Goutham /* Supported devices */
546465859aSSunil Goutham static const struct pci_device_id xcv_id_table[] = {
556465859aSSunil Goutham { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xA056) },
566465859aSSunil Goutham { 0, } /* end of table */
576465859aSSunil Goutham };
586465859aSSunil Goutham
596465859aSSunil Goutham MODULE_AUTHOR("Cavium Inc");
606465859aSSunil Goutham MODULE_DESCRIPTION("Cavium Thunder RGX/XCV Driver");
616465859aSSunil Goutham MODULE_LICENSE("GPL v2");
626465859aSSunil Goutham MODULE_VERSION(DRV_VERSION);
636465859aSSunil Goutham MODULE_DEVICE_TABLE(pci, xcv_id_table);
646465859aSSunil Goutham
xcv_init_hw(void)656465859aSSunil Goutham void xcv_init_hw(void)
666465859aSSunil Goutham {
676465859aSSunil Goutham u64 cfg;
686465859aSSunil Goutham
696465859aSSunil Goutham /* Take DLL out of reset */
706465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
716465859aSSunil Goutham cfg &= ~DLL_RESET;
726465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
736465859aSSunil Goutham
746465859aSSunil Goutham /* Take clock tree out of reset */
756465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
766465859aSSunil Goutham cfg &= ~CLK_RESET;
776465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
786465859aSSunil Goutham /* Wait for DLL to lock */
796465859aSSunil Goutham msleep(1);
806465859aSSunil Goutham
816465859aSSunil Goutham /* Configure DLL - enable or bypass
826465859aSSunil Goutham * TX no bypass, RX bypass
836465859aSSunil Goutham */
846465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_DLL_CTL);
856465859aSSunil Goutham cfg &= ~0xFF03;
866465859aSSunil Goutham cfg |= CLKRX_BYP;
876465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_DLL_CTL);
886465859aSSunil Goutham
896465859aSSunil Goutham /* Enable compensation controller and force the
906465859aSSunil Goutham * write to be visible to HW by readig back.
916465859aSSunil Goutham */
926465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
936465859aSSunil Goutham cfg |= COMP_EN;
946465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
956465859aSSunil Goutham readq_relaxed(xcv->reg_base + XCV_RESET);
966465859aSSunil Goutham /* Wait for compensation state machine to lock */
976465859aSSunil Goutham msleep(10);
986465859aSSunil Goutham
996465859aSSunil Goutham /* enable the XCV block */
1006465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
1016465859aSSunil Goutham cfg |= PORT_EN;
1026465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
1036465859aSSunil Goutham
1046465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
1056465859aSSunil Goutham cfg |= CLK_RESET;
1066465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
1076465859aSSunil Goutham }
1086465859aSSunil Goutham EXPORT_SYMBOL(xcv_init_hw);
1096465859aSSunil Goutham
xcv_setup_link(bool link_up,int link_speed)1106465859aSSunil Goutham void xcv_setup_link(bool link_up, int link_speed)
1116465859aSSunil Goutham {
1126465859aSSunil Goutham u64 cfg;
1136465859aSSunil Goutham int speed = 2;
1146465859aSSunil Goutham
1156465859aSSunil Goutham if (!xcv) {
116c73e4426SVincent pr_err("XCV init not done, probe may have failed\n");
1176465859aSSunil Goutham return;
1186465859aSSunil Goutham }
1196465859aSSunil Goutham
1206465859aSSunil Goutham if (link_speed == 100)
1216465859aSSunil Goutham speed = 1;
1226465859aSSunil Goutham else if (link_speed == 10)
1236465859aSSunil Goutham speed = 0;
1246465859aSSunil Goutham
1256465859aSSunil Goutham if (link_up) {
1266465859aSSunil Goutham /* set operating speed */
1276465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_CTL);
1286465859aSSunil Goutham cfg &= ~0x03;
1296465859aSSunil Goutham cfg |= speed;
1306465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_CTL);
1316465859aSSunil Goutham
1326465859aSSunil Goutham /* Reset datapaths */
1336465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
1346465859aSSunil Goutham cfg |= TX_DATA_RESET | RX_DATA_RESET;
1356465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
1366465859aSSunil Goutham
1376465859aSSunil Goutham /* Enable the packet flow */
1386465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
1396465859aSSunil Goutham cfg |= TX_PKT_RESET | RX_PKT_RESET;
1406465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
1416465859aSSunil Goutham
1426465859aSSunil Goutham /* Return credits to RGX */
1436465859aSSunil Goutham writeq_relaxed(0x01, xcv->reg_base + XCV_BATCH_CRD_RET);
1446465859aSSunil Goutham } else {
1456465859aSSunil Goutham /* Disable packet flow */
1466465859aSSunil Goutham cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
1476465859aSSunil Goutham cfg &= ~(TX_PKT_RESET | RX_PKT_RESET);
1486465859aSSunil Goutham writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
1496465859aSSunil Goutham readq_relaxed(xcv->reg_base + XCV_RESET);
1506465859aSSunil Goutham }
1516465859aSSunil Goutham }
1526465859aSSunil Goutham EXPORT_SYMBOL(xcv_setup_link);
1536465859aSSunil Goutham
xcv_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1546465859aSSunil Goutham static int xcv_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1556465859aSSunil Goutham {
1566465859aSSunil Goutham int err;
1576465859aSSunil Goutham struct device *dev = &pdev->dev;
1586465859aSSunil Goutham
1596465859aSSunil Goutham xcv = devm_kzalloc(dev, sizeof(struct xcv), GFP_KERNEL);
1606465859aSSunil Goutham if (!xcv)
1616465859aSSunil Goutham return -ENOMEM;
1626465859aSSunil Goutham xcv->pdev = pdev;
1636465859aSSunil Goutham
1646465859aSSunil Goutham pci_set_drvdata(pdev, xcv);
1656465859aSSunil Goutham
1666465859aSSunil Goutham err = pci_enable_device(pdev);
1676465859aSSunil Goutham if (err) {
1686465859aSSunil Goutham dev_err(dev, "Failed to enable PCI device\n");
1696465859aSSunil Goutham goto err_kfree;
1706465859aSSunil Goutham }
1716465859aSSunil Goutham
1726465859aSSunil Goutham err = pci_request_regions(pdev, DRV_NAME);
1736465859aSSunil Goutham if (err) {
1746465859aSSunil Goutham dev_err(dev, "PCI request regions failed 0x%x\n", err);
1756465859aSSunil Goutham goto err_disable_device;
1766465859aSSunil Goutham }
1776465859aSSunil Goutham
1786465859aSSunil Goutham /* MAP configuration registers */
1796465859aSSunil Goutham xcv->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1806465859aSSunil Goutham if (!xcv->reg_base) {
1816465859aSSunil Goutham dev_err(dev, "XCV: Cannot map CSR memory space, aborting\n");
1826465859aSSunil Goutham err = -ENOMEM;
1836465859aSSunil Goutham goto err_release_regions;
1846465859aSSunil Goutham }
1856465859aSSunil Goutham
1866465859aSSunil Goutham return 0;
1876465859aSSunil Goutham
1886465859aSSunil Goutham err_release_regions:
1896465859aSSunil Goutham pci_release_regions(pdev);
1906465859aSSunil Goutham err_disable_device:
1916465859aSSunil Goutham pci_disable_device(pdev);
1926465859aSSunil Goutham err_kfree:
1936465859aSSunil Goutham devm_kfree(dev, xcv);
1946465859aSSunil Goutham xcv = NULL;
1956465859aSSunil Goutham return err;
1966465859aSSunil Goutham }
1976465859aSSunil Goutham
xcv_remove(struct pci_dev * pdev)1986465859aSSunil Goutham static void xcv_remove(struct pci_dev *pdev)
1996465859aSSunil Goutham {
2006465859aSSunil Goutham struct device *dev = &pdev->dev;
2016465859aSSunil Goutham
2026465859aSSunil Goutham if (xcv) {
2036465859aSSunil Goutham devm_kfree(dev, xcv);
2046465859aSSunil Goutham xcv = NULL;
2056465859aSSunil Goutham }
2066465859aSSunil Goutham
2076465859aSSunil Goutham pci_release_regions(pdev);
2086465859aSSunil Goutham pci_disable_device(pdev);
2096465859aSSunil Goutham }
2106465859aSSunil Goutham
2116465859aSSunil Goutham static struct pci_driver xcv_driver = {
2126465859aSSunil Goutham .name = DRV_NAME,
2136465859aSSunil Goutham .id_table = xcv_id_table,
2146465859aSSunil Goutham .probe = xcv_probe,
2156465859aSSunil Goutham .remove = xcv_remove,
2166465859aSSunil Goutham };
2176465859aSSunil Goutham
xcv_init_module(void)2186465859aSSunil Goutham static int __init xcv_init_module(void)
2196465859aSSunil Goutham {
2206465859aSSunil Goutham pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
2216465859aSSunil Goutham
2226465859aSSunil Goutham return pci_register_driver(&xcv_driver);
2236465859aSSunil Goutham }
2246465859aSSunil Goutham
xcv_cleanup_module(void)2256465859aSSunil Goutham static void __exit xcv_cleanup_module(void)
2266465859aSSunil Goutham {
2276465859aSSunil Goutham pci_unregister_driver(&xcv_driver);
2286465859aSSunil Goutham }
2296465859aSSunil Goutham
2306465859aSSunil Goutham module_init(xcv_init_module);
2316465859aSSunil Goutham module_exit(xcv_cleanup_module);
232