xref: /linux/drivers/usb/host/fhci-hcd.c (revision 03cd4fd620bdfc33ea25f2f7504f0d49c7dd1f9e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale QUICC Engine USB Host Controller Driver
4  *
5  * Copyright (c) Freescale Semicondutor, Inc. 2006.
6  *               Shlomi Gridish <gridish@freescale.com>
7  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
8  * Copyright (c) Logic Product Development, Inc. 2007
9  *               Peter Barada <peterb@logicpd.com>
10  * Copyright (c) MontaVista Software, Inc. 2008.
11  *               Anton Vorontsov <avorontsov@ru.mvista.com>
12  */
13 
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/spinlock.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/gpio/consumer.h>
31 #include <soc/fsl/qe/qe.h>
32 #include <asm/fsl_gtm.h>
33 #include "fhci.h"
34 
35 void fhci_start_sof_timer(struct fhci_hcd *fhci)
36 {
37 	fhci_dbg(fhci, "-> %s\n", __func__);
38 
39 	/* clear frame_n */
40 	out_be16(&fhci->pram->frame_num, 0);
41 
42 	out_be16(&fhci->regs->usb_ussft, 0);
43 	setbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
44 
45 	fhci_dbg(fhci, "<- %s\n", __func__);
46 }
47 
48 void fhci_stop_sof_timer(struct fhci_hcd *fhci)
49 {
50 	fhci_dbg(fhci, "-> %s\n", __func__);
51 
52 	clrbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
53 	gtm_stop_timer16(fhci->timer);
54 
55 	fhci_dbg(fhci, "<- %s\n", __func__);
56 }
57 
58 u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
59 {
60 	return be16_to_cpu(in_be16(&usb->fhci->regs->usb_ussft) / 12);
61 }
62 
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)
67 {
68 	u32 rc;
69 
70 	rc = fhci_create_ep(usb, data_mem, ring_len);
71 	if (rc)
72 		return rc;
73 
74 	/* inilialize endpoint registers */
75 	fhci_init_ep_registers(usb, usb->ep0, data_mem);
76 
77 	return 0;
78 }
79 
80 /* enable the USB interrupts */
81 void fhci_usb_enable_interrupt(struct fhci_usb *usb)
82 {
83 	struct fhci_hcd *fhci = usb->fhci;
84 
85 	if (usb->intr_nesting_cnt == 1) {
86 		/* initialize the USB interrupt */
87 		enable_irq(fhci_to_hcd(fhci)->irq);
88 
89 		/* initialize the event register and mask register */
90 		out_be16(&usb->fhci->regs->usb_usber, 0xffff);
91 		out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
92 
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--;
98 }
99 
100 /* disable the usb interrupt */
101 void fhci_usb_disable_interrupt(struct fhci_usb *usb)
102 {
103 	struct fhci_hcd *fhci = usb->fhci;
104 
105 	if (usb->intr_nesting_cnt == 0) {
106 		/* disable the timer interrupt */
107 		disable_irq_nosync(fhci->timer->irq);
108 
109 		/* disable the usb interrupt */
110 		disable_irq_nosync(fhci_to_hcd(fhci)->irq);
111 		out_be16(&usb->fhci->regs->usb_usbmr, 0);
112 	}
113 	usb->intr_nesting_cnt++;
114 }
115 
116 /* enable the USB controller */
117 static u32 fhci_usb_enable(struct fhci_hcd *fhci)
118 {
119 	struct fhci_usb *usb = fhci->usb_lld;
120 
121 	out_be16(&usb->fhci->regs->usb_usber, 0xffff);
122 	out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
123 	setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
124 
125 	mdelay(100);
126 
127 	return 0;
128 }
129 
130 /* disable the USB controller */
131 static u32 fhci_usb_disable(struct fhci_hcd *fhci)
132 {
133 	struct fhci_usb *usb = fhci->usb_lld;
134 
135 	fhci_usb_disable_interrupt(usb);
136 	fhci_port_disable(fhci);
137 
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);
142 
143 	clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
144 
145 	return 0;
146 }
147 
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)
150 {
151 	u8 bits = 0;
152 
153 	/* check USBOE,if transmitting,exit */
154 	if (!gpiod_get_value(fhci->gpiods[GPIO_USBOE]))
155 		return -1;
156 
157 	/* check USBRP */
158 	if (gpiod_get_value(fhci->gpiods[GPIO_USBRP]))
159 		bits |= 0x2;
160 
161 	/* check USBRN */
162 	if (gpiod_get_value(fhci->gpiods[GPIO_USBRN]))
163 		bits |= 0x1;
164 
165 	return bits;
166 }
167 
168 static void fhci_mem_free(struct fhci_hcd *fhci)
169 {
170 	struct ed *ed;
171 	struct ed *next_ed;
172 	struct td *td;
173 	struct td *next_td;
174 
175 	list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
176 		list_del(&ed->node);
177 		kfree(ed);
178 	}
179 
180 	list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
181 		list_del(&td->node);
182 		kfree(td);
183 	}
184 
185 	kfree(fhci->vroot_hub);
186 	fhci->vroot_hub = NULL;
187 
188 	kfree(fhci->hc_list);
189 	fhci->hc_list = NULL;
190 }
191 
192 static int fhci_mem_init(struct fhci_hcd *fhci)
193 {
194 	int i;
195 
196 	fhci->hc_list = kzalloc_obj(*fhci->hc_list);
197 	if (!fhci->hc_list)
198 		goto err;
199 
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);
205 
206 	fhci->vroot_hub = kzalloc_obj(*fhci->vroot_hub);
207 	if (!fhci->vroot_hub)
208 		goto err;
209 
210 	INIT_LIST_HEAD(&fhci->empty_eds);
211 	INIT_LIST_HEAD(&fhci->empty_tds);
212 
213 	/* initialize work queue to handle done list */
214 	fhci_tasklet.data = (unsigned long)fhci;
215 	fhci->process_done_task = &fhci_tasklet;
216 
217 	for (i = 0; i < MAX_TDS; i++) {
218 		struct td *td;
219 
220 		td = kmalloc_obj(*td);
221 		if (!td)
222 			goto err;
223 		fhci_recycle_empty_td(fhci, td);
224 	}
225 	for (i = 0; i < MAX_EDS; i++) {
226 		struct ed *ed;
227 
228 		ed = kmalloc_obj(*ed);
229 		if (!ed)
230 			goto err;
231 		fhci_recycle_empty_ed(fhci, ed);
232 	}
233 
234 	fhci->active_urbs = 0;
235 	return 0;
236 err:
237 	fhci_mem_free(fhci);
238 	return -ENOMEM;
239 }
240 
241 /* destroy the fhci_usb structure */
242 static void fhci_usb_free(void *lld)
243 {
244 	struct fhci_usb *usb = lld;
245 	struct fhci_hcd *fhci;
246 
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);
253 	}
254 }
255 
256 /* initialize the USB */
257 static int fhci_usb_init(struct fhci_hcd *fhci)
258 {
259 	struct fhci_usb *usb = fhci->usb_lld;
260 
261 	memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
262 
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;
266 
267 	usb->actual_frame = kzalloc_obj(*usb->actual_frame);
268 	if (!usb->actual_frame) {
269 		fhci_usb_free(usb);
270 		return -ENOMEM;
271 	}
272 
273 	INIT_LIST_HEAD(&usb->actual_frame->tds_list);
274 
275 	/* initializing registers on chip, clear frame number */
276 	out_be16(&fhci->pram->frame_num, 0);
277 
278 	/* clear rx state */
279 	out_be32(&fhci->pram->rx_state, 0);
280 
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);
286 
287 	out_8(&usb->fhci->regs->usb_usmod, USB_MODE_HOST | USB_MODE_EN);
288 
289 	/* clearing the mask register */
290 	out_be16(&usb->fhci->regs->usb_usbmr, 0);
291 
292 	/* initialing the event register */
293 	out_be16(&usb->fhci->regs->usb_usber, 0xffff);
294 
295 	if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
296 		fhci_usb_free(usb);
297 		return -EINVAL;
298 	}
299 
300 	return 0;
301 }
302 
303 /* initialize the fhci_usb struct and the corresponding data staruct */
304 static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
305 {
306 	struct fhci_usb *usb;
307 
308 	/* allocate memory for SCC data structure */
309 	usb = kzalloc_obj(*usb);
310 	if (!usb)
311 		return NULL;
312 
313 	usb->fhci = fhci;
314 	usb->hc_list = fhci->hc_list;
315 	usb->vroot_hub = fhci->vroot_hub;
316 
317 	usb->transfer_confirm = fhci_transfer_confirm_callback;
318 
319 	return usb;
320 }
321 
322 static int fhci_start(struct usb_hcd *hcd)
323 {
324 	int ret;
325 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
326 
327 	ret = fhci_mem_init(fhci);
328 	if (ret) {
329 		fhci_err(fhci, "failed to allocate memory\n");
330 		goto err;
331 	}
332 
333 	fhci->usb_lld = fhci_create_lld(fhci);
334 	if (!fhci->usb_lld) {
335 		fhci_err(fhci, "low level driver config failed\n");
336 		ret = -ENOMEM;
337 		goto err;
338 	}
339 
340 	ret = fhci_usb_init(fhci);
341 	if (ret) {
342 		fhci_err(fhci, "low level driver initialize failed\n");
343 		goto err;
344 	}
345 
346 	spin_lock_init(&fhci->lock);
347 
348 	/* connect the virtual root hub */
349 	fhci->vroot_hub->dev_num = 1;	/* this field may be needed to fix */
350 	fhci->vroot_hub->hub.wHubStatus = 0;
351 	fhci->vroot_hub->hub.wHubChange = 0;
352 	fhci->vroot_hub->port.wPortStatus = 0;
353 	fhci->vroot_hub->port.wPortChange = 0;
354 
355 	hcd->state = HC_STATE_RUNNING;
356 
357 	/*
358 	 * From here on, hub_wq concurrently accesses the root
359 	 * hub; drivers will be talking to enumerated devices.
360 	 * (On restart paths, hub_wq already knows about the root
361 	 * hub and could find work as soon as we wrote FLAG_CF.)
362 	 *
363 	 * Before this point the HC was idle/ready.  After, hub_wq
364 	 * and device drivers may start it running.
365 	 */
366 	fhci_usb_enable(fhci);
367 	return 0;
368 err:
369 	fhci_mem_free(fhci);
370 	return ret;
371 }
372 
373 static void fhci_stop(struct usb_hcd *hcd)
374 {
375 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
376 
377 	fhci_usb_disable_interrupt(fhci->usb_lld);
378 	fhci_usb_disable(fhci);
379 
380 	fhci_usb_free(fhci->usb_lld);
381 	fhci->usb_lld = NULL;
382 	fhci_mem_free(fhci);
383 }
384 
385 static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
386 			    gfp_t mem_flags)
387 {
388 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
389 	u32 pipe = urb->pipe;
390 	int ret;
391 	int i;
392 	int size = 0;
393 	struct urb_priv *urb_priv;
394 	unsigned long flags;
395 
396 	switch (usb_pipetype(pipe)) {
397 	case PIPE_CONTROL:
398 		/* 1 td fro setup,1 for ack */
399 		size = 2;
400 		fallthrough;
401 	case PIPE_BULK:
402 		/* one td for every 4096 bytes(can be up to 8k) */
403 		size += urb->transfer_buffer_length / 4096;
404 		/* ...add for any remaining bytes... */
405 		if ((urb->transfer_buffer_length % 4096) != 0)
406 			size++;
407 		/* ..and maybe a zero length packet to wrap it up */
408 		if (size == 0)
409 			size++;
410 		else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
411 			 && (urb->transfer_buffer_length
412 			     % usb_maxpacket(urb->dev, pipe)) != 0)
413 			size++;
414 		break;
415 	case PIPE_ISOCHRONOUS:
416 		size = urb->number_of_packets;
417 		if (size <= 0)
418 			return -EINVAL;
419 		for (i = 0; i < urb->number_of_packets; i++) {
420 			urb->iso_frame_desc[i].actual_length = 0;
421 			urb->iso_frame_desc[i].status = (u32) (-EXDEV);
422 		}
423 		break;
424 	case PIPE_INTERRUPT:
425 		size = 1;
426 	}
427 
428 	/* allocate the private part of the URB */
429 	urb_priv = kzalloc_flex(*urb_priv, tds, size, mem_flags);
430 	if (!urb_priv)
431 		return -ENOMEM;
432 
433 	urb_priv->num_of_tds = size;
434 
435 	spin_lock_irqsave(&fhci->lock, flags);
436 
437 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
438 	if (ret)
439 		goto err;
440 
441 	/* fill the private part of the URB */
442 	urb->status = -EINPROGRESS;
443 	urb->actual_length = 0;
444 	urb->error_count = 0;
445 	urb->hcpriv = urb_priv;
446 
447 	fhci_queue_urb(fhci, urb);
448 err:
449 	if (ret)
450 		kfree(urb_priv);
451 	spin_unlock_irqrestore(&fhci->lock, flags);
452 	return ret;
453 }
454 
455 /* dequeue FHCI URB */
456 static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
457 {
458 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
459 	struct fhci_usb *usb = fhci->usb_lld;
460 	int ret = -EINVAL;
461 	unsigned long flags;
462 
463 	if (!urb || !urb->dev || !urb->dev->bus)
464 		goto out;
465 
466 	spin_lock_irqsave(&fhci->lock, flags);
467 
468 	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
469 	if (ret)
470 		goto out2;
471 
472 	if (usb->port_status != FHCI_PORT_DISABLED) {
473 		struct urb_priv *urb_priv;
474 
475 		/*
476 		 * flag the urb's data for deletion in some upcoming
477 		 * SF interrupt's delete list processing
478 		 */
479 		urb_priv = urb->hcpriv;
480 
481 		if (!urb_priv || (urb_priv->state == URB_DEL))
482 			goto out2;
483 
484 		urb_priv->state = URB_DEL;
485 
486 		/* already pending? */
487 		urb_priv->ed->state = FHCI_ED_URB_DEL;
488 	} else {
489 		fhci_urb_complete_free(fhci, urb);
490 	}
491 
492 out2:
493 	spin_unlock_irqrestore(&fhci->lock, flags);
494 out:
495 	return ret;
496 }
497 
498 static void fhci_endpoint_disable(struct usb_hcd *hcd,
499 				  struct usb_host_endpoint *ep)
500 {
501 	struct fhci_hcd *fhci;
502 	struct ed *ed;
503 	unsigned long flags;
504 
505 	fhci = hcd_to_fhci(hcd);
506 	spin_lock_irqsave(&fhci->lock, flags);
507 	ed = ep->hcpriv;
508 	if (ed) {
509 		while (ed->td_head != NULL) {
510 			struct td *td = fhci_remove_td_from_ed(ed);
511 			fhci_urb_complete_free(fhci, td->urb);
512 		}
513 		fhci_recycle_empty_ed(fhci, ed);
514 		ep->hcpriv = NULL;
515 	}
516 	spin_unlock_irqrestore(&fhci->lock, flags);
517 }
518 
519 static int fhci_get_frame_number(struct usb_hcd *hcd)
520 {
521 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
522 
523 	return get_frame_num(fhci);
524 }
525 
526 static const struct hc_driver fhci_driver = {
527 	.description = "fsl,usb-fhci",
528 	.product_desc = "FHCI HOST Controller",
529 	.hcd_priv_size = sizeof(struct fhci_hcd),
530 
531 	/* generic hardware linkage */
532 	.irq = fhci_irq,
533 	.flags = HCD_DMA | HCD_USB11 | HCD_MEMORY,
534 
535 	/* basic lifecycle operation */
536 	.start = fhci_start,
537 	.stop = fhci_stop,
538 
539 	/* managing i/o requests and associated device resources */
540 	.urb_enqueue = fhci_urb_enqueue,
541 	.urb_dequeue = fhci_urb_dequeue,
542 	.endpoint_disable = fhci_endpoint_disable,
543 
544 	/* scheduling support */
545 	.get_frame_number = fhci_get_frame_number,
546 
547 	/* root hub support */
548 	.hub_status_data = fhci_hub_status_data,
549 	.hub_control = fhci_hub_control,
550 };
551 
552 static int of_fhci_probe(struct platform_device *ofdev)
553 {
554 	struct device *dev = &ofdev->dev;
555 	struct device_node *node = dev->of_node;
556 	struct usb_hcd *hcd;
557 	struct fhci_hcd *fhci;
558 	struct resource usb_regs;
559 	unsigned long pram_addr;
560 	unsigned int usb_irq;
561 	const char *sprop;
562 	const u32 *iprop;
563 	int size;
564 	int ret;
565 	int i;
566 	int j;
567 
568 	if (usb_disabled())
569 		return -ENODEV;
570 
571 	sprop = of_get_property(node, "mode", NULL);
572 	if (sprop && strcmp(sprop, "host"))
573 		return -ENODEV;
574 
575 	hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
576 	if (!hcd) {
577 		dev_err(dev, "could not create hcd\n");
578 		return -ENOMEM;
579 	}
580 
581 	fhci = hcd_to_fhci(hcd);
582 	hcd->self.controller = dev;
583 	dev_set_drvdata(dev, hcd);
584 
585 	iprop = of_get_property(node, "hub-power-budget", &size);
586 	if (iprop && size == sizeof(*iprop))
587 		hcd->power_budget = *iprop;
588 
589 	/* FHCI registers. */
590 	ret = of_address_to_resource(node, 0, &usb_regs);
591 	if (ret) {
592 		dev_err(dev, "could not get regs\n");
593 		goto err_regs;
594 	}
595 
596 	hcd->regs = ioremap(usb_regs.start, resource_size(&usb_regs));
597 	if (!hcd->regs) {
598 		dev_err(dev, "could not ioremap regs\n");
599 		ret = -ENOMEM;
600 		goto err_regs;
601 	}
602 	fhci->regs = hcd->regs;
603 
604 	/* Parameter RAM. */
605 	iprop = of_get_property(node, "reg", &size);
606 	if (!iprop || size < sizeof(*iprop) * 4) {
607 		dev_err(dev, "can't get pram offset\n");
608 		ret = -EINVAL;
609 		goto err_pram;
610 	}
611 
612 	pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
613 	if (IS_ERR_VALUE(pram_addr)) {
614 		dev_err(dev, "failed to allocate usb pram\n");
615 		ret = -ENOMEM;
616 		goto err_pram;
617 	}
618 
619 	qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
620 		     QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
621 	fhci->pram = cpm_muram_addr(pram_addr);
622 
623 	/* GPIOs and pins */
624 	for (i = 0; i < NUM_GPIOS; i++) {
625 		if (i < GPIO_SPEED)
626 			fhci->gpiods[i] = devm_gpiod_get_index(dev,
627 					NULL, i, GPIOD_IN);
628 
629 		else
630 			fhci->gpiods[i] = devm_gpiod_get_index_optional(dev,
631 					NULL, i, GPIOD_OUT_LOW);
632 
633 		if (IS_ERR(fhci->gpiods[i])) {
634 			dev_err(dev, "incorrect GPIO%d: %ld\n",
635 				i, PTR_ERR(fhci->gpiods[i]));
636 			goto err_gpios;
637 		}
638 		if (!fhci->gpiods[i]) {
639 			dev_info(dev, "assuming board doesn't have "
640 				 "%s gpio\n", i == GPIO_SPEED ?
641 				 "speed" : "power");
642 		}
643 	}
644 
645 	for (j = 0; j < NUM_PINS; j++) {
646 		fhci->pins[j] = qe_pin_request(dev, j);
647 		if (IS_ERR(fhci->pins[j])) {
648 			ret = PTR_ERR(fhci->pins[j]);
649 			dev_err(dev, "can't get pin %d: %d\n", j, ret);
650 			goto err_pins;
651 		}
652 	}
653 
654 	/* Frame limit timer and its interrupt. */
655 	fhci->timer = gtm_get_timer16();
656 	if (IS_ERR(fhci->timer)) {
657 		ret = PTR_ERR(fhci->timer);
658 		dev_err(dev, "failed to request qe timer: %i", ret);
659 		goto err_get_timer;
660 	}
661 
662 	ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
663 			  0, "qe timer (usb)", hcd);
664 	if (ret) {
665 		dev_err(dev, "failed to request timer irq");
666 		goto err_timer_irq;
667 	}
668 
669 	/* USB Host interrupt. */
670 	usb_irq = irq_of_parse_and_map(node, 0);
671 	if (!usb_irq) {
672 		dev_err(dev, "could not get usb irq\n");
673 		ret = -EINVAL;
674 		goto err_usb_irq;
675 	}
676 
677 	/* Clocks. */
678 	sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
679 	if (sprop) {
680 		fhci->fullspeed_clk = qe_clock_source(sprop);
681 		if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
682 			dev_err(dev, "wrong fullspeed-clock\n");
683 			ret = -EINVAL;
684 			goto err_clocks;
685 		}
686 	}
687 
688 	sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
689 	if (sprop) {
690 		fhci->lowspeed_clk = qe_clock_source(sprop);
691 		if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
692 			dev_err(dev, "wrong lowspeed-clock\n");
693 			ret = -EINVAL;
694 			goto err_clocks;
695 		}
696 	}
697 
698 	if (fhci->fullspeed_clk == QE_CLK_NONE &&
699 			fhci->lowspeed_clk == QE_CLK_NONE) {
700 		dev_err(dev, "no clocks specified\n");
701 		ret = -EINVAL;
702 		goto err_clocks;
703 	}
704 
705 	dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
706 
707 	fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
708 
709 	/* Start with full-speed, if possible. */
710 	if (fhci->fullspeed_clk != QE_CLK_NONE) {
711 		fhci_config_transceiver(fhci, FHCI_PORT_FULL);
712 		qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
713 	} else {
714 		fhci_config_transceiver(fhci, FHCI_PORT_LOW);
715 		qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
716 	}
717 
718 	/* Clear and disable any pending interrupts. */
719 	out_be16(&fhci->regs->usb_usber, 0xffff);
720 	out_be16(&fhci->regs->usb_usbmr, 0);
721 
722 	ret = usb_add_hcd(hcd, usb_irq, 0);
723 	if (ret < 0)
724 		goto err_add_hcd;
725 
726 	device_wakeup_enable(hcd->self.controller);
727 
728 	fhci_dfs_create(fhci);
729 
730 	return 0;
731 
732 err_add_hcd:
733 err_clocks:
734 	irq_dispose_mapping(usb_irq);
735 err_usb_irq:
736 	free_irq(fhci->timer->irq, hcd);
737 err_timer_irq:
738 	gtm_put_timer16(fhci->timer);
739 err_get_timer:
740 err_pins:
741 	while (--j >= 0)
742 		qe_pin_free(fhci->pins[j]);
743 err_gpios:
744 	cpm_muram_free(pram_addr);
745 err_pram:
746 	iounmap(hcd->regs);
747 err_regs:
748 	usb_put_hcd(hcd);
749 	return ret;
750 }
751 
752 static void fhci_remove(struct device *dev)
753 {
754 	struct usb_hcd *hcd = dev_get_drvdata(dev);
755 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
756 	int j;
757 
758 	usb_remove_hcd(hcd);
759 	free_irq(fhci->timer->irq, hcd);
760 	gtm_put_timer16(fhci->timer);
761 	cpm_muram_free(cpm_muram_offset(fhci->pram));
762 	for (j = 0; j < NUM_PINS; j++)
763 		qe_pin_free(fhci->pins[j]);
764 	fhci_dfs_destroy(fhci);
765 	usb_put_hcd(hcd);
766 }
767 
768 static void of_fhci_remove(struct platform_device *ofdev)
769 {
770 	fhci_remove(&ofdev->dev);
771 }
772 
773 static const struct of_device_id of_fhci_match[] = {
774 	{ .compatible = "fsl,mpc8323-qe-usb", },
775 	{},
776 };
777 MODULE_DEVICE_TABLE(of, of_fhci_match);
778 
779 static struct platform_driver of_fhci_driver = {
780 	.driver = {
781 		.name = "fsl,usb-fhci",
782 		.of_match_table = of_fhci_match,
783 	},
784 	.probe		= of_fhci_probe,
785 	.remove		= of_fhci_remove,
786 };
787 
788 module_platform_driver(of_fhci_driver);
789 
790 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
791 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
792 	      "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
793 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
794 MODULE_LICENSE("GPL");
795