1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCOM FSI Client device driver
5 * Copyright (C) IBM Corporation 2016
9 #include <linux/module.h>
10 #include <linux/cdev.h>
11 #include <linux/delay.h>
13 #include <linux/uaccess.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
17 #include <uapi/linux/fsi.h>
19 #define FSI_ENGID_SCOM 0x5
21 /* SCOM engine register set */
22 #define SCOM_DATA0_REG 0x00
23 #define SCOM_DATA1_REG 0x04
24 #define SCOM_CMD_REG 0x08
25 #define SCOM_FSI2PIB_RESET_REG 0x18
26 #define SCOM_STATUS_REG 0x1C /* Read */
27 #define SCOM_PIB_RESET_REG 0x1C /* Write */
29 /* Command register */
30 #define SCOM_WRITE_CMD 0x80000000
31 #define SCOM_READ_CMD 0x00000000
33 /* Status register bits */
34 #define SCOM_STATUS_ERR_SUMMARY 0x80000000
35 #define SCOM_STATUS_PROTECTION 0x01000000
36 #define SCOM_STATUS_PARITY 0x04000000
37 #define SCOM_STATUS_PIB_ABORT 0x00100000
38 #define SCOM_STATUS_PIB_RESP_MASK 0x00007000
39 #define SCOM_STATUS_PIB_RESP_SHIFT 12
41 #define SCOM_STATUS_FSI2PIB_ERROR (SCOM_STATUS_PROTECTION | \
42 SCOM_STATUS_PARITY | \
43 SCOM_STATUS_PIB_ABORT)
44 #define SCOM_STATUS_ANY_ERR (SCOM_STATUS_FSI2PIB_ERROR | \
45 SCOM_STATUS_PIB_RESP_MASK)
46 /* SCOM address encodings */
47 #define XSCOM_ADDR_IND_FLAG BIT_ULL(63)
48 #define XSCOM_ADDR_INF_FORM1 BIT_ULL(60)
50 /* SCOM indirect stuff */
51 #define XSCOM_ADDR_DIRECT_PART 0x7fffffffull
52 #define XSCOM_ADDR_INDIRECT_PART 0x000fffff00000000ull
53 #define XSCOM_DATA_IND_READ BIT_ULL(63)
54 #define XSCOM_DATA_IND_COMPLETE BIT_ULL(31)
55 #define XSCOM_DATA_IND_ERR_MASK 0x70000000ull
56 #define XSCOM_DATA_IND_ERR_SHIFT 28
57 #define XSCOM_DATA_IND_DATA 0x0000ffffull
58 #define XSCOM_DATA_IND_FORM1_DATA 0x000fffffffffffffull
59 #define XSCOM_ADDR_FORM1_LOW 0x000ffffffffull
60 #define XSCOM_ADDR_FORM1_HI 0xfff00000000ull
61 #define XSCOM_ADDR_FORM1_HI_SHIFT 20
64 #define SCOM_MAX_IND_RETRIES 10 /* Retries indirect not ready */
67 struct list_head link;
68 struct fsi_device *fsi_dev;
75 static int __put_scom(struct scom_device *scom_dev, uint64_t value,
76 uint32_t addr, uint32_t *status)
78 __be32 data, raw_status;
81 data = cpu_to_be32((value >> 32) & 0xffffffff);
82 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
87 data = cpu_to_be32(value & 0xffffffff);
88 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
93 data = cpu_to_be32(SCOM_WRITE_CMD | addr);
94 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
98 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
102 *status = be32_to_cpu(raw_status);
107 static int __get_scom(struct scom_device *scom_dev, uint64_t *value,
108 uint32_t addr, uint32_t *status)
110 __be32 data, raw_status;
115 data = cpu_to_be32(SCOM_READ_CMD | addr);
116 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
120 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
126 * Read the data registers even on error, so we don't have
127 * to interpret the status register here.
129 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
133 *value |= (uint64_t)be32_to_cpu(data) << 32;
134 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
138 *value |= be32_to_cpu(data);
139 *status = be32_to_cpu(raw_status);
144 static int put_indirect_scom_form0(struct scom_device *scom, uint64_t value,
145 uint64_t addr, uint32_t *status)
147 uint64_t ind_data, ind_addr;
148 int rc, retries, err = 0;
150 if (value & ~XSCOM_DATA_IND_DATA)
153 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
154 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | value;
155 rc = __put_scom(scom, ind_data, ind_addr, status);
156 if (rc || (*status & SCOM_STATUS_ANY_ERR))
159 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
160 rc = __get_scom(scom, &ind_data, addr, status);
161 if (rc || (*status & SCOM_STATUS_ANY_ERR))
164 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
165 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
166 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
174 static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
175 uint64_t addr, uint32_t *status)
177 uint64_t ind_data, ind_addr;
179 if (value & ~XSCOM_DATA_IND_FORM1_DATA)
182 ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
183 ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
184 return __put_scom(scom, ind_data, ind_addr, status);
187 static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
188 uint64_t addr, uint32_t *status)
190 uint64_t ind_data, ind_addr;
191 int rc, retries, err = 0;
193 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
194 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
195 rc = __put_scom(scom, ind_data, ind_addr, status);
196 if (rc || (*status & SCOM_STATUS_ANY_ERR))
199 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
200 rc = __get_scom(scom, &ind_data, addr, status);
201 if (rc || (*status & SCOM_STATUS_ANY_ERR))
204 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
205 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
206 *value = ind_data & XSCOM_DATA_IND_DATA;
208 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
216 static int raw_put_scom(struct scom_device *scom, uint64_t value,
217 uint64_t addr, uint32_t *status)
219 if (addr & XSCOM_ADDR_IND_FLAG) {
220 if (addr & XSCOM_ADDR_INF_FORM1)
221 return put_indirect_scom_form1(scom, value, addr, status);
223 return put_indirect_scom_form0(scom, value, addr, status);
225 return __put_scom(scom, value, addr, status);
228 static int raw_get_scom(struct scom_device *scom, uint64_t *value,
229 uint64_t addr, uint32_t *status)
231 if (addr & XSCOM_ADDR_IND_FLAG) {
232 if (addr & XSCOM_ADDR_INF_FORM1)
234 return get_indirect_scom_form0(scom, value, addr, status);
236 return __get_scom(scom, value, addr, status);
239 static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
243 if (status & SCOM_STATUS_FSI2PIB_ERROR)
244 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
247 if (status & SCOM_STATUS_PROTECTION)
249 if (status & SCOM_STATUS_PARITY)
252 if (status & SCOM_STATUS_PIB_ABORT)
257 static int handle_pib_status(struct scom_device *scom, uint8_t status)
261 if (status == SCOM_PIB_SUCCESS)
263 if (status == SCOM_PIB_BLOCKED)
266 /* Reset the bridge */
267 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
271 case SCOM_PIB_OFFLINE:
273 case SCOM_PIB_BAD_ADDR:
275 case SCOM_PIB_TIMEOUT:
277 case SCOM_PIB_PARTIAL:
278 case SCOM_PIB_CLK_ERR:
279 case SCOM_PIB_PARITY_ERR:
285 static int put_scom(struct scom_device *scom, uint64_t value,
291 rc = raw_put_scom(scom, value, addr, &status);
295 rc = handle_fsi2pib_status(scom, status);
299 return handle_pib_status(scom,
300 (status & SCOM_STATUS_PIB_RESP_MASK)
301 >> SCOM_STATUS_PIB_RESP_SHIFT);
304 static int get_scom(struct scom_device *scom, uint64_t *value,
310 rc = raw_get_scom(scom, value, addr, &status);
314 rc = handle_fsi2pib_status(scom, status);
318 return handle_pib_status(scom,
319 (status & SCOM_STATUS_PIB_RESP_MASK)
320 >> SCOM_STATUS_PIB_RESP_SHIFT);
323 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
326 struct scom_device *scom = filep->private_data;
327 struct device *dev = &scom->fsi_dev->dev;
331 if (len != sizeof(uint64_t))
334 mutex_lock(&scom->lock);
338 rc = get_scom(scom, &val, *offset);
339 mutex_unlock(&scom->lock);
341 dev_dbg(dev, "get_scom fail:%d\n", rc);
345 rc = copy_to_user(buf, &val, len);
347 dev_dbg(dev, "copy to user failed:%d\n", rc);
349 return rc ? rc : len;
352 static ssize_t scom_write(struct file *filep, const char __user *buf,
353 size_t len, loff_t *offset)
356 struct scom_device *scom = filep->private_data;
357 struct device *dev = &scom->fsi_dev->dev;
360 if (len != sizeof(uint64_t))
363 rc = copy_from_user(&val, buf, len);
365 dev_dbg(dev, "copy from user failed:%d\n", rc);
369 mutex_lock(&scom->lock);
373 rc = put_scom(scom, val, *offset);
374 mutex_unlock(&scom->lock);
376 dev_dbg(dev, "put_scom failed with:%d\n", rc);
383 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
389 file->f_pos = offset;
398 static void raw_convert_status(struct scom_access *acc, uint32_t status)
400 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
401 SCOM_STATUS_PIB_RESP_SHIFT;
402 acc->intf_errors = 0;
404 if (status & SCOM_STATUS_PROTECTION)
405 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
406 else if (status & SCOM_STATUS_PARITY)
407 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
408 else if (status & SCOM_STATUS_PIB_ABORT)
409 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
410 else if (status & SCOM_STATUS_ERR_SUMMARY)
411 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
414 static int scom_raw_read(struct scom_device *scom, void __user *argp)
416 struct scom_access acc;
420 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
423 rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
426 raw_convert_status(&acc, status);
427 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
432 static int scom_raw_write(struct scom_device *scom, void __user *argp)
434 u64 prev_data, mask, data;
435 struct scom_access acc;
439 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
443 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
446 if (status & SCOM_STATUS_ANY_ERR)
450 prev_data = mask = -1ull;
452 data = (prev_data & ~mask) | (acc.data & mask);
453 rc = raw_put_scom(scom, data, acc.addr, &status);
457 raw_convert_status(&acc, status);
458 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
463 static int scom_reset(struct scom_device *scom, void __user *argp)
465 uint32_t flags, dummy = -1;
468 if (get_user(flags, (__u32 __user *)argp))
470 if (flags & SCOM_RESET_PIB)
471 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
473 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
474 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
479 static int scom_check(struct scom_device *scom, void __user *argp)
481 /* Still need to find out how to get "protected" */
482 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
485 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
487 struct scom_device *scom = file->private_data;
488 void __user *argp = (void __user *)arg;
491 mutex_lock(&scom->lock);
493 mutex_unlock(&scom->lock);
498 rc = scom_check(scom, argp);
501 rc = scom_raw_read(scom, argp);
504 rc = scom_raw_write(scom, argp);
507 rc = scom_reset(scom, argp);
510 mutex_unlock(&scom->lock);
514 static int scom_open(struct inode *inode, struct file *file)
516 struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
518 file->private_data = scom;
523 static const struct file_operations scom_fops = {
524 .owner = THIS_MODULE,
526 .llseek = scom_llseek,
529 .unlocked_ioctl = scom_ioctl,
532 static void scom_free(struct device *dev)
534 struct scom_device *scom = container_of(dev, struct scom_device, dev);
536 put_device(&scom->fsi_dev->dev);
540 static int scom_probe(struct device *dev)
542 struct fsi_device *fsi_dev = to_fsi_dev(dev);
543 struct scom_device *scom;
546 scom = kzalloc(sizeof(*scom), GFP_KERNEL);
549 dev_set_drvdata(dev, scom);
550 mutex_init(&scom->lock);
552 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
553 if (!get_device(dev)) {
557 scom->fsi_dev = fsi_dev;
559 /* Create chardev for userspace access */
560 scom->dev.type = &fsi_cdev_type;
561 scom->dev.parent = dev;
562 scom->dev.release = scom_free;
563 device_initialize(&scom->dev);
565 /* Allocate a minor in the FSI space */
566 rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
570 dev_set_name(&scom->dev, "scom%d", didx);
571 cdev_init(&scom->cdev, &scom_fops);
572 rc = cdev_device_add(&scom->cdev, &scom->dev);
574 dev_err(dev, "Error %d creating char device %s\n",
575 rc, dev_name(&scom->dev));
581 fsi_free_minor(scom->dev.devt);
583 put_device(&scom->dev);
587 static int scom_remove(struct device *dev)
589 struct scom_device *scom = dev_get_drvdata(dev);
591 mutex_lock(&scom->lock);
593 mutex_unlock(&scom->lock);
594 cdev_device_del(&scom->cdev, &scom->dev);
595 fsi_free_minor(scom->dev.devt);
596 put_device(&scom->dev);
601 static const struct fsi_device_id scom_ids[] = {
603 .engine_type = FSI_ENGID_SCOM,
604 .version = FSI_VERSION_ANY,
609 static struct fsi_driver scom_drv = {
610 .id_table = scom_ids,
613 .bus = &fsi_bus_type,
615 .remove = scom_remove,
619 static int scom_init(void)
621 return fsi_driver_register(&scom_drv);
624 static void scom_exit(void)
626 fsi_driver_unregister(&scom_drv);
629 module_init(scom_init);
630 module_exit(scom_exit);
631 MODULE_LICENSE("GPL");