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
33 #define FSI_GPIO_DPOLL_CLOCKS 50 /* < 21 will cause slave to hang */
36 #define FSI_GPIO_ERR_BUSY 1 /* Slave stuck in busy state */
37 #define FSI_GPIO_RESP_ERRA 2 /* Any (misc) Error */
38 #define FSI_GPIO_RESP_ERRC 3 /* Slave reports master CRC error */
39 #define FSI_GPIO_MTOE 4 /* Master time out error */
40 #define FSI_GPIO_CRC_INVAL 5 /* Master reports slave CRC error */
42 /* Normal slave responses */
43 #define FSI_GPIO_RESP_BUSY 1
44 #define FSI_GPIO_RESP_ACK 0
45 #define FSI_GPIO_RESP_ACKD 4
47 #define FSI_GPIO_MAX_BUSY 200
48 #define FSI_GPIO_MTOE_COUNT 1000
49 #define FSI_GPIO_DRAIN_BITS 20
50 #define FSI_GPIO_CRC_SIZE 4
51 #define FSI_GPIO_MSG_ID_SIZE 2
52 #define FSI_GPIO_MSG_RESPID_SIZE 2
53 #define FSI_GPIO_PRIME_SLAVE_CLOCKS 20
55 struct fsi_master_gpio {
56 struct fsi_master master;
58 struct mutex cmd_lock; /* mutex for command ordering */
59 spinlock_t bit_lock; /* lock for clocking bits out */
60 struct gpio_desc *gpio_clk;
61 struct gpio_desc *gpio_data;
62 struct gpio_desc *gpio_trans; /* Voltage translator */
63 struct gpio_desc *gpio_enable; /* FSI enable */
64 struct gpio_desc *gpio_mux; /* Mux control */
69 #define CREATE_TRACE_POINTS
70 #include <trace/events/fsi_master_gpio.h>
72 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
79 static void clock_toggle(struct fsi_master_gpio *master, int count)
83 for (i = 0; i < count; i++) {
84 if (!master->no_delays)
85 ndelay(FSI_GPIO_STD_DLY);
86 gpiod_set_value(master->gpio_clk, 0);
87 if (!master->no_delays)
88 ndelay(FSI_GPIO_STD_DLY);
89 gpiod_set_value(master->gpio_clk, 1);
93 static int sda_clock_in(struct fsi_master_gpio *master)
97 if (!master->no_delays)
98 ndelay(FSI_GPIO_STD_DLY);
99 gpiod_set_value(master->gpio_clk, 0);
101 /* Dummy read to feed the synchronizers */
102 gpiod_get_value(master->gpio_data);
104 /* Actual data read */
105 in = gpiod_get_value(master->gpio_data);
106 if (!master->no_delays)
107 ndelay(FSI_GPIO_STD_DLY);
108 gpiod_set_value(master->gpio_clk, 1);
112 static void sda_out(struct fsi_master_gpio *master, int value)
114 gpiod_set_value(master->gpio_data, value);
117 static void set_sda_input(struct fsi_master_gpio *master)
119 gpiod_direction_input(master->gpio_data);
120 gpiod_set_value(master->gpio_trans, 0);
123 static void set_sda_output(struct fsi_master_gpio *master, int value)
125 gpiod_set_value(master->gpio_trans, 1);
126 gpiod_direction_output(master->gpio_data, value);
129 static void clock_zeros(struct fsi_master_gpio *master, int count)
131 set_sda_output(master, 1);
132 clock_toggle(master, count);
135 static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
140 set_sda_input(master);
142 for (bit = 0; bit < num_bits; bit++) {
143 in_bit = sda_clock_in(master);
145 msg->msg |= ~in_bit & 0x1; /* Data is active low */
147 msg->bits += num_bits;
149 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
152 static void serial_out(struct fsi_master_gpio *master,
153 const struct fsi_gpio_msg *cmd)
156 uint64_t msg = ~cmd->msg; /* Data is active low */
157 uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
158 uint64_t last_bit = ~0;
161 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
164 dev_warn(master->dev, "trying to output 0 bits\n");
167 set_sda_output(master, 0);
169 /* Send the start bit */
171 clock_toggle(master, 1);
173 /* Send the message */
174 for (bit = 0; bit < cmd->bits; bit++) {
175 next_bit = (msg & sda_mask) >> (cmd->bits - 1);
176 if (last_bit ^ next_bit) {
177 sda_out(master, next_bit);
180 clock_toggle(master, 1);
185 static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
188 msg->msg |= data & ((1ull << bits) - 1);
192 static void msg_push_crc(struct fsi_gpio_msg *msg)
197 top = msg->bits & 0x3;
199 /* start bit, and any non-aligned top bits */
200 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
203 crc = crc4(crc, msg->msg, msg->bits - top);
205 msg_push_bits(msg, crc, 4);
209 * Encode an Absolute Address command
211 static void build_abs_ar_command(struct fsi_gpio_msg *cmd,
212 uint8_t id, uint32_t addr, size_t size, const void *data)
221 msg_push_bits(cmd, id, 2);
222 msg_push_bits(cmd, FSI_GPIO_CMD_ABS_AR, 3);
223 msg_push_bits(cmd, write ? 0 : 1, 1);
226 * The read/write size is encoded in the lower bits of the address
227 * (as it must be naturally-aligned), and the following ds bit.
229 * size addr:1 addr:0 ds
235 ds = size > 1 ? 1 : 0;
240 msg_push_bits(cmd, addr & ((1 << 21) - 1), 21);
241 msg_push_bits(cmd, ds, 1);
242 for (i = 0; write && i < size; i++)
243 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
248 static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
253 msg_push_bits(cmd, slave_id, 2);
254 msg_push_bits(cmd, FSI_GPIO_CMD_DPOLL, 3);
258 static void echo_delay(struct fsi_master_gpio *master)
260 set_sda_output(master, 1);
261 clock_toggle(master, FSI_ECHO_DELAY_CLOCKS);
264 static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
269 msg_push_bits(cmd, slave_id, 2);
270 msg_push_bits(cmd, FSI_GPIO_CMD_TERM, 6);
275 * Store information on master errors so handler can detect and clean
278 static void fsi_master_gpio_error(struct fsi_master_gpio *master, int error)
283 static int read_one_response(struct fsi_master_gpio *master,
284 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
286 struct fsi_gpio_msg msg;
292 spin_lock_irqsave(&master->bit_lock, flags);
294 /* wait for the start bit */
295 for (i = 0; i < FSI_GPIO_MTOE_COUNT; i++) {
298 serial_in(master, &msg, 1);
302 if (i == FSI_GPIO_MTOE_COUNT) {
304 "Master time out waiting for response\n");
305 fsi_master_gpio_error(master, FSI_GPIO_MTOE);
306 spin_unlock_irqrestore(&master->bit_lock, flags);
313 /* Read slave ID & response tag */
314 serial_in(master, &msg, 4);
318 /* If we have an ACK and we're expecting data, clock the data in too */
319 if (tag == FSI_GPIO_RESP_ACK && data_size)
320 serial_in(master, &msg, data_size * 8);
323 serial_in(master, &msg, FSI_GPIO_CRC_SIZE);
325 spin_unlock_irqrestore(&master->bit_lock, flags);
327 /* we have a whole message now; check CRC */
329 crc = crc4(crc, msg.msg, msg.bits);
331 dev_dbg(master->dev, "ERR response CRC\n");
332 fsi_master_gpio_error(master, FSI_GPIO_CRC_INVAL);
344 static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
346 struct fsi_gpio_msg cmd;
351 build_term_command(&cmd, slave);
353 spin_lock_irqsave(&master->bit_lock, flags);
354 serial_out(master, &cmd);
356 spin_unlock_irqrestore(&master->bit_lock, flags);
358 rc = read_one_response(master, 0, NULL, &tag);
361 "TERM failed; lost communication with slave\n");
363 } else if (tag != FSI_GPIO_RESP_ACK) {
364 dev_err(master->dev, "TERM failed; response %d\n", tag);
371 static int poll_for_response(struct fsi_master_gpio *master,
372 uint8_t slave, uint8_t size, void *data)
374 struct fsi_gpio_msg response, cmd;
375 int busy_count = 0, rc, i;
378 uint8_t *data_byte = data;
381 rc = read_one_response(master, size, &response, &tag);
386 case FSI_GPIO_RESP_ACK:
388 uint64_t val = response.msg;
389 /* clear crc & mask */
391 val &= (1ull << (size * 8)) - 1;
393 for (i = 0; i < size; i++) {
394 data_byte[size-i-1] = val;
399 case FSI_GPIO_RESP_BUSY:
401 * Its necessary to clock slave before issuing
402 * d-poll, not indicated in the hardware protocol
403 * spec. < 20 clocks causes slave to hang, 21 ok.
405 if (busy_count++ < FSI_GPIO_MAX_BUSY) {
406 build_dpoll_command(&cmd, slave);
407 spin_lock_irqsave(&master->bit_lock, flags);
408 clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
409 serial_out(master, &cmd);
411 spin_unlock_irqrestore(&master->bit_lock, flags);
414 dev_warn(master->dev,
415 "ERR slave is stuck in busy state, issuing TERM\n");
416 spin_lock_irqsave(&master->bit_lock, flags);
417 clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
418 spin_unlock_irqrestore(&master->bit_lock, flags);
419 issue_term(master, slave);
423 case FSI_GPIO_RESP_ERRA:
424 case FSI_GPIO_RESP_ERRC:
425 dev_dbg(master->dev, "ERR%c received: 0x%x\n",
426 tag == FSI_GPIO_RESP_ERRA ? 'A' : 'C',
428 fsi_master_gpio_error(master, response.msg);
434 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
436 /* Clock the slave enough to be ready for next operation */
437 spin_lock_irqsave(&master->bit_lock, flags);
438 clock_zeros(master, FSI_GPIO_PRIME_SLAVE_CLOCKS);
439 spin_unlock_irqrestore(&master->bit_lock, flags);
443 static int send_request(struct fsi_master_gpio *master,
444 struct fsi_gpio_msg *cmd)
448 spin_lock_irqsave(&master->bit_lock, flags);
449 if (master->external_mode) {
450 spin_unlock_irqrestore(&master->bit_lock, flags);
454 serial_out(master, cmd);
456 spin_unlock_irqrestore(&master->bit_lock, flags);
461 static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
462 struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
466 rc = send_request(master, cmd);
468 rc = poll_for_response(master, slave, resp_len, resp);
473 static int fsi_master_gpio_read(struct fsi_master *_master, int link,
474 uint8_t id, uint32_t addr, void *val, size_t size)
476 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
477 struct fsi_gpio_msg cmd;
483 mutex_lock(&master->cmd_lock);
484 build_abs_ar_command(&cmd, id, addr, size, NULL);
485 rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
486 mutex_unlock(&master->cmd_lock);
491 static int fsi_master_gpio_write(struct fsi_master *_master, int link,
492 uint8_t id, uint32_t addr, const void *val, size_t size)
494 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
495 struct fsi_gpio_msg cmd;
501 mutex_lock(&master->cmd_lock);
502 build_abs_ar_command(&cmd, id, addr, size, val);
503 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
504 mutex_unlock(&master->cmd_lock);
509 static int fsi_master_gpio_term(struct fsi_master *_master,
510 int link, uint8_t id)
512 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
513 struct fsi_gpio_msg cmd;
519 mutex_lock(&master->cmd_lock);
520 build_term_command(&cmd, id);
521 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
522 mutex_unlock(&master->cmd_lock);
527 static int fsi_master_gpio_break(struct fsi_master *_master, int link)
529 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
535 trace_fsi_master_gpio_break(master);
537 mutex_lock(&master->cmd_lock);
538 if (master->external_mode) {
539 mutex_unlock(&master->cmd_lock);
543 spin_lock_irqsave(&master->bit_lock, flags);
545 set_sda_output(master, 1);
547 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
549 clock_toggle(master, FSI_BREAK_CLOCKS);
552 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
554 spin_unlock_irqrestore(&master->bit_lock, flags);
555 mutex_unlock(&master->cmd_lock);
557 /* Wait for logic reset to take effect */
563 static void fsi_master_gpio_init(struct fsi_master_gpio *master)
567 gpiod_direction_output(master->gpio_mux, 1);
568 gpiod_direction_output(master->gpio_trans, 1);
569 gpiod_direction_output(master->gpio_enable, 1);
570 gpiod_direction_output(master->gpio_clk, 1);
571 gpiod_direction_output(master->gpio_data, 1);
573 /* todo: evaluate if clocks can be reduced */
574 spin_lock_irqsave(&master->bit_lock, flags);
575 clock_zeros(master, FSI_INIT_CLOCKS);
576 spin_unlock_irqrestore(&master->bit_lock, flags);
579 static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
581 gpiod_direction_output(master->gpio_mux, 0);
582 gpiod_direction_output(master->gpio_trans, 0);
583 gpiod_direction_output(master->gpio_enable, 1);
584 gpiod_direction_input(master->gpio_clk);
585 gpiod_direction_input(master->gpio_data);
588 static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
590 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
596 mutex_lock(&master->cmd_lock);
597 if (!master->external_mode) {
598 gpiod_set_value(master->gpio_enable, 1);
601 mutex_unlock(&master->cmd_lock);
606 static ssize_t external_mode_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct fsi_master_gpio *master = dev_get_drvdata(dev);
611 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
612 master->external_mode ? 1 : 0);
615 static ssize_t external_mode_store(struct device *dev,
616 struct device_attribute *attr, const char *buf, size_t count)
618 struct fsi_master_gpio *master = dev_get_drvdata(dev);
623 err = kstrtoul(buf, 0, &val);
627 external_mode = !!val;
629 mutex_lock(&master->cmd_lock);
631 if (external_mode == master->external_mode) {
632 mutex_unlock(&master->cmd_lock);
636 master->external_mode = external_mode;
637 if (master->external_mode)
638 fsi_master_gpio_init_external(master);
640 fsi_master_gpio_init(master);
642 mutex_unlock(&master->cmd_lock);
644 fsi_master_rescan(&master->master);
649 static DEVICE_ATTR(external_mode, 0664,
650 external_mode_show, external_mode_store);
652 static int fsi_master_gpio_probe(struct platform_device *pdev)
654 struct fsi_master_gpio *master;
655 struct gpio_desc *gpio;
658 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
662 master->dev = &pdev->dev;
663 master->master.dev.parent = master->dev;
664 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
666 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
668 dev_err(&pdev->dev, "failed to get clock gpio\n");
669 return PTR_ERR(gpio);
671 master->gpio_clk = gpio;
673 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
675 dev_err(&pdev->dev, "failed to get data gpio\n");
676 return PTR_ERR(gpio);
678 master->gpio_data = gpio;
681 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
683 dev_err(&pdev->dev, "failed to get trans gpio\n");
684 return PTR_ERR(gpio);
686 master->gpio_trans = gpio;
688 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
690 dev_err(&pdev->dev, "failed to get enable gpio\n");
691 return PTR_ERR(gpio);
693 master->gpio_enable = gpio;
695 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
697 dev_err(&pdev->dev, "failed to get mux gpio\n");
698 return PTR_ERR(gpio);
700 master->gpio_mux = gpio;
703 * Check if GPIO block is slow enought that no extra delays
704 * are necessary. This improves performance on ast2500 by
705 * an order of magnitude.
707 master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
709 master->master.n_links = 1;
710 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
711 master->master.read = fsi_master_gpio_read;
712 master->master.write = fsi_master_gpio_write;
713 master->master.term = fsi_master_gpio_term;
714 master->master.send_break = fsi_master_gpio_break;
715 master->master.link_enable = fsi_master_gpio_link_enable;
716 platform_set_drvdata(pdev, master);
717 spin_lock_init(&master->bit_lock);
718 mutex_init(&master->cmd_lock);
720 fsi_master_gpio_init(master);
722 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
726 return fsi_master_register(&master->master);
730 static int fsi_master_gpio_remove(struct platform_device *pdev)
732 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
734 devm_gpiod_put(&pdev->dev, master->gpio_clk);
735 devm_gpiod_put(&pdev->dev, master->gpio_data);
736 if (master->gpio_trans)
737 devm_gpiod_put(&pdev->dev, master->gpio_trans);
738 if (master->gpio_enable)
739 devm_gpiod_put(&pdev->dev, master->gpio_enable);
740 if (master->gpio_mux)
741 devm_gpiod_put(&pdev->dev, master->gpio_mux);
742 fsi_master_unregister(&master->master);
744 of_node_put(master->master.dev.of_node);
749 static const struct of_device_id fsi_master_gpio_match[] = {
750 { .compatible = "fsi-master-gpio" },
754 static struct platform_driver fsi_master_gpio_driver = {
756 .name = "fsi-master-gpio",
757 .of_match_table = fsi_master_gpio_match,
759 .probe = fsi_master_gpio_probe,
760 .remove = fsi_master_gpio_remove,
763 module_platform_driver(fsi_master_gpio_driver);
764 MODULE_LICENSE("GPL");