gate.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Clock driver for Keystone 2 based devices
  3. *
  4. * Copyright (C) 2013 Texas Instruments.
  5. * Murali Karicheri <m-karicheri2@ti.com>
  6. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk-provider.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of.h>
  19. #include <linux/module.h>
  20. /* PSC register offsets */
  21. #define PTCMD 0x120
  22. #define PTSTAT 0x128
  23. #define PDSTAT 0x200
  24. #define PDCTL 0x300
  25. #define MDSTAT 0x800
  26. #define MDCTL 0xa00
  27. /* PSC module states */
  28. #define PSC_STATE_SWRSTDISABLE 0
  29. #define PSC_STATE_SYNCRST 1
  30. #define PSC_STATE_DISABLE 2
  31. #define PSC_STATE_ENABLE 3
  32. #define MDSTAT_STATE_MASK 0x3f
  33. #define MDSTAT_MCKOUT BIT(12)
  34. #define PDSTAT_STATE_MASK 0x1f
  35. #define MDCTL_FORCE BIT(31)
  36. #define MDCTL_LRESET BIT(8)
  37. #define PDCTL_NEXT BIT(0)
  38. /* Maximum timeout to bail out state transition for module */
  39. #define STATE_TRANS_MAX_COUNT 0xffff
  40. static void __iomem *domain_transition_base;
  41. /**
  42. * struct clk_psc_data - PSC data
  43. * @control_base: Base address for a PSC control
  44. * @domain_base: Base address for a PSC domain
  45. * @domain_id: PSC domain id number
  46. */
  47. struct clk_psc_data {
  48. void __iomem *control_base;
  49. void __iomem *domain_base;
  50. u32 domain_id;
  51. };
  52. /**
  53. * struct clk_psc - PSC clock structure
  54. * @hw: clk_hw for the psc
  55. * @psc_data: PSC driver specific data
  56. * @lock: Spinlock used by the driver
  57. */
  58. struct clk_psc {
  59. struct clk_hw hw;
  60. struct clk_psc_data *psc_data;
  61. spinlock_t *lock;
  62. };
  63. static DEFINE_SPINLOCK(psc_lock);
  64. #define to_clk_psc(_hw) container_of(_hw, struct clk_psc, hw)
  65. static void psc_config(void __iomem *control_base, void __iomem *domain_base,
  66. u32 next_state, u32 domain_id)
  67. {
  68. u32 ptcmd, pdstat, pdctl, mdstat, mdctl, ptstat;
  69. u32 count = STATE_TRANS_MAX_COUNT;
  70. mdctl = readl(control_base + MDCTL);
  71. mdctl &= ~MDSTAT_STATE_MASK;
  72. mdctl |= next_state;
  73. /* For disable, we always put the module in local reset */
  74. if (next_state == PSC_STATE_DISABLE)
  75. mdctl &= ~MDCTL_LRESET;
  76. writel(mdctl, control_base + MDCTL);
  77. pdstat = readl(domain_base + PDSTAT);
  78. if (!(pdstat & PDSTAT_STATE_MASK)) {
  79. pdctl = readl(domain_base + PDCTL);
  80. pdctl |= PDCTL_NEXT;
  81. writel(pdctl, domain_base + PDCTL);
  82. }
  83. ptcmd = 1 << domain_id;
  84. writel(ptcmd, domain_transition_base + PTCMD);
  85. do {
  86. ptstat = readl(domain_transition_base + PTSTAT);
  87. } while (((ptstat >> domain_id) & 1) && count--);
  88. count = STATE_TRANS_MAX_COUNT;
  89. do {
  90. mdstat = readl(control_base + MDSTAT);
  91. } while (!((mdstat & MDSTAT_STATE_MASK) == next_state) && count--);
  92. }
  93. static int keystone_clk_is_enabled(struct clk_hw *hw)
  94. {
  95. struct clk_psc *psc = to_clk_psc(hw);
  96. struct clk_psc_data *data = psc->psc_data;
  97. u32 mdstat = readl(data->control_base + MDSTAT);
  98. return (mdstat & MDSTAT_MCKOUT) ? 1 : 0;
  99. }
  100. static int keystone_clk_enable(struct clk_hw *hw)
  101. {
  102. struct clk_psc *psc = to_clk_psc(hw);
  103. struct clk_psc_data *data = psc->psc_data;
  104. unsigned long flags = 0;
  105. if (psc->lock)
  106. spin_lock_irqsave(psc->lock, flags);
  107. psc_config(data->control_base, data->domain_base,
  108. PSC_STATE_ENABLE, data->domain_id);
  109. if (psc->lock)
  110. spin_unlock_irqrestore(psc->lock, flags);
  111. return 0;
  112. }
  113. static void keystone_clk_disable(struct clk_hw *hw)
  114. {
  115. struct clk_psc *psc = to_clk_psc(hw);
  116. struct clk_psc_data *data = psc->psc_data;
  117. unsigned long flags = 0;
  118. if (psc->lock)
  119. spin_lock_irqsave(psc->lock, flags);
  120. psc_config(data->control_base, data->domain_base,
  121. PSC_STATE_DISABLE, data->domain_id);
  122. if (psc->lock)
  123. spin_unlock_irqrestore(psc->lock, flags);
  124. }
  125. static const struct clk_ops clk_psc_ops = {
  126. .enable = keystone_clk_enable,
  127. .disable = keystone_clk_disable,
  128. .is_enabled = keystone_clk_is_enabled,
  129. };
  130. /**
  131. * clk_register_psc - register psc clock
  132. * @dev: device that is registering this clock
  133. * @name: name of this clock
  134. * @parent_name: name of clock's parent
  135. * @psc_data: platform data to configure this clock
  136. * @lock: spinlock used by this clock
  137. */
  138. static struct clk *clk_register_psc(struct device *dev,
  139. const char *name,
  140. const char *parent_name,
  141. struct clk_psc_data *psc_data,
  142. spinlock_t *lock)
  143. {
  144. struct clk_init_data init;
  145. struct clk_psc *psc;
  146. struct clk *clk;
  147. psc = kzalloc(sizeof(*psc), GFP_KERNEL);
  148. if (!psc)
  149. return ERR_PTR(-ENOMEM);
  150. init.name = name;
  151. init.ops = &clk_psc_ops;
  152. init.flags = 0;
  153. init.parent_names = (parent_name ? &parent_name : NULL);
  154. init.num_parents = (parent_name ? 1 : 0);
  155. psc->psc_data = psc_data;
  156. psc->lock = lock;
  157. psc->hw.init = &init;
  158. clk = clk_register(NULL, &psc->hw);
  159. if (IS_ERR(clk))
  160. kfree(psc);
  161. return clk;
  162. }
  163. /**
  164. * of_psc_clk_init - initialize psc clock through DT
  165. * @node: device tree node for this clock
  166. * @lock: spinlock used by this clock
  167. */
  168. static void __init of_psc_clk_init(struct device_node *node, spinlock_t *lock)
  169. {
  170. const char *clk_name = node->name;
  171. const char *parent_name;
  172. struct clk_psc_data *data;
  173. struct clk *clk;
  174. int i;
  175. data = kzalloc(sizeof(*data), GFP_KERNEL);
  176. if (!data) {
  177. pr_err("%s: Out of memory\n", __func__);
  178. return;
  179. }
  180. i = of_property_match_string(node, "reg-names", "control");
  181. data->control_base = of_iomap(node, i);
  182. if (!data->control_base) {
  183. pr_err("%s: control ioremap failed\n", __func__);
  184. goto out;
  185. }
  186. i = of_property_match_string(node, "reg-names", "domain");
  187. data->domain_base = of_iomap(node, i);
  188. if (!data->domain_base) {
  189. pr_err("%s: domain ioremap failed\n", __func__);
  190. goto unmap_ctrl;
  191. }
  192. of_property_read_u32(node, "domain-id", &data->domain_id);
  193. /* Domain transition registers at fixed address space of domain_id 0 */
  194. if (!domain_transition_base && !data->domain_id)
  195. domain_transition_base = data->domain_base;
  196. of_property_read_string(node, "clock-output-names", &clk_name);
  197. parent_name = of_clk_get_parent_name(node, 0);
  198. if (!parent_name) {
  199. pr_err("%s: Parent clock not found\n", __func__);
  200. goto unmap_domain;
  201. }
  202. clk = clk_register_psc(NULL, clk_name, parent_name, data, lock);
  203. if (!IS_ERR(clk)) {
  204. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  205. return;
  206. }
  207. pr_err("%s: error registering clk %s\n", __func__, node->name);
  208. unmap_domain:
  209. iounmap(data->domain_base);
  210. unmap_ctrl:
  211. iounmap(data->control_base);
  212. out:
  213. kfree(data);
  214. return;
  215. }
  216. /**
  217. * of_keystone_psc_clk_init - initialize psc clock through DT
  218. * @node: device tree node for this clock
  219. */
  220. static void __init of_keystone_psc_clk_init(struct device_node *node)
  221. {
  222. of_psc_clk_init(node, &psc_lock);
  223. }
  224. CLK_OF_DECLARE(keystone_gate_clk, "ti,keystone,psc-clock",
  225. of_keystone_psc_clk_init);