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_ANY_ERR (SCOM_STATUS_ERR_SUMMARY | \
42 SCOM_STATUS_PROTECTION | \
43 SCOM_STATUS_PARITY | \
44 SCOM_STATUS_PIB_ABORT | \
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_RETRIES 100 /* Retries on busy */
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;
149 int rc, retries, err = 0;
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 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
161 rc = __get_scom(scom, &ind_data, addr, status);
162 if (rc || (*status & SCOM_STATUS_ANY_ERR))
165 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
166 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
167 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
175 static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
176 uint64_t addr, uint32_t *status)
178 uint64_t ind_data, ind_addr;
180 if (value & ~XSCOM_DATA_IND_FORM1_DATA)
183 ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
184 ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
185 return __put_scom(scom, ind_data, ind_addr, status);
188 static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
189 uint64_t addr, uint32_t *status)
191 uint64_t ind_data, ind_addr;
192 int rc, retries, err = 0;
194 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
195 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
196 rc = __put_scom(scom, ind_data, ind_addr, status);
197 if (rc || (*status & SCOM_STATUS_ANY_ERR))
200 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
201 rc = __get_scom(scom, &ind_data, addr, status);
202 if (rc || (*status & SCOM_STATUS_ANY_ERR))
205 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
206 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
207 *value = ind_data & XSCOM_DATA_IND_DATA;
209 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
217 static int raw_put_scom(struct scom_device *scom, uint64_t value,
218 uint64_t addr, uint32_t *status)
220 if (addr & XSCOM_ADDR_IND_FLAG) {
221 if (addr & XSCOM_ADDR_INF_FORM1)
222 return put_indirect_scom_form1(scom, value, addr, status);
224 return put_indirect_scom_form0(scom, value, addr, status);
226 return __put_scom(scom, value, addr, status);
229 static int raw_get_scom(struct scom_device *scom, uint64_t *value,
230 uint64_t addr, uint32_t *status)
232 if (addr & XSCOM_ADDR_IND_FLAG) {
233 if (addr & XSCOM_ADDR_INF_FORM1)
235 return get_indirect_scom_form0(scom, value, addr, status);
237 return __get_scom(scom, value, addr, status);
240 static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
244 if (status & SCOM_STATUS_PROTECTION)
246 if (status & SCOM_STATUS_PARITY) {
247 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
251 /* Return -EBUSY on PIB abort to force a retry */
252 if (status & SCOM_STATUS_PIB_ABORT)
254 if (status & SCOM_STATUS_ERR_SUMMARY) {
255 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
262 static int handle_pib_status(struct scom_device *scom, uint8_t status)
266 if (status == SCOM_PIB_SUCCESS)
268 if (status == SCOM_PIB_BLOCKED)
271 /* Reset the bridge */
272 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
276 case SCOM_PIB_OFFLINE:
278 case SCOM_PIB_BAD_ADDR:
280 case SCOM_PIB_TIMEOUT:
282 case SCOM_PIB_PARTIAL:
283 case SCOM_PIB_CLK_ERR:
284 case SCOM_PIB_PARITY_ERR:
290 static int put_scom(struct scom_device *scom, uint64_t value,
293 uint32_t status, dummy = -1;
296 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
297 rc = raw_put_scom(scom, value, addr, &status);
299 /* Try resetting the bridge if FSI fails */
300 if (rc != -ENODEV && retries == 0) {
301 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
302 &dummy, sizeof(uint32_t));
307 rc = handle_fsi2pib_status(scom, status);
308 if (rc && rc != -EBUSY)
311 rc = handle_pib_status(scom,
312 (status & SCOM_STATUS_PIB_RESP_MASK)
313 >> SCOM_STATUS_PIB_RESP_SHIFT);
314 if (rc && rc != -EBUSY)
324 static int get_scom(struct scom_device *scom, uint64_t *value,
327 uint32_t status, dummy = -1;
330 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
331 rc = raw_get_scom(scom, value, addr, &status);
333 /* Try resetting the bridge if FSI fails */
334 if (rc != -ENODEV && retries == 0) {
335 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
336 &dummy, sizeof(uint32_t));
341 rc = handle_fsi2pib_status(scom, status);
342 if (rc && rc != -EBUSY)
345 rc = handle_pib_status(scom,
346 (status & SCOM_STATUS_PIB_RESP_MASK)
347 >> SCOM_STATUS_PIB_RESP_SHIFT);
348 if (rc && rc != -EBUSY)
358 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
361 struct scom_device *scom = filep->private_data;
362 struct device *dev = &scom->fsi_dev->dev;
366 if (len != sizeof(uint64_t))
369 mutex_lock(&scom->lock);
373 rc = get_scom(scom, &val, *offset);
374 mutex_unlock(&scom->lock);
376 dev_dbg(dev, "get_scom fail:%d\n", rc);
380 rc = copy_to_user(buf, &val, len);
382 dev_dbg(dev, "copy to user failed:%d\n", rc);
384 return rc ? rc : len;
387 static ssize_t scom_write(struct file *filep, const char __user *buf,
388 size_t len, loff_t *offset)
391 struct scom_device *scom = filep->private_data;
392 struct device *dev = &scom->fsi_dev->dev;
395 if (len != sizeof(uint64_t))
398 rc = copy_from_user(&val, buf, len);
400 dev_dbg(dev, "copy from user failed:%d\n", rc);
404 mutex_lock(&scom->lock);
408 rc = put_scom(scom, val, *offset);
409 mutex_unlock(&scom->lock);
411 dev_dbg(dev, "put_scom failed with:%d\n", rc);
418 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
424 file->f_pos = offset;
433 static void raw_convert_status(struct scom_access *acc, uint32_t status)
435 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
436 SCOM_STATUS_PIB_RESP_SHIFT;
437 acc->intf_errors = 0;
439 if (status & SCOM_STATUS_PROTECTION)
440 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
441 else if (status & SCOM_STATUS_PARITY)
442 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
443 else if (status & SCOM_STATUS_PIB_ABORT)
444 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
445 else if (status & SCOM_STATUS_ERR_SUMMARY)
446 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
449 static int scom_raw_read(struct scom_device *scom, void __user *argp)
451 struct scom_access acc;
455 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
458 rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
461 raw_convert_status(&acc, status);
462 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
467 static int scom_raw_write(struct scom_device *scom, void __user *argp)
469 u64 prev_data, mask, data;
470 struct scom_access acc;
474 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
478 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
481 if (status & SCOM_STATUS_ANY_ERR)
485 prev_data = mask = -1ull;
487 data = (prev_data & ~mask) | (acc.data & mask);
488 rc = raw_put_scom(scom, data, acc.addr, &status);
492 raw_convert_status(&acc, status);
493 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
498 static int scom_reset(struct scom_device *scom, void __user *argp)
500 uint32_t flags, dummy = -1;
503 if (get_user(flags, (__u32 __user *)argp))
505 if (flags & SCOM_RESET_PIB)
506 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
508 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
509 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
514 static int scom_check(struct scom_device *scom, void __user *argp)
516 /* Still need to find out how to get "protected" */
517 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
520 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
522 struct scom_device *scom = file->private_data;
523 void __user *argp = (void __user *)arg;
526 mutex_lock(&scom->lock);
528 mutex_unlock(&scom->lock);
533 rc = scom_check(scom, argp);
536 rc = scom_raw_read(scom, argp);
539 rc = scom_raw_write(scom, argp);
542 rc = scom_reset(scom, argp);
545 mutex_unlock(&scom->lock);
549 static int scom_open(struct inode *inode, struct file *file)
551 struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
553 file->private_data = scom;
558 static const struct file_operations scom_fops = {
559 .owner = THIS_MODULE,
561 .llseek = scom_llseek,
564 .unlocked_ioctl = scom_ioctl,
567 static void scom_free(struct device *dev)
569 struct scom_device *scom = container_of(dev, struct scom_device, dev);
571 put_device(&scom->fsi_dev->dev);
575 static int scom_probe(struct device *dev)
577 struct fsi_device *fsi_dev = to_fsi_dev(dev);
578 struct scom_device *scom;
581 scom = kzalloc(sizeof(*scom), GFP_KERNEL);
584 dev_set_drvdata(dev, scom);
585 mutex_init(&scom->lock);
587 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
588 if (!get_device(dev)) {
592 scom->fsi_dev = fsi_dev;
594 /* Create chardev for userspace access */
595 scom->dev.type = &fsi_cdev_type;
596 scom->dev.parent = dev;
597 scom->dev.release = scom_free;
598 device_initialize(&scom->dev);
600 /* Allocate a minor in the FSI space */
601 rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
605 dev_set_name(&scom->dev, "scom%d", didx);
606 cdev_init(&scom->cdev, &scom_fops);
607 rc = cdev_device_add(&scom->cdev, &scom->dev);
609 dev_err(dev, "Error %d creating char device %s\n",
610 rc, dev_name(&scom->dev));
616 fsi_free_minor(scom->dev.devt);
618 put_device(&scom->dev);
622 static int scom_remove(struct device *dev)
624 struct scom_device *scom = dev_get_drvdata(dev);
626 mutex_lock(&scom->lock);
628 mutex_unlock(&scom->lock);
629 cdev_device_del(&scom->cdev, &scom->dev);
630 fsi_free_minor(scom->dev.devt);
631 put_device(&scom->dev);
636 static struct fsi_device_id scom_ids[] = {
638 .engine_type = FSI_ENGID_SCOM,
639 .version = FSI_VERSION_ANY,
644 static struct fsi_driver scom_drv = {
645 .id_table = scom_ids,
648 .bus = &fsi_bus_type,
650 .remove = scom_remove,
654 static int scom_init(void)
656 return fsi_driver_register(&scom_drv);
659 static void scom_exit(void)
661 fsi_driver_unregister(&scom_drv);
664 module_init(scom_init);
665 module_exit(scom_exit);
666 MODULE_LICENSE("GPL");