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