2 * Greybus interface code
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
11 #include "greybus_trace.h"
13 #define GB_INTERFACE_MODE_SWITCH_TIMEOUT 1000
15 #define GB_INTERFACE_DEVICE_ID_BAD 0xff
17 /* Don't-care selector index */
18 #define DME_SELECTOR_INDEX_NULL 0
21 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
22 #define DME_T_TST_SRC_INCREMENT 0x4083
24 #define DME_DDBL1_MANUFACTURERID 0x5003
25 #define DME_DDBL1_PRODUCTID 0x5004
27 #define DME_TOSHIBA_ARA_VID 0x6000
28 #define DME_TOSHIBA_ARA_PID 0x6001
29 #define DME_TOSHIBA_ARA_SN0 0x6002
30 #define DME_TOSHIBA_ARA_SN1 0x6003
31 #define DME_TOSHIBA_ARA_INIT_STATUS 0x6101
33 /* DDBL1 Manufacturer and Product ids */
34 #define TOSHIBA_DMID 0x0126
35 #define TOSHIBA_ES2_BRIDGE_DPID 0x1000
36 #define TOSHIBA_ES3_APBRIDGE_DPID 0x1001
37 #define TOSHIBA_ES3_GBPHY_DPID 0x1002
40 static int gb_interface_dme_attr_get(struct gb_interface *intf,
43 return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
44 attr, DME_SELECTOR_INDEX_NULL, val);
47 static int gb_interface_read_ara_dme(struct gb_interface *intf)
53 * Unless this is a Toshiba bridge, bail out until we have defined
54 * standard Ara attributes.
56 if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
57 dev_err(&intf->dev, "unknown manufacturer %08x\n",
58 intf->ddbl1_manufacturer_id);
62 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_VID,
67 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_PID,
72 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN0, &sn0);
76 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN1, &sn1);
80 intf->serial_number = (u64)sn1 << 32 | sn0;
85 static int gb_interface_read_dme(struct gb_interface *intf)
89 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
90 &intf->ddbl1_manufacturer_id);
94 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
95 &intf->ddbl1_product_id);
99 if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
100 intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
101 intf->quirks |= GB_INTERFACE_QUIRK_NO_ARA_IDS;
102 intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
105 return gb_interface_read_ara_dme(intf);
108 static int gb_interface_route_create(struct gb_interface *intf)
110 struct gb_svc *svc = intf->hd->svc;
111 u8 intf_id = intf->interface_id;
115 /* Allocate an interface device id. */
116 ret = ida_simple_get(&svc->device_id_map,
117 GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1,
120 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
125 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
127 dev_err(&intf->dev, "failed to set device id %u: %d\n",
132 /* FIXME: Hard-coded AP device id. */
133 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
136 dev_err(&intf->dev, "failed to create route: %d\n", ret);
137 goto err_svc_id_free;
140 intf->device_id = device_id;
146 * XXX Should we tell SVC that this id doesn't belong to interface
150 ida_simple_remove(&svc->device_id_map, device_id);
155 static void gb_interface_route_destroy(struct gb_interface *intf)
157 struct gb_svc *svc = intf->hd->svc;
159 if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
162 gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
163 ida_simple_remove(&svc->device_id_map, intf->device_id);
164 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
167 /* Locking: Caller holds the interface mutex. */
168 static int gb_interface_legacy_mode_switch(struct gb_interface *intf)
172 dev_info(&intf->dev, "legacy mode switch detected\n");
174 /* Mark as disconnected to prevent I/O during disable. */
175 intf->disconnected = true;
176 gb_interface_disable(intf);
177 intf->disconnected = false;
179 ret = gb_interface_enable(intf);
181 dev_err(&intf->dev, "failed to re-enable interface: %d\n", ret);
182 gb_interface_deactivate(intf);
188 void gb_interface_mailbox_event(struct gb_interface *intf, u16 result,
191 mutex_lock(&intf->mutex);
195 "mailbox event with UniPro error: 0x%04x\n",
200 if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) {
202 "mailbox event with unexpected value: 0x%08x\n",
207 if (intf->quirks & GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH) {
208 gb_interface_legacy_mode_switch(intf);
212 if (!intf->mode_switch) {
213 dev_warn(&intf->dev, "unexpected mailbox event: 0x%08x\n",
218 dev_info(&intf->dev, "mode switch detected\n");
220 complete(&intf->mode_switch_completion);
223 mutex_unlock(&intf->mutex);
228 gb_interface_disable(intf);
229 gb_interface_deactivate(intf);
230 mutex_unlock(&intf->mutex);
233 static void gb_interface_mode_switch_work(struct work_struct *work)
235 struct gb_interface *intf;
236 struct gb_control *control;
237 unsigned long timeout;
240 intf = container_of(work, struct gb_interface, mode_switch_work);
242 mutex_lock(&intf->mutex);
243 /* Make sure interface is still enabled. */
244 if (!intf->enabled) {
245 dev_dbg(&intf->dev, "mode switch aborted\n");
246 intf->mode_switch = false;
247 mutex_unlock(&intf->mutex);
248 goto out_interface_put;
252 * Prepare the control device for mode switch and make sure to get an
253 * extra reference before it goes away during interface disable.
255 control = gb_control_get(intf->control);
256 gb_control_mode_switch_prepare(control);
257 gb_interface_disable(intf);
258 mutex_unlock(&intf->mutex);
260 timeout = msecs_to_jiffies(GB_INTERFACE_MODE_SWITCH_TIMEOUT);
261 ret = wait_for_completion_interruptible_timeout(
262 &intf->mode_switch_completion, timeout);
264 /* Finalise control-connection mode switch. */
265 gb_control_mode_switch_complete(control);
266 gb_control_put(control);
269 dev_err(&intf->dev, "mode switch interrupted\n");
271 } else if (ret == 0) {
272 dev_err(&intf->dev, "mode switch timed out\n");
276 /* Re-enable (re-enumerate) interface if still active. */
277 mutex_lock(&intf->mutex);
278 intf->mode_switch = false;
280 ret = gb_interface_enable(intf);
282 dev_err(&intf->dev, "failed to re-enable interface: %d\n",
284 gb_interface_deactivate(intf);
287 mutex_unlock(&intf->mutex);
290 gb_interface_put(intf);
295 mutex_lock(&intf->mutex);
296 intf->mode_switch = false;
297 gb_interface_deactivate(intf);
298 mutex_unlock(&intf->mutex);
300 gb_interface_put(intf);
303 int gb_interface_request_mode_switch(struct gb_interface *intf)
307 mutex_lock(&intf->mutex);
308 if (intf->mode_switch) {
313 intf->mode_switch = true;
314 reinit_completion(&intf->mode_switch_completion);
317 * Get a reference to the interface device, which will be put once the
318 * mode switch is complete.
320 get_device(&intf->dev);
322 if (!queue_work(system_long_wq, &intf->mode_switch_work)) {
323 put_device(&intf->dev);
329 mutex_unlock(&intf->mutex);
333 EXPORT_SYMBOL_GPL(gb_interface_request_mode_switch);
336 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
337 * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
338 * clear it after reading a non-zero value from it.
340 * FIXME: This is module-hardware dependent and needs to be extended for every
341 * type of module we want to support.
343 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
345 struct gb_host_device *hd = intf->hd;
346 unsigned long bootrom_quirks;
353 * ES2 bridges use T_TstSrcIncrement for the init status.
355 * FIXME: Remove ES2 support
357 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
358 attr = DME_T_TST_SRC_INCREMENT;
360 attr = DME_TOSHIBA_ARA_INIT_STATUS;
362 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
363 DME_SELECTOR_INDEX_NULL, &value);
368 * A nonzero init status indicates the module has finished
372 dev_err(&intf->dev, "invalid init status\n");
377 * Extract the init status.
379 * For ES2: We need to check lowest 8 bits of 'value'.
380 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
382 * FIXME: Remove ES2 support
384 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
385 init_status = value & 0xff;
387 init_status = value >> 24;
390 * Check if the interface is executing the quirky ES3 bootrom that,
391 * for example, requires E2EFC, CSD and CSV to be disabled.
393 bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES |
394 GB_INTERFACE_QUIRK_FORCED_DISABLE |
395 GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH;
396 switch (init_status) {
397 case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
398 case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
399 intf->quirks |= bootrom_quirks;
402 intf->quirks &= ~bootrom_quirks;
405 /* Clear the init status. */
406 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
407 DME_SELECTOR_INDEX_NULL, 0);
410 /* interface sysfs attributes */
411 #define gb_interface_attr(field, type) \
412 static ssize_t field##_show(struct device *dev, \
413 struct device_attribute *attr, \
416 struct gb_interface *intf = to_gb_interface(dev); \
417 return scnprintf(buf, PAGE_SIZE, type"\n", intf->field); \
419 static DEVICE_ATTR_RO(field)
421 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
422 gb_interface_attr(ddbl1_product_id, "0x%08x");
423 gb_interface_attr(interface_id, "%u");
424 gb_interface_attr(vendor_id, "0x%08x");
425 gb_interface_attr(product_id, "0x%08x");
426 gb_interface_attr(serial_number, "0x%016llx");
428 static ssize_t voltage_now_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct gb_interface *intf = to_gb_interface(dev);
435 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
436 GB_SVC_PWRMON_TYPE_VOL,
439 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
443 return sprintf(buf, "%u\n", measurement);
445 static DEVICE_ATTR_RO(voltage_now);
447 static ssize_t current_now_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
450 struct gb_interface *intf = to_gb_interface(dev);
454 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
455 GB_SVC_PWRMON_TYPE_CURR,
458 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
462 return sprintf(buf, "%u\n", measurement);
464 static DEVICE_ATTR_RO(current_now);
466 static ssize_t power_now_show(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 struct gb_interface *intf = to_gb_interface(dev);
473 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
474 GB_SVC_PWRMON_TYPE_PWR,
477 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
481 return sprintf(buf, "%u\n", measurement);
483 static DEVICE_ATTR_RO(power_now);
485 static struct attribute *interface_attrs[] = {
486 &dev_attr_ddbl1_manufacturer_id.attr,
487 &dev_attr_ddbl1_product_id.attr,
488 &dev_attr_interface_id.attr,
489 &dev_attr_vendor_id.attr,
490 &dev_attr_product_id.attr,
491 &dev_attr_serial_number.attr,
492 &dev_attr_voltage_now.attr,
493 &dev_attr_current_now.attr,
494 &dev_attr_power_now.attr,
497 ATTRIBUTE_GROUPS(interface);
499 static void gb_interface_release(struct device *dev)
501 struct gb_interface *intf = to_gb_interface(dev);
503 trace_gb_interface_release(intf);
508 struct device_type greybus_interface_type = {
509 .name = "greybus_interface",
510 .release = gb_interface_release,
514 * A Greybus module represents a user-replaceable component on an Ara
515 * phone. An interface is the physical connection on that module. A
516 * module may have more than one interface.
518 * Create a gb_interface structure to represent a discovered interface.
519 * The position of interface within the Endo is encoded in "interface_id"
522 * Returns a pointer to the new interfce or a null pointer if a
523 * failure occurs due to memory exhaustion.
525 struct gb_interface *gb_interface_create(struct gb_module *module,
528 struct gb_host_device *hd = module->hd;
529 struct gb_interface *intf;
531 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
535 intf->hd = hd; /* XXX refcount? */
536 intf->module = module;
537 intf->interface_id = interface_id;
538 INIT_LIST_HEAD(&intf->bundles);
539 INIT_LIST_HEAD(&intf->manifest_descs);
540 mutex_init(&intf->mutex);
541 INIT_WORK(&intf->mode_switch_work, gb_interface_mode_switch_work);
542 init_completion(&intf->mode_switch_completion);
544 /* Invalid device id to start with */
545 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
547 intf->dev.parent = &module->dev;
548 intf->dev.bus = &greybus_bus_type;
549 intf->dev.type = &greybus_interface_type;
550 intf->dev.groups = interface_groups;
551 intf->dev.dma_mask = module->dev.dma_mask;
552 device_initialize(&intf->dev);
553 dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev),
556 trace_gb_interface_create(intf);
561 static int gb_interface_vsys_set(struct gb_interface *intf, bool enable)
563 struct gb_svc *svc = intf->hd->svc;
566 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
568 ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable);
570 dev_err(&intf->dev, "failed to set v_sys: %d\n", ret);
577 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable)
579 struct gb_svc *svc = intf->hd->svc;
582 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
584 ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable);
586 dev_err(&intf->dev, "failed to set refclk: %d\n", ret);
593 static int gb_interface_unipro_set(struct gb_interface *intf, bool enable)
595 struct gb_svc *svc = intf->hd->svc;
598 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
600 ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable);
602 dev_err(&intf->dev, "failed to set UniPro: %d\n", ret);
609 static int gb_interface_activate_operation(struct gb_interface *intf)
611 struct gb_svc *svc = intf->hd->svc;
615 dev_dbg(&intf->dev, "%s\n", __func__);
617 ret = gb_svc_intf_activate(svc, intf->interface_id, &type);
619 dev_err(&intf->dev, "failed to activate: %d\n", ret);
624 case GB_SVC_INTF_TYPE_DUMMY:
625 dev_info(&intf->dev, "dummy interface detected\n");
626 /* FIXME: handle as an error for now */
628 case GB_SVC_INTF_TYPE_UNIPRO:
629 dev_err(&intf->dev, "interface type UniPro not supported\n");
630 /* FIXME: check if this is a Toshiba bridge before retrying? */
632 case GB_SVC_INTF_TYPE_GREYBUS:
635 dev_err(&intf->dev, "unknown interface type: %u\n", type);
642 static int gb_interface_hibernate_link(struct gb_interface *intf)
644 dev_dbg(&intf->dev, "%s\n", __func__);
646 /* FIXME: implement */
652 * Activate an interface.
654 * Locking: Caller holds the interface mutex.
656 int gb_interface_activate(struct gb_interface *intf)
663 ret = gb_interface_vsys_set(intf, true);
667 ret = gb_interface_refclk_set(intf, true);
669 goto err_vsys_disable;
671 ret = gb_interface_unipro_set(intf, true);
673 goto err_refclk_disable;
675 ret = gb_interface_activate_operation(intf);
677 goto err_unipro_disable;
679 ret = gb_interface_read_dme(intf);
681 goto err_hibernate_link;
683 ret = gb_interface_route_create(intf);
685 goto err_hibernate_link;
689 trace_gb_interface_activate(intf);
694 gb_interface_hibernate_link(intf);
696 gb_interface_unipro_set(intf, false);
698 gb_interface_refclk_set(intf, false);
700 gb_interface_vsys_set(intf, false);
706 * Deactivate an interface.
708 * Locking: Caller holds the interface mutex.
710 void gb_interface_deactivate(struct gb_interface *intf)
715 trace_gb_interface_deactivate(intf);
717 /* Abort any ongoing mode switch. */
718 if (intf->mode_switch)
719 complete(&intf->mode_switch_completion);
721 gb_interface_route_destroy(intf);
722 gb_interface_hibernate_link(intf);
723 gb_interface_unipro_set(intf, false);
724 gb_interface_refclk_set(intf, false);
725 gb_interface_vsys_set(intf, false);
727 intf->active = false;
731 * Enable an interface by enabling its control connection, fetching the
732 * manifest and other information over it, and finally registering its child
735 * Locking: Caller holds the interface mutex.
737 int gb_interface_enable(struct gb_interface *intf)
739 struct gb_control *control;
740 struct gb_bundle *bundle, *tmp;
744 ret = gb_interface_read_and_clear_init_status(intf);
746 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
750 /* Establish control connection */
751 control = gb_control_create(intf);
752 if (IS_ERR(control)) {
753 dev_err(&intf->dev, "failed to create control device: %ld\n",
755 return PTR_ERR(control);
757 intf->control = control;
759 ret = gb_control_enable(intf->control);
761 goto err_put_control;
763 /* Get manifest size using control protocol on CPort */
764 size = gb_control_get_manifest_size_operation(intf);
766 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
773 goto err_disable_control;
776 manifest = kmalloc(size, GFP_KERNEL);
779 goto err_disable_control;
782 /* Get manifest using control protocol on CPort */
783 ret = gb_control_get_manifest_operation(intf, manifest, size);
785 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
786 goto err_free_manifest;
790 * Parse the manifest and build up our data structures representing
793 if (!gb_manifest_parse(intf, manifest, size)) {
794 dev_err(&intf->dev, "failed to parse manifest\n");
796 goto err_destroy_bundles;
799 ret = gb_control_get_bundle_versions(intf->control);
801 goto err_destroy_bundles;
803 /* Register the control device and any bundles */
804 ret = gb_control_add(intf->control);
806 goto err_destroy_bundles;
808 list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
809 ret = gb_bundle_add(bundle);
811 gb_bundle_destroy(bundle);
818 intf->enabled = true;
820 trace_gb_interface_enable(intf);
825 list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
826 gb_bundle_destroy(bundle);
830 gb_control_disable(intf->control);
832 gb_control_put(intf->control);
833 intf->control = NULL;
839 * Disable an interface and destroy its bundles.
841 * Locking: Caller holds the interface mutex.
843 void gb_interface_disable(struct gb_interface *intf)
845 struct gb_bundle *bundle;
846 struct gb_bundle *next;
851 trace_gb_interface_disable(intf);
853 /* Set disconnected flag to avoid I/O during connection tear down. */
854 if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE)
855 intf->disconnected = true;
857 list_for_each_entry_safe(bundle, next, &intf->bundles, links)
858 gb_bundle_destroy(bundle);
860 gb_control_del(intf->control);
861 gb_control_disable(intf->control);
862 gb_control_put(intf->control);
863 intf->control = NULL;
865 intf->enabled = false;
868 /* Register an interface. */
869 int gb_interface_add(struct gb_interface *intf)
873 ret = device_add(&intf->dev);
875 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
879 trace_gb_interface_add(intf);
881 dev_info(&intf->dev, "Interface added: VID=0x%08x, PID=0x%08x\n",
882 intf->vendor_id, intf->product_id);
883 dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
884 intf->ddbl1_manufacturer_id, intf->ddbl1_product_id);
889 /* Deregister an interface. */
890 void gb_interface_del(struct gb_interface *intf)
892 if (device_is_registered(&intf->dev)) {
893 trace_gb_interface_del(intf);
895 device_del(&intf->dev);
896 dev_info(&intf->dev, "Interface removed\n");
900 void gb_interface_put(struct gb_interface *intf)
902 put_device(&intf->dev);