sckc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/clk/at91/sckc.c
  4. *
  5. * Copyright (C) 2013 Boris BREZILLON <[email protected]>
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/clkdev.h>
  9. #include <linux/delay.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/io.h>
  13. #define SLOW_CLOCK_FREQ 32768
  14. #define SLOWCK_SW_CYCLES 5
  15. #define SLOWCK_SW_TIME_USEC ((SLOWCK_SW_CYCLES * USEC_PER_SEC) / \
  16. SLOW_CLOCK_FREQ)
  17. #define AT91_SCKC_CR 0x00
  18. struct clk_slow_bits {
  19. u32 cr_rcen;
  20. u32 cr_osc32en;
  21. u32 cr_osc32byp;
  22. u32 cr_oscsel;
  23. };
  24. struct clk_slow_osc {
  25. struct clk_hw hw;
  26. void __iomem *sckcr;
  27. const struct clk_slow_bits *bits;
  28. unsigned long startup_usec;
  29. };
  30. #define to_clk_slow_osc(hw) container_of(hw, struct clk_slow_osc, hw)
  31. struct clk_sama5d4_slow_osc {
  32. struct clk_hw hw;
  33. void __iomem *sckcr;
  34. const struct clk_slow_bits *bits;
  35. unsigned long startup_usec;
  36. bool prepared;
  37. };
  38. #define to_clk_sama5d4_slow_osc(hw) container_of(hw, struct clk_sama5d4_slow_osc, hw)
  39. struct clk_slow_rc_osc {
  40. struct clk_hw hw;
  41. void __iomem *sckcr;
  42. const struct clk_slow_bits *bits;
  43. unsigned long frequency;
  44. unsigned long accuracy;
  45. unsigned long startup_usec;
  46. };
  47. #define to_clk_slow_rc_osc(hw) container_of(hw, struct clk_slow_rc_osc, hw)
  48. struct clk_sam9x5_slow {
  49. struct clk_hw hw;
  50. void __iomem *sckcr;
  51. const struct clk_slow_bits *bits;
  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 & (osc->bits->cr_osc32byp | osc->bits->cr_osc32en))
  61. return 0;
  62. writel(tmp | osc->bits->cr_osc32en, sckcr);
  63. if (system_state < SYSTEM_RUNNING)
  64. udelay(osc->startup_usec);
  65. else
  66. usleep_range(osc->startup_usec, osc->startup_usec + 1);
  67. return 0;
  68. }
  69. static void clk_slow_osc_unprepare(struct clk_hw *hw)
  70. {
  71. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  72. void __iomem *sckcr = osc->sckcr;
  73. u32 tmp = readl(sckcr);
  74. if (tmp & osc->bits->cr_osc32byp)
  75. return;
  76. writel(tmp & ~osc->bits->cr_osc32en, sckcr);
  77. }
  78. static int clk_slow_osc_is_prepared(struct clk_hw *hw)
  79. {
  80. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  81. void __iomem *sckcr = osc->sckcr;
  82. u32 tmp = readl(sckcr);
  83. if (tmp & osc->bits->cr_osc32byp)
  84. return 1;
  85. return !!(tmp & osc->bits->cr_osc32en);
  86. }
  87. static const struct clk_ops slow_osc_ops = {
  88. .prepare = clk_slow_osc_prepare,
  89. .unprepare = clk_slow_osc_unprepare,
  90. .is_prepared = clk_slow_osc_is_prepared,
  91. };
  92. static struct clk_hw * __init
  93. at91_clk_register_slow_osc(void __iomem *sckcr,
  94. const char *name,
  95. const char *parent_name,
  96. unsigned long startup,
  97. bool bypass,
  98. const struct clk_slow_bits *bits)
  99. {
  100. struct clk_slow_osc *osc;
  101. struct clk_hw *hw;
  102. struct clk_init_data init;
  103. int ret;
  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. osc->bits = bits;
  118. if (bypass)
  119. writel((readl(sckcr) & ~osc->bits->cr_osc32en) |
  120. osc->bits->cr_osc32byp, sckcr);
  121. hw = &osc->hw;
  122. ret = clk_hw_register(NULL, &osc->hw);
  123. if (ret) {
  124. kfree(osc);
  125. hw = ERR_PTR(ret);
  126. }
  127. return hw;
  128. }
  129. static void at91_clk_unregister_slow_osc(struct clk_hw *hw)
  130. {
  131. struct clk_slow_osc *osc = to_clk_slow_osc(hw);
  132. clk_hw_unregister(hw);
  133. kfree(osc);
  134. }
  135. static unsigned long clk_slow_rc_osc_recalc_rate(struct clk_hw *hw,
  136. unsigned long parent_rate)
  137. {
  138. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  139. return osc->frequency;
  140. }
  141. static unsigned long clk_slow_rc_osc_recalc_accuracy(struct clk_hw *hw,
  142. unsigned long parent_acc)
  143. {
  144. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  145. return osc->accuracy;
  146. }
  147. static int clk_slow_rc_osc_prepare(struct clk_hw *hw)
  148. {
  149. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  150. void __iomem *sckcr = osc->sckcr;
  151. writel(readl(sckcr) | osc->bits->cr_rcen, sckcr);
  152. if (system_state < SYSTEM_RUNNING)
  153. udelay(osc->startup_usec);
  154. else
  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) & ~osc->bits->cr_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) & osc->bits->cr_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_hw * __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. const struct clk_slow_bits *bits)
  183. {
  184. struct clk_slow_rc_osc *osc;
  185. struct clk_hw *hw;
  186. struct clk_init_data init;
  187. int ret;
  188. if (!sckcr || !name)
  189. return ERR_PTR(-EINVAL);
  190. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  191. if (!osc)
  192. return ERR_PTR(-ENOMEM);
  193. init.name = name;
  194. init.ops = &slow_rc_osc_ops;
  195. init.parent_names = NULL;
  196. init.num_parents = 0;
  197. init.flags = CLK_IGNORE_UNUSED;
  198. osc->hw.init = &init;
  199. osc->sckcr = sckcr;
  200. osc->bits = bits;
  201. osc->frequency = frequency;
  202. osc->accuracy = accuracy;
  203. osc->startup_usec = startup;
  204. hw = &osc->hw;
  205. ret = clk_hw_register(NULL, &osc->hw);
  206. if (ret) {
  207. kfree(osc);
  208. hw = ERR_PTR(ret);
  209. }
  210. return hw;
  211. }
  212. static void at91_clk_unregister_slow_rc_osc(struct clk_hw *hw)
  213. {
  214. struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
  215. clk_hw_unregister(hw);
  216. kfree(osc);
  217. }
  218. static int clk_sam9x5_slow_set_parent(struct clk_hw *hw, u8 index)
  219. {
  220. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  221. void __iomem *sckcr = slowck->sckcr;
  222. u32 tmp;
  223. if (index > 1)
  224. return -EINVAL;
  225. tmp = readl(sckcr);
  226. if ((!index && !(tmp & slowck->bits->cr_oscsel)) ||
  227. (index && (tmp & slowck->bits->cr_oscsel)))
  228. return 0;
  229. if (index)
  230. tmp |= slowck->bits->cr_oscsel;
  231. else
  232. tmp &= ~slowck->bits->cr_oscsel;
  233. writel(tmp, sckcr);
  234. if (system_state < SYSTEM_RUNNING)
  235. udelay(SLOWCK_SW_TIME_USEC);
  236. else
  237. usleep_range(SLOWCK_SW_TIME_USEC, SLOWCK_SW_TIME_USEC + 1);
  238. return 0;
  239. }
  240. static u8 clk_sam9x5_slow_get_parent(struct clk_hw *hw)
  241. {
  242. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  243. return !!(readl(slowck->sckcr) & slowck->bits->cr_oscsel);
  244. }
  245. static const struct clk_ops sam9x5_slow_ops = {
  246. .set_parent = clk_sam9x5_slow_set_parent,
  247. .get_parent = clk_sam9x5_slow_get_parent,
  248. };
  249. static struct clk_hw * __init
  250. at91_clk_register_sam9x5_slow(void __iomem *sckcr,
  251. const char *name,
  252. const char **parent_names,
  253. int num_parents,
  254. const struct clk_slow_bits *bits)
  255. {
  256. struct clk_sam9x5_slow *slowck;
  257. struct clk_hw *hw;
  258. struct clk_init_data init;
  259. int ret;
  260. if (!sckcr || !name || !parent_names || !num_parents)
  261. return ERR_PTR(-EINVAL);
  262. slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
  263. if (!slowck)
  264. return ERR_PTR(-ENOMEM);
  265. init.name = name;
  266. init.ops = &sam9x5_slow_ops;
  267. init.parent_names = parent_names;
  268. init.num_parents = num_parents;
  269. init.flags = 0;
  270. slowck->hw.init = &init;
  271. slowck->sckcr = sckcr;
  272. slowck->bits = bits;
  273. slowck->parent = !!(readl(sckcr) & slowck->bits->cr_oscsel);
  274. hw = &slowck->hw;
  275. ret = clk_hw_register(NULL, &slowck->hw);
  276. if (ret) {
  277. kfree(slowck);
  278. hw = ERR_PTR(ret);
  279. }
  280. return hw;
  281. }
  282. static void at91_clk_unregister_sam9x5_slow(struct clk_hw *hw)
  283. {
  284. struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
  285. clk_hw_unregister(hw);
  286. kfree(slowck);
  287. }
  288. static void __init at91sam9x5_sckc_register(struct device_node *np,
  289. unsigned int rc_osc_startup_us,
  290. const struct clk_slow_bits *bits)
  291. {
  292. const char *parent_names[2] = { "slow_rc_osc", "slow_osc" };
  293. void __iomem *regbase = of_iomap(np, 0);
  294. struct device_node *child = NULL;
  295. const char *xtal_name;
  296. struct clk_hw *slow_rc, *slow_osc, *slowck;
  297. bool bypass;
  298. int ret;
  299. if (!regbase)
  300. return;
  301. slow_rc = at91_clk_register_slow_rc_osc(regbase, parent_names[0],
  302. 32768, 50000000,
  303. rc_osc_startup_us, bits);
  304. if (IS_ERR(slow_rc))
  305. return;
  306. xtal_name = of_clk_get_parent_name(np, 0);
  307. if (!xtal_name) {
  308. /* DT backward compatibility */
  309. child = of_get_compatible_child(np, "atmel,at91sam9x5-clk-slow-osc");
  310. if (!child)
  311. goto unregister_slow_rc;
  312. xtal_name = of_clk_get_parent_name(child, 0);
  313. bypass = of_property_read_bool(child, "atmel,osc-bypass");
  314. child = of_get_compatible_child(np, "atmel,at91sam9x5-clk-slow");
  315. } else {
  316. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  317. }
  318. if (!xtal_name)
  319. goto unregister_slow_rc;
  320. slow_osc = at91_clk_register_slow_osc(regbase, parent_names[1],
  321. xtal_name, 1200000, bypass, bits);
  322. if (IS_ERR(slow_osc))
  323. goto unregister_slow_rc;
  324. slowck = at91_clk_register_sam9x5_slow(regbase, "slowck", parent_names,
  325. 2, bits);
  326. if (IS_ERR(slowck))
  327. goto unregister_slow_osc;
  328. /* DT backward compatibility */
  329. if (child)
  330. ret = of_clk_add_hw_provider(child, of_clk_hw_simple_get,
  331. slowck);
  332. else
  333. ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, slowck);
  334. if (WARN_ON(ret))
  335. goto unregister_slowck;
  336. return;
  337. unregister_slowck:
  338. at91_clk_unregister_sam9x5_slow(slowck);
  339. unregister_slow_osc:
  340. at91_clk_unregister_slow_osc(slow_osc);
  341. unregister_slow_rc:
  342. at91_clk_unregister_slow_rc_osc(slow_rc);
  343. }
  344. static const struct clk_slow_bits at91sam9x5_bits = {
  345. .cr_rcen = BIT(0),
  346. .cr_osc32en = BIT(1),
  347. .cr_osc32byp = BIT(2),
  348. .cr_oscsel = BIT(3),
  349. };
  350. static void __init of_at91sam9x5_sckc_setup(struct device_node *np)
  351. {
  352. at91sam9x5_sckc_register(np, 75, &at91sam9x5_bits);
  353. }
  354. CLK_OF_DECLARE(at91sam9x5_clk_sckc, "atmel,at91sam9x5-sckc",
  355. of_at91sam9x5_sckc_setup);
  356. static void __init of_sama5d3_sckc_setup(struct device_node *np)
  357. {
  358. at91sam9x5_sckc_register(np, 500, &at91sam9x5_bits);
  359. }
  360. CLK_OF_DECLARE(sama5d3_clk_sckc, "atmel,sama5d3-sckc",
  361. of_sama5d3_sckc_setup);
  362. static const struct clk_slow_bits at91sam9x60_bits = {
  363. .cr_osc32en = BIT(1),
  364. .cr_osc32byp = BIT(2),
  365. .cr_oscsel = BIT(24),
  366. };
  367. static void __init of_sam9x60_sckc_setup(struct device_node *np)
  368. {
  369. void __iomem *regbase = of_iomap(np, 0);
  370. struct clk_hw_onecell_data *clk_data;
  371. struct clk_hw *slow_rc, *slow_osc;
  372. const char *xtal_name;
  373. const char *parent_names[2] = { "slow_rc_osc", "slow_osc" };
  374. bool bypass;
  375. int ret;
  376. if (!regbase)
  377. return;
  378. slow_rc = clk_hw_register_fixed_rate_with_accuracy(NULL, parent_names[0],
  379. NULL, 0, 32768,
  380. 93750000);
  381. if (IS_ERR(slow_rc))
  382. return;
  383. xtal_name = of_clk_get_parent_name(np, 0);
  384. if (!xtal_name)
  385. goto unregister_slow_rc;
  386. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  387. slow_osc = at91_clk_register_slow_osc(regbase, parent_names[1],
  388. xtal_name, 5000000, bypass,
  389. &at91sam9x60_bits);
  390. if (IS_ERR(slow_osc))
  391. goto unregister_slow_rc;
  392. clk_data = kzalloc(struct_size(clk_data, hws, 2), GFP_KERNEL);
  393. if (!clk_data)
  394. goto unregister_slow_osc;
  395. /* MD_SLCK and TD_SLCK. */
  396. clk_data->num = 2;
  397. clk_data->hws[0] = clk_hw_register_fixed_rate(NULL, "md_slck",
  398. parent_names[0],
  399. 0, 32768);
  400. if (IS_ERR(clk_data->hws[0]))
  401. goto clk_data_free;
  402. clk_data->hws[1] = at91_clk_register_sam9x5_slow(regbase, "td_slck",
  403. parent_names, 2,
  404. &at91sam9x60_bits);
  405. if (IS_ERR(clk_data->hws[1]))
  406. goto unregister_md_slck;
  407. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
  408. if (WARN_ON(ret))
  409. goto unregister_td_slck;
  410. return;
  411. unregister_td_slck:
  412. at91_clk_unregister_sam9x5_slow(clk_data->hws[1]);
  413. unregister_md_slck:
  414. clk_hw_unregister(clk_data->hws[0]);
  415. clk_data_free:
  416. kfree(clk_data);
  417. unregister_slow_osc:
  418. at91_clk_unregister_slow_osc(slow_osc);
  419. unregister_slow_rc:
  420. clk_hw_unregister(slow_rc);
  421. }
  422. CLK_OF_DECLARE(sam9x60_clk_sckc, "microchip,sam9x60-sckc",
  423. of_sam9x60_sckc_setup);
  424. static int clk_sama5d4_slow_osc_prepare(struct clk_hw *hw)
  425. {
  426. struct clk_sama5d4_slow_osc *osc = to_clk_sama5d4_slow_osc(hw);
  427. if (osc->prepared)
  428. return 0;
  429. /*
  430. * Assume that if it has already been selected (for example by the
  431. * bootloader), enough time has already passed.
  432. */
  433. if ((readl(osc->sckcr) & osc->bits->cr_oscsel)) {
  434. osc->prepared = true;
  435. return 0;
  436. }
  437. if (system_state < SYSTEM_RUNNING)
  438. udelay(osc->startup_usec);
  439. else
  440. usleep_range(osc->startup_usec, osc->startup_usec + 1);
  441. osc->prepared = true;
  442. return 0;
  443. }
  444. static int clk_sama5d4_slow_osc_is_prepared(struct clk_hw *hw)
  445. {
  446. struct clk_sama5d4_slow_osc *osc = to_clk_sama5d4_slow_osc(hw);
  447. return osc->prepared;
  448. }
  449. static const struct clk_ops sama5d4_slow_osc_ops = {
  450. .prepare = clk_sama5d4_slow_osc_prepare,
  451. .is_prepared = clk_sama5d4_slow_osc_is_prepared,
  452. };
  453. static const struct clk_slow_bits at91sama5d4_bits = {
  454. .cr_oscsel = BIT(3),
  455. };
  456. static void __init of_sama5d4_sckc_setup(struct device_node *np)
  457. {
  458. void __iomem *regbase = of_iomap(np, 0);
  459. struct clk_hw *slow_rc, *slowck;
  460. struct clk_sama5d4_slow_osc *osc;
  461. struct clk_init_data init;
  462. const char *xtal_name;
  463. const char *parent_names[2] = { "slow_rc_osc", "slow_osc" };
  464. int ret;
  465. if (!regbase)
  466. return;
  467. slow_rc = clk_hw_register_fixed_rate_with_accuracy(NULL,
  468. parent_names[0],
  469. NULL, 0, 32768,
  470. 250000000);
  471. if (IS_ERR(slow_rc))
  472. return;
  473. xtal_name = of_clk_get_parent_name(np, 0);
  474. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  475. if (!osc)
  476. goto unregister_slow_rc;
  477. init.name = parent_names[1];
  478. init.ops = &sama5d4_slow_osc_ops;
  479. init.parent_names = &xtal_name;
  480. init.num_parents = 1;
  481. init.flags = CLK_IGNORE_UNUSED;
  482. osc->hw.init = &init;
  483. osc->sckcr = regbase;
  484. osc->startup_usec = 1200000;
  485. osc->bits = &at91sama5d4_bits;
  486. ret = clk_hw_register(NULL, &osc->hw);
  487. if (ret)
  488. goto free_slow_osc_data;
  489. slowck = at91_clk_register_sam9x5_slow(regbase, "slowck",
  490. parent_names, 2,
  491. &at91sama5d4_bits);
  492. if (IS_ERR(slowck))
  493. goto unregister_slow_osc;
  494. ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, slowck);
  495. if (WARN_ON(ret))
  496. goto unregister_slowck;
  497. return;
  498. unregister_slowck:
  499. at91_clk_unregister_sam9x5_slow(slowck);
  500. unregister_slow_osc:
  501. clk_hw_unregister(&osc->hw);
  502. free_slow_osc_data:
  503. kfree(osc);
  504. unregister_slow_rc:
  505. clk_hw_unregister(slow_rc);
  506. }
  507. CLK_OF_DECLARE(sama5d4_clk_sckc, "atmel,sama5d4-sckc",
  508. of_sama5d4_sckc_setup);