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