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_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_FSI2PIB_ERROR)
245 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
248 if (status & SCOM_STATUS_PROTECTION)
250 if (status & SCOM_STATUS_PARITY)
252 /* Return -EBUSY on PIB abort to force a retry */
253 if (status & SCOM_STATUS_PIB_ABORT)
258 static int handle_pib_status(struct scom_device *scom, uint8_t status)
262 if (status == SCOM_PIB_SUCCESS)
264 if (status == SCOM_PIB_BLOCKED)
267 /* Reset the bridge */
268 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
272 case SCOM_PIB_OFFLINE:
274 case SCOM_PIB_BAD_ADDR:
276 case SCOM_PIB_TIMEOUT:
278 case SCOM_PIB_PARTIAL:
279 case SCOM_PIB_CLK_ERR:
280 case SCOM_PIB_PARITY_ERR:
286 static int put_scom(struct scom_device *scom, uint64_t value,
289 uint32_t status, dummy = -1;
292 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
293 rc = raw_put_scom(scom, value, addr, &status);
295 /* Try resetting the bridge if FSI fails */
296 if (rc != -ENODEV && retries == 0) {
297 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
298 &dummy, sizeof(uint32_t));
303 rc = handle_fsi2pib_status(scom, status);
304 if (rc && rc != -EBUSY)
307 rc = handle_pib_status(scom,
308 (status & SCOM_STATUS_PIB_RESP_MASK)
309 >> SCOM_STATUS_PIB_RESP_SHIFT);
310 if (rc && rc != -EBUSY)
320 static int get_scom(struct scom_device *scom, uint64_t *value,
323 uint32_t status, dummy = -1;
326 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
327 rc = raw_get_scom(scom, value, addr, &status);
329 /* Try resetting the bridge if FSI fails */
330 if (rc != -ENODEV && retries == 0) {
331 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
332 &dummy, sizeof(uint32_t));
337 rc = handle_fsi2pib_status(scom, status);
338 if (rc && rc != -EBUSY)
341 rc = handle_pib_status(scom,
342 (status & SCOM_STATUS_PIB_RESP_MASK)
343 >> SCOM_STATUS_PIB_RESP_SHIFT);
344 if (rc && rc != -EBUSY)
354 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
357 struct scom_device *scom = filep->private_data;
358 struct device *dev = &scom->fsi_dev->dev;
362 if (len != sizeof(uint64_t))
365 mutex_lock(&scom->lock);
369 rc = get_scom(scom, &val, *offset);
370 mutex_unlock(&scom->lock);
372 dev_dbg(dev, "get_scom fail:%d\n", rc);
376 rc = copy_to_user(buf, &val, len);
378 dev_dbg(dev, "copy to user failed:%d\n", rc);
380 return rc ? rc : len;
383 static ssize_t scom_write(struct file *filep, const char __user *buf,
384 size_t len, loff_t *offset)
387 struct scom_device *scom = filep->private_data;
388 struct device *dev = &scom->fsi_dev->dev;
391 if (len != sizeof(uint64_t))
394 rc = copy_from_user(&val, buf, len);
396 dev_dbg(dev, "copy from user failed:%d\n", rc);
400 mutex_lock(&scom->lock);
404 rc = put_scom(scom, val, *offset);
405 mutex_unlock(&scom->lock);
407 dev_dbg(dev, "put_scom failed with:%d\n", rc);
414 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
420 file->f_pos = offset;
429 static void raw_convert_status(struct scom_access *acc, uint32_t status)
431 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
432 SCOM_STATUS_PIB_RESP_SHIFT;
433 acc->intf_errors = 0;
435 if (status & SCOM_STATUS_PROTECTION)
436 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
437 else if (status & SCOM_STATUS_PARITY)
438 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
439 else if (status & SCOM_STATUS_PIB_ABORT)
440 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
441 else if (status & SCOM_STATUS_ERR_SUMMARY)
442 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
445 static int scom_raw_read(struct scom_device *scom, void __user *argp)
447 struct scom_access acc;
451 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
454 rc = raw_get_scom(scom, &acc.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_raw_write(struct scom_device *scom, void __user *argp)
465 u64 prev_data, mask, data;
466 struct scom_access acc;
470 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
474 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
477 if (status & SCOM_STATUS_ANY_ERR)
481 prev_data = mask = -1ull;
483 data = (prev_data & ~mask) | (acc.data & mask);
484 rc = raw_put_scom(scom, data, acc.addr, &status);
488 raw_convert_status(&acc, status);
489 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
494 static int scom_reset(struct scom_device *scom, void __user *argp)
496 uint32_t flags, dummy = -1;
499 if (get_user(flags, (__u32 __user *)argp))
501 if (flags & SCOM_RESET_PIB)
502 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
504 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
505 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
510 static int scom_check(struct scom_device *scom, void __user *argp)
512 /* Still need to find out how to get "protected" */
513 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
516 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
518 struct scom_device *scom = file->private_data;
519 void __user *argp = (void __user *)arg;
522 mutex_lock(&scom->lock);
524 mutex_unlock(&scom->lock);
529 rc = scom_check(scom, argp);
532 rc = scom_raw_read(scom, argp);
535 rc = scom_raw_write(scom, argp);
538 rc = scom_reset(scom, argp);
541 mutex_unlock(&scom->lock);
545 static int scom_open(struct inode *inode, struct file *file)
547 struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
549 file->private_data = scom;
554 static const struct file_operations scom_fops = {
555 .owner = THIS_MODULE,
557 .llseek = scom_llseek,
560 .unlocked_ioctl = scom_ioctl,
563 static void scom_free(struct device *dev)
565 struct scom_device *scom = container_of(dev, struct scom_device, dev);
567 put_device(&scom->fsi_dev->dev);
571 static int scom_probe(struct device *dev)
573 struct fsi_device *fsi_dev = to_fsi_dev(dev);
574 struct scom_device *scom;
577 scom = kzalloc(sizeof(*scom), GFP_KERNEL);
580 dev_set_drvdata(dev, scom);
581 mutex_init(&scom->lock);
583 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
584 if (!get_device(dev)) {
588 scom->fsi_dev = fsi_dev;
590 /* Create chardev for userspace access */
591 scom->dev.type = &fsi_cdev_type;
592 scom->dev.parent = dev;
593 scom->dev.release = scom_free;
594 device_initialize(&scom->dev);
596 /* Allocate a minor in the FSI space */
597 rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
601 dev_set_name(&scom->dev, "scom%d", didx);
602 cdev_init(&scom->cdev, &scom_fops);
603 rc = cdev_device_add(&scom->cdev, &scom->dev);
605 dev_err(dev, "Error %d creating char device %s\n",
606 rc, dev_name(&scom->dev));
612 fsi_free_minor(scom->dev.devt);
614 put_device(&scom->dev);
618 static int scom_remove(struct device *dev)
620 struct scom_device *scom = dev_get_drvdata(dev);
622 mutex_lock(&scom->lock);
624 mutex_unlock(&scom->lock);
625 cdev_device_del(&scom->cdev, &scom->dev);
626 fsi_free_minor(scom->dev.devt);
627 put_device(&scom->dev);
632 static const struct fsi_device_id scom_ids[] = {
634 .engine_type = FSI_ENGID_SCOM,
635 .version = FSI_VERSION_ANY,
640 static struct fsi_driver scom_drv = {
641 .id_table = scom_ids,
644 .bus = &fsi_bus_type,
646 .remove = scom_remove,
650 static int scom_init(void)
652 return fsi_driver_register(&scom_drv);
655 static void scom_exit(void)
657 fsi_driver_unregister(&scom_drv);
660 module_init(scom_init);
661 module_exit(scom_exit);
662 MODULE_LICENSE("GPL");