From patchwork Wed Mar 5 15:49:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Balbi X-Patchwork-Id: 25767 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f69.google.com (mail-yh0-f69.google.com [209.85.213.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 497F7203C3 for ; Wed, 5 Mar 2014 15:51:13 +0000 (UTC) Received: by mail-yh0-f69.google.com with SMTP id a41sf2822069yho.8 for ; Wed, 05 Mar 2014 07:51:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:date:from:to:cc:subject:message-id :reply-to:references:mime-version:content-type:content-disposition :in-reply-to:user-agent:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=Dcrqd5FZjbY1SPkwa+i4M1r6CDVaFpD39ar1vD3ozZg=; b=m3ODSTE0Yu4YuLAsILtehe0aqRxtIaYPm9Cx3usu1HKlidZu23EUIWzJm9qe0vW1up MlNo4vcNN5ODZrTiFfPP1FR1VA2sFrs/wZ9c25qmvmTzU+yEZ2JpWHTYf4T+D78bgtp+ ygZpdrG/gWoJ7YrPFNJS+r/cvm0nthosql3n4I9JPwOOK9iqeChf9gZOkdqL/TfR0gw0 6ryleaJxDN2yKq7ulke+hxYuBD49PNK9/aSER5wjpO8JNuKNm6wIlN8AqvpaIEcmgf1o uxbX0FH4qp3Vj1f4A+h7YTAHIccwxkpmn8n0iot3T+lwxuQ+8U8EroMFfD5uUmaTDQ3T Y0JQ== X-Gm-Message-State: ALoCoQnZuMtiiWwNnWyOZIUpQm7N+I46qyw83wsRrtFPevfLxc5AbU3OR0cXOqzKer3hUL1+IfMw X-Received: by 10.236.134.174 with SMTP id s34mr2444570yhi.32.1394034673010; Wed, 05 Mar 2014 07:51:13 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.20.166 with SMTP id 35ls367403qgj.20.gmail; Wed, 05 Mar 2014 07:51:12 -0800 (PST) X-Received: by 10.52.104.68 with SMTP id gc4mr4137791vdb.2.1394034672782; Wed, 05 Mar 2014 07:51:12 -0800 (PST) Received: from mail-ve0-f178.google.com (mail-ve0-f178.google.com [209.85.128.178]) by mx.google.com with ESMTPS id cp10si122616ved.134.2014.03.05.07.51.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 05 Mar 2014 07:51:12 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.178; Received: by mail-ve0-f178.google.com with SMTP id jw12so1205139veb.23 for ; Wed, 05 Mar 2014 07:51:12 -0800 (PST) X-Received: by 10.220.147.16 with SMTP id j16mr594808vcv.28.1394034672696; Wed, 05 Mar 2014 07:51:12 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.78.9 with SMTP id i9csp28908vck; Wed, 5 Mar 2014 07:51:11 -0800 (PST) X-Received: by 10.68.229.164 with SMTP id sr4mr7835932pbc.82.1394034671096; Wed, 05 Mar 2014 07:51:11 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id tg2si2768983pbc.143.2014.03.05.07.51.09; Wed, 05 Mar 2014 07:51:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753538AbaCEPvH (ORCPT + 3 others); Wed, 5 Mar 2014 10:51:07 -0500 Received: from devils.ext.ti.com ([198.47.26.153]:56888 "EHLO devils.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752215AbaCEPvG (ORCPT ); Wed, 5 Mar 2014 10:51:06 -0500 Received: from dflxv15.itg.ti.com ([128.247.5.124]) by devils.ext.ti.com (8.13.7/8.13.7) with ESMTP id s25Fp50G012097 for ; Wed, 5 Mar 2014 09:51:05 -0600 Received: from DFLE72.ent.ti.com (dfle72.ent.ti.com [128.247.5.109]) by dflxv15.itg.ti.com (8.14.3/8.13.8) with ESMTP id s25Fp5Eo000940 for ; Wed, 5 Mar 2014 09:51:05 -0600 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE72.ent.ti.com (128.247.5.109) with Microsoft SMTP Server id 14.3.174.1; Wed, 5 Mar 2014 09:51:04 -0600 Received: from localhost (ileax41-snat.itg.ti.com [10.172.224.153]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id s25Fp4tK001583; Wed, 5 Mar 2014 09:51:04 -0600 Date: Wed, 5 Mar 2014 09:49:32 -0600 From: Felipe Balbi To: George Cherian CC: Felipe Balbi , Linux USB Mailing List Subject: Re: [PATCH 06/11] usb: dwc3: core: allocate scratch buffers Message-ID: <20140305154932.GH16430@saruman.home> Reply-To: References: <1393357243-2958-1-git-send-email-balbi@ti.com> <1393357243-2958-7-git-send-email-balbi@ti.com> <531707CB.4090703@ti.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <531707CB.4090703@ti.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-usb-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: balbi@ti.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , On Wed, Mar 05, 2014 at 04:47:31PM +0530, George Cherian wrote: > On 2/26/2014 1:10 AM, Felipe Balbi wrote: > >We must read HWPARAMS4 register to figure out > >how many scratch buffers we should allocate. > > > >Later patch will use "Set Scratchpad Buffer > >Array" command to pass the pointer to the > >IP so it can be used during hibernation. > > > >Signed-off-by: Felipe Balbi > >--- > > drivers/usb/dwc3/core.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ > > drivers/usb/dwc3/core.h | 6 ++++ > > 2 files changed, 101 insertions(+) > > > >diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c > >index a49217a..2864aad 100644 > >--- a/drivers/usb/dwc3/core.c > >+++ b/drivers/usb/dwc3/core.c > >@@ -242,6 +242,80 @@ static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) > > } > > } > >+static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc) > >+{ > >+ if (!dwc->has_hibernation) > >+ return 0; > >+ > >+ if (!dwc->nr_scratch) > >+ return 0; > >+ > >+ dwc->scratchbuf = kmalloc_array(dwc->nr_scratch, > >+ DWC3_SCRATCHBUF_SIZE, GFP_KERNEL); > >+ if (!dwc->scratchbuf) > >+ return -ENOMEM; > >+ > >+ return 0; > >+} > >+ > >+static int dwc3_setup_scratch_buffers(struct dwc3 *dwc) > >+{ > >+ dma_addr_t scratch_addr; > >+ u32 param; > >+ int ret; > >+ > > Shouldn't there be check for dwc->scratchbuf and dwc->nr_scratch > before calling dma_map_single? fixed, thanks: commit ae53c955b61a6270b65f384665e5e856109a9c9f Author: Felipe Balbi Date: Thu Dec 19 13:04:28 2013 -0600 usb: dwc3: core: allocate scratch buffers We must read HWPARAMS4 register to figure out how many scratch buffers we should allocate. Later patch will use "Set Scratchpad Buffer Array" command to pass the pointer to the IP so it can be used during hibernation. Signed-off-by: Felipe Balbi diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 785feee..e3f8fd5 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -242,6 +242,90 @@ static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) } } +static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc) +{ + if (!dwc->has_hibernation) + return 0; + + if (!dwc->nr_scratch) + return 0; + + dwc->scratchbuf = kmalloc_array(dwc->nr_scratch, + DWC3_SCRATCHBUF_SIZE, GFP_KERNEL); + if (!dwc->scratchbuf) + return -ENOMEM; + + return 0; +} + +static int dwc3_setup_scratch_buffers(struct dwc3 *dwc) +{ + dma_addr_t scratch_addr; + u32 param; + int ret; + + if (!dwc->has_hibernation) + return 0; + + if (!dwc->nr_scratch) + return 0; + + /* should never fall here */ + if (!WARN_ON(dwc->scratchbuf)) + return; + + scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf, + dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE, + DMA_BIDIRECTIONAL); + if (dma_mapping_error(dwc->dev, scratch_addr)) { + dev_err(dwc->dev, "failed to map scratch buffer\n"); + ret = -EFAULT; + goto err0; + } + + dwc->scratch_addr = scratch_addr; + + param = lower_32_bits(scratch_addr); + + ret = dwc3_send_gadget_generic_command(dwc, + DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param); + if (ret < 0) + goto err1; + + param = upper_32_bits(scratch_addr); + + ret = dwc3_send_gadget_generic_command(dwc, + DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param); + if (ret < 0) + goto err1; + + return 0; + +err1: + dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch * + DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); + +err0: + return ret; +} + +static void dwc3_free_scratch_buffers(struct dwc3 *dwc) +{ + if (!dwc->has_hibernation) + return; + + if (!dwc->nr_scratch) + return; + + /* should never fall here */ + if (!WARN_ON(dwc->scratchbuf)) + return; + + dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch * + DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); + kfree(dwc->scratchbuf); +} + static void dwc3_core_num_eps(struct dwc3 *dwc) { struct dwc3_hwparams *parms = &dwc->hwparams; @@ -277,6 +361,7 @@ static void dwc3_cache_hwparams(struct dwc3 *dwc) static int dwc3_core_init(struct dwc3 *dwc) { unsigned long timeout; + u32 hwparams4 = dwc->hwparams.hwparams4; u32 reg; int ret; @@ -334,6 +419,10 @@ static int dwc3_core_init(struct dwc3 *dwc) else reg &= ~DWC3_GCTL_DSBLCLKGTNG; break; + case DWC3_GHWPARAMS1_EN_PWROPT_HIB: + /* enable hibernation here */ + dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4); + break; default: dev_dbg(dwc->dev, "No power optimization available\n"); } @@ -351,14 +440,30 @@ static int dwc3_core_init(struct dwc3 *dwc) dwc3_writel(dwc->regs, DWC3_GCTL, reg); + ret = dwc3_alloc_scratch_buffers(dwc); + if (ret) + goto err1; + + ret = dwc3_setup_scratch_buffers(dwc); + if (ret) + goto err2; + return 0; +err2: + dwc3_free_scratch_buffers(dwc); + +err1: + usb_phy_shutdown(dwc->usb2_phy); + usb_phy_shutdown(dwc->usb3_phy); + err0: return ret; } static void dwc3_core_exit(struct dwc3 *dwc) { + dwc3_free_scratch_buffers(dwc); usb_phy_shutdown(dwc->usb2_phy); usb_phy_shutdown(dwc->usb3_phy); } diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 4c14796..bbb5b78e 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -36,6 +36,7 @@ #define DWC3_ENDPOINTS_NUM 32 #define DWC3_XHCI_RESOURCES_NUM 2 +#define DWC3_SCRATCHBUF_SIZE 4096 /* each buffer is assumed to be 4KiB */ #define DWC3_EVENT_SIZE 4 /* bytes */ #define DWC3_EVENT_MAX_NUM 64 /* 2 events/endpoint */ #define DWC3_EVENT_BUFFERS_SIZE (DWC3_EVENT_SIZE * DWC3_EVENT_MAX_NUM) @@ -601,6 +602,7 @@ struct dwc3_scratchpad_array { * @ep0_trb: dma address of ep0_trb * @ep0_usb_req: dummy req used while handling STD USB requests * @ep0_bounce_addr: dma address of ep0_bounce + * @scratch_addr: dma address of scratchbuf * @lock: for synchronizing * @dev: pointer to our struct device * @xhci: pointer to our xHCI child @@ -609,6 +611,7 @@ struct dwc3_scratchpad_array { * @gadget_driver: pointer to the gadget driver * @regs: base address for our registers * @regs_size: address space size + * @nr_scratch: number of scratch buffers * @num_event_buffers: calculated number of event buffers * @u1u2: only used on revisions <1.83a for workaround * @maximum_speed: maximum speed requested (mainly for testing purposes) @@ -651,10 +654,12 @@ struct dwc3 { struct usb_ctrlrequest *ctrl_req; struct dwc3_trb *ep0_trb; void *ep0_bounce; + void *scratchbuf; u8 *setup_buf; dma_addr_t ctrl_req_addr; dma_addr_t ep0_trb_addr; dma_addr_t ep0_bounce_addr; + dma_addr_t scratch_addr; struct dwc3_request ep0_usb_req; /* device lock */ @@ -683,6 +688,7 @@ struct dwc3 { u32 dcfg; u32 gctl; + u32 nr_scratch; u32 num_event_buffers; u32 u1u2; u32 maximum_speed;