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