• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Freescale QUICC Engine USB Host Controller Driver
3  *
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>
11  *
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.
16  */
17 
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"
34 
fhci_start_sof_timer(struct fhci_hcd * fhci)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_sof_tmr, 0);
43 	setbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
44 
45 	fhci_dbg(fhci, "<- %s\n", __func__);
46 }
47 
fhci_stop_sof_timer(struct fhci_hcd * fhci)48 void fhci_stop_sof_timer(struct fhci_hcd *fhci)
49 {
50 	fhci_dbg(fhci, "-> %s\n", __func__);
51 
52 	clrbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
53 	gtm_stop_timer16(fhci->timer);
54 
55 	fhci_dbg(fhci, "<- %s\n", __func__);
56 }
57 
fhci_get_sof_timer_count(struct fhci_usb * usb)58 u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
59 {
60 	return be16_to_cpu(in_be16(&usb->fhci->regs->usb_sof_tmr) / 12);
61 }
62 
63 /* initialize the endpoint zero */
endpoint_zero_init(struct fhci_usb * usb,enum fhci_mem_alloc data_mem,u32 ring_len)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 */
fhci_usb_enable_interrupt(struct fhci_usb * usb)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_event, 0xffff);
91 		out_be16(&usb->fhci->regs->usb_mask, 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 /* diable the usb interrupt */
fhci_usb_disable_interrupt(struct fhci_usb * usb)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 		/* diable 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_mask, 0);
112 	}
113 	usb->intr_nesting_cnt++;
114 }
115 
116 /* enable the USB controller */
fhci_usb_enable(struct fhci_hcd * fhci)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_event, 0xffff);
122 	out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
123 	setbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
124 
125 	mdelay(100);
126 
127 	return 0;
128 }
129 
130 /* disable the USB controller */
fhci_usb_disable(struct fhci_hcd * fhci)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_mod, USB_MODE_EN);
144 
145 	return 0;
146 }
147 
148 /* check the bus state by polling the QE bit on the IO ports */
fhci_ioports_check_bus_state(struct fhci_hcd * fhci)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 (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
155 		return -1;
156 
157 	/* check USBRP */
158 	if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
159 		bits |= 0x2;
160 
161 	/* check USBRN */
162 	if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
163 		bits |= 0x1;
164 
165 	return bits;
166 }
167 
fhci_mem_free(struct fhci_hcd * fhci)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 
fhci_mem_init(struct fhci_hcd * fhci)192 static int fhci_mem_init(struct fhci_hcd *fhci)
193 {
194 	int i;
195 
196 	fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
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(sizeof(*fhci->vroot_hub), GFP_KERNEL);
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(sizeof(*td), GFP_KERNEL);
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(sizeof(*ed), GFP_KERNEL);
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 */
fhci_usb_free(void * lld)242 static void fhci_usb_free(void *lld)
243 {
244 	struct fhci_usb *usb = lld;
245 	struct fhci_hcd *fhci = usb->fhci;
246 
247 	if (usb) {
248 		fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
249 		fhci_ep0_free(usb);
250 		kfree(usb->actual_frame);
251 		kfree(usb);
252 	}
253 }
254 
255 /* initialize the USB */
fhci_usb_init(struct fhci_hcd * fhci)256 static int fhci_usb_init(struct fhci_hcd *fhci)
257 {
258 	struct fhci_usb *usb = fhci->usb_lld;
259 
260 	memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
261 
262 	usb->port_status = FHCI_PORT_DISABLED;
263 	usb->max_frame_usage = FRAME_TIME_USAGE;
264 	usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
265 
266 	usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
267 	if (!usb->actual_frame) {
268 		fhci_usb_free(usb);
269 		return -ENOMEM;
270 	}
271 
272 	INIT_LIST_HEAD(&usb->actual_frame->tds_list);
273 
274 	/* initializing registers on chip, clear frame number */
275 	out_be16(&fhci->pram->frame_num, 0);
276 
277 	/* clear rx state */
278 	out_be32(&fhci->pram->rx_state, 0);
279 
280 	/* set mask register */
281 	usb->saved_msk = (USB_E_TXB_MASK |
282 			  USB_E_TXE1_MASK |
283 			  USB_E_IDLE_MASK |
284 			  USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
285 
286 	out_8(&usb->fhci->regs->usb_mod, USB_MODE_HOST | USB_MODE_EN);
287 
288 	/* clearing the mask register */
289 	out_be16(&usb->fhci->regs->usb_mask, 0);
290 
291 	/* initialing the event register */
292 	out_be16(&usb->fhci->regs->usb_event, 0xffff);
293 
294 	if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
295 		fhci_usb_free(usb);
296 		return -EINVAL;
297 	}
298 
299 	return 0;
300 }
301 
302 /* initialize the fhci_usb struct and the corresponding data staruct */
fhci_create_lld(struct fhci_hcd * fhci)303 static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
304 {
305 	struct fhci_usb *usb;
306 
307 	/* allocate memory for SCC data structure */
308 	usb = kzalloc(sizeof(*usb), GFP_KERNEL);
309 	if (!usb) {
310 		fhci_err(fhci, "no memory for SCC data struct\n");
311 		return NULL;
312 	}
313 
314 	usb->fhci = fhci;
315 	usb->hc_list = fhci->hc_list;
316 	usb->vroot_hub = fhci->vroot_hub;
317 
318 	usb->transfer_confirm = fhci_transfer_confirm_callback;
319 
320 	return usb;
321 }
322 
fhci_start(struct usb_hcd * hcd)323 static int fhci_start(struct usb_hcd *hcd)
324 {
325 	int ret;
326 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
327 
328 	ret = fhci_mem_init(fhci);
329 	if (ret) {
330 		fhci_err(fhci, "failed to allocate memory\n");
331 		goto err;
332 	}
333 
334 	fhci->usb_lld = fhci_create_lld(fhci);
335 	if (!fhci->usb_lld) {
336 		fhci_err(fhci, "low level driver config failed\n");
337 		ret = -ENOMEM;
338 		goto err;
339 	}
340 
341 	ret = fhci_usb_init(fhci);
342 	if (ret) {
343 		fhci_err(fhci, "low level driver initialize failed\n");
344 		goto err;
345 	}
346 
347 	spin_lock_init(&fhci->lock);
348 
349 	/* connect the virtual root hub */
350 	fhci->vroot_hub->dev_num = 1;	/* this field may be needed to fix */
351 	fhci->vroot_hub->hub.wHubStatus = 0;
352 	fhci->vroot_hub->hub.wHubChange = 0;
353 	fhci->vroot_hub->port.wPortStatus = 0;
354 	fhci->vroot_hub->port.wPortChange = 0;
355 
356 	hcd->state = HC_STATE_RUNNING;
357 
358 	/*
359 	 * From here on, khubd concurrently accesses the root
360 	 * hub; drivers will be talking to enumerated devices.
361 	 * (On restart paths, khubd already knows about the root
362 	 * hub and could find work as soon as we wrote FLAG_CF.)
363 	 *
364 	 * Before this point the HC was idle/ready.  After, khubd
365 	 * and device drivers may start it running.
366 	 */
367 	fhci_usb_enable(fhci);
368 	return 0;
369 err:
370 	fhci_mem_free(fhci);
371 	return ret;
372 }
373 
fhci_stop(struct usb_hcd * hcd)374 static void fhci_stop(struct usb_hcd *hcd)
375 {
376 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
377 
378 	fhci_usb_disable_interrupt(fhci->usb_lld);
379 	fhci_usb_disable(fhci);
380 
381 	fhci_usb_free(fhci->usb_lld);
382 	fhci->usb_lld = NULL;
383 	fhci_mem_free(fhci);
384 }
385 
fhci_urb_enqueue(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)386 static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
387 			    gfp_t mem_flags)
388 {
389 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
390 	u32 pipe = urb->pipe;
391 	int ret;
392 	int i;
393 	int size = 0;
394 	struct urb_priv *urb_priv;
395 	unsigned long flags;
396 
397 	switch (usb_pipetype(pipe)) {
398 	case PIPE_CONTROL:
399 		/* 1 td fro setup,1 for ack */
400 		size = 2;
401 	case PIPE_BULK:
402 		/* one td for every 4096 bytes(can be upto 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,
413 					     usb_pipeout(pipe))) != 0)
414 			size++;
415 		break;
416 	case PIPE_ISOCHRONOUS:
417 		size = urb->number_of_packets;
418 		if (size <= 0)
419 			return -EINVAL;
420 		for (i = 0; i < urb->number_of_packets; i++) {
421 			urb->iso_frame_desc[i].actual_length = 0;
422 			urb->iso_frame_desc[i].status = (u32) (-EXDEV);
423 		}
424 		break;
425 	case PIPE_INTERRUPT:
426 		size = 1;
427 	}
428 
429 	/* allocate the private part of the URB */
430 	urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
431 	if (!urb_priv)
432 		return -ENOMEM;
433 
434 	/* allocate the private part of the URB */
435 	urb_priv->tds = kzalloc(size * sizeof(struct td), mem_flags);
436 	if (!urb_priv->tds) {
437 		kfree(urb_priv);
438 		return -ENOMEM;
439 	}
440 
441 	spin_lock_irqsave(&fhci->lock, flags);
442 
443 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
444 	if (ret)
445 		goto err;
446 
447 	/* fill the private part of the URB */
448 	urb_priv->num_of_tds = size;
449 
450 	urb->status = -EINPROGRESS;
451 	urb->actual_length = 0;
452 	urb->error_count = 0;
453 	urb->hcpriv = urb_priv;
454 
455 	fhci_queue_urb(fhci, urb);
456 err:
457 	if (ret) {
458 		kfree(urb_priv->tds);
459 		kfree(urb_priv);
460 	}
461 	spin_unlock_irqrestore(&fhci->lock, flags);
462 	return ret;
463 }
464 
465 /* dequeue FHCI URB */
fhci_urb_dequeue(struct usb_hcd * hcd,struct urb * urb,int status)466 static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
467 {
468 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
469 	struct fhci_usb *usb = fhci->usb_lld;
470 	int ret = -EINVAL;
471 	unsigned long flags;
472 
473 	if (!urb || !urb->dev || !urb->dev->bus)
474 		goto out;
475 
476 	spin_lock_irqsave(&fhci->lock, flags);
477 
478 	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
479 	if (ret)
480 		goto out2;
481 
482 	if (usb->port_status != FHCI_PORT_DISABLED) {
483 		struct urb_priv *urb_priv;
484 
485 		/*
486 		 * flag the urb's data for deletion in some upcoming
487 		 * SF interrupt's delete list processing
488 		 */
489 		urb_priv = urb->hcpriv;
490 
491 		if (!urb_priv || (urb_priv->state == URB_DEL))
492 			goto out2;
493 
494 		urb_priv->state = URB_DEL;
495 
496 		/* already pending? */
497 		urb_priv->ed->state = FHCI_ED_URB_DEL;
498 	} else {
499 		fhci_urb_complete_free(fhci, urb);
500 	}
501 
502 out2:
503 	spin_unlock_irqrestore(&fhci->lock, flags);
504 out:
505 	return ret;
506 }
507 
fhci_endpoint_disable(struct usb_hcd * hcd,struct usb_host_endpoint * ep)508 static void fhci_endpoint_disable(struct usb_hcd *hcd,
509 				  struct usb_host_endpoint *ep)
510 {
511 	struct fhci_hcd *fhci;
512 	struct ed *ed;
513 	unsigned long flags;
514 
515 	fhci = hcd_to_fhci(hcd);
516 	spin_lock_irqsave(&fhci->lock, flags);
517 	ed = ep->hcpriv;
518 	if (ed) {
519 		while (ed->td_head != NULL) {
520 			struct td *td = fhci_remove_td_from_ed(ed);
521 			fhci_urb_complete_free(fhci, td->urb);
522 		}
523 		fhci_recycle_empty_ed(fhci, ed);
524 		ep->hcpriv = NULL;
525 	}
526 	spin_unlock_irqrestore(&fhci->lock, flags);
527 }
528 
fhci_get_frame_number(struct usb_hcd * hcd)529 static int fhci_get_frame_number(struct usb_hcd *hcd)
530 {
531 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
532 
533 	return get_frame_num(fhci);
534 }
535 
536 static const struct hc_driver fhci_driver = {
537 	.description = "fsl,usb-fhci",
538 	.product_desc = "FHCI HOST Controller",
539 	.hcd_priv_size = sizeof(struct fhci_hcd),
540 
541 	/* generic hardware linkage */
542 	.irq = fhci_irq,
543 	.flags = HCD_USB11 | HCD_MEMORY,
544 
545 	/* basic lifecycle operation */
546 	.start = fhci_start,
547 	.stop = fhci_stop,
548 
549 	/* managing i/o requests and associated device resources */
550 	.urb_enqueue = fhci_urb_enqueue,
551 	.urb_dequeue = fhci_urb_dequeue,
552 	.endpoint_disable = fhci_endpoint_disable,
553 
554 	/* scheduling support */
555 	.get_frame_number = fhci_get_frame_number,
556 
557 	/* root hub support */
558 	.hub_status_data = fhci_hub_status_data,
559 	.hub_control = fhci_hub_control,
560 };
561 
of_fhci_probe(struct of_device * ofdev,const struct of_device_id * ofid)562 static int __devinit of_fhci_probe(struct of_device *ofdev,
563 				   const struct of_device_id *ofid)
564 {
565 	struct device *dev = &ofdev->dev;
566 	struct device_node *node = ofdev->node;
567 	struct usb_hcd *hcd;
568 	struct fhci_hcd *fhci;
569 	struct resource usb_regs;
570 	unsigned long pram_addr;
571 	unsigned int usb_irq;
572 	const char *sprop;
573 	const u32 *iprop;
574 	int size;
575 	int ret;
576 	int i;
577 	int j;
578 
579 	if (usb_disabled())
580 		return -ENODEV;
581 
582 	sprop = of_get_property(node, "mode", NULL);
583 	if (sprop && strcmp(sprop, "host"))
584 		return -ENODEV;
585 
586 	hcd = usb_create_hcd(&fhci_driver, dev, dev->bus_id);
587 	if (!hcd) {
588 		dev_err(dev, "could not create hcd\n");
589 		return -ENOMEM;
590 	}
591 
592 	fhci = hcd_to_fhci(hcd);
593 	hcd->self.controller = dev;
594 	dev_set_drvdata(dev, hcd);
595 
596 	iprop = of_get_property(node, "hub-power-budget", &size);
597 	if (iprop && size == sizeof(*iprop))
598 		hcd->power_budget = *iprop;
599 
600 	/* FHCI registers. */
601 	ret = of_address_to_resource(node, 0, &usb_regs);
602 	if (ret) {
603 		dev_err(dev, "could not get regs\n");
604 		goto err_regs;
605 	}
606 
607 	hcd->regs = ioremap(usb_regs.start, usb_regs.end - usb_regs.start + 1);
608 	if (!hcd->regs) {
609 		dev_err(dev, "could not ioremap regs\n");
610 		ret = -ENOMEM;
611 		goto err_regs;
612 	}
613 	fhci->regs = hcd->regs;
614 
615 	/* Parameter RAM. */
616 	iprop = of_get_property(node, "reg", &size);
617 	if (!iprop || size < sizeof(*iprop) * 4) {
618 		dev_err(dev, "can't get pram offset\n");
619 		ret = -EINVAL;
620 		goto err_pram;
621 	}
622 
623 	pram_addr = cpm_muram_alloc_fixed(iprop[2], FHCI_PRAM_SIZE);
624 	if (IS_ERR_VALUE(pram_addr)) {
625 		dev_err(dev, "failed to allocate usb pram\n");
626 		ret = -ENOMEM;
627 		goto err_pram;
628 	}
629 	fhci->pram = cpm_muram_addr(pram_addr);
630 
631 	/* GPIOs and pins */
632 	for (i = 0; i < NUM_GPIOS; i++) {
633 		int gpio;
634 		enum of_gpio_flags flags;
635 
636 		gpio = of_get_gpio_flags(node, i, &flags);
637 		fhci->gpios[i] = gpio;
638 		fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
639 
640 		if (!gpio_is_valid(gpio)) {
641 			if (i < GPIO_SPEED) {
642 				dev_err(dev, "incorrect GPIO%d: %d\n",
643 					i, gpio);
644 				goto err_gpios;
645 			} else {
646 				dev_info(dev, "assuming board doesn't have "
647 					"%s gpio\n", i == GPIO_SPEED ?
648 					"speed" : "power");
649 				continue;
650 			}
651 		}
652 
653 		ret = gpio_request(gpio, dev->bus_id);
654 		if (ret) {
655 			dev_err(dev, "failed to request gpio %d", i);
656 			goto err_gpios;
657 		}
658 
659 		if (i >= GPIO_SPEED) {
660 			ret = gpio_direction_output(gpio, 0);
661 			if (ret) {
662 				dev_err(dev, "failed to set gpio %d as "
663 					"an output\n", i);
664 				i++;
665 				goto err_gpios;
666 			}
667 		}
668 	}
669 
670 	for (j = 0; j < NUM_PINS; j++) {
671 		fhci->pins[j] = qe_pin_request(ofdev->node, j);
672 		if (IS_ERR(fhci->pins[j])) {
673 			ret = PTR_ERR(fhci->pins[j]);
674 			dev_err(dev, "can't get pin %d: %d\n", j, ret);
675 			goto err_pins;
676 		}
677 	}
678 
679 	/* Frame limit timer and its interrupt. */
680 	fhci->timer = gtm_get_timer16();
681 	if (IS_ERR(fhci->timer)) {
682 		ret = PTR_ERR(fhci->timer);
683 		dev_err(dev, "failed to request qe timer: %i", ret);
684 		goto err_get_timer;
685 	}
686 
687 	ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
688 			  IRQF_DISABLED, "qe timer (usb)", hcd);
689 	if (ret) {
690 		dev_err(dev, "failed to request timer irq");
691 		goto err_timer_irq;
692 	}
693 
694 	/* USB Host interrupt. */
695 	usb_irq = irq_of_parse_and_map(node, 0);
696 	if (usb_irq == NO_IRQ) {
697 		dev_err(dev, "could not get usb irq\n");
698 		ret = -EINVAL;
699 		goto err_usb_irq;
700 	}
701 
702 	/* Clocks. */
703 	sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
704 	if (sprop) {
705 		fhci->fullspeed_clk = qe_clock_source(sprop);
706 		if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
707 			dev_err(dev, "wrong fullspeed-clock\n");
708 			ret = -EINVAL;
709 			goto err_clocks;
710 		}
711 	}
712 
713 	sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
714 	if (sprop) {
715 		fhci->lowspeed_clk = qe_clock_source(sprop);
716 		if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
717 			dev_err(dev, "wrong lowspeed-clock\n");
718 			ret = -EINVAL;
719 			goto err_clocks;
720 		}
721 	}
722 
723 	if (fhci->fullspeed_clk == QE_CLK_NONE &&
724 			fhci->lowspeed_clk == QE_CLK_NONE) {
725 		dev_err(dev, "no clocks specified\n");
726 		ret = -EINVAL;
727 		goto err_clocks;
728 	}
729 
730 	dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
731 
732 	fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
733 
734 	/* Start with full-speed, if possible. */
735 	if (fhci->fullspeed_clk != QE_CLK_NONE) {
736 		fhci_config_transceiver(fhci, FHCI_PORT_FULL);
737 		qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
738 	} else {
739 		fhci_config_transceiver(fhci, FHCI_PORT_LOW);
740 		qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
741 	}
742 
743 	/* Clear and disable any pending interrupts. */
744 	out_be16(&fhci->regs->usb_event, 0xffff);
745 	out_be16(&fhci->regs->usb_mask, 0);
746 
747 	ret = usb_add_hcd(hcd, usb_irq, IRQF_DISABLED);
748 	if (ret < 0)
749 		goto err_add_hcd;
750 
751 	fhci_dfs_create(fhci);
752 
753 	return 0;
754 
755 err_add_hcd:
756 err_clocks:
757 	irq_dispose_mapping(usb_irq);
758 err_usb_irq:
759 	free_irq(fhci->timer->irq, hcd);
760 err_timer_irq:
761 	gtm_put_timer16(fhci->timer);
762 err_get_timer:
763 err_pins:
764 	while (--j >= 0)
765 		qe_pin_free(fhci->pins[j]);
766 err_gpios:
767 	while (--i >= 0) {
768 		if (gpio_is_valid(fhci->gpios[i]))
769 			gpio_free(fhci->gpios[i]);
770 	}
771 	cpm_muram_free(pram_addr);
772 err_pram:
773 	iounmap(hcd->regs);
774 err_regs:
775 	usb_put_hcd(hcd);
776 	return ret;
777 }
778 
fhci_remove(struct device * dev)779 static int __devexit fhci_remove(struct device *dev)
780 {
781 	struct usb_hcd *hcd = dev_get_drvdata(dev);
782 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
783 	int i;
784 	int j;
785 
786 	usb_remove_hcd(hcd);
787 	free_irq(fhci->timer->irq, hcd);
788 	gtm_put_timer16(fhci->timer);
789 	cpm_muram_free(cpm_muram_offset(fhci->pram));
790 	for (i = 0; i < NUM_GPIOS; i++) {
791 		if (!gpio_is_valid(fhci->gpios[i]))
792 			continue;
793 		gpio_free(fhci->gpios[i]);
794 	}
795 	for (j = 0; j < NUM_PINS; j++)
796 		qe_pin_free(fhci->pins[j]);
797 	fhci_dfs_destroy(fhci);
798 	usb_put_hcd(hcd);
799 	return 0;
800 }
801 
of_fhci_remove(struct of_device * ofdev)802 static int __devexit of_fhci_remove(struct of_device *ofdev)
803 {
804 	return fhci_remove(&ofdev->dev);
805 }
806 
807 static struct of_device_id of_fhci_match[] = {
808 	{ .compatible = "fsl,mpc8323-qe-usb", },
809 	{},
810 };
811 MODULE_DEVICE_TABLE(of, of_fhci_match);
812 
813 static struct of_platform_driver of_fhci_driver = {
814 	.name		= "fsl,usb-fhci",
815 	.match_table	= of_fhci_match,
816 	.probe		= of_fhci_probe,
817 	.remove		= __devexit_p(of_fhci_remove),
818 };
819 
fhci_module_init(void)820 static int __init fhci_module_init(void)
821 {
822 	return of_register_platform_driver(&of_fhci_driver);
823 }
824 module_init(fhci_module_init);
825 
fhci_module_exit(void)826 static void __exit fhci_module_exit(void)
827 {
828 	of_unregister_platform_driver(&of_fhci_driver);
829 }
830 module_exit(fhci_module_exit);
831 
832 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
833 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
834 	      "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
835 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
836 MODULE_LICENSE("GPL");
837