clk-usb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 USB_SOURCE_MAX 2
  18. #define SAM9X5_USB_DIV_SHIFT 8
  19. #define SAM9X5_USB_MAX_DIV 0xf
  20. #define RM9200_USB_DIV_SHIFT 28
  21. #define RM9200_USB_DIV_TAB_SIZE 4
  22. struct at91sam9x5_clk_usb {
  23. struct clk_hw hw;
  24. struct at91_pmc *pmc;
  25. };
  26. #define to_at91sam9x5_clk_usb(hw) \
  27. container_of(hw, struct at91sam9x5_clk_usb, hw)
  28. struct at91rm9200_clk_usb {
  29. struct clk_hw hw;
  30. struct at91_pmc *pmc;
  31. u32 divisors[4];
  32. };
  33. #define to_at91rm9200_clk_usb(hw) \
  34. container_of(hw, struct at91rm9200_clk_usb, hw)
  35. static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
  36. unsigned long parent_rate)
  37. {
  38. u32 tmp;
  39. u8 usbdiv;
  40. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  41. struct at91_pmc *pmc = usb->pmc;
  42. tmp = pmc_read(pmc, AT91_PMC_USB);
  43. usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  44. return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
  45. }
  46. static long at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
  47. unsigned long rate,
  48. unsigned long *best_parent_rate,
  49. struct clk_hw **best_parent_hw)
  50. {
  51. struct clk *parent = NULL;
  52. long best_rate = -EINVAL;
  53. unsigned long tmp_rate;
  54. int best_diff = -1;
  55. int tmp_diff;
  56. int i;
  57. for (i = 0; i < __clk_get_num_parents(hw->clk); i++) {
  58. int div;
  59. parent = clk_get_parent_by_index(hw->clk, i);
  60. if (!parent)
  61. continue;
  62. for (div = 1; div < SAM9X5_USB_MAX_DIV + 2; div++) {
  63. unsigned long tmp_parent_rate;
  64. tmp_parent_rate = rate * div;
  65. tmp_parent_rate = __clk_round_rate(parent,
  66. tmp_parent_rate);
  67. tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div);
  68. if (tmp_rate < rate)
  69. tmp_diff = rate - tmp_rate;
  70. else
  71. tmp_diff = tmp_rate - rate;
  72. if (best_diff < 0 || best_diff > tmp_diff) {
  73. best_rate = tmp_rate;
  74. best_diff = tmp_diff;
  75. *best_parent_rate = tmp_parent_rate;
  76. *best_parent_hw = __clk_get_hw(parent);
  77. }
  78. if (!best_diff || tmp_rate < rate)
  79. break;
  80. }
  81. if (!best_diff)
  82. break;
  83. }
  84. return best_rate;
  85. }
  86. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  87. {
  88. u32 tmp;
  89. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  90. struct at91_pmc *pmc = usb->pmc;
  91. if (index > 1)
  92. return -EINVAL;
  93. tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
  94. if (index)
  95. tmp |= AT91_PMC_USBS;
  96. pmc_write(pmc, AT91_PMC_USB, tmp);
  97. return 0;
  98. }
  99. static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
  100. {
  101. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  102. struct at91_pmc *pmc = usb->pmc;
  103. return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
  104. }
  105. static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  106. unsigned long parent_rate)
  107. {
  108. u32 tmp;
  109. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  110. struct at91_pmc *pmc = usb->pmc;
  111. unsigned long div;
  112. if (!rate)
  113. return -EINVAL;
  114. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  115. if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
  116. return -EINVAL;
  117. tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
  118. tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
  119. pmc_write(pmc, AT91_PMC_USB, tmp);
  120. return 0;
  121. }
  122. static const struct clk_ops at91sam9x5_usb_ops = {
  123. .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
  124. .determine_rate = at91sam9x5_clk_usb_determine_rate,
  125. .get_parent = at91sam9x5_clk_usb_get_parent,
  126. .set_parent = at91sam9x5_clk_usb_set_parent,
  127. .set_rate = at91sam9x5_clk_usb_set_rate,
  128. };
  129. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  130. {
  131. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  132. struct at91_pmc *pmc = usb->pmc;
  133. pmc_write(pmc, AT91_PMC_USB,
  134. pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
  135. return 0;
  136. }
  137. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  138. {
  139. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  140. struct at91_pmc *pmc = usb->pmc;
  141. pmc_write(pmc, AT91_PMC_USB,
  142. pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
  143. }
  144. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  145. {
  146. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  147. struct at91_pmc *pmc = usb->pmc;
  148. return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
  149. }
  150. static const struct clk_ops at91sam9n12_usb_ops = {
  151. .enable = at91sam9n12_clk_usb_enable,
  152. .disable = at91sam9n12_clk_usb_disable,
  153. .is_enabled = at91sam9n12_clk_usb_is_enabled,
  154. .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
  155. .determine_rate = at91sam9x5_clk_usb_determine_rate,
  156. .set_rate = at91sam9x5_clk_usb_set_rate,
  157. };
  158. static struct clk * __init
  159. at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  160. const char **parent_names, u8 num_parents)
  161. {
  162. struct at91sam9x5_clk_usb *usb;
  163. struct clk *clk = NULL;
  164. struct clk_init_data init;
  165. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  166. if (!usb)
  167. return ERR_PTR(-ENOMEM);
  168. init.name = name;
  169. init.ops = &at91sam9x5_usb_ops;
  170. init.parent_names = parent_names;
  171. init.num_parents = num_parents;
  172. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  173. CLK_SET_RATE_PARENT;
  174. usb->hw.init = &init;
  175. usb->pmc = pmc;
  176. clk = clk_register(NULL, &usb->hw);
  177. if (IS_ERR(clk))
  178. kfree(usb);
  179. return clk;
  180. }
  181. static struct clk * __init
  182. at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  183. const char *parent_name)
  184. {
  185. struct at91sam9x5_clk_usb *usb;
  186. struct clk *clk = NULL;
  187. struct clk_init_data init;
  188. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  189. if (!usb)
  190. return ERR_PTR(-ENOMEM);
  191. init.name = name;
  192. init.ops = &at91sam9n12_usb_ops;
  193. init.parent_names = &parent_name;
  194. init.num_parents = 1;
  195. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  196. usb->hw.init = &init;
  197. usb->pmc = pmc;
  198. clk = clk_register(NULL, &usb->hw);
  199. if (IS_ERR(clk))
  200. kfree(usb);
  201. return clk;
  202. }
  203. static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  204. unsigned long parent_rate)
  205. {
  206. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  207. struct at91_pmc *pmc = usb->pmc;
  208. u32 tmp;
  209. u8 usbdiv;
  210. tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
  211. usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  212. if (usb->divisors[usbdiv])
  213. return parent_rate / usb->divisors[usbdiv];
  214. return 0;
  215. }
  216. static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  217. unsigned long *parent_rate)
  218. {
  219. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  220. struct clk *parent = __clk_get_parent(hw->clk);
  221. unsigned long bestrate = 0;
  222. int bestdiff = -1;
  223. unsigned long tmprate;
  224. int tmpdiff;
  225. int i = 0;
  226. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  227. unsigned long tmp_parent_rate;
  228. if (!usb->divisors[i])
  229. continue;
  230. tmp_parent_rate = rate * usb->divisors[i];
  231. tmp_parent_rate = __clk_round_rate(parent, tmp_parent_rate);
  232. tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
  233. if (tmprate < rate)
  234. tmpdiff = rate - tmprate;
  235. else
  236. tmpdiff = tmprate - rate;
  237. if (bestdiff < 0 || bestdiff > tmpdiff) {
  238. bestrate = tmprate;
  239. bestdiff = tmpdiff;
  240. *parent_rate = tmp_parent_rate;
  241. }
  242. if (!bestdiff)
  243. break;
  244. }
  245. return bestrate;
  246. }
  247. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  248. unsigned long parent_rate)
  249. {
  250. u32 tmp;
  251. int i;
  252. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  253. struct at91_pmc *pmc = usb->pmc;
  254. unsigned long div;
  255. if (!rate)
  256. return -EINVAL;
  257. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  258. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  259. if (usb->divisors[i] == div) {
  260. tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
  261. ~AT91_PMC_USBDIV;
  262. tmp |= i << RM9200_USB_DIV_SHIFT;
  263. pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
  264. return 0;
  265. }
  266. }
  267. return -EINVAL;
  268. }
  269. static const struct clk_ops at91rm9200_usb_ops = {
  270. .recalc_rate = at91rm9200_clk_usb_recalc_rate,
  271. .round_rate = at91rm9200_clk_usb_round_rate,
  272. .set_rate = at91rm9200_clk_usb_set_rate,
  273. };
  274. static struct clk * __init
  275. at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  276. const char *parent_name, const u32 *divisors)
  277. {
  278. struct at91rm9200_clk_usb *usb;
  279. struct clk *clk = NULL;
  280. struct clk_init_data init;
  281. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  282. if (!usb)
  283. return ERR_PTR(-ENOMEM);
  284. init.name = name;
  285. init.ops = &at91rm9200_usb_ops;
  286. init.parent_names = &parent_name;
  287. init.num_parents = 1;
  288. init.flags = CLK_SET_RATE_PARENT;
  289. usb->hw.init = &init;
  290. usb->pmc = pmc;
  291. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  292. clk = clk_register(NULL, &usb->hw);
  293. if (IS_ERR(clk))
  294. kfree(usb);
  295. return clk;
  296. }
  297. void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  298. struct at91_pmc *pmc)
  299. {
  300. struct clk *clk;
  301. int i;
  302. int num_parents;
  303. const char *parent_names[USB_SOURCE_MAX];
  304. const char *name = np->name;
  305. num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  306. if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
  307. return;
  308. for (i = 0; i < num_parents; i++) {
  309. parent_names[i] = of_clk_get_parent_name(np, i);
  310. if (!parent_names[i])
  311. return;
  312. }
  313. of_property_read_string(np, "clock-output-names", &name);
  314. clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
  315. if (IS_ERR(clk))
  316. return;
  317. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  318. }
  319. void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  320. struct at91_pmc *pmc)
  321. {
  322. struct clk *clk;
  323. const char *parent_name;
  324. const char *name = np->name;
  325. parent_name = of_clk_get_parent_name(np, 0);
  326. if (!parent_name)
  327. return;
  328. of_property_read_string(np, "clock-output-names", &name);
  329. clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
  330. if (IS_ERR(clk))
  331. return;
  332. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  333. }
  334. void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  335. struct at91_pmc *pmc)
  336. {
  337. struct clk *clk;
  338. const char *parent_name;
  339. const char *name = np->name;
  340. u32 divisors[4] = {0, 0, 0, 0};
  341. parent_name = of_clk_get_parent_name(np, 0);
  342. if (!parent_name)
  343. return;
  344. of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
  345. if (!divisors[0])
  346. return;
  347. of_property_read_string(np, "clock-output-names", &name);
  348. clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
  349. if (IS_ERR(clk))
  350. return;
  351. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  352. }