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/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
17 #include "fsi-master.h"
19 #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
20 #define FSI_ECHO_DELAY_CLOCKS 16 /* Number clocks for echo delay */
21 #define FSI_PRE_BREAK_CLOCKS 50 /* Number clocks to prep for break */
22 #define FSI_BREAK_CLOCKS 256 /* Number of clocks to issue break */
23 #define FSI_POST_BREAK_CLOCKS 16000 /* Number clocks to set up cfam */
24 #define FSI_INIT_CLOCKS 5000 /* Clock out any old data */
25 #define FSI_GPIO_STD_DELAY 10 /* Standard GPIO delay in nS */
26 /* todo: adjust down as low as */
27 /* possible or eliminate */
28 #define FSI_GPIO_CMD_DPOLL 0x2
29 #define FSI_GPIO_CMD_TERM 0x3f
30 #define FSI_GPIO_CMD_ABS_AR 0x4
32 #define FSI_GPIO_DPOLL_CLOCKS 100 /* < 21 will cause slave to hang */
35 #define FSI_GPIO_ERR_BUSY 1 /* Slave stuck in busy state */
36 #define FSI_GPIO_RESP_ERRA 2 /* Any (misc) Error */
37 #define FSI_GPIO_RESP_ERRC 3 /* Slave reports master CRC error */
38 #define FSI_GPIO_MTOE 4 /* Master time out error */
39 #define FSI_GPIO_CRC_INVAL 5 /* Master reports slave CRC error */
41 /* Normal slave responses */
42 #define FSI_GPIO_RESP_BUSY 1
43 #define FSI_GPIO_RESP_ACK 0
44 #define FSI_GPIO_RESP_ACKD 4
46 #define FSI_GPIO_MAX_BUSY 100
47 #define FSI_GPIO_MTOE_COUNT 1000
48 #define FSI_GPIO_DRAIN_BITS 20
49 #define FSI_GPIO_CRC_SIZE 4
50 #define FSI_GPIO_MSG_ID_SIZE 2
51 #define FSI_GPIO_MSG_RESPID_SIZE 2
52 #define FSI_GPIO_PRIME_SLAVE_CLOCKS 20
54 struct fsi_master_gpio {
55 struct fsi_master master;
57 struct mutex cmd_lock; /* mutex for command ordering */
58 spinlock_t bit_lock; /* lock for clocking bits out */
59 struct gpio_desc *gpio_clk;
60 struct gpio_desc *gpio_data;
61 struct gpio_desc *gpio_trans; /* Voltage translator */
62 struct gpio_desc *gpio_enable; /* FSI enable */
63 struct gpio_desc *gpio_mux; /* Mux control */
68 #define CREATE_TRACE_POINTS
69 #include <trace/events/fsi_master_gpio.h>
71 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
78 static void clock_toggle(struct fsi_master_gpio *master, int count)
82 for (i = 0; i < count; i++) {
83 if (!master->no_delays)
84 ndelay(FSI_GPIO_STD_DLY);
85 gpiod_set_value(master->gpio_clk, 0);
86 if (!master->no_delays)
87 ndelay(FSI_GPIO_STD_DLY);
88 gpiod_set_value(master->gpio_clk, 1);
92 static int sda_clock_in(struct fsi_master_gpio *master)
96 if (!master->no_delays)
97 ndelay(FSI_GPIO_STD_DLY);
98 gpiod_set_value(master->gpio_clk, 0);
99 in = gpiod_get_value(master->gpio_data);
100 if (!master->no_delays)
101 ndelay(FSI_GPIO_STD_DLY);
102 gpiod_set_value(master->gpio_clk, 1);
106 static void sda_out(struct fsi_master_gpio *master, int value)
108 gpiod_set_value(master->gpio_data, value);
111 static void set_sda_input(struct fsi_master_gpio *master)
113 gpiod_direction_input(master->gpio_data);
114 gpiod_set_value(master->gpio_trans, 0);
117 static void set_sda_output(struct fsi_master_gpio *master, int value)
119 gpiod_set_value(master->gpio_trans, 1);
120 gpiod_direction_output(master->gpio_data, value);
123 static void clock_zeros(struct fsi_master_gpio *master, int count)
125 set_sda_output(master, 1);
126 clock_toggle(master, count);
129 static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
134 set_sda_input(master);
136 for (bit = 0; bit < num_bits; bit++) {
137 in_bit = sda_clock_in(master);
139 msg->msg |= ~in_bit & 0x1; /* Data is active low */
141 msg->bits += num_bits;
143 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
146 static void serial_out(struct fsi_master_gpio *master,
147 const struct fsi_gpio_msg *cmd)
150 uint64_t msg = ~cmd->msg; /* Data is active low */
151 uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
152 uint64_t last_bit = ~0;
155 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
158 dev_warn(master->dev, "trying to output 0 bits\n");
161 set_sda_output(master, 0);
163 /* Send the start bit */
165 clock_toggle(master, 1);
167 /* Send the message */
168 for (bit = 0; bit < cmd->bits; bit++) {
169 next_bit = (msg & sda_mask) >> (cmd->bits - 1);
170 if (last_bit ^ next_bit) {
171 sda_out(master, next_bit);
174 clock_toggle(master, 1);
179 static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
182 msg->msg |= data & ((1ull << bits) - 1);
186 static void msg_push_crc(struct fsi_gpio_msg *msg)
191 top = msg->bits & 0x3;
193 /* start bit, and any non-aligned top bits */
194 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
197 crc = crc4(crc, msg->msg, msg->bits - top);
199 msg_push_bits(msg, crc, 4);
203 * Encode an Absolute Address command
205 static void build_abs_ar_command(struct fsi_gpio_msg *cmd,
206 uint8_t id, uint32_t addr, size_t size, const void *data)
215 msg_push_bits(cmd, id, 2);
216 msg_push_bits(cmd, FSI_GPIO_CMD_ABS_AR, 3);
217 msg_push_bits(cmd, write ? 0 : 1, 1);
220 * The read/write size is encoded in the lower bits of the address
221 * (as it must be naturally-aligned), and the following ds bit.
223 * size addr:1 addr:0 ds
229 ds = size > 1 ? 1 : 0;
234 msg_push_bits(cmd, addr & ((1 << 21) - 1), 21);
235 msg_push_bits(cmd, ds, 1);
236 for (i = 0; write && i < size; i++)
237 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
242 static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
247 msg_push_bits(cmd, slave_id, 2);
248 msg_push_bits(cmd, FSI_GPIO_CMD_DPOLL, 3);
252 static void echo_delay(struct fsi_master_gpio *master)
254 set_sda_output(master, 1);
255 clock_toggle(master, FSI_ECHO_DELAY_CLOCKS);
258 static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
263 msg_push_bits(cmd, slave_id, 2);
264 msg_push_bits(cmd, FSI_GPIO_CMD_TERM, 6);
269 * Store information on master errors so handler can detect and clean
272 static void fsi_master_gpio_error(struct fsi_master_gpio *master, int error)
277 static int read_one_response(struct fsi_master_gpio *master,
278 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
280 struct fsi_gpio_msg msg;
286 spin_lock_irqsave(&master->bit_lock, flags);
288 /* wait for the start bit */
289 for (i = 0; i < FSI_GPIO_MTOE_COUNT; i++) {
292 serial_in(master, &msg, 1);
296 if (i == FSI_GPIO_MTOE_COUNT) {
298 "Master time out waiting for response\n");
299 fsi_master_gpio_error(master, FSI_GPIO_MTOE);
300 spin_unlock_irqrestore(&master->bit_lock, flags);
307 /* Read slave ID & response tag */
308 serial_in(master, &msg, 4);
312 /* If we have an ACK and we're expecting data, clock the data in too */
313 if (tag == FSI_GPIO_RESP_ACK && data_size)
314 serial_in(master, &msg, data_size * 8);
317 serial_in(master, &msg, FSI_GPIO_CRC_SIZE);
319 spin_unlock_irqrestore(&master->bit_lock, flags);
321 /* we have a whole message now; check CRC */
323 crc = crc4(crc, msg.msg, msg.bits);
325 dev_dbg(master->dev, "ERR response CRC\n");
326 fsi_master_gpio_error(master, FSI_GPIO_CRC_INVAL);
338 static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
340 struct fsi_gpio_msg cmd;
345 build_term_command(&cmd, slave);
347 spin_lock_irqsave(&master->bit_lock, flags);
348 serial_out(master, &cmd);
350 spin_unlock_irqrestore(&master->bit_lock, flags);
352 rc = read_one_response(master, 0, NULL, &tag);
355 "TERM failed; lost communication with slave\n");
357 } else if (tag != FSI_GPIO_RESP_ACK) {
358 dev_err(master->dev, "TERM failed; response %d\n", tag);
365 static int poll_for_response(struct fsi_master_gpio *master,
366 uint8_t slave, uint8_t size, void *data)
368 struct fsi_gpio_msg response, cmd;
369 int busy_count = 0, rc, i;
372 uint8_t *data_byte = data;
375 rc = read_one_response(master, size, &response, &tag);
380 case FSI_GPIO_RESP_ACK:
382 uint64_t val = response.msg;
383 /* clear crc & mask */
385 val &= (1ull << (size * 8)) - 1;
387 for (i = 0; i < size; i++) {
388 data_byte[size-i-1] = val;
393 case FSI_GPIO_RESP_BUSY:
395 * Its necessary to clock slave before issuing
396 * d-poll, not indicated in the hardware protocol
397 * spec. < 20 clocks causes slave to hang, 21 ok.
399 if (busy_count++ < FSI_GPIO_MAX_BUSY) {
400 build_dpoll_command(&cmd, slave);
401 spin_lock_irqsave(&master->bit_lock, flags);
402 clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
403 serial_out(master, &cmd);
405 spin_unlock_irqrestore(&master->bit_lock, flags);
408 dev_warn(master->dev,
409 "ERR slave is stuck in busy state, issuing TERM\n");
410 spin_lock_irqsave(&master->bit_lock, flags);
411 clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
412 spin_unlock_irqrestore(&master->bit_lock, flags);
413 issue_term(master, slave);
417 case FSI_GPIO_RESP_ERRA:
418 case FSI_GPIO_RESP_ERRC:
419 dev_dbg(master->dev, "ERR%c received: 0x%x\n",
420 tag == FSI_GPIO_RESP_ERRA ? 'A' : 'C',
422 fsi_master_gpio_error(master, response.msg);
428 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
430 /* Clock the slave enough to be ready for next operation */
431 spin_lock_irqsave(&master->bit_lock, flags);
432 clock_zeros(master, FSI_GPIO_PRIME_SLAVE_CLOCKS);
433 spin_unlock_irqrestore(&master->bit_lock, flags);
437 static int send_request(struct fsi_master_gpio *master,
438 struct fsi_gpio_msg *cmd)
442 spin_lock_irqsave(&master->bit_lock, flags);
443 if (master->external_mode) {
444 spin_unlock_irqrestore(&master->bit_lock, flags);
448 serial_out(master, cmd);
450 spin_unlock_irqrestore(&master->bit_lock, flags);
455 static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
456 struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
460 mutex_lock(&master->cmd_lock);
462 rc = send_request(master, cmd);
464 rc = poll_for_response(master, slave, resp_len, resp);
466 mutex_unlock(&master->cmd_lock);
471 static int fsi_master_gpio_read(struct fsi_master *_master, int link,
472 uint8_t id, uint32_t addr, void *val, size_t size)
474 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
475 struct fsi_gpio_msg cmd;
480 build_abs_ar_command(&cmd, id, addr, size, NULL);
481 return fsi_master_gpio_xfer(master, id, &cmd, size, val);
484 static int fsi_master_gpio_write(struct fsi_master *_master, int link,
485 uint8_t id, uint32_t addr, const void *val, size_t size)
487 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
488 struct fsi_gpio_msg cmd;
493 build_abs_ar_command(&cmd, id, addr, size, val);
494 return fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
497 static int fsi_master_gpio_term(struct fsi_master *_master,
498 int link, uint8_t id)
500 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
501 struct fsi_gpio_msg cmd;
506 build_term_command(&cmd, id);
507 return fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
510 static int fsi_master_gpio_break(struct fsi_master *_master, int link)
512 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
518 trace_fsi_master_gpio_break(master);
520 mutex_lock(&master->cmd_lock);
521 if (master->external_mode) {
522 mutex_unlock(&master->cmd_lock);
526 spin_lock_irqsave(&master->bit_lock, flags);
528 set_sda_output(master, 1);
530 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
532 clock_toggle(master, FSI_BREAK_CLOCKS);
535 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
537 spin_unlock_irqrestore(&master->bit_lock, flags);
538 mutex_unlock(&master->cmd_lock);
540 /* Wait for logic reset to take effect */
546 static void fsi_master_gpio_init(struct fsi_master_gpio *master)
550 gpiod_direction_output(master->gpio_mux, 1);
551 gpiod_direction_output(master->gpio_trans, 1);
552 gpiod_direction_output(master->gpio_enable, 1);
553 gpiod_direction_output(master->gpio_clk, 1);
554 gpiod_direction_output(master->gpio_data, 1);
556 /* todo: evaluate if clocks can be reduced */
557 spin_lock_irqsave(&master->bit_lock, flags);
558 clock_zeros(master, FSI_INIT_CLOCKS);
559 spin_unlock_irqrestore(&master->bit_lock, flags);
562 static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
564 gpiod_direction_output(master->gpio_mux, 0);
565 gpiod_direction_output(master->gpio_trans, 0);
566 gpiod_direction_output(master->gpio_enable, 1);
567 gpiod_direction_input(master->gpio_clk);
568 gpiod_direction_input(master->gpio_data);
571 static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
573 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
579 mutex_lock(&master->cmd_lock);
580 if (!master->external_mode) {
581 gpiod_set_value(master->gpio_enable, 1);
584 mutex_unlock(&master->cmd_lock);
589 static ssize_t external_mode_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
592 struct fsi_master_gpio *master = dev_get_drvdata(dev);
594 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
595 master->external_mode ? 1 : 0);
598 static ssize_t external_mode_store(struct device *dev,
599 struct device_attribute *attr, const char *buf, size_t count)
601 struct fsi_master_gpio *master = dev_get_drvdata(dev);
606 err = kstrtoul(buf, 0, &val);
610 external_mode = !!val;
612 mutex_lock(&master->cmd_lock);
614 if (external_mode == master->external_mode) {
615 mutex_unlock(&master->cmd_lock);
619 master->external_mode = external_mode;
620 if (master->external_mode)
621 fsi_master_gpio_init_external(master);
623 fsi_master_gpio_init(master);
625 mutex_unlock(&master->cmd_lock);
627 fsi_master_rescan(&master->master);
632 static DEVICE_ATTR(external_mode, 0664,
633 external_mode_show, external_mode_store);
635 static int fsi_master_gpio_probe(struct platform_device *pdev)
637 struct fsi_master_gpio *master;
638 struct gpio_desc *gpio;
641 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
645 master->dev = &pdev->dev;
646 master->master.dev.parent = master->dev;
647 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
649 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
651 dev_err(&pdev->dev, "failed to get clock gpio\n");
652 return PTR_ERR(gpio);
654 master->gpio_clk = gpio;
656 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
658 dev_err(&pdev->dev, "failed to get data gpio\n");
659 return PTR_ERR(gpio);
661 master->gpio_data = gpio;
664 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
666 dev_err(&pdev->dev, "failed to get trans gpio\n");
667 return PTR_ERR(gpio);
669 master->gpio_trans = gpio;
671 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
673 dev_err(&pdev->dev, "failed to get enable gpio\n");
674 return PTR_ERR(gpio);
676 master->gpio_enable = gpio;
678 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
680 dev_err(&pdev->dev, "failed to get mux gpio\n");
681 return PTR_ERR(gpio);
683 master->gpio_mux = gpio;
686 * Check if GPIO block is slow enought that no extra delays
687 * are necessary. This improves performance on ast2500 by
688 * an order of magnitude.
690 master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
692 master->master.n_links = 1;
693 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
694 master->master.read = fsi_master_gpio_read;
695 master->master.write = fsi_master_gpio_write;
696 master->master.term = fsi_master_gpio_term;
697 master->master.send_break = fsi_master_gpio_break;
698 master->master.link_enable = fsi_master_gpio_link_enable;
699 platform_set_drvdata(pdev, master);
700 spin_lock_init(&master->bit_lock);
701 mutex_init(&master->cmd_lock);
703 fsi_master_gpio_init(master);
705 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
709 return fsi_master_register(&master->master);
713 static int fsi_master_gpio_remove(struct platform_device *pdev)
715 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
717 devm_gpiod_put(&pdev->dev, master->gpio_clk);
718 devm_gpiod_put(&pdev->dev, master->gpio_data);
719 if (master->gpio_trans)
720 devm_gpiod_put(&pdev->dev, master->gpio_trans);
721 if (master->gpio_enable)
722 devm_gpiod_put(&pdev->dev, master->gpio_enable);
723 if (master->gpio_mux)
724 devm_gpiod_put(&pdev->dev, master->gpio_mux);
725 fsi_master_unregister(&master->master);
727 of_node_put(master->master.dev.of_node);
732 static const struct of_device_id fsi_master_gpio_match[] = {
733 { .compatible = "fsi-master-gpio" },
737 static struct platform_driver fsi_master_gpio_driver = {
739 .name = "fsi-master-gpio",
740 .of_match_table = fsi_master_gpio_match,
742 .probe = fsi_master_gpio_probe,
743 .remove = fsi_master_gpio_remove,
746 module_platform_driver(fsi_master_gpio_driver);
747 MODULE_LICENSE("GPL");