clk-kona-setup.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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(parent_count * sizeof(parent_names), GFP_KERNEL);
  499. if (!parent_names) {
  500. pr_err("%s: error allocating %u parent names\n", __func__,
  501. parent_count);
  502. return ERR_PTR(-ENOMEM);
  503. }
  504. /* There is at least one parent, so allocate a selector array */
  505. parent_sel = kmalloc(parent_count * sizeof(*parent_sel), GFP_KERNEL);
  506. if (!parent_sel) {
  507. pr_err("%s: error allocating %u parent selectors\n", __func__,
  508. parent_count);
  509. kfree(parent_names);
  510. return ERR_PTR(-ENOMEM);
  511. }
  512. /* Now fill in the parent names and selector arrays */
  513. for (i = 0, j = 0; i < orig_count; i++) {
  514. if (clocks[i] != BAD_CLK_NAME) {
  515. parent_names[j] = clocks[i];
  516. parent_sel[j] = i;
  517. j++;
  518. }
  519. }
  520. *names = parent_names;
  521. *count = parent_count;
  522. return parent_sel;
  523. }
  524. static int
  525. clk_sel_setup(const char **clocks, struct bcm_clk_sel *sel,
  526. struct clk_init_data *init_data)
  527. {
  528. const char **parent_names = NULL;
  529. u32 parent_count = 0;
  530. u32 *parent_sel;
  531. /*
  532. * If a peripheral clock has multiple parents, the value
  533. * used by the hardware to select that parent is represented
  534. * by the parent clock's position in the "clocks" list. Some
  535. * values don't have defined or supported clocks; these will
  536. * have BAD_CLK_NAME entries in the parents[] array. The
  537. * list is terminated by a NULL entry.
  538. *
  539. * We need to supply (only) the names of defined parent
  540. * clocks when registering a clock though, so we use an
  541. * array of parent selector values to map between the
  542. * indexes the common clock code uses and the selector
  543. * values we need.
  544. */
  545. parent_sel = parent_process(clocks, &parent_count, &parent_names);
  546. if (IS_ERR(parent_sel)) {
  547. int ret = PTR_ERR(parent_sel);
  548. pr_err("%s: error processing parent clocks for %s (%d)\n",
  549. __func__, init_data->name, ret);
  550. return ret;
  551. }
  552. init_data->parent_names = parent_names;
  553. init_data->num_parents = parent_count;
  554. sel->parent_count = parent_count;
  555. sel->parent_sel = parent_sel;
  556. return 0;
  557. }
  558. static void clk_sel_teardown(struct bcm_clk_sel *sel,
  559. struct clk_init_data *init_data)
  560. {
  561. kfree(sel->parent_sel);
  562. sel->parent_sel = NULL;
  563. sel->parent_count = 0;
  564. init_data->num_parents = 0;
  565. kfree(init_data->parent_names);
  566. init_data->parent_names = NULL;
  567. }
  568. static void peri_clk_teardown(struct peri_clk_data *data,
  569. struct clk_init_data *init_data)
  570. {
  571. clk_sel_teardown(&data->sel, init_data);
  572. }
  573. /*
  574. * Caller is responsible for freeing the parent_names[] and
  575. * parent_sel[] arrays in the peripheral clock's "data" structure
  576. * that can be assigned if the clock has one or more parent clocks
  577. * associated with it.
  578. */
  579. static int
  580. peri_clk_setup(struct peri_clk_data *data, struct clk_init_data *init_data)
  581. {
  582. init_data->flags = CLK_IGNORE_UNUSED;
  583. return clk_sel_setup(data->clocks, &data->sel, init_data);
  584. }
  585. static void bcm_clk_teardown(struct kona_clk *bcm_clk)
  586. {
  587. switch (bcm_clk->type) {
  588. case bcm_clk_peri:
  589. peri_clk_teardown(bcm_clk->u.data, &bcm_clk->init_data);
  590. break;
  591. default:
  592. break;
  593. }
  594. bcm_clk->u.data = NULL;
  595. bcm_clk->type = bcm_clk_none;
  596. }
  597. static void kona_clk_teardown(struct clk *clk)
  598. {
  599. struct clk_hw *hw;
  600. struct kona_clk *bcm_clk;
  601. if (!clk)
  602. return;
  603. hw = __clk_get_hw(clk);
  604. if (!hw) {
  605. pr_err("%s: clk %p has null hw pointer\n", __func__, clk);
  606. return;
  607. }
  608. clk_unregister(clk);
  609. bcm_clk = to_kona_clk(hw);
  610. bcm_clk_teardown(bcm_clk);
  611. }
  612. struct clk *kona_clk_setup(struct kona_clk *bcm_clk)
  613. {
  614. struct clk_init_data *init_data = &bcm_clk->init_data;
  615. struct clk *clk = NULL;
  616. switch (bcm_clk->type) {
  617. case bcm_clk_peri:
  618. if (peri_clk_setup(bcm_clk->u.data, init_data))
  619. return NULL;
  620. break;
  621. default:
  622. pr_err("%s: clock type %d invalid for %s\n", __func__,
  623. (int)bcm_clk->type, init_data->name);
  624. return NULL;
  625. }
  626. /* Make sure everything makes sense before we set it up */
  627. if (!kona_clk_valid(bcm_clk)) {
  628. pr_err("%s: clock data invalid for %s\n", __func__,
  629. init_data->name);
  630. goto out_teardown;
  631. }
  632. bcm_clk->hw.init = init_data;
  633. clk = clk_register(NULL, &bcm_clk->hw);
  634. if (IS_ERR(clk)) {
  635. pr_err("%s: error registering clock %s (%ld)\n", __func__,
  636. init_data->name, PTR_ERR(clk));
  637. goto out_teardown;
  638. }
  639. BUG_ON(!clk);
  640. return clk;
  641. out_teardown:
  642. bcm_clk_teardown(bcm_clk);
  643. return NULL;
  644. }
  645. static void ccu_clks_teardown(struct ccu_data *ccu)
  646. {
  647. u32 i;
  648. for (i = 0; i < ccu->clk_data.clk_num; i++)
  649. kona_clk_teardown(ccu->clk_data.clks[i]);
  650. kfree(ccu->clk_data.clks);
  651. }
  652. static void kona_ccu_teardown(struct ccu_data *ccu)
  653. {
  654. kfree(ccu->clk_data.clks);
  655. ccu->clk_data.clks = NULL;
  656. if (!ccu->base)
  657. return;
  658. of_clk_del_provider(ccu->node); /* safe if never added */
  659. ccu_clks_teardown(ccu);
  660. of_node_put(ccu->node);
  661. ccu->node = NULL;
  662. iounmap(ccu->base);
  663. ccu->base = NULL;
  664. }
  665. static bool ccu_data_valid(struct ccu_data *ccu)
  666. {
  667. struct ccu_policy *ccu_policy;
  668. if (!ccu_data_offsets_valid(ccu))
  669. return false;
  670. ccu_policy = &ccu->policy;
  671. if (ccu_policy_exists(ccu_policy))
  672. if (!ccu_policy_valid(ccu_policy, ccu->name))
  673. return false;
  674. return true;
  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. if (ccu->clk_data.clk_num) {
  688. size_t size;
  689. size = ccu->clk_data.clk_num * sizeof(*ccu->clk_data.clks);
  690. ccu->clk_data.clks = kzalloc(size, GFP_KERNEL);
  691. if (!ccu->clk_data.clks) {
  692. pr_err("%s: unable to allocate %u clocks for %s\n",
  693. __func__, ccu->clk_data.clk_num, node->name);
  694. return;
  695. }
  696. }
  697. ret = of_address_to_resource(node, 0, &res);
  698. if (ret) {
  699. pr_err("%s: no valid CCU registers found for %s\n", __func__,
  700. node->name);
  701. goto out_err;
  702. }
  703. range = resource_size(&res);
  704. if (range > (resource_size_t)U32_MAX) {
  705. pr_err("%s: address range too large for %s\n", __func__,
  706. node->name);
  707. goto out_err;
  708. }
  709. ccu->range = (u32)range;
  710. if (!ccu_data_valid(ccu)) {
  711. pr_err("%s: ccu data not valid for %s\n", __func__, node->name);
  712. goto out_err;
  713. }
  714. ccu->base = ioremap(res.start, ccu->range);
  715. if (!ccu->base) {
  716. pr_err("%s: unable to map CCU registers for %s\n", __func__,
  717. node->name);
  718. goto out_err;
  719. }
  720. ccu->node = of_node_get(node);
  721. /*
  722. * Set up each defined kona clock and save the result in
  723. * the clock framework clock array (in ccu->data). Then
  724. * register as a provider for these clocks.
  725. */
  726. for (i = 0; i < ccu->clk_data.clk_num; i++) {
  727. if (!ccu->kona_clks[i].ccu)
  728. continue;
  729. ccu->clk_data.clks[i] = kona_clk_setup(&ccu->kona_clks[i]);
  730. }
  731. ret = of_clk_add_provider(node, of_clk_src_onecell_get, &ccu->clk_data);
  732. if (ret) {
  733. pr_err("%s: error adding ccu %s as provider (%d)\n", __func__,
  734. node->name, ret);
  735. goto out_err;
  736. }
  737. if (!kona_ccu_init(ccu))
  738. pr_err("Broadcom %s initialization had errors\n", node->name);
  739. return;
  740. out_err:
  741. kona_ccu_teardown(ccu);
  742. pr_err("Broadcom %s setup aborted\n", node->name);
  743. }