Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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