Linux Audio

Check our new training course

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