1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) KEBA Industrial Automation Gmbh 2024
5 * Driver for KEBA system FPGA
7 * The KEBA system FPGA implements various devices. This driver registers
8 * auxiliary devices for every device within the FPGA.
11 #include <linux/device.h>
12 #include <linux/i2c.h>
13 #include <linux/misc/keba.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
19 #define PCI_VENDOR_ID_KEBA 0xCEBA
20 #define PCI_DEVICE_ID_KEBA_CP035 0x2706
21 #define PCI_DEVICE_ID_KEBA_CP505 0x2703
22 #define PCI_DEVICE_ID_KEBA_CP520 0x2696
24 #define CP500_SYS_BAR 0
25 #define CP500_ECM_BAR 1
28 #define CP500_VERSION_REG 0x00
29 #define CP500_RECONFIG_REG 0x11 /* upper 8-bits of STARTUP register */
30 #define CP500_AXI_REG 0x40
32 /* Bits in BUILD_REG */
33 #define CP500_BUILD_TEST 0x8000 /* FPGA test version */
35 /* Bits in RECONFIG_REG */
36 #define CP500_RECFG_REQ 0x01 /* reconfigure FPGA on next reset */
39 #define CP500_AXI_MSIX 3
40 #define CP500_NUM_MSIX 8
41 #define CP500_NUM_MSIX_NO_MMI 2
42 #define CP500_NUM_MSIX_NO_AXI 3
45 #define CP500_HW_CPU_EEPROM_NAME "cp500_cpu_eeprom"
47 #define CP500_IS_CP035(dev) ((dev)->pci_dev->device == PCI_DEVICE_ID_KEBA_CP035)
48 #define CP500_IS_CP505(dev) ((dev)->pci_dev->device == PCI_DEVICE_ID_KEBA_CP505)
49 #define CP500_IS_CP520(dev) ((dev)->pci_dev->device == PCI_DEVICE_ID_KEBA_CP520)
51 struct cp500_dev_info {
57 struct cp500_dev_info startup;
58 struct cp500_dev_info i2c;
61 /* list of devices within FPGA of CP035 family (CP035, CP056, CP057) */
62 static struct cp500_devs cp035_devices = {
63 .startup = { 0x0000, SZ_4K },
64 .i2c = { 0x4000, SZ_4K },
67 /* list of devices within FPGA of CP505 family (CP503, CP505, CP507) */
68 static struct cp500_devs cp505_devices = {
69 .startup = { 0x0000, SZ_4K },
70 .i2c = { 0x5000, SZ_4K },
73 /* list of devices within FPGA of CP520 family (CP520, CP530) */
74 static struct cp500_devs cp520_devices = {
75 .startup = { 0x0000, SZ_4K },
76 .i2c = { 0x5000, SZ_4K },
80 struct pci_dev *pci_dev;
81 struct cp500_devs *devs;
90 resource_size_t sys_hwbase;
91 struct keba_i2c_auxdev *i2c;
93 /* ECM EtherCAT BAR */
94 resource_size_t ecm_hwbase;
96 void __iomem *system_startup_addr;
100 static struct i2c_board_info cp500_i2c_info[] = {
101 { /* temperature sensor */
102 I2C_BOARD_INFO("emc1403", 0x4c),
106 * CP035 family: CPU board
107 * CP505 family: bridge board
108 * CP520 family: carrier board
110 I2C_BOARD_INFO("24c32", 0x50),
111 .dev_name = CP500_HW_CPU_EEPROM_NAME,
113 { /* interface board EEPROM */
114 I2C_BOARD_INFO("24c32", 0x51),
118 * CP505 family: CPU board
119 * CP520 family: MMI board
121 I2C_BOARD_INFO("24c32", 0x52),
123 { /* extension module 0 EEPROM (optional) */
124 I2C_BOARD_INFO("24c32", 0x53),
126 { /* extension module 1 EEPROM (optional) */
127 I2C_BOARD_INFO("24c32", 0x54),
129 { /* extension module 2 EEPROM (optional) */
130 I2C_BOARD_INFO("24c32", 0x55),
132 { /* extension module 3 EEPROM (optional) */
133 I2C_BOARD_INFO("24c32", 0x56),
137 static ssize_t cp500_get_fpga_version(struct cp500 *cp500, char *buf,
142 if (CP500_IS_CP035(cp500))
143 n = scnprintf(buf, max_len, "CP035");
144 else if (CP500_IS_CP505(cp500))
145 n = scnprintf(buf, max_len, "CP505");
147 n = scnprintf(buf, max_len, "CP500");
149 n += scnprintf(buf + n, max_len - n, "_FPGA_%d.%02d",
150 cp500->version.major, cp500->version.minor);
152 /* test versions have test bit set */
153 if (cp500->version.build & CP500_BUILD_TEST)
154 n += scnprintf(buf + n, max_len - n, "Test%d",
155 cp500->version.build & ~CP500_BUILD_TEST);
157 n += scnprintf(buf + n, max_len - n, "\n");
162 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
165 struct cp500 *cp500 = dev_get_drvdata(dev);
167 return cp500_get_fpga_version(cp500, buf, PAGE_SIZE);
169 static DEVICE_ATTR_RO(version);
171 static ssize_t keep_cfg_show(struct device *dev, struct device_attribute *attr,
174 struct cp500 *cp500 = dev_get_drvdata(dev);
175 unsigned long keep_cfg = 1;
178 * FPGA configuration stream is kept during reset when RECONFIG bit is
181 if (ioread8(cp500->system_startup_addr + CP500_RECONFIG_REG) &
185 return sysfs_emit(buf, "%lu\n", keep_cfg);
188 static ssize_t keep_cfg_store(struct device *dev, struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct cp500 *cp500 = dev_get_drvdata(dev);
192 unsigned long keep_cfg;
194 if (kstrtoul(buf, 10, &keep_cfg) < 0)
198 * In normal operation "keep_cfg" is "1". This means that the FPGA keeps
199 * its configuration stream during a reset.
200 * In case of a firmware update of the FPGA, the configuration stream
201 * needs to be reloaded. This can be done without a powercycle by
202 * writing a "0" into the "keep_cfg" attribute. After a reset/reboot th
203 * new configuration stream will be loaded.
206 iowrite8(0, cp500->system_startup_addr + CP500_RECONFIG_REG);
208 iowrite8(CP500_RECFG_REQ,
209 cp500->system_startup_addr + CP500_RECONFIG_REG);
213 static DEVICE_ATTR_RW(keep_cfg);
215 static struct attribute *attrs[] = {
216 &dev_attr_version.attr,
217 &dev_attr_keep_cfg.attr,
220 static const struct attribute_group attrs_group = { .attrs = attrs };
222 static void cp500_i2c_release(struct device *dev)
224 struct keba_i2c_auxdev *i2c =
225 container_of(dev, struct keba_i2c_auxdev, auxdev.dev);
230 static int cp500_register_i2c(struct cp500 *cp500)
234 cp500->i2c = kzalloc(sizeof(*cp500->i2c), GFP_KERNEL);
238 cp500->i2c->auxdev.name = "i2c";
239 cp500->i2c->auxdev.id = 0;
240 cp500->i2c->auxdev.dev.release = cp500_i2c_release;
241 cp500->i2c->auxdev.dev.parent = &cp500->pci_dev->dev;
242 cp500->i2c->io = (struct resource) {
243 /* I2C register area */
244 .start = (resource_size_t) cp500->sys_hwbase +
245 cp500->devs->i2c.offset,
246 .end = (resource_size_t) cp500->sys_hwbase +
247 cp500->devs->i2c.offset +
248 cp500->devs->i2c.size - 1,
249 .flags = IORESOURCE_MEM,
251 cp500->i2c->info_size = ARRAY_SIZE(cp500_i2c_info);
252 cp500->i2c->info = cp500_i2c_info;
254 retval = auxiliary_device_init(&cp500->i2c->auxdev);
261 retval = __auxiliary_device_add(&cp500->i2c->auxdev, "keba");
263 auxiliary_device_uninit(&cp500->i2c->auxdev);
272 static void cp500_register_auxiliary_devs(struct cp500 *cp500)
274 struct device *dev = &cp500->pci_dev->dev;
276 if (cp500_register_i2c(cp500))
277 dev_warn(dev, "Failed to register i2c!\n");
280 static void cp500_unregister_dev(struct auxiliary_device *auxdev)
282 auxiliary_device_delete(auxdev);
283 auxiliary_device_uninit(auxdev);
286 static void cp500_unregister_auxiliary_devs(struct cp500 *cp500)
290 cp500_unregister_dev(&cp500->i2c->auxdev);
295 static irqreturn_t cp500_axi_handler(int irq, void *dev)
297 struct cp500 *cp500 = dev;
298 u32 axi_address = ioread32(cp500->system_startup_addr + CP500_AXI_REG);
301 * FPGA signals AXI response error, print AXI address to indicate which
302 * IP core was affected
304 dev_err(&cp500->pci_dev->dev, "AXI response error at 0x%08x\n",
310 static int cp500_enable(struct cp500 *cp500)
315 if (cp500->msix_num > CP500_NUM_MSIX_NO_AXI) {
316 axi_irq = pci_irq_vector(cp500->pci_dev, CP500_AXI_MSIX);
317 ret = request_irq(axi_irq, cp500_axi_handler, 0,
320 dev_err(&cp500->pci_dev->dev,
321 "Failed to register AXI response error!\n");
329 static void cp500_disable(struct cp500 *cp500)
333 if (cp500->msix_num > CP500_NUM_MSIX_NO_AXI) {
334 axi_irq = pci_irq_vector(cp500->pci_dev, CP500_AXI_MSIX);
335 free_irq(axi_irq, cp500);
339 static int cp500_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
341 struct device *dev = &pci_dev->dev;
342 struct resource startup;
348 cp500 = devm_kzalloc(dev, sizeof(*cp500), GFP_KERNEL);
351 cp500->pci_dev = pci_dev;
352 cp500->sys_hwbase = pci_resource_start(pci_dev, CP500_SYS_BAR);
353 cp500->ecm_hwbase = pci_resource_start(pci_dev, CP500_ECM_BAR);
354 if (!cp500->sys_hwbase || !cp500->ecm_hwbase)
357 if (CP500_IS_CP035(cp500))
358 cp500->devs = &cp035_devices;
359 else if (CP500_IS_CP505(cp500))
360 cp500->devs = &cp505_devices;
361 else if (CP500_IS_CP520(cp500))
362 cp500->devs = &cp520_devices;
366 ret = pci_enable_device(pci_dev);
369 pci_set_master(pci_dev);
371 startup = *pci_resource_n(pci_dev, CP500_SYS_BAR);
372 startup.end = startup.start + cp500->devs->startup.size - 1;
373 cp500->system_startup_addr = devm_ioremap_resource(&pci_dev->dev,
375 if (IS_ERR(cp500->system_startup_addr)) {
376 ret = PTR_ERR(cp500->system_startup_addr);
380 cp500->msix_num = pci_alloc_irq_vectors(pci_dev, CP500_NUM_MSIX_NO_MMI,
381 CP500_NUM_MSIX, PCI_IRQ_MSIX);
382 if (cp500->msix_num < CP500_NUM_MSIX_NO_MMI) {
383 dev_err(&pci_dev->dev,
384 "Hardware does not support enough MSI-X interrupts\n");
389 cp500_vers = ioread32(cp500->system_startup_addr + CP500_VERSION_REG);
390 cp500->version.major = (cp500_vers & 0xff);
391 cp500->version.minor = (cp500_vers >> 8) & 0xff;
392 cp500->version.build = (cp500_vers >> 16) & 0xffff;
393 cp500_get_fpga_version(cp500, buf, sizeof(buf));
395 dev_info(&pci_dev->dev, "FPGA version %s", buf);
397 pci_set_drvdata(pci_dev, cp500);
399 ret = sysfs_create_group(&pci_dev->dev.kobj, &attrs_group);
403 ret = cp500_enable(cp500);
405 goto out_remove_group;
407 cp500_register_auxiliary_devs(cp500);
412 sysfs_remove_group(&pci_dev->dev.kobj, &attrs_group);
414 pci_free_irq_vectors(pci_dev);
416 pci_clear_master(pci_dev);
417 pci_disable_device(pci_dev);
422 static void cp500_remove(struct pci_dev *pci_dev)
424 struct cp500 *cp500 = pci_get_drvdata(pci_dev);
426 cp500_unregister_auxiliary_devs(cp500);
428 cp500_disable(cp500);
430 sysfs_remove_group(&pci_dev->dev.kobj, &attrs_group);
432 pci_set_drvdata(pci_dev, 0);
434 pci_free_irq_vectors(pci_dev);
436 pci_clear_master(pci_dev);
437 pci_disable_device(pci_dev);
440 static struct pci_device_id cp500_ids[] = {
441 { PCI_DEVICE(PCI_VENDOR_ID_KEBA, PCI_DEVICE_ID_KEBA_CP035) },
442 { PCI_DEVICE(PCI_VENDOR_ID_KEBA, PCI_DEVICE_ID_KEBA_CP505) },
443 { PCI_DEVICE(PCI_VENDOR_ID_KEBA, PCI_DEVICE_ID_KEBA_CP520) },
446 MODULE_DEVICE_TABLE(pci, cp500_ids);
448 static struct pci_driver cp500_driver = {
450 .id_table = cp500_ids,
451 .probe = cp500_probe,
452 .remove = cp500_remove,
454 module_pci_driver(cp500_driver);
457 MODULE_DESCRIPTION("KEBA CP500 system FPGA driver");
458 MODULE_LICENSE("GPL");