clk-usb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. unsigned long bestrate = 0;
  194. int bestdiff = -1;
  195. unsigned long tmprate;
  196. int tmpdiff;
  197. int i = 0;
  198. for (i = 0; i < 4; i++) {
  199. if (!usb->divisors[i])
  200. continue;
  201. tmprate = *parent_rate / usb->divisors[i];
  202. if (tmprate < rate)
  203. tmpdiff = rate - tmprate;
  204. else
  205. tmpdiff = tmprate - rate;
  206. if (bestdiff < 0 || bestdiff > tmpdiff) {
  207. bestrate = tmprate;
  208. bestdiff = tmpdiff;
  209. }
  210. if (!bestdiff)
  211. break;
  212. }
  213. return bestrate;
  214. }
  215. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  216. unsigned long parent_rate)
  217. {
  218. u32 tmp;
  219. int i;
  220. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  221. struct at91_pmc *pmc = usb->pmc;
  222. unsigned long div = parent_rate / rate;
  223. if (parent_rate % rate)
  224. return -EINVAL;
  225. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  226. if (usb->divisors[i] == div) {
  227. tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
  228. ~AT91_PMC_USBDIV;
  229. tmp |= i << RM9200_USB_DIV_SHIFT;
  230. pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
  231. return 0;
  232. }
  233. }
  234. return -EINVAL;
  235. }
  236. static const struct clk_ops at91rm9200_usb_ops = {
  237. .recalc_rate = at91rm9200_clk_usb_recalc_rate,
  238. .round_rate = at91rm9200_clk_usb_round_rate,
  239. .set_rate = at91rm9200_clk_usb_set_rate,
  240. };
  241. static struct clk * __init
  242. at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  243. const char *parent_name, const u32 *divisors)
  244. {
  245. struct at91rm9200_clk_usb *usb;
  246. struct clk *clk = NULL;
  247. struct clk_init_data init;
  248. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  249. if (!usb)
  250. return ERR_PTR(-ENOMEM);
  251. init.name = name;
  252. init.ops = &at91rm9200_usb_ops;
  253. init.parent_names = &parent_name;
  254. init.num_parents = 1;
  255. init.flags = 0;
  256. usb->hw.init = &init;
  257. usb->pmc = pmc;
  258. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  259. clk = clk_register(NULL, &usb->hw);
  260. if (IS_ERR(clk))
  261. kfree(usb);
  262. return clk;
  263. }
  264. void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  265. struct at91_pmc *pmc)
  266. {
  267. struct clk *clk;
  268. int i;
  269. int num_parents;
  270. const char *parent_names[USB_SOURCE_MAX];
  271. const char *name = np->name;
  272. num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  273. if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
  274. return;
  275. for (i = 0; i < num_parents; i++) {
  276. parent_names[i] = of_clk_get_parent_name(np, i);
  277. if (!parent_names[i])
  278. return;
  279. }
  280. of_property_read_string(np, "clock-output-names", &name);
  281. clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
  282. if (IS_ERR(clk))
  283. return;
  284. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  285. }
  286. void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  287. struct at91_pmc *pmc)
  288. {
  289. struct clk *clk;
  290. const char *parent_name;
  291. const char *name = np->name;
  292. parent_name = of_clk_get_parent_name(np, 0);
  293. if (!parent_name)
  294. return;
  295. of_property_read_string(np, "clock-output-names", &name);
  296. clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
  297. if (IS_ERR(clk))
  298. return;
  299. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  300. }
  301. void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  302. struct at91_pmc *pmc)
  303. {
  304. struct clk *clk;
  305. const char *parent_name;
  306. const char *name = np->name;
  307. u32 divisors[4] = {0, 0, 0, 0};
  308. parent_name = of_clk_get_parent_name(np, 0);
  309. if (!parent_name)
  310. return;
  311. of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
  312. if (!divisors[0])
  313. return;
  314. of_property_read_string(np, "clock-output-names", &name);
  315. clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
  316. if (IS_ERR(clk))
  317. return;
  318. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  319. }