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/mod_devicetable.h>
14 #include <linux/uaccess.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
18 #include <uapi/linux/fsi.h>
20 #define FSI_ENGID_SCOM 0x5
22 /* SCOM engine register set */
23 #define SCOM_DATA0_REG 0x00
24 #define SCOM_DATA1_REG 0x04
25 #define SCOM_CMD_REG 0x08
26 #define SCOM_FSI2PIB_RESET_REG 0x18
27 #define SCOM_STATUS_REG 0x1C /* Read */
28 #define SCOM_PIB_RESET_REG 0x1C /* Write */
30 /* Command register */
31 #define SCOM_WRITE_CMD 0x80000000
32 #define SCOM_READ_CMD 0x00000000
34 /* Status register bits */
35 #define SCOM_STATUS_ERR_SUMMARY 0x80000000
36 #define SCOM_STATUS_PROTECTION 0x01000000
37 #define SCOM_STATUS_PARITY 0x04000000
38 #define SCOM_STATUS_PIB_ABORT 0x00100000
39 #define SCOM_STATUS_PIB_RESP_MASK 0x00007000
40 #define SCOM_STATUS_PIB_RESP_SHIFT 12
42 #define SCOM_STATUS_FSI2PIB_ERROR (SCOM_STATUS_PROTECTION | \
43 SCOM_STATUS_PARITY | \
44 SCOM_STATUS_PIB_ABORT)
45 #define SCOM_STATUS_ANY_ERR (SCOM_STATUS_FSI2PIB_ERROR | \
46 SCOM_STATUS_PIB_RESP_MASK)
47 /* SCOM address encodings */
48 #define XSCOM_ADDR_IND_FLAG BIT_ULL(63)
49 #define XSCOM_ADDR_INF_FORM1 BIT_ULL(60)
51 /* SCOM indirect stuff */
52 #define XSCOM_ADDR_DIRECT_PART 0x7fffffffull
53 #define XSCOM_ADDR_INDIRECT_PART 0x000fffff00000000ull
54 #define XSCOM_DATA_IND_READ BIT_ULL(63)
55 #define XSCOM_DATA_IND_COMPLETE BIT_ULL(31)
56 #define XSCOM_DATA_IND_ERR_MASK 0x70000000ull
57 #define XSCOM_DATA_IND_ERR_SHIFT 28
58 #define XSCOM_DATA_IND_DATA 0x0000ffffull
59 #define XSCOM_DATA_IND_FORM1_DATA 0x000fffffffffffffull
60 #define XSCOM_ADDR_FORM1_LOW 0x000ffffffffull
61 #define XSCOM_ADDR_FORM1_HI 0xfff00000000ull
62 #define XSCOM_ADDR_FORM1_HI_SHIFT 20
65 #define SCOM_MAX_IND_RETRIES 10 /* Retries indirect not ready */
68 struct list_head link;
69 struct fsi_device *fsi_dev;
76 static int __put_scom(struct scom_device *scom_dev, uint64_t value,
77 uint32_t addr, uint32_t *status)
79 __be32 data, raw_status;
82 data = cpu_to_be32((value >> 32) & 0xffffffff);
83 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
88 data = cpu_to_be32(value & 0xffffffff);
89 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
94 data = cpu_to_be32(SCOM_WRITE_CMD | addr);
95 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
99 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
103 *status = be32_to_cpu(raw_status);
108 static int __get_scom(struct scom_device *scom_dev, uint64_t *value,
109 uint32_t addr, uint32_t *status)
111 __be32 data, raw_status;
116 data = cpu_to_be32(SCOM_READ_CMD | addr);
117 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
121 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
127 * Read the data registers even on error, so we don't have
128 * to interpret the status register here.
130 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
134 *value |= (uint64_t)be32_to_cpu(data) << 32;
135 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
139 *value |= be32_to_cpu(data);
140 *status = be32_to_cpu(raw_status);
145 static int put_indirect_scom_form0(struct scom_device *scom, uint64_t value,
146 uint64_t addr, uint32_t *status)
148 uint64_t ind_data, ind_addr;
151 if (value & ~XSCOM_DATA_IND_DATA)
154 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
155 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | value;
156 rc = __put_scom(scom, ind_data, ind_addr, status);
157 if (rc || (*status & SCOM_STATUS_ANY_ERR))
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;
170 static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
171 uint64_t addr, uint32_t *status)
173 uint64_t ind_data, ind_addr;
175 if (value & ~XSCOM_DATA_IND_FORM1_DATA)
178 ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
179 ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
180 return __put_scom(scom, ind_data, ind_addr, status);
183 static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
184 uint64_t addr, uint32_t *status)
186 uint64_t ind_data, ind_addr;
189 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
190 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
191 rc = __put_scom(scom, ind_data, ind_addr, status);
192 if (rc || (*status & SCOM_STATUS_ANY_ERR))
195 rc = __get_scom(scom, &ind_data, addr, status);
196 if (rc || (*status & SCOM_STATUS_ANY_ERR))
199 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
200 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
201 *value = ind_data & XSCOM_DATA_IND_DATA;
206 static int raw_put_scom(struct scom_device *scom, uint64_t value,
207 uint64_t addr, uint32_t *status)
209 if (addr & XSCOM_ADDR_IND_FLAG) {
210 if (addr & XSCOM_ADDR_INF_FORM1)
211 return put_indirect_scom_form1(scom, value, addr, status);
213 return put_indirect_scom_form0(scom, value, addr, status);
215 return __put_scom(scom, value, addr, status);
218 static int raw_get_scom(struct scom_device *scom, uint64_t *value,
219 uint64_t addr, uint32_t *status)
221 if (addr & XSCOM_ADDR_IND_FLAG) {
222 if (addr & XSCOM_ADDR_INF_FORM1)
224 return get_indirect_scom_form0(scom, value, addr, status);
226 return __get_scom(scom, value, addr, status);
229 static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
233 if (status & SCOM_STATUS_FSI2PIB_ERROR)
234 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
237 if (status & SCOM_STATUS_PROTECTION)
239 if (status & SCOM_STATUS_PARITY)
242 if (status & SCOM_STATUS_PIB_ABORT)
247 static int handle_pib_status(struct scom_device *scom, uint8_t status)
251 if (status == SCOM_PIB_SUCCESS)
253 if (status == SCOM_PIB_BLOCKED)
256 /* Reset the bridge */
257 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
261 case SCOM_PIB_OFFLINE:
263 case SCOM_PIB_BAD_ADDR:
265 case SCOM_PIB_TIMEOUT:
267 case SCOM_PIB_PARTIAL:
268 case SCOM_PIB_CLK_ERR:
269 case SCOM_PIB_PARITY_ERR:
275 static int put_scom(struct scom_device *scom, uint64_t value,
281 rc = raw_put_scom(scom, value, addr, &status);
285 rc = handle_fsi2pib_status(scom, status);
289 return handle_pib_status(scom,
290 (status & SCOM_STATUS_PIB_RESP_MASK)
291 >> SCOM_STATUS_PIB_RESP_SHIFT);
294 static int get_scom(struct scom_device *scom, uint64_t *value,
300 rc = raw_get_scom(scom, value, addr, &status);
304 rc = handle_fsi2pib_status(scom, status);
308 return handle_pib_status(scom,
309 (status & SCOM_STATUS_PIB_RESP_MASK)
310 >> SCOM_STATUS_PIB_RESP_SHIFT);
313 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
316 struct scom_device *scom = filep->private_data;
317 struct device *dev = &scom->fsi_dev->dev;
321 if (len != sizeof(uint64_t))
324 mutex_lock(&scom->lock);
328 rc = get_scom(scom, &val, *offset);
329 mutex_unlock(&scom->lock);
331 dev_dbg(dev, "get_scom fail:%d\n", rc);
335 rc = copy_to_user(buf, &val, len);
337 dev_dbg(dev, "copy to user failed:%d\n", rc);
339 return rc ? rc : len;
342 static ssize_t scom_write(struct file *filep, const char __user *buf,
343 size_t len, loff_t *offset)
346 struct scom_device *scom = filep->private_data;
347 struct device *dev = &scom->fsi_dev->dev;
350 if (len != sizeof(uint64_t))
353 rc = copy_from_user(&val, buf, len);
355 dev_dbg(dev, "copy from user failed:%d\n", rc);
359 mutex_lock(&scom->lock);
363 rc = put_scom(scom, val, *offset);
364 mutex_unlock(&scom->lock);
366 dev_dbg(dev, "put_scom failed with:%d\n", rc);
373 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
379 file->f_pos = offset;
388 static void raw_convert_status(struct scom_access *acc, uint32_t status)
390 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
391 SCOM_STATUS_PIB_RESP_SHIFT;
392 acc->intf_errors = 0;
394 if (status & SCOM_STATUS_PROTECTION)
395 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
396 else if (status & SCOM_STATUS_PARITY)
397 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
398 else if (status & SCOM_STATUS_PIB_ABORT)
399 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
400 else if (status & SCOM_STATUS_ERR_SUMMARY)
401 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
404 static int scom_raw_read(struct scom_device *scom, void __user *argp)
406 struct scom_access acc;
410 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
413 rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
416 raw_convert_status(&acc, status);
417 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
422 static int scom_raw_write(struct scom_device *scom, void __user *argp)
424 u64 prev_data, mask, data;
425 struct scom_access acc;
429 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
433 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
436 if (status & SCOM_STATUS_ANY_ERR)
440 prev_data = mask = -1ull;
442 data = (prev_data & ~mask) | (acc.data & mask);
443 rc = raw_put_scom(scom, data, acc.addr, &status);
447 raw_convert_status(&acc, status);
448 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
453 static int scom_reset(struct scom_device *scom, void __user *argp)
455 uint32_t flags, dummy = -1;
458 if (get_user(flags, (__u32 __user *)argp))
460 if (flags & SCOM_RESET_PIB)
461 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
463 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
464 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
469 static int scom_check(struct scom_device *scom, void __user *argp)
471 /* Still need to find out how to get "protected" */
472 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
475 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
477 struct scom_device *scom = file->private_data;
478 void __user *argp = (void __user *)arg;
481 mutex_lock(&scom->lock);
483 mutex_unlock(&scom->lock);
488 rc = scom_check(scom, argp);
491 rc = scom_raw_read(scom, argp);
494 rc = scom_raw_write(scom, argp);
497 rc = scom_reset(scom, argp);
500 mutex_unlock(&scom->lock);
504 static int scom_open(struct inode *inode, struct file *file)
506 struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
508 file->private_data = scom;
513 static const struct file_operations scom_fops = {
514 .owner = THIS_MODULE,
516 .llseek = scom_llseek,
519 .unlocked_ioctl = scom_ioctl,
522 static void scom_free(struct device *dev)
524 struct scom_device *scom = container_of(dev, struct scom_device, dev);
526 put_device(&scom->fsi_dev->dev);
530 static int scom_probe(struct device *dev)
532 struct fsi_device *fsi_dev = to_fsi_dev(dev);
533 struct scom_device *scom;
536 scom = kzalloc(sizeof(*scom), GFP_KERNEL);
539 dev_set_drvdata(dev, scom);
540 mutex_init(&scom->lock);
542 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
543 if (!get_device(dev)) {
547 scom->fsi_dev = fsi_dev;
549 /* Create chardev for userspace access */
550 scom->dev.type = &fsi_cdev_type;
551 scom->dev.parent = dev;
552 scom->dev.release = scom_free;
553 device_initialize(&scom->dev);
555 /* Allocate a minor in the FSI space */
556 rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
560 dev_set_name(&scom->dev, "scom%d", didx);
561 cdev_init(&scom->cdev, &scom_fops);
562 rc = cdev_device_add(&scom->cdev, &scom->dev);
564 dev_err(dev, "Error %d creating char device %s\n",
565 rc, dev_name(&scom->dev));
571 fsi_free_minor(scom->dev.devt);
573 put_device(&scom->dev);
577 static int scom_remove(struct device *dev)
579 struct scom_device *scom = dev_get_drvdata(dev);
581 mutex_lock(&scom->lock);
583 mutex_unlock(&scom->lock);
584 cdev_device_del(&scom->cdev, &scom->dev);
585 fsi_free_minor(scom->dev.devt);
586 put_device(&scom->dev);
591 static const struct of_device_id scom_of_ids[] = {
592 { .compatible = "ibm,fsi2pib" },
595 MODULE_DEVICE_TABLE(of, scom_of_ids);
597 static const struct fsi_device_id scom_ids[] = {
599 .engine_type = FSI_ENGID_SCOM,
600 .version = FSI_VERSION_ANY,
605 static struct fsi_driver scom_drv = {
606 .id_table = scom_ids,
609 .bus = &fsi_bus_type,
610 .of_match_table = scom_of_ids,
612 .remove = scom_remove,
616 static int scom_init(void)
618 return fsi_driver_register(&scom_drv);
621 static void scom_exit(void)
623 fsi_driver_unregister(&scom_drv);
626 module_init(scom_init);
627 module_exit(scom_exit);
628 MODULE_LICENSE("GPL");