2 * Greybus interface code
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
12 #include "greybus_trace.h"
14 #define GB_INTERFACE_DEVICE_ID_BAD 0xff
16 /* Don't-care selector index */
17 #define DME_SELECTOR_INDEX_NULL 0
20 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
21 #define DME_T_TST_SRC_INCREMENT 0x4083
23 #define DME_DDBL1_MANUFACTURERID 0x5003
24 #define DME_DDBL1_PRODUCTID 0x5004
26 #define DME_TOSHIBA_ARA_VID 0x6000
27 #define DME_TOSHIBA_ARA_PID 0x6001
28 #define DME_TOSHIBA_ARA_SN0 0x6002
29 #define DME_TOSHIBA_ARA_SN1 0x6003
30 #define DME_TOSHIBA_ARA_INIT_STATUS 0x6101
32 /* DDBL1 Manufacturer and Product ids */
33 #define TOSHIBA_DMID 0x0126
34 #define TOSHIBA_ES2_BRIDGE_DPID 0x1000
35 #define TOSHIBA_ES3_APBRIDGE_DPID 0x1001
36 #define TOSHIBA_ES3_GBPHY_DPID 0x1002
39 static int gb_interface_dme_attr_get(struct gb_interface *intf,
42 return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
43 attr, DME_SELECTOR_INDEX_NULL, val);
46 static int gb_interface_read_ara_dme(struct gb_interface *intf)
52 * Unless this is a Toshiba bridge, bail out until we have defined
53 * standard Ara attributes.
55 if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
56 dev_err(&intf->dev, "unknown manufacturer %08x\n",
57 intf->ddbl1_manufacturer_id);
61 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_VID,
66 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_PID,
71 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN0, &sn0);
75 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN1, &sn1);
79 intf->serial_number = (u64)sn1 << 32 | sn0;
84 static int gb_interface_read_dme(struct gb_interface *intf)
88 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
89 &intf->ddbl1_manufacturer_id);
93 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
94 &intf->ddbl1_product_id);
98 if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
99 intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
100 intf->quirks |= GB_INTERFACE_QUIRK_NO_ARA_IDS;
101 intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
104 return gb_interface_read_ara_dme(intf);
107 static int gb_interface_route_create(struct gb_interface *intf)
109 struct gb_svc *svc = intf->hd->svc;
110 u8 intf_id = intf->interface_id;
114 /* Allocate an interface device id. */
115 ret = ida_simple_get(&svc->device_id_map,
116 GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1,
119 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
124 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
126 dev_err(&intf->dev, "failed to set device id %u: %d\n",
131 /* FIXME: Hard-coded AP device id. */
132 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
135 dev_err(&intf->dev, "failed to create route: %d\n", ret);
136 goto err_svc_id_free;
139 intf->device_id = device_id;
145 * XXX Should we tell SVC that this id doesn't belong to interface
149 ida_simple_remove(&svc->device_id_map, device_id);
154 static void gb_interface_route_destroy(struct gb_interface *intf)
156 struct gb_svc *svc = intf->hd->svc;
158 if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
161 gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
162 ida_simple_remove(&svc->device_id_map, intf->device_id);
163 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
167 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
168 * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
169 * clear it after reading a non-zero value from it.
171 * FIXME: This is module-hardware dependent and needs to be extended for every
172 * type of module we want to support.
174 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
176 struct gb_host_device *hd = intf->hd;
177 unsigned long bootrom_quirks;
184 * ES2 bridges use T_TstSrcIncrement for the init status.
186 * FIXME: Remove ES2 support
188 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
189 attr = DME_T_TST_SRC_INCREMENT;
191 attr = DME_TOSHIBA_ARA_INIT_STATUS;
193 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
194 DME_SELECTOR_INDEX_NULL, &value);
199 * A nonzero init status indicates the module has finished
203 dev_err(&intf->dev, "invalid init status\n");
208 * Extract the init status.
210 * For ES2: We need to check lowest 8 bits of 'value'.
211 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
213 * FIXME: Remove ES2 support
215 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
216 init_status = value & 0xff;
218 init_status = value >> 24;
221 * Check if the interface is executing the quirky ES3 bootrom that,
222 * for example, requires E2EFC, CSD and CSV to be disabled.
224 bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES |
225 GB_INTERFACE_QUIRK_FORCED_DISABLE;
226 switch (init_status) {
227 case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
228 case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
229 intf->quirks |= bootrom_quirks;
232 intf->quirks &= ~bootrom_quirks;
235 /* Clear the init status. */
236 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
237 DME_SELECTOR_INDEX_NULL, 0);
240 /* interface sysfs attributes */
241 #define gb_interface_attr(field, type) \
242 static ssize_t field##_show(struct device *dev, \
243 struct device_attribute *attr, \
246 struct gb_interface *intf = to_gb_interface(dev); \
247 return scnprintf(buf, PAGE_SIZE, type"\n", intf->field); \
249 static DEVICE_ATTR_RO(field)
251 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
252 gb_interface_attr(ddbl1_product_id, "0x%08x");
253 gb_interface_attr(interface_id, "%u");
254 gb_interface_attr(vendor_id, "0x%08x");
255 gb_interface_attr(product_id, "0x%08x");
256 gb_interface_attr(serial_number, "0x%016llx");
258 static ssize_t voltage_now_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct gb_interface *intf = to_gb_interface(dev);
265 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
266 GB_SVC_PWRMON_TYPE_VOL,
269 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
273 return sprintf(buf, "%u\n", measurement);
275 static DEVICE_ATTR_RO(voltage_now);
277 static ssize_t current_now_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct gb_interface *intf = to_gb_interface(dev);
284 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
285 GB_SVC_PWRMON_TYPE_CURR,
288 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
292 return sprintf(buf, "%u\n", measurement);
294 static DEVICE_ATTR_RO(current_now);
296 static ssize_t power_now_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
299 struct gb_interface *intf = to_gb_interface(dev);
303 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
304 GB_SVC_PWRMON_TYPE_PWR,
307 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
311 return sprintf(buf, "%u\n", measurement);
313 static DEVICE_ATTR_RO(power_now);
315 static struct attribute *interface_attrs[] = {
316 &dev_attr_ddbl1_manufacturer_id.attr,
317 &dev_attr_ddbl1_product_id.attr,
318 &dev_attr_interface_id.attr,
319 &dev_attr_vendor_id.attr,
320 &dev_attr_product_id.attr,
321 &dev_attr_serial_number.attr,
322 &dev_attr_voltage_now.attr,
323 &dev_attr_current_now.attr,
324 &dev_attr_power_now.attr,
327 ATTRIBUTE_GROUPS(interface);
329 static void gb_interface_release(struct device *dev)
331 struct gb_interface *intf = to_gb_interface(dev);
333 trace_gb_interface_release(intf);
338 struct device_type greybus_interface_type = {
339 .name = "greybus_interface",
340 .release = gb_interface_release,
344 * A Greybus module represents a user-replaceable component on an Ara
345 * phone. An interface is the physical connection on that module. A
346 * module may have more than one interface.
348 * Create a gb_interface structure to represent a discovered interface.
349 * The position of interface within the Endo is encoded in "interface_id"
352 * Returns a pointer to the new interfce or a null pointer if a
353 * failure occurs due to memory exhaustion.
355 struct gb_interface *gb_interface_create(struct gb_module *module,
358 struct gb_host_device *hd = module->hd;
359 struct gb_interface *intf;
361 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
365 intf->hd = hd; /* XXX refcount? */
366 intf->module = module;
367 intf->interface_id = interface_id;
368 INIT_LIST_HEAD(&intf->bundles);
369 INIT_LIST_HEAD(&intf->manifest_descs);
370 mutex_init(&intf->mutex);
372 /* Invalid device id to start with */
373 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
375 intf->dev.parent = &module->dev;
376 intf->dev.bus = &greybus_bus_type;
377 intf->dev.type = &greybus_interface_type;
378 intf->dev.groups = interface_groups;
379 intf->dev.dma_mask = module->dev.dma_mask;
380 device_initialize(&intf->dev);
381 dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev),
384 trace_gb_interface_create(intf);
389 static int gb_interface_vsys_set(struct gb_interface *intf, bool enable)
391 struct gb_svc *svc = intf->hd->svc;
394 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
396 ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable);
398 dev_err(&intf->dev, "failed to set v_sys: %d\n", ret);
405 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable)
407 struct gb_svc *svc = intf->hd->svc;
410 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
412 ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable);
414 dev_err(&intf->dev, "failed to set refclk: %d\n", ret);
421 static int gb_interface_unipro_set(struct gb_interface *intf, bool enable)
423 struct gb_svc *svc = intf->hd->svc;
426 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
428 ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable);
430 dev_err(&intf->dev, "failed to set UniPro: %d\n", ret);
437 static int gb_interface_activate_operation(struct gb_interface *intf)
439 struct gb_svc *svc = intf->hd->svc;
443 dev_dbg(&intf->dev, "%s\n", __func__);
445 ret = gb_svc_intf_activate(svc, intf->interface_id, &type);
447 dev_err(&intf->dev, "failed to activate: %d\n", ret);
452 case GB_SVC_INTF_TYPE_DUMMY:
453 dev_info(&intf->dev, "dummy interface detected\n");
454 /* FIXME: handle as an error for now */
456 case GB_SVC_INTF_TYPE_UNIPRO:
457 dev_err(&intf->dev, "interface type UniPro not supported\n");
458 /* FIXME: check if this is a Toshiba bridge before retrying? */
460 case GB_SVC_INTF_TYPE_GREYBUS:
463 dev_err(&intf->dev, "unknown interface type: %u\n", type);
470 static int gb_interface_hibernate_link(struct gb_interface *intf)
472 dev_dbg(&intf->dev, "%s\n", __func__);
474 /* FIXME: implement */
480 * Activate an interface.
482 * Locking: Caller holds the interface mutex.
484 int gb_interface_activate(struct gb_interface *intf)
491 ret = gb_interface_vsys_set(intf, true);
495 ret = gb_interface_refclk_set(intf, true);
497 goto err_vsys_disable;
499 ret = gb_interface_unipro_set(intf, true);
501 goto err_refclk_disable;
503 ret = gb_interface_activate_operation(intf);
505 goto err_unipro_disable;
507 ret = gb_interface_read_dme(intf);
509 goto err_hibernate_link;
511 ret = gb_interface_route_create(intf);
513 goto err_hibernate_link;
517 trace_gb_interface_activate(intf);
522 gb_interface_hibernate_link(intf);
524 gb_interface_unipro_set(intf, false);
526 gb_interface_refclk_set(intf, false);
528 gb_interface_vsys_set(intf, false);
534 * Deactivate an interface.
536 * Locking: Caller holds the interface mutex.
538 void gb_interface_deactivate(struct gb_interface *intf)
543 trace_gb_interface_deactivate(intf);
545 gb_interface_route_destroy(intf);
546 gb_interface_hibernate_link(intf);
547 gb_interface_unipro_set(intf, false);
548 gb_interface_refclk_set(intf, false);
549 gb_interface_vsys_set(intf, false);
551 intf->active = false;
555 * Enable an interface by enabling its control connection, fetching the
556 * manifest and other information over it, and finally registering its child
559 * Locking: Caller holds the interface mutex.
561 int gb_interface_enable(struct gb_interface *intf)
563 struct gb_control *control;
564 struct gb_bundle *bundle, *tmp;
568 ret = gb_interface_read_and_clear_init_status(intf);
570 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
574 /* Establish control connection */
575 control = gb_control_create(intf);
576 if (IS_ERR(control)) {
577 dev_err(&intf->dev, "failed to create control device: %ld\n",
579 return PTR_ERR(control);
581 intf->control = control;
583 ret = gb_control_enable(intf->control);
585 goto err_put_control;
587 /* Get manifest size using control protocol on CPort */
588 size = gb_control_get_manifest_size_operation(intf);
590 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
597 goto err_disable_control;
600 manifest = kmalloc(size, GFP_KERNEL);
603 goto err_disable_control;
606 /* Get manifest using control protocol on CPort */
607 ret = gb_control_get_manifest_operation(intf, manifest, size);
609 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
610 goto err_free_manifest;
614 * Parse the manifest and build up our data structures representing
617 if (!gb_manifest_parse(intf, manifest, size)) {
618 dev_err(&intf->dev, "failed to parse manifest\n");
620 goto err_destroy_bundles;
623 ret = gb_control_get_bundle_versions(intf->control);
625 goto err_destroy_bundles;
627 /* Register the control device and any bundles */
628 ret = gb_control_add(intf->control);
630 goto err_destroy_bundles;
632 list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
633 ret = gb_bundle_add(bundle);
635 gb_bundle_destroy(bundle);
642 intf->enabled = true;
644 trace_gb_interface_enable(intf);
649 list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
650 gb_bundle_destroy(bundle);
654 gb_control_disable(intf->control);
656 gb_control_put(intf->control);
657 intf->control = NULL;
663 * Disable an interface and destroy its bundles.
665 * Locking: Caller holds the interface mutex.
667 void gb_interface_disable(struct gb_interface *intf)
669 struct gb_bundle *bundle;
670 struct gb_bundle *next;
675 trace_gb_interface_disable(intf);
677 /* Set disconnected flag to avoid I/O during connection tear down. */
678 if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE)
679 intf->disconnected = true;
681 list_for_each_entry_safe(bundle, next, &intf->bundles, links)
682 gb_bundle_destroy(bundle);
684 gb_control_del(intf->control);
685 gb_control_disable(intf->control);
686 gb_control_put(intf->control);
687 intf->control = NULL;
689 intf->enabled = false;
692 /* Register an interface. */
693 int gb_interface_add(struct gb_interface *intf)
697 ret = device_add(&intf->dev);
699 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
703 trace_gb_interface_add(intf);
705 dev_info(&intf->dev, "Interface added: VID=0x%08x, PID=0x%08x\n",
706 intf->vendor_id, intf->product_id);
707 dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
708 intf->ddbl1_manufacturer_id, intf->ddbl1_product_id);
713 /* Deregister an interface. */
714 void gb_interface_del(struct gb_interface *intf)
716 if (device_is_registered(&intf->dev)) {
717 trace_gb_interface_del(intf);
719 device_del(&intf->dev);
720 dev_info(&intf->dev, "Interface removed\n");
724 void gb_interface_put(struct gb_interface *intf)
726 put_device(&intf->dev);