Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Freescale QUICC Engine USB Host Controller Driver
0004  *
0005  * Copyright (c) Freescale Semicondutor, Inc. 2006.
0006  *               Shlomi Gridish <gridish@freescale.com>
0007  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
0008  * Copyright (c) Logic Product Development, Inc. 2007
0009  *               Peter Barada <peterb@logicpd.com>
0010  * Copyright (c) MontaVista Software, Inc. 2008.
0011  *               Anton Vorontsov <avorontsov@ru.mvista.com>
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/types.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/kernel.h>
0018 #include <linux/delay.h>
0019 #include <linux/errno.h>
0020 #include <linux/list.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/io.h>
0023 #include <linux/usb.h>
0024 #include <linux/usb/hcd.h>
0025 #include <linux/of_address.h>
0026 #include <linux/of_irq.h>
0027 #include <linux/of_platform.h>
0028 #include <linux/of_gpio.h>
0029 #include <linux/slab.h>
0030 #include <soc/fsl/qe/qe.h>
0031 #include <asm/fsl_gtm.h>
0032 #include "fhci.h"
0033 
0034 void fhci_start_sof_timer(struct fhci_hcd *fhci)
0035 {
0036     fhci_dbg(fhci, "-> %s\n", __func__);
0037 
0038     /* clear frame_n */
0039     out_be16(&fhci->pram->frame_num, 0);
0040 
0041     out_be16(&fhci->regs->usb_ussft, 0);
0042     setbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
0043 
0044     fhci_dbg(fhci, "<- %s\n", __func__);
0045 }
0046 
0047 void fhci_stop_sof_timer(struct fhci_hcd *fhci)
0048 {
0049     fhci_dbg(fhci, "-> %s\n", __func__);
0050 
0051     clrbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
0052     gtm_stop_timer16(fhci->timer);
0053 
0054     fhci_dbg(fhci, "<- %s\n", __func__);
0055 }
0056 
0057 u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
0058 {
0059     return be16_to_cpu(in_be16(&usb->fhci->regs->usb_ussft) / 12);
0060 }
0061 
0062 /* initialize the endpoint zero */
0063 static u32 endpoint_zero_init(struct fhci_usb *usb,
0064                   enum fhci_mem_alloc data_mem,
0065                   u32 ring_len)
0066 {
0067     u32 rc;
0068 
0069     rc = fhci_create_ep(usb, data_mem, ring_len);
0070     if (rc)
0071         return rc;
0072 
0073     /* inilialize endpoint registers */
0074     fhci_init_ep_registers(usb, usb->ep0, data_mem);
0075 
0076     return 0;
0077 }
0078 
0079 /* enable the USB interrupts */
0080 void fhci_usb_enable_interrupt(struct fhci_usb *usb)
0081 {
0082     struct fhci_hcd *fhci = usb->fhci;
0083 
0084     if (usb->intr_nesting_cnt == 1) {
0085         /* initialize the USB interrupt */
0086         enable_irq(fhci_to_hcd(fhci)->irq);
0087 
0088         /* initialize the event register and mask register */
0089         out_be16(&usb->fhci->regs->usb_usber, 0xffff);
0090         out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
0091 
0092         /* enable the timer interrupts */
0093         enable_irq(fhci->timer->irq);
0094     } else if (usb->intr_nesting_cnt > 1)
0095         fhci_info(fhci, "unbalanced USB interrupts nesting\n");
0096     usb->intr_nesting_cnt--;
0097 }
0098 
0099 /* disable the usb interrupt */
0100 void fhci_usb_disable_interrupt(struct fhci_usb *usb)
0101 {
0102     struct fhci_hcd *fhci = usb->fhci;
0103 
0104     if (usb->intr_nesting_cnt == 0) {
0105         /* disable the timer interrupt */
0106         disable_irq_nosync(fhci->timer->irq);
0107 
0108         /* disable the usb interrupt */
0109         disable_irq_nosync(fhci_to_hcd(fhci)->irq);
0110         out_be16(&usb->fhci->regs->usb_usbmr, 0);
0111     }
0112     usb->intr_nesting_cnt++;
0113 }
0114 
0115 /* enable the USB controller */
0116 static u32 fhci_usb_enable(struct fhci_hcd *fhci)
0117 {
0118     struct fhci_usb *usb = fhci->usb_lld;
0119 
0120     out_be16(&usb->fhci->regs->usb_usber, 0xffff);
0121     out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
0122     setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
0123 
0124     mdelay(100);
0125 
0126     return 0;
0127 }
0128 
0129 /* disable the USB controller */
0130 static u32 fhci_usb_disable(struct fhci_hcd *fhci)
0131 {
0132     struct fhci_usb *usb = fhci->usb_lld;
0133 
0134     fhci_usb_disable_interrupt(usb);
0135     fhci_port_disable(fhci);
0136 
0137     /* disable the usb controller */
0138     if (usb->port_status == FHCI_PORT_FULL ||
0139             usb->port_status == FHCI_PORT_LOW)
0140         fhci_device_disconnected_interrupt(fhci);
0141 
0142     clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
0143 
0144     return 0;
0145 }
0146 
0147 /* check the bus state by polling the QE bit on the IO ports */
0148 int fhci_ioports_check_bus_state(struct fhci_hcd *fhci)
0149 {
0150     u8 bits = 0;
0151 
0152     /* check USBOE,if transmitting,exit */
0153     if (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
0154         return -1;
0155 
0156     /* check USBRP */
0157     if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
0158         bits |= 0x2;
0159 
0160     /* check USBRN */
0161     if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
0162         bits |= 0x1;
0163 
0164     return bits;
0165 }
0166 
0167 static void fhci_mem_free(struct fhci_hcd *fhci)
0168 {
0169     struct ed *ed;
0170     struct ed *next_ed;
0171     struct td *td;
0172     struct td *next_td;
0173 
0174     list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
0175         list_del(&ed->node);
0176         kfree(ed);
0177     }
0178 
0179     list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
0180         list_del(&td->node);
0181         kfree(td);
0182     }
0183 
0184     kfree(fhci->vroot_hub);
0185     fhci->vroot_hub = NULL;
0186 
0187     kfree(fhci->hc_list);
0188     fhci->hc_list = NULL;
0189 }
0190 
0191 static int fhci_mem_init(struct fhci_hcd *fhci)
0192 {
0193     int i;
0194 
0195     fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
0196     if (!fhci->hc_list)
0197         goto err;
0198 
0199     INIT_LIST_HEAD(&fhci->hc_list->ctrl_list);
0200     INIT_LIST_HEAD(&fhci->hc_list->bulk_list);
0201     INIT_LIST_HEAD(&fhci->hc_list->iso_list);
0202     INIT_LIST_HEAD(&fhci->hc_list->intr_list);
0203     INIT_LIST_HEAD(&fhci->hc_list->done_list);
0204 
0205     fhci->vroot_hub = kzalloc(sizeof(*fhci->vroot_hub), GFP_KERNEL);
0206     if (!fhci->vroot_hub)
0207         goto err;
0208 
0209     INIT_LIST_HEAD(&fhci->empty_eds);
0210     INIT_LIST_HEAD(&fhci->empty_tds);
0211 
0212     /* initialize work queue to handle done list */
0213     fhci_tasklet.data = (unsigned long)fhci;
0214     fhci->process_done_task = &fhci_tasklet;
0215 
0216     for (i = 0; i < MAX_TDS; i++) {
0217         struct td *td;
0218 
0219         td = kmalloc(sizeof(*td), GFP_KERNEL);
0220         if (!td)
0221             goto err;
0222         fhci_recycle_empty_td(fhci, td);
0223     }
0224     for (i = 0; i < MAX_EDS; i++) {
0225         struct ed *ed;
0226 
0227         ed = kmalloc(sizeof(*ed), GFP_KERNEL);
0228         if (!ed)
0229             goto err;
0230         fhci_recycle_empty_ed(fhci, ed);
0231     }
0232 
0233     fhci->active_urbs = 0;
0234     return 0;
0235 err:
0236     fhci_mem_free(fhci);
0237     return -ENOMEM;
0238 }
0239 
0240 /* destroy the fhci_usb structure */
0241 static void fhci_usb_free(void *lld)
0242 {
0243     struct fhci_usb *usb = lld;
0244     struct fhci_hcd *fhci;
0245 
0246     if (usb) {
0247         fhci = usb->fhci;
0248         fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
0249         fhci_ep0_free(usb);
0250         kfree(usb->actual_frame);
0251         kfree(usb);
0252     }
0253 }
0254 
0255 /* initialize the USB */
0256 static int fhci_usb_init(struct fhci_hcd *fhci)
0257 {
0258     struct fhci_usb *usb = fhci->usb_lld;
0259 
0260     memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
0261 
0262     usb->port_status = FHCI_PORT_DISABLED;
0263     usb->max_frame_usage = FRAME_TIME_USAGE;
0264     usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
0265 
0266     usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
0267     if (!usb->actual_frame) {
0268         fhci_usb_free(usb);
0269         return -ENOMEM;
0270     }
0271 
0272     INIT_LIST_HEAD(&usb->actual_frame->tds_list);
0273 
0274     /* initializing registers on chip, clear frame number */
0275     out_be16(&fhci->pram->frame_num, 0);
0276 
0277     /* clear rx state */
0278     out_be32(&fhci->pram->rx_state, 0);
0279 
0280     /* set mask register */
0281     usb->saved_msk = (USB_E_TXB_MASK |
0282               USB_E_TXE1_MASK |
0283               USB_E_IDLE_MASK |
0284               USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
0285 
0286     out_8(&usb->fhci->regs->usb_usmod, USB_MODE_HOST | USB_MODE_EN);
0287 
0288     /* clearing the mask register */
0289     out_be16(&usb->fhci->regs->usb_usbmr, 0);
0290 
0291     /* initialing the event register */
0292     out_be16(&usb->fhci->regs->usb_usber, 0xffff);
0293 
0294     if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
0295         fhci_usb_free(usb);
0296         return -EINVAL;
0297     }
0298 
0299     return 0;
0300 }
0301 
0302 /* initialize the fhci_usb struct and the corresponding data staruct */
0303 static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
0304 {
0305     struct fhci_usb *usb;
0306 
0307     /* allocate memory for SCC data structure */
0308     usb = kzalloc(sizeof(*usb), GFP_KERNEL);
0309     if (!usb)
0310         return NULL;
0311 
0312     usb->fhci = fhci;
0313     usb->hc_list = fhci->hc_list;
0314     usb->vroot_hub = fhci->vroot_hub;
0315 
0316     usb->transfer_confirm = fhci_transfer_confirm_callback;
0317 
0318     return usb;
0319 }
0320 
0321 static int fhci_start(struct usb_hcd *hcd)
0322 {
0323     int ret;
0324     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0325 
0326     ret = fhci_mem_init(fhci);
0327     if (ret) {
0328         fhci_err(fhci, "failed to allocate memory\n");
0329         goto err;
0330     }
0331 
0332     fhci->usb_lld = fhci_create_lld(fhci);
0333     if (!fhci->usb_lld) {
0334         fhci_err(fhci, "low level driver config failed\n");
0335         ret = -ENOMEM;
0336         goto err;
0337     }
0338 
0339     ret = fhci_usb_init(fhci);
0340     if (ret) {
0341         fhci_err(fhci, "low level driver initialize failed\n");
0342         goto err;
0343     }
0344 
0345     spin_lock_init(&fhci->lock);
0346 
0347     /* connect the virtual root hub */
0348     fhci->vroot_hub->dev_num = 1;   /* this field may be needed to fix */
0349     fhci->vroot_hub->hub.wHubStatus = 0;
0350     fhci->vroot_hub->hub.wHubChange = 0;
0351     fhci->vroot_hub->port.wPortStatus = 0;
0352     fhci->vroot_hub->port.wPortChange = 0;
0353 
0354     hcd->state = HC_STATE_RUNNING;
0355 
0356     /*
0357      * From here on, hub_wq concurrently accesses the root
0358      * hub; drivers will be talking to enumerated devices.
0359      * (On restart paths, hub_wq already knows about the root
0360      * hub and could find work as soon as we wrote FLAG_CF.)
0361      *
0362      * Before this point the HC was idle/ready.  After, hub_wq
0363      * and device drivers may start it running.
0364      */
0365     fhci_usb_enable(fhci);
0366     return 0;
0367 err:
0368     fhci_mem_free(fhci);
0369     return ret;
0370 }
0371 
0372 static void fhci_stop(struct usb_hcd *hcd)
0373 {
0374     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0375 
0376     fhci_usb_disable_interrupt(fhci->usb_lld);
0377     fhci_usb_disable(fhci);
0378 
0379     fhci_usb_free(fhci->usb_lld);
0380     fhci->usb_lld = NULL;
0381     fhci_mem_free(fhci);
0382 }
0383 
0384 static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
0385                 gfp_t mem_flags)
0386 {
0387     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0388     u32 pipe = urb->pipe;
0389     int ret;
0390     int i;
0391     int size = 0;
0392     struct urb_priv *urb_priv;
0393     unsigned long flags;
0394 
0395     switch (usb_pipetype(pipe)) {
0396     case PIPE_CONTROL:
0397         /* 1 td fro setup,1 for ack */
0398         size = 2;
0399         fallthrough;
0400     case PIPE_BULK:
0401         /* one td for every 4096 bytes(can be up to 8k) */
0402         size += urb->transfer_buffer_length / 4096;
0403         /* ...add for any remaining bytes... */
0404         if ((urb->transfer_buffer_length % 4096) != 0)
0405             size++;
0406         /* ..and maybe a zero length packet to wrap it up */
0407         if (size == 0)
0408             size++;
0409         else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
0410              && (urb->transfer_buffer_length
0411                  % usb_maxpacket(urb->dev, pipe)) != 0)
0412             size++;
0413         break;
0414     case PIPE_ISOCHRONOUS:
0415         size = urb->number_of_packets;
0416         if (size <= 0)
0417             return -EINVAL;
0418         for (i = 0; i < urb->number_of_packets; i++) {
0419             urb->iso_frame_desc[i].actual_length = 0;
0420             urb->iso_frame_desc[i].status = (u32) (-EXDEV);
0421         }
0422         break;
0423     case PIPE_INTERRUPT:
0424         size = 1;
0425     }
0426 
0427     /* allocate the private part of the URB */
0428     urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
0429     if (!urb_priv)
0430         return -ENOMEM;
0431 
0432     /* allocate the private part of the URB */
0433     urb_priv->tds = kcalloc(size, sizeof(*urb_priv->tds), mem_flags);
0434     if (!urb_priv->tds) {
0435         kfree(urb_priv);
0436         return -ENOMEM;
0437     }
0438 
0439     spin_lock_irqsave(&fhci->lock, flags);
0440 
0441     ret = usb_hcd_link_urb_to_ep(hcd, urb);
0442     if (ret)
0443         goto err;
0444 
0445     /* fill the private part of the URB */
0446     urb_priv->num_of_tds = size;
0447 
0448     urb->status = -EINPROGRESS;
0449     urb->actual_length = 0;
0450     urb->error_count = 0;
0451     urb->hcpriv = urb_priv;
0452 
0453     fhci_queue_urb(fhci, urb);
0454 err:
0455     if (ret) {
0456         kfree(urb_priv->tds);
0457         kfree(urb_priv);
0458     }
0459     spin_unlock_irqrestore(&fhci->lock, flags);
0460     return ret;
0461 }
0462 
0463 /* dequeue FHCI URB */
0464 static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
0465 {
0466     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0467     struct fhci_usb *usb = fhci->usb_lld;
0468     int ret = -EINVAL;
0469     unsigned long flags;
0470 
0471     if (!urb || !urb->dev || !urb->dev->bus)
0472         goto out;
0473 
0474     spin_lock_irqsave(&fhci->lock, flags);
0475 
0476     ret = usb_hcd_check_unlink_urb(hcd, urb, status);
0477     if (ret)
0478         goto out2;
0479 
0480     if (usb->port_status != FHCI_PORT_DISABLED) {
0481         struct urb_priv *urb_priv;
0482 
0483         /*
0484          * flag the urb's data for deletion in some upcoming
0485          * SF interrupt's delete list processing
0486          */
0487         urb_priv = urb->hcpriv;
0488 
0489         if (!urb_priv || (urb_priv->state == URB_DEL))
0490             goto out2;
0491 
0492         urb_priv->state = URB_DEL;
0493 
0494         /* already pending? */
0495         urb_priv->ed->state = FHCI_ED_URB_DEL;
0496     } else {
0497         fhci_urb_complete_free(fhci, urb);
0498     }
0499 
0500 out2:
0501     spin_unlock_irqrestore(&fhci->lock, flags);
0502 out:
0503     return ret;
0504 }
0505 
0506 static void fhci_endpoint_disable(struct usb_hcd *hcd,
0507                   struct usb_host_endpoint *ep)
0508 {
0509     struct fhci_hcd *fhci;
0510     struct ed *ed;
0511     unsigned long flags;
0512 
0513     fhci = hcd_to_fhci(hcd);
0514     spin_lock_irqsave(&fhci->lock, flags);
0515     ed = ep->hcpriv;
0516     if (ed) {
0517         while (ed->td_head != NULL) {
0518             struct td *td = fhci_remove_td_from_ed(ed);
0519             fhci_urb_complete_free(fhci, td->urb);
0520         }
0521         fhci_recycle_empty_ed(fhci, ed);
0522         ep->hcpriv = NULL;
0523     }
0524     spin_unlock_irqrestore(&fhci->lock, flags);
0525 }
0526 
0527 static int fhci_get_frame_number(struct usb_hcd *hcd)
0528 {
0529     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0530 
0531     return get_frame_num(fhci);
0532 }
0533 
0534 static const struct hc_driver fhci_driver = {
0535     .description = "fsl,usb-fhci",
0536     .product_desc = "FHCI HOST Controller",
0537     .hcd_priv_size = sizeof(struct fhci_hcd),
0538 
0539     /* generic hardware linkage */
0540     .irq = fhci_irq,
0541     .flags = HCD_DMA | HCD_USB11 | HCD_MEMORY,
0542 
0543     /* basic lifecycle operation */
0544     .start = fhci_start,
0545     .stop = fhci_stop,
0546 
0547     /* managing i/o requests and associated device resources */
0548     .urb_enqueue = fhci_urb_enqueue,
0549     .urb_dequeue = fhci_urb_dequeue,
0550     .endpoint_disable = fhci_endpoint_disable,
0551 
0552     /* scheduling support */
0553     .get_frame_number = fhci_get_frame_number,
0554 
0555     /* root hub support */
0556     .hub_status_data = fhci_hub_status_data,
0557     .hub_control = fhci_hub_control,
0558 };
0559 
0560 static int of_fhci_probe(struct platform_device *ofdev)
0561 {
0562     struct device *dev = &ofdev->dev;
0563     struct device_node *node = dev->of_node;
0564     struct usb_hcd *hcd;
0565     struct fhci_hcd *fhci;
0566     struct resource usb_regs;
0567     unsigned long pram_addr;
0568     unsigned int usb_irq;
0569     const char *sprop;
0570     const u32 *iprop;
0571     int size;
0572     int ret;
0573     int i;
0574     int j;
0575 
0576     if (usb_disabled())
0577         return -ENODEV;
0578 
0579     sprop = of_get_property(node, "mode", NULL);
0580     if (sprop && strcmp(sprop, "host"))
0581         return -ENODEV;
0582 
0583     hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
0584     if (!hcd) {
0585         dev_err(dev, "could not create hcd\n");
0586         return -ENOMEM;
0587     }
0588 
0589     fhci = hcd_to_fhci(hcd);
0590     hcd->self.controller = dev;
0591     dev_set_drvdata(dev, hcd);
0592 
0593     iprop = of_get_property(node, "hub-power-budget", &size);
0594     if (iprop && size == sizeof(*iprop))
0595         hcd->power_budget = *iprop;
0596 
0597     /* FHCI registers. */
0598     ret = of_address_to_resource(node, 0, &usb_regs);
0599     if (ret) {
0600         dev_err(dev, "could not get regs\n");
0601         goto err_regs;
0602     }
0603 
0604     hcd->regs = ioremap(usb_regs.start, resource_size(&usb_regs));
0605     if (!hcd->regs) {
0606         dev_err(dev, "could not ioremap regs\n");
0607         ret = -ENOMEM;
0608         goto err_regs;
0609     }
0610     fhci->regs = hcd->regs;
0611 
0612     /* Parameter RAM. */
0613     iprop = of_get_property(node, "reg", &size);
0614     if (!iprop || size < sizeof(*iprop) * 4) {
0615         dev_err(dev, "can't get pram offset\n");
0616         ret = -EINVAL;
0617         goto err_pram;
0618     }
0619 
0620     pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
0621     if (IS_ERR_VALUE(pram_addr)) {
0622         dev_err(dev, "failed to allocate usb pram\n");
0623         ret = -ENOMEM;
0624         goto err_pram;
0625     }
0626 
0627     qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
0628              QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
0629     fhci->pram = cpm_muram_addr(pram_addr);
0630 
0631     /* GPIOs and pins */
0632     for (i = 0; i < NUM_GPIOS; i++) {
0633         int gpio;
0634         enum of_gpio_flags flags;
0635 
0636         gpio = of_get_gpio_flags(node, i, &flags);
0637         fhci->gpios[i] = gpio;
0638         fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
0639 
0640         if (!gpio_is_valid(gpio)) {
0641             if (i < GPIO_SPEED) {
0642                 dev_err(dev, "incorrect GPIO%d: %d\n",
0643                     i, gpio);
0644                 goto err_gpios;
0645             } else {
0646                 dev_info(dev, "assuming board doesn't have "
0647                     "%s gpio\n", i == GPIO_SPEED ?
0648                     "speed" : "power");
0649                 continue;
0650             }
0651         }
0652 
0653         ret = gpio_request(gpio, dev_name(dev));
0654         if (ret) {
0655             dev_err(dev, "failed to request gpio %d", i);
0656             goto err_gpios;
0657         }
0658 
0659         if (i >= GPIO_SPEED) {
0660             ret = gpio_direction_output(gpio, 0);
0661             if (ret) {
0662                 dev_err(dev, "failed to set gpio %d as "
0663                     "an output\n", i);
0664                 i++;
0665                 goto err_gpios;
0666             }
0667         }
0668     }
0669 
0670     for (j = 0; j < NUM_PINS; j++) {
0671         fhci->pins[j] = qe_pin_request(node, j);
0672         if (IS_ERR(fhci->pins[j])) {
0673             ret = PTR_ERR(fhci->pins[j]);
0674             dev_err(dev, "can't get pin %d: %d\n", j, ret);
0675             goto err_pins;
0676         }
0677     }
0678 
0679     /* Frame limit timer and its interrupt. */
0680     fhci->timer = gtm_get_timer16();
0681     if (IS_ERR(fhci->timer)) {
0682         ret = PTR_ERR(fhci->timer);
0683         dev_err(dev, "failed to request qe timer: %i", ret);
0684         goto err_get_timer;
0685     }
0686 
0687     ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
0688               0, "qe timer (usb)", hcd);
0689     if (ret) {
0690         dev_err(dev, "failed to request timer irq");
0691         goto err_timer_irq;
0692     }
0693 
0694     /* USB Host interrupt. */
0695     usb_irq = irq_of_parse_and_map(node, 0);
0696     if (usb_irq == NO_IRQ) {
0697         dev_err(dev, "could not get usb irq\n");
0698         ret = -EINVAL;
0699         goto err_usb_irq;
0700     }
0701 
0702     /* Clocks. */
0703     sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
0704     if (sprop) {
0705         fhci->fullspeed_clk = qe_clock_source(sprop);
0706         if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
0707             dev_err(dev, "wrong fullspeed-clock\n");
0708             ret = -EINVAL;
0709             goto err_clocks;
0710         }
0711     }
0712 
0713     sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
0714     if (sprop) {
0715         fhci->lowspeed_clk = qe_clock_source(sprop);
0716         if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
0717             dev_err(dev, "wrong lowspeed-clock\n");
0718             ret = -EINVAL;
0719             goto err_clocks;
0720         }
0721     }
0722 
0723     if (fhci->fullspeed_clk == QE_CLK_NONE &&
0724             fhci->lowspeed_clk == QE_CLK_NONE) {
0725         dev_err(dev, "no clocks specified\n");
0726         ret = -EINVAL;
0727         goto err_clocks;
0728     }
0729 
0730     dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
0731 
0732     fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
0733 
0734     /* Start with full-speed, if possible. */
0735     if (fhci->fullspeed_clk != QE_CLK_NONE) {
0736         fhci_config_transceiver(fhci, FHCI_PORT_FULL);
0737         qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
0738     } else {
0739         fhci_config_transceiver(fhci, FHCI_PORT_LOW);
0740         qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
0741     }
0742 
0743     /* Clear and disable any pending interrupts. */
0744     out_be16(&fhci->regs->usb_usber, 0xffff);
0745     out_be16(&fhci->regs->usb_usbmr, 0);
0746 
0747     ret = usb_add_hcd(hcd, usb_irq, 0);
0748     if (ret < 0)
0749         goto err_add_hcd;
0750 
0751     device_wakeup_enable(hcd->self.controller);
0752 
0753     fhci_dfs_create(fhci);
0754 
0755     return 0;
0756 
0757 err_add_hcd:
0758 err_clocks:
0759     irq_dispose_mapping(usb_irq);
0760 err_usb_irq:
0761     free_irq(fhci->timer->irq, hcd);
0762 err_timer_irq:
0763     gtm_put_timer16(fhci->timer);
0764 err_get_timer:
0765 err_pins:
0766     while (--j >= 0)
0767         qe_pin_free(fhci->pins[j]);
0768 err_gpios:
0769     while (--i >= 0) {
0770         if (gpio_is_valid(fhci->gpios[i]))
0771             gpio_free(fhci->gpios[i]);
0772     }
0773     cpm_muram_free(pram_addr);
0774 err_pram:
0775     iounmap(hcd->regs);
0776 err_regs:
0777     usb_put_hcd(hcd);
0778     return ret;
0779 }
0780 
0781 static int fhci_remove(struct device *dev)
0782 {
0783     struct usb_hcd *hcd = dev_get_drvdata(dev);
0784     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0785     int i;
0786     int j;
0787 
0788     usb_remove_hcd(hcd);
0789     free_irq(fhci->timer->irq, hcd);
0790     gtm_put_timer16(fhci->timer);
0791     cpm_muram_free(cpm_muram_offset(fhci->pram));
0792     for (i = 0; i < NUM_GPIOS; i++) {
0793         if (!gpio_is_valid(fhci->gpios[i]))
0794             continue;
0795         gpio_free(fhci->gpios[i]);
0796     }
0797     for (j = 0; j < NUM_PINS; j++)
0798         qe_pin_free(fhci->pins[j]);
0799     fhci_dfs_destroy(fhci);
0800     usb_put_hcd(hcd);
0801     return 0;
0802 }
0803 
0804 static int of_fhci_remove(struct platform_device *ofdev)
0805 {
0806     return fhci_remove(&ofdev->dev);
0807 }
0808 
0809 static const struct of_device_id of_fhci_match[] = {
0810     { .compatible = "fsl,mpc8323-qe-usb", },
0811     {},
0812 };
0813 MODULE_DEVICE_TABLE(of, of_fhci_match);
0814 
0815 static struct platform_driver of_fhci_driver = {
0816     .driver = {
0817         .name = "fsl,usb-fhci",
0818         .of_match_table = of_fhci_match,
0819     },
0820     .probe      = of_fhci_probe,
0821     .remove     = of_fhci_remove,
0822 };
0823 
0824 module_platform_driver(of_fhci_driver);
0825 
0826 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
0827 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
0828           "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
0829           "Anton Vorontsov <avorontsov@ru.mvista.com>");
0830 MODULE_LICENSE("GPL");