clk-kona-setup.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Broadcom Corporation
  4. * Copyright 2013 Linaro Limited
  5. */
  6. #include <linux/io.h>
  7. #include <linux/of_address.h>
  8. #include "clk-kona.h"
  9. /* These are used when a selector or trigger is found to be unneeded */
  10. #define selector_clear_exists(sel) ((sel)->width = 0)
  11. #define trigger_clear_exists(trig) FLAG_CLEAR(trig, TRIG, EXISTS)
  12. /* Validity checking */
  13. static bool ccu_data_offsets_valid(struct ccu_data *ccu)
  14. {
  15. struct ccu_policy *ccu_policy = &ccu->policy;
  16. u32 limit;
  17. limit = ccu->range - sizeof(u32);
  18. limit = round_down(limit, sizeof(u32));
  19. if (ccu_policy_exists(ccu_policy)) {
  20. if (ccu_policy->enable.offset > limit) {
  21. pr_err("%s: bad policy enable offset for %s "
  22. "(%u > %u)\n", __func__,
  23. ccu->name, ccu_policy->enable.offset, limit);
  24. return false;
  25. }
  26. if (ccu_policy->control.offset > limit) {
  27. pr_err("%s: bad policy control offset for %s "
  28. "(%u > %u)\n", __func__,
  29. ccu->name, ccu_policy->control.offset, limit);
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. static bool clk_requires_trigger(struct kona_clk *bcm_clk)
  36. {
  37. struct peri_clk_data *peri = bcm_clk->u.peri;
  38. struct bcm_clk_sel *sel;
  39. struct bcm_clk_div *div;
  40. if (bcm_clk->type != bcm_clk_peri)
  41. return false;
  42. sel = &peri->sel;
  43. if (sel->parent_count && selector_exists(sel))
  44. return true;
  45. div = &peri->div;
  46. if (!divider_exists(div))
  47. return false;
  48. /* Fixed dividers don't need triggers */
  49. if (!divider_is_fixed(div))
  50. return true;
  51. div = &peri->pre_div;
  52. return divider_exists(div) && !divider_is_fixed(div);
  53. }
  54. static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk)
  55. {
  56. struct peri_clk_data *peri;
  57. struct bcm_clk_policy *policy;
  58. struct bcm_clk_gate *gate;
  59. struct bcm_clk_hyst *hyst;
  60. struct bcm_clk_div *div;
  61. struct bcm_clk_sel *sel;
  62. struct bcm_clk_trig *trig;
  63. const char *name;
  64. u32 range;
  65. u32 limit;
  66. BUG_ON(bcm_clk->type != bcm_clk_peri);
  67. peri = bcm_clk->u.peri;
  68. name = bcm_clk->init_data.name;
  69. range = bcm_clk->ccu->range;
  70. limit = range - sizeof(u32);
  71. limit = round_down(limit, sizeof(u32));
  72. policy = &peri->policy;
  73. if (policy_exists(policy)) {
  74. if (policy->offset > limit) {
  75. pr_err("%s: bad policy offset for %s (%u > %u)\n",
  76. __func__, name, policy->offset, limit);
  77. return false;
  78. }
  79. }
  80. gate = &peri->gate;
  81. hyst = &peri->hyst;
  82. if (gate_exists(gate)) {
  83. if (gate->offset > limit) {
  84. pr_err("%s: bad gate offset for %s (%u > %u)\n",
  85. __func__, name, gate->offset, limit);
  86. return false;
  87. }
  88. if (hyst_exists(hyst)) {
  89. if (hyst->offset > limit) {
  90. pr_err("%s: bad hysteresis offset for %s "
  91. "(%u > %u)\n", __func__,
  92. name, hyst->offset, limit);
  93. return false;
  94. }
  95. }
  96. } else if (hyst_exists(hyst)) {
  97. pr_err("%s: hysteresis but no gate for %s\n", __func__, name);
  98. return false;
  99. }
  100. div = &peri->div;
  101. if (divider_exists(div)) {
  102. if (div->u.s.offset > limit) {
  103. pr_err("%s: bad divider offset for %s (%u > %u)\n",
  104. __func__, name, div->u.s.offset, limit);
  105. return false;
  106. }
  107. }
  108. div = &peri->pre_div;
  109. if (divider_exists(div)) {
  110. if (div->u.s.offset > limit) {
  111. pr_err("%s: bad pre-divider offset for %s "
  112. "(%u > %u)\n",
  113. __func__, name, div->u.s.offset, limit);
  114. return false;
  115. }
  116. }
  117. sel = &peri->sel;
  118. if (selector_exists(sel)) {
  119. if (sel->offset > limit) {
  120. pr_err("%s: bad selector offset for %s (%u > %u)\n",
  121. __func__, name, sel->offset, limit);
  122. return false;
  123. }
  124. }
  125. trig = &peri->trig;
  126. if (trigger_exists(trig)) {
  127. if (trig->offset > limit) {
  128. pr_err("%s: bad trigger offset for %s (%u > %u)\n",
  129. __func__, name, trig->offset, limit);
  130. return false;
  131. }
  132. }
  133. trig = &peri->pre_trig;
  134. if (trigger_exists(trig)) {
  135. if (trig->offset > limit) {
  136. pr_err("%s: bad pre-trigger offset for %s (%u > %u)\n",
  137. __func__, name, trig->offset, limit);
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. /* A bit position must be less than the number of bits in a 32-bit register. */
  144. static bool bit_posn_valid(u32 bit_posn, const char *field_name,
  145. const char *clock_name)
  146. {
  147. u32 limit = BITS_PER_BYTE * sizeof(u32) - 1;
  148. if (bit_posn > limit) {
  149. pr_err("%s: bad %s bit for %s (%u > %u)\n", __func__,
  150. field_name, clock_name, bit_posn, limit);
  151. return false;
  152. }
  153. return true;
  154. }
  155. /*
  156. * A bitfield must be at least 1 bit wide. Both the low-order and
  157. * high-order bits must lie within a 32-bit register. We require
  158. * fields to be less than 32 bits wide, mainly because we use
  159. * shifting to produce field masks, and shifting a full word width
  160. * is not well-defined by the C standard.
  161. */
  162. static bool bitfield_valid(u32 shift, u32 width, const char *field_name,
  163. const char *clock_name)
  164. {
  165. u32 limit = BITS_PER_BYTE * sizeof(u32);
  166. if (!width) {
  167. pr_err("%s: bad %s field width 0 for %s\n", __func__,
  168. field_name, clock_name);
  169. return false;
  170. }
  171. if (shift + width > limit) {
  172. pr_err("%s: bad %s for %s (%u + %u > %u)\n", __func__,
  173. field_name, clock_name, shift, width, limit);
  174. return false;
  175. }
  176. return true;
  177. }
  178. static bool
  179. ccu_policy_valid(struct ccu_policy *ccu_policy, const char *ccu_name)
  180. {
  181. struct bcm_lvm_en *enable = &ccu_policy->enable;
  182. struct bcm_policy_ctl *control;
  183. if (!bit_posn_valid(enable->bit, "policy enable", ccu_name))
  184. return false;
  185. control = &ccu_policy->control;
  186. if (!bit_posn_valid(control->go_bit, "policy control GO", ccu_name))
  187. return false;
  188. if (!bit_posn_valid(control->atl_bit, "policy control ATL", ccu_name))
  189. return false;
  190. if (!bit_posn_valid(control->ac_bit, "policy control AC", ccu_name))
  191. return false;
  192. return true;
  193. }
  194. static bool policy_valid(struct bcm_clk_policy *policy, const char *clock_name)
  195. {
  196. if (!bit_posn_valid(policy->bit, "policy", clock_name))
  197. return false;
  198. return true;
  199. }
  200. /*
  201. * All gates, if defined, have a status bit, and for hardware-only
  202. * gates, that's it. Gates that can be software controlled also
  203. * have an enable bit. And a gate that can be hardware or software
  204. * controlled will have a hardware/software select bit.
  205. */
  206. static bool gate_valid(struct bcm_clk_gate *gate, const char *field_name,
  207. const char *clock_name)
  208. {
  209. if (!bit_posn_valid(gate->status_bit, "gate status", clock_name))
  210. return false;
  211. if (gate_is_sw_controllable(gate)) {
  212. if (!bit_posn_valid(gate->en_bit, "gate enable", clock_name))
  213. return false;
  214. if (gate_is_hw_controllable(gate)) {
  215. if (!bit_posn_valid(gate->hw_sw_sel_bit,
  216. "gate hw/sw select",
  217. clock_name))
  218. return false;
  219. }
  220. } else {
  221. BUG_ON(!gate_is_hw_controllable(gate));
  222. }
  223. return true;
  224. }
  225. static bool hyst_valid(struct bcm_clk_hyst *hyst, const char *clock_name)
  226. {
  227. if (!bit_posn_valid(hyst->en_bit, "hysteresis enable", clock_name))
  228. return false;
  229. if (!bit_posn_valid(hyst->val_bit, "hysteresis value", clock_name))
  230. return false;
  231. return true;
  232. }
  233. /*
  234. * A selector bitfield must be valid. Its parent_sel array must
  235. * also be reasonable for the field.
  236. */
  237. static bool sel_valid(struct bcm_clk_sel *sel, const char *field_name,
  238. const char *clock_name)
  239. {
  240. if (!bitfield_valid(sel->shift, sel->width, field_name, clock_name))
  241. return false;
  242. if (sel->parent_count) {
  243. u32 max_sel;
  244. u32 limit;
  245. /*
  246. * Make sure the selector field can hold all the
  247. * selector values we expect to be able to use. A
  248. * clock only needs to have a selector defined if it
  249. * has more than one parent. And in that case the
  250. * highest selector value will be in the last entry
  251. * in the array.
  252. */
  253. max_sel = sel->parent_sel[sel->parent_count - 1];
  254. limit = (1 << sel->width) - 1;
  255. if (max_sel > limit) {
  256. pr_err("%s: bad selector for %s "
  257. "(%u needs > %u bits)\n",
  258. __func__, clock_name, max_sel,
  259. sel->width);
  260. return false;
  261. }
  262. } else {
  263. pr_warn("%s: ignoring selector for %s (no parents)\n",
  264. __func__, clock_name);
  265. selector_clear_exists(sel);
  266. kfree(sel->parent_sel);
  267. sel->parent_sel = NULL;
  268. }
  269. return true;
  270. }
  271. /*
  272. * A fixed divider just needs to be non-zero. A variable divider
  273. * has to have a valid divider bitfield, and if it has a fraction,
  274. * the width of the fraction must not be no more than the width of
  275. * the divider as a whole.
  276. */
  277. static bool div_valid(struct bcm_clk_div *div, const char *field_name,
  278. const char *clock_name)
  279. {
  280. if (divider_is_fixed(div)) {
  281. /* Any fixed divider value but 0 is OK */
  282. if (div->u.fixed == 0) {
  283. pr_err("%s: bad %s fixed value 0 for %s\n", __func__,
  284. field_name, clock_name);
  285. return false;
  286. }
  287. return true;
  288. }
  289. if (!bitfield_valid(div->u.s.shift, div->u.s.width,
  290. field_name, clock_name))
  291. return false;
  292. if (divider_has_fraction(div))
  293. if (div->u.s.frac_width > div->u.s.width) {
  294. pr_warn("%s: bad %s fraction width for %s (%u > %u)\n",
  295. __func__, field_name, clock_name,
  296. div->u.s.frac_width, div->u.s.width);
  297. return false;
  298. }
  299. return true;
  300. }
  301. /*
  302. * If a clock has two dividers, the combined number of fractional
  303. * bits must be representable in a 32-bit unsigned value. This
  304. * is because we scale up a dividend using both dividers before
  305. * dividing to improve accuracy, and we need to avoid overflow.
  306. */
  307. static bool kona_dividers_valid(struct kona_clk *bcm_clk)
  308. {
  309. struct peri_clk_data *peri = bcm_clk->u.peri;
  310. struct bcm_clk_div *div;
  311. struct bcm_clk_div *pre_div;
  312. u32 limit;
  313. BUG_ON(bcm_clk->type != bcm_clk_peri);
  314. if (!divider_exists(&peri->div) || !divider_exists(&peri->pre_div))
  315. return true;
  316. div = &peri->div;
  317. pre_div = &peri->pre_div;
  318. if (divider_is_fixed(div) || divider_is_fixed(pre_div))
  319. return true;
  320. limit = BITS_PER_BYTE * sizeof(u32);
  321. return div->u.s.frac_width + pre_div->u.s.frac_width <= limit;
  322. }
  323. /* A trigger just needs to represent a valid bit position */
  324. static bool trig_valid(struct bcm_clk_trig *trig, const char *field_name,
  325. const char *clock_name)
  326. {
  327. return bit_posn_valid(trig->bit, field_name, clock_name);
  328. }
  329. /* Determine whether the set of peripheral clock registers are valid. */
  330. static bool
  331. peri_clk_data_valid(struct kona_clk *bcm_clk)
  332. {
  333. struct peri_clk_data *peri;
  334. struct bcm_clk_policy *policy;
  335. struct bcm_clk_gate *gate;
  336. struct bcm_clk_hyst *hyst;
  337. struct bcm_clk_sel *sel;
  338. struct bcm_clk_div *div;
  339. struct bcm_clk_div *pre_div;
  340. struct bcm_clk_trig *trig;
  341. const char *name;
  342. BUG_ON(bcm_clk->type != bcm_clk_peri);
  343. /*
  344. * First validate register offsets. This is the only place
  345. * where we need something from the ccu, so we do these
  346. * together.
  347. */
  348. if (!peri_clk_data_offsets_valid(bcm_clk))
  349. return false;
  350. peri = bcm_clk->u.peri;
  351. name = bcm_clk->init_data.name;
  352. policy = &peri->policy;
  353. if (policy_exists(policy) && !policy_valid(policy, name))
  354. return false;
  355. gate = &peri->gate;
  356. if (gate_exists(gate) && !gate_valid(gate, "gate", name))
  357. return false;
  358. hyst = &peri->hyst;
  359. if (hyst_exists(hyst) && !hyst_valid(hyst, name))
  360. return false;
  361. sel = &peri->sel;
  362. if (selector_exists(sel)) {
  363. if (!sel_valid(sel, "selector", name))
  364. return false;
  365. } else if (sel->parent_count > 1) {
  366. pr_err("%s: multiple parents but no selector for %s\n",
  367. __func__, name);
  368. return false;
  369. }
  370. div = &peri->div;
  371. pre_div = &peri->pre_div;
  372. if (divider_exists(div)) {
  373. if (!div_valid(div, "divider", name))
  374. return false;
  375. if (divider_exists(pre_div))
  376. if (!div_valid(pre_div, "pre-divider", name))
  377. return false;
  378. } else if (divider_exists(pre_div)) {
  379. pr_err("%s: pre-divider but no divider for %s\n", __func__,
  380. name);
  381. return false;
  382. }
  383. trig = &peri->trig;
  384. if (trigger_exists(trig)) {
  385. if (!trig_valid(trig, "trigger", name))
  386. return false;
  387. if (trigger_exists(&peri->pre_trig)) {
  388. if (!trig_valid(trig, "pre-trigger", name)) {
  389. return false;
  390. }
  391. }
  392. if (!clk_requires_trigger(bcm_clk)) {
  393. pr_warn("%s: ignoring trigger for %s (not needed)\n",
  394. __func__, name);
  395. trigger_clear_exists(trig);
  396. }
  397. } else if (trigger_exists(&peri->pre_trig)) {
  398. pr_err("%s: pre-trigger but no trigger for %s\n", __func__,
  399. name);
  400. return false;
  401. } else if (clk_requires_trigger(bcm_clk)) {
  402. pr_err("%s: required trigger missing for %s\n", __func__,
  403. name);
  404. return false;
  405. }
  406. return kona_dividers_valid(bcm_clk);
  407. }
  408. static bool kona_clk_valid(struct kona_clk *bcm_clk)
  409. {
  410. switch (bcm_clk->type) {
  411. case bcm_clk_peri:
  412. if (!peri_clk_data_valid(bcm_clk))
  413. return false;
  414. break;
  415. default:
  416. pr_err("%s: unrecognized clock type (%d)\n", __func__,
  417. (int)bcm_clk->type);
  418. return false;
  419. }
  420. return true;
  421. }
  422. /*
  423. * Scan an array of parent clock names to determine whether there
  424. * are any entries containing BAD_CLK_NAME. Such entries are
  425. * placeholders for non-supported clocks. Keep track of the
  426. * position of each clock name in the original array.
  427. *
  428. * Allocates an array of pointers to hold the names of all
  429. * non-null entries in the original array, and returns a pointer to
  430. * that array in *names. This will be used for registering the
  431. * clock with the common clock code. On successful return,
  432. * *count indicates how many entries are in that names array.
  433. *
  434. * If there is more than one entry in the resulting names array,
  435. * another array is allocated to record the parent selector value
  436. * for each (defined) parent clock. This is the value that
  437. * represents this parent clock in the clock's source selector
  438. * register. The position of the clock in the original parent array
  439. * defines that selector value. The number of entries in this array
  440. * is the same as the number of entries in the parent names array.
  441. *
  442. * The array of selector values is returned. If the clock has no
  443. * parents, no selector is required and a null pointer is returned.
  444. *
  445. * Returns a null pointer if the clock names array supplied was
  446. * null. (This is not an error.)
  447. *
  448. * Returns a pointer-coded error if an error occurs.
  449. */
  450. static u32 *parent_process(const char *clocks[],
  451. u32 *count, const char ***names)
  452. {
  453. static const char **parent_names;
  454. static u32 *parent_sel;
  455. const char **clock;
  456. u32 parent_count;
  457. u32 bad_count = 0;
  458. u32 orig_count;
  459. u32 i;
  460. u32 j;
  461. *count = 0; /* In case of early return */
  462. *names = NULL;
  463. if (!clocks)
  464. return NULL;
  465. /*
  466. * Count the number of names in the null-terminated array,
  467. * and find out how many of those are actually clock names.
  468. */
  469. for (clock = clocks; *clock; clock++)
  470. if (*clock == BAD_CLK_NAME)
  471. bad_count++;
  472. orig_count = (u32)(clock - clocks);
  473. parent_count = orig_count - bad_count;
  474. /* If all clocks are unsupported, we treat it as no clock */
  475. if (!parent_count)
  476. return NULL;
  477. /* Avoid exceeding our parent clock limit */
  478. if (parent_count > PARENT_COUNT_MAX) {
  479. pr_err("%s: too many parents (%u > %u)\n", __func__,
  480. parent_count, PARENT_COUNT_MAX);
  481. return ERR_PTR(-EINVAL);
  482. }
  483. /*
  484. * There is one parent name for each defined parent clock.
  485. * We also maintain an array containing the selector value
  486. * for each defined clock. If there's only one clock, the
  487. * selector is not required, but we allocate space for the
  488. * array anyway to keep things simple.
  489. */
  490. parent_names = kmalloc_array(parent_count, sizeof(*parent_names),
  491. GFP_KERNEL);
  492. if (!parent_names)
  493. return ERR_PTR(-ENOMEM);
  494. /* There is at least one parent, so allocate a selector array */
  495. parent_sel = kmalloc_array(parent_count, sizeof(*parent_sel),
  496. GFP_KERNEL);
  497. if (!parent_sel) {
  498. kfree(parent_names);
  499. return ERR_PTR(-ENOMEM);
  500. }
  501. /* Now fill in the parent names and selector arrays */
  502. for (i = 0, j = 0; i < orig_count; i++) {
  503. if (clocks[i] != BAD_CLK_NAME) {
  504. parent_names[j] = clocks[i];
  505. parent_sel[j] = i;
  506. j++;
  507. }
  508. }
  509. *names = parent_names;
  510. *count = parent_count;
  511. return parent_sel;
  512. }
  513. static int
  514. clk_sel_setup(const char **clocks, struct bcm_clk_sel *sel,
  515. struct clk_init_data *init_data)
  516. {
  517. const char **parent_names = NULL;
  518. u32 parent_count = 0;
  519. u32 *parent_sel;
  520. /*
  521. * If a peripheral clock has multiple parents, the value
  522. * used by the hardware to select that parent is represented
  523. * by the parent clock's position in the "clocks" list. Some
  524. * values don't have defined or supported clocks; these will
  525. * have BAD_CLK_NAME entries in the parents[] array. The
  526. * list is terminated by a NULL entry.
  527. *
  528. * We need to supply (only) the names of defined parent
  529. * clocks when registering a clock though, so we use an
  530. * array of parent selector values to map between the
  531. * indexes the common clock code uses and the selector
  532. * values we need.
  533. */
  534. parent_sel = parent_process(clocks, &parent_count, &parent_names);
  535. if (IS_ERR(parent_sel)) {
  536. int ret = PTR_ERR(parent_sel);
  537. pr_err("%s: error processing parent clocks for %s (%d)\n",
  538. __func__, init_data->name, ret);
  539. return ret;
  540. }
  541. init_data->parent_names = parent_names;
  542. init_data->num_parents = parent_count;
  543. sel->parent_count = parent_count;
  544. sel->parent_sel = parent_sel;
  545. return 0;
  546. }
  547. static void clk_sel_teardown(struct bcm_clk_sel *sel,
  548. struct clk_init_data *init_data)
  549. {
  550. kfree(sel->parent_sel);
  551. sel->parent_sel = NULL;
  552. sel->parent_count = 0;
  553. init_data->num_parents = 0;
  554. kfree(init_data->parent_names);
  555. init_data->parent_names = NULL;
  556. }
  557. static void peri_clk_teardown(struct peri_clk_data *data,
  558. struct clk_init_data *init_data)
  559. {
  560. clk_sel_teardown(&data->sel, init_data);
  561. }
  562. /*
  563. * Caller is responsible for freeing the parent_names[] and
  564. * parent_sel[] arrays in the peripheral clock's "data" structure
  565. * that can be assigned if the clock has one or more parent clocks
  566. * associated with it.
  567. */
  568. static int
  569. peri_clk_setup(struct peri_clk_data *data, struct clk_init_data *init_data)
  570. {
  571. init_data->flags = CLK_IGNORE_UNUSED;
  572. return clk_sel_setup(data->clocks, &data->sel, init_data);
  573. }
  574. static void bcm_clk_teardown(struct kona_clk *bcm_clk)
  575. {
  576. switch (bcm_clk->type) {
  577. case bcm_clk_peri:
  578. peri_clk_teardown(bcm_clk->u.data, &bcm_clk->init_data);
  579. break;
  580. default:
  581. break;
  582. }
  583. bcm_clk->u.data = NULL;
  584. bcm_clk->type = bcm_clk_none;
  585. }
  586. static void kona_clk_teardown(struct clk_hw *hw)
  587. {
  588. struct kona_clk *bcm_clk;
  589. if (!hw)
  590. return;
  591. clk_hw_unregister(hw);
  592. bcm_clk = to_kona_clk(hw);
  593. bcm_clk_teardown(bcm_clk);
  594. }
  595. static int kona_clk_setup(struct kona_clk *bcm_clk)
  596. {
  597. int ret;
  598. struct clk_init_data *init_data = &bcm_clk->init_data;
  599. switch (bcm_clk->type) {
  600. case bcm_clk_peri:
  601. ret = peri_clk_setup(bcm_clk->u.data, init_data);
  602. if (ret)
  603. return ret;
  604. break;
  605. default:
  606. pr_err("%s: clock type %d invalid for %s\n", __func__,
  607. (int)bcm_clk->type, init_data->name);
  608. return -EINVAL;
  609. }
  610. /* Make sure everything makes sense before we set it up */
  611. if (!kona_clk_valid(bcm_clk)) {
  612. pr_err("%s: clock data invalid for %s\n", __func__,
  613. init_data->name);
  614. ret = -EINVAL;
  615. goto out_teardown;
  616. }
  617. bcm_clk->hw.init = init_data;
  618. ret = clk_hw_register(NULL, &bcm_clk->hw);
  619. if (ret) {
  620. pr_err("%s: error registering clock %s (%d)\n", __func__,
  621. init_data->name, ret);
  622. goto out_teardown;
  623. }
  624. return 0;
  625. out_teardown:
  626. bcm_clk_teardown(bcm_clk);
  627. return ret;
  628. }
  629. static void ccu_clks_teardown(struct ccu_data *ccu)
  630. {
  631. u32 i;
  632. for (i = 0; i < ccu->clk_num; i++)
  633. kona_clk_teardown(&ccu->kona_clks[i].hw);
  634. }
  635. static void kona_ccu_teardown(struct ccu_data *ccu)
  636. {
  637. if (!ccu->base)
  638. return;
  639. of_clk_del_provider(ccu->node); /* safe if never added */
  640. ccu_clks_teardown(ccu);
  641. of_node_put(ccu->node);
  642. ccu->node = NULL;
  643. iounmap(ccu->base);
  644. ccu->base = NULL;
  645. }
  646. static bool ccu_data_valid(struct ccu_data *ccu)
  647. {
  648. struct ccu_policy *ccu_policy;
  649. if (!ccu_data_offsets_valid(ccu))
  650. return false;
  651. ccu_policy = &ccu->policy;
  652. if (ccu_policy_exists(ccu_policy))
  653. if (!ccu_policy_valid(ccu_policy, ccu->name))
  654. return false;
  655. return true;
  656. }
  657. static struct clk_hw *
  658. of_clk_kona_onecell_get(struct of_phandle_args *clkspec, void *data)
  659. {
  660. struct ccu_data *ccu = data;
  661. unsigned int idx = clkspec->args[0];
  662. if (idx >= ccu->clk_num) {
  663. pr_err("%s: invalid index %u\n", __func__, idx);
  664. return ERR_PTR(-EINVAL);
  665. }
  666. return &ccu->kona_clks[idx].hw;
  667. }
  668. /*
  669. * Set up a CCU. Call the provided ccu_clks_setup callback to
  670. * initialize the array of clocks provided by the CCU.
  671. */
  672. void __init kona_dt_ccu_setup(struct ccu_data *ccu,
  673. struct device_node *node)
  674. {
  675. struct resource res = { 0 };
  676. resource_size_t range;
  677. unsigned int i;
  678. int ret;
  679. ret = of_address_to_resource(node, 0, &res);
  680. if (ret) {
  681. pr_err("%s: no valid CCU registers found for %pOFn\n", __func__,
  682. node);
  683. goto out_err;
  684. }
  685. range = resource_size(&res);
  686. if (range > (resource_size_t)U32_MAX) {
  687. pr_err("%s: address range too large for %pOFn\n", __func__,
  688. node);
  689. goto out_err;
  690. }
  691. ccu->range = (u32)range;
  692. if (!ccu_data_valid(ccu)) {
  693. pr_err("%s: ccu data not valid for %pOFn\n", __func__, node);
  694. goto out_err;
  695. }
  696. ccu->base = ioremap(res.start, ccu->range);
  697. if (!ccu->base) {
  698. pr_err("%s: unable to map CCU registers for %pOFn\n", __func__,
  699. node);
  700. goto out_err;
  701. }
  702. ccu->node = of_node_get(node);
  703. /*
  704. * Set up each defined kona clock and save the result in
  705. * the clock framework clock array (in ccu->data). Then
  706. * register as a provider for these clocks.
  707. */
  708. for (i = 0; i < ccu->clk_num; i++) {
  709. if (!ccu->kona_clks[i].ccu)
  710. continue;
  711. kona_clk_setup(&ccu->kona_clks[i]);
  712. }
  713. ret = of_clk_add_hw_provider(node, of_clk_kona_onecell_get, ccu);
  714. if (ret) {
  715. pr_err("%s: error adding ccu %pOFn as provider (%d)\n", __func__,
  716. node, ret);
  717. goto out_err;
  718. }
  719. if (!kona_ccu_init(ccu))
  720. pr_err("Broadcom %pOFn initialization had errors\n", node);
  721. return;
  722. out_err:
  723. kona_ccu_teardown(ccu);
  724. pr_err("Broadcom %pOFn setup aborted\n", node);
  725. }