clk-apple-nco.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /*
  3. * Driver for an SoC block (Numerically Controlled Oscillator)
  4. * found on t8103 (M1) and other Apple chips
  5. *
  6. * Copyright (C) The Asahi Linux Contributors
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/math64.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/spinlock.h>
  18. #define NCO_CHANNEL_STRIDE 0x4000
  19. #define NCO_CHANNEL_REGSIZE 20
  20. #define REG_CTRL 0
  21. #define CTRL_ENABLE BIT(31)
  22. #define REG_DIV 4
  23. #define DIV_FINE GENMASK(1, 0)
  24. #define DIV_COARSE GENMASK(12, 2)
  25. #define REG_INC1 8
  26. #define REG_INC2 12
  27. #define REG_ACCINIT 16
  28. /*
  29. * Theory of operation (postulated)
  30. *
  31. * The REG_DIV register indirectly expresses a base integer divisor, roughly
  32. * corresponding to twice the desired ratio of input to output clock. This
  33. * base divisor is adjusted on a cycle-by-cycle basis based on the state of a
  34. * 32-bit phase accumulator to achieve a desired precise clock ratio over the
  35. * long term.
  36. *
  37. * Specifically an output clock cycle is produced after (REG_DIV divisor)/2
  38. * or (REG_DIV divisor + 1)/2 input cycles, the latter taking effect when top
  39. * bit of the 32-bit accumulator is set. The accumulator is incremented each
  40. * produced output cycle, by the value from either REG_INC1 or REG_INC2, which
  41. * of the two is selected depending again on the accumulator's current top bit.
  42. *
  43. * Because the NCO hardware implements counting of input clock cycles in part
  44. * in a Galois linear-feedback shift register, the higher bits of divisor
  45. * are programmed into REG_DIV by picking an appropriate LFSR state. See
  46. * applnco_compute_tables/applnco_div_translate for details on this.
  47. */
  48. #define LFSR_POLY 0xa01
  49. #define LFSR_INIT 0x7ff
  50. #define LFSR_LEN 11
  51. #define LFSR_PERIOD ((1 << LFSR_LEN) - 1)
  52. #define LFSR_TBLSIZE (1 << LFSR_LEN)
  53. /* The minimal attainable coarse divisor (first value in table) */
  54. #define COARSE_DIV_OFFSET 2
  55. struct applnco_tables {
  56. u16 fwd[LFSR_TBLSIZE];
  57. u16 inv[LFSR_TBLSIZE];
  58. };
  59. struct applnco_channel {
  60. void __iomem *base;
  61. struct applnco_tables *tbl;
  62. struct clk_hw hw;
  63. spinlock_t lock;
  64. };
  65. #define to_applnco_channel(_hw) container_of(_hw, struct applnco_channel, hw)
  66. static void applnco_enable_nolock(struct clk_hw *hw)
  67. {
  68. struct applnco_channel *chan = to_applnco_channel(hw);
  69. u32 val;
  70. val = readl_relaxed(chan->base + REG_CTRL);
  71. writel_relaxed(val | CTRL_ENABLE, chan->base + REG_CTRL);
  72. }
  73. static void applnco_disable_nolock(struct clk_hw *hw)
  74. {
  75. struct applnco_channel *chan = to_applnco_channel(hw);
  76. u32 val;
  77. val = readl_relaxed(chan->base + REG_CTRL);
  78. writel_relaxed(val & ~CTRL_ENABLE, chan->base + REG_CTRL);
  79. }
  80. static int applnco_is_enabled(struct clk_hw *hw)
  81. {
  82. struct applnco_channel *chan = to_applnco_channel(hw);
  83. return (readl_relaxed(chan->base + REG_CTRL) & CTRL_ENABLE) != 0;
  84. }
  85. static void applnco_compute_tables(struct applnco_tables *tbl)
  86. {
  87. int i;
  88. u32 state = LFSR_INIT;
  89. /*
  90. * Go through the states of a Galois LFSR and build
  91. * a coarse divisor translation table.
  92. */
  93. for (i = LFSR_PERIOD; i > 0; i--) {
  94. if (state & 1)
  95. state = (state >> 1) ^ (LFSR_POLY >> 1);
  96. else
  97. state = (state >> 1);
  98. tbl->fwd[i] = state;
  99. tbl->inv[state] = i;
  100. }
  101. /* Zero value is special-cased */
  102. tbl->fwd[0] = 0;
  103. tbl->inv[0] = 0;
  104. }
  105. static bool applnco_div_out_of_range(unsigned int div)
  106. {
  107. unsigned int coarse = div / 4;
  108. return coarse < COARSE_DIV_OFFSET ||
  109. coarse >= COARSE_DIV_OFFSET + LFSR_TBLSIZE;
  110. }
  111. static u32 applnco_div_translate(struct applnco_tables *tbl, unsigned int div)
  112. {
  113. unsigned int coarse = div / 4;
  114. if (WARN_ON(applnco_div_out_of_range(div)))
  115. return 0;
  116. return FIELD_PREP(DIV_COARSE, tbl->fwd[coarse - COARSE_DIV_OFFSET]) |
  117. FIELD_PREP(DIV_FINE, div % 4);
  118. }
  119. static unsigned int applnco_div_translate_inv(struct applnco_tables *tbl, u32 regval)
  120. {
  121. unsigned int coarse, fine;
  122. coarse = tbl->inv[FIELD_GET(DIV_COARSE, regval)] + COARSE_DIV_OFFSET;
  123. fine = FIELD_GET(DIV_FINE, regval);
  124. return coarse * 4 + fine;
  125. }
  126. static int applnco_set_rate(struct clk_hw *hw, unsigned long rate,
  127. unsigned long parent_rate)
  128. {
  129. struct applnco_channel *chan = to_applnco_channel(hw);
  130. unsigned long flags;
  131. u32 div, inc1, inc2;
  132. bool was_enabled;
  133. div = 2 * parent_rate / rate;
  134. inc1 = 2 * parent_rate - div * rate;
  135. inc2 = inc1 - rate;
  136. if (applnco_div_out_of_range(div))
  137. return -EINVAL;
  138. div = applnco_div_translate(chan->tbl, div);
  139. spin_lock_irqsave(&chan->lock, flags);
  140. was_enabled = applnco_is_enabled(hw);
  141. applnco_disable_nolock(hw);
  142. writel_relaxed(div, chan->base + REG_DIV);
  143. writel_relaxed(inc1, chan->base + REG_INC1);
  144. writel_relaxed(inc2, chan->base + REG_INC2);
  145. /* Presumably a neutral initial value for accumulator */
  146. writel_relaxed(1 << 31, chan->base + REG_ACCINIT);
  147. if (was_enabled)
  148. applnco_enable_nolock(hw);
  149. spin_unlock_irqrestore(&chan->lock, flags);
  150. return 0;
  151. }
  152. static unsigned long applnco_recalc_rate(struct clk_hw *hw,
  153. unsigned long parent_rate)
  154. {
  155. struct applnco_channel *chan = to_applnco_channel(hw);
  156. u32 div, inc1, inc2, incbase;
  157. div = applnco_div_translate_inv(chan->tbl,
  158. readl_relaxed(chan->base + REG_DIV));
  159. inc1 = readl_relaxed(chan->base + REG_INC1);
  160. inc2 = readl_relaxed(chan->base + REG_INC2);
  161. /*
  162. * We don't support wraparound of accumulator
  163. * nor the edge case of both increments being zero
  164. */
  165. if (inc1 >= (1 << 31) || inc2 < (1 << 31) || (inc1 == 0 && inc2 == 0))
  166. return 0;
  167. /* Scale both sides of division by incbase to maintain precision */
  168. incbase = inc1 - inc2;
  169. return div64_u64(((u64) parent_rate) * 2 * incbase,
  170. ((u64) div) * incbase + inc1);
  171. }
  172. static long applnco_round_rate(struct clk_hw *hw, unsigned long rate,
  173. unsigned long *parent_rate)
  174. {
  175. unsigned long lo = *parent_rate / (COARSE_DIV_OFFSET + LFSR_TBLSIZE) + 1;
  176. unsigned long hi = *parent_rate / COARSE_DIV_OFFSET;
  177. return clamp(rate, lo, hi);
  178. }
  179. static int applnco_enable(struct clk_hw *hw)
  180. {
  181. struct applnco_channel *chan = to_applnco_channel(hw);
  182. unsigned long flags;
  183. spin_lock_irqsave(&chan->lock, flags);
  184. applnco_enable_nolock(hw);
  185. spin_unlock_irqrestore(&chan->lock, flags);
  186. return 0;
  187. }
  188. static void applnco_disable(struct clk_hw *hw)
  189. {
  190. struct applnco_channel *chan = to_applnco_channel(hw);
  191. unsigned long flags;
  192. spin_lock_irqsave(&chan->lock, flags);
  193. applnco_disable_nolock(hw);
  194. spin_unlock_irqrestore(&chan->lock, flags);
  195. }
  196. static const struct clk_ops applnco_ops = {
  197. .set_rate = applnco_set_rate,
  198. .recalc_rate = applnco_recalc_rate,
  199. .round_rate = applnco_round_rate,
  200. .enable = applnco_enable,
  201. .disable = applnco_disable,
  202. .is_enabled = applnco_is_enabled,
  203. };
  204. static int applnco_probe(struct platform_device *pdev)
  205. {
  206. struct device_node *np = pdev->dev.of_node;
  207. struct clk_parent_data pdata = { .index = 0 };
  208. struct clk_init_data init;
  209. struct clk_hw_onecell_data *onecell_data;
  210. void __iomem *base;
  211. struct resource *res;
  212. struct applnco_tables *tbl;
  213. unsigned int nchannels;
  214. int ret, i;
  215. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  216. if (IS_ERR(base))
  217. return PTR_ERR(base);
  218. if (resource_size(res) < NCO_CHANNEL_REGSIZE)
  219. return -EINVAL;
  220. nchannels = (resource_size(res) - NCO_CHANNEL_REGSIZE)
  221. / NCO_CHANNEL_STRIDE + 1;
  222. onecell_data = devm_kzalloc(&pdev->dev, struct_size(onecell_data, hws,
  223. nchannels), GFP_KERNEL);
  224. if (!onecell_data)
  225. return -ENOMEM;
  226. onecell_data->num = nchannels;
  227. tbl = devm_kzalloc(&pdev->dev, sizeof(*tbl), GFP_KERNEL);
  228. if (!tbl)
  229. return -ENOMEM;
  230. applnco_compute_tables(tbl);
  231. for (i = 0; i < nchannels; i++) {
  232. struct applnco_channel *chan;
  233. chan = devm_kzalloc(&pdev->dev, sizeof(*chan), GFP_KERNEL);
  234. if (!chan)
  235. return -ENOMEM;
  236. chan->base = base + NCO_CHANNEL_STRIDE * i;
  237. chan->tbl = tbl;
  238. spin_lock_init(&chan->lock);
  239. memset(&init, 0, sizeof(init));
  240. init.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  241. "%s-%d", np->name, i);
  242. init.ops = &applnco_ops;
  243. init.parent_data = &pdata;
  244. init.num_parents = 1;
  245. init.flags = 0;
  246. chan->hw.init = &init;
  247. ret = devm_clk_hw_register(&pdev->dev, &chan->hw);
  248. if (ret)
  249. return ret;
  250. onecell_data->hws[i] = &chan->hw;
  251. }
  252. return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_onecell_get,
  253. onecell_data);
  254. }
  255. static const struct of_device_id applnco_ids[] = {
  256. { .compatible = "apple,nco" },
  257. { }
  258. };
  259. MODULE_DEVICE_TABLE(of, applnco_ids);
  260. static struct platform_driver applnco_driver = {
  261. .driver = {
  262. .name = "apple-nco",
  263. .of_match_table = applnco_ids,
  264. },
  265. .probe = applnco_probe,
  266. };
  267. module_platform_driver(applnco_driver);
  268. MODULE_AUTHOR("Martin Povišer <[email protected]>");
  269. MODULE_DESCRIPTION("Clock driver for NCO blocks on Apple SoCs");
  270. MODULE_LICENSE("GPL");