xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/cavium/thunder/thunder_xcv.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2016 Cavium, Inc.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/acpi.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/interrupt.h>
9*4882a593Smuzhiyun #include <linux/pci.h>
10*4882a593Smuzhiyun #include <linux/netdevice.h>
11*4882a593Smuzhiyun #include <linux/etherdevice.h>
12*4882a593Smuzhiyun #include <linux/phy.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/of_mdio.h>
15*4882a593Smuzhiyun #include <linux/of_net.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "nic.h"
18*4882a593Smuzhiyun #include "thunder_bgx.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define DRV_NAME	"thunder_xcv"
21*4882a593Smuzhiyun #define DRV_VERSION	"1.0"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Register offsets */
24*4882a593Smuzhiyun #define XCV_RESET		0x00
25*4882a593Smuzhiyun #define   PORT_EN		BIT_ULL(63)
26*4882a593Smuzhiyun #define   CLK_RESET		BIT_ULL(15)
27*4882a593Smuzhiyun #define   DLL_RESET		BIT_ULL(11)
28*4882a593Smuzhiyun #define   COMP_EN		BIT_ULL(7)
29*4882a593Smuzhiyun #define   TX_PKT_RESET		BIT_ULL(3)
30*4882a593Smuzhiyun #define   TX_DATA_RESET		BIT_ULL(2)
31*4882a593Smuzhiyun #define   RX_PKT_RESET		BIT_ULL(1)
32*4882a593Smuzhiyun #define   RX_DATA_RESET		BIT_ULL(0)
33*4882a593Smuzhiyun #define XCV_DLL_CTL		0x10
34*4882a593Smuzhiyun #define   CLKRX_BYP		BIT_ULL(23)
35*4882a593Smuzhiyun #define   CLKTX_BYP		BIT_ULL(15)
36*4882a593Smuzhiyun #define XCV_COMP_CTL		0x20
37*4882a593Smuzhiyun #define   DRV_BYP		BIT_ULL(63)
38*4882a593Smuzhiyun #define XCV_CTL			0x30
39*4882a593Smuzhiyun #define XCV_INT			0x40
40*4882a593Smuzhiyun #define XCV_INT_W1S		0x48
41*4882a593Smuzhiyun #define XCV_INT_ENA_W1C		0x50
42*4882a593Smuzhiyun #define XCV_INT_ENA_W1S		0x58
43*4882a593Smuzhiyun #define XCV_INBND_STATUS	0x80
44*4882a593Smuzhiyun #define XCV_BATCH_CRD_RET	0x100
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun struct xcv {
47*4882a593Smuzhiyun 	void __iomem		*reg_base;
48*4882a593Smuzhiyun 	struct pci_dev		*pdev;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun static struct xcv *xcv;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /* Supported devices */
54*4882a593Smuzhiyun static const struct pci_device_id xcv_id_table[] = {
55*4882a593Smuzhiyun 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xA056) },
56*4882a593Smuzhiyun 	{ 0, }  /* end of table */
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun MODULE_AUTHOR("Cavium Inc");
60*4882a593Smuzhiyun MODULE_DESCRIPTION("Cavium Thunder RGX/XCV Driver");
61*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
62*4882a593Smuzhiyun MODULE_VERSION(DRV_VERSION);
63*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, xcv_id_table);
64*4882a593Smuzhiyun 
xcv_init_hw(void)65*4882a593Smuzhiyun void xcv_init_hw(void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	u64  cfg;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	/* Take DLL out of reset */
70*4882a593Smuzhiyun 	cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
71*4882a593Smuzhiyun 	cfg &= ~DLL_RESET;
72*4882a593Smuzhiyun 	writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/* Take clock tree out of reset */
75*4882a593Smuzhiyun 	cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
76*4882a593Smuzhiyun 	cfg &= ~CLK_RESET;
77*4882a593Smuzhiyun 	writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
78*4882a593Smuzhiyun 	/* Wait for DLL to lock */
79*4882a593Smuzhiyun 	msleep(1);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/* Configure DLL - enable or bypass
82*4882a593Smuzhiyun 	 * TX no bypass, RX bypass
83*4882a593Smuzhiyun 	 */
84*4882a593Smuzhiyun 	cfg = readq_relaxed(xcv->reg_base + XCV_DLL_CTL);
85*4882a593Smuzhiyun 	cfg &= ~0xFF03;
86*4882a593Smuzhiyun 	cfg |= CLKRX_BYP;
87*4882a593Smuzhiyun 	writeq_relaxed(cfg, xcv->reg_base + XCV_DLL_CTL);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* Enable compensation controller and force the
90*4882a593Smuzhiyun 	 * write to be visible to HW by readig back.
91*4882a593Smuzhiyun 	 */
92*4882a593Smuzhiyun 	cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
93*4882a593Smuzhiyun 	cfg |= COMP_EN;
94*4882a593Smuzhiyun 	writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
95*4882a593Smuzhiyun 	readq_relaxed(xcv->reg_base + XCV_RESET);
96*4882a593Smuzhiyun 	/* Wait for compensation state machine to lock */
97*4882a593Smuzhiyun 	msleep(10);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* enable the XCV block */
100*4882a593Smuzhiyun 	cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
101*4882a593Smuzhiyun 	cfg |= PORT_EN;
102*4882a593Smuzhiyun 	writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
105*4882a593Smuzhiyun 	cfg |= CLK_RESET;
106*4882a593Smuzhiyun 	writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun EXPORT_SYMBOL(xcv_init_hw);
109*4882a593Smuzhiyun 
xcv_setup_link(bool link_up,int link_speed)110*4882a593Smuzhiyun void xcv_setup_link(bool link_up, int link_speed)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	u64  cfg;
113*4882a593Smuzhiyun 	int speed = 2;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (!xcv) {
116*4882a593Smuzhiyun 		pr_err("XCV init not done, probe may have failed\n");
117*4882a593Smuzhiyun 		return;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (link_speed == 100)
121*4882a593Smuzhiyun 		speed = 1;
122*4882a593Smuzhiyun 	else if (link_speed == 10)
123*4882a593Smuzhiyun 		speed = 0;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (link_up) {
126*4882a593Smuzhiyun 		/* set operating speed */
127*4882a593Smuzhiyun 		cfg = readq_relaxed(xcv->reg_base + XCV_CTL);
128*4882a593Smuzhiyun 		cfg &= ~0x03;
129*4882a593Smuzhiyun 		cfg |= speed;
130*4882a593Smuzhiyun 		writeq_relaxed(cfg, xcv->reg_base + XCV_CTL);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		/* Reset datapaths */
133*4882a593Smuzhiyun 		cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
134*4882a593Smuzhiyun 		cfg |= TX_DATA_RESET | RX_DATA_RESET;
135*4882a593Smuzhiyun 		writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 		/* Enable the packet flow */
138*4882a593Smuzhiyun 		cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
139*4882a593Smuzhiyun 		cfg |= TX_PKT_RESET | RX_PKT_RESET;
140*4882a593Smuzhiyun 		writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		/* Return credits to RGX */
143*4882a593Smuzhiyun 		writeq_relaxed(0x01, xcv->reg_base + XCV_BATCH_CRD_RET);
144*4882a593Smuzhiyun 	} else {
145*4882a593Smuzhiyun 		/* Disable packet flow */
146*4882a593Smuzhiyun 		cfg = readq_relaxed(xcv->reg_base + XCV_RESET);
147*4882a593Smuzhiyun 		cfg &= ~(TX_PKT_RESET | RX_PKT_RESET);
148*4882a593Smuzhiyun 		writeq_relaxed(cfg, xcv->reg_base + XCV_RESET);
149*4882a593Smuzhiyun 		readq_relaxed(xcv->reg_base + XCV_RESET);
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun EXPORT_SYMBOL(xcv_setup_link);
153*4882a593Smuzhiyun 
xcv_probe(struct pci_dev * pdev,const struct pci_device_id * ent)154*4882a593Smuzhiyun static int xcv_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	int err;
157*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	xcv = devm_kzalloc(dev, sizeof(struct xcv), GFP_KERNEL);
160*4882a593Smuzhiyun 	if (!xcv)
161*4882a593Smuzhiyun 		return -ENOMEM;
162*4882a593Smuzhiyun 	xcv->pdev = pdev;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	pci_set_drvdata(pdev, xcv);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	err = pci_enable_device(pdev);
167*4882a593Smuzhiyun 	if (err) {
168*4882a593Smuzhiyun 		dev_err(dev, "Failed to enable PCI device\n");
169*4882a593Smuzhiyun 		goto err_kfree;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	err = pci_request_regions(pdev, DRV_NAME);
173*4882a593Smuzhiyun 	if (err) {
174*4882a593Smuzhiyun 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
175*4882a593Smuzhiyun 		goto err_disable_device;
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/* MAP configuration registers */
179*4882a593Smuzhiyun 	xcv->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
180*4882a593Smuzhiyun 	if (!xcv->reg_base) {
181*4882a593Smuzhiyun 		dev_err(dev, "XCV: Cannot map CSR memory space, aborting\n");
182*4882a593Smuzhiyun 		err = -ENOMEM;
183*4882a593Smuzhiyun 		goto err_release_regions;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	return 0;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun err_release_regions:
189*4882a593Smuzhiyun 	pci_release_regions(pdev);
190*4882a593Smuzhiyun err_disable_device:
191*4882a593Smuzhiyun 	pci_disable_device(pdev);
192*4882a593Smuzhiyun err_kfree:
193*4882a593Smuzhiyun 	devm_kfree(dev, xcv);
194*4882a593Smuzhiyun 	xcv = NULL;
195*4882a593Smuzhiyun 	return err;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
xcv_remove(struct pci_dev * pdev)198*4882a593Smuzhiyun static void xcv_remove(struct pci_dev *pdev)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (xcv) {
203*4882a593Smuzhiyun 		devm_kfree(dev, xcv);
204*4882a593Smuzhiyun 		xcv = NULL;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	pci_release_regions(pdev);
208*4882a593Smuzhiyun 	pci_disable_device(pdev);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun static struct pci_driver xcv_driver = {
212*4882a593Smuzhiyun 	.name = DRV_NAME,
213*4882a593Smuzhiyun 	.id_table = xcv_id_table,
214*4882a593Smuzhiyun 	.probe = xcv_probe,
215*4882a593Smuzhiyun 	.remove = xcv_remove,
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun 
xcv_init_module(void)218*4882a593Smuzhiyun static int __init xcv_init_module(void)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	return pci_register_driver(&xcv_driver);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
xcv_cleanup_module(void)225*4882a593Smuzhiyun static void __exit xcv_cleanup_module(void)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	pci_unregister_driver(&xcv_driver);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun module_init(xcv_init_module);
231*4882a593Smuzhiyun module_exit(xcv_cleanup_module);
232