clk-main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/delay.h>
  14. #include <linux/of.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/regmap.h>
  17. #include "pmc.h"
  18. #define SLOW_CLOCK_FREQ 32768
  19. #define MAINF_DIV 16
  20. #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
  21. SLOW_CLOCK_FREQ)
  22. #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
  23. #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
  24. #define MOR_KEY_MASK (0xff << 16)
  25. #define clk_main_parent_select(s) (((s) & \
  26. (AT91_PMC_MOSCEN | \
  27. AT91_PMC_OSCBYPASS)) ? 1 : 0)
  28. struct clk_main_osc {
  29. struct clk_hw hw;
  30. struct regmap *regmap;
  31. };
  32. #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
  33. struct clk_main_rc_osc {
  34. struct clk_hw hw;
  35. struct regmap *regmap;
  36. unsigned long frequency;
  37. unsigned long accuracy;
  38. };
  39. #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
  40. struct clk_rm9200_main {
  41. struct clk_hw hw;
  42. struct regmap *regmap;
  43. };
  44. #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
  45. struct clk_sam9x5_main {
  46. struct clk_hw hw;
  47. struct regmap *regmap;
  48. u8 parent;
  49. };
  50. #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
  51. static inline bool clk_main_osc_ready(struct regmap *regmap)
  52. {
  53. unsigned int status;
  54. regmap_read(regmap, AT91_PMC_SR, &status);
  55. return status & AT91_PMC_MOSCS;
  56. }
  57. static int clk_main_osc_prepare(struct clk_hw *hw)
  58. {
  59. struct clk_main_osc *osc = to_clk_main_osc(hw);
  60. struct regmap *regmap = osc->regmap;
  61. u32 tmp;
  62. regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  63. tmp &= ~MOR_KEY_MASK;
  64. if (tmp & AT91_PMC_OSCBYPASS)
  65. return 0;
  66. if (!(tmp & AT91_PMC_MOSCEN)) {
  67. tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
  68. regmap_write(regmap, AT91_CKGR_MOR, tmp);
  69. }
  70. while (!clk_main_osc_ready(regmap))
  71. cpu_relax();
  72. return 0;
  73. }
  74. static void clk_main_osc_unprepare(struct clk_hw *hw)
  75. {
  76. struct clk_main_osc *osc = to_clk_main_osc(hw);
  77. struct regmap *regmap = osc->regmap;
  78. u32 tmp;
  79. regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  80. if (tmp & AT91_PMC_OSCBYPASS)
  81. return;
  82. if (!(tmp & AT91_PMC_MOSCEN))
  83. return;
  84. tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
  85. regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  86. }
  87. static int clk_main_osc_is_prepared(struct clk_hw *hw)
  88. {
  89. struct clk_main_osc *osc = to_clk_main_osc(hw);
  90. struct regmap *regmap = osc->regmap;
  91. u32 tmp, status;
  92. regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  93. if (tmp & AT91_PMC_OSCBYPASS)
  94. return 1;
  95. regmap_read(regmap, AT91_PMC_SR, &status);
  96. return (status & AT91_PMC_MOSCS) && clk_main_parent_select(tmp);
  97. }
  98. static const struct clk_ops main_osc_ops = {
  99. .prepare = clk_main_osc_prepare,
  100. .unprepare = clk_main_osc_unprepare,
  101. .is_prepared = clk_main_osc_is_prepared,
  102. };
  103. static struct clk_hw * __init
  104. at91_clk_register_main_osc(struct regmap *regmap,
  105. const char *name,
  106. const char *parent_name,
  107. bool bypass)
  108. {
  109. struct clk_main_osc *osc;
  110. struct clk_init_data init;
  111. struct clk_hw *hw;
  112. int ret;
  113. if (!name || !parent_name)
  114. return ERR_PTR(-EINVAL);
  115. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  116. if (!osc)
  117. return ERR_PTR(-ENOMEM);
  118. init.name = name;
  119. init.ops = &main_osc_ops;
  120. init.parent_names = &parent_name;
  121. init.num_parents = 1;
  122. init.flags = CLK_IGNORE_UNUSED;
  123. osc->hw.init = &init;
  124. osc->regmap = regmap;
  125. if (bypass)
  126. regmap_update_bits(regmap,
  127. AT91_CKGR_MOR, MOR_KEY_MASK |
  128. AT91_PMC_OSCBYPASS,
  129. AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  130. hw = &osc->hw;
  131. ret = clk_hw_register(NULL, &osc->hw);
  132. if (ret) {
  133. kfree(osc);
  134. hw = ERR_PTR(ret);
  135. }
  136. return hw;
  137. }
  138. static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
  139. {
  140. struct clk_hw *hw;
  141. const char *name = np->name;
  142. const char *parent_name;
  143. struct regmap *regmap;
  144. bool bypass;
  145. of_property_read_string(np, "clock-output-names", &name);
  146. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  147. parent_name = of_clk_get_parent_name(np, 0);
  148. regmap = syscon_node_to_regmap(of_get_parent(np));
  149. if (IS_ERR(regmap))
  150. return;
  151. hw = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
  152. if (IS_ERR(hw))
  153. return;
  154. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  155. }
  156. CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
  157. of_at91rm9200_clk_main_osc_setup);
  158. static bool clk_main_rc_osc_ready(struct regmap *regmap)
  159. {
  160. unsigned int status;
  161. regmap_read(regmap, AT91_PMC_SR, &status);
  162. return status & AT91_PMC_MOSCRCS;
  163. }
  164. static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  165. {
  166. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  167. struct regmap *regmap = osc->regmap;
  168. unsigned int mor;
  169. regmap_read(regmap, AT91_CKGR_MOR, &mor);
  170. if (!(mor & AT91_PMC_MOSCRCEN))
  171. regmap_update_bits(regmap, AT91_CKGR_MOR,
  172. MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
  173. AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
  174. while (!clk_main_rc_osc_ready(regmap))
  175. cpu_relax();
  176. return 0;
  177. }
  178. static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
  179. {
  180. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  181. struct regmap *regmap = osc->regmap;
  182. unsigned int mor;
  183. regmap_read(regmap, AT91_CKGR_MOR, &mor);
  184. if (!(mor & AT91_PMC_MOSCRCEN))
  185. return;
  186. regmap_update_bits(regmap, AT91_CKGR_MOR,
  187. MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
  188. }
  189. static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
  190. {
  191. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  192. struct regmap *regmap = osc->regmap;
  193. unsigned int mor, status;
  194. regmap_read(regmap, AT91_CKGR_MOR, &mor);
  195. regmap_read(regmap, AT91_PMC_SR, &status);
  196. return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
  197. }
  198. static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
  199. unsigned long parent_rate)
  200. {
  201. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  202. return osc->frequency;
  203. }
  204. static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
  205. unsigned long parent_acc)
  206. {
  207. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  208. return osc->accuracy;
  209. }
  210. static const struct clk_ops main_rc_osc_ops = {
  211. .prepare = clk_main_rc_osc_prepare,
  212. .unprepare = clk_main_rc_osc_unprepare,
  213. .is_prepared = clk_main_rc_osc_is_prepared,
  214. .recalc_rate = clk_main_rc_osc_recalc_rate,
  215. .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
  216. };
  217. static struct clk_hw * __init
  218. at91_clk_register_main_rc_osc(struct regmap *regmap,
  219. const char *name,
  220. u32 frequency, u32 accuracy)
  221. {
  222. struct clk_main_rc_osc *osc;
  223. struct clk_init_data init;
  224. struct clk_hw *hw;
  225. int ret;
  226. if (!name || !frequency)
  227. return ERR_PTR(-EINVAL);
  228. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  229. if (!osc)
  230. return ERR_PTR(-ENOMEM);
  231. init.name = name;
  232. init.ops = &main_rc_osc_ops;
  233. init.parent_names = NULL;
  234. init.num_parents = 0;
  235. init.flags = CLK_IGNORE_UNUSED;
  236. osc->hw.init = &init;
  237. osc->regmap = regmap;
  238. osc->frequency = frequency;
  239. osc->accuracy = accuracy;
  240. hw = &osc->hw;
  241. ret = clk_hw_register(NULL, hw);
  242. if (ret) {
  243. kfree(osc);
  244. hw = ERR_PTR(ret);
  245. }
  246. return hw;
  247. }
  248. static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
  249. {
  250. struct clk_hw *hw;
  251. u32 frequency = 0;
  252. u32 accuracy = 0;
  253. const char *name = np->name;
  254. struct regmap *regmap;
  255. of_property_read_string(np, "clock-output-names", &name);
  256. of_property_read_u32(np, "clock-frequency", &frequency);
  257. of_property_read_u32(np, "clock-accuracy", &accuracy);
  258. regmap = syscon_node_to_regmap(of_get_parent(np));
  259. if (IS_ERR(regmap))
  260. return;
  261. hw = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
  262. if (IS_ERR(hw))
  263. return;
  264. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  265. }
  266. CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
  267. of_at91sam9x5_clk_main_rc_osc_setup);
  268. static int clk_main_probe_frequency(struct regmap *regmap)
  269. {
  270. unsigned long prep_time, timeout;
  271. unsigned int mcfr;
  272. timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
  273. do {
  274. prep_time = jiffies;
  275. regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  276. if (mcfr & AT91_PMC_MAINRDY)
  277. return 0;
  278. if (system_state < SYSTEM_RUNNING)
  279. udelay(MAINF_LOOP_MIN_WAIT);
  280. else
  281. usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
  282. } while (time_before(prep_time, timeout));
  283. return -ETIMEDOUT;
  284. }
  285. static unsigned long clk_main_recalc_rate(struct regmap *regmap,
  286. unsigned long parent_rate)
  287. {
  288. unsigned int mcfr;
  289. if (parent_rate)
  290. return parent_rate;
  291. pr_warn("Main crystal frequency not set, using approximate value\n");
  292. regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  293. if (!(mcfr & AT91_PMC_MAINRDY))
  294. return 0;
  295. return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  296. }
  297. static int clk_rm9200_main_prepare(struct clk_hw *hw)
  298. {
  299. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  300. return clk_main_probe_frequency(clkmain->regmap);
  301. }
  302. static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
  303. {
  304. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  305. unsigned int status;
  306. regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
  307. return status & AT91_PMC_MAINRDY ? 1 : 0;
  308. }
  309. static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  310. unsigned long parent_rate)
  311. {
  312. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  313. return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  314. }
  315. static const struct clk_ops rm9200_main_ops = {
  316. .prepare = clk_rm9200_main_prepare,
  317. .is_prepared = clk_rm9200_main_is_prepared,
  318. .recalc_rate = clk_rm9200_main_recalc_rate,
  319. };
  320. static struct clk_hw * __init
  321. at91_clk_register_rm9200_main(struct regmap *regmap,
  322. const char *name,
  323. const char *parent_name)
  324. {
  325. struct clk_rm9200_main *clkmain;
  326. struct clk_init_data init;
  327. struct clk_hw *hw;
  328. int ret;
  329. if (!name)
  330. return ERR_PTR(-EINVAL);
  331. if (!parent_name)
  332. return ERR_PTR(-EINVAL);
  333. clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
  334. if (!clkmain)
  335. return ERR_PTR(-ENOMEM);
  336. init.name = name;
  337. init.ops = &rm9200_main_ops;
  338. init.parent_names = &parent_name;
  339. init.num_parents = 1;
  340. init.flags = 0;
  341. clkmain->hw.init = &init;
  342. clkmain->regmap = regmap;
  343. hw = &clkmain->hw;
  344. ret = clk_hw_register(NULL, &clkmain->hw);
  345. if (ret) {
  346. kfree(clkmain);
  347. hw = ERR_PTR(ret);
  348. }
  349. return hw;
  350. }
  351. static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
  352. {
  353. struct clk_hw *hw;
  354. const char *parent_name;
  355. const char *name = np->name;
  356. struct regmap *regmap;
  357. parent_name = of_clk_get_parent_name(np, 0);
  358. of_property_read_string(np, "clock-output-names", &name);
  359. regmap = syscon_node_to_regmap(of_get_parent(np));
  360. if (IS_ERR(regmap))
  361. return;
  362. hw = at91_clk_register_rm9200_main(regmap, name, parent_name);
  363. if (IS_ERR(hw))
  364. return;
  365. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  366. }
  367. CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
  368. of_at91rm9200_clk_main_setup);
  369. static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
  370. {
  371. unsigned int status;
  372. regmap_read(regmap, AT91_PMC_SR, &status);
  373. return status & AT91_PMC_MOSCSELS ? 1 : 0;
  374. }
  375. static int clk_sam9x5_main_prepare(struct clk_hw *hw)
  376. {
  377. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  378. struct regmap *regmap = clkmain->regmap;
  379. while (!clk_sam9x5_main_ready(regmap))
  380. cpu_relax();
  381. return clk_main_probe_frequency(regmap);
  382. }
  383. static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
  384. {
  385. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  386. return clk_sam9x5_main_ready(clkmain->regmap);
  387. }
  388. static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  389. unsigned long parent_rate)
  390. {
  391. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  392. return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  393. }
  394. static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  395. {
  396. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  397. struct regmap *regmap = clkmain->regmap;
  398. unsigned int tmp;
  399. if (index > 1)
  400. return -EINVAL;
  401. regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  402. if (index && !(tmp & AT91_PMC_MOSCSEL))
  403. tmp = AT91_PMC_MOSCSEL;
  404. else if (!index && (tmp & AT91_PMC_MOSCSEL))
  405. tmp = 0;
  406. else
  407. return 0;
  408. regmap_update_bits(regmap, AT91_CKGR_MOR,
  409. AT91_PMC_MOSCSEL | MOR_KEY_MASK,
  410. tmp | AT91_PMC_KEY);
  411. while (!clk_sam9x5_main_ready(regmap))
  412. cpu_relax();
  413. return 0;
  414. }
  415. static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
  416. {
  417. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  418. unsigned int status;
  419. regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  420. return clk_main_parent_select(status);
  421. }
  422. static const struct clk_ops sam9x5_main_ops = {
  423. .prepare = clk_sam9x5_main_prepare,
  424. .is_prepared = clk_sam9x5_main_is_prepared,
  425. .recalc_rate = clk_sam9x5_main_recalc_rate,
  426. .set_parent = clk_sam9x5_main_set_parent,
  427. .get_parent = clk_sam9x5_main_get_parent,
  428. };
  429. static struct clk_hw * __init
  430. at91_clk_register_sam9x5_main(struct regmap *regmap,
  431. const char *name,
  432. const char **parent_names,
  433. int num_parents)
  434. {
  435. struct clk_sam9x5_main *clkmain;
  436. struct clk_init_data init;
  437. unsigned int status;
  438. struct clk_hw *hw;
  439. int ret;
  440. if (!name)
  441. return ERR_PTR(-EINVAL);
  442. if (!parent_names || !num_parents)
  443. return ERR_PTR(-EINVAL);
  444. clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
  445. if (!clkmain)
  446. return ERR_PTR(-ENOMEM);
  447. init.name = name;
  448. init.ops = &sam9x5_main_ops;
  449. init.parent_names = parent_names;
  450. init.num_parents = num_parents;
  451. init.flags = CLK_SET_PARENT_GATE;
  452. clkmain->hw.init = &init;
  453. clkmain->regmap = regmap;
  454. regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  455. clkmain->parent = clk_main_parent_select(status);
  456. hw = &clkmain->hw;
  457. ret = clk_hw_register(NULL, &clkmain->hw);
  458. if (ret) {
  459. kfree(clkmain);
  460. hw = ERR_PTR(ret);
  461. }
  462. return hw;
  463. }
  464. static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
  465. {
  466. struct clk_hw *hw;
  467. const char *parent_names[2];
  468. unsigned int num_parents;
  469. const char *name = np->name;
  470. struct regmap *regmap;
  471. num_parents = of_clk_get_parent_count(np);
  472. if (num_parents == 0 || num_parents > 2)
  473. return;
  474. of_clk_parent_fill(np, parent_names, num_parents);
  475. regmap = syscon_node_to_regmap(of_get_parent(np));
  476. if (IS_ERR(regmap))
  477. return;
  478. of_property_read_string(np, "clock-output-names", &name);
  479. hw = at91_clk_register_sam9x5_main(regmap, name, parent_names,
  480. num_parents);
  481. if (IS_ERR(hw))
  482. return;
  483. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  484. }
  485. CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
  486. of_at91sam9x5_clk_main_setup);