Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / fsi / fsi-core.c
blob1d83f3ba478b961426415acb10b99487a165ca83
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * FSI core driver
5 * Copyright (C) IBM Corporation 2016
7 * TODO:
8 * - Rework topology
9 * - s/chip_id/chip_loc
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>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/bitops.h>
21 #include <linux/cdev.h>
22 #include <linux/fs.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 */
56 * SMODE fields
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 */
70 * LLMODE fields
72 #define FSI_LLMODE_ASYNC 0x1
74 #define FSI_SLAVE_SIZE_23b 0x800000
76 static DEFINE_IDA(master_ida);
78 struct fsi_slave {
79 struct device dev;
80 struct fsi_master *master;
81 struct cdev cdev;
82 int cdev_idx;
83 int id; /* FSI address */
84 int link; /* FSI link# */
85 u32 cfam_id;
86 int chip_id;
87 uint32_t size; /* size of slave address space */
88 u8 t_send_delay;
89 u8 t_echo_delay;
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
118 * Parameters:
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,
129 size_t size)
131 if (addr > dev->size || size > dev->size || addr > dev->size - size)
132 return -EINVAL;
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,
139 size_t size)
141 if (addr > dev->size || size > dev->size || addr > dev->size - size)
142 return -EINVAL;
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);
160 kfree(device);
163 static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
165 struct fsi_device *dev;
167 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
168 if (!dev)
169 return NULL;
171 dev->dev.parent = &slave->dev;
172 dev->dev.bus = &fsi_bus_type;
173 dev->dev.release = fsi_device_release;
175 return dev;
178 /* FSI slave support */
179 static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
180 uint8_t *idp)
182 uint32_t addr = *addrp;
183 uint8_t id = *idp;
185 if (addr > slave->size)
186 return -EINVAL;
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) {
192 if (slave->id != 0)
193 return -EINVAL;
194 id = (addr >> 21) & 0x3;
195 addr &= 0x1fffff;
198 *addrp = addr;
199 *idp = id;
200 return 0;
203 static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
205 struct fsi_master *master = slave->master;
206 __be32 irq, stat;
207 int rc, link;
208 uint8_t id;
210 link = slave->link;
211 id = slave->id;
213 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
214 &irq, sizeof(irq));
215 if (rc)
216 return rc;
218 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
219 &stat, sizeof(stat));
220 if (rc)
221 return rc;
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,
228 &irq, sizeof(irq));
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
258 | fsi_smode_sid(id)
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)
265 uint32_t smode;
266 __be32 data;
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;
283 int rc, link;
284 uint32_t reg;
285 uint8_t id, send_delay, echo_delay;
287 if (discard_errors)
288 return -1;
290 link = slave->link;
291 id = slave->id;
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);
300 if (!rc)
301 return 0;
303 /* send a TERM and retry */
304 if (master->term) {
305 rc = master->term(master, link, id);
306 if (!rc) {
307 rc = fsi_master_read(master, link, id, 0,
308 &reg, sizeof(reg));
309 if (!rc)
310 rc = fsi_slave_report_and_clear_errors(slave);
311 if (!rc)
312 return 0;
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);
321 if (rc)
322 return rc;
324 slave->t_send_delay = send_delay;
325 slave->t_echo_delay = echo_delay;
327 rc = fsi_slave_set_smode(slave);
328 if (rc)
329 return rc;
331 if (master->link_config)
332 master->link_config(master, link,
333 slave->t_send_delay,
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;
343 int rc, err_rc, i;
345 rc = fsi_slave_calc_addr(slave, &addr, &id);
346 if (rc)
347 return rc;
349 for (i = 0; i < slave_retries; i++) {
350 rc = fsi_master_read(slave->master, slave->link,
351 id, addr, val, size);
352 if (!rc)
353 break;
355 err_rc = fsi_slave_handle_error(slave, false, addr, size);
356 if (err_rc)
357 break;
360 return rc;
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;
368 int rc, err_rc, i;
370 rc = fsi_slave_calc_addr(slave, &addr, &id);
371 if (rc)
372 return rc;
374 for (i = 0; i < slave_retries; i++) {
375 rc = fsi_master_write(slave->master, slave->link,
376 id, addr, val, size);
377 if (!rc)
378 break;
380 err_rc = fsi_slave_handle_error(slave, true, addr, size);
381 if (err_rc)
382 break;
385 return rc;
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)
393 return -EINVAL;
395 if (addr + size > slave->size)
396 return -EINVAL;
398 /* todo: check for overlapping claims */
399 return 0;
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;
413 const __be32 *prop;
414 uint32_t psize;
416 na = of_n_addr_cells(np);
417 ns = of_n_size_cells(np);
419 if (na != 1 || ns != 1)
420 return false;
422 prop = of_get_property(np, "reg", &len);
423 if (!prop || len != 8)
424 return false;
426 if (of_read_number(prop, 1) != addr)
427 return false;
429 psize = of_read_number(prop + 1, 1);
430 if (psize != size) {
431 dev_warn(dev,
432 "node %s matches probed address, but not size (got 0x%x, expected 0x%x)",
433 of_node_full_name(np), psize, size);
436 return true;
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);
448 if (!parent)
449 return NULL;
451 for_each_child_of_node(parent, np) {
452 if (fsi_device_node_matches(&dev->dev, np,
453 dev->addr, dev->size))
454 return np;
457 return NULL;
460 static int fsi_slave_scan(struct fsi_slave *slave)
462 uint32_t engine_addr;
463 int rc, i;
466 * scan engines
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;
476 uint32_t conf;
477 __be32 data;
479 rc = fsi_slave_read(slave, (i + 1) * sizeof(data),
480 &data, sizeof(data));
481 if (rc) {
482 dev_warn(&slave->dev,
483 "error reading slave registers\n");
484 return -1;
486 conf = be32_to_cpu(data);
488 crc = crc4(0, conf, 32);
489 if (crc) {
490 dev_warn(&slave->dev,
491 "crc error in slave register at 0x%04x\n",
493 return -1;
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) {
509 /* create device */
510 dev = fsi_create_device(slave);
511 if (!dev)
512 return -ENOMEM;
514 dev->slave = slave;
515 dev->engine_type = type;
516 dev->version = version;
517 dev->unit = i;
518 dev->addr = engine_addr;
519 dev->size = slots * engine_page_size;
521 dev_dbg(&slave->dev,
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,
528 slave->id, i - 2);
529 dev->dev.of_node = fsi_device_find_of_node(dev);
531 rc = device_register(&dev->dev);
532 if (rc) {
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))
541 break;
544 return 0;
547 static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
548 struct kobject *kobj, struct bin_attribute *attr, char *buf,
549 loff_t off, size_t count)
551 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
552 size_t total_len, read_len;
553 int rc;
555 if (off < 0)
556 return -EINVAL;
558 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
559 return -EINVAL;
561 for (total_len = 0; total_len < count; total_len += read_len) {
562 read_len = min_t(size_t, count, 4);
563 read_len -= off & 0x3;
565 rc = fsi_slave_read(slave, off, buf + total_len, read_len);
566 if (rc)
567 return rc;
569 off += read_len;
572 return count;
575 static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
576 struct kobject *kobj, struct bin_attribute *attr,
577 char *buf, loff_t off, size_t count)
579 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
580 size_t total_len, write_len;
581 int rc;
583 if (off < 0)
584 return -EINVAL;
586 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
587 return -EINVAL;
589 for (total_len = 0; total_len < count; total_len += write_len) {
590 write_len = min_t(size_t, count, 4);
591 write_len -= off & 0x3;
593 rc = fsi_slave_write(slave, off, buf + total_len, write_len);
594 if (rc)
595 return rc;
597 off += write_len;
600 return count;
603 static const struct bin_attribute fsi_slave_raw_attr = {
604 .attr = {
605 .name = "raw",
606 .mode = 0600,
608 .size = 0,
609 .read = fsi_slave_sysfs_raw_read,
610 .write = fsi_slave_sysfs_raw_write,
613 static void fsi_slave_release(struct device *dev)
615 struct fsi_slave *slave = to_fsi_slave(dev);
617 fsi_free_minor(slave->dev.devt);
618 of_node_put(dev->of_node);
619 kfree(slave);
622 static bool fsi_slave_node_matches(struct device_node *np,
623 int link, uint8_t id)
625 unsigned int len, na, ns;
626 const __be32 *prop;
628 na = of_n_addr_cells(np);
629 ns = of_n_size_cells(np);
631 /* Ensure we have the correct format for addresses and sizes in
632 * reg properties
634 if (na != 2 || ns != 0)
635 return false;
637 prop = of_get_property(np, "reg", &len);
638 if (!prop || len != 8)
639 return false;
641 return (of_read_number(prop, 1) == link) &&
642 (of_read_number(prop + 1, 1) == id);
645 /* Find a matching node for the slave at (link, id). Returns NULL if none
646 * found, or a matching node with refcount already incremented.
648 static struct device_node *fsi_slave_find_of_node(struct fsi_master *master,
649 int link, uint8_t id)
651 struct device_node *parent, *np;
653 parent = dev_of_node(&master->dev);
654 if (!parent)
655 return NULL;
657 for_each_child_of_node(parent, np) {
658 if (fsi_slave_node_matches(np, link, id))
659 return np;
662 return NULL;
665 static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
666 loff_t *offset)
668 struct fsi_slave *slave = filep->private_data;
669 size_t total_len, read_len;
670 loff_t off = *offset;
671 ssize_t rc;
673 if (off < 0)
674 return -EINVAL;
676 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
677 return -EINVAL;
679 for (total_len = 0; total_len < count; total_len += read_len) {
680 __be32 data;
682 read_len = min_t(size_t, count, 4);
683 read_len -= off & 0x3;
685 rc = fsi_slave_read(slave, off, &data, read_len);
686 if (rc)
687 goto fail;
688 rc = copy_to_user(buf + total_len, &data, read_len);
689 if (rc) {
690 rc = -EFAULT;
691 goto fail;
693 off += read_len;
695 rc = count;
696 fail:
697 *offset = off;
698 return count;
701 static ssize_t cfam_write(struct file *filep, const char __user *buf,
702 size_t count, loff_t *offset)
704 struct fsi_slave *slave = filep->private_data;
705 size_t total_len, write_len;
706 loff_t off = *offset;
707 ssize_t rc;
710 if (off < 0)
711 return -EINVAL;
713 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
714 return -EINVAL;
716 for (total_len = 0; total_len < count; total_len += write_len) {
717 __be32 data;
719 write_len = min_t(size_t, count, 4);
720 write_len -= off & 0x3;
722 rc = copy_from_user(&data, buf + total_len, write_len);
723 if (rc) {
724 rc = -EFAULT;
725 goto fail;
727 rc = fsi_slave_write(slave, off, &data, write_len);
728 if (rc)
729 goto fail;
730 off += write_len;
732 rc = count;
733 fail:
734 *offset = off;
735 return count;
738 static loff_t cfam_llseek(struct file *file, loff_t offset, int whence)
740 switch (whence) {
741 case SEEK_CUR:
742 break;
743 case SEEK_SET:
744 file->f_pos = offset;
745 break;
746 default:
747 return -EINVAL;
750 return offset;
753 static int cfam_open(struct inode *inode, struct file *file)
755 struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev);
757 file->private_data = slave;
759 return 0;
762 static const struct file_operations cfam_fops = {
763 .owner = THIS_MODULE,
764 .open = cfam_open,
765 .llseek = cfam_llseek,
766 .read = cfam_read,
767 .write = cfam_write,
770 static ssize_t send_term_store(struct device *dev,
771 struct device_attribute *attr,
772 const char *buf, size_t count)
774 struct fsi_slave *slave = to_fsi_slave(dev);
775 struct fsi_master *master = slave->master;
777 if (!master->term)
778 return -ENODEV;
780 master->term(master, slave->link, slave->id);
781 return count;
784 static DEVICE_ATTR_WO(send_term);
786 static ssize_t slave_send_echo_show(struct device *dev,
787 struct device_attribute *attr,
788 char *buf)
790 struct fsi_slave *slave = to_fsi_slave(dev);
792 return sprintf(buf, "%u\n", slave->t_send_delay);
795 static ssize_t slave_send_echo_store(struct device *dev,
796 struct device_attribute *attr, const char *buf, size_t count)
798 struct fsi_slave *slave = to_fsi_slave(dev);
799 struct fsi_master *master = slave->master;
800 unsigned long val;
801 int rc;
803 if (kstrtoul(buf, 0, &val) < 0)
804 return -EINVAL;
806 if (val < 1 || val > 16)
807 return -EINVAL;
809 if (!master->link_config)
810 return -ENXIO;
812 /* Current HW mandates that send and echo delay are identical */
813 slave->t_send_delay = val;
814 slave->t_echo_delay = val;
816 rc = fsi_slave_set_smode(slave);
817 if (rc < 0)
818 return rc;
819 if (master->link_config)
820 master->link_config(master, slave->link,
821 slave->t_send_delay,
822 slave->t_echo_delay);
824 return count;
827 static DEVICE_ATTR(send_echo_delays, 0600,
828 slave_send_echo_show, slave_send_echo_store);
830 static ssize_t chip_id_show(struct device *dev,
831 struct device_attribute *attr,
832 char *buf)
834 struct fsi_slave *slave = to_fsi_slave(dev);
836 return sprintf(buf, "%d\n", slave->chip_id);
839 static DEVICE_ATTR_RO(chip_id);
841 static ssize_t cfam_id_show(struct device *dev,
842 struct device_attribute *attr,
843 char *buf)
845 struct fsi_slave *slave = to_fsi_slave(dev);
847 return sprintf(buf, "0x%x\n", slave->cfam_id);
850 static DEVICE_ATTR_RO(cfam_id);
852 static struct attribute *cfam_attr[] = {
853 &dev_attr_send_echo_delays.attr,
854 &dev_attr_chip_id.attr,
855 &dev_attr_cfam_id.attr,
856 &dev_attr_send_term.attr,
857 NULL,
860 static const struct attribute_group cfam_attr_group = {
861 .attrs = cfam_attr,
864 static const struct attribute_group *cfam_attr_groups[] = {
865 &cfam_attr_group,
866 NULL,
869 static char *cfam_devnode(struct device *dev, umode_t *mode,
870 kuid_t *uid, kgid_t *gid)
872 struct fsi_slave *slave = to_fsi_slave(dev);
874 #ifdef CONFIG_FSI_NEW_DEV_NODE
875 return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx);
876 #else
877 return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx);
878 #endif
881 static const struct device_type cfam_type = {
882 .name = "cfam",
883 .devnode = cfam_devnode,
884 .groups = cfam_attr_groups
887 static char *fsi_cdev_devnode(struct device *dev, umode_t *mode,
888 kuid_t *uid, kgid_t *gid)
890 #ifdef CONFIG_FSI_NEW_DEV_NODE
891 return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev));
892 #else
893 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
894 #endif
897 const struct device_type fsi_cdev_type = {
898 .name = "fsi-cdev",
899 .devnode = fsi_cdev_devnode,
901 EXPORT_SYMBOL_GPL(fsi_cdev_type);
903 /* Backward compatible /dev/ numbering in "old style" mode */
904 static int fsi_adjust_index(int index)
906 #ifdef CONFIG_FSI_NEW_DEV_NODE
907 return index;
908 #else
909 return index + 1;
910 #endif
913 static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type,
914 dev_t *out_dev, int *out_index)
916 int cid = slave->chip_id;
917 int id;
919 /* Check if we qualify for legacy numbering */
920 if (cid >= 0 && cid < 16 && type < 4) {
921 /* Try reserving the legacy number */
922 id = (cid << 4) | type;
923 id = ida_simple_get(&fsi_minor_ida, id, id + 1, GFP_KERNEL);
924 if (id >= 0) {
925 *out_index = fsi_adjust_index(cid);
926 *out_dev = fsi_base_dev + id;
927 return 0;
929 /* Other failure */
930 if (id != -ENOSPC)
931 return id;
932 /* Fallback to non-legacy allocation */
934 id = ida_simple_get(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP,
935 FSI_CHAR_MAX_DEVICES, GFP_KERNEL);
936 if (id < 0)
937 return id;
938 *out_index = fsi_adjust_index(id);
939 *out_dev = fsi_base_dev + id;
940 return 0;
943 int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type,
944 dev_t *out_dev, int *out_index)
946 return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index);
948 EXPORT_SYMBOL_GPL(fsi_get_new_minor);
950 void fsi_free_minor(dev_t dev)
952 ida_simple_remove(&fsi_minor_ida, MINOR(dev));
954 EXPORT_SYMBOL_GPL(fsi_free_minor);
956 static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
958 uint32_t cfam_id;
959 struct fsi_slave *slave;
960 uint8_t crc;
961 __be32 data, llmode;
962 int rc;
964 /* Currently, we only support single slaves on a link, and use the
965 * full 23-bit address range
967 if (id != 0)
968 return -EINVAL;
970 rc = fsi_master_read(master, link, id, 0, &data, sizeof(data));
971 if (rc) {
972 dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
973 link, id, rc);
974 return -ENODEV;
976 cfam_id = be32_to_cpu(data);
978 crc = crc4(0, cfam_id, 32);
979 if (crc) {
980 dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n",
981 link, id);
982 return -EIO;
985 dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
986 cfam_id, master->idx, link, id);
988 /* If we're behind a master that doesn't provide a self-running bus
989 * clock, put the slave into async mode
991 if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
992 llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
993 rc = fsi_master_write(master, link, id,
994 FSI_SLAVE_BASE + FSI_LLMODE,
995 &llmode, sizeof(llmode));
996 if (rc)
997 dev_warn(&master->dev,
998 "can't set llmode on slave:%02x:%02x %d\n",
999 link, id, rc);
1002 /* We can communicate with a slave; create the slave device and
1003 * register.
1005 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1006 if (!slave)
1007 return -ENOMEM;
1009 dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
1010 slave->dev.type = &cfam_type;
1011 slave->dev.parent = &master->dev;
1012 slave->dev.of_node = fsi_slave_find_of_node(master, link, id);
1013 slave->dev.release = fsi_slave_release;
1014 device_initialize(&slave->dev);
1015 slave->cfam_id = cfam_id;
1016 slave->master = master;
1017 slave->link = link;
1018 slave->id = id;
1019 slave->size = FSI_SLAVE_SIZE_23b;
1020 slave->t_send_delay = 16;
1021 slave->t_echo_delay = 16;
1023 /* Get chip ID if any */
1024 slave->chip_id = -1;
1025 if (slave->dev.of_node) {
1026 uint32_t prop;
1027 if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop))
1028 slave->chip_id = prop;
1032 /* Allocate a minor in the FSI space */
1033 rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt,
1034 &slave->cdev_idx);
1035 if (rc)
1036 goto err_free;
1038 /* Create chardev for userspace access */
1039 cdev_init(&slave->cdev, &cfam_fops);
1040 rc = cdev_device_add(&slave->cdev, &slave->dev);
1041 if (rc) {
1042 dev_err(&slave->dev, "Error %d creating slave device\n", rc);
1043 goto err_free;
1046 rc = fsi_slave_set_smode(slave);
1047 if (rc) {
1048 dev_warn(&master->dev,
1049 "can't set smode on slave:%02x:%02x %d\n",
1050 link, id, rc);
1051 kfree(slave);
1052 return -ENODEV;
1054 if (master->link_config)
1055 master->link_config(master, link,
1056 slave->t_send_delay,
1057 slave->t_echo_delay);
1059 /* Legacy raw file -> to be removed */
1060 rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
1061 if (rc)
1062 dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
1065 rc = fsi_slave_scan(slave);
1066 if (rc)
1067 dev_dbg(&master->dev, "failed during slave scan with: %d\n",
1068 rc);
1070 return rc;
1072 err_free:
1073 put_device(&slave->dev);
1074 return rc;
1077 /* FSI master support */
1078 static int fsi_check_access(uint32_t addr, size_t size)
1080 if (size == 4) {
1081 if (addr & 0x3)
1082 return -EINVAL;
1083 } else if (size == 2) {
1084 if (addr & 0x1)
1085 return -EINVAL;
1086 } else if (size != 1)
1087 return -EINVAL;
1089 return 0;
1092 static int fsi_master_read(struct fsi_master *master, int link,
1093 uint8_t slave_id, uint32_t addr, void *val, size_t size)
1095 int rc;
1097 trace_fsi_master_read(master, link, slave_id, addr, size);
1099 rc = fsi_check_access(addr, size);
1100 if (!rc)
1101 rc = master->read(master, link, slave_id, addr, val, size);
1103 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
1104 false, val, rc);
1106 return rc;
1109 static int fsi_master_write(struct fsi_master *master, int link,
1110 uint8_t slave_id, uint32_t addr, const void *val, size_t size)
1112 int rc;
1114 trace_fsi_master_write(master, link, slave_id, addr, size, val);
1116 rc = fsi_check_access(addr, size);
1117 if (!rc)
1118 rc = master->write(master, link, slave_id, addr, val, size);
1120 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
1121 true, val, rc);
1123 return rc;
1126 static int fsi_master_link_enable(struct fsi_master *master, int link)
1128 if (master->link_enable)
1129 return master->link_enable(master, link);
1131 return 0;
1135 * Issue a break command on this link
1137 static int fsi_master_break(struct fsi_master *master, int link)
1139 int rc = 0;
1141 trace_fsi_master_break(master, link);
1143 if (master->send_break)
1144 rc = master->send_break(master, link);
1145 if (master->link_config)
1146 master->link_config(master, link, 16, 16);
1148 return rc;
1151 static int fsi_master_scan(struct fsi_master *master)
1153 int link, rc;
1155 for (link = 0; link < master->n_links; link++) {
1156 rc = fsi_master_link_enable(master, link);
1157 if (rc) {
1158 dev_dbg(&master->dev,
1159 "enable link %d failed: %d\n", link, rc);
1160 continue;
1162 rc = fsi_master_break(master, link);
1163 if (rc) {
1164 dev_dbg(&master->dev,
1165 "break to link %d failed: %d\n", link, rc);
1166 continue;
1169 fsi_slave_init(master, link, 0);
1172 return 0;
1175 static int fsi_slave_remove_device(struct device *dev, void *arg)
1177 device_unregister(dev);
1178 return 0;
1181 static int fsi_master_remove_slave(struct device *dev, void *arg)
1183 struct fsi_slave *slave = to_fsi_slave(dev);
1185 device_for_each_child(dev, NULL, fsi_slave_remove_device);
1186 cdev_device_del(&slave->cdev, &slave->dev);
1187 put_device(dev);
1188 return 0;
1191 static void fsi_master_unscan(struct fsi_master *master)
1193 device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
1196 int fsi_master_rescan(struct fsi_master *master)
1198 int rc;
1200 mutex_lock(&master->scan_lock);
1201 fsi_master_unscan(master);
1202 rc = fsi_master_scan(master);
1203 mutex_unlock(&master->scan_lock);
1205 return rc;
1207 EXPORT_SYMBOL_GPL(fsi_master_rescan);
1209 static ssize_t master_rescan_store(struct device *dev,
1210 struct device_attribute *attr, const char *buf, size_t count)
1212 struct fsi_master *master = to_fsi_master(dev);
1213 int rc;
1215 rc = fsi_master_rescan(master);
1216 if (rc < 0)
1217 return rc;
1219 return count;
1222 static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
1224 static ssize_t master_break_store(struct device *dev,
1225 struct device_attribute *attr, const char *buf, size_t count)
1227 struct fsi_master *master = to_fsi_master(dev);
1229 fsi_master_break(master, 0);
1231 return count;
1234 static DEVICE_ATTR(break, 0200, NULL, master_break_store);
1236 int fsi_master_register(struct fsi_master *master)
1238 int rc;
1239 struct device_node *np;
1241 mutex_init(&master->scan_lock);
1242 master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
1243 dev_set_name(&master->dev, "fsi%d", master->idx);
1245 rc = device_register(&master->dev);
1246 if (rc) {
1247 ida_simple_remove(&master_ida, master->idx);
1248 return rc;
1251 rc = device_create_file(&master->dev, &dev_attr_rescan);
1252 if (rc) {
1253 device_del(&master->dev);
1254 ida_simple_remove(&master_ida, master->idx);
1255 return rc;
1258 rc = device_create_file(&master->dev, &dev_attr_break);
1259 if (rc) {
1260 device_del(&master->dev);
1261 ida_simple_remove(&master_ida, master->idx);
1262 return rc;
1265 np = dev_of_node(&master->dev);
1266 if (!of_property_read_bool(np, "no-scan-on-init")) {
1267 mutex_lock(&master->scan_lock);
1268 fsi_master_scan(master);
1269 mutex_unlock(&master->scan_lock);
1272 return 0;
1274 EXPORT_SYMBOL_GPL(fsi_master_register);
1276 void fsi_master_unregister(struct fsi_master *master)
1278 if (master->idx >= 0) {
1279 ida_simple_remove(&master_ida, master->idx);
1280 master->idx = -1;
1283 mutex_lock(&master->scan_lock);
1284 fsi_master_unscan(master);
1285 mutex_unlock(&master->scan_lock);
1286 device_unregister(&master->dev);
1288 EXPORT_SYMBOL_GPL(fsi_master_unregister);
1290 /* FSI core & Linux bus type definitions */
1292 static int fsi_bus_match(struct device *dev, struct device_driver *drv)
1294 struct fsi_device *fsi_dev = to_fsi_dev(dev);
1295 struct fsi_driver *fsi_drv = to_fsi_drv(drv);
1296 const struct fsi_device_id *id;
1298 if (!fsi_drv->id_table)
1299 return 0;
1301 for (id = fsi_drv->id_table; id->engine_type; id++) {
1302 if (id->engine_type != fsi_dev->engine_type)
1303 continue;
1304 if (id->version == FSI_VERSION_ANY ||
1305 id->version == fsi_dev->version)
1306 return 1;
1309 return 0;
1312 int fsi_driver_register(struct fsi_driver *fsi_drv)
1314 if (!fsi_drv)
1315 return -EINVAL;
1316 if (!fsi_drv->id_table)
1317 return -EINVAL;
1319 return driver_register(&fsi_drv->drv);
1321 EXPORT_SYMBOL_GPL(fsi_driver_register);
1323 void fsi_driver_unregister(struct fsi_driver *fsi_drv)
1325 driver_unregister(&fsi_drv->drv);
1327 EXPORT_SYMBOL_GPL(fsi_driver_unregister);
1329 struct bus_type fsi_bus_type = {
1330 .name = "fsi",
1331 .match = fsi_bus_match,
1333 EXPORT_SYMBOL_GPL(fsi_bus_type);
1335 static int __init fsi_init(void)
1337 int rc;
1339 rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi");
1340 if (rc)
1341 return rc;
1342 rc = bus_register(&fsi_bus_type);
1343 if (rc)
1344 goto fail_bus;
1345 return 0;
1347 fail_bus:
1348 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
1349 return rc;
1351 postcore_initcall(fsi_init);
1353 static void fsi_exit(void)
1355 bus_unregister(&fsi_bus_type);
1356 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
1357 ida_destroy(&fsi_minor_ida);
1359 module_exit(fsi_exit);
1360 module_param(discard_errors, int, 0664);
1361 MODULE_LICENSE("GPL");
1362 MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");