1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * FSI core driver 4 * 5 * Copyright (C) IBM Corporation 2016 6 * 7 * TODO: 8 * - Rework topology 9 * - s/chip_id/chip_loc 10 * - s/cfam/chip (cfam_id -> chip_id etc...) 11 */ 12 13 #include <linux/crc4.h> 14 #include <linux/device.h> 15 #include <linux/fsi.h> 16 #include <linux/idr.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/of_address.h> 20 #include <linux/of_device.h> 21 #include <linux/slab.h> 22 #include <linux/bitops.h> 23 #include <linux/cdev.h> 24 #include <linux/fs.h> 25 #include <linux/uaccess.h> 26 27 #include "fsi-master.h" 28 #include "fsi-slave.h" 29 30 #define CREATE_TRACE_POINTS 31 #include <trace/events/fsi.h> 32 33 #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31) 34 #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16) 35 #define FSI_SLAVE_CONF_SLOTS_SHIFT 16 36 #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12) 37 #define FSI_SLAVE_CONF_VERSION_SHIFT 12 38 #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4) 39 #define FSI_SLAVE_CONF_TYPE_SHIFT 4 40 #define FSI_SLAVE_CONF_CRC_SHIFT 4 41 #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0) 42 #define FSI_SLAVE_CONF_DATA_BITS 28 43 44 #define FSI_PEEK_BASE 0x410 45 46 static const int engine_page_size = 0x400; 47 48 #define FSI_SLAVE_BASE 0x800 49 50 /* 51 * FSI slave engine control register offsets 52 */ 53 #define FSI_SMODE 0x0 /* R/W: Mode register */ 54 #define FSI_SISC 0x8 /* R/W: Interrupt condition */ 55 #define FSI_SSTAT 0x14 /* R : Slave status */ 56 #define FSI_SLBUS 0x30 /* W : LBUS Ownership */ 57 #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */ 58 59 /* 60 * SMODE fields 61 */ 62 #define FSI_SMODE_WSC 0x80000000 /* Warm start done */ 63 #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */ 64 #define FSI_SMODE_SID_SHIFT 24 /* ID shift */ 65 #define FSI_SMODE_SID_MASK 3 /* ID Mask */ 66 #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */ 67 #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */ 68 #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */ 69 #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */ 70 #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */ 71 #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */ 72 73 /* 74 * SLBUS fields 75 */ 76 #define FSI_SLBUS_FORCE 0x80000000 /* Force LBUS ownership */ 77 78 /* 79 * LLMODE fields 80 */ 81 #define FSI_LLMODE_ASYNC 0x1 82 83 #define FSI_SLAVE_SIZE_23b 0x800000 84 85 static DEFINE_IDA(master_ida); 86 87 static const int slave_retries = 2; 88 static int discard_errors; 89 90 static dev_t fsi_base_dev; 91 static DEFINE_IDA(fsi_minor_ida); 92 #define FSI_CHAR_MAX_DEVICES 0x1000 93 94 /* Legacy /dev numbering: 4 devices per chip, 16 chips */ 95 #define FSI_CHAR_LEGACY_TOP 64 96 97 static int fsi_master_read(struct fsi_master *master, int link, 98 uint8_t slave_id, uint32_t addr, void *val, size_t size); 99 static int fsi_master_write(struct fsi_master *master, int link, 100 uint8_t slave_id, uint32_t addr, const void *val, size_t size); 101 static int fsi_master_break(struct fsi_master *master, int link); 102 103 /* FSI core & Linux bus type definitions */ 104 105 static int fsi_bus_match(struct device *dev, const struct device_driver *drv) 106 { 107 struct fsi_device *fsi_dev = to_fsi_dev(dev); 108 const struct fsi_driver *fsi_drv = to_fsi_drv(drv); 109 const struct fsi_device_id *id; 110 111 if (!fsi_drv->id_table) 112 return 0; 113 114 for (id = fsi_drv->id_table; id->engine_type; id++) { 115 if (id->engine_type != fsi_dev->engine_type) 116 continue; 117 if (id->version == FSI_VERSION_ANY || 118 id->version == fsi_dev->version) { 119 if (drv->of_match_table) { 120 if (of_driver_match_device(dev, drv)) 121 return 1; 122 } else { 123 return 1; 124 } 125 } 126 } 127 128 return 0; 129 } 130 131 static int fsi_probe(struct device *dev) 132 { 133 struct fsi_device *fsidev = to_fsi_dev(dev); 134 struct fsi_driver *fsidrv = to_fsi_drv(dev->driver); 135 136 if (fsidrv->probe) 137 return fsidrv->probe(fsidev); 138 else 139 return 0; 140 } 141 142 static void fsi_remove(struct device *dev) 143 { 144 struct fsi_device *fsidev = to_fsi_dev(dev); 145 struct fsi_driver *fsidrv = to_fsi_drv(dev->driver); 146 147 if (fsidrv->remove) 148 fsidrv->remove(fsidev); 149 } 150 151 static const struct bus_type fsi_bus_type = { 152 .name = "fsi", 153 .match = fsi_bus_match, 154 .probe = fsi_probe, 155 .remove = fsi_remove, 156 }; 157 158 /* 159 * fsi_device_read() / fsi_device_write() / fsi_device_peek() 160 * 161 * FSI endpoint-device support 162 * 163 * Read / write / peek accessors for a client 164 * 165 * Parameters: 166 * dev: Structure passed to FSI client device drivers on probe(). 167 * addr: FSI address of given device. Client should pass in its base address 168 * plus desired offset to access its register space. 169 * val: For read/peek this is the value read at the specified address. For 170 * write this is value to write to the specified address. 171 * The data in val must be FSI bus endian (big endian). 172 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes. 173 * Addresses must be aligned on size boundaries or an error will result. 174 */ 175 int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val, 176 size_t size) 177 { 178 if (addr > dev->size || size > dev->size || addr > dev->size - size) 179 return -EINVAL; 180 181 return fsi_slave_read(dev->slave, dev->addr + addr, val, size); 182 } 183 EXPORT_SYMBOL_GPL(fsi_device_read); 184 185 int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val, 186 size_t size) 187 { 188 if (addr > dev->size || size > dev->size || addr > dev->size - size) 189 return -EINVAL; 190 191 return fsi_slave_write(dev->slave, dev->addr + addr, val, size); 192 } 193 EXPORT_SYMBOL_GPL(fsi_device_write); 194 195 int fsi_device_peek(struct fsi_device *dev, void *val) 196 { 197 uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t)); 198 199 return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t)); 200 } 201 202 static void fsi_device_release(struct device *_device) 203 { 204 struct fsi_device *device = to_fsi_dev(_device); 205 206 of_node_put(device->dev.of_node); 207 kfree(device); 208 } 209 210 static struct fsi_device *fsi_create_device(struct fsi_slave *slave) 211 { 212 struct fsi_device *dev; 213 214 dev = kzalloc_obj(*dev); 215 if (!dev) 216 return NULL; 217 218 dev->dev.parent = &slave->dev; 219 dev->dev.bus = &fsi_bus_type; 220 dev->dev.release = fsi_device_release; 221 222 return dev; 223 } 224 225 /* FSI slave support */ 226 static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp, 227 uint8_t *idp) 228 { 229 uint32_t addr = *addrp; 230 uint8_t id = *idp; 231 232 if (addr > slave->size) 233 return -EINVAL; 234 235 /* For 23 bit addressing, we encode the extra two bits in the slave 236 * id (and the slave's actual ID needs to be 0). 237 */ 238 if (addr > 0x1fffff) { 239 if (slave->id != 0) 240 return -EINVAL; 241 id = (addr >> 21) & 0x3; 242 addr &= 0x1fffff; 243 } 244 245 *addrp = addr; 246 *idp = id; 247 return 0; 248 } 249 250 static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave) 251 { 252 struct fsi_master *master = slave->master; 253 __be32 irq, stat; 254 int rc, link; 255 uint8_t id; 256 257 link = slave->link; 258 id = slave->id; 259 260 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC, 261 &irq, sizeof(irq)); 262 if (rc) 263 return rc; 264 265 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT, 266 &stat, sizeof(stat)); 267 if (rc) 268 return rc; 269 270 dev_dbg(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n", 271 be32_to_cpu(stat), be32_to_cpu(irq)); 272 273 /* clear interrupts */ 274 return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC, 275 &irq, sizeof(irq)); 276 } 277 278 /* Encode slave local bus echo delay */ 279 static inline uint32_t fsi_smode_echodly(int x) 280 { 281 return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT; 282 } 283 284 /* Encode slave local bus send delay */ 285 static inline uint32_t fsi_smode_senddly(int x) 286 { 287 return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT; 288 } 289 290 /* Encode slave local bus clock rate ratio */ 291 static inline uint32_t fsi_smode_lbcrr(int x) 292 { 293 return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT; 294 } 295 296 /* Encode slave ID */ 297 static inline uint32_t fsi_smode_sid(int x) 298 { 299 return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT; 300 } 301 302 static uint32_t fsi_slave_smode(int id, u8 t_senddly, u8 t_echodly) 303 { 304 return FSI_SMODE_WSC | FSI_SMODE_ECRC 305 | fsi_smode_sid(id) 306 | fsi_smode_echodly(t_echodly - 1) | fsi_smode_senddly(t_senddly - 1) 307 | fsi_smode_lbcrr(0x8); 308 } 309 310 static int fsi_slave_set_smode(struct fsi_slave *slave) 311 { 312 uint32_t smode; 313 __be32 data; 314 315 /* set our smode register with the slave ID field to 0; this enables 316 * extended slave addressing 317 */ 318 smode = fsi_slave_smode(slave->id, slave->t_send_delay, slave->t_echo_delay); 319 data = cpu_to_be32(smode); 320 321 return fsi_master_write(slave->master, slave->link, slave->id, 322 FSI_SLAVE_BASE + FSI_SMODE, 323 &data, sizeof(data)); 324 } 325 326 static int fsi_slave_handle_error(struct fsi_slave *slave, bool write, 327 uint32_t addr, size_t size) 328 { 329 struct fsi_master *master = slave->master; 330 int rc, link; 331 uint32_t reg; 332 uint8_t id, send_delay, echo_delay; 333 334 if (discard_errors) 335 return -1; 336 337 link = slave->link; 338 id = slave->id; 339 340 dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]", 341 write ? "write" : "read", addr, size); 342 343 /* try a simple clear of error conditions, which may fail if we've lost 344 * communication with the slave 345 */ 346 rc = fsi_slave_report_and_clear_errors(slave); 347 if (!rc) 348 return 0; 349 350 /* send a TERM and retry */ 351 if (master->term) { 352 rc = master->term(master, link, id); 353 if (!rc) { 354 rc = fsi_master_read(master, link, id, 0, 355 ®, sizeof(reg)); 356 if (!rc) 357 rc = fsi_slave_report_and_clear_errors(slave); 358 if (!rc) 359 return 0; 360 } 361 } 362 363 send_delay = slave->t_send_delay; 364 echo_delay = slave->t_echo_delay; 365 366 /* getting serious, reset the slave via BREAK */ 367 rc = fsi_master_break(master, link); 368 if (rc) 369 return rc; 370 371 slave->t_send_delay = send_delay; 372 slave->t_echo_delay = echo_delay; 373 374 rc = fsi_slave_set_smode(slave); 375 if (rc) 376 return rc; 377 378 if (master->link_config) 379 master->link_config(master, link, 380 slave->t_send_delay, 381 slave->t_echo_delay); 382 383 return fsi_slave_report_and_clear_errors(slave); 384 } 385 386 int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, 387 void *val, size_t size) 388 { 389 uint8_t id = slave->id; 390 int rc, err_rc, i; 391 392 rc = fsi_slave_calc_addr(slave, &addr, &id); 393 if (rc) 394 return rc; 395 396 for (i = 0; i < slave_retries; i++) { 397 rc = fsi_master_read(slave->master, slave->link, 398 id, addr, val, size); 399 if (!rc) 400 break; 401 402 err_rc = fsi_slave_handle_error(slave, false, addr, size); 403 if (err_rc) 404 break; 405 } 406 407 return rc; 408 } 409 EXPORT_SYMBOL_GPL(fsi_slave_read); 410 411 int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, 412 const void *val, size_t size) 413 { 414 uint8_t id = slave->id; 415 int rc, err_rc, i; 416 417 rc = fsi_slave_calc_addr(slave, &addr, &id); 418 if (rc) 419 return rc; 420 421 for (i = 0; i < slave_retries; i++) { 422 rc = fsi_master_write(slave->master, slave->link, 423 id, addr, val, size); 424 if (!rc) 425 break; 426 427 err_rc = fsi_slave_handle_error(slave, true, addr, size); 428 if (err_rc) 429 break; 430 } 431 432 return rc; 433 } 434 EXPORT_SYMBOL_GPL(fsi_slave_write); 435 436 int fsi_slave_claim_range(struct fsi_slave *slave, 437 uint32_t addr, uint32_t size) 438 { 439 if (addr + size < addr) 440 return -EINVAL; 441 442 if (addr + size > slave->size) 443 return -EINVAL; 444 445 /* todo: check for overlapping claims */ 446 return 0; 447 } 448 EXPORT_SYMBOL_GPL(fsi_slave_claim_range); 449 450 void fsi_slave_release_range(struct fsi_slave *slave, 451 uint32_t addr, uint32_t size) 452 { 453 } 454 EXPORT_SYMBOL_GPL(fsi_slave_release_range); 455 456 static bool fsi_device_node_matches(struct device *dev, struct device_node *np, 457 uint32_t addr, uint32_t size) 458 { 459 u64 paddr, psize; 460 461 if (of_property_read_reg(np, 0, &paddr, &psize)) 462 return false; 463 464 if (paddr != addr) 465 return false; 466 467 if (psize != size) { 468 dev_warn(dev, 469 "node %pOF matches probed address, but not size (got 0x%llx, expected 0x%x)", 470 np, psize, size); 471 } 472 473 return true; 474 } 475 476 /* Find a matching node for the slave engine at @address, using @size bytes 477 * of space. Returns NULL if not found, or a matching node with refcount 478 * already incremented. 479 */ 480 static struct device_node *fsi_device_find_of_node(struct fsi_device *dev) 481 { 482 struct device_node *parent, *np; 483 484 parent = dev_of_node(&dev->slave->dev); 485 if (!parent) 486 return NULL; 487 488 for_each_child_of_node(parent, np) { 489 if (fsi_device_node_matches(&dev->dev, np, 490 dev->addr, dev->size)) 491 return np; 492 } 493 494 return NULL; 495 } 496 497 static int fsi_slave_scan(struct fsi_slave *slave) 498 { 499 uint32_t engine_addr; 500 int rc, i; 501 502 /* 503 * scan engines 504 * 505 * We keep the peek mode and slave engines for the core; so start 506 * at the third slot in the configuration table. We also need to 507 * skip the chip ID entry at the start of the address space. 508 */ 509 engine_addr = engine_page_size * 3; 510 for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) { 511 uint8_t slots, version, type, crc; 512 struct fsi_device *dev; 513 uint32_t conf; 514 __be32 data; 515 516 rc = fsi_slave_read(slave, (i + 1) * sizeof(data), 517 &data, sizeof(data)); 518 if (rc) { 519 dev_warn(&slave->dev, 520 "error reading slave registers\n"); 521 return -1; 522 } 523 conf = be32_to_cpu(data); 524 525 crc = crc4(0, conf, 32); 526 if (crc) { 527 dev_warn(&slave->dev, 528 "crc error in slave register at 0x%04x\n", 529 i); 530 return -1; 531 } 532 533 slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK) 534 >> FSI_SLAVE_CONF_SLOTS_SHIFT; 535 version = (conf & FSI_SLAVE_CONF_VERSION_MASK) 536 >> FSI_SLAVE_CONF_VERSION_SHIFT; 537 type = (conf & FSI_SLAVE_CONF_TYPE_MASK) 538 >> FSI_SLAVE_CONF_TYPE_SHIFT; 539 540 /* 541 * Unused address areas are marked by a zero type value; this 542 * skips the defined address areas 543 */ 544 if (type != 0 && slots != 0) { 545 546 /* create device */ 547 dev = fsi_create_device(slave); 548 if (!dev) 549 return -ENOMEM; 550 551 dev->slave = slave; 552 dev->engine_type = type; 553 dev->version = version; 554 dev->unit = i; 555 dev->addr = engine_addr; 556 dev->size = slots * engine_page_size; 557 558 trace_fsi_dev_init(dev); 559 560 dev_dbg(&slave->dev, 561 "engine[%i]: type %x, version %x, addr %x size %x\n", 562 dev->unit, dev->engine_type, version, 563 dev->addr, dev->size); 564 565 dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x", 566 slave->master->idx, slave->link, 567 slave->id, i - 2); 568 dev->dev.of_node = fsi_device_find_of_node(dev); 569 570 rc = device_register(&dev->dev); 571 if (rc) { 572 dev_warn(&slave->dev, "add failed: %d\n", rc); 573 put_device(&dev->dev); 574 } 575 } 576 577 engine_addr += slots * engine_page_size; 578 579 if (!(conf & FSI_SLAVE_CONF_NEXT_MASK)) 580 break; 581 } 582 583 return 0; 584 } 585 586 static unsigned long aligned_access_size(size_t offset, size_t count) 587 { 588 unsigned long offset_unit, count_unit; 589 590 /* Criteria: 591 * 592 * 1. Access size must be less than or equal to the maximum access 593 * width or the highest power-of-two factor of offset 594 * 2. Access size must be less than or equal to the amount specified by 595 * count 596 * 597 * The access width is optimal if we can calculate 1 to be strictly 598 * equal while still satisfying 2. 599 */ 600 601 /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */ 602 offset_unit = BIT(__builtin_ctzl(offset | 4)); 603 604 /* Find 2 by the top bit of count */ 605 count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count)); 606 607 /* Constrain the maximum access width to the minimum of both criteria */ 608 return BIT(__builtin_ctzl(offset_unit | count_unit)); 609 } 610 611 static ssize_t fsi_slave_sysfs_raw_read(struct file *file, 612 struct kobject *kobj, const struct bin_attribute *attr, char *buf, 613 loff_t off, size_t count) 614 { 615 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); 616 size_t total_len, read_len; 617 int rc; 618 619 if (off < 0) 620 return -EINVAL; 621 622 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) 623 return -EINVAL; 624 625 for (total_len = 0; total_len < count; total_len += read_len) { 626 read_len = aligned_access_size(off, count - total_len); 627 628 rc = fsi_slave_read(slave, off, buf + total_len, read_len); 629 if (rc) 630 return rc; 631 632 off += read_len; 633 } 634 635 return count; 636 } 637 638 static ssize_t fsi_slave_sysfs_raw_write(struct file *file, 639 struct kobject *kobj, const struct bin_attribute *attr, 640 char *buf, loff_t off, size_t count) 641 { 642 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); 643 size_t total_len, write_len; 644 int rc; 645 646 if (off < 0) 647 return -EINVAL; 648 649 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) 650 return -EINVAL; 651 652 for (total_len = 0; total_len < count; total_len += write_len) { 653 write_len = aligned_access_size(off, count - total_len); 654 655 rc = fsi_slave_write(slave, off, buf + total_len, write_len); 656 if (rc) 657 return rc; 658 659 off += write_len; 660 } 661 662 return count; 663 } 664 665 static const struct bin_attribute fsi_slave_raw_attr = { 666 .attr = { 667 .name = "raw", 668 .mode = 0600, 669 }, 670 .size = 0, 671 .read = fsi_slave_sysfs_raw_read, 672 .write = fsi_slave_sysfs_raw_write, 673 }; 674 675 static void fsi_slave_release(struct device *dev) 676 { 677 struct fsi_slave *slave = to_fsi_slave(dev); 678 679 fsi_free_minor(slave->dev.devt); 680 of_node_put(dev->of_node); 681 kfree(slave); 682 } 683 684 static bool fsi_slave_node_matches(struct device_node *np, 685 int link, uint8_t id) 686 { 687 u64 addr; 688 689 if (of_property_read_reg(np, 0, &addr, NULL)) 690 return false; 691 692 return addr == (((u64)link << 32) | id); 693 } 694 695 /* Find a matching node for the slave at (link, id). Returns NULL if none 696 * found, or a matching node with refcount already incremented. 697 */ 698 static struct device_node *fsi_slave_find_of_node(struct fsi_master *master, 699 int link, uint8_t id) 700 { 701 struct device_node *parent, *np; 702 703 parent = dev_of_node(&master->dev); 704 if (!parent) 705 return NULL; 706 707 for_each_child_of_node(parent, np) { 708 if (fsi_slave_node_matches(np, link, id)) 709 return np; 710 } 711 712 return NULL; 713 } 714 715 static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count, 716 loff_t *offset) 717 { 718 struct fsi_slave *slave = filep->private_data; 719 size_t total_len, read_len; 720 loff_t off = *offset; 721 ssize_t rc; 722 723 if (off < 0) 724 return -EINVAL; 725 726 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) 727 return -EINVAL; 728 729 for (total_len = 0; total_len < count; total_len += read_len) { 730 __be32 data; 731 732 read_len = min_t(size_t, count, 4); 733 read_len -= off & 0x3; 734 735 rc = fsi_slave_read(slave, off, &data, read_len); 736 if (rc) 737 goto fail; 738 rc = copy_to_user(buf + total_len, &data, read_len); 739 if (rc) { 740 rc = -EFAULT; 741 goto fail; 742 } 743 off += read_len; 744 } 745 rc = count; 746 fail: 747 *offset = off; 748 return rc; 749 } 750 751 static ssize_t cfam_write(struct file *filep, const char __user *buf, 752 size_t count, loff_t *offset) 753 { 754 struct fsi_slave *slave = filep->private_data; 755 size_t total_len, write_len; 756 loff_t off = *offset; 757 ssize_t rc; 758 759 760 if (off < 0) 761 return -EINVAL; 762 763 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) 764 return -EINVAL; 765 766 for (total_len = 0; total_len < count; total_len += write_len) { 767 __be32 data; 768 769 write_len = min_t(size_t, count, 4); 770 write_len -= off & 0x3; 771 772 rc = copy_from_user(&data, buf + total_len, write_len); 773 if (rc) { 774 rc = -EFAULT; 775 goto fail; 776 } 777 rc = fsi_slave_write(slave, off, &data, write_len); 778 if (rc) 779 goto fail; 780 off += write_len; 781 } 782 rc = count; 783 fail: 784 *offset = off; 785 return rc; 786 } 787 788 static loff_t cfam_llseek(struct file *file, loff_t offset, int whence) 789 { 790 switch (whence) { 791 case SEEK_CUR: 792 break; 793 case SEEK_SET: 794 file->f_pos = offset; 795 break; 796 default: 797 return -EINVAL; 798 } 799 800 return offset; 801 } 802 803 static int cfam_open(struct inode *inode, struct file *file) 804 { 805 struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev); 806 807 file->private_data = slave; 808 809 return 0; 810 } 811 812 static const struct file_operations cfam_fops = { 813 .owner = THIS_MODULE, 814 .open = cfam_open, 815 .llseek = cfam_llseek, 816 .read = cfam_read, 817 .write = cfam_write, 818 }; 819 820 static ssize_t send_term_store(struct device *dev, 821 struct device_attribute *attr, 822 const char *buf, size_t count) 823 { 824 struct fsi_slave *slave = to_fsi_slave(dev); 825 struct fsi_master *master = slave->master; 826 827 if (!master->term) 828 return -ENODEV; 829 830 master->term(master, slave->link, slave->id); 831 return count; 832 } 833 834 static DEVICE_ATTR_WO(send_term); 835 836 static ssize_t slave_send_echo_show(struct device *dev, 837 struct device_attribute *attr, 838 char *buf) 839 { 840 struct fsi_slave *slave = to_fsi_slave(dev); 841 842 return sprintf(buf, "%u\n", slave->t_send_delay); 843 } 844 845 static ssize_t slave_send_echo_store(struct device *dev, 846 struct device_attribute *attr, const char *buf, size_t count) 847 { 848 struct fsi_slave *slave = to_fsi_slave(dev); 849 struct fsi_master *master = slave->master; 850 unsigned long val; 851 int rc; 852 853 if (kstrtoul(buf, 0, &val) < 0) 854 return -EINVAL; 855 856 if (val < 1 || val > 16) 857 return -EINVAL; 858 859 if (!master->link_config) 860 return -ENXIO; 861 862 /* Current HW mandates that send and echo delay are identical */ 863 slave->t_send_delay = val; 864 slave->t_echo_delay = val; 865 866 rc = fsi_slave_set_smode(slave); 867 if (rc < 0) 868 return rc; 869 if (master->link_config) 870 master->link_config(master, slave->link, 871 slave->t_send_delay, 872 slave->t_echo_delay); 873 874 return count; 875 } 876 877 static DEVICE_ATTR(send_echo_delays, 0600, 878 slave_send_echo_show, slave_send_echo_store); 879 880 static ssize_t chip_id_show(struct device *dev, 881 struct device_attribute *attr, 882 char *buf) 883 { 884 struct fsi_slave *slave = to_fsi_slave(dev); 885 886 return sprintf(buf, "%d\n", slave->chip_id); 887 } 888 889 static DEVICE_ATTR_RO(chip_id); 890 891 static ssize_t cfam_id_show(struct device *dev, 892 struct device_attribute *attr, 893 char *buf) 894 { 895 struct fsi_slave *slave = to_fsi_slave(dev); 896 897 return sprintf(buf, "0x%x\n", slave->cfam_id); 898 } 899 900 static DEVICE_ATTR_RO(cfam_id); 901 902 static struct attribute *cfam_attr[] = { 903 &dev_attr_send_echo_delays.attr, 904 &dev_attr_chip_id.attr, 905 &dev_attr_cfam_id.attr, 906 &dev_attr_send_term.attr, 907 NULL, 908 }; 909 910 static const struct attribute_group cfam_attr_group = { 911 .attrs = cfam_attr, 912 }; 913 914 static const struct attribute_group *cfam_attr_groups[] = { 915 &cfam_attr_group, 916 NULL, 917 }; 918 919 static char *cfam_devnode(const struct device *dev, umode_t *mode, 920 kuid_t *uid, kgid_t *gid) 921 { 922 const struct fsi_slave *slave = to_fsi_slave(dev); 923 924 #ifdef CONFIG_FSI_NEW_DEV_NODE 925 return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx); 926 #else 927 return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx); 928 #endif 929 } 930 931 static const struct device_type cfam_type = { 932 .name = "cfam", 933 .devnode = cfam_devnode, 934 .groups = cfam_attr_groups 935 }; 936 937 static char *fsi_cdev_devnode(const struct device *dev, umode_t *mode, 938 kuid_t *uid, kgid_t *gid) 939 { 940 #ifdef CONFIG_FSI_NEW_DEV_NODE 941 return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev)); 942 #else 943 return kasprintf(GFP_KERNEL, "%s", dev_name(dev)); 944 #endif 945 } 946 947 const struct device_type fsi_cdev_type = { 948 .name = "fsi-cdev", 949 .devnode = fsi_cdev_devnode, 950 }; 951 EXPORT_SYMBOL_GPL(fsi_cdev_type); 952 953 /* Backward compatible /dev/ numbering in "old style" mode */ 954 static int fsi_adjust_index(int index) 955 { 956 #ifdef CONFIG_FSI_NEW_DEV_NODE 957 return index; 958 #else 959 return index + 1; 960 #endif 961 } 962 963 static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type, 964 dev_t *out_dev, int *out_index) 965 { 966 int cid = slave->chip_id; 967 int id; 968 969 /* Check if we qualify for legacy numbering */ 970 if (cid >= 0 && cid < 16 && type < 4) { 971 /* 972 * Try reserving the legacy number, which has 0 - 0x3f reserved 973 * in the ida range. cid goes up to 0xf and type contains two 974 * bits, so construct the id with the below two bit shift. 975 */ 976 id = (cid << 2) | type; 977 id = ida_alloc_range(&fsi_minor_ida, id, id, GFP_KERNEL); 978 if (id >= 0) { 979 *out_index = fsi_adjust_index(cid); 980 *out_dev = fsi_base_dev + id; 981 return 0; 982 } 983 /* Other failure */ 984 if (id != -ENOSPC) 985 return id; 986 /* Fallback to non-legacy allocation */ 987 } 988 id = ida_alloc_range(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP, 989 FSI_CHAR_MAX_DEVICES - 1, GFP_KERNEL); 990 if (id < 0) 991 return id; 992 *out_index = fsi_adjust_index(id); 993 *out_dev = fsi_base_dev + id; 994 return 0; 995 } 996 997 static const char *const fsi_dev_type_names[] = { 998 "cfam", 999 "sbefifo", 1000 "scom", 1001 "occ", 1002 }; 1003 1004 int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type, 1005 dev_t *out_dev, int *out_index) 1006 { 1007 if (fdev->dev.of_node) { 1008 int aid = of_alias_get_id(fdev->dev.of_node, fsi_dev_type_names[type]); 1009 1010 if (aid >= 0) { 1011 /* Use the same scheme as the legacy numbers. */ 1012 int id = (aid << 2) | type; 1013 1014 id = ida_alloc_range(&fsi_minor_ida, id, id, GFP_KERNEL); 1015 if (id >= 0) { 1016 *out_index = aid; 1017 *out_dev = fsi_base_dev + id; 1018 return 0; 1019 } 1020 1021 if (id != -ENOSPC) 1022 return id; 1023 } 1024 } 1025 1026 return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index); 1027 } 1028 EXPORT_SYMBOL_GPL(fsi_get_new_minor); 1029 1030 void fsi_free_minor(dev_t dev) 1031 { 1032 ida_free(&fsi_minor_ida, MINOR(dev)); 1033 } 1034 EXPORT_SYMBOL_GPL(fsi_free_minor); 1035 1036 static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id) 1037 { 1038 uint32_t cfam_id; 1039 struct fsi_slave *slave; 1040 uint8_t crc; 1041 __be32 data, llmode, slbus; 1042 int rc; 1043 1044 /* Currently, we only support single slaves on a link, and use the 1045 * full 23-bit address range 1046 */ 1047 if (id != 0) 1048 return -EINVAL; 1049 1050 rc = fsi_master_read(master, link, id, 0, &data, sizeof(data)); 1051 if (rc) { 1052 dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n", 1053 link, id, rc); 1054 return -ENODEV; 1055 } 1056 cfam_id = be32_to_cpu(data); 1057 1058 crc = crc4(0, cfam_id, 32); 1059 if (crc) { 1060 trace_fsi_slave_invalid_cfam(master, link, cfam_id); 1061 dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n", 1062 link, id); 1063 return -EIO; 1064 } 1065 1066 dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n", 1067 cfam_id, master->idx, link, id); 1068 1069 /* If we're behind a master that doesn't provide a self-running bus 1070 * clock, put the slave into async mode 1071 */ 1072 if (master->flags & FSI_MASTER_FLAG_SWCLOCK) { 1073 llmode = cpu_to_be32(FSI_LLMODE_ASYNC); 1074 rc = fsi_master_write(master, link, id, 1075 FSI_SLAVE_BASE + FSI_LLMODE, 1076 &llmode, sizeof(llmode)); 1077 if (rc) 1078 dev_warn(&master->dev, 1079 "can't set llmode on slave:%02x:%02x %d\n", 1080 link, id, rc); 1081 } 1082 1083 /* We can communicate with a slave; create the slave device and 1084 * register. 1085 */ 1086 slave = kzalloc_obj(*slave); 1087 if (!slave) 1088 return -ENOMEM; 1089 1090 dev_set_name(&slave->dev, "slave@%02x:%02x", link, id); 1091 slave->dev.type = &cfam_type; 1092 slave->dev.parent = &master->dev; 1093 slave->dev.of_node = fsi_slave_find_of_node(master, link, id); 1094 slave->dev.release = fsi_slave_release; 1095 device_initialize(&slave->dev); 1096 slave->cfam_id = cfam_id; 1097 slave->master = master; 1098 slave->link = link; 1099 slave->id = id; 1100 slave->size = FSI_SLAVE_SIZE_23b; 1101 slave->t_send_delay = 16; 1102 slave->t_echo_delay = 16; 1103 1104 /* Get chip ID if any */ 1105 slave->chip_id = -1; 1106 if (slave->dev.of_node) { 1107 uint32_t prop; 1108 if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop)) 1109 slave->chip_id = prop; 1110 1111 } 1112 1113 slbus = cpu_to_be32(FSI_SLBUS_FORCE); 1114 rc = fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SLBUS, 1115 &slbus, sizeof(slbus)); 1116 if (rc) 1117 dev_warn(&master->dev, 1118 "can't set slbus on slave:%02x:%02x %d\n", link, id, 1119 rc); 1120 1121 rc = fsi_slave_set_smode(slave); 1122 if (rc) { 1123 dev_warn(&master->dev, 1124 "can't set smode on slave:%02x:%02x %d\n", 1125 link, id, rc); 1126 goto err_free; 1127 } 1128 1129 /* Allocate a minor in the FSI space */ 1130 rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt, 1131 &slave->cdev_idx); 1132 if (rc) 1133 goto err_free; 1134 1135 trace_fsi_slave_init(slave); 1136 1137 /* Create chardev for userspace access */ 1138 cdev_init(&slave->cdev, &cfam_fops); 1139 rc = cdev_device_add(&slave->cdev, &slave->dev); 1140 if (rc) { 1141 dev_err(&slave->dev, "Error %d creating slave device\n", rc); 1142 goto err_free_ida; 1143 } 1144 1145 /* Now that we have the cdev registered with the core, any fatal 1146 * failures beyond this point will need to clean up through 1147 * cdev_device_del(). Fortunately though, nothing past here is fatal. 1148 */ 1149 1150 if (master->link_config) 1151 master->link_config(master, link, 1152 slave->t_send_delay, 1153 slave->t_echo_delay); 1154 1155 /* Legacy raw file -> to be removed */ 1156 rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr); 1157 if (rc) 1158 dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc); 1159 1160 1161 rc = fsi_slave_scan(slave); 1162 if (rc) 1163 dev_dbg(&master->dev, "failed during slave scan with: %d\n", 1164 rc); 1165 1166 return 0; 1167 1168 err_free_ida: 1169 fsi_free_minor(slave->dev.devt); 1170 err_free: 1171 of_node_put(slave->dev.of_node); 1172 kfree(slave); 1173 return rc; 1174 } 1175 1176 /* FSI master support */ 1177 static int fsi_check_access(uint32_t addr, size_t size) 1178 { 1179 if (size == 4) { 1180 if (addr & 0x3) 1181 return -EINVAL; 1182 } else if (size == 2) { 1183 if (addr & 0x1) 1184 return -EINVAL; 1185 } else if (size != 1) 1186 return -EINVAL; 1187 1188 return 0; 1189 } 1190 1191 static int fsi_master_read(struct fsi_master *master, int link, 1192 uint8_t slave_id, uint32_t addr, void *val, size_t size) 1193 { 1194 int rc; 1195 1196 trace_fsi_master_read(master, link, slave_id, addr, size); 1197 1198 rc = fsi_check_access(addr, size); 1199 if (!rc) 1200 rc = master->read(master, link, slave_id, addr, val, size); 1201 1202 trace_fsi_master_rw_result(master, link, slave_id, addr, size, 1203 false, val, rc); 1204 1205 return rc; 1206 } 1207 1208 static int fsi_master_write(struct fsi_master *master, int link, 1209 uint8_t slave_id, uint32_t addr, const void *val, size_t size) 1210 { 1211 int rc; 1212 1213 trace_fsi_master_write(master, link, slave_id, addr, size, val); 1214 1215 rc = fsi_check_access(addr, size); 1216 if (!rc) 1217 rc = master->write(master, link, slave_id, addr, val, size); 1218 1219 trace_fsi_master_rw_result(master, link, slave_id, addr, size, 1220 true, val, rc); 1221 1222 return rc; 1223 } 1224 1225 static int fsi_master_link_disable(struct fsi_master *master, int link) 1226 { 1227 if (master->link_enable) 1228 return master->link_enable(master, link, false); 1229 1230 return 0; 1231 } 1232 1233 static int fsi_master_link_enable(struct fsi_master *master, int link) 1234 { 1235 if (master->link_enable) 1236 return master->link_enable(master, link, true); 1237 1238 return 0; 1239 } 1240 1241 /* 1242 * Issue a break command on this link 1243 */ 1244 static int fsi_master_break(struct fsi_master *master, int link) 1245 { 1246 int rc = 0; 1247 1248 trace_fsi_master_break(master, link); 1249 1250 if (master->send_break) 1251 rc = master->send_break(master, link); 1252 if (master->link_config) 1253 master->link_config(master, link, 16, 16); 1254 1255 return rc; 1256 } 1257 1258 static int fsi_master_scan(struct fsi_master *master) 1259 { 1260 int link, rc; 1261 1262 trace_fsi_master_scan(master, true); 1263 for (link = 0; link < master->n_links; link++) { 1264 rc = fsi_master_link_enable(master, link); 1265 if (rc) { 1266 dev_dbg(&master->dev, 1267 "enable link %d failed: %d\n", link, rc); 1268 continue; 1269 } 1270 rc = fsi_master_break(master, link); 1271 if (rc) { 1272 fsi_master_link_disable(master, link); 1273 dev_dbg(&master->dev, 1274 "break to link %d failed: %d\n", link, rc); 1275 continue; 1276 } 1277 1278 rc = fsi_slave_init(master, link, 0); 1279 if (rc) 1280 fsi_master_link_disable(master, link); 1281 } 1282 1283 return 0; 1284 } 1285 1286 static int fsi_slave_remove_device(struct device *dev, void *arg) 1287 { 1288 device_unregister(dev); 1289 return 0; 1290 } 1291 1292 static int fsi_master_remove_slave(struct device *dev, void *arg) 1293 { 1294 struct fsi_slave *slave = to_fsi_slave(dev); 1295 1296 device_for_each_child(dev, NULL, fsi_slave_remove_device); 1297 cdev_device_del(&slave->cdev, &slave->dev); 1298 put_device(dev); 1299 return 0; 1300 } 1301 1302 static void fsi_master_unscan(struct fsi_master *master) 1303 { 1304 trace_fsi_master_scan(master, false); 1305 device_for_each_child(&master->dev, NULL, fsi_master_remove_slave); 1306 } 1307 1308 int fsi_master_rescan(struct fsi_master *master) 1309 { 1310 int rc; 1311 1312 mutex_lock(&master->scan_lock); 1313 fsi_master_unscan(master); 1314 rc = fsi_master_scan(master); 1315 mutex_unlock(&master->scan_lock); 1316 1317 return rc; 1318 } 1319 EXPORT_SYMBOL_GPL(fsi_master_rescan); 1320 1321 static ssize_t master_rescan_store(struct device *dev, 1322 struct device_attribute *attr, const char *buf, size_t count) 1323 { 1324 struct fsi_master *master = to_fsi_master(dev); 1325 int rc; 1326 1327 rc = fsi_master_rescan(master); 1328 if (rc < 0) 1329 return rc; 1330 1331 return count; 1332 } 1333 1334 static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store); 1335 1336 static ssize_t master_break_store(struct device *dev, 1337 struct device_attribute *attr, const char *buf, size_t count) 1338 { 1339 struct fsi_master *master = to_fsi_master(dev); 1340 1341 fsi_master_break(master, 0); 1342 1343 return count; 1344 } 1345 1346 static DEVICE_ATTR(break, 0200, NULL, master_break_store); 1347 1348 static struct attribute *master_attrs[] = { 1349 &dev_attr_break.attr, 1350 &dev_attr_rescan.attr, 1351 NULL 1352 }; 1353 1354 ATTRIBUTE_GROUPS(master); 1355 1356 static struct class fsi_master_class = { 1357 .name = "fsi-master", 1358 .dev_groups = master_groups, 1359 }; 1360 1361 int fsi_master_register(struct fsi_master *master) 1362 { 1363 int rc; 1364 struct device_node *np; 1365 1366 mutex_init(&master->scan_lock); 1367 1368 /* Alloc the requested index if it's non-zero */ 1369 if (master->idx) { 1370 master->idx = ida_alloc_range(&master_ida, master->idx, 1371 master->idx, GFP_KERNEL); 1372 } else { 1373 master->idx = ida_alloc(&master_ida, GFP_KERNEL); 1374 } 1375 1376 if (master->idx < 0) 1377 return master->idx; 1378 1379 if (!dev_name(&master->dev)) 1380 dev_set_name(&master->dev, "fsi%d", master->idx); 1381 1382 master->dev.class = &fsi_master_class; 1383 1384 mutex_lock(&master->scan_lock); 1385 rc = device_register(&master->dev); 1386 if (rc) { 1387 ida_free(&master_ida, master->idx); 1388 goto out; 1389 } 1390 1391 np = dev_of_node(&master->dev); 1392 if (!of_property_read_bool(np, "no-scan-on-init")) { 1393 fsi_master_scan(master); 1394 } 1395 out: 1396 mutex_unlock(&master->scan_lock); 1397 return rc; 1398 } 1399 EXPORT_SYMBOL_GPL(fsi_master_register); 1400 1401 void fsi_master_unregister(struct fsi_master *master) 1402 { 1403 int idx = master->idx; 1404 1405 trace_fsi_master_unregister(master); 1406 1407 mutex_lock(&master->scan_lock); 1408 fsi_master_unscan(master); 1409 master->n_links = 0; 1410 mutex_unlock(&master->scan_lock); 1411 1412 device_unregister(&master->dev); 1413 ida_free(&master_ida, idx); 1414 } 1415 EXPORT_SYMBOL_GPL(fsi_master_unregister); 1416 1417 static int fsi_legacy_probe(struct fsi_device *fsidev) 1418 { 1419 struct device *dev = &fsidev->dev; 1420 struct device_driver *driver = dev->driver; 1421 1422 return driver->probe(dev); 1423 } 1424 1425 static void fsi_legacy_remove(struct fsi_device *fsidev) 1426 { 1427 struct device *dev = &fsidev->dev; 1428 struct device_driver *driver = dev->driver; 1429 int ret; 1430 1431 ret = driver->remove(dev); 1432 if (unlikely(ret)) 1433 dev_warn(dev, "Ignoring return value of remove callback (%pe)\n", ERR_PTR(ret)); 1434 } 1435 1436 int fsi_driver_register(struct fsi_driver *fsi_drv) 1437 { 1438 if (!fsi_drv) 1439 return -EINVAL; 1440 if (!fsi_drv->id_table) 1441 return -EINVAL; 1442 1443 fsi_drv->drv.bus = &fsi_bus_type; 1444 1445 /* 1446 * This driver needs updating. Note that driver_register() warns about 1447 * this, so we're not adding another warning here. 1448 */ 1449 if (!fsi_drv->probe && fsi_drv->drv.probe) 1450 fsi_drv->probe = fsi_legacy_probe; 1451 if (!fsi_drv->remove && fsi_drv->drv.remove) 1452 fsi_drv->remove = fsi_legacy_remove; 1453 1454 return driver_register(&fsi_drv->drv); 1455 } 1456 EXPORT_SYMBOL_GPL(fsi_driver_register); 1457 1458 void fsi_driver_unregister(struct fsi_driver *fsi_drv) 1459 { 1460 driver_unregister(&fsi_drv->drv); 1461 } 1462 EXPORT_SYMBOL_GPL(fsi_driver_unregister); 1463 1464 static int __init fsi_init(void) 1465 { 1466 int rc; 1467 1468 rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi"); 1469 if (rc) 1470 return rc; 1471 rc = bus_register(&fsi_bus_type); 1472 if (rc) 1473 goto fail_bus; 1474 1475 rc = class_register(&fsi_master_class); 1476 if (rc) 1477 goto fail_class; 1478 1479 return 0; 1480 1481 fail_class: 1482 bus_unregister(&fsi_bus_type); 1483 fail_bus: 1484 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES); 1485 return rc; 1486 } 1487 postcore_initcall(fsi_init); 1488 1489 static void fsi_exit(void) 1490 { 1491 class_unregister(&fsi_master_class); 1492 bus_unregister(&fsi_bus_type); 1493 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES); 1494 ida_destroy(&fsi_minor_ida); 1495 } 1496 module_exit(fsi_exit); 1497 module_param(discard_errors, int, 0664); 1498 MODULE_DESCRIPTION("FSI core driver"); 1499 MODULE_LICENSE("GPL"); 1500 MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses"); 1501