Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-1.0+
  2/*
  3 * OHCI HCD (Host Controller Driver) for USB.
  4 *
  5 *  Copyright (C) 2004 SAN People (Pty) Ltd.
  6 *  Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
  7 *
  8 * AT91 Bus Glue
  9 *
 10 * Based on fragments of 2.4 driver by Rick Bronson.
 11 * Based on ohci-omap.c
 12 *
 13 * This file is licenced under the GPL.
 14 */
 15
 16#include <linux/clk.h>
 17#include <linux/dma-mapping.h>
 18#include <linux/gpio/consumer.h>
 19#include <linux/of_platform.h>
 20#include <linux/platform_device.h>
 21#include <linux/platform_data/atmel.h>
 22#include <linux/io.h>
 23#include <linux/kernel.h>
 24#include <linux/module.h>
 25#include <linux/mfd/syscon.h>
 26#include <linux/regmap.h>
 27#include <linux/usb.h>
 28#include <linux/usb/hcd.h>
 29#include <soc/at91/atmel-sfr.h>
 30
 31#include "ohci.h"
 32
 33#define valid_port(index)	((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
 34#define at91_for_each_port(index)	\
 35		for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
 36
 37/* interface, function and usb clocks; sometimes also an AHB clock */
 38#define hcd_to_ohci_at91_priv(h) \
 39	((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
 40
 41#define AT91_MAX_USBH_PORTS	3
 42struct at91_usbh_data {
 43	struct gpio_desc *vbus_pin[AT91_MAX_USBH_PORTS];
 44	struct gpio_desc *overcurrent_pin[AT91_MAX_USBH_PORTS];
 45	u8 ports;				/* number of ports on root hub */
 46	u8 overcurrent_supported;
 47	u8 overcurrent_status[AT91_MAX_USBH_PORTS];
 48	u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
 49};
 50
 51struct ohci_at91_priv {
 52	struct clk *iclk;
 53	struct clk *fclk;
 54	struct clk *hclk;
 55	bool clocked;
 56	bool wakeup;		/* Saved wake-up state for resume */
 57	struct regmap *sfr_regmap;
 58};
 59/* interface and function clocks; sometimes also an AHB clock */
 60
 61#define DRIVER_DESC "OHCI Atmel driver"
 62
 63static const char hcd_name[] = "ohci-atmel";
 64
 65static struct hc_driver __read_mostly ohci_at91_hc_driver;
 66
 67static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
 68	.extra_priv_size = sizeof(struct ohci_at91_priv),
 69};
 70
 71/*-------------------------------------------------------------------------*/
 72
 73static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
 74{
 75	if (ohci_at91->clocked)
 76		return;
 77
 78	clk_set_rate(ohci_at91->fclk, 48000000);
 79	clk_prepare_enable(ohci_at91->hclk);
 80	clk_prepare_enable(ohci_at91->iclk);
 81	clk_prepare_enable(ohci_at91->fclk);
 82	ohci_at91->clocked = true;
 83}
 84
 85static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
 86{
 87	if (!ohci_at91->clocked)
 88		return;
 89
 90	clk_disable_unprepare(ohci_at91->fclk);
 91	clk_disable_unprepare(ohci_at91->iclk);
 92	clk_disable_unprepare(ohci_at91->hclk);
 93	ohci_at91->clocked = false;
 94}
 95
 96static void at91_start_hc(struct platform_device *pdev)
 97{
 98	struct usb_hcd *hcd = platform_get_drvdata(pdev);
 99	struct ohci_regs __iomem *regs = hcd->regs;
100	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
101
102	dev_dbg(&pdev->dev, "start\n");
103
104	/*
105	 * Start the USB clocks.
106	 */
107	at91_start_clock(ohci_at91);
108
109	/*
110	 * The USB host controller must remain in reset.
111	 */
112	writel(0, &regs->control);
113}
114
115static void at91_stop_hc(struct platform_device *pdev)
116{
117	struct usb_hcd *hcd = platform_get_drvdata(pdev);
118	struct ohci_regs __iomem *regs = hcd->regs;
119	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
120
121	dev_dbg(&pdev->dev, "stop\n");
122
123	/*
124	 * Put the USB host controller into reset.
125	 */
126	writel(0, &regs->control);
127
128	/*
129	 * Stop the USB clocks.
130	 */
131	at91_stop_clock(ohci_at91);
132}
133
134
135/*-------------------------------------------------------------------------*/
136
137static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
138
139static struct regmap *at91_dt_syscon_sfr(void)
140{
141	struct regmap *regmap;
142
143	regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
144	if (IS_ERR(regmap)) {
145		regmap = syscon_regmap_lookup_by_compatible("microchip,sam9x60-sfr");
146		if (IS_ERR(regmap))
147			regmap = NULL;
148	}
149
150	return regmap;
151}
152
153/* configure so an HC device and id are always provided */
154/* always called with process context; sleeping is OK */
155
156
157/**
158 * usb_hcd_at91_probe - initialize AT91-based HCDs
159 * Context: !in_interrupt()
160 *
161 * Allocates basic resources for this USB host controller, and
162 * then invokes the start() method for the HCD associated with it
163 * through the hotplug entry's driver_data.
164 */
165static int usb_hcd_at91_probe(const struct hc_driver *driver,
166			struct platform_device *pdev)
167{
168	struct at91_usbh_data *board;
169	struct ohci_hcd *ohci;
170	int retval;
171	struct usb_hcd *hcd;
172	struct ohci_at91_priv *ohci_at91;
173	struct device *dev = &pdev->dev;
174	struct resource *res;
175	int irq;
176
177	irq = platform_get_irq(pdev, 0);
178	if (irq < 0) {
179		dev_dbg(dev, "hcd probe: missing irq resource\n");
180		return irq;
181	}
182
183	hcd = usb_create_hcd(driver, dev, "at91");
184	if (!hcd)
185		return -ENOMEM;
186	ohci_at91 = hcd_to_ohci_at91_priv(hcd);
187
188	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189	hcd->regs = devm_ioremap_resource(dev, res);
190	if (IS_ERR(hcd->regs)) {
191		retval = PTR_ERR(hcd->regs);
192		goto err;
193	}
194	hcd->rsrc_start = res->start;
195	hcd->rsrc_len = resource_size(res);
196
197	ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
198	if (IS_ERR(ohci_at91->iclk)) {
199		dev_err(dev, "failed to get ohci_clk\n");
200		retval = PTR_ERR(ohci_at91->iclk);
201		goto err;
202	}
203	ohci_at91->fclk = devm_clk_get(dev, "uhpck");
204	if (IS_ERR(ohci_at91->fclk)) {
205		dev_err(dev, "failed to get uhpck\n");
206		retval = PTR_ERR(ohci_at91->fclk);
207		goto err;
208	}
209	ohci_at91->hclk = devm_clk_get(dev, "hclk");
210	if (IS_ERR(ohci_at91->hclk)) {
211		dev_err(dev, "failed to get hclk\n");
212		retval = PTR_ERR(ohci_at91->hclk);
213		goto err;
214	}
215
216	ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
217	if (!ohci_at91->sfr_regmap)
218		dev_dbg(dev, "failed to find sfr node\n");
219
220	board = hcd->self.controller->platform_data;
221	ohci = hcd_to_ohci(hcd);
222	ohci->num_ports = board->ports;
223	at91_start_hc(pdev);
224
225	/*
226	 * The RemoteWakeupConnected bit has to be set explicitly
227	 * before calling ohci_run. The reset value of this bit is 0.
228	 */
229	ohci->hc_control = OHCI_CTRL_RWC;
230
231	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
232	if (retval == 0) {
233		device_wakeup_enable(hcd->self.controller);
234		return retval;
235	}
236
237	/* Error handling */
238	at91_stop_hc(pdev);
239
240 err:
241	usb_put_hcd(hcd);
242	return retval;
243}
244
245
246/* may be called with controller, bus, and devices active */
247
248/**
249 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
250 * @dev: USB Host Controller being removed
251 * Context: !in_interrupt()
252 *
253 * Reverses the effect of usb_hcd_at91_probe(), first invoking
254 * the HCD's stop() method.  It is always called from a thread
255 * context, "rmmod" or something similar.
256 *
257 */
258static void usb_hcd_at91_remove(struct usb_hcd *hcd,
259				struct platform_device *pdev)
260{
261	usb_remove_hcd(hcd);
262	at91_stop_hc(pdev);
263	usb_put_hcd(hcd);
264}
265
266/*-------------------------------------------------------------------------*/
267static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
268{
269	if (!valid_port(port))
270		return;
271
272	gpiod_set_value(pdata->vbus_pin[port], enable);
273}
274
275static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
276{
277	if (!valid_port(port))
278		return -EINVAL;
279
280	return gpiod_get_value(pdata->vbus_pin[port]);
281}
282
283/*
284 * Update the status data from the hub with the over-current indicator change.
285 */
286static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
287{
288	struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
289	int length = ohci_hub_status_data(hcd, buf);
290	int port;
291
292	at91_for_each_port(port) {
293		if (pdata->overcurrent_changed[port]) {
294			if (!length)
295				length = 1;
296			buf[0] |= 1 << (port + 1);
297		}
298	}
299
300	return length;
301}
302
303static int ohci_at91_port_suspend(struct regmap *regmap, u8 set)
304{
305	u32 regval;
306	int ret;
307
308	if (!regmap)
309		return 0;
310
311	ret = regmap_read(regmap, AT91_SFR_OHCIICR, &regval);
312	if (ret)
313		return ret;
314
315	if (set)
316		regval |= AT91_OHCIICR_USB_SUSPEND;
317	else
318		regval &= ~AT91_OHCIICR_USB_SUSPEND;
319
320	regmap_write(regmap, AT91_SFR_OHCIICR, regval);
321
322	return 0;
323}
324
325/*
326 * Look at the control requests to the root hub and see if we need to override.
327 */
328static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
329				 u16 wIndex, char *buf, u16 wLength)
330{
331	struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
332	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
333	struct usb_hub_descriptor *desc;
334	int ret = -EINVAL;
335	u32 *data = (u32 *)buf;
336
337	dev_dbg(hcd->self.controller,
338		"ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
339		hcd, typeReq, wValue, wIndex, buf, wLength);
340
341	wIndex--;
342
343	switch (typeReq) {
344	case SetPortFeature:
345		switch (wValue) {
346		case USB_PORT_FEAT_POWER:
347			dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
348			if (valid_port(wIndex)) {
349				ohci_at91_usb_set_power(pdata, wIndex, 1);
350				ret = 0;
351			}
352
353			goto out;
354
355		case USB_PORT_FEAT_SUSPEND:
356			dev_dbg(hcd->self.controller, "SetPortFeat: SUSPEND\n");
357			if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
358				ohci_at91_port_suspend(ohci_at91->sfr_regmap,
359						       1);
360				return 0;
361			}
362			break;
363		}
364		break;
365
366	case ClearPortFeature:
367		switch (wValue) {
368		case USB_PORT_FEAT_C_OVER_CURRENT:
369			dev_dbg(hcd->self.controller,
370				"ClearPortFeature: C_OVER_CURRENT\n");
371
372			if (valid_port(wIndex)) {
373				pdata->overcurrent_changed[wIndex] = 0;
374				pdata->overcurrent_status[wIndex] = 0;
375			}
376
377			goto out;
378
379		case USB_PORT_FEAT_OVER_CURRENT:
380			dev_dbg(hcd->self.controller,
381				"ClearPortFeature: OVER_CURRENT\n");
382
383			if (valid_port(wIndex))
384				pdata->overcurrent_status[wIndex] = 0;
385
386			goto out;
387
388		case USB_PORT_FEAT_POWER:
389			dev_dbg(hcd->self.controller,
390				"ClearPortFeature: POWER\n");
391
392			if (valid_port(wIndex)) {
393				ohci_at91_usb_set_power(pdata, wIndex, 0);
394				return 0;
395			}
396			break;
397
398		case USB_PORT_FEAT_SUSPEND:
399			dev_dbg(hcd->self.controller, "ClearPortFeature: SUSPEND\n");
400			if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
401				ohci_at91_port_suspend(ohci_at91->sfr_regmap,
402						       0);
403				return 0;
404			}
405			break;
406		}
407		break;
408	}
409
410	ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
411	if (ret)
412		goto out;
413
414	switch (typeReq) {
415	case GetHubDescriptor:
416
417		/* update the hub's descriptor */
418
419		desc = (struct usb_hub_descriptor *)buf;
420
421		dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
422			desc->wHubCharacteristics);
423
424		/* remove the old configurations for power-switching, and
425		 * over-current protection, and insert our new configuration
426		 */
427
428		desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
429		desc->wHubCharacteristics |=
430			cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
431
432		if (pdata->overcurrent_supported) {
433			desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
434			desc->wHubCharacteristics |=
435				cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
436		}
437
438		dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
439			desc->wHubCharacteristics);
440
441		return ret;
442
443	case GetPortStatus:
444		/* check port status */
445
446		dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
447
448		if (valid_port(wIndex)) {
449			if (!ohci_at91_usb_get_power(pdata, wIndex))
450				*data &= ~cpu_to_le32(RH_PS_PPS);
451
452			if (pdata->overcurrent_changed[wIndex])
453				*data |= cpu_to_le32(RH_PS_OCIC);
454
455			if (pdata->overcurrent_status[wIndex])
456				*data |= cpu_to_le32(RH_PS_POCI);
457		}
458	}
459
460 out:
461	return ret;
462}
463
464/*-------------------------------------------------------------------------*/
465
466static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
467{
468	struct platform_device *pdev = data;
469	struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
470	int val, port;
471
472	/* From the GPIO notifying the over-current situation, find
473	 * out the corresponding port */
474	at91_for_each_port(port) {
475		if (gpiod_to_irq(pdata->overcurrent_pin[port]) == irq)
476			break;
477	}
478
479	if (port == AT91_MAX_USBH_PORTS) {
480		dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
481		return IRQ_HANDLED;
482	}
483
484	val = gpiod_get_value(pdata->overcurrent_pin[port]);
485
486	/* When notified of an over-current situation, disable power
487	   on the corresponding port, and mark this port in
488	   over-current. */
489	if (!val) {
490		ohci_at91_usb_set_power(pdata, port, 0);
491		pdata->overcurrent_status[port]  = 1;
492		pdata->overcurrent_changed[port] = 1;
493	}
494
495	dev_dbg(& pdev->dev, "overcurrent situation %s\n",
496		val ? "exited" : "notified");
497
498	return IRQ_HANDLED;
499}
500
501static const struct of_device_id at91_ohci_dt_ids[] = {
502	{ .compatible = "atmel,at91rm9200-ohci" },
503	{ /* sentinel */ }
504};
505
506MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
507
508/*-------------------------------------------------------------------------*/
509
510static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
511{
512	struct device_node *np = pdev->dev.of_node;
513	struct at91_usbh_data	*pdata;
514	int			i;
515	int			ret;
516	int			err;
517	u32			ports;
518
519	/* Right now device-tree probed devices don't get dma_mask set.
520	 * Since shared usb code relies on it, set it here for now.
521	 * Once we have dma capability bindings this can go away.
522	 */
523	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
524	if (ret)
525		return ret;
526
527	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
528	if (!pdata)
529		return -ENOMEM;
530
531	pdev->dev.platform_data = pdata;
532
533	if (!of_property_read_u32(np, "num-ports", &ports))
534		pdata->ports = ports;
535
536	at91_for_each_port(i) {
537		if (i >= pdata->ports)
538			break;
539
540		pdata->vbus_pin[i] =
541			devm_gpiod_get_index_optional(&pdev->dev, "atmel,vbus",
542						      i, GPIOD_OUT_HIGH);
543		if (IS_ERR(pdata->vbus_pin[i])) {
544			err = PTR_ERR(pdata->vbus_pin[i]);
545			dev_err(&pdev->dev, "unable to claim gpio \"vbus\": %d\n", err);
546			continue;
547		}
548	}
549
550	at91_for_each_port(i) {
551		if (i >= pdata->ports)
552			break;
553
554		pdata->overcurrent_pin[i] =
555			devm_gpiod_get_index_optional(&pdev->dev, "atmel,oc",
556						      i, GPIOD_IN);
557		if (!pdata->overcurrent_pin[i])
558			continue;
559		if (IS_ERR(pdata->overcurrent_pin[i])) {
560			err = PTR_ERR(pdata->overcurrent_pin[i]);
561			dev_err(&pdev->dev, "unable to claim gpio \"overcurrent\": %d\n", err);
562			continue;
563		}
564
565		ret = devm_request_irq(&pdev->dev,
566				       gpiod_to_irq(pdata->overcurrent_pin[i]),
567				       ohci_hcd_at91_overcurrent_irq,
568				       IRQF_SHARED,
569				       "ohci_overcurrent", pdev);
570		if (ret)
571			dev_info(&pdev->dev, "failed to request gpio \"overcurrent\" IRQ\n");
572	}
573
574	device_init_wakeup(&pdev->dev, 1);
575	return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
576}
577
578static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
579{
580	struct at91_usbh_data	*pdata = dev_get_platdata(&pdev->dev);
581	int			i;
582
583	if (pdata) {
584		at91_for_each_port(i)
585			ohci_at91_usb_set_power(pdata, i, 0);
586	}
587
588	device_init_wakeup(&pdev->dev, 0);
589	usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
590	return 0;
591}
592
593static int __maybe_unused
594ohci_hcd_at91_drv_suspend(struct device *dev)
595{
596	struct usb_hcd	*hcd = dev_get_drvdata(dev);
597	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);
598	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
599	int		ret;
600
601	/*
602	 * Disable wakeup if we are going to sleep with slow clock mode
603	 * enabled.
604	 */
605	ohci_at91->wakeup = device_may_wakeup(dev)
606			&& !at91_suspend_entering_slow_clock();
607
608	if (ohci_at91->wakeup)
609		enable_irq_wake(hcd->irq);
610
611	ohci_at91_port_suspend(ohci_at91->sfr_regmap, 1);
612
613	ret = ohci_suspend(hcd, ohci_at91->wakeup);
614	if (ret) {
615		if (ohci_at91->wakeup)
616			disable_irq_wake(hcd->irq);
617		return ret;
618	}
619	/*
620	 * The integrated transceivers seem unable to notice disconnect,
621	 * reconnect, or wakeup without the 48 MHz clock active.  so for
622	 * correctness, always discard connection state (using reset).
623	 *
624	 * REVISIT: some boards will be able to turn VBUS off...
625	 */
626	if (!ohci_at91->wakeup) {
627		ohci->rh_state = OHCI_RH_HALTED;
628
629		/* flush the writes */
630		(void) ohci_readl (ohci, &ohci->regs->control);
631		at91_stop_clock(ohci_at91);
632	}
633
634	return ret;
635}
636
637static int __maybe_unused
638ohci_hcd_at91_drv_resume(struct device *dev)
639{
640	struct usb_hcd	*hcd = dev_get_drvdata(dev);
641	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
642
643	if (ohci_at91->wakeup)
644		disable_irq_wake(hcd->irq);
645
646	at91_start_clock(ohci_at91);
647
648	ohci_resume(hcd, false);
649
650	ohci_at91_port_suspend(ohci_at91->sfr_regmap, 0);
651
652	return 0;
653}
654
655static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
656					ohci_hcd_at91_drv_resume);
657
658static struct platform_driver ohci_hcd_at91_driver = {
659	.probe		= ohci_hcd_at91_drv_probe,
660	.remove		= ohci_hcd_at91_drv_remove,
661	.shutdown	= usb_hcd_platform_shutdown,
662	.driver		= {
663		.name	= "at91_ohci",
664		.pm	= &ohci_hcd_at91_pm_ops,
665		.of_match_table	= at91_ohci_dt_ids,
666	},
667};
668
669static int __init ohci_at91_init(void)
670{
671	if (usb_disabled())
672		return -ENODEV;
673
674	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
675	ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
676
677	/*
678	 * The Atmel HW has some unusual quirks, which require Atmel-specific
679	 * workarounds. We override certain hc_driver functions here to
680	 * achieve that. We explicitly do not enhance ohci_driver_overrides to
681	 * allow this more easily, since this is an unusual case, and we don't
682	 * want to encourage others to override these functions by making it
683	 * too easy.
684	 */
685
686	ohci_at91_hc_driver.hub_status_data	= ohci_at91_hub_status_data;
687	ohci_at91_hc_driver.hub_control		= ohci_at91_hub_control;
688
689	return platform_driver_register(&ohci_hcd_at91_driver);
690}
691module_init(ohci_at91_init);
692
693static void __exit ohci_at91_cleanup(void)
694{
695	platform_driver_unregister(&ohci_hcd_at91_driver);
696}
697module_exit(ohci_at91_cleanup);
698
699MODULE_DESCRIPTION(DRIVER_DESC);
700MODULE_LICENSE("GPL");
701MODULE_ALIAS("platform:at91_ohci");
v4.17
  1// SPDX-License-Identifier: GPL-1.0+
  2/*
  3 * OHCI HCD (Host Controller Driver) for USB.
  4 *
  5 *  Copyright (C) 2004 SAN People (Pty) Ltd.
  6 *  Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
  7 *
  8 * AT91 Bus Glue
  9 *
 10 * Based on fragments of 2.4 driver by Rick Bronson.
 11 * Based on ohci-omap.c
 12 *
 13 * This file is licenced under the GPL.
 14 */
 15
 16#include <linux/clk.h>
 17#include <linux/dma-mapping.h>
 18#include <linux/gpio/consumer.h>
 19#include <linux/of_platform.h>
 20#include <linux/platform_device.h>
 21#include <linux/platform_data/atmel.h>
 22#include <linux/io.h>
 23#include <linux/kernel.h>
 24#include <linux/module.h>
 25#include <linux/mfd/syscon.h>
 26#include <linux/regmap.h>
 27#include <linux/usb.h>
 28#include <linux/usb/hcd.h>
 29#include <soc/at91/atmel-sfr.h>
 30
 31#include "ohci.h"
 32
 33#define valid_port(index)	((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
 34#define at91_for_each_port(index)	\
 35		for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
 36
 37/* interface, function and usb clocks; sometimes also an AHB clock */
 38#define hcd_to_ohci_at91_priv(h) \
 39	((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
 40
 41#define AT91_MAX_USBH_PORTS	3
 42struct at91_usbh_data {
 43	struct gpio_desc *vbus_pin[AT91_MAX_USBH_PORTS];
 44	struct gpio_desc *overcurrent_pin[AT91_MAX_USBH_PORTS];
 45	u8 ports;				/* number of ports on root hub */
 46	u8 overcurrent_supported;
 47	u8 overcurrent_status[AT91_MAX_USBH_PORTS];
 48	u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
 49};
 50
 51struct ohci_at91_priv {
 52	struct clk *iclk;
 53	struct clk *fclk;
 54	struct clk *hclk;
 55	bool clocked;
 56	bool wakeup;		/* Saved wake-up state for resume */
 57	struct regmap *sfr_regmap;
 58};
 59/* interface and function clocks; sometimes also an AHB clock */
 60
 61#define DRIVER_DESC "OHCI Atmel driver"
 62
 63static const char hcd_name[] = "ohci-atmel";
 64
 65static struct hc_driver __read_mostly ohci_at91_hc_driver;
 66
 67static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
 68	.extra_priv_size = sizeof(struct ohci_at91_priv),
 69};
 70
 71/*-------------------------------------------------------------------------*/
 72
 73static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
 74{
 75	if (ohci_at91->clocked)
 76		return;
 77
 78	clk_set_rate(ohci_at91->fclk, 48000000);
 79	clk_prepare_enable(ohci_at91->hclk);
 80	clk_prepare_enable(ohci_at91->iclk);
 81	clk_prepare_enable(ohci_at91->fclk);
 82	ohci_at91->clocked = true;
 83}
 84
 85static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
 86{
 87	if (!ohci_at91->clocked)
 88		return;
 89
 90	clk_disable_unprepare(ohci_at91->fclk);
 91	clk_disable_unprepare(ohci_at91->iclk);
 92	clk_disable_unprepare(ohci_at91->hclk);
 93	ohci_at91->clocked = false;
 94}
 95
 96static void at91_start_hc(struct platform_device *pdev)
 97{
 98	struct usb_hcd *hcd = platform_get_drvdata(pdev);
 99	struct ohci_regs __iomem *regs = hcd->regs;
100	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
101
102	dev_dbg(&pdev->dev, "start\n");
103
104	/*
105	 * Start the USB clocks.
106	 */
107	at91_start_clock(ohci_at91);
108
109	/*
110	 * The USB host controller must remain in reset.
111	 */
112	writel(0, &regs->control);
113}
114
115static void at91_stop_hc(struct platform_device *pdev)
116{
117	struct usb_hcd *hcd = platform_get_drvdata(pdev);
118	struct ohci_regs __iomem *regs = hcd->regs;
119	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
120
121	dev_dbg(&pdev->dev, "stop\n");
122
123	/*
124	 * Put the USB host controller into reset.
125	 */
126	writel(0, &regs->control);
127
128	/*
129	 * Stop the USB clocks.
130	 */
131	at91_stop_clock(ohci_at91);
132}
133
134
135/*-------------------------------------------------------------------------*/
136
137static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
138
139static struct regmap *at91_dt_syscon_sfr(void)
140{
141	struct regmap *regmap;
142
143	regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
144	if (IS_ERR(regmap))
145		regmap = NULL;
 
 
 
146
147	return regmap;
148}
149
150/* configure so an HC device and id are always provided */
151/* always called with process context; sleeping is OK */
152
153
154/**
155 * usb_hcd_at91_probe - initialize AT91-based HCDs
156 * Context: !in_interrupt()
157 *
158 * Allocates basic resources for this USB host controller, and
159 * then invokes the start() method for the HCD associated with it
160 * through the hotplug entry's driver_data.
161 */
162static int usb_hcd_at91_probe(const struct hc_driver *driver,
163			struct platform_device *pdev)
164{
165	struct at91_usbh_data *board;
166	struct ohci_hcd *ohci;
167	int retval;
168	struct usb_hcd *hcd;
169	struct ohci_at91_priv *ohci_at91;
170	struct device *dev = &pdev->dev;
171	struct resource *res;
172	int irq;
173
174	irq = platform_get_irq(pdev, 0);
175	if (irq < 0) {
176		dev_dbg(dev, "hcd probe: missing irq resource\n");
177		return irq;
178	}
179
180	hcd = usb_create_hcd(driver, dev, "at91");
181	if (!hcd)
182		return -ENOMEM;
183	ohci_at91 = hcd_to_ohci_at91_priv(hcd);
184
185	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
186	hcd->regs = devm_ioremap_resource(dev, res);
187	if (IS_ERR(hcd->regs)) {
188		retval = PTR_ERR(hcd->regs);
189		goto err;
190	}
191	hcd->rsrc_start = res->start;
192	hcd->rsrc_len = resource_size(res);
193
194	ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
195	if (IS_ERR(ohci_at91->iclk)) {
196		dev_err(dev, "failed to get ohci_clk\n");
197		retval = PTR_ERR(ohci_at91->iclk);
198		goto err;
199	}
200	ohci_at91->fclk = devm_clk_get(dev, "uhpck");
201	if (IS_ERR(ohci_at91->fclk)) {
202		dev_err(dev, "failed to get uhpck\n");
203		retval = PTR_ERR(ohci_at91->fclk);
204		goto err;
205	}
206	ohci_at91->hclk = devm_clk_get(dev, "hclk");
207	if (IS_ERR(ohci_at91->hclk)) {
208		dev_err(dev, "failed to get hclk\n");
209		retval = PTR_ERR(ohci_at91->hclk);
210		goto err;
211	}
212
213	ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
214	if (!ohci_at91->sfr_regmap)
215		dev_warn(dev, "failed to find sfr node\n");
216
217	board = hcd->self.controller->platform_data;
218	ohci = hcd_to_ohci(hcd);
219	ohci->num_ports = board->ports;
220	at91_start_hc(pdev);
221
222	/*
223	 * The RemoteWakeupConnected bit has to be set explicitly
224	 * before calling ohci_run. The reset value of this bit is 0.
225	 */
226	ohci->hc_control = OHCI_CTRL_RWC;
227
228	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
229	if (retval == 0) {
230		device_wakeup_enable(hcd->self.controller);
231		return retval;
232	}
233
234	/* Error handling */
235	at91_stop_hc(pdev);
236
237 err:
238	usb_put_hcd(hcd);
239	return retval;
240}
241
242
243/* may be called with controller, bus, and devices active */
244
245/**
246 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
247 * @dev: USB Host Controller being removed
248 * Context: !in_interrupt()
249 *
250 * Reverses the effect of usb_hcd_at91_probe(), first invoking
251 * the HCD's stop() method.  It is always called from a thread
252 * context, "rmmod" or something similar.
253 *
254 */
255static void usb_hcd_at91_remove(struct usb_hcd *hcd,
256				struct platform_device *pdev)
257{
258	usb_remove_hcd(hcd);
259	at91_stop_hc(pdev);
260	usb_put_hcd(hcd);
261}
262
263/*-------------------------------------------------------------------------*/
264static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
265{
266	if (!valid_port(port))
267		return;
268
269	gpiod_set_value(pdata->vbus_pin[port], enable);
270}
271
272static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
273{
274	if (!valid_port(port))
275		return -EINVAL;
276
277	return gpiod_get_value(pdata->vbus_pin[port]);
278}
279
280/*
281 * Update the status data from the hub with the over-current indicator change.
282 */
283static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
284{
285	struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
286	int length = ohci_hub_status_data(hcd, buf);
287	int port;
288
289	at91_for_each_port(port) {
290		if (pdata->overcurrent_changed[port]) {
291			if (!length)
292				length = 1;
293			buf[0] |= 1 << (port + 1);
294		}
295	}
296
297	return length;
298}
299
300static int ohci_at91_port_suspend(struct regmap *regmap, u8 set)
301{
302	u32 regval;
303	int ret;
304
305	if (!regmap)
306		return 0;
307
308	ret = regmap_read(regmap, AT91_SFR_OHCIICR, &regval);
309	if (ret)
310		return ret;
311
312	if (set)
313		regval |= AT91_OHCIICR_USB_SUSPEND;
314	else
315		regval &= ~AT91_OHCIICR_USB_SUSPEND;
316
317	regmap_write(regmap, AT91_SFR_OHCIICR, regval);
318
319	return 0;
320}
321
322/*
323 * Look at the control requests to the root hub and see if we need to override.
324 */
325static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
326				 u16 wIndex, char *buf, u16 wLength)
327{
328	struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
329	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
330	struct usb_hub_descriptor *desc;
331	int ret = -EINVAL;
332	u32 *data = (u32 *)buf;
333
334	dev_dbg(hcd->self.controller,
335		"ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
336		hcd, typeReq, wValue, wIndex, buf, wLength);
337
338	wIndex--;
339
340	switch (typeReq) {
341	case SetPortFeature:
342		switch (wValue) {
343		case USB_PORT_FEAT_POWER:
344			dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
345			if (valid_port(wIndex)) {
346				ohci_at91_usb_set_power(pdata, wIndex, 1);
347				ret = 0;
348			}
349
350			goto out;
351
352		case USB_PORT_FEAT_SUSPEND:
353			dev_dbg(hcd->self.controller, "SetPortFeat: SUSPEND\n");
354			if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
355				ohci_at91_port_suspend(ohci_at91->sfr_regmap,
356						       1);
357				return 0;
358			}
359			break;
360		}
361		break;
362
363	case ClearPortFeature:
364		switch (wValue) {
365		case USB_PORT_FEAT_C_OVER_CURRENT:
366			dev_dbg(hcd->self.controller,
367				"ClearPortFeature: C_OVER_CURRENT\n");
368
369			if (valid_port(wIndex)) {
370				pdata->overcurrent_changed[wIndex] = 0;
371				pdata->overcurrent_status[wIndex] = 0;
372			}
373
374			goto out;
375
376		case USB_PORT_FEAT_OVER_CURRENT:
377			dev_dbg(hcd->self.controller,
378				"ClearPortFeature: OVER_CURRENT\n");
379
380			if (valid_port(wIndex))
381				pdata->overcurrent_status[wIndex] = 0;
382
383			goto out;
384
385		case USB_PORT_FEAT_POWER:
386			dev_dbg(hcd->self.controller,
387				"ClearPortFeature: POWER\n");
388
389			if (valid_port(wIndex)) {
390				ohci_at91_usb_set_power(pdata, wIndex, 0);
391				return 0;
392			}
393			break;
394
395		case USB_PORT_FEAT_SUSPEND:
396			dev_dbg(hcd->self.controller, "ClearPortFeature: SUSPEND\n");
397			if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
398				ohci_at91_port_suspend(ohci_at91->sfr_regmap,
399						       0);
400				return 0;
401			}
402			break;
403		}
404		break;
405	}
406
407	ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
408	if (ret)
409		goto out;
410
411	switch (typeReq) {
412	case GetHubDescriptor:
413
414		/* update the hub's descriptor */
415
416		desc = (struct usb_hub_descriptor *)buf;
417
418		dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
419			desc->wHubCharacteristics);
420
421		/* remove the old configurations for power-switching, and
422		 * over-current protection, and insert our new configuration
423		 */
424
425		desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
426		desc->wHubCharacteristics |=
427			cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
428
429		if (pdata->overcurrent_supported) {
430			desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
431			desc->wHubCharacteristics |=
432				cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
433		}
434
435		dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
436			desc->wHubCharacteristics);
437
438		return ret;
439
440	case GetPortStatus:
441		/* check port status */
442
443		dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
444
445		if (valid_port(wIndex)) {
446			if (!ohci_at91_usb_get_power(pdata, wIndex))
447				*data &= ~cpu_to_le32(RH_PS_PPS);
448
449			if (pdata->overcurrent_changed[wIndex])
450				*data |= cpu_to_le32(RH_PS_OCIC);
451
452			if (pdata->overcurrent_status[wIndex])
453				*data |= cpu_to_le32(RH_PS_POCI);
454		}
455	}
456
457 out:
458	return ret;
459}
460
461/*-------------------------------------------------------------------------*/
462
463static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
464{
465	struct platform_device *pdev = data;
466	struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
467	int val, port;
468
469	/* From the GPIO notifying the over-current situation, find
470	 * out the corresponding port */
471	at91_for_each_port(port) {
472		if (gpiod_to_irq(pdata->overcurrent_pin[port]) == irq)
473			break;
474	}
475
476	if (port == AT91_MAX_USBH_PORTS) {
477		dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
478		return IRQ_HANDLED;
479	}
480
481	val = gpiod_get_value(pdata->overcurrent_pin[port]);
482
483	/* When notified of an over-current situation, disable power
484	   on the corresponding port, and mark this port in
485	   over-current. */
486	if (!val) {
487		ohci_at91_usb_set_power(pdata, port, 0);
488		pdata->overcurrent_status[port]  = 1;
489		pdata->overcurrent_changed[port] = 1;
490	}
491
492	dev_dbg(& pdev->dev, "overcurrent situation %s\n",
493		val ? "exited" : "notified");
494
495	return IRQ_HANDLED;
496}
497
498static const struct of_device_id at91_ohci_dt_ids[] = {
499	{ .compatible = "atmel,at91rm9200-ohci" },
500	{ /* sentinel */ }
501};
502
503MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
504
505/*-------------------------------------------------------------------------*/
506
507static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
508{
509	struct device_node *np = pdev->dev.of_node;
510	struct at91_usbh_data	*pdata;
511	int			i;
512	int			ret;
513	int			err;
514	u32			ports;
515
516	/* Right now device-tree probed devices don't get dma_mask set.
517	 * Since shared usb code relies on it, set it here for now.
518	 * Once we have dma capability bindings this can go away.
519	 */
520	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
521	if (ret)
522		return ret;
523
524	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
525	if (!pdata)
526		return -ENOMEM;
527
528	pdev->dev.platform_data = pdata;
529
530	if (!of_property_read_u32(np, "num-ports", &ports))
531		pdata->ports = ports;
532
533	at91_for_each_port(i) {
534		if (i >= pdata->ports)
535			break;
536
537		pdata->vbus_pin[i] =
538			devm_gpiod_get_index_optional(&pdev->dev, "atmel,vbus",
539						      i, GPIOD_OUT_HIGH);
540		if (IS_ERR(pdata->vbus_pin[i])) {
541			err = PTR_ERR(pdata->vbus_pin[i]);
542			dev_err(&pdev->dev, "unable to claim gpio \"vbus\": %d\n", err);
543			continue;
544		}
545	}
546
547	at91_for_each_port(i) {
548		if (i >= pdata->ports)
549			break;
550
551		pdata->overcurrent_pin[i] =
552			devm_gpiod_get_index_optional(&pdev->dev, "atmel,oc",
553						      i, GPIOD_IN);
 
 
554		if (IS_ERR(pdata->overcurrent_pin[i])) {
555			err = PTR_ERR(pdata->overcurrent_pin[i]);
556			dev_err(&pdev->dev, "unable to claim gpio \"overcurrent\": %d\n", err);
557			continue;
558		}
559
560		ret = devm_request_irq(&pdev->dev,
561				       gpiod_to_irq(pdata->overcurrent_pin[i]),
562				       ohci_hcd_at91_overcurrent_irq,
563				       IRQF_SHARED,
564				       "ohci_overcurrent", pdev);
565		if (ret)
566			dev_info(&pdev->dev, "failed to request gpio \"overcurrent\" IRQ\n");
567	}
568
569	device_init_wakeup(&pdev->dev, 1);
570	return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
571}
572
573static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
574{
575	struct at91_usbh_data	*pdata = dev_get_platdata(&pdev->dev);
576	int			i;
577
578	if (pdata) {
579		at91_for_each_port(i)
580			ohci_at91_usb_set_power(pdata, i, 0);
581	}
582
583	device_init_wakeup(&pdev->dev, 0);
584	usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
585	return 0;
586}
587
588static int __maybe_unused
589ohci_hcd_at91_drv_suspend(struct device *dev)
590{
591	struct usb_hcd	*hcd = dev_get_drvdata(dev);
592	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);
593	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
594	int		ret;
595
596	/*
597	 * Disable wakeup if we are going to sleep with slow clock mode
598	 * enabled.
599	 */
600	ohci_at91->wakeup = device_may_wakeup(dev)
601			&& !at91_suspend_entering_slow_clock();
602
603	if (ohci_at91->wakeup)
604		enable_irq_wake(hcd->irq);
605
606	ohci_at91_port_suspend(ohci_at91->sfr_regmap, 1);
607
608	ret = ohci_suspend(hcd, ohci_at91->wakeup);
609	if (ret) {
610		if (ohci_at91->wakeup)
611			disable_irq_wake(hcd->irq);
612		return ret;
613	}
614	/*
615	 * The integrated transceivers seem unable to notice disconnect,
616	 * reconnect, or wakeup without the 48 MHz clock active.  so for
617	 * correctness, always discard connection state (using reset).
618	 *
619	 * REVISIT: some boards will be able to turn VBUS off...
620	 */
621	if (!ohci_at91->wakeup) {
622		ohci->rh_state = OHCI_RH_HALTED;
623
624		/* flush the writes */
625		(void) ohci_readl (ohci, &ohci->regs->control);
626		at91_stop_clock(ohci_at91);
627	}
628
629	return ret;
630}
631
632static int __maybe_unused
633ohci_hcd_at91_drv_resume(struct device *dev)
634{
635	struct usb_hcd	*hcd = dev_get_drvdata(dev);
636	struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
637
638	if (ohci_at91->wakeup)
639		disable_irq_wake(hcd->irq);
640
641	at91_start_clock(ohci_at91);
642
643	ohci_resume(hcd, false);
644
645	ohci_at91_port_suspend(ohci_at91->sfr_regmap, 0);
646
647	return 0;
648}
649
650static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
651					ohci_hcd_at91_drv_resume);
652
653static struct platform_driver ohci_hcd_at91_driver = {
654	.probe		= ohci_hcd_at91_drv_probe,
655	.remove		= ohci_hcd_at91_drv_remove,
656	.shutdown	= usb_hcd_platform_shutdown,
657	.driver		= {
658		.name	= "at91_ohci",
659		.pm	= &ohci_hcd_at91_pm_ops,
660		.of_match_table	= at91_ohci_dt_ids,
661	},
662};
663
664static int __init ohci_at91_init(void)
665{
666	if (usb_disabled())
667		return -ENODEV;
668
669	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
670	ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
671
672	/*
673	 * The Atmel HW has some unusual quirks, which require Atmel-specific
674	 * workarounds. We override certain hc_driver functions here to
675	 * achieve that. We explicitly do not enhance ohci_driver_overrides to
676	 * allow this more easily, since this is an unusual case, and we don't
677	 * want to encourage others to override these functions by making it
678	 * too easy.
679	 */
680
681	ohci_at91_hc_driver.hub_status_data	= ohci_at91_hub_status_data;
682	ohci_at91_hc_driver.hub_control		= ohci_at91_hub_control;
683
684	return platform_driver_register(&ohci_hcd_at91_driver);
685}
686module_init(ohci_at91_init);
687
688static void __exit ohci_at91_cleanup(void)
689{
690	platform_driver_unregister(&ohci_hcd_at91_driver);
691}
692module_exit(ohci_at91_cleanup);
693
694MODULE_DESCRIPTION(DRIVER_DESC);
695MODULE_LICENSE("GPL");
696MODULE_ALIAS("platform:at91_ohci");