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/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
23 #include <linux/cdev.h>
25 #include <linux/uaccess.h>
27 #include "fsi-master.h"
28 #include "fsi-slave.h"
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/fsi.h>
33 #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
34 #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
35 #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
36 #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
37 #define FSI_SLAVE_CONF_VERSION_SHIFT 12
38 #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
39 #define FSI_SLAVE_CONF_TYPE_SHIFT 4
40 #define FSI_SLAVE_CONF_CRC_SHIFT 4
41 #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
42 #define FSI_SLAVE_CONF_DATA_BITS 28
44 #define FSI_PEEK_BASE 0x410
46 static const int engine_page_size
= 0x400;
48 #define FSI_SLAVE_BASE 0x800
51 * FSI slave engine control register offsets
53 #define FSI_SMODE 0x0 /* R/W: Mode register */
54 #define FSI_SISC 0x8 /* R/W: Interrupt condition */
55 #define FSI_SSTAT 0x14 /* R : Slave status */
56 #define FSI_SLBUS 0x30 /* W : LBUS Ownership */
57 #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
62 #define FSI_SMODE_WSC 0x80000000 /* Warm start done */
63 #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
64 #define FSI_SMODE_SID_SHIFT 24 /* ID shift */
65 #define FSI_SMODE_SID_MASK 3 /* ID Mask */
66 #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
67 #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
68 #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
69 #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
70 #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
71 #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
76 #define FSI_SLBUS_FORCE 0x80000000 /* Force LBUS ownership */
81 #define FSI_LLMODE_ASYNC 0x1
83 #define FSI_SLAVE_SIZE_23b 0x800000
85 static DEFINE_IDA(master_ida
);
87 static const int slave_retries
= 2;
88 static int discard_errors
;
90 static dev_t fsi_base_dev
;
91 static DEFINE_IDA(fsi_minor_ida
);
92 #define FSI_CHAR_MAX_DEVICES 0x1000
94 /* Legacy /dev numbering: 4 devices per chip, 16 chips */
95 #define FSI_CHAR_LEGACY_TOP 64
97 static int fsi_master_read(struct fsi_master
*master
, int link
,
98 uint8_t slave_id
, uint32_t addr
, void *val
, size_t size
);
99 static int fsi_master_write(struct fsi_master
*master
, int link
,
100 uint8_t slave_id
, uint32_t addr
, const void *val
, size_t size
);
101 static int fsi_master_break(struct fsi_master
*master
, int link
);
104 * fsi_device_read() / fsi_device_write() / fsi_device_peek()
106 * FSI endpoint-device support
108 * Read / write / peek accessors for a client
111 * dev: Structure passed to FSI client device drivers on probe().
112 * addr: FSI address of given device. Client should pass in its base address
113 * plus desired offset to access its register space.
114 * val: For read/peek this is the value read at the specified address. For
115 * write this is value to write to the specified address.
116 * The data in val must be FSI bus endian (big endian).
117 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
118 * Addresses must be aligned on size boundaries or an error will result.
120 int fsi_device_read(struct fsi_device
*dev
, uint32_t addr
, void *val
,
123 if (addr
> dev
->size
|| size
> dev
->size
|| addr
> dev
->size
- size
)
126 return fsi_slave_read(dev
->slave
, dev
->addr
+ addr
, val
, size
);
128 EXPORT_SYMBOL_GPL(fsi_device_read
);
130 int fsi_device_write(struct fsi_device
*dev
, uint32_t addr
, const void *val
,
133 if (addr
> dev
->size
|| size
> dev
->size
|| addr
> dev
->size
- size
)
136 return fsi_slave_write(dev
->slave
, dev
->addr
+ addr
, val
, size
);
138 EXPORT_SYMBOL_GPL(fsi_device_write
);
140 int fsi_device_peek(struct fsi_device
*dev
, void *val
)
142 uint32_t addr
= FSI_PEEK_BASE
+ ((dev
->unit
- 2) * sizeof(uint32_t));
144 return fsi_slave_read(dev
->slave
, addr
, val
, sizeof(uint32_t));
147 static void fsi_device_release(struct device
*_device
)
149 struct fsi_device
*device
= to_fsi_dev(_device
);
151 of_node_put(device
->dev
.of_node
);
155 static struct fsi_device
*fsi_create_device(struct fsi_slave
*slave
)
157 struct fsi_device
*dev
;
159 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
163 dev
->dev
.parent
= &slave
->dev
;
164 dev
->dev
.bus
= &fsi_bus_type
;
165 dev
->dev
.release
= fsi_device_release
;
170 /* FSI slave support */
171 static int fsi_slave_calc_addr(struct fsi_slave
*slave
, uint32_t *addrp
,
174 uint32_t addr
= *addrp
;
177 if (addr
> slave
->size
)
180 /* For 23 bit addressing, we encode the extra two bits in the slave
181 * id (and the slave's actual ID needs to be 0).
183 if (addr
> 0x1fffff) {
186 id
= (addr
>> 21) & 0x3;
195 static int fsi_slave_report_and_clear_errors(struct fsi_slave
*slave
)
197 struct fsi_master
*master
= slave
->master
;
205 rc
= fsi_master_read(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SISC
,
210 rc
= fsi_master_read(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SSTAT
,
211 &stat
, sizeof(stat
));
215 dev_dbg(&slave
->dev
, "status: 0x%08x, sisc: 0x%08x\n",
216 be32_to_cpu(stat
), be32_to_cpu(irq
));
218 /* clear interrupts */
219 return fsi_master_write(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SISC
,
223 /* Encode slave local bus echo delay */
224 static inline uint32_t fsi_smode_echodly(int x
)
226 return (x
& FSI_SMODE_ED_MASK
) << FSI_SMODE_ED_SHIFT
;
229 /* Encode slave local bus send delay */
230 static inline uint32_t fsi_smode_senddly(int x
)
232 return (x
& FSI_SMODE_SD_MASK
) << FSI_SMODE_SD_SHIFT
;
235 /* Encode slave local bus clock rate ratio */
236 static inline uint32_t fsi_smode_lbcrr(int x
)
238 return (x
& FSI_SMODE_LBCRR_MASK
) << FSI_SMODE_LBCRR_SHIFT
;
241 /* Encode slave ID */
242 static inline uint32_t fsi_smode_sid(int x
)
244 return (x
& FSI_SMODE_SID_MASK
) << FSI_SMODE_SID_SHIFT
;
247 static uint32_t fsi_slave_smode(int id
, u8 t_senddly
, u8 t_echodly
)
249 return FSI_SMODE_WSC
| FSI_SMODE_ECRC
251 | fsi_smode_echodly(t_echodly
- 1) | fsi_smode_senddly(t_senddly
- 1)
252 | fsi_smode_lbcrr(0x8);
255 static int fsi_slave_set_smode(struct fsi_slave
*slave
)
260 /* set our smode register with the slave ID field to 0; this enables
261 * extended slave addressing
263 smode
= fsi_slave_smode(slave
->id
, slave
->t_send_delay
, slave
->t_echo_delay
);
264 data
= cpu_to_be32(smode
);
266 return fsi_master_write(slave
->master
, slave
->link
, slave
->id
,
267 FSI_SLAVE_BASE
+ FSI_SMODE
,
268 &data
, sizeof(data
));
271 static int fsi_slave_handle_error(struct fsi_slave
*slave
, bool write
,
272 uint32_t addr
, size_t size
)
274 struct fsi_master
*master
= slave
->master
;
277 uint8_t id
, send_delay
, echo_delay
;
285 dev_dbg(&slave
->dev
, "handling error on %s to 0x%08x[%zd]",
286 write
? "write" : "read", addr
, size
);
288 /* try a simple clear of error conditions, which may fail if we've lost
289 * communication with the slave
291 rc
= fsi_slave_report_and_clear_errors(slave
);
295 /* send a TERM and retry */
297 rc
= master
->term(master
, link
, id
);
299 rc
= fsi_master_read(master
, link
, id
, 0,
302 rc
= fsi_slave_report_and_clear_errors(slave
);
308 send_delay
= slave
->t_send_delay
;
309 echo_delay
= slave
->t_echo_delay
;
311 /* getting serious, reset the slave via BREAK */
312 rc
= fsi_master_break(master
, link
);
316 slave
->t_send_delay
= send_delay
;
317 slave
->t_echo_delay
= echo_delay
;
319 rc
= fsi_slave_set_smode(slave
);
323 if (master
->link_config
)
324 master
->link_config(master
, link
,
326 slave
->t_echo_delay
);
328 return fsi_slave_report_and_clear_errors(slave
);
331 int fsi_slave_read(struct fsi_slave
*slave
, uint32_t addr
,
332 void *val
, size_t size
)
334 uint8_t id
= slave
->id
;
337 rc
= fsi_slave_calc_addr(slave
, &addr
, &id
);
341 for (i
= 0; i
< slave_retries
; i
++) {
342 rc
= fsi_master_read(slave
->master
, slave
->link
,
343 id
, addr
, val
, size
);
347 err_rc
= fsi_slave_handle_error(slave
, false, addr
, size
);
354 EXPORT_SYMBOL_GPL(fsi_slave_read
);
356 int fsi_slave_write(struct fsi_slave
*slave
, uint32_t addr
,
357 const void *val
, size_t size
)
359 uint8_t id
= slave
->id
;
362 rc
= fsi_slave_calc_addr(slave
, &addr
, &id
);
366 for (i
= 0; i
< slave_retries
; i
++) {
367 rc
= fsi_master_write(slave
->master
, slave
->link
,
368 id
, addr
, val
, size
);
372 err_rc
= fsi_slave_handle_error(slave
, true, addr
, size
);
379 EXPORT_SYMBOL_GPL(fsi_slave_write
);
381 int fsi_slave_claim_range(struct fsi_slave
*slave
,
382 uint32_t addr
, uint32_t size
)
384 if (addr
+ size
< addr
)
387 if (addr
+ size
> slave
->size
)
390 /* todo: check for overlapping claims */
393 EXPORT_SYMBOL_GPL(fsi_slave_claim_range
);
395 void fsi_slave_release_range(struct fsi_slave
*slave
,
396 uint32_t addr
, uint32_t size
)
399 EXPORT_SYMBOL_GPL(fsi_slave_release_range
);
401 static bool fsi_device_node_matches(struct device
*dev
, struct device_node
*np
,
402 uint32_t addr
, uint32_t size
)
406 if (of_property_read_reg(np
, 0, &paddr
, &psize
))
414 "node %pOF matches probed address, but not size (got 0x%llx, expected 0x%x)",
421 /* Find a matching node for the slave engine at @address, using @size bytes
422 * of space. Returns NULL if not found, or a matching node with refcount
423 * already incremented.
425 static struct device_node
*fsi_device_find_of_node(struct fsi_device
*dev
)
427 struct device_node
*parent
, *np
;
429 parent
= dev_of_node(&dev
->slave
->dev
);
433 for_each_child_of_node(parent
, np
) {
434 if (fsi_device_node_matches(&dev
->dev
, np
,
435 dev
->addr
, dev
->size
))
442 static int fsi_slave_scan(struct fsi_slave
*slave
)
444 uint32_t engine_addr
;
450 * We keep the peek mode and slave engines for the core; so start
451 * at the third slot in the configuration table. We also need to
452 * skip the chip ID entry at the start of the address space.
454 engine_addr
= engine_page_size
* 3;
455 for (i
= 2; i
< engine_page_size
/ sizeof(uint32_t); i
++) {
456 uint8_t slots
, version
, type
, crc
;
457 struct fsi_device
*dev
;
461 rc
= fsi_slave_read(slave
, (i
+ 1) * sizeof(data
),
462 &data
, sizeof(data
));
464 dev_warn(&slave
->dev
,
465 "error reading slave registers\n");
468 conf
= be32_to_cpu(data
);
470 crc
= crc4(0, conf
, 32);
472 dev_warn(&slave
->dev
,
473 "crc error in slave register at 0x%04x\n",
478 slots
= (conf
& FSI_SLAVE_CONF_SLOTS_MASK
)
479 >> FSI_SLAVE_CONF_SLOTS_SHIFT
;
480 version
= (conf
& FSI_SLAVE_CONF_VERSION_MASK
)
481 >> FSI_SLAVE_CONF_VERSION_SHIFT
;
482 type
= (conf
& FSI_SLAVE_CONF_TYPE_MASK
)
483 >> FSI_SLAVE_CONF_TYPE_SHIFT
;
486 * Unused address areas are marked by a zero type value; this
487 * skips the defined address areas
489 if (type
!= 0 && slots
!= 0) {
492 dev
= fsi_create_device(slave
);
497 dev
->engine_type
= type
;
498 dev
->version
= version
;
500 dev
->addr
= engine_addr
;
501 dev
->size
= slots
* engine_page_size
;
503 trace_fsi_dev_init(dev
);
506 "engine[%i]: type %x, version %x, addr %x size %x\n",
507 dev
->unit
, dev
->engine_type
, version
,
508 dev
->addr
, dev
->size
);
510 dev_set_name(&dev
->dev
, "%02x:%02x:%02x:%02x",
511 slave
->master
->idx
, slave
->link
,
513 dev
->dev
.of_node
= fsi_device_find_of_node(dev
);
515 rc
= device_register(&dev
->dev
);
517 dev_warn(&slave
->dev
, "add failed: %d\n", rc
);
518 put_device(&dev
->dev
);
522 engine_addr
+= slots
* engine_page_size
;
524 if (!(conf
& FSI_SLAVE_CONF_NEXT_MASK
))
531 static unsigned long aligned_access_size(size_t offset
, size_t count
)
533 unsigned long offset_unit
, count_unit
;
537 * 1. Access size must be less than or equal to the maximum access
538 * width or the highest power-of-two factor of offset
539 * 2. Access size must be less than or equal to the amount specified by
542 * The access width is optimal if we can calculate 1 to be strictly
543 * equal while still satisfying 2.
546 /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
547 offset_unit
= BIT(__builtin_ctzl(offset
| 4));
549 /* Find 2 by the top bit of count */
550 count_unit
= BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count
));
552 /* Constrain the maximum access width to the minimum of both criteria */
553 return BIT(__builtin_ctzl(offset_unit
| count_unit
));
556 static ssize_t
fsi_slave_sysfs_raw_read(struct file
*file
,
557 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
558 loff_t off
, size_t count
)
560 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
561 size_t total_len
, read_len
;
567 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
570 for (total_len
= 0; total_len
< count
; total_len
+= read_len
) {
571 read_len
= aligned_access_size(off
, count
- total_len
);
573 rc
= fsi_slave_read(slave
, off
, buf
+ total_len
, read_len
);
583 static ssize_t
fsi_slave_sysfs_raw_write(struct file
*file
,
584 struct kobject
*kobj
, struct bin_attribute
*attr
,
585 char *buf
, loff_t off
, size_t count
)
587 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
588 size_t total_len
, write_len
;
594 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
597 for (total_len
= 0; total_len
< count
; total_len
+= write_len
) {
598 write_len
= aligned_access_size(off
, count
- total_len
);
600 rc
= fsi_slave_write(slave
, off
, buf
+ total_len
, write_len
);
610 static const struct bin_attribute fsi_slave_raw_attr
= {
616 .read
= fsi_slave_sysfs_raw_read
,
617 .write
= fsi_slave_sysfs_raw_write
,
620 static void fsi_slave_release(struct device
*dev
)
622 struct fsi_slave
*slave
= to_fsi_slave(dev
);
624 fsi_free_minor(slave
->dev
.devt
);
625 of_node_put(dev
->of_node
);
629 static bool fsi_slave_node_matches(struct device_node
*np
,
630 int link
, uint8_t id
)
634 if (of_property_read_reg(np
, 0, &addr
, NULL
))
637 return addr
== (((u64
)link
<< 32) | id
);
640 /* Find a matching node for the slave at (link, id). Returns NULL if none
641 * found, or a matching node with refcount already incremented.
643 static struct device_node
*fsi_slave_find_of_node(struct fsi_master
*master
,
644 int link
, uint8_t id
)
646 struct device_node
*parent
, *np
;
648 parent
= dev_of_node(&master
->dev
);
652 for_each_child_of_node(parent
, np
) {
653 if (fsi_slave_node_matches(np
, link
, id
))
660 static ssize_t
cfam_read(struct file
*filep
, char __user
*buf
, size_t count
,
663 struct fsi_slave
*slave
= filep
->private_data
;
664 size_t total_len
, read_len
;
665 loff_t off
= *offset
;
671 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
674 for (total_len
= 0; total_len
< count
; total_len
+= read_len
) {
677 read_len
= min_t(size_t, count
, 4);
678 read_len
-= off
& 0x3;
680 rc
= fsi_slave_read(slave
, off
, &data
, read_len
);
683 rc
= copy_to_user(buf
+ total_len
, &data
, read_len
);
696 static ssize_t
cfam_write(struct file
*filep
, const char __user
*buf
,
697 size_t count
, loff_t
*offset
)
699 struct fsi_slave
*slave
= filep
->private_data
;
700 size_t total_len
, write_len
;
701 loff_t off
= *offset
;
708 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
711 for (total_len
= 0; total_len
< count
; total_len
+= write_len
) {
714 write_len
= min_t(size_t, count
, 4);
715 write_len
-= off
& 0x3;
717 rc
= copy_from_user(&data
, buf
+ total_len
, write_len
);
722 rc
= fsi_slave_write(slave
, off
, &data
, write_len
);
733 static loff_t
cfam_llseek(struct file
*file
, loff_t offset
, int whence
)
739 file
->f_pos
= offset
;
748 static int cfam_open(struct inode
*inode
, struct file
*file
)
750 struct fsi_slave
*slave
= container_of(inode
->i_cdev
, struct fsi_slave
, cdev
);
752 file
->private_data
= slave
;
757 static const struct file_operations cfam_fops
= {
758 .owner
= THIS_MODULE
,
760 .llseek
= cfam_llseek
,
765 static ssize_t
send_term_store(struct device
*dev
,
766 struct device_attribute
*attr
,
767 const char *buf
, size_t count
)
769 struct fsi_slave
*slave
= to_fsi_slave(dev
);
770 struct fsi_master
*master
= slave
->master
;
775 master
->term(master
, slave
->link
, slave
->id
);
779 static DEVICE_ATTR_WO(send_term
);
781 static ssize_t
slave_send_echo_show(struct device
*dev
,
782 struct device_attribute
*attr
,
785 struct fsi_slave
*slave
= to_fsi_slave(dev
);
787 return sprintf(buf
, "%u\n", slave
->t_send_delay
);
790 static ssize_t
slave_send_echo_store(struct device
*dev
,
791 struct device_attribute
*attr
, const char *buf
, size_t count
)
793 struct fsi_slave
*slave
= to_fsi_slave(dev
);
794 struct fsi_master
*master
= slave
->master
;
798 if (kstrtoul(buf
, 0, &val
) < 0)
801 if (val
< 1 || val
> 16)
804 if (!master
->link_config
)
807 /* Current HW mandates that send and echo delay are identical */
808 slave
->t_send_delay
= val
;
809 slave
->t_echo_delay
= val
;
811 rc
= fsi_slave_set_smode(slave
);
814 if (master
->link_config
)
815 master
->link_config(master
, slave
->link
,
817 slave
->t_echo_delay
);
822 static DEVICE_ATTR(send_echo_delays
, 0600,
823 slave_send_echo_show
, slave_send_echo_store
);
825 static ssize_t
chip_id_show(struct device
*dev
,
826 struct device_attribute
*attr
,
829 struct fsi_slave
*slave
= to_fsi_slave(dev
);
831 return sprintf(buf
, "%d\n", slave
->chip_id
);
834 static DEVICE_ATTR_RO(chip_id
);
836 static ssize_t
cfam_id_show(struct device
*dev
,
837 struct device_attribute
*attr
,
840 struct fsi_slave
*slave
= to_fsi_slave(dev
);
842 return sprintf(buf
, "0x%x\n", slave
->cfam_id
);
845 static DEVICE_ATTR_RO(cfam_id
);
847 static struct attribute
*cfam_attr
[] = {
848 &dev_attr_send_echo_delays
.attr
,
849 &dev_attr_chip_id
.attr
,
850 &dev_attr_cfam_id
.attr
,
851 &dev_attr_send_term
.attr
,
855 static const struct attribute_group cfam_attr_group
= {
859 static const struct attribute_group
*cfam_attr_groups
[] = {
864 static char *cfam_devnode(const struct device
*dev
, umode_t
*mode
,
865 kuid_t
*uid
, kgid_t
*gid
)
867 const struct fsi_slave
*slave
= to_fsi_slave(dev
);
869 #ifdef CONFIG_FSI_NEW_DEV_NODE
870 return kasprintf(GFP_KERNEL
, "fsi/cfam%d", slave
->cdev_idx
);
872 return kasprintf(GFP_KERNEL
, "cfam%d", slave
->cdev_idx
);
876 static const struct device_type cfam_type
= {
878 .devnode
= cfam_devnode
,
879 .groups
= cfam_attr_groups
882 static char *fsi_cdev_devnode(const struct device
*dev
, umode_t
*mode
,
883 kuid_t
*uid
, kgid_t
*gid
)
885 #ifdef CONFIG_FSI_NEW_DEV_NODE
886 return kasprintf(GFP_KERNEL
, "fsi/%s", dev_name(dev
));
888 return kasprintf(GFP_KERNEL
, "%s", dev_name(dev
));
892 const struct device_type fsi_cdev_type
= {
894 .devnode
= fsi_cdev_devnode
,
896 EXPORT_SYMBOL_GPL(fsi_cdev_type
);
898 /* Backward compatible /dev/ numbering in "old style" mode */
899 static int fsi_adjust_index(int index
)
901 #ifdef CONFIG_FSI_NEW_DEV_NODE
908 static int __fsi_get_new_minor(struct fsi_slave
*slave
, enum fsi_dev_type type
,
909 dev_t
*out_dev
, int *out_index
)
911 int cid
= slave
->chip_id
;
914 /* Check if we qualify for legacy numbering */
915 if (cid
>= 0 && cid
< 16 && type
< 4) {
917 * Try reserving the legacy number, which has 0 - 0x3f reserved
918 * in the ida range. cid goes up to 0xf and type contains two
919 * bits, so construct the id with the below two bit shift.
921 id
= (cid
<< 2) | type
;
922 id
= ida_alloc_range(&fsi_minor_ida
, id
, id
, GFP_KERNEL
);
924 *out_index
= fsi_adjust_index(cid
);
925 *out_dev
= fsi_base_dev
+ id
;
931 /* Fallback to non-legacy allocation */
933 id
= ida_alloc_range(&fsi_minor_ida
, FSI_CHAR_LEGACY_TOP
,
934 FSI_CHAR_MAX_DEVICES
- 1, GFP_KERNEL
);
937 *out_index
= fsi_adjust_index(id
);
938 *out_dev
= fsi_base_dev
+ id
;
942 static const char *const fsi_dev_type_names
[] = {
949 int fsi_get_new_minor(struct fsi_device
*fdev
, enum fsi_dev_type type
,
950 dev_t
*out_dev
, int *out_index
)
952 if (fdev
->dev
.of_node
) {
953 int aid
= of_alias_get_id(fdev
->dev
.of_node
, fsi_dev_type_names
[type
]);
956 /* Use the same scheme as the legacy numbers. */
957 int id
= (aid
<< 2) | type
;
959 id
= ida_alloc_range(&fsi_minor_ida
, id
, id
, GFP_KERNEL
);
962 *out_dev
= fsi_base_dev
+ id
;
971 return __fsi_get_new_minor(fdev
->slave
, type
, out_dev
, out_index
);
973 EXPORT_SYMBOL_GPL(fsi_get_new_minor
);
975 void fsi_free_minor(dev_t dev
)
977 ida_free(&fsi_minor_ida
, MINOR(dev
));
979 EXPORT_SYMBOL_GPL(fsi_free_minor
);
981 static int fsi_slave_init(struct fsi_master
*master
, int link
, uint8_t id
)
984 struct fsi_slave
*slave
;
986 __be32 data
, llmode
, slbus
;
989 /* Currently, we only support single slaves on a link, and use the
990 * full 23-bit address range
995 rc
= fsi_master_read(master
, link
, id
, 0, &data
, sizeof(data
));
997 dev_dbg(&master
->dev
, "can't read slave %02x:%02x %d\n",
1001 cfam_id
= be32_to_cpu(data
);
1003 crc
= crc4(0, cfam_id
, 32);
1005 trace_fsi_slave_invalid_cfam(master
, link
, cfam_id
);
1006 dev_warn(&master
->dev
, "slave %02x:%02x invalid cfam id CRC!\n",
1011 dev_dbg(&master
->dev
, "fsi: found chip %08x at %02x:%02x:%02x\n",
1012 cfam_id
, master
->idx
, link
, id
);
1014 /* If we're behind a master that doesn't provide a self-running bus
1015 * clock, put the slave into async mode
1017 if (master
->flags
& FSI_MASTER_FLAG_SWCLOCK
) {
1018 llmode
= cpu_to_be32(FSI_LLMODE_ASYNC
);
1019 rc
= fsi_master_write(master
, link
, id
,
1020 FSI_SLAVE_BASE
+ FSI_LLMODE
,
1021 &llmode
, sizeof(llmode
));
1023 dev_warn(&master
->dev
,
1024 "can't set llmode on slave:%02x:%02x %d\n",
1028 /* We can communicate with a slave; create the slave device and
1031 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
1035 dev_set_name(&slave
->dev
, "slave@%02x:%02x", link
, id
);
1036 slave
->dev
.type
= &cfam_type
;
1037 slave
->dev
.parent
= &master
->dev
;
1038 slave
->dev
.of_node
= fsi_slave_find_of_node(master
, link
, id
);
1039 slave
->dev
.release
= fsi_slave_release
;
1040 device_initialize(&slave
->dev
);
1041 slave
->cfam_id
= cfam_id
;
1042 slave
->master
= master
;
1045 slave
->size
= FSI_SLAVE_SIZE_23b
;
1046 slave
->t_send_delay
= 16;
1047 slave
->t_echo_delay
= 16;
1049 /* Get chip ID if any */
1050 slave
->chip_id
= -1;
1051 if (slave
->dev
.of_node
) {
1053 if (!of_property_read_u32(slave
->dev
.of_node
, "chip-id", &prop
))
1054 slave
->chip_id
= prop
;
1058 slbus
= cpu_to_be32(FSI_SLBUS_FORCE
);
1059 rc
= fsi_master_write(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SLBUS
,
1060 &slbus
, sizeof(slbus
));
1062 dev_warn(&master
->dev
,
1063 "can't set slbus on slave:%02x:%02x %d\n", link
, id
,
1066 rc
= fsi_slave_set_smode(slave
);
1068 dev_warn(&master
->dev
,
1069 "can't set smode on slave:%02x:%02x %d\n",
1074 /* Allocate a minor in the FSI space */
1075 rc
= __fsi_get_new_minor(slave
, fsi_dev_cfam
, &slave
->dev
.devt
,
1080 trace_fsi_slave_init(slave
);
1082 /* Create chardev for userspace access */
1083 cdev_init(&slave
->cdev
, &cfam_fops
);
1084 rc
= cdev_device_add(&slave
->cdev
, &slave
->dev
);
1086 dev_err(&slave
->dev
, "Error %d creating slave device\n", rc
);
1090 /* Now that we have the cdev registered with the core, any fatal
1091 * failures beyond this point will need to clean up through
1092 * cdev_device_del(). Fortunately though, nothing past here is fatal.
1095 if (master
->link_config
)
1096 master
->link_config(master
, link
,
1097 slave
->t_send_delay
,
1098 slave
->t_echo_delay
);
1100 /* Legacy raw file -> to be removed */
1101 rc
= device_create_bin_file(&slave
->dev
, &fsi_slave_raw_attr
);
1103 dev_warn(&slave
->dev
, "failed to create raw attr: %d\n", rc
);
1106 rc
= fsi_slave_scan(slave
);
1108 dev_dbg(&master
->dev
, "failed during slave scan with: %d\n",
1114 fsi_free_minor(slave
->dev
.devt
);
1116 of_node_put(slave
->dev
.of_node
);
1121 /* FSI master support */
1122 static int fsi_check_access(uint32_t addr
, size_t size
)
1127 } else if (size
== 2) {
1130 } else if (size
!= 1)
1136 static int fsi_master_read(struct fsi_master
*master
, int link
,
1137 uint8_t slave_id
, uint32_t addr
, void *val
, size_t size
)
1141 trace_fsi_master_read(master
, link
, slave_id
, addr
, size
);
1143 rc
= fsi_check_access(addr
, size
);
1145 rc
= master
->read(master
, link
, slave_id
, addr
, val
, size
);
1147 trace_fsi_master_rw_result(master
, link
, slave_id
, addr
, size
,
1153 static int fsi_master_write(struct fsi_master
*master
, int link
,
1154 uint8_t slave_id
, uint32_t addr
, const void *val
, size_t size
)
1158 trace_fsi_master_write(master
, link
, slave_id
, addr
, size
, val
);
1160 rc
= fsi_check_access(addr
, size
);
1162 rc
= master
->write(master
, link
, slave_id
, addr
, val
, size
);
1164 trace_fsi_master_rw_result(master
, link
, slave_id
, addr
, size
,
1170 static int fsi_master_link_disable(struct fsi_master
*master
, int link
)
1172 if (master
->link_enable
)
1173 return master
->link_enable(master
, link
, false);
1178 static int fsi_master_link_enable(struct fsi_master
*master
, int link
)
1180 if (master
->link_enable
)
1181 return master
->link_enable(master
, link
, true);
1187 * Issue a break command on this link
1189 static int fsi_master_break(struct fsi_master
*master
, int link
)
1193 trace_fsi_master_break(master
, link
);
1195 if (master
->send_break
)
1196 rc
= master
->send_break(master
, link
);
1197 if (master
->link_config
)
1198 master
->link_config(master
, link
, 16, 16);
1203 static int fsi_master_scan(struct fsi_master
*master
)
1207 trace_fsi_master_scan(master
, true);
1208 for (link
= 0; link
< master
->n_links
; link
++) {
1209 rc
= fsi_master_link_enable(master
, link
);
1211 dev_dbg(&master
->dev
,
1212 "enable link %d failed: %d\n", link
, rc
);
1215 rc
= fsi_master_break(master
, link
);
1217 fsi_master_link_disable(master
, link
);
1218 dev_dbg(&master
->dev
,
1219 "break to link %d failed: %d\n", link
, rc
);
1223 rc
= fsi_slave_init(master
, link
, 0);
1225 fsi_master_link_disable(master
, link
);
1231 static int fsi_slave_remove_device(struct device
*dev
, void *arg
)
1233 device_unregister(dev
);
1237 static int fsi_master_remove_slave(struct device
*dev
, void *arg
)
1239 struct fsi_slave
*slave
= to_fsi_slave(dev
);
1241 device_for_each_child(dev
, NULL
, fsi_slave_remove_device
);
1242 cdev_device_del(&slave
->cdev
, &slave
->dev
);
1247 static void fsi_master_unscan(struct fsi_master
*master
)
1249 trace_fsi_master_scan(master
, false);
1250 device_for_each_child(&master
->dev
, NULL
, fsi_master_remove_slave
);
1253 int fsi_master_rescan(struct fsi_master
*master
)
1257 mutex_lock(&master
->scan_lock
);
1258 fsi_master_unscan(master
);
1259 rc
= fsi_master_scan(master
);
1260 mutex_unlock(&master
->scan_lock
);
1264 EXPORT_SYMBOL_GPL(fsi_master_rescan
);
1266 static ssize_t
master_rescan_store(struct device
*dev
,
1267 struct device_attribute
*attr
, const char *buf
, size_t count
)
1269 struct fsi_master
*master
= to_fsi_master(dev
);
1272 rc
= fsi_master_rescan(master
);
1279 static DEVICE_ATTR(rescan
, 0200, NULL
, master_rescan_store
);
1281 static ssize_t
master_break_store(struct device
*dev
,
1282 struct device_attribute
*attr
, const char *buf
, size_t count
)
1284 struct fsi_master
*master
= to_fsi_master(dev
);
1286 fsi_master_break(master
, 0);
1291 static DEVICE_ATTR(break, 0200, NULL
, master_break_store
);
1293 static struct attribute
*master_attrs
[] = {
1294 &dev_attr_break
.attr
,
1295 &dev_attr_rescan
.attr
,
1299 ATTRIBUTE_GROUPS(master
);
1301 static struct class fsi_master_class
= {
1302 .name
= "fsi-master",
1303 .dev_groups
= master_groups
,
1306 int fsi_master_register(struct fsi_master
*master
)
1309 struct device_node
*np
;
1311 mutex_init(&master
->scan_lock
);
1313 /* Alloc the requested index if it's non-zero */
1315 master
->idx
= ida_alloc_range(&master_ida
, master
->idx
,
1316 master
->idx
, GFP_KERNEL
);
1318 master
->idx
= ida_alloc(&master_ida
, GFP_KERNEL
);
1321 if (master
->idx
< 0)
1324 if (!dev_name(&master
->dev
))
1325 dev_set_name(&master
->dev
, "fsi%d", master
->idx
);
1327 master
->dev
.class = &fsi_master_class
;
1329 mutex_lock(&master
->scan_lock
);
1330 rc
= device_register(&master
->dev
);
1332 ida_free(&master_ida
, master
->idx
);
1336 np
= dev_of_node(&master
->dev
);
1337 if (!of_property_read_bool(np
, "no-scan-on-init")) {
1338 fsi_master_scan(master
);
1341 mutex_unlock(&master
->scan_lock
);
1344 EXPORT_SYMBOL_GPL(fsi_master_register
);
1346 void fsi_master_unregister(struct fsi_master
*master
)
1348 int idx
= master
->idx
;
1350 trace_fsi_master_unregister(master
);
1352 mutex_lock(&master
->scan_lock
);
1353 fsi_master_unscan(master
);
1354 master
->n_links
= 0;
1355 mutex_unlock(&master
->scan_lock
);
1357 device_unregister(&master
->dev
);
1358 ida_free(&master_ida
, idx
);
1360 EXPORT_SYMBOL_GPL(fsi_master_unregister
);
1362 /* FSI core & Linux bus type definitions */
1364 static int fsi_bus_match(struct device
*dev
, const struct device_driver
*drv
)
1366 struct fsi_device
*fsi_dev
= to_fsi_dev(dev
);
1367 const struct fsi_driver
*fsi_drv
= to_fsi_drv(drv
);
1368 const struct fsi_device_id
*id
;
1370 if (!fsi_drv
->id_table
)
1373 for (id
= fsi_drv
->id_table
; id
->engine_type
; id
++) {
1374 if (id
->engine_type
!= fsi_dev
->engine_type
)
1376 if (id
->version
== FSI_VERSION_ANY
||
1377 id
->version
== fsi_dev
->version
) {
1378 if (drv
->of_match_table
) {
1379 if (of_driver_match_device(dev
, drv
))
1390 int fsi_driver_register(struct fsi_driver
*fsi_drv
)
1394 if (!fsi_drv
->id_table
)
1397 return driver_register(&fsi_drv
->drv
);
1399 EXPORT_SYMBOL_GPL(fsi_driver_register
);
1401 void fsi_driver_unregister(struct fsi_driver
*fsi_drv
)
1403 driver_unregister(&fsi_drv
->drv
);
1405 EXPORT_SYMBOL_GPL(fsi_driver_unregister
);
1407 struct bus_type fsi_bus_type
= {
1409 .match
= fsi_bus_match
,
1411 EXPORT_SYMBOL_GPL(fsi_bus_type
);
1413 static int __init
fsi_init(void)
1417 rc
= alloc_chrdev_region(&fsi_base_dev
, 0, FSI_CHAR_MAX_DEVICES
, "fsi");
1420 rc
= bus_register(&fsi_bus_type
);
1424 rc
= class_register(&fsi_master_class
);
1431 bus_unregister(&fsi_bus_type
);
1433 unregister_chrdev_region(fsi_base_dev
, FSI_CHAR_MAX_DEVICES
);
1436 postcore_initcall(fsi_init
);
1438 static void fsi_exit(void)
1440 class_unregister(&fsi_master_class
);
1441 bus_unregister(&fsi_bus_type
);
1442 unregister_chrdev_region(fsi_base_dev
, FSI_CHAR_MAX_DEVICES
);
1443 ida_destroy(&fsi_minor_ida
);
1445 module_exit(fsi_exit
);
1446 module_param(discard_errors
, int, 0664);
1447 MODULE_DESCRIPTION("FSI core driver");
1448 MODULE_LICENSE("GPL");
1449 MODULE_PARM_DESC(discard_errors
, "Don't invoke error handling on bus accesses");