clk-slow.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * drivers/clk/at91/clk-slow.c
  3. *
  4. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/slab.h>
  16. #include <linux/clk/at91_pmc.h>
  17. #include <linux/delay.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/io.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/sched.h>
  25. #include <linux/wait.h>
  26. #include "pmc.h"
  27. #include "sckc.h"
  28. #define SLOW_CLOCK_FREQ 32768
  29. #define SLOWCK_SW_CYCLES 5
  30. #define SLOWCK_SW_TIME_USEC ((SLOWCK_SW_CYCLES * USEC_PER_SEC) / \
  31. SLOW_CLOCK_FREQ)
  32. #define AT91_SCKC_CR 0x00
  33. #define AT91_SCKC_RCEN (1 << 0)
  34. #define AT91_SCKC_OSC32EN (1 << 1)
  35. #define AT91_SCKC_OSC32BYP (1 << 2)
  36. #define AT91_SCKC_OSCSEL (1 << 3)
  37. struct clk_slow_osc {
  38. struct clk_hw hw;
  39. void __iomem *sckcr;
  40. unsigned long startup_usec;
  41. };
  42. #define to_clk_slow_osc(hw) container_of(hw, struct clk_slow_osc, hw)
  43. struct clk_slow_rc_osc {
  44. struct clk_hw hw;
  45. void __iomem *sckcr;
  46. unsigned long frequency;
  47. unsigned long accuracy;
  48. unsigned long startup_usec;
  49. };
  50. #define to_clk_slow_rc_osc(hw) container_of(hw, struct clk_slow_rc_osc, hw)
  51. struct clk_sam9260_slow {
  52. struct clk_hw hw;
  53. struct at91_pmc *pmc;
  54. };
  55. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  56. struct clk_sam9x5_slow {
  57. struct clk_hw hw;
  58. void __iomem *sckcr;
  59. u8 parent;
  60. };
  61. #define to_clk_sam9x5_slow(hw) container_of(hw, struct clk_sam9x5_slow, hw)
  62. static struct clk *slow_clk;
  63. static int clk_slow_osc_prepare(struct clk_hw *hw)
  64. {
  65. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  66. void __iomem *sckcr = osc->sckcr;
  67. u32 tmp = readl(sckcr);
  68. if (tmp & AT91_SCKC_OSC32BYP)
  69. return 0;
  70. writel(tmp | AT91_SCKC_OSC32EN, sckcr);
  71. usleep_range(osc->startup_usec, osc->startup_usec + 1);
  72. return 0;
  73. }
  74. static void clk_slow_osc_unprepare(struct clk_hw *hw)
  75. {
  76. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  77. void __iomem *sckcr = osc->sckcr;
  78. u32 tmp = readl(sckcr);
  79. if (tmp & AT91_SCKC_OSC32BYP)
  80. return;
  81. writel(tmp & ~AT91_SCKC_OSC32EN, sckcr);
  82. }
  83. static int clk_slow_osc_is_prepared(struct clk_hw *hw)
  84. {
  85. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  86. void __iomem *sckcr = osc->sckcr;
  87. u32 tmp = readl(sckcr);
  88. if (tmp & AT91_SCKC_OSC32BYP)
  89. return 1;
  90. return !!(tmp & AT91_SCKC_OSC32EN);
  91. }
  92. static const struct clk_ops slow_osc_ops = {
  93. .prepare = clk_slow_osc_prepare,
  94. .unprepare = clk_slow_osc_unprepare,
  95. .is_prepared = clk_slow_osc_is_prepared,
  96. };
  97. static struct clk * __init
  98. at91_clk_register_slow_osc(void __iomem *sckcr,
  99. const char *name,
  100. const char *parent_name,
  101. unsigned long startup,
  102. bool bypass)
  103. {
  104. struct clk_slow_osc *osc;
  105. struct clk *clk = NULL;
  106. struct clk_init_data init;
  107. if (!sckcr || !name || !parent_name)
  108. return ERR_PTR(-EINVAL);
  109. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  110. if (!osc)
  111. return ERR_PTR(-ENOMEM);
  112. init.name = name;
  113. init.ops = &slow_osc_ops;
  114. init.parent_names = &parent_name;
  115. init.num_parents = 1;
  116. init.flags = CLK_IGNORE_UNUSED;
  117. osc->hw.init = &init;
  118. osc->sckcr = sckcr;
  119. osc->startup_usec = startup;
  120. if (bypass)
  121. writel((readl(sckcr) & ~AT91_SCKC_OSC32EN) | AT91_SCKC_OSC32BYP,
  122. sckcr);
  123. clk = clk_register(NULL, &osc->hw);
  124. if (IS_ERR(clk))
  125. kfree(osc);
  126. return clk;
  127. }
  128. void __init of_at91sam9x5_clk_slow_osc_setup(struct device_node *np,
  129. void __iomem *sckcr)
  130. {
  131. struct clk *clk;
  132. const char *parent_name;
  133. const char *name = np->name;
  134. u32 startup;
  135. bool bypass;
  136. parent_name = of_clk_get_parent_name(np, 0);
  137. of_property_read_string(np, "clock-output-names", &name);
  138. of_property_read_u32(np, "atmel,startup-time-usec", &startup);
  139. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  140. clk = at91_clk_register_slow_osc(sckcr, name, parent_name, startup,
  141. bypass);
  142. if (IS_ERR(clk))
  143. return;
  144. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  145. }
  146. static unsigned long clk_slow_rc_osc_recalc_rate(struct clk_hw *hw,
  147. unsigned long parent_rate)
  148. {
  149. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  150. return osc->frequency;
  151. }
  152. static unsigned long clk_slow_rc_osc_recalc_accuracy(struct clk_hw *hw,
  153. unsigned long parent_acc)
  154. {
  155. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  156. return osc->accuracy;
  157. }
  158. static int clk_slow_rc_osc_prepare(struct clk_hw *hw)
  159. {
  160. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  161. void __iomem *sckcr = osc->sckcr;
  162. writel(readl(sckcr) | AT91_SCKC_RCEN, sckcr);
  163. usleep_range(osc->startup_usec, osc->startup_usec + 1);
  164. return 0;
  165. }
  166. static void clk_slow_rc_osc_unprepare(struct clk_hw *hw)
  167. {
  168. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  169. void __iomem *sckcr = osc->sckcr;
  170. writel(readl(sckcr) & ~AT91_SCKC_RCEN, sckcr);
  171. }
  172. static int clk_slow_rc_osc_is_prepared(struct clk_hw *hw)
  173. {
  174. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  175. return !!(readl(osc->sckcr) & AT91_SCKC_RCEN);
  176. }
  177. static const struct clk_ops slow_rc_osc_ops = {
  178. .prepare = clk_slow_rc_osc_prepare,
  179. .unprepare = clk_slow_rc_osc_unprepare,
  180. .is_prepared = clk_slow_rc_osc_is_prepared,
  181. .recalc_rate = clk_slow_rc_osc_recalc_rate,
  182. .recalc_accuracy = clk_slow_rc_osc_recalc_accuracy,
  183. };
  184. static struct clk * __init
  185. at91_clk_register_slow_rc_osc(void __iomem *sckcr,
  186. const char *name,
  187. unsigned long frequency,
  188. unsigned long accuracy,
  189. unsigned long startup)
  190. {
  191. struct clk_slow_rc_osc *osc;
  192. struct clk *clk = NULL;
  193. struct clk_init_data init;
  194. if (!sckcr || !name)
  195. return ERR_PTR(-EINVAL);
  196. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  197. if (!osc)
  198. return ERR_PTR(-ENOMEM);
  199. init.name = name;
  200. init.ops = &slow_rc_osc_ops;
  201. init.parent_names = NULL;
  202. init.num_parents = 0;
  203. init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
  204. osc->hw.init = &init;
  205. osc->sckcr = sckcr;
  206. osc->frequency = frequency;
  207. osc->accuracy = accuracy;
  208. osc->startup_usec = startup;
  209. clk = clk_register(NULL, &osc->hw);
  210. if (IS_ERR(clk))
  211. kfree(osc);
  212. return clk;
  213. }
  214. void __init of_at91sam9x5_clk_slow_rc_osc_setup(struct device_node *np,
  215. void __iomem *sckcr)
  216. {
  217. struct clk *clk;
  218. u32 frequency = 0;
  219. u32 accuracy = 0;
  220. u32 startup = 0;
  221. const char *name = np->name;
  222. of_property_read_string(np, "clock-output-names", &name);
  223. of_property_read_u32(np, "clock-frequency", &frequency);
  224. of_property_read_u32(np, "clock-accuracy", &accuracy);
  225. of_property_read_u32(np, "atmel,startup-time-usec", &startup);
  226. clk = at91_clk_register_slow_rc_osc(sckcr, name, frequency, accuracy,
  227. startup);
  228. if (IS_ERR(clk))
  229. return;
  230. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  231. }
  232. static int clk_sam9x5_slow_set_parent(struct clk_hw *hw, u8 index)
  233. {
  234. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  235. void __iomem *sckcr = slowck->sckcr;
  236. u32 tmp;
  237. if (index > 1)
  238. return -EINVAL;
  239. tmp = readl(sckcr);
  240. if ((!index && !(tmp & AT91_SCKC_OSCSEL)) ||
  241. (index && (tmp & AT91_SCKC_OSCSEL)))
  242. return 0;
  243. if (index)
  244. tmp |= AT91_SCKC_OSCSEL;
  245. else
  246. tmp &= ~AT91_SCKC_OSCSEL;
  247. writel(tmp, sckcr);
  248. usleep_range(SLOWCK_SW_TIME_USEC, SLOWCK_SW_TIME_USEC + 1);
  249. return 0;
  250. }
  251. static u8 clk_sam9x5_slow_get_parent(struct clk_hw *hw)
  252. {
  253. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  254. return !!(readl(slowck->sckcr) & AT91_SCKC_OSCSEL);
  255. }
  256. static const struct clk_ops sam9x5_slow_ops = {
  257. .set_parent = clk_sam9x5_slow_set_parent,
  258. .get_parent = clk_sam9x5_slow_get_parent,
  259. };
  260. static struct clk * __init
  261. at91_clk_register_sam9x5_slow(void __iomem *sckcr,
  262. const char *name,
  263. const char **parent_names,
  264. int num_parents)
  265. {
  266. struct clk_sam9x5_slow *slowck;
  267. struct clk *clk = NULL;
  268. struct clk_init_data init;
  269. if (!sckcr || !name || !parent_names || !num_parents)
  270. return ERR_PTR(-EINVAL);
  271. slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
  272. if (!slowck)
  273. return ERR_PTR(-ENOMEM);
  274. init.name = name;
  275. init.ops = &sam9x5_slow_ops;
  276. init.parent_names = parent_names;
  277. init.num_parents = num_parents;
  278. init.flags = 0;
  279. slowck->hw.init = &init;
  280. slowck->sckcr = sckcr;
  281. slowck->parent = !!(readl(sckcr) & AT91_SCKC_OSCSEL);
  282. clk = clk_register(NULL, &slowck->hw);
  283. if (IS_ERR(clk))
  284. kfree(slowck);
  285. else
  286. slow_clk = clk;
  287. return clk;
  288. }
  289. void __init of_at91sam9x5_clk_slow_setup(struct device_node *np,
  290. void __iomem *sckcr)
  291. {
  292. struct clk *clk;
  293. const char *parent_names[2];
  294. int num_parents;
  295. const char *name = np->name;
  296. num_parents = of_clk_get_parent_count(np);
  297. if (num_parents <= 0 || num_parents > 2)
  298. return;
  299. of_clk_parent_fill(np, parent_names, num_parents);
  300. of_property_read_string(np, "clock-output-names", &name);
  301. clk = at91_clk_register_sam9x5_slow(sckcr, name, parent_names,
  302. num_parents);
  303. if (IS_ERR(clk))
  304. return;
  305. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  306. }
  307. static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
  308. {
  309. struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
  310. return !!(pmc_read(slowck->pmc, AT91_PMC_SR) & AT91_PMC_OSCSEL);
  311. }
  312. static const struct clk_ops sam9260_slow_ops = {
  313. .get_parent = clk_sam9260_slow_get_parent,
  314. };
  315. static struct clk * __init
  316. at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  317. const char *name,
  318. const char **parent_names,
  319. int num_parents)
  320. {
  321. struct clk_sam9260_slow *slowck;
  322. struct clk *clk = NULL;
  323. struct clk_init_data init;
  324. if (!pmc || !name)
  325. return ERR_PTR(-EINVAL);
  326. if (!parent_names || !num_parents)
  327. return ERR_PTR(-EINVAL);
  328. slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
  329. if (!slowck)
  330. return ERR_PTR(-ENOMEM);
  331. init.name = name;
  332. init.ops = &sam9260_slow_ops;
  333. init.parent_names = parent_names;
  334. init.num_parents = num_parents;
  335. init.flags = 0;
  336. slowck->hw.init = &init;
  337. slowck->pmc = pmc;
  338. clk = clk_register(NULL, &slowck->hw);
  339. if (IS_ERR(clk))
  340. kfree(slowck);
  341. else
  342. slow_clk = clk;
  343. return clk;
  344. }
  345. void __init of_at91sam9260_clk_slow_setup(struct device_node *np,
  346. struct at91_pmc *pmc)
  347. {
  348. struct clk *clk;
  349. const char *parent_names[2];
  350. int num_parents;
  351. const char *name = np->name;
  352. num_parents = of_clk_get_parent_count(np);
  353. if (num_parents != 2)
  354. return;
  355. of_clk_parent_fill(np, parent_names, num_parents);
  356. of_property_read_string(np, "clock-output-names", &name);
  357. clk = at91_clk_register_sam9260_slow(pmc, name, parent_names,
  358. num_parents);
  359. if (IS_ERR(clk))
  360. return;
  361. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  362. }
  363. /*
  364. * FIXME: All slow clk users are not properly claiming it (get + prepare +
  365. * enable) before using it.
  366. * If all users properly claiming this clock decide that they don't need it
  367. * anymore (or are removed), it is disabled while faulty users are still
  368. * requiring it, and the system hangs.
  369. * Prevent this clock from being disabled until all users are properly
  370. * requesting it.
  371. * Once this is done we should remove this function and the slow_clk variable.
  372. */
  373. static int __init of_at91_clk_slow_retain(void)
  374. {
  375. if (!slow_clk)
  376. return 0;
  377. __clk_get(slow_clk);
  378. clk_prepare_enable(slow_clk);
  379. return 0;
  380. }
  381. arch_initcall(of_at91_clk_slow_retain);