USB: FHCI: avoid NULL pointer dereference
[linux-2.6/linux-2.6-openrd.git] / drivers / usb / host / fhci-hcd.c
blob78e7c3cfcb7282757d85fc331de14ab559580e4c
1 /*
2 * Freescale QUICC Engine USB Host Controller Driver
4 * Copyright (c) Freescale Semicondutor, Inc. 2006.
5 * Shlomi Gridish <gridish@freescale.com>
6 * Jerry Huang <Chang-Ming.Huang@freescale.com>
7 * Copyright (c) Logic Product Development, Inc. 2007
8 * Peter Barada <peterb@logicpd.com>
9 * Copyright (c) MontaVista Software, Inc. 2008.
10 * Anton Vorontsov <avorontsov@ru.mvista.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
24 #include <linux/list.h>
25 #include <linux/interrupt.h>
26 #include <linux/io.h>
27 #include <linux/usb.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_gpio.h>
30 #include <asm/qe.h>
31 #include <asm/fsl_gtm.h>
32 #include "../core/hcd.h"
33 #include "fhci.h"
35 void fhci_start_sof_timer(struct fhci_hcd *fhci)
37 fhci_dbg(fhci, "-> %s\n", __func__);
39 /* clear frame_n */
40 out_be16(&fhci->pram->frame_num, 0);
42 out_be16(&fhci->regs->usb_sof_tmr, 0);
43 setbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
45 fhci_dbg(fhci, "<- %s\n", __func__);
48 void fhci_stop_sof_timer(struct fhci_hcd *fhci)
50 fhci_dbg(fhci, "-> %s\n", __func__);
52 clrbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
53 gtm_stop_timer16(fhci->timer);
55 fhci_dbg(fhci, "<- %s\n", __func__);
58 u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
60 return be16_to_cpu(in_be16(&usb->fhci->regs->usb_sof_tmr) / 12);
63 /* initialize the endpoint zero */
64 static u32 endpoint_zero_init(struct fhci_usb *usb,
65 enum fhci_mem_alloc data_mem,
66 u32 ring_len)
68 u32 rc;
70 rc = fhci_create_ep(usb, data_mem, ring_len);
71 if (rc)
72 return rc;
74 /* inilialize endpoint registers */
75 fhci_init_ep_registers(usb, usb->ep0, data_mem);
77 return 0;
80 /* enable the USB interrupts */
81 void fhci_usb_enable_interrupt(struct fhci_usb *usb)
83 struct fhci_hcd *fhci = usb->fhci;
85 if (usb->intr_nesting_cnt == 1) {
86 /* initialize the USB interrupt */
87 enable_irq(fhci_to_hcd(fhci)->irq);
89 /* initialize the event register and mask register */
90 out_be16(&usb->fhci->regs->usb_event, 0xffff);
91 out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
93 /* enable the timer interrupts */
94 enable_irq(fhci->timer->irq);
95 } else if (usb->intr_nesting_cnt > 1)
96 fhci_info(fhci, "unbalanced USB interrupts nesting\n");
97 usb->intr_nesting_cnt--;
100 /* diable the usb interrupt */
101 void fhci_usb_disable_interrupt(struct fhci_usb *usb)
103 struct fhci_hcd *fhci = usb->fhci;
105 if (usb->intr_nesting_cnt == 0) {
106 /* diable the timer interrupt */
107 disable_irq_nosync(fhci->timer->irq);
109 /* disable the usb interrupt */
110 disable_irq_nosync(fhci_to_hcd(fhci)->irq);
111 out_be16(&usb->fhci->regs->usb_mask, 0);
113 usb->intr_nesting_cnt++;
116 /* enable the USB controller */
117 static u32 fhci_usb_enable(struct fhci_hcd *fhci)
119 struct fhci_usb *usb = fhci->usb_lld;
121 out_be16(&usb->fhci->regs->usb_event, 0xffff);
122 out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
123 setbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
125 mdelay(100);
127 return 0;
130 /* disable the USB controller */
131 static u32 fhci_usb_disable(struct fhci_hcd *fhci)
133 struct fhci_usb *usb = fhci->usb_lld;
135 fhci_usb_disable_interrupt(usb);
136 fhci_port_disable(fhci);
138 /* disable the usb controller */
139 if (usb->port_status == FHCI_PORT_FULL ||
140 usb->port_status == FHCI_PORT_LOW)
141 fhci_device_disconnected_interrupt(fhci);
143 clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
145 return 0;
148 /* check the bus state by polling the QE bit on the IO ports */
149 int fhci_ioports_check_bus_state(struct fhci_hcd *fhci)
151 u8 bits = 0;
153 /* check USBOE,if transmitting,exit */
154 if (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
155 return -1;
157 /* check USBRP */
158 if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
159 bits |= 0x2;
161 /* check USBRN */
162 if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
163 bits |= 0x1;
165 return bits;
168 static void fhci_mem_free(struct fhci_hcd *fhci)
170 struct ed *ed;
171 struct ed *next_ed;
172 struct td *td;
173 struct td *next_td;
175 list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
176 list_del(&ed->node);
177 kfree(ed);
180 list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
181 list_del(&td->node);
182 kfree(td);
185 kfree(fhci->vroot_hub);
186 fhci->vroot_hub = NULL;
188 kfree(fhci->hc_list);
189 fhci->hc_list = NULL;
192 static int fhci_mem_init(struct fhci_hcd *fhci)
194 int i;
196 fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
197 if (!fhci->hc_list)
198 goto err;
200 INIT_LIST_HEAD(&fhci->hc_list->ctrl_list);
201 INIT_LIST_HEAD(&fhci->hc_list->bulk_list);
202 INIT_LIST_HEAD(&fhci->hc_list->iso_list);
203 INIT_LIST_HEAD(&fhci->hc_list->intr_list);
204 INIT_LIST_HEAD(&fhci->hc_list->done_list);
206 fhci->vroot_hub = kzalloc(sizeof(*fhci->vroot_hub), GFP_KERNEL);
207 if (!fhci->vroot_hub)
208 goto err;
210 INIT_LIST_HEAD(&fhci->empty_eds);
211 INIT_LIST_HEAD(&fhci->empty_tds);
213 /* initialize work queue to handle done list */
214 fhci_tasklet.data = (unsigned long)fhci;
215 fhci->process_done_task = &fhci_tasklet;
217 for (i = 0; i < MAX_TDS; i++) {
218 struct td *td;
220 td = kmalloc(sizeof(*td), GFP_KERNEL);
221 if (!td)
222 goto err;
223 fhci_recycle_empty_td(fhci, td);
225 for (i = 0; i < MAX_EDS; i++) {
226 struct ed *ed;
228 ed = kmalloc(sizeof(*ed), GFP_KERNEL);
229 if (!ed)
230 goto err;
231 fhci_recycle_empty_ed(fhci, ed);
234 fhci->active_urbs = 0;
235 return 0;
236 err:
237 fhci_mem_free(fhci);
238 return -ENOMEM;
241 /* destroy the fhci_usb structure */
242 static void fhci_usb_free(void *lld)
244 struct fhci_usb *usb = lld;
245 struct fhci_hcd *fhci;
247 if (usb) {
248 fhci = usb->fhci;
249 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
250 fhci_ep0_free(usb);
251 kfree(usb->actual_frame);
252 kfree(usb);
256 /* initialize the USB */
257 static int fhci_usb_init(struct fhci_hcd *fhci)
259 struct fhci_usb *usb = fhci->usb_lld;
261 memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
263 usb->port_status = FHCI_PORT_DISABLED;
264 usb->max_frame_usage = FRAME_TIME_USAGE;
265 usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
267 usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
268 if (!usb->actual_frame) {
269 fhci_usb_free(usb);
270 return -ENOMEM;
273 INIT_LIST_HEAD(&usb->actual_frame->tds_list);
275 /* initializing registers on chip, clear frame number */
276 out_be16(&fhci->pram->frame_num, 0);
278 /* clear rx state */
279 out_be32(&fhci->pram->rx_state, 0);
281 /* set mask register */
282 usb->saved_msk = (USB_E_TXB_MASK |
283 USB_E_TXE1_MASK |
284 USB_E_IDLE_MASK |
285 USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
287 out_8(&usb->fhci->regs->usb_mod, USB_MODE_HOST | USB_MODE_EN);
289 /* clearing the mask register */
290 out_be16(&usb->fhci->regs->usb_mask, 0);
292 /* initialing the event register */
293 out_be16(&usb->fhci->regs->usb_event, 0xffff);
295 if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
296 fhci_usb_free(usb);
297 return -EINVAL;
300 return 0;
303 /* initialize the fhci_usb struct and the corresponding data staruct */
304 static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
306 struct fhci_usb *usb;
308 /* allocate memory for SCC data structure */
309 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
310 if (!usb) {
311 fhci_err(fhci, "no memory for SCC data struct\n");
312 return NULL;
315 usb->fhci = fhci;
316 usb->hc_list = fhci->hc_list;
317 usb->vroot_hub = fhci->vroot_hub;
319 usb->transfer_confirm = fhci_transfer_confirm_callback;
321 return usb;
324 static int fhci_start(struct usb_hcd *hcd)
326 int ret;
327 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
329 ret = fhci_mem_init(fhci);
330 if (ret) {
331 fhci_err(fhci, "failed to allocate memory\n");
332 goto err;
335 fhci->usb_lld = fhci_create_lld(fhci);
336 if (!fhci->usb_lld) {
337 fhci_err(fhci, "low level driver config failed\n");
338 ret = -ENOMEM;
339 goto err;
342 ret = fhci_usb_init(fhci);
343 if (ret) {
344 fhci_err(fhci, "low level driver initialize failed\n");
345 goto err;
348 spin_lock_init(&fhci->lock);
350 /* connect the virtual root hub */
351 fhci->vroot_hub->dev_num = 1; /* this field may be needed to fix */
352 fhci->vroot_hub->hub.wHubStatus = 0;
353 fhci->vroot_hub->hub.wHubChange = 0;
354 fhci->vroot_hub->port.wPortStatus = 0;
355 fhci->vroot_hub->port.wPortChange = 0;
357 hcd->state = HC_STATE_RUNNING;
360 * From here on, khubd concurrently accesses the root
361 * hub; drivers will be talking to enumerated devices.
362 * (On restart paths, khubd already knows about the root
363 * hub and could find work as soon as we wrote FLAG_CF.)
365 * Before this point the HC was idle/ready. After, khubd
366 * and device drivers may start it running.
368 fhci_usb_enable(fhci);
369 return 0;
370 err:
371 fhci_mem_free(fhci);
372 return ret;
375 static void fhci_stop(struct usb_hcd *hcd)
377 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
379 fhci_usb_disable_interrupt(fhci->usb_lld);
380 fhci_usb_disable(fhci);
382 fhci_usb_free(fhci->usb_lld);
383 fhci->usb_lld = NULL;
384 fhci_mem_free(fhci);
387 static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
388 gfp_t mem_flags)
390 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
391 u32 pipe = urb->pipe;
392 int ret;
393 int i;
394 int size = 0;
395 struct urb_priv *urb_priv;
396 unsigned long flags;
398 switch (usb_pipetype(pipe)) {
399 case PIPE_CONTROL:
400 /* 1 td fro setup,1 for ack */
401 size = 2;
402 case PIPE_BULK:
403 /* one td for every 4096 bytes(can be upto 8k) */
404 size += urb->transfer_buffer_length / 4096;
405 /* ...add for any remaining bytes... */
406 if ((urb->transfer_buffer_length % 4096) != 0)
407 size++;
408 /* ..and maybe a zero length packet to wrap it up */
409 if (size == 0)
410 size++;
411 else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
412 && (urb->transfer_buffer_length
413 % usb_maxpacket(urb->dev, pipe,
414 usb_pipeout(pipe))) != 0)
415 size++;
416 break;
417 case PIPE_ISOCHRONOUS:
418 size = urb->number_of_packets;
419 if (size <= 0)
420 return -EINVAL;
421 for (i = 0; i < urb->number_of_packets; i++) {
422 urb->iso_frame_desc[i].actual_length = 0;
423 urb->iso_frame_desc[i].status = (u32) (-EXDEV);
425 break;
426 case PIPE_INTERRUPT:
427 size = 1;
430 /* allocate the private part of the URB */
431 urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
432 if (!urb_priv)
433 return -ENOMEM;
435 /* allocate the private part of the URB */
436 urb_priv->tds = kzalloc(size * sizeof(struct td), mem_flags);
437 if (!urb_priv->tds) {
438 kfree(urb_priv);
439 return -ENOMEM;
442 spin_lock_irqsave(&fhci->lock, flags);
444 ret = usb_hcd_link_urb_to_ep(hcd, urb);
445 if (ret)
446 goto err;
448 /* fill the private part of the URB */
449 urb_priv->num_of_tds = size;
451 urb->status = -EINPROGRESS;
452 urb->actual_length = 0;
453 urb->error_count = 0;
454 urb->hcpriv = urb_priv;
456 fhci_queue_urb(fhci, urb);
457 err:
458 if (ret) {
459 kfree(urb_priv->tds);
460 kfree(urb_priv);
462 spin_unlock_irqrestore(&fhci->lock, flags);
463 return ret;
466 /* dequeue FHCI URB */
467 static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
469 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
470 struct fhci_usb *usb = fhci->usb_lld;
471 int ret = -EINVAL;
472 unsigned long flags;
474 if (!urb || !urb->dev || !urb->dev->bus)
475 goto out;
477 spin_lock_irqsave(&fhci->lock, flags);
479 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
480 if (ret)
481 goto out2;
483 if (usb->port_status != FHCI_PORT_DISABLED) {
484 struct urb_priv *urb_priv;
487 * flag the urb's data for deletion in some upcoming
488 * SF interrupt's delete list processing
490 urb_priv = urb->hcpriv;
492 if (!urb_priv || (urb_priv->state == URB_DEL))
493 goto out2;
495 urb_priv->state = URB_DEL;
497 /* already pending? */
498 urb_priv->ed->state = FHCI_ED_URB_DEL;
499 } else {
500 fhci_urb_complete_free(fhci, urb);
503 out2:
504 spin_unlock_irqrestore(&fhci->lock, flags);
505 out:
506 return ret;
509 static void fhci_endpoint_disable(struct usb_hcd *hcd,
510 struct usb_host_endpoint *ep)
512 struct fhci_hcd *fhci;
513 struct ed *ed;
514 unsigned long flags;
516 fhci = hcd_to_fhci(hcd);
517 spin_lock_irqsave(&fhci->lock, flags);
518 ed = ep->hcpriv;
519 if (ed) {
520 while (ed->td_head != NULL) {
521 struct td *td = fhci_remove_td_from_ed(ed);
522 fhci_urb_complete_free(fhci, td->urb);
524 fhci_recycle_empty_ed(fhci, ed);
525 ep->hcpriv = NULL;
527 spin_unlock_irqrestore(&fhci->lock, flags);
530 static int fhci_get_frame_number(struct usb_hcd *hcd)
532 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
534 return get_frame_num(fhci);
537 static const struct hc_driver fhci_driver = {
538 .description = "fsl,usb-fhci",
539 .product_desc = "FHCI HOST Controller",
540 .hcd_priv_size = sizeof(struct fhci_hcd),
542 /* generic hardware linkage */
543 .irq = fhci_irq,
544 .flags = HCD_USB11 | HCD_MEMORY,
546 /* basic lifecycle operation */
547 .start = fhci_start,
548 .stop = fhci_stop,
550 /* managing i/o requests and associated device resources */
551 .urb_enqueue = fhci_urb_enqueue,
552 .urb_dequeue = fhci_urb_dequeue,
553 .endpoint_disable = fhci_endpoint_disable,
555 /* scheduling support */
556 .get_frame_number = fhci_get_frame_number,
558 /* root hub support */
559 .hub_status_data = fhci_hub_status_data,
560 .hub_control = fhci_hub_control,
563 static int __devinit of_fhci_probe(struct of_device *ofdev,
564 const struct of_device_id *ofid)
566 struct device *dev = &ofdev->dev;
567 struct device_node *node = ofdev->node;
568 struct usb_hcd *hcd;
569 struct fhci_hcd *fhci;
570 struct resource usb_regs;
571 unsigned long pram_addr;
572 unsigned int usb_irq;
573 const char *sprop;
574 const u32 *iprop;
575 int size;
576 int ret;
577 int i;
578 int j;
580 if (usb_disabled())
581 return -ENODEV;
583 sprop = of_get_property(node, "mode", NULL);
584 if (sprop && strcmp(sprop, "host"))
585 return -ENODEV;
587 hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
588 if (!hcd) {
589 dev_err(dev, "could not create hcd\n");
590 return -ENOMEM;
593 fhci = hcd_to_fhci(hcd);
594 hcd->self.controller = dev;
595 dev_set_drvdata(dev, hcd);
597 iprop = of_get_property(node, "hub-power-budget", &size);
598 if (iprop && size == sizeof(*iprop))
599 hcd->power_budget = *iprop;
601 /* FHCI registers. */
602 ret = of_address_to_resource(node, 0, &usb_regs);
603 if (ret) {
604 dev_err(dev, "could not get regs\n");
605 goto err_regs;
608 hcd->regs = ioremap(usb_regs.start, usb_regs.end - usb_regs.start + 1);
609 if (!hcd->regs) {
610 dev_err(dev, "could not ioremap regs\n");
611 ret = -ENOMEM;
612 goto err_regs;
614 fhci->regs = hcd->regs;
616 /* Parameter RAM. */
617 iprop = of_get_property(node, "reg", &size);
618 if (!iprop || size < sizeof(*iprop) * 4) {
619 dev_err(dev, "can't get pram offset\n");
620 ret = -EINVAL;
621 goto err_pram;
624 pram_addr = cpm_muram_alloc_fixed(iprop[2], FHCI_PRAM_SIZE);
625 if (IS_ERR_VALUE(pram_addr)) {
626 dev_err(dev, "failed to allocate usb pram\n");
627 ret = -ENOMEM;
628 goto err_pram;
630 fhci->pram = cpm_muram_addr(pram_addr);
632 /* GPIOs and pins */
633 for (i = 0; i < NUM_GPIOS; i++) {
634 int gpio;
635 enum of_gpio_flags flags;
637 gpio = of_get_gpio_flags(node, i, &flags);
638 fhci->gpios[i] = gpio;
639 fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
641 if (!gpio_is_valid(gpio)) {
642 if (i < GPIO_SPEED) {
643 dev_err(dev, "incorrect GPIO%d: %d\n",
644 i, gpio);
645 goto err_gpios;
646 } else {
647 dev_info(dev, "assuming board doesn't have "
648 "%s gpio\n", i == GPIO_SPEED ?
649 "speed" : "power");
650 continue;
654 ret = gpio_request(gpio, dev_name(dev));
655 if (ret) {
656 dev_err(dev, "failed to request gpio %d", i);
657 goto err_gpios;
660 if (i >= GPIO_SPEED) {
661 ret = gpio_direction_output(gpio, 0);
662 if (ret) {
663 dev_err(dev, "failed to set gpio %d as "
664 "an output\n", i);
665 i++;
666 goto err_gpios;
671 for (j = 0; j < NUM_PINS; j++) {
672 fhci->pins[j] = qe_pin_request(ofdev->node, j);
673 if (IS_ERR(fhci->pins[j])) {
674 ret = PTR_ERR(fhci->pins[j]);
675 dev_err(dev, "can't get pin %d: %d\n", j, ret);
676 goto err_pins;
680 /* Frame limit timer and its interrupt. */
681 fhci->timer = gtm_get_timer16();
682 if (IS_ERR(fhci->timer)) {
683 ret = PTR_ERR(fhci->timer);
684 dev_err(dev, "failed to request qe timer: %i", ret);
685 goto err_get_timer;
688 ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
689 IRQF_DISABLED, "qe timer (usb)", hcd);
690 if (ret) {
691 dev_err(dev, "failed to request timer irq");
692 goto err_timer_irq;
695 /* USB Host interrupt. */
696 usb_irq = irq_of_parse_and_map(node, 0);
697 if (usb_irq == NO_IRQ) {
698 dev_err(dev, "could not get usb irq\n");
699 ret = -EINVAL;
700 goto err_usb_irq;
703 /* Clocks. */
704 sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
705 if (sprop) {
706 fhci->fullspeed_clk = qe_clock_source(sprop);
707 if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
708 dev_err(dev, "wrong fullspeed-clock\n");
709 ret = -EINVAL;
710 goto err_clocks;
714 sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
715 if (sprop) {
716 fhci->lowspeed_clk = qe_clock_source(sprop);
717 if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
718 dev_err(dev, "wrong lowspeed-clock\n");
719 ret = -EINVAL;
720 goto err_clocks;
724 if (fhci->fullspeed_clk == QE_CLK_NONE &&
725 fhci->lowspeed_clk == QE_CLK_NONE) {
726 dev_err(dev, "no clocks specified\n");
727 ret = -EINVAL;
728 goto err_clocks;
731 dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
733 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
735 /* Start with full-speed, if possible. */
736 if (fhci->fullspeed_clk != QE_CLK_NONE) {
737 fhci_config_transceiver(fhci, FHCI_PORT_FULL);
738 qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
739 } else {
740 fhci_config_transceiver(fhci, FHCI_PORT_LOW);
741 qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
744 /* Clear and disable any pending interrupts. */
745 out_be16(&fhci->regs->usb_event, 0xffff);
746 out_be16(&fhci->regs->usb_mask, 0);
748 ret = usb_add_hcd(hcd, usb_irq, IRQF_DISABLED);
749 if (ret < 0)
750 goto err_add_hcd;
752 fhci_dfs_create(fhci);
754 return 0;
756 err_add_hcd:
757 err_clocks:
758 irq_dispose_mapping(usb_irq);
759 err_usb_irq:
760 free_irq(fhci->timer->irq, hcd);
761 err_timer_irq:
762 gtm_put_timer16(fhci->timer);
763 err_get_timer:
764 err_pins:
765 while (--j >= 0)
766 qe_pin_free(fhci->pins[j]);
767 err_gpios:
768 while (--i >= 0) {
769 if (gpio_is_valid(fhci->gpios[i]))
770 gpio_free(fhci->gpios[i]);
772 cpm_muram_free(pram_addr);
773 err_pram:
774 iounmap(hcd->regs);
775 err_regs:
776 usb_put_hcd(hcd);
777 return ret;
780 static int __devexit fhci_remove(struct device *dev)
782 struct usb_hcd *hcd = dev_get_drvdata(dev);
783 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
784 int i;
785 int j;
787 usb_remove_hcd(hcd);
788 free_irq(fhci->timer->irq, hcd);
789 gtm_put_timer16(fhci->timer);
790 cpm_muram_free(cpm_muram_offset(fhci->pram));
791 for (i = 0; i < NUM_GPIOS; i++) {
792 if (!gpio_is_valid(fhci->gpios[i]))
793 continue;
794 gpio_free(fhci->gpios[i]);
796 for (j = 0; j < NUM_PINS; j++)
797 qe_pin_free(fhci->pins[j]);
798 fhci_dfs_destroy(fhci);
799 usb_put_hcd(hcd);
800 return 0;
803 static int __devexit of_fhci_remove(struct of_device *ofdev)
805 return fhci_remove(&ofdev->dev);
808 static struct of_device_id of_fhci_match[] = {
809 { .compatible = "fsl,mpc8323-qe-usb", },
812 MODULE_DEVICE_TABLE(of, of_fhci_match);
814 static struct of_platform_driver of_fhci_driver = {
815 .name = "fsl,usb-fhci",
816 .match_table = of_fhci_match,
817 .probe = of_fhci_probe,
818 .remove = __devexit_p(of_fhci_remove),
821 static int __init fhci_module_init(void)
823 return of_register_platform_driver(&of_fhci_driver);
825 module_init(fhci_module_init);
827 static void __exit fhci_module_exit(void)
829 of_unregister_platform_driver(&of_fhci_driver);
831 module_exit(fhci_module_exit);
833 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
834 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
835 "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
836 "Anton Vorontsov <avorontsov@ru.mvista.com>");
837 MODULE_LICENSE("GPL");