Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments System Control Interface (TI SCI) clock driver |
| 4 | * |
| 5 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * Andreas Dannenberg <dannenberg@ti.com> |
| 7 | * |
| 8 | * Loosely based on Linux kernel sci-clk.c... |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <clk-uclass.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 19 | #include <linux/soc/ti/ti_sci_protocol.h> |
Keerthy | 280d7a8 | 2019-10-24 15:00:47 +0530 | [diff] [blame] | 20 | #include <k3-avs.h> |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * struct ti_sci_clk_data - clock controller information structure |
| 24 | * @sci: TI SCI handle used for communication with system controller |
| 25 | */ |
| 26 | struct ti_sci_clk_data { |
| 27 | const struct ti_sci_handle *sci; |
| 28 | }; |
| 29 | |
| 30 | static int ti_sci_clk_probe(struct udevice *dev) |
| 31 | { |
| 32 | struct ti_sci_clk_data *data = dev_get_priv(dev); |
| 33 | |
| 34 | debug("%s(dev=%p)\n", __func__, dev); |
| 35 | |
| 36 | if (!data) |
| 37 | return -ENOMEM; |
| 38 | |
| 39 | /* Store handle for communication with the system controller */ |
| 40 | data->sci = ti_sci_get_handle(dev); |
| 41 | if (IS_ERR(data->sci)) |
| 42 | return PTR_ERR(data->sci); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int ti_sci_clk_of_xlate(struct clk *clk, |
| 48 | struct ofnode_phandle_args *args) |
| 49 | { |
| 50 | debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count); |
| 51 | |
| 52 | if (args->args_count != 2) { |
| 53 | debug("Invalid args_count: %d\n", args->args_count); |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * On TI SCI-based devices, the clock provider id field is used as a |
| 59 | * device ID, and the data field is used as the associated sub-ID. |
| 60 | */ |
| 61 | clk->id = args->args[0]; |
| 62 | clk->data = args->args[1]; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 67 | static ulong ti_sci_clk_get_rate(struct clk *clk) |
| 68 | { |
| 69 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 70 | const struct ti_sci_handle *sci = data->sci; |
| 71 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 72 | u64 current_freq; |
| 73 | int ret; |
| 74 | |
| 75 | debug("%s(clk=%p)\n", __func__, clk); |
| 76 | |
| 77 | ret = cops->get_freq(sci, clk->id, clk->data, ¤t_freq); |
| 78 | if (ret) { |
| 79 | dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | debug("%s(current_freq=%llu)\n", __func__, current_freq); |
| 84 | |
| 85 | return current_freq; |
| 86 | } |
| 87 | |
| 88 | static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate) |
| 89 | { |
| 90 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 91 | const struct ti_sci_handle *sci = data->sci; |
| 92 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 93 | int ret; |
Udit Kumar | c31616f | 2023-09-21 22:33:43 +0530 | [diff] [blame] | 94 | int freq_scale_up = rate >= ti_sci_clk_get_rate(clk) ? 1 : 0; |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 95 | |
| 96 | debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate); |
| 97 | |
Udit Kumar | c31616f | 2023-09-21 22:33:43 +0530 | [diff] [blame] | 98 | if (IS_ENABLED(CONFIG_K3_AVS0) && freq_scale_up) |
| 99 | k3_avs_notify_freq(clk->id, clk->data, rate); |
Keerthy | 280d7a8 | 2019-10-24 15:00:47 +0530 | [diff] [blame] | 100 | |
Lokesh Vutla | 74fc6b8 | 2020-01-17 11:57:30 +0530 | [diff] [blame] | 101 | ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX); |
Tero Kristo | 1770884 | 2021-06-11 11:45:10 +0300 | [diff] [blame] | 102 | if (ret) { |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 103 | dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret); |
Tero Kristo | 1770884 | 2021-06-11 11:45:10 +0300 | [diff] [blame] | 104 | return ret; |
| 105 | } |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 106 | |
Udit Kumar | c31616f | 2023-09-21 22:33:43 +0530 | [diff] [blame] | 107 | if (IS_ENABLED(CONFIG_K3_AVS0) && !freq_scale_up) |
| 108 | k3_avs_notify_freq(clk->id, clk->data, rate); |
| 109 | |
Tero Kristo | 1770884 | 2021-06-11 11:45:10 +0300 | [diff] [blame] | 110 | return rate; |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent) |
| 114 | { |
| 115 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 116 | const struct ti_sci_handle *sci = data->sci; |
| 117 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 118 | u8 num_parents; |
| 119 | u8 parent_cid; |
| 120 | int ret; |
| 121 | |
| 122 | debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent); |
| 123 | |
| 124 | /* Make sure the clock parent is valid for a given device ID */ |
| 125 | if (clk->id != parent->id) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | /* Make sure clock has parents that can be set */ |
| 129 | ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents); |
| 130 | if (ret) { |
| 131 | dev_err(clk->dev, "%s: get_num_parents failed (%d)\n", |
| 132 | __func__, ret); |
| 133 | return ret; |
| 134 | } |
| 135 | if (num_parents < 2) { |
| 136 | dev_err(clk->dev, "%s: clock has no settable parents!\n", |
| 137 | __func__); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | /* Make sure parent clock ID is valid */ |
| 142 | parent_cid = parent->data - clk->data - 1; |
| 143 | if (parent_cid >= num_parents) { |
| 144 | dev_err(clk->dev, "%s: invalid parent clock!\n", __func__); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
| 148 | /* Ready to proceed to configure the new clock parent */ |
| 149 | ret = cops->set_parent(sci, clk->id, clk->data, parent->data); |
| 150 | if (ret) |
| 151 | dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__, |
| 152 | ret); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int ti_sci_clk_enable(struct clk *clk) |
| 158 | { |
| 159 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 160 | const struct ti_sci_handle *sci = data->sci; |
| 161 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 162 | int ret; |
| 163 | |
| 164 | debug("%s(clk=%p)\n", __func__, clk); |
| 165 | |
| 166 | /* |
| 167 | * Allow the System Controller to automatically manage the state of |
| 168 | * this clock. If the device is enabled, then the clock is enabled. |
| 169 | */ |
| 170 | ret = cops->put_clock(sci, clk->id, clk->data); |
| 171 | if (ret) |
| 172 | dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret); |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static int ti_sci_clk_disable(struct clk *clk) |
| 178 | { |
| 179 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 180 | const struct ti_sci_handle *sci = data->sci; |
| 181 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 182 | int ret; |
| 183 | |
| 184 | debug("%s(clk=%p)\n", __func__, clk); |
| 185 | |
| 186 | /* Unconditionally disable clock, regardless of state of the device */ |
| 187 | ret = cops->idle_clock(sci, clk->id, clk->data); |
| 188 | if (ret) |
| 189 | dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__, |
| 190 | ret); |
| 191 | |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static const struct udevice_id ti_sci_clk_of_match[] = { |
| 196 | { .compatible = "ti,k2g-sci-clk" }, |
| 197 | { /* sentinel */ }, |
| 198 | }; |
| 199 | |
| 200 | static struct clk_ops ti_sci_clk_ops = { |
| 201 | .of_xlate = ti_sci_clk_of_xlate, |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 202 | .get_rate = ti_sci_clk_get_rate, |
| 203 | .set_rate = ti_sci_clk_set_rate, |
| 204 | .set_parent = ti_sci_clk_set_parent, |
| 205 | .enable = ti_sci_clk_enable, |
| 206 | .disable = ti_sci_clk_disable, |
| 207 | }; |
| 208 | |
| 209 | U_BOOT_DRIVER(ti_sci_clk) = { |
| 210 | .name = "ti-sci-clk", |
| 211 | .id = UCLASS_CLK, |
| 212 | .of_match = ti_sci_clk_of_match, |
| 213 | .probe = ti_sci_clk_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 214 | .priv_auto = sizeof(struct ti_sci_clk_data), |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 215 | .ops = &ti_sci_clk_ops, |
| 216 | }; |