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/cdev.h>
24 #include <linux/list.h>
26 #include <uapi/linux/fsi.h>
28 #define FSI_ENGID_SCOM 0x5
30 /* SCOM engine register set */
31 #define SCOM_DATA0_REG 0x00
32 #define SCOM_DATA1_REG 0x04
33 #define SCOM_CMD_REG 0x08
34 #define SCOM_FSI2PIB_RESET_REG 0x18
35 #define SCOM_STATUS_REG 0x1C /* Read */
36 #define SCOM_PIB_RESET_REG 0x1C /* Write */
38 /* Command register */
39 #define SCOM_WRITE_CMD 0x80000000
40 #define SCOM_READ_CMD 0x00000000
42 /* Status register bits */
43 #define SCOM_STATUS_ERR_SUMMARY 0x80000000
44 #define SCOM_STATUS_PROTECTION 0x01000000
45 #define SCOM_STATUS_PARITY 0x04000000
46 #define SCOM_STATUS_PIB_ABORT 0x00100000
47 #define SCOM_STATUS_PIB_RESP_MASK 0x00007000
48 #define SCOM_STATUS_PIB_RESP_SHIFT 12
50 #define SCOM_STATUS_FSI2PIB_ERROR (SCOM_STATUS_PROTECTION | \
51 SCOM_STATUS_PARITY | \
52 SCOM_STATUS_PIB_ABORT)
53 #define SCOM_STATUS_ANY_ERR (SCOM_STATUS_FSI2PIB_ERROR | \
54 SCOM_STATUS_PIB_RESP_MASK)
55 /* SCOM address encodings */
56 #define XSCOM_ADDR_IND_FLAG BIT_ULL(63)
57 #define XSCOM_ADDR_INF_FORM1 BIT_ULL(60)
59 /* SCOM indirect stuff */
60 #define XSCOM_ADDR_DIRECT_PART 0x7fffffffull
61 #define XSCOM_ADDR_INDIRECT_PART 0x000fffff00000000ull
62 #define XSCOM_DATA_IND_READ BIT_ULL(63)
63 #define XSCOM_DATA_IND_COMPLETE BIT_ULL(31)
64 #define XSCOM_DATA_IND_ERR_MASK 0x70000000ull
65 #define XSCOM_DATA_IND_ERR_SHIFT 28
66 #define XSCOM_DATA_IND_DATA 0x0000ffffull
67 #define XSCOM_DATA_IND_FORM1_DATA 0x000fffffffffffffull
68 #define XSCOM_ADDR_FORM1_LOW 0x000ffffffffull
69 #define XSCOM_ADDR_FORM1_HI 0xfff00000000ull
70 #define XSCOM_ADDR_FORM1_HI_SHIFT 20
73 #define SCOM_MAX_RETRIES 100 /* Retries on busy */
74 #define SCOM_MAX_IND_RETRIES 10 /* Retries indirect not ready */
77 struct list_head link;
78 struct fsi_device *fsi_dev;
85 static int __put_scom(struct scom_device *scom_dev, uint64_t value,
86 uint32_t addr, uint32_t *status)
88 __be32 data, raw_status;
91 data = cpu_to_be32((value >> 32) & 0xffffffff);
92 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
97 data = cpu_to_be32(value & 0xffffffff);
98 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
103 data = cpu_to_be32(SCOM_WRITE_CMD | addr);
104 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
108 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
112 *status = be32_to_cpu(raw_status);
117 static int __get_scom(struct scom_device *scom_dev, uint64_t *value,
118 uint32_t addr, uint32_t *status)
120 __be32 data, raw_status;
125 data = cpu_to_be32(SCOM_READ_CMD | addr);
126 rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
130 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
136 * Read the data registers even on error, so we don't have
137 * to interpret the status register here.
139 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
143 *value |= (uint64_t)be32_to_cpu(data) << 32;
144 rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
148 *value |= be32_to_cpu(data);
149 *status = be32_to_cpu(raw_status);
154 static int put_indirect_scom_form0(struct scom_device *scom, uint64_t value,
155 uint64_t addr, uint32_t *status)
157 uint64_t ind_data, ind_addr;
158 int rc, retries, err = 0;
160 if (value & ~XSCOM_DATA_IND_DATA)
163 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
164 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | value;
165 rc = __put_scom(scom, ind_data, ind_addr, status);
166 if (rc || (*status & SCOM_STATUS_ANY_ERR))
169 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
170 rc = __get_scom(scom, &ind_data, addr, status);
171 if (rc || (*status & SCOM_STATUS_ANY_ERR))
174 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
175 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
176 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
184 static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
185 uint64_t addr, uint32_t *status)
187 uint64_t ind_data, ind_addr;
189 if (value & ~XSCOM_DATA_IND_FORM1_DATA)
192 ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
193 ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
194 return __put_scom(scom, ind_data, ind_addr, status);
197 static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
198 uint64_t addr, uint32_t *status)
200 uint64_t ind_data, ind_addr;
201 int rc, retries, err = 0;
203 ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
204 ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
205 rc = __put_scom(scom, ind_data, ind_addr, status);
206 if (rc || (*status & SCOM_STATUS_ANY_ERR))
209 for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
210 rc = __get_scom(scom, &ind_data, addr, status);
211 if (rc || (*status & SCOM_STATUS_ANY_ERR))
214 err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
215 *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
216 *value = ind_data & XSCOM_DATA_IND_DATA;
218 if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
226 static int raw_put_scom(struct scom_device *scom, uint64_t value,
227 uint64_t addr, uint32_t *status)
229 if (addr & XSCOM_ADDR_IND_FLAG) {
230 if (addr & XSCOM_ADDR_INF_FORM1)
231 return put_indirect_scom_form1(scom, value, addr, status);
233 return put_indirect_scom_form0(scom, value, addr, status);
235 return __put_scom(scom, value, addr, status);
238 static int raw_get_scom(struct scom_device *scom, uint64_t *value,
239 uint64_t addr, uint32_t *status)
241 if (addr & XSCOM_ADDR_IND_FLAG) {
242 if (addr & XSCOM_ADDR_INF_FORM1)
244 return get_indirect_scom_form0(scom, value, addr, status);
246 return __get_scom(scom, value, addr, status);
249 static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
253 if (status & SCOM_STATUS_FSI2PIB_ERROR)
254 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
257 if (status & SCOM_STATUS_PROTECTION)
259 if (status & SCOM_STATUS_PARITY)
261 /* Return -EBUSY on PIB abort to force a retry */
262 if (status & SCOM_STATUS_PIB_ABORT)
267 static int handle_pib_status(struct scom_device *scom, uint8_t status)
271 if (status == SCOM_PIB_SUCCESS)
273 if (status == SCOM_PIB_BLOCKED)
276 /* Reset the bridge */
277 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
281 case SCOM_PIB_OFFLINE:
283 case SCOM_PIB_BAD_ADDR:
285 case SCOM_PIB_TIMEOUT:
287 case SCOM_PIB_PARTIAL:
288 case SCOM_PIB_CLK_ERR:
289 case SCOM_PIB_PARITY_ERR:
295 static int put_scom(struct scom_device *scom, uint64_t value,
298 uint32_t status, dummy = -1;
301 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
302 rc = raw_put_scom(scom, value, addr, &status);
304 /* Try resetting the bridge if FSI fails */
305 if (rc != -ENODEV && retries == 0) {
306 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
307 &dummy, sizeof(uint32_t));
312 rc = handle_fsi2pib_status(scom, status);
313 if (rc && rc != -EBUSY)
316 rc = handle_pib_status(scom,
317 (status & SCOM_STATUS_PIB_RESP_MASK)
318 >> SCOM_STATUS_PIB_RESP_SHIFT);
319 if (rc && rc != -EBUSY)
329 static int get_scom(struct scom_device *scom, uint64_t *value,
332 uint32_t status, dummy = -1;
335 for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
336 rc = raw_get_scom(scom, value, addr, &status);
338 /* Try resetting the bridge if FSI fails */
339 if (rc != -ENODEV && retries == 0) {
340 fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
341 &dummy, sizeof(uint32_t));
346 rc = handle_fsi2pib_status(scom, status);
347 if (rc && rc != -EBUSY)
350 rc = handle_pib_status(scom,
351 (status & SCOM_STATUS_PIB_RESP_MASK)
352 >> SCOM_STATUS_PIB_RESP_SHIFT);
353 if (rc && rc != -EBUSY)
363 static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
366 struct scom_device *scom = filep->private_data;
367 struct device *dev = &scom->fsi_dev->dev;
371 if (len != sizeof(uint64_t))
374 mutex_lock(&scom->lock);
378 rc = get_scom(scom, &val, *offset);
379 mutex_unlock(&scom->lock);
381 dev_dbg(dev, "get_scom fail:%d\n", rc);
385 rc = copy_to_user(buf, &val, len);
387 dev_dbg(dev, "copy to user failed:%d\n", rc);
389 return rc ? rc : len;
392 static ssize_t scom_write(struct file *filep, const char __user *buf,
393 size_t len, loff_t *offset)
396 struct scom_device *scom = filep->private_data;
397 struct device *dev = &scom->fsi_dev->dev;
400 if (len != sizeof(uint64_t))
403 rc = copy_from_user(&val, buf, len);
405 dev_dbg(dev, "copy from user failed:%d\n", rc);
409 mutex_lock(&scom->lock);
413 rc = put_scom(scom, val, *offset);
414 mutex_unlock(&scom->lock);
416 dev_dbg(dev, "put_scom failed with:%d\n", rc);
423 static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
429 file->f_pos = offset;
438 static void raw_convert_status(struct scom_access *acc, uint32_t status)
440 acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
441 SCOM_STATUS_PIB_RESP_SHIFT;
442 acc->intf_errors = 0;
444 if (status & SCOM_STATUS_PROTECTION)
445 acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
446 else if (status & SCOM_STATUS_PARITY)
447 acc->intf_errors |= SCOM_INTF_ERR_PARITY;
448 else if (status & SCOM_STATUS_PIB_ABORT)
449 acc->intf_errors |= SCOM_INTF_ERR_ABORT;
450 else if (status & SCOM_STATUS_ERR_SUMMARY)
451 acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
454 static int scom_raw_read(struct scom_device *scom, void __user *argp)
456 struct scom_access acc;
460 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
463 rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
466 raw_convert_status(&acc, status);
467 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
472 static int scom_raw_write(struct scom_device *scom, void __user *argp)
474 u64 prev_data, mask, data;
475 struct scom_access acc;
479 if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
483 rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
486 if (status & SCOM_STATUS_ANY_ERR)
490 prev_data = mask = -1ull;
492 data = (prev_data & ~mask) | (acc.data & mask);
493 rc = raw_put_scom(scom, data, acc.addr, &status);
497 raw_convert_status(&acc, status);
498 if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
503 static int scom_reset(struct scom_device *scom, void __user *argp)
505 uint32_t flags, dummy = -1;
508 if (get_user(flags, (__u32 __user *)argp))
510 if (flags & SCOM_RESET_PIB)
511 rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
513 if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
514 rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
519 static int scom_check(struct scom_device *scom, void __user *argp)
521 /* Still need to find out how to get "protected" */
522 return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
525 static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
527 struct scom_device *scom = file->private_data;
528 void __user *argp = (void __user *)arg;
531 mutex_lock(&scom->lock);
533 mutex_unlock(&scom->lock);
538 rc = scom_check(scom, argp);
541 rc = scom_raw_read(scom, argp);
544 rc = scom_raw_write(scom, argp);
547 rc = scom_reset(scom, argp);
550 mutex_unlock(&scom->lock);
554 static int scom_open(struct inode *inode, struct file *file)
556 struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
558 file->private_data = scom;
563 static const struct file_operations scom_fops = {
564 .owner = THIS_MODULE,
566 .llseek = scom_llseek,
569 .unlocked_ioctl = scom_ioctl,
572 static void scom_free(struct device *dev)
574 struct scom_device *scom = container_of(dev, struct scom_device, dev);
576 put_device(&scom->fsi_dev->dev);
580 static int scom_probe(struct device *dev)
582 struct fsi_device *fsi_dev = to_fsi_dev(dev);
583 struct scom_device *scom;
586 scom = kzalloc(sizeof(*scom), GFP_KERNEL);
589 dev_set_drvdata(dev, scom);
590 mutex_init(&scom->lock);
592 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
593 if (!get_device(dev)) {
597 scom->fsi_dev = fsi_dev;
599 /* Create chardev for userspace access */
600 scom->dev.type = &fsi_cdev_type;
601 scom->dev.parent = dev;
602 scom->dev.release = scom_free;
603 device_initialize(&scom->dev);
605 /* Allocate a minor in the FSI space */
606 rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
610 dev_set_name(&scom->dev, "scom%d", didx);
611 cdev_init(&scom->cdev, &scom_fops);
612 rc = cdev_device_add(&scom->cdev, &scom->dev);
614 dev_err(dev, "Error %d creating char device %s\n",
615 rc, dev_name(&scom->dev));
621 fsi_free_minor(scom->dev.devt);
623 put_device(&scom->dev);
627 static int scom_remove(struct device *dev)
629 struct scom_device *scom = dev_get_drvdata(dev);
631 mutex_lock(&scom->lock);
633 mutex_unlock(&scom->lock);
634 cdev_device_del(&scom->cdev, &scom->dev);
635 fsi_free_minor(scom->dev.devt);
636 put_device(&scom->dev);
641 static struct fsi_device_id scom_ids[] = {
643 .engine_type = FSI_ENGID_SCOM,
644 .version = FSI_VERSION_ANY,
649 static struct fsi_driver scom_drv = {
650 .id_table = scom_ids,
653 .bus = &fsi_bus_type,
655 .remove = scom_remove,
659 static int scom_init(void)
661 return fsi_driver_register(&scom_drv);
664 static void scom_exit(void)
666 fsi_driver_unregister(&scom_drv);
669 module_init(scom_init);
670 module_exit(scom_exit);
671 MODULE_LICENSE("GPL");