clk-main.c 13 KB

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