blob: 95d54c1f098bf15238886347e93fc11bff4ae6a6 [file] [log] [blame]
Alexey Brodkinfee331f2015-12-14 17:18:50 +03001/*
2 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +02008#include <clk.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +03009#include <dm.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +020010#include <dm/ofnode.h>
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020011#include <reset.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +030012#include "ohci.h"
13
14#if !defined(CONFIG_USB_OHCI_NEW)
15# error "Generic OHCI driver requires CONFIG_USB_OHCI_NEW"
16#endif
17
18struct generic_ohci {
19 ohci_t ohci;
Patrice Chotard155d9f62017-07-18 11:57:12 +020020 struct clk *clocks; /* clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020021 struct reset_ctl *resets; /* reset list */
Patrice Chotard155d9f62017-07-18 11:57:12 +020022 int clock_count; /* number of clock in clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020023 int reset_count; /* number of reset in reset list */
Alexey Brodkinfee331f2015-12-14 17:18:50 +030024};
25
26static int ohci_usb_probe(struct udevice *dev)
27{
Simon Glassa821c4a2017-05-17 17:18:05 -060028 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
Patrice Chotard155d9f62017-07-18 11:57:12 +020029 struct generic_ohci *priv = dev_get_priv(dev);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020030 int i, err, ret, clock_nb, reset_nb;
Alexey Brodkinfee331f2015-12-14 17:18:50 +030031
Patrice Chotard155d9f62017-07-18 11:57:12 +020032 err = 0;
33 priv->clock_count = 0;
34 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
35 if (clock_nb > 0) {
36 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
37 GFP_KERNEL);
38 if (!priv->clocks)
39 return -ENOMEM;
40
41 for (i = 0; i < clock_nb; i++) {
42 err = clk_get_by_index(dev, i, &priv->clocks[i]);
43 if (err < 0)
44 break;
45
46 err = clk_enable(&priv->clocks[i]);
47 if (err) {
48 error("failed to enable clock %d\n", i);
49 clk_free(&priv->clocks[i]);
50 goto clk_err;
51 }
52 priv->clock_count++;
53 }
54 } else if (clock_nb != -ENOENT) {
55 error("failed to get clock phandle(%d)\n", clock_nb);
56 return clock_nb;
57 }
58
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020059 priv->reset_count = 0;
60 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
61 if (reset_nb > 0) {
62 priv->resets = devm_kcalloc(dev, reset_nb,
63 sizeof(struct reset_ctl),
64 GFP_KERNEL);
65 if (!priv->resets)
66 return -ENOMEM;
67
68 for (i = 0; i < reset_nb; i++) {
69 err = reset_get_by_index(dev, i, &priv->resets[i]);
70 if (err < 0)
71 break;
72
73 err = reset_deassert(&priv->resets[i]);
74 if (err) {
75 error("failed to deassert reset %d\n", i);
76 reset_free(&priv->resets[i]);
77 goto reset_err;
78 }
79 priv->reset_count++;
80 }
81 } else if (reset_nb != -ENOENT) {
82 error("failed to get reset phandle(%d)\n", reset_nb);
83 goto clk_err;
84 }
85
Patrice Chotard155d9f62017-07-18 11:57:12 +020086 err = ohci_register(dev, regs);
87 if (err)
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020088 goto reset_err;
Patrice Chotard155d9f62017-07-18 11:57:12 +020089
90 return 0;
91
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020092reset_err:
93 ret = reset_release_all(priv->resets, priv->reset_count);
94 if (ret)
95 error("failed to assert all resets\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +020096clk_err:
97 ret = clk_release_all(priv->clocks, priv->clock_count);
98 if (ret)
99 error("failed to disable all clocks\n");
100
101 return err;
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300102}
103
104static int ohci_usb_remove(struct udevice *dev)
105{
Patrice Chotard155d9f62017-07-18 11:57:12 +0200106 struct generic_ohci *priv = dev_get_priv(dev);
107 int ret;
108
109 ret = ohci_deregister(dev);
110 if (ret)
111 return ret;
112
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200113 ret = reset_release_all(priv->resets, priv->reset_count);
114 if (ret)
115 return ret;
116
Patrice Chotard155d9f62017-07-18 11:57:12 +0200117 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300118}
119
120static const struct udevice_id ohci_usb_ids[] = {
121 { .compatible = "generic-ohci" },
122 { }
123};
124
125U_BOOT_DRIVER(ohci_generic) = {
126 .name = "ohci_generic",
127 .id = UCLASS_USB,
128 .of_match = ohci_usb_ids,
129 .probe = ohci_usb_probe,
130 .remove = ohci_usb_remove,
131 .ops = &ohci_usb_ops,
132 .priv_auto_alloc_size = sizeof(struct generic_ohci),
133 .flags = DM_FLAG_ALLOC_PRIV_DMA,
134};