1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SCI Clock driver for keystone based devices 4 * 5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/ 6 * Tero Kristo <t-kristo@ti.com> 7 */ 8 #include <linux/clk-provider.h> 9 #include <linux/err.h> 10 #include <linux/io.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/slab.h> 15 #include <linux/soc/ti/ti_sci_protocol.h> 16 #include <linux/bsearch.h> 17 #include <linux/list_sort.h> 18 19 #define SCI_CLK_SSC_ENABLE BIT(0) 20 #define SCI_CLK_ALLOW_FREQ_CHANGE BIT(1) 21 #define SCI_CLK_INPUT_TERMINATION BIT(2) 22 23 /** 24 * struct sci_clk_provider - TI SCI clock provider representation 25 * @sci: Handle to the System Control Interface protocol handler 26 * @ops: Pointer to the SCI ops to be used by the clocks 27 * @dev: Device pointer for the clock provider 28 * @clocks: Clocks array for this device 29 * @num_clocks: Total number of clocks for this provider 30 */ 31 struct sci_clk_provider { 32 const struct ti_sci_handle *sci; 33 const struct ti_sci_clk_ops *ops; 34 struct device *dev; 35 struct sci_clk **clocks; 36 int num_clocks; 37 }; 38 39 /** 40 * struct sci_clk - TI SCI clock representation 41 * @hw: Hardware clock cookie for common clock framework 42 * @dev_id: Device index 43 * @clk_id: Clock index 44 * @num_parents: Number of parents for this clock 45 * @provider: Master clock provider 46 * @flags: Flags for the clock 47 * @node: Link for handling clocks probed via DT 48 * @cached_req: Cached requested freq for determine rate calls 49 * @cached_res: Cached result freq for determine rate calls 50 * @parent_id: Parent index for this clock 51 * @rate: Clock rate 52 */ 53 struct sci_clk { 54 struct clk_hw hw; 55 u16 dev_id; 56 u32 clk_id; 57 u32 num_parents; 58 struct sci_clk_provider *provider; 59 u8 flags; 60 struct list_head node; 61 unsigned long cached_req; 62 unsigned long cached_res; 63 int parent_id; 64 unsigned long rate; 65 }; 66 67 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw) 68 69 /** 70 * sci_clk_prepare - Prepare (enable) a TI SCI clock 71 * @hw: clock to prepare 72 * 73 * Prepares a clock to be actively used. Returns the SCI protocol status. 74 */ 75 static int sci_clk_prepare(struct clk_hw *hw) 76 { 77 struct sci_clk *clk = to_sci_clk(hw); 78 bool enable_ssc = clk->flags & SCI_CLK_SSC_ENABLE; 79 bool allow_freq_change = clk->flags & SCI_CLK_ALLOW_FREQ_CHANGE; 80 bool input_termination = clk->flags & SCI_CLK_INPUT_TERMINATION; 81 82 return clk->provider->ops->get_clock(clk->provider->sci, clk->dev_id, 83 clk->clk_id, enable_ssc, 84 allow_freq_change, 85 input_termination); 86 } 87 88 /** 89 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock 90 * @hw: clock to unprepare 91 * 92 * Un-prepares a clock from active state. 93 */ 94 static void sci_clk_unprepare(struct clk_hw *hw) 95 { 96 struct sci_clk *clk = to_sci_clk(hw); 97 int ret; 98 99 ret = clk->provider->ops->put_clock(clk->provider->sci, clk->dev_id, 100 clk->clk_id); 101 if (ret) 102 dev_err(clk->provider->dev, 103 "unprepare failed for dev=%d, clk=%d, ret=%d\n", 104 clk->dev_id, clk->clk_id, ret); 105 } 106 107 /** 108 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not 109 * @hw: clock to check status for 110 * 111 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero 112 * value if clock is enabled, zero otherwise. 113 */ 114 static int sci_clk_is_prepared(struct clk_hw *hw) 115 { 116 struct sci_clk *clk = to_sci_clk(hw); 117 bool req_state, current_state; 118 int ret; 119 120 ret = clk->provider->ops->is_on(clk->provider->sci, clk->dev_id, 121 clk->clk_id, &req_state, 122 ¤t_state); 123 if (ret) { 124 dev_err(clk->provider->dev, 125 "is_prepared failed for dev=%d, clk=%d, ret=%d\n", 126 clk->dev_id, clk->clk_id, ret); 127 return 0; 128 } 129 130 return req_state; 131 } 132 133 /** 134 * sci_clk_recalc_rate - Get clock rate for a TI SCI clock 135 * @hw: clock to get rate for 136 * @parent_rate: parent rate provided by common clock framework, not used 137 * 138 * Gets the current clock rate of a TI SCI clock. Returns the current 139 * clock rate, or zero in failure. 140 */ 141 static unsigned long sci_clk_recalc_rate(struct clk_hw *hw, 142 unsigned long parent_rate) 143 { 144 struct sci_clk *clk = to_sci_clk(hw); 145 u64 freq; 146 int ret; 147 148 ret = clk->provider->ops->get_freq(clk->provider->sci, clk->dev_id, 149 clk->clk_id, &freq); 150 if (ret) { 151 dev_err(clk->provider->dev, 152 "recalc-rate failed for dev=%d, clk=%d, ret=%d\n", 153 clk->dev_id, clk->clk_id, ret); 154 return 0; 155 } 156 157 clk->rate = freq; 158 159 return freq; 160 } 161 162 /** 163 * sci_clk_determine_rate - Determines a clock rate a clock can be set to 164 * @hw: clock to change rate for 165 * @req: requested rate configuration for the clock 166 * 167 * Determines a suitable clock rate and parent for a TI SCI clock. 168 * The parent handling is un-used, as generally the parent clock rates 169 * are not known by the kernel; instead these are internally handled 170 * by the firmware. Returns 0 on success, negative error value on failure. 171 */ 172 static int sci_clk_determine_rate(struct clk_hw *hw, 173 struct clk_rate_request *req) 174 { 175 struct sci_clk *clk = to_sci_clk(hw); 176 int ret; 177 u64 new_rate; 178 179 if (clk->cached_req && clk->cached_req == req->rate) { 180 req->rate = clk->cached_res; 181 return 0; 182 } 183 184 ret = clk->provider->ops->get_best_match_freq(clk->provider->sci, 185 clk->dev_id, 186 clk->clk_id, 187 req->min_rate, 188 req->rate, 189 req->max_rate, 190 &new_rate); 191 if (ret) { 192 dev_err(clk->provider->dev, 193 "determine-rate failed for dev=%d, clk=%d, ret=%d\n", 194 clk->dev_id, clk->clk_id, ret); 195 return ret; 196 } 197 198 clk->cached_req = req->rate; 199 clk->cached_res = new_rate; 200 201 req->rate = new_rate; 202 203 return 0; 204 } 205 206 /** 207 * sci_clk_set_rate - Set rate for a TI SCI clock 208 * @hw: clock to change rate for 209 * @rate: target rate for the clock 210 * @parent_rate: rate of the clock parent, not used for TI SCI clocks 211 * 212 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI 213 * protocol status. 214 */ 215 static int sci_clk_set_rate(struct clk_hw *hw, unsigned long rate, 216 unsigned long parent_rate) 217 { 218 struct sci_clk *clk = to_sci_clk(hw); 219 int ret; 220 221 ret = clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id, 222 clk->clk_id, rate / 10 * 9, rate, 223 rate / 10 * 11); 224 if (!ret) 225 clk->rate = rate; 226 227 return ret; 228 } 229 230 /** 231 * sci_clk_get_parent - Get the current parent of a TI SCI clock 232 * @hw: clock to get parent for 233 * 234 * Returns the index of the currently selected parent for a TI SCI clock. 235 */ 236 static u8 sci_clk_get_parent(struct clk_hw *hw) 237 { 238 struct sci_clk *clk = to_sci_clk(hw); 239 u32 parent_id = 0; 240 int ret; 241 242 ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id, 243 clk->clk_id, (void *)&parent_id); 244 if (ret) { 245 dev_err(clk->provider->dev, 246 "get-parent failed for dev=%d, clk=%d, ret=%d\n", 247 clk->dev_id, clk->clk_id, ret); 248 clk->parent_id = ret; 249 return 0; 250 } 251 252 clk->parent_id = parent_id - clk->clk_id - 1; 253 254 return (u8)clk->parent_id; 255 } 256 257 /** 258 * sci_clk_set_parent - Set the parent of a TI SCI clock 259 * @hw: clock to set parent for 260 * @index: new parent index for the clock 261 * 262 * Sets the parent of a TI SCI clock. Return TI SCI protocol status. 263 */ 264 static int sci_clk_set_parent(struct clk_hw *hw, u8 index) 265 { 266 struct sci_clk *clk = to_sci_clk(hw); 267 int ret; 268 269 clk->cached_req = 0; 270 271 ret = clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id, 272 clk->clk_id, 273 index + 1 + clk->clk_id); 274 if (!ret) 275 clk->parent_id = index; 276 277 return ret; 278 } 279 280 static void sci_clk_restore_context(struct clk_hw *hw) 281 { 282 struct sci_clk *clk = to_sci_clk(hw); 283 284 if (clk->num_parents > 1 && clk->parent_id >= 0) 285 sci_clk_set_parent(hw, (u8)clk->parent_id); 286 287 if (clk->rate) 288 sci_clk_set_rate(hw, clk->rate, 0); 289 } 290 291 static const struct clk_ops sci_clk_ops = { 292 .prepare = sci_clk_prepare, 293 .unprepare = sci_clk_unprepare, 294 .is_prepared = sci_clk_is_prepared, 295 .recalc_rate = sci_clk_recalc_rate, 296 .determine_rate = sci_clk_determine_rate, 297 .set_rate = sci_clk_set_rate, 298 .get_parent = sci_clk_get_parent, 299 .set_parent = sci_clk_set_parent, 300 .restore_context = sci_clk_restore_context, 301 }; 302 303 /** 304 * _sci_clk_build - Gets a handle for an SCI clock 305 * @provider: Handle to SCI clock provider 306 * @sci_clk: Handle to the SCI clock to populate 307 * 308 * Gets a handle to an existing TI SCI hw clock, or builds a new clock 309 * entry and registers it with the common clock framework. Called from 310 * the common clock framework, when a corresponding of_clk_get call is 311 * executed, or recursively from itself when parsing parent clocks. 312 * Returns 0 on success, negative error code on failure. 313 */ 314 static int _sci_clk_build(struct sci_clk_provider *provider, 315 struct sci_clk *sci_clk) 316 { 317 struct clk_init_data init = { NULL }; 318 char *name = NULL; 319 char **parent_names = NULL; 320 int i; 321 int ret = 0; 322 323 name = kasprintf(GFP_KERNEL, "clk:%d:%d", sci_clk->dev_id, 324 sci_clk->clk_id); 325 if (!name) 326 return -ENOMEM; 327 328 init.name = name; 329 330 /* 331 * From kernel point of view, we only care about a clocks parents, 332 * if it has more than 1 possible parent. In this case, it is going 333 * to have mux functionality. Otherwise it is going to act as a root 334 * clock. 335 */ 336 if (sci_clk->num_parents < 2) 337 sci_clk->num_parents = 0; 338 339 if (sci_clk->num_parents) { 340 parent_names = kcalloc(sci_clk->num_parents, sizeof(char *), 341 GFP_KERNEL); 342 343 if (!parent_names) { 344 ret = -ENOMEM; 345 goto err; 346 } 347 348 for (i = 0; i < sci_clk->num_parents; i++) { 349 char *parent_name; 350 351 parent_name = kasprintf(GFP_KERNEL, "clk:%d:%d", 352 sci_clk->dev_id, 353 sci_clk->clk_id + 1 + i); 354 if (!parent_name) { 355 ret = -ENOMEM; 356 goto err; 357 } 358 parent_names[i] = parent_name; 359 } 360 init.parent_names = (void *)parent_names; 361 } 362 363 init.ops = &sci_clk_ops; 364 init.num_parents = sci_clk->num_parents; 365 sci_clk->hw.init = &init; 366 367 ret = devm_clk_hw_register(provider->dev, &sci_clk->hw); 368 if (ret) 369 dev_err(provider->dev, "failed clk register with %d\n", ret); 370 371 err: 372 if (parent_names) { 373 for (i = 0; i < sci_clk->num_parents; i++) 374 kfree(parent_names[i]); 375 376 kfree(parent_names); 377 } 378 379 kfree(name); 380 381 return ret; 382 } 383 384 static int _cmp_sci_clk(const void *a, const void *b) 385 { 386 const struct sci_clk *ca = a; 387 const struct sci_clk *cb = *(struct sci_clk **)b; 388 389 if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id) 390 return 0; 391 if (ca->dev_id > cb->dev_id || 392 (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id)) 393 return 1; 394 return -1; 395 } 396 397 /** 398 * sci_clk_get - Xlate function for getting clock handles 399 * @clkspec: device tree clock specifier 400 * @data: pointer to the clock provider 401 * 402 * Xlate function for retrieving clock TI SCI hw clock handles based on 403 * device tree clock specifier. Called from the common clock framework, 404 * when a corresponding of_clk_get call is executed. Returns a pointer 405 * to the TI SCI hw clock struct, or ERR_PTR value in failure. 406 */ 407 static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data) 408 { 409 struct sci_clk_provider *provider = data; 410 struct sci_clk **clk; 411 struct sci_clk key; 412 413 if (clkspec->args_count != 2) 414 return ERR_PTR(-EINVAL); 415 416 key.dev_id = clkspec->args[0]; 417 key.clk_id = clkspec->args[1]; 418 419 clk = bsearch(&key, provider->clocks, provider->num_clocks, 420 sizeof(clk), _cmp_sci_clk); 421 422 if (!clk) 423 return ERR_PTR(-ENODEV); 424 425 return &(*clk)->hw; 426 } 427 428 static int ti_sci_init_clocks(struct sci_clk_provider *p) 429 { 430 int i; 431 int ret; 432 433 for (i = 0; i < p->num_clocks; i++) { 434 ret = _sci_clk_build(p, p->clocks[i]); 435 if (ret) 436 return ret; 437 } 438 439 return 0; 440 } 441 442 static const struct of_device_id ti_sci_clk_of_match[] = { 443 { .compatible = "ti,k2g-sci-clk" }, 444 { /* Sentinel */ }, 445 }; 446 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match); 447 448 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW 449 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider) 450 { 451 int ret; 452 int num_clks = 0; 453 struct sci_clk **clks = NULL; 454 struct sci_clk **tmp_clks; 455 struct sci_clk *sci_clk; 456 int max_clks = 0; 457 int clk_id = 0; 458 int dev_id = 0; 459 u32 num_parents = 0; 460 int gap_size = 0; 461 struct device *dev = provider->dev; 462 463 while (1) { 464 ret = provider->ops->get_num_parents(provider->sci, dev_id, 465 clk_id, 466 (void *)&num_parents); 467 if (ret) { 468 gap_size++; 469 if (!clk_id) { 470 if (gap_size >= 5) 471 break; 472 dev_id++; 473 } else { 474 if (gap_size >= 2) { 475 dev_id++; 476 clk_id = 0; 477 gap_size = 0; 478 } else { 479 clk_id++; 480 } 481 } 482 continue; 483 } 484 485 gap_size = 0; 486 487 if (num_clks == max_clks) { 488 tmp_clks = devm_kmalloc_array(dev, max_clks + 64, 489 sizeof(sci_clk), 490 GFP_KERNEL); 491 memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk)); 492 if (max_clks) 493 devm_kfree(dev, clks); 494 max_clks += 64; 495 clks = tmp_clks; 496 } 497 498 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), GFP_KERNEL); 499 if (!sci_clk) 500 return -ENOMEM; 501 sci_clk->dev_id = dev_id; 502 sci_clk->clk_id = clk_id; 503 sci_clk->provider = provider; 504 sci_clk->num_parents = num_parents; 505 506 clks[num_clks] = sci_clk; 507 508 clk_id++; 509 num_clks++; 510 } 511 512 provider->clocks = devm_kmemdup_array(dev, clks, num_clks, sizeof(sci_clk), GFP_KERNEL); 513 if (!provider->clocks) 514 return -ENOMEM; 515 516 provider->num_clocks = num_clks; 517 518 devm_kfree(dev, clks); 519 520 return 0; 521 } 522 523 #else 524 525 static int _cmp_sci_clk_list(void *priv, const struct list_head *a, 526 const struct list_head *b) 527 { 528 const struct sci_clk *ca = container_of(a, struct sci_clk, node); 529 const struct sci_clk *cb = container_of(b, struct sci_clk, node); 530 531 return _cmp_sci_clk(ca, &cb); 532 } 533 534 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider *provider) 535 { 536 struct device *dev = provider->dev; 537 struct device_node *np = NULL; 538 int ret; 539 int index; 540 struct of_phandle_args args; 541 struct list_head clks; 542 struct sci_clk *sci_clk, *prev; 543 int num_clks = 0; 544 int num_parents; 545 bool state; 546 int clk_id; 547 const char * const clk_names[] = { 548 "clocks", "assigned-clocks", "assigned-clock-parents", NULL 549 }; 550 const char * const *clk_name; 551 552 INIT_LIST_HEAD(&clks); 553 554 clk_name = clk_names; 555 556 while (*clk_name) { 557 np = of_find_node_with_property(np, *clk_name); 558 if (!np) { 559 clk_name++; 560 continue; 561 } 562 563 if (!of_device_is_available(np)) 564 continue; 565 566 index = 0; 567 568 do { 569 ret = of_parse_phandle_with_args(np, *clk_name, 570 "#clock-cells", index, 571 &args); 572 if (ret) 573 break; 574 575 if (args.args_count == 2 && args.np == dev->of_node) { 576 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), 577 GFP_KERNEL); 578 if (!sci_clk) 579 return -ENOMEM; 580 581 sci_clk->dev_id = args.args[0]; 582 sci_clk->clk_id = args.args[1]; 583 sci_clk->provider = provider; 584 provider->ops->get_num_parents(provider->sci, 585 sci_clk->dev_id, 586 sci_clk->clk_id, 587 (void *)&sci_clk->num_parents); 588 list_add_tail(&sci_clk->node, &clks); 589 590 num_clks++; 591 592 num_parents = sci_clk->num_parents; 593 if (num_parents == 1) 594 num_parents = 0; 595 596 /* 597 * Linux kernel has inherent limitation 598 * of 255 clock parents at the moment. 599 * Right now, it is not expected that 600 * any mux clock from sci-clk driver 601 * would exceed that limit either, but 602 * the ABI basically provides that 603 * possibility. Print out a warning if 604 * this happens for any clock. 605 */ 606 if (num_parents >= 255) { 607 dev_warn(dev, "too many parents for dev=%d, clk=%d (%d), cropping to 255.\n", 608 sci_clk->dev_id, 609 sci_clk->clk_id, num_parents); 610 num_parents = 255; 611 } 612 613 clk_id = args.args[1] + 1; 614 615 while (num_parents--) { 616 /* Check if this clock id is valid */ 617 ret = provider->ops->is_auto(provider->sci, 618 sci_clk->dev_id, clk_id, &state); 619 620 if (ret) { 621 clk_id++; 622 continue; 623 } 624 625 sci_clk = devm_kzalloc(dev, 626 sizeof(*sci_clk), 627 GFP_KERNEL); 628 if (!sci_clk) 629 return -ENOMEM; 630 sci_clk->dev_id = args.args[0]; 631 sci_clk->clk_id = clk_id++; 632 sci_clk->provider = provider; 633 list_add_tail(&sci_clk->node, &clks); 634 635 num_clks++; 636 } 637 } 638 639 index++; 640 } while (args.np); 641 } 642 643 list_sort(NULL, &clks, _cmp_sci_clk_list); 644 645 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk), 646 GFP_KERNEL); 647 if (!provider->clocks) 648 return -ENOMEM; 649 650 num_clks = 0; 651 prev = NULL; 652 653 list_for_each_entry(sci_clk, &clks, node) { 654 if (prev && prev->dev_id == sci_clk->dev_id && 655 prev->clk_id == sci_clk->clk_id) 656 continue; 657 658 provider->clocks[num_clks++] = sci_clk; 659 prev = sci_clk; 660 } 661 662 provider->num_clocks = num_clks; 663 664 return 0; 665 } 666 #endif 667 668 /** 669 * ti_sci_clk_probe - Probe function for the TI SCI clock driver 670 * @pdev: platform device pointer to be probed 671 * 672 * Probes the TI SCI clock device. Allocates a new clock provider 673 * and registers this to the common clock framework. Also applies 674 * any required flags to the identified clocks via clock lists 675 * supplied from DT. Returns 0 for success, negative error value 676 * for failure. 677 */ 678 static int ti_sci_clk_probe(struct platform_device *pdev) 679 { 680 struct device *dev = &pdev->dev; 681 struct device_node *np = dev->of_node; 682 struct sci_clk_provider *provider; 683 const struct ti_sci_handle *handle; 684 int ret; 685 686 handle = devm_ti_sci_get_handle(dev); 687 if (IS_ERR(handle)) 688 return PTR_ERR(handle); 689 690 provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL); 691 if (!provider) 692 return -ENOMEM; 693 694 provider->sci = handle; 695 provider->ops = &handle->ops.clk_ops; 696 provider->dev = dev; 697 698 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW 699 ret = ti_sci_scan_clocks_from_fw(provider); 700 if (ret) { 701 dev_err(dev, "scan clocks from FW failed: %d\n", ret); 702 return ret; 703 } 704 #else 705 ret = ti_sci_scan_clocks_from_dt(provider); 706 if (ret) { 707 dev_err(dev, "scan clocks from DT failed: %d\n", ret); 708 return ret; 709 } 710 #endif 711 712 ret = ti_sci_init_clocks(provider); 713 if (ret) { 714 pr_err("ti-sci-init-clocks failed.\n"); 715 return ret; 716 } 717 718 return of_clk_add_hw_provider(np, sci_clk_get, provider); 719 } 720 721 /** 722 * ti_sci_clk_remove - Remove TI SCI clock device 723 * @pdev: platform device pointer for the device to be removed 724 * 725 * Removes the TI SCI device. Unregisters the clock provider registered 726 * via common clock framework. Any memory allocated for the device will 727 * be free'd silently via the devm framework. Returns 0 always. 728 */ 729 static void ti_sci_clk_remove(struct platform_device *pdev) 730 { 731 of_clk_del_provider(pdev->dev.of_node); 732 } 733 734 static struct platform_driver ti_sci_clk_driver = { 735 .probe = ti_sci_clk_probe, 736 .remove = ti_sci_clk_remove, 737 .driver = { 738 .name = "ti-sci-clk", 739 .of_match_table = of_match_ptr(ti_sci_clk_of_match), 740 }, 741 }; 742 module_platform_driver(ti_sci_clk_driver); 743 744 MODULE_LICENSE("GPL v2"); 745 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver"); 746 MODULE_AUTHOR("Tero Kristo"); 747 MODULE_ALIAS("platform:ti-sci-clk"); 748