blob: 59ddc9fd5bca47cdf6f997c3ac72e08a7d215e6e [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Jeremy Kerr0508ad12017-02-01 10:53:41 -06002/*
3 * FSI core driver
4 *
5 * Copyright (C) IBM Corporation 2016
6 *
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10007 * TODO:
8 * - Rework topology
9 * - s/chip_id/chip_loc
10 * - s/cfam/chip (cfam_id -> chip_id etc...)
Jeremy Kerr0508ad12017-02-01 10:53:41 -060011 */
12
Jeremy Kerr2b545cd2017-06-06 16:08:42 -050013#include <linux/crc4.h>
Jeremy Kerr0508ad12017-02-01 10:53:41 -060014#include <linux/device.h>
15#include <linux/fsi.h>
Jeremy Kerr09aecfa2017-06-06 16:08:36 -050016#include <linux/idr.h>
Jeremy Kerr0508ad12017-02-01 10:53:41 -060017#include <linux/module.h>
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +103018#include <linux/of.h>
Jeremy Kerr2b545cd2017-06-06 16:08:42 -050019#include <linux/slab.h>
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -050020#include <linux/bitops.h>
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +100021#include <linux/cdev.h>
22#include <linux/fs.h>
23#include <linux/uaccess.h>
Jeremy Kerr0508ad12017-02-01 10:53:41 -060024
Jeremy Kerr09aecfa2017-06-06 16:08:36 -050025#include "fsi-master.h"
26
Jeremy Kerr66433b02017-06-06 16:08:51 -050027#define CREATE_TRACE_POINTS
28#include <trace/events/fsi.h>
29
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -050030#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
Jeremy Kerr4efe37f2017-06-06 16:08:45 -050041#define FSI_PEEK_BASE 0x410
42
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -050043static const int engine_page_size = 0x400;
44
Christopher Bostic2b37c3e2017-06-06 16:08:43 -050045#define FSI_SLAVE_BASE 0x800
46
47/*
48 * FSI slave engine control register offsets
49 */
Jeremy Kerr1fa847d2017-06-06 16:08:52 -050050#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 */
Eddie James196964a2020-05-20 13:17:07 -050053#define FSI_SLBUS 0x30 /* W : LBUS Ownership */
Jeremy Kerr4af889b2017-06-06 16:08:58 -050054#define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
Christopher Bostic2b37c3e2017-06-06 16:08:43 -050055
56/*
57 * SMODE fields
58 */
59#define FSI_SMODE_WSC 0x80000000 /* Warm start done */
60#define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
61#define FSI_SMODE_SID_SHIFT 24 /* ID shift */
62#define FSI_SMODE_SID_MASK 3 /* ID Mask */
63#define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
64#define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
65#define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
66#define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
67#define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
68#define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
69
Jeremy Kerr4af889b2017-06-06 16:08:58 -050070/*
Eddie James196964a2020-05-20 13:17:07 -050071 * SLBUS fields
72 */
73#define FSI_SLBUS_FORCE 0x80000000 /* Force LBUS ownership */
74
75/*
Jeremy Kerr4af889b2017-06-06 16:08:58 -050076 * LLMODE fields
77 */
78#define FSI_LLMODE_ASYNC 0x1
79
Jeremy Kerr2b545cd2017-06-06 16:08:42 -050080#define FSI_SLAVE_SIZE_23b 0x800000
81
Jeremy Kerr09aecfa2017-06-06 16:08:36 -050082static DEFINE_IDA(master_ida);
83
Jeremy Kerrfaf0b1162017-06-06 16:08:37 -050084struct fsi_slave {
85 struct device dev;
86 struct fsi_master *master;
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +100087 struct cdev cdev;
88 int cdev_idx;
89 int id; /* FSI address */
90 int link; /* FSI link# */
91 u32 cfam_id;
Benjamin Herrenschmidt0a213772018-06-20 15:16:31 +100092 int chip_id;
Jeremy Kerrfaf0b1162017-06-06 16:08:37 -050093 uint32_t size; /* size of slave address space */
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +100094 u8 t_send_delay;
95 u8 t_echo_delay;
Jeremy Kerrfaf0b1162017-06-06 16:08:37 -050096};
97
Christopher Bosticcd0fdb52017-06-06 16:08:46 -050098#define to_fsi_master(d) container_of(d, struct fsi_master, dev)
Jeremy Kerrfaf0b1162017-06-06 16:08:37 -050099#define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
100
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500101static const int slave_retries = 2;
102static int discard_errors;
103
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +1000104static dev_t fsi_base_dev;
105static DEFINE_IDA(fsi_minor_ida);
106#define FSI_CHAR_MAX_DEVICES 0x1000
107
108/* Legacy /dev numbering: 4 devices per chip, 16 chips */
109#define FSI_CHAR_LEGACY_TOP 64
110
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500111static int fsi_master_read(struct fsi_master *master, int link,
112 uint8_t slave_id, uint32_t addr, void *val, size_t size);
113static int fsi_master_write(struct fsi_master *master, int link,
114 uint8_t slave_id, uint32_t addr, const void *val, size_t size);
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500115static int fsi_master_break(struct fsi_master *master, int link);
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500116
Jeremy Kerr4efe37f2017-06-06 16:08:45 -0500117/*
118 * fsi_device_read() / fsi_device_write() / fsi_device_peek()
119 *
120 * FSI endpoint-device support
121 *
122 * Read / write / peek accessors for a client
123 *
124 * Parameters:
125 * dev: Structure passed to FSI client device drivers on probe().
126 * addr: FSI address of given device. Client should pass in its base address
127 * plus desired offset to access its register space.
128 * val: For read/peek this is the value read at the specified address. For
129 * write this is value to write to the specified address.
130 * The data in val must be FSI bus endian (big endian).
131 * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
132 * Addresses must be aligned on size boundaries or an error will result.
133 */
134int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
135 size_t size)
136{
137 if (addr > dev->size || size > dev->size || addr > dev->size - size)
138 return -EINVAL;
139
140 return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
141}
142EXPORT_SYMBOL_GPL(fsi_device_read);
143
144int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
145 size_t size)
146{
147 if (addr > dev->size || size > dev->size || addr > dev->size - size)
148 return -EINVAL;
149
150 return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
151}
152EXPORT_SYMBOL_GPL(fsi_device_write);
153
154int fsi_device_peek(struct fsi_device *dev, void *val)
155{
156 uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));
157
158 return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
159}
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500160
161static void fsi_device_release(struct device *_device)
162{
163 struct fsi_device *device = to_fsi_dev(_device);
164
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030165 of_node_put(device->dev.of_node);
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500166 kfree(device);
167}
168
169static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
170{
171 struct fsi_device *dev;
172
173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
174 if (!dev)
175 return NULL;
176
177 dev->dev.parent = &slave->dev;
178 dev->dev.bus = &fsi_bus_type;
179 dev->dev.release = fsi_device_release;
180
181 return dev;
182}
183
Jeremy Kerr414c1022017-06-06 16:08:38 -0500184/* FSI slave support */
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500185static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
186 uint8_t *idp)
187{
188 uint32_t addr = *addrp;
189 uint8_t id = *idp;
190
191 if (addr > slave->size)
192 return -EINVAL;
193
194 /* For 23 bit addressing, we encode the extra two bits in the slave
195 * id (and the slave's actual ID needs to be 0).
196 */
197 if (addr > 0x1fffff) {
198 if (slave->id != 0)
199 return -EINVAL;
200 id = (addr >> 21) & 0x3;
201 addr &= 0x1fffff;
202 }
203
204 *addrp = addr;
205 *idp = id;
206 return 0;
207}
208
Colin Ian Kinged50a082017-10-03 09:31:45 +0100209static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500210{
211 struct fsi_master *master = slave->master;
Joel Stanley11454d62018-06-18 13:13:35 +0930212 __be32 irq, stat;
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500213 int rc, link;
214 uint8_t id;
215
216 link = slave->link;
217 id = slave->id;
218
219 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
220 &irq, sizeof(irq));
221 if (rc)
222 return rc;
223
224 rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
225 &stat, sizeof(stat));
226 if (rc)
227 return rc;
228
Christopher Bostic638bd9a2018-02-12 15:45:46 +1030229 dev_dbg(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n",
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500230 be32_to_cpu(stat), be32_to_cpu(irq));
231
232 /* clear interrupts */
233 return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
234 &irq, sizeof(irq));
235}
236
Benjamin Herrenschmidt935f9632018-05-29 14:44:08 +1000237/* Encode slave local bus echo delay */
238static inline uint32_t fsi_smode_echodly(int x)
239{
240 return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
241}
242
243/* Encode slave local bus send delay */
244static inline uint32_t fsi_smode_senddly(int x)
245{
246 return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
247}
248
249/* Encode slave local bus clock rate ratio */
250static inline uint32_t fsi_smode_lbcrr(int x)
251{
252 return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
253}
254
255/* Encode slave ID */
256static inline uint32_t fsi_smode_sid(int x)
257{
258 return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
259}
260
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000261static uint32_t fsi_slave_smode(int id, u8 t_senddly, u8 t_echodly)
Benjamin Herrenschmidt935f9632018-05-29 14:44:08 +1000262{
263 return FSI_SMODE_WSC | FSI_SMODE_ECRC
264 | fsi_smode_sid(id)
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000265 | fsi_smode_echodly(t_echodly - 1) | fsi_smode_senddly(t_senddly - 1)
Benjamin Herrenschmidt935f9632018-05-29 14:44:08 +1000266 | fsi_smode_lbcrr(0x8);
267}
268
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000269static int fsi_slave_set_smode(struct fsi_slave *slave)
Benjamin Herrenschmidt935f9632018-05-29 14:44:08 +1000270{
271 uint32_t smode;
272 __be32 data;
273
274 /* set our smode register with the slave ID field to 0; this enables
275 * extended slave addressing
276 */
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000277 smode = fsi_slave_smode(slave->id, slave->t_send_delay, slave->t_echo_delay);
Benjamin Herrenschmidt935f9632018-05-29 14:44:08 +1000278 data = cpu_to_be32(smode);
279
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000280 return fsi_master_write(slave->master, slave->link, slave->id,
281 FSI_SLAVE_BASE + FSI_SMODE,
282 &data, sizeof(data));
Benjamin Herrenschmidt935f9632018-05-29 14:44:08 +1000283}
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500284
Colin Ian Kinged50a082017-10-03 09:31:45 +0100285static int fsi_slave_handle_error(struct fsi_slave *slave, bool write,
286 uint32_t addr, size_t size)
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500287{
288 struct fsi_master *master = slave->master;
289 int rc, link;
290 uint32_t reg;
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000291 uint8_t id, send_delay, echo_delay;
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500292
293 if (discard_errors)
294 return -1;
295
296 link = slave->link;
297 id = slave->id;
298
299 dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]",
300 write ? "write" : "read", addr, size);
301
302 /* try a simple clear of error conditions, which may fail if we've lost
303 * communication with the slave
304 */
305 rc = fsi_slave_report_and_clear_errors(slave);
306 if (!rc)
307 return 0;
308
309 /* send a TERM and retry */
310 if (master->term) {
311 rc = master->term(master, link, id);
312 if (!rc) {
313 rc = fsi_master_read(master, link, id, 0,
314 &reg, sizeof(reg));
315 if (!rc)
316 rc = fsi_slave_report_and_clear_errors(slave);
317 if (!rc)
318 return 0;
319 }
320 }
321
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000322 send_delay = slave->t_send_delay;
323 echo_delay = slave->t_echo_delay;
324
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500325 /* getting serious, reset the slave via BREAK */
326 rc = fsi_master_break(master, link);
327 if (rc)
328 return rc;
329
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000330 slave->t_send_delay = send_delay;
331 slave->t_echo_delay = echo_delay;
332
333 rc = fsi_slave_set_smode(slave);
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500334 if (rc)
335 return rc;
336
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000337 if (master->link_config)
338 master->link_config(master, link,
339 slave->t_send_delay,
340 slave->t_echo_delay);
341
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500342 return fsi_slave_report_and_clear_errors(slave);
343}
344
Jeremy Kerrda36cad2017-06-06 16:08:50 -0500345int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500346 void *val, size_t size)
347{
348 uint8_t id = slave->id;
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500349 int rc, err_rc, i;
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500350
351 rc = fsi_slave_calc_addr(slave, &addr, &id);
352 if (rc)
353 return rc;
354
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500355 for (i = 0; i < slave_retries; i++) {
356 rc = fsi_master_read(slave->master, slave->link,
357 id, addr, val, size);
358 if (!rc)
359 break;
360
361 err_rc = fsi_slave_handle_error(slave, false, addr, size);
362 if (err_rc)
363 break;
364 }
365
366 return rc;
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500367}
Jeremy Kerrda36cad2017-06-06 16:08:50 -0500368EXPORT_SYMBOL_GPL(fsi_slave_read);
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500369
Jeremy Kerrda36cad2017-06-06 16:08:50 -0500370int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500371 const void *val, size_t size)
372{
373 uint8_t id = slave->id;
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500374 int rc, err_rc, i;
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500375
376 rc = fsi_slave_calc_addr(slave, &addr, &id);
377 if (rc)
378 return rc;
379
Jeremy Kerr1fa847d2017-06-06 16:08:52 -0500380 for (i = 0; i < slave_retries; i++) {
381 rc = fsi_master_write(slave->master, slave->link,
382 id, addr, val, size);
383 if (!rc)
384 break;
385
386 err_rc = fsi_slave_handle_error(slave, true, addr, size);
387 if (err_rc)
388 break;
389 }
390
391 return rc;
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500392}
Jeremy Kerrda36cad2017-06-06 16:08:50 -0500393EXPORT_SYMBOL_GPL(fsi_slave_write);
394
395extern int fsi_slave_claim_range(struct fsi_slave *slave,
396 uint32_t addr, uint32_t size)
397{
398 if (addr + size < addr)
399 return -EINVAL;
400
401 if (addr + size > slave->size)
402 return -EINVAL;
403
404 /* todo: check for overlapping claims */
405 return 0;
406}
407EXPORT_SYMBOL_GPL(fsi_slave_claim_range);
408
409extern void fsi_slave_release_range(struct fsi_slave *slave,
410 uint32_t addr, uint32_t size)
411{
412}
413EXPORT_SYMBOL_GPL(fsi_slave_release_range);
Jeremy Kerr014c2ab2017-06-06 16:08:40 -0500414
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030415static bool fsi_device_node_matches(struct device *dev, struct device_node *np,
416 uint32_t addr, uint32_t size)
417{
418 unsigned int len, na, ns;
419 const __be32 *prop;
420 uint32_t psize;
421
422 na = of_n_addr_cells(np);
423 ns = of_n_size_cells(np);
424
425 if (na != 1 || ns != 1)
426 return false;
427
428 prop = of_get_property(np, "reg", &len);
429 if (!prop || len != 8)
430 return false;
431
432 if (of_read_number(prop, 1) != addr)
433 return false;
434
435 psize = of_read_number(prop + 1, 1);
436 if (psize != size) {
437 dev_warn(dev,
438 "node %s matches probed address, but not size (got 0x%x, expected 0x%x)",
439 of_node_full_name(np), psize, size);
440 }
441
442 return true;
443}
444
445/* Find a matching node for the slave engine at @address, using @size bytes
446 * of space. Returns NULL if not found, or a matching node with refcount
447 * already incremented.
448 */
449static struct device_node *fsi_device_find_of_node(struct fsi_device *dev)
450{
451 struct device_node *parent, *np;
452
453 parent = dev_of_node(&dev->slave->dev);
454 if (!parent)
455 return NULL;
456
457 for_each_child_of_node(parent, np) {
458 if (fsi_device_node_matches(&dev->dev, np,
459 dev->addr, dev->size))
460 return np;
461 }
462
463 return NULL;
464}
465
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500466static int fsi_slave_scan(struct fsi_slave *slave)
467{
468 uint32_t engine_addr;
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500469 int rc, i;
470
471 /*
472 * scan engines
473 *
474 * We keep the peek mode and slave engines for the core; so start
475 * at the third slot in the configuration table. We also need to
476 * skip the chip ID entry at the start of the address space.
477 */
478 engine_addr = engine_page_size * 3;
479 for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
480 uint8_t slots, version, type, crc;
481 struct fsi_device *dev;
Joel Stanley11454d62018-06-18 13:13:35 +0930482 uint32_t conf;
483 __be32 data;
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500484
Joel Stanley11454d62018-06-18 13:13:35 +0930485 rc = fsi_slave_read(slave, (i + 1) * sizeof(data),
486 &data, sizeof(data));
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500487 if (rc) {
488 dev_warn(&slave->dev,
489 "error reading slave registers\n");
490 return -1;
491 }
Joel Stanley11454d62018-06-18 13:13:35 +0930492 conf = be32_to_cpu(data);
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500493
494 crc = crc4(0, conf, 32);
495 if (crc) {
496 dev_warn(&slave->dev,
497 "crc error in slave register at 0x%04x\n",
498 i);
499 return -1;
500 }
501
502 slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
503 >> FSI_SLAVE_CONF_SLOTS_SHIFT;
504 version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
505 >> FSI_SLAVE_CONF_VERSION_SHIFT;
506 type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
507 >> FSI_SLAVE_CONF_TYPE_SHIFT;
508
509 /*
510 * Unused address areas are marked by a zero type value; this
511 * skips the defined address areas
512 */
513 if (type != 0 && slots != 0) {
514
515 /* create device */
516 dev = fsi_create_device(slave);
517 if (!dev)
518 return -ENOMEM;
519
520 dev->slave = slave;
521 dev->engine_type = type;
522 dev->version = version;
523 dev->unit = i;
524 dev->addr = engine_addr;
525 dev->size = slots * engine_page_size;
526
527 dev_dbg(&slave->dev,
528 "engine[%i]: type %x, version %x, addr %x size %x\n",
529 dev->unit, dev->engine_type, version,
530 dev->addr, dev->size);
531
532 dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
533 slave->master->idx, slave->link,
534 slave->id, i - 2);
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030535 dev->dev.of_node = fsi_device_find_of_node(dev);
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -0500536
537 rc = device_register(&dev->dev);
538 if (rc) {
539 dev_warn(&slave->dev, "add failed: %d\n", rc);
540 put_device(&dev->dev);
541 }
542 }
543
544 engine_addr += slots * engine_page_size;
545
546 if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
547 break;
548 }
549
550 return 0;
551}
552
Andrew Jeffery9f4c2b52019-11-08 15:49:39 +1030553static unsigned long aligned_access_size(size_t offset, size_t count)
554{
555 unsigned long offset_unit, count_unit;
556
557 /* Criteria:
558 *
559 * 1. Access size must be less than or equal to the maximum access
560 * width or the highest power-of-two factor of offset
561 * 2. Access size must be less than or equal to the amount specified by
562 * count
563 *
564 * The access width is optimal if we can calculate 1 to be strictly
565 * equal while still satisfying 2.
566 */
567
568 /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
569 offset_unit = BIT(__builtin_ctzl(offset | 4));
570
571 /* Find 2 by the top bit of count */
572 count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count));
573
574 /* Constrain the maximum access width to the minimum of both criteria */
575 return BIT(__builtin_ctzl(offset_unit | count_unit));
576}
577
Jeremy Kerr125739c2017-06-06 16:08:49 -0500578static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
579 struct kobject *kobj, struct bin_attribute *attr, char *buf,
580 loff_t off, size_t count)
581{
582 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
583 size_t total_len, read_len;
584 int rc;
585
586 if (off < 0)
587 return -EINVAL;
588
589 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
590 return -EINVAL;
591
592 for (total_len = 0; total_len < count; total_len += read_len) {
Andrew Jeffery9f4c2b52019-11-08 15:49:39 +1030593 read_len = aligned_access_size(off, count - total_len);
Jeremy Kerr125739c2017-06-06 16:08:49 -0500594
595 rc = fsi_slave_read(slave, off, buf + total_len, read_len);
596 if (rc)
597 return rc;
598
599 off += read_len;
600 }
601
602 return count;
603}
604
605static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
606 struct kobject *kobj, struct bin_attribute *attr,
607 char *buf, loff_t off, size_t count)
608{
609 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
610 size_t total_len, write_len;
611 int rc;
612
613 if (off < 0)
614 return -EINVAL;
615
616 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
617 return -EINVAL;
618
619 for (total_len = 0; total_len < count; total_len += write_len) {
Andrew Jeffery9f4c2b52019-11-08 15:49:39 +1030620 write_len = aligned_access_size(off, count - total_len);
Jeremy Kerr125739c2017-06-06 16:08:49 -0500621
622 rc = fsi_slave_write(slave, off, buf + total_len, write_len);
623 if (rc)
624 return rc;
625
626 off += write_len;
627 }
628
629 return count;
630}
631
Bhumika Goyal061c0952017-08-02 19:39:32 +0530632static const struct bin_attribute fsi_slave_raw_attr = {
Jeremy Kerr125739c2017-06-06 16:08:49 -0500633 .attr = {
634 .name = "raw",
635 .mode = 0600,
636 },
637 .size = 0,
638 .read = fsi_slave_sysfs_raw_read,
639 .write = fsi_slave_sysfs_raw_write,
640};
641
Jeremy Kerr2b545cd2017-06-06 16:08:42 -0500642static void fsi_slave_release(struct device *dev)
643{
644 struct fsi_slave *slave = to_fsi_slave(dev);
645
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +1000646 fsi_free_minor(slave->dev.devt);
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030647 of_node_put(dev->of_node);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -0500648 kfree(slave);
649}
650
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +1030651static bool fsi_slave_node_matches(struct device_node *np,
652 int link, uint8_t id)
653{
654 unsigned int len, na, ns;
655 const __be32 *prop;
656
657 na = of_n_addr_cells(np);
658 ns = of_n_size_cells(np);
659
660 /* Ensure we have the correct format for addresses and sizes in
661 * reg properties
662 */
663 if (na != 2 || ns != 0)
664 return false;
665
666 prop = of_get_property(np, "reg", &len);
667 if (!prop || len != 8)
668 return false;
669
670 return (of_read_number(prop, 1) == link) &&
671 (of_read_number(prop + 1, 1) == id);
672}
673
674/* Find a matching node for the slave at (link, id). Returns NULL if none
675 * found, or a matching node with refcount already incremented.
676 */
677static struct device_node *fsi_slave_find_of_node(struct fsi_master *master,
678 int link, uint8_t id)
679{
680 struct device_node *parent, *np;
681
682 parent = dev_of_node(&master->dev);
683 if (!parent)
684 return NULL;
685
686 for_each_child_of_node(parent, np) {
687 if (fsi_slave_node_matches(np, link, id))
688 return np;
689 }
690
691 return NULL;
692}
693
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +1000694static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
695 loff_t *offset)
696{
697 struct fsi_slave *slave = filep->private_data;
698 size_t total_len, read_len;
699 loff_t off = *offset;
700 ssize_t rc;
701
702 if (off < 0)
703 return -EINVAL;
704
705 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
706 return -EINVAL;
707
708 for (total_len = 0; total_len < count; total_len += read_len) {
709 __be32 data;
710
711 read_len = min_t(size_t, count, 4);
712 read_len -= off & 0x3;
713
714 rc = fsi_slave_read(slave, off, &data, read_len);
715 if (rc)
716 goto fail;
717 rc = copy_to_user(buf + total_len, &data, read_len);
718 if (rc) {
719 rc = -EFAULT;
720 goto fail;
721 }
722 off += read_len;
723 }
724 rc = count;
725 fail:
726 *offset = off;
Colin Ian King91081092021-06-03 13:28:12 +0100727 return rc;
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +1000728}
729
730static ssize_t cfam_write(struct file *filep, const char __user *buf,
731 size_t count, loff_t *offset)
732{
733 struct fsi_slave *slave = filep->private_data;
734 size_t total_len, write_len;
735 loff_t off = *offset;
736 ssize_t rc;
737
738
739 if (off < 0)
740 return -EINVAL;
741
742 if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
743 return -EINVAL;
744
745 for (total_len = 0; total_len < count; total_len += write_len) {
746 __be32 data;
747
748 write_len = min_t(size_t, count, 4);
749 write_len -= off & 0x3;
750
751 rc = copy_from_user(&data, buf + total_len, write_len);
752 if (rc) {
753 rc = -EFAULT;
754 goto fail;
755 }
756 rc = fsi_slave_write(slave, off, &data, write_len);
757 if (rc)
758 goto fail;
759 off += write_len;
760 }
761 rc = count;
762 fail:
763 *offset = off;
Colin Ian King91081092021-06-03 13:28:12 +0100764 return rc;
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +1000765}
766
767static loff_t cfam_llseek(struct file *file, loff_t offset, int whence)
768{
769 switch (whence) {
770 case SEEK_CUR:
771 break;
772 case SEEK_SET:
773 file->f_pos = offset;
774 break;
775 default:
776 return -EINVAL;
777 }
778
779 return offset;
780}
781
782static int cfam_open(struct inode *inode, struct file *file)
783{
784 struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev);
785
786 file->private_data = slave;
787
788 return 0;
789}
790
791static const struct file_operations cfam_fops = {
792 .owner = THIS_MODULE,
793 .open = cfam_open,
794 .llseek = cfam_llseek,
795 .read = cfam_read,
796 .write = cfam_write,
797};
798
799static ssize_t send_term_store(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf, size_t count)
802{
803 struct fsi_slave *slave = to_fsi_slave(dev);
804 struct fsi_master *master = slave->master;
805
806 if (!master->term)
807 return -ENODEV;
808
809 master->term(master, slave->link, slave->id);
810 return count;
811}
812
813static DEVICE_ATTR_WO(send_term);
814
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +1000815static ssize_t slave_send_echo_show(struct device *dev,
816 struct device_attribute *attr,
817 char *buf)
818{
819 struct fsi_slave *slave = to_fsi_slave(dev);
820
821 return sprintf(buf, "%u\n", slave->t_send_delay);
822}
823
824static ssize_t slave_send_echo_store(struct device *dev,
825 struct device_attribute *attr, const char *buf, size_t count)
826{
827 struct fsi_slave *slave = to_fsi_slave(dev);
828 struct fsi_master *master = slave->master;
829 unsigned long val;
830 int rc;
831
832 if (kstrtoul(buf, 0, &val) < 0)
833 return -EINVAL;
834
835 if (val < 1 || val > 16)
836 return -EINVAL;
837
838 if (!master->link_config)
839 return -ENXIO;
840
841 /* Current HW mandates that send and echo delay are identical */
842 slave->t_send_delay = val;
843 slave->t_echo_delay = val;
844
845 rc = fsi_slave_set_smode(slave);
846 if (rc < 0)
847 return rc;
848 if (master->link_config)
849 master->link_config(master, slave->link,
850 slave->t_send_delay,
851 slave->t_echo_delay);
852
853 return count;
854}
855
856static DEVICE_ATTR(send_echo_delays, 0600,
857 slave_send_echo_show, slave_send_echo_store);
858
Benjamin Herrenschmidt0a213772018-06-20 15:16:31 +1000859static ssize_t chip_id_show(struct device *dev,
860 struct device_attribute *attr,
861 char *buf)
862{
863 struct fsi_slave *slave = to_fsi_slave(dev);
864
865 return sprintf(buf, "%d\n", slave->chip_id);
866}
867
868static DEVICE_ATTR_RO(chip_id);
869
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +1000870static ssize_t cfam_id_show(struct device *dev,
871 struct device_attribute *attr,
872 char *buf)
873{
874 struct fsi_slave *slave = to_fsi_slave(dev);
875
876 return sprintf(buf, "0x%x\n", slave->cfam_id);
877}
878
879static DEVICE_ATTR_RO(cfam_id);
880
881static struct attribute *cfam_attr[] = {
882 &dev_attr_send_echo_delays.attr,
883 &dev_attr_chip_id.attr,
884 &dev_attr_cfam_id.attr,
885 &dev_attr_send_term.attr,
886 NULL,
887};
888
889static const struct attribute_group cfam_attr_group = {
890 .attrs = cfam_attr,
891};
892
893static const struct attribute_group *cfam_attr_groups[] = {
894 &cfam_attr_group,
895 NULL,
896};
897
898static char *cfam_devnode(struct device *dev, umode_t *mode,
899 kuid_t *uid, kgid_t *gid)
900{
901 struct fsi_slave *slave = to_fsi_slave(dev);
902
903#ifdef CONFIG_FSI_NEW_DEV_NODE
904 return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx);
905#else
906 return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx);
907#endif
908}
909
910static const struct device_type cfam_type = {
911 .name = "cfam",
912 .devnode = cfam_devnode,
913 .groups = cfam_attr_groups
914};
915
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +1000916static char *fsi_cdev_devnode(struct device *dev, umode_t *mode,
917 kuid_t *uid, kgid_t *gid)
918{
919#ifdef CONFIG_FSI_NEW_DEV_NODE
920 return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev));
921#else
922 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
923#endif
924}
925
926const struct device_type fsi_cdev_type = {
927 .name = "fsi-cdev",
928 .devnode = fsi_cdev_devnode,
929};
930EXPORT_SYMBOL_GPL(fsi_cdev_type);
931
932/* Backward compatible /dev/ numbering in "old style" mode */
933static int fsi_adjust_index(int index)
934{
935#ifdef CONFIG_FSI_NEW_DEV_NODE
936 return index;
937#else
938 return index + 1;
939#endif
940}
941
942static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type,
943 dev_t *out_dev, int *out_index)
944{
945 int cid = slave->chip_id;
946 int id;
947
948 /* Check if we qualify for legacy numbering */
949 if (cid >= 0 && cid < 16 && type < 4) {
950 /* Try reserving the legacy number */
951 id = (cid << 4) | type;
952 id = ida_simple_get(&fsi_minor_ida, id, id + 1, GFP_KERNEL);
953 if (id >= 0) {
954 *out_index = fsi_adjust_index(cid);
955 *out_dev = fsi_base_dev + id;
956 return 0;
957 }
958 /* Other failure */
959 if (id != -ENOSPC)
960 return id;
961 /* Fallback to non-legacy allocation */
962 }
963 id = ida_simple_get(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP,
964 FSI_CHAR_MAX_DEVICES, GFP_KERNEL);
965 if (id < 0)
966 return id;
967 *out_index = fsi_adjust_index(id);
968 *out_dev = fsi_base_dev + id;
969 return 0;
970}
971
972int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type,
973 dev_t *out_dev, int *out_index)
974{
975 return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index);
976}
977EXPORT_SYMBOL_GPL(fsi_get_new_minor);
978
979void fsi_free_minor(dev_t dev)
980{
981 ida_simple_remove(&fsi_minor_ida, MINOR(dev));
982}
983EXPORT_SYMBOL_GPL(fsi_free_minor);
984
Jeremy Kerr414c1022017-06-06 16:08:38 -0500985static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
986{
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +1000987 uint32_t cfam_id;
Jeremy Kerr2b545cd2017-06-06 16:08:42 -0500988 struct fsi_slave *slave;
Jeremy Kerr2b545cd2017-06-06 16:08:42 -0500989 uint8_t crc;
Eddie James196964a2020-05-20 13:17:07 -0500990 __be32 data, llmode, slbus;
Jeremy Kerr2b545cd2017-06-06 16:08:42 -0500991 int rc;
Jeremy Kerr414c1022017-06-06 16:08:38 -0500992
Jeremy Kerr2b545cd2017-06-06 16:08:42 -0500993 /* Currently, we only support single slaves on a link, and use the
994 * full 23-bit address range
995 */
996 if (id != 0)
997 return -EINVAL;
998
Joel Stanley11454d62018-06-18 13:13:35 +0930999 rc = fsi_master_read(master, link, id, 0, &data, sizeof(data));
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001000 if (rc) {
1001 dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
1002 link, id, rc);
1003 return -ENODEV;
1004 }
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001005 cfam_id = be32_to_cpu(data);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001006
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001007 crc = crc4(0, cfam_id, 32);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001008 if (crc) {
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001009 dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n",
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001010 link, id);
1011 return -EIO;
1012 }
1013
Christopher Bostic638bd9a2018-02-12 15:45:46 +10301014 dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001015 cfam_id, master->idx, link, id);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001016
Jeremy Kerr4af889b2017-06-06 16:08:58 -05001017 /* If we're behind a master that doesn't provide a self-running bus
1018 * clock, put the slave into async mode
1019 */
1020 if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
1021 llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
1022 rc = fsi_master_write(master, link, id,
1023 FSI_SLAVE_BASE + FSI_LLMODE,
1024 &llmode, sizeof(llmode));
1025 if (rc)
1026 dev_warn(&master->dev,
1027 "can't set llmode on slave:%02x:%02x %d\n",
1028 link, id, rc);
1029 }
1030
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001031 /* We can communicate with a slave; create the slave device and
1032 * register.
1033 */
1034 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1035 if (!slave)
1036 return -ENOMEM;
1037
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001038 dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
1039 slave->dev.type = &cfam_type;
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001040 slave->dev.parent = &master->dev;
Jeremy Kerrf6a2f8e2018-02-12 15:45:45 +10301041 slave->dev.of_node = fsi_slave_find_of_node(master, link, id);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001042 slave->dev.release = fsi_slave_release;
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001043 device_initialize(&slave->dev);
1044 slave->cfam_id = cfam_id;
1045 slave->master = master;
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001046 slave->link = link;
1047 slave->id = id;
1048 slave->size = FSI_SLAVE_SIZE_23b;
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +10001049 slave->t_send_delay = 16;
1050 slave->t_echo_delay = 16;
1051
Benjamin Herrenschmidt0a213772018-06-20 15:16:31 +10001052 /* Get chip ID if any */
1053 slave->chip_id = -1;
1054 if (slave->dev.of_node) {
1055 uint32_t prop;
1056 if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop))
1057 slave->chip_id = prop;
1058
1059 }
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001060
Eddie James196964a2020-05-20 13:17:07 -05001061 slbus = cpu_to_be32(FSI_SLBUS_FORCE);
1062 rc = fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SLBUS,
1063 &slbus, sizeof(slbus));
1064 if (rc)
1065 dev_warn(&master->dev,
1066 "can't set slbus on slave:%02x:%02x %d\n", link, id,
1067 rc);
1068
Jeremy Kerr371975b2019-06-28 16:07:37 +08001069 rc = fsi_slave_set_smode(slave);
1070 if (rc) {
1071 dev_warn(&master->dev,
1072 "can't set smode on slave:%02x:%02x %d\n",
1073 link, id, rc);
1074 goto err_free;
1075 }
1076
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001077 /* Allocate a minor in the FSI space */
1078 rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt,
1079 &slave->cdev_idx);
1080 if (rc)
1081 goto err_free;
1082
1083 /* Create chardev for userspace access */
1084 cdev_init(&slave->cdev, &cfam_fops);
1085 rc = cdev_device_add(&slave->cdev, &slave->dev);
1086 if (rc) {
1087 dev_err(&slave->dev, "Error %d creating slave device\n", rc);
Jeremy Kerr371975b2019-06-28 16:07:37 +08001088 goto err_free_ida;
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001089 }
1090
Jeremy Kerr371975b2019-06-28 16:07:37 +08001091 /* Now that we have the cdev registered with the core, any fatal
1092 * failures beyond this point will need to clean up through
1093 * cdev_device_del(). Fortunately though, nothing past here is fatal.
1094 */
1095
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +10001096 if (master->link_config)
1097 master->link_config(master, link,
1098 slave->t_send_delay,
1099 slave->t_echo_delay);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001100
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001101 /* Legacy raw file -> to be removed */
Jeremy Kerr125739c2017-06-06 16:08:49 -05001102 rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
1103 if (rc)
1104 dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
1105
Benjamin Herrenschmidt0a213772018-06-20 15:16:31 +10001106
Jeremy Kerrf7ade2a2017-06-06 16:08:44 -05001107 rc = fsi_slave_scan(slave);
1108 if (rc)
1109 dev_dbg(&master->dev, "failed during slave scan with: %d\n",
1110 rc);
Jeremy Kerr2b545cd2017-06-06 16:08:42 -05001111
Jeremy Kerr371975b2019-06-28 16:07:37 +08001112 return 0;
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001113
Jeremy Kerr371975b2019-06-28 16:07:37 +08001114err_free_ida:
1115 fsi_free_minor(slave->dev.devt);
1116err_free:
1117 of_node_put(slave->dev.of_node);
1118 kfree(slave);
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001119 return rc;
Jeremy Kerr414c1022017-06-06 16:08:38 -05001120}
1121
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001122/* FSI master support */
Jeremy Kerr014c2ab2017-06-06 16:08:40 -05001123static int fsi_check_access(uint32_t addr, size_t size)
1124{
Eddie James99f039e2018-02-12 15:45:43 +10301125 if (size == 4) {
1126 if (addr & 0x3)
1127 return -EINVAL;
1128 } else if (size == 2) {
1129 if (addr & 0x1)
1130 return -EINVAL;
1131 } else if (size != 1)
Jeremy Kerr014c2ab2017-06-06 16:08:40 -05001132 return -EINVAL;
1133
1134 return 0;
1135}
1136
1137static int fsi_master_read(struct fsi_master *master, int link,
1138 uint8_t slave_id, uint32_t addr, void *val, size_t size)
1139{
1140 int rc;
1141
Jeremy Kerr66433b02017-06-06 16:08:51 -05001142 trace_fsi_master_read(master, link, slave_id, addr, size);
Jeremy Kerr014c2ab2017-06-06 16:08:40 -05001143
Jeremy Kerr66433b02017-06-06 16:08:51 -05001144 rc = fsi_check_access(addr, size);
1145 if (!rc)
1146 rc = master->read(master, link, slave_id, addr, val, size);
1147
1148 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
1149 false, val, rc);
1150
1151 return rc;
Jeremy Kerr014c2ab2017-06-06 16:08:40 -05001152}
1153
1154static int fsi_master_write(struct fsi_master *master, int link,
1155 uint8_t slave_id, uint32_t addr, const void *val, size_t size)
1156{
1157 int rc;
1158
Jeremy Kerr66433b02017-06-06 16:08:51 -05001159 trace_fsi_master_write(master, link, slave_id, addr, size, val);
Jeremy Kerr014c2ab2017-06-06 16:08:40 -05001160
Jeremy Kerr66433b02017-06-06 16:08:51 -05001161 rc = fsi_check_access(addr, size);
1162 if (!rc)
1163 rc = master->write(master, link, slave_id, addr, val, size);
1164
1165 trace_fsi_master_rw_result(master, link, slave_id, addr, size,
1166 true, val, rc);
1167
1168 return rc;
Jeremy Kerr014c2ab2017-06-06 16:08:40 -05001169}
1170
Eddie Jamesb36875a2020-06-09 16:39:28 -05001171static int fsi_master_link_disable(struct fsi_master *master, int link)
1172{
1173 if (master->link_enable)
1174 return master->link_enable(master, link, false);
1175
1176 return 0;
1177}
1178
Christopher Bostic26095282017-06-06 16:08:41 -05001179static int fsi_master_link_enable(struct fsi_master *master, int link)
1180{
1181 if (master->link_enable)
Eddie James04635a32020-06-09 16:39:27 -05001182 return master->link_enable(master, link, true);
Christopher Bostic26095282017-06-06 16:08:41 -05001183
1184 return 0;
1185}
1186
1187/*
1188 * Issue a break command on this link
1189 */
1190static int fsi_master_break(struct fsi_master *master, int link)
1191{
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +10001192 int rc = 0;
1193
Jeremy Kerr66433b02017-06-06 16:08:51 -05001194 trace_fsi_master_break(master, link);
1195
Christopher Bostic26095282017-06-06 16:08:41 -05001196 if (master->send_break)
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +10001197 rc = master->send_break(master, link);
1198 if (master->link_config)
1199 master->link_config(master, link, 16, 16);
Christopher Bostic26095282017-06-06 16:08:41 -05001200
Benjamin Herrenschmidta2e7da82018-05-29 15:01:07 +10001201 return rc;
Christopher Bostic26095282017-06-06 16:08:41 -05001202}
1203
Jeremy Kerr414c1022017-06-06 16:08:38 -05001204static int fsi_master_scan(struct fsi_master *master)
1205{
Christopher Bostic26095282017-06-06 16:08:41 -05001206 int link, rc;
Jeremy Kerr414c1022017-06-06 16:08:38 -05001207
Christopher Bostic26095282017-06-06 16:08:41 -05001208 for (link = 0; link < master->n_links; link++) {
1209 rc = fsi_master_link_enable(master, link);
1210 if (rc) {
1211 dev_dbg(&master->dev,
1212 "enable link %d failed: %d\n", link, rc);
1213 continue;
1214 }
1215 rc = fsi_master_break(master, link);
1216 if (rc) {
Eddie Jamesb36875a2020-06-09 16:39:28 -05001217 fsi_master_link_disable(master, link);
Christopher Bostic26095282017-06-06 16:08:41 -05001218 dev_dbg(&master->dev,
1219 "break to link %d failed: %d\n", link, rc);
1220 continue;
1221 }
1222
Eddie Jamesb36875a2020-06-09 16:39:28 -05001223 rc = fsi_slave_init(master, link, 0);
1224 if (rc)
1225 fsi_master_link_disable(master, link);
Christopher Bostic26095282017-06-06 16:08:41 -05001226 }
Jeremy Kerr414c1022017-06-06 16:08:38 -05001227
1228 return 0;
1229}
1230
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001231static int fsi_slave_remove_device(struct device *dev, void *arg)
1232{
1233 device_unregister(dev);
1234 return 0;
1235}
1236
1237static int fsi_master_remove_slave(struct device *dev, void *arg)
1238{
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001239 struct fsi_slave *slave = to_fsi_slave(dev);
1240
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001241 device_for_each_child(dev, NULL, fsi_slave_remove_device);
Benjamin Herrenschmidtd1dcd672018-06-21 12:34:22 +10001242 cdev_device_del(&slave->cdev, &slave->dev);
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +10001243 put_device(dev);
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001244 return 0;
1245}
1246
1247static void fsi_master_unscan(struct fsi_master *master)
1248{
1249 device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
1250}
1251
Jeremy Kerr15362d62018-02-12 15:45:40 +10301252int fsi_master_rescan(struct fsi_master *master)
1253{
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001254 int rc;
1255
1256 mutex_lock(&master->scan_lock);
Jeremy Kerr15362d62018-02-12 15:45:40 +10301257 fsi_master_unscan(master);
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001258 rc = fsi_master_scan(master);
1259 mutex_unlock(&master->scan_lock);
1260
1261 return rc;
Jeremy Kerr15362d62018-02-12 15:45:40 +10301262}
1263EXPORT_SYMBOL_GPL(fsi_master_rescan);
1264
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001265static ssize_t master_rescan_store(struct device *dev,
1266 struct device_attribute *attr, const char *buf, size_t count)
1267{
1268 struct fsi_master *master = to_fsi_master(dev);
1269 int rc;
1270
Jeremy Kerr15362d62018-02-12 15:45:40 +10301271 rc = fsi_master_rescan(master);
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001272 if (rc < 0)
1273 return rc;
1274
1275 return count;
1276}
1277
1278static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
1279
Jeremy Kerr125739c2017-06-06 16:08:49 -05001280static ssize_t master_break_store(struct device *dev,
1281 struct device_attribute *attr, const char *buf, size_t count)
1282{
1283 struct fsi_master *master = to_fsi_master(dev);
1284
1285 fsi_master_break(master, 0);
1286
1287 return count;
1288}
1289
1290static DEVICE_ATTR(break, 0200, NULL, master_break_store);
1291
Jeremy Kerrcf700ba2019-11-08 15:49:36 +10301292static struct attribute *master_attrs[] = {
1293 &dev_attr_break.attr,
1294 &dev_attr_rescan.attr,
1295 NULL
1296};
1297
1298ATTRIBUTE_GROUPS(master);
1299
kbuild test robot56ec3112019-11-08 15:49:40 +10301300static struct class fsi_master_class = {
Jeremy Kerr2e32c2d2019-11-08 15:49:35 +10301301 .name = "fsi-master",
Jeremy Kerrcf700ba2019-11-08 15:49:36 +10301302 .dev_groups = master_groups,
Jeremy Kerr2e32c2d2019-11-08 15:49:35 +10301303};
1304
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001305int fsi_master_register(struct fsi_master *master)
1306{
1307 int rc;
Christopher Bosticf3aa2c62018-02-12 15:45:49 +10301308 struct device_node *np;
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001309
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001310 mutex_init(&master->scan_lock);
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001311 master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
1312 dev_set_name(&master->dev, "fsi%d", master->idx);
Jeremy Kerr2e32c2d2019-11-08 15:49:35 +10301313 master->dev.class = &fsi_master_class;
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001314
1315 rc = device_register(&master->dev);
Jeremy Kerr414c1022017-06-06 16:08:38 -05001316 if (rc) {
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001317 ida_simple_remove(&master_ida, master->idx);
Jeremy Kerr414c1022017-06-06 16:08:38 -05001318 return rc;
1319 }
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001320
Christopher Bosticf3aa2c62018-02-12 15:45:49 +10301321 np = dev_of_node(&master->dev);
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001322 if (!of_property_read_bool(np, "no-scan-on-init")) {
1323 mutex_lock(&master->scan_lock);
Christopher Bosticf3aa2c62018-02-12 15:45:49 +10301324 fsi_master_scan(master);
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001325 mutex_unlock(&master->scan_lock);
1326 }
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001327
Jeremy Kerr414c1022017-06-06 16:08:38 -05001328 return 0;
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001329}
1330EXPORT_SYMBOL_GPL(fsi_master_register);
1331
1332void fsi_master_unregister(struct fsi_master *master)
1333{
1334 if (master->idx >= 0) {
1335 ida_simple_remove(&master_ida, master->idx);
1336 master->idx = -1;
1337 }
1338
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001339 mutex_lock(&master->scan_lock);
Christopher Bosticcd0fdb52017-06-06 16:08:46 -05001340 fsi_master_unscan(master);
Benjamin Herrenschmidt9840fcd2018-06-21 18:00:05 +10001341 mutex_unlock(&master->scan_lock);
Jeremy Kerr09aecfa2017-06-06 16:08:36 -05001342 device_unregister(&master->dev);
1343}
1344EXPORT_SYMBOL_GPL(fsi_master_unregister);
1345
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001346/* FSI core & Linux bus type definitions */
1347
Jeremy Kerrdd37eed2017-02-01 10:53:43 -06001348static int fsi_bus_match(struct device *dev, struct device_driver *drv)
1349{
1350 struct fsi_device *fsi_dev = to_fsi_dev(dev);
1351 struct fsi_driver *fsi_drv = to_fsi_drv(drv);
1352 const struct fsi_device_id *id;
1353
1354 if (!fsi_drv->id_table)
1355 return 0;
1356
1357 for (id = fsi_drv->id_table; id->engine_type; id++) {
1358 if (id->engine_type != fsi_dev->engine_type)
1359 continue;
1360 if (id->version == FSI_VERSION_ANY ||
1361 id->version == fsi_dev->version)
1362 return 1;
1363 }
1364
1365 return 0;
1366}
1367
Christopher Bostic356d8002017-06-06 16:08:48 -05001368int fsi_driver_register(struct fsi_driver *fsi_drv)
1369{
1370 if (!fsi_drv)
1371 return -EINVAL;
1372 if (!fsi_drv->id_table)
1373 return -EINVAL;
1374
1375 return driver_register(&fsi_drv->drv);
1376}
1377EXPORT_SYMBOL_GPL(fsi_driver_register);
1378
1379void fsi_driver_unregister(struct fsi_driver *fsi_drv)
1380{
1381 driver_unregister(&fsi_drv->drv);
1382}
1383EXPORT_SYMBOL_GPL(fsi_driver_unregister);
1384
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001385struct bus_type fsi_bus_type = {
1386 .name = "fsi",
Jeremy Kerrdd37eed2017-02-01 10:53:43 -06001387 .match = fsi_bus_match,
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001388};
1389EXPORT_SYMBOL_GPL(fsi_bus_type);
1390
Joel Stanley496f8932017-07-11 17:00:39 +09301391static int __init fsi_init(void)
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001392{
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +10001393 int rc;
1394
1395 rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi");
1396 if (rc)
1397 return rc;
1398 rc = bus_register(&fsi_bus_type);
1399 if (rc)
1400 goto fail_bus;
Jeremy Kerr2e32c2d2019-11-08 15:49:35 +10301401
1402 rc = class_register(&fsi_master_class);
1403 if (rc)
1404 goto fail_class;
1405
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +10001406 return 0;
1407
Jeremy Kerr2e32c2d2019-11-08 15:49:35 +10301408 fail_class:
1409 bus_unregister(&fsi_bus_type);
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +10001410 fail_bus:
1411 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
1412 return rc;
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001413}
Joel Stanley496f8932017-07-11 17:00:39 +09301414postcore_initcall(fsi_init);
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001415
1416static void fsi_exit(void)
1417{
Jeremy Kerr2e32c2d2019-11-08 15:49:35 +10301418 class_unregister(&fsi_master_class);
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001419 bus_unregister(&fsi_bus_type);
Benjamin Herrenschmidt0ab5fe52018-06-20 15:22:52 +10001420 unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
1421 ida_destroy(&fsi_minor_ida);
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001422}
Jeremy Kerr0508ad12017-02-01 10:53:41 -06001423module_exit(fsi_exit);
Jeremy Kerr1fa847d2017-06-06 16:08:52 -05001424module_param(discard_errors, int, 0664);
Christopher Bosticacb7e8f2017-06-06 16:08:59 -05001425MODULE_LICENSE("GPL");
Jeremy Kerr1fa847d2017-06-06 16:08:52 -05001426MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");