clk-kona-setup.c 21 KB

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