1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) IBM Corporation 2016
10 * - s/cfam/chip (cfam_id -> chip_id etc...)
13 #include <linux/crc4.h>
14 #include <linux/device.h>
15 #include <linux/fsi.h>
16 #include <linux/idr.h>
17 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/bitops.h>
21 #include <linux/cdev.h>
23 #include <linux/uaccess.h>
25 #include "fsi-master.h"
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/fsi.h>
30 #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
31 #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
32 #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
33 #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
34 #define FSI_SLAVE_CONF_VERSION_SHIFT 12
35 #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
36 #define FSI_SLAVE_CONF_TYPE_SHIFT 4
37 #define FSI_SLAVE_CONF_CRC_SHIFT 4
38 #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
39 #define FSI_SLAVE_CONF_DATA_BITS 28
41 #define FSI_PEEK_BASE 0x410
43 static const int engine_page_size = 0x400;
45 #define FSI_SLAVE_BASE 0x800
48 * FSI slave engine control register offsets
50 #define FSI_SMODE 0x0 /* R/W: Mode register */
51 #define FSI_SISC 0x8 /* R/W: Interrupt condition */
52 #define FSI_SSTAT 0x14 /* R : Slave status */
53 #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
58 #define FSI_SMODE_WSC 0x80000000 /* Warm start done */
59 #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
60 #define FSI_SMODE_SID_SHIFT 24 /* ID shift */
61 #define FSI_SMODE_SID_MASK 3 /* ID Mask */
62 #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
63 #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
64 #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
65 #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
66 #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
67 #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
72 #define FSI_LLMODE_ASYNC 0x1
74 #define FSI_SLAVE_SIZE_23b 0x800000
76 static DEFINE_IDA(master_ida);
80 struct fsi_master *master;
83 int id; /* FSI address */
84 int link; /* FSI link# */
87 uint32_t size; /* size of slave address space */
92 #define to_fsi_master(d) container_of(d, struct fsi_master, dev)
93 #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
95 static const int slave_retries = 2;
96 static int discard_errors;
98 static dev_t fsi_base_dev;
99 static DEFINE_IDA(fsi_minor_ida);
100 #define FSI_CHAR_MAX_DEVICES 0x1000
102 /* Legacy /dev numbering: 4 devices per chip, 16 chips */
103 #define FSI_CHAR_LEGACY_TOP 64
105 static int fsi_master_read(struct fsi_master *master, int link,
106 uint8_t slave_id, uint32_t addr, void *val, size_t size);
107 static int fsi_master_write(struct fsi_master *master, int link,
108 uint8_t slave_id, uint32_t addr, const void *val, size_t size);
109 static int fsi_master_break(struct fsi_master *master, int link);
112 * fsi_device_read() / fsi_device_write() / fsi_device_peek()
114 * FSI endpoint-device support
116 * Read / write / peek accessors for a client
119 * dev: Structure passed to FSI client device drivers on probe().
120 * addr: FSI address of given device. Client should pass in its base address
121 * plus desired offset to access its register space.
122 * val: For read/peek this is the value read at the specified address. For
123 * write this is value to write to the specified address.
124 * The data in val must be FSI bus endian (big endian).
125 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
126 * Addresses must be aligned on size boundaries or an error will result.
128 int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
131 if (addr > dev->size || size > dev->size || addr > dev->size - size)
134 return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
136 EXPORT_SYMBOL_GPL(fsi_device_read);
138 int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
141 if (addr > dev->size || size > dev->size || addr > dev->size - size)
144 return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
146 EXPORT_SYMBOL_GPL(fsi_device_write);
148 int fsi_device_peek(struct fsi_device *dev, void *val)
150 uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));
152 return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
155 static void fsi_device_release(struct device *_device)
157 struct fsi_device *device = to_fsi_dev(_device);
159 of_node_put(device->dev.of_node);
163 static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
165 struct fsi_device *dev;
167 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
171 dev->dev.parent = &slave->dev;
172 dev->dev.bus = &fsi_bus_type;
173 dev->dev.release = fsi_device_release;
178 /* FSI slave support */
179 static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
182 uint32_t addr = *addrp;
185 if (addr > slave->size)
188 /* For 23 bit addressing, we encode the extra two bits in the slave
189 * id (and the slave's actual ID needs to be 0).
191 if (addr > 0x1fffff) {
194 id = (addr >> 21) & 0x3;
203 static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
205 struct fsi_master *master = slave->master;
213 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
218 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
219 &stat, sizeof(stat));
223 dev_dbg(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n",
224 be32_to_cpu(stat), be32_to_cpu(irq));
226 /* clear interrupts */
227 return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
231 /* Encode slave local bus echo delay */
232 static inline uint32_t fsi_smode_echodly(int x)
234 return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
237 /* Encode slave local bus send delay */
238 static inline uint32_t fsi_smode_senddly(int x)
240 return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
243 /* Encode slave local bus clock rate ratio */
244 static inline uint32_t fsi_smode_lbcrr(int x)
246 return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
249 /* Encode slave ID */
250 static inline uint32_t fsi_smode_sid(int x)
252 return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
255 static uint32_t fsi_slave_smode(int id, u8 t_senddly, u8 t_echodly)
257 return FSI_SMODE_WSC | FSI_SMODE_ECRC
259 | fsi_smode_echodly(t_echodly - 1) | fsi_smode_senddly(t_senddly - 1)
260 | fsi_smode_lbcrr(0x8);
263 static int fsi_slave_set_smode(struct fsi_slave *slave)
268 /* set our smode register with the slave ID field to 0; this enables
269 * extended slave addressing
271 smode = fsi_slave_smode(slave->id, slave->t_send_delay, slave->t_echo_delay);
272 data = cpu_to_be32(smode);
274 return fsi_master_write(slave->master, slave->link, slave->id,
275 FSI_SLAVE_BASE + FSI_SMODE,
276 &data, sizeof(data));
279 static int fsi_slave_handle_error(struct fsi_slave *slave, bool write,
280 uint32_t addr, size_t size)
282 struct fsi_master *master = slave->master;
285 uint8_t id, send_delay, echo_delay;
293 dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]",
294 write ? "write" : "read", addr, size);
296 /* try a simple clear of error conditions, which may fail if we've lost
297 * communication with the slave
299 rc = fsi_slave_report_and_clear_errors(slave);
303 /* send a TERM and retry */
305 rc = master->term(master, link, id);
307 rc = fsi_master_read(master, link, id, 0,
310 rc = fsi_slave_report_and_clear_errors(slave);
316 send_delay = slave->t_send_delay;
317 echo_delay = slave->t_echo_delay;
319 /* getting serious, reset the slave via BREAK */
320 rc = fsi_master_break(master, link);
324 slave->t_send_delay = send_delay;
325 slave->t_echo_delay = echo_delay;
327 rc = fsi_slave_set_smode(slave);
331 if (master->link_config)
332 master->link_config(master, link,
334 slave->t_echo_delay);
336 return fsi_slave_report_and_clear_errors(slave);
339 int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
340 void *val, size_t size)
342 uint8_t id = slave->id;
345 rc = fsi_slave_calc_addr(slave, &addr, &id);
349 for (i = 0; i < slave_retries; i++) {
350 rc = fsi_master_read(slave->master, slave->link,
351 id, addr, val, size);
355 err_rc = fsi_slave_handle_error(slave, false, addr, size);
362 EXPORT_SYMBOL_GPL(fsi_slave_read);
364 int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
365 const void *val, size_t size)
367 uint8_t id = slave->id;
370 rc = fsi_slave_calc_addr(slave, &addr, &id);
374 for (i = 0; i < slave_retries; i++) {
375 rc = fsi_master_write(slave->master, slave->link,
376 id, addr, val, size);
380 err_rc = fsi_slave_handle_error(slave, true, addr, size);
387 EXPORT_SYMBOL_GPL(fsi_slave_write);
389 extern int fsi_slave_claim_range(struct fsi_slave *slave,
390 uint32_t addr, uint32_t size)
392 if (addr + size < addr)
395 if (addr + size > slave->size)
398 /* todo: check for overlapping claims */
401 EXPORT_SYMBOL_GPL(fsi_slave_claim_range);
403 extern void fsi_slave_release_range(struct fsi_slave *slave,
404 uint32_t addr, uint32_t size)
407 EXPORT_SYMBOL_GPL(fsi_slave_release_range);
409 static bool fsi_device_node_matches(struct device *dev, struct device_node *np,
410 uint32_t addr, uint32_t size)
412 unsigned int len, na, ns;
416 na = of_n_addr_cells(np);
417 ns = of_n_size_cells(np);
419 if (na != 1 || ns != 1)
422 prop = of_get_property(np, "reg", &len);
423 if (!prop || len != 8)
426 if (of_read_number(prop, 1) != addr)
429 psize = of_read_number(prop + 1, 1);
432 "node %s matches probed address, but not size (got 0x%x, expected 0x%x)",
433 of_node_full_name(np), psize, size);
439 /* Find a matching node for the slave engine at @address, using @size bytes
440 * of space. Returns NULL if not found, or a matching node with refcount
441 * already incremented.
443 static struct device_node *fsi_device_find_of_node(struct fsi_device *dev)
445 struct device_node *parent, *np;
447 parent = dev_of_node(&dev->slave->dev);
451 for_each_child_of_node(parent, np) {
452 if (fsi_device_node_matches(&dev->dev, np,
453 dev->addr, dev->size))
460 static int fsi_slave_scan(struct fsi_slave *slave)
462 uint32_t engine_addr;
468 * We keep the peek mode and slave engines for the core; so start
469 * at the third slot in the configuration table. We also need to
470 * skip the chip ID entry at the start of the address space.
472 engine_addr = engine_page_size * 3;
473 for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
474 uint8_t slots, version, type, crc;
475 struct fsi_device *dev;
479 rc = fsi_slave_read(slave, (i + 1) * sizeof(data),
480 &data, sizeof(data));
482 dev_warn(&slave->dev,
483 "error reading slave registers\n");
486 conf = be32_to_cpu(data);
488 crc = crc4(0, conf, 32);
490 dev_warn(&slave->dev,
491 "crc error in slave register at 0x%04x\n",
496 slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
497 >> FSI_SLAVE_CONF_SLOTS_SHIFT;
498 version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
499 >> FSI_SLAVE_CONF_VERSION_SHIFT;
500 type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
501 >> FSI_SLAVE_CONF_TYPE_SHIFT;
504 * Unused address areas are marked by a zero type value; this
505 * skips the defined address areas
507 if (type != 0 && slots != 0) {
510 dev = fsi_create_device(slave);
515 dev->engine_type = type;
516 dev->version = version;
518 dev->addr = engine_addr;
519 dev->size = slots * engine_page_size;
522 "engine[%i]: type %x, version %x, addr %x size %x\n",
523 dev->unit, dev->engine_type, version,
524 dev->addr, dev->size);
526 dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
527 slave->master->idx, slave->link,
529 dev->dev.of_node = fsi_device_find_of_node(dev);
531 rc = device_register(&dev->dev);
533 dev_warn(&slave->dev, "add failed: %d\n", rc);
534 put_device(&dev->dev);
538 engine_addr += slots * engine_page_size;
540 if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
547 static unsigned long aligned_access_size(size_t offset, size_t count)
549 unsigned long offset_unit, count_unit;
553 * 1. Access size must be less than or equal to the maximum access
554 * width or the highest power-of-two factor of offset
555 * 2. Access size must be less than or equal to the amount specified by
558 * The access width is optimal if we can calculate 1 to be strictly
559 * equal while still satisfying 2.
562 /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
563 offset_unit = BIT(__builtin_ctzl(offset | 4));
565 /* Find 2 by the top bit of count */
566 count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count));
568 /* Constrain the maximum access width to the minimum of both criteria */
569 return BIT(__builtin_ctzl(offset_unit | count_unit));
572 static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
573 struct kobject *kobj, struct bin_attribute *attr, char *buf,
574 loff_t off, size_t count)
576 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
577 size_t total_len, read_len;
583 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
586 for (total_len = 0; total_len < count; total_len += read_len) {
587 read_len = aligned_access_size(off, count - total_len);
589 rc = fsi_slave_read(slave, off, buf + total_len, read_len);
599 static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
600 struct kobject *kobj, struct bin_attribute *attr,
601 char *buf, loff_t off, size_t count)
603 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
604 size_t total_len, write_len;
610 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
613 for (total_len = 0; total_len < count; total_len += write_len) {
614 write_len = aligned_access_size(off, count - total_len);
616 rc = fsi_slave_write(slave, off, buf + total_len, write_len);
626 static const struct bin_attribute fsi_slave_raw_attr = {
632 .read = fsi_slave_sysfs_raw_read,
633 .write = fsi_slave_sysfs_raw_write,
636 static void fsi_slave_release(struct device *dev)
638 struct fsi_slave *slave = to_fsi_slave(dev);
640 fsi_free_minor(slave->dev.devt);
641 of_node_put(dev->of_node);
645 static bool fsi_slave_node_matches(struct device_node *np,
646 int link, uint8_t id)
648 unsigned int len, na, ns;
651 na = of_n_addr_cells(np);
652 ns = of_n_size_cells(np);
654 /* Ensure we have the correct format for addresses and sizes in
657 if (na != 2 || ns != 0)
660 prop = of_get_property(np, "reg", &len);
661 if (!prop || len != 8)
664 return (of_read_number(prop, 1) == link) &&
665 (of_read_number(prop + 1, 1) == id);
668 /* Find a matching node for the slave at (link, id). Returns NULL if none
669 * found, or a matching node with refcount already incremented.
671 static struct device_node *fsi_slave_find_of_node(struct fsi_master *master,
672 int link, uint8_t id)
674 struct device_node *parent, *np;
676 parent = dev_of_node(&master->dev);
680 for_each_child_of_node(parent, np) {
681 if (fsi_slave_node_matches(np, link, id))
688 static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
691 struct fsi_slave *slave = filep->private_data;
692 size_t total_len, read_len;
693 loff_t off = *offset;
699 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
702 for (total_len = 0; total_len < count; total_len += read_len) {
705 read_len = min_t(size_t, count, 4);
706 read_len -= off & 0x3;
708 rc = fsi_slave_read(slave, off, &data, read_len);
711 rc = copy_to_user(buf + total_len, &data, read_len);
724 static ssize_t cfam_write(struct file *filep, const char __user *buf,
725 size_t count, loff_t *offset)
727 struct fsi_slave *slave = filep->private_data;
728 size_t total_len, write_len;
729 loff_t off = *offset;
736 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
739 for (total_len = 0; total_len < count; total_len += write_len) {
742 write_len = min_t(size_t, count, 4);
743 write_len -= off & 0x3;
745 rc = copy_from_user(&data, buf + total_len, write_len);
750 rc = fsi_slave_write(slave, off, &data, write_len);
761 static loff_t cfam_llseek(struct file *file, loff_t offset, int whence)
767 file->f_pos = offset;
776 static int cfam_open(struct inode *inode, struct file *file)
778 struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev);
780 file->private_data = slave;
785 static const struct file_operations cfam_fops = {
786 .owner = THIS_MODULE,
788 .llseek = cfam_llseek,
793 static ssize_t send_term_store(struct device *dev,
794 struct device_attribute *attr,
795 const char *buf, size_t count)
797 struct fsi_slave *slave = to_fsi_slave(dev);
798 struct fsi_master *master = slave->master;
803 master->term(master, slave->link, slave->id);
807 static DEVICE_ATTR_WO(send_term);
809 static ssize_t slave_send_echo_show(struct device *dev,
810 struct device_attribute *attr,
813 struct fsi_slave *slave = to_fsi_slave(dev);
815 return sprintf(buf, "%u\n", slave->t_send_delay);
818 static ssize_t slave_send_echo_store(struct device *dev,
819 struct device_attribute *attr, const char *buf, size_t count)
821 struct fsi_slave *slave = to_fsi_slave(dev);
822 struct fsi_master *master = slave->master;
826 if (kstrtoul(buf, 0, &val) < 0)
829 if (val < 1 || val > 16)
832 if (!master->link_config)
835 /* Current HW mandates that send and echo delay are identical */
836 slave->t_send_delay = val;
837 slave->t_echo_delay = val;
839 rc = fsi_slave_set_smode(slave);
842 if (master->link_config)
843 master->link_config(master, slave->link,
845 slave->t_echo_delay);
850 static DEVICE_ATTR(send_echo_delays, 0600,
851 slave_send_echo_show, slave_send_echo_store);
853 static ssize_t chip_id_show(struct device *dev,
854 struct device_attribute *attr,
857 struct fsi_slave *slave = to_fsi_slave(dev);
859 return sprintf(buf, "%d\n", slave->chip_id);
862 static DEVICE_ATTR_RO(chip_id);
864 static ssize_t cfam_id_show(struct device *dev,
865 struct device_attribute *attr,
868 struct fsi_slave *slave = to_fsi_slave(dev);
870 return sprintf(buf, "0x%x\n", slave->cfam_id);
873 static DEVICE_ATTR_RO(cfam_id);
875 static struct attribute *cfam_attr[] = {
876 &dev_attr_send_echo_delays.attr,
877 &dev_attr_chip_id.attr,
878 &dev_attr_cfam_id.attr,
879 &dev_attr_send_term.attr,
883 static const struct attribute_group cfam_attr_group = {
887 static const struct attribute_group *cfam_attr_groups[] = {
892 static char *cfam_devnode(struct device *dev, umode_t *mode,
893 kuid_t *uid, kgid_t *gid)
895 struct fsi_slave *slave = to_fsi_slave(dev);
897 #ifdef CONFIG_FSI_NEW_DEV_NODE
898 return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx);
900 return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx);
904 static const struct device_type cfam_type = {
906 .devnode = cfam_devnode,
907 .groups = cfam_attr_groups
910 static char *fsi_cdev_devnode(struct device *dev, umode_t *mode,
911 kuid_t *uid, kgid_t *gid)
913 #ifdef CONFIG_FSI_NEW_DEV_NODE
914 return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev));
916 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
920 const struct device_type fsi_cdev_type = {
922 .devnode = fsi_cdev_devnode,
924 EXPORT_SYMBOL_GPL(fsi_cdev_type);
926 /* Backward compatible /dev/ numbering in "old style" mode */
927 static int fsi_adjust_index(int index)
929 #ifdef CONFIG_FSI_NEW_DEV_NODE
936 static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type,
937 dev_t *out_dev, int *out_index)
939 int cid = slave->chip_id;
942 /* Check if we qualify for legacy numbering */
943 if (cid >= 0 && cid < 16 && type < 4) {
944 /* Try reserving the legacy number */
945 id = (cid << 4) | type;
946 id = ida_simple_get(&fsi_minor_ida, id, id + 1, GFP_KERNEL);
948 *out_index = fsi_adjust_index(cid);
949 *out_dev = fsi_base_dev + id;
955 /* Fallback to non-legacy allocation */
957 id = ida_simple_get(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP,
958 FSI_CHAR_MAX_DEVICES, GFP_KERNEL);
961 *out_index = fsi_adjust_index(id);
962 *out_dev = fsi_base_dev + id;
966 int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type,
967 dev_t *out_dev, int *out_index)
969 return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index);
971 EXPORT_SYMBOL_GPL(fsi_get_new_minor);
973 void fsi_free_minor(dev_t dev)
975 ida_simple_remove(&fsi_minor_ida, MINOR(dev));
977 EXPORT_SYMBOL_GPL(fsi_free_minor);
979 static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
982 struct fsi_slave *slave;
987 /* Currently, we only support single slaves on a link, and use the
988 * full 23-bit address range
993 rc = fsi_master_read(master, link, id, 0, &data, sizeof(data));
995 dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
999 cfam_id = be32_to_cpu(data);
1001 crc = crc4(0, cfam_id, 32);
1003 dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n",
1008 dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
1009 cfam_id, master->idx, link, id);
1011 /* If we're behind a master that doesn't provide a self-running bus
1012 * clock, put the slave into async mode
1014 if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
1015 llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
1016 rc = fsi_master_write(master, link, id,
1017 FSI_SLAVE_BASE + FSI_LLMODE,
1018 &llmode, sizeof(llmode));
1020 dev_warn(&master->dev,
1021 "can't set llmode on slave:%02x:%02x %d\n",
1025 /* We can communicate with a slave; create the slave device and
1028 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1032 dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
1033 slave->dev.type = &cfam_type;
1034 slave->dev.parent = &master->dev;
1035 slave->dev.of_node = fsi_slave_find_of_node(master, link, id);
1036 slave->dev.release = fsi_slave_release;
1037 device_initialize(&slave->dev);
1038 slave->cfam_id = cfam_id;
1039 slave->master = master;
1042 slave->size = FSI_SLAVE_SIZE_23b;
1043 slave->t_send_delay = 16;
1044 slave->t_echo_delay = 16;
1046 /* Get chip ID if any */
1047 slave->chip_id = -1;
1048 if (slave->dev.of_node) {
1050 if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop))
1051 slave->chip_id = prop;
1055 rc = fsi_slave_set_smode(slave);
1057 dev_warn(&master->dev,
1058 "can't set smode on slave:%02x:%02x %d\n",
1063 /* Allocate a minor in the FSI space */
1064 rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt,
1069 /* Create chardev for userspace access */
1070 cdev_init(&slave->cdev, &cfam_fops);
1071 rc = cdev_device_add(&slave->cdev, &slave->dev);
1073 dev_err(&slave->dev, "Error %d creating slave device\n", rc);
1077 /* Now that we have the cdev registered with the core, any fatal
1078 * failures beyond this point will need to clean up through
1079 * cdev_device_del(). Fortunately though, nothing past here is fatal.
1082 if (master->link_config)
1083 master->link_config(master, link,
1084 slave->t_send_delay,
1085 slave->t_echo_delay);
1087 /* Legacy raw file -> to be removed */
1088 rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
1090 dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
1093 rc = fsi_slave_scan(slave);
1095 dev_dbg(&master->dev, "failed during slave scan with: %d\n",
1101 fsi_free_minor(slave->dev.devt);
1103 of_node_put(slave->dev.of_node);
1108 /* FSI master support */
1109 static int fsi_check_access(uint32_t addr, size_t size)
1114 } else if (size == 2) {
1117 } else if (size != 1)
1123 static int fsi_master_read(struct fsi_master *master, int link,
1124 uint8_t slave_id, uint32_t addr, void *val, size_t size)
1128 trace_fsi_master_read(master, link, slave_id, addr, size);
1130 rc = fsi_check_access(addr, size);
1132 rc = master->read(master, link, slave_id, addr, val, size);
1134 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
1140 static int fsi_master_write(struct fsi_master *master, int link,
1141 uint8_t slave_id, uint32_t addr, const void *val, size_t size)
1145 trace_fsi_master_write(master, link, slave_id, addr, size, val);
1147 rc = fsi_check_access(addr, size);
1149 rc = master->write(master, link, slave_id, addr, val, size);
1151 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
1157 static int fsi_master_link_enable(struct fsi_master *master, int link)
1159 if (master->link_enable)
1160 return master->link_enable(master, link);
1166 * Issue a break command on this link
1168 static int fsi_master_break(struct fsi_master *master, int link)
1172 trace_fsi_master_break(master, link);
1174 if (master->send_break)
1175 rc = master->send_break(master, link);
1176 if (master->link_config)
1177 master->link_config(master, link, 16, 16);
1182 static int fsi_master_scan(struct fsi_master *master)
1186 for (link = 0; link < master->n_links; link++) {
1187 rc = fsi_master_link_enable(master, link);
1189 dev_dbg(&master->dev,
1190 "enable link %d failed: %d\n", link, rc);
1193 rc = fsi_master_break(master, link);
1195 dev_dbg(&master->dev,
1196 "break to link %d failed: %d\n", link, rc);
1200 fsi_slave_init(master, link, 0);
1206 static int fsi_slave_remove_device(struct device *dev, void *arg)
1208 device_unregister(dev);
1212 static int fsi_master_remove_slave(struct device *dev, void *arg)
1214 struct fsi_slave *slave = to_fsi_slave(dev);
1216 device_for_each_child(dev, NULL, fsi_slave_remove_device);
1217 cdev_device_del(&slave->cdev, &slave->dev);
1222 static void fsi_master_unscan(struct fsi_master *master)
1224 device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
1227 int fsi_master_rescan(struct fsi_master *master)
1231 mutex_lock(&master->scan_lock);
1232 fsi_master_unscan(master);
1233 rc = fsi_master_scan(master);
1234 mutex_unlock(&master->scan_lock);
1238 EXPORT_SYMBOL_GPL(fsi_master_rescan);
1240 static ssize_t master_rescan_store(struct device *dev,
1241 struct device_attribute *attr, const char *buf, size_t count)
1243 struct fsi_master *master = to_fsi_master(dev);
1246 rc = fsi_master_rescan(master);
1253 static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
1255 static ssize_t master_break_store(struct device *dev,
1256 struct device_attribute *attr, const char *buf, size_t count)
1258 struct fsi_master *master = to_fsi_master(dev);
1260 fsi_master_break(master, 0);
1265 static DEVICE_ATTR(break, 0200, NULL, master_break_store);
1267 int fsi_master_register(struct fsi_master *master)
1270 struct device_node *np;
1272 mutex_init(&master->scan_lock);
1273 master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
1274 if (master->idx < 0)
1277 dev_set_name(&master->dev, "fsi%d", master->idx);
1279 rc = device_register(&master->dev);
1281 ida_simple_remove(&master_ida, master->idx);
1285 rc = device_create_file(&master->dev, &dev_attr_rescan);
1287 device_del(&master->dev);
1288 ida_simple_remove(&master_ida, master->idx);
1292 rc = device_create_file(&master->dev, &dev_attr_break);
1294 device_del(&master->dev);
1295 ida_simple_remove(&master_ida, master->idx);
1299 np = dev_of_node(&master->dev);
1300 if (!of_property_read_bool(np, "no-scan-on-init")) {
1301 mutex_lock(&master->scan_lock);
1302 fsi_master_scan(master);
1303 mutex_unlock(&master->scan_lock);
1308 EXPORT_SYMBOL_GPL(fsi_master_register);
1310 void fsi_master_unregister(struct fsi_master *master)
1312 if (master->idx >= 0) {
1313 ida_simple_remove(&master_ida, master->idx);
1317 mutex_lock(&master->scan_lock);
1318 fsi_master_unscan(master);
1319 mutex_unlock(&master->scan_lock);
1320 device_unregister(&master->dev);
1322 EXPORT_SYMBOL_GPL(fsi_master_unregister);
1324 /* FSI core & Linux bus type definitions */
1326 static int fsi_bus_match(struct device *dev, struct device_driver *drv)
1328 struct fsi_device *fsi_dev = to_fsi_dev(dev);
1329 struct fsi_driver *fsi_drv = to_fsi_drv(drv);
1330 const struct fsi_device_id *id;
1332 if (!fsi_drv->id_table)
1335 for (id = fsi_drv->id_table; id->engine_type; id++) {
1336 if (id->engine_type != fsi_dev->engine_type)
1338 if (id->version == FSI_VERSION_ANY ||
1339 id->version == fsi_dev->version)
1346 int fsi_driver_register(struct fsi_driver *fsi_drv)
1350 if (!fsi_drv->id_table)
1353 return driver_register(&fsi_drv->drv);
1355 EXPORT_SYMBOL_GPL(fsi_driver_register);
1357 void fsi_driver_unregister(struct fsi_driver *fsi_drv)
1359 driver_unregister(&fsi_drv->drv);
1361 EXPORT_SYMBOL_GPL(fsi_driver_unregister);
1363 struct bus_type fsi_bus_type = {
1365 .match = fsi_bus_match,
1367 EXPORT_SYMBOL_GPL(fsi_bus_type);
1369 static int __init fsi_init(void)
1373 rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi");
1376 rc = bus_register(&fsi_bus_type);
1382 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
1385 postcore_initcall(fsi_init);
1387 static void fsi_exit(void)
1389 bus_unregister(&fsi_bus_type);
1390 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
1391 ida_destroy(&fsi_minor_ida);
1393 module_exit(fsi_exit);
1394 module_param(discard_errors, int, 0664);
1395 MODULE_LICENSE("GPL");
1396 MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");