• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * host.c - ChipIdea USB host controller driver
4  *
5  * Copyright (c) 2012 Intel Corporation
6  *
7  * Author: Alexander Shishkin
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/usb.h>
13 #include <linux/usb/hcd.h>
14 #include <linux/usb/chipidea.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/pinctrl/consumer.h>
17 
18 #include "../host/ehci.h"
19 
20 #include "ci.h"
21 #include "bits.h"
22 #include "host.h"
23 
24 static struct hc_driver __read_mostly ci_ehci_hc_driver;
25 static int (*orig_bus_suspend)(struct usb_hcd *hcd);
26 
27 struct ehci_ci_priv {
28 	struct regulator *reg_vbus;
29 	bool enabled;
30 };
31 
32 struct ci_hdrc_dma_aligned_buffer {
33 	void *original_buffer;
34 	u8 data[];
35 };
36 
ehci_ci_portpower(struct usb_hcd * hcd,int portnum,bool enable)37 static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
38 {
39 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
40 	struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
41 	struct device *dev = hcd->self.controller;
42 	struct ci_hdrc *ci = dev_get_drvdata(dev);
43 	int ret = 0;
44 	int port = HCS_N_PORTS(ehci->hcs_params);
45 
46 	if (priv->reg_vbus && enable != priv->enabled) {
47 		if (port > 1) {
48 			dev_warn(dev,
49 				"Not support multi-port regulator control\n");
50 			return 0;
51 		}
52 		if (enable)
53 			ret = regulator_enable(priv->reg_vbus);
54 		else
55 			ret = regulator_disable(priv->reg_vbus);
56 		if (ret) {
57 			dev_err(dev,
58 				"Failed to %s vbus regulator, ret=%d\n",
59 				enable ? "enable" : "disable", ret);
60 			return ret;
61 		}
62 		priv->enabled = enable;
63 	}
64 
65 	if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
66 		/*
67 		 * Marvell 28nm HSIC PHY requires forcing the port to HS mode.
68 		 * As HSIC is always HS, this should be safe for others.
69 		 */
70 		hw_port_test_set(ci, 5);
71 		hw_port_test_set(ci, 0);
72 	}
73 	return 0;
74 };
75 
ehci_ci_reset(struct usb_hcd * hcd)76 static int ehci_ci_reset(struct usb_hcd *hcd)
77 {
78 	struct device *dev = hcd->self.controller;
79 	struct ci_hdrc *ci = dev_get_drvdata(dev);
80 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
81 	int ret;
82 
83 	ret = ehci_setup(hcd);
84 	if (ret)
85 		return ret;
86 
87 	ehci->need_io_watchdog = 0;
88 
89 	if (ci->platdata->notify_event) {
90 		ret = ci->platdata->notify_event(ci,
91 				CI_HDRC_CONTROLLER_RESET_EVENT);
92 		if (ret)
93 			return ret;
94 	}
95 
96 	ci_platform_configure(ci);
97 
98 	return ret;
99 }
100 
101 static const struct ehci_driver_overrides ehci_ci_overrides = {
102 	.extra_priv_size = sizeof(struct ehci_ci_priv),
103 	.port_power	 = ehci_ci_portpower,
104 	.reset		 = ehci_ci_reset,
105 };
106 
host_irq(struct ci_hdrc * ci)107 static irqreturn_t host_irq(struct ci_hdrc *ci)
108 {
109 	return usb_hcd_irq(ci->irq, ci->hcd);
110 }
111 
host_start(struct ci_hdrc * ci)112 static int host_start(struct ci_hdrc *ci)
113 {
114 	struct usb_hcd *hcd;
115 	struct ehci_hcd *ehci;
116 	struct ehci_ci_priv *priv;
117 	int ret;
118 
119 	if (usb_disabled())
120 		return -ENODEV;
121 
122 	hcd = __usb_create_hcd(&ci_ehci_hc_driver, ci->dev->parent,
123 			       ci->dev, dev_name(ci->dev), NULL);
124 	if (!hcd)
125 		return -ENOMEM;
126 
127 	dev_set_drvdata(ci->dev, ci);
128 	hcd->rsrc_start = ci->hw_bank.phys;
129 	hcd->rsrc_len = ci->hw_bank.size;
130 	hcd->regs = ci->hw_bank.abs;
131 	hcd->has_tt = 1;
132 
133 	hcd->power_budget = ci->platdata->power_budget;
134 	hcd->tpl_support = ci->platdata->tpl_support;
135 	if (ci->phy || ci->usb_phy) {
136 		hcd->skip_phy_initialization = 1;
137 		if (ci->usb_phy)
138 			hcd->usb_phy = ci->usb_phy;
139 	}
140 
141 	ehci = hcd_to_ehci(hcd);
142 	ehci->caps = ci->hw_bank.cap;
143 	ehci->has_hostpc = ci->hw_bank.lpm;
144 	ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
145 	ehci->imx28_write_fix = ci->imx28_write_fix;
146 
147 	priv = (struct ehci_ci_priv *)ehci->priv;
148 	priv->reg_vbus = NULL;
149 
150 	if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
151 		if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
152 			ret = regulator_enable(ci->platdata->reg_vbus);
153 			if (ret) {
154 				dev_err(ci->dev,
155 				"Failed to enable vbus regulator, ret=%d\n",
156 									ret);
157 				goto put_hcd;
158 			}
159 		} else {
160 			priv->reg_vbus = ci->platdata->reg_vbus;
161 		}
162 	}
163 
164 	if (ci->platdata->pins_host)
165 		pinctrl_select_state(ci->platdata->pctl,
166 				     ci->platdata->pins_host);
167 
168 	ci->hcd = hcd;
169 
170 	ret = usb_add_hcd(hcd, 0, 0);
171 	if (ret) {
172 		ci->hcd = NULL;
173 		goto disable_reg;
174 	} else {
175 		struct usb_otg *otg = &ci->otg;
176 
177 		if (ci_otg_is_fsm_mode(ci)) {
178 			otg->host = &hcd->self;
179 			hcd->self.otg_port = 1;
180 		}
181 
182 		if (ci->platdata->notify_event &&
183 			(ci->platdata->flags & CI_HDRC_IMX_IS_HSIC))
184 			ci->platdata->notify_event
185 				(ci, CI_HDRC_IMX_HSIC_ACTIVE_EVENT);
186 	}
187 
188 	return ret;
189 
190 disable_reg:
191 	if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
192 			(ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
193 		regulator_disable(ci->platdata->reg_vbus);
194 put_hcd:
195 	usb_put_hcd(hcd);
196 
197 	return ret;
198 }
199 
host_stop(struct ci_hdrc * ci)200 static void host_stop(struct ci_hdrc *ci)
201 {
202 	struct usb_hcd *hcd = ci->hcd;
203 
204 	if (hcd) {
205 		if (ci->platdata->notify_event)
206 			ci->platdata->notify_event(ci,
207 				CI_HDRC_CONTROLLER_STOPPED_EVENT);
208 		usb_remove_hcd(hcd);
209 		ci->role = CI_ROLE_END;
210 		synchronize_irq(ci->irq);
211 		usb_put_hcd(hcd);
212 		if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
213 			(ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
214 				regulator_disable(ci->platdata->reg_vbus);
215 	}
216 	ci->hcd = NULL;
217 	ci->otg.host = NULL;
218 
219 	if (ci->platdata->pins_host && ci->platdata->pins_default)
220 		pinctrl_select_state(ci->platdata->pctl,
221 				     ci->platdata->pins_default);
222 }
223 
224 
ci_hdrc_host_destroy(struct ci_hdrc * ci)225 void ci_hdrc_host_destroy(struct ci_hdrc *ci)
226 {
227 	if (ci->role == CI_ROLE_HOST && ci->hcd)
228 		host_stop(ci);
229 }
230 
231 /* The below code is based on tegra ehci driver */
ci_ehci_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)232 static int ci_ehci_hub_control(
233 	struct usb_hcd	*hcd,
234 	u16		typeReq,
235 	u16		wValue,
236 	u16		wIndex,
237 	char		*buf,
238 	u16		wLength
239 )
240 {
241 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
242 	unsigned int	ports = HCS_N_PORTS(ehci->hcs_params);
243 	u32 __iomem	*status_reg;
244 	u32		temp, port_index;
245 	unsigned long	flags;
246 	int		retval = 0;
247 	bool		done = false;
248 	struct device *dev = hcd->self.controller;
249 	struct ci_hdrc *ci = dev_get_drvdata(dev);
250 
251 	port_index = wIndex & 0xff;
252 	port_index -= (port_index > 0);
253 	status_reg = &ehci->regs->port_status[port_index];
254 
255 	spin_lock_irqsave(&ehci->lock, flags);
256 
257 	if (ci->platdata->hub_control) {
258 		retval = ci->platdata->hub_control(ci, typeReq, wValue, wIndex,
259 						   buf, wLength, &done, &flags);
260 		if (done)
261 			goto done;
262 	}
263 
264 	if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
265 		if (!wIndex || wIndex > ports) {
266 			retval = -EPIPE;
267 			goto done;
268 		}
269 
270 		temp = ehci_readl(ehci, status_reg);
271 		if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
272 			retval = -EPIPE;
273 			goto done;
274 		}
275 
276 		temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
277 		temp |= PORT_WKDISC_E | PORT_WKOC_E;
278 		ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
279 
280 		/*
281 		 * If a transaction is in progress, there may be a delay in
282 		 * suspending the port. Poll until the port is suspended.
283 		 */
284 		if (ehci_handshake(ehci, status_reg, PORT_SUSPEND,
285 			PORT_SUSPEND, 5000))
286 			ehci_err(ehci, "timeout waiting for SUSPEND\n");
287 
288 		if (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC) {
289 			if (ci->platdata->notify_event)
290 				ci->platdata->notify_event(ci,
291 					CI_HDRC_IMX_HSIC_SUSPEND_EVENT);
292 
293 			temp = ehci_readl(ehci, status_reg);
294 			temp &= ~(PORT_WKDISC_E | PORT_WKCONN_E);
295 			ehci_writel(ehci, temp, status_reg);
296 		}
297 
298 		set_bit(port_index, &ehci->suspended_ports);
299 		goto done;
300 	}
301 
302 	/*
303 	 * After resume has finished, it needs do some post resume
304 	 * operation for some SoCs.
305 	 */
306 	else if (typeReq == ClearPortFeature &&
307 		wValue == USB_PORT_FEAT_C_SUSPEND) {
308 		/* Make sure the resume has finished, it should be finished */
309 		if (ehci_handshake(ehci, status_reg, PORT_RESUME, 0, 25000))
310 			ehci_err(ehci, "timeout waiting for resume\n");
311 	}
312 
313 	spin_unlock_irqrestore(&ehci->lock, flags);
314 
315 	/* Handle the hub control events here */
316 	return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
317 done:
318 	spin_unlock_irqrestore(&ehci->lock, flags);
319 	return retval;
320 }
ci_ehci_bus_suspend(struct usb_hcd * hcd)321 static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
322 {
323 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
324 	struct device *dev = hcd->self.controller;
325 	struct ci_hdrc *ci = dev_get_drvdata(dev);
326 	int port;
327 	u32 tmp;
328 
329 	int ret = orig_bus_suspend(hcd);
330 
331 	if (ret)
332 		return ret;
333 
334 	port = HCS_N_PORTS(ehci->hcs_params);
335 	while (port--) {
336 		u32 __iomem *reg = &ehci->regs->port_status[port];
337 		u32 portsc = ehci_readl(ehci, reg);
338 
339 		if (portsc & PORT_CONNECT) {
340 			/*
341 			 * For chipidea, the resume signal will be ended
342 			 * automatically, so for remote wakeup case, the
343 			 * usbcmd.rs may not be set before the resume has
344 			 * ended if other resume paths consumes too much
345 			 * time (~24ms), in that case, the SOF will not
346 			 * send out within 3ms after resume ends, then the
347 			 * high speed device will enter full speed mode.
348 			 */
349 
350 			tmp = ehci_readl(ehci, &ehci->regs->command);
351 			tmp |= CMD_RUN;
352 			ehci_writel(ehci, tmp, &ehci->regs->command);
353 			/*
354 			 * It needs a short delay between set RS bit and PHCD.
355 			 */
356 			usleep_range(150, 200);
357 			/*
358 			 * Need to clear WKCN and WKOC for imx HSIC,
359 			 * otherwise, there will be wakeup event.
360 			 */
361 			if (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC) {
362 				tmp = ehci_readl(ehci, reg);
363 				tmp &= ~(PORT_WKDISC_E | PORT_WKCONN_E);
364 				ehci_writel(ehci, tmp, reg);
365 			}
366 
367 			break;
368 		}
369 	}
370 
371 	return 0;
372 }
373 
ci_hdrc_free_dma_aligned_buffer(struct urb * urb,bool copy_back)374 static void ci_hdrc_free_dma_aligned_buffer(struct urb *urb, bool copy_back)
375 {
376 	struct ci_hdrc_dma_aligned_buffer *temp;
377 
378 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
379 		return;
380 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
381 
382 	temp = container_of(urb->transfer_buffer,
383 			    struct ci_hdrc_dma_aligned_buffer, data);
384 	urb->transfer_buffer = temp->original_buffer;
385 
386 	if (copy_back && usb_urb_dir_in(urb)) {
387 		size_t length;
388 
389 		if (usb_pipeisoc(urb->pipe))
390 			length = urb->transfer_buffer_length;
391 		else
392 			length = urb->actual_length;
393 
394 		memcpy(temp->original_buffer, temp->data, length);
395 	}
396 
397 	kfree(temp);
398 }
399 
ci_hdrc_alloc_dma_aligned_buffer(struct urb * urb,gfp_t mem_flags)400 static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
401 {
402 	struct ci_hdrc_dma_aligned_buffer *temp;
403 
404 	if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0)
405 		return 0;
406 	if (IS_ALIGNED((uintptr_t)urb->transfer_buffer, 4)
407 	    && IS_ALIGNED(urb->transfer_buffer_length, 4))
408 		return 0;
409 
410 	temp = kmalloc(sizeof(*temp) + ALIGN(urb->transfer_buffer_length, 4), mem_flags);
411 	if (!temp)
412 		return -ENOMEM;
413 
414 	if (usb_urb_dir_out(urb))
415 		memcpy(temp->data, urb->transfer_buffer,
416 		       urb->transfer_buffer_length);
417 
418 	temp->original_buffer = urb->transfer_buffer;
419 	urb->transfer_buffer = temp->data;
420 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
421 
422 	return 0;
423 }
424 
ci_hdrc_map_urb_for_dma(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)425 static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
426 				   gfp_t mem_flags)
427 {
428 	int ret;
429 
430 	ret = ci_hdrc_alloc_dma_aligned_buffer(urb, mem_flags);
431 	if (ret)
432 		return ret;
433 
434 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
435 	if (ret)
436 		ci_hdrc_free_dma_aligned_buffer(urb, false);
437 
438 	return ret;
439 }
440 
ci_hdrc_unmap_urb_for_dma(struct usb_hcd * hcd,struct urb * urb)441 static void ci_hdrc_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
442 {
443 	usb_hcd_unmap_urb_for_dma(hcd, urb);
444 	ci_hdrc_free_dma_aligned_buffer(urb, true);
445 }
446 
ci_hdrc_host_init(struct ci_hdrc * ci)447 int ci_hdrc_host_init(struct ci_hdrc *ci)
448 {
449 	struct ci_role_driver *rdrv;
450 
451 	if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
452 		return -ENXIO;
453 
454 	rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
455 	if (!rdrv)
456 		return -ENOMEM;
457 
458 	rdrv->start	= host_start;
459 	rdrv->stop	= host_stop;
460 	rdrv->irq	= host_irq;
461 	rdrv->name	= "host";
462 	ci->roles[CI_ROLE_HOST] = rdrv;
463 
464 	if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA) {
465 		ci_ehci_hc_driver.map_urb_for_dma = ci_hdrc_map_urb_for_dma;
466 		ci_ehci_hc_driver.unmap_urb_for_dma = ci_hdrc_unmap_urb_for_dma;
467 	}
468 
469 	return 0;
470 }
471 
ci_hdrc_host_driver_init(void)472 void ci_hdrc_host_driver_init(void)
473 {
474 	ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
475 	orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
476 	ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
477 	ci_ehci_hc_driver.hub_control = ci_ehci_hub_control;
478 }
479