lantiq: Tune the XWAY subtarget cflags
[openwrt.git] / package / platform / lantiq / ltq-hcd / src / ifxhcd_queue.c
1 /*****************************************************************************
2  **   FILE NAME       : ifxhcd_queue.c
3  **   PROJECT         : IFX USB sub-system V3
4  **   MODULES         : IFX USB sub-system Host and Device driver
5  **   SRC VERSION     : 3.2
6  **   DATE            : 1/Jan/2011
7  **   AUTHOR          : Chen, Howard
8  **   DESCRIPTION     : This file contains the functions to manage Queue Heads and Queue
9  **                     Transfer Descriptors.
10  **   FUNCTIONS       :
11  **   COMPILER        : gcc
12  **   REFERENCE       : Synopsys DWC-OTG Driver 2.7
13  **   COPYRIGHT       :  Copyright (c) 2010
14  **                      LANTIQ DEUTSCHLAND GMBH,
15  **                      Am Campeon 3, 85579 Neubiberg, Germany
16  **
17  **    This program is free software; you can redistribute it and/or modify
18  **    it under the terms of the GNU General Public License as published by
19  **    the Free Software Foundation; either version 2 of the License, or
20  **    (at your option) any later version.
21  **
22  **  Version Control Section  **
23  **   $Author$
24  **   $Date$
25  **   $Revisions$
26  **   $Log$       Revision history
27  *****************************************************************************/
28
29 /*
30  * This file contains code fragments from Synopsys HS OTG Linux Software Driver.
31  * For this code the following notice is applicable:
32  *
33  * ==========================================================================
34  *
35  * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
36  * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
37  * otherwise expressly agreed to in writing between Synopsys and you.
38  *
39  * The Software IS NOT an item of Licensed Software or Licensed Product under
40  * any End User Software License Agreement or Agreement for Licensed Product
41  * with Synopsys or any supplement thereto. You are permitted to use and
42  * redistribute this Software in source and binary forms, with or without
43  * modification, provided that redistributions of source code must retain this
44  * notice. You may not view, use, disclose, copy or distribute this file or
45  * any information contained herein except pursuant to this license grant from
46  * Synopsys. If you do not agree with this notice, including the disclaimer
47  * below, then you are not authorized to use the Software.
48  *
49  * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
53  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
59  * DAMAGE.
60  * ========================================================================== */
61
62 /*!
63  \file ifxhcd_queue.c
64  \ingroup IFXUSB_DRIVER_V3
65   \brief This file contains the functions to manage Queue Heads and Queue
66   Transfer Descriptors.
67 */
68 #include <linux/version.h>
69 #include "ifxusb_version.h"
70
71 #include <linux/kernel.h>
72 #include <linux/module.h>
73 #include <linux/moduleparam.h>
74 #include <linux/init.h>
75 #include <linux/device.h>
76 #include <linux/errno.h>
77 #include <linux/list.h>
78 #include <linux/interrupt.h>
79 #include <linux/string.h>
80
81 #include "ifxusb_plat.h"
82 #include "ifxusb_regs.h"
83 #include "ifxusb_cif.h"
84 #include "ifxhcd.h"
85
86 #ifdef __EPQD_DESTROY_TIMEOUT__
87         #define epqh_self_destroy_timeout 300
88         static void eqph_destroy_func(unsigned long _ptr)
89         {
90                 ifxhcd_epqh_t *epqh=(ifxhcd_epqh_t *)_ptr;
91                 if(epqh)
92                 {
93                         if(epqh->sysep)
94                         {
95                                 epqh->sysep->hcpriv=NULL;
96                         }
97                         ifxhcd_epqh_free (epqh);
98                 }
99         }
100 #endif
101
102 /*!
103   \brief This function allocates and initializes a EPQH.
104
105   \param _ifxhcd The HCD state structure for the USB Host controller.
106   \param[in] _urb Holds the information about the device/endpoint that we need
107   to initialize the EPQH.
108
109   \return Returns pointer to the newly allocated EPQH, or NULL on error.
110  */
111 static ifxhcd_epqh_t *ifxhcd_epqh_create (ifxhcd_hcd_t *_ifxhcd, struct urb *_urb)
112 {
113         ifxhcd_epqh_t *epqh;
114
115         hprt0_data_t   hprt0;
116         struct usb_host_endpoint *sysep = ifxhcd_urb_to_endpoint(_urb);
117
118         /* Allocate memory */
119 //      epqh=(ifxhcd_epqh_t *) kmalloc (sizeof(ifxhcd_epqh_t), GFP_KERNEL);
120         epqh=(ifxhcd_epqh_t *) kmalloc (sizeof(ifxhcd_epqh_t), GFP_ATOMIC);
121
122         if(epqh == NULL)
123                 return NULL;
124
125         memset (epqh, 0, sizeof (ifxhcd_epqh_t));
126
127         epqh->sysep=sysep;
128
129         epqh->devno=_urb->dev->devnum;
130
131         epqh->ifxhcd=_ifxhcd;
132         epqh->phase=EPQH_IDLE;
133
134         /* Initialize EPQH */
135         switch (usb_pipetype(_urb->pipe))
136         {
137                 case PIPE_CONTROL    : epqh->ep_type = IFXUSB_EP_TYPE_CTRL; break;
138                 case PIPE_BULK       : epqh->ep_type = IFXUSB_EP_TYPE_BULK; break;
139                 case PIPE_ISOCHRONOUS: epqh->ep_type = IFXUSB_EP_TYPE_ISOC; break;
140                 case PIPE_INTERRUPT  : epqh->ep_type = IFXUSB_EP_TYPE_INTR; break;
141         }
142
143         usb_settoggle(_urb->dev, usb_pipeendpoint (_urb->pipe), !usb_pipein(_urb->pipe), IFXUSB_HC_PID_DATA0);
144         epqh->mps = usb_maxpacket(_urb->dev, _urb->pipe, !(usb_pipein(_urb->pipe)));
145
146         INIT_LIST_HEAD(&epqh->urbd_list);
147         #ifdef __STRICT_ORDER__
148                 INIT_LIST_HEAD(&epqh->release_list);
149         #endif
150         INIT_LIST_HEAD(&epqh->ql);
151         INIT_LIST_HEAD(&epqh->ql_all);
152         INIT_URBD_LIST(epqh);
153
154         epqh->hc = NULL;
155
156         /* FS/LS Enpoint on HS Hub
157          * NOT virtual root hub */
158         epqh->need_split = 0;
159         hprt0.d32 = ifxusb_read_hprt0 (&_ifxhcd->core_if);
160         if (hprt0.b.prtspd == IFXUSB_HPRT0_PRTSPD_HIGH_SPEED &&
161             ((_urb->dev->speed == USB_SPEED_LOW) ||
162              (_urb->dev->speed == USB_SPEED_FULL)) &&
163              (_urb->dev->tt) && (_urb->dev->tt->hub) && (_urb->dev->tt->hub->devnum != 1))
164         {
165                 IFX_DEBUGPL(DBG_HCD, "QH init: EP %d: TT found at hub addr %d, for port %d\n",
166                        usb_pipeendpoint(_urb->pipe), _urb->dev->tt->hub->devnum,
167                        _urb->dev->ttport);
168                 epqh->need_split = 1;
169         }
170
171         if (epqh->ep_type == IFXUSB_EP_TYPE_INTR ||
172             epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
173         {
174                 /* Compute scheduling parameters once and save them. */
175                 epqh->interval    = _urb->interval;
176                 if(epqh->need_split)
177                         epqh->interval *= 8;
178         }
179
180         #ifdef __EN_ISOC__
181                 if (epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
182                         _ifxhcd->isoc_ep_count++;
183         #endif
184
185         epqh->period_counter=0;
186
187         #ifdef __EPQD_DESTROY_TIMEOUT__
188                 /* Start a timer for this transfer. */
189                 init_timer(&epqh->destroy_timer);
190                 epqh->destroy_timer.function = eqph_destroy_func;
191                 epqh->destroy_timer.data = (unsigned long)(epqh);
192         #endif
193
194         #ifdef __DEBUG__
195                 IFX_DEBUGPL(DBG_HCD , "IFXUSB HCD EPQH Initialized\n");
196                 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH  - epqh = %p\n", epqh);
197                 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH  - Device Address = %d EP %d, %s\n",
198                             _urb->dev->devnum,
199                             usb_pipeendpoint(_urb->pipe),
200                             usb_pipein(_urb->pipe) == USB_DIR_IN ? "IN" : "OUT");
201                 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH  - Speed = %s\n",
202                             ({ char *speed; switch (_urb->dev->speed) {
203                             case USB_SPEED_LOW: speed  = "low" ; break;
204                             case USB_SPEED_FULL: speed = "full"; break;
205                             case USB_SPEED_HIGH: speed = "high"; break;
206                             default: speed = "?";       break;
207                             }; speed;}));
208                 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH  - Type = %s\n",
209                         ({
210                                 char *type; switch (epqh->ep_type)
211                                 {
212                                     case IFXUSB_EP_TYPE_ISOC: type = "isochronous"; break;
213                                     case IFXUSB_EP_TYPE_INTR: type = "interrupt"  ; break;
214                                     case IFXUSB_EP_TYPE_CTRL: type = "control"    ; break;
215                                     case IFXUSB_EP_TYPE_BULK: type = "bulk"       ; break;
216                                     default: type = "?";        break;
217                                 };
218                                 type;
219                         }));
220                 if (epqh->ep_type == IFXUSB_EP_TYPE_INTR)
221                         IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - interval = %d\n", epqh->interval);
222         #endif
223
224         LOCK_EPQH_LIST_ALL(_ifxhcd);
225         list_add_tail(&epqh->ql_all, &_ifxhcd->epqh_list_all);
226         UNLOCK_EPQH_LIST_ALL(_ifxhcd);
227
228         LOCK_EPQH_LIST(_ifxhcd);
229         switch (epqh->ep_type)
230         {
231                 case IFXUSB_EP_TYPE_CTRL:
232                 case IFXUSB_EP_TYPE_BULK:
233
234                         list_add_tail(&epqh->ql, &_ifxhcd->epqh_list_np);
235                         break;
236                 case IFXUSB_EP_TYPE_INTR:
237                         list_add_tail(&epqh->ql, &_ifxhcd->epqh_list_intr);
238                         break;
239                 #ifdef __EN_ISOC__
240                         case IFXUSB_EP_TYPE_ISOC:
241                                 list_add_tail(&epqh->ql, &_ifxhcd->epqh_list_isoc);
242
243                                 break;
244                 #endif
245         }
246         UNLOCK_EPQH_LIST(_ifxhcd);
247         return epqh;
248 }
249
250
251
252
253
254
255 /*!
256   \brief Free the EPQH.  EPQH should already be removed from a list.
257   URBD list should already be empty if called from URB Dequeue.
258
259   \param[in] _epqh The EPQH to free.
260  */
261 void ifxhcd_epqh_free (ifxhcd_epqh_t *_epqh)
262 {
263         unsigned long     flags;
264         if(!_epqh)
265                 return;
266
267         if(_epqh->sysep) _epqh->sysep->hcpriv=NULL;
268         _epqh->sysep=NULL;
269
270         local_irq_save (flags);
271         if (!list_empty(&_epqh->urbd_list))
272                 IFX_WARN("%s() invalid epqh state\n",__func__);
273         else
274         {
275                 LOCK_EPQH_LIST_ALL(_epqh->ifxhcd);
276                 if (!list_empty(&_epqh->ql_all))
277                         list_del_init (&_epqh->ql_all);
278                 UNLOCK_EPQH_LIST_ALL(_epqh->ifxhcd);
279
280                 LOCK_EPQH_LIST(_epqh->ifxhcd);
281                 if (!list_empty(&_epqh->ql))
282                         list_del_init (&_epqh->ql);
283                 UNLOCK_EPQH_LIST(_epqh->ifxhcd);
284
285                 #ifdef __EPQD_DESTROY_TIMEOUT__
286                         del_timer(&_epqh->destroy_timer);
287                 #endif
288                 kfree (_epqh);
289         }
290         local_irq_restore (flags);
291 }
292
293
294 void ifxhcd_epqh_idle(ifxhcd_epqh_t *_epqh)
295 {
296         unsigned long flags;
297         local_irq_save(flags);
298         LOCK_URBD_LIST(_epqh);
299         if (list_empty(&_epqh->urbd_list))
300         {
301                 if(_epqh->ep_type == IFXUSB_EP_TYPE_ISOC || _epqh->ep_type == IFXUSB_EP_TYPE_INTR)
302                         _epqh->phase=EPQH_STDBY;
303                 else
304                 {
305                         _epqh->phase=EPQH_IDLE;
306                         #ifdef __EPQD_DESTROY_TIMEOUT__
307                                 del_timer(&_epqh->destroy_timer);
308                                 _epqh->destroy_timer.expires = jiffies + (HZ*epqh_self_destroy_timeout);
309                                 add_timer(&_epqh->destroy_timer );
310                         #endif
311                 }
312         }
313         else
314         {
315                 _epqh->phase=EPQH_READY;
316                 #ifdef __EPQD_DESTROY_TIMEOUT__
317                         del_timer(&_epqh->destroy_timer);
318                 #endif
319         }
320         UNLOCK_URBD_LIST(_epqh);
321         local_irq_restore(flags);
322 }
323
324
325 void ifxhcd_epqh_idle_periodic(ifxhcd_epqh_t *_epqh)
326 {
327         unsigned long flags;
328         if(_epqh->ep_type != IFXUSB_EP_TYPE_ISOC && _epqh->ep_type != IFXUSB_EP_TYPE_INTR && _epqh->phase!=EPQH_STDBY)
329                 return;
330
331         local_irq_save(flags);
332         LOCK_URBD_LIST(_epqh);
333         if (!list_empty(&_epqh->urbd_list))
334                 IFX_WARN("%s() invalid epqh state(not empty)\n",__func__);
335
336         _epqh->phase=EPQH_IDLE;
337
338         #ifdef __EPQD_DESTROY_TIMEOUT__
339                 del_timer(&_epqh->destroy_timer);
340                 _epqh->destroy_timer.expires = jiffies + (HZ*epqh_self_destroy_timeout);
341                 add_timer(&_epqh->destroy_timer );
342         #endif
343
344         #ifdef __EN_ISOC__
345                 if (_epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
346                         _epqh->ifxhcd->isoc_ep_count--;
347         #endif
348         UNLOCK_URBD_LIST(_epqh);
349         local_irq_restore(flags);
350 }
351
352
353 ifxhcd_epqh_t *ifxhcd_urbd_create (ifxhcd_hcd_t *_ifxhcd,struct urb *_urb)
354 {
355         ifxhcd_urbd_t            *urbd;
356         struct usb_host_endpoint *sysep;
357         ifxhcd_epqh_t            *epqh=NULL;
358         unsigned long             flags;
359
360         local_irq_save(flags);
361
362         sysep = ifxhcd_urb_to_endpoint(_urb);
363
364         LOCK_EPQH_LIST_ALL(_ifxhcd);
365         epqh = sysep_to_epqh(_ifxhcd, sysep);
366
367         if (!epqh)
368         {
369                 sysep->hcpriv = NULL;
370                 epqh = ifxhcd_epqh_create (_ifxhcd, _urb);
371         }
372         UNLOCK_EPQH_LIST_ALL(_ifxhcd);
373
374         if (!epqh)
375         {
376                 IFX_ERROR("EPQH Error alloc\n");
377                 local_irq_restore (flags);
378                 return (ifxhcd_epqh_t *)NULL;
379         }
380         if(epqh->phase==EPQH_DISABLING)
381         {
382                 IFX_ERROR("EPQH Error alloc while disabling\n");
383                 local_irq_restore (flags);
384                 return (ifxhcd_epqh_t *)NULL;
385         }
386         sysep->hcpriv = epqh;
387
388         if(_urb->hcpriv)
389         {
390                 IFX_WARN("%s() Previous urb->hcpriv exist %p\n",__func__,_urb->hcpriv);
391         #if 1
392                 local_irq_restore (flags);
393                 return (ifxhcd_epqh_t *)NULL;
394         #else
395                 urbd = _urb->hcpriv;
396                 if(urbd->epqh!=epqh)
397                         IFX_WARN("%s() Previous urb->hcpriv exist %p and epqh not the same %p %p\n",__func__,_urb->hcpriv,urbd->epqh,epqh);
398         #endif
399         }
400         else
401         {
402                 urbd =  (ifxhcd_urbd_t *) kmalloc (sizeof(ifxhcd_urbd_t), GFP_ATOMIC);
403                 if (!urbd)
404                 {
405                         local_irq_restore (flags);
406                         return (ifxhcd_epqh_t *)NULL;
407                 }
408                 memset (urbd, 0, sizeof (ifxhcd_urbd_t));
409                 INIT_LIST_HEAD(&urbd->ql);
410         }
411
412         _urb->hcpriv = urbd;
413         urbd->urb   = _urb;
414         urbd->epqh  =  epqh;
415         urbd->status= -EINPROGRESS;
416
417         urbd->is_in=usb_pipein(_urb->pipe) ? 1 : 0;
418 #define URB_NO_SETUP_DMA_MAP         0
419         #ifdef __EN_ISOC__
420         if(epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
421         {
422                 if(_urb->transfer_flags && URB_NO_TRANSFER_DMA_MAP)
423                         urbd->xfer_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->transfer_dma));
424                 else
425                         urbd->xfer_buff = (uint8_t *) _urb->transfer_buffer;
426         }
427         else
428         #endif
429         {
430                 urbd->xfer_len=_urb->transfer_buffer_length;
431                 if(urbd->xfer_len>0)
432                 {
433                         if(_urb->transfer_flags && URB_NO_TRANSFER_DMA_MAP)
434                                 urbd->xfer_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->transfer_dma));
435                         else
436                                 urbd->xfer_buff = (uint8_t *) _urb->transfer_buffer;
437                 }
438         }
439
440         #if 1 // cache write-back, so DMA engine can get correct content. Precaution
441                 if(urbd->xfer_len)
442                         dma_cache_wback_inv((unsigned long)urbd->xfer_buff, urbd->xfer_len);
443         #endif
444
445         if(epqh->ep_type == IFXUSB_EP_TYPE_CTRL)
446         {
447                 if(_urb->transfer_flags && URB_NO_SETUP_DMA_MAP)
448                         urbd->setup_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->setup_dma));
449                 else
450                         urbd->setup_buff = (uint8_t *) _urb->setup_packet;
451                 #if 1 // cache write-back, so DMA engine can get correct content. Precaution
452                         dma_cache_wback_inv((unsigned long)urbd->setup_buff, 16);
453                 #endif
454         }
455
456         LOCK_URBD_LIST(epqh);
457         if (!list_empty(&urbd->ql))
458                 list_del_init(&urbd->ql);
459         list_add_tail(&urbd->ql, &epqh->urbd_list);
460         epqh->urbd_count++;
461         UNLOCK_URBD_LIST(epqh);
462
463         local_irq_restore (flags);
464         return epqh;
465 }
466
467
468
469 ifxhcd_epqh_t * sysep_to_epqh(ifxhcd_hcd_t *_ifxhcd, struct usb_host_endpoint *_sysep)
470 {
471         ifxhcd_epqh_t *epqh;
472
473         LOCK_EPQH_LIST_ALL(_ifxhcd);
474         list_for_each_entry( epqh, &_ifxhcd->epqh_list_all, ql_all)
475         {
476                 if(epqh->sysep==_sysep)
477                 {
478                         UNLOCK_EPQH_LIST_ALL(_ifxhcd);
479                         return epqh;
480                 }
481         }
482         UNLOCK_EPQH_LIST_ALL(_ifxhcd);
483         return NULL;
484 }
485