Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: chipidea: tegra: Support host mode

Add USB host mode to the Tegra HDRC driver. This allows us to benefit from
support provided by the generic ChipIdea driver instead of duplicating the
effort in a separate ehci-tegra driver.

Tested-by: Matt Merhar <mattmerhar@protonmail.com>
Tested-by: Nicolas Chauvet <kwizart@gmail.com>
Tested-by: Ion Agorria <ion@agorria.com>
Acked-by: Thierry Reding <treding@nvidia.com>
Acked-by: Peter Chen <peter.chen@kernel.org>
Signed-off-by: Peter Geis <pgwipeout@gmail.com>
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Link: https://lore.kernel.org/r/20201218120246.7759-6-digetx@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Peter Geis and committed by
Greg Kroah-Hartman
fc53d527 711e2344

+356 -8
-1
drivers/usb/chipidea/Kconfig
··· 55 55 config USB_CHIPIDEA_TEGRA 56 56 tristate "Enable Tegra USB glue driver" if EMBEDDED 57 57 depends on OF 58 - depends on USB_CHIPIDEA_UDC 59 58 default USB_CHIPIDEA 60 59 61 60 endif
+239 -4
drivers/usb/chipidea/ci_hdrc_tegra.c
··· 4 4 */ 5 5 6 6 #include <linux/clk.h> 7 + #include <linux/io.h> 7 8 #include <linux/module.h> 8 9 #include <linux/of_device.h> 9 10 #include <linux/reset.h> 10 11 12 + #include <linux/usb.h> 11 13 #include <linux/usb/chipidea.h> 14 + #include <linux/usb/hcd.h> 15 + #include <linux/usb/of.h> 16 + #include <linux/usb/phy.h> 17 + 18 + #include "../host/ehci.h" 12 19 13 20 #include "ci.h" 14 21 ··· 23 16 struct ci_hdrc_platform_data data; 24 17 struct platform_device *dev; 25 18 19 + const struct tegra_usb_soc_info *soc; 26 20 struct usb_phy *phy; 27 21 struct clk *clk; 22 + 23 + bool needs_double_reset; 28 24 }; 29 25 30 26 struct tegra_usb_soc_info { 31 27 unsigned long flags; 28 + unsigned int txfifothresh; 29 + enum usb_dr_mode dr_mode; 30 + }; 31 + 32 + static const struct tegra_usb_soc_info tegra20_ehci_soc_info = { 33 + .flags = CI_HDRC_REQUIRES_ALIGNED_DMA | 34 + CI_HDRC_OVERRIDE_PHY_CONTROL, 35 + .dr_mode = USB_DR_MODE_HOST, 36 + .txfifothresh = 10, 37 + }; 38 + 39 + static const struct tegra_usb_soc_info tegra30_ehci_soc_info = { 40 + .flags = CI_HDRC_REQUIRES_ALIGNED_DMA | 41 + CI_HDRC_OVERRIDE_PHY_CONTROL, 42 + .dr_mode = USB_DR_MODE_HOST, 43 + .txfifothresh = 16, 32 44 }; 33 45 34 46 static const struct tegra_usb_soc_info tegra_udc_soc_info = { 35 - .flags = CI_HDRC_REQUIRES_ALIGNED_DMA, 47 + .flags = CI_HDRC_REQUIRES_ALIGNED_DMA | 48 + CI_HDRC_OVERRIDE_PHY_CONTROL, 49 + .dr_mode = USB_DR_MODE_UNKNOWN, 36 50 }; 37 51 38 52 static const struct of_device_id tegra_usb_of_match[] = { 39 53 { 54 + .compatible = "nvidia,tegra20-ehci", 55 + .data = &tegra20_ehci_soc_info, 56 + }, { 57 + .compatible = "nvidia,tegra30-ehci", 58 + .data = &tegra30_ehci_soc_info, 59 + }, { 40 60 .compatible = "nvidia,tegra20-udc", 41 61 .data = &tegra_udc_soc_info, 42 62 }, { ··· 80 46 } 81 47 }; 82 48 MODULE_DEVICE_TABLE(of, tegra_usb_of_match); 49 + 50 + static int tegra_usb_reset_controller(struct device *dev) 51 + { 52 + struct reset_control *rst, *rst_utmi; 53 + struct device_node *phy_np; 54 + int err; 55 + 56 + rst = devm_reset_control_get_shared(dev, "usb"); 57 + if (IS_ERR(rst)) { 58 + dev_err(dev, "can't get ehci reset: %pe\n", rst); 59 + return PTR_ERR(rst); 60 + } 61 + 62 + phy_np = of_parse_phandle(dev->of_node, "nvidia,phy", 0); 63 + if (!phy_np) 64 + return -ENOENT; 65 + 66 + /* 67 + * The 1st USB controller contains some UTMI pad registers that are 68 + * global for all the controllers on the chip. Those registers are 69 + * also cleared when reset is asserted to the 1st controller. 70 + */ 71 + rst_utmi = of_reset_control_get_shared(phy_np, "utmi-pads"); 72 + if (IS_ERR(rst_utmi)) { 73 + dev_warn(dev, "can't get utmi-pads reset from the PHY\n"); 74 + dev_warn(dev, "continuing, but please update your DT\n"); 75 + } else { 76 + /* 77 + * PHY driver performs UTMI-pads reset in a case of a 78 + * non-legacy DT. 79 + */ 80 + reset_control_put(rst_utmi); 81 + } 82 + 83 + of_node_put(phy_np); 84 + 85 + /* reset control is shared, hence initialize it first */ 86 + err = reset_control_deassert(rst); 87 + if (err) 88 + return err; 89 + 90 + err = reset_control_assert(rst); 91 + if (err) 92 + return err; 93 + 94 + udelay(1); 95 + 96 + err = reset_control_deassert(rst); 97 + if (err) 98 + return err; 99 + 100 + return 0; 101 + } 102 + 103 + static int tegra_usb_notify_event(struct ci_hdrc *ci, unsigned int event) 104 + { 105 + struct tegra_usb *usb = dev_get_drvdata(ci->dev->parent); 106 + struct ehci_hcd *ehci; 107 + 108 + switch (event) { 109 + case CI_HDRC_CONTROLLER_RESET_EVENT: 110 + if (ci->hcd) { 111 + ehci = hcd_to_ehci(ci->hcd); 112 + ehci->has_tdi_phy_lpm = false; 113 + ehci_writel(ehci, usb->soc->txfifothresh << 16, 114 + &ehci->regs->txfill_tuning); 115 + } 116 + break; 117 + } 118 + 119 + return 0; 120 + } 121 + 122 + static int tegra_usb_internal_port_reset(struct ehci_hcd *ehci, 123 + u32 __iomem *portsc_reg, 124 + unsigned long *flags) 125 + { 126 + u32 saved_usbintr, temp; 127 + unsigned int i, tries; 128 + int retval = 0; 129 + 130 + saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable); 131 + /* disable USB interrupt */ 132 + ehci_writel(ehci, 0, &ehci->regs->intr_enable); 133 + spin_unlock_irqrestore(&ehci->lock, *flags); 134 + 135 + /* 136 + * Here we have to do Port Reset at most twice for 137 + * Port Enable bit to be set. 138 + */ 139 + for (i = 0; i < 2; i++) { 140 + temp = ehci_readl(ehci, portsc_reg); 141 + temp |= PORT_RESET; 142 + ehci_writel(ehci, temp, portsc_reg); 143 + fsleep(10000); 144 + temp &= ~PORT_RESET; 145 + ehci_writel(ehci, temp, portsc_reg); 146 + fsleep(1000); 147 + tries = 100; 148 + do { 149 + fsleep(1000); 150 + /* 151 + * Up to this point, Port Enable bit is 152 + * expected to be set after 2 ms waiting. 153 + * USB1 usually takes extra 45 ms, for safety, 154 + * we take 100 ms as timeout. 155 + */ 156 + temp = ehci_readl(ehci, portsc_reg); 157 + } while (!(temp & PORT_PE) && tries--); 158 + if (temp & PORT_PE) 159 + break; 160 + } 161 + if (i == 2) 162 + retval = -ETIMEDOUT; 163 + 164 + /* 165 + * Clear Connect Status Change bit if it's set. 166 + * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared. 167 + */ 168 + if (temp & PORT_CSC) 169 + ehci_writel(ehci, PORT_CSC, portsc_reg); 170 + 171 + /* 172 + * Write to clear any interrupt status bits that might be set 173 + * during port reset. 174 + */ 175 + temp = ehci_readl(ehci, &ehci->regs->status); 176 + ehci_writel(ehci, temp, &ehci->regs->status); 177 + 178 + /* restore original interrupt-enable bits */ 179 + spin_lock_irqsave(&ehci->lock, *flags); 180 + ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable); 181 + 182 + return retval; 183 + } 184 + 185 + static int tegra_ehci_hub_control(struct ci_hdrc *ci, u16 typeReq, u16 wValue, 186 + u16 wIndex, char *buf, u16 wLength, 187 + bool *done, unsigned long *flags) 188 + { 189 + struct tegra_usb *usb = dev_get_drvdata(ci->dev->parent); 190 + struct ehci_hcd *ehci = hcd_to_ehci(ci->hcd); 191 + u32 __iomem *status_reg; 192 + int retval = 0; 193 + 194 + status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1]; 195 + 196 + switch (typeReq) { 197 + case SetPortFeature: 198 + if (wValue != USB_PORT_FEAT_RESET || !usb->needs_double_reset) 199 + break; 200 + 201 + /* for USB1 port we need to issue Port Reset twice internally */ 202 + retval = tegra_usb_internal_port_reset(ehci, status_reg, flags); 203 + *done = true; 204 + break; 205 + } 206 + 207 + return retval; 208 + } 209 + 210 + static void tegra_usb_enter_lpm(struct ci_hdrc *ci, bool enable) 211 + { 212 + /* 213 + * Touching any register which belongs to AHB clock domain will 214 + * hang CPU if USB controller is put into low power mode because 215 + * AHB USB clock is gated on Tegra in the LPM. 216 + * 217 + * Tegra PHY has a separate register for checking the clock status 218 + * and usb_phy_set_suspend() takes care of gating/ungating the clocks 219 + * and restoring the PHY state on Tegra. Hence DEVLC/PORTSC registers 220 + * shouldn't be touched directly by the CI driver. 221 + */ 222 + usb_phy_set_suspend(ci->usb_phy, enable); 223 + } 83 224 84 225 static int tegra_usb_probe(struct platform_device *pdev) 85 226 { ··· 292 83 return err; 293 84 } 294 85 86 + if (device_property_present(&pdev->dev, "nvidia,needs-double-reset")) 87 + usb->needs_double_reset = true; 88 + 89 + err = tegra_usb_reset_controller(&pdev->dev); 90 + if (err) { 91 + dev_err(&pdev->dev, "failed to reset controller: %d\n", err); 92 + goto fail_power_off; 93 + } 94 + 95 + /* 96 + * USB controller registers shouldn't be touched before PHY is 97 + * initialized, otherwise CPU will hang because clocks are gated. 98 + * PHY driver controls gating of internal USB clocks on Tegra. 99 + */ 100 + err = usb_phy_init(usb->phy); 101 + if (err) 102 + goto fail_power_off; 103 + 104 + platform_set_drvdata(pdev, usb); 105 + 295 106 /* setup and register ChipIdea HDRC device */ 107 + usb->soc = soc; 296 108 usb->data.name = "tegra-usb"; 297 109 usb->data.flags = soc->flags; 298 110 usb->data.usb_phy = usb->phy; 111 + usb->data.dr_mode = soc->dr_mode; 299 112 usb->data.capoffset = DEF_CAPOFFSET; 113 + usb->data.enter_lpm = tegra_usb_enter_lpm; 114 + usb->data.hub_control = tegra_ehci_hub_control; 115 + usb->data.notify_event = tegra_usb_notify_event; 300 116 301 117 usb->dev = ci_hdrc_add_device(&pdev->dev, pdev->resource, 302 118 pdev->num_resources, &usb->data); 303 119 if (IS_ERR(usb->dev)) { 304 120 err = PTR_ERR(usb->dev); 305 121 dev_err(&pdev->dev, "failed to add HDRC device: %d\n", err); 306 - goto fail_power_off; 122 + goto phy_shutdown; 307 123 } 308 - 309 - platform_set_drvdata(pdev, usb); 310 124 311 125 return 0; 312 126 127 + phy_shutdown: 128 + usb_phy_shutdown(usb->phy); 313 129 fail_power_off: 314 130 clk_disable_unprepare(usb->clk); 315 131 return err; ··· 345 111 struct tegra_usb *usb = platform_get_drvdata(pdev); 346 112 347 113 ci_hdrc_remove_device(usb->dev); 114 + usb_phy_shutdown(usb->phy); 348 115 clk_disable_unprepare(usb->clk); 349 116 350 117 return 0;
+9 -1
drivers/usb/chipidea/core.c
··· 195 195 } 196 196 197 197 /* The PHY enters/leaves low power mode */ 198 - static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable) 198 + static void ci_hdrc_enter_lpm_common(struct ci_hdrc *ci, bool enable) 199 199 { 200 200 enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC; 201 201 bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm))); ··· 206 206 else if (!enable && lpm) 207 207 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm), 208 208 0); 209 + } 210 + 211 + static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable) 212 + { 213 + return ci->platdata->enter_lpm(ci, enable); 209 214 } 210 215 211 216 static int hw_device_init(struct ci_hdrc *ci, void __iomem *base) ··· 794 789 if (!IS_ERR(p)) 795 790 platdata->pins_device = p; 796 791 } 792 + 793 + if (!platdata->enter_lpm) 794 + platdata->enter_lpm = ci_hdrc_enter_lpm_common; 797 795 798 796 return 0; 799 797 }
+102 -2
drivers/usb/chipidea/host.c
··· 29 29 bool enabled; 30 30 }; 31 31 32 + struct ci_hdrc_dma_aligned_buffer { 33 + void *kmalloc_ptr; 34 + void *old_xfer_buffer; 35 + u8 data[0]; 36 + }; 37 + 32 38 static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable) 33 39 { 34 40 struct ehci_hcd *ehci = hcd_to_ehci(hcd); ··· 166 160 pinctrl_select_state(ci->platdata->pctl, 167 161 ci->platdata->pins_host); 168 162 163 + ci->hcd = hcd; 164 + 169 165 ret = usb_add_hcd(hcd, 0, 0); 170 166 if (ret) { 167 + ci->hcd = NULL; 171 168 goto disable_reg; 172 169 } else { 173 170 struct usb_otg *otg = &ci->otg; 174 - 175 - ci->hcd = hcd; 176 171 177 172 if (ci_otg_is_fsm_mode(ci)) { 178 173 otg->host = &hcd->self; ··· 244 237 u32 temp; 245 238 unsigned long flags; 246 239 int retval = 0; 240 + bool done = false; 247 241 struct device *dev = hcd->self.controller; 248 242 struct ci_hdrc *ci = dev_get_drvdata(dev); 249 243 250 244 status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1]; 251 245 252 246 spin_lock_irqsave(&ehci->lock, flags); 247 + 248 + if (ci->platdata->hub_control) { 249 + retval = ci->platdata->hub_control(ci, typeReq, wValue, wIndex, 250 + buf, wLength, &done, &flags); 251 + if (done) 252 + goto done; 253 + } 253 254 254 255 if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) { 255 256 temp = ehci_readl(ehci, status_reg); ··· 364 349 return 0; 365 350 } 366 351 352 + static void ci_hdrc_free_dma_aligned_buffer(struct urb *urb) 353 + { 354 + struct ci_hdrc_dma_aligned_buffer *temp; 355 + size_t length; 356 + 357 + if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) 358 + return; 359 + 360 + temp = container_of(urb->transfer_buffer, 361 + struct ci_hdrc_dma_aligned_buffer, data); 362 + 363 + if (usb_urb_dir_in(urb)) { 364 + if (usb_pipeisoc(urb->pipe)) 365 + length = urb->transfer_buffer_length; 366 + else 367 + length = urb->actual_length; 368 + 369 + memcpy(temp->old_xfer_buffer, temp->data, length); 370 + } 371 + urb->transfer_buffer = temp->old_xfer_buffer; 372 + kfree(temp->kmalloc_ptr); 373 + 374 + urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; 375 + } 376 + 377 + static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) 378 + { 379 + struct ci_hdrc_dma_aligned_buffer *temp, *kmalloc_ptr; 380 + const unsigned int ci_hdrc_usb_dma_align = 32; 381 + size_t kmalloc_size; 382 + 383 + if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0 || 384 + !((uintptr_t)urb->transfer_buffer & (ci_hdrc_usb_dma_align - 1))) 385 + return 0; 386 + 387 + /* Allocate a buffer with enough padding for alignment */ 388 + kmalloc_size = urb->transfer_buffer_length + 389 + sizeof(struct ci_hdrc_dma_aligned_buffer) + 390 + ci_hdrc_usb_dma_align - 1; 391 + 392 + kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); 393 + if (!kmalloc_ptr) 394 + return -ENOMEM; 395 + 396 + /* Position our struct dma_aligned_buffer such that data is aligned */ 397 + temp = PTR_ALIGN(kmalloc_ptr + 1, ci_hdrc_usb_dma_align) - 1; 398 + temp->kmalloc_ptr = kmalloc_ptr; 399 + temp->old_xfer_buffer = urb->transfer_buffer; 400 + if (usb_urb_dir_out(urb)) 401 + memcpy(temp->data, urb->transfer_buffer, 402 + urb->transfer_buffer_length); 403 + urb->transfer_buffer = temp->data; 404 + 405 + urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; 406 + 407 + return 0; 408 + } 409 + 410 + static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, 411 + gfp_t mem_flags) 412 + { 413 + int ret; 414 + 415 + ret = ci_hdrc_alloc_dma_aligned_buffer(urb, mem_flags); 416 + if (ret) 417 + return ret; 418 + 419 + ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); 420 + if (ret) 421 + ci_hdrc_free_dma_aligned_buffer(urb); 422 + 423 + return ret; 424 + } 425 + 426 + static void ci_hdrc_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) 427 + { 428 + usb_hcd_unmap_urb_for_dma(hcd, urb); 429 + ci_hdrc_free_dma_aligned_buffer(urb); 430 + } 431 + 367 432 int ci_hdrc_host_init(struct ci_hdrc *ci) 368 433 { 369 434 struct ci_role_driver *rdrv; ··· 460 365 rdrv->irq = host_irq; 461 366 rdrv->name = "host"; 462 367 ci->roles[CI_ROLE_HOST] = rdrv; 368 + 369 + if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA) { 370 + ci_ehci_hc_driver.map_urb_for_dma = ci_hdrc_map_urb_for_dma; 371 + ci_ehci_hc_driver.unmap_urb_for_dma = ci_hdrc_unmap_urb_for_dma; 372 + } 463 373 464 374 return 0; 465 375 }
+6
include/linux/usb/chipidea.h
··· 88 88 struct pinctrl_state *pins_default; 89 89 struct pinctrl_state *pins_host; 90 90 struct pinctrl_state *pins_device; 91 + 92 + /* platform-specific hooks */ 93 + int (*hub_control)(struct ci_hdrc *ci, u16 typeReq, u16 wValue, 94 + u16 wIndex, char *buf, u16 wLength, 95 + bool *done, unsigned long *flags); 96 + void (*enter_lpm)(struct ci_hdrc *ci, bool enable); 91 97 }; 92 98 93 99 /* Default offset of capability registers */