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