2 * A FSI master controller, using a simple GPIO bit-banging interface
5 #include <linux/crc4.h>
6 #include <linux/delay.h>
7 #include <linux/device.h>
9 #include <linux/gpio/consumer.h>
11 #include <linux/irqflags.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
17 #include "fsi-master.h"
19 #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
20 #define LAST_ADDR_INVALID 0x1
22 struct fsi_master_gpio
{
23 struct fsi_master master
;
25 struct mutex cmd_lock
; /* mutex for command ordering */
26 struct gpio_desc
*gpio_clk
;
27 struct gpio_desc
*gpio_data
;
28 struct gpio_desc
*gpio_trans
; /* Voltage translator */
29 struct gpio_desc
*gpio_enable
; /* FSI enable */
30 struct gpio_desc
*gpio_mux
; /* Mux control */
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/fsi_master_gpio.h>
41 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
48 static void clock_toggle(struct fsi_master_gpio
*master
, int count
)
52 for (i
= 0; i
< count
; i
++) {
53 if (!master
->no_delays
)
54 ndelay(FSI_GPIO_STD_DLY
);
55 gpiod_set_value(master
->gpio_clk
, 0);
56 if (!master
->no_delays
)
57 ndelay(FSI_GPIO_STD_DLY
);
58 gpiod_set_value(master
->gpio_clk
, 1);
62 static int sda_clock_in(struct fsi_master_gpio
*master
)
66 if (!master
->no_delays
)
67 ndelay(FSI_GPIO_STD_DLY
);
68 gpiod_set_value(master
->gpio_clk
, 0);
70 /* Dummy read to feed the synchronizers */
71 gpiod_get_value(master
->gpio_data
);
73 /* Actual data read */
74 in
= gpiod_get_value(master
->gpio_data
);
75 if (!master
->no_delays
)
76 ndelay(FSI_GPIO_STD_DLY
);
77 gpiod_set_value(master
->gpio_clk
, 1);
81 static void sda_out(struct fsi_master_gpio
*master
, int value
)
83 gpiod_set_value(master
->gpio_data
, value
);
86 static void set_sda_input(struct fsi_master_gpio
*master
)
88 gpiod_direction_input(master
->gpio_data
);
89 gpiod_set_value(master
->gpio_trans
, 0);
92 static void set_sda_output(struct fsi_master_gpio
*master
, int value
)
94 gpiod_set_value(master
->gpio_trans
, 1);
95 gpiod_direction_output(master
->gpio_data
, value
);
98 static void clock_zeros(struct fsi_master_gpio
*master
, int count
)
100 trace_fsi_master_gpio_clock_zeros(master
, count
);
101 set_sda_output(master
, 1);
102 clock_toggle(master
, count
);
105 static void echo_delay(struct fsi_master_gpio
*master
)
107 clock_zeros(master
, master
->t_echo_delay
);
111 static void serial_in(struct fsi_master_gpio
*master
, struct fsi_gpio_msg
*msg
,
116 set_sda_input(master
);
118 for (bit
= 0; bit
< num_bits
; bit
++) {
119 in_bit
= sda_clock_in(master
);
121 msg
->msg
|= ~in_bit
& 0x1; /* Data is active low */
123 msg
->bits
+= num_bits
;
125 trace_fsi_master_gpio_in(master
, num_bits
, msg
->msg
);
128 static void serial_out(struct fsi_master_gpio
*master
,
129 const struct fsi_gpio_msg
*cmd
)
132 uint64_t msg
= ~cmd
->msg
; /* Data is active low */
133 uint64_t sda_mask
= 0x1ULL
<< (cmd
->bits
- 1);
134 uint64_t last_bit
= ~0;
137 trace_fsi_master_gpio_out(master
, cmd
->bits
, cmd
->msg
);
140 dev_warn(master
->dev
, "trying to output 0 bits\n");
143 set_sda_output(master
, 0);
145 /* Send the start bit */
147 clock_toggle(master
, 1);
149 /* Send the message */
150 for (bit
= 0; bit
< cmd
->bits
; bit
++) {
151 next_bit
= (msg
& sda_mask
) >> (cmd
->bits
- 1);
152 if (last_bit
^ next_bit
) {
153 sda_out(master
, next_bit
);
156 clock_toggle(master
, 1);
161 static void msg_push_bits(struct fsi_gpio_msg
*msg
, uint64_t data
, int bits
)
164 msg
->msg
|= data
& ((1ull << bits
) - 1);
168 static void msg_push_crc(struct fsi_gpio_msg
*msg
)
173 top
= msg
->bits
& 0x3;
175 /* start bit, and any non-aligned top bits */
176 crc
= crc4(0, 1 << top
| msg
->msg
>> (msg
->bits
- top
), top
+ 1);
179 crc
= crc4(crc
, msg
->msg
, msg
->bits
- top
);
181 msg_push_bits(msg
, crc
, 4);
184 static bool check_same_address(struct fsi_master_gpio
*master
, int id
,
187 /* this will also handle LAST_ADDR_INVALID */
188 return master
->last_addr
== (((id
& 0x3) << 21) | (addr
& ~0x3));
191 static bool check_relative_address(struct fsi_master_gpio
*master
, int id
,
192 uint32_t addr
, uint32_t *rel_addrp
)
194 uint32_t last_addr
= master
->last_addr
;
197 if (last_addr
== LAST_ADDR_INVALID
)
200 /* We may be in 23-bit addressing mode, which uses the id as the
201 * top two address bits. So, if we're referencing a different ID,
202 * use absolute addresses.
204 if (((last_addr
>> 21) & 0x3) != id
)
207 /* remove the top two bits from any 23-bit addressing */
208 last_addr
&= (1 << 21) - 1;
210 /* We know that the addresses are limited to 21 bits, so this won't
211 * overflow the signed rel_addr */
212 rel_addr
= addr
- last_addr
;
213 if (rel_addr
> 255 || rel_addr
< -256)
216 *rel_addrp
= (uint32_t)rel_addr
;
221 static void last_address_update(struct fsi_master_gpio
*master
,
222 int id
, bool valid
, uint32_t addr
)
225 master
->last_addr
= LAST_ADDR_INVALID
;
227 master
->last_addr
= ((id
& 0x3) << 21) | (addr
& ~0x3);
231 * Encode an Absolute/Relative/Same Address command
233 static void build_ar_command(struct fsi_master_gpio
*master
,
234 struct fsi_gpio_msg
*cmd
, uint8_t id
,
235 uint32_t addr
, size_t size
, const void *data
)
237 int i
, addr_bits
, opcode_bits
;
245 /* we have 21 bits of address max */
246 addr
&= ((1 << 21) - 1);
248 /* cmd opcodes are variable length - SAME_AR is only two bits */
251 if (check_same_address(master
, id
, addr
)) {
252 /* we still address the byte offset within the word */
255 opcode
= FSI_CMD_SAME_AR
;
256 trace_fsi_master_gpio_cmd_same_addr(master
);
258 } else if (check_relative_address(master
, id
, addr
, &rel_addr
)) {
259 /* 8 bits plus sign */
262 opcode
= FSI_CMD_REL_AR
;
263 trace_fsi_master_gpio_cmd_rel_addr(master
, rel_addr
);
267 opcode
= FSI_CMD_ABS_AR
;
268 trace_fsi_master_gpio_cmd_abs_addr(master
, addr
);
272 * The read/write size is encoded in the lower bits of the address
273 * (as it must be naturally-aligned), and the following ds bit.
275 * size addr:1 addr:0 ds
281 ds
= size
> 1 ? 1 : 0;
286 msg_push_bits(cmd
, id
, 2);
287 msg_push_bits(cmd
, opcode
, opcode_bits
);
288 msg_push_bits(cmd
, write
? 0 : 1, 1);
289 msg_push_bits(cmd
, addr
, addr_bits
);
290 msg_push_bits(cmd
, ds
, 1);
291 for (i
= 0; write
&& i
< size
; i
++)
292 msg_push_bits(cmd
, ((uint8_t *)data
)[i
], 8);
297 static void build_dpoll_command(struct fsi_gpio_msg
*cmd
, uint8_t slave_id
)
302 msg_push_bits(cmd
, slave_id
, 2);
303 msg_push_bits(cmd
, FSI_CMD_DPOLL
, 3);
307 static void build_epoll_command(struct fsi_gpio_msg
*cmd
, uint8_t slave_id
)
312 msg_push_bits(cmd
, slave_id
, 2);
313 msg_push_bits(cmd
, FSI_CMD_EPOLL
, 3);
317 static void build_term_command(struct fsi_gpio_msg
*cmd
, uint8_t slave_id
)
322 msg_push_bits(cmd
, slave_id
, 2);
323 msg_push_bits(cmd
, FSI_CMD_TERM
, 6);
328 * Note: callers rely specifically on this returning -EAGAIN for
329 * a CRC error detected in the response. Use other error code
330 * for other situations. It will be converted to something else
331 * higher up the stack before it reaches userspace.
333 static int read_one_response(struct fsi_master_gpio
*master
,
334 uint8_t data_size
, struct fsi_gpio_msg
*msgp
, uint8_t *tagp
)
336 struct fsi_gpio_msg msg
;
342 local_irq_save(flags
);
344 /* wait for the start bit */
345 for (i
= 0; i
< FSI_MASTER_MTOE_COUNT
; i
++) {
348 serial_in(master
, &msg
, 1);
352 if (i
== FSI_MASTER_MTOE_COUNT
) {
354 "Master time out waiting for response\n");
355 local_irq_restore(flags
);
362 /* Read slave ID & response tag */
363 serial_in(master
, &msg
, 4);
367 /* If we have an ACK and we're expecting data, clock the data in too */
368 if (tag
== FSI_RESP_ACK
&& data_size
)
369 serial_in(master
, &msg
, data_size
* 8);
372 serial_in(master
, &msg
, FSI_CRC_SIZE
);
374 local_irq_restore(flags
);
376 /* we have a whole message now; check CRC */
378 crc
= crc4(crc
, msg
.msg
, msg
.bits
);
380 /* Check if it's all 1's, that probably means the host is off */
381 if (((~msg
.msg
) & ((1ull << msg
.bits
) - 1)) == 0)
383 dev_dbg(master
->dev
, "ERR response CRC msg: 0x%016llx (%d bits)\n",
396 static int issue_term(struct fsi_master_gpio
*master
, uint8_t slave
)
398 struct fsi_gpio_msg cmd
;
403 build_term_command(&cmd
, slave
);
405 local_irq_save(flags
);
406 serial_out(master
, &cmd
);
408 local_irq_restore(flags
);
410 rc
= read_one_response(master
, 0, NULL
, &tag
);
413 "TERM failed; lost communication with slave\n");
415 } else if (tag
!= FSI_RESP_ACK
) {
416 dev_err(master
->dev
, "TERM failed; response %d\n", tag
);
423 static int poll_for_response(struct fsi_master_gpio
*master
,
424 uint8_t slave
, uint8_t size
, void *data
)
426 struct fsi_gpio_msg response
, cmd
;
427 int busy_count
= 0, rc
, i
;
430 uint8_t *data_byte
= data
;
431 int crc_err_retries
= 0;
433 rc
= read_one_response(master
, size
, &response
, &tag
);
435 /* Handle retries on CRC errors */
437 /* Too many retries ? */
438 if (crc_err_retries
++ > FSI_CRC_ERR_RETRIES
) {
440 * Pass it up as a -EIO otherwise upper level will retry
441 * the whole command which isn't what we want here.
447 "CRC error retry %d\n", crc_err_retries
);
448 trace_fsi_master_gpio_crc_rsp_error(master
);
449 build_epoll_command(&cmd
, slave
);
450 local_irq_save(flags
);
451 clock_zeros(master
, FSI_MASTER_EPOLL_CLOCKS
);
452 serial_out(master
, &cmd
);
454 local_irq_restore(flags
);
462 uint64_t val
= response
.msg
;
463 /* clear crc & mask */
465 val
&= (1ull << (size
* 8)) - 1;
467 for (i
= 0; i
< size
; i
++) {
468 data_byte
[size
-i
-1] = val
;
475 * Its necessary to clock slave before issuing
476 * d-poll, not indicated in the hardware protocol
477 * spec. < 20 clocks causes slave to hang, 21 ok.
479 if (busy_count
++ < FSI_MASTER_MAX_BUSY
) {
480 build_dpoll_command(&cmd
, slave
);
481 local_irq_save(flags
);
482 clock_zeros(master
, FSI_MASTER_DPOLL_CLOCKS
);
483 serial_out(master
, &cmd
);
485 local_irq_restore(flags
);
488 dev_warn(master
->dev
,
489 "ERR slave is stuck in busy state, issuing TERM\n");
490 local_irq_save(flags
);
491 clock_zeros(master
, FSI_MASTER_DPOLL_CLOCKS
);
492 local_irq_restore(flags
);
493 issue_term(master
, slave
);
498 dev_dbg(master
->dev
, "ERRA received: 0x%x\n", (int)response
.msg
);
502 dev_dbg(master
->dev
, "ERRC received: 0x%x\n", (int)response
.msg
);
503 trace_fsi_master_gpio_crc_cmd_error(master
);
509 trace_fsi_master_gpio_poll_response_busy(master
, busy_count
);
512 * tSendDelay clocks, avoids signal reflections when switching
513 * from receive of response back to send of data.
515 local_irq_save(flags
);
516 clock_zeros(master
, master
->t_send_delay
);
517 local_irq_restore(flags
);
522 static int send_request(struct fsi_master_gpio
*master
,
523 struct fsi_gpio_msg
*cmd
)
527 if (master
->external_mode
)
530 local_irq_save(flags
);
531 serial_out(master
, cmd
);
533 local_irq_restore(flags
);
538 static int fsi_master_gpio_xfer(struct fsi_master_gpio
*master
, uint8_t slave
,
539 struct fsi_gpio_msg
*cmd
, size_t resp_len
, void *resp
)
541 int rc
= -EAGAIN
, retries
= 0;
543 while ((retries
++) < FSI_CRC_ERR_RETRIES
) {
544 rc
= send_request(master
, cmd
);
547 rc
= poll_for_response(master
, slave
, resp_len
, resp
);
551 dev_warn(master
->dev
, "ECRC retry %d\n", retries
);
553 /* Pace it a bit before retry */
560 static int fsi_master_gpio_read(struct fsi_master
*_master
, int link
,
561 uint8_t id
, uint32_t addr
, void *val
, size_t size
)
563 struct fsi_master_gpio
*master
= to_fsi_master_gpio(_master
);
564 struct fsi_gpio_msg cmd
;
570 mutex_lock(&master
->cmd_lock
);
571 build_ar_command(master
, &cmd
, id
, addr
, size
, NULL
);
572 rc
= fsi_master_gpio_xfer(master
, id
, &cmd
, size
, val
);
573 last_address_update(master
, id
, rc
== 0, addr
);
574 mutex_unlock(&master
->cmd_lock
);
579 static int fsi_master_gpio_write(struct fsi_master
*_master
, int link
,
580 uint8_t id
, uint32_t addr
, const void *val
, size_t size
)
582 struct fsi_master_gpio
*master
= to_fsi_master_gpio(_master
);
583 struct fsi_gpio_msg cmd
;
589 mutex_lock(&master
->cmd_lock
);
590 build_ar_command(master
, &cmd
, id
, addr
, size
, val
);
591 rc
= fsi_master_gpio_xfer(master
, id
, &cmd
, 0, NULL
);
592 last_address_update(master
, id
, rc
== 0, addr
);
593 mutex_unlock(&master
->cmd_lock
);
598 static int fsi_master_gpio_term(struct fsi_master
*_master
,
599 int link
, uint8_t id
)
601 struct fsi_master_gpio
*master
= to_fsi_master_gpio(_master
);
602 struct fsi_gpio_msg cmd
;
608 mutex_lock(&master
->cmd_lock
);
609 build_term_command(&cmd
, id
);
610 rc
= fsi_master_gpio_xfer(master
, id
, &cmd
, 0, NULL
);
611 last_address_update(master
, id
, false, 0);
612 mutex_unlock(&master
->cmd_lock
);
617 static int fsi_master_gpio_break(struct fsi_master
*_master
, int link
)
619 struct fsi_master_gpio
*master
= to_fsi_master_gpio(_master
);
625 trace_fsi_master_gpio_break(master
);
627 mutex_lock(&master
->cmd_lock
);
628 if (master
->external_mode
) {
629 mutex_unlock(&master
->cmd_lock
);
633 local_irq_save(flags
);
635 set_sda_output(master
, 1);
637 clock_toggle(master
, FSI_PRE_BREAK_CLOCKS
);
639 clock_toggle(master
, FSI_BREAK_CLOCKS
);
642 clock_toggle(master
, FSI_POST_BREAK_CLOCKS
);
644 local_irq_restore(flags
);
646 last_address_update(master
, 0, false, 0);
647 mutex_unlock(&master
->cmd_lock
);
649 /* Wait for logic reset to take effect */
655 static void fsi_master_gpio_init(struct fsi_master_gpio
*master
)
659 gpiod_direction_output(master
->gpio_mux
, 1);
660 gpiod_direction_output(master
->gpio_trans
, 1);
661 gpiod_direction_output(master
->gpio_enable
, 1);
662 gpiod_direction_output(master
->gpio_clk
, 1);
663 gpiod_direction_output(master
->gpio_data
, 1);
665 /* todo: evaluate if clocks can be reduced */
666 local_irq_save(flags
);
667 clock_zeros(master
, FSI_INIT_CLOCKS
);
668 local_irq_restore(flags
);
671 static void fsi_master_gpio_init_external(struct fsi_master_gpio
*master
)
673 gpiod_direction_output(master
->gpio_mux
, 0);
674 gpiod_direction_output(master
->gpio_trans
, 0);
675 gpiod_direction_output(master
->gpio_enable
, 1);
676 gpiod_direction_input(master
->gpio_clk
);
677 gpiod_direction_input(master
->gpio_data
);
680 static int fsi_master_gpio_link_enable(struct fsi_master
*_master
, int link
)
682 struct fsi_master_gpio
*master
= to_fsi_master_gpio(_master
);
688 mutex_lock(&master
->cmd_lock
);
689 if (!master
->external_mode
) {
690 gpiod_set_value(master
->gpio_enable
, 1);
693 mutex_unlock(&master
->cmd_lock
);
698 static int fsi_master_gpio_link_config(struct fsi_master
*_master
, int link
,
699 u8 t_send_delay
, u8 t_echo_delay
)
701 struct fsi_master_gpio
*master
= to_fsi_master_gpio(_master
);
706 mutex_lock(&master
->cmd_lock
);
707 master
->t_send_delay
= t_send_delay
;
708 master
->t_echo_delay
= t_echo_delay
;
709 mutex_unlock(&master
->cmd_lock
);
714 static ssize_t
external_mode_show(struct device
*dev
,
715 struct device_attribute
*attr
, char *buf
)
717 struct fsi_master_gpio
*master
= dev_get_drvdata(dev
);
719 return snprintf(buf
, PAGE_SIZE
- 1, "%u\n",
720 master
->external_mode
? 1 : 0);
723 static ssize_t
external_mode_store(struct device
*dev
,
724 struct device_attribute
*attr
, const char *buf
, size_t count
)
726 struct fsi_master_gpio
*master
= dev_get_drvdata(dev
);
731 err
= kstrtoul(buf
, 0, &val
);
735 external_mode
= !!val
;
737 mutex_lock(&master
->cmd_lock
);
739 if (external_mode
== master
->external_mode
) {
740 mutex_unlock(&master
->cmd_lock
);
744 master
->external_mode
= external_mode
;
745 if (master
->external_mode
)
746 fsi_master_gpio_init_external(master
);
748 fsi_master_gpio_init(master
);
750 mutex_unlock(&master
->cmd_lock
);
752 fsi_master_rescan(&master
->master
);
757 static DEVICE_ATTR(external_mode
, 0664,
758 external_mode_show
, external_mode_store
);
760 static void fsi_master_gpio_release(struct device
*dev
)
762 struct fsi_master_gpio
*master
= to_fsi_master_gpio(dev_to_fsi_master(dev
));
764 of_node_put(dev_of_node(master
->dev
));
769 static int fsi_master_gpio_probe(struct platform_device
*pdev
)
771 struct fsi_master_gpio
*master
;
772 struct gpio_desc
*gpio
;
775 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
779 master
->dev
= &pdev
->dev
;
780 master
->master
.dev
.parent
= master
->dev
;
781 master
->master
.dev
.of_node
= of_node_get(dev_of_node(master
->dev
));
782 master
->master
.dev
.release
= fsi_master_gpio_release
;
783 master
->last_addr
= LAST_ADDR_INVALID
;
785 gpio
= devm_gpiod_get(&pdev
->dev
, "clock", 0);
787 dev_err(&pdev
->dev
, "failed to get clock gpio\n");
791 master
->gpio_clk
= gpio
;
793 gpio
= devm_gpiod_get(&pdev
->dev
, "data", 0);
795 dev_err(&pdev
->dev
, "failed to get data gpio\n");
799 master
->gpio_data
= gpio
;
802 gpio
= devm_gpiod_get_optional(&pdev
->dev
, "trans", 0);
804 dev_err(&pdev
->dev
, "failed to get trans gpio\n");
808 master
->gpio_trans
= gpio
;
810 gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable", 0);
812 dev_err(&pdev
->dev
, "failed to get enable gpio\n");
816 master
->gpio_enable
= gpio
;
818 gpio
= devm_gpiod_get_optional(&pdev
->dev
, "mux", 0);
820 dev_err(&pdev
->dev
, "failed to get mux gpio\n");
824 master
->gpio_mux
= gpio
;
827 * Check if GPIO block is slow enought that no extra delays
828 * are necessary. This improves performance on ast2500 by
829 * an order of magnitude.
831 master
->no_delays
= device_property_present(&pdev
->dev
, "no-gpio-delays");
833 /* Default FSI command delays */
834 master
->t_send_delay
= FSI_SEND_DELAY_CLOCKS
;
835 master
->t_echo_delay
= FSI_ECHO_DELAY_CLOCKS
;
837 master
->master
.n_links
= 1;
838 master
->master
.flags
= FSI_MASTER_FLAG_SWCLOCK
;
839 master
->master
.read
= fsi_master_gpio_read
;
840 master
->master
.write
= fsi_master_gpio_write
;
841 master
->master
.term
= fsi_master_gpio_term
;
842 master
->master
.send_break
= fsi_master_gpio_break
;
843 master
->master
.link_enable
= fsi_master_gpio_link_enable
;
844 master
->master
.link_config
= fsi_master_gpio_link_config
;
845 platform_set_drvdata(pdev
, master
);
846 mutex_init(&master
->cmd_lock
);
848 fsi_master_gpio_init(master
);
850 rc
= device_create_file(&pdev
->dev
, &dev_attr_external_mode
);
854 rc
= fsi_master_register(&master
->master
);
856 device_remove_file(&pdev
->dev
, &dev_attr_external_mode
);
857 put_device(&master
->master
.dev
);
868 static int fsi_master_gpio_remove(struct platform_device
*pdev
)
870 struct fsi_master_gpio
*master
= platform_get_drvdata(pdev
);
872 device_remove_file(&pdev
->dev
, &dev_attr_external_mode
);
874 fsi_master_unregister(&master
->master
);
879 static const struct of_device_id fsi_master_gpio_match
[] = {
880 { .compatible
= "fsi-master-gpio" },
884 static struct platform_driver fsi_master_gpio_driver
= {
886 .name
= "fsi-master-gpio",
887 .of_match_table
= fsi_master_gpio_match
,
889 .probe
= fsi_master_gpio_probe
,
890 .remove
= fsi_master_gpio_remove
,
893 module_platform_driver(fsi_master_gpio_driver
);
894 MODULE_LICENSE("GPL");