Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Amarula Solutions. |
| 4 | * Author: Jagan Teki <jagan@amarulasolutions.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk-uclass.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Andre Przywara | 030bab8 | 2019-01-29 15:54:08 +0000 | [diff] [blame] | 12 | #include <reset.h> |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
Samuel Holland | 12e3faa | 2021-09-12 11:48:43 -0500 | [diff] [blame] | 14 | #include <clk/sunxi.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 16 | #include <linux/log2.h> |
| 17 | |
| 18 | static const struct ccu_clk_gate *priv_to_gate(struct ccu_priv *priv, |
| 19 | unsigned long id) |
| 20 | { |
Samuel Holland | a496907 | 2022-05-09 00:29:32 -0500 | [diff] [blame] | 21 | if (id >= priv->desc->num_gates) |
| 22 | return NULL; |
| 23 | |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 24 | return &priv->desc->gates[id]; |
| 25 | } |
| 26 | |
| 27 | static int sunxi_set_gate(struct clk *clk, bool on) |
| 28 | { |
| 29 | struct ccu_priv *priv = dev_get_priv(clk->dev); |
| 30 | const struct ccu_clk_gate *gate = priv_to_gate(priv, clk->id); |
| 31 | u32 reg; |
| 32 | |
Samuel Holland | a496907 | 2022-05-09 00:29:32 -0500 | [diff] [blame] | 33 | if (gate && (gate->flags & CCU_CLK_F_DUMMY_GATE)) |
Andre Przywara | 2d1864f | 2022-05-05 01:25:43 +0100 | [diff] [blame] | 34 | return 0; |
| 35 | |
Samuel Holland | a496907 | 2022-05-09 00:29:32 -0500 | [diff] [blame] | 36 | if (!gate || !(gate->flags & CCU_CLK_F_IS_VALID)) { |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 37 | printf("%s: (CLK#%ld) unhandled\n", __func__, clk->id); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | debug("%s: (CLK#%ld) off#0x%x, BIT(%d)\n", __func__, |
| 42 | clk->id, gate->off, ilog2(gate->bit)); |
| 43 | |
| 44 | reg = readl(priv->base + gate->off); |
| 45 | if (on) |
| 46 | reg |= gate->bit; |
| 47 | else |
| 48 | reg &= ~gate->bit; |
| 49 | |
| 50 | writel(reg, priv->base + gate->off); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int sunxi_clk_enable(struct clk *clk) |
| 56 | { |
| 57 | return sunxi_set_gate(clk, true); |
| 58 | } |
| 59 | |
| 60 | static int sunxi_clk_disable(struct clk *clk) |
| 61 | { |
| 62 | return sunxi_set_gate(clk, false); |
| 63 | } |
| 64 | |
| 65 | struct clk_ops sunxi_clk_ops = { |
| 66 | .enable = sunxi_clk_enable, |
| 67 | .disable = sunxi_clk_disable, |
| 68 | }; |
| 69 | |
Samuel Holland | 1567fdf | 2022-05-09 00:29:33 -0500 | [diff] [blame^] | 70 | int sunxi_clk_bind(struct udevice *dev) |
| 71 | { |
| 72 | return sunxi_reset_bind(dev); |
| 73 | } |
| 74 | |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 75 | int sunxi_clk_probe(struct udevice *dev) |
| 76 | { |
| 77 | struct ccu_priv *priv = dev_get_priv(dev); |
Andre Przywara | 030bab8 | 2019-01-29 15:54:08 +0000 | [diff] [blame] | 78 | struct clk_bulk clk_bulk; |
| 79 | struct reset_ctl_bulk rst_bulk; |
| 80 | int ret; |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 81 | |
| 82 | priv->base = dev_read_addr_ptr(dev); |
| 83 | if (!priv->base) |
| 84 | return -ENOMEM; |
| 85 | |
| 86 | priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev); |
| 87 | if (!priv->desc) |
| 88 | return -EINVAL; |
| 89 | |
Andre Przywara | 030bab8 | 2019-01-29 15:54:08 +0000 | [diff] [blame] | 90 | ret = clk_get_bulk(dev, &clk_bulk); |
| 91 | if (!ret) |
| 92 | clk_enable_bulk(&clk_bulk); |
| 93 | |
| 94 | ret = reset_get_bulk(dev, &rst_bulk); |
| 95 | if (!ret) |
| 96 | reset_deassert_bulk(&rst_bulk); |
| 97 | |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 98 | return 0; |
| 99 | } |