]> Pileus Git - ~andy/linux/blob - drivers/staging/octeon-usb/octeon-hcd.c
Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
[~andy/linux] / drivers / staging / octeon-usb / octeon-hcd.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2008 Cavium Networks
7  */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/pci.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/usb.h>
15
16 #include <linux/time.h>
17 #include <linux/delay.h>
18
19 #include <asm/octeon/cvmx.h>
20 #include "cvmx-usb.h"
21 #include <asm/octeon/cvmx-iob-defs.h>
22
23 #include <linux/usb/hcd.h>
24
25 #include <linux/err.h>
26
27 struct octeon_hcd {
28         spinlock_t lock;
29         struct cvmx_usb_state usb;
30         struct tasklet_struct dequeue_tasklet;
31         struct list_head dequeue_list;
32 };
33
34 /* convert between an HCD pointer and the corresponding struct octeon_hcd */
35 static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
36 {
37         return (struct octeon_hcd *)(hcd->hcd_priv);
38 }
39
40 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
41 {
42         return container_of((void *)p, struct usb_hcd, hcd_priv);
43 }
44
45 static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
46 {
47         return container_of(p, struct octeon_hcd, usb);
48 }
49
50 static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
51 {
52         struct octeon_hcd *priv = hcd_to_octeon(hcd);
53         unsigned long flags;
54
55         spin_lock_irqsave(&priv->lock, flags);
56         cvmx_usb_poll(&priv->usb);
57         spin_unlock_irqrestore(&priv->lock, flags);
58         return IRQ_HANDLED;
59 }
60
61 static void octeon_usb_port_callback(struct cvmx_usb_state *usb,
62                                      enum cvmx_usb_callback reason,
63                                      enum cvmx_usb_complete status,
64                                      int pipe_handle,
65                                      int submit_handle,
66                                      int bytes_transferred,
67                                      void *user_data)
68 {
69         struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
70
71         spin_unlock(&priv->lock);
72         usb_hcd_poll_rh_status(octeon_to_hcd(priv));
73         spin_lock(&priv->lock);
74 }
75
76 static int octeon_usb_start(struct usb_hcd *hcd)
77 {
78         struct octeon_hcd *priv = hcd_to_octeon(hcd);
79         unsigned long flags;
80
81         hcd->state = HC_STATE_RUNNING;
82         spin_lock_irqsave(&priv->lock, flags);
83         cvmx_usb_register_callback(&priv->usb, CVMX_USB_CALLBACK_PORT_CHANGED,
84                                    octeon_usb_port_callback, NULL);
85         spin_unlock_irqrestore(&priv->lock, flags);
86         return 0;
87 }
88
89 static void octeon_usb_stop(struct usb_hcd *hcd)
90 {
91         struct octeon_hcd *priv = hcd_to_octeon(hcd);
92         unsigned long flags;
93
94         spin_lock_irqsave(&priv->lock, flags);
95         cvmx_usb_register_callback(&priv->usb, CVMX_USB_CALLBACK_PORT_CHANGED,
96                                    NULL, NULL);
97         spin_unlock_irqrestore(&priv->lock, flags);
98         hcd->state = HC_STATE_HALT;
99 }
100
101 static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
102 {
103         struct octeon_hcd *priv = hcd_to_octeon(hcd);
104
105         return cvmx_usb_get_frame_number(&priv->usb);
106 }
107
108 static void octeon_usb_urb_complete_callback(struct cvmx_usb_state *usb,
109                                              enum cvmx_usb_callback reason,
110                                              enum cvmx_usb_complete status,
111                                              int pipe_handle,
112                                              int submit_handle,
113                                              int bytes_transferred,
114                                              void *user_data)
115 {
116         struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
117         struct usb_hcd *hcd = octeon_to_hcd(priv);
118         struct device *dev = hcd->self.controller;
119         struct urb *urb = user_data;
120
121         urb->actual_length = bytes_transferred;
122         urb->hcpriv = NULL;
123
124         if (!list_empty(&urb->urb_list)) {
125                 /*
126                  * It is on the dequeue_list, but we are going to call
127                  * usb_hcd_giveback_urb(), so we must clear it from
128                  * the list.  We got to it before the
129                  * octeon_usb_urb_dequeue_work() tasklet did.
130                  */
131                 list_del(&urb->urb_list);
132                 /* No longer on the dequeue_list. */
133                 INIT_LIST_HEAD(&urb->urb_list);
134         }
135
136         /* For Isochronous transactions we need to update the URB packet status
137            list from data in our private copy */
138         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
139                 int i;
140                 /*
141                  * The pointer to the private list is stored in the setup_packet
142                  * field.
143                  */
144                 struct cvmx_usb_iso_packet *iso_packet =
145                         (struct cvmx_usb_iso_packet *) urb->setup_packet;
146                 /* Recalculate the transfer size by adding up each packet */
147                 urb->actual_length = 0;
148                 for (i = 0; i < urb->number_of_packets; i++) {
149                         if (iso_packet[i].status == CVMX_USB_COMPLETE_SUCCESS) {
150                                 urb->iso_frame_desc[i].status = 0;
151                                 urb->iso_frame_desc[i].actual_length = iso_packet[i].length;
152                                 urb->actual_length += urb->iso_frame_desc[i].actual_length;
153                         } else {
154                                 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%d submit=%d size=%d\n",
155                                         i, urb->number_of_packets,
156                                         iso_packet[i].status, pipe_handle,
157                                         submit_handle, iso_packet[i].length);
158                                 urb->iso_frame_desc[i].status = -EREMOTEIO;
159                         }
160                 }
161                 /* Free the private list now that we don't need it anymore */
162                 kfree(iso_packet);
163                 urb->setup_packet = NULL;
164         }
165
166         switch (status) {
167         case CVMX_USB_COMPLETE_SUCCESS:
168                 urb->status = 0;
169                 break;
170         case CVMX_USB_COMPLETE_CANCEL:
171                 if (urb->status == 0)
172                         urb->status = -ENOENT;
173                 break;
174         case CVMX_USB_COMPLETE_STALL:
175                 dev_dbg(dev, "status=stall pipe=%d submit=%d size=%d\n",
176                         pipe_handle, submit_handle, bytes_transferred);
177                 urb->status = -EPIPE;
178                 break;
179         case CVMX_USB_COMPLETE_BABBLEERR:
180                 dev_dbg(dev, "status=babble pipe=%d submit=%d size=%d\n",
181                         pipe_handle, submit_handle, bytes_transferred);
182                 urb->status = -EPIPE;
183                 break;
184         case CVMX_USB_COMPLETE_SHORT:
185                 dev_dbg(dev, "status=short pipe=%d submit=%d size=%d\n",
186                         pipe_handle, submit_handle, bytes_transferred);
187                 urb->status = -EREMOTEIO;
188                 break;
189         case CVMX_USB_COMPLETE_ERROR:
190         case CVMX_USB_COMPLETE_XACTERR:
191         case CVMX_USB_COMPLETE_DATATGLERR:
192         case CVMX_USB_COMPLETE_FRAMEERR:
193                 dev_dbg(dev, "status=%d pipe=%d submit=%d size=%d\n",
194                         status, pipe_handle, submit_handle, bytes_transferred);
195                 urb->status = -EPROTO;
196                 break;
197         }
198         spin_unlock(&priv->lock);
199         usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
200         spin_lock(&priv->lock);
201 }
202
203 static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
204                                   struct urb *urb,
205                                   gfp_t mem_flags)
206 {
207         struct octeon_hcd *priv = hcd_to_octeon(hcd);
208         struct device *dev = hcd->self.controller;
209         int submit_handle = -1;
210         int pipe_handle;
211         unsigned long flags;
212         struct cvmx_usb_iso_packet *iso_packet;
213         struct usb_host_endpoint *ep = urb->ep;
214
215         urb->status = 0;
216         INIT_LIST_HEAD(&urb->urb_list); /* not enqueued on dequeue_list */
217         spin_lock_irqsave(&priv->lock, flags);
218
219         if (!ep->hcpriv) {
220                 enum cvmx_usb_transfer transfer_type;
221                 enum cvmx_usb_speed speed;
222                 int split_device = 0;
223                 int split_port = 0;
224                 switch (usb_pipetype(urb->pipe)) {
225                 case PIPE_ISOCHRONOUS:
226                         transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
227                         break;
228                 case PIPE_INTERRUPT:
229                         transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
230                         break;
231                 case PIPE_CONTROL:
232                         transfer_type = CVMX_USB_TRANSFER_CONTROL;
233                         break;
234                 default:
235                         transfer_type = CVMX_USB_TRANSFER_BULK;
236                         break;
237                 }
238                 switch (urb->dev->speed) {
239                 case USB_SPEED_LOW:
240                         speed = CVMX_USB_SPEED_LOW;
241                         break;
242                 case USB_SPEED_FULL:
243                         speed = CVMX_USB_SPEED_FULL;
244                         break;
245                 default:
246                         speed = CVMX_USB_SPEED_HIGH;
247                         break;
248                 }
249                 /*
250                  * For slow devices on high speed ports we need to find the hub
251                  * that does the speed translation so we know where to send the
252                  * split transactions.
253                  */
254                 if (speed != CVMX_USB_SPEED_HIGH) {
255                         /*
256                          * Start at this device and work our way up the usb
257                          * tree.
258                          */
259                         struct usb_device *dev = urb->dev;
260                         while (dev->parent) {
261                                 /*
262                                  * If our parent is high speed then he'll
263                                  * receive the splits.
264                                  */
265                                 if (dev->parent->speed == USB_SPEED_HIGH) {
266                                         split_device = dev->parent->devnum;
267                                         split_port = dev->portnum;
268                                         break;
269                                 }
270                                 /*
271                                  * Move up the tree one level. If we make it all
272                                  * the way up the tree, then the port must not
273                                  * be in high speed mode and we don't need a
274                                  * split.
275                                  */
276                                 dev = dev->parent;
277                         }
278                 }
279                 pipe_handle = cvmx_usb_open_pipe(&priv->usb,
280                                                  0,
281                                                  usb_pipedevice(urb->pipe),
282                                                  usb_pipeendpoint(urb->pipe),
283                                                  speed,
284                                                  le16_to_cpu(ep->desc.wMaxPacketSize) & 0x7ff,
285                                                  transfer_type,
286                                                  usb_pipein(urb->pipe) ? CVMX_USB_DIRECTION_IN : CVMX_USB_DIRECTION_OUT,
287                                                  urb->interval,
288                                                  (le16_to_cpu(ep->desc.wMaxPacketSize) >> 11) & 0x3,
289                                                  split_device,
290                                                  split_port);
291                 if (pipe_handle < 0) {
292                         spin_unlock_irqrestore(&priv->lock, flags);
293                         dev_dbg(dev, "Failed to create pipe\n");
294                         return -ENOMEM;
295                 }
296                 ep->hcpriv = (void *)(0x10000L + pipe_handle);
297         } else {
298                 pipe_handle = 0xffff & (long)ep->hcpriv;
299         }
300
301         switch (usb_pipetype(urb->pipe)) {
302         case PIPE_ISOCHRONOUS:
303                 dev_dbg(dev, "Submit isochronous to %d.%d\n",
304                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
305                 /*
306                  * Allocate a structure to use for our private list of
307                  * isochronous packets.
308                  */
309                 iso_packet = kmalloc(urb->number_of_packets *
310                                      sizeof(struct cvmx_usb_iso_packet),
311                                      GFP_ATOMIC);
312                 if (iso_packet) {
313                         int i;
314                         /* Fill the list with the data from the URB */
315                         for (i = 0; i < urb->number_of_packets; i++) {
316                                 iso_packet[i].offset = urb->iso_frame_desc[i].offset;
317                                 iso_packet[i].length = urb->iso_frame_desc[i].length;
318                                 iso_packet[i].status = CVMX_USB_COMPLETE_ERROR;
319                         }
320                         /*
321                          * Store a pointer to the list in the URB setup_packet
322                          * field. We know this currently isn't being used and
323                          * this saves us a bunch of logic.
324                          */
325                         urb->setup_packet = (char *)iso_packet;
326                         submit_handle = cvmx_usb_submit_isochronous(&priv->usb, pipe_handle,
327                                                         urb->start_frame,
328                                                         0 /* flags */ ,
329                                                         urb->number_of_packets,
330                                                         iso_packet,
331                                                         urb->transfer_dma,
332                                                         urb->transfer_buffer_length,
333                                                         octeon_usb_urb_complete_callback,
334                                                         urb);
335                         /*
336                          * If submit failed we need to free our private packet
337                          * list.
338                          */
339                         if (submit_handle < 0) {
340                                 urb->setup_packet = NULL;
341                                 kfree(iso_packet);
342                         }
343                 }
344                 break;
345         case PIPE_INTERRUPT:
346                 dev_dbg(dev, "Submit interrupt to %d.%d\n",
347                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
348                 submit_handle = cvmx_usb_submit_interrupt(&priv->usb, pipe_handle,
349                                               urb->transfer_dma,
350                                               urb->transfer_buffer_length,
351                                               octeon_usb_urb_complete_callback,
352                                               urb);
353                 break;
354         case PIPE_CONTROL:
355                 dev_dbg(dev, "Submit control to %d.%d\n",
356                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
357                 submit_handle = cvmx_usb_submit_control(&priv->usb, pipe_handle,
358                                             urb->setup_dma,
359                                             urb->transfer_dma,
360                                             urb->transfer_buffer_length,
361                                             octeon_usb_urb_complete_callback,
362                                             urb);
363                 break;
364         case PIPE_BULK:
365                 dev_dbg(dev, "Submit bulk to %d.%d\n",
366                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
367                 submit_handle = cvmx_usb_submit_bulk(&priv->usb, pipe_handle,
368                                          urb->transfer_dma,
369                                          urb->transfer_buffer_length,
370                                          octeon_usb_urb_complete_callback,
371                                          urb);
372                 break;
373         }
374         if (submit_handle < 0) {
375                 spin_unlock_irqrestore(&priv->lock, flags);
376                 dev_dbg(dev, "Failed to submit\n");
377                 return -ENOMEM;
378         }
379         urb->hcpriv = (void *)(long)(((submit_handle & 0xffff) << 16) | pipe_handle);
380         spin_unlock_irqrestore(&priv->lock, flags);
381         return 0;
382 }
383
384 static void octeon_usb_urb_dequeue_work(unsigned long arg)
385 {
386         unsigned long flags;
387         struct octeon_hcd *priv = (struct octeon_hcd *)arg;
388
389         spin_lock_irqsave(&priv->lock, flags);
390
391         while (!list_empty(&priv->dequeue_list)) {
392                 int pipe_handle;
393                 int submit_handle;
394                 struct urb *urb = container_of(priv->dequeue_list.next, struct urb, urb_list);
395                 list_del(&urb->urb_list);
396                 /* not enqueued on dequeue_list */
397                 INIT_LIST_HEAD(&urb->urb_list);
398                 pipe_handle = 0xffff & (long)urb->hcpriv;
399                 submit_handle = ((long)urb->hcpriv) >> 16;
400                 cvmx_usb_cancel(&priv->usb, pipe_handle, submit_handle);
401         }
402
403         spin_unlock_irqrestore(&priv->lock, flags);
404 }
405
406 static int octeon_usb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
407 {
408         struct octeon_hcd *priv = hcd_to_octeon(hcd);
409         unsigned long flags;
410
411         if (!urb->dev)
412                 return -EINVAL;
413
414         spin_lock_irqsave(&priv->lock, flags);
415
416         urb->status = status;
417         list_add_tail(&urb->urb_list, &priv->dequeue_list);
418
419         spin_unlock_irqrestore(&priv->lock, flags);
420
421         tasklet_schedule(&priv->dequeue_tasklet);
422
423         return 0;
424 }
425
426 static void octeon_usb_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
427 {
428         struct device *dev = hcd->self.controller;
429
430         if (ep->hcpriv) {
431                 struct octeon_hcd *priv = hcd_to_octeon(hcd);
432                 int pipe_handle = 0xffff & (long)ep->hcpriv;
433                 unsigned long flags;
434                 spin_lock_irqsave(&priv->lock, flags);
435                 cvmx_usb_cancel_all(&priv->usb, pipe_handle);
436                 if (cvmx_usb_close_pipe(&priv->usb, pipe_handle))
437                         dev_dbg(dev, "Closing pipe %d failed\n", pipe_handle);
438                 spin_unlock_irqrestore(&priv->lock, flags);
439                 ep->hcpriv = NULL;
440         }
441 }
442
443 static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
444 {
445         struct octeon_hcd *priv = hcd_to_octeon(hcd);
446         struct cvmx_usb_port_status port_status;
447         unsigned long flags;
448
449         spin_lock_irqsave(&priv->lock, flags);
450         port_status = cvmx_usb_get_status(&priv->usb);
451         spin_unlock_irqrestore(&priv->lock, flags);
452         buf[0] = 0;
453         buf[0] = port_status.connect_change << 1;
454
455         return (buf[0] != 0);
456 }
457
458 static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
459 {
460         struct octeon_hcd *priv = hcd_to_octeon(hcd);
461         struct device *dev = hcd->self.controller;
462         struct cvmx_usb_port_status usb_port_status;
463         int port_status;
464         struct usb_hub_descriptor *desc;
465         unsigned long flags;
466
467         switch (typeReq) {
468         case ClearHubFeature:
469                 dev_dbg(dev, "ClearHubFeature\n");
470                 switch (wValue) {
471                 case C_HUB_LOCAL_POWER:
472                 case C_HUB_OVER_CURRENT:
473                         /* Nothing required here */
474                         break;
475                 default:
476                         return -EINVAL;
477                 }
478                 break;
479         case ClearPortFeature:
480                 dev_dbg(dev, "ClearPortFeature\n");
481                 if (wIndex != 1) {
482                         dev_dbg(dev, " INVALID\n");
483                         return -EINVAL;
484                 }
485
486                 switch (wValue) {
487                 case USB_PORT_FEAT_ENABLE:
488                         dev_dbg(dev, " ENABLE\n");
489                         spin_lock_irqsave(&priv->lock, flags);
490                         cvmx_usb_disable(&priv->usb);
491                         spin_unlock_irqrestore(&priv->lock, flags);
492                         break;
493                 case USB_PORT_FEAT_SUSPEND:
494                         dev_dbg(dev, " SUSPEND\n");
495                         /* Not supported on Octeon */
496                         break;
497                 case USB_PORT_FEAT_POWER:
498                         dev_dbg(dev, " POWER\n");
499                         /* Not supported on Octeon */
500                         break;
501                 case USB_PORT_FEAT_INDICATOR:
502                         dev_dbg(dev, " INDICATOR\n");
503                         /* Port inidicator not supported */
504                         break;
505                 case USB_PORT_FEAT_C_CONNECTION:
506                         dev_dbg(dev, " C_CONNECTION\n");
507                         /* Clears drivers internal connect status change flag */
508                         spin_lock_irqsave(&priv->lock, flags);
509                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
510                         spin_unlock_irqrestore(&priv->lock, flags);
511                         break;
512                 case USB_PORT_FEAT_C_RESET:
513                         dev_dbg(dev, " C_RESET\n");
514                         /*
515                          * Clears the driver's internal Port Reset Change flag.
516                          */
517                         spin_lock_irqsave(&priv->lock, flags);
518                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
519                         spin_unlock_irqrestore(&priv->lock, flags);
520                         break;
521                 case USB_PORT_FEAT_C_ENABLE:
522                         dev_dbg(dev, " C_ENABLE\n");
523                         /*
524                          * Clears the driver's internal Port Enable/Disable
525                          * Change flag.
526                          */
527                         spin_lock_irqsave(&priv->lock, flags);
528                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
529                         spin_unlock_irqrestore(&priv->lock, flags);
530                         break;
531                 case USB_PORT_FEAT_C_SUSPEND:
532                         dev_dbg(dev, " C_SUSPEND\n");
533                         /*
534                          * Clears the driver's internal Port Suspend Change
535                          * flag, which is set when resume signaling on the host
536                          * port is complete.
537                          */
538                         break;
539                 case USB_PORT_FEAT_C_OVER_CURRENT:
540                         dev_dbg(dev, " C_OVER_CURRENT\n");
541                         /* Clears the driver's overcurrent Change flag */
542                         spin_lock_irqsave(&priv->lock, flags);
543                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
544                         spin_unlock_irqrestore(&priv->lock, flags);
545                         break;
546                 default:
547                         dev_dbg(dev, " UNKNOWN\n");
548                         return -EINVAL;
549                 }
550                 break;
551         case GetHubDescriptor:
552                 dev_dbg(dev, "GetHubDescriptor\n");
553                 desc = (struct usb_hub_descriptor *)buf;
554                 desc->bDescLength = 9;
555                 desc->bDescriptorType = 0x29;
556                 desc->bNbrPorts = 1;
557                 desc->wHubCharacteristics = 0x08;
558                 desc->bPwrOn2PwrGood = 1;
559                 desc->bHubContrCurrent = 0;
560                 desc->u.hs.DeviceRemovable[0] = 0;
561                 desc->u.hs.DeviceRemovable[1] = 0xff;
562                 break;
563         case GetHubStatus:
564                 dev_dbg(dev, "GetHubStatus\n");
565                 *(__le32 *) buf = 0;
566                 break;
567         case GetPortStatus:
568                 dev_dbg(dev, "GetPortStatus\n");
569                 if (wIndex != 1) {
570                         dev_dbg(dev, " INVALID\n");
571                         return -EINVAL;
572                 }
573
574                 spin_lock_irqsave(&priv->lock, flags);
575                 usb_port_status = cvmx_usb_get_status(&priv->usb);
576                 spin_unlock_irqrestore(&priv->lock, flags);
577                 port_status = 0;
578
579                 if (usb_port_status.connect_change) {
580                         port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
581                         dev_dbg(dev, " C_CONNECTION\n");
582                 }
583
584                 if (usb_port_status.port_enabled) {
585                         port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
586                         dev_dbg(dev, " C_ENABLE\n");
587                 }
588
589                 if (usb_port_status.connected) {
590                         port_status |= (1 << USB_PORT_FEAT_CONNECTION);
591                         dev_dbg(dev, " CONNECTION\n");
592                 }
593
594                 if (usb_port_status.port_enabled) {
595                         port_status |= (1 << USB_PORT_FEAT_ENABLE);
596                         dev_dbg(dev, " ENABLE\n");
597                 }
598
599                 if (usb_port_status.port_over_current) {
600                         port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
601                         dev_dbg(dev, " OVER_CURRENT\n");
602                 }
603
604                 if (usb_port_status.port_powered) {
605                         port_status |= (1 << USB_PORT_FEAT_POWER);
606                         dev_dbg(dev, " POWER\n");
607                 }
608
609                 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
610                         port_status |= USB_PORT_STAT_HIGH_SPEED;
611                         dev_dbg(dev, " HIGHSPEED\n");
612                 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
613                         port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
614                         dev_dbg(dev, " LOWSPEED\n");
615                 }
616
617                 *((__le32 *) buf) = cpu_to_le32(port_status);
618                 break;
619         case SetHubFeature:
620                 dev_dbg(dev, "SetHubFeature\n");
621                 /* No HUB features supported */
622                 break;
623         case SetPortFeature:
624                 dev_dbg(dev, "SetPortFeature\n");
625                 if (wIndex != 1) {
626                         dev_dbg(dev, " INVALID\n");
627                         return -EINVAL;
628                 }
629
630                 switch (wValue) {
631                 case USB_PORT_FEAT_SUSPEND:
632                         dev_dbg(dev, " SUSPEND\n");
633                         return -EINVAL;
634                 case USB_PORT_FEAT_POWER:
635                         dev_dbg(dev, " POWER\n");
636                         return -EINVAL;
637                 case USB_PORT_FEAT_RESET:
638                         dev_dbg(dev, " RESET\n");
639                         spin_lock_irqsave(&priv->lock, flags);
640                         cvmx_usb_disable(&priv->usb);
641                         if (cvmx_usb_enable(&priv->usb))
642                                 dev_dbg(dev, "Failed to enable the port\n");
643                         spin_unlock_irqrestore(&priv->lock, flags);
644                         return 0;
645                 case USB_PORT_FEAT_INDICATOR:
646                         dev_dbg(dev, " INDICATOR\n");
647                         /* Not supported */
648                         break;
649                 default:
650                         dev_dbg(dev, " UNKNOWN\n");
651                         return -EINVAL;
652                 }
653                 break;
654         default:
655                 dev_dbg(dev, "Unknown root hub request\n");
656                 return -EINVAL;
657         }
658         return 0;
659 }
660
661
662 static const struct hc_driver octeon_hc_driver = {
663         .description            = "Octeon USB",
664         .product_desc           = "Octeon Host Controller",
665         .hcd_priv_size          = sizeof(struct octeon_hcd),
666         .irq                    = octeon_usb_irq,
667         .flags                  = HCD_MEMORY | HCD_USB2,
668         .start                  = octeon_usb_start,
669         .stop                   = octeon_usb_stop,
670         .urb_enqueue            = octeon_usb_urb_enqueue,
671         .urb_dequeue            = octeon_usb_urb_dequeue,
672         .endpoint_disable       = octeon_usb_endpoint_disable,
673         .get_frame_number       = octeon_usb_get_frame_number,
674         .hub_status_data        = octeon_usb_hub_status_data,
675         .hub_control            = octeon_usb_hub_control,
676 };
677
678
679 static int octeon_usb_driver_probe(struct device *dev)
680 {
681         int status;
682         int usb_num = to_platform_device(dev)->id;
683         int irq = platform_get_irq(to_platform_device(dev), 0);
684         struct octeon_hcd *priv;
685         struct usb_hcd *hcd;
686         unsigned long flags;
687
688         /*
689          * Set the DMA mask to 64bits so we get buffers already translated for
690          * DMA.
691          */
692         dev->coherent_dma_mask = ~0;
693         dev->dma_mask = &dev->coherent_dma_mask;
694
695         hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
696         if (!hcd) {
697                 dev_dbg(dev, "Failed to allocate memory for HCD\n");
698                 return -1;
699         }
700         hcd->uses_new_polling = 1;
701         priv = (struct octeon_hcd *)hcd->hcd_priv;
702
703         spin_lock_init(&priv->lock);
704
705         tasklet_init(&priv->dequeue_tasklet, octeon_usb_urb_dequeue_work, (unsigned long)priv);
706         INIT_LIST_HEAD(&priv->dequeue_list);
707
708         status = cvmx_usb_initialize(&priv->usb, usb_num, CVMX_USB_INITIALIZE_FLAGS_CLOCK_AUTO);
709         if (status) {
710                 dev_dbg(dev, "USB initialization failed with %d\n", status);
711                 kfree(hcd);
712                 return -1;
713         }
714
715         /* This delay is needed for CN3010, but I don't know why... */
716         mdelay(10);
717
718         spin_lock_irqsave(&priv->lock, flags);
719         cvmx_usb_poll(&priv->usb);
720         spin_unlock_irqrestore(&priv->lock, flags);
721
722         status = usb_add_hcd(hcd, irq, IRQF_SHARED);
723         if (status) {
724                 dev_dbg(dev, "USB add HCD failed with %d\n", status);
725                 kfree(hcd);
726                 return -1;
727         }
728
729         dev_dbg(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
730
731         return 0;
732 }
733
734 static int octeon_usb_driver_remove(struct device *dev)
735 {
736         int status;
737         struct usb_hcd *hcd = dev_get_drvdata(dev);
738         struct octeon_hcd *priv = hcd_to_octeon(hcd);
739         unsigned long flags;
740
741         usb_remove_hcd(hcd);
742         tasklet_kill(&priv->dequeue_tasklet);
743         spin_lock_irqsave(&priv->lock, flags);
744         status = cvmx_usb_shutdown(&priv->usb);
745         spin_unlock_irqrestore(&priv->lock, flags);
746         if (status)
747                 dev_dbg(dev, "USB shutdown failed with %d\n", status);
748
749         kfree(hcd);
750
751         return 0;
752 }
753
754 static struct device_driver octeon_usb_driver = {
755         .name   = "OcteonUSB",
756         .bus    = &platform_bus_type,
757         .probe  = octeon_usb_driver_probe,
758         .remove = octeon_usb_driver_remove,
759 };
760
761
762 #define MAX_USB_PORTS   10
763 static struct platform_device *pdev_glob[MAX_USB_PORTS];
764 static int octeon_usb_registered;
765 static int __init octeon_usb_module_init(void)
766 {
767         int num_devices = cvmx_usb_get_num_ports();
768         int device;
769
770         if (usb_disabled() || num_devices == 0)
771                 return -ENODEV;
772
773         if (driver_register(&octeon_usb_driver))
774                 return -ENOMEM;
775
776         octeon_usb_registered = 1;
777
778         /*
779          * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
780          * IOB priority registers.  Under heavy network load USB
781          * hardware can be starved by the IOB causing a crash.  Give
782          * it a priority boost if it has been waiting more than 400
783          * cycles to avoid this situation.
784          *
785          * Testing indicates that a cnt_val of 8192 is not sufficient,
786          * but no failures are seen with 4096.  We choose a value of
787          * 400 to give a safety factor of 10.
788          */
789         if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
790                 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
791
792                 pri_cnt.u64 = 0;
793                 pri_cnt.s.cnt_enb = 1;
794                 pri_cnt.s.cnt_val = 400;
795                 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
796         }
797
798         for (device = 0; device < num_devices; device++) {
799                 struct resource irq_resource;
800                 struct platform_device *pdev;
801                 memset(&irq_resource, 0, sizeof(irq_resource));
802                 irq_resource.start = (device == 0) ? OCTEON_IRQ_USB0 : OCTEON_IRQ_USB1;
803                 irq_resource.end = irq_resource.start;
804                 irq_resource.flags = IORESOURCE_IRQ;
805                 pdev = platform_device_register_simple((char *)octeon_usb_driver.  name, device, &irq_resource, 1);
806                 if (IS_ERR(pdev)) {
807                         driver_unregister(&octeon_usb_driver);
808                         octeon_usb_registered = 0;
809                         return PTR_ERR(pdev);
810                 }
811                 if (device < MAX_USB_PORTS)
812                         pdev_glob[device] = pdev;
813
814         }
815         return 0;
816 }
817
818 static void __exit octeon_usb_module_cleanup(void)
819 {
820         int i;
821
822         for (i = 0; i < MAX_USB_PORTS; i++)
823                 if (pdev_glob[i]) {
824                         platform_device_unregister(pdev_glob[i]);
825                         pdev_glob[i] = NULL;
826                 }
827         if (octeon_usb_registered)
828                 driver_unregister(&octeon_usb_driver);
829 }
830
831 MODULE_LICENSE("GPL");
832 MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
833 MODULE_DESCRIPTION("Cavium Networks Octeon USB Host driver.");
834 module_init(octeon_usb_module_init);
835 module_exit(octeon_usb_module_cleanup);