4 * Copyright (C) IBM Corporation 2016
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 * - s/chip_id/chip_loc
18 * - s/cfam/chip (cfam_id -> chip_id etc...)
21 #include <linux/crc4.h>
22 #include <linux/device.h>
23 #include <linux/fsi.h>
24 #include <linux/idr.h>
25 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/bitops.h>
29 #include <linux/cdev.h>
31 #include <linux/uaccess.h>
33 #include "fsi-master.h"
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/fsi.h>
38 #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
39 #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
40 #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
41 #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
42 #define FSI_SLAVE_CONF_VERSION_SHIFT 12
43 #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
44 #define FSI_SLAVE_CONF_TYPE_SHIFT 4
45 #define FSI_SLAVE_CONF_CRC_SHIFT 4
46 #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
47 #define FSI_SLAVE_CONF_DATA_BITS 28
49 #define FSI_PEEK_BASE 0x410
51 static const int engine_page_size
= 0x400;
53 #define FSI_SLAVE_BASE 0x800
56 * FSI slave engine control register offsets
58 #define FSI_SMODE 0x0 /* R/W: Mode register */
59 #define FSI_SISC 0x8 /* R/W: Interrupt condition */
60 #define FSI_SSTAT 0x14 /* R : Slave status */
61 #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
66 #define FSI_SMODE_WSC 0x80000000 /* Warm start done */
67 #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
68 #define FSI_SMODE_SID_SHIFT 24 /* ID shift */
69 #define FSI_SMODE_SID_MASK 3 /* ID Mask */
70 #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
71 #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
72 #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
73 #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
74 #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
75 #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
80 #define FSI_LLMODE_ASYNC 0x1
82 #define FSI_SLAVE_SIZE_23b 0x800000
84 static DEFINE_IDA(master_ida
);
88 struct fsi_master
*master
;
91 int id
; /* FSI address */
92 int link
; /* FSI link# */
95 uint32_t size
; /* size of slave address space */
100 #define to_fsi_master(d) container_of(d, struct fsi_master, dev)
101 #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
103 static const int slave_retries
= 2;
104 static int discard_errors
;
106 static dev_t fsi_base_dev
;
107 static DEFINE_IDA(fsi_minor_ida
);
108 #define FSI_CHAR_MAX_DEVICES 0x1000
110 /* Legacy /dev numbering: 4 devices per chip, 16 chips */
111 #define FSI_CHAR_LEGACY_TOP 64
113 static int fsi_master_read(struct fsi_master
*master
, int link
,
114 uint8_t slave_id
, uint32_t addr
, void *val
, size_t size
);
115 static int fsi_master_write(struct fsi_master
*master
, int link
,
116 uint8_t slave_id
, uint32_t addr
, const void *val
, size_t size
);
117 static int fsi_master_break(struct fsi_master
*master
, int link
);
120 * fsi_device_read() / fsi_device_write() / fsi_device_peek()
122 * FSI endpoint-device support
124 * Read / write / peek accessors for a client
127 * dev: Structure passed to FSI client device drivers on probe().
128 * addr: FSI address of given device. Client should pass in its base address
129 * plus desired offset to access its register space.
130 * val: For read/peek this is the value read at the specified address. For
131 * write this is value to write to the specified address.
132 * The data in val must be FSI bus endian (big endian).
133 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
134 * Addresses must be aligned on size boundaries or an error will result.
136 int fsi_device_read(struct fsi_device
*dev
, uint32_t addr
, void *val
,
139 if (addr
> dev
->size
|| size
> dev
->size
|| addr
> dev
->size
- size
)
142 return fsi_slave_read(dev
->slave
, dev
->addr
+ addr
, val
, size
);
144 EXPORT_SYMBOL_GPL(fsi_device_read
);
146 int fsi_device_write(struct fsi_device
*dev
, uint32_t addr
, const void *val
,
149 if (addr
> dev
->size
|| size
> dev
->size
|| addr
> dev
->size
- size
)
152 return fsi_slave_write(dev
->slave
, dev
->addr
+ addr
, val
, size
);
154 EXPORT_SYMBOL_GPL(fsi_device_write
);
156 int fsi_device_peek(struct fsi_device
*dev
, void *val
)
158 uint32_t addr
= FSI_PEEK_BASE
+ ((dev
->unit
- 2) * sizeof(uint32_t));
160 return fsi_slave_read(dev
->slave
, addr
, val
, sizeof(uint32_t));
163 static void fsi_device_release(struct device
*_device
)
165 struct fsi_device
*device
= to_fsi_dev(_device
);
167 of_node_put(device
->dev
.of_node
);
171 static struct fsi_device
*fsi_create_device(struct fsi_slave
*slave
)
173 struct fsi_device
*dev
;
175 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
179 dev
->dev
.parent
= &slave
->dev
;
180 dev
->dev
.bus
= &fsi_bus_type
;
181 dev
->dev
.release
= fsi_device_release
;
186 /* FSI slave support */
187 static int fsi_slave_calc_addr(struct fsi_slave
*slave
, uint32_t *addrp
,
190 uint32_t addr
= *addrp
;
193 if (addr
> slave
->size
)
196 /* For 23 bit addressing, we encode the extra two bits in the slave
197 * id (and the slave's actual ID needs to be 0).
199 if (addr
> 0x1fffff) {
202 id
= (addr
>> 21) & 0x3;
211 static int fsi_slave_report_and_clear_errors(struct fsi_slave
*slave
)
213 struct fsi_master
*master
= slave
->master
;
221 rc
= fsi_master_read(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SISC
,
226 rc
= fsi_master_read(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SSTAT
,
227 &stat
, sizeof(stat
));
231 dev_dbg(&slave
->dev
, "status: 0x%08x, sisc: 0x%08x\n",
232 be32_to_cpu(stat
), be32_to_cpu(irq
));
234 /* clear interrupts */
235 return fsi_master_write(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SISC
,
239 /* Encode slave local bus echo delay */
240 static inline uint32_t fsi_smode_echodly(int x
)
242 return (x
& FSI_SMODE_ED_MASK
) << FSI_SMODE_ED_SHIFT
;
245 /* Encode slave local bus send delay */
246 static inline uint32_t fsi_smode_senddly(int x
)
248 return (x
& FSI_SMODE_SD_MASK
) << FSI_SMODE_SD_SHIFT
;
251 /* Encode slave local bus clock rate ratio */
252 static inline uint32_t fsi_smode_lbcrr(int x
)
254 return (x
& FSI_SMODE_LBCRR_MASK
) << FSI_SMODE_LBCRR_SHIFT
;
257 /* Encode slave ID */
258 static inline uint32_t fsi_smode_sid(int x
)
260 return (x
& FSI_SMODE_SID_MASK
) << FSI_SMODE_SID_SHIFT
;
263 static uint32_t fsi_slave_smode(int id
, u8 t_senddly
, u8 t_echodly
)
265 return FSI_SMODE_WSC
| FSI_SMODE_ECRC
267 | fsi_smode_echodly(t_echodly
- 1) | fsi_smode_senddly(t_senddly
- 1)
268 | fsi_smode_lbcrr(0x8);
271 static int fsi_slave_set_smode(struct fsi_slave
*slave
)
276 /* set our smode register with the slave ID field to 0; this enables
277 * extended slave addressing
279 smode
= fsi_slave_smode(slave
->id
, slave
->t_send_delay
, slave
->t_echo_delay
);
280 data
= cpu_to_be32(smode
);
282 return fsi_master_write(slave
->master
, slave
->link
, slave
->id
,
283 FSI_SLAVE_BASE
+ FSI_SMODE
,
284 &data
, sizeof(data
));
287 static int fsi_slave_handle_error(struct fsi_slave
*slave
, bool write
,
288 uint32_t addr
, size_t size
)
290 struct fsi_master
*master
= slave
->master
;
293 uint8_t id
, send_delay
, echo_delay
;
301 dev_dbg(&slave
->dev
, "handling error on %s to 0x%08x[%zd]",
302 write
? "write" : "read", addr
, size
);
304 /* try a simple clear of error conditions, which may fail if we've lost
305 * communication with the slave
307 rc
= fsi_slave_report_and_clear_errors(slave
);
311 /* send a TERM and retry */
313 rc
= master
->term(master
, link
, id
);
315 rc
= fsi_master_read(master
, link
, id
, 0,
318 rc
= fsi_slave_report_and_clear_errors(slave
);
324 send_delay
= slave
->t_send_delay
;
325 echo_delay
= slave
->t_echo_delay
;
327 /* getting serious, reset the slave via BREAK */
328 rc
= fsi_master_break(master
, link
);
332 slave
->t_send_delay
= send_delay
;
333 slave
->t_echo_delay
= echo_delay
;
335 rc
= fsi_slave_set_smode(slave
);
339 if (master
->link_config
)
340 master
->link_config(master
, link
,
342 slave
->t_echo_delay
);
344 return fsi_slave_report_and_clear_errors(slave
);
347 int fsi_slave_read(struct fsi_slave
*slave
, uint32_t addr
,
348 void *val
, size_t size
)
350 uint8_t id
= slave
->id
;
353 rc
= fsi_slave_calc_addr(slave
, &addr
, &id
);
357 for (i
= 0; i
< slave_retries
; i
++) {
358 rc
= fsi_master_read(slave
->master
, slave
->link
,
359 id
, addr
, val
, size
);
363 err_rc
= fsi_slave_handle_error(slave
, false, addr
, size
);
370 EXPORT_SYMBOL_GPL(fsi_slave_read
);
372 int fsi_slave_write(struct fsi_slave
*slave
, uint32_t addr
,
373 const void *val
, size_t size
)
375 uint8_t id
= slave
->id
;
378 rc
= fsi_slave_calc_addr(slave
, &addr
, &id
);
382 for (i
= 0; i
< slave_retries
; i
++) {
383 rc
= fsi_master_write(slave
->master
, slave
->link
,
384 id
, addr
, val
, size
);
388 err_rc
= fsi_slave_handle_error(slave
, true, addr
, size
);
395 EXPORT_SYMBOL_GPL(fsi_slave_write
);
397 extern int fsi_slave_claim_range(struct fsi_slave
*slave
,
398 uint32_t addr
, uint32_t size
)
400 if (addr
+ size
< addr
)
403 if (addr
+ size
> slave
->size
)
406 /* todo: check for overlapping claims */
409 EXPORT_SYMBOL_GPL(fsi_slave_claim_range
);
411 extern void fsi_slave_release_range(struct fsi_slave
*slave
,
412 uint32_t addr
, uint32_t size
)
415 EXPORT_SYMBOL_GPL(fsi_slave_release_range
);
417 static bool fsi_device_node_matches(struct device
*dev
, struct device_node
*np
,
418 uint32_t addr
, uint32_t size
)
420 unsigned int len
, na
, ns
;
424 na
= of_n_addr_cells(np
);
425 ns
= of_n_size_cells(np
);
427 if (na
!= 1 || ns
!= 1)
430 prop
= of_get_property(np
, "reg", &len
);
431 if (!prop
|| len
!= 8)
434 if (of_read_number(prop
, 1) != addr
)
437 psize
= of_read_number(prop
+ 1, 1);
440 "node %s matches probed address, but not size (got 0x%x, expected 0x%x)",
441 of_node_full_name(np
), psize
, size
);
447 /* Find a matching node for the slave engine at @address, using @size bytes
448 * of space. Returns NULL if not found, or a matching node with refcount
449 * already incremented.
451 static struct device_node
*fsi_device_find_of_node(struct fsi_device
*dev
)
453 struct device_node
*parent
, *np
;
455 parent
= dev_of_node(&dev
->slave
->dev
);
459 for_each_child_of_node(parent
, np
) {
460 if (fsi_device_node_matches(&dev
->dev
, np
,
461 dev
->addr
, dev
->size
))
468 static int fsi_slave_scan(struct fsi_slave
*slave
)
470 uint32_t engine_addr
;
476 * We keep the peek mode and slave engines for the core; so start
477 * at the third slot in the configuration table. We also need to
478 * skip the chip ID entry at the start of the address space.
480 engine_addr
= engine_page_size
* 3;
481 for (i
= 2; i
< engine_page_size
/ sizeof(uint32_t); i
++) {
482 uint8_t slots
, version
, type
, crc
;
483 struct fsi_device
*dev
;
487 rc
= fsi_slave_read(slave
, (i
+ 1) * sizeof(data
),
488 &data
, sizeof(data
));
490 dev_warn(&slave
->dev
,
491 "error reading slave registers\n");
494 conf
= be32_to_cpu(data
);
496 crc
= crc4(0, conf
, 32);
498 dev_warn(&slave
->dev
,
499 "crc error in slave register at 0x%04x\n",
504 slots
= (conf
& FSI_SLAVE_CONF_SLOTS_MASK
)
505 >> FSI_SLAVE_CONF_SLOTS_SHIFT
;
506 version
= (conf
& FSI_SLAVE_CONF_VERSION_MASK
)
507 >> FSI_SLAVE_CONF_VERSION_SHIFT
;
508 type
= (conf
& FSI_SLAVE_CONF_TYPE_MASK
)
509 >> FSI_SLAVE_CONF_TYPE_SHIFT
;
512 * Unused address areas are marked by a zero type value; this
513 * skips the defined address areas
515 if (type
!= 0 && slots
!= 0) {
518 dev
= fsi_create_device(slave
);
523 dev
->engine_type
= type
;
524 dev
->version
= version
;
526 dev
->addr
= engine_addr
;
527 dev
->size
= slots
* engine_page_size
;
530 "engine[%i]: type %x, version %x, addr %x size %x\n",
531 dev
->unit
, dev
->engine_type
, version
,
532 dev
->addr
, dev
->size
);
534 dev_set_name(&dev
->dev
, "%02x:%02x:%02x:%02x",
535 slave
->master
->idx
, slave
->link
,
537 dev
->dev
.of_node
= fsi_device_find_of_node(dev
);
539 rc
= device_register(&dev
->dev
);
541 dev_warn(&slave
->dev
, "add failed: %d\n", rc
);
542 put_device(&dev
->dev
);
546 engine_addr
+= slots
* engine_page_size
;
548 if (!(conf
& FSI_SLAVE_CONF_NEXT_MASK
))
555 static unsigned long aligned_access_size(size_t offset
, size_t count
)
557 unsigned long offset_unit
, count_unit
;
561 * 1. Access size must be less than or equal to the maximum access
562 * width or the highest power-of-two factor of offset
563 * 2. Access size must be less than or equal to the amount specified by
566 * The access width is optimal if we can calculate 1 to be strictly
567 * equal while still satisfying 2.
570 /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
571 offset_unit
= BIT(__builtin_ctzl(offset
| 4));
573 /* Find 2 by the top bit of count */
574 count_unit
= BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count
));
576 /* Constrain the maximum access width to the minimum of both criteria */
577 return BIT(__builtin_ctzl(offset_unit
| count_unit
));
580 static ssize_t
fsi_slave_sysfs_raw_read(struct file
*file
,
581 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
582 loff_t off
, size_t count
)
584 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
585 size_t total_len
, read_len
;
591 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
594 for (total_len
= 0; total_len
< count
; total_len
+= read_len
) {
595 read_len
= aligned_access_size(off
, count
- total_len
);
597 rc
= fsi_slave_read(slave
, off
, buf
+ total_len
, read_len
);
607 static ssize_t
fsi_slave_sysfs_raw_write(struct file
*file
,
608 struct kobject
*kobj
, struct bin_attribute
*attr
,
609 char *buf
, loff_t off
, size_t count
)
611 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
612 size_t total_len
, write_len
;
618 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
621 for (total_len
= 0; total_len
< count
; total_len
+= write_len
) {
622 write_len
= aligned_access_size(off
, count
- total_len
);
624 rc
= fsi_slave_write(slave
, off
, buf
+ total_len
, write_len
);
634 static const struct bin_attribute fsi_slave_raw_attr
= {
640 .read
= fsi_slave_sysfs_raw_read
,
641 .write
= fsi_slave_sysfs_raw_write
,
644 static void fsi_slave_release(struct device
*dev
)
646 struct fsi_slave
*slave
= to_fsi_slave(dev
);
648 fsi_free_minor(slave
->dev
.devt
);
649 of_node_put(dev
->of_node
);
653 static bool fsi_slave_node_matches(struct device_node
*np
,
654 int link
, uint8_t id
)
656 unsigned int len
, na
, ns
;
659 na
= of_n_addr_cells(np
);
660 ns
= of_n_size_cells(np
);
662 /* Ensure we have the correct format for addresses and sizes in
665 if (na
!= 2 || ns
!= 0)
668 prop
= of_get_property(np
, "reg", &len
);
669 if (!prop
|| len
!= 8)
672 return (of_read_number(prop
, 1) == link
) &&
673 (of_read_number(prop
+ 1, 1) == id
);
676 /* Find a matching node for the slave at (link, id). Returns NULL if none
677 * found, or a matching node with refcount already incremented.
679 static struct device_node
*fsi_slave_find_of_node(struct fsi_master
*master
,
680 int link
, uint8_t id
)
682 struct device_node
*parent
, *np
;
684 parent
= dev_of_node(&master
->dev
);
688 for_each_child_of_node(parent
, np
) {
689 if (fsi_slave_node_matches(np
, link
, id
))
696 static ssize_t
cfam_read(struct file
*filep
, char __user
*buf
, size_t count
,
699 struct fsi_slave
*slave
= filep
->private_data
;
700 size_t total_len
, read_len
;
701 loff_t off
= *offset
;
707 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
710 for (total_len
= 0; total_len
< count
; total_len
+= read_len
) {
713 read_len
= min_t(size_t, count
, 4);
714 read_len
-= off
& 0x3;
716 rc
= fsi_slave_read(slave
, off
, &data
, read_len
);
719 rc
= copy_to_user(buf
+ total_len
, &data
, read_len
);
732 static ssize_t
cfam_write(struct file
*filep
, const char __user
*buf
,
733 size_t count
, loff_t
*offset
)
735 struct fsi_slave
*slave
= filep
->private_data
;
736 size_t total_len
, write_len
;
737 loff_t off
= *offset
;
744 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
747 for (total_len
= 0; total_len
< count
; total_len
+= write_len
) {
750 write_len
= min_t(size_t, count
, 4);
751 write_len
-= off
& 0x3;
753 rc
= copy_from_user(&data
, buf
+ total_len
, write_len
);
758 rc
= fsi_slave_write(slave
, off
, &data
, write_len
);
769 static loff_t
cfam_llseek(struct file
*file
, loff_t offset
, int whence
)
775 file
->f_pos
= offset
;
784 static int cfam_open(struct inode
*inode
, struct file
*file
)
786 struct fsi_slave
*slave
= container_of(inode
->i_cdev
, struct fsi_slave
, cdev
);
788 file
->private_data
= slave
;
793 static const struct file_operations cfam_fops
= {
794 .owner
= THIS_MODULE
,
796 .llseek
= cfam_llseek
,
801 static ssize_t
send_term_store(struct device
*dev
,
802 struct device_attribute
*attr
,
803 const char *buf
, size_t count
)
805 struct fsi_slave
*slave
= to_fsi_slave(dev
);
806 struct fsi_master
*master
= slave
->master
;
811 master
->term(master
, slave
->link
, slave
->id
);
815 static DEVICE_ATTR_WO(send_term
);
817 static ssize_t
slave_send_echo_show(struct device
*dev
,
818 struct device_attribute
*attr
,
821 struct fsi_slave
*slave
= to_fsi_slave(dev
);
823 return sprintf(buf
, "%u\n", slave
->t_send_delay
);
826 static ssize_t
slave_send_echo_store(struct device
*dev
,
827 struct device_attribute
*attr
, const char *buf
, size_t count
)
829 struct fsi_slave
*slave
= to_fsi_slave(dev
);
830 struct fsi_master
*master
= slave
->master
;
834 if (kstrtoul(buf
, 0, &val
) < 0)
837 if (val
< 1 || val
> 16)
840 if (!master
->link_config
)
843 /* Current HW mandates that send and echo delay are identical */
844 slave
->t_send_delay
= val
;
845 slave
->t_echo_delay
= val
;
847 rc
= fsi_slave_set_smode(slave
);
850 if (master
->link_config
)
851 master
->link_config(master
, slave
->link
,
853 slave
->t_echo_delay
);
858 static DEVICE_ATTR(send_echo_delays
, 0600,
859 slave_send_echo_show
, slave_send_echo_store
);
861 static ssize_t
chip_id_show(struct device
*dev
,
862 struct device_attribute
*attr
,
865 struct fsi_slave
*slave
= to_fsi_slave(dev
);
867 return sprintf(buf
, "%d\n", slave
->chip_id
);
870 static DEVICE_ATTR_RO(chip_id
);
872 static ssize_t
cfam_id_show(struct device
*dev
,
873 struct device_attribute
*attr
,
876 struct fsi_slave
*slave
= to_fsi_slave(dev
);
878 return sprintf(buf
, "0x%x\n", slave
->cfam_id
);
881 static DEVICE_ATTR_RO(cfam_id
);
883 static struct attribute
*cfam_attr
[] = {
884 &dev_attr_send_echo_delays
.attr
,
885 &dev_attr_chip_id
.attr
,
886 &dev_attr_cfam_id
.attr
,
887 &dev_attr_send_term
.attr
,
891 static const struct attribute_group cfam_attr_group
= {
895 static const struct attribute_group
*cfam_attr_groups
[] = {
900 static char *cfam_devnode(struct device
*dev
, umode_t
*mode
,
901 kuid_t
*uid
, kgid_t
*gid
)
903 struct fsi_slave
*slave
= to_fsi_slave(dev
);
905 #ifdef CONFIG_FSI_NEW_DEV_NODE
906 return kasprintf(GFP_KERNEL
, "fsi/cfam%d", slave
->cdev_idx
);
908 return kasprintf(GFP_KERNEL
, "cfam%d", slave
->cdev_idx
);
912 static const struct device_type cfam_type
= {
914 .devnode
= cfam_devnode
,
915 .groups
= cfam_attr_groups
918 static char *fsi_cdev_devnode(struct device
*dev
, umode_t
*mode
,
919 kuid_t
*uid
, kgid_t
*gid
)
921 #ifdef CONFIG_FSI_NEW_DEV_NODE
922 return kasprintf(GFP_KERNEL
, "fsi/%s", dev_name(dev
));
924 return kasprintf(GFP_KERNEL
, "%s", dev_name(dev
));
928 const struct device_type fsi_cdev_type
= {
930 .devnode
= fsi_cdev_devnode
,
932 EXPORT_SYMBOL_GPL(fsi_cdev_type
);
934 /* Backward compatible /dev/ numbering in "old style" mode */
935 static int fsi_adjust_index(int index
)
937 #ifdef CONFIG_FSI_NEW_DEV_NODE
944 static int __fsi_get_new_minor(struct fsi_slave
*slave
, enum fsi_dev_type type
,
945 dev_t
*out_dev
, int *out_index
)
947 int cid
= slave
->chip_id
;
950 /* Check if we qualify for legacy numbering */
951 if (cid
>= 0 && cid
< 16 && type
< 4) {
952 /* Try reserving the legacy number */
953 id
= (cid
<< 4) | type
;
954 id
= ida_simple_get(&fsi_minor_ida
, id
, id
+ 1, GFP_KERNEL
);
956 *out_index
= fsi_adjust_index(cid
);
957 *out_dev
= fsi_base_dev
+ id
;
963 /* Fallback to non-legacy allocation */
965 id
= ida_simple_get(&fsi_minor_ida
, FSI_CHAR_LEGACY_TOP
,
966 FSI_CHAR_MAX_DEVICES
, GFP_KERNEL
);
969 *out_index
= fsi_adjust_index(id
);
970 *out_dev
= fsi_base_dev
+ id
;
974 int fsi_get_new_minor(struct fsi_device
*fdev
, enum fsi_dev_type type
,
975 dev_t
*out_dev
, int *out_index
)
977 return __fsi_get_new_minor(fdev
->slave
, type
, out_dev
, out_index
);
979 EXPORT_SYMBOL_GPL(fsi_get_new_minor
);
981 void fsi_free_minor(dev_t dev
)
983 ida_simple_remove(&fsi_minor_ida
, MINOR(dev
));
985 EXPORT_SYMBOL_GPL(fsi_free_minor
);
987 static int fsi_slave_init(struct fsi_master
*master
, int link
, uint8_t id
)
990 struct fsi_slave
*slave
;
995 /* Currently, we only support single slaves on a link, and use the
996 * full 23-bit address range
1001 rc
= fsi_master_read(master
, link
, id
, 0, &data
, sizeof(data
));
1003 dev_dbg(&master
->dev
, "can't read slave %02x:%02x %d\n",
1007 cfam_id
= be32_to_cpu(data
);
1009 crc
= crc4(0, cfam_id
, 32);
1011 dev_warn(&master
->dev
, "slave %02x:%02x invalid cfam id CRC!\n",
1016 dev_dbg(&master
->dev
, "fsi: found chip %08x at %02x:%02x:%02x\n",
1017 cfam_id
, master
->idx
, link
, id
);
1019 /* If we're behind a master that doesn't provide a self-running bus
1020 * clock, put the slave into async mode
1022 if (master
->flags
& FSI_MASTER_FLAG_SWCLOCK
) {
1023 llmode
= cpu_to_be32(FSI_LLMODE_ASYNC
);
1024 rc
= fsi_master_write(master
, link
, id
,
1025 FSI_SLAVE_BASE
+ FSI_LLMODE
,
1026 &llmode
, sizeof(llmode
));
1028 dev_warn(&master
->dev
,
1029 "can't set llmode on slave:%02x:%02x %d\n",
1033 /* We can communicate with a slave; create the slave device and
1036 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
1040 dev_set_name(&slave
->dev
, "slave@%02x:%02x", link
, id
);
1041 slave
->dev
.type
= &cfam_type
;
1042 slave
->dev
.parent
= &master
->dev
;
1043 slave
->dev
.of_node
= fsi_slave_find_of_node(master
, link
, id
);
1044 slave
->dev
.release
= fsi_slave_release
;
1045 device_initialize(&slave
->dev
);
1046 slave
->cfam_id
= cfam_id
;
1047 slave
->master
= master
;
1050 slave
->size
= FSI_SLAVE_SIZE_23b
;
1051 slave
->t_send_delay
= 16;
1052 slave
->t_echo_delay
= 16;
1054 /* Get chip ID if any */
1055 slave
->chip_id
= -1;
1056 if (slave
->dev
.of_node
) {
1058 if (!of_property_read_u32(slave
->dev
.of_node
, "chip-id", &prop
))
1059 slave
->chip_id
= prop
;
1063 rc
= fsi_slave_set_smode(slave
);
1065 dev_warn(&master
->dev
,
1066 "can't set smode on slave:%02x:%02x %d\n",
1071 /* Allocate a minor in the FSI space */
1072 rc
= __fsi_get_new_minor(slave
, fsi_dev_cfam
, &slave
->dev
.devt
,
1077 /* Create chardev for userspace access */
1078 cdev_init(&slave
->cdev
, &cfam_fops
);
1079 rc
= cdev_device_add(&slave
->cdev
, &slave
->dev
);
1081 dev_err(&slave
->dev
, "Error %d creating slave device\n", rc
);
1085 /* Now that we have the cdev registered with the core, any fatal
1086 * failures beyond this point will need to clean up through
1087 * cdev_device_del(). Fortunately though, nothing past here is fatal.
1090 if (master
->link_config
)
1091 master
->link_config(master
, link
,
1092 slave
->t_send_delay
,
1093 slave
->t_echo_delay
);
1095 /* Legacy raw file -> to be removed */
1096 rc
= device_create_bin_file(&slave
->dev
, &fsi_slave_raw_attr
);
1098 dev_warn(&slave
->dev
, "failed to create raw attr: %d\n", rc
);
1101 rc
= fsi_slave_scan(slave
);
1103 dev_dbg(&master
->dev
, "failed during slave scan with: %d\n",
1109 fsi_free_minor(slave
->dev
.devt
);
1111 of_node_put(slave
->dev
.of_node
);
1116 /* FSI master support */
1117 static int fsi_check_access(uint32_t addr
, size_t size
)
1122 } else if (size
== 2) {
1125 } else if (size
!= 1)
1131 static int fsi_master_read(struct fsi_master
*master
, int link
,
1132 uint8_t slave_id
, uint32_t addr
, void *val
, size_t size
)
1136 trace_fsi_master_read(master
, link
, slave_id
, addr
, size
);
1138 rc
= fsi_check_access(addr
, size
);
1140 rc
= master
->read(master
, link
, slave_id
, addr
, val
, size
);
1142 trace_fsi_master_rw_result(master
, link
, slave_id
, addr
, size
,
1148 static int fsi_master_write(struct fsi_master
*master
, int link
,
1149 uint8_t slave_id
, uint32_t addr
, const void *val
, size_t size
)
1153 trace_fsi_master_write(master
, link
, slave_id
, addr
, size
, val
);
1155 rc
= fsi_check_access(addr
, size
);
1157 rc
= master
->write(master
, link
, slave_id
, addr
, val
, size
);
1159 trace_fsi_master_rw_result(master
, link
, slave_id
, addr
, size
,
1165 static int fsi_master_link_enable(struct fsi_master
*master
, int link
)
1167 if (master
->link_enable
)
1168 return master
->link_enable(master
, link
);
1174 * Issue a break command on this link
1176 static int fsi_master_break(struct fsi_master
*master
, int link
)
1180 trace_fsi_master_break(master
, link
);
1182 if (master
->send_break
)
1183 rc
= master
->send_break(master
, link
);
1184 if (master
->link_config
)
1185 master
->link_config(master
, link
, 16, 16);
1190 static int fsi_master_scan(struct fsi_master
*master
)
1194 for (link
= 0; link
< master
->n_links
; link
++) {
1195 rc
= fsi_master_link_enable(master
, link
);
1197 dev_dbg(&master
->dev
,
1198 "enable link %d failed: %d\n", link
, rc
);
1201 rc
= fsi_master_break(master
, link
);
1203 dev_dbg(&master
->dev
,
1204 "break to link %d failed: %d\n", link
, rc
);
1208 fsi_slave_init(master
, link
, 0);
1214 static int fsi_slave_remove_device(struct device
*dev
, void *arg
)
1216 device_unregister(dev
);
1220 static int fsi_master_remove_slave(struct device
*dev
, void *arg
)
1222 struct fsi_slave
*slave
= to_fsi_slave(dev
);
1224 device_for_each_child(dev
, NULL
, fsi_slave_remove_device
);
1225 cdev_device_del(&slave
->cdev
, &slave
->dev
);
1230 static void fsi_master_unscan(struct fsi_master
*master
)
1232 device_for_each_child(&master
->dev
, NULL
, fsi_master_remove_slave
);
1235 int fsi_master_rescan(struct fsi_master
*master
)
1239 mutex_lock(&master
->scan_lock
);
1240 fsi_master_unscan(master
);
1241 rc
= fsi_master_scan(master
);
1242 mutex_unlock(&master
->scan_lock
);
1246 EXPORT_SYMBOL_GPL(fsi_master_rescan
);
1248 static ssize_t
master_rescan_store(struct device
*dev
,
1249 struct device_attribute
*attr
, const char *buf
, size_t count
)
1251 struct fsi_master
*master
= to_fsi_master(dev
);
1254 rc
= fsi_master_rescan(master
);
1261 static DEVICE_ATTR(rescan
, 0200, NULL
, master_rescan_store
);
1263 static ssize_t
master_break_store(struct device
*dev
,
1264 struct device_attribute
*attr
, const char *buf
, size_t count
)
1266 struct fsi_master
*master
= to_fsi_master(dev
);
1268 fsi_master_break(master
, 0);
1273 static DEVICE_ATTR(break, 0200, NULL
, master_break_store
);
1275 int fsi_master_register(struct fsi_master
*master
)
1278 struct device_node
*np
;
1280 mutex_init(&master
->scan_lock
);
1281 master
->idx
= ida_simple_get(&master_ida
, 0, INT_MAX
, GFP_KERNEL
);
1282 dev_set_name(&master
->dev
, "fsi%d", master
->idx
);
1284 rc
= device_register(&master
->dev
);
1286 ida_simple_remove(&master_ida
, master
->idx
);
1290 rc
= device_create_file(&master
->dev
, &dev_attr_rescan
);
1292 device_del(&master
->dev
);
1293 ida_simple_remove(&master_ida
, master
->idx
);
1297 rc
= device_create_file(&master
->dev
, &dev_attr_break
);
1299 device_del(&master
->dev
);
1300 ida_simple_remove(&master_ida
, master
->idx
);
1304 np
= dev_of_node(&master
->dev
);
1305 if (!of_property_read_bool(np
, "no-scan-on-init")) {
1306 mutex_lock(&master
->scan_lock
);
1307 fsi_master_scan(master
);
1308 mutex_unlock(&master
->scan_lock
);
1313 EXPORT_SYMBOL_GPL(fsi_master_register
);
1315 void fsi_master_unregister(struct fsi_master
*master
)
1317 if (master
->idx
>= 0) {
1318 ida_simple_remove(&master_ida
, master
->idx
);
1322 mutex_lock(&master
->scan_lock
);
1323 fsi_master_unscan(master
);
1324 mutex_unlock(&master
->scan_lock
);
1325 device_unregister(&master
->dev
);
1327 EXPORT_SYMBOL_GPL(fsi_master_unregister
);
1329 /* FSI core & Linux bus type definitions */
1331 static int fsi_bus_match(struct device
*dev
, struct device_driver
*drv
)
1333 struct fsi_device
*fsi_dev
= to_fsi_dev(dev
);
1334 struct fsi_driver
*fsi_drv
= to_fsi_drv(drv
);
1335 const struct fsi_device_id
*id
;
1337 if (!fsi_drv
->id_table
)
1340 for (id
= fsi_drv
->id_table
; id
->engine_type
; id
++) {
1341 if (id
->engine_type
!= fsi_dev
->engine_type
)
1343 if (id
->version
== FSI_VERSION_ANY
||
1344 id
->version
== fsi_dev
->version
)
1351 int fsi_driver_register(struct fsi_driver
*fsi_drv
)
1355 if (!fsi_drv
->id_table
)
1358 return driver_register(&fsi_drv
->drv
);
1360 EXPORT_SYMBOL_GPL(fsi_driver_register
);
1362 void fsi_driver_unregister(struct fsi_driver
*fsi_drv
)
1364 driver_unregister(&fsi_drv
->drv
);
1366 EXPORT_SYMBOL_GPL(fsi_driver_unregister
);
1368 struct bus_type fsi_bus_type
= {
1370 .match
= fsi_bus_match
,
1372 EXPORT_SYMBOL_GPL(fsi_bus_type
);
1374 static int __init
fsi_init(void)
1378 rc
= alloc_chrdev_region(&fsi_base_dev
, 0, FSI_CHAR_MAX_DEVICES
, "fsi");
1381 rc
= bus_register(&fsi_bus_type
);
1387 unregister_chrdev_region(fsi_base_dev
, FSI_CHAR_MAX_DEVICES
);
1390 postcore_initcall(fsi_init
);
1392 static void fsi_exit(void)
1394 bus_unregister(&fsi_bus_type
);
1395 unregister_chrdev_region(fsi_base_dev
, FSI_CHAR_MAX_DEVICES
);
1396 ida_destroy(&fsi_minor_ida
);
1398 module_exit(fsi_exit
);
1399 module_param(discard_errors
, int, 0664);
1400 MODULE_LICENSE("GPL");
1401 MODULE_PARM_DESC(discard_errors
, "Don't invoke error handling on bus accesses");