xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/newhost/drvEHCI_MEM.cxx (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 
79 #include "include/drvPorts.h"
80 #include "drvEHCI.h"
81 
82 static __inline__ void ms_ehci_qtd_init (struct ehci_qtd *pQtd, dma_addr_t tDmaAddr);
83 
84 /*-------------------------------------------------------------------------*/
ms_ehci_qtd_alloc(struct ehci_hcd * pEhci,int iFlags)85 static struct ehci_qtd *ms_ehci_qtd_alloc (struct ehci_hcd *pEhci, int iFlags)
86 {
87 	struct ehci_qtd    *pQtd;
88 	dma_addr_t    tDmaAddr;
89 
90 	pQtd = (struct ehci_qtd*) ms_mem_pool_alloc (pEhci->pQtdPool, iFlags, &tDmaAddr);
91 	if (pQtd != 0)
92 	{
93 		ms_ehci_qtd_init (pQtd, tDmaAddr);
94 	}
95 
96 	return pQtd;
97 }
98 
ms_ehci_qtd_init(struct ehci_qtd * pQtd,dma_addr_t tDmaAddr)99 static __inline__ void ms_ehci_qtd_init (struct ehci_qtd *pQtd, dma_addr_t tDmaAddr)
100 {
101 	memset (pQtd, 0, sizeof *pQtd);
102 	pQtd->qtd_dma_addr = tDmaAddr;
103 	pQtd->hw_token = QTD_STS_HALT;
104 	pQtd->hw_next_qtd = EHCI_LIST_END;
105 	pQtd->hw_alt_next_qtd = EHCI_LIST_END;
106 	ms_list_init (&pQtd->qtd_list);
107 }
108 
ms_ehci_qtd_free(struct ehci_hcd * pEhci,struct ehci_qtd * pQtd)109 static __inline__ void ms_ehci_qtd_free (struct ehci_hcd *pEhci, struct ehci_qtd *pQtd)
110 {
111 	ms_mem_pool_free (pEhci->pQtdPool, pQtd, pQtd->qtd_dma_addr);
112 }
113 
114 
ms_ehci_qh_alloc(struct ehci_hcd * pEhci,int iFlags)115 static struct ehci_qh *ms_ehci_qh_alloc (struct ehci_hcd *pEhci, int iFlags)
116 {
117 	struct ehci_qh    *pQh;
118 	dma_addr_t    tDmaAddr;
119 
120 	pQh = (struct ehci_qh *)ms_mem_pool_alloc (pEhci->pQhPool, iFlags, &tDmaAddr);
121 	if (!pQh)
122 		return pQh;
123 
124 	memset (pQh, 0, sizeof *pQh);
125 	osapi_atomic_set (&pQh->tRefCnt, 1);
126 	pQh->qh_dma_addr = tDmaAddr;
127 	// INIT_LIST_HEAD (&pQh->qh_list);
128 	diag_printf("pQh: %p, pQh->qh_dma_addr: %p\n", (void *)pQh, (void *)pQh->qh_dma_addr);
129 	ms_list_init (&pQh->qtd_list);
130 
131 	pQh->pDummyQtd = ms_ehci_qtd_alloc (pEhci, iFlags);
132 	if (pQh->pDummyQtd == 0)
133 	{
134 		diag_printf ("no dummy td\n");
135 		ms_mem_pool_free (pEhci->pQhPool, pQh, pQh->qh_dma_addr);
136         // TODO: free qH?
137 		pQh = 0;
138 	}
139 
140 	return pQh;
141 }
142 
ms_qh_get(struct ehci_qh * pQh)143 static __inline__ struct ehci_qh *ms_qh_get (struct ehci_qh *pQh)
144 {
145 	osapi_atomic_inc (&pQh->tRefCnt);
146 	return pQh;
147 }
148 
ms_qh_put(struct ehci_hcd * pEhci,struct ehci_qh * pQh)149 static void ms_qh_put (struct ehci_hcd *pEhci, struct ehci_qh *pQh)
150 {
151 	if (!osapi_atomic_dec_and_test (&pQh->tRefCnt))
152 		return;
153 
154 	if (!ms_is_empty_list (&pQh->qtd_list) || pQh->qh_next.ptr)
155 	{
156 		USB_ASSERT (0, "unused qh not empty!\n");
157 	}
158 
159 	if (pQh->pDummyQtd)
160 		ms_ehci_qtd_free (pEhci, pQh->pDummyQtd);
161 	ms_mem_pool_free (pEhci->pQhPool, pQh, pQh->qh_dma_addr);
162 }
163 
164 /*-------------------------------------------------------------------------*/
165 
ms_ehci_mem_cleanup(struct ehci_hcd * pEhci)166 static void ms_ehci_mem_cleanup (struct ehci_hcd *pEhci)
167 {
168 	if (pEhci->stAsync)
169 		ms_qh_put (pEhci, pEhci->stAsync);
170 	pEhci->stAsync = 0;
171 
172 	/* PCI consistent memory and pools */
173 	if (pEhci->pQtdPool)
174 		ms_mem_pool_destroy (pEhci->pQtdPool);
175 	pEhci->pQtdPool = 0;
176 
177 	if (pEhci->pQhPool)
178 	{
179 		ms_mem_pool_destroy (pEhci->pQhPool);
180 		pEhci->pQhPool = 0;
181 	}
182 
183 	if (pEhci->pPeriodic)
184 	{
185 		pEhci->u32PeriodicSize = DEFAULT_I_TDPS; // to match with allocated size
186 		ms_debug_debug("Free periodic frame list array %p, size %d\n",pEhci->pPeriodic, pEhci->u32PeriodicSize * sizeof (void *));
187 		ms_hcd_buffer_free(&pEhci->hcd.self, pEhci->u32PeriodicSize * sizeof (void *), pEhci->pPeriodic, pEhci->tPeriodicDma);
188 	}
189 	pEhci->pPeriodic = 0;
190 	pEhci->tPeriodicDma = 0;
191 
192 	/* shadow periodic table */
193 	if (pEhci->pshadow)
194 		kfree (pEhci->pshadow);
195 	pEhci->pshadow = 0;
196 }
197 
ms_ehci_mem_init(struct ehci_hcd * pEhci,int iFlags)198 static int ms_ehci_mem_init (struct ehci_hcd *pEhci, int iFlags)
199 {
200 	U32 i;
201 
202 	ms_debug_func("Creat Page memory pool for qtd\n");
203 	pEhci->pQtdPool = ms_mem_pool_create ("ehci_qtd", sizeof (struct ehci_qtd),
204 #if (_USB_128_ALIGMENT)
205 		128 /* byte alignment (for hw parts) */,
206 #else
207 		32 /* byte alignment (for hw parts) */,
208 #endif
209 		4096 /* can't cross 4K */);
210 	if (!pEhci->pQtdPool)
211 	{
212 		goto fail;
213 	}
214 
215 	ms_debug_func("Creat Page memory pool for qh\n");
216 	pEhci->pQhPool = ms_mem_pool_create ("ehci_qh", sizeof (struct ehci_qh),
217 #if (_USB_128_ALIGMENT)
218 		128 /* byte alignment (for hw parts) */,
219 #else
220 		32 /* byte alignment (for hw parts) */,
221 #endif
222 		4096 /* can't cross 4K */);
223 	if (!pEhci->pQhPool)
224 	{
225 		goto fail;
226 	}
227 	pEhci->stAsync = ms_ehci_qh_alloc (pEhci, iFlags);
228 	if (!pEhci->stAsync)
229 	{
230 		goto fail;
231 	}
232 
233 	/* Hardware periodic table */
234 	pEhci->pPeriodic = (U32*) ms_hcd_buffer_alloc(&pEhci->hcd.self,pEhci->u32PeriodicSize * sizeof (void *),0,&pEhci->tPeriodicDma);
235 	//ehci->periodic_dma = (dma_addr_t) ehci->periodic;	//Note_HC_Test
236 	ms_debug_msg("Allocate a non-cacheable memory pool for periodic frame list array %p, size %d\n",pEhci->pPeriodic, pEhci->u32PeriodicSize * sizeof (void *));
237 	if (pEhci->pPeriodic == 0)
238 	{
239 		goto fail;
240 	}
241 	for (i = 0; i < pEhci->u32PeriodicSize; i++)
242 		pEhci->pPeriodic [i] = EHCI_LIST_END;
243 
244 	pEhci->pshadow = (union ehci_qh_shadow  *) kmalloc (pEhci->u32PeriodicSize * sizeof (void *), iFlags);
245 	if (pEhci->pshadow == 0)
246 	{
247 		goto fail;
248 	}
249 	memset (pEhci->pshadow, 0, pEhci->u32PeriodicSize * sizeof (void *));
250 	return 0;
251 
252 fail:
253 	ms_debug_err("couldn't init memory\n");
254 	ms_ehci_mem_cleanup (pEhci);
255 	return -ENOMEM;
256 }
257 
258