xref: /openbmc/linux/drivers/misc/dw-xdata-pcie.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1*e8a30eefSGustavo Pimentel // SPDX-License-Identifier: GPL-2.0
2*e8a30eefSGustavo Pimentel /*
3*e8a30eefSGustavo Pimentel  * Copyright (c) 2020 Synopsys, Inc. and/or its affiliates.
4*e8a30eefSGustavo Pimentel  * Synopsys DesignWare xData driver
5*e8a30eefSGustavo Pimentel  *
6*e8a30eefSGustavo Pimentel  * Author: Gustavo Pimentel <gustavo.pimentel@synopsys.com>
7*e8a30eefSGustavo Pimentel  */
8*e8a30eefSGustavo Pimentel 
9*e8a30eefSGustavo Pimentel #include <linux/miscdevice.h>
10*e8a30eefSGustavo Pimentel #include <linux/bitfield.h>
11*e8a30eefSGustavo Pimentel #include <linux/pci-epf.h>
12*e8a30eefSGustavo Pimentel #include <linux/kernel.h>
13*e8a30eefSGustavo Pimentel #include <linux/module.h>
14*e8a30eefSGustavo Pimentel #include <linux/device.h>
15*e8a30eefSGustavo Pimentel #include <linux/bitops.h>
16*e8a30eefSGustavo Pimentel #include <linux/mutex.h>
17*e8a30eefSGustavo Pimentel #include <linux/delay.h>
18*e8a30eefSGustavo Pimentel #include <linux/pci.h>
19*e8a30eefSGustavo Pimentel 
20*e8a30eefSGustavo Pimentel #define DW_XDATA_DRIVER_NAME		"dw-xdata-pcie"
21*e8a30eefSGustavo Pimentel 
22*e8a30eefSGustavo Pimentel #define DW_XDATA_EP_MEM_OFFSET		0x8000000
23*e8a30eefSGustavo Pimentel 
24*e8a30eefSGustavo Pimentel static DEFINE_IDA(xdata_ida);
25*e8a30eefSGustavo Pimentel 
26*e8a30eefSGustavo Pimentel #define STATUS_DONE			BIT(0)
27*e8a30eefSGustavo Pimentel 
28*e8a30eefSGustavo Pimentel #define CONTROL_DOORBELL		BIT(0)
29*e8a30eefSGustavo Pimentel #define CONTROL_IS_WRITE		BIT(1)
30*e8a30eefSGustavo Pimentel #define CONTROL_LENGTH(a)		FIELD_PREP(GENMASK(13, 2), a)
31*e8a30eefSGustavo Pimentel #define CONTROL_PATTERN_INC		BIT(16)
32*e8a30eefSGustavo Pimentel #define CONTROL_NO_ADDR_INC		BIT(18)
33*e8a30eefSGustavo Pimentel 
34*e8a30eefSGustavo Pimentel #define XPERF_CONTROL_ENABLE		BIT(5)
35*e8a30eefSGustavo Pimentel 
36*e8a30eefSGustavo Pimentel #define BURST_REPEAT			BIT(31)
37*e8a30eefSGustavo Pimentel #define BURST_VALUE			0x1001
38*e8a30eefSGustavo Pimentel 
39*e8a30eefSGustavo Pimentel #define PATTERN_VALUE			0x0
40*e8a30eefSGustavo Pimentel 
41*e8a30eefSGustavo Pimentel struct dw_xdata_regs {
42*e8a30eefSGustavo Pimentel 	u32 addr_lsb;					/* 0x000 */
43*e8a30eefSGustavo Pimentel 	u32 addr_msb;					/* 0x004 */
44*e8a30eefSGustavo Pimentel 	u32 burst_cnt;					/* 0x008 */
45*e8a30eefSGustavo Pimentel 	u32 control;					/* 0x00c */
46*e8a30eefSGustavo Pimentel 	u32 pattern;					/* 0x010 */
47*e8a30eefSGustavo Pimentel 	u32 status;					/* 0x014 */
48*e8a30eefSGustavo Pimentel 	u32 RAM_addr;					/* 0x018 */
49*e8a30eefSGustavo Pimentel 	u32 RAM_port;					/* 0x01c */
50*e8a30eefSGustavo Pimentel 	u32 _reserved0[14];				/* 0x020..0x054 */
51*e8a30eefSGustavo Pimentel 	u32 perf_control;				/* 0x058 */
52*e8a30eefSGustavo Pimentel 	u32 _reserved1[41];				/* 0x05c..0x0fc */
53*e8a30eefSGustavo Pimentel 	u32 wr_cnt_lsb;					/* 0x100 */
54*e8a30eefSGustavo Pimentel 	u32 wr_cnt_msb;					/* 0x104 */
55*e8a30eefSGustavo Pimentel 	u32 rd_cnt_lsb;					/* 0x108 */
56*e8a30eefSGustavo Pimentel 	u32 rd_cnt_msb;					/* 0x10c */
57*e8a30eefSGustavo Pimentel } __packed;
58*e8a30eefSGustavo Pimentel 
59*e8a30eefSGustavo Pimentel struct dw_xdata_region {
60*e8a30eefSGustavo Pimentel 	phys_addr_t paddr;				/* physical address */
61*e8a30eefSGustavo Pimentel 	void __iomem *vaddr;				/* virtual address */
62*e8a30eefSGustavo Pimentel };
63*e8a30eefSGustavo Pimentel 
64*e8a30eefSGustavo Pimentel struct dw_xdata {
65*e8a30eefSGustavo Pimentel 	struct dw_xdata_region rg_region;		/* registers */
66*e8a30eefSGustavo Pimentel 	size_t max_wr_len;				/* max wr xfer len */
67*e8a30eefSGustavo Pimentel 	size_t max_rd_len;				/* max rd xfer len */
68*e8a30eefSGustavo Pimentel 	struct mutex mutex;
69*e8a30eefSGustavo Pimentel 	struct pci_dev *pdev;
70*e8a30eefSGustavo Pimentel 	struct miscdevice misc_dev;
71*e8a30eefSGustavo Pimentel };
72*e8a30eefSGustavo Pimentel 
__dw_regs(struct dw_xdata * dw)73*e8a30eefSGustavo Pimentel static inline struct dw_xdata_regs __iomem *__dw_regs(struct dw_xdata *dw)
74*e8a30eefSGustavo Pimentel {
75*e8a30eefSGustavo Pimentel 	return dw->rg_region.vaddr;
76*e8a30eefSGustavo Pimentel }
77*e8a30eefSGustavo Pimentel 
dw_xdata_stop(struct dw_xdata * dw)78*e8a30eefSGustavo Pimentel static void dw_xdata_stop(struct dw_xdata *dw)
79*e8a30eefSGustavo Pimentel {
80*e8a30eefSGustavo Pimentel 	u32 burst;
81*e8a30eefSGustavo Pimentel 
82*e8a30eefSGustavo Pimentel 	mutex_lock(&dw->mutex);
83*e8a30eefSGustavo Pimentel 
84*e8a30eefSGustavo Pimentel 	burst = readl(&(__dw_regs(dw)->burst_cnt));
85*e8a30eefSGustavo Pimentel 
86*e8a30eefSGustavo Pimentel 	if (burst & BURST_REPEAT) {
87*e8a30eefSGustavo Pimentel 		burst &= ~(u32)BURST_REPEAT;
88*e8a30eefSGustavo Pimentel 		writel(burst, &(__dw_regs(dw)->burst_cnt));
89*e8a30eefSGustavo Pimentel 	}
90*e8a30eefSGustavo Pimentel 
91*e8a30eefSGustavo Pimentel 	mutex_unlock(&dw->mutex);
92*e8a30eefSGustavo Pimentel }
93*e8a30eefSGustavo Pimentel 
dw_xdata_start(struct dw_xdata * dw,bool write)94*e8a30eefSGustavo Pimentel static void dw_xdata_start(struct dw_xdata *dw, bool write)
95*e8a30eefSGustavo Pimentel {
96*e8a30eefSGustavo Pimentel 	struct device *dev = &dw->pdev->dev;
97*e8a30eefSGustavo Pimentel 	u32 control, status;
98*e8a30eefSGustavo Pimentel 
99*e8a30eefSGustavo Pimentel 	/* Stop first if xfer in progress */
100*e8a30eefSGustavo Pimentel 	dw_xdata_stop(dw);
101*e8a30eefSGustavo Pimentel 
102*e8a30eefSGustavo Pimentel 	mutex_lock(&dw->mutex);
103*e8a30eefSGustavo Pimentel 
104*e8a30eefSGustavo Pimentel 	/* Clear status register */
105*e8a30eefSGustavo Pimentel 	writel(0x0, &(__dw_regs(dw)->status));
106*e8a30eefSGustavo Pimentel 
107*e8a30eefSGustavo Pimentel 	/* Burst count register set for continuous until stopped */
108*e8a30eefSGustavo Pimentel 	writel(BURST_REPEAT | BURST_VALUE, &(__dw_regs(dw)->burst_cnt));
109*e8a30eefSGustavo Pimentel 
110*e8a30eefSGustavo Pimentel 	/* Pattern register */
111*e8a30eefSGustavo Pimentel 	writel(PATTERN_VALUE, &(__dw_regs(dw)->pattern));
112*e8a30eefSGustavo Pimentel 
113*e8a30eefSGustavo Pimentel 	/* Control register */
114*e8a30eefSGustavo Pimentel 	control = CONTROL_DOORBELL | CONTROL_PATTERN_INC | CONTROL_NO_ADDR_INC;
115*e8a30eefSGustavo Pimentel 	if (write) {
116*e8a30eefSGustavo Pimentel 		control |= CONTROL_IS_WRITE;
117*e8a30eefSGustavo Pimentel 		control |= CONTROL_LENGTH(dw->max_wr_len);
118*e8a30eefSGustavo Pimentel 	} else {
119*e8a30eefSGustavo Pimentel 		control |= CONTROL_LENGTH(dw->max_rd_len);
120*e8a30eefSGustavo Pimentel 	}
121*e8a30eefSGustavo Pimentel 	writel(control, &(__dw_regs(dw)->control));
122*e8a30eefSGustavo Pimentel 
123*e8a30eefSGustavo Pimentel 	/*
124*e8a30eefSGustavo Pimentel 	 * The xData HW block needs about 100 ms to initiate the traffic
125*e8a30eefSGustavo Pimentel 	 * generation according this HW block datasheet.
126*e8a30eefSGustavo Pimentel 	 */
127*e8a30eefSGustavo Pimentel 	usleep_range(100, 150);
128*e8a30eefSGustavo Pimentel 
129*e8a30eefSGustavo Pimentel 	status = readl(&(__dw_regs(dw)->status));
130*e8a30eefSGustavo Pimentel 
131*e8a30eefSGustavo Pimentel 	mutex_unlock(&dw->mutex);
132*e8a30eefSGustavo Pimentel 
133*e8a30eefSGustavo Pimentel 	if (!(status & STATUS_DONE))
134*e8a30eefSGustavo Pimentel 		dev_dbg(dev, "xData: started %s direction\n",
135*e8a30eefSGustavo Pimentel 			write ? "write" : "read");
136*e8a30eefSGustavo Pimentel }
137*e8a30eefSGustavo Pimentel 
dw_xdata_perf_meas(struct dw_xdata * dw,u64 * data,bool write)138*e8a30eefSGustavo Pimentel static void dw_xdata_perf_meas(struct dw_xdata *dw, u64 *data, bool write)
139*e8a30eefSGustavo Pimentel {
140*e8a30eefSGustavo Pimentel 	if (write) {
141*e8a30eefSGustavo Pimentel 		*data = readl(&(__dw_regs(dw)->wr_cnt_msb));
142*e8a30eefSGustavo Pimentel 		*data <<= 32;
143*e8a30eefSGustavo Pimentel 		*data |= readl(&(__dw_regs(dw)->wr_cnt_lsb));
144*e8a30eefSGustavo Pimentel 	} else {
145*e8a30eefSGustavo Pimentel 		*data = readl(&(__dw_regs(dw)->rd_cnt_msb));
146*e8a30eefSGustavo Pimentel 		*data <<= 32;
147*e8a30eefSGustavo Pimentel 		*data |= readl(&(__dw_regs(dw)->rd_cnt_lsb));
148*e8a30eefSGustavo Pimentel 	}
149*e8a30eefSGustavo Pimentel }
150*e8a30eefSGustavo Pimentel 
dw_xdata_perf_diff(u64 * m1,u64 * m2,u64 time)151*e8a30eefSGustavo Pimentel static u64 dw_xdata_perf_diff(u64 *m1, u64 *m2, u64 time)
152*e8a30eefSGustavo Pimentel {
153*e8a30eefSGustavo Pimentel 	u64 rate = (*m1 - *m2);
154*e8a30eefSGustavo Pimentel 
155*e8a30eefSGustavo Pimentel 	rate *= (1000 * 1000 * 1000);
156*e8a30eefSGustavo Pimentel 	rate >>= 20;
157*e8a30eefSGustavo Pimentel 	rate = DIV_ROUND_CLOSEST_ULL(rate, time);
158*e8a30eefSGustavo Pimentel 
159*e8a30eefSGustavo Pimentel 	return rate;
160*e8a30eefSGustavo Pimentel }
161*e8a30eefSGustavo Pimentel 
dw_xdata_perf(struct dw_xdata * dw,u64 * rate,bool write)162*e8a30eefSGustavo Pimentel static void dw_xdata_perf(struct dw_xdata *dw, u64 *rate, bool write)
163*e8a30eefSGustavo Pimentel {
164*e8a30eefSGustavo Pimentel 	struct device *dev = &dw->pdev->dev;
165*e8a30eefSGustavo Pimentel 	u64 data[2], time[2], diff;
166*e8a30eefSGustavo Pimentel 
167*e8a30eefSGustavo Pimentel 	mutex_lock(&dw->mutex);
168*e8a30eefSGustavo Pimentel 
169*e8a30eefSGustavo Pimentel 	/* First acquisition of current count frames */
170*e8a30eefSGustavo Pimentel 	writel(0x0, &(__dw_regs(dw)->perf_control));
171*e8a30eefSGustavo Pimentel 	dw_xdata_perf_meas(dw, &data[0], write);
172*e8a30eefSGustavo Pimentel 	time[0] = jiffies;
173*e8a30eefSGustavo Pimentel 	writel((u32)XPERF_CONTROL_ENABLE, &(__dw_regs(dw)->perf_control));
174*e8a30eefSGustavo Pimentel 
175*e8a30eefSGustavo Pimentel 	/*
176*e8a30eefSGustavo Pimentel 	 * Wait 100ms between the 1st count frame acquisition and the 2nd
177*e8a30eefSGustavo Pimentel 	 * count frame acquisition, in order to calculate the speed later
178*e8a30eefSGustavo Pimentel 	 */
179*e8a30eefSGustavo Pimentel 	mdelay(100);
180*e8a30eefSGustavo Pimentel 
181*e8a30eefSGustavo Pimentel 	/* Second acquisition of current count frames */
182*e8a30eefSGustavo Pimentel 	writel(0x0, &(__dw_regs(dw)->perf_control));
183*e8a30eefSGustavo Pimentel 	dw_xdata_perf_meas(dw, &data[1], write);
184*e8a30eefSGustavo Pimentel 	time[1] = jiffies;
185*e8a30eefSGustavo Pimentel 	writel((u32)XPERF_CONTROL_ENABLE, &(__dw_regs(dw)->perf_control));
186*e8a30eefSGustavo Pimentel 
187*e8a30eefSGustavo Pimentel 	/*
188*e8a30eefSGustavo Pimentel 	 * Speed calculation
189*e8a30eefSGustavo Pimentel 	 *
190*e8a30eefSGustavo Pimentel 	 * rate = (2nd count frames - 1st count frames) / (time elapsed)
191*e8a30eefSGustavo Pimentel 	 */
192*e8a30eefSGustavo Pimentel 	diff = jiffies_to_nsecs(time[1] - time[0]);
193*e8a30eefSGustavo Pimentel 	*rate = dw_xdata_perf_diff(&data[1], &data[0], diff);
194*e8a30eefSGustavo Pimentel 
195*e8a30eefSGustavo Pimentel 	mutex_unlock(&dw->mutex);
196*e8a30eefSGustavo Pimentel 
197*e8a30eefSGustavo Pimentel 	dev_dbg(dev, "xData: time=%llu us, %s=%llu MB/s\n",
198*e8a30eefSGustavo Pimentel 		diff, write ? "write" : "read", *rate);
199*e8a30eefSGustavo Pimentel }
200*e8a30eefSGustavo Pimentel 
misc_dev_to_dw(struct miscdevice * misc_dev)201*e8a30eefSGustavo Pimentel static struct dw_xdata *misc_dev_to_dw(struct miscdevice *misc_dev)
202*e8a30eefSGustavo Pimentel {
203*e8a30eefSGustavo Pimentel 	return container_of(misc_dev, struct dw_xdata, misc_dev);
204*e8a30eefSGustavo Pimentel }
205*e8a30eefSGustavo Pimentel 
write_show(struct device * dev,struct device_attribute * attr,char * buf)206*e8a30eefSGustavo Pimentel static ssize_t write_show(struct device *dev, struct device_attribute *attr,
207*e8a30eefSGustavo Pimentel 			  char *buf)
208*e8a30eefSGustavo Pimentel {
209*e8a30eefSGustavo Pimentel 	struct miscdevice *misc_dev = dev_get_drvdata(dev);
210*e8a30eefSGustavo Pimentel 	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
211*e8a30eefSGustavo Pimentel 	u64 rate;
212*e8a30eefSGustavo Pimentel 
213*e8a30eefSGustavo Pimentel 	dw_xdata_perf(dw, &rate, true);
214*e8a30eefSGustavo Pimentel 
215*e8a30eefSGustavo Pimentel 	return sysfs_emit(buf, "%llu\n", rate);
216*e8a30eefSGustavo Pimentel }
217*e8a30eefSGustavo Pimentel 
write_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)218*e8a30eefSGustavo Pimentel static ssize_t write_store(struct device *dev, struct device_attribute *attr,
219*e8a30eefSGustavo Pimentel 			   const char *buf, size_t size)
220*e8a30eefSGustavo Pimentel {
221*e8a30eefSGustavo Pimentel 	struct miscdevice *misc_dev = dev_get_drvdata(dev);
222*e8a30eefSGustavo Pimentel 	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
223*e8a30eefSGustavo Pimentel 	bool enabled;
224*e8a30eefSGustavo Pimentel 	int ret;
225*e8a30eefSGustavo Pimentel 
226*e8a30eefSGustavo Pimentel 	ret = kstrtobool(buf, &enabled);
227*e8a30eefSGustavo Pimentel 	if (ret < 0)
228*e8a30eefSGustavo Pimentel 		return ret;
229*e8a30eefSGustavo Pimentel 
230*e8a30eefSGustavo Pimentel 	if (enabled) {
231*e8a30eefSGustavo Pimentel 		dev_dbg(dev, "xData: requested write transfer\n");
232*e8a30eefSGustavo Pimentel 		dw_xdata_start(dw, true);
233*e8a30eefSGustavo Pimentel 	} else {
234*e8a30eefSGustavo Pimentel 		dev_dbg(dev, "xData: requested stop transfer\n");
235*e8a30eefSGustavo Pimentel 		dw_xdata_stop(dw);
236*e8a30eefSGustavo Pimentel 	}
237*e8a30eefSGustavo Pimentel 
238*e8a30eefSGustavo Pimentel 	return size;
239*e8a30eefSGustavo Pimentel }
240*e8a30eefSGustavo Pimentel 
241*e8a30eefSGustavo Pimentel static DEVICE_ATTR_RW(write);
242*e8a30eefSGustavo Pimentel 
read_show(struct device * dev,struct device_attribute * attr,char * buf)243*e8a30eefSGustavo Pimentel static ssize_t read_show(struct device *dev, struct device_attribute *attr,
244*e8a30eefSGustavo Pimentel 			 char *buf)
245*e8a30eefSGustavo Pimentel {
246*e8a30eefSGustavo Pimentel 	struct miscdevice *misc_dev = dev_get_drvdata(dev);
247*e8a30eefSGustavo Pimentel 	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
248*e8a30eefSGustavo Pimentel 	u64 rate;
249*e8a30eefSGustavo Pimentel 
250*e8a30eefSGustavo Pimentel 	dw_xdata_perf(dw, &rate, false);
251*e8a30eefSGustavo Pimentel 
252*e8a30eefSGustavo Pimentel 	return sysfs_emit(buf, "%llu\n", rate);
253*e8a30eefSGustavo Pimentel }
254*e8a30eefSGustavo Pimentel 
read_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)255*e8a30eefSGustavo Pimentel static ssize_t read_store(struct device *dev, struct device_attribute *attr,
256*e8a30eefSGustavo Pimentel 			  const char *buf, size_t size)
257*e8a30eefSGustavo Pimentel {
258*e8a30eefSGustavo Pimentel 	struct miscdevice *misc_dev = dev_get_drvdata(dev);
259*e8a30eefSGustavo Pimentel 	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
260*e8a30eefSGustavo Pimentel 	bool enabled;
261*e8a30eefSGustavo Pimentel 	int ret;
262*e8a30eefSGustavo Pimentel 
263*e8a30eefSGustavo Pimentel 	ret = kstrtobool(buf, &enabled);
264*e8a30eefSGustavo Pimentel 	if (ret < 0)
265*e8a30eefSGustavo Pimentel 		return ret;
266*e8a30eefSGustavo Pimentel 
267*e8a30eefSGustavo Pimentel 	if (enabled) {
268*e8a30eefSGustavo Pimentel 		dev_dbg(dev, "xData: requested read transfer\n");
269*e8a30eefSGustavo Pimentel 		dw_xdata_start(dw, false);
270*e8a30eefSGustavo Pimentel 	} else {
271*e8a30eefSGustavo Pimentel 		dev_dbg(dev, "xData: requested stop transfer\n");
272*e8a30eefSGustavo Pimentel 		dw_xdata_stop(dw);
273*e8a30eefSGustavo Pimentel 	}
274*e8a30eefSGustavo Pimentel 
275*e8a30eefSGustavo Pimentel 	return size;
276*e8a30eefSGustavo Pimentel }
277*e8a30eefSGustavo Pimentel 
278*e8a30eefSGustavo Pimentel static DEVICE_ATTR_RW(read);
279*e8a30eefSGustavo Pimentel 
280*e8a30eefSGustavo Pimentel static struct attribute *xdata_attrs[] = {
281*e8a30eefSGustavo Pimentel 	&dev_attr_write.attr,
282*e8a30eefSGustavo Pimentel 	&dev_attr_read.attr,
283*e8a30eefSGustavo Pimentel 	NULL,
284*e8a30eefSGustavo Pimentel };
285*e8a30eefSGustavo Pimentel 
286*e8a30eefSGustavo Pimentel ATTRIBUTE_GROUPS(xdata);
287*e8a30eefSGustavo Pimentel 
dw_xdata_pcie_probe(struct pci_dev * pdev,const struct pci_device_id * pid)288*e8a30eefSGustavo Pimentel static int dw_xdata_pcie_probe(struct pci_dev *pdev,
289*e8a30eefSGustavo Pimentel 			       const struct pci_device_id *pid)
290*e8a30eefSGustavo Pimentel {
291*e8a30eefSGustavo Pimentel 	struct device *dev = &pdev->dev;
292*e8a30eefSGustavo Pimentel 	struct dw_xdata *dw;
293*e8a30eefSGustavo Pimentel 	char name[24];
294*e8a30eefSGustavo Pimentel 	u64 addr;
295*e8a30eefSGustavo Pimentel 	int err;
296*e8a30eefSGustavo Pimentel 	int id;
297*e8a30eefSGustavo Pimentel 
298*e8a30eefSGustavo Pimentel 	/* Enable PCI device */
299*e8a30eefSGustavo Pimentel 	err = pcim_enable_device(pdev);
300*e8a30eefSGustavo Pimentel 	if (err) {
301*e8a30eefSGustavo Pimentel 		dev_err(dev, "enabling device failed\n");
302*e8a30eefSGustavo Pimentel 		return err;
303*e8a30eefSGustavo Pimentel 	}
304*e8a30eefSGustavo Pimentel 
305*e8a30eefSGustavo Pimentel 	/* Mapping PCI BAR regions */
306*e8a30eefSGustavo Pimentel 	err = pcim_iomap_regions(pdev, BIT(BAR_0), pci_name(pdev));
307*e8a30eefSGustavo Pimentel 	if (err) {
308*e8a30eefSGustavo Pimentel 		dev_err(dev, "xData BAR I/O remapping failed\n");
309*e8a30eefSGustavo Pimentel 		return err;
310*e8a30eefSGustavo Pimentel 	}
311*e8a30eefSGustavo Pimentel 
312*e8a30eefSGustavo Pimentel 	pci_set_master(pdev);
313*e8a30eefSGustavo Pimentel 
314*e8a30eefSGustavo Pimentel 	/* Allocate memory */
315*e8a30eefSGustavo Pimentel 	dw = devm_kzalloc(dev, sizeof(*dw), GFP_KERNEL);
316*e8a30eefSGustavo Pimentel 	if (!dw)
317*e8a30eefSGustavo Pimentel 		return -ENOMEM;
318*e8a30eefSGustavo Pimentel 
319*e8a30eefSGustavo Pimentel 	/* Data structure initialization */
320*e8a30eefSGustavo Pimentel 	mutex_init(&dw->mutex);
321*e8a30eefSGustavo Pimentel 
322*e8a30eefSGustavo Pimentel 	dw->rg_region.vaddr = pcim_iomap_table(pdev)[BAR_0];
323*e8a30eefSGustavo Pimentel 	if (!dw->rg_region.vaddr)
324*e8a30eefSGustavo Pimentel 		return -ENOMEM;
325*e8a30eefSGustavo Pimentel 
326*e8a30eefSGustavo Pimentel 	dw->rg_region.paddr = pdev->resource[BAR_0].start;
327*e8a30eefSGustavo Pimentel 
328*e8a30eefSGustavo Pimentel 	dw->max_wr_len = pcie_get_mps(pdev);
329*e8a30eefSGustavo Pimentel 	dw->max_wr_len >>= 2;
330*e8a30eefSGustavo Pimentel 
331*e8a30eefSGustavo Pimentel 	dw->max_rd_len = pcie_get_readrq(pdev);
332*e8a30eefSGustavo Pimentel 	dw->max_rd_len >>= 2;
333*e8a30eefSGustavo Pimentel 
334*e8a30eefSGustavo Pimentel 	dw->pdev = pdev;
335*e8a30eefSGustavo Pimentel 
336*e8a30eefSGustavo Pimentel 	id = ida_simple_get(&xdata_ida, 0, 0, GFP_KERNEL);
337*e8a30eefSGustavo Pimentel 	if (id < 0) {
338*e8a30eefSGustavo Pimentel 		dev_err(dev, "xData: unable to get id\n");
339*e8a30eefSGustavo Pimentel 		return id;
340*e8a30eefSGustavo Pimentel 	}
341*e8a30eefSGustavo Pimentel 
342*e8a30eefSGustavo Pimentel 	snprintf(name, sizeof(name), DW_XDATA_DRIVER_NAME ".%d", id);
343*e8a30eefSGustavo Pimentel 	dw->misc_dev.name = kstrdup(name, GFP_KERNEL);
344*e8a30eefSGustavo Pimentel 	if (!dw->misc_dev.name) {
345*e8a30eefSGustavo Pimentel 		err = -ENOMEM;
346*e8a30eefSGustavo Pimentel 		goto err_ida_remove;
347*e8a30eefSGustavo Pimentel 	}
348*e8a30eefSGustavo Pimentel 
349*e8a30eefSGustavo Pimentel 	dw->misc_dev.minor = MISC_DYNAMIC_MINOR;
350*e8a30eefSGustavo Pimentel 	dw->misc_dev.parent = dev;
351*e8a30eefSGustavo Pimentel 	dw->misc_dev.groups = xdata_groups;
352*e8a30eefSGustavo Pimentel 
353*e8a30eefSGustavo Pimentel 	writel(0x0, &(__dw_regs(dw)->RAM_addr));
354*e8a30eefSGustavo Pimentel 	writel(0x0, &(__dw_regs(dw)->RAM_port));
355*e8a30eefSGustavo Pimentel 
356*e8a30eefSGustavo Pimentel 	addr = dw->rg_region.paddr + DW_XDATA_EP_MEM_OFFSET;
357*e8a30eefSGustavo Pimentel 	writel(lower_32_bits(addr), &(__dw_regs(dw)->addr_lsb));
358*e8a30eefSGustavo Pimentel 	writel(upper_32_bits(addr), &(__dw_regs(dw)->addr_msb));
359*e8a30eefSGustavo Pimentel 	dev_dbg(dev, "xData: target address = 0x%.16llx\n", addr);
360*e8a30eefSGustavo Pimentel 
361*e8a30eefSGustavo Pimentel 	dev_dbg(dev, "xData: wr_len = %zu, rd_len = %zu\n",
362*e8a30eefSGustavo Pimentel 		dw->max_wr_len * 4, dw->max_rd_len * 4);
363*e8a30eefSGustavo Pimentel 
364*e8a30eefSGustavo Pimentel 	/* Saving data structure reference */
365*e8a30eefSGustavo Pimentel 	pci_set_drvdata(pdev, dw);
366*e8a30eefSGustavo Pimentel 
367*e8a30eefSGustavo Pimentel 	/* Register misc device */
368*e8a30eefSGustavo Pimentel 	err = misc_register(&dw->misc_dev);
369*e8a30eefSGustavo Pimentel 	if (err) {
370*e8a30eefSGustavo Pimentel 		dev_err(dev, "xData: failed to register device\n");
371*e8a30eefSGustavo Pimentel 		goto err_kfree_name;
372*e8a30eefSGustavo Pimentel 	}
373*e8a30eefSGustavo Pimentel 
374*e8a30eefSGustavo Pimentel 	return 0;
375*e8a30eefSGustavo Pimentel 
376*e8a30eefSGustavo Pimentel err_kfree_name:
377*e8a30eefSGustavo Pimentel 	kfree(dw->misc_dev.name);
378*e8a30eefSGustavo Pimentel 
379*e8a30eefSGustavo Pimentel err_ida_remove:
380*e8a30eefSGustavo Pimentel 	ida_simple_remove(&xdata_ida, id);
381*e8a30eefSGustavo Pimentel 
382*e8a30eefSGustavo Pimentel 	return err;
383*e8a30eefSGustavo Pimentel }
384*e8a30eefSGustavo Pimentel 
dw_xdata_pcie_remove(struct pci_dev * pdev)385*e8a30eefSGustavo Pimentel static void dw_xdata_pcie_remove(struct pci_dev *pdev)
386*e8a30eefSGustavo Pimentel {
387*e8a30eefSGustavo Pimentel 	struct dw_xdata *dw = pci_get_drvdata(pdev);
388*e8a30eefSGustavo Pimentel 	int id;
389*e8a30eefSGustavo Pimentel 
390*e8a30eefSGustavo Pimentel 	if (sscanf(dw->misc_dev.name, DW_XDATA_DRIVER_NAME ".%d", &id) != 1)
391*e8a30eefSGustavo Pimentel 		return;
392*e8a30eefSGustavo Pimentel 
393*e8a30eefSGustavo Pimentel 	if (id < 0)
394*e8a30eefSGustavo Pimentel 		return;
395*e8a30eefSGustavo Pimentel 
396*e8a30eefSGustavo Pimentel 	dw_xdata_stop(dw);
397*e8a30eefSGustavo Pimentel 	misc_deregister(&dw->misc_dev);
398*e8a30eefSGustavo Pimentel 	kfree(dw->misc_dev.name);
399*e8a30eefSGustavo Pimentel 	ida_simple_remove(&xdata_ida, id);
400*e8a30eefSGustavo Pimentel }
401*e8a30eefSGustavo Pimentel 
402*e8a30eefSGustavo Pimentel static const struct pci_device_id dw_xdata_pcie_id_table[] = {
403*e8a30eefSGustavo Pimentel 	{ PCI_DEVICE_DATA(SYNOPSYS, EDDA, NULL) },
404*e8a30eefSGustavo Pimentel 	{ }
405*e8a30eefSGustavo Pimentel };
406*e8a30eefSGustavo Pimentel MODULE_DEVICE_TABLE(pci, dw_xdata_pcie_id_table);
407*e8a30eefSGustavo Pimentel 
408*e8a30eefSGustavo Pimentel static struct pci_driver dw_xdata_pcie_driver = {
409*e8a30eefSGustavo Pimentel 	.name		= DW_XDATA_DRIVER_NAME,
410*e8a30eefSGustavo Pimentel 	.id_table	= dw_xdata_pcie_id_table,
411*e8a30eefSGustavo Pimentel 	.probe		= dw_xdata_pcie_probe,
412*e8a30eefSGustavo Pimentel 	.remove		= dw_xdata_pcie_remove,
413*e8a30eefSGustavo Pimentel };
414*e8a30eefSGustavo Pimentel 
415*e8a30eefSGustavo Pimentel module_pci_driver(dw_xdata_pcie_driver);
416*e8a30eefSGustavo Pimentel 
417*e8a30eefSGustavo Pimentel MODULE_LICENSE("GPL v2");
418*e8a30eefSGustavo Pimentel MODULE_DESCRIPTION("Synopsys DesignWare xData PCIe driver");
419*e8a30eefSGustavo Pimentel MODULE_AUTHOR("Gustavo Pimentel <gustavo.pimentel@synopsys.com>");
420*e8a30eefSGustavo Pimentel 
421