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