Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Xilinx, Inc. |
| 4 | * Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <dm/device_compat.h> |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 9 | #include <linux/bitops.h> |
| 10 | #include <linux/bitfield.h> |
| 11 | #include <malloc.h> |
| 12 | #include <clk-uclass.h> |
| 13 | #include <clk.h> |
| 14 | #include <dm.h> |
| 15 | #include <asm/arch/sys_proto.h> |
Michal Simek | e50c104 | 2019-10-04 15:25:18 +0200 | [diff] [blame] | 16 | #include <zynqmp_firmware.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 18 | |
| 19 | #define MAX_PARENT 100 |
| 20 | #define MAX_NODES 6 |
| 21 | #define MAX_NAME_LEN 50 |
| 22 | |
| 23 | #define CLK_TYPE_SHIFT 2 |
| 24 | |
| 25 | #define PM_API_PAYLOAD_LEN 3 |
| 26 | |
| 27 | #define NA_PARENT 0xFFFFFFFF |
| 28 | #define DUMMY_PARENT 0xFFFFFFFE |
| 29 | |
| 30 | #define CLK_TYPE_FIELD_LEN 4 |
| 31 | #define CLK_TOPOLOGY_NODE_OFFSET 16 |
| 32 | #define NODES_PER_RESP 3 |
| 33 | |
| 34 | #define CLK_TYPE_FIELD_MASK 0xF |
| 35 | #define CLK_FLAG_FIELD_MASK GENMASK(21, 8) |
| 36 | #define CLK_TYPE_FLAG_FIELD_MASK GENMASK(31, 24) |
| 37 | #define CLK_TYPE_FLAG2_FIELD_MASK GENMASK(7, 4) |
| 38 | #define CLK_TYPE_FLAG_BITS 8 |
| 39 | |
| 40 | #define CLK_PARENTS_ID_LEN 16 |
| 41 | #define CLK_PARENTS_ID_MASK 0xFFFF |
| 42 | |
| 43 | #define END_OF_TOPOLOGY_NODE 1 |
| 44 | #define END_OF_PARENTS 1 |
| 45 | |
| 46 | #define CLK_VALID_MASK 0x1 |
| 47 | #define NODE_CLASS_SHIFT 26U |
| 48 | #define NODE_SUBCLASS_SHIFT 20U |
| 49 | #define NODE_TYPE_SHIFT 14U |
| 50 | #define NODE_INDEX_SHIFT 0U |
| 51 | |
| 52 | #define CLK_GET_NAME_RESP_LEN 16 |
| 53 | #define CLK_GET_TOPOLOGY_RESP_WORDS 3 |
| 54 | #define CLK_GET_PARENTS_RESP_WORDS 3 |
| 55 | #define CLK_GET_ATTR_RESP_WORDS 1 |
| 56 | |
| 57 | #define NODE_SUBCLASS_CLOCK_PLL 1 |
| 58 | #define NODE_SUBCLASS_CLOCK_OUT 2 |
| 59 | #define NODE_SUBCLASS_CLOCK_REF 3 |
| 60 | |
| 61 | #define NODE_CLASS_CLOCK 2 |
| 62 | #define NODE_CLASS_MASK 0x3F |
| 63 | |
| 64 | #define CLOCK_NODE_TYPE_MUX 1 |
| 65 | #define CLOCK_NODE_TYPE_DIV 4 |
| 66 | #define CLOCK_NODE_TYPE_GATE 6 |
| 67 | |
| 68 | enum pm_query_id { |
| 69 | PM_QID_INVALID, |
| 70 | PM_QID_CLOCK_GET_NAME, |
| 71 | PM_QID_CLOCK_GET_TOPOLOGY, |
| 72 | PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS, |
| 73 | PM_QID_CLOCK_GET_PARENTS, |
| 74 | PM_QID_CLOCK_GET_ATTRIBUTES, |
| 75 | PM_QID_PINCTRL_GET_NUM_PINS, |
| 76 | PM_QID_PINCTRL_GET_NUM_FUNCTIONS, |
| 77 | PM_QID_PINCTRL_GET_NUM_FUNCTION_GROUPS, |
| 78 | PM_QID_PINCTRL_GET_FUNCTION_NAME, |
| 79 | PM_QID_PINCTRL_GET_FUNCTION_GROUPS, |
| 80 | PM_QID_PINCTRL_GET_PIN_GROUPS, |
| 81 | PM_QID_CLOCK_GET_NUM_CLOCKS, |
| 82 | PM_QID_CLOCK_GET_MAX_DIVISOR, |
| 83 | }; |
| 84 | |
| 85 | enum clk_type { |
| 86 | CLK_TYPE_OUTPUT, |
| 87 | CLK_TYPE_EXTERNAL, |
| 88 | }; |
| 89 | |
| 90 | struct clock_parent { |
| 91 | char name[MAX_NAME_LEN]; |
| 92 | int id; |
| 93 | u32 flag; |
| 94 | }; |
| 95 | |
| 96 | struct clock_topology { |
| 97 | u32 type; |
| 98 | u32 flag; |
| 99 | u32 type_flag; |
| 100 | }; |
| 101 | |
| 102 | struct versal_clock { |
| 103 | char clk_name[MAX_NAME_LEN]; |
| 104 | u32 valid; |
| 105 | enum clk_type type; |
| 106 | struct clock_topology node[MAX_NODES]; |
| 107 | u32 num_nodes; |
| 108 | struct clock_parent parent[MAX_PARENT]; |
| 109 | u32 num_parents; |
| 110 | u32 clk_id; |
| 111 | }; |
| 112 | |
| 113 | struct versal_clk_priv { |
| 114 | struct versal_clock *clk; |
| 115 | }; |
| 116 | |
| 117 | static ulong alt_ref_clk; |
| 118 | static ulong pl_alt_ref_clk; |
| 119 | static ulong ref_clk; |
| 120 | |
| 121 | struct versal_pm_query_data { |
| 122 | u32 qid; |
| 123 | u32 arg1; |
| 124 | u32 arg2; |
| 125 | u32 arg3; |
| 126 | }; |
| 127 | |
| 128 | static struct versal_clock *clock; |
| 129 | static unsigned int clock_max_idx; |
| 130 | |
| 131 | #define PM_QUERY_DATA 35 |
| 132 | |
| 133 | static int versal_pm_query(struct versal_pm_query_data qdata, u32 *ret_payload) |
| 134 | { |
| 135 | struct pt_regs regs; |
| 136 | |
| 137 | regs.regs[0] = PM_SIP_SVC | PM_QUERY_DATA; |
| 138 | regs.regs[1] = ((u64)qdata.arg1 << 32) | qdata.qid; |
| 139 | regs.regs[2] = ((u64)qdata.arg3 << 32) | qdata.arg2; |
| 140 | |
| 141 | smc_call(®s); |
| 142 | |
| 143 | if (ret_payload) { |
| 144 | ret_payload[0] = (u32)regs.regs[0]; |
| 145 | ret_payload[1] = upper_32_bits(regs.regs[0]); |
| 146 | ret_payload[2] = (u32)regs.regs[1]; |
| 147 | ret_payload[3] = upper_32_bits(regs.regs[1]); |
| 148 | ret_payload[4] = (u32)regs.regs[2]; |
| 149 | } |
| 150 | |
| 151 | return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : regs.regs[0]; |
| 152 | } |
| 153 | |
| 154 | static inline int versal_is_valid_clock(u32 clk_id) |
| 155 | { |
| 156 | if (clk_id >= clock_max_idx) |
| 157 | return -ENODEV; |
| 158 | |
| 159 | return clock[clk_id].valid; |
| 160 | } |
| 161 | |
| 162 | static int versal_get_clock_name(u32 clk_id, char *clk_name) |
| 163 | { |
| 164 | int ret; |
| 165 | |
| 166 | ret = versal_is_valid_clock(clk_id); |
| 167 | if (ret == 1) { |
| 168 | strncpy(clk_name, clock[clk_id].clk_name, MAX_NAME_LEN); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | return ret == 0 ? -EINVAL : ret; |
| 173 | } |
| 174 | |
| 175 | static int versal_get_clock_type(u32 clk_id, u32 *type) |
| 176 | { |
| 177 | int ret; |
| 178 | |
| 179 | ret = versal_is_valid_clock(clk_id); |
| 180 | if (ret == 1) { |
| 181 | *type = clock[clk_id].type; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | return ret == 0 ? -EINVAL : ret; |
| 186 | } |
| 187 | |
| 188 | static int versal_pm_clock_get_num_clocks(u32 *nclocks) |
| 189 | { |
| 190 | struct versal_pm_query_data qdata = {0}; |
| 191 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 192 | int ret; |
| 193 | |
| 194 | qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS; |
| 195 | |
| 196 | ret = versal_pm_query(qdata, ret_payload); |
| 197 | *nclocks = ret_payload[1]; |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static int versal_pm_clock_get_name(u32 clock_id, char *name) |
| 203 | { |
| 204 | struct versal_pm_query_data qdata = {0}; |
| 205 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 206 | int ret; |
| 207 | |
| 208 | qdata.qid = PM_QID_CLOCK_GET_NAME; |
| 209 | qdata.arg1 = clock_id; |
| 210 | |
| 211 | ret = versal_pm_query(qdata, ret_payload); |
| 212 | if (ret) |
| 213 | return ret; |
| 214 | memcpy(name, ret_payload, CLK_GET_NAME_RESP_LEN); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int versal_pm_clock_get_topology(u32 clock_id, u32 index, u32 *topology) |
| 220 | { |
| 221 | struct versal_pm_query_data qdata = {0}; |
| 222 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 223 | int ret; |
| 224 | |
| 225 | qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY; |
| 226 | qdata.arg1 = clock_id; |
| 227 | qdata.arg2 = index; |
| 228 | |
| 229 | ret = versal_pm_query(qdata, ret_payload); |
| 230 | memcpy(topology, &ret_payload[1], CLK_GET_TOPOLOGY_RESP_WORDS * 4); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static int versal_pm_clock_get_parents(u32 clock_id, u32 index, u32 *parents) |
| 236 | { |
| 237 | struct versal_pm_query_data qdata = {0}; |
| 238 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 239 | int ret; |
| 240 | |
| 241 | qdata.qid = PM_QID_CLOCK_GET_PARENTS; |
| 242 | qdata.arg1 = clock_id; |
| 243 | qdata.arg2 = index; |
| 244 | |
| 245 | ret = versal_pm_query(qdata, ret_payload); |
| 246 | memcpy(parents, &ret_payload[1], CLK_GET_PARENTS_RESP_WORDS * 4); |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int versal_pm_clock_get_attributes(u32 clock_id, u32 *attr) |
| 252 | { |
| 253 | struct versal_pm_query_data qdata = {0}; |
| 254 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 255 | int ret; |
| 256 | |
| 257 | qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES; |
| 258 | qdata.arg1 = clock_id; |
| 259 | |
| 260 | ret = versal_pm_query(qdata, ret_payload); |
| 261 | memcpy(attr, &ret_payload[1], CLK_GET_ATTR_RESP_WORDS * 4); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int __versal_clock_get_topology(struct clock_topology *topology, |
| 267 | u32 *data, u32 *nnodes) |
| 268 | { |
| 269 | int i; |
| 270 | |
| 271 | for (i = 0; i < PM_API_PAYLOAD_LEN; i++) { |
| 272 | if (!(data[i] & CLK_TYPE_FIELD_MASK)) |
| 273 | return END_OF_TOPOLOGY_NODE; |
| 274 | topology[*nnodes].type = data[i] & CLK_TYPE_FIELD_MASK; |
| 275 | topology[*nnodes].flag = FIELD_GET(CLK_FLAG_FIELD_MASK, |
| 276 | data[i]); |
| 277 | topology[*nnodes].type_flag = |
| 278 | FIELD_GET(CLK_TYPE_FLAG_FIELD_MASK, data[i]); |
| 279 | topology[*nnodes].type_flag |= |
| 280 | FIELD_GET(CLK_TYPE_FLAG2_FIELD_MASK, data[i]) << |
| 281 | CLK_TYPE_FLAG_BITS; |
| 282 | debug("topology type:0x%x, flag:0x%x, type_flag:0x%x\n", |
| 283 | topology[*nnodes].type, topology[*nnodes].flag, |
| 284 | topology[*nnodes].type_flag); |
| 285 | (*nnodes)++; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int versal_clock_get_topology(u32 clk_id, |
| 292 | struct clock_topology *topology, |
| 293 | u32 *num_nodes) |
| 294 | { |
| 295 | int j, ret; |
| 296 | u32 pm_resp[PM_API_PAYLOAD_LEN] = {0}; |
| 297 | |
| 298 | *num_nodes = 0; |
| 299 | for (j = 0; j <= MAX_NODES; j += 3) { |
| 300 | ret = versal_pm_clock_get_topology(clock[clk_id].clk_id, j, |
| 301 | pm_resp); |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | ret = __versal_clock_get_topology(topology, pm_resp, num_nodes); |
| 305 | if (ret == END_OF_TOPOLOGY_NODE) |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int __versal_clock_get_parents(struct clock_parent *parents, u32 *data, |
| 313 | u32 *nparent) |
| 314 | { |
| 315 | int i; |
| 316 | struct clock_parent *parent; |
| 317 | |
| 318 | for (i = 0; i < PM_API_PAYLOAD_LEN; i++) { |
| 319 | if (data[i] == NA_PARENT) |
| 320 | return END_OF_PARENTS; |
| 321 | |
| 322 | parent = &parents[i]; |
| 323 | parent->id = data[i] & CLK_PARENTS_ID_MASK; |
| 324 | if (data[i] == DUMMY_PARENT) { |
| 325 | strcpy(parent->name, "dummy_name"); |
| 326 | parent->flag = 0; |
| 327 | } else { |
| 328 | parent->flag = data[i] >> CLK_PARENTS_ID_LEN; |
| 329 | if (versal_get_clock_name(parent->id, parent->name)) |
| 330 | continue; |
| 331 | } |
| 332 | debug("parent name:%s\n", parent->name); |
| 333 | *nparent += 1; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int versal_clock_get_parents(u32 clk_id, struct clock_parent *parents, |
| 340 | u32 *num_parents) |
| 341 | { |
| 342 | int j = 0, ret; |
| 343 | u32 pm_resp[PM_API_PAYLOAD_LEN] = {0}; |
| 344 | |
| 345 | *num_parents = 0; |
| 346 | do { |
| 347 | /* Get parents from firmware */ |
| 348 | ret = versal_pm_clock_get_parents(clock[clk_id].clk_id, j, |
| 349 | pm_resp); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | |
| 353 | ret = __versal_clock_get_parents(&parents[j], pm_resp, |
| 354 | num_parents); |
| 355 | if (ret == END_OF_PARENTS) |
| 356 | return 0; |
| 357 | j += PM_API_PAYLOAD_LEN; |
| 358 | } while (*num_parents <= MAX_PARENT); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static u32 versal_clock_get_div(u32 clk_id) |
| 364 | { |
| 365 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 366 | u32 div; |
| 367 | |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 368 | xilinx_pm_request(PM_CLOCK_GETDIVIDER, clk_id, 0, 0, 0, ret_payload); |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 369 | div = ret_payload[1]; |
| 370 | |
| 371 | return div; |
| 372 | } |
| 373 | |
| 374 | static u32 versal_clock_set_div(u32 clk_id, u32 div) |
| 375 | { |
| 376 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 377 | |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 378 | xilinx_pm_request(PM_CLOCK_SETDIVIDER, clk_id, div, 0, 0, ret_payload); |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 379 | |
| 380 | return div; |
| 381 | } |
| 382 | |
| 383 | static u64 versal_clock_ref(u32 clk_id) |
| 384 | { |
| 385 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 386 | int ref; |
| 387 | |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 388 | xilinx_pm_request(PM_CLOCK_GETPARENT, clk_id, 0, 0, 0, ret_payload); |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 389 | ref = ret_payload[0]; |
| 390 | if (!(ref & 1)) |
| 391 | return ref_clk; |
| 392 | if (ref & 2) |
| 393 | return pl_alt_ref_clk; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static u64 versal_clock_get_pll_rate(u32 clk_id) |
| 398 | { |
| 399 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 400 | u32 fbdiv; |
| 401 | u32 res; |
| 402 | u32 frac; |
| 403 | u64 freq; |
| 404 | u32 parent_rate, parent_id; |
| 405 | u32 id = clk_id & 0xFFF; |
| 406 | |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 407 | xilinx_pm_request(PM_CLOCK_GETSTATE, clk_id, 0, 0, 0, ret_payload); |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 408 | res = ret_payload[1]; |
| 409 | if (!res) { |
| 410 | printf("0%x PLL not enabled\n", clk_id); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | parent_id = clock[clock[id].parent[0].id].clk_id; |
| 415 | parent_rate = versal_clock_ref(parent_id); |
| 416 | |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 417 | xilinx_pm_request(PM_CLOCK_GETDIVIDER, clk_id, 0, 0, 0, ret_payload); |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 418 | fbdiv = ret_payload[1]; |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 419 | xilinx_pm_request(PM_CLOCK_PLL_GETPARAM, clk_id, 2, 0, 0, ret_payload); |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 420 | frac = ret_payload[1]; |
| 421 | |
| 422 | freq = (fbdiv * parent_rate) >> (1 << frac); |
| 423 | |
| 424 | return freq; |
| 425 | } |
| 426 | |
| 427 | static u32 versal_clock_mux(u32 clk_id) |
| 428 | { |
| 429 | int i; |
| 430 | u32 id = clk_id & 0xFFF; |
| 431 | |
| 432 | for (i = 0; i < clock[id].num_nodes; i++) |
| 433 | if (clock[id].node[i].type == CLOCK_NODE_TYPE_MUX) |
| 434 | return 1; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static u32 versal_clock_get_parentid(u32 clk_id) |
| 440 | { |
| 441 | u32 parent_id = 0; |
| 442 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 443 | u32 id = clk_id & 0xFFF; |
| 444 | |
| 445 | if (versal_clock_mux(clk_id)) { |
Michal Simek | 142fb5b | 2019-10-04 15:52:43 +0200 | [diff] [blame] | 446 | xilinx_pm_request(PM_CLOCK_GETPARENT, clk_id, 0, 0, 0, |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 447 | ret_payload); |
| 448 | parent_id = ret_payload[1]; |
| 449 | } |
| 450 | |
| 451 | debug("parent_id:0x%x\n", clock[clock[id].parent[parent_id].id].clk_id); |
| 452 | return clock[clock[id].parent[parent_id].id].clk_id; |
| 453 | } |
| 454 | |
| 455 | static u32 versal_clock_gate(u32 clk_id) |
| 456 | { |
| 457 | u32 id = clk_id & 0xFFF; |
| 458 | int i; |
| 459 | |
| 460 | for (i = 0; i < clock[id].num_nodes; i++) |
| 461 | if (clock[id].node[i].type == CLOCK_NODE_TYPE_GATE) |
| 462 | return 1; |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static u32 versal_clock_div(u32 clk_id) |
| 468 | { |
| 469 | int i; |
| 470 | u32 id = clk_id & 0xFFF; |
| 471 | |
| 472 | for (i = 0; i < clock[id].num_nodes; i++) |
| 473 | if (clock[id].node[i].type == CLOCK_NODE_TYPE_DIV) |
| 474 | return 1; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static u32 versal_clock_pll(u32 clk_id, u64 *clk_rate) |
| 480 | { |
| 481 | if (((clk_id >> NODE_SUBCLASS_SHIFT) & NODE_CLASS_MASK) == |
| 482 | NODE_SUBCLASS_CLOCK_PLL && |
| 483 | ((clk_id >> NODE_CLASS_SHIFT) & NODE_CLASS_MASK) == |
| 484 | NODE_CLASS_CLOCK) { |
| 485 | *clk_rate = versal_clock_get_pll_rate(clk_id); |
| 486 | return 1; |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static u64 versal_clock_calc(u32 clk_id) |
| 493 | { |
| 494 | u32 parent_id; |
| 495 | u64 clk_rate; |
| 496 | u32 div; |
| 497 | |
| 498 | if (versal_clock_pll(clk_id, &clk_rate)) |
| 499 | return clk_rate; |
| 500 | |
| 501 | parent_id = versal_clock_get_parentid(clk_id); |
| 502 | if (((parent_id >> NODE_SUBCLASS_SHIFT) & |
| 503 | NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_REF) |
| 504 | return versal_clock_ref(clk_id); |
| 505 | |
T Karthik Reddy | aa12d51 | 2020-04-08 21:34:54 -0600 | [diff] [blame] | 506 | if (!parent_id) |
| 507 | return 0; |
| 508 | |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 509 | clk_rate = versal_clock_calc(parent_id); |
| 510 | |
| 511 | if (versal_clock_div(clk_id)) { |
| 512 | div = versal_clock_get_div(clk_id); |
| 513 | clk_rate = DIV_ROUND_CLOSEST(clk_rate, div); |
| 514 | } |
| 515 | |
| 516 | return clk_rate; |
| 517 | } |
| 518 | |
| 519 | static int versal_clock_get_rate(u32 clk_id, u64 *clk_rate) |
| 520 | { |
| 521 | if (((clk_id >> NODE_SUBCLASS_SHIFT) & |
| 522 | NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_REF) |
| 523 | *clk_rate = versal_clock_ref(clk_id); |
| 524 | |
| 525 | if (versal_clock_pll(clk_id, clk_rate)) |
| 526 | return 0; |
| 527 | |
| 528 | if (((clk_id >> NODE_SUBCLASS_SHIFT) & |
| 529 | NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_OUT && |
| 530 | ((clk_id >> NODE_CLASS_SHIFT) & |
| 531 | NODE_CLASS_MASK) == NODE_CLASS_CLOCK) { |
T Karthik Reddy | aa12d51 | 2020-04-08 21:34:54 -0600 | [diff] [blame] | 532 | if (!versal_clock_gate(clk_id) && !versal_clock_mux(clk_id)) |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 533 | return -EINVAL; |
| 534 | *clk_rate = versal_clock_calc(clk_id); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | int soc_clk_dump(void) |
| 542 | { |
| 543 | u64 clk_rate = 0; |
| 544 | u32 type, ret, i = 0; |
| 545 | |
| 546 | printf("\n ****** VERSAL CLOCKS *****\n"); |
| 547 | |
| 548 | printf("alt_ref_clk:%ld pl_alt_ref_clk:%ld ref_clk:%ld\n", |
| 549 | alt_ref_clk, pl_alt_ref_clk, ref_clk); |
| 550 | for (i = 0; i < clock_max_idx; i++) { |
| 551 | debug("%s\n", clock[i].clk_name); |
| 552 | ret = versal_get_clock_type(i, &type); |
| 553 | if (ret || type != CLK_TYPE_OUTPUT) |
| 554 | continue; |
| 555 | |
| 556 | ret = versal_clock_get_rate(clock[i].clk_id, &clk_rate); |
| 557 | |
| 558 | if (ret != -EINVAL) |
| 559 | printf("clk: %s freq:%lld\n", |
| 560 | clock[i].clk_name, clk_rate); |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static void versal_get_clock_info(void) |
| 567 | { |
| 568 | int i, ret; |
| 569 | u32 attr, type = 0, nodetype, subclass, class; |
| 570 | |
| 571 | for (i = 0; i < clock_max_idx; i++) { |
| 572 | ret = versal_pm_clock_get_attributes(i, &attr); |
| 573 | if (ret) |
| 574 | continue; |
| 575 | |
| 576 | clock[i].valid = attr & CLK_VALID_MASK; |
Rajan Vaja | 5986efc | 2020-01-16 03:55:05 -0800 | [diff] [blame] | 577 | |
| 578 | /* skip query for Invalid clock */ |
| 579 | ret = versal_is_valid_clock(i); |
| 580 | if (ret != CLK_VALID_MASK) |
| 581 | continue; |
| 582 | |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 583 | clock[i].type = ((attr >> CLK_TYPE_SHIFT) & 0x1) ? |
| 584 | CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT; |
| 585 | nodetype = (attr >> NODE_TYPE_SHIFT) & NODE_CLASS_MASK; |
| 586 | subclass = (attr >> NODE_SUBCLASS_SHIFT) & NODE_CLASS_MASK; |
| 587 | class = (attr >> NODE_CLASS_SHIFT) & NODE_CLASS_MASK; |
| 588 | |
| 589 | clock[i].clk_id = (class << NODE_CLASS_SHIFT) | |
| 590 | (subclass << NODE_SUBCLASS_SHIFT) | |
| 591 | (nodetype << NODE_TYPE_SHIFT) | |
| 592 | (i << NODE_INDEX_SHIFT); |
| 593 | |
| 594 | ret = versal_pm_clock_get_name(clock[i].clk_id, |
| 595 | clock[i].clk_name); |
| 596 | if (ret) |
| 597 | continue; |
| 598 | debug("clk name:%s, Valid:%d, type:%d, clk_id:0x%x\n", |
| 599 | clock[i].clk_name, clock[i].valid, |
| 600 | clock[i].type, clock[i].clk_id); |
| 601 | } |
| 602 | |
| 603 | /* Get topology of all clock */ |
| 604 | for (i = 0; i < clock_max_idx; i++) { |
| 605 | ret = versal_get_clock_type(i, &type); |
| 606 | if (ret || type != CLK_TYPE_OUTPUT) |
| 607 | continue; |
| 608 | debug("clk name:%s\n", clock[i].clk_name); |
| 609 | ret = versal_clock_get_topology(i, clock[i].node, |
| 610 | &clock[i].num_nodes); |
| 611 | if (ret) |
| 612 | continue; |
| 613 | |
| 614 | ret = versal_clock_get_parents(i, clock[i].parent, |
| 615 | &clock[i].num_parents); |
| 616 | if (ret) |
| 617 | continue; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | int versal_clock_setup(void) |
| 622 | { |
| 623 | int ret; |
| 624 | |
| 625 | ret = versal_pm_clock_get_num_clocks(&clock_max_idx); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | |
| 629 | debug("%s, clock_max_idx:0x%x\n", __func__, clock_max_idx); |
| 630 | clock = calloc(clock_max_idx, sizeof(*clock)); |
| 631 | if (!clock) |
| 632 | return -ENOMEM; |
| 633 | |
| 634 | versal_get_clock_info(); |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static int versal_clock_get_freq_by_name(char *name, struct udevice *dev, |
| 640 | ulong *freq) |
| 641 | { |
| 642 | struct clk clk; |
| 643 | int ret; |
| 644 | |
| 645 | ret = clk_get_by_name(dev, name, &clk); |
| 646 | if (ret < 0) { |
| 647 | dev_err(dev, "failed to get %s\n", name); |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | *freq = clk_get_rate(&clk); |
| 652 | if (IS_ERR_VALUE(*freq)) { |
| 653 | dev_err(dev, "failed to get rate %s\n", name); |
| 654 | return -EINVAL; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int versal_clk_probe(struct udevice *dev) |
| 661 | { |
| 662 | int ret; |
| 663 | struct versal_clk_priv *priv = dev_get_priv(dev); |
| 664 | |
| 665 | debug("%s\n", __func__); |
| 666 | |
| 667 | ret = versal_clock_get_freq_by_name("alt_ref_clk", dev, &alt_ref_clk); |
| 668 | if (ret < 0) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | ret = versal_clock_get_freq_by_name("pl_alt_ref_clk", |
| 672 | dev, &pl_alt_ref_clk); |
| 673 | if (ret < 0) |
| 674 | return -EINVAL; |
| 675 | |
| 676 | ret = versal_clock_get_freq_by_name("ref_clk", dev, &ref_clk); |
| 677 | if (ret < 0) |
| 678 | return -EINVAL; |
| 679 | |
| 680 | versal_clock_setup(); |
| 681 | |
| 682 | priv->clk = clock; |
| 683 | |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | static ulong versal_clk_get_rate(struct clk *clk) |
| 688 | { |
| 689 | struct versal_clk_priv *priv = dev_get_priv(clk->dev); |
| 690 | u32 id = clk->id; |
| 691 | u32 clk_id; |
| 692 | u64 clk_rate = 0; |
| 693 | |
| 694 | debug("%s\n", __func__); |
| 695 | |
| 696 | clk_id = priv->clk[id].clk_id; |
| 697 | |
| 698 | versal_clock_get_rate(clk_id, &clk_rate); |
| 699 | |
| 700 | return clk_rate; |
| 701 | } |
| 702 | |
| 703 | static ulong versal_clk_set_rate(struct clk *clk, ulong rate) |
| 704 | { |
| 705 | struct versal_clk_priv *priv = dev_get_priv(clk->dev); |
| 706 | u32 id = clk->id; |
| 707 | u32 clk_id; |
| 708 | u64 clk_rate = 0; |
| 709 | u32 div; |
| 710 | int ret; |
| 711 | |
| 712 | debug("%s\n", __func__); |
| 713 | |
| 714 | clk_id = priv->clk[id].clk_id; |
| 715 | |
| 716 | ret = versal_clock_get_rate(clk_id, &clk_rate); |
| 717 | if (ret) { |
| 718 | printf("Clock is not a Gate:0x%x\n", clk_id); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | do { |
| 723 | if (versal_clock_div(clk_id)) { |
| 724 | div = versal_clock_get_div(clk_id); |
| 725 | clk_rate *= div; |
| 726 | div = DIV_ROUND_CLOSEST(clk_rate, rate); |
| 727 | versal_clock_set_div(clk_id, div); |
| 728 | debug("%s, div:%d, newrate:%lld\n", __func__, |
| 729 | div, DIV_ROUND_CLOSEST(clk_rate, div)); |
| 730 | return DIV_ROUND_CLOSEST(clk_rate, div); |
| 731 | } |
| 732 | clk_id = versal_clock_get_parentid(clk_id); |
| 733 | } while (((clk_id >> NODE_SUBCLASS_SHIFT) & |
| 734 | NODE_CLASS_MASK) != NODE_SUBCLASS_CLOCK_REF); |
| 735 | |
| 736 | printf("Clock didn't has Divisors:0x%x\n", priv->clk[id].clk_id); |
| 737 | |
| 738 | return clk_rate; |
| 739 | } |
| 740 | |
| 741 | static struct clk_ops versal_clk_ops = { |
| 742 | .set_rate = versal_clk_set_rate, |
| 743 | .get_rate = versal_clk_get_rate, |
| 744 | }; |
| 745 | |
| 746 | static const struct udevice_id versal_clk_ids[] = { |
| 747 | { .compatible = "xlnx,versal-clk" }, |
| 748 | { } |
| 749 | }; |
| 750 | |
| 751 | U_BOOT_DRIVER(versal_clk) = { |
| 752 | .name = "versal-clk", |
| 753 | .id = UCLASS_CLK, |
| 754 | .of_match = versal_clk_ids, |
| 755 | .probe = versal_clk_probe, |
| 756 | .ops = &versal_clk_ops, |
| 757 | .priv_auto_alloc_size = sizeof(struct versal_clk_priv), |
| 758 | }; |