clk-sun8i-bus-gates.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2015 Jens Kuske <jenskuske@gmail.com>
  3. *
  4. * Based on clk-simple-gates.c, which is:
  5. * Copyright 2015 Maxime Ripard
  6. *
  7. * Maxime Ripard <maxime.ripard@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk-provider.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. static DEFINE_SPINLOCK(gates_lock);
  25. static void __init sun8i_h3_bus_gates_init(struct device_node *node)
  26. {
  27. static const char * const names[] = { "ahb1", "ahb2", "apb1", "apb2" };
  28. enum { AHB1, AHB2, APB1, APB2, PARENT_MAX } clk_parent;
  29. const char *parents[PARENT_MAX];
  30. struct clk_onecell_data *clk_data;
  31. const char *clk_name;
  32. struct property *prop;
  33. struct resource res;
  34. void __iomem *clk_reg;
  35. void __iomem *reg;
  36. const __be32 *p;
  37. int number, i;
  38. u8 clk_bit;
  39. int index;
  40. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  41. if (IS_ERR(reg))
  42. return;
  43. for (i = 0; i < ARRAY_SIZE(names); i++) {
  44. int idx = of_property_match_string(node, "clock-names",
  45. names[i]);
  46. if (idx < 0)
  47. return;
  48. parents[i] = of_clk_get_parent_name(node, idx);
  49. }
  50. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  51. if (!clk_data)
  52. goto err_unmap;
  53. number = of_property_count_u32_elems(node, "clock-indices");
  54. of_property_read_u32_index(node, "clock-indices", number - 1, &number);
  55. clk_data->clks = kcalloc(number + 1, sizeof(struct clk *), GFP_KERNEL);
  56. if (!clk_data->clks)
  57. goto err_free_data;
  58. i = 0;
  59. of_property_for_each_u32(node, "clock-indices", prop, p, index) {
  60. of_property_read_string_index(node, "clock-output-names",
  61. i, &clk_name);
  62. if (index == 17 || (index >= 29 && index <= 31))
  63. clk_parent = AHB2;
  64. else if (index <= 63 || index >= 128)
  65. clk_parent = AHB1;
  66. else if (index >= 64 && index <= 95)
  67. clk_parent = APB1;
  68. else if (index >= 96 && index <= 127)
  69. clk_parent = APB2;
  70. clk_reg = reg + 4 * (index / 32);
  71. clk_bit = index % 32;
  72. clk_data->clks[index] = clk_register_gate(NULL, clk_name,
  73. parents[clk_parent],
  74. 0, clk_reg, clk_bit,
  75. 0, &gates_lock);
  76. i++;
  77. if (IS_ERR(clk_data->clks[index])) {
  78. WARN_ON(true);
  79. continue;
  80. }
  81. }
  82. clk_data->clk_num = number + 1;
  83. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  84. return;
  85. err_free_data:
  86. kfree(clk_data);
  87. err_unmap:
  88. iounmap(reg);
  89. of_address_to_resource(node, 0, &res);
  90. release_mem_region(res.start, resource_size(&res));
  91. }
  92. CLK_OF_DECLARE(sun8i_h3_bus_gates, "allwinner,sun8i-h3-bus-gates-clk",
  93. sun8i_h3_bus_gates_init);
  94. CLK_OF_DECLARE(sun8i_a83t_bus_gates, "allwinner,sun8i-a83t-bus-gates-clk",
  95. sun8i_h3_bus_gates_init);