clk-usb.c 9.7 KB

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