1 // SPDX-License-Identifier: GPL-2.0-only
3 * A FSI master controller, using a simple GPIO bit-banging interface
6 #include <linux/crc4.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
12 #include <linux/irqflags.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #include "fsi-master.h"
20 #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
21 #define LAST_ADDR_INVALID 0x1
23 struct fsi_master_gpio {
24 struct fsi_master master;
26 struct mutex cmd_lock; /* mutex for command ordering */
27 struct gpio_desc *gpio_clk;
28 struct gpio_desc *gpio_data;
29 struct gpio_desc *gpio_trans; /* Voltage translator */
30 struct gpio_desc *gpio_enable; /* FSI enable */
31 struct gpio_desc *gpio_mux; /* Mux control */
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/fsi_master_gpio.h>
42 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
49 static void clock_toggle(struct fsi_master_gpio *master, int count)
53 for (i = 0; i < count; i++) {
54 if (!master->no_delays)
55 ndelay(FSI_GPIO_STD_DLY);
56 gpiod_set_value(master->gpio_clk, 0);
57 if (!master->no_delays)
58 ndelay(FSI_GPIO_STD_DLY);
59 gpiod_set_value(master->gpio_clk, 1);
63 static int sda_clock_in(struct fsi_master_gpio *master)
67 if (!master->no_delays)
68 ndelay(FSI_GPIO_STD_DLY);
69 gpiod_set_value(master->gpio_clk, 0);
71 /* Dummy read to feed the synchronizers */
72 gpiod_get_value(master->gpio_data);
74 /* Actual data read */
75 in = gpiod_get_value(master->gpio_data);
76 if (!master->no_delays)
77 ndelay(FSI_GPIO_STD_DLY);
78 gpiod_set_value(master->gpio_clk, 1);
82 static void sda_out(struct fsi_master_gpio *master, int value)
84 gpiod_set_value(master->gpio_data, value);
87 static void set_sda_input(struct fsi_master_gpio *master)
89 gpiod_direction_input(master->gpio_data);
90 gpiod_set_value(master->gpio_trans, 0);
93 static void set_sda_output(struct fsi_master_gpio *master, int value)
95 gpiod_set_value(master->gpio_trans, 1);
96 gpiod_direction_output(master->gpio_data, value);
99 static void clock_zeros(struct fsi_master_gpio *master, int count)
101 trace_fsi_master_gpio_clock_zeros(master, count);
102 set_sda_output(master, 1);
103 clock_toggle(master, count);
106 static void echo_delay(struct fsi_master_gpio *master)
108 clock_zeros(master, master->t_echo_delay);
112 static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
117 set_sda_input(master);
119 for (bit = 0; bit < num_bits; bit++) {
120 in_bit = sda_clock_in(master);
122 msg->msg |= ~in_bit & 0x1; /* Data is active low */
124 msg->bits += num_bits;
126 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
129 static void serial_out(struct fsi_master_gpio *master,
130 const struct fsi_gpio_msg *cmd)
133 uint64_t msg = ~cmd->msg; /* Data is active low */
134 uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
135 uint64_t last_bit = ~0;
138 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
141 dev_warn(master->dev, "trying to output 0 bits\n");
144 set_sda_output(master, 0);
146 /* Send the start bit */
148 clock_toggle(master, 1);
150 /* Send the message */
151 for (bit = 0; bit < cmd->bits; bit++) {
152 next_bit = (msg & sda_mask) >> (cmd->bits - 1);
153 if (last_bit ^ next_bit) {
154 sda_out(master, next_bit);
157 clock_toggle(master, 1);
162 static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
165 msg->msg |= data & ((1ull << bits) - 1);
169 static void msg_push_crc(struct fsi_gpio_msg *msg)
174 top = msg->bits & 0x3;
176 /* start bit, and any non-aligned top bits */
177 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
180 crc = crc4(crc, msg->msg, msg->bits - top);
182 msg_push_bits(msg, crc, 4);
185 static bool check_same_address(struct fsi_master_gpio *master, int id,
188 /* this will also handle LAST_ADDR_INVALID */
189 return master->last_addr == (((id & 0x3) << 21) | (addr & ~0x3));
192 static bool check_relative_address(struct fsi_master_gpio *master, int id,
193 uint32_t addr, uint32_t *rel_addrp)
195 uint32_t last_addr = master->last_addr;
198 if (last_addr == LAST_ADDR_INVALID)
201 /* We may be in 23-bit addressing mode, which uses the id as the
202 * top two address bits. So, if we're referencing a different ID,
203 * use absolute addresses.
205 if (((last_addr >> 21) & 0x3) != id)
208 /* remove the top two bits from any 23-bit addressing */
209 last_addr &= (1 << 21) - 1;
211 /* We know that the addresses are limited to 21 bits, so this won't
212 * overflow the signed rel_addr */
213 rel_addr = addr - last_addr;
214 if (rel_addr > 255 || rel_addr < -256)
217 *rel_addrp = (uint32_t)rel_addr;
222 static void last_address_update(struct fsi_master_gpio *master,
223 int id, bool valid, uint32_t addr)
226 master->last_addr = LAST_ADDR_INVALID;
228 master->last_addr = ((id & 0x3) << 21) | (addr & ~0x3);
232 * Encode an Absolute/Relative/Same Address command
234 static void build_ar_command(struct fsi_master_gpio *master,
235 struct fsi_gpio_msg *cmd, uint8_t id,
236 uint32_t addr, size_t size, const void *data)
238 int i, addr_bits, opcode_bits;
246 /* we have 21 bits of address max */
247 addr &= ((1 << 21) - 1);
249 /* cmd opcodes are variable length - SAME_AR is only two bits */
252 if (check_same_address(master, id, addr)) {
253 /* we still address the byte offset within the word */
256 opcode = FSI_CMD_SAME_AR;
257 trace_fsi_master_gpio_cmd_same_addr(master);
259 } else if (check_relative_address(master, id, addr, &rel_addr)) {
260 /* 8 bits plus sign */
263 opcode = FSI_CMD_REL_AR;
264 trace_fsi_master_gpio_cmd_rel_addr(master, rel_addr);
268 opcode = FSI_CMD_ABS_AR;
269 trace_fsi_master_gpio_cmd_abs_addr(master, addr);
273 * The read/write size is encoded in the lower bits of the address
274 * (as it must be naturally-aligned), and the following ds bit.
276 * size addr:1 addr:0 ds
282 ds = size > 1 ? 1 : 0;
287 msg_push_bits(cmd, id, 2);
288 msg_push_bits(cmd, opcode, opcode_bits);
289 msg_push_bits(cmd, write ? 0 : 1, 1);
290 msg_push_bits(cmd, addr, addr_bits);
291 msg_push_bits(cmd, ds, 1);
292 for (i = 0; write && i < size; i++)
293 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
298 static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
303 msg_push_bits(cmd, slave_id, 2);
304 msg_push_bits(cmd, FSI_CMD_DPOLL, 3);
308 static void build_epoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
313 msg_push_bits(cmd, slave_id, 2);
314 msg_push_bits(cmd, FSI_CMD_EPOLL, 3);
318 static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
323 msg_push_bits(cmd, slave_id, 2);
324 msg_push_bits(cmd, FSI_CMD_TERM, 6);
329 * Note: callers rely specifically on this returning -EAGAIN for
330 * a CRC error detected in the response. Use other error code
331 * for other situations. It will be converted to something else
332 * higher up the stack before it reaches userspace.
334 static int read_one_response(struct fsi_master_gpio *master,
335 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
337 struct fsi_gpio_msg msg;
343 local_irq_save(flags);
345 /* wait for the start bit */
346 for (i = 0; i < FSI_MASTER_MTOE_COUNT; i++) {
349 serial_in(master, &msg, 1);
353 if (i == FSI_MASTER_MTOE_COUNT) {
355 "Master time out waiting for response\n");
356 local_irq_restore(flags);
363 /* Read slave ID & response tag */
364 serial_in(master, &msg, 4);
368 /* If we have an ACK and we're expecting data, clock the data in too */
369 if (tag == FSI_RESP_ACK && data_size)
370 serial_in(master, &msg, data_size * 8);
373 serial_in(master, &msg, FSI_CRC_SIZE);
375 local_irq_restore(flags);
377 /* we have a whole message now; check CRC */
379 crc = crc4(crc, msg.msg, msg.bits);
381 /* Check if it's all 1's, that probably means the host is off */
382 if (((~msg.msg) & ((1ull << msg.bits) - 1)) == 0)
384 dev_dbg(master->dev, "ERR response CRC msg: 0x%016llx (%d bits)\n",
397 static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
399 struct fsi_gpio_msg cmd;
404 build_term_command(&cmd, slave);
406 local_irq_save(flags);
407 serial_out(master, &cmd);
409 local_irq_restore(flags);
411 rc = read_one_response(master, 0, NULL, &tag);
414 "TERM failed; lost communication with slave\n");
416 } else if (tag != FSI_RESP_ACK) {
417 dev_err(master->dev, "TERM failed; response %d\n", tag);
424 static int poll_for_response(struct fsi_master_gpio *master,
425 uint8_t slave, uint8_t size, void *data)
427 struct fsi_gpio_msg response, cmd;
428 int busy_count = 0, rc, i;
431 uint8_t *data_byte = data;
432 int crc_err_retries = 0;
434 rc = read_one_response(master, size, &response, &tag);
436 /* Handle retries on CRC errors */
438 /* Too many retries ? */
439 if (crc_err_retries++ > FSI_CRC_ERR_RETRIES) {
441 * Pass it up as a -EIO otherwise upper level will retry
442 * the whole command which isn't what we want here.
448 "CRC error retry %d\n", crc_err_retries);
449 trace_fsi_master_gpio_crc_rsp_error(master);
450 build_epoll_command(&cmd, slave);
451 local_irq_save(flags);
452 clock_zeros(master, FSI_MASTER_EPOLL_CLOCKS);
453 serial_out(master, &cmd);
455 local_irq_restore(flags);
463 uint64_t val = response.msg;
464 /* clear crc & mask */
466 val &= (1ull << (size * 8)) - 1;
468 for (i = 0; i < size; i++) {
469 data_byte[size-i-1] = val;
476 * Its necessary to clock slave before issuing
477 * d-poll, not indicated in the hardware protocol
478 * spec. < 20 clocks causes slave to hang, 21 ok.
480 if (busy_count++ < FSI_MASTER_MAX_BUSY) {
481 build_dpoll_command(&cmd, slave);
482 local_irq_save(flags);
483 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
484 serial_out(master, &cmd);
486 local_irq_restore(flags);
489 dev_warn(master->dev,
490 "ERR slave is stuck in busy state, issuing TERM\n");
491 local_irq_save(flags);
492 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
493 local_irq_restore(flags);
494 issue_term(master, slave);
499 dev_dbg(master->dev, "ERRA received: 0x%x\n", (int)response.msg);
503 dev_dbg(master->dev, "ERRC received: 0x%x\n", (int)response.msg);
504 trace_fsi_master_gpio_crc_cmd_error(master);
510 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
513 * tSendDelay clocks, avoids signal reflections when switching
514 * from receive of response back to send of data.
516 local_irq_save(flags);
517 clock_zeros(master, master->t_send_delay);
518 local_irq_restore(flags);
523 static int send_request(struct fsi_master_gpio *master,
524 struct fsi_gpio_msg *cmd)
528 if (master->external_mode)
531 local_irq_save(flags);
532 serial_out(master, cmd);
534 local_irq_restore(flags);
539 static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
540 struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
542 int rc = -EAGAIN, retries = 0;
544 while ((retries++) < FSI_CRC_ERR_RETRIES) {
545 rc = send_request(master, cmd);
548 rc = poll_for_response(master, slave, resp_len, resp);
552 dev_warn(master->dev, "ECRC retry %d\n", retries);
554 /* Pace it a bit before retry */
561 static int fsi_master_gpio_read(struct fsi_master *_master, int link,
562 uint8_t id, uint32_t addr, void *val, size_t size)
564 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
565 struct fsi_gpio_msg cmd;
571 mutex_lock(&master->cmd_lock);
572 build_ar_command(master, &cmd, id, addr, size, NULL);
573 rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
574 last_address_update(master, id, rc == 0, addr);
575 mutex_unlock(&master->cmd_lock);
580 static int fsi_master_gpio_write(struct fsi_master *_master, int link,
581 uint8_t id, uint32_t addr, const void *val, size_t size)
583 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
584 struct fsi_gpio_msg cmd;
590 mutex_lock(&master->cmd_lock);
591 build_ar_command(master, &cmd, id, addr, size, val);
592 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
593 last_address_update(master, id, rc == 0, addr);
594 mutex_unlock(&master->cmd_lock);
599 static int fsi_master_gpio_term(struct fsi_master *_master,
600 int link, uint8_t id)
602 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
603 struct fsi_gpio_msg cmd;
609 mutex_lock(&master->cmd_lock);
610 build_term_command(&cmd, id);
611 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
612 last_address_update(master, id, false, 0);
613 mutex_unlock(&master->cmd_lock);
618 static int fsi_master_gpio_break(struct fsi_master *_master, int link)
620 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
626 trace_fsi_master_gpio_break(master);
628 mutex_lock(&master->cmd_lock);
629 if (master->external_mode) {
630 mutex_unlock(&master->cmd_lock);
634 local_irq_save(flags);
636 set_sda_output(master, 1);
638 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
640 clock_toggle(master, FSI_BREAK_CLOCKS);
643 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
645 local_irq_restore(flags);
647 last_address_update(master, 0, false, 0);
648 mutex_unlock(&master->cmd_lock);
650 /* Wait for logic reset to take effect */
656 static void fsi_master_gpio_init(struct fsi_master_gpio *master)
660 gpiod_direction_output(master->gpio_mux, 1);
661 gpiod_direction_output(master->gpio_trans, 1);
662 gpiod_direction_output(master->gpio_enable, 1);
663 gpiod_direction_output(master->gpio_clk, 1);
664 gpiod_direction_output(master->gpio_data, 1);
666 /* todo: evaluate if clocks can be reduced */
667 local_irq_save(flags);
668 clock_zeros(master, FSI_INIT_CLOCKS);
669 local_irq_restore(flags);
672 static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
674 gpiod_direction_output(master->gpio_mux, 0);
675 gpiod_direction_output(master->gpio_trans, 0);
676 gpiod_direction_output(master->gpio_enable, 1);
677 gpiod_direction_input(master->gpio_clk);
678 gpiod_direction_input(master->gpio_data);
681 static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link,
684 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
690 mutex_lock(&master->cmd_lock);
691 if (!master->external_mode) {
692 gpiod_set_value(master->gpio_enable, enable ? 1 : 0);
695 mutex_unlock(&master->cmd_lock);
700 static int fsi_master_gpio_link_config(struct fsi_master *_master, int link,
701 u8 t_send_delay, u8 t_echo_delay)
703 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
708 mutex_lock(&master->cmd_lock);
709 master->t_send_delay = t_send_delay;
710 master->t_echo_delay = t_echo_delay;
711 mutex_unlock(&master->cmd_lock);
716 static ssize_t external_mode_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
719 struct fsi_master_gpio *master = dev_get_drvdata(dev);
721 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
722 master->external_mode ? 1 : 0);
725 static ssize_t external_mode_store(struct device *dev,
726 struct device_attribute *attr, const char *buf, size_t count)
728 struct fsi_master_gpio *master = dev_get_drvdata(dev);
733 err = kstrtoul(buf, 0, &val);
737 external_mode = !!val;
739 mutex_lock(&master->cmd_lock);
741 if (external_mode == master->external_mode) {
742 mutex_unlock(&master->cmd_lock);
746 master->external_mode = external_mode;
747 if (master->external_mode)
748 fsi_master_gpio_init_external(master);
750 fsi_master_gpio_init(master);
752 mutex_unlock(&master->cmd_lock);
754 fsi_master_rescan(&master->master);
759 static DEVICE_ATTR(external_mode, 0664,
760 external_mode_show, external_mode_store);
762 static void fsi_master_gpio_release(struct device *dev)
764 struct fsi_master_gpio *master = to_fsi_master_gpio(dev_to_fsi_master(dev));
766 of_node_put(dev_of_node(master->dev));
771 static int fsi_master_gpio_probe(struct platform_device *pdev)
773 struct fsi_master_gpio *master;
774 struct gpio_desc *gpio;
777 master = kzalloc(sizeof(*master), GFP_KERNEL);
781 master->dev = &pdev->dev;
782 master->master.dev.parent = master->dev;
783 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
784 master->master.dev.release = fsi_master_gpio_release;
785 master->last_addr = LAST_ADDR_INVALID;
787 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
789 dev_err(&pdev->dev, "failed to get clock gpio\n");
793 master->gpio_clk = gpio;
795 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
797 dev_err(&pdev->dev, "failed to get data gpio\n");
801 master->gpio_data = gpio;
804 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
806 dev_err(&pdev->dev, "failed to get trans gpio\n");
810 master->gpio_trans = gpio;
812 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
814 dev_err(&pdev->dev, "failed to get enable gpio\n");
818 master->gpio_enable = gpio;
820 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
822 dev_err(&pdev->dev, "failed to get mux gpio\n");
826 master->gpio_mux = gpio;
829 * Check if GPIO block is slow enought that no extra delays
830 * are necessary. This improves performance on ast2500 by
831 * an order of magnitude.
833 master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
835 /* Default FSI command delays */
836 master->t_send_delay = FSI_SEND_DELAY_CLOCKS;
837 master->t_echo_delay = FSI_ECHO_DELAY_CLOCKS;
839 master->master.n_links = 1;
840 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
841 master->master.read = fsi_master_gpio_read;
842 master->master.write = fsi_master_gpio_write;
843 master->master.term = fsi_master_gpio_term;
844 master->master.send_break = fsi_master_gpio_break;
845 master->master.link_enable = fsi_master_gpio_link_enable;
846 master->master.link_config = fsi_master_gpio_link_config;
847 platform_set_drvdata(pdev, master);
848 mutex_init(&master->cmd_lock);
850 fsi_master_gpio_init(master);
852 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
856 rc = fsi_master_register(&master->master);
858 device_remove_file(&pdev->dev, &dev_attr_external_mode);
859 put_device(&master->master.dev);
870 static int fsi_master_gpio_remove(struct platform_device *pdev)
872 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
874 device_remove_file(&pdev->dev, &dev_attr_external_mode);
876 fsi_master_unregister(&master->master);
881 static const struct of_device_id fsi_master_gpio_match[] = {
882 { .compatible = "fsi-master-gpio" },
885 MODULE_DEVICE_TABLE(of, fsi_master_gpio_match);
887 static struct platform_driver fsi_master_gpio_driver = {
889 .name = "fsi-master-gpio",
890 .of_match_table = fsi_master_gpio_match,
892 .probe = fsi_master_gpio_probe,
893 .remove = fsi_master_gpio_remove,
896 module_platform_driver(fsi_master_gpio_driver);
897 MODULE_LICENSE("GPL");