clk-peripheral.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/io.h>
  16. #include "pmc.h"
  17. #define PERIPHERAL_MAX 64
  18. #define PERIPHERAL_AT91RM9200 0
  19. #define PERIPHERAL_AT91SAM9X5 1
  20. #define PERIPHERAL_ID_MIN 2
  21. #define PERIPHERAL_ID_MAX 31
  22. #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
  23. #define PERIPHERAL_RSHIFT_MASK 0x3
  24. #define PERIPHERAL_RSHIFT(val) (((val) >> 16) & PERIPHERAL_RSHIFT_MASK)
  25. #define PERIPHERAL_MAX_SHIFT 3
  26. struct clk_peripheral {
  27. struct clk_hw hw;
  28. struct at91_pmc *pmc;
  29. u32 id;
  30. };
  31. #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
  32. struct clk_sam9x5_peripheral {
  33. struct clk_hw hw;
  34. struct at91_pmc *pmc;
  35. struct clk_range range;
  36. u32 id;
  37. u32 div;
  38. bool auto_div;
  39. };
  40. #define to_clk_sam9x5_peripheral(hw) \
  41. container_of(hw, struct clk_sam9x5_peripheral, hw)
  42. static int clk_peripheral_enable(struct clk_hw *hw)
  43. {
  44. struct clk_peripheral *periph = to_clk_peripheral(hw);
  45. struct at91_pmc *pmc = periph->pmc;
  46. int offset = AT91_PMC_PCER;
  47. u32 id = periph->id;
  48. if (id < PERIPHERAL_ID_MIN)
  49. return 0;
  50. if (id > PERIPHERAL_ID_MAX)
  51. offset = AT91_PMC_PCER1;
  52. pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  53. return 0;
  54. }
  55. static void clk_peripheral_disable(struct clk_hw *hw)
  56. {
  57. struct clk_peripheral *periph = to_clk_peripheral(hw);
  58. struct at91_pmc *pmc = periph->pmc;
  59. int offset = AT91_PMC_PCDR;
  60. u32 id = periph->id;
  61. if (id < PERIPHERAL_ID_MIN)
  62. return;
  63. if (id > PERIPHERAL_ID_MAX)
  64. offset = AT91_PMC_PCDR1;
  65. pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  66. }
  67. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  68. {
  69. struct clk_peripheral *periph = to_clk_peripheral(hw);
  70. struct at91_pmc *pmc = periph->pmc;
  71. int offset = AT91_PMC_PCSR;
  72. u32 id = periph->id;
  73. if (id < PERIPHERAL_ID_MIN)
  74. return 1;
  75. if (id > PERIPHERAL_ID_MAX)
  76. offset = AT91_PMC_PCSR1;
  77. return !!(pmc_read(pmc, offset) & PERIPHERAL_MASK(id));
  78. }
  79. static const struct clk_ops peripheral_ops = {
  80. .enable = clk_peripheral_enable,
  81. .disable = clk_peripheral_disable,
  82. .is_enabled = clk_peripheral_is_enabled,
  83. };
  84. static struct clk * __init
  85. at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  86. const char *parent_name, u32 id)
  87. {
  88. struct clk_peripheral *periph;
  89. struct clk *clk = NULL;
  90. struct clk_init_data init;
  91. if (!pmc || !name || !parent_name || id > PERIPHERAL_ID_MAX)
  92. return ERR_PTR(-EINVAL);
  93. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  94. if (!periph)
  95. return ERR_PTR(-ENOMEM);
  96. init.name = name;
  97. init.ops = &peripheral_ops;
  98. init.parent_names = (parent_name ? &parent_name : NULL);
  99. init.num_parents = (parent_name ? 1 : 0);
  100. init.flags = 0;
  101. periph->id = id;
  102. periph->hw.init = &init;
  103. periph->pmc = pmc;
  104. clk = clk_register(NULL, &periph->hw);
  105. if (IS_ERR(clk))
  106. kfree(periph);
  107. return clk;
  108. }
  109. static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  110. {
  111. struct clk_hw *parent;
  112. unsigned long parent_rate;
  113. int shift = 0;
  114. if (!periph->auto_div)
  115. return;
  116. if (periph->range.max) {
  117. parent = clk_hw_get_parent_by_index(&periph->hw, 0);
  118. parent_rate = clk_hw_get_rate(parent);
  119. if (!parent_rate)
  120. return;
  121. for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
  122. if (parent_rate >> shift <= periph->range.max)
  123. break;
  124. }
  125. }
  126. periph->auto_div = false;
  127. periph->div = shift;
  128. }
  129. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  130. {
  131. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  132. struct at91_pmc *pmc = periph->pmc;
  133. u32 tmp;
  134. if (periph->id < PERIPHERAL_ID_MIN)
  135. return 0;
  136. pmc_lock(pmc);
  137. pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  138. tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_DIV_MASK;
  139. pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_DIV(periph->div)
  140. | AT91_PMC_PCR_CMD
  141. | AT91_PMC_PCR_EN);
  142. pmc_unlock(pmc);
  143. return 0;
  144. }
  145. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  146. {
  147. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  148. struct at91_pmc *pmc = periph->pmc;
  149. u32 tmp;
  150. if (periph->id < PERIPHERAL_ID_MIN)
  151. return;
  152. pmc_lock(pmc);
  153. pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  154. tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_EN;
  155. pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  156. pmc_unlock(pmc);
  157. }
  158. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  159. {
  160. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  161. struct at91_pmc *pmc = periph->pmc;
  162. int ret;
  163. if (periph->id < PERIPHERAL_ID_MIN)
  164. return 1;
  165. pmc_lock(pmc);
  166. pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  167. ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_EN);
  168. pmc_unlock(pmc);
  169. return ret;
  170. }
  171. static unsigned long
  172. clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  173. unsigned long parent_rate)
  174. {
  175. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  176. struct at91_pmc *pmc = periph->pmc;
  177. u32 tmp;
  178. if (periph->id < PERIPHERAL_ID_MIN)
  179. return parent_rate;
  180. pmc_lock(pmc);
  181. pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  182. tmp = pmc_read(pmc, AT91_PMC_PCR);
  183. pmc_unlock(pmc);
  184. if (tmp & AT91_PMC_PCR_EN) {
  185. periph->div = PERIPHERAL_RSHIFT(tmp);
  186. periph->auto_div = false;
  187. } else {
  188. clk_sam9x5_peripheral_autodiv(periph);
  189. }
  190. return parent_rate >> periph->div;
  191. }
  192. static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
  193. unsigned long rate,
  194. unsigned long *parent_rate)
  195. {
  196. int shift = 0;
  197. unsigned long best_rate;
  198. unsigned long best_diff;
  199. unsigned long cur_rate = *parent_rate;
  200. unsigned long cur_diff;
  201. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  202. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  203. return *parent_rate;
  204. if (periph->range.max) {
  205. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  206. cur_rate = *parent_rate >> shift;
  207. if (cur_rate <= periph->range.max)
  208. break;
  209. }
  210. }
  211. if (rate >= cur_rate)
  212. return cur_rate;
  213. best_diff = cur_rate - rate;
  214. best_rate = cur_rate;
  215. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  216. cur_rate = *parent_rate >> shift;
  217. if (cur_rate < rate)
  218. cur_diff = rate - cur_rate;
  219. else
  220. cur_diff = cur_rate - rate;
  221. if (cur_diff < best_diff) {
  222. best_diff = cur_diff;
  223. best_rate = cur_rate;
  224. }
  225. if (!best_diff || cur_rate < rate)
  226. break;
  227. }
  228. return best_rate;
  229. }
  230. static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
  231. unsigned long rate,
  232. unsigned long parent_rate)
  233. {
  234. int shift;
  235. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  236. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
  237. if (parent_rate == rate)
  238. return 0;
  239. else
  240. return -EINVAL;
  241. }
  242. if (periph->range.max && rate > periph->range.max)
  243. return -EINVAL;
  244. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  245. if (parent_rate >> shift == rate) {
  246. periph->auto_div = false;
  247. periph->div = shift;
  248. return 0;
  249. }
  250. }
  251. return -EINVAL;
  252. }
  253. static const struct clk_ops sam9x5_peripheral_ops = {
  254. .enable = clk_sam9x5_peripheral_enable,
  255. .disable = clk_sam9x5_peripheral_disable,
  256. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  257. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  258. .round_rate = clk_sam9x5_peripheral_round_rate,
  259. .set_rate = clk_sam9x5_peripheral_set_rate,
  260. };
  261. static struct clk * __init
  262. at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  263. const char *parent_name, u32 id,
  264. const struct clk_range *range)
  265. {
  266. struct clk_sam9x5_peripheral *periph;
  267. struct clk *clk = NULL;
  268. struct clk_init_data init;
  269. if (!pmc || !name || !parent_name)
  270. return ERR_PTR(-EINVAL);
  271. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  272. if (!periph)
  273. return ERR_PTR(-ENOMEM);
  274. init.name = name;
  275. init.ops = &sam9x5_peripheral_ops;
  276. init.parent_names = (parent_name ? &parent_name : NULL);
  277. init.num_parents = (parent_name ? 1 : 0);
  278. init.flags = 0;
  279. periph->id = id;
  280. periph->hw.init = &init;
  281. periph->div = 0;
  282. periph->pmc = pmc;
  283. periph->auto_div = true;
  284. periph->range = *range;
  285. clk = clk_register(NULL, &periph->hw);
  286. if (IS_ERR(clk))
  287. kfree(periph);
  288. else
  289. clk_sam9x5_peripheral_autodiv(periph);
  290. return clk;
  291. }
  292. static void __init
  293. of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  294. {
  295. int num;
  296. u32 id;
  297. struct clk *clk;
  298. const char *parent_name;
  299. const char *name;
  300. struct device_node *periphclknp;
  301. parent_name = of_clk_get_parent_name(np, 0);
  302. if (!parent_name)
  303. return;
  304. num = of_get_child_count(np);
  305. if (!num || num > PERIPHERAL_MAX)
  306. return;
  307. for_each_child_of_node(np, periphclknp) {
  308. if (of_property_read_u32(periphclknp, "reg", &id))
  309. continue;
  310. if (id >= PERIPHERAL_MAX)
  311. continue;
  312. if (of_property_read_string(np, "clock-output-names", &name))
  313. name = periphclknp->name;
  314. if (type == PERIPHERAL_AT91RM9200) {
  315. clk = at91_clk_register_peripheral(pmc, name,
  316. parent_name, id);
  317. } else {
  318. struct clk_range range = CLK_RANGE(0, 0);
  319. of_at91_get_clk_range(periphclknp,
  320. "atmel,clk-output-range",
  321. &range);
  322. clk = at91_clk_register_sam9x5_peripheral(pmc, name,
  323. parent_name,
  324. id, &range);
  325. }
  326. if (IS_ERR(clk))
  327. continue;
  328. of_clk_add_provider(periphclknp, of_clk_src_simple_get, clk);
  329. }
  330. }
  331. void __init of_at91rm9200_clk_periph_setup(struct device_node *np,
  332. struct at91_pmc *pmc)
  333. {
  334. of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91RM9200);
  335. }
  336. void __init of_at91sam9x5_clk_periph_setup(struct device_node *np,
  337. struct at91_pmc *pmc)
  338. {
  339. of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91SAM9X5);
  340. }