blob: 4eb3a766fd4a1fafeb8b55718ddd545a17845322 [file] [log] [blame]
Christopher Bosticac0385d2017-06-06 16:08:54 -05001/*
2 * A FSI master controller, using a simple GPIO bit-banging interface
3 */
4
5#include <linux/crc4.h>
6#include <linux/delay.h>
7#include <linux/device.h>
8#include <linux/fsi.h>
9#include <linux/gpio/consumer.h>
10#include <linux/io.h>
Jeremy Kerr26d79b22018-05-24 11:29:56 +080011#include <linux/irqflags.h>
Christopher Bosticac0385d2017-06-06 16:08:54 -050012#include <linux/module.h>
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +103013#include <linux/of.h>
Christopher Bosticac0385d2017-06-06 16:08:54 -050014#include <linux/platform_device.h>
15#include <linux/slab.h>
Christopher Bosticac0385d2017-06-06 16:08:54 -050016
17#include "fsi-master.h"
18
19#define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +080020#define LAST_ADDR_INVALID 0x1
21
Christopher Bosticac0385d2017-06-06 16:08:54 -050022struct fsi_master_gpio {
23 struct fsi_master master;
24 struct device *dev;
Jeremy Kerre5538132018-02-20 14:48:44 +103025 struct mutex cmd_lock; /* mutex for command ordering */
Christopher Bosticac0385d2017-06-06 16:08:54 -050026 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 */
Jeremy Kerrb8bd1462018-02-12 15:45:42 +103031 bool external_mode;
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100032 bool no_delays;
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +080033 uint32_t last_addr;
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +100034 uint8_t t_send_delay;
35 uint8_t t_echo_delay;
Christopher Bosticac0385d2017-06-06 16:08:54 -050036};
37
Jeremy Kerr1247cf72017-06-06 16:08:55 -050038#define CREATE_TRACE_POINTS
39#include <trace/events/fsi_master_gpio.h>
40
Christopher Bosticac0385d2017-06-06 16:08:54 -050041#define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
42
43struct fsi_gpio_msg {
44 uint64_t msg;
45 uint8_t bits;
46};
47
48static void clock_toggle(struct fsi_master_gpio *master, int count)
49{
50 int i;
51
52 for (i = 0; i < count; i++) {
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100053 if (!master->no_delays)
54 ndelay(FSI_GPIO_STD_DLY);
Christopher Bosticac0385d2017-06-06 16:08:54 -050055 gpiod_set_value(master->gpio_clk, 0);
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100056 if (!master->no_delays)
57 ndelay(FSI_GPIO_STD_DLY);
Christopher Bosticac0385d2017-06-06 16:08:54 -050058 gpiod_set_value(master->gpio_clk, 1);
59 }
60}
61
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +100062static int sda_clock_in(struct fsi_master_gpio *master)
Christopher Bosticac0385d2017-06-06 16:08:54 -050063{
64 int in;
65
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100066 if (!master->no_delays)
67 ndelay(FSI_GPIO_STD_DLY);
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +100068 gpiod_set_value(master->gpio_clk, 0);
Benjamin Herrenschmidtf3ca4832018-05-15 23:05:58 +100069
70 /* Dummy read to feed the synchronizers */
71 gpiod_get_value(master->gpio_data);
72
73 /* Actual data read */
Christopher Bosticac0385d2017-06-06 16:08:54 -050074 in = gpiod_get_value(master->gpio_data);
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +100075 if (!master->no_delays)
76 ndelay(FSI_GPIO_STD_DLY);
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +100077 gpiod_set_value(master->gpio_clk, 1);
Christopher Bosticac0385d2017-06-06 16:08:54 -050078 return in ? 1 : 0;
79}
80
81static void sda_out(struct fsi_master_gpio *master, int value)
82{
83 gpiod_set_value(master->gpio_data, value);
84}
85
86static void set_sda_input(struct fsi_master_gpio *master)
87{
88 gpiod_direction_input(master->gpio_data);
89 gpiod_set_value(master->gpio_trans, 0);
90}
91
92static void set_sda_output(struct fsi_master_gpio *master, int value)
93{
94 gpiod_set_value(master->gpio_trans, 1);
95 gpiod_direction_output(master->gpio_data, value);
96}
97
98static void clock_zeros(struct fsi_master_gpio *master, int count)
99{
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000100 trace_fsi_master_gpio_clock_zeros(master, count);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500101 set_sda_output(master, 1);
102 clock_toggle(master, count);
103}
104
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000105static void echo_delay(struct fsi_master_gpio *master)
106{
107 clock_zeros(master, master->t_echo_delay);
108}
109
110
Christopher Bosticac0385d2017-06-06 16:08:54 -0500111static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
112 uint8_t num_bits)
113{
114 uint8_t bit, in_bit;
115
116 set_sda_input(master);
117
118 for (bit = 0; bit < num_bits; bit++) {
Benjamin Herrenschmidt5d0d16f2018-05-08 11:06:37 +1000119 in_bit = sda_clock_in(master);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500120 msg->msg <<= 1;
121 msg->msg |= ~in_bit & 0x1; /* Data is active low */
122 }
123 msg->bits += num_bits;
Jeremy Kerr1247cf72017-06-06 16:08:55 -0500124
125 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500126}
127
128static void serial_out(struct fsi_master_gpio *master,
129 const struct fsi_gpio_msg *cmd)
130{
131 uint8_t bit;
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;
135 int next_bit;
136
Jeremy Kerr1247cf72017-06-06 16:08:55 -0500137 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
138
Christopher Bosticac0385d2017-06-06 16:08:54 -0500139 if (!cmd->bits) {
140 dev_warn(master->dev, "trying to output 0 bits\n");
141 return;
142 }
143 set_sda_output(master, 0);
144
145 /* Send the start bit */
146 sda_out(master, 0);
147 clock_toggle(master, 1);
148
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);
154 last_bit = next_bit;
155 }
156 clock_toggle(master, 1);
157 msg <<= 1;
158 }
159}
160
161static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
162{
163 msg->msg <<= bits;
164 msg->msg |= data & ((1ull << bits) - 1);
165 msg->bits += bits;
166}
167
168static void msg_push_crc(struct fsi_gpio_msg *msg)
169{
170 uint8_t crc;
171 int top;
172
173 top = msg->bits & 0x3;
174
175 /* start bit, and any non-aligned top bits */
176 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
177
178 /* aligned bits */
179 crc = crc4(crc, msg->msg, msg->bits - top);
180
181 msg_push_bits(msg, crc, 4);
182}
183
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800184static bool check_same_address(struct fsi_master_gpio *master, int id,
185 uint32_t addr)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500186{
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800187 /* this will also handle LAST_ADDR_INVALID */
188 return master->last_addr == (((id & 0x3) << 21) | (addr & ~0x3));
189}
190
191static bool check_relative_address(struct fsi_master_gpio *master, int id,
192 uint32_t addr, uint32_t *rel_addrp)
193{
194 uint32_t last_addr = master->last_addr;
195 int32_t rel_addr;
196
197 if (last_addr == LAST_ADDR_INVALID)
198 return false;
199
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.
203 */
204 if (((last_addr >> 21) & 0x3) != id)
205 return false;
206
207 /* remove the top two bits from any 23-bit addressing */
208 last_addr &= (1 << 21) - 1;
209
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)
214 return false;
215
216 *rel_addrp = (uint32_t)rel_addr;
217
218 return true;
219}
220
221static void last_address_update(struct fsi_master_gpio *master,
222 int id, bool valid, uint32_t addr)
223{
224 if (!valid)
225 master->last_addr = LAST_ADDR_INVALID;
226 else
227 master->last_addr = ((id & 0x3) << 21) | (addr & ~0x3);
228}
229
230/*
231 * Encode an Absolute/Relative/Same Address command
232 */
233static 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)
236{
237 int i, addr_bits, opcode_bits;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500238 bool write = !!data;
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800239 uint8_t ds, opcode;
240 uint32_t rel_addr;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500241
242 cmd->bits = 0;
243 cmd->msg = 0;
244
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800245 /* we have 21 bits of address max */
246 addr &= ((1 << 21) - 1);
247
248 /* cmd opcodes are variable length - SAME_AR is only two bits */
249 opcode_bits = 3;
250
251 if (check_same_address(master, id, addr)) {
252 /* we still address the byte offset within the word */
253 addr_bits = 2;
254 opcode_bits = 2;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000255 opcode = FSI_CMD_SAME_AR;
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000256 trace_fsi_master_gpio_cmd_same_addr(master);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800257
258 } else if (check_relative_address(master, id, addr, &rel_addr)) {
259 /* 8 bits plus sign */
260 addr_bits = 9;
261 addr = rel_addr;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000262 opcode = FSI_CMD_REL_AR;
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000263 trace_fsi_master_gpio_cmd_rel_addr(master, rel_addr);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800264
265 } else {
266 addr_bits = 21;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000267 opcode = FSI_CMD_ABS_AR;
Benjamin Herrenschmidt777fd522018-05-29 19:28:38 +1000268 trace_fsi_master_gpio_cmd_abs_addr(master, addr);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800269 }
Christopher Bosticac0385d2017-06-06 16:08:54 -0500270
271 /*
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.
274 *
275 * size addr:1 addr:0 ds
276 * 1 x x 0
277 * 2 x 0 1
278 * 4 0 1 1
279 *
280 */
281 ds = size > 1 ? 1 : 0;
282 addr &= ~(size - 1);
283 if (size == 4)
284 addr |= 1;
285
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800286 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);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500290 msg_push_bits(cmd, ds, 1);
291 for (i = 0; write && i < size; i++)
292 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
293
294 msg_push_crc(cmd);
295}
296
297static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
298{
299 cmd->bits = 0;
300 cmd->msg = 0;
301
302 msg_push_bits(cmd, slave_id, 2);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000303 msg_push_bits(cmd, FSI_CMD_DPOLL, 3);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500304 msg_push_crc(cmd);
305}
306
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000307static void build_epoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
308{
309 cmd->bits = 0;
310 cmd->msg = 0;
311
312 msg_push_bits(cmd, slave_id, 2);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000313 msg_push_bits(cmd, FSI_CMD_EPOLL, 3);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000314 msg_push_crc(cmd);
315}
316
Christopher Bosticac0385d2017-06-06 16:08:54 -0500317static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
318{
319 cmd->bits = 0;
320 cmd->msg = 0;
321
322 msg_push_bits(cmd, slave_id, 2);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000323 msg_push_bits(cmd, FSI_CMD_TERM, 6);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500324 msg_push_crc(cmd);
325}
326
327/*
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000328 * 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.
332 */
Christopher Bosticac0385d2017-06-06 16:08:54 -0500333static int read_one_response(struct fsi_master_gpio *master,
334 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
335{
336 struct fsi_gpio_msg msg;
Jeremy Kerre5538132018-02-20 14:48:44 +1030337 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500338 uint32_t crc;
Jeremy Kerre5538132018-02-20 14:48:44 +1030339 uint8_t tag;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500340 int i;
341
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800342 local_irq_save(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030343
Christopher Bosticac0385d2017-06-06 16:08:54 -0500344 /* wait for the start bit */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000345 for (i = 0; i < FSI_MASTER_MTOE_COUNT; i++) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500346 msg.bits = 0;
347 msg.msg = 0;
348 serial_in(master, &msg, 1);
349 if (msg.msg)
350 break;
351 }
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000352 if (i == FSI_MASTER_MTOE_COUNT) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500353 dev_dbg(master->dev,
354 "Master time out waiting for response\n");
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800355 local_irq_restore(flags);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000356 return -ETIMEDOUT;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500357 }
358
359 msg.bits = 0;
360 msg.msg = 0;
361
362 /* Read slave ID & response tag */
363 serial_in(master, &msg, 4);
364
Christopher Bosticac0385d2017-06-06 16:08:54 -0500365 tag = msg.msg & 0x3;
366
367 /* If we have an ACK and we're expecting data, clock the data in too */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000368 if (tag == FSI_RESP_ACK && data_size)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500369 serial_in(master, &msg, data_size * 8);
370
371 /* read CRC */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000372 serial_in(master, &msg, FSI_CRC_SIZE);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500373
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800374 local_irq_restore(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030375
Christopher Bosticac0385d2017-06-06 16:08:54 -0500376 /* we have a whole message now; check CRC */
377 crc = crc4(0, 1, 1);
378 crc = crc4(crc, msg.msg, msg.bits);
379 if (crc) {
Benjamin Herrenschmidtc49e3442018-05-15 13:40:21 +1000380 /* Check if it's all 1's, that probably means the host is off */
381 if (((~msg.msg) & ((1ull << msg.bits) - 1)) == 0)
382 return -ENODEV;
383 dev_dbg(master->dev, "ERR response CRC msg: 0x%016llx (%d bits)\n",
384 msg.msg, msg.bits);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000385 return -EAGAIN;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500386 }
387
388 if (msgp)
389 *msgp = msg;
390 if (tagp)
391 *tagp = tag;
392
393 return 0;
394}
395
396static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
397{
398 struct fsi_gpio_msg cmd;
Jeremy Kerre5538132018-02-20 14:48:44 +1030399 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500400 uint8_t tag;
401 int rc;
402
403 build_term_command(&cmd, slave);
Jeremy Kerre5538132018-02-20 14:48:44 +1030404
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800405 local_irq_save(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500406 serial_out(master, &cmd);
407 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800408 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500409
410 rc = read_one_response(master, 0, NULL, &tag);
411 if (rc < 0) {
412 dev_err(master->dev,
413 "TERM failed; lost communication with slave\n");
414 return -EIO;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000415 } else if (tag != FSI_RESP_ACK) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500416 dev_err(master->dev, "TERM failed; response %d\n", tag);
417 return -EIO;
418 }
419
420 return 0;
421}
422
423static int poll_for_response(struct fsi_master_gpio *master,
424 uint8_t slave, uint8_t size, void *data)
425{
426 struct fsi_gpio_msg response, cmd;
427 int busy_count = 0, rc, i;
Jeremy Kerre5538132018-02-20 14:48:44 +1030428 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500429 uint8_t tag;
430 uint8_t *data_byte = data;
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000431 int crc_err_retries = 0;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500432retry:
433 rc = read_one_response(master, size, &response, &tag);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000434
435 /* Handle retries on CRC errors */
436 if (rc == -EAGAIN) {
437 /* Too many retries ? */
438 if (crc_err_retries++ > FSI_CRC_ERR_RETRIES) {
439 /*
440 * Pass it up as a -EIO otherwise upper level will retry
441 * the whole command which isn't what we want here.
442 */
443 rc = -EIO;
444 goto fail;
445 }
446 dev_dbg(master->dev,
447 "CRC error retry %d\n", crc_err_retries);
448 trace_fsi_master_gpio_crc_rsp_error(master);
449 build_epoll_command(&cmd, slave);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800450 local_irq_save(flags);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000451 clock_zeros(master, FSI_MASTER_EPOLL_CLOCKS);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000452 serial_out(master, &cmd);
453 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800454 local_irq_restore(flags);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000455 goto retry;
456 } else if (rc)
457 goto fail;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500458
459 switch (tag) {
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000460 case FSI_RESP_ACK:
Christopher Bosticac0385d2017-06-06 16:08:54 -0500461 if (size && data) {
462 uint64_t val = response.msg;
463 /* clear crc & mask */
464 val >>= 4;
465 val &= (1ull << (size * 8)) - 1;
466
467 for (i = 0; i < size; i++) {
468 data_byte[size-i-1] = val;
469 val >>= 8;
470 }
471 }
472 break;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000473 case FSI_RESP_BUSY:
Christopher Bosticac0385d2017-06-06 16:08:54 -0500474 /*
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.
478 */
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000479 if (busy_count++ < FSI_MASTER_MAX_BUSY) {
Christopher Bosticac0385d2017-06-06 16:08:54 -0500480 build_dpoll_command(&cmd, slave);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800481 local_irq_save(flags);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000482 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500483 serial_out(master, &cmd);
484 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800485 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500486 goto retry;
487 }
488 dev_warn(master->dev,
489 "ERR slave is stuck in busy state, issuing TERM\n");
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800490 local_irq_save(flags);
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000491 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800492 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500493 issue_term(master, slave);
494 rc = -EIO;
495 break;
496
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000497 case FSI_RESP_ERRA:
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000498 dev_dbg(master->dev, "ERRA received: 0x%x\n", (int)response.msg);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500499 rc = -EIO;
500 break;
Benjamin Herrenschmidt55382d32018-07-12 12:04:24 +1000501 case FSI_RESP_ERRC:
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000502 dev_dbg(master->dev, "ERRC received: 0x%x\n", (int)response.msg);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000503 trace_fsi_master_gpio_crc_cmd_error(master);
504 rc = -EAGAIN;
505 break;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500506 }
507
Andrew Jeffery918da952018-02-20 14:48:33 +1030508 if (busy_count > 0)
509 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000510 fail:
Benjamin Herrenschmidtedc24852018-05-29 15:09:45 +1000511 /*
512 * tSendDelay clocks, avoids signal reflections when switching
513 * from receive of response back to send of data.
514 */
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800515 local_irq_save(flags);
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000516 clock_zeros(master, master->t_send_delay);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800517 local_irq_restore(flags);
518
Christopher Bosticac0385d2017-06-06 16:08:54 -0500519 return rc;
520}
521
Jeremy Kerre5538132018-02-20 14:48:44 +1030522static int send_request(struct fsi_master_gpio *master,
523 struct fsi_gpio_msg *cmd)
Christopher Bosticac0385d2017-06-06 16:08:54 -0500524{
525 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500526
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800527 if (master->external_mode)
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030528 return -EBUSY;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030529
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800530 local_irq_save(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500531 serial_out(master, cmd);
532 echo_delay(master);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800533 local_irq_restore(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030534
535 return 0;
536}
537
538static 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)
540{
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000541 int rc = -EAGAIN, retries = 0;
Jeremy Kerre5538132018-02-20 14:48:44 +1030542
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000543 while ((retries++) < FSI_CRC_ERR_RETRIES) {
544 rc = send_request(master, cmd);
545 if (rc)
546 break;
Jeremy Kerre5538132018-02-20 14:48:44 +1030547 rc = poll_for_response(master, slave, resp_len, resp);
Benjamin Herrenschmidt4e568282018-05-15 16:14:43 +1000548 if (rc != -EAGAIN)
549 break;
550 rc = -EIO;
551 dev_warn(master->dev, "ECRC retry %d\n", retries);
552
553 /* Pace it a bit before retry */
554 msleep(1);
555 }
Jeremy Kerre5538132018-02-20 14:48:44 +1030556
Christopher Bosticac0385d2017-06-06 16:08:54 -0500557 return rc;
558}
559
560static int fsi_master_gpio_read(struct fsi_master *_master, int link,
561 uint8_t id, uint32_t addr, void *val, size_t size)
562{
563 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
564 struct fsi_gpio_msg cmd;
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800565 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500566
567 if (link != 0)
568 return -ENODEV;
569
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800570 mutex_lock(&master->cmd_lock);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800571 build_ar_command(master, &cmd, id, addr, size, NULL);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800572 rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800573 last_address_update(master, id, rc == 0, addr);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800574 mutex_unlock(&master->cmd_lock);
575
576 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500577}
578
579static int fsi_master_gpio_write(struct fsi_master *_master, int link,
580 uint8_t id, uint32_t addr, const void *val, size_t size)
581{
582 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
583 struct fsi_gpio_msg cmd;
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800584 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500585
586 if (link != 0)
587 return -ENODEV;
588
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800589 mutex_lock(&master->cmd_lock);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800590 build_ar_command(master, &cmd, id, addr, size, val);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800591 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800592 last_address_update(master, id, rc == 0, addr);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800593 mutex_unlock(&master->cmd_lock);
594
595 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500596}
597
598static int fsi_master_gpio_term(struct fsi_master *_master,
599 int link, uint8_t id)
600{
601 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
602 struct fsi_gpio_msg cmd;
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800603 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500604
605 if (link != 0)
606 return -ENODEV;
607
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800608 mutex_lock(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500609 build_term_command(&cmd, id);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800610 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800611 last_address_update(master, id, false, 0);
Jeremy Kerr8193fb42018-05-10 17:22:04 +0800612 mutex_unlock(&master->cmd_lock);
613
614 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500615}
616
617static int fsi_master_gpio_break(struct fsi_master *_master, int link)
618{
619 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
Jeremy Kerr29d9b922018-02-12 15:45:41 +1030620 unsigned long flags;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500621
622 if (link != 0)
623 return -ENODEV;
624
Jeremy Kerr1247cf72017-06-06 16:08:55 -0500625 trace_fsi_master_gpio_break(master);
626
Jeremy Kerre5538132018-02-20 14:48:44 +1030627 mutex_lock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030628 if (master->external_mode) {
Jeremy Kerre5538132018-02-20 14:48:44 +1030629 mutex_unlock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030630 return -EBUSY;
631 }
Jeremy Kerre5538132018-02-20 14:48:44 +1030632
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800633 local_irq_save(flags);
Jeremy Kerre5538132018-02-20 14:48:44 +1030634
Christopher Bosticac0385d2017-06-06 16:08:54 -0500635 set_sda_output(master, 1);
636 sda_out(master, 1);
637 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
638 sda_out(master, 0);
639 clock_toggle(master, FSI_BREAK_CLOCKS);
640 echo_delay(master);
641 sda_out(master, 1);
642 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
Jeremy Kerre5538132018-02-20 14:48:44 +1030643
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800644 local_irq_restore(flags);
645
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800646 last_address_update(master, 0, false, 0);
Jeremy Kerre5538132018-02-20 14:48:44 +1030647 mutex_unlock(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500648
649 /* Wait for logic reset to take effect */
650 udelay(200);
651
652 return 0;
653}
654
655static void fsi_master_gpio_init(struct fsi_master_gpio *master)
656{
Jeremy Kerre5538132018-02-20 14:48:44 +1030657 unsigned long flags;
658
Christopher Bosticac0385d2017-06-06 16:08:54 -0500659 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);
664
665 /* todo: evaluate if clocks can be reduced */
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800666 local_irq_save(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500667 clock_zeros(master, FSI_INIT_CLOCKS);
Jeremy Kerr26d79b22018-05-24 11:29:56 +0800668 local_irq_restore(flags);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500669}
670
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030671static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
672{
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);
678}
679
Christopher Bosticac0385d2017-06-06 16:08:54 -0500680static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
681{
682 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030683 int rc = -EBUSY;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500684
685 if (link != 0)
686 return -ENODEV;
Jeremy Kerr29d9b922018-02-12 15:45:41 +1030687
Jeremy Kerre5538132018-02-20 14:48:44 +1030688 mutex_lock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030689 if (!master->external_mode) {
690 gpiod_set_value(master->gpio_enable, 1);
691 rc = 0;
692 }
Jeremy Kerre5538132018-02-20 14:48:44 +1030693 mutex_unlock(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500694
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030695 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500696}
697
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000698static int fsi_master_gpio_link_config(struct fsi_master *_master, int link,
699 u8 t_send_delay, u8 t_echo_delay)
700{
701 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
702
703 if (link != 0)
704 return -ENODEV;
705
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);
710
711 return 0;
712}
713
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030714static ssize_t external_mode_show(struct device *dev,
715 struct device_attribute *attr, char *buf)
716{
717 struct fsi_master_gpio *master = dev_get_drvdata(dev);
718
719 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
720 master->external_mode ? 1 : 0);
721}
722
723static ssize_t external_mode_store(struct device *dev,
724 struct device_attribute *attr, const char *buf, size_t count)
725{
726 struct fsi_master_gpio *master = dev_get_drvdata(dev);
Jeremy Kerre5538132018-02-20 14:48:44 +1030727 unsigned long val;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030728 bool external_mode;
729 int err;
730
731 err = kstrtoul(buf, 0, &val);
732 if (err)
733 return err;
734
735 external_mode = !!val;
736
Jeremy Kerre5538132018-02-20 14:48:44 +1030737 mutex_lock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030738
739 if (external_mode == master->external_mode) {
Jeremy Kerre5538132018-02-20 14:48:44 +1030740 mutex_unlock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030741 return count;
742 }
743
744 master->external_mode = external_mode;
745 if (master->external_mode)
746 fsi_master_gpio_init_external(master);
747 else
748 fsi_master_gpio_init(master);
Jeremy Kerre5538132018-02-20 14:48:44 +1030749
750 mutex_unlock(&master->cmd_lock);
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030751
752 fsi_master_rescan(&master->master);
753
754 return count;
755}
756
757static DEVICE_ATTR(external_mode, 0664,
758 external_mode_show, external_mode_store);
759
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000760static void fsi_master_gpio_release(struct device *dev)
761{
762 struct fsi_master_gpio *master = to_fsi_master_gpio(dev_to_fsi_master(dev));
763
764 of_node_put(dev_of_node(master->dev));
765
766 kfree(master);
767}
768
Christopher Bosticac0385d2017-06-06 16:08:54 -0500769static int fsi_master_gpio_probe(struct platform_device *pdev)
770{
771 struct fsi_master_gpio *master;
772 struct gpio_desc *gpio;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030773 int rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500774
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000775 master = kzalloc(sizeof(*master), GFP_KERNEL);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500776 if (!master)
777 return -ENOMEM;
778
779 master->dev = &pdev->dev;
780 master->master.dev.parent = master->dev;
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030781 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000782 master->master.dev.release = fsi_master_gpio_release;
Jeremy Kerr0e82e5c2018-05-10 17:22:05 +0800783 master->last_addr = LAST_ADDR_INVALID;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500784
785 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
786 if (IS_ERR(gpio)) {
787 dev_err(&pdev->dev, "failed to get clock gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000788 rc = PTR_ERR(gpio);
789 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500790 }
791 master->gpio_clk = gpio;
792
793 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
794 if (IS_ERR(gpio)) {
795 dev_err(&pdev->dev, "failed to get data gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000796 rc = PTR_ERR(gpio);
797 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500798 }
799 master->gpio_data = gpio;
800
801 /* Optional GPIOs */
802 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
803 if (IS_ERR(gpio)) {
804 dev_err(&pdev->dev, "failed to get trans gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000805 rc = PTR_ERR(gpio);
806 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500807 }
808 master->gpio_trans = gpio;
809
810 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
811 if (IS_ERR(gpio)) {
812 dev_err(&pdev->dev, "failed to get enable gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000813 rc = PTR_ERR(gpio);
814 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500815 }
816 master->gpio_enable = gpio;
817
818 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
819 if (IS_ERR(gpio)) {
820 dev_err(&pdev->dev, "failed to get mux gpio\n");
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000821 rc = PTR_ERR(gpio);
822 goto err_free;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500823 }
824 master->gpio_mux = gpio;
825
Benjamin Herrenschmidtbc1099d2018-05-08 11:06:38 +1000826 /*
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.
830 */
831 master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
832
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000833 /* Default FSI command delays */
834 master->t_send_delay = FSI_SEND_DELAY_CLOCKS;
835 master->t_echo_delay = FSI_ECHO_DELAY_CLOCKS;
836
Christopher Bosticac0385d2017-06-06 16:08:54 -0500837 master->master.n_links = 1;
Jeremy Kerr4af889b2017-06-06 16:08:58 -0500838 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500839 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;
Benjamin Herrenschmidt75854c12018-05-29 15:11:32 +1000844 master->master.link_config = fsi_master_gpio_link_config;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500845 platform_set_drvdata(pdev, master);
Jeremy Kerre5538132018-02-20 14:48:44 +1030846 mutex_init(&master->cmd_lock);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500847
848 fsi_master_gpio_init(master);
849
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030850 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
851 if (rc)
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000852 goto err_free;
Jeremy Kerrb8bd1462018-02-12 15:45:42 +1030853
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000854 rc = fsi_master_register(&master->master);
855 if (rc) {
856 device_remove_file(&pdev->dev, &dev_attr_external_mode);
857 put_device(&master->master.dev);
858 return rc;
859 }
860 return 0;
861 err_free:
862 kfree(master);
863 return rc;
Christopher Bosticac0385d2017-06-06 16:08:54 -0500864}
865
866
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000867
Christopher Bosticac0385d2017-06-06 16:08:54 -0500868static int fsi_master_gpio_remove(struct platform_device *pdev)
869{
870 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
871
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000872 device_remove_file(&pdev->dev, &dev_attr_external_mode);
Christopher Bosticac0385d2017-06-06 16:08:54 -0500873
Benjamin Herrenschmidt8ef9ccf2018-06-13 10:05:17 +1000874 fsi_master_unregister(&master->master);
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030875
Christopher Bosticac0385d2017-06-06 16:08:54 -0500876 return 0;
877}
878
879static const struct of_device_id fsi_master_gpio_match[] = {
880 { .compatible = "fsi-master-gpio" },
881 { },
882};
883
884static struct platform_driver fsi_master_gpio_driver = {
885 .driver = {
886 .name = "fsi-master-gpio",
887 .of_match_table = fsi_master_gpio_match,
888 },
889 .probe = fsi_master_gpio_probe,
890 .remove = fsi_master_gpio_remove,
891};
892
893module_platform_driver(fsi_master_gpio_driver);
894MODULE_LICENSE("GPL");