Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sughosh Ganu | 282e2af | 2012-08-09 10:45:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Sughosh Ganu <urwithsughosh@gmail.com> |
Sughosh Ganu | 282e2af | 2012-08-09 10:45:20 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 6 | #include <malloc.h> |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 7 | #include <asm/io.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <dm/devres.h> |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 12 | #include <dm/ofnode.h> |
| 13 | #include <generic-phy.h> |
| 14 | #include <reset.h> |
| 15 | #include "ohci.h" |
Sughosh Ganu | 282e2af | 2012-08-09 10:45:20 +0000 | [diff] [blame] | 16 | #include <asm/arch/da8xx-usb.h> |
| 17 | |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 18 | struct da8xx_ohci { |
| 19 | ohci_t ohci; |
| 20 | struct clk *clocks; /* clock list */ |
| 21 | struct phy phy; |
| 22 | int clock_count; /* number of clock in clock list */ |
| 23 | }; |
| 24 | |
| 25 | static int usb_phy_on(void) |
| 26 | { |
| 27 | unsigned long timeout; |
| 28 | |
| 29 | clrsetbits_le32(&davinci_syscfg_regs->cfgchip2, |
| 30 | (CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN | |
| 31 | CFGCHIP2_OTGPWRDN | CFGCHIP2_OTGMODE | |
| 32 | CFGCHIP2_REFFREQ | CFGCHIP2_USB1PHYCLKMUX), |
| 33 | (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN | |
| 34 | CFGCHIP2_PHY_PLLON | CFGCHIP2_REFFREQ_24MHZ | |
| 35 | CFGCHIP2_USB2PHYCLKMUX | CFGCHIP2_USB1SUSPENDM)); |
| 36 | |
| 37 | /* wait until the usb phy pll locks */ |
| 38 | timeout = get_timer(0); |
| 39 | while (get_timer(timeout) < 10) { |
| 40 | if (readl(&davinci_syscfg_regs->cfgchip2) & CFGCHIP2_PHYCLKGD) |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | /* USB phy was not turned on */ |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static void usb_phy_off(void) |
| 49 | { |
| 50 | /* Power down the on-chip PHY. */ |
| 51 | clrsetbits_le32(&davinci_syscfg_regs->cfgchip2, |
| 52 | CFGCHIP2_PHY_PLLON | CFGCHIP2_USB1SUSPENDM, |
| 53 | CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN | |
| 54 | CFGCHIP2_RESET); |
| 55 | } |
| 56 | |
Sughosh Ganu | 282e2af | 2012-08-09 10:45:20 +0000 | [diff] [blame] | 57 | int usb_cpu_init(void) |
| 58 | { |
| 59 | /* enable psc for usb2.0 */ |
| 60 | lpsc_on(DAVINCI_LPSC_USB20); |
| 61 | |
| 62 | /* enable psc for usb1.0 */ |
| 63 | lpsc_on(DAVINCI_LPSC_USB11); |
| 64 | |
| 65 | /* start the on-chip usb phy and its pll */ |
| 66 | if (usb_phy_on()) |
| 67 | return 0; |
| 68 | |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | int usb_cpu_stop(void) |
| 73 | { |
| 74 | usb_phy_off(); |
| 75 | |
| 76 | /* turn off the usb clock and assert the module reset */ |
| 77 | lpsc_disable(DAVINCI_LPSC_USB11); |
| 78 | lpsc_disable(DAVINCI_LPSC_USB20); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int usb_cpu_init_fail(void) |
| 84 | { |
| 85 | return usb_cpu_stop(); |
| 86 | } |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 87 | |
| 88 | #if CONFIG_IS_ENABLED(DM_USB) |
| 89 | static int ohci_da8xx_probe(struct udevice *dev) |
| 90 | { |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 91 | struct ohci_regs *regs = dev_read_addr_ptr(dev); |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 92 | struct da8xx_ohci *priv = dev_get_priv(dev); |
| 93 | int i, err, ret, clock_nb; |
| 94 | |
| 95 | err = 0; |
| 96 | priv->clock_count = 0; |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 97 | clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", |
| 98 | 0); |
Adam Ford | 331b30f | 2019-05-07 06:57:39 -0500 | [diff] [blame] | 99 | |
| 100 | if (clock_nb < 0) |
| 101 | return clock_nb; |
| 102 | |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 103 | if (clock_nb > 0) { |
| 104 | priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk), |
| 105 | GFP_KERNEL); |
| 106 | if (!priv->clocks) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | for (i = 0; i < clock_nb; i++) { |
| 110 | err = clk_get_by_index(dev, i, &priv->clocks[i]); |
| 111 | if (err < 0) |
| 112 | break; |
| 113 | |
| 114 | err = clk_enable(&priv->clocks[i]); |
| 115 | if (err) { |
| 116 | dev_err(dev, "failed to enable clock %d\n", i); |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 117 | goto clk_err; |
| 118 | } |
| 119 | priv->clock_count++; |
| 120 | } |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | err = usb_cpu_init(); |
| 124 | |
| 125 | if (err) |
| 126 | goto clk_err; |
| 127 | |
| 128 | err = ohci_register(dev, regs); |
| 129 | if (err) |
| 130 | goto phy_err; |
| 131 | |
| 132 | return 0; |
| 133 | |
| 134 | phy_err: |
| 135 | ret = usb_cpu_stop(); |
| 136 | if (ret) |
| 137 | dev_err(dev, "failed to shutdown usb phy\n"); |
| 138 | |
| 139 | clk_err: |
| 140 | ret = clk_release_all(priv->clocks, priv->clock_count); |
| 141 | if (ret) |
| 142 | dev_err(dev, "failed to disable all clocks\n"); |
| 143 | |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | static int ohci_da8xx_remove(struct udevice *dev) |
| 148 | { |
| 149 | struct da8xx_ohci *priv = dev_get_priv(dev); |
| 150 | int ret; |
| 151 | |
| 152 | ret = ohci_deregister(dev); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | ret = usb_cpu_stop(); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | |
| 160 | return clk_release_all(priv->clocks, priv->clock_count); |
| 161 | } |
| 162 | |
| 163 | static const struct udevice_id da8xx_ohci_ids[] = { |
| 164 | { .compatible = "ti,da830-ohci" }, |
| 165 | { } |
| 166 | }; |
| 167 | |
| 168 | U_BOOT_DRIVER(ohci_generic) = { |
| 169 | .name = "ohci-da8xx", |
| 170 | .id = UCLASS_USB, |
| 171 | .of_match = da8xx_ohci_ids, |
| 172 | .probe = ohci_da8xx_probe, |
| 173 | .remove = ohci_da8xx_remove, |
| 174 | .ops = &ohci_usb_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 175 | .priv_auto = sizeof(struct da8xx_ohci), |
Adam Ford | 331b30f | 2019-05-07 06:57:39 -0500 | [diff] [blame] | 176 | .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE, |
Adam Ford | 5f364f5 | 2019-04-30 05:21:41 -0500 | [diff] [blame] | 177 | }; |
| 178 | #endif |