Linux Audio

Check our new training course

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