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
6 * Copyright (C) 2008 Cavium Networks
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>
21 #include <asm/octeon/cvmx-iob-defs.h>
23 #include <linux/usb/hcd.h>
25 #include <linux/err.h>
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
);
55 spin_lock_irqsave(&priv
->lock
, flags
);
56 cvmx_usb_poll(&priv
->usb
);
57 spin_unlock_irqrestore(&priv
->lock
, flags
);
61 static void octeon_usb_port_callback(struct cvmx_usb_state
*usb
,
62 enum cvmx_usb_callback reason
,
63 enum cvmx_usb_complete status
,
66 int bytes_transferred
,
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
);
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
);
89 static void octeon_usb_stop(struct usb_hcd
*hcd
)
91 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
94 spin_lock_irqsave(&priv
->lock
, flags
);
95 cvmx_usb_register_callback(&priv
->usb
, CVMX_USB_CALLBACK_PORT_CHANGED
,
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
,
113 int bytes_transferred
,
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
;
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
) {
141 * The pointer to the private list is stored in the setup_packet
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
;
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 */
163 urb
->setup_packet
= NULL
;
167 case CVMX_USB_COMPLETE_SUCCESS
:
170 case CVMX_USB_COMPLETE_CANCEL
:
171 if (urb
->status
== 0)
172 urb
->status
= -ENOENT
;
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
;
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
;
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
;
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
;
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
,
207 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
208 struct device
*dev
= hcd
->self
.controller
;
209 int submit_handle
= -1;
212 struct cvmx_usb_iso_packet
*iso_packet
;
213 struct usb_host_endpoint
*ep
= urb
->ep
;
216 INIT_LIST_HEAD(&urb
->urb_list
); /* not enqueued on dequeue_list */
217 spin_lock_irqsave(&priv
->lock
, flags
);
220 enum cvmx_usb_transfer transfer_type
;
221 enum cvmx_usb_speed speed
;
222 int split_device
= 0;
224 switch (usb_pipetype(urb
->pipe
)) {
225 case PIPE_ISOCHRONOUS
:
226 transfer_type
= CVMX_USB_TRANSFER_ISOCHRONOUS
;
229 transfer_type
= CVMX_USB_TRANSFER_INTERRUPT
;
232 transfer_type
= CVMX_USB_TRANSFER_CONTROL
;
235 transfer_type
= CVMX_USB_TRANSFER_BULK
;
238 switch (urb
->dev
->speed
) {
240 speed
= CVMX_USB_SPEED_LOW
;
243 speed
= CVMX_USB_SPEED_FULL
;
246 speed
= CVMX_USB_SPEED_HIGH
;
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
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
;
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
279 pipe_handle
= cvmx_usb_open_pipe(&priv
->usb
,
281 usb_pipedevice(urb
->pipe
),
282 usb_pipeendpoint(urb
->pipe
),
284 le16_to_cpu(ep
->desc
.wMaxPacketSize
) & 0x7ff,
286 usb_pipein(urb
->pipe
) ? CVMX_USB_DIRECTION_IN
: CVMX_USB_DIRECTION_OUT
,
288 (le16_to_cpu(ep
->desc
.wMaxPacketSize
) >> 11) & 0x3,
291 if (pipe_handle
< 0) {
292 spin_unlock_irqrestore(&priv
->lock
, flags
);
293 dev_dbg(dev
, "Failed to create pipe\n");
296 ep
->hcpriv
= (void *)(0x10000L
+ pipe_handle
);
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
),
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
,
329 urb
->number_of_packets
,
332 urb
->transfer_buffer_length
,
333 octeon_usb_urb_complete_callback
,
336 * If submit failed we need to free our private packet
339 if (submit_handle
< 0) {
340 urb
->setup_packet
= NULL
;
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
,
350 urb
->transfer_buffer_length
,
351 octeon_usb_urb_complete_callback
,
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
,
360 urb
->transfer_buffer_length
,
361 octeon_usb_urb_complete_callback
,
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
,
369 urb
->transfer_buffer_length
,
370 octeon_usb_urb_complete_callback
,
374 if (submit_handle
< 0) {
375 spin_unlock_irqrestore(&priv
->lock
, flags
);
376 dev_dbg(dev
, "Failed to submit\n");
379 urb
->hcpriv
= (void *)(long)(((submit_handle
& 0xffff) << 16) | pipe_handle
);
380 spin_unlock_irqrestore(&priv
->lock
, flags
);
384 static void octeon_usb_urb_dequeue_work(unsigned long arg
)
387 struct octeon_hcd
*priv
= (struct octeon_hcd
*)arg
;
389 spin_lock_irqsave(&priv
->lock
, flags
);
391 while (!list_empty(&priv
->dequeue_list
)) {
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
);
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
);
426 static void octeon_usb_endpoint_disable(struct usb_hcd
*hcd
, struct usb_host_endpoint
*ep
)
428 struct device
*dev
= hcd
->self
.controller
;
431 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
432 int pipe_handle
= 0xffff & (long)ep
->hcpriv
;
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
);
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
;
449 spin_lock_irqsave(&priv
->lock
, flags
);
450 port_status
= cvmx_usb_get_status(&priv
->usb
);
451 spin_unlock_irqrestore(&priv
->lock
, flags
);
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
;
464 struct usb_hub_descriptor
*desc
;
468 case ClearHubFeature
:
469 dev_dbg(dev
, "ClearHubFeature\n");
471 case C_HUB_LOCAL_POWER
:
472 case C_HUB_OVER_CURRENT
:
473 /* Nothing required here */
479 case ClearPortFeature
:
480 dev_dbg(dev
, "ClearPortFeature\n");
482 dev_dbg(dev
, " INVALID\n");
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
);
493 case USB_PORT_FEAT_SUSPEND
:
494 dev_dbg(dev
, " SUSPEND\n");
495 /* Not supported on Octeon */
497 case USB_PORT_FEAT_POWER
:
498 dev_dbg(dev
, " POWER\n");
499 /* Not supported on Octeon */
501 case USB_PORT_FEAT_INDICATOR
:
502 dev_dbg(dev
, " INDICATOR\n");
503 /* Port inidicator not supported */
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
);
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
);
521 case USB_PORT_FEAT_C_ENABLE
:
522 dev_dbg(dev
, " C_ENABLE\n");
524 * Clears the driver's internal Port Enable/Disable
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
);
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
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
);
547 dev_dbg(dev
, " UNKNOWN\n");
551 case GetHubDescriptor
:
552 dev_dbg(dev
, "GetHubDescriptor\n");
553 desc
= (struct usb_hub_descriptor
*)buf
;
554 desc
->bDescLength
= 9;
555 desc
->bDescriptorType
= 0x29;
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;
564 dev_dbg(dev
, "GetHubStatus\n");
568 dev_dbg(dev
, "GetPortStatus\n");
570 dev_dbg(dev
, " INVALID\n");
574 spin_lock_irqsave(&priv
->lock
, flags
);
575 usb_port_status
= cvmx_usb_get_status(&priv
->usb
);
576 spin_unlock_irqrestore(&priv
->lock
, flags
);
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
);
620 dev_dbg(dev
, "SetHubFeature\n");
621 /* No HUB features supported */
624 dev_dbg(dev
, "SetPortFeature\n");
626 dev_dbg(dev
, " INVALID\n");
631 case USB_PORT_FEAT_SUSPEND
:
632 dev_dbg(dev
, " SUSPEND\n");
634 case USB_PORT_FEAT_POWER
:
635 dev_dbg(dev
, " POWER\n");
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
);
645 case USB_PORT_FEAT_INDICATOR
:
646 dev_dbg(dev
, " INDICATOR\n");
650 dev_dbg(dev
, " UNKNOWN\n");
655 dev_dbg(dev
, "Unknown root hub request\n");
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
)
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
;
689 * Set the DMA mask to 64bits so we get buffers already translated for
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
));
697 dev_dbg(dev
, "Failed to allocate memory for HCD\n");
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
);
710 dev_dbg(dev
, "USB initialization failed with %d\n", status
);
715 /* This delay is needed for CN3010, but I don't know why... */
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
);
724 dev_dbg(dev
, "USB add HCD failed with %d\n", status
);
729 dev_dbg(dev
, "Registered HCD for port %d on irq %d\n", usb_num
, irq
);
734 static int octeon_usb_driver_remove(struct device
*dev
)
737 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
738 struct octeon_hcd
*priv
= hcd_to_octeon(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
);
747 dev_dbg(dev
, "USB shutdown failed with %d\n", status
);
754 static struct device_driver octeon_usb_driver
= {
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();
770 if (usb_disabled() || num_devices
== 0)
773 if (driver_register(&octeon_usb_driver
))
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
;
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);
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
;
818 static void __exit
octeon_usb_module_cleanup(void)
822 for (i
= 0; i
< MAX_USB_PORTS
; i
++)
824 platform_device_unregister(pdev_glob
[i
]);
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
);