Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | fa1f73f | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 2 | * Copyright (C) 2016 Socionext Inc. |
| 3 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 9 | #include <clk-uclass.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 11 | #include <linux/bitops.h> |
| 12 | #include <linux/io.h> |
Masahiro Yamada | 46a624f | 2016-03-24 22:32:39 +0900 | [diff] [blame] | 13 | #include <linux/sizes.h> |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 14 | |
| 15 | #include "clk-uniphier.h" |
| 16 | |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 17 | /** |
| 18 | * struct uniphier_clk_priv - private data for UniPhier clock driver |
| 19 | * |
| 20 | * @base: base address of the clock provider |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 21 | * @data: SoC specific data |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 22 | */ |
| 23 | struct uniphier_clk_priv { |
| 24 | void __iomem *base; |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 25 | const struct uniphier_clk_data *data; |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 26 | }; |
| 27 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 28 | static int uniphier_clk_enable(struct clk *clk) |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 29 | { |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 30 | struct uniphier_clk_priv *priv = dev_get_priv(clk->dev); |
| 31 | unsigned long id = clk->id; |
| 32 | const struct uniphier_clk_gate_data *p; |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 33 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 34 | for (p = priv->data->gate; p->id != UNIPHIER_CLK_ID_END; p++) { |
| 35 | u32 val; |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 36 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 37 | if (p->id != id) |
| 38 | continue; |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 39 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 40 | val = readl(priv->base + p->reg); |
| 41 | val |= BIT(p->bit); |
| 42 | writel(val, priv->base + p->reg); |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 43 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | dev_err(priv->dev, "clk_id=%lu was not handled\n", id); |
| 48 | return -EINVAL; |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 51 | static const struct uniphier_clk_mux_data * |
| 52 | uniphier_clk_get_mux_data(struct uniphier_clk_priv *priv, unsigned long id) |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 53 | { |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 54 | const struct uniphier_clk_mux_data *p; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 55 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 56 | for (p = priv->data->mux; p->id != UNIPHIER_CLK_ID_END; p++) { |
| 57 | if (p->id == id) |
| 58 | return p; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 61 | return NULL; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 64 | static ulong uniphier_clk_get_rate(struct clk *clk) |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 65 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 66 | struct uniphier_clk_priv *priv = dev_get_priv(clk->dev); |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 67 | const struct uniphier_clk_mux_data *mux; |
| 68 | u32 val; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 69 | int i; |
| 70 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 71 | mux = uniphier_clk_get_mux_data(priv, clk->id); |
| 72 | if (!mux) |
| 73 | return 0; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 74 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 75 | if (!mux->nr_muxs) /* fixed-rate */ |
| 76 | return mux->rates[0]; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 77 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 78 | val = readl(priv->base + mux->reg); |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 79 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 80 | for (i = 0; i < mux->nr_muxs; i++) |
| 81 | if ((mux->masks[i] & val) == mux->vals[i]) |
| 82 | return mux->rates[i]; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 83 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 84 | return -EINVAL; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 87 | static ulong uniphier_clk_set_rate(struct clk *clk, ulong rate) |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 88 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 89 | struct uniphier_clk_priv *priv = dev_get_priv(clk->dev); |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 90 | const struct uniphier_clk_mux_data *mux; |
| 91 | u32 val; |
| 92 | int i, best_rate_id = -1; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 93 | ulong best_rate = 0; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 94 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 95 | mux = uniphier_clk_get_mux_data(priv, clk->id); |
| 96 | if (!mux) |
| 97 | return 0; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 98 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 99 | if (!mux->nr_muxs) /* fixed-rate */ |
| 100 | return mux->rates[0]; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 101 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 102 | /* first, decide the best match rate */ |
| 103 | for (i = 0; i < mux->nr_muxs; i++) { |
| 104 | if (mux->rates[i] > best_rate && mux->rates[i] <= rate) { |
| 105 | best_rate = mux->rates[i]; |
| 106 | best_rate_id = i; |
| 107 | } |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 110 | if (best_rate_id < 0) |
| 111 | return -EINVAL; |
| 112 | |
| 113 | val = readl(priv->base + mux->reg); |
| 114 | val &= ~mux->masks[best_rate_id]; |
| 115 | val |= mux->vals[best_rate_id]; |
| 116 | writel(val, priv->base + mux->reg); |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 117 | |
| 118 | debug("%s: requested rate = %lu, set rate = %lu\n", __func__, |
| 119 | rate, best_rate); |
| 120 | |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 121 | return best_rate; |
| 122 | } |
| 123 | |
Masahiro Yamada | 5e05c4d | 2017-06-22 16:42:04 +0900 | [diff] [blame] | 124 | static const struct clk_ops uniphier_clk_ops = { |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 125 | .enable = uniphier_clk_enable, |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 126 | .get_rate = uniphier_clk_get_rate, |
| 127 | .set_rate = uniphier_clk_set_rate, |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 128 | }; |
| 129 | |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 130 | static int uniphier_clk_probe(struct udevice *dev) |
| 131 | { |
| 132 | struct uniphier_clk_priv *priv = dev_get_priv(dev); |
| 133 | fdt_addr_t addr; |
| 134 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 135 | addr = devfdt_get_addr(dev->parent); |
Masahiro Yamada | 8bdfe0a | 2016-10-17 22:18:01 +0900 | [diff] [blame] | 136 | if (addr == FDT_ADDR_T_NONE) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | priv->base = devm_ioremap(dev, addr, SZ_4K); |
| 140 | if (!priv->base) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | priv->data = (void *)dev_get_driver_data(dev); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 148 | static const struct udevice_id uniphier_clk_match[] = { |
| 149 | { |
Masahiro Yamada | c3a29fe | 2016-09-22 07:42:22 +0900 | [diff] [blame] | 150 | .compatible = "socionext,uniphier-sld3-mio-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 151 | .data = (ulong)&uniphier_mio_clk_data, |
| 152 | }, |
| 153 | { |
Masahiro Yamada | c3a29fe | 2016-09-22 07:42:22 +0900 | [diff] [blame] | 154 | .compatible = "socionext,uniphier-ld4-mio-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 155 | .data = (ulong)&uniphier_mio_clk_data, |
| 156 | }, |
| 157 | { |
Masahiro Yamada | c3a29fe | 2016-09-22 07:42:22 +0900 | [diff] [blame] | 158 | .compatible = "socionext,uniphier-pro4-mio-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 159 | .data = (ulong)&uniphier_mio_clk_data, |
| 160 | }, |
| 161 | { |
Masahiro Yamada | c3a29fe | 2016-09-22 07:42:22 +0900 | [diff] [blame] | 162 | .compatible = "socionext,uniphier-sld8-mio-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 163 | .data = (ulong)&uniphier_mio_clk_data, |
| 164 | }, |
| 165 | { |
Masahiro Yamada | 9e34de5 | 2017-01-28 06:53:41 +0900 | [diff] [blame] | 166 | .compatible = "socionext,uniphier-pro5-sd-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 167 | .data = (ulong)&uniphier_mio_clk_data, |
| 168 | }, |
| 169 | { |
Masahiro Yamada | 9e34de5 | 2017-01-28 06:53:41 +0900 | [diff] [blame] | 170 | .compatible = "socionext,uniphier-pxs2-sd-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 171 | .data = (ulong)&uniphier_mio_clk_data, |
| 172 | }, |
| 173 | { |
Masahiro Yamada | c3a29fe | 2016-09-22 07:42:22 +0900 | [diff] [blame] | 174 | .compatible = "socionext,uniphier-ld11-mio-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 175 | .data = (ulong)&uniphier_mio_clk_data, |
| 176 | }, |
| 177 | { |
Masahiro Yamada | 9e34de5 | 2017-01-28 06:53:41 +0900 | [diff] [blame] | 178 | .compatible = "socionext,uniphier-ld20-sd-clock", |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 179 | .data = (ulong)&uniphier_mio_clk_data, |
| 180 | }, |
| 181 | { /* sentinel */ } |
| 182 | }; |
Masahiro Yamada | e4dfb05 | 2016-02-02 21:11:32 +0900 | [diff] [blame] | 183 | |
Masahiro Yamada | f18559e | 2016-09-22 07:42:21 +0900 | [diff] [blame] | 184 | U_BOOT_DRIVER(uniphier_clk) = { |
| 185 | .name = "uniphier-clk", |
| 186 | .id = UCLASS_CLK, |
| 187 | .of_match = uniphier_clk_match, |
| 188 | .probe = uniphier_clk_probe, |
| 189 | .priv_auto_alloc_size = sizeof(struct uniphier_clk_priv), |
| 190 | .ops = &uniphier_clk_ops, |
| 191 | }; |