Linux Audio

Check our new training course

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