x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / staging / octeon-usb / octeon-hcd.c
blob5dbbd14ec615aba59d16c72a2e7acbe08f9073cf
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.
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>
16 #include <linux/time.h>
17 #include <linux/delay.h>
19 #include <asm/octeon/cvmx.h>
20 #include "cvmx-usb.h"
21 #include <asm/octeon/cvmx-iob-defs.h>
23 #include <linux/usb/hcd.h>
25 #include <linux/err.h>
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;
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)
37 return (struct octeon_hcd *)(hcd->hcd_priv);
40 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
42 return container_of((void *)p, struct usb_hcd, hcd_priv);
45 static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
47 return container_of(p, struct octeon_hcd, usb);
50 static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
52 struct octeon_hcd *priv = hcd_to_octeon(hcd);
53 unsigned long flags;
55 spin_lock_irqsave(&priv->lock, flags);
56 cvmx_usb_poll(&priv->usb);
57 spin_unlock_irqrestore(&priv->lock, flags);
58 return IRQ_HANDLED;
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)
69 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
71 spin_unlock(&priv->lock);
72 usb_hcd_poll_rh_status(octeon_to_hcd(priv));
73 spin_lock(&priv->lock);
76 static int octeon_usb_start(struct usb_hcd *hcd)
78 struct octeon_hcd *priv = hcd_to_octeon(hcd);
79 unsigned long flags;
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;
89 static void octeon_usb_stop(struct usb_hcd *hcd)
91 struct octeon_hcd *priv = hcd_to_octeon(hcd);
92 unsigned long flags;
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;
101 static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
103 struct octeon_hcd *priv = hcd_to_octeon(hcd);
105 return cvmx_usb_get_frame_number(&priv->usb);
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)
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;
121 urb->actual_length = bytes_transferred;
122 urb->hcpriv = NULL;
124 if (!list_empty(&urb->urb_list)) {
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.
131 list_del(&urb->urb_list);
132 /* No longer on the dequeue_list. */
133 INIT_LIST_HEAD(&urb->urb_list);
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;
141 * The pointer to the private list is stored in the setup_packet
142 * field.
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;
161 /* Free the private list now that we don't need it anymore */
162 kfree(iso_packet);
163 urb->setup_packet = NULL;
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;
198 spin_unlock(&priv->lock);
199 usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
200 spin_lock(&priv->lock);
203 static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
204 struct urb *urb,
205 gfp_t mem_flags)
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;
215 urb->status = 0;
216 INIT_LIST_HEAD(&urb->urb_list); /* not enqueued on dequeue_list */
217 spin_lock_irqsave(&priv->lock, flags);
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;
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;
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.
254 if (speed != CVMX_USB_SPEED_HIGH) {
256 * Start at this device and work our way up the usb
257 * tree.
259 struct usb_device *dev = urb->dev;
260 while (dev->parent) {
262 * If our parent is high speed then he'll
263 * receive the splits.
265 if (dev->parent->speed == USB_SPEED_HIGH) {
266 split_device = dev->parent->devnum;
267 split_port = dev->portnum;
268 break;
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.
276 dev = dev->parent;
279 pipe_handle = cvmx_usb_open_pipe(&priv->usb,
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;
296 ep->hcpriv = (void *)(0x10000L + pipe_handle);
297 } else {
298 pipe_handle = 0xffff & (long)ep->hcpriv;
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));
306 * Allocate a structure to use for our private list of
307 * isochronous packets.
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;
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.
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);
336 * If submit failed we need to free our private packet
337 * list.
339 if (submit_handle < 0) {
340 urb->setup_packet = NULL;
341 kfree(iso_packet);
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;
374 if (submit_handle < 0) {
375 spin_unlock_irqrestore(&priv->lock, flags);
376 dev_dbg(dev, "Failed to submit\n");
377 return -ENOMEM;
379 urb->hcpriv = (void *)(long)(((submit_handle & 0xffff) << 16) | pipe_handle);
380 spin_unlock_irqrestore(&priv->lock, flags);
381 return 0;
384 static void octeon_usb_urb_dequeue_work(unsigned long arg)
386 unsigned long flags;
387 struct octeon_hcd *priv = (struct octeon_hcd *)arg;
389 spin_lock_irqsave(&priv->lock, flags);
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);
403 spin_unlock_irqrestore(&priv->lock, flags);
406 static int octeon_usb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
408 struct octeon_hcd *priv = hcd_to_octeon(hcd);
409 unsigned long flags;
411 if (!urb->dev)
412 return -EINVAL;
414 spin_lock_irqsave(&priv->lock, flags);
416 urb->status = status;
417 list_add_tail(&urb->urb_list, &priv->dequeue_list);
419 spin_unlock_irqrestore(&priv->lock, flags);
421 tasklet_schedule(&priv->dequeue_tasklet);
423 return 0;
426 static void octeon_usb_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
428 struct device *dev = hcd->self.controller;
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;
443 static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
445 struct octeon_hcd *priv = hcd_to_octeon(hcd);
446 struct cvmx_usb_port_status port_status;
447 unsigned long flags;
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;
455 return (buf[0] != 0);
458 static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
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;
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;
478 break;
479 case ClearPortFeature:
480 dev_dbg(dev, "ClearPortFeature\n");
481 if (wIndex != 1) {
482 dev_dbg(dev, " INVALID\n");
483 return -EINVAL;
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");
515 * Clears the driver's internal Port Reset Change flag.
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");
524 * Clears the driver's internal Port Enable/Disable
525 * Change flag.
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");
534 * Clears the driver's internal Port Suspend Change
535 * flag, which is set when resume signaling on the host
536 * port is complete.
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;
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;
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;
579 if (usb_port_status.connect_change) {
580 port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
581 dev_dbg(dev, " C_CONNECTION\n");
584 if (usb_port_status.port_enabled) {
585 port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
586 dev_dbg(dev, " C_ENABLE\n");
589 if (usb_port_status.connected) {
590 port_status |= (1 << USB_PORT_FEAT_CONNECTION);
591 dev_dbg(dev, " CONNECTION\n");
594 if (usb_port_status.port_enabled) {
595 port_status |= (1 << USB_PORT_FEAT_ENABLE);
596 dev_dbg(dev, " ENABLE\n");
599 if (usb_port_status.port_over_current) {
600 port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
601 dev_dbg(dev, " OVER_CURRENT\n");
604 if (usb_port_status.port_powered) {
605 port_status |= (1 << USB_PORT_FEAT_POWER);
606 dev_dbg(dev, " POWER\n");
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");
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;
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;
653 break;
654 default:
655 dev_dbg(dev, "Unknown root hub request\n");
656 return -EINVAL;
658 return 0;
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,
679 static int octeon_usb_driver_probe(struct device *dev)
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;
689 * Set the DMA mask to 64bits so we get buffers already translated for
690 * DMA.
692 dev->coherent_dma_mask = ~0;
693 dev->dma_mask = &dev->coherent_dma_mask;
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;
700 hcd->uses_new_polling = 1;
701 priv = (struct octeon_hcd *)hcd->hcd_priv;
703 spin_lock_init(&priv->lock);
705 tasklet_init(&priv->dequeue_tasklet, octeon_usb_urb_dequeue_work, (unsigned long)priv);
706 INIT_LIST_HEAD(&priv->dequeue_list);
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;
715 /* This delay is needed for CN3010, but I don't know why... */
716 mdelay(10);
718 spin_lock_irqsave(&priv->lock, flags);
719 cvmx_usb_poll(&priv->usb);
720 spin_unlock_irqrestore(&priv->lock, flags);
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;
729 dev_dbg(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
731 return 0;
734 static int octeon_usb_driver_remove(struct device *dev)
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;
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);
749 kfree(hcd);
751 return 0;
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,
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)
767 int num_devices = cvmx_usb_get_num_ports();
768 int device;
770 if (usb_disabled() || num_devices == 0)
771 return -ENODEV;
773 if (driver_register(&octeon_usb_driver))
774 return -ENOMEM;
776 octeon_usb_registered = 1;
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.
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.
789 if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
790 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
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);
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);
811 if (device < MAX_USB_PORTS)
812 pdev_glob[device] = pdev;
815 return 0;
818 static void __exit octeon_usb_module_cleanup(void)
820 int i;
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;
827 if (octeon_usb_registered)
828 driver_unregister(&octeon_usb_driver);
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);