clk-usb.c 10 KB

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