blob: 7d5f29b4b595d24ae48cd9dd0d0f69bc2209de2d [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Christopher Bosticac0385d2017-06-06 16:08:54 -05002/*
3 * A FSI master controller, using a simple GPIO bit-banging interface
4 */
5
6#include <linux/crc4.h>
7#include <linux/delay.h>
8#include <linux/device.h>
9#include <linux/fsi.h>
10#include <linux/gpio/consumer.h>
11#include <linux/io.h>
Jeremy Kerr26d79b22018-05-24 11:29:56 +080012#include <linux/irqflags.h>
Christopher Bosticac0385d2017-06-06 16:08:54 -050013#include <linux/module.h>
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +103014#include <linux/of.h>
Christopher Bosticac0385d2017-06-06 16:08:54 -050015#include <linux/platform_device.h>
16#include <linux/slab.h>
Christopher Bosticac0385d2017-06-06 16:08:54 -050017
18#include "fsi-master.h"
19
20#define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +080021#define LAST_ADDR_INVALID 0x1
22
Christopher Bosticac0385d2017-06-06 16:08:54 -050023struct fsi_master_gpio {
24 struct fsi_master master;
25 struct device *dev;
Jeremy Kerre5538132018-02-20 14:48:44 +103026 struct mutex cmd_lock; /* mutex for command ordering */
Christopher Bosticac0385d2017-06-06 16:08:54 -050027 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 */
Jeremy Kerrb8bd1462018-02-12 15:45:42 +103032 bool external_mode;
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100033 bool no_delays;
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +080034 uint32_t last_addr;
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +100035 uint8_t t_send_delay;
36 uint8_t t_echo_delay;
Christopher Bosticac0385d2017-06-06 16:08:54 -050037};
38
Jeremy Kerr1247cf72017-06-06 16:08:55 -050039#define CREATE_TRACE_POINTS
40#include <trace/events/fsi_master_gpio.h>
41
Christopher Bosticac0385d2017-06-06 16:08:54 -050042#define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
43
44struct fsi_gpio_msg {
45 uint64_t msg;
46 uint8_t bits;
47};
48
49static void clock_toggle(struct fsi_master_gpio *master, int count)
50{
51 int i;
52
53 for (i = 0; i < count; i++) {
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100054 if (!master->no_delays)
55 ndelay(FSI_GPIO_STD_DLY);
Christopher Bosticac0385d2017-06-06 16:08:54 -050056 gpiod_set_value(master->gpio_clk, 0);
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100057 if (!master->no_delays)
58 ndelay(FSI_GPIO_STD_DLY);
Christopher Bosticac0385d2017-06-06 16:08:54 -050059 gpiod_set_value(master->gpio_clk, 1);
60 }
61}
62
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +100063static int sda_clock_in(struct fsi_master_gpio *master)
Christopher Bosticac0385d2017-06-06 16:08:54 -050064{
65 int in;
66
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100067 if (!master->no_delays)
68 ndelay(FSI_GPIO_STD_DLY);
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +100069 gpiod_set_value(master->gpio_clk, 0);
Benjamin Herrenschmidtf3ca4832018-05-15 23:05:58 +100070
71 /* Dummy read to feed the synchronizers */
72 gpiod_get_value(master->gpio_data);
73
74 /* Actual data read */
Christopher Bosticac0385d2017-06-06 16:08:54 -050075 in = gpiod_get_value(master->gpio_data);
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100076 if (!master->no_delays)
77 ndelay(FSI_GPIO_STD_DLY);
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +100078 gpiod_set_value(master->gpio_clk, 1);
Christopher Bosticac0385d2017-06-06 16:08:54 -050079 return in ? 1 : 0;
80}
81
82static void sda_out(struct fsi_master_gpio *master, int value)
83{
84 gpiod_set_value(master->gpio_data, value);
85}
86
87static void set_sda_input(struct fsi_master_gpio *master)
88{
89 gpiod_direction_input(master->gpio_data);
90 gpiod_set_value(master->gpio_trans, 0);
91}
92
93static void set_sda_output(struct fsi_master_gpio *master, int value)
94{
95 gpiod_set_value(master->gpio_trans, 1);
96 gpiod_direction_output(master->gpio_data, value);
97}
98
99static void clock_zeros(struct fsi_master_gpio *master, int count)
100{
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000101 trace_fsi_master_gpio_clock_zeros(master, count);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500102 set_sda_output(master, 1);
103 clock_toggle(master, count);
104}
105
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000106static void echo_delay(struct fsi_master_gpio *master)
107{
108 clock_zeros(master, master->t_echo_delay);
109}
110
111
Christopher Bosticac0385d2017-06-06 16:08:54 -0500112static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
113 uint8_t num_bits)
114{
115 uint8_t bit, in_bit;
116
117 set_sda_input(master);
118
119 for (bit = 0; bit < num_bits; bit++) {
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +1000120 in_bit = sda_clock_in(master);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500121 msg->msg <<= 1;
122 msg->msg |= ~in_bit & 0x1; /* Data is active low */
123 }
124 msg->bits += num_bits;
Jeremy Kerr1247cf72017-06-06 16:08:55 -0500125
126 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500127}
128
129static void serial_out(struct fsi_master_gpio *master,
130 const struct fsi_gpio_msg *cmd)
131{
132 uint8_t bit;
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;
136 int next_bit;
137
Jeremy Kerr1247cf72017-06-06 16:08:55 -0500138 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
139
Christopher Bosticac0385d2017-06-06 16:08:54 -0500140 if (!cmd->bits) {
141 dev_warn(master->dev, "trying to output 0 bits\n");
142 return;
143 }
144 set_sda_output(master, 0);
145
146 /* Send the start bit */
147 sda_out(master, 0);
148 clock_toggle(master, 1);
149
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);
155 last_bit = next_bit;
156 }
157 clock_toggle(master, 1);
158 msg <<= 1;
159 }
160}
161
162static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
163{
164 msg->msg <<= bits;
165 msg->msg |= data & ((1ull << bits) - 1);
166 msg->bits += bits;
167}
168
169static void msg_push_crc(struct fsi_gpio_msg *msg)
170{
171 uint8_t crc;
172 int top;
173
174 top = msg->bits & 0x3;
175
176 /* start bit, and any non-aligned top bits */
177 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
178
179 /* aligned bits */
180 crc = crc4(crc, msg->msg, msg->bits - top);
181
182 msg_push_bits(msg, crc, 4);
183}
184
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800185static bool check_same_address(struct fsi_master_gpio *master, int id,
186 uint32_t addr)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500187{
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800188 /* this will also handle LAST_ADDR_INVALID */
189 return master->last_addr == (((id & 0x3) << 21) | (addr & ~0x3));
190}
191
192static bool check_relative_address(struct fsi_master_gpio *master, int id,
193 uint32_t addr, uint32_t *rel_addrp)
194{
195 uint32_t last_addr = master->last_addr;
196 int32_t rel_addr;
197
198 if (last_addr == LAST_ADDR_INVALID)
199 return false;
200
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.
204 */
205 if (((last_addr >> 21) & 0x3) != id)
206 return false;
207
208 /* remove the top two bits from any 23-bit addressing */
209 last_addr &= (1 << 21) - 1;
210
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)
215 return false;
216
217 *rel_addrp = (uint32_t)rel_addr;
218
219 return true;
220}
221
222static void last_address_update(struct fsi_master_gpio *master,
223 int id, bool valid, uint32_t addr)
224{
225 if (!valid)
226 master->last_addr = LAST_ADDR_INVALID;
227 else
228 master->last_addr = ((id & 0x3) << 21) | (addr & ~0x3);
229}
230
231/*
232 * Encode an Absolute/Relative/Same Address command
233 */
234static 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)
237{
238 int i, addr_bits, opcode_bits;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500239 bool write = !!data;
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800240 uint8_t ds, opcode;
241 uint32_t rel_addr;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500242
243 cmd->bits = 0;
244 cmd->msg = 0;
245
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800246 /* we have 21 bits of address max */
247 addr &= ((1 << 21) - 1);
248
249 /* cmd opcodes are variable length - SAME_AR is only two bits */
250 opcode_bits = 3;
251
252 if (check_same_address(master, id, addr)) {
253 /* we still address the byte offset within the word */
254 addr_bits = 2;
255 opcode_bits = 2;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000256 opcode = FSI_CMD_SAME_AR;
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000257 trace_fsi_master_gpio_cmd_same_addr(master);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800258
259 } else if (check_relative_address(master, id, addr, &rel_addr)) {
260 /* 8 bits plus sign */
261 addr_bits = 9;
262 addr = rel_addr;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000263 opcode = FSI_CMD_REL_AR;
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000264 trace_fsi_master_gpio_cmd_rel_addr(master, rel_addr);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800265
266 } else {
267 addr_bits = 21;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000268 opcode = FSI_CMD_ABS_AR;
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000269 trace_fsi_master_gpio_cmd_abs_addr(master, addr);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800270 }
Christopher Bosticac0385d2017-06-06 16:08:54 -0500271
272 /*
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.
275 *
276 * size addr:1 addr:0 ds
277 * 1 x x 0
278 * 2 x 0 1
279 * 4 0 1 1
280 *
281 */
282 ds = size > 1 ? 1 : 0;
283 addr &= ~(size - 1);
284 if (size == 4)
285 addr |= 1;
286
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800287 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);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500291 msg_push_bits(cmd, ds, 1);
292 for (i = 0; write && i < size; i++)
293 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
294
295 msg_push_crc(cmd);
296}
297
298static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
299{
300 cmd->bits = 0;
301 cmd->msg = 0;
302
303 msg_push_bits(cmd, slave_id, 2);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000304 msg_push_bits(cmd, FSI_CMD_DPOLL, 3);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500305 msg_push_crc(cmd);
306}
307
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000308static void build_epoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
309{
310 cmd->bits = 0;
311 cmd->msg = 0;
312
313 msg_push_bits(cmd, slave_id, 2);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000314 msg_push_bits(cmd, FSI_CMD_EPOLL, 3);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000315 msg_push_crc(cmd);
316}
317
Christopher Bosticac0385d2017-06-06 16:08:54 -0500318static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
319{
320 cmd->bits = 0;
321 cmd->msg = 0;
322
323 msg_push_bits(cmd, slave_id, 2);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000324 msg_push_bits(cmd, FSI_CMD_TERM, 6);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500325 msg_push_crc(cmd);
326}
327
328/*
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000329 * 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.
333 */
Christopher Bosticac0385d2017-06-06 16:08:54 -0500334static int read_one_response(struct fsi_master_gpio *master,
335 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
336{
337 struct fsi_gpio_msg msg;
Jeremy Kerre5538132018-02-20 14:48:44 +1030338 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500339 uint32_t crc;
Jeremy Kerre5538132018-02-20 14:48:44 +1030340 uint8_t tag;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500341 int i;
342
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800343 local_irq_save(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030344
Christopher Bosticac0385d2017-06-06 16:08:54 -0500345 /* wait for the start bit */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000346 for (i = 0; i < FSI_MASTER_MTOE_COUNT; i++) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500347 msg.bits = 0;
348 msg.msg = 0;
349 serial_in(master, &msg, 1);
350 if (msg.msg)
351 break;
352 }
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000353 if (i == FSI_MASTER_MTOE_COUNT) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500354 dev_dbg(master->dev,
355 "Master time out waiting for response\n");
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800356 local_irq_restore(flags);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000357 return -ETIMEDOUT;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500358 }
359
360 msg.bits = 0;
361 msg.msg = 0;
362
363 /* Read slave ID & response tag */
364 serial_in(master, &msg, 4);
365
Christopher Bosticac0385d2017-06-06 16:08:54 -0500366 tag = msg.msg & 0x3;
367
368 /* If we have an ACK and we're expecting data, clock the data in too */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000369 if (tag == FSI_RESP_ACK && data_size)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500370 serial_in(master, &msg, data_size * 8);
371
372 /* read CRC */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000373 serial_in(master, &msg, FSI_CRC_SIZE);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500374
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800375 local_irq_restore(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030376
Christopher Bosticac0385d2017-06-06 16:08:54 -0500377 /* we have a whole message now; check CRC */
378 crc = crc4(0, 1, 1);
379 crc = crc4(crc, msg.msg, msg.bits);
380 if (crc) {
Benjamin Herrenschmidtc49e3442018-05-15 13:40:21 +1000381 /* Check if it's all 1's, that probably means the host is off */
382 if (((~msg.msg) & ((1ull << msg.bits) - 1)) == 0)
383 return -ENODEV;
384 dev_dbg(master->dev, "ERR response CRC msg: 0x%016llx (%d bits)\n",
385 msg.msg, msg.bits);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000386 return -EAGAIN;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500387 }
388
389 if (msgp)
390 *msgp = msg;
391 if (tagp)
392 *tagp = tag;
393
394 return 0;
395}
396
397static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
398{
399 struct fsi_gpio_msg cmd;
Jeremy Kerre5538132018-02-20 14:48:44 +1030400 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500401 uint8_t tag;
402 int rc;
403
404 build_term_command(&cmd, slave);
Jeremy Kerre5538132018-02-20 14:48:44 +1030405
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800406 local_irq_save(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500407 serial_out(master, &cmd);
408 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800409 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500410
411 rc = read_one_response(master, 0, NULL, &tag);
412 if (rc < 0) {
413 dev_err(master->dev,
414 "TERM failed; lost communication with slave\n");
415 return -EIO;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000416 } else if (tag != FSI_RESP_ACK) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500417 dev_err(master->dev, "TERM failed; response %d\n", tag);
418 return -EIO;
419 }
420
421 return 0;
422}
423
424static int poll_for_response(struct fsi_master_gpio *master,
425 uint8_t slave, uint8_t size, void *data)
426{
427 struct fsi_gpio_msg response, cmd;
428 int busy_count = 0, rc, i;
Jeremy Kerre5538132018-02-20 14:48:44 +1030429 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500430 uint8_t tag;
431 uint8_t *data_byte = data;
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000432 int crc_err_retries = 0;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500433retry:
434 rc = read_one_response(master, size, &response, &tag);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000435
436 /* Handle retries on CRC errors */
437 if (rc == -EAGAIN) {
438 /* Too many retries ? */
439 if (crc_err_retries++ > FSI_CRC_ERR_RETRIES) {
440 /*
441 * Pass it up as a -EIO otherwise upper level will retry
442 * the whole command which isn't what we want here.
443 */
444 rc = -EIO;
445 goto fail;
446 }
447 dev_dbg(master->dev,
448 "CRC error retry %d\n", crc_err_retries);
449 trace_fsi_master_gpio_crc_rsp_error(master);
450 build_epoll_command(&cmd, slave);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800451 local_irq_save(flags);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000452 clock_zeros(master, FSI_MASTER_EPOLL_CLOCKS);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000453 serial_out(master, &cmd);
454 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800455 local_irq_restore(flags);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000456 goto retry;
457 } else if (rc)
458 goto fail;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500459
460 switch (tag) {
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000461 case FSI_RESP_ACK:
Christopher Bosticac0385d2017-06-06 16:08:54 -0500462 if (size && data) {
463 uint64_t val = response.msg;
464 /* clear crc & mask */
465 val >>= 4;
466 val &= (1ull << (size * 8)) - 1;
467
468 for (i = 0; i < size; i++) {
469 data_byte[size-i-1] = val;
470 val >>= 8;
471 }
472 }
473 break;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000474 case FSI_RESP_BUSY:
Christopher Bosticac0385d2017-06-06 16:08:54 -0500475 /*
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.
479 */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000480 if (busy_count++ < FSI_MASTER_MAX_BUSY) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500481 build_dpoll_command(&cmd, slave);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800482 local_irq_save(flags);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000483 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500484 serial_out(master, &cmd);
485 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800486 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500487 goto retry;
488 }
489 dev_warn(master->dev,
490 "ERR slave is stuck in busy state, issuing TERM\n");
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800491 local_irq_save(flags);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000492 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800493 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500494 issue_term(master, slave);
495 rc = -EIO;
496 break;
497
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000498 case FSI_RESP_ERRA:
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000499 dev_dbg(master->dev, "ERRA received: 0x%x\n", (int)response.msg);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500500 rc = -EIO;
501 break;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000502 case FSI_RESP_ERRC:
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000503 dev_dbg(master->dev, "ERRC received: 0x%x\n", (int)response.msg);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000504 trace_fsi_master_gpio_crc_cmd_error(master);
505 rc = -EAGAIN;
506 break;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500507 }
508
Andrew Jeffery918da952018-02-20 14:48:33 +1030509 if (busy_count > 0)
510 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000511 fail:
Benjamin Herrenschmidtedc24852018-05-29 15:09:45 +1000512 /*
513 * tSendDelay clocks, avoids signal reflections when switching
514 * from receive of response back to send of data.
515 */
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800516 local_irq_save(flags);
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000517 clock_zeros(master, master->t_send_delay);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800518 local_irq_restore(flags);
519
Christopher Bosticac0385d2017-06-06 16:08:54 -0500520 return rc;
521}
522
Jeremy Kerre5538132018-02-20 14:48:44 +1030523static int send_request(struct fsi_master_gpio *master,
524 struct fsi_gpio_msg *cmd)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500525{
526 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500527
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800528 if (master->external_mode)
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030529 return -EBUSY;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030530
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800531 local_irq_save(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500532 serial_out(master, cmd);
533 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800534 local_irq_restore(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030535
536 return 0;
537}
538
539static 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)
541{
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000542 int rc = -EAGAIN, retries = 0;
Jeremy Kerre5538132018-02-20 14:48:44 +1030543
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000544 while ((retries++) < FSI_CRC_ERR_RETRIES) {
545 rc = send_request(master, cmd);
546 if (rc)
547 break;
Jeremy Kerre5538132018-02-20 14:48:44 +1030548 rc = poll_for_response(master, slave, resp_len, resp);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000549 if (rc != -EAGAIN)
550 break;
551 rc = -EIO;
552 dev_warn(master->dev, "ECRC retry %d\n", retries);
553
554 /* Pace it a bit before retry */
555 msleep(1);
556 }
Jeremy Kerre5538132018-02-20 14:48:44 +1030557
Christopher Bosticac0385d2017-06-06 16:08:54 -0500558 return rc;
559}
560
561static int fsi_master_gpio_read(struct fsi_master *_master, int link,
562 uint8_t id, uint32_t addr, void *val, size_t size)
563{
564 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
565 struct fsi_gpio_msg cmd;
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800566 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500567
568 if (link != 0)
569 return -ENODEV;
570
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800571 mutex_lock(&master->cmd_lock);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800572 build_ar_command(master, &cmd, id, addr, size, NULL);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800573 rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800574 last_address_update(master, id, rc == 0, addr);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800575 mutex_unlock(&master->cmd_lock);
576
577 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500578}
579
580static int fsi_master_gpio_write(struct fsi_master *_master, int link,
581 uint8_t id, uint32_t addr, const void *val, size_t size)
582{
583 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
584 struct fsi_gpio_msg cmd;
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800585 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500586
587 if (link != 0)
588 return -ENODEV;
589
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800590 mutex_lock(&master->cmd_lock);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800591 build_ar_command(master, &cmd, id, addr, size, val);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800592 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800593 last_address_update(master, id, rc == 0, addr);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800594 mutex_unlock(&master->cmd_lock);
595
596 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500597}
598
599static int fsi_master_gpio_term(struct fsi_master *_master,
600 int link, uint8_t id)
601{
602 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
603 struct fsi_gpio_msg cmd;
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800604 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500605
606 if (link != 0)
607 return -ENODEV;
608
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800609 mutex_lock(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500610 build_term_command(&cmd, id);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800611 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800612 last_address_update(master, id, false, 0);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800613 mutex_unlock(&master->cmd_lock);
614
615 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500616}
617
618static int fsi_master_gpio_break(struct fsi_master *_master, int link)
619{
620 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
Jeremy Kerr29d9b922018-02-12 15:45:41 +1030621 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500622
623 if (link != 0)
624 return -ENODEV;
625
Jeremy Kerr1247cf72017-06-06 16:08:55 -0500626 trace_fsi_master_gpio_break(master);
627
Jeremy Kerre5538132018-02-20 14:48:44 +1030628 mutex_lock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030629 if (master->external_mode) {
Jeremy Kerre5538132018-02-20 14:48:44 +1030630 mutex_unlock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030631 return -EBUSY;
632 }
Jeremy Kerre5538132018-02-20 14:48:44 +1030633
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800634 local_irq_save(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030635
Christopher Bosticac0385d2017-06-06 16:08:54 -0500636 set_sda_output(master, 1);
637 sda_out(master, 1);
638 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
639 sda_out(master, 0);
640 clock_toggle(master, FSI_BREAK_CLOCKS);
641 echo_delay(master);
642 sda_out(master, 1);
643 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
Jeremy Kerre5538132018-02-20 14:48:44 +1030644
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800645 local_irq_restore(flags);
646
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800647 last_address_update(master, 0, false, 0);
Jeremy Kerre5538132018-02-20 14:48:44 +1030648 mutex_unlock(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500649
650 /* Wait for logic reset to take effect */
651 udelay(200);
652
653 return 0;
654}
655
656static void fsi_master_gpio_init(struct fsi_master_gpio *master)
657{
Jeremy Kerre5538132018-02-20 14:48:44 +1030658 unsigned long flags;
659
Christopher Bosticac0385d2017-06-06 16:08:54 -0500660 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);
665
666 /* todo: evaluate if clocks can be reduced */
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800667 local_irq_save(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500668 clock_zeros(master, FSI_INIT_CLOCKS);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800669 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500670}
671
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030672static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
673{
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);
679}
680
Eddie James04635a32020-06-09 16:39:27 -0500681static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link,
682 bool enable)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500683{
684 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030685 int rc = -EBUSY;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500686
687 if (link != 0)
688 return -ENODEV;
Jeremy Kerr29d9b922018-02-12 15:45:41 +1030689
Jeremy Kerre5538132018-02-20 14:48:44 +1030690 mutex_lock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030691 if (!master->external_mode) {
Eddie James04635a32020-06-09 16:39:27 -0500692 gpiod_set_value(master->gpio_enable, enable ? 1 : 0);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030693 rc = 0;
694 }
Jeremy Kerre5538132018-02-20 14:48:44 +1030695 mutex_unlock(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500696
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030697 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500698}
699
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000700static int fsi_master_gpio_link_config(struct fsi_master *_master, int link,
701 u8 t_send_delay, u8 t_echo_delay)
702{
703 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
704
705 if (link != 0)
706 return -ENODEV;
707
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);
712
713 return 0;
714}
715
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030716static ssize_t external_mode_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
718{
719 struct fsi_master_gpio *master = dev_get_drvdata(dev);
720
721 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
722 master->external_mode ? 1 : 0);
723}
724
725static ssize_t external_mode_store(struct device *dev,
726 struct device_attribute *attr, const char *buf, size_t count)
727{
728 struct fsi_master_gpio *master = dev_get_drvdata(dev);
Jeremy Kerre5538132018-02-20 14:48:44 +1030729 unsigned long val;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030730 bool external_mode;
731 int err;
732
733 err = kstrtoul(buf, 0, &val);
734 if (err)
735 return err;
736
737 external_mode = !!val;
738
Jeremy Kerre5538132018-02-20 14:48:44 +1030739 mutex_lock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030740
741 if (external_mode == master->external_mode) {
Jeremy Kerre5538132018-02-20 14:48:44 +1030742 mutex_unlock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030743 return count;
744 }
745
746 master->external_mode = external_mode;
747 if (master->external_mode)
748 fsi_master_gpio_init_external(master);
749 else
750 fsi_master_gpio_init(master);
Jeremy Kerre5538132018-02-20 14:48:44 +1030751
752 mutex_unlock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030753
754 fsi_master_rescan(&master->master);
755
756 return count;
757}
758
759static DEVICE_ATTR(external_mode, 0664,
760 external_mode_show, external_mode_store);
761
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000762static void fsi_master_gpio_release(struct device *dev)
763{
764 struct fsi_master_gpio *master = to_fsi_master_gpio(dev_to_fsi_master(dev));
765
766 of_node_put(dev_of_node(master->dev));
767
768 kfree(master);
769}
770
Christopher Bosticac0385d2017-06-06 16:08:54 -0500771static int fsi_master_gpio_probe(struct platform_device *pdev)
772{
773 struct fsi_master_gpio *master;
774 struct gpio_desc *gpio;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030775 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500776
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000777 master = kzalloc(sizeof(*master), GFP_KERNEL);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500778 if (!master)
779 return -ENOMEM;
780
781 master->dev = &pdev->dev;
782 master->master.dev.parent = master->dev;
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030783 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000784 master->master.dev.release = fsi_master_gpio_release;
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800785 master->last_addr = LAST_ADDR_INVALID;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500786
787 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
788 if (IS_ERR(gpio)) {
789 dev_err(&pdev->dev, "failed to get clock gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000790 rc = PTR_ERR(gpio);
791 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500792 }
793 master->gpio_clk = gpio;
794
795 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
796 if (IS_ERR(gpio)) {
797 dev_err(&pdev->dev, "failed to get data gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000798 rc = PTR_ERR(gpio);
799 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500800 }
801 master->gpio_data = gpio;
802
803 /* Optional GPIOs */
804 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
805 if (IS_ERR(gpio)) {
806 dev_err(&pdev->dev, "failed to get trans gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000807 rc = PTR_ERR(gpio);
808 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500809 }
810 master->gpio_trans = gpio;
811
812 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
813 if (IS_ERR(gpio)) {
814 dev_err(&pdev->dev, "failed to get enable gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000815 rc = PTR_ERR(gpio);
816 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500817 }
818 master->gpio_enable = gpio;
819
820 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
821 if (IS_ERR(gpio)) {
822 dev_err(&pdev->dev, "failed to get mux gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000823 rc = PTR_ERR(gpio);
824 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500825 }
826 master->gpio_mux = gpio;
827
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +1000828 /*
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.
832 */
833 master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
834
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000835 /* Default FSI command delays */
836 master->t_send_delay = FSI_SEND_DELAY_CLOCKS;
837 master->t_echo_delay = FSI_ECHO_DELAY_CLOCKS;
838
Christopher Bosticac0385d2017-06-06 16:08:54 -0500839 master->master.n_links = 1;
Jeremy Kerr4af889b2017-06-06 16:08:58 -0500840 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500841 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;
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000846 master->master.link_config = fsi_master_gpio_link_config;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500847 platform_set_drvdata(pdev, master);
Jeremy Kerre5538132018-02-20 14:48:44 +1030848 mutex_init(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500849
850 fsi_master_gpio_init(master);
851
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030852 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
853 if (rc)
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000854 goto err_free;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030855
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000856 rc = fsi_master_register(&master->master);
857 if (rc) {
858 device_remove_file(&pdev->dev, &dev_attr_external_mode);
859 put_device(&master->master.dev);
860 return rc;
861 }
862 return 0;
863 err_free:
864 kfree(master);
865 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500866}
867
868
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000869
Christopher Bosticac0385d2017-06-06 16:08:54 -0500870static int fsi_master_gpio_remove(struct platform_device *pdev)
871{
872 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
873
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000874 device_remove_file(&pdev->dev, &dev_attr_external_mode);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500875
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000876 fsi_master_unregister(&master->master);
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030877
Christopher Bosticac0385d2017-06-06 16:08:54 -0500878 return 0;
879}
880
881static const struct of_device_id fsi_master_gpio_match[] = {
882 { .compatible = "fsi-master-gpio" },
883 { },
884};
Zou Wei19a52172021-05-13 16:57:29 +0800885MODULE_DEVICE_TABLE(of, fsi_master_gpio_match);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500886
887static struct platform_driver fsi_master_gpio_driver = {
888 .driver = {
889 .name = "fsi-master-gpio",
890 .of_match_table = fsi_master_gpio_match,
891 },
892 .probe = fsi_master_gpio_probe,
893 .remove = fsi_master_gpio_remove,
894};
895
896module_platform_driver(fsi_master_gpio_driver);
897MODULE_LICENSE("GPL");