xref: /linux/drivers/fsi/fsi-core.c (revision 7ff836f064e2c814a7504c91a4464eea45d475bd)
1 /*
2  * FSI core driver
3  *
4  * Copyright (C) IBM Corporation 2016
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * TODO:
16  *  - Rework topology
17  *  - s/chip_id/chip_loc
18  *  - s/cfam/chip (cfam_id -> chip_id etc...)
19  */
20 
21 #include <linux/crc4.h>
22 #include <linux/device.h>
23 #include <linux/fsi.h>
24 #include <linux/idr.h>
25 #include <linux/module.h>
26 #include <linux/of.h>
27 #include <linux/slab.h>
28 #include <linux/bitops.h>
29 #include <linux/cdev.h>
30 #include <linux/fs.h>
31 #include <linux/uaccess.h>
32 
33 #include "fsi-master.h"
34 
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/fsi.h>
37 
38 #define FSI_SLAVE_CONF_NEXT_MASK	GENMASK(31, 31)
39 #define FSI_SLAVE_CONF_SLOTS_MASK	GENMASK(23, 16)
40 #define FSI_SLAVE_CONF_SLOTS_SHIFT	16
41 #define FSI_SLAVE_CONF_VERSION_MASK	GENMASK(15, 12)
42 #define FSI_SLAVE_CONF_VERSION_SHIFT	12
43 #define FSI_SLAVE_CONF_TYPE_MASK	GENMASK(11, 4)
44 #define FSI_SLAVE_CONF_TYPE_SHIFT	4
45 #define FSI_SLAVE_CONF_CRC_SHIFT	4
46 #define FSI_SLAVE_CONF_CRC_MASK		GENMASK(3, 0)
47 #define FSI_SLAVE_CONF_DATA_BITS	28
48 
49 #define FSI_PEEK_BASE			0x410
50 
51 static const int engine_page_size = 0x400;
52 
53 #define FSI_SLAVE_BASE			0x800
54 
55 /*
56  * FSI slave engine control register offsets
57  */
58 #define FSI_SMODE		0x0	/* R/W: Mode register */
59 #define FSI_SISC		0x8	/* R/W: Interrupt condition */
60 #define FSI_SSTAT		0x14	/* R  : Slave status */
61 #define FSI_LLMODE		0x100	/* R/W: Link layer mode register */
62 
63 /*
64  * SMODE fields
65  */
66 #define FSI_SMODE_WSC		0x80000000	/* Warm start done */
67 #define FSI_SMODE_ECRC		0x20000000	/* Hw CRC check */
68 #define FSI_SMODE_SID_SHIFT	24		/* ID shift */
69 #define FSI_SMODE_SID_MASK	3		/* ID Mask */
70 #define FSI_SMODE_ED_SHIFT	20		/* Echo delay shift */
71 #define FSI_SMODE_ED_MASK	0xf		/* Echo delay mask */
72 #define FSI_SMODE_SD_SHIFT	16		/* Send delay shift */
73 #define FSI_SMODE_SD_MASK	0xf		/* Send delay mask */
74 #define FSI_SMODE_LBCRR_SHIFT	8		/* Clk ratio shift */
75 #define FSI_SMODE_LBCRR_MASK	0xf		/* Clk ratio mask */
76 
77 /*
78  * LLMODE fields
79  */
80 #define FSI_LLMODE_ASYNC	0x1
81 
82 #define FSI_SLAVE_SIZE_23b		0x800000
83 
84 static DEFINE_IDA(master_ida);
85 
86 struct fsi_slave {
87 	struct device		dev;
88 	struct fsi_master	*master;
89 	struct cdev		cdev;
90 	int			cdev_idx;
91 	int			id;	/* FSI address */
92 	int			link;	/* FSI link# */
93 	u32			cfam_id;
94 	int			chip_id;
95 	uint32_t		size;	/* size of slave address space */
96 	u8			t_send_delay;
97 	u8			t_echo_delay;
98 };
99 
100 #define to_fsi_master(d) container_of(d, struct fsi_master, dev)
101 #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
102 
103 static const int slave_retries = 2;
104 static int discard_errors;
105 
106 static dev_t fsi_base_dev;
107 static DEFINE_IDA(fsi_minor_ida);
108 #define FSI_CHAR_MAX_DEVICES	0x1000
109 
110 /* Legacy /dev numbering: 4 devices per chip, 16 chips */
111 #define FSI_CHAR_LEGACY_TOP	64
112 
113 static int fsi_master_read(struct fsi_master *master, int link,
114 		uint8_t slave_id, uint32_t addr, void *val, size_t size);
115 static int fsi_master_write(struct fsi_master *master, int link,
116 		uint8_t slave_id, uint32_t addr, const void *val, size_t size);
117 static int fsi_master_break(struct fsi_master *master, int link);
118 
119 /*
120  * fsi_device_read() / fsi_device_write() / fsi_device_peek()
121  *
122  * FSI endpoint-device support
123  *
124  * Read / write / peek accessors for a client
125  *
126  * Parameters:
127  * dev:  Structure passed to FSI client device drivers on probe().
128  * addr: FSI address of given device.  Client should pass in its base address
129  *       plus desired offset to access its register space.
130  * val:  For read/peek this is the value read at the specified address. For
131  *       write this is value to write to the specified address.
132  *       The data in val must be FSI bus endian (big endian).
133  * size: Size in bytes of the operation.  Sizes supported are 1, 2 and 4 bytes.
134  *       Addresses must be aligned on size boundaries or an error will result.
135  */
136 int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
137 		size_t size)
138 {
139 	if (addr > dev->size || size > dev->size || addr > dev->size - size)
140 		return -EINVAL;
141 
142 	return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
143 }
144 EXPORT_SYMBOL_GPL(fsi_device_read);
145 
146 int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
147 		size_t size)
148 {
149 	if (addr > dev->size || size > dev->size || addr > dev->size - size)
150 		return -EINVAL;
151 
152 	return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
153 }
154 EXPORT_SYMBOL_GPL(fsi_device_write);
155 
156 int fsi_device_peek(struct fsi_device *dev, void *val)
157 {
158 	uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));
159 
160 	return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
161 }
162 
163 static void fsi_device_release(struct device *_device)
164 {
165 	struct fsi_device *device = to_fsi_dev(_device);
166 
167 	of_node_put(device->dev.of_node);
168 	kfree(device);
169 }
170 
171 static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
172 {
173 	struct fsi_device *dev;
174 
175 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
176 	if (!dev)
177 		return NULL;
178 
179 	dev->dev.parent = &slave->dev;
180 	dev->dev.bus = &fsi_bus_type;
181 	dev->dev.release = fsi_device_release;
182 
183 	return dev;
184 }
185 
186 /* FSI slave support */
187 static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
188 		uint8_t *idp)
189 {
190 	uint32_t addr = *addrp;
191 	uint8_t id = *idp;
192 
193 	if (addr > slave->size)
194 		return -EINVAL;
195 
196 	/* For 23 bit addressing, we encode the extra two bits in the slave
197 	 * id (and the slave's actual ID needs to be 0).
198 	 */
199 	if (addr > 0x1fffff) {
200 		if (slave->id != 0)
201 			return -EINVAL;
202 		id = (addr >> 21) & 0x3;
203 		addr &= 0x1fffff;
204 	}
205 
206 	*addrp = addr;
207 	*idp = id;
208 	return 0;
209 }
210 
211 static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
212 {
213 	struct fsi_master *master = slave->master;
214 	__be32 irq, stat;
215 	int rc, link;
216 	uint8_t id;
217 
218 	link = slave->link;
219 	id = slave->id;
220 
221 	rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
222 			&irq, sizeof(irq));
223 	if (rc)
224 		return rc;
225 
226 	rc =  fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
227 			&stat, sizeof(stat));
228 	if (rc)
229 		return rc;
230 
231 	dev_dbg(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n",
232 			be32_to_cpu(stat), be32_to_cpu(irq));
233 
234 	/* clear interrupts */
235 	return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
236 			&irq, sizeof(irq));
237 }
238 
239 /* Encode slave local bus echo delay */
240 static inline uint32_t fsi_smode_echodly(int x)
241 {
242 	return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
243 }
244 
245 /* Encode slave local bus send delay */
246 static inline uint32_t fsi_smode_senddly(int x)
247 {
248 	return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
249 }
250 
251 /* Encode slave local bus clock rate ratio */
252 static inline uint32_t fsi_smode_lbcrr(int x)
253 {
254 	return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
255 }
256 
257 /* Encode slave ID */
258 static inline uint32_t fsi_smode_sid(int x)
259 {
260 	return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
261 }
262 
263 static uint32_t fsi_slave_smode(int id, u8 t_senddly, u8 t_echodly)
264 {
265 	return FSI_SMODE_WSC | FSI_SMODE_ECRC
266 		| fsi_smode_sid(id)
267 		| fsi_smode_echodly(t_echodly - 1) | fsi_smode_senddly(t_senddly - 1)
268 		| fsi_smode_lbcrr(0x8);
269 }
270 
271 static int fsi_slave_set_smode(struct fsi_slave *slave)
272 {
273 	uint32_t smode;
274 	__be32 data;
275 
276 	/* set our smode register with the slave ID field to 0; this enables
277 	 * extended slave addressing
278 	 */
279 	smode = fsi_slave_smode(slave->id, slave->t_send_delay, slave->t_echo_delay);
280 	data = cpu_to_be32(smode);
281 
282 	return fsi_master_write(slave->master, slave->link, slave->id,
283 				FSI_SLAVE_BASE + FSI_SMODE,
284 				&data, sizeof(data));
285 }
286 
287 static int fsi_slave_handle_error(struct fsi_slave *slave, bool write,
288 				  uint32_t addr, size_t size)
289 {
290 	struct fsi_master *master = slave->master;
291 	int rc, link;
292 	uint32_t reg;
293 	uint8_t id, send_delay, echo_delay;
294 
295 	if (discard_errors)
296 		return -1;
297 
298 	link = slave->link;
299 	id = slave->id;
300 
301 	dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]",
302 			write ? "write" : "read", addr, size);
303 
304 	/* try a simple clear of error conditions, which may fail if we've lost
305 	 * communication with the slave
306 	 */
307 	rc = fsi_slave_report_and_clear_errors(slave);
308 	if (!rc)
309 		return 0;
310 
311 	/* send a TERM and retry */
312 	if (master->term) {
313 		rc = master->term(master, link, id);
314 		if (!rc) {
315 			rc = fsi_master_read(master, link, id, 0,
316 					&reg, sizeof(reg));
317 			if (!rc)
318 				rc = fsi_slave_report_and_clear_errors(slave);
319 			if (!rc)
320 				return 0;
321 		}
322 	}
323 
324 	send_delay = slave->t_send_delay;
325 	echo_delay = slave->t_echo_delay;
326 
327 	/* getting serious, reset the slave via BREAK */
328 	rc = fsi_master_break(master, link);
329 	if (rc)
330 		return rc;
331 
332 	slave->t_send_delay = send_delay;
333 	slave->t_echo_delay = echo_delay;
334 
335 	rc = fsi_slave_set_smode(slave);
336 	if (rc)
337 		return rc;
338 
339 	if (master->link_config)
340 		master->link_config(master, link,
341 				    slave->t_send_delay,
342 				    slave->t_echo_delay);
343 
344 	return fsi_slave_report_and_clear_errors(slave);
345 }
346 
347 int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
348 			void *val, size_t size)
349 {
350 	uint8_t id = slave->id;
351 	int rc, err_rc, i;
352 
353 	rc = fsi_slave_calc_addr(slave, &addr, &id);
354 	if (rc)
355 		return rc;
356 
357 	for (i = 0; i < slave_retries; i++) {
358 		rc = fsi_master_read(slave->master, slave->link,
359 				id, addr, val, size);
360 		if (!rc)
361 			break;
362 
363 		err_rc = fsi_slave_handle_error(slave, false, addr, size);
364 		if (err_rc)
365 			break;
366 	}
367 
368 	return rc;
369 }
370 EXPORT_SYMBOL_GPL(fsi_slave_read);
371 
372 int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
373 			const void *val, size_t size)
374 {
375 	uint8_t id = slave->id;
376 	int rc, err_rc, i;
377 
378 	rc = fsi_slave_calc_addr(slave, &addr, &id);
379 	if (rc)
380 		return rc;
381 
382 	for (i = 0; i < slave_retries; i++) {
383 		rc = fsi_master_write(slave->master, slave->link,
384 				id, addr, val, size);
385 		if (!rc)
386 			break;
387 
388 		err_rc = fsi_slave_handle_error(slave, true, addr, size);
389 		if (err_rc)
390 			break;
391 	}
392 
393 	return rc;
394 }
395 EXPORT_SYMBOL_GPL(fsi_slave_write);
396 
397 extern int fsi_slave_claim_range(struct fsi_slave *slave,
398 		uint32_t addr, uint32_t size)
399 {
400 	if (addr + size < addr)
401 		return -EINVAL;
402 
403 	if (addr + size > slave->size)
404 		return -EINVAL;
405 
406 	/* todo: check for overlapping claims */
407 	return 0;
408 }
409 EXPORT_SYMBOL_GPL(fsi_slave_claim_range);
410 
411 extern void fsi_slave_release_range(struct fsi_slave *slave,
412 		uint32_t addr, uint32_t size)
413 {
414 }
415 EXPORT_SYMBOL_GPL(fsi_slave_release_range);
416 
417 static bool fsi_device_node_matches(struct device *dev, struct device_node *np,
418 		uint32_t addr, uint32_t size)
419 {
420 	unsigned int len, na, ns;
421 	const __be32 *prop;
422 	uint32_t psize;
423 
424 	na = of_n_addr_cells(np);
425 	ns = of_n_size_cells(np);
426 
427 	if (na != 1 || ns != 1)
428 		return false;
429 
430 	prop = of_get_property(np, "reg", &len);
431 	if (!prop || len != 8)
432 		return false;
433 
434 	if (of_read_number(prop, 1) != addr)
435 		return false;
436 
437 	psize = of_read_number(prop + 1, 1);
438 	if (psize != size) {
439 		dev_warn(dev,
440 			"node %s matches probed address, but not size (got 0x%x, expected 0x%x)",
441 			of_node_full_name(np), psize, size);
442 	}
443 
444 	return true;
445 }
446 
447 /* Find a matching node for the slave engine at @address, using @size bytes
448  * of space. Returns NULL if not found, or a matching node with refcount
449  * already incremented.
450  */
451 static struct device_node *fsi_device_find_of_node(struct fsi_device *dev)
452 {
453 	struct device_node *parent, *np;
454 
455 	parent = dev_of_node(&dev->slave->dev);
456 	if (!parent)
457 		return NULL;
458 
459 	for_each_child_of_node(parent, np) {
460 		if (fsi_device_node_matches(&dev->dev, np,
461 					dev->addr, dev->size))
462 			return np;
463 	}
464 
465 	return NULL;
466 }
467 
468 static int fsi_slave_scan(struct fsi_slave *slave)
469 {
470 	uint32_t engine_addr;
471 	int rc, i;
472 
473 	/*
474 	 * scan engines
475 	 *
476 	 * We keep the peek mode and slave engines for the core; so start
477 	 * at the third slot in the configuration table. We also need to
478 	 * skip the chip ID entry at the start of the address space.
479 	 */
480 	engine_addr = engine_page_size * 3;
481 	for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
482 		uint8_t slots, version, type, crc;
483 		struct fsi_device *dev;
484 		uint32_t conf;
485 		__be32 data;
486 
487 		rc = fsi_slave_read(slave, (i + 1) * sizeof(data),
488 				&data, sizeof(data));
489 		if (rc) {
490 			dev_warn(&slave->dev,
491 				"error reading slave registers\n");
492 			return -1;
493 		}
494 		conf = be32_to_cpu(data);
495 
496 		crc = crc4(0, conf, 32);
497 		if (crc) {
498 			dev_warn(&slave->dev,
499 				"crc error in slave register at 0x%04x\n",
500 				i);
501 			return -1;
502 		}
503 
504 		slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
505 			>> FSI_SLAVE_CONF_SLOTS_SHIFT;
506 		version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
507 			>> FSI_SLAVE_CONF_VERSION_SHIFT;
508 		type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
509 			>> FSI_SLAVE_CONF_TYPE_SHIFT;
510 
511 		/*
512 		 * Unused address areas are marked by a zero type value; this
513 		 * skips the defined address areas
514 		 */
515 		if (type != 0 && slots != 0) {
516 
517 			/* create device */
518 			dev = fsi_create_device(slave);
519 			if (!dev)
520 				return -ENOMEM;
521 
522 			dev->slave = slave;
523 			dev->engine_type = type;
524 			dev->version = version;
525 			dev->unit = i;
526 			dev->addr = engine_addr;
527 			dev->size = slots * engine_page_size;
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 
555 static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
556 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
557 		loff_t off, size_t count)
558 {
559 	struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
560 	size_t total_len, read_len;
561 	int rc;
562 
563 	if (off < 0)
564 		return -EINVAL;
565 
566 	if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
567 		return -EINVAL;
568 
569 	for (total_len = 0; total_len < count; total_len += read_len) {
570 		read_len = min_t(size_t, count, 4);
571 		read_len -= off & 0x3;
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 
583 static 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 = min_t(size_t, count, 4);
599 		write_len -= off & 0x3;
600 
601 		rc = fsi_slave_write(slave, off, buf + total_len, write_len);
602 		if (rc)
603 			return rc;
604 
605 		off += write_len;
606 	}
607 
608 	return count;
609 }
610 
611 static const struct bin_attribute fsi_slave_raw_attr = {
612 	.attr = {
613 		.name = "raw",
614 		.mode = 0600,
615 	},
616 	.size = 0,
617 	.read = fsi_slave_sysfs_raw_read,
618 	.write = fsi_slave_sysfs_raw_write,
619 };
620 
621 static void fsi_slave_release(struct device *dev)
622 {
623 	struct fsi_slave *slave = to_fsi_slave(dev);
624 
625 	fsi_free_minor(slave->dev.devt);
626 	of_node_put(dev->of_node);
627 	kfree(slave);
628 }
629 
630 static bool fsi_slave_node_matches(struct device_node *np,
631 		int link, uint8_t id)
632 {
633 	unsigned int len, na, ns;
634 	const __be32 *prop;
635 
636 	na = of_n_addr_cells(np);
637 	ns = of_n_size_cells(np);
638 
639 	/* Ensure we have the correct format for addresses and sizes in
640 	 * reg properties
641 	 */
642 	if (na != 2 || ns != 0)
643 		return false;
644 
645 	prop = of_get_property(np, "reg", &len);
646 	if (!prop || len != 8)
647 		return false;
648 
649 	return (of_read_number(prop, 1) == link) &&
650 		(of_read_number(prop + 1, 1) == id);
651 }
652 
653 /* Find a matching node for the slave at (link, id). Returns NULL if none
654  * found, or a matching node with refcount already incremented.
655  */
656 static struct device_node *fsi_slave_find_of_node(struct fsi_master *master,
657 		int link, uint8_t id)
658 {
659 	struct device_node *parent, *np;
660 
661 	parent = dev_of_node(&master->dev);
662 	if (!parent)
663 		return NULL;
664 
665 	for_each_child_of_node(parent, np) {
666 		if (fsi_slave_node_matches(np, link, id))
667 			return np;
668 	}
669 
670 	return NULL;
671 }
672 
673 static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
674 			 loff_t *offset)
675 {
676 	struct fsi_slave *slave = filep->private_data;
677 	size_t total_len, read_len;
678 	loff_t off = *offset;
679 	ssize_t rc;
680 
681 	if (off < 0)
682 		return -EINVAL;
683 
684 	if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
685 		return -EINVAL;
686 
687 	for (total_len = 0; total_len < count; total_len += read_len) {
688 		__be32 data;
689 
690 		read_len = min_t(size_t, count, 4);
691 		read_len -= off & 0x3;
692 
693 		rc = fsi_slave_read(slave, off, &data, read_len);
694 		if (rc)
695 			goto fail;
696 		rc = copy_to_user(buf + total_len, &data, read_len);
697 		if (rc) {
698 			rc = -EFAULT;
699 			goto fail;
700 		}
701 		off += read_len;
702 	}
703 	rc = count;
704  fail:
705 	*offset = off;
706 	return count;
707 }
708 
709 static ssize_t cfam_write(struct file *filep, const char __user *buf,
710 			  size_t count, loff_t *offset)
711 {
712 	struct fsi_slave *slave = filep->private_data;
713 	size_t total_len, write_len;
714 	loff_t off = *offset;
715 	ssize_t rc;
716 
717 
718 	if (off < 0)
719 		return -EINVAL;
720 
721 	if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
722 		return -EINVAL;
723 
724 	for (total_len = 0; total_len < count; total_len += write_len) {
725 		__be32 data;
726 
727 		write_len = min_t(size_t, count, 4);
728 		write_len -= off & 0x3;
729 
730 		rc = copy_from_user(&data, buf + total_len, write_len);
731 		if (rc) {
732 			rc = -EFAULT;
733 			goto fail;
734 		}
735 		rc = fsi_slave_write(slave, off, &data, write_len);
736 		if (rc)
737 			goto fail;
738 		off += write_len;
739 	}
740 	rc = count;
741  fail:
742 	*offset = off;
743 	return count;
744 }
745 
746 static loff_t cfam_llseek(struct file *file, loff_t offset, int whence)
747 {
748 	switch (whence) {
749 	case SEEK_CUR:
750 		break;
751 	case SEEK_SET:
752 		file->f_pos = offset;
753 		break;
754 	default:
755 		return -EINVAL;
756 	}
757 
758 	return offset;
759 }
760 
761 static int cfam_open(struct inode *inode, struct file *file)
762 {
763 	struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev);
764 
765 	file->private_data = slave;
766 
767 	return 0;
768 }
769 
770 static const struct file_operations cfam_fops = {
771 	.owner		= THIS_MODULE,
772 	.open		= cfam_open,
773 	.llseek		= cfam_llseek,
774 	.read		= cfam_read,
775 	.write		= cfam_write,
776 };
777 
778 static ssize_t send_term_store(struct device *dev,
779 			       struct device_attribute *attr,
780 			       const char *buf, size_t count)
781 {
782 	struct fsi_slave *slave = to_fsi_slave(dev);
783 	struct fsi_master *master = slave->master;
784 
785 	if (!master->term)
786 		return -ENODEV;
787 
788 	master->term(master, slave->link, slave->id);
789 	return count;
790 }
791 
792 static DEVICE_ATTR_WO(send_term);
793 
794 static ssize_t slave_send_echo_show(struct device *dev,
795 				    struct device_attribute *attr,
796 				    char *buf)
797 {
798 	struct fsi_slave *slave = to_fsi_slave(dev);
799 
800 	return sprintf(buf, "%u\n", slave->t_send_delay);
801 }
802 
803 static ssize_t slave_send_echo_store(struct device *dev,
804 		struct device_attribute *attr, const char *buf, size_t count)
805 {
806 	struct fsi_slave *slave = to_fsi_slave(dev);
807 	struct fsi_master *master = slave->master;
808 	unsigned long val;
809 	int rc;
810 
811 	if (kstrtoul(buf, 0, &val) < 0)
812 		return -EINVAL;
813 
814 	if (val < 1 || val > 16)
815 		return -EINVAL;
816 
817 	if (!master->link_config)
818 		return -ENXIO;
819 
820 	/* Current HW mandates that send and echo delay are identical */
821 	slave->t_send_delay = val;
822 	slave->t_echo_delay = val;
823 
824 	rc = fsi_slave_set_smode(slave);
825 	if (rc < 0)
826 		return rc;
827 	if (master->link_config)
828 		master->link_config(master, slave->link,
829 				    slave->t_send_delay,
830 				    slave->t_echo_delay);
831 
832 	return count;
833 }
834 
835 static DEVICE_ATTR(send_echo_delays, 0600,
836 		   slave_send_echo_show, slave_send_echo_store);
837 
838 static ssize_t chip_id_show(struct device *dev,
839 			    struct device_attribute *attr,
840 			    char *buf)
841 {
842 	struct fsi_slave *slave = to_fsi_slave(dev);
843 
844 	return sprintf(buf, "%d\n", slave->chip_id);
845 }
846 
847 static DEVICE_ATTR_RO(chip_id);
848 
849 static ssize_t cfam_id_show(struct device *dev,
850 			    struct device_attribute *attr,
851 			    char *buf)
852 {
853 	struct fsi_slave *slave = to_fsi_slave(dev);
854 
855 	return sprintf(buf, "0x%x\n", slave->cfam_id);
856 }
857 
858 static DEVICE_ATTR_RO(cfam_id);
859 
860 static struct attribute *cfam_attr[] = {
861 	&dev_attr_send_echo_delays.attr,
862 	&dev_attr_chip_id.attr,
863 	&dev_attr_cfam_id.attr,
864 	&dev_attr_send_term.attr,
865 	NULL,
866 };
867 
868 static const struct attribute_group cfam_attr_group = {
869 	.attrs = cfam_attr,
870 };
871 
872 static const struct attribute_group *cfam_attr_groups[] = {
873 	&cfam_attr_group,
874 	NULL,
875 };
876 
877 static char *cfam_devnode(struct device *dev, umode_t *mode,
878 			  kuid_t *uid, kgid_t *gid)
879 {
880 	struct fsi_slave *slave = to_fsi_slave(dev);
881 
882 #ifdef CONFIG_FSI_NEW_DEV_NODE
883 	return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx);
884 #else
885 	return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx);
886 #endif
887 }
888 
889 static const struct device_type cfam_type = {
890 	.name = "cfam",
891 	.devnode = cfam_devnode,
892 	.groups = cfam_attr_groups
893 };
894 
895 static char *fsi_cdev_devnode(struct device *dev, umode_t *mode,
896 			      kuid_t *uid, kgid_t *gid)
897 {
898 #ifdef CONFIG_FSI_NEW_DEV_NODE
899 	return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev));
900 #else
901 	return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
902 #endif
903 }
904 
905 const struct device_type fsi_cdev_type = {
906 	.name = "fsi-cdev",
907 	.devnode = fsi_cdev_devnode,
908 };
909 EXPORT_SYMBOL_GPL(fsi_cdev_type);
910 
911 /* Backward compatible /dev/ numbering in "old style" mode */
912 static int fsi_adjust_index(int index)
913 {
914 #ifdef CONFIG_FSI_NEW_DEV_NODE
915 	return index;
916 #else
917 	return index + 1;
918 #endif
919 }
920 
921 static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type,
922 			       dev_t *out_dev, int *out_index)
923 {
924 	int cid = slave->chip_id;
925 	int id;
926 
927 	/* Check if we qualify for legacy numbering */
928 	if (cid >= 0 && cid < 16 && type < 4) {
929 		/* Try reserving the legacy number */
930 		id = (cid << 4) | type;
931 		id = ida_simple_get(&fsi_minor_ida, id, id + 1, GFP_KERNEL);
932 		if (id >= 0) {
933 			*out_index = fsi_adjust_index(cid);
934 			*out_dev = fsi_base_dev + id;
935 			return 0;
936 		}
937 		/* Other failure */
938 		if (id != -ENOSPC)
939 			return id;
940 		/* Fallback to non-legacy allocation */
941 	}
942 	id = ida_simple_get(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP,
943 			    FSI_CHAR_MAX_DEVICES, GFP_KERNEL);
944 	if (id < 0)
945 		return id;
946 	*out_index = fsi_adjust_index(id);
947 	*out_dev = fsi_base_dev + id;
948 	return 0;
949 }
950 
951 int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type,
952 		      dev_t *out_dev, int *out_index)
953 {
954 	return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index);
955 }
956 EXPORT_SYMBOL_GPL(fsi_get_new_minor);
957 
958 void fsi_free_minor(dev_t dev)
959 {
960 	ida_simple_remove(&fsi_minor_ida, MINOR(dev));
961 }
962 EXPORT_SYMBOL_GPL(fsi_free_minor);
963 
964 static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
965 {
966 	uint32_t cfam_id;
967 	struct fsi_slave *slave;
968 	uint8_t crc;
969 	__be32 data, llmode;
970 	int rc;
971 
972 	/* Currently, we only support single slaves on a link, and use the
973 	 * full 23-bit address range
974 	 */
975 	if (id != 0)
976 		return -EINVAL;
977 
978 	rc = fsi_master_read(master, link, id, 0, &data, sizeof(data));
979 	if (rc) {
980 		dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
981 				link, id, rc);
982 		return -ENODEV;
983 	}
984 	cfam_id = be32_to_cpu(data);
985 
986 	crc = crc4(0, cfam_id, 32);
987 	if (crc) {
988 		dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n",
989 				link, id);
990 		return -EIO;
991 	}
992 
993 	dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
994 			cfam_id, master->idx, link, id);
995 
996 	/* If we're behind a master that doesn't provide a self-running bus
997 	 * clock, put the slave into async mode
998 	 */
999 	if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
1000 		llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
1001 		rc = fsi_master_write(master, link, id,
1002 				FSI_SLAVE_BASE + FSI_LLMODE,
1003 				&llmode, sizeof(llmode));
1004 		if (rc)
1005 			dev_warn(&master->dev,
1006 				"can't set llmode on slave:%02x:%02x %d\n",
1007 				link, id, rc);
1008 	}
1009 
1010 	/* We can communicate with a slave; create the slave device and
1011 	 * register.
1012 	 */
1013 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1014 	if (!slave)
1015 		return -ENOMEM;
1016 
1017 	dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
1018 	slave->dev.type = &cfam_type;
1019 	slave->dev.parent = &master->dev;
1020 	slave->dev.of_node = fsi_slave_find_of_node(master, link, id);
1021 	slave->dev.release = fsi_slave_release;
1022 	device_initialize(&slave->dev);
1023 	slave->cfam_id = cfam_id;
1024 	slave->master = master;
1025 	slave->link = link;
1026 	slave->id = id;
1027 	slave->size = FSI_SLAVE_SIZE_23b;
1028 	slave->t_send_delay = 16;
1029 	slave->t_echo_delay = 16;
1030 
1031 	/* Get chip ID if any */
1032 	slave->chip_id = -1;
1033 	if (slave->dev.of_node) {
1034 		uint32_t prop;
1035 		if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop))
1036 			slave->chip_id = prop;
1037 
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;
1052 	}
1053 
1054 	rc = fsi_slave_set_smode(slave);
1055 	if (rc) {
1056 		dev_warn(&master->dev,
1057 				"can't set smode on slave:%02x:%02x %d\n",
1058 				link, id, rc);
1059 		kfree(slave);
1060 		return -ENODEV;
1061 	}
1062 	if (master->link_config)
1063 		master->link_config(master, link,
1064 				    slave->t_send_delay,
1065 				    slave->t_echo_delay);
1066 
1067 	/* Legacy raw file -> to be removed */
1068 	rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
1069 	if (rc)
1070 		dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
1071 
1072 
1073 	rc = fsi_slave_scan(slave);
1074 	if (rc)
1075 		dev_dbg(&master->dev, "failed during slave scan with: %d\n",
1076 				rc);
1077 
1078 	return rc;
1079 
1080  err_free:
1081 	put_device(&slave->dev);
1082 	return rc;
1083 }
1084 
1085 /* FSI master support */
1086 static 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 
1100 static 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 
1117 static 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 
1134 static 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  */
1145 static 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 
1159 static 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 
1183 static int fsi_slave_remove_device(struct device *dev, void *arg)
1184 {
1185 	device_unregister(dev);
1186 	return 0;
1187 }
1188 
1189 static 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 
1199 static void fsi_master_unscan(struct fsi_master *master)
1200 {
1201 	device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
1202 }
1203 
1204 int 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 }
1215 EXPORT_SYMBOL_GPL(fsi_master_rescan);
1216 
1217 static 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 
1230 static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
1231 
1232 static 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 
1242 static DEVICE_ATTR(break, 0200, NULL, master_break_store);
1243 
1244 int 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 }
1282 EXPORT_SYMBOL_GPL(fsi_master_register);
1283 
1284 void 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 }
1296 EXPORT_SYMBOL_GPL(fsi_master_unregister);
1297 
1298 /* FSI core & Linux bus type definitions */
1299 
1300 static 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 
1320 int 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 }
1329 EXPORT_SYMBOL_GPL(fsi_driver_register);
1330 
1331 void fsi_driver_unregister(struct fsi_driver *fsi_drv)
1332 {
1333 	driver_unregister(&fsi_drv->drv);
1334 }
1335 EXPORT_SYMBOL_GPL(fsi_driver_unregister);
1336 
1337 struct bus_type fsi_bus_type = {
1338 	.name		= "fsi",
1339 	.match		= fsi_bus_match,
1340 };
1341 EXPORT_SYMBOL_GPL(fsi_bus_type);
1342 
1343 static 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 }
1359 postcore_initcall(fsi_init);
1360 
1361 static 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 }
1367 module_exit(fsi_exit);
1368 module_param(discard_errors, int, 0664);
1369 MODULE_LICENSE("GPL");
1370 MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");
1371