Skip to content

Commit 76d83e1

Browse files
Dragan Cveticgregkh
authored andcommitted
misc: xilinx-sdfec: add core driver
Implement a platform driver that matches with xlnx, sd-fec-1.1 device tree node and registers as a character device, including: - SD-FEC driver binds to sdfec DT node. - creates and initialise an initial driver dev structure. - add the driver in Linux build and Kconfig. Tested-by: Dragan Cvetic <dragan.cvetic@xilinx.com> Signed-off-by: Derek Kiernan <derek.kiernan@xilinx.com> Signed-off-by: Dragan Cvetic <dragan.cvetic@xilinx.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a00b004 commit 76d83e1

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

‎drivers/misc/Kconfig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ config PCI_ENDPOINT_TEST
472472
Enable this configuration option to enable the host side test driver
473473
for PCI Endpoint.
474474

475+
config XILINX_SDFEC
476+
tristate "Xilinx SDFEC 16"
477+
help
478+
This option enables support for the Xilinx SDFEC (Soft Decision
479+
Forward Error Correction) driver. This enables a char driver
480+
for the SDFEC.
481+
482+
You may select this driver if your design instantiates the
483+
SDFEC(16nm) hardened block. To compile this as a module choose M.
484+
485+
If unsure, say N.
486+
475487
config MISC_RTSX
476488
tristate
477489
default MISC_RTSX_PCI || MISC_RTSX_USB

‎drivers/misc/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ obj-$(CONFIG_OCXL) += ocxl/
5959
obj-y += cardreader/
6060
obj-$(CONFIG_PVPANIC) += pvpanic.o
6161
obj-$(CONFIG_HABANA_AI) += habanalabs/
62+
obj-$(CONFIG_XILINX_SDFEC) += xilinx_sdfec.o

‎drivers/misc/xilinx_sdfec.c‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Xilinx SDFEC
4+
*
5+
* Copyright (C) 2019 Xilinx, Inc.
6+
*
7+
* Description:
8+
* This driver is developed for SDFEC16 (Soft Decision FEC 16nm)
9+
* IP. It exposes a char device which supports file operations
10+
* like open(), close() and ioctl().
11+
*/
12+
13+
#include <linux/miscdevice.h>
14+
#include <linux/io.h>
15+
#include <linux/interrupt.h>
16+
#include <linux/kernel.h>
17+
#include <linux/module.h>
18+
#include <linux/of_platform.h>
19+
#include <linux/poll.h>
20+
#include <linux/slab.h>
21+
#include <linux/clk.h>
22+
23+
#define DEV_NAME_LEN 12
24+
25+
static struct idr dev_idr;
26+
static struct mutex dev_idr_lock;
27+
28+
/**
29+
* struct xsdfec_dev - Driver data for SDFEC
30+
* @regs: device physical base address
31+
* @dev: pointer to device struct
32+
* @miscdev: Misc device handle
33+
* @error_data_lock: Error counter and states spinlock
34+
* @dev_name: Device name
35+
* @dev_id: Device ID
36+
*
37+
* This structure contains necessary state for SDFEC driver to operate
38+
*/
39+
struct xsdfec_dev {
40+
void __iomem *regs;
41+
struct device *dev;
42+
struct miscdevice miscdev;
43+
/* Spinlock to protect state_updated and stats_updated */
44+
spinlock_t error_data_lock;
45+
char dev_name[DEV_NAME_LEN];
46+
int dev_id;
47+
};
48+
49+
static const struct file_operations xsdfec_fops = {
50+
.owner = THIS_MODULE,
51+
};
52+
53+
static void xsdfec_idr_remove(struct xsdfec_dev *xsdfec)
54+
{
55+
mutex_lock(&dev_idr_lock);
56+
idr_remove(&dev_idr, xsdfec->dev_id);
57+
mutex_unlock(&dev_idr_lock);
58+
}
59+
60+
static int xsdfec_probe(struct platform_device *pdev)
61+
{
62+
struct xsdfec_dev *xsdfec;
63+
struct device *dev;
64+
struct resource *res;
65+
int err;
66+
67+
xsdfec = devm_kzalloc(&pdev->dev, sizeof(*xsdfec), GFP_KERNEL);
68+
if (!xsdfec)
69+
return -ENOMEM;
70+
71+
xsdfec->dev = &pdev->dev;
72+
spin_lock_init(&xsdfec->error_data_lock);
73+
74+
dev = xsdfec->dev;
75+
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
76+
xsdfec->regs = devm_ioremap_resource(dev, res);
77+
if (IS_ERR(xsdfec->regs)) {
78+
err = PTR_ERR(xsdfec->regs);
79+
return err;
80+
}
81+
82+
/* Save driver private data */
83+
platform_set_drvdata(pdev, xsdfec);
84+
85+
mutex_lock(&dev_idr_lock);
86+
err = idr_alloc(&dev_idr, xsdfec->dev_name, 0, 0, GFP_KERNEL);
87+
mutex_unlock(&dev_idr_lock);
88+
if (err < 0)
89+
goto err_xsddev_idr;
90+
xsdfec->dev_id = err;
91+
92+
snprintf(xsdfec->dev_name, DEV_NAME_LEN, "xsdfec%d", xsdfec->dev_id);
93+
xsdfec->miscdev.minor = MISC_DYNAMIC_MINOR;
94+
xsdfec->miscdev.name = xsdfec->dev_name;
95+
xsdfec->miscdev.fops = &xsdfec_fops;
96+
xsdfec->miscdev.parent = dev;
97+
err = misc_register(&xsdfec->miscdev);
98+
if (err) {
99+
dev_err(dev, "error:%d. Unable to register device", err);
100+
return err;
101+
}
102+
return 0;
103+
104+
err_xsddev_idr:
105+
xsdfec_idr_remove(xsdfec);
106+
107+
return err;
108+
}
109+
110+
static int xsdfec_remove(struct platform_device *pdev)
111+
{
112+
struct xsdfec_dev *xsdfec;
113+
114+
xsdfec = platform_get_drvdata(pdev);
115+
misc_deregister(&xsdfec->miscdev);
116+
xsdfec_idr_remove(xsdfec);
117+
return 0;
118+
}
119+
120+
static const struct of_device_id xsdfec_of_match[] = {
121+
{
122+
.compatible = "xlnx,sd-fec-1.1",
123+
},
124+
{ /* end of table */ }
125+
};
126+
MODULE_DEVICE_TABLE(of, xsdfec_of_match);
127+
128+
static struct platform_driver xsdfec_driver = {
129+
.driver = {
130+
.name = "xilinx-sdfec",
131+
.of_match_table = xsdfec_of_match,
132+
},
133+
.probe = xsdfec_probe,
134+
.remove = xsdfec_remove,
135+
};
136+
137+
static int __init xsdfec_init(void)
138+
{
139+
int err;
140+
141+
mutex_init(&dev_idr_lock);
142+
idr_init(&dev_idr);
143+
err = platform_driver_register(&xsdfec_driver);
144+
if (err < 0) {
145+
pr_err("%s Unabled to register SDFEC driver", __func__);
146+
return err;
147+
}
148+
return 0;
149+
}
150+
151+
static void __exit xsdfec_exit(void)
152+
{
153+
platform_driver_unregister(&xsdfec_driver);
154+
idr_destroy(&dev_idr);
155+
}
156+
157+
module_init(xsdfec_init);
158+
module_exit(xsdfec_exit);
159+
160+
MODULE_AUTHOR("Xilinx, Inc");
161+
MODULE_DESCRIPTION("Xilinx SD-FEC16 Driver");
162+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)