clk-usb.c 6.6 KB

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