clk-oxnas.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2010 Broadcom
  3. * Copyright (C) 2012 Stephen Warren
  4. * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk-provider.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/stringify.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <dt-bindings/clock/oxsemi,ox810se.h>
  28. #include <dt-bindings/clock/oxsemi,ox820.h>
  29. /* Standard regmap gate clocks */
  30. struct clk_oxnas_gate {
  31. struct clk_hw hw;
  32. unsigned int bit;
  33. struct regmap *regmap;
  34. };
  35. struct oxnas_stdclk_data {
  36. struct clk_hw_onecell_data *onecell_data;
  37. struct clk_oxnas_gate **gates;
  38. unsigned int ngates;
  39. struct clk_oxnas_pll **plls;
  40. unsigned int nplls;
  41. };
  42. /* Regmap offsets */
  43. #define CLK_STAT_REGOFFSET 0x24
  44. #define CLK_SET_REGOFFSET 0x2c
  45. #define CLK_CLR_REGOFFSET 0x30
  46. static inline struct clk_oxnas_gate *to_clk_oxnas_gate(struct clk_hw *hw)
  47. {
  48. return container_of(hw, struct clk_oxnas_gate, hw);
  49. }
  50. static int oxnas_clk_gate_is_enabled(struct clk_hw *hw)
  51. {
  52. struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw);
  53. int ret;
  54. unsigned int val;
  55. ret = regmap_read(std->regmap, CLK_STAT_REGOFFSET, &val);
  56. if (ret < 0)
  57. return ret;
  58. return val & BIT(std->bit);
  59. }
  60. static int oxnas_clk_gate_enable(struct clk_hw *hw)
  61. {
  62. struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw);
  63. regmap_write(std->regmap, CLK_SET_REGOFFSET, BIT(std->bit));
  64. return 0;
  65. }
  66. static void oxnas_clk_gate_disable(struct clk_hw *hw)
  67. {
  68. struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw);
  69. regmap_write(std->regmap, CLK_CLR_REGOFFSET, BIT(std->bit));
  70. }
  71. static const struct clk_ops oxnas_clk_gate_ops = {
  72. .enable = oxnas_clk_gate_enable,
  73. .disable = oxnas_clk_gate_disable,
  74. .is_enabled = oxnas_clk_gate_is_enabled,
  75. };
  76. static const char *const osc_parents[] = {
  77. "oscillator",
  78. };
  79. static const char *const eth_parents[] = {
  80. "gmacclk",
  81. };
  82. #define OXNAS_GATE(_name, _bit, _parents) \
  83. struct clk_oxnas_gate _name = { \
  84. .bit = (_bit), \
  85. .hw.init = &(struct clk_init_data) { \
  86. .name = #_name, \
  87. .ops = &oxnas_clk_gate_ops, \
  88. .parent_names = _parents, \
  89. .num_parents = ARRAY_SIZE(_parents), \
  90. .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED), \
  91. }, \
  92. }
  93. static OXNAS_GATE(ox810se_leon, 0, osc_parents);
  94. static OXNAS_GATE(ox810se_dma_sgdma, 1, osc_parents);
  95. static OXNAS_GATE(ox810se_cipher, 2, osc_parents);
  96. static OXNAS_GATE(ox810se_sata, 4, osc_parents);
  97. static OXNAS_GATE(ox810se_audio, 5, osc_parents);
  98. static OXNAS_GATE(ox810se_usbmph, 6, osc_parents);
  99. static OXNAS_GATE(ox810se_etha, 7, eth_parents);
  100. static OXNAS_GATE(ox810se_pciea, 8, osc_parents);
  101. static OXNAS_GATE(ox810se_nand, 9, osc_parents);
  102. static struct clk_oxnas_gate *ox810se_gates[] = {
  103. &ox810se_leon,
  104. &ox810se_dma_sgdma,
  105. &ox810se_cipher,
  106. &ox810se_sata,
  107. &ox810se_audio,
  108. &ox810se_usbmph,
  109. &ox810se_etha,
  110. &ox810se_pciea,
  111. &ox810se_nand,
  112. };
  113. static OXNAS_GATE(ox820_leon, 0, osc_parents);
  114. static OXNAS_GATE(ox820_dma_sgdma, 1, osc_parents);
  115. static OXNAS_GATE(ox820_cipher, 2, osc_parents);
  116. static OXNAS_GATE(ox820_sd, 3, osc_parents);
  117. static OXNAS_GATE(ox820_sata, 4, osc_parents);
  118. static OXNAS_GATE(ox820_audio, 5, osc_parents);
  119. static OXNAS_GATE(ox820_usbmph, 6, osc_parents);
  120. static OXNAS_GATE(ox820_etha, 7, eth_parents);
  121. static OXNAS_GATE(ox820_pciea, 8, osc_parents);
  122. static OXNAS_GATE(ox820_nand, 9, osc_parents);
  123. static OXNAS_GATE(ox820_ethb, 10, eth_parents);
  124. static OXNAS_GATE(ox820_pcieb, 11, osc_parents);
  125. static OXNAS_GATE(ox820_ref600, 12, osc_parents);
  126. static OXNAS_GATE(ox820_usbdev, 13, osc_parents);
  127. static struct clk_oxnas_gate *ox820_gates[] = {
  128. &ox820_leon,
  129. &ox820_dma_sgdma,
  130. &ox820_cipher,
  131. &ox820_sd,
  132. &ox820_sata,
  133. &ox820_audio,
  134. &ox820_usbmph,
  135. &ox820_etha,
  136. &ox820_pciea,
  137. &ox820_nand,
  138. &ox820_etha,
  139. &ox820_pciea,
  140. &ox820_ref600,
  141. &ox820_usbdev,
  142. };
  143. static struct clk_hw_onecell_data ox810se_hw_onecell_data = {
  144. .hws = {
  145. [CLK_810_LEON] = &ox810se_leon.hw,
  146. [CLK_810_DMA_SGDMA] = &ox810se_dma_sgdma.hw,
  147. [CLK_810_CIPHER] = &ox810se_cipher.hw,
  148. [CLK_810_SATA] = &ox810se_sata.hw,
  149. [CLK_810_AUDIO] = &ox810se_audio.hw,
  150. [CLK_810_USBMPH] = &ox810se_usbmph.hw,
  151. [CLK_810_ETHA] = &ox810se_etha.hw,
  152. [CLK_810_PCIEA] = &ox810se_pciea.hw,
  153. [CLK_810_NAND] = &ox810se_nand.hw,
  154. },
  155. .num = ARRAY_SIZE(ox810se_gates),
  156. };
  157. static struct clk_hw_onecell_data ox820_hw_onecell_data = {
  158. .hws = {
  159. [CLK_820_LEON] = &ox820_leon.hw,
  160. [CLK_820_DMA_SGDMA] = &ox820_dma_sgdma.hw,
  161. [CLK_820_CIPHER] = &ox820_cipher.hw,
  162. [CLK_820_SD] = &ox820_sd.hw,
  163. [CLK_820_SATA] = &ox820_sata.hw,
  164. [CLK_820_AUDIO] = &ox820_audio.hw,
  165. [CLK_820_USBMPH] = &ox820_usbmph.hw,
  166. [CLK_820_ETHA] = &ox820_etha.hw,
  167. [CLK_820_PCIEA] = &ox820_pciea.hw,
  168. [CLK_820_NAND] = &ox820_nand.hw,
  169. [CLK_820_ETHB] = &ox820_ethb.hw,
  170. [CLK_820_PCIEB] = &ox820_pcieb.hw,
  171. [CLK_820_REF600] = &ox820_ref600.hw,
  172. [CLK_820_USBDEV] = &ox820_usbdev.hw,
  173. },
  174. .num = ARRAY_SIZE(ox820_gates),
  175. };
  176. static struct oxnas_stdclk_data ox810se_stdclk_data = {
  177. .onecell_data = &ox810se_hw_onecell_data,
  178. .gates = ox810se_gates,
  179. .ngates = ARRAY_SIZE(ox810se_gates),
  180. };
  181. static struct oxnas_stdclk_data ox820_stdclk_data = {
  182. .onecell_data = &ox820_hw_onecell_data,
  183. .gates = ox820_gates,
  184. .ngates = ARRAY_SIZE(ox820_gates),
  185. };
  186. static const struct of_device_id oxnas_stdclk_dt_ids[] = {
  187. { .compatible = "oxsemi,ox810se-stdclk", &ox810se_stdclk_data },
  188. { .compatible = "oxsemi,ox820-stdclk", &ox820_stdclk_data },
  189. { }
  190. };
  191. static int oxnas_stdclk_probe(struct platform_device *pdev)
  192. {
  193. struct device_node *np = pdev->dev.of_node;
  194. const struct oxnas_stdclk_data *data;
  195. const struct of_device_id *id;
  196. struct regmap *regmap;
  197. int ret;
  198. int i;
  199. id = of_match_device(oxnas_stdclk_dt_ids, &pdev->dev);
  200. if (!id)
  201. return -ENODEV;
  202. data = id->data;
  203. regmap = syscon_node_to_regmap(of_get_parent(np));
  204. if (IS_ERR(regmap)) {
  205. dev_err(&pdev->dev, "failed to have parent regmap\n");
  206. return PTR_ERR(regmap);
  207. }
  208. for (i = 0 ; i < data->ngates ; ++i)
  209. data->gates[i]->regmap = regmap;
  210. for (i = 0; i < data->onecell_data->num; i++) {
  211. if (!data->onecell_data->hws[i])
  212. continue;
  213. ret = devm_clk_hw_register(&pdev->dev,
  214. data->onecell_data->hws[i]);
  215. if (ret)
  216. return ret;
  217. }
  218. return of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
  219. data->onecell_data);
  220. }
  221. static struct platform_driver oxnas_stdclk_driver = {
  222. .probe = oxnas_stdclk_probe,
  223. .driver = {
  224. .name = "oxnas-stdclk",
  225. .suppress_bind_attrs = true,
  226. .of_match_table = oxnas_stdclk_dt_ids,
  227. },
  228. };
  229. builtin_platform_driver(oxnas_stdclk_driver);