Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com> |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Masahiro Yamada | 4feefdc | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 7 | #include <clk.h> |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 8 | #include <dm/ofnode.h> |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 9 | #include <generic-phy.h> |
Masahiro Yamada | 8824cfc | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 10 | #include <reset.h> |
Marek Vasut | 643cacb | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include "ehci.h" |
| 14 | |
| 15 | /* |
| 16 | * Even though here we don't explicitly use "struct ehci_ctrl" |
| 17 | * ehci_register() expects it to be the first thing that resides in |
| 18 | * device's private data. |
| 19 | */ |
| 20 | struct generic_ehci { |
| 21 | struct ehci_ctrl ctrl; |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 22 | struct clk *clocks; |
| 23 | struct reset_ctl *resets; |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 24 | struct phy phy; |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 25 | int clock_count; |
| 26 | int reset_count; |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static int ehci_usb_probe(struct udevice *dev) |
| 30 | { |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 31 | struct generic_ehci *priv = dev_get_priv(dev); |
Marek Vasut | 643cacb | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 32 | struct ehci_hccr *hccr; |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 33 | struct ehci_hcor *hcor; |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 34 | int i, err, ret, clock_nb, reset_nb; |
Masahiro Yamada | 4feefdc | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 35 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 36 | err = 0; |
| 37 | priv->clock_count = 0; |
| 38 | clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks", |
| 39 | "#clock-cells"); |
| 40 | if (clock_nb > 0) { |
| 41 | priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk), |
| 42 | GFP_KERNEL); |
| 43 | if (!priv->clocks) |
| 44 | return -ENOMEM; |
Masahiro Yamada | 4feefdc | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 45 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 46 | for (i = 0; i < clock_nb; i++) { |
| 47 | err = clk_get_by_index(dev, i, &priv->clocks[i]); |
| 48 | |
| 49 | if (err < 0) |
| 50 | break; |
| 51 | err = clk_enable(&priv->clocks[i]); |
| 52 | if (err) { |
Patrice Chotard | df7777a | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 53 | dev_err(dev, "failed to enable clock %d\n", i); |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 54 | clk_free(&priv->clocks[i]); |
| 55 | goto clk_err; |
| 56 | } |
| 57 | priv->clock_count++; |
| 58 | } |
| 59 | } else { |
| 60 | if (clock_nb != -ENOENT) { |
Patrice Chotard | df7777a | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 61 | dev_err(dev, "failed to get clock phandle(%d)\n", |
| 62 | clock_nb); |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 63 | return clock_nb; |
| 64 | } |
Masahiro Yamada | 4feefdc | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 65 | } |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 66 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 67 | priv->reset_count = 0; |
| 68 | reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets", |
| 69 | "#reset-cells"); |
| 70 | if (reset_nb > 0) { |
| 71 | priv->resets = devm_kcalloc(dev, reset_nb, |
| 72 | sizeof(struct reset_ctl), |
| 73 | GFP_KERNEL); |
| 74 | if (!priv->resets) |
| 75 | return -ENOMEM; |
Masahiro Yamada | 8824cfc | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 76 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 77 | for (i = 0; i < reset_nb; i++) { |
| 78 | err = reset_get_by_index(dev, i, &priv->resets[i]); |
| 79 | if (err < 0) |
| 80 | break; |
| 81 | |
| 82 | if (reset_deassert(&priv->resets[i])) { |
Patrice Chotard | df7777a | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 83 | dev_err(dev, "failed to deassert reset %d\n", |
| 84 | i); |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 85 | reset_free(&priv->resets[i]); |
| 86 | goto reset_err; |
| 87 | } |
| 88 | priv->reset_count++; |
| 89 | } |
| 90 | } else { |
| 91 | if (reset_nb != -ENOENT) { |
Patrice Chotard | df7777a | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 92 | dev_err(dev, "failed to get reset phandle(%d)\n", |
| 93 | reset_nb); |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 94 | goto clk_err; |
| 95 | } |
Masahiro Yamada | 8824cfc | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Marek Vasut | b43cdf9 | 2018-08-08 14:29:55 +0200 | [diff] [blame^] | 98 | err = ehci_setup_phy(dev, &priv->phy, 0); |
Patrice Chotard | 20f06a4 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 99 | if (err) |
Patrice Chotard | 20f06a4 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 100 | goto reset_err; |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 101 | |
Philipp Tomsich | 6e652e3 | 2017-09-12 17:32:28 +0200 | [diff] [blame] | 102 | hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE); |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 103 | hcor = (struct ehci_hcor *)((uintptr_t)hccr + |
| 104 | HC_LENGTH(ehci_readl(&hccr->cr_capbase))); |
| 105 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 106 | err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST); |
| 107 | if (err) |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 108 | goto phy_err; |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 112 | phy_err: |
Marek Vasut | b43cdf9 | 2018-08-08 14:29:55 +0200 | [diff] [blame^] | 113 | ret = ehci_shutdown_phy(dev, &priv->phy); |
Patrice Chotard | 20f06a4 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 114 | if (ret) |
| 115 | dev_err(dev, "failed to shutdown usb phy\n"); |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 116 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 117 | reset_err: |
| 118 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 119 | if (ret) |
Patrice Chotard | df7777a | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 120 | dev_err(dev, "failed to assert all resets\n"); |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 121 | clk_err: |
| 122 | ret = clk_release_all(priv->clocks, priv->clock_count); |
| 123 | if (ret) |
Patrice Chotard | df7777a | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 124 | dev_err(dev, "failed to disable all clocks\n"); |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | static int ehci_usb_remove(struct udevice *dev) |
| 130 | { |
| 131 | struct generic_ehci *priv = dev_get_priv(dev); |
| 132 | int ret; |
| 133 | |
| 134 | ret = ehci_deregister(dev); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
Marek Vasut | b43cdf9 | 2018-08-08 14:29:55 +0200 | [diff] [blame^] | 138 | ret = ehci_shutdown_phy(dev, &priv->phy); |
Patrice Chotard | 20f06a4 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 139 | if (ret) |
| 140 | return ret; |
Patrice Chotard | 0d0ba1a | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 141 | |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 142 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | return clk_release_all(priv->clocks, priv->clock_count); |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 147 | } |
| 148 | |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 149 | static const struct udevice_id ehci_usb_ids[] = { |
| 150 | { .compatible = "generic-ehci" }, |
| 151 | { } |
| 152 | }; |
| 153 | |
| 154 | U_BOOT_DRIVER(ehci_generic) = { |
| 155 | .name = "ehci_generic", |
| 156 | .id = UCLASS_USB, |
| 157 | .of_match = ehci_usb_ids, |
| 158 | .probe = ehci_usb_probe, |
Patrice Chotard | a1cee8e | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 159 | .remove = ehci_usb_remove, |
Alexey Brodkin | 90fbb28 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 160 | .ops = &ehci_usb_ops, |
| 161 | .priv_auto_alloc_size = sizeof(struct generic_ehci), |
| 162 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 163 | }; |