clk-slow.c 10 KB

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