Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * FSI core driver |
| 3 | * |
| 4 | * Copyright (C) IBM Corporation 2016 |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 16 | #include <linux/crc4.h> |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | #include <linux/fsi.h> |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 19 | #include <linux/idr.h> |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 20 | #include <linux/module.h> |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 21 | #include <linux/of.h> |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 22 | #include <linux/slab.h> |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 24 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 25 | #include "fsi-master.h" |
| 26 | |
Jeremy Kerr | 66433b0 | 2017-06-06 16:08:51 -0500 | [diff] [blame] | 27 | #define CREATE_TRACE_POINTS |
| 28 | #include <trace/events/fsi.h> |
| 29 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 30 | #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31) |
| 31 | #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16) |
| 32 | #define FSI_SLAVE_CONF_SLOTS_SHIFT 16 |
| 33 | #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12) |
| 34 | #define FSI_SLAVE_CONF_VERSION_SHIFT 12 |
| 35 | #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4) |
| 36 | #define FSI_SLAVE_CONF_TYPE_SHIFT 4 |
| 37 | #define FSI_SLAVE_CONF_CRC_SHIFT 4 |
| 38 | #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0) |
| 39 | #define FSI_SLAVE_CONF_DATA_BITS 28 |
| 40 | |
Jeremy Kerr | 4efe37f | 2017-06-06 16:08:45 -0500 | [diff] [blame] | 41 | #define FSI_PEEK_BASE 0x410 |
| 42 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 43 | static const int engine_page_size = 0x400; |
| 44 | |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 45 | #define FSI_SLAVE_BASE 0x800 |
| 46 | |
| 47 | /* |
| 48 | * FSI slave engine control register offsets |
| 49 | */ |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 50 | #define FSI_SMODE 0x0 /* R/W: Mode register */ |
| 51 | #define FSI_SISC 0x8 /* R/W: Interrupt condition */ |
| 52 | #define FSI_SSTAT 0x14 /* R : Slave status */ |
Jeremy Kerr | 4af889b | 2017-06-06 16:08:58 -0500 | [diff] [blame] | 53 | #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */ |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * SMODE fields |
| 57 | */ |
| 58 | #define FSI_SMODE_WSC 0x80000000 /* Warm start done */ |
| 59 | #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */ |
| 60 | #define FSI_SMODE_SID_SHIFT 24 /* ID shift */ |
| 61 | #define FSI_SMODE_SID_MASK 3 /* ID Mask */ |
| 62 | #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */ |
| 63 | #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */ |
| 64 | #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */ |
| 65 | #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */ |
| 66 | #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */ |
| 67 | #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */ |
| 68 | |
Jeremy Kerr | 4af889b | 2017-06-06 16:08:58 -0500 | [diff] [blame] | 69 | /* |
| 70 | * LLMODE fields |
| 71 | */ |
| 72 | #define FSI_LLMODE_ASYNC 0x1 |
| 73 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 74 | #define FSI_SLAVE_SIZE_23b 0x800000 |
| 75 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 76 | static DEFINE_IDA(master_ida); |
| 77 | |
Jeremy Kerr | faf0b116 | 2017-06-06 16:08:37 -0500 | [diff] [blame] | 78 | struct fsi_slave { |
| 79 | struct device dev; |
| 80 | struct fsi_master *master; |
| 81 | int id; |
| 82 | int link; |
| 83 | uint32_t size; /* size of slave address space */ |
| 84 | }; |
| 85 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 86 | #define to_fsi_master(d) container_of(d, struct fsi_master, dev) |
Jeremy Kerr | faf0b116 | 2017-06-06 16:08:37 -0500 | [diff] [blame] | 87 | #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev) |
| 88 | |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 89 | static const int slave_retries = 2; |
| 90 | static int discard_errors; |
| 91 | |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 92 | static int fsi_master_read(struct fsi_master *master, int link, |
| 93 | uint8_t slave_id, uint32_t addr, void *val, size_t size); |
| 94 | static int fsi_master_write(struct fsi_master *master, int link, |
| 95 | uint8_t slave_id, uint32_t addr, const void *val, size_t size); |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 96 | static int fsi_master_break(struct fsi_master *master, int link); |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 97 | |
Jeremy Kerr | 4efe37f | 2017-06-06 16:08:45 -0500 | [diff] [blame] | 98 | /* |
| 99 | * fsi_device_read() / fsi_device_write() / fsi_device_peek() |
| 100 | * |
| 101 | * FSI endpoint-device support |
| 102 | * |
| 103 | * Read / write / peek accessors for a client |
| 104 | * |
| 105 | * Parameters: |
| 106 | * dev: Structure passed to FSI client device drivers on probe(). |
| 107 | * addr: FSI address of given device. Client should pass in its base address |
| 108 | * plus desired offset to access its register space. |
| 109 | * val: For read/peek this is the value read at the specified address. For |
| 110 | * write this is value to write to the specified address. |
| 111 | * The data in val must be FSI bus endian (big endian). |
| 112 | * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes. |
| 113 | * Addresses must be aligned on size boundaries or an error will result. |
| 114 | */ |
| 115 | int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val, |
| 116 | size_t size) |
| 117 | { |
| 118 | if (addr > dev->size || size > dev->size || addr > dev->size - size) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | return fsi_slave_read(dev->slave, dev->addr + addr, val, size); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(fsi_device_read); |
| 124 | |
| 125 | int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val, |
| 126 | size_t size) |
| 127 | { |
| 128 | if (addr > dev->size || size > dev->size || addr > dev->size - size) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | return fsi_slave_write(dev->slave, dev->addr + addr, val, size); |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(fsi_device_write); |
| 134 | |
| 135 | int fsi_device_peek(struct fsi_device *dev, void *val) |
| 136 | { |
| 137 | uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t)); |
| 138 | |
| 139 | return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t)); |
| 140 | } |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 141 | |
| 142 | static void fsi_device_release(struct device *_device) |
| 143 | { |
| 144 | struct fsi_device *device = to_fsi_dev(_device); |
| 145 | |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 146 | of_node_put(device->dev.of_node); |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 147 | kfree(device); |
| 148 | } |
| 149 | |
| 150 | static struct fsi_device *fsi_create_device(struct fsi_slave *slave) |
| 151 | { |
| 152 | struct fsi_device *dev; |
| 153 | |
| 154 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 155 | if (!dev) |
| 156 | return NULL; |
| 157 | |
| 158 | dev->dev.parent = &slave->dev; |
| 159 | dev->dev.bus = &fsi_bus_type; |
| 160 | dev->dev.release = fsi_device_release; |
| 161 | |
| 162 | return dev; |
| 163 | } |
| 164 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 165 | /* FSI slave support */ |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 166 | static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp, |
| 167 | uint8_t *idp) |
| 168 | { |
| 169 | uint32_t addr = *addrp; |
| 170 | uint8_t id = *idp; |
| 171 | |
| 172 | if (addr > slave->size) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | /* For 23 bit addressing, we encode the extra two bits in the slave |
| 176 | * id (and the slave's actual ID needs to be 0). |
| 177 | */ |
| 178 | if (addr > 0x1fffff) { |
| 179 | if (slave->id != 0) |
| 180 | return -EINVAL; |
| 181 | id = (addr >> 21) & 0x3; |
| 182 | addr &= 0x1fffff; |
| 183 | } |
| 184 | |
| 185 | *addrp = addr; |
| 186 | *idp = id; |
| 187 | return 0; |
| 188 | } |
| 189 | |
Colin Ian King | ed50a08 | 2017-10-03 09:31:45 +0100 | [diff] [blame] | 190 | static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave) |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 191 | { |
| 192 | struct fsi_master *master = slave->master; |
| 193 | uint32_t irq, stat; |
| 194 | int rc, link; |
| 195 | uint8_t id; |
| 196 | |
| 197 | link = slave->link; |
| 198 | id = slave->id; |
| 199 | |
| 200 | rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC, |
| 201 | &irq, sizeof(irq)); |
| 202 | if (rc) |
| 203 | return rc; |
| 204 | |
| 205 | rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT, |
| 206 | &stat, sizeof(stat)); |
| 207 | if (rc) |
| 208 | return rc; |
| 209 | |
| 210 | dev_info(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n", |
| 211 | be32_to_cpu(stat), be32_to_cpu(irq)); |
| 212 | |
| 213 | /* clear interrupts */ |
| 214 | return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC, |
| 215 | &irq, sizeof(irq)); |
| 216 | } |
| 217 | |
| 218 | static int fsi_slave_set_smode(struct fsi_master *master, int link, int id); |
| 219 | |
Colin Ian King | ed50a08 | 2017-10-03 09:31:45 +0100 | [diff] [blame] | 220 | static int fsi_slave_handle_error(struct fsi_slave *slave, bool write, |
| 221 | uint32_t addr, size_t size) |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 222 | { |
| 223 | struct fsi_master *master = slave->master; |
| 224 | int rc, link; |
| 225 | uint32_t reg; |
| 226 | uint8_t id; |
| 227 | |
| 228 | if (discard_errors) |
| 229 | return -1; |
| 230 | |
| 231 | link = slave->link; |
| 232 | id = slave->id; |
| 233 | |
| 234 | dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]", |
| 235 | write ? "write" : "read", addr, size); |
| 236 | |
| 237 | /* try a simple clear of error conditions, which may fail if we've lost |
| 238 | * communication with the slave |
| 239 | */ |
| 240 | rc = fsi_slave_report_and_clear_errors(slave); |
| 241 | if (!rc) |
| 242 | return 0; |
| 243 | |
| 244 | /* send a TERM and retry */ |
| 245 | if (master->term) { |
| 246 | rc = master->term(master, link, id); |
| 247 | if (!rc) { |
| 248 | rc = fsi_master_read(master, link, id, 0, |
| 249 | ®, sizeof(reg)); |
| 250 | if (!rc) |
| 251 | rc = fsi_slave_report_and_clear_errors(slave); |
| 252 | if (!rc) |
| 253 | return 0; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* getting serious, reset the slave via BREAK */ |
| 258 | rc = fsi_master_break(master, link); |
| 259 | if (rc) |
| 260 | return rc; |
| 261 | |
| 262 | rc = fsi_slave_set_smode(master, link, id); |
| 263 | if (rc) |
| 264 | return rc; |
| 265 | |
| 266 | return fsi_slave_report_and_clear_errors(slave); |
| 267 | } |
| 268 | |
Jeremy Kerr | da36cad | 2017-06-06 16:08:50 -0500 | [diff] [blame] | 269 | int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 270 | void *val, size_t size) |
| 271 | { |
| 272 | uint8_t id = slave->id; |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 273 | int rc, err_rc, i; |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 274 | |
| 275 | rc = fsi_slave_calc_addr(slave, &addr, &id); |
| 276 | if (rc) |
| 277 | return rc; |
| 278 | |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 279 | for (i = 0; i < slave_retries; i++) { |
| 280 | rc = fsi_master_read(slave->master, slave->link, |
| 281 | id, addr, val, size); |
| 282 | if (!rc) |
| 283 | break; |
| 284 | |
| 285 | err_rc = fsi_slave_handle_error(slave, false, addr, size); |
| 286 | if (err_rc) |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | return rc; |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 291 | } |
Jeremy Kerr | da36cad | 2017-06-06 16:08:50 -0500 | [diff] [blame] | 292 | EXPORT_SYMBOL_GPL(fsi_slave_read); |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 293 | |
Jeremy Kerr | da36cad | 2017-06-06 16:08:50 -0500 | [diff] [blame] | 294 | int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 295 | const void *val, size_t size) |
| 296 | { |
| 297 | uint8_t id = slave->id; |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 298 | int rc, err_rc, i; |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 299 | |
| 300 | rc = fsi_slave_calc_addr(slave, &addr, &id); |
| 301 | if (rc) |
| 302 | return rc; |
| 303 | |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 304 | for (i = 0; i < slave_retries; i++) { |
| 305 | rc = fsi_master_write(slave->master, slave->link, |
| 306 | id, addr, val, size); |
| 307 | if (!rc) |
| 308 | break; |
| 309 | |
| 310 | err_rc = fsi_slave_handle_error(slave, true, addr, size); |
| 311 | if (err_rc) |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | return rc; |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 316 | } |
Jeremy Kerr | da36cad | 2017-06-06 16:08:50 -0500 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(fsi_slave_write); |
| 318 | |
| 319 | extern int fsi_slave_claim_range(struct fsi_slave *slave, |
| 320 | uint32_t addr, uint32_t size) |
| 321 | { |
| 322 | if (addr + size < addr) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | if (addr + size > slave->size) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | /* todo: check for overlapping claims */ |
| 329 | return 0; |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(fsi_slave_claim_range); |
| 332 | |
| 333 | extern void fsi_slave_release_range(struct fsi_slave *slave, |
| 334 | uint32_t addr, uint32_t size) |
| 335 | { |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(fsi_slave_release_range); |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 338 | |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 339 | static bool fsi_device_node_matches(struct device *dev, struct device_node *np, |
| 340 | uint32_t addr, uint32_t size) |
| 341 | { |
| 342 | unsigned int len, na, ns; |
| 343 | const __be32 *prop; |
| 344 | uint32_t psize; |
| 345 | |
| 346 | na = of_n_addr_cells(np); |
| 347 | ns = of_n_size_cells(np); |
| 348 | |
| 349 | if (na != 1 || ns != 1) |
| 350 | return false; |
| 351 | |
| 352 | prop = of_get_property(np, "reg", &len); |
| 353 | if (!prop || len != 8) |
| 354 | return false; |
| 355 | |
| 356 | if (of_read_number(prop, 1) != addr) |
| 357 | return false; |
| 358 | |
| 359 | psize = of_read_number(prop + 1, 1); |
| 360 | if (psize != size) { |
| 361 | dev_warn(dev, |
| 362 | "node %s matches probed address, but not size (got 0x%x, expected 0x%x)", |
| 363 | of_node_full_name(np), psize, size); |
| 364 | } |
| 365 | |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | /* Find a matching node for the slave engine at @address, using @size bytes |
| 370 | * of space. Returns NULL if not found, or a matching node with refcount |
| 371 | * already incremented. |
| 372 | */ |
| 373 | static struct device_node *fsi_device_find_of_node(struct fsi_device *dev) |
| 374 | { |
| 375 | struct device_node *parent, *np; |
| 376 | |
| 377 | parent = dev_of_node(&dev->slave->dev); |
| 378 | if (!parent) |
| 379 | return NULL; |
| 380 | |
| 381 | for_each_child_of_node(parent, np) { |
| 382 | if (fsi_device_node_matches(&dev->dev, np, |
| 383 | dev->addr, dev->size)) |
| 384 | return np; |
| 385 | } |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 390 | static int fsi_slave_scan(struct fsi_slave *slave) |
| 391 | { |
| 392 | uint32_t engine_addr; |
| 393 | uint32_t conf; |
| 394 | int rc, i; |
| 395 | |
| 396 | /* |
| 397 | * scan engines |
| 398 | * |
| 399 | * We keep the peek mode and slave engines for the core; so start |
| 400 | * at the third slot in the configuration table. We also need to |
| 401 | * skip the chip ID entry at the start of the address space. |
| 402 | */ |
| 403 | engine_addr = engine_page_size * 3; |
| 404 | for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) { |
| 405 | uint8_t slots, version, type, crc; |
| 406 | struct fsi_device *dev; |
| 407 | |
| 408 | rc = fsi_slave_read(slave, (i + 1) * sizeof(conf), |
| 409 | &conf, sizeof(conf)); |
| 410 | if (rc) { |
| 411 | dev_warn(&slave->dev, |
| 412 | "error reading slave registers\n"); |
| 413 | return -1; |
| 414 | } |
| 415 | conf = be32_to_cpu(conf); |
| 416 | |
| 417 | crc = crc4(0, conf, 32); |
| 418 | if (crc) { |
| 419 | dev_warn(&slave->dev, |
| 420 | "crc error in slave register at 0x%04x\n", |
| 421 | i); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK) |
| 426 | >> FSI_SLAVE_CONF_SLOTS_SHIFT; |
| 427 | version = (conf & FSI_SLAVE_CONF_VERSION_MASK) |
| 428 | >> FSI_SLAVE_CONF_VERSION_SHIFT; |
| 429 | type = (conf & FSI_SLAVE_CONF_TYPE_MASK) |
| 430 | >> FSI_SLAVE_CONF_TYPE_SHIFT; |
| 431 | |
| 432 | /* |
| 433 | * Unused address areas are marked by a zero type value; this |
| 434 | * skips the defined address areas |
| 435 | */ |
| 436 | if (type != 0 && slots != 0) { |
| 437 | |
| 438 | /* create device */ |
| 439 | dev = fsi_create_device(slave); |
| 440 | if (!dev) |
| 441 | return -ENOMEM; |
| 442 | |
| 443 | dev->slave = slave; |
| 444 | dev->engine_type = type; |
| 445 | dev->version = version; |
| 446 | dev->unit = i; |
| 447 | dev->addr = engine_addr; |
| 448 | dev->size = slots * engine_page_size; |
| 449 | |
| 450 | dev_dbg(&slave->dev, |
| 451 | "engine[%i]: type %x, version %x, addr %x size %x\n", |
| 452 | dev->unit, dev->engine_type, version, |
| 453 | dev->addr, dev->size); |
| 454 | |
| 455 | dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x", |
| 456 | slave->master->idx, slave->link, |
| 457 | slave->id, i - 2); |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 458 | dev->dev.of_node = fsi_device_find_of_node(dev); |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 459 | |
| 460 | rc = device_register(&dev->dev); |
| 461 | if (rc) { |
| 462 | dev_warn(&slave->dev, "add failed: %d\n", rc); |
| 463 | put_device(&dev->dev); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | engine_addr += slots * engine_page_size; |
| 468 | |
| 469 | if (!(conf & FSI_SLAVE_CONF_NEXT_MASK)) |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Jeremy Kerr | 125739c | 2017-06-06 16:08:49 -0500 | [diff] [blame] | 476 | static ssize_t fsi_slave_sysfs_raw_read(struct file *file, |
| 477 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 478 | loff_t off, size_t count) |
| 479 | { |
| 480 | struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); |
| 481 | size_t total_len, read_len; |
| 482 | int rc; |
| 483 | |
| 484 | if (off < 0) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | for (total_len = 0; total_len < count; total_len += read_len) { |
| 491 | read_len = min_t(size_t, count, 4); |
| 492 | read_len -= off & 0x3; |
| 493 | |
| 494 | rc = fsi_slave_read(slave, off, buf + total_len, read_len); |
| 495 | if (rc) |
| 496 | return rc; |
| 497 | |
| 498 | off += read_len; |
| 499 | } |
| 500 | |
| 501 | return count; |
| 502 | } |
| 503 | |
| 504 | static ssize_t fsi_slave_sysfs_raw_write(struct file *file, |
| 505 | struct kobject *kobj, struct bin_attribute *attr, |
| 506 | char *buf, loff_t off, size_t count) |
| 507 | { |
| 508 | struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); |
| 509 | size_t total_len, write_len; |
| 510 | int rc; |
| 511 | |
| 512 | if (off < 0) |
| 513 | return -EINVAL; |
| 514 | |
| 515 | if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | for (total_len = 0; total_len < count; total_len += write_len) { |
| 519 | write_len = min_t(size_t, count, 4); |
| 520 | write_len -= off & 0x3; |
| 521 | |
| 522 | rc = fsi_slave_write(slave, off, buf + total_len, write_len); |
| 523 | if (rc) |
| 524 | return rc; |
| 525 | |
| 526 | off += write_len; |
| 527 | } |
| 528 | |
| 529 | return count; |
| 530 | } |
| 531 | |
Bhumika Goyal | 061c095 | 2017-08-02 19:39:32 +0530 | [diff] [blame] | 532 | static const struct bin_attribute fsi_slave_raw_attr = { |
Jeremy Kerr | 125739c | 2017-06-06 16:08:49 -0500 | [diff] [blame] | 533 | .attr = { |
| 534 | .name = "raw", |
| 535 | .mode = 0600, |
| 536 | }, |
| 537 | .size = 0, |
| 538 | .read = fsi_slave_sysfs_raw_read, |
| 539 | .write = fsi_slave_sysfs_raw_write, |
| 540 | }; |
| 541 | |
| 542 | static ssize_t fsi_slave_sysfs_term_write(struct file *file, |
| 543 | struct kobject *kobj, struct bin_attribute *attr, |
| 544 | char *buf, loff_t off, size_t count) |
| 545 | { |
| 546 | struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); |
| 547 | struct fsi_master *master = slave->master; |
| 548 | |
| 549 | if (!master->term) |
| 550 | return -ENODEV; |
| 551 | |
| 552 | master->term(master, slave->link, slave->id); |
| 553 | return count; |
| 554 | } |
| 555 | |
Bhumika Goyal | 061c095 | 2017-08-02 19:39:32 +0530 | [diff] [blame] | 556 | static const struct bin_attribute fsi_slave_term_attr = { |
Jeremy Kerr | 125739c | 2017-06-06 16:08:49 -0500 | [diff] [blame] | 557 | .attr = { |
| 558 | .name = "term", |
| 559 | .mode = 0200, |
| 560 | }, |
| 561 | .size = 0, |
| 562 | .write = fsi_slave_sysfs_term_write, |
| 563 | }; |
| 564 | |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 565 | /* Encode slave local bus echo delay */ |
| 566 | static inline uint32_t fsi_smode_echodly(int x) |
| 567 | { |
| 568 | return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT; |
| 569 | } |
| 570 | |
| 571 | /* Encode slave local bus send delay */ |
| 572 | static inline uint32_t fsi_smode_senddly(int x) |
| 573 | { |
| 574 | return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT; |
| 575 | } |
| 576 | |
| 577 | /* Encode slave local bus clock rate ratio */ |
| 578 | static inline uint32_t fsi_smode_lbcrr(int x) |
| 579 | { |
| 580 | return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT; |
| 581 | } |
| 582 | |
| 583 | /* Encode slave ID */ |
| 584 | static inline uint32_t fsi_smode_sid(int x) |
| 585 | { |
| 586 | return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT; |
| 587 | } |
| 588 | |
Arnd Bergmann | ceb8a12 | 2017-06-20 22:43:42 +0200 | [diff] [blame] | 589 | static uint32_t fsi_slave_smode(int id) |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 590 | { |
| 591 | return FSI_SMODE_WSC | FSI_SMODE_ECRC |
| 592 | | fsi_smode_sid(id) |
| 593 | | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf) |
| 594 | | fsi_smode_lbcrr(0x8); |
| 595 | } |
| 596 | |
| 597 | static int fsi_slave_set_smode(struct fsi_master *master, int link, int id) |
| 598 | { |
| 599 | uint32_t smode; |
| 600 | |
| 601 | /* set our smode register with the slave ID field to 0; this enables |
| 602 | * extended slave addressing |
| 603 | */ |
| 604 | smode = fsi_slave_smode(id); |
| 605 | smode = cpu_to_be32(smode); |
| 606 | |
| 607 | return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SMODE, |
| 608 | &smode, sizeof(smode)); |
| 609 | } |
| 610 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 611 | static void fsi_slave_release(struct device *dev) |
| 612 | { |
| 613 | struct fsi_slave *slave = to_fsi_slave(dev); |
| 614 | |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 615 | of_node_put(dev->of_node); |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 616 | kfree(slave); |
| 617 | } |
| 618 | |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 619 | static bool fsi_slave_node_matches(struct device_node *np, |
| 620 | int link, uint8_t id) |
| 621 | { |
| 622 | unsigned int len, na, ns; |
| 623 | const __be32 *prop; |
| 624 | |
| 625 | na = of_n_addr_cells(np); |
| 626 | ns = of_n_size_cells(np); |
| 627 | |
| 628 | /* Ensure we have the correct format for addresses and sizes in |
| 629 | * reg properties |
| 630 | */ |
| 631 | if (na != 2 || ns != 0) |
| 632 | return false; |
| 633 | |
| 634 | prop = of_get_property(np, "reg", &len); |
| 635 | if (!prop || len != 8) |
| 636 | return false; |
| 637 | |
| 638 | return (of_read_number(prop, 1) == link) && |
| 639 | (of_read_number(prop + 1, 1) == id); |
| 640 | } |
| 641 | |
| 642 | /* Find a matching node for the slave at (link, id). Returns NULL if none |
| 643 | * found, or a matching node with refcount already incremented. |
| 644 | */ |
| 645 | static struct device_node *fsi_slave_find_of_node(struct fsi_master *master, |
| 646 | int link, uint8_t id) |
| 647 | { |
| 648 | struct device_node *parent, *np; |
| 649 | |
| 650 | parent = dev_of_node(&master->dev); |
| 651 | if (!parent) |
| 652 | return NULL; |
| 653 | |
| 654 | for_each_child_of_node(parent, np) { |
| 655 | if (fsi_slave_node_matches(np, link, id)) |
| 656 | return np; |
| 657 | } |
| 658 | |
| 659 | return NULL; |
| 660 | } |
| 661 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 662 | static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id) |
| 663 | { |
Jeremy Kerr | 4af889b | 2017-06-06 16:08:58 -0500 | [diff] [blame] | 664 | uint32_t chip_id, llmode; |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 665 | struct fsi_slave *slave; |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 666 | uint8_t crc; |
| 667 | int rc; |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 668 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 669 | /* Currently, we only support single slaves on a link, and use the |
| 670 | * full 23-bit address range |
| 671 | */ |
| 672 | if (id != 0) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | rc = fsi_master_read(master, link, id, 0, &chip_id, sizeof(chip_id)); |
| 676 | if (rc) { |
| 677 | dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n", |
| 678 | link, id, rc); |
| 679 | return -ENODEV; |
| 680 | } |
| 681 | chip_id = be32_to_cpu(chip_id); |
| 682 | |
| 683 | crc = crc4(0, chip_id, 32); |
| 684 | if (crc) { |
| 685 | dev_warn(&master->dev, "slave %02x:%02x invalid chip id CRC!\n", |
| 686 | link, id); |
| 687 | return -EIO; |
| 688 | } |
| 689 | |
| 690 | dev_info(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n", |
| 691 | chip_id, master->idx, link, id); |
| 692 | |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 693 | rc = fsi_slave_set_smode(master, link, id); |
| 694 | if (rc) { |
| 695 | dev_warn(&master->dev, |
| 696 | "can't set smode on slave:%02x:%02x %d\n", |
| 697 | link, id, rc); |
| 698 | return -ENODEV; |
| 699 | } |
| 700 | |
Jeremy Kerr | 4af889b | 2017-06-06 16:08:58 -0500 | [diff] [blame] | 701 | /* If we're behind a master that doesn't provide a self-running bus |
| 702 | * clock, put the slave into async mode |
| 703 | */ |
| 704 | if (master->flags & FSI_MASTER_FLAG_SWCLOCK) { |
| 705 | llmode = cpu_to_be32(FSI_LLMODE_ASYNC); |
| 706 | rc = fsi_master_write(master, link, id, |
| 707 | FSI_SLAVE_BASE + FSI_LLMODE, |
| 708 | &llmode, sizeof(llmode)); |
| 709 | if (rc) |
| 710 | dev_warn(&master->dev, |
| 711 | "can't set llmode on slave:%02x:%02x %d\n", |
| 712 | link, id, rc); |
| 713 | } |
| 714 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 715 | /* We can communicate with a slave; create the slave device and |
| 716 | * register. |
| 717 | */ |
| 718 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
| 719 | if (!slave) |
| 720 | return -ENOMEM; |
| 721 | |
| 722 | slave->master = master; |
| 723 | slave->dev.parent = &master->dev; |
Jeremy Kerr | f6a2f8e | 2018-02-12 15:45:45 +1030 | [diff] [blame^] | 724 | slave->dev.of_node = fsi_slave_find_of_node(master, link, id); |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 725 | slave->dev.release = fsi_slave_release; |
| 726 | slave->link = link; |
| 727 | slave->id = id; |
| 728 | slave->size = FSI_SLAVE_SIZE_23b; |
| 729 | |
| 730 | dev_set_name(&slave->dev, "slave@%02x:%02x", link, id); |
| 731 | rc = device_register(&slave->dev); |
| 732 | if (rc < 0) { |
| 733 | dev_warn(&master->dev, "failed to create slave device: %d\n", |
| 734 | rc); |
| 735 | put_device(&slave->dev); |
| 736 | return rc; |
| 737 | } |
| 738 | |
Jeremy Kerr | 125739c | 2017-06-06 16:08:49 -0500 | [diff] [blame] | 739 | rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr); |
| 740 | if (rc) |
| 741 | dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc); |
| 742 | |
| 743 | rc = device_create_bin_file(&slave->dev, &fsi_slave_term_attr); |
| 744 | if (rc) |
| 745 | dev_warn(&slave->dev, "failed to create term attr: %d\n", rc); |
| 746 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 747 | rc = fsi_slave_scan(slave); |
| 748 | if (rc) |
| 749 | dev_dbg(&master->dev, "failed during slave scan with: %d\n", |
| 750 | rc); |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 751 | |
| 752 | return rc; |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 753 | } |
| 754 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 755 | /* FSI master support */ |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 756 | static int fsi_check_access(uint32_t addr, size_t size) |
| 757 | { |
Eddie James | 99f039e | 2018-02-12 15:45:43 +1030 | [diff] [blame] | 758 | if (size == 4) { |
| 759 | if (addr & 0x3) |
| 760 | return -EINVAL; |
| 761 | } else if (size == 2) { |
| 762 | if (addr & 0x1) |
| 763 | return -EINVAL; |
| 764 | } else if (size != 1) |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 765 | return -EINVAL; |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int fsi_master_read(struct fsi_master *master, int link, |
| 771 | uint8_t slave_id, uint32_t addr, void *val, size_t size) |
| 772 | { |
| 773 | int rc; |
| 774 | |
Jeremy Kerr | 66433b0 | 2017-06-06 16:08:51 -0500 | [diff] [blame] | 775 | trace_fsi_master_read(master, link, slave_id, addr, size); |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 776 | |
Jeremy Kerr | 66433b0 | 2017-06-06 16:08:51 -0500 | [diff] [blame] | 777 | rc = fsi_check_access(addr, size); |
| 778 | if (!rc) |
| 779 | rc = master->read(master, link, slave_id, addr, val, size); |
| 780 | |
| 781 | trace_fsi_master_rw_result(master, link, slave_id, addr, size, |
| 782 | false, val, rc); |
| 783 | |
| 784 | return rc; |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static int fsi_master_write(struct fsi_master *master, int link, |
| 788 | uint8_t slave_id, uint32_t addr, const void *val, size_t size) |
| 789 | { |
| 790 | int rc; |
| 791 | |
Jeremy Kerr | 66433b0 | 2017-06-06 16:08:51 -0500 | [diff] [blame] | 792 | trace_fsi_master_write(master, link, slave_id, addr, size, val); |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 793 | |
Jeremy Kerr | 66433b0 | 2017-06-06 16:08:51 -0500 | [diff] [blame] | 794 | rc = fsi_check_access(addr, size); |
| 795 | if (!rc) |
| 796 | rc = master->write(master, link, slave_id, addr, val, size); |
| 797 | |
| 798 | trace_fsi_master_rw_result(master, link, slave_id, addr, size, |
| 799 | true, val, rc); |
| 800 | |
| 801 | return rc; |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 802 | } |
| 803 | |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 804 | static int fsi_master_link_enable(struct fsi_master *master, int link) |
| 805 | { |
| 806 | if (master->link_enable) |
| 807 | return master->link_enable(master, link); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Issue a break command on this link |
| 814 | */ |
| 815 | static int fsi_master_break(struct fsi_master *master, int link) |
| 816 | { |
Jeremy Kerr | 66433b0 | 2017-06-06 16:08:51 -0500 | [diff] [blame] | 817 | trace_fsi_master_break(master, link); |
| 818 | |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 819 | if (master->send_break) |
| 820 | return master->send_break(master, link); |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 825 | static int fsi_master_scan(struct fsi_master *master) |
| 826 | { |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 827 | int link, rc; |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 828 | |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 829 | for (link = 0; link < master->n_links; link++) { |
| 830 | rc = fsi_master_link_enable(master, link); |
| 831 | if (rc) { |
| 832 | dev_dbg(&master->dev, |
| 833 | "enable link %d failed: %d\n", link, rc); |
| 834 | continue; |
| 835 | } |
| 836 | rc = fsi_master_break(master, link); |
| 837 | if (rc) { |
| 838 | dev_dbg(&master->dev, |
| 839 | "break to link %d failed: %d\n", link, rc); |
| 840 | continue; |
| 841 | } |
| 842 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 843 | fsi_slave_init(master, link, 0); |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 844 | } |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 849 | static int fsi_slave_remove_device(struct device *dev, void *arg) |
| 850 | { |
| 851 | device_unregister(dev); |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int fsi_master_remove_slave(struct device *dev, void *arg) |
| 856 | { |
| 857 | device_for_each_child(dev, NULL, fsi_slave_remove_device); |
| 858 | device_unregister(dev); |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static void fsi_master_unscan(struct fsi_master *master) |
| 863 | { |
| 864 | device_for_each_child(&master->dev, NULL, fsi_master_remove_slave); |
| 865 | } |
| 866 | |
Jeremy Kerr | 15362d6 | 2018-02-12 15:45:40 +1030 | [diff] [blame] | 867 | int fsi_master_rescan(struct fsi_master *master) |
| 868 | { |
| 869 | fsi_master_unscan(master); |
| 870 | return fsi_master_scan(master); |
| 871 | } |
| 872 | EXPORT_SYMBOL_GPL(fsi_master_rescan); |
| 873 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 874 | static ssize_t master_rescan_store(struct device *dev, |
| 875 | struct device_attribute *attr, const char *buf, size_t count) |
| 876 | { |
| 877 | struct fsi_master *master = to_fsi_master(dev); |
| 878 | int rc; |
| 879 | |
Jeremy Kerr | 15362d6 | 2018-02-12 15:45:40 +1030 | [diff] [blame] | 880 | rc = fsi_master_rescan(master); |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 881 | if (rc < 0) |
| 882 | return rc; |
| 883 | |
| 884 | return count; |
| 885 | } |
| 886 | |
| 887 | static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store); |
| 888 | |
Jeremy Kerr | 125739c | 2017-06-06 16:08:49 -0500 | [diff] [blame] | 889 | static ssize_t master_break_store(struct device *dev, |
| 890 | struct device_attribute *attr, const char *buf, size_t count) |
| 891 | { |
| 892 | struct fsi_master *master = to_fsi_master(dev); |
| 893 | |
| 894 | fsi_master_break(master, 0); |
| 895 | |
| 896 | return count; |
| 897 | } |
| 898 | |
| 899 | static DEVICE_ATTR(break, 0200, NULL, master_break_store); |
| 900 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 901 | int fsi_master_register(struct fsi_master *master) |
| 902 | { |
| 903 | int rc; |
| 904 | |
| 905 | if (!master) |
| 906 | return -EINVAL; |
| 907 | |
| 908 | master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL); |
| 909 | dev_set_name(&master->dev, "fsi%d", master->idx); |
| 910 | |
| 911 | rc = device_register(&master->dev); |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 912 | if (rc) { |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 913 | ida_simple_remove(&master_ida, master->idx); |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 914 | return rc; |
| 915 | } |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 916 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 917 | rc = device_create_file(&master->dev, &dev_attr_rescan); |
| 918 | if (rc) { |
| 919 | device_unregister(&master->dev); |
| 920 | ida_simple_remove(&master_ida, master->idx); |
| 921 | return rc; |
| 922 | } |
| 923 | |
Jeremy Kerr | 125739c | 2017-06-06 16:08:49 -0500 | [diff] [blame] | 924 | rc = device_create_file(&master->dev, &dev_attr_break); |
| 925 | if (rc) { |
| 926 | device_unregister(&master->dev); |
| 927 | ida_simple_remove(&master_ida, master->idx); |
| 928 | return rc; |
| 929 | } |
| 930 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 931 | fsi_master_scan(master); |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 932 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 933 | return 0; |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 934 | } |
| 935 | EXPORT_SYMBOL_GPL(fsi_master_register); |
| 936 | |
| 937 | void fsi_master_unregister(struct fsi_master *master) |
| 938 | { |
| 939 | if (master->idx >= 0) { |
| 940 | ida_simple_remove(&master_ida, master->idx); |
| 941 | master->idx = -1; |
| 942 | } |
| 943 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 944 | fsi_master_unscan(master); |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 945 | device_unregister(&master->dev); |
| 946 | } |
| 947 | EXPORT_SYMBOL_GPL(fsi_master_unregister); |
| 948 | |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 949 | /* FSI core & Linux bus type definitions */ |
| 950 | |
Jeremy Kerr | dd37eed | 2017-02-01 10:53:43 -0600 | [diff] [blame] | 951 | static int fsi_bus_match(struct device *dev, struct device_driver *drv) |
| 952 | { |
| 953 | struct fsi_device *fsi_dev = to_fsi_dev(dev); |
| 954 | struct fsi_driver *fsi_drv = to_fsi_drv(drv); |
| 955 | const struct fsi_device_id *id; |
| 956 | |
| 957 | if (!fsi_drv->id_table) |
| 958 | return 0; |
| 959 | |
| 960 | for (id = fsi_drv->id_table; id->engine_type; id++) { |
| 961 | if (id->engine_type != fsi_dev->engine_type) |
| 962 | continue; |
| 963 | if (id->version == FSI_VERSION_ANY || |
| 964 | id->version == fsi_dev->version) |
| 965 | return 1; |
| 966 | } |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
Christopher Bostic | 356d800 | 2017-06-06 16:08:48 -0500 | [diff] [blame] | 971 | int fsi_driver_register(struct fsi_driver *fsi_drv) |
| 972 | { |
| 973 | if (!fsi_drv) |
| 974 | return -EINVAL; |
| 975 | if (!fsi_drv->id_table) |
| 976 | return -EINVAL; |
| 977 | |
| 978 | return driver_register(&fsi_drv->drv); |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(fsi_driver_register); |
| 981 | |
| 982 | void fsi_driver_unregister(struct fsi_driver *fsi_drv) |
| 983 | { |
| 984 | driver_unregister(&fsi_drv->drv); |
| 985 | } |
| 986 | EXPORT_SYMBOL_GPL(fsi_driver_unregister); |
| 987 | |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 988 | struct bus_type fsi_bus_type = { |
| 989 | .name = "fsi", |
Jeremy Kerr | dd37eed | 2017-02-01 10:53:43 -0600 | [diff] [blame] | 990 | .match = fsi_bus_match, |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 991 | }; |
| 992 | EXPORT_SYMBOL_GPL(fsi_bus_type); |
| 993 | |
Joel Stanley | 496f893 | 2017-07-11 17:00:39 +0930 | [diff] [blame] | 994 | static int __init fsi_init(void) |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 995 | { |
| 996 | return bus_register(&fsi_bus_type); |
| 997 | } |
Joel Stanley | 496f893 | 2017-07-11 17:00:39 +0930 | [diff] [blame] | 998 | postcore_initcall(fsi_init); |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 999 | |
| 1000 | static void fsi_exit(void) |
| 1001 | { |
| 1002 | bus_unregister(&fsi_bus_type); |
| 1003 | } |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 1004 | module_exit(fsi_exit); |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 1005 | module_param(discard_errors, int, 0664); |
Christopher Bostic | acb7e8f | 2017-06-06 16:08:59 -0500 | [diff] [blame] | 1006 | MODULE_LICENSE("GPL"); |
Jeremy Kerr | 1fa847d | 2017-06-06 16:08:52 -0500 | [diff] [blame] | 1007 | MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses"); |