]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/fsi/fsi-core.c
drivers/fsi: Add error handling for slave
[mirror_ubuntu-bionic-kernel.git] / drivers / fsi / fsi-core.c
CommitLineData
0508ad1f
JK
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
2b545cd8 16#include <linux/crc4.h>
0508ad1f
JK
17#include <linux/device.h>
18#include <linux/fsi.h>
09aecfab 19#include <linux/idr.h>
0508ad1f 20#include <linux/module.h>
2b545cd8 21#include <linux/slab.h>
f7ade2a6 22#include <linux/bitops.h>
0508ad1f 23
09aecfab
JK
24#include "fsi-master.h"
25
66433b05
JK
26#define CREATE_TRACE_POINTS
27#include <trace/events/fsi.h>
28
f7ade2a6
JK
29#define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
30#define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
31#define FSI_SLAVE_CONF_SLOTS_SHIFT 16
32#define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
33#define FSI_SLAVE_CONF_VERSION_SHIFT 12
34#define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
35#define FSI_SLAVE_CONF_TYPE_SHIFT 4
36#define FSI_SLAVE_CONF_CRC_SHIFT 4
37#define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
38#define FSI_SLAVE_CONF_DATA_BITS 28
39
4efe37f4
JK
40#define FSI_PEEK_BASE 0x410
41
f7ade2a6
JK
42static const int engine_page_size = 0x400;
43
2b37c3e2
CB
44#define FSI_SLAVE_BASE 0x800
45
46/*
47 * FSI slave engine control register offsets
48 */
1fa847d7
JK
49#define FSI_SMODE 0x0 /* R/W: Mode register */
50#define FSI_SISC 0x8 /* R/W: Interrupt condition */
51#define FSI_SSTAT 0x14 /* R : Slave status */
2b37c3e2
CB
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
2b545cd8
JK
67#define FSI_SLAVE_SIZE_23b 0x800000
68
09aecfab
JK
69static DEFINE_IDA(master_ida);
70
faf0b116
JK
71struct fsi_slave {
72 struct device dev;
73 struct fsi_master *master;
74 int id;
75 int link;
76 uint32_t size; /* size of slave address space */
77};
78
cd0fdb5c 79#define to_fsi_master(d) container_of(d, struct fsi_master, dev)
faf0b116
JK
80#define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
81
1fa847d7
JK
82static const int slave_retries = 2;
83static int discard_errors;
84
014c2abc
JK
85static int fsi_master_read(struct fsi_master *master, int link,
86 uint8_t slave_id, uint32_t addr, void *val, size_t size);
87static int fsi_master_write(struct fsi_master *master, int link,
88 uint8_t slave_id, uint32_t addr, const void *val, size_t size);
1fa847d7 89static int fsi_master_break(struct fsi_master *master, int link);
4efe37f4
JK
90
91/*
92 * fsi_device_read() / fsi_device_write() / fsi_device_peek()
93 *
94 * FSI endpoint-device support
95 *
96 * Read / write / peek accessors for a client
97 *
98 * Parameters:
99 * dev: Structure passed to FSI client device drivers on probe().
100 * addr: FSI address of given device. Client should pass in its base address
101 * plus desired offset to access its register space.
102 * val: For read/peek this is the value read at the specified address. For
103 * write this is value to write to the specified address.
104 * The data in val must be FSI bus endian (big endian).
105 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
106 * Addresses must be aligned on size boundaries or an error will result.
107 */
108int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
109 size_t size)
110{
111 if (addr > dev->size || size > dev->size || addr > dev->size - size)
112 return -EINVAL;
113
114 return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
115}
116EXPORT_SYMBOL_GPL(fsi_device_read);
117
118int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
119 size_t size)
120{
121 if (addr > dev->size || size > dev->size || addr > dev->size - size)
122 return -EINVAL;
123
124 return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
125}
126EXPORT_SYMBOL_GPL(fsi_device_write);
127
128int fsi_device_peek(struct fsi_device *dev, void *val)
129{
130 uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));
014c2abc 131
4efe37f4
JK
132 return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
133}
f7ade2a6
JK
134
135static void fsi_device_release(struct device *_device)
136{
137 struct fsi_device *device = to_fsi_dev(_device);
138
139 kfree(device);
140}
141
142static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
143{
144 struct fsi_device *dev;
145
146 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
147 if (!dev)
148 return NULL;
149
150 dev->dev.parent = &slave->dev;
151 dev->dev.bus = &fsi_bus_type;
152 dev->dev.release = fsi_device_release;
153
154 return dev;
155}
156
414c1026 157/* FSI slave support */
014c2abc
JK
158static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
159 uint8_t *idp)
160{
161 uint32_t addr = *addrp;
162 uint8_t id = *idp;
163
164 if (addr > slave->size)
165 return -EINVAL;
166
167 /* For 23 bit addressing, we encode the extra two bits in the slave
168 * id (and the slave's actual ID needs to be 0).
169 */
170 if (addr > 0x1fffff) {
171 if (slave->id != 0)
172 return -EINVAL;
173 id = (addr >> 21) & 0x3;
174 addr &= 0x1fffff;
175 }
176
177 *addrp = addr;
178 *idp = id;
179 return 0;
180}
181
1fa847d7
JK
182int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
183{
184 struct fsi_master *master = slave->master;
185 uint32_t irq, stat;
186 int rc, link;
187 uint8_t id;
188
189 link = slave->link;
190 id = slave->id;
191
192 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
193 &irq, sizeof(irq));
194 if (rc)
195 return rc;
196
197 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
198 &stat, sizeof(stat));
199 if (rc)
200 return rc;
201
202 dev_info(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n",
203 be32_to_cpu(stat), be32_to_cpu(irq));
204
205 /* clear interrupts */
206 return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
207 &irq, sizeof(irq));
208}
209
210static int fsi_slave_set_smode(struct fsi_master *master, int link, int id);
211
212int fsi_slave_handle_error(struct fsi_slave *slave, bool write, uint32_t addr,
213 size_t size)
214{
215 struct fsi_master *master = slave->master;
216 int rc, link;
217 uint32_t reg;
218 uint8_t id;
219
220 if (discard_errors)
221 return -1;
222
223 link = slave->link;
224 id = slave->id;
225
226 dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]",
227 write ? "write" : "read", addr, size);
228
229 /* try a simple clear of error conditions, which may fail if we've lost
230 * communication with the slave
231 */
232 rc = fsi_slave_report_and_clear_errors(slave);
233 if (!rc)
234 return 0;
235
236 /* send a TERM and retry */
237 if (master->term) {
238 rc = master->term(master, link, id);
239 if (!rc) {
240 rc = fsi_master_read(master, link, id, 0,
241 &reg, sizeof(reg));
242 if (!rc)
243 rc = fsi_slave_report_and_clear_errors(slave);
244 if (!rc)
245 return 0;
246 }
247 }
248
249 /* getting serious, reset the slave via BREAK */
250 rc = fsi_master_break(master, link);
251 if (rc)
252 return rc;
253
254 rc = fsi_slave_set_smode(master, link, id);
255 if (rc)
256 return rc;
257
258 return fsi_slave_report_and_clear_errors(slave);
259}
260
da36cadf 261int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
014c2abc
JK
262 void *val, size_t size)
263{
264 uint8_t id = slave->id;
1fa847d7 265 int rc, err_rc, i;
014c2abc
JK
266
267 rc = fsi_slave_calc_addr(slave, &addr, &id);
268 if (rc)
269 return rc;
270
1fa847d7
JK
271 for (i = 0; i < slave_retries; i++) {
272 rc = fsi_master_read(slave->master, slave->link,
273 id, addr, val, size);
274 if (!rc)
275 break;
276
277 err_rc = fsi_slave_handle_error(slave, false, addr, size);
278 if (err_rc)
279 break;
280 }
281
282 return rc;
014c2abc 283}
da36cadf 284EXPORT_SYMBOL_GPL(fsi_slave_read);
014c2abc 285
da36cadf 286int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
014c2abc
JK
287 const void *val, size_t size)
288{
289 uint8_t id = slave->id;
1fa847d7 290 int rc, err_rc, i;
014c2abc
JK
291
292 rc = fsi_slave_calc_addr(slave, &addr, &id);
293 if (rc)
294 return rc;
295
1fa847d7
JK
296 for (i = 0; i < slave_retries; i++) {
297 rc = fsi_master_write(slave->master, slave->link,
298 id, addr, val, size);
299 if (!rc)
300 break;
301
302 err_rc = fsi_slave_handle_error(slave, true, addr, size);
303 if (err_rc)
304 break;
305 }
306
307 return rc;
014c2abc 308}
da36cadf
JK
309EXPORT_SYMBOL_GPL(fsi_slave_write);
310
311extern int fsi_slave_claim_range(struct fsi_slave *slave,
312 uint32_t addr, uint32_t size)
313{
314 if (addr + size < addr)
315 return -EINVAL;
316
317 if (addr + size > slave->size)
318 return -EINVAL;
319
320 /* todo: check for overlapping claims */
321 return 0;
322}
323EXPORT_SYMBOL_GPL(fsi_slave_claim_range);
324
325extern void fsi_slave_release_range(struct fsi_slave *slave,
326 uint32_t addr, uint32_t size)
327{
328}
329EXPORT_SYMBOL_GPL(fsi_slave_release_range);
014c2abc 330
f7ade2a6
JK
331static int fsi_slave_scan(struct fsi_slave *slave)
332{
333 uint32_t engine_addr;
334 uint32_t conf;
335 int rc, i;
336
337 /*
338 * scan engines
339 *
340 * We keep the peek mode and slave engines for the core; so start
341 * at the third slot in the configuration table. We also need to
342 * skip the chip ID entry at the start of the address space.
343 */
344 engine_addr = engine_page_size * 3;
345 for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
346 uint8_t slots, version, type, crc;
347 struct fsi_device *dev;
348
349 rc = fsi_slave_read(slave, (i + 1) * sizeof(conf),
350 &conf, sizeof(conf));
351 if (rc) {
352 dev_warn(&slave->dev,
353 "error reading slave registers\n");
354 return -1;
355 }
356 conf = be32_to_cpu(conf);
357
358 crc = crc4(0, conf, 32);
359 if (crc) {
360 dev_warn(&slave->dev,
361 "crc error in slave register at 0x%04x\n",
362 i);
363 return -1;
364 }
365
366 slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
367 >> FSI_SLAVE_CONF_SLOTS_SHIFT;
368 version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
369 >> FSI_SLAVE_CONF_VERSION_SHIFT;
370 type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
371 >> FSI_SLAVE_CONF_TYPE_SHIFT;
372
373 /*
374 * Unused address areas are marked by a zero type value; this
375 * skips the defined address areas
376 */
377 if (type != 0 && slots != 0) {
378
379 /* create device */
380 dev = fsi_create_device(slave);
381 if (!dev)
382 return -ENOMEM;
383
384 dev->slave = slave;
385 dev->engine_type = type;
386 dev->version = version;
387 dev->unit = i;
388 dev->addr = engine_addr;
389 dev->size = slots * engine_page_size;
390
391 dev_dbg(&slave->dev,
392 "engine[%i]: type %x, version %x, addr %x size %x\n",
393 dev->unit, dev->engine_type, version,
394 dev->addr, dev->size);
395
396 dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
397 slave->master->idx, slave->link,
398 slave->id, i - 2);
399
400 rc = device_register(&dev->dev);
401 if (rc) {
402 dev_warn(&slave->dev, "add failed: %d\n", rc);
403 put_device(&dev->dev);
404 }
405 }
406
407 engine_addr += slots * engine_page_size;
408
409 if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
410 break;
411 }
412
413 return 0;
414}
415
125739cb
JK
416static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
417 struct kobject *kobj, struct bin_attribute *attr, char *buf,
418 loff_t off, size_t count)
419{
420 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
421 size_t total_len, read_len;
422 int rc;
423
424 if (off < 0)
425 return -EINVAL;
426
427 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
428 return -EINVAL;
429
430 for (total_len = 0; total_len < count; total_len += read_len) {
431 read_len = min_t(size_t, count, 4);
432 read_len -= off & 0x3;
433
434 rc = fsi_slave_read(slave, off, buf + total_len, read_len);
435 if (rc)
436 return rc;
437
438 off += read_len;
439 }
440
441 return count;
442}
443
444static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
445 struct kobject *kobj, struct bin_attribute *attr,
446 char *buf, loff_t off, size_t count)
447{
448 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
449 size_t total_len, write_len;
450 int rc;
451
452 if (off < 0)
453 return -EINVAL;
454
455 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
456 return -EINVAL;
457
458 for (total_len = 0; total_len < count; total_len += write_len) {
459 write_len = min_t(size_t, count, 4);
460 write_len -= off & 0x3;
461
462 rc = fsi_slave_write(slave, off, buf + total_len, write_len);
463 if (rc)
464 return rc;
465
466 off += write_len;
467 }
468
469 return count;
470}
471
472static struct bin_attribute fsi_slave_raw_attr = {
473 .attr = {
474 .name = "raw",
475 .mode = 0600,
476 },
477 .size = 0,
478 .read = fsi_slave_sysfs_raw_read,
479 .write = fsi_slave_sysfs_raw_write,
480};
481
482static ssize_t fsi_slave_sysfs_term_write(struct file *file,
483 struct kobject *kobj, struct bin_attribute *attr,
484 char *buf, loff_t off, size_t count)
485{
486 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
487 struct fsi_master *master = slave->master;
488
489 if (!master->term)
490 return -ENODEV;
491
492 master->term(master, slave->link, slave->id);
493 return count;
494}
495
496static struct bin_attribute fsi_slave_term_attr = {
497 .attr = {
498 .name = "term",
499 .mode = 0200,
500 },
501 .size = 0,
502 .write = fsi_slave_sysfs_term_write,
503};
504
2b37c3e2
CB
505/* Encode slave local bus echo delay */
506static inline uint32_t fsi_smode_echodly(int x)
507{
508 return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
509}
510
511/* Encode slave local bus send delay */
512static inline uint32_t fsi_smode_senddly(int x)
513{
514 return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
515}
516
517/* Encode slave local bus clock rate ratio */
518static inline uint32_t fsi_smode_lbcrr(int x)
519{
520 return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
521}
522
523/* Encode slave ID */
524static inline uint32_t fsi_smode_sid(int x)
525{
526 return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
527}
528
529static const uint32_t fsi_slave_smode(int id)
530{
531 return FSI_SMODE_WSC | FSI_SMODE_ECRC
532 | fsi_smode_sid(id)
533 | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf)
534 | fsi_smode_lbcrr(0x8);
535}
536
537static int fsi_slave_set_smode(struct fsi_master *master, int link, int id)
538{
539 uint32_t smode;
540
541 /* set our smode register with the slave ID field to 0; this enables
542 * extended slave addressing
543 */
544 smode = fsi_slave_smode(id);
545 smode = cpu_to_be32(smode);
546
547 return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SMODE,
548 &smode, sizeof(smode));
549}
550
2b545cd8
JK
551static void fsi_slave_release(struct device *dev)
552{
553 struct fsi_slave *slave = to_fsi_slave(dev);
554
555 kfree(slave);
556}
557
414c1026
JK
558static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
559{
2b545cd8
JK
560 struct fsi_slave *slave;
561 uint32_t chip_id;
562 uint8_t crc;
563 int rc;
564
565 /* Currently, we only support single slaves on a link, and use the
566 * full 23-bit address range
567 */
568 if (id != 0)
569 return -EINVAL;
570
571 rc = fsi_master_read(master, link, id, 0, &chip_id, sizeof(chip_id));
572 if (rc) {
573 dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
574 link, id, rc);
575 return -ENODEV;
576 }
577 chip_id = be32_to_cpu(chip_id);
578
579 crc = crc4(0, chip_id, 32);
580 if (crc) {
581 dev_warn(&master->dev, "slave %02x:%02x invalid chip id CRC!\n",
582 link, id);
583 return -EIO;
584 }
585
586 dev_info(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
587 chip_id, master->idx, link, id);
588
2b37c3e2
CB
589 rc = fsi_slave_set_smode(master, link, id);
590 if (rc) {
591 dev_warn(&master->dev,
592 "can't set smode on slave:%02x:%02x %d\n",
593 link, id, rc);
594 return -ENODEV;
595 }
596
2b545cd8
JK
597 /* We can communicate with a slave; create the slave device and
598 * register.
599 */
600 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
601 if (!slave)
602 return -ENOMEM;
603
604 slave->master = master;
605 slave->dev.parent = &master->dev;
606 slave->dev.release = fsi_slave_release;
607 slave->link = link;
608 slave->id = id;
609 slave->size = FSI_SLAVE_SIZE_23b;
610
611 dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
612 rc = device_register(&slave->dev);
613 if (rc < 0) {
614 dev_warn(&master->dev, "failed to create slave device: %d\n",
615 rc);
616 put_device(&slave->dev);
617 return rc;
618 }
619
125739cb
JK
620 rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
621 if (rc)
622 dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
623
624 rc = device_create_bin_file(&slave->dev, &fsi_slave_term_attr);
625 if (rc)
626 dev_warn(&slave->dev, "failed to create term attr: %d\n", rc);
627
f7ade2a6
JK
628 rc = fsi_slave_scan(slave);
629 if (rc)
630 dev_dbg(&master->dev, "failed during slave scan with: %d\n",
631 rc);
414c1026 632
2b545cd8 633 return rc;
414c1026
JK
634}
635
09aecfab 636/* FSI master support */
014c2abc
JK
637static int fsi_check_access(uint32_t addr, size_t size)
638{
639 if (size != 1 && size != 2 && size != 4)
640 return -EINVAL;
641
642 if ((addr & 0x3) != (size & 0x3))
643 return -EINVAL;
644
645 return 0;
646}
647
648static int fsi_master_read(struct fsi_master *master, int link,
649 uint8_t slave_id, uint32_t addr, void *val, size_t size)
650{
651 int rc;
652
66433b05
JK
653 trace_fsi_master_read(master, link, slave_id, addr, size);
654
014c2abc 655 rc = fsi_check_access(addr, size);
66433b05
JK
656 if (!rc)
657 rc = master->read(master, link, slave_id, addr, val, size);
658
659 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
660 false, val, rc);
014c2abc 661
66433b05 662 return rc;
014c2abc
JK
663}
664
665static int fsi_master_write(struct fsi_master *master, int link,
666 uint8_t slave_id, uint32_t addr, const void *val, size_t size)
667{
668 int rc;
669
66433b05
JK
670 trace_fsi_master_write(master, link, slave_id, addr, size, val);
671
014c2abc 672 rc = fsi_check_access(addr, size);
66433b05
JK
673 if (!rc)
674 rc = master->write(master, link, slave_id, addr, val, size);
014c2abc 675
66433b05
JK
676 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
677 true, val, rc);
678
679 return rc;
014c2abc
JK
680}
681
26095282
CB
682static int fsi_master_link_enable(struct fsi_master *master, int link)
683{
684 if (master->link_enable)
685 return master->link_enable(master, link);
686
687 return 0;
688}
689
690/*
691 * Issue a break command on this link
692 */
693static int fsi_master_break(struct fsi_master *master, int link)
694{
66433b05
JK
695 trace_fsi_master_break(master, link);
696
26095282
CB
697 if (master->send_break)
698 return master->send_break(master, link);
699
700 return 0;
701}
702
414c1026
JK
703static int fsi_master_scan(struct fsi_master *master)
704{
26095282
CB
705 int link, rc;
706
707 for (link = 0; link < master->n_links; link++) {
708 rc = fsi_master_link_enable(master, link);
709 if (rc) {
710 dev_dbg(&master->dev,
711 "enable link %d failed: %d\n", link, rc);
712 continue;
713 }
714 rc = fsi_master_break(master, link);
715 if (rc) {
716 dev_dbg(&master->dev,
717 "break to link %d failed: %d\n", link, rc);
718 continue;
719 }
414c1026 720
414c1026 721 fsi_slave_init(master, link, 0);
26095282 722 }
414c1026
JK
723
724 return 0;
725}
726
cd0fdb5c
CB
727static int fsi_slave_remove_device(struct device *dev, void *arg)
728{
729 device_unregister(dev);
730 return 0;
731}
732
733static int fsi_master_remove_slave(struct device *dev, void *arg)
734{
735 device_for_each_child(dev, NULL, fsi_slave_remove_device);
736 device_unregister(dev);
737 return 0;
738}
739
740static void fsi_master_unscan(struct fsi_master *master)
741{
742 device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
743}
744
745static ssize_t master_rescan_store(struct device *dev,
746 struct device_attribute *attr, const char *buf, size_t count)
747{
748 struct fsi_master *master = to_fsi_master(dev);
749 int rc;
750
751 fsi_master_unscan(master);
752 rc = fsi_master_scan(master);
753 if (rc < 0)
754 return rc;
755
756 return count;
757}
758
759static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
760
125739cb
JK
761static ssize_t master_break_store(struct device *dev,
762 struct device_attribute *attr, const char *buf, size_t count)
763{
764 struct fsi_master *master = to_fsi_master(dev);
765
766 fsi_master_break(master, 0);
767
768 return count;
769}
770
771static DEVICE_ATTR(break, 0200, NULL, master_break_store);
772
09aecfab
JK
773int fsi_master_register(struct fsi_master *master)
774{
775 int rc;
776
777 if (!master)
778 return -EINVAL;
779
780 master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
781 dev_set_name(&master->dev, "fsi%d", master->idx);
782
783 rc = device_register(&master->dev);
414c1026 784 if (rc) {
09aecfab 785 ida_simple_remove(&master_ida, master->idx);
414c1026
JK
786 return rc;
787 }
09aecfab 788
cd0fdb5c
CB
789 rc = device_create_file(&master->dev, &dev_attr_rescan);
790 if (rc) {
791 device_unregister(&master->dev);
792 ida_simple_remove(&master_ida, master->idx);
793 return rc;
794 }
795
125739cb
JK
796 rc = device_create_file(&master->dev, &dev_attr_break);
797 if (rc) {
798 device_unregister(&master->dev);
799 ida_simple_remove(&master_ida, master->idx);
800 return rc;
801 }
802
414c1026 803 fsi_master_scan(master);
cd0fdb5c 804
414c1026 805 return 0;
09aecfab
JK
806}
807EXPORT_SYMBOL_GPL(fsi_master_register);
808
809void fsi_master_unregister(struct fsi_master *master)
810{
811 if (master->idx >= 0) {
812 ida_simple_remove(&master_ida, master->idx);
813 master->idx = -1;
814 }
815
cd0fdb5c 816 fsi_master_unscan(master);
09aecfab
JK
817 device_unregister(&master->dev);
818}
819EXPORT_SYMBOL_GPL(fsi_master_unregister);
820
0508ad1f
JK
821/* FSI core & Linux bus type definitions */
822
dd37eed7
JK
823static int fsi_bus_match(struct device *dev, struct device_driver *drv)
824{
825 struct fsi_device *fsi_dev = to_fsi_dev(dev);
826 struct fsi_driver *fsi_drv = to_fsi_drv(drv);
827 const struct fsi_device_id *id;
828
829 if (!fsi_drv->id_table)
830 return 0;
831
832 for (id = fsi_drv->id_table; id->engine_type; id++) {
833 if (id->engine_type != fsi_dev->engine_type)
834 continue;
835 if (id->version == FSI_VERSION_ANY ||
836 id->version == fsi_dev->version)
837 return 1;
838 }
839
840 return 0;
841}
842
356d8009
CB
843int fsi_driver_register(struct fsi_driver *fsi_drv)
844{
845 if (!fsi_drv)
846 return -EINVAL;
847 if (!fsi_drv->id_table)
848 return -EINVAL;
849
850 return driver_register(&fsi_drv->drv);
851}
852EXPORT_SYMBOL_GPL(fsi_driver_register);
853
854void fsi_driver_unregister(struct fsi_driver *fsi_drv)
855{
856 driver_unregister(&fsi_drv->drv);
857}
858EXPORT_SYMBOL_GPL(fsi_driver_unregister);
859
0508ad1f
JK
860struct bus_type fsi_bus_type = {
861 .name = "fsi",
dd37eed7 862 .match = fsi_bus_match,
0508ad1f
JK
863};
864EXPORT_SYMBOL_GPL(fsi_bus_type);
865
866static int fsi_init(void)
867{
868 return bus_register(&fsi_bus_type);
869}
870
871static void fsi_exit(void)
872{
873 bus_unregister(&fsi_bus_type);
874}
875
876module_init(fsi_init);
877module_exit(fsi_exit);
1fa847d7
JK
878module_param(discard_errors, int, 0664);
879MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");