clk-usb.c 9.6 KB

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