Lines Matching +full:fsi +full:- +full:slave +full:- +full:engine
1 // SPDX-License-Identifier: GPL-2.0-only
3 * FSI core driver
8 * - Rework topology
9 * - s/chip_id/chip_loc
10 * - s/cfam/chip (cfam_id -> chip_id etc...)
15 #include <linux/fsi.h>
27 #include "fsi-master.h"
28 #include "fsi-slave.h"
31 #include <trace/events/fsi.h>
51 * FSI slave engine control register offsets
55 #define FSI_SSTAT 0x14 /* R : Slave status */
106 * FSI endpoint-device support
111 * dev: Structure passed to FSI client device drivers on probe().
112 * addr: FSI address of given device. Client should pass in its base address
116 * The data in val must be FSI bus endian (big endian).
123 if (addr > dev->size || size > dev->size || addr > dev->size - size) in fsi_device_read()
124 return -EINVAL; in fsi_device_read()
126 return fsi_slave_read(dev->slave, dev->addr + addr, val, size); in fsi_device_read()
133 if (addr > dev->size || size > dev->size || addr > dev->size - size) in fsi_device_write()
134 return -EINVAL; in fsi_device_write()
136 return fsi_slave_write(dev->slave, dev->addr + addr, val, size); in fsi_device_write()
142 uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t)); in fsi_device_peek()
144 return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t)); in fsi_device_peek()
151 of_node_put(device->dev.of_node); in fsi_device_release()
155 static struct fsi_device *fsi_create_device(struct fsi_slave *slave) in fsi_create_device() argument
163 dev->dev.parent = &slave->dev; in fsi_create_device()
164 dev->dev.bus = &fsi_bus_type; in fsi_create_device()
165 dev->dev.release = fsi_device_release; in fsi_create_device()
170 /* FSI slave support */
171 static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp, in fsi_slave_calc_addr() argument
177 if (addr > slave->size) in fsi_slave_calc_addr()
178 return -EINVAL; in fsi_slave_calc_addr()
180 /* For 23 bit addressing, we encode the extra two bits in the slave in fsi_slave_calc_addr()
181 * id (and the slave's actual ID needs to be 0). in fsi_slave_calc_addr()
184 if (slave->id != 0) in fsi_slave_calc_addr()
185 return -EINVAL; in fsi_slave_calc_addr()
195 static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave) in fsi_slave_report_and_clear_errors() argument
197 struct fsi_master *master = slave->master; in fsi_slave_report_and_clear_errors()
202 link = slave->link; in fsi_slave_report_and_clear_errors()
203 id = slave->id; in fsi_slave_report_and_clear_errors()
215 dev_dbg(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n", in fsi_slave_report_and_clear_errors()
223 /* Encode slave local bus echo delay */
229 /* Encode slave local bus send delay */
235 /* Encode slave local bus clock rate ratio */
241 /* Encode slave ID */
251 | fsi_smode_echodly(t_echodly - 1) | fsi_smode_senddly(t_senddly - 1) in fsi_slave_smode()
255 static int fsi_slave_set_smode(struct fsi_slave *slave) in fsi_slave_set_smode() argument
260 /* set our smode register with the slave ID field to 0; this enables in fsi_slave_set_smode()
261 * extended slave addressing in fsi_slave_set_smode()
263 smode = fsi_slave_smode(slave->id, slave->t_send_delay, slave->t_echo_delay); in fsi_slave_set_smode()
266 return fsi_master_write(slave->master, slave->link, slave->id, in fsi_slave_set_smode()
271 static int fsi_slave_handle_error(struct fsi_slave *slave, bool write, in fsi_slave_handle_error() argument
274 struct fsi_master *master = slave->master; in fsi_slave_handle_error()
280 return -1; in fsi_slave_handle_error()
282 link = slave->link; in fsi_slave_handle_error()
283 id = slave->id; in fsi_slave_handle_error()
285 dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]", in fsi_slave_handle_error()
289 * communication with the slave in fsi_slave_handle_error()
291 rc = fsi_slave_report_and_clear_errors(slave); in fsi_slave_handle_error()
296 if (master->term) { in fsi_slave_handle_error()
297 rc = master->term(master, link, id); in fsi_slave_handle_error()
302 rc = fsi_slave_report_and_clear_errors(slave); in fsi_slave_handle_error()
308 send_delay = slave->t_send_delay; in fsi_slave_handle_error()
309 echo_delay = slave->t_echo_delay; in fsi_slave_handle_error()
311 /* getting serious, reset the slave via BREAK */ in fsi_slave_handle_error()
316 slave->t_send_delay = send_delay; in fsi_slave_handle_error()
317 slave->t_echo_delay = echo_delay; in fsi_slave_handle_error()
319 rc = fsi_slave_set_smode(slave); in fsi_slave_handle_error()
323 if (master->link_config) in fsi_slave_handle_error()
324 master->link_config(master, link, in fsi_slave_handle_error()
325 slave->t_send_delay, in fsi_slave_handle_error()
326 slave->t_echo_delay); in fsi_slave_handle_error()
328 return fsi_slave_report_and_clear_errors(slave); in fsi_slave_handle_error()
331 int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, in fsi_slave_read() argument
334 uint8_t id = slave->id; in fsi_slave_read()
337 rc = fsi_slave_calc_addr(slave, &addr, &id); in fsi_slave_read()
342 rc = fsi_master_read(slave->master, slave->link, in fsi_slave_read()
347 err_rc = fsi_slave_handle_error(slave, false, addr, size); in fsi_slave_read()
356 int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, in fsi_slave_write() argument
359 uint8_t id = slave->id; in fsi_slave_write()
362 rc = fsi_slave_calc_addr(slave, &addr, &id); in fsi_slave_write()
367 rc = fsi_master_write(slave->master, slave->link, in fsi_slave_write()
372 err_rc = fsi_slave_handle_error(slave, true, addr, size); in fsi_slave_write()
381 int fsi_slave_claim_range(struct fsi_slave *slave, in fsi_slave_claim_range() argument
385 return -EINVAL; in fsi_slave_claim_range()
387 if (addr + size > slave->size) in fsi_slave_claim_range()
388 return -EINVAL; in fsi_slave_claim_range()
395 void fsi_slave_release_range(struct fsi_slave *slave, in fsi_slave_release_range() argument
421 /* Find a matching node for the slave engine at @address, using @size bytes
429 parent = dev_of_node(&dev->slave->dev); in fsi_device_find_of_node()
434 if (fsi_device_node_matches(&dev->dev, np, in fsi_device_find_of_node()
435 dev->addr, dev->size)) in fsi_device_find_of_node()
442 static int fsi_slave_scan(struct fsi_slave *slave) in fsi_slave_scan() argument
450 * We keep the peek mode and slave engines for the core; so start in fsi_slave_scan()
461 rc = fsi_slave_read(slave, (i + 1) * sizeof(data), in fsi_slave_scan()
464 dev_warn(&slave->dev, in fsi_slave_scan()
465 "error reading slave registers\n"); in fsi_slave_scan()
466 return -1; in fsi_slave_scan()
472 dev_warn(&slave->dev, in fsi_slave_scan()
473 "crc error in slave register at 0x%04x\n", in fsi_slave_scan()
475 return -1; in fsi_slave_scan()
492 dev = fsi_create_device(slave); in fsi_slave_scan()
494 return -ENOMEM; in fsi_slave_scan()
496 dev->slave = slave; in fsi_slave_scan()
497 dev->engine_type = type; in fsi_slave_scan()
498 dev->version = version; in fsi_slave_scan()
499 dev->unit = i; in fsi_slave_scan()
500 dev->addr = engine_addr; in fsi_slave_scan()
501 dev->size = slots * engine_page_size; in fsi_slave_scan()
505 dev_dbg(&slave->dev, in fsi_slave_scan()
506 "engine[%i]: type %x, version %x, addr %x size %x\n", in fsi_slave_scan()
507 dev->unit, dev->engine_type, version, in fsi_slave_scan()
508 dev->addr, dev->size); in fsi_slave_scan()
510 dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x", in fsi_slave_scan()
511 slave->master->idx, slave->link, in fsi_slave_scan()
512 slave->id, i - 2); in fsi_slave_scan()
513 dev->dev.of_node = fsi_device_find_of_node(dev); in fsi_slave_scan()
515 rc = device_register(&dev->dev); in fsi_slave_scan()
517 dev_warn(&slave->dev, "add failed: %d\n", rc); in fsi_slave_scan()
518 put_device(&dev->dev); in fsi_slave_scan()
538 * width or the highest power-of-two factor of offset in aligned_access_size()
550 count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count)); in aligned_access_size()
560 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); in fsi_slave_sysfs_raw_read() local
565 return -EINVAL; in fsi_slave_sysfs_raw_read()
568 return -EINVAL; in fsi_slave_sysfs_raw_read()
571 read_len = aligned_access_size(off, count - total_len); in fsi_slave_sysfs_raw_read()
573 rc = fsi_slave_read(slave, off, buf + total_len, read_len); in fsi_slave_sysfs_raw_read()
587 struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); in fsi_slave_sysfs_raw_write() local
592 return -EINVAL; in fsi_slave_sysfs_raw_write()
595 return -EINVAL; in fsi_slave_sysfs_raw_write()
598 write_len = aligned_access_size(off, count - total_len); in fsi_slave_sysfs_raw_write()
600 rc = fsi_slave_write(slave, off, buf + total_len, write_len); in fsi_slave_sysfs_raw_write()
622 struct fsi_slave *slave = to_fsi_slave(dev); in fsi_slave_release() local
624 fsi_free_minor(slave->dev.devt); in fsi_slave_release()
625 of_node_put(dev->of_node); in fsi_slave_release()
626 kfree(slave); in fsi_slave_release()
640 /* Find a matching node for the slave at (link, id). Returns NULL if none
648 parent = dev_of_node(&master->dev); in fsi_slave_find_of_node()
663 struct fsi_slave *slave = filep->private_data; in cfam_read() local
669 return -EINVAL; in cfam_read()
672 return -EINVAL; in cfam_read()
678 read_len -= off & 0x3; in cfam_read()
680 rc = fsi_slave_read(slave, off, &data, read_len); in cfam_read()
685 rc = -EFAULT; in cfam_read()
699 struct fsi_slave *slave = filep->private_data; in cfam_write() local
706 return -EINVAL; in cfam_write()
709 return -EINVAL; in cfam_write()
715 write_len -= off & 0x3; in cfam_write()
719 rc = -EFAULT; in cfam_write()
722 rc = fsi_slave_write(slave, off, &data, write_len); in cfam_write()
739 file->f_pos = offset; in cfam_llseek()
742 return -EINVAL; in cfam_llseek()
750 struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev); in cfam_open() local
752 file->private_data = slave; in cfam_open()
769 struct fsi_slave *slave = to_fsi_slave(dev); in send_term_store() local
770 struct fsi_master *master = slave->master; in send_term_store()
772 if (!master->term) in send_term_store()
773 return -ENODEV; in send_term_store()
775 master->term(master, slave->link, slave->id); in send_term_store()
785 struct fsi_slave *slave = to_fsi_slave(dev); in slave_send_echo_show() local
787 return sprintf(buf, "%u\n", slave->t_send_delay); in slave_send_echo_show()
793 struct fsi_slave *slave = to_fsi_slave(dev); in slave_send_echo_store() local
794 struct fsi_master *master = slave->master; in slave_send_echo_store()
799 return -EINVAL; in slave_send_echo_store()
802 return -EINVAL; in slave_send_echo_store()
804 if (!master->link_config) in slave_send_echo_store()
805 return -ENXIO; in slave_send_echo_store()
808 slave->t_send_delay = val; in slave_send_echo_store()
809 slave->t_echo_delay = val; in slave_send_echo_store()
811 rc = fsi_slave_set_smode(slave); in slave_send_echo_store()
814 if (master->link_config) in slave_send_echo_store()
815 master->link_config(master, slave->link, in slave_send_echo_store()
816 slave->t_send_delay, in slave_send_echo_store()
817 slave->t_echo_delay); in slave_send_echo_store()
829 struct fsi_slave *slave = to_fsi_slave(dev); in chip_id_show() local
831 return sprintf(buf, "%d\n", slave->chip_id); in chip_id_show()
840 struct fsi_slave *slave = to_fsi_slave(dev); in cfam_id_show() local
842 return sprintf(buf, "0x%x\n", slave->cfam_id); in cfam_id_show()
867 const struct fsi_slave *slave = to_fsi_slave(dev); in cfam_devnode() local
870 return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx); in cfam_devnode()
872 return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx); in cfam_devnode()
886 return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev)); in fsi_cdev_devnode()
893 .name = "fsi-cdev",
908 static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type, in __fsi_get_new_minor() argument
911 int cid = slave->chip_id; in __fsi_get_new_minor()
917 * Try reserving the legacy number, which has 0 - 0x3f reserved in __fsi_get_new_minor()
929 if (id != -ENOSPC) in __fsi_get_new_minor()
931 /* Fallback to non-legacy allocation */ in __fsi_get_new_minor()
934 FSI_CHAR_MAX_DEVICES - 1, GFP_KERNEL); in __fsi_get_new_minor()
952 if (fdev->dev.of_node) { in fsi_get_new_minor()
953 int aid = of_alias_get_id(fdev->dev.of_node, fsi_dev_type_names[type]); in fsi_get_new_minor()
966 if (id != -ENOSPC) in fsi_get_new_minor()
971 return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index); in fsi_get_new_minor()
984 struct fsi_slave *slave; in fsi_slave_init() local
990 * full 23-bit address range in fsi_slave_init()
993 return -EINVAL; in fsi_slave_init()
997 dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n", in fsi_slave_init()
999 return -ENODEV; in fsi_slave_init()
1006 dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n", in fsi_slave_init()
1008 return -EIO; in fsi_slave_init()
1011 dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n", in fsi_slave_init()
1012 cfam_id, master->idx, link, id); in fsi_slave_init()
1014 /* If we're behind a master that doesn't provide a self-running bus in fsi_slave_init()
1015 * clock, put the slave into async mode in fsi_slave_init()
1017 if (master->flags & FSI_MASTER_FLAG_SWCLOCK) { in fsi_slave_init()
1023 dev_warn(&master->dev, in fsi_slave_init()
1024 "can't set llmode on slave:%02x:%02x %d\n", in fsi_slave_init()
1028 /* We can communicate with a slave; create the slave device and in fsi_slave_init()
1031 slave = kzalloc(sizeof(*slave), GFP_KERNEL); in fsi_slave_init()
1032 if (!slave) in fsi_slave_init()
1033 return -ENOMEM; in fsi_slave_init()
1035 dev_set_name(&slave->dev, "slave@%02x:%02x", link, id); in fsi_slave_init()
1036 slave->dev.type = &cfam_type; in fsi_slave_init()
1037 slave->dev.parent = &master->dev; in fsi_slave_init()
1038 slave->dev.of_node = fsi_slave_find_of_node(master, link, id); in fsi_slave_init()
1039 slave->dev.release = fsi_slave_release; in fsi_slave_init()
1040 device_initialize(&slave->dev); in fsi_slave_init()
1041 slave->cfam_id = cfam_id; in fsi_slave_init()
1042 slave->master = master; in fsi_slave_init()
1043 slave->link = link; in fsi_slave_init()
1044 slave->id = id; in fsi_slave_init()
1045 slave->size = FSI_SLAVE_SIZE_23b; in fsi_slave_init()
1046 slave->t_send_delay = 16; in fsi_slave_init()
1047 slave->t_echo_delay = 16; in fsi_slave_init()
1050 slave->chip_id = -1; in fsi_slave_init()
1051 if (slave->dev.of_node) { in fsi_slave_init()
1053 if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop)) in fsi_slave_init()
1054 slave->chip_id = prop; in fsi_slave_init()
1062 dev_warn(&master->dev, in fsi_slave_init()
1063 "can't set slbus on slave:%02x:%02x %d\n", link, id, in fsi_slave_init()
1066 rc = fsi_slave_set_smode(slave); in fsi_slave_init()
1068 dev_warn(&master->dev, in fsi_slave_init()
1069 "can't set smode on slave:%02x:%02x %d\n", in fsi_slave_init()
1074 /* Allocate a minor in the FSI space */ in fsi_slave_init()
1075 rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt, in fsi_slave_init()
1076 &slave->cdev_idx); in fsi_slave_init()
1080 trace_fsi_slave_init(slave); in fsi_slave_init()
1083 cdev_init(&slave->cdev, &cfam_fops); in fsi_slave_init()
1084 rc = cdev_device_add(&slave->cdev, &slave->dev); in fsi_slave_init()
1086 dev_err(&slave->dev, "Error %d creating slave device\n", rc); in fsi_slave_init()
1095 if (master->link_config) in fsi_slave_init()
1096 master->link_config(master, link, in fsi_slave_init()
1097 slave->t_send_delay, in fsi_slave_init()
1098 slave->t_echo_delay); in fsi_slave_init()
1100 /* Legacy raw file -> to be removed */ in fsi_slave_init()
1101 rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr); in fsi_slave_init()
1103 dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc); in fsi_slave_init()
1106 rc = fsi_slave_scan(slave); in fsi_slave_init()
1108 dev_dbg(&master->dev, "failed during slave scan with: %d\n", in fsi_slave_init()
1114 fsi_free_minor(slave->dev.devt); in fsi_slave_init()
1116 of_node_put(slave->dev.of_node); in fsi_slave_init()
1117 kfree(slave); in fsi_slave_init()
1121 /* FSI master support */
1126 return -EINVAL; in fsi_check_access()
1129 return -EINVAL; in fsi_check_access()
1131 return -EINVAL; in fsi_check_access()
1145 rc = master->read(master, link, slave_id, addr, val, size); in fsi_master_read()
1162 rc = master->write(master, link, slave_id, addr, val, size); in fsi_master_write()
1172 if (master->link_enable) in fsi_master_link_disable()
1173 return master->link_enable(master, link, false); in fsi_master_link_disable()
1180 if (master->link_enable) in fsi_master_link_enable()
1181 return master->link_enable(master, link, true); in fsi_master_link_enable()
1195 if (master->send_break) in fsi_master_break()
1196 rc = master->send_break(master, link); in fsi_master_break()
1197 if (master->link_config) in fsi_master_break()
1198 master->link_config(master, link, 16, 16); in fsi_master_break()
1208 for (link = 0; link < master->n_links; link++) { in fsi_master_scan()
1211 dev_dbg(&master->dev, in fsi_master_scan()
1218 dev_dbg(&master->dev, in fsi_master_scan()
1239 struct fsi_slave *slave = to_fsi_slave(dev); in fsi_master_remove_slave() local
1242 cdev_device_del(&slave->cdev, &slave->dev); in fsi_master_remove_slave()
1250 device_for_each_child(&master->dev, NULL, fsi_master_remove_slave); in fsi_master_unscan()
1257 mutex_lock(&master->scan_lock); in fsi_master_rescan()
1260 mutex_unlock(&master->scan_lock); in fsi_master_rescan()
1302 .name = "fsi-master",
1311 mutex_init(&master->scan_lock); in fsi_master_register()
1313 /* Alloc the requested index if it's non-zero */ in fsi_master_register()
1314 if (master->idx) { in fsi_master_register()
1315 master->idx = ida_alloc_range(&master_ida, master->idx, in fsi_master_register()
1316 master->idx, GFP_KERNEL); in fsi_master_register()
1318 master->idx = ida_alloc(&master_ida, GFP_KERNEL); in fsi_master_register()
1321 if (master->idx < 0) in fsi_master_register()
1322 return master->idx; in fsi_master_register()
1324 if (!dev_name(&master->dev)) in fsi_master_register()
1325 dev_set_name(&master->dev, "fsi%d", master->idx); in fsi_master_register()
1327 master->dev.class = &fsi_master_class; in fsi_master_register()
1329 mutex_lock(&master->scan_lock); in fsi_master_register()
1330 rc = device_register(&master->dev); in fsi_master_register()
1332 ida_free(&master_ida, master->idx); in fsi_master_register()
1336 np = dev_of_node(&master->dev); in fsi_master_register()
1337 if (!of_property_read_bool(np, "no-scan-on-init")) { in fsi_master_register()
1341 mutex_unlock(&master->scan_lock); in fsi_master_register()
1348 int idx = master->idx; in fsi_master_unregister()
1352 mutex_lock(&master->scan_lock); in fsi_master_unregister()
1354 master->n_links = 0; in fsi_master_unregister()
1355 mutex_unlock(&master->scan_lock); in fsi_master_unregister()
1357 device_unregister(&master->dev); in fsi_master_unregister()
1362 /* FSI core & Linux bus type definitions */
1370 if (!fsi_drv->id_table) in fsi_bus_match()
1373 for (id = fsi_drv->id_table; id->engine_type; id++) { in fsi_bus_match()
1374 if (id->engine_type != fsi_dev->engine_type) in fsi_bus_match()
1376 if (id->version == FSI_VERSION_ANY || in fsi_bus_match()
1377 id->version == fsi_dev->version) { in fsi_bus_match()
1378 if (drv->of_match_table) { in fsi_bus_match()
1393 return -EINVAL; in fsi_driver_register()
1394 if (!fsi_drv->id_table) in fsi_driver_register()
1395 return -EINVAL; in fsi_driver_register()
1397 return driver_register(&fsi_drv->drv); in fsi_driver_register()
1403 driver_unregister(&fsi_drv->drv); in fsi_driver_unregister()
1408 .name = "fsi",
1417 rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi"); in fsi_init()
1447 MODULE_DESCRIPTION("FSI core driver");