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