clk-usb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2013-2015 Emilio López
  3. *
  4. * Emilio López <emilio@elopez.com.ar>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/reset-controller.h>
  21. #include <linux/spinlock.h>
  22. /**
  23. * sunxi_usb_reset... - reset bits in usb clk registers handling
  24. */
  25. struct usb_reset_data {
  26. void __iomem *reg;
  27. spinlock_t *lock;
  28. struct clk *clk;
  29. struct reset_controller_dev rcdev;
  30. };
  31. static int sunxi_usb_reset_assert(struct reset_controller_dev *rcdev,
  32. unsigned long id)
  33. {
  34. struct usb_reset_data *data = container_of(rcdev,
  35. struct usb_reset_data,
  36. rcdev);
  37. unsigned long flags;
  38. u32 reg;
  39. clk_prepare_enable(data->clk);
  40. spin_lock_irqsave(data->lock, flags);
  41. reg = readl(data->reg);
  42. writel(reg & ~BIT(id), data->reg);
  43. spin_unlock_irqrestore(data->lock, flags);
  44. clk_disable_unprepare(data->clk);
  45. return 0;
  46. }
  47. static int sunxi_usb_reset_deassert(struct reset_controller_dev *rcdev,
  48. unsigned long id)
  49. {
  50. struct usb_reset_data *data = container_of(rcdev,
  51. struct usb_reset_data,
  52. rcdev);
  53. unsigned long flags;
  54. u32 reg;
  55. clk_prepare_enable(data->clk);
  56. spin_lock_irqsave(data->lock, flags);
  57. reg = readl(data->reg);
  58. writel(reg | BIT(id), data->reg);
  59. spin_unlock_irqrestore(data->lock, flags);
  60. clk_disable_unprepare(data->clk);
  61. return 0;
  62. }
  63. static struct reset_control_ops sunxi_usb_reset_ops = {
  64. .assert = sunxi_usb_reset_assert,
  65. .deassert = sunxi_usb_reset_deassert,
  66. };
  67. /**
  68. * sunxi_usb_clk_setup() - Setup function for usb gate clocks
  69. */
  70. #define SUNXI_USB_MAX_SIZE 32
  71. struct usb_clk_data {
  72. u32 clk_mask;
  73. u32 reset_mask;
  74. bool reset_needs_clk;
  75. };
  76. static void __init sunxi_usb_clk_setup(struct device_node *node,
  77. const struct usb_clk_data *data,
  78. spinlock_t *lock)
  79. {
  80. struct clk_onecell_data *clk_data;
  81. struct usb_reset_data *reset_data;
  82. const char *clk_parent;
  83. const char *clk_name;
  84. void __iomem *reg;
  85. int qty;
  86. int i = 0;
  87. int j = 0;
  88. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  89. if (IS_ERR(reg))
  90. return;
  91. clk_parent = of_clk_get_parent_name(node, 0);
  92. if (!clk_parent)
  93. return;
  94. /* Worst-case size approximation and memory allocation */
  95. qty = find_last_bit((unsigned long *)&data->clk_mask,
  96. SUNXI_USB_MAX_SIZE);
  97. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  98. if (!clk_data)
  99. return;
  100. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  101. if (!clk_data->clks) {
  102. kfree(clk_data);
  103. return;
  104. }
  105. for_each_set_bit(i, (unsigned long *)&data->clk_mask,
  106. SUNXI_USB_MAX_SIZE) {
  107. of_property_read_string_index(node, "clock-output-names",
  108. j, &clk_name);
  109. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  110. clk_parent, 0,
  111. reg, i, 0, lock);
  112. WARN_ON(IS_ERR(clk_data->clks[i]));
  113. j++;
  114. }
  115. /* Adjust to the real max */
  116. clk_data->clk_num = i;
  117. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  118. /* Register a reset controller for usb with reset bits */
  119. if (data->reset_mask == 0)
  120. return;
  121. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  122. if (!reset_data)
  123. return;
  124. if (data->reset_needs_clk) {
  125. reset_data->clk = of_clk_get(node, 0);
  126. if (IS_ERR(reset_data->clk)) {
  127. pr_err("Could not get clock for reset controls\n");
  128. kfree(reset_data);
  129. return;
  130. }
  131. }
  132. reset_data->reg = reg;
  133. reset_data->lock = lock;
  134. reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
  135. reset_data->rcdev.ops = &sunxi_usb_reset_ops;
  136. reset_data->rcdev.of_node = node;
  137. reset_controller_register(&reset_data->rcdev);
  138. }
  139. static const struct usb_clk_data sun4i_a10_usb_clk_data __initconst = {
  140. .clk_mask = BIT(8) | BIT(7) | BIT(6),
  141. .reset_mask = BIT(2) | BIT(1) | BIT(0),
  142. };
  143. static DEFINE_SPINLOCK(sun4i_a10_usb_lock);
  144. static void __init sun4i_a10_usb_setup(struct device_node *node)
  145. {
  146. sunxi_usb_clk_setup(node, &sun4i_a10_usb_clk_data, &sun4i_a10_usb_lock);
  147. }
  148. CLK_OF_DECLARE(sun4i_a10_usb, "allwinner,sun4i-a10-usb-clk", sun4i_a10_usb_setup);
  149. static const struct usb_clk_data sun5i_a13_usb_clk_data __initconst = {
  150. .clk_mask = BIT(8) | BIT(6),
  151. .reset_mask = BIT(1) | BIT(0),
  152. };
  153. static void __init sun5i_a13_usb_setup(struct device_node *node)
  154. {
  155. sunxi_usb_clk_setup(node, &sun5i_a13_usb_clk_data, &sun4i_a10_usb_lock);
  156. }
  157. CLK_OF_DECLARE(sun5i_a13_usb, "allwinner,sun5i-a13-usb-clk", sun5i_a13_usb_setup);
  158. static const struct usb_clk_data sun6i_a31_usb_clk_data __initconst = {
  159. .clk_mask = BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8),
  160. .reset_mask = BIT(2) | BIT(1) | BIT(0),
  161. };
  162. static void __init sun6i_a31_usb_setup(struct device_node *node)
  163. {
  164. sunxi_usb_clk_setup(node, &sun6i_a31_usb_clk_data, &sun4i_a10_usb_lock);
  165. }
  166. CLK_OF_DECLARE(sun6i_a31_usb, "allwinner,sun6i-a31-usb-clk", sun6i_a31_usb_setup);
  167. static const struct usb_clk_data sun9i_a80_usb_mod_data __initconst = {
  168. .clk_mask = BIT(6) | BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1),
  169. .reset_mask = BIT(19) | BIT(18) | BIT(17),
  170. .reset_needs_clk = 1,
  171. };
  172. static DEFINE_SPINLOCK(a80_usb_mod_lock);
  173. static void __init sun9i_a80_usb_mod_setup(struct device_node *node)
  174. {
  175. sunxi_usb_clk_setup(node, &sun9i_a80_usb_mod_data, &a80_usb_mod_lock);
  176. }
  177. CLK_OF_DECLARE(sun9i_a80_usb_mod, "allwinner,sun9i-a80-usb-mod-clk", sun9i_a80_usb_mod_setup);
  178. static const struct usb_clk_data sun9i_a80_usb_phy_data __initconst = {
  179. .clk_mask = BIT(10) | BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1),
  180. .reset_mask = BIT(21) | BIT(20) | BIT(19) | BIT(18) | BIT(17),
  181. .reset_needs_clk = 1,
  182. };
  183. static DEFINE_SPINLOCK(a80_usb_phy_lock);
  184. static void __init sun9i_a80_usb_phy_setup(struct device_node *node)
  185. {
  186. sunxi_usb_clk_setup(node, &sun9i_a80_usb_phy_data, &a80_usb_phy_lock);
  187. }
  188. CLK_OF_DECLARE(sun9i_a80_usb_phy, "allwinner,sun9i-a80-usb-phy-clk", sun9i_a80_usb_phy_setup);