Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 2 | /* |
| 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 Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 12 | #include <linux/irqflags.h> |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 13 | #include <linux/module.h> |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame] | 14 | #include <linux/of.h> |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/slab.h> |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 17 | |
| 18 | #include "fsi-master.h" |
| 19 | |
| 20 | #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */ |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 21 | #define LAST_ADDR_INVALID 0x1 |
| 22 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 23 | struct fsi_master_gpio { |
| 24 | struct fsi_master master; |
| 25 | struct device *dev; |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 26 | struct mutex cmd_lock; /* mutex for command ordering */ |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 27 | struct gpio_desc *gpio_clk; |
| 28 | struct gpio_desc *gpio_data; |
| 29 | struct gpio_desc *gpio_trans; /* Voltage translator */ |
| 30 | struct gpio_desc *gpio_enable; /* FSI enable */ |
| 31 | struct gpio_desc *gpio_mux; /* Mux control */ |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 32 | bool external_mode; |
Benjamin Herrenschmidt | bc1099d | 2018-05-08 11:06:38 +1000 | [diff] [blame] | 33 | bool no_delays; |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 34 | uint32_t last_addr; |
Benjamin Herrenschmidt | 75854c1 | 2018-05-29 15:11:32 +1000 | [diff] [blame] | 35 | uint8_t t_send_delay; |
| 36 | uint8_t t_echo_delay; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
Jeremy Kerr | 1247cf7 | 2017-06-06 16:08:55 -0500 | [diff] [blame] | 39 | #define CREATE_TRACE_POINTS |
| 40 | #include <trace/events/fsi_master_gpio.h> |
| 41 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 42 | #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master) |
| 43 | |
| 44 | struct fsi_gpio_msg { |
| 45 | uint64_t msg; |
| 46 | uint8_t bits; |
| 47 | }; |
| 48 | |
| 49 | static void clock_toggle(struct fsi_master_gpio *master, int count) |
| 50 | { |
| 51 | int i; |
| 52 | |
| 53 | for (i = 0; i < count; i++) { |
Benjamin Herrenschmidt | bc1099d | 2018-05-08 11:06:38 +1000 | [diff] [blame] | 54 | if (!master->no_delays) |
| 55 | ndelay(FSI_GPIO_STD_DLY); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 56 | gpiod_set_value(master->gpio_clk, 0); |
Benjamin Herrenschmidt | bc1099d | 2018-05-08 11:06:38 +1000 | [diff] [blame] | 57 | if (!master->no_delays) |
| 58 | ndelay(FSI_GPIO_STD_DLY); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 59 | gpiod_set_value(master->gpio_clk, 1); |
| 60 | } |
| 61 | } |
| 62 | |
Benjamin Herrenschmidt | 5d0d16f | 2018-05-08 11:06:37 +1000 | [diff] [blame] | 63 | static int sda_clock_in(struct fsi_master_gpio *master) |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 64 | { |
| 65 | int in; |
| 66 | |
Benjamin Herrenschmidt | bc1099d | 2018-05-08 11:06:38 +1000 | [diff] [blame] | 67 | if (!master->no_delays) |
| 68 | ndelay(FSI_GPIO_STD_DLY); |
Benjamin Herrenschmidt | 5d0d16f | 2018-05-08 11:06:37 +1000 | [diff] [blame] | 69 | gpiod_set_value(master->gpio_clk, 0); |
Benjamin Herrenschmidt | f3ca483 | 2018-05-15 23:05:58 +1000 | [diff] [blame] | 70 | |
| 71 | /* Dummy read to feed the synchronizers */ |
| 72 | gpiod_get_value(master->gpio_data); |
| 73 | |
| 74 | /* Actual data read */ |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 75 | in = gpiod_get_value(master->gpio_data); |
Benjamin Herrenschmidt | bc1099d | 2018-05-08 11:06:38 +1000 | [diff] [blame] | 76 | if (!master->no_delays) |
| 77 | ndelay(FSI_GPIO_STD_DLY); |
Benjamin Herrenschmidt | 5d0d16f | 2018-05-08 11:06:37 +1000 | [diff] [blame] | 78 | gpiod_set_value(master->gpio_clk, 1); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 79 | return in ? 1 : 0; |
| 80 | } |
| 81 | |
| 82 | static void sda_out(struct fsi_master_gpio *master, int value) |
| 83 | { |
| 84 | gpiod_set_value(master->gpio_data, value); |
| 85 | } |
| 86 | |
| 87 | static 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 | |
| 93 | static 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 | |
| 99 | static void clock_zeros(struct fsi_master_gpio *master, int count) |
| 100 | { |
Benjamin Herrenschmidt | 777fd52 | 2018-05-29 19:28:38 +1000 | [diff] [blame] | 101 | trace_fsi_master_gpio_clock_zeros(master, count); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 102 | set_sda_output(master, 1); |
| 103 | clock_toggle(master, count); |
| 104 | } |
| 105 | |
Benjamin Herrenschmidt | 777fd52 | 2018-05-29 19:28:38 +1000 | [diff] [blame] | 106 | static void echo_delay(struct fsi_master_gpio *master) |
| 107 | { |
| 108 | clock_zeros(master, master->t_echo_delay); |
| 109 | } |
| 110 | |
| 111 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 112 | static 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 Herrenschmidt | 5d0d16f | 2018-05-08 11:06:37 +1000 | [diff] [blame] | 120 | in_bit = sda_clock_in(master); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 121 | msg->msg <<= 1; |
| 122 | msg->msg |= ~in_bit & 0x1; /* Data is active low */ |
| 123 | } |
| 124 | msg->bits += num_bits; |
Jeremy Kerr | 1247cf7 | 2017-06-06 16:08:55 -0500 | [diff] [blame] | 125 | |
| 126 | trace_fsi_master_gpio_in(master, num_bits, msg->msg); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static 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 Kerr | 1247cf7 | 2017-06-06 16:08:55 -0500 | [diff] [blame] | 138 | trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg); |
| 139 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 140 | 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 | |
| 162 | static 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 | |
| 169 | static 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 Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 185 | static bool check_same_address(struct fsi_master_gpio *master, int id, |
| 186 | uint32_t addr) |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 187 | { |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 188 | /* this will also handle LAST_ADDR_INVALID */ |
| 189 | return master->last_addr == (((id & 0x3) << 21) | (addr & ~0x3)); |
| 190 | } |
| 191 | |
| 192 | static 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 | |
| 222 | static 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 | */ |
| 234 | static void build_ar_command(struct fsi_master_gpio *master, |
| 235 | struct fsi_gpio_msg *cmd, uint8_t id, |
| 236 | uint32_t addr, size_t size, const void *data) |
| 237 | { |
| 238 | int i, addr_bits, opcode_bits; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 239 | bool write = !!data; |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 240 | uint8_t ds, opcode; |
| 241 | uint32_t rel_addr; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 242 | |
| 243 | cmd->bits = 0; |
| 244 | cmd->msg = 0; |
| 245 | |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 246 | /* 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 256 | opcode = FSI_CMD_SAME_AR; |
Benjamin Herrenschmidt | 777fd52 | 2018-05-29 19:28:38 +1000 | [diff] [blame] | 257 | trace_fsi_master_gpio_cmd_same_addr(master); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 258 | |
| 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 263 | opcode = FSI_CMD_REL_AR; |
Benjamin Herrenschmidt | 777fd52 | 2018-05-29 19:28:38 +1000 | [diff] [blame] | 264 | trace_fsi_master_gpio_cmd_rel_addr(master, rel_addr); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 265 | |
| 266 | } else { |
| 267 | addr_bits = 21; |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 268 | opcode = FSI_CMD_ABS_AR; |
Benjamin Herrenschmidt | 777fd52 | 2018-05-29 19:28:38 +1000 | [diff] [blame] | 269 | trace_fsi_master_gpio_cmd_abs_addr(master, addr); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 270 | } |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 271 | |
| 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 Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 287 | msg_push_bits(cmd, id, 2); |
| 288 | msg_push_bits(cmd, opcode, opcode_bits); |
| 289 | msg_push_bits(cmd, write ? 0 : 1, 1); |
| 290 | msg_push_bits(cmd, addr, addr_bits); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 291 | msg_push_bits(cmd, ds, 1); |
| 292 | for (i = 0; write && i < size; i++) |
| 293 | msg_push_bits(cmd, ((uint8_t *)data)[i], 8); |
| 294 | |
| 295 | msg_push_crc(cmd); |
| 296 | } |
| 297 | |
| 298 | static 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 304 | msg_push_bits(cmd, FSI_CMD_DPOLL, 3); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 305 | msg_push_crc(cmd); |
| 306 | } |
| 307 | |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 308 | static 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 314 | msg_push_bits(cmd, FSI_CMD_EPOLL, 3); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 315 | msg_push_crc(cmd); |
| 316 | } |
| 317 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 318 | static 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 324 | msg_push_bits(cmd, FSI_CMD_TERM, 6); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 325 | msg_push_crc(cmd); |
| 326 | } |
| 327 | |
| 328 | /* |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 329 | * Note: callers rely specifically on this returning -EAGAIN for |
| 330 | * a CRC error detected in the response. Use other error code |
| 331 | * for other situations. It will be converted to something else |
| 332 | * higher up the stack before it reaches userspace. |
| 333 | */ |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 334 | static 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 338 | unsigned long flags; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 339 | uint32_t crc; |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 340 | uint8_t tag; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 341 | int i; |
| 342 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 343 | local_irq_save(flags); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 344 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 345 | /* wait for the start bit */ |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 346 | for (i = 0; i < FSI_MASTER_MTOE_COUNT; i++) { |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 347 | msg.bits = 0; |
| 348 | msg.msg = 0; |
| 349 | serial_in(master, &msg, 1); |
| 350 | if (msg.msg) |
| 351 | break; |
| 352 | } |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 353 | if (i == FSI_MASTER_MTOE_COUNT) { |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 354 | dev_dbg(master->dev, |
| 355 | "Master time out waiting for response\n"); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 356 | local_irq_restore(flags); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 357 | return -ETIMEDOUT; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | msg.bits = 0; |
| 361 | msg.msg = 0; |
| 362 | |
| 363 | /* Read slave ID & response tag */ |
| 364 | serial_in(master, &msg, 4); |
| 365 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 366 | tag = msg.msg & 0x3; |
| 367 | |
| 368 | /* If we have an ACK and we're expecting data, clock the data in too */ |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 369 | if (tag == FSI_RESP_ACK && data_size) |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 370 | serial_in(master, &msg, data_size * 8); |
| 371 | |
| 372 | /* read CRC */ |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 373 | serial_in(master, &msg, FSI_CRC_SIZE); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 374 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 375 | local_irq_restore(flags); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 376 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 377 | /* 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 Herrenschmidt | c49e344 | 2018-05-15 13:40:21 +1000 | [diff] [blame] | 381 | /* 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 Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 386 | return -EAGAIN; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (msgp) |
| 390 | *msgp = msg; |
| 391 | if (tagp) |
| 392 | *tagp = tag; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int issue_term(struct fsi_master_gpio *master, uint8_t slave) |
| 398 | { |
| 399 | struct fsi_gpio_msg cmd; |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 400 | unsigned long flags; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 401 | uint8_t tag; |
| 402 | int rc; |
| 403 | |
| 404 | build_term_command(&cmd, slave); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 405 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 406 | local_irq_save(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 407 | serial_out(master, &cmd); |
| 408 | echo_delay(master); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 409 | local_irq_restore(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 410 | |
| 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 416 | } else if (tag != FSI_RESP_ACK) { |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 417 | dev_err(master->dev, "TERM failed; response %d\n", tag); |
| 418 | return -EIO; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 429 | unsigned long flags; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 430 | uint8_t tag; |
| 431 | uint8_t *data_byte = data; |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 432 | int crc_err_retries = 0; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 433 | retry: |
| 434 | rc = read_one_response(master, size, &response, &tag); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 435 | |
| 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 Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 451 | local_irq_save(flags); |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 452 | clock_zeros(master, FSI_MASTER_EPOLL_CLOCKS); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 453 | serial_out(master, &cmd); |
| 454 | echo_delay(master); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 455 | local_irq_restore(flags); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 456 | goto retry; |
| 457 | } else if (rc) |
| 458 | goto fail; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 459 | |
| 460 | switch (tag) { |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 461 | case FSI_RESP_ACK: |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 462 | 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 474 | case FSI_RESP_BUSY: |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 475 | /* |
| 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 Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 480 | if (busy_count++ < FSI_MASTER_MAX_BUSY) { |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 481 | build_dpoll_command(&cmd, slave); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 482 | local_irq_save(flags); |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 483 | clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 484 | serial_out(master, &cmd); |
| 485 | echo_delay(master); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 486 | local_irq_restore(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 487 | goto retry; |
| 488 | } |
| 489 | dev_warn(master->dev, |
| 490 | "ERR slave is stuck in busy state, issuing TERM\n"); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 491 | local_irq_save(flags); |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 492 | clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 493 | local_irq_restore(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 494 | issue_term(master, slave); |
| 495 | rc = -EIO; |
| 496 | break; |
| 497 | |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 498 | case FSI_RESP_ERRA: |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 499 | dev_dbg(master->dev, "ERRA received: 0x%x\n", (int)response.msg); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 500 | rc = -EIO; |
| 501 | break; |
Benjamin Herrenschmidt | 55382d3 | 2018-07-12 12:04:24 +1000 | [diff] [blame] | 502 | case FSI_RESP_ERRC: |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 503 | dev_dbg(master->dev, "ERRC received: 0x%x\n", (int)response.msg); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 504 | trace_fsi_master_gpio_crc_cmd_error(master); |
| 505 | rc = -EAGAIN; |
| 506 | break; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Andrew Jeffery | 918da95 | 2018-02-20 14:48:33 +1030 | [diff] [blame] | 509 | if (busy_count > 0) |
| 510 | trace_fsi_master_gpio_poll_response_busy(master, busy_count); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 511 | fail: |
Benjamin Herrenschmidt | edc2485 | 2018-05-29 15:09:45 +1000 | [diff] [blame] | 512 | /* |
| 513 | * tSendDelay clocks, avoids signal reflections when switching |
| 514 | * from receive of response back to send of data. |
| 515 | */ |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 516 | local_irq_save(flags); |
Benjamin Herrenschmidt | 75854c1 | 2018-05-29 15:11:32 +1000 | [diff] [blame] | 517 | clock_zeros(master, master->t_send_delay); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 518 | local_irq_restore(flags); |
| 519 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 520 | return rc; |
| 521 | } |
| 522 | |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 523 | static int send_request(struct fsi_master_gpio *master, |
| 524 | struct fsi_gpio_msg *cmd) |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 525 | { |
| 526 | unsigned long flags; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 527 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 528 | if (master->external_mode) |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 529 | return -EBUSY; |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 530 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 531 | local_irq_save(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 532 | serial_out(master, cmd); |
| 533 | echo_delay(master); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 534 | local_irq_restore(flags); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave, |
| 540 | struct fsi_gpio_msg *cmd, size_t resp_len, void *resp) |
| 541 | { |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 542 | int rc = -EAGAIN, retries = 0; |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 543 | |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 544 | while ((retries++) < FSI_CRC_ERR_RETRIES) { |
| 545 | rc = send_request(master, cmd); |
| 546 | if (rc) |
| 547 | break; |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 548 | rc = poll_for_response(master, slave, resp_len, resp); |
Benjamin Herrenschmidt | 4e56828 | 2018-05-15 16:14:43 +1000 | [diff] [blame] | 549 | 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 557 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 558 | return rc; |
| 559 | } |
| 560 | |
| 561 | static int fsi_master_gpio_read(struct fsi_master *_master, int link, |
| 562 | uint8_t id, uint32_t addr, void *val, size_t size) |
| 563 | { |
| 564 | struct fsi_master_gpio *master = to_fsi_master_gpio(_master); |
| 565 | struct fsi_gpio_msg cmd; |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 566 | int rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 567 | |
| 568 | if (link != 0) |
| 569 | return -ENODEV; |
| 570 | |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 571 | mutex_lock(&master->cmd_lock); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 572 | build_ar_command(master, &cmd, id, addr, size, NULL); |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 573 | rc = fsi_master_gpio_xfer(master, id, &cmd, size, val); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 574 | last_address_update(master, id, rc == 0, addr); |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 575 | mutex_unlock(&master->cmd_lock); |
| 576 | |
| 577 | return rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static int fsi_master_gpio_write(struct fsi_master *_master, int link, |
| 581 | uint8_t id, uint32_t addr, const void *val, size_t size) |
| 582 | { |
| 583 | struct fsi_master_gpio *master = to_fsi_master_gpio(_master); |
| 584 | struct fsi_gpio_msg cmd; |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 585 | int rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 586 | |
| 587 | if (link != 0) |
| 588 | return -ENODEV; |
| 589 | |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 590 | mutex_lock(&master->cmd_lock); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 591 | build_ar_command(master, &cmd, id, addr, size, val); |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 592 | rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 593 | last_address_update(master, id, rc == 0, addr); |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 594 | mutex_unlock(&master->cmd_lock); |
| 595 | |
| 596 | return rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static 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 Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 604 | int rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 605 | |
| 606 | if (link != 0) |
| 607 | return -ENODEV; |
| 608 | |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 609 | mutex_lock(&master->cmd_lock); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 610 | build_term_command(&cmd, id); |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 611 | rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL); |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 612 | last_address_update(master, id, false, 0); |
Jeremy Kerr | 8193fb4 | 2018-05-10 17:22:04 +0800 | [diff] [blame] | 613 | mutex_unlock(&master->cmd_lock); |
| 614 | |
| 615 | return rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int fsi_master_gpio_break(struct fsi_master *_master, int link) |
| 619 | { |
| 620 | struct fsi_master_gpio *master = to_fsi_master_gpio(_master); |
Jeremy Kerr | 29d9b92 | 2018-02-12 15:45:41 +1030 | [diff] [blame] | 621 | unsigned long flags; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 622 | |
| 623 | if (link != 0) |
| 624 | return -ENODEV; |
| 625 | |
Jeremy Kerr | 1247cf7 | 2017-06-06 16:08:55 -0500 | [diff] [blame] | 626 | trace_fsi_master_gpio_break(master); |
| 627 | |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 628 | mutex_lock(&master->cmd_lock); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 629 | if (master->external_mode) { |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 630 | mutex_unlock(&master->cmd_lock); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 631 | return -EBUSY; |
| 632 | } |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 633 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 634 | local_irq_save(flags); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 635 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 636 | 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 644 | |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 645 | local_irq_restore(flags); |
| 646 | |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 647 | last_address_update(master, 0, false, 0); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 648 | mutex_unlock(&master->cmd_lock); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 649 | |
| 650 | /* Wait for logic reset to take effect */ |
| 651 | udelay(200); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static void fsi_master_gpio_init(struct fsi_master_gpio *master) |
| 657 | { |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 658 | unsigned long flags; |
| 659 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 660 | gpiod_direction_output(master->gpio_mux, 1); |
| 661 | gpiod_direction_output(master->gpio_trans, 1); |
| 662 | gpiod_direction_output(master->gpio_enable, 1); |
| 663 | gpiod_direction_output(master->gpio_clk, 1); |
| 664 | gpiod_direction_output(master->gpio_data, 1); |
| 665 | |
| 666 | /* todo: evaluate if clocks can be reduced */ |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 667 | local_irq_save(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 668 | clock_zeros(master, FSI_INIT_CLOCKS); |
Jeremy Kerr | 26d79b2 | 2018-05-24 11:29:56 +0800 | [diff] [blame] | 669 | local_irq_restore(flags); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 670 | } |
| 671 | |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 672 | static 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 James | 04635a3 | 2020-06-09 16:39:27 -0500 | [diff] [blame] | 681 | static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link, |
| 682 | bool enable) |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 683 | { |
| 684 | struct fsi_master_gpio *master = to_fsi_master_gpio(_master); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 685 | int rc = -EBUSY; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 686 | |
| 687 | if (link != 0) |
| 688 | return -ENODEV; |
Jeremy Kerr | 29d9b92 | 2018-02-12 15:45:41 +1030 | [diff] [blame] | 689 | |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 690 | mutex_lock(&master->cmd_lock); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 691 | if (!master->external_mode) { |
Eddie James | 04635a3 | 2020-06-09 16:39:27 -0500 | [diff] [blame] | 692 | gpiod_set_value(master->gpio_enable, enable ? 1 : 0); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 693 | rc = 0; |
| 694 | } |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 695 | mutex_unlock(&master->cmd_lock); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 696 | |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 697 | return rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 698 | } |
| 699 | |
Benjamin Herrenschmidt | 75854c1 | 2018-05-29 15:11:32 +1000 | [diff] [blame] | 700 | static 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 Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 716 | static 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 | |
| 725 | static 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 729 | unsigned long val; |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 730 | 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 739 | mutex_lock(&master->cmd_lock); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 740 | |
| 741 | if (external_mode == master->external_mode) { |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 742 | mutex_unlock(&master->cmd_lock); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 743 | 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 Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 751 | |
| 752 | mutex_unlock(&master->cmd_lock); |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 753 | |
| 754 | fsi_master_rescan(&master->master); |
| 755 | |
| 756 | return count; |
| 757 | } |
| 758 | |
| 759 | static DEVICE_ATTR(external_mode, 0664, |
| 760 | external_mode_show, external_mode_store); |
| 761 | |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 762 | static 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 Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 771 | static int fsi_master_gpio_probe(struct platform_device *pdev) |
| 772 | { |
| 773 | struct fsi_master_gpio *master; |
| 774 | struct gpio_desc *gpio; |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 775 | int rc; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 776 | |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 777 | master = kzalloc(sizeof(*master), GFP_KERNEL); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 778 | if (!master) |
| 779 | return -ENOMEM; |
| 780 | |
| 781 | master->dev = &pdev->dev; |
| 782 | master->master.dev.parent = master->dev; |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame] | 783 | master->master.dev.of_node = of_node_get(dev_of_node(master->dev)); |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 784 | master->master.dev.release = fsi_master_gpio_release; |
Jeremy Kerr | 0e82e5c | 2018-05-10 17:22:05 +0800 | [diff] [blame] | 785 | master->last_addr = LAST_ADDR_INVALID; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 786 | |
| 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 Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 790 | rc = PTR_ERR(gpio); |
| 791 | goto err_free; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 792 | } |
| 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 Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 798 | rc = PTR_ERR(gpio); |
| 799 | goto err_free; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 800 | } |
| 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 Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 807 | rc = PTR_ERR(gpio); |
| 808 | goto err_free; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 809 | } |
| 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 Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 815 | rc = PTR_ERR(gpio); |
| 816 | goto err_free; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 817 | } |
| 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 Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 823 | rc = PTR_ERR(gpio); |
| 824 | goto err_free; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 825 | } |
| 826 | master->gpio_mux = gpio; |
| 827 | |
Benjamin Herrenschmidt | bc1099d | 2018-05-08 11:06:38 +1000 | [diff] [blame] | 828 | /* |
| 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 Herrenschmidt | 75854c1 | 2018-05-29 15:11:32 +1000 | [diff] [blame] | 835 | /* Default FSI command delays */ |
| 836 | master->t_send_delay = FSI_SEND_DELAY_CLOCKS; |
| 837 | master->t_echo_delay = FSI_ECHO_DELAY_CLOCKS; |
| 838 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 839 | master->master.n_links = 1; |
Jeremy Kerr | 4af889b | 2017-06-06 16:08:58 -0500 | [diff] [blame] | 840 | master->master.flags = FSI_MASTER_FLAG_SWCLOCK; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 841 | master->master.read = fsi_master_gpio_read; |
| 842 | master->master.write = fsi_master_gpio_write; |
| 843 | master->master.term = fsi_master_gpio_term; |
| 844 | master->master.send_break = fsi_master_gpio_break; |
| 845 | master->master.link_enable = fsi_master_gpio_link_enable; |
Benjamin Herrenschmidt | 75854c1 | 2018-05-29 15:11:32 +1000 | [diff] [blame] | 846 | master->master.link_config = fsi_master_gpio_link_config; |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 847 | platform_set_drvdata(pdev, master); |
Jeremy Kerr | e553813 | 2018-02-20 14:48:44 +1030 | [diff] [blame] | 848 | mutex_init(&master->cmd_lock); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 849 | |
| 850 | fsi_master_gpio_init(master); |
| 851 | |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 852 | rc = device_create_file(&pdev->dev, &dev_attr_external_mode); |
| 853 | if (rc) |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 854 | goto err_free; |
Jeremy Kerr | b8bd146 | 2018-02-12 15:45:42 +1030 | [diff] [blame] | 855 | |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 856 | 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 Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 869 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 870 | static int fsi_master_gpio_remove(struct platform_device *pdev) |
| 871 | { |
| 872 | struct fsi_master_gpio *master = platform_get_drvdata(pdev); |
| 873 | |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 874 | device_remove_file(&pdev->dev, &dev_attr_external_mode); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 875 | |
Benjamin Herrenschmidt | 8ef9ccf | 2018-06-13 10:05:17 +1000 | [diff] [blame] | 876 | fsi_master_unregister(&master->master); |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame] | 877 | |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static const struct of_device_id fsi_master_gpio_match[] = { |
| 882 | { .compatible = "fsi-master-gpio" }, |
| 883 | { }, |
| 884 | }; |
Zou Wei | 19a5217 | 2021-05-13 16:57:29 +0800 | [diff] [blame] | 885 | MODULE_DEVICE_TABLE(of, fsi_master_gpio_match); |
Christopher Bostic | ac0385d | 2017-06-06 16:08:54 -0500 | [diff] [blame] | 886 | |
| 887 | static 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 | |
| 896 | module_platform_driver(fsi_master_gpio_driver); |
| 897 | MODULE_LICENSE("GPL"); |