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.
16 #include <linux/crc4.h>
17 #include <linux/device.h>
18 #include <linux/fsi.h>
19 #include <linux/idr.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
24 #include "fsi-master.h"
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/fsi.h>
29 #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
30 #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
31 #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
32 #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
33 #define FSI_SLAVE_CONF_VERSION_SHIFT 12
34 #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
35 #define FSI_SLAVE_CONF_TYPE_SHIFT 4
36 #define FSI_SLAVE_CONF_CRC_SHIFT 4
37 #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
38 #define FSI_SLAVE_CONF_DATA_BITS 28
40 #define FSI_PEEK_BASE 0x410
42 static const int engine_page_size
= 0x400;
44 #define FSI_SLAVE_BASE 0x800
47 * FSI slave engine control register offsets
49 #define FSI_SMODE 0x0 /* R/W: Mode register */
50 #define FSI_SISC 0x8 /* R/W: Interrupt condition */
51 #define FSI_SSTAT 0x14 /* R : Slave status */
52 #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
57 #define FSI_SMODE_WSC 0x80000000 /* Warm start done */
58 #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
59 #define FSI_SMODE_SID_SHIFT 24 /* ID shift */
60 #define FSI_SMODE_SID_MASK 3 /* ID Mask */
61 #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
62 #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
63 #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
64 #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
65 #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
66 #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
71 #define FSI_LLMODE_ASYNC 0x1
73 #define FSI_SLAVE_SIZE_23b 0x800000
75 static DEFINE_IDA(master_ida
);
79 struct fsi_master
*master
;
82 uint32_t size
; /* size of slave address space */
85 #define to_fsi_master(d) container_of(d, struct fsi_master, dev)
86 #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
88 static const int slave_retries
= 2;
89 static int discard_errors
;
91 static int fsi_master_read(struct fsi_master
*master
, int link
,
92 uint8_t slave_id
, uint32_t addr
, void *val
, size_t size
);
93 static int fsi_master_write(struct fsi_master
*master
, int link
,
94 uint8_t slave_id
, uint32_t addr
, const void *val
, size_t size
);
95 static int fsi_master_break(struct fsi_master
*master
, int link
);
98 * fsi_device_read() / fsi_device_write() / fsi_device_peek()
100 * FSI endpoint-device support
102 * Read / write / peek accessors for a client
105 * dev: Structure passed to FSI client device drivers on probe().
106 * addr: FSI address of given device. Client should pass in its base address
107 * plus desired offset to access its register space.
108 * val: For read/peek this is the value read at the specified address. For
109 * write this is value to write to the specified address.
110 * The data in val must be FSI bus endian (big endian).
111 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
112 * Addresses must be aligned on size boundaries or an error will result.
114 int fsi_device_read(struct fsi_device
*dev
, uint32_t addr
, void *val
,
117 if (addr
> dev
->size
|| size
> dev
->size
|| addr
> dev
->size
- size
)
120 return fsi_slave_read(dev
->slave
, dev
->addr
+ addr
, val
, size
);
122 EXPORT_SYMBOL_GPL(fsi_device_read
);
124 int fsi_device_write(struct fsi_device
*dev
, uint32_t addr
, const void *val
,
127 if (addr
> dev
->size
|| size
> dev
->size
|| addr
> dev
->size
- size
)
130 return fsi_slave_write(dev
->slave
, dev
->addr
+ addr
, val
, size
);
132 EXPORT_SYMBOL_GPL(fsi_device_write
);
134 int fsi_device_peek(struct fsi_device
*dev
, void *val
)
136 uint32_t addr
= FSI_PEEK_BASE
+ ((dev
->unit
- 2) * sizeof(uint32_t));
138 return fsi_slave_read(dev
->slave
, addr
, val
, sizeof(uint32_t));
141 static void fsi_device_release(struct device
*_device
)
143 struct fsi_device
*device
= to_fsi_dev(_device
);
148 static struct fsi_device
*fsi_create_device(struct fsi_slave
*slave
)
150 struct fsi_device
*dev
;
152 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
156 dev
->dev
.parent
= &slave
->dev
;
157 dev
->dev
.bus
= &fsi_bus_type
;
158 dev
->dev
.release
= fsi_device_release
;
163 /* FSI slave support */
164 static int fsi_slave_calc_addr(struct fsi_slave
*slave
, uint32_t *addrp
,
167 uint32_t addr
= *addrp
;
170 if (addr
> slave
->size
)
173 /* For 23 bit addressing, we encode the extra two bits in the slave
174 * id (and the slave's actual ID needs to be 0).
176 if (addr
> 0x1fffff) {
179 id
= (addr
>> 21) & 0x3;
188 static int fsi_slave_report_and_clear_errors(struct fsi_slave
*slave
)
190 struct fsi_master
*master
= slave
->master
;
198 rc
= fsi_master_read(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SISC
,
203 rc
= fsi_master_read(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SSTAT
,
204 &stat
, sizeof(stat
));
208 dev_info(&slave
->dev
, "status: 0x%08x, sisc: 0x%08x\n",
209 be32_to_cpu(stat
), be32_to_cpu(irq
));
211 /* clear interrupts */
212 return fsi_master_write(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SISC
,
216 static int fsi_slave_set_smode(struct fsi_master
*master
, int link
, int id
);
218 static int fsi_slave_handle_error(struct fsi_slave
*slave
, bool write
,
219 uint32_t addr
, size_t size
)
221 struct fsi_master
*master
= slave
->master
;
232 dev_dbg(&slave
->dev
, "handling error on %s to 0x%08x[%zd]",
233 write
? "write" : "read", addr
, size
);
235 /* try a simple clear of error conditions, which may fail if we've lost
236 * communication with the slave
238 rc
= fsi_slave_report_and_clear_errors(slave
);
242 /* send a TERM and retry */
244 rc
= master
->term(master
, link
, id
);
246 rc
= fsi_master_read(master
, link
, id
, 0,
249 rc
= fsi_slave_report_and_clear_errors(slave
);
255 /* getting serious, reset the slave via BREAK */
256 rc
= fsi_master_break(master
, link
);
260 rc
= fsi_slave_set_smode(master
, link
, id
);
264 return fsi_slave_report_and_clear_errors(slave
);
267 int fsi_slave_read(struct fsi_slave
*slave
, uint32_t addr
,
268 void *val
, size_t size
)
270 uint8_t id
= slave
->id
;
273 rc
= fsi_slave_calc_addr(slave
, &addr
, &id
);
277 for (i
= 0; i
< slave_retries
; i
++) {
278 rc
= fsi_master_read(slave
->master
, slave
->link
,
279 id
, addr
, val
, size
);
283 err_rc
= fsi_slave_handle_error(slave
, false, addr
, size
);
290 EXPORT_SYMBOL_GPL(fsi_slave_read
);
292 int fsi_slave_write(struct fsi_slave
*slave
, uint32_t addr
,
293 const void *val
, size_t size
)
295 uint8_t id
= slave
->id
;
298 rc
= fsi_slave_calc_addr(slave
, &addr
, &id
);
302 for (i
= 0; i
< slave_retries
; i
++) {
303 rc
= fsi_master_write(slave
->master
, slave
->link
,
304 id
, addr
, val
, size
);
308 err_rc
= fsi_slave_handle_error(slave
, true, addr
, size
);
315 EXPORT_SYMBOL_GPL(fsi_slave_write
);
317 extern int fsi_slave_claim_range(struct fsi_slave
*slave
,
318 uint32_t addr
, uint32_t size
)
320 if (addr
+ size
< addr
)
323 if (addr
+ size
> slave
->size
)
326 /* todo: check for overlapping claims */
329 EXPORT_SYMBOL_GPL(fsi_slave_claim_range
);
331 extern void fsi_slave_release_range(struct fsi_slave
*slave
,
332 uint32_t addr
, uint32_t size
)
335 EXPORT_SYMBOL_GPL(fsi_slave_release_range
);
337 static int fsi_slave_scan(struct fsi_slave
*slave
)
339 uint32_t engine_addr
;
346 * We keep the peek mode and slave engines for the core; so start
347 * at the third slot in the configuration table. We also need to
348 * skip the chip ID entry at the start of the address space.
350 engine_addr
= engine_page_size
* 3;
351 for (i
= 2; i
< engine_page_size
/ sizeof(uint32_t); i
++) {
352 uint8_t slots
, version
, type
, crc
;
353 struct fsi_device
*dev
;
355 rc
= fsi_slave_read(slave
, (i
+ 1) * sizeof(conf
),
356 &conf
, sizeof(conf
));
358 dev_warn(&slave
->dev
,
359 "error reading slave registers\n");
362 conf
= be32_to_cpu(conf
);
364 crc
= crc4(0, conf
, 32);
366 dev_warn(&slave
->dev
,
367 "crc error in slave register at 0x%04x\n",
372 slots
= (conf
& FSI_SLAVE_CONF_SLOTS_MASK
)
373 >> FSI_SLAVE_CONF_SLOTS_SHIFT
;
374 version
= (conf
& FSI_SLAVE_CONF_VERSION_MASK
)
375 >> FSI_SLAVE_CONF_VERSION_SHIFT
;
376 type
= (conf
& FSI_SLAVE_CONF_TYPE_MASK
)
377 >> FSI_SLAVE_CONF_TYPE_SHIFT
;
380 * Unused address areas are marked by a zero type value; this
381 * skips the defined address areas
383 if (type
!= 0 && slots
!= 0) {
386 dev
= fsi_create_device(slave
);
391 dev
->engine_type
= type
;
392 dev
->version
= version
;
394 dev
->addr
= engine_addr
;
395 dev
->size
= slots
* engine_page_size
;
398 "engine[%i]: type %x, version %x, addr %x size %x\n",
399 dev
->unit
, dev
->engine_type
, version
,
400 dev
->addr
, dev
->size
);
402 dev_set_name(&dev
->dev
, "%02x:%02x:%02x:%02x",
403 slave
->master
->idx
, slave
->link
,
406 rc
= device_register(&dev
->dev
);
408 dev_warn(&slave
->dev
, "add failed: %d\n", rc
);
409 put_device(&dev
->dev
);
413 engine_addr
+= slots
* engine_page_size
;
415 if (!(conf
& FSI_SLAVE_CONF_NEXT_MASK
))
422 static ssize_t
fsi_slave_sysfs_raw_read(struct file
*file
,
423 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
424 loff_t off
, size_t count
)
426 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
427 size_t total_len
, read_len
;
433 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
436 for (total_len
= 0; total_len
< count
; total_len
+= read_len
) {
437 read_len
= min_t(size_t, count
, 4);
438 read_len
-= off
& 0x3;
440 rc
= fsi_slave_read(slave
, off
, buf
+ total_len
, read_len
);
450 static ssize_t
fsi_slave_sysfs_raw_write(struct file
*file
,
451 struct kobject
*kobj
, struct bin_attribute
*attr
,
452 char *buf
, loff_t off
, size_t count
)
454 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
455 size_t total_len
, write_len
;
461 if (off
> 0xffffffff || count
> 0xffffffff || off
+ count
> 0xffffffff)
464 for (total_len
= 0; total_len
< count
; total_len
+= write_len
) {
465 write_len
= min_t(size_t, count
, 4);
466 write_len
-= off
& 0x3;
468 rc
= fsi_slave_write(slave
, off
, buf
+ total_len
, write_len
);
478 static const struct bin_attribute fsi_slave_raw_attr
= {
484 .read
= fsi_slave_sysfs_raw_read
,
485 .write
= fsi_slave_sysfs_raw_write
,
488 static ssize_t
fsi_slave_sysfs_term_write(struct file
*file
,
489 struct kobject
*kobj
, struct bin_attribute
*attr
,
490 char *buf
, loff_t off
, size_t count
)
492 struct fsi_slave
*slave
= to_fsi_slave(kobj_to_dev(kobj
));
493 struct fsi_master
*master
= slave
->master
;
498 master
->term(master
, slave
->link
, slave
->id
);
502 static const struct bin_attribute fsi_slave_term_attr
= {
508 .write
= fsi_slave_sysfs_term_write
,
511 /* Encode slave local bus echo delay */
512 static inline uint32_t fsi_smode_echodly(int x
)
514 return (x
& FSI_SMODE_ED_MASK
) << FSI_SMODE_ED_SHIFT
;
517 /* Encode slave local bus send delay */
518 static inline uint32_t fsi_smode_senddly(int x
)
520 return (x
& FSI_SMODE_SD_MASK
) << FSI_SMODE_SD_SHIFT
;
523 /* Encode slave local bus clock rate ratio */
524 static inline uint32_t fsi_smode_lbcrr(int x
)
526 return (x
& FSI_SMODE_LBCRR_MASK
) << FSI_SMODE_LBCRR_SHIFT
;
529 /* Encode slave ID */
530 static inline uint32_t fsi_smode_sid(int x
)
532 return (x
& FSI_SMODE_SID_MASK
) << FSI_SMODE_SID_SHIFT
;
535 static uint32_t fsi_slave_smode(int id
)
537 return FSI_SMODE_WSC
| FSI_SMODE_ECRC
539 | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf)
540 | fsi_smode_lbcrr(0x8);
543 static int fsi_slave_set_smode(struct fsi_master
*master
, int link
, int id
)
547 /* set our smode register with the slave ID field to 0; this enables
548 * extended slave addressing
550 smode
= fsi_slave_smode(id
);
551 smode
= cpu_to_be32(smode
);
553 return fsi_master_write(master
, link
, id
, FSI_SLAVE_BASE
+ FSI_SMODE
,
554 &smode
, sizeof(smode
));
557 static void fsi_slave_release(struct device
*dev
)
559 struct fsi_slave
*slave
= to_fsi_slave(dev
);
564 static int fsi_slave_init(struct fsi_master
*master
, int link
, uint8_t id
)
566 uint32_t chip_id
, llmode
;
567 struct fsi_slave
*slave
;
571 /* Currently, we only support single slaves on a link, and use the
572 * full 23-bit address range
577 rc
= fsi_master_read(master
, link
, id
, 0, &chip_id
, sizeof(chip_id
));
579 dev_dbg(&master
->dev
, "can't read slave %02x:%02x %d\n",
583 chip_id
= be32_to_cpu(chip_id
);
585 crc
= crc4(0, chip_id
, 32);
587 dev_warn(&master
->dev
, "slave %02x:%02x invalid chip id CRC!\n",
592 dev_info(&master
->dev
, "fsi: found chip %08x at %02x:%02x:%02x\n",
593 chip_id
, master
->idx
, link
, id
);
595 rc
= fsi_slave_set_smode(master
, link
, id
);
597 dev_warn(&master
->dev
,
598 "can't set smode on slave:%02x:%02x %d\n",
603 /* If we're behind a master that doesn't provide a self-running bus
604 * clock, put the slave into async mode
606 if (master
->flags
& FSI_MASTER_FLAG_SWCLOCK
) {
607 llmode
= cpu_to_be32(FSI_LLMODE_ASYNC
);
608 rc
= fsi_master_write(master
, link
, id
,
609 FSI_SLAVE_BASE
+ FSI_LLMODE
,
610 &llmode
, sizeof(llmode
));
612 dev_warn(&master
->dev
,
613 "can't set llmode on slave:%02x:%02x %d\n",
617 /* We can communicate with a slave; create the slave device and
620 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
624 slave
->master
= master
;
625 slave
->dev
.parent
= &master
->dev
;
626 slave
->dev
.release
= fsi_slave_release
;
629 slave
->size
= FSI_SLAVE_SIZE_23b
;
631 dev_set_name(&slave
->dev
, "slave@%02x:%02x", link
, id
);
632 rc
= device_register(&slave
->dev
);
634 dev_warn(&master
->dev
, "failed to create slave device: %d\n",
636 put_device(&slave
->dev
);
640 rc
= device_create_bin_file(&slave
->dev
, &fsi_slave_raw_attr
);
642 dev_warn(&slave
->dev
, "failed to create raw attr: %d\n", rc
);
644 rc
= device_create_bin_file(&slave
->dev
, &fsi_slave_term_attr
);
646 dev_warn(&slave
->dev
, "failed to create term attr: %d\n", rc
);
648 rc
= fsi_slave_scan(slave
);
650 dev_dbg(&master
->dev
, "failed during slave scan with: %d\n",
656 /* FSI master support */
657 static int fsi_check_access(uint32_t addr
, size_t size
)
659 if (size
!= 1 && size
!= 2 && size
!= 4)
662 if ((addr
& 0x3) != (size
& 0x3))
668 static int fsi_master_read(struct fsi_master
*master
, int link
,
669 uint8_t slave_id
, uint32_t addr
, void *val
, size_t size
)
673 trace_fsi_master_read(master
, link
, slave_id
, addr
, size
);
675 rc
= fsi_check_access(addr
, size
);
677 rc
= master
->read(master
, link
, slave_id
, addr
, val
, size
);
679 trace_fsi_master_rw_result(master
, link
, slave_id
, addr
, size
,
685 static int fsi_master_write(struct fsi_master
*master
, int link
,
686 uint8_t slave_id
, uint32_t addr
, const void *val
, size_t size
)
690 trace_fsi_master_write(master
, link
, slave_id
, addr
, size
, val
);
692 rc
= fsi_check_access(addr
, size
);
694 rc
= master
->write(master
, link
, slave_id
, addr
, val
, size
);
696 trace_fsi_master_rw_result(master
, link
, slave_id
, addr
, size
,
702 static int fsi_master_link_enable(struct fsi_master
*master
, int link
)
704 if (master
->link_enable
)
705 return master
->link_enable(master
, link
);
711 * Issue a break command on this link
713 static int fsi_master_break(struct fsi_master
*master
, int link
)
715 trace_fsi_master_break(master
, link
);
717 if (master
->send_break
)
718 return master
->send_break(master
, link
);
723 static int fsi_master_scan(struct fsi_master
*master
)
727 for (link
= 0; link
< master
->n_links
; link
++) {
728 rc
= fsi_master_link_enable(master
, link
);
730 dev_dbg(&master
->dev
,
731 "enable link %d failed: %d\n", link
, rc
);
734 rc
= fsi_master_break(master
, link
);
736 dev_dbg(&master
->dev
,
737 "break to link %d failed: %d\n", link
, rc
);
741 fsi_slave_init(master
, link
, 0);
747 static int fsi_slave_remove_device(struct device
*dev
, void *arg
)
749 device_unregister(dev
);
753 static int fsi_master_remove_slave(struct device
*dev
, void *arg
)
755 device_for_each_child(dev
, NULL
, fsi_slave_remove_device
);
756 device_unregister(dev
);
760 static void fsi_master_unscan(struct fsi_master
*master
)
762 device_for_each_child(&master
->dev
, NULL
, fsi_master_remove_slave
);
765 static ssize_t
master_rescan_store(struct device
*dev
,
766 struct device_attribute
*attr
, const char *buf
, size_t count
)
768 struct fsi_master
*master
= to_fsi_master(dev
);
771 fsi_master_unscan(master
);
772 rc
= fsi_master_scan(master
);
779 static DEVICE_ATTR(rescan
, 0200, NULL
, master_rescan_store
);
781 static ssize_t
master_break_store(struct device
*dev
,
782 struct device_attribute
*attr
, const char *buf
, size_t count
)
784 struct fsi_master
*master
= to_fsi_master(dev
);
786 fsi_master_break(master
, 0);
791 static DEVICE_ATTR(break, 0200, NULL
, master_break_store
);
793 int fsi_master_register(struct fsi_master
*master
)
800 master
->idx
= ida_simple_get(&master_ida
, 0, INT_MAX
, GFP_KERNEL
);
801 dev_set_name(&master
->dev
, "fsi%d", master
->idx
);
803 rc
= device_register(&master
->dev
);
805 ida_simple_remove(&master_ida
, master
->idx
);
809 rc
= device_create_file(&master
->dev
, &dev_attr_rescan
);
811 device_unregister(&master
->dev
);
812 ida_simple_remove(&master_ida
, master
->idx
);
816 rc
= device_create_file(&master
->dev
, &dev_attr_break
);
818 device_unregister(&master
->dev
);
819 ida_simple_remove(&master_ida
, master
->idx
);
823 fsi_master_scan(master
);
827 EXPORT_SYMBOL_GPL(fsi_master_register
);
829 void fsi_master_unregister(struct fsi_master
*master
)
831 if (master
->idx
>= 0) {
832 ida_simple_remove(&master_ida
, master
->idx
);
836 fsi_master_unscan(master
);
837 device_unregister(&master
->dev
);
839 EXPORT_SYMBOL_GPL(fsi_master_unregister
);
841 /* FSI core & Linux bus type definitions */
843 static int fsi_bus_match(struct device
*dev
, struct device_driver
*drv
)
845 struct fsi_device
*fsi_dev
= to_fsi_dev(dev
);
846 struct fsi_driver
*fsi_drv
= to_fsi_drv(drv
);
847 const struct fsi_device_id
*id
;
849 if (!fsi_drv
->id_table
)
852 for (id
= fsi_drv
->id_table
; id
->engine_type
; id
++) {
853 if (id
->engine_type
!= fsi_dev
->engine_type
)
855 if (id
->version
== FSI_VERSION_ANY
||
856 id
->version
== fsi_dev
->version
)
863 int fsi_driver_register(struct fsi_driver
*fsi_drv
)
867 if (!fsi_drv
->id_table
)
870 return driver_register(&fsi_drv
->drv
);
872 EXPORT_SYMBOL_GPL(fsi_driver_register
);
874 void fsi_driver_unregister(struct fsi_driver
*fsi_drv
)
876 driver_unregister(&fsi_drv
->drv
);
878 EXPORT_SYMBOL_GPL(fsi_driver_unregister
);
880 struct bus_type fsi_bus_type
= {
882 .match
= fsi_bus_match
,
884 EXPORT_SYMBOL_GPL(fsi_bus_type
);
886 static int __init
fsi_init(void)
888 return bus_register(&fsi_bus_type
);
890 postcore_initcall(fsi_init
);
892 static void fsi_exit(void)
894 bus_unregister(&fsi_bus_type
);
896 module_exit(fsi_exit
);
897 module_param(discard_errors
, int, 0664);
898 MODULE_LICENSE("GPL");
899 MODULE_PARM_DESC(discard_errors
, "Don't invoke error handling on bus accesses");