clk-slow.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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-provider.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/clk/at91_pmc.h>
  15. #include <linux/delay.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/io.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include "pmc.h"
  25. #include "sckc.h"
  26. #define SLOW_CLOCK_FREQ 32768
  27. #define SLOWCK_SW_CYCLES 5
  28. #define SLOWCK_SW_TIME_USEC ((SLOWCK_SW_CYCLES * USEC_PER_SEC) / \
  29. SLOW_CLOCK_FREQ)
  30. #define AT91_SCKC_CR 0x00
  31. #define AT91_SCKC_RCEN (1 << 0)
  32. #define AT91_SCKC_OSC32EN (1 << 1)
  33. #define AT91_SCKC_OSC32BYP (1 << 2)
  34. #define AT91_SCKC_OSCSEL (1 << 3)
  35. struct clk_slow_osc {
  36. struct clk_hw hw;
  37. void __iomem *sckcr;
  38. unsigned long startup_usec;
  39. };
  40. #define to_clk_slow_osc(hw) container_of(hw, struct clk_slow_osc, hw)
  41. struct clk_slow_rc_osc {
  42. struct clk_hw hw;
  43. void __iomem *sckcr;
  44. unsigned long frequency;
  45. unsigned long accuracy;
  46. unsigned long startup_usec;
  47. };
  48. #define to_clk_slow_rc_osc(hw) container_of(hw, struct clk_slow_rc_osc, hw)
  49. struct clk_sam9260_slow {
  50. struct clk_hw hw;
  51. struct at91_pmc *pmc;
  52. };
  53. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  54. struct clk_sam9x5_slow {
  55. struct clk_hw hw;
  56. void __iomem *sckcr;
  57. u8 parent;
  58. };
  59. #define to_clk_sam9x5_slow(hw) container_of(hw, struct clk_sam9x5_slow, hw)
  60. static int clk_slow_osc_prepare(struct clk_hw *hw)
  61. {
  62. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  63. void __iomem *sckcr = osc->sckcr;
  64. u32 tmp = readl(sckcr);
  65. if (tmp & AT91_SCKC_OSC32BYP)
  66. return 0;
  67. writel(tmp | AT91_SCKC_OSC32EN, sckcr);
  68. usleep_range(osc->startup_usec, osc->startup_usec + 1);
  69. return 0;
  70. }
  71. static void clk_slow_osc_unprepare(struct clk_hw *hw)
  72. {
  73. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  74. void __iomem *sckcr = osc->sckcr;
  75. u32 tmp = readl(sckcr);
  76. if (tmp & AT91_SCKC_OSC32BYP)
  77. return;
  78. writel(tmp & ~AT91_SCKC_OSC32EN, sckcr);
  79. }
  80. static int clk_slow_osc_is_prepared(struct clk_hw *hw)
  81. {
  82. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  83. void __iomem *sckcr = osc->sckcr;
  84. u32 tmp = readl(sckcr);
  85. if (tmp & AT91_SCKC_OSC32BYP)
  86. return 1;
  87. return !!(tmp & AT91_SCKC_OSC32EN);
  88. }
  89. static const struct clk_ops slow_osc_ops = {
  90. .prepare = clk_slow_osc_prepare,
  91. .unprepare = clk_slow_osc_unprepare,
  92. .is_prepared = clk_slow_osc_is_prepared,
  93. };
  94. static struct clk * __init
  95. at91_clk_register_slow_osc(void __iomem *sckcr,
  96. const char *name,
  97. const char *parent_name,
  98. unsigned long startup,
  99. bool bypass)
  100. {
  101. struct clk_slow_osc *osc;
  102. struct clk *clk = NULL;
  103. struct clk_init_data init;
  104. if (!sckcr || !name || !parent_name)
  105. return ERR_PTR(-EINVAL);
  106. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  107. if (!osc)
  108. return ERR_PTR(-ENOMEM);
  109. init.name = name;
  110. init.ops = &slow_osc_ops;
  111. init.parent_names = &parent_name;
  112. init.num_parents = 1;
  113. init.flags = CLK_IGNORE_UNUSED;
  114. osc->hw.init = &init;
  115. osc->sckcr = sckcr;
  116. osc->startup_usec = startup;
  117. if (bypass)
  118. writel((readl(sckcr) & ~AT91_SCKC_OSC32EN) | AT91_SCKC_OSC32BYP,
  119. sckcr);
  120. clk = clk_register(NULL, &osc->hw);
  121. if (IS_ERR(clk))
  122. kfree(osc);
  123. return clk;
  124. }
  125. void __init of_at91sam9x5_clk_slow_osc_setup(struct device_node *np,
  126. void __iomem *sckcr)
  127. {
  128. struct clk *clk;
  129. const char *parent_name;
  130. const char *name = np->name;
  131. u32 startup;
  132. bool bypass;
  133. parent_name = of_clk_get_parent_name(np, 0);
  134. of_property_read_string(np, "clock-output-names", &name);
  135. of_property_read_u32(np, "atmel,startup-time-usec", &startup);
  136. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  137. clk = at91_clk_register_slow_osc(sckcr, name, parent_name, startup,
  138. bypass);
  139. if (IS_ERR(clk))
  140. return;
  141. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  142. }
  143. static unsigned long clk_slow_rc_osc_recalc_rate(struct clk_hw *hw,
  144. unsigned long parent_rate)
  145. {
  146. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  147. return osc->frequency;
  148. }
  149. static unsigned long clk_slow_rc_osc_recalc_accuracy(struct clk_hw *hw,
  150. unsigned long parent_acc)
  151. {
  152. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  153. return osc->accuracy;
  154. }
  155. static int clk_slow_rc_osc_prepare(struct clk_hw *hw)
  156. {
  157. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  158. void __iomem *sckcr = osc->sckcr;
  159. writel(readl(sckcr) | AT91_SCKC_RCEN, sckcr);
  160. usleep_range(osc->startup_usec, osc->startup_usec + 1);
  161. return 0;
  162. }
  163. static void clk_slow_rc_osc_unprepare(struct clk_hw *hw)
  164. {
  165. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  166. void __iomem *sckcr = osc->sckcr;
  167. writel(readl(sckcr) & ~AT91_SCKC_RCEN, sckcr);
  168. }
  169. static int clk_slow_rc_osc_is_prepared(struct clk_hw *hw)
  170. {
  171. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  172. return !!(readl(osc->sckcr) & AT91_SCKC_RCEN);
  173. }
  174. static const struct clk_ops slow_rc_osc_ops = {
  175. .prepare = clk_slow_rc_osc_prepare,
  176. .unprepare = clk_slow_rc_osc_unprepare,
  177. .is_prepared = clk_slow_rc_osc_is_prepared,
  178. .recalc_rate = clk_slow_rc_osc_recalc_rate,
  179. .recalc_accuracy = clk_slow_rc_osc_recalc_accuracy,
  180. };
  181. static struct clk * __init
  182. at91_clk_register_slow_rc_osc(void __iomem *sckcr,
  183. const char *name,
  184. unsigned long frequency,
  185. unsigned long accuracy,
  186. unsigned long startup)
  187. {
  188. struct clk_slow_rc_osc *osc;
  189. struct clk *clk = NULL;
  190. struct clk_init_data init;
  191. if (!sckcr || !name)
  192. return ERR_PTR(-EINVAL);
  193. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  194. if (!osc)
  195. return ERR_PTR(-ENOMEM);
  196. init.name = name;
  197. init.ops = &slow_rc_osc_ops;
  198. init.parent_names = NULL;
  199. init.num_parents = 0;
  200. init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
  201. osc->hw.init = &init;
  202. osc->sckcr = sckcr;
  203. osc->frequency = frequency;
  204. osc->accuracy = accuracy;
  205. osc->startup_usec = startup;
  206. clk = clk_register(NULL, &osc->hw);
  207. if (IS_ERR(clk))
  208. kfree(osc);
  209. return clk;
  210. }
  211. void __init of_at91sam9x5_clk_slow_rc_osc_setup(struct device_node *np,
  212. void __iomem *sckcr)
  213. {
  214. struct clk *clk;
  215. u32 frequency = 0;
  216. u32 accuracy = 0;
  217. u32 startup = 0;
  218. const char *name = np->name;
  219. of_property_read_string(np, "clock-output-names", &name);
  220. of_property_read_u32(np, "clock-frequency", &frequency);
  221. of_property_read_u32(np, "clock-accuracy", &accuracy);
  222. of_property_read_u32(np, "atmel,startup-time-usec", &startup);
  223. clk = at91_clk_register_slow_rc_osc(sckcr, name, frequency, accuracy,
  224. startup);
  225. if (IS_ERR(clk))
  226. return;
  227. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  228. }
  229. static int clk_sam9x5_slow_set_parent(struct clk_hw *hw, u8 index)
  230. {
  231. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  232. void __iomem *sckcr = slowck->sckcr;
  233. u32 tmp;
  234. if (index > 1)
  235. return -EINVAL;
  236. tmp = readl(sckcr);
  237. if ((!index && !(tmp & AT91_SCKC_OSCSEL)) ||
  238. (index && (tmp & AT91_SCKC_OSCSEL)))
  239. return 0;
  240. if (index)
  241. tmp |= AT91_SCKC_OSCSEL;
  242. else
  243. tmp &= ~AT91_SCKC_OSCSEL;
  244. writel(tmp, sckcr);
  245. usleep_range(SLOWCK_SW_TIME_USEC, SLOWCK_SW_TIME_USEC + 1);
  246. return 0;
  247. }
  248. static u8 clk_sam9x5_slow_get_parent(struct clk_hw *hw)
  249. {
  250. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  251. return !!(readl(slowck->sckcr) & AT91_SCKC_OSCSEL);
  252. }
  253. static const struct clk_ops sam9x5_slow_ops = {
  254. .set_parent = clk_sam9x5_slow_set_parent,
  255. .get_parent = clk_sam9x5_slow_get_parent,
  256. };
  257. static struct clk * __init
  258. at91_clk_register_sam9x5_slow(void __iomem *sckcr,
  259. const char *name,
  260. const char **parent_names,
  261. int num_parents)
  262. {
  263. struct clk_sam9x5_slow *slowck;
  264. struct clk *clk = NULL;
  265. struct clk_init_data init;
  266. if (!sckcr || !name || !parent_names || !num_parents)
  267. return ERR_PTR(-EINVAL);
  268. slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
  269. if (!slowck)
  270. return ERR_PTR(-ENOMEM);
  271. init.name = name;
  272. init.ops = &sam9x5_slow_ops;
  273. init.parent_names = parent_names;
  274. init.num_parents = num_parents;
  275. init.flags = 0;
  276. slowck->hw.init = &init;
  277. slowck->sckcr = sckcr;
  278. slowck->parent = !!(readl(sckcr) & AT91_SCKC_OSCSEL);
  279. clk = clk_register(NULL, &slowck->hw);
  280. if (IS_ERR(clk))
  281. kfree(slowck);
  282. return clk;
  283. }
  284. void __init of_at91sam9x5_clk_slow_setup(struct device_node *np,
  285. void __iomem *sckcr)
  286. {
  287. struct clk *clk;
  288. const char *parent_names[2];
  289. int num_parents;
  290. const char *name = np->name;
  291. int i;
  292. num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  293. if (num_parents <= 0 || num_parents > 2)
  294. return;
  295. for (i = 0; i < num_parents; ++i) {
  296. parent_names[i] = of_clk_get_parent_name(np, i);
  297. if (!parent_names[i])
  298. return;
  299. }
  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. return clk;
  342. }
  343. void __init of_at91sam9260_clk_slow_setup(struct device_node *np,
  344. struct at91_pmc *pmc)
  345. {
  346. struct clk *clk;
  347. const char *parent_names[2];
  348. int num_parents;
  349. const char *name = np->name;
  350. int i;
  351. num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  352. if (num_parents <= 0 || num_parents > 1)
  353. return;
  354. for (i = 0; i < num_parents; ++i) {
  355. parent_names[i] = of_clk_get_parent_name(np, i);
  356. if (!parent_names[i])
  357. return;
  358. }
  359. of_property_read_string(np, "clock-output-names", &name);
  360. clk = at91_clk_register_sam9260_slow(pmc, name, parent_names,
  361. num_parents);
  362. if (IS_ERR(clk))
  363. return;
  364. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  365. }