clk-kona-setup.c 22 KB

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