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;
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 rc = __get_scom(scom, &ind_data, addr, status);
160 if (rc || (*status & SCOM_STATUS_ANY_ERR))
163 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
164 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
169 static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
170 uint64_t addr, uint32_t *status)
172 uint64_t ind_data, ind_addr;
174 if (value & ~XSCOM_DATA_IND_FORM1_DATA)
177 ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
178 ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
179 return __put_scom(scom, ind_data, ind_addr, status);
182 static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
183 uint64_t addr, uint32_t *status)
185 uint64_t ind_data, ind_addr;
188 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
189 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
190 rc = __put_scom(scom, ind_data, ind_addr, status);
191 if (rc || (*status & SCOM_STATUS_ANY_ERR))
194 rc = __get_scom(scom, &ind_data, addr, status);
195 if (rc || (*status & SCOM_STATUS_ANY_ERR))
198 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
199 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
200 *value = ind_data & XSCOM_DATA_IND_DATA;
205 static int raw_put_scom(struct scom_device *scom, uint64_t value,
206 uint64_t addr, uint32_t *status)
208 if (addr & XSCOM_ADDR_IND_FLAG) {
209 if (addr & XSCOM_ADDR_INF_FORM1)
210 return put_indirect_scom_form1(scom, value, addr, status);
212 return put_indirect_scom_form0(scom, value, addr, status);
214 return __put_scom(scom, value, addr, status);
217 static int raw_get_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)
223 return get_indirect_scom_form0(scom, value, addr, status);
225 return __get_scom(scom, value, addr, status);
228 static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
232 if (status & SCOM_STATUS_FSI2PIB_ERROR)
233 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
236 if (status & SCOM_STATUS_PROTECTION)
238 if (status & SCOM_STATUS_PARITY)
241 if (status & SCOM_STATUS_PIB_ABORT)
246 static int handle_pib_status(struct scom_device *scom, uint8_t status)
250 if (status == SCOM_PIB_SUCCESS)
252 if (status == SCOM_PIB_BLOCKED)
255 /* Reset the bridge */
256 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
260 case SCOM_PIB_OFFLINE:
262 case SCOM_PIB_BAD_ADDR:
264 case SCOM_PIB_TIMEOUT:
266 case SCOM_PIB_PARTIAL:
267 case SCOM_PIB_CLK_ERR:
268 case SCOM_PIB_PARITY_ERR:
274 static int put_scom(struct scom_device *scom, uint64_t value,
280 rc = raw_put_scom(scom, value, addr, &status);
284 rc = handle_fsi2pib_status(scom, status);
288 return handle_pib_status(scom,
289 (status & SCOM_STATUS_PIB_RESP_MASK)
290 >> SCOM_STATUS_PIB_RESP_SHIFT);
293 static int get_scom(struct scom_device *scom, uint64_t *value,
299 rc = raw_get_scom(scom, value, addr, &status);
303 rc = handle_fsi2pib_status(scom, status);
307 return handle_pib_status(scom,
308 (status & SCOM_STATUS_PIB_RESP_MASK)
309 >> SCOM_STATUS_PIB_RESP_SHIFT);
312 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
315 struct scom_device *scom = filep->private_data;
316 struct device *dev = &scom->fsi_dev->dev;
320 if (len != sizeof(uint64_t))
323 mutex_lock(&scom->lock);
327 rc = get_scom(scom, &val, *offset);
328 mutex_unlock(&scom->lock);
330 dev_dbg(dev, "get_scom fail:%d\n", rc);
334 rc = copy_to_user(buf, &val, len);
336 dev_dbg(dev, "copy to user failed:%d\n", rc);
338 return rc ? rc : len;
341 static ssize_t scom_write(struct file *filep, const char __user *buf,
342 size_t len, loff_t *offset)
345 struct scom_device *scom = filep->private_data;
346 struct device *dev = &scom->fsi_dev->dev;
349 if (len != sizeof(uint64_t))
352 rc = copy_from_user(&val, buf, len);
354 dev_dbg(dev, "copy from user failed:%d\n", rc);
358 mutex_lock(&scom->lock);
362 rc = put_scom(scom, val, *offset);
363 mutex_unlock(&scom->lock);
365 dev_dbg(dev, "put_scom failed with:%d\n", rc);
372 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
378 file->f_pos = offset;
387 static void raw_convert_status(struct scom_access *acc, uint32_t status)
389 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
390 SCOM_STATUS_PIB_RESP_SHIFT;
391 acc->intf_errors = 0;
393 if (status & SCOM_STATUS_PROTECTION)
394 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
395 else if (status & SCOM_STATUS_PARITY)
396 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
397 else if (status & SCOM_STATUS_PIB_ABORT)
398 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
399 else if (status & SCOM_STATUS_ERR_SUMMARY)
400 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
403 static int scom_raw_read(struct scom_device *scom, void __user *argp)
405 struct scom_access acc;
409 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
412 rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
415 raw_convert_status(&acc, status);
416 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
421 static int scom_raw_write(struct scom_device *scom, void __user *argp)
423 u64 prev_data, mask, data;
424 struct scom_access acc;
428 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
432 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
435 if (status & SCOM_STATUS_ANY_ERR)
439 prev_data = mask = -1ull;
441 data = (prev_data & ~mask) | (acc.data & mask);
442 rc = raw_put_scom(scom, data, acc.addr, &status);
446 raw_convert_status(&acc, status);
447 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
452 static int scom_reset(struct scom_device *scom, void __user *argp)
454 uint32_t flags, dummy = -1;
457 if (get_user(flags, (__u32 __user *)argp))
459 if (flags & SCOM_RESET_PIB)
460 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
462 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
463 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
468 static int scom_check(struct scom_device *scom, void __user *argp)
470 /* Still need to find out how to get "protected" */
471 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
474 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
476 struct scom_device *scom = file->private_data;
477 void __user *argp = (void __user *)arg;
480 mutex_lock(&scom->lock);
482 mutex_unlock(&scom->lock);
487 rc = scom_check(scom, argp);
490 rc = scom_raw_read(scom, argp);
493 rc = scom_raw_write(scom, argp);
496 rc = scom_reset(scom, argp);
499 mutex_unlock(&scom->lock);
503 static int scom_open(struct inode *inode, struct file *file)
505 struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
507 file->private_data = scom;
512 static const struct file_operations scom_fops = {
513 .owner = THIS_MODULE,
515 .llseek = scom_llseek,
518 .unlocked_ioctl = scom_ioctl,
521 static void scom_free(struct device *dev)
523 struct scom_device *scom = container_of(dev, struct scom_device, dev);
525 put_device(&scom->fsi_dev->dev);
529 static int scom_probe(struct device *dev)
531 struct fsi_device *fsi_dev = to_fsi_dev(dev);
532 struct scom_device *scom;
535 scom = kzalloc(sizeof(*scom), GFP_KERNEL);
538 dev_set_drvdata(dev, scom);
539 mutex_init(&scom->lock);
541 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
542 if (!get_device(dev)) {
546 scom->fsi_dev = fsi_dev;
548 /* Create chardev for userspace access */
549 scom->dev.type = &fsi_cdev_type;
550 scom->dev.parent = dev;
551 scom->dev.release = scom_free;
552 device_initialize(&scom->dev);
554 /* Allocate a minor in the FSI space */
555 rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
559 dev_set_name(&scom->dev, "scom%d", didx);
560 cdev_init(&scom->cdev, &scom_fops);
561 rc = cdev_device_add(&scom->cdev, &scom->dev);
563 dev_err(dev, "Error %d creating char device %s\n",
564 rc, dev_name(&scom->dev));
570 fsi_free_minor(scom->dev.devt);
572 put_device(&scom->dev);
576 static int scom_remove(struct device *dev)
578 struct scom_device *scom = dev_get_drvdata(dev);
580 mutex_lock(&scom->lock);
582 mutex_unlock(&scom->lock);
583 cdev_device_del(&scom->cdev, &scom->dev);
584 fsi_free_minor(scom->dev.devt);
585 put_device(&scom->dev);
590 static const struct fsi_device_id scom_ids[] = {
592 .engine_type = FSI_ENGID_SCOM,
593 .version = FSI_VERSION_ANY,
598 static struct fsi_driver scom_drv = {
599 .id_table = scom_ids,
602 .bus = &fsi_bus_type,
604 .remove = scom_remove,
608 static int scom_init(void)
610 return fsi_driver_register(&scom_drv);
613 static void scom_exit(void)
615 fsi_driver_unregister(&scom_drv);
618 module_init(scom_init);
619 module_exit(scom_exit);
620 MODULE_LICENSE("GPL");