2 * SCOM FSI Client device driver
4 * Copyright (C) IBM Corporation 2016
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERGCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/fsi.h>
17 #include <linux/module.h>
18 #include <linux/cdev.h>
19 #include <linux/delay.h>
21 #include <linux/uaccess.h>
22 #include <linux/slab.h>
23 #include <linux/miscdevice.h>
24 #include <linux/list.h>
25 #include <linux/idr.h>
27 #include <uapi/linux/fsi.h>
29 #define FSI_ENGID_SCOM 0x5
31 /* SCOM engine register set */
32 #define SCOM_DATA0_REG 0x00
33 #define SCOM_DATA1_REG 0x04
34 #define SCOM_CMD_REG 0x08
35 #define SCOM_FSI2PIB_RESET_REG 0x18
36 #define SCOM_STATUS_REG 0x1C /* Read */
37 #define SCOM_PIB_RESET_REG 0x1C /* Write */
39 /* Command register */
40 #define SCOM_WRITE_CMD 0x80000000
41 #define SCOM_READ_CMD 0x00000000
43 /* Status register bits */
44 #define SCOM_STATUS_ERR_SUMMARY 0x80000000
45 #define SCOM_STATUS_PROTECTION 0x01000000
46 #define SCOM_STATUS_PARITY 0x04000000
47 #define SCOM_STATUS_PIB_ABORT 0x00100000
48 #define SCOM_STATUS_PIB_RESP_MASK 0x00007000
49 #define SCOM_STATUS_PIB_RESP_SHIFT 12
51 #define SCOM_STATUS_ANY_ERR (SCOM_STATUS_ERR_SUMMARY | \
52 SCOM_STATUS_PROTECTION | \
53 SCOM_STATUS_PARITY | \
54 SCOM_STATUS_PIB_ABORT | \
55 SCOM_STATUS_PIB_RESP_MASK)
56 /* SCOM address encodings */
57 #define XSCOM_ADDR_IND_FLAG BIT_ULL(63)
58 #define XSCOM_ADDR_INF_FORM1 BIT_ULL(60)
60 /* SCOM indirect stuff */
61 #define XSCOM_ADDR_DIRECT_PART 0x7fffffffull
62 #define XSCOM_ADDR_INDIRECT_PART 0x000fffff00000000ull
63 #define XSCOM_DATA_IND_READ BIT_ULL(63)
64 #define XSCOM_DATA_IND_COMPLETE BIT_ULL(31)
65 #define XSCOM_DATA_IND_ERR_MASK 0x70000000ull
66 #define XSCOM_DATA_IND_ERR_SHIFT 28
67 #define XSCOM_DATA_IND_DATA 0x0000ffffull
68 #define XSCOM_DATA_IND_FORM1_DATA 0x000fffffffffffffull
69 #define XSCOM_ADDR_FORM1_LOW 0x000ffffffffull
70 #define XSCOM_ADDR_FORM1_HI 0xfff00000000ull
71 #define XSCOM_ADDR_FORM1_HI_SHIFT 20
74 #define SCOM_MAX_RETRIES 100 /* Retries on busy */
75 #define SCOM_MAX_IND_RETRIES 10 /* Retries indirect not ready */
78 struct list_head link;
79 struct fsi_device *fsi_dev;
80 struct miscdevice mdev;
86 #define to_scom_dev(x) container_of((x), struct scom_device, mdev)
88 static struct list_head scom_devices;
90 static DEFINE_IDA(scom_ida);
92 static int __put_scom(struct scom_device *scom_dev, uint64_t value,
93 uint32_t addr, uint32_t *status)
95 __be32 data, raw_status;
98 data = cpu_to_be32((value >> 32) & 0xffffffff);
99 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
104 data = cpu_to_be32(value & 0xffffffff);
105 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
110 data = cpu_to_be32(SCOM_WRITE_CMD | addr);
111 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
115 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
119 *status = be32_to_cpu(raw_status);
124 static int __get_scom(struct scom_device *scom_dev, uint64_t *value,
125 uint32_t addr, uint32_t *status)
127 __be32 data, raw_status;
132 data = cpu_to_be32(SCOM_READ_CMD | addr);
133 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
137 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
143 * Read the data registers even on error, so we don't have
144 * to interpret the status register here.
146 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
150 *value |= (uint64_t)be32_to_cpu(data) << 32;
151 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
155 *value |= be32_to_cpu(data);
156 *status = be32_to_cpu(raw_status);
161 static int put_indirect_scom_form0(struct scom_device *scom, uint64_t value,
162 uint64_t addr, uint32_t *status)
164 uint64_t ind_data, ind_addr;
165 int rc, retries, err = 0;
167 if (value & ~XSCOM_DATA_IND_DATA)
170 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
171 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | value;
172 rc = __put_scom(scom, ind_data, ind_addr, status);
173 if (rc || (*status & SCOM_STATUS_ANY_ERR))
176 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
177 rc = __get_scom(scom, &ind_data, addr, status);
178 if (rc || (*status & SCOM_STATUS_ANY_ERR))
181 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
182 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
183 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
191 static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
192 uint64_t addr, uint32_t *status)
194 uint64_t ind_data, ind_addr;
196 if (value & ~XSCOM_DATA_IND_FORM1_DATA)
199 ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
200 ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
201 return __put_scom(scom, ind_data, ind_addr, status);
204 static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
205 uint64_t addr, uint32_t *status)
207 uint64_t ind_data, ind_addr;
208 int rc, retries, err = 0;
210 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
211 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
212 rc = __put_scom(scom, ind_data, ind_addr, status);
213 if (rc || (*status & SCOM_STATUS_ANY_ERR))
216 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
217 rc = __get_scom(scom, &ind_data, addr, status);
218 if (rc || (*status & SCOM_STATUS_ANY_ERR))
221 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
222 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
223 *value = ind_data & XSCOM_DATA_IND_DATA;
225 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
233 static int raw_put_scom(struct scom_device *scom, uint64_t value,
234 uint64_t addr, uint32_t *status)
236 if (addr & XSCOM_ADDR_IND_FLAG) {
237 if (addr & XSCOM_ADDR_INF_FORM1)
238 return put_indirect_scom_form1(scom, value, addr, status);
240 return put_indirect_scom_form0(scom, value, addr, status);
242 return __put_scom(scom, value, addr, status);
245 static int raw_get_scom(struct scom_device *scom, uint64_t *value,
246 uint64_t addr, uint32_t *status)
248 if (addr & XSCOM_ADDR_IND_FLAG) {
249 if (addr & XSCOM_ADDR_INF_FORM1)
251 return get_indirect_scom_form0(scom, value, addr, status);
253 return __get_scom(scom, value, addr, status);
256 static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
260 if (status & SCOM_STATUS_PROTECTION)
262 if (status & SCOM_STATUS_PARITY) {
263 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
267 /* Return -EBUSY on PIB abort to force a retry */
268 if (status & SCOM_STATUS_PIB_ABORT)
270 if (status & SCOM_STATUS_ERR_SUMMARY) {
271 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
278 static int handle_pib_status(struct scom_device *scom, uint8_t status)
282 if (status == SCOM_PIB_SUCCESS)
284 if (status == SCOM_PIB_BLOCKED)
287 /* Reset the bridge */
288 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
292 case SCOM_PIB_OFFLINE:
294 case SCOM_PIB_BAD_ADDR:
296 case SCOM_PIB_TIMEOUT:
298 case SCOM_PIB_PARTIAL:
299 case SCOM_PIB_CLK_ERR:
300 case SCOM_PIB_PARITY_ERR:
306 static int put_scom(struct scom_device *scom, uint64_t value,
309 uint32_t status, dummy = -1;
312 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
313 rc = raw_put_scom(scom, value, addr, &status);
315 /* Try resetting the bridge if FSI fails */
316 if (rc != -ENODEV && retries == 0) {
317 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
318 &dummy, sizeof(uint32_t));
323 rc = handle_fsi2pib_status(scom, status);
324 if (rc && rc != -EBUSY)
327 rc = handle_pib_status(scom,
328 (status & SCOM_STATUS_PIB_RESP_MASK)
329 >> SCOM_STATUS_PIB_RESP_SHIFT);
330 if (rc && rc != -EBUSY)
340 static int get_scom(struct scom_device *scom, uint64_t *value,
343 uint32_t status, dummy = -1;
346 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
347 rc = raw_get_scom(scom, value, addr, &status);
349 /* Try resetting the bridge if FSI fails */
350 if (rc != -ENODEV && retries == 0) {
351 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
352 &dummy, sizeof(uint32_t));
357 rc = handle_fsi2pib_status(scom, status);
358 if (rc && rc != -EBUSY)
361 rc = handle_pib_status(scom,
362 (status & SCOM_STATUS_PIB_RESP_MASK)
363 >> SCOM_STATUS_PIB_RESP_SHIFT);
364 if (rc && rc != -EBUSY)
374 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
377 struct miscdevice *mdev =
378 (struct miscdevice *)filep->private_data;
379 struct scom_device *scom = to_scom_dev(mdev);
380 struct device *dev = &scom->fsi_dev->dev;
384 if (len != sizeof(uint64_t))
387 mutex_lock(&scom->lock);
388 rc = get_scom(scom, &val, *offset);
389 mutex_unlock(&scom->lock);
391 dev_dbg(dev, "get_scom fail:%d\n", rc);
395 rc = copy_to_user(buf, &val, len);
397 dev_dbg(dev, "copy to user failed:%d\n", rc);
399 return rc ? rc : len;
402 static ssize_t scom_write(struct file *filep, const char __user *buf,
403 size_t len, loff_t *offset)
406 struct miscdevice *mdev = filep->private_data;
407 struct scom_device *scom = to_scom_dev(mdev);
408 struct device *dev = &scom->fsi_dev->dev;
411 if (len != sizeof(uint64_t))
414 rc = copy_from_user(&val, buf, len);
416 dev_dbg(dev, "copy from user failed:%d\n", rc);
420 mutex_lock(&scom->lock);
421 rc = put_scom(scom, val, *offset);
422 mutex_unlock(&scom->lock);
424 dev_dbg(dev, "put_scom failed with:%d\n", rc);
431 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
437 file->f_pos = offset;
446 static void raw_convert_status(struct scom_access *acc, uint32_t status)
448 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
449 SCOM_STATUS_PIB_RESP_SHIFT;
450 acc->intf_errors = 0;
452 if (status & SCOM_STATUS_PROTECTION)
453 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
454 else if (status & SCOM_STATUS_PARITY)
455 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
456 else if (status & SCOM_STATUS_PIB_ABORT)
457 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
458 else if (status & SCOM_STATUS_ERR_SUMMARY)
459 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
462 static int scom_raw_read(struct scom_device *scom, void __user *argp)
464 struct scom_access acc;
468 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
471 rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
474 raw_convert_status(&acc, status);
475 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
480 static int scom_raw_write(struct scom_device *scom, void __user *argp)
482 u64 prev_data, mask, data;
483 struct scom_access acc;
487 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
491 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
494 if (status & SCOM_STATUS_ANY_ERR)
498 prev_data = mask = -1ull;
500 data = (prev_data & ~mask) | (acc.data & mask);
501 rc = raw_put_scom(scom, data, acc.addr, &status);
505 raw_convert_status(&acc, status);
506 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
511 static int scom_reset(struct scom_device *scom, void __user *argp)
513 uint32_t flags, dummy = -1;
516 if (get_user(flags, (__u32 __user *)argp))
518 if (flags & SCOM_RESET_PIB)
519 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
521 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
522 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
527 static int scom_check(struct scom_device *scom, void __user *argp)
529 /* Still need to find out how to get "protected" */
530 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
533 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
535 struct miscdevice *mdev = file->private_data;
536 struct scom_device *scom = to_scom_dev(mdev);
537 void __user *argp = (void __user *)arg;
540 mutex_lock(&scom->lock);
543 rc = scom_check(scom, argp);
546 rc = scom_raw_read(scom, argp);
549 rc = scom_raw_write(scom, argp);
552 rc = scom_reset(scom, argp);
555 mutex_unlock(&scom->lock);
559 static const struct file_operations scom_fops = {
560 .owner = THIS_MODULE,
561 .llseek = scom_llseek,
564 .unlocked_ioctl = scom_ioctl,
567 static int scom_probe(struct device *dev)
569 struct fsi_device *fsi_dev = to_fsi_dev(dev);
570 struct scom_device *scom;
572 scom = devm_kzalloc(dev, sizeof(*scom), GFP_KERNEL);
576 mutex_init(&scom->lock);
577 scom->idx = ida_simple_get(&scom_ida, 1, INT_MAX, GFP_KERNEL);
578 snprintf(scom->name, sizeof(scom->name), "scom%d", scom->idx);
579 scom->fsi_dev = fsi_dev;
580 scom->mdev.minor = MISC_DYNAMIC_MINOR;
581 scom->mdev.fops = &scom_fops;
582 scom->mdev.name = scom->name;
583 scom->mdev.parent = dev;
584 list_add(&scom->link, &scom_devices);
586 return misc_register(&scom->mdev);
589 static int scom_remove(struct device *dev)
591 struct scom_device *scom, *scom_tmp;
592 struct fsi_device *fsi_dev = to_fsi_dev(dev);
594 list_for_each_entry_safe(scom, scom_tmp, &scom_devices, link) {
595 if (scom->fsi_dev == fsi_dev) {
596 list_del(&scom->link);
597 ida_simple_remove(&scom_ida, scom->idx);
598 misc_deregister(&scom->mdev);
605 static struct fsi_device_id scom_ids[] = {
607 .engine_type = FSI_ENGID_SCOM,
608 .version = FSI_VERSION_ANY,
613 static struct fsi_driver scom_drv = {
614 .id_table = scom_ids,
617 .bus = &fsi_bus_type,
619 .remove = scom_remove,
623 static int scom_init(void)
625 INIT_LIST_HEAD(&scom_devices);
626 return fsi_driver_register(&scom_drv);
629 static void scom_exit(void)
631 struct list_head *pos;
632 struct scom_device *scom;
634 list_for_each(pos, &scom_devices) {
635 scom = list_entry(pos, struct scom_device, link);
636 misc_deregister(&scom->mdev);
637 devm_kfree(&scom->fsi_dev->dev, scom);
639 fsi_driver_unregister(&scom_drv);
642 module_init(scom_init);
643 module_exit(scom_exit);
644 MODULE_LICENSE("GPL");