dwc3: debug: Add logging APIs using IPC logging framework

This change creates separate debug buffer with each DWC instance and
provides debug API to log different important events using IPC logging
framework.

CRs-Fixed: 1040809
Change-Id: I9aee21a99f5bcd0e82e81092c05bc2238863f5e4
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 1b152a3..400c0e1 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -50,6 +50,9 @@
 
 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY	5000 /* ms */
 
+static int count;
+static struct dwc3 *dwc3_instance[DWC_CTRL_COUNT];
+
 void dwc3_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
 {
 	u32			reg;
@@ -1125,6 +1128,13 @@
 	void __iomem		*regs;
 	void			*mem;
 
+	if (count >= DWC_CTRL_COUNT) {
+		dev_err(dev, "Err dwc instance %d >= %d available\n",
+				count, DWC_CTRL_COUNT);
+		ret = -EINVAL;
+		return ret;
+	}
+
 	mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
 	if (!mem)
 		return -ENOMEM;
@@ -1336,6 +1346,15 @@
 		goto err_core_init;
 	}
 
+	dwc->dwc_ipc_log_ctxt = ipc_log_context_create(NUM_LOG_PAGES,
+					dev_name(dwc->dev), 0);
+	if (!dwc->dwc_ipc_log_ctxt)
+		dev_err(dwc->dev, "Error getting ipc_log_ctxt\n");
+
+	dwc3_instance[count] = dwc;
+	dwc->index = count;
+	count++;
+
 	pm_runtime_allow(dev);
 	return 0;
 
@@ -1378,6 +1397,11 @@
 	dwc3_free_event_buffers(dwc);
 	dwc3_free_scratch_buffers(dwc);
 
+	ipc_log_context_destroy(dwc->dwc_ipc_log_ctxt);
+	dwc->dwc_ipc_log_ctxt = NULL;
+	count--;
+	dwc3_instance[dwc->index] = NULL;
+
 	return 0;
 }