12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2bd358132SIra W. Snyder /*
3bd358132SIra W. Snyder * Janz CMOD-IO MODULbus Carrier Board PCI Driver
4bd358132SIra W. Snyder *
5bd358132SIra W. Snyder * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
6bd358132SIra W. Snyder *
7bd358132SIra W. Snyder * Lots of inspiration and code was copied from drivers/mfd/sm501.c
8bd358132SIra W. Snyder */
9bd358132SIra W. Snyder
10bd358132SIra W. Snyder #include <linux/kernel.h>
11bd358132SIra W. Snyder #include <linux/module.h>
12bd358132SIra W. Snyder #include <linux/pci.h>
13bd358132SIra W. Snyder #include <linux/interrupt.h>
14bd358132SIra W. Snyder #include <linux/delay.h>
15bd358132SIra W. Snyder #include <linux/platform_device.h>
168102bad2SDavid Miller #include <linux/slab.h>
17bd358132SIra W. Snyder #include <linux/mfd/core.h>
18bd358132SIra W. Snyder
19bd358132SIra W. Snyder #include <linux/mfd/janz.h>
20bd358132SIra W. Snyder
21bd358132SIra W. Snyder #define DRV_NAME "janz-cmodio"
22bd358132SIra W. Snyder
23bd358132SIra W. Snyder /* Size of each MODULbus module in PCI BAR4 */
24bd358132SIra W. Snyder #define CMODIO_MODULBUS_SIZE 0x200
25bd358132SIra W. Snyder
26bd358132SIra W. Snyder /* Maximum number of MODULbus modules on a CMOD-IO carrier board */
27bd358132SIra W. Snyder #define CMODIO_MAX_MODULES 4
28bd358132SIra W. Snyder
29bd358132SIra W. Snyder /* Module Parameters */
30bd358132SIra W. Snyder static unsigned int num_modules = CMODIO_MAX_MODULES;
31bafeafeaSRusty Russell static char *modules[CMODIO_MAX_MODULES] = {
32bd358132SIra W. Snyder "empty", "empty", "empty", "empty",
33bd358132SIra W. Snyder };
34bd358132SIra W. Snyder
35bd358132SIra W. Snyder module_param_array(modules, charp, &num_modules, S_IRUGO);
36bd358132SIra W. Snyder MODULE_PARM_DESC(modules, "MODULbus modules attached to the carrier board");
37bd358132SIra W. Snyder
38bd358132SIra W. Snyder /* Unique Device Id */
39bd358132SIra W. Snyder static unsigned int cmodio_id;
40bd358132SIra W. Snyder
41bd358132SIra W. Snyder struct cmodio_device {
42bd358132SIra W. Snyder /* Parent PCI device */
43bd358132SIra W. Snyder struct pci_dev *pdev;
44bd358132SIra W. Snyder
45bd358132SIra W. Snyder /* PLX control registers */
46bd358132SIra W. Snyder struct janz_cmodio_onboard_regs __iomem *ctrl;
47bd358132SIra W. Snyder
48bd358132SIra W. Snyder /* hex switch position */
49bd358132SIra W. Snyder u8 hex;
50bd358132SIra W. Snyder
51bd358132SIra W. Snyder /* mfd-core API */
52bd358132SIra W. Snyder struct mfd_cell cells[CMODIO_MAX_MODULES];
53bd358132SIra W. Snyder struct resource resources[3 * CMODIO_MAX_MODULES];
54bd358132SIra W. Snyder struct janz_platform_data pdata[CMODIO_MAX_MODULES];
55bd358132SIra W. Snyder };
56bd358132SIra W. Snyder
57bd358132SIra W. Snyder /*
58bd358132SIra W. Snyder * Subdevices using the mfd-core API
59bd358132SIra W. Snyder */
60bd358132SIra W. Snyder
cmodio_setup_subdevice(struct cmodio_device * priv,char * name,unsigned int devno,unsigned int modno)61f791be49SBill Pemberton static int cmodio_setup_subdevice(struct cmodio_device *priv,
62bd358132SIra W. Snyder char *name, unsigned int devno,
63bd358132SIra W. Snyder unsigned int modno)
64bd358132SIra W. Snyder {
65bd358132SIra W. Snyder struct janz_platform_data *pdata;
66bd358132SIra W. Snyder struct mfd_cell *cell;
67bd358132SIra W. Snyder struct resource *res;
68bd358132SIra W. Snyder struct pci_dev *pci;
69bd358132SIra W. Snyder
70bd358132SIra W. Snyder pci = priv->pdev;
71bd358132SIra W. Snyder cell = &priv->cells[devno];
72bd358132SIra W. Snyder res = &priv->resources[devno * 3];
73bd358132SIra W. Snyder pdata = &priv->pdata[devno];
74bd358132SIra W. Snyder
75bd358132SIra W. Snyder cell->name = name;
76bd358132SIra W. Snyder cell->resources = res;
77bd358132SIra W. Snyder cell->num_resources = 3;
78bd358132SIra W. Snyder
79bd358132SIra W. Snyder /* Setup the subdevice ID -- must be unique */
80bd358132SIra W. Snyder cell->id = cmodio_id++;
81bd358132SIra W. Snyder
82bd358132SIra W. Snyder /* Add platform data */
83bd358132SIra W. Snyder pdata->modno = modno;
843d2bdf75SSamuel Ortiz cell->platform_data = pdata;
853d2bdf75SSamuel Ortiz cell->pdata_size = sizeof(*pdata);
86bd358132SIra W. Snyder
87bd358132SIra W. Snyder /* MODULbus registers -- PCI BAR3 is big-endian MODULbus access */
88bd358132SIra W. Snyder res->flags = IORESOURCE_MEM;
89bd358132SIra W. Snyder res->parent = &pci->resource[3];
90bd358132SIra W. Snyder res->start = pci->resource[3].start + (CMODIO_MODULBUS_SIZE * modno);
91bd358132SIra W. Snyder res->end = res->start + CMODIO_MODULBUS_SIZE - 1;
92bd358132SIra W. Snyder res++;
93bd358132SIra W. Snyder
94bd358132SIra W. Snyder /* PLX Control Registers -- PCI BAR4 is interrupt and other registers */
95bd358132SIra W. Snyder res->flags = IORESOURCE_MEM;
96bd358132SIra W. Snyder res->parent = &pci->resource[4];
97bd358132SIra W. Snyder res->start = pci->resource[4].start;
98bd358132SIra W. Snyder res->end = pci->resource[4].end;
99bd358132SIra W. Snyder res++;
100bd358132SIra W. Snyder
101bd358132SIra W. Snyder /*
102bd358132SIra W. Snyder * IRQ
103bd358132SIra W. Snyder *
104bd358132SIra W. Snyder * The start and end fields are used as an offset to the irq_base
105bd358132SIra W. Snyder * parameter passed into the mfd_add_devices() function call. All
106bd358132SIra W. Snyder * devices share the same IRQ.
107bd358132SIra W. Snyder */
108bd358132SIra W. Snyder res->flags = IORESOURCE_IRQ;
109bd358132SIra W. Snyder res->parent = NULL;
110bd358132SIra W. Snyder res->start = 0;
111bd358132SIra W. Snyder res->end = 0;
112bd358132SIra W. Snyder res++;
113bd358132SIra W. Snyder
114bd358132SIra W. Snyder return 0;
115bd358132SIra W. Snyder }
116bd358132SIra W. Snyder
117bd358132SIra W. Snyder /* Probe each submodule using kernel parameters */
cmodio_probe_submodules(struct cmodio_device * priv)118f791be49SBill Pemberton static int cmodio_probe_submodules(struct cmodio_device *priv)
119bd358132SIra W. Snyder {
120bd358132SIra W. Snyder struct pci_dev *pdev = priv->pdev;
121bd358132SIra W. Snyder unsigned int num_probed = 0;
122bd358132SIra W. Snyder char *name;
123bd358132SIra W. Snyder int i;
124bd358132SIra W. Snyder
125bd358132SIra W. Snyder for (i = 0; i < num_modules; i++) {
126bd358132SIra W. Snyder name = modules[i];
127bd358132SIra W. Snyder if (!strcmp(name, "") || !strcmp(name, "empty"))
128bd358132SIra W. Snyder continue;
129bd358132SIra W. Snyder
130bd358132SIra W. Snyder dev_dbg(&priv->pdev->dev, "MODULbus %d: name %s\n", i, name);
131bd358132SIra W. Snyder cmodio_setup_subdevice(priv, name, num_probed, i);
132bd358132SIra W. Snyder num_probed++;
133bd358132SIra W. Snyder }
134bd358132SIra W. Snyder
135bd358132SIra W. Snyder /* print an error message if no modules were probed */
136bd358132SIra W. Snyder if (num_probed == 0) {
137bd358132SIra W. Snyder dev_err(&priv->pdev->dev, "no MODULbus modules specified, "
138bd358132SIra W. Snyder "please set the ``modules'' kernel "
139bd358132SIra W. Snyder "parameter according to your "
140bd358132SIra W. Snyder "hardware configuration\n");
141bd358132SIra W. Snyder return -ENODEV;
142bd358132SIra W. Snyder }
143bd358132SIra W. Snyder
144bd358132SIra W. Snyder return mfd_add_devices(&pdev->dev, 0, priv->cells,
1450848c94fSMark Brown num_probed, NULL, pdev->irq, NULL);
146bd358132SIra W. Snyder }
147bd358132SIra W. Snyder
148bd358132SIra W. Snyder /*
149bd358132SIra W. Snyder * SYSFS Attributes
150bd358132SIra W. Snyder */
151bd358132SIra W. Snyder
modulbus_number_show(struct device * dev,struct device_attribute * attr,char * buf)15224676b3eSZhen Lei static ssize_t modulbus_number_show(struct device *dev,
15324676b3eSZhen Lei struct device_attribute *attr, char *buf)
154bd358132SIra W. Snyder {
155bd358132SIra W. Snyder struct cmodio_device *priv = dev_get_drvdata(dev);
156bd358132SIra W. Snyder
157*6ae210f1SQing Wang return sysfs_emit(buf, "%x\n", priv->hex);
158bd358132SIra W. Snyder }
159bd358132SIra W. Snyder
16024676b3eSZhen Lei static DEVICE_ATTR_RO(modulbus_number);
161bd358132SIra W. Snyder
162bd358132SIra W. Snyder static struct attribute *cmodio_sysfs_attrs[] = {
163bd358132SIra W. Snyder &dev_attr_modulbus_number.attr,
164bd358132SIra W. Snyder NULL,
165bd358132SIra W. Snyder };
166bd358132SIra W. Snyder
167bd358132SIra W. Snyder static const struct attribute_group cmodio_sysfs_attr_group = {
168bd358132SIra W. Snyder .attrs = cmodio_sysfs_attrs,
169bd358132SIra W. Snyder };
170bd358132SIra W. Snyder
171bd358132SIra W. Snyder /*
172bd358132SIra W. Snyder * PCI Driver
173bd358132SIra W. Snyder */
174bd358132SIra W. Snyder
cmodio_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)175f791be49SBill Pemberton static int cmodio_pci_probe(struct pci_dev *dev,
176bd358132SIra W. Snyder const struct pci_device_id *id)
177bd358132SIra W. Snyder {
178bd358132SIra W. Snyder struct cmodio_device *priv;
179bd358132SIra W. Snyder int ret;
180bd358132SIra W. Snyder
181fa75d4a6SLee Jones priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
182292155beSMarkus Elfring if (!priv)
183fa75d4a6SLee Jones return -ENOMEM;
184bd358132SIra W. Snyder
185bd358132SIra W. Snyder pci_set_drvdata(dev, priv);
186bd358132SIra W. Snyder priv->pdev = dev;
187bd358132SIra W. Snyder
188bd358132SIra W. Snyder /* Hardware Initialization */
189bd358132SIra W. Snyder ret = pci_enable_device(dev);
190bd358132SIra W. Snyder if (ret) {
191bd358132SIra W. Snyder dev_err(&dev->dev, "unable to enable device\n");
192fa75d4a6SLee Jones return ret;
193bd358132SIra W. Snyder }
194bd358132SIra W. Snyder
195bd358132SIra W. Snyder pci_set_master(dev);
196bd358132SIra W. Snyder ret = pci_request_regions(dev, DRV_NAME);
197bd358132SIra W. Snyder if (ret) {
198bd358132SIra W. Snyder dev_err(&dev->dev, "unable to request regions\n");
199bd358132SIra W. Snyder goto out_pci_disable_device;
200bd358132SIra W. Snyder }
201bd358132SIra W. Snyder
202bd358132SIra W. Snyder /* Onboard configuration registers */
203bd358132SIra W. Snyder priv->ctrl = pci_ioremap_bar(dev, 4);
204bd358132SIra W. Snyder if (!priv->ctrl) {
205bd358132SIra W. Snyder dev_err(&dev->dev, "unable to remap onboard regs\n");
206bd358132SIra W. Snyder ret = -ENOMEM;
207bd358132SIra W. Snyder goto out_pci_release_regions;
208bd358132SIra W. Snyder }
209bd358132SIra W. Snyder
210bd358132SIra W. Snyder /* Read the hex switch on the carrier board */
211bd358132SIra W. Snyder priv->hex = ioread8(&priv->ctrl->int_enable);
212bd358132SIra W. Snyder
213bd358132SIra W. Snyder /* Add the MODULbus number (hex switch value) to the device's sysfs */
214bd358132SIra W. Snyder ret = sysfs_create_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
215bd358132SIra W. Snyder if (ret) {
216bd358132SIra W. Snyder dev_err(&dev->dev, "unable to create sysfs attributes\n");
217bd358132SIra W. Snyder goto out_unmap_ctrl;
218bd358132SIra W. Snyder }
219bd358132SIra W. Snyder
220bd358132SIra W. Snyder /*
221bd358132SIra W. Snyder * Disable all interrupt lines, each submodule will enable its
222bd358132SIra W. Snyder * own interrupt line if needed
223bd358132SIra W. Snyder */
224bd358132SIra W. Snyder iowrite8(0xf, &priv->ctrl->int_disable);
225bd358132SIra W. Snyder
226bd358132SIra W. Snyder /* Register drivers for all submodules */
227bd358132SIra W. Snyder ret = cmodio_probe_submodules(priv);
228bd358132SIra W. Snyder if (ret) {
229bd358132SIra W. Snyder dev_err(&dev->dev, "unable to probe submodules\n");
230bd358132SIra W. Snyder goto out_sysfs_remove_group;
231bd358132SIra W. Snyder }
232bd358132SIra W. Snyder
233bd358132SIra W. Snyder return 0;
234bd358132SIra W. Snyder
235bd358132SIra W. Snyder out_sysfs_remove_group:
236bd358132SIra W. Snyder sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
237bd358132SIra W. Snyder out_unmap_ctrl:
238bd358132SIra W. Snyder iounmap(priv->ctrl);
239bd358132SIra W. Snyder out_pci_release_regions:
240bd358132SIra W. Snyder pci_release_regions(dev);
241bd358132SIra W. Snyder out_pci_disable_device:
242bd358132SIra W. Snyder pci_disable_device(dev);
243fa75d4a6SLee Jones
244bd358132SIra W. Snyder return ret;
245bd358132SIra W. Snyder }
246bd358132SIra W. Snyder
cmodio_pci_remove(struct pci_dev * dev)2474740f73fSBill Pemberton static void cmodio_pci_remove(struct pci_dev *dev)
248bd358132SIra W. Snyder {
249bd358132SIra W. Snyder struct cmodio_device *priv = pci_get_drvdata(dev);
250bd358132SIra W. Snyder
251bd358132SIra W. Snyder mfd_remove_devices(&dev->dev);
252bd358132SIra W. Snyder sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
253bd358132SIra W. Snyder iounmap(priv->ctrl);
254bd358132SIra W. Snyder pci_release_regions(dev);
255bd358132SIra W. Snyder pci_disable_device(dev);
256bd358132SIra W. Snyder }
257bd358132SIra W. Snyder
258bd358132SIra W. Snyder #define PCI_VENDOR_ID_JANZ 0x13c3
259bd358132SIra W. Snyder
260bd358132SIra W. Snyder /* The list of devices that this module will support */
26136fcd06cSJingoo Han static const struct pci_device_id cmodio_pci_ids[] = {
262bd358132SIra W. Snyder { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0101 },
263bd358132SIra W. Snyder { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0100 },
264844e0edfSAndreas Gröger { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0201 },
265844e0edfSAndreas Gröger { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0202 },
266844e0edfSAndreas Gröger { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0201 },
267844e0edfSAndreas Gröger { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0202 },
268bd358132SIra W. Snyder { 0, }
269bd358132SIra W. Snyder };
270bd358132SIra W. Snyder MODULE_DEVICE_TABLE(pci, cmodio_pci_ids);
271bd358132SIra W. Snyder
272bd358132SIra W. Snyder static struct pci_driver cmodio_pci_driver = {
273bd358132SIra W. Snyder .name = DRV_NAME,
274bd358132SIra W. Snyder .id_table = cmodio_pci_ids,
275bd358132SIra W. Snyder .probe = cmodio_pci_probe,
27684449216SBill Pemberton .remove = cmodio_pci_remove,
277bd358132SIra W. Snyder };
278bd358132SIra W. Snyder
27938a36f5aSAxel Lin module_pci_driver(cmodio_pci_driver);
280bd358132SIra W. Snyder
281bd358132SIra W. Snyder MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
282bd358132SIra W. Snyder MODULE_DESCRIPTION("Janz CMOD-IO PCI MODULbus Carrier Board Driver");
283bd358132SIra W. Snyder MODULE_LICENSE("GPL");
284