clk-kona.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 "clk-kona.h"
  15. #include <linux/delay.h>
  16. /*
  17. * "Policies" affect the frequencies of bus clocks provided by a
  18. * CCU. (I believe these polices are named "Deep Sleep", "Economy",
  19. * "Normal", and "Turbo".) A lower policy number has lower power
  20. * consumption, and policy 2 is the default.
  21. */
  22. #define CCU_POLICY_COUNT 4
  23. #define CCU_ACCESS_PASSWORD 0xA5A500
  24. #define CLK_GATE_DELAY_LOOP 2000
  25. /* Bitfield operations */
  26. /* Produces a mask of set bits covering a range of a 32-bit value */
  27. static inline u32 bitfield_mask(u32 shift, u32 width)
  28. {
  29. return ((1 << width) - 1) << shift;
  30. }
  31. /* Extract the value of a bitfield found within a given register value */
  32. static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
  33. {
  34. return (reg_val & bitfield_mask(shift, width)) >> shift;
  35. }
  36. /* Replace the value of a bitfield found within a given register value */
  37. static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
  38. {
  39. u32 mask = bitfield_mask(shift, width);
  40. return (reg_val & ~mask) | (val << shift);
  41. }
  42. /* Divider and scaling helpers */
  43. /*
  44. * Implement DIV_ROUND_CLOSEST() for 64-bit dividend and both values
  45. * unsigned. Note that unlike do_div(), the remainder is discarded
  46. * and the return value is the quotient (not the remainder).
  47. */
  48. u64 do_div_round_closest(u64 dividend, unsigned long divisor)
  49. {
  50. u64 result;
  51. result = dividend + ((u64)divisor >> 1);
  52. (void)do_div(result, divisor);
  53. return result;
  54. }
  55. /* Convert a divider into the scaled divisor value it represents. */
  56. static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
  57. {
  58. return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
  59. }
  60. /*
  61. * Build a scaled divider value as close as possible to the
  62. * given whole part (div_value) and fractional part (expressed
  63. * in billionths).
  64. */
  65. u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
  66. {
  67. u64 combined;
  68. BUG_ON(!div_value);
  69. BUG_ON(billionths >= BILLION);
  70. combined = (u64)div_value * BILLION + billionths;
  71. combined <<= div->u.s.frac_width;
  72. return do_div_round_closest(combined, BILLION);
  73. }
  74. /* The scaled minimum divisor representable by a divider */
  75. static inline u64
  76. scaled_div_min(struct bcm_clk_div *div)
  77. {
  78. if (divider_is_fixed(div))
  79. return (u64)div->u.fixed;
  80. return scaled_div_value(div, 0);
  81. }
  82. /* The scaled maximum divisor representable by a divider */
  83. u64 scaled_div_max(struct bcm_clk_div *div)
  84. {
  85. u32 reg_div;
  86. if (divider_is_fixed(div))
  87. return (u64)div->u.fixed;
  88. reg_div = ((u32)1 << div->u.s.width) - 1;
  89. return scaled_div_value(div, reg_div);
  90. }
  91. /*
  92. * Convert a scaled divisor into its divider representation as
  93. * stored in a divider register field.
  94. */
  95. static inline u32
  96. divider(struct bcm_clk_div *div, u64 scaled_div)
  97. {
  98. BUG_ON(scaled_div < scaled_div_min(div));
  99. BUG_ON(scaled_div > scaled_div_max(div));
  100. return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
  101. }
  102. /* Return a rate scaled for use when dividing by a scaled divisor. */
  103. static inline u64
  104. scale_rate(struct bcm_clk_div *div, u32 rate)
  105. {
  106. if (divider_is_fixed(div))
  107. return (u64)rate;
  108. return (u64)rate << div->u.s.frac_width;
  109. }
  110. /* CCU access */
  111. /* Read a 32-bit register value from a CCU's address space. */
  112. static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
  113. {
  114. return readl(ccu->base + reg_offset);
  115. }
  116. /* Write a 32-bit register value into a CCU's address space. */
  117. static inline void
  118. __ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
  119. {
  120. writel(reg_val, ccu->base + reg_offset);
  121. }
  122. static inline unsigned long ccu_lock(struct ccu_data *ccu)
  123. {
  124. unsigned long flags;
  125. spin_lock_irqsave(&ccu->lock, flags);
  126. return flags;
  127. }
  128. static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
  129. {
  130. spin_unlock_irqrestore(&ccu->lock, flags);
  131. }
  132. /*
  133. * Enable/disable write access to CCU protected registers. The
  134. * WR_ACCESS register for all CCUs is at offset 0.
  135. */
  136. static inline void __ccu_write_enable(struct ccu_data *ccu)
  137. {
  138. if (ccu->write_enabled) {
  139. pr_err("%s: access already enabled for %s\n", __func__,
  140. ccu->name);
  141. return;
  142. }
  143. ccu->write_enabled = true;
  144. __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
  145. }
  146. static inline void __ccu_write_disable(struct ccu_data *ccu)
  147. {
  148. if (!ccu->write_enabled) {
  149. pr_err("%s: access wasn't enabled for %s\n", __func__,
  150. ccu->name);
  151. return;
  152. }
  153. __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
  154. ccu->write_enabled = false;
  155. }
  156. /*
  157. * Poll a register in a CCU's address space, returning when the
  158. * specified bit in that register's value is set (or clear). Delay
  159. * a microsecond after each read of the register. Returns true if
  160. * successful, or false if we gave up trying.
  161. *
  162. * Caller must ensure the CCU lock is held.
  163. */
  164. static inline bool
  165. __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
  166. {
  167. unsigned int tries;
  168. u32 bit_mask = 1 << bit;
  169. for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
  170. u32 val;
  171. bool bit_val;
  172. val = __ccu_read(ccu, reg_offset);
  173. bit_val = (val & bit_mask) != 0;
  174. if (bit_val == want)
  175. return true;
  176. udelay(1);
  177. }
  178. pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
  179. ccu->name, reg_offset, bit, want ? "set" : "clear");
  180. return false;
  181. }
  182. /* Policy operations */
  183. static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
  184. {
  185. struct bcm_policy_ctl *control = &ccu->policy.control;
  186. u32 offset;
  187. u32 go_bit;
  188. u32 mask;
  189. bool ret;
  190. /* If we don't need to control policy for this CCU, we're done. */
  191. if (!policy_ctl_exists(control))
  192. return true;
  193. offset = control->offset;
  194. go_bit = control->go_bit;
  195. /* Ensure we're not busy before we start */
  196. ret = __ccu_wait_bit(ccu, offset, go_bit, false);
  197. if (!ret) {
  198. pr_err("%s: ccu %s policy engine wouldn't go idle\n",
  199. __func__, ccu->name);
  200. return false;
  201. }
  202. /*
  203. * If it's a synchronous request, we'll wait for the voltage
  204. * and frequency of the active load to stabilize before
  205. * returning. To do this we select the active load by
  206. * setting the ATL bit.
  207. *
  208. * An asynchronous request instead ramps the voltage in the
  209. * background, and when that process stabilizes, the target
  210. * load is copied to the active load and the CCU frequency
  211. * is switched. We do this by selecting the target load
  212. * (ATL bit clear) and setting the request auto-copy (AC bit
  213. * set).
  214. *
  215. * Note, we do NOT read-modify-write this register.
  216. */
  217. mask = (u32)1 << go_bit;
  218. if (sync)
  219. mask |= 1 << control->atl_bit;
  220. else
  221. mask |= 1 << control->ac_bit;
  222. __ccu_write(ccu, offset, mask);
  223. /* Wait for indication that operation is complete. */
  224. ret = __ccu_wait_bit(ccu, offset, go_bit, false);
  225. if (!ret)
  226. pr_err("%s: ccu %s policy engine never started\n",
  227. __func__, ccu->name);
  228. return ret;
  229. }
  230. static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
  231. {
  232. struct bcm_lvm_en *enable = &ccu->policy.enable;
  233. u32 offset;
  234. u32 enable_bit;
  235. bool ret;
  236. /* If we don't need to control policy for this CCU, we're done. */
  237. if (!policy_lvm_en_exists(enable))
  238. return true;
  239. /* Ensure we're not busy before we start */
  240. offset = enable->offset;
  241. enable_bit = enable->bit;
  242. ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
  243. if (!ret) {
  244. pr_err("%s: ccu %s policy engine already stopped\n",
  245. __func__, ccu->name);
  246. return false;
  247. }
  248. /* Now set the bit to stop the engine (NO read-modify-write) */
  249. __ccu_write(ccu, offset, (u32)1 << enable_bit);
  250. /* Wait for indication that it has stopped. */
  251. ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
  252. if (!ret)
  253. pr_err("%s: ccu %s policy engine never stopped\n",
  254. __func__, ccu->name);
  255. return ret;
  256. }
  257. /*
  258. * A CCU has four operating conditions ("policies"), and some clocks
  259. * can be disabled or enabled based on which policy is currently in
  260. * effect. Such clocks have a bit in a "policy mask" register for
  261. * each policy indicating whether the clock is enabled for that
  262. * policy or not. The bit position for a clock is the same for all
  263. * four registers, and the 32-bit registers are at consecutive
  264. * addresses.
  265. */
  266. static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
  267. {
  268. u32 offset;
  269. u32 mask;
  270. int i;
  271. bool ret;
  272. if (!policy_exists(policy))
  273. return true;
  274. /*
  275. * We need to stop the CCU policy engine to allow update
  276. * of our policy bits.
  277. */
  278. if (!__ccu_policy_engine_stop(ccu)) {
  279. pr_err("%s: unable to stop CCU %s policy engine\n",
  280. __func__, ccu->name);
  281. return false;
  282. }
  283. /*
  284. * For now, if a clock defines its policy bit we just mark
  285. * it "enabled" for all four policies.
  286. */
  287. offset = policy->offset;
  288. mask = (u32)1 << policy->bit;
  289. for (i = 0; i < CCU_POLICY_COUNT; i++) {
  290. u32 reg_val;
  291. reg_val = __ccu_read(ccu, offset);
  292. reg_val |= mask;
  293. __ccu_write(ccu, offset, reg_val);
  294. offset += sizeof(u32);
  295. }
  296. /* We're done updating; fire up the policy engine again. */
  297. ret = __ccu_policy_engine_start(ccu, true);
  298. if (!ret)
  299. pr_err("%s: unable to restart CCU %s policy engine\n",
  300. __func__, ccu->name);
  301. return ret;
  302. }
  303. /* Gate operations */
  304. /* Determine whether a clock is gated. CCU lock must be held. */
  305. static bool
  306. __is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
  307. {
  308. u32 bit_mask;
  309. u32 reg_val;
  310. /* If there is no gate we can assume it's enabled. */
  311. if (!gate_exists(gate))
  312. return true;
  313. bit_mask = 1 << gate->status_bit;
  314. reg_val = __ccu_read(ccu, gate->offset);
  315. return (reg_val & bit_mask) != 0;
  316. }
  317. /* Determine whether a clock is gated. */
  318. static bool
  319. is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
  320. {
  321. long flags;
  322. bool ret;
  323. /* Avoid taking the lock if we can */
  324. if (!gate_exists(gate))
  325. return true;
  326. flags = ccu_lock(ccu);
  327. ret = __is_clk_gate_enabled(ccu, gate);
  328. ccu_unlock(ccu, flags);
  329. return ret;
  330. }
  331. /*
  332. * Commit our desired gate state to the hardware.
  333. * Returns true if successful, false otherwise.
  334. */
  335. static bool
  336. __gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
  337. {
  338. u32 reg_val;
  339. u32 mask;
  340. bool enabled = false;
  341. BUG_ON(!gate_exists(gate));
  342. if (!gate_is_sw_controllable(gate))
  343. return true; /* Nothing we can change */
  344. reg_val = __ccu_read(ccu, gate->offset);
  345. /* For a hardware/software gate, set which is in control */
  346. if (gate_is_hw_controllable(gate)) {
  347. mask = (u32)1 << gate->hw_sw_sel_bit;
  348. if (gate_is_sw_managed(gate))
  349. reg_val |= mask;
  350. else
  351. reg_val &= ~mask;
  352. }
  353. /*
  354. * If software is in control, enable or disable the gate.
  355. * If hardware is, clear the enabled bit for good measure.
  356. * If a software controlled gate can't be disabled, we're
  357. * required to write a 0 into the enable bit (but the gate
  358. * will be enabled).
  359. */
  360. mask = (u32)1 << gate->en_bit;
  361. if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) &&
  362. !gate_is_no_disable(gate))
  363. reg_val |= mask;
  364. else
  365. reg_val &= ~mask;
  366. __ccu_write(ccu, gate->offset, reg_val);
  367. /* For a hardware controlled gate, we're done */
  368. if (!gate_is_sw_managed(gate))
  369. return true;
  370. /* Otherwise wait for the gate to be in desired state */
  371. return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
  372. }
  373. /*
  374. * Initialize a gate. Our desired state (hardware/software select,
  375. * and if software, its enable state) is committed to hardware
  376. * without the usual checks to see if it's already set up that way.
  377. * Returns true if successful, false otherwise.
  378. */
  379. static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
  380. {
  381. if (!gate_exists(gate))
  382. return true;
  383. return __gate_commit(ccu, gate);
  384. }
  385. /*
  386. * Set a gate to enabled or disabled state. Does nothing if the
  387. * gate is not currently under software control, or if it is already
  388. * in the requested state. Returns true if successful, false
  389. * otherwise. CCU lock must be held.
  390. */
  391. static bool
  392. __clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
  393. {
  394. bool ret;
  395. if (!gate_exists(gate) || !gate_is_sw_managed(gate))
  396. return true; /* Nothing to do */
  397. if (!enable && gate_is_no_disable(gate)) {
  398. pr_warn("%s: invalid gate disable request (ignoring)\n",
  399. __func__);
  400. return true;
  401. }
  402. if (enable == gate_is_enabled(gate))
  403. return true; /* No change */
  404. gate_flip_enabled(gate);
  405. ret = __gate_commit(ccu, gate);
  406. if (!ret)
  407. gate_flip_enabled(gate); /* Revert the change */
  408. return ret;
  409. }
  410. /* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */
  411. static int clk_gate(struct ccu_data *ccu, const char *name,
  412. struct bcm_clk_gate *gate, bool enable)
  413. {
  414. unsigned long flags;
  415. bool success;
  416. /*
  417. * Avoid taking the lock if we can. We quietly ignore
  418. * requests to change state that don't make sense.
  419. */
  420. if (!gate_exists(gate) || !gate_is_sw_managed(gate))
  421. return 0;
  422. if (!enable && gate_is_no_disable(gate))
  423. return 0;
  424. flags = ccu_lock(ccu);
  425. __ccu_write_enable(ccu);
  426. success = __clk_gate(ccu, gate, enable);
  427. __ccu_write_disable(ccu);
  428. ccu_unlock(ccu, flags);
  429. if (success)
  430. return 0;
  431. pr_err("%s: failed to %s gate for %s\n", __func__,
  432. enable ? "enable" : "disable", name);
  433. return -EIO;
  434. }
  435. /* Hysteresis operations */
  436. /*
  437. * If a clock gate requires a turn-off delay it will have
  438. * "hysteresis" register bits defined. The first, if set, enables
  439. * the delay; and if enabled, the second bit determines whether the
  440. * delay is "low" or "high" (1 means high). For now, if it's
  441. * defined for a clock, we set it.
  442. */
  443. static bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst)
  444. {
  445. u32 offset;
  446. u32 reg_val;
  447. u32 mask;
  448. if (!hyst_exists(hyst))
  449. return true;
  450. offset = hyst->offset;
  451. mask = (u32)1 << hyst->en_bit;
  452. mask |= (u32)1 << hyst->val_bit;
  453. reg_val = __ccu_read(ccu, offset);
  454. reg_val |= mask;
  455. __ccu_write(ccu, offset, reg_val);
  456. return true;
  457. }
  458. /* Trigger operations */
  459. /*
  460. * Caller must ensure CCU lock is held and access is enabled.
  461. * Returns true if successful, false otherwise.
  462. */
  463. static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
  464. {
  465. /* Trigger the clock and wait for it to finish */
  466. __ccu_write(ccu, trig->offset, 1 << trig->bit);
  467. return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
  468. }
  469. /* Divider operations */
  470. /* Read a divider value and return the scaled divisor it represents. */
  471. static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
  472. {
  473. unsigned long flags;
  474. u32 reg_val;
  475. u32 reg_div;
  476. if (divider_is_fixed(div))
  477. return (u64)div->u.fixed;
  478. flags = ccu_lock(ccu);
  479. reg_val = __ccu_read(ccu, div->u.s.offset);
  480. ccu_unlock(ccu, flags);
  481. /* Extract the full divider field from the register value */
  482. reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
  483. /* Return the scaled divisor value it represents */
  484. return scaled_div_value(div, reg_div);
  485. }
  486. /*
  487. * Convert a divider's scaled divisor value into its recorded form
  488. * and commit it into the hardware divider register.
  489. *
  490. * Returns 0 on success. Returns -EINVAL for invalid arguments.
  491. * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
  492. */
  493. static int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
  494. struct bcm_clk_div *div, struct bcm_clk_trig *trig)
  495. {
  496. bool enabled;
  497. u32 reg_div;
  498. u32 reg_val;
  499. int ret = 0;
  500. BUG_ON(divider_is_fixed(div));
  501. /*
  502. * If we're just initializing the divider, and no initial
  503. * state was defined in the device tree, we just find out
  504. * what its current value is rather than updating it.
  505. */
  506. if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) {
  507. reg_val = __ccu_read(ccu, div->u.s.offset);
  508. reg_div = bitfield_extract(reg_val, div->u.s.shift,
  509. div->u.s.width);
  510. div->u.s.scaled_div = scaled_div_value(div, reg_div);
  511. return 0;
  512. }
  513. /* Convert the scaled divisor to the value we need to record */
  514. reg_div = divider(div, div->u.s.scaled_div);
  515. /* Clock needs to be enabled before changing the rate */
  516. enabled = __is_clk_gate_enabled(ccu, gate);
  517. if (!enabled && !__clk_gate(ccu, gate, true)) {
  518. ret = -ENXIO;
  519. goto out;
  520. }
  521. /* Replace the divider value and record the result */
  522. reg_val = __ccu_read(ccu, div->u.s.offset);
  523. reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width,
  524. reg_div);
  525. __ccu_write(ccu, div->u.s.offset, reg_val);
  526. /* If the trigger fails we still want to disable the gate */
  527. if (!__clk_trigger(ccu, trig))
  528. ret = -EIO;
  529. /* Disable the clock again if it was disabled to begin with */
  530. if (!enabled && !__clk_gate(ccu, gate, false))
  531. ret = ret ? ret : -ENXIO; /* return first error */
  532. out:
  533. return ret;
  534. }
  535. /*
  536. * Initialize a divider by committing our desired state to hardware
  537. * without the usual checks to see if it's already set up that way.
  538. * Returns true if successful, false otherwise.
  539. */
  540. static bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
  541. struct bcm_clk_div *div, struct bcm_clk_trig *trig)
  542. {
  543. if (!divider_exists(div) || divider_is_fixed(div))
  544. return true;
  545. return !__div_commit(ccu, gate, div, trig);
  546. }
  547. static int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
  548. struct bcm_clk_div *div, struct bcm_clk_trig *trig,
  549. u64 scaled_div)
  550. {
  551. unsigned long flags;
  552. u64 previous;
  553. int ret;
  554. BUG_ON(divider_is_fixed(div));
  555. previous = div->u.s.scaled_div;
  556. if (previous == scaled_div)
  557. return 0; /* No change */
  558. div->u.s.scaled_div = scaled_div;
  559. flags = ccu_lock(ccu);
  560. __ccu_write_enable(ccu);
  561. ret = __div_commit(ccu, gate, div, trig);
  562. __ccu_write_disable(ccu);
  563. ccu_unlock(ccu, flags);
  564. if (ret)
  565. div->u.s.scaled_div = previous; /* Revert the change */
  566. return ret;
  567. }
  568. /* Common clock rate helpers */
  569. /*
  570. * Implement the common clock framework recalc_rate method, taking
  571. * into account a divider and an optional pre-divider. The
  572. * pre-divider register pointer may be NULL.
  573. */
  574. static unsigned long clk_recalc_rate(struct ccu_data *ccu,
  575. struct bcm_clk_div *div, struct bcm_clk_div *pre_div,
  576. unsigned long parent_rate)
  577. {
  578. u64 scaled_parent_rate;
  579. u64 scaled_div;
  580. u64 result;
  581. if (!divider_exists(div))
  582. return parent_rate;
  583. if (parent_rate > (unsigned long)LONG_MAX)
  584. return 0; /* actually this would be a caller bug */
  585. /*
  586. * If there is a pre-divider, divide the scaled parent rate
  587. * by the pre-divider value first. In this case--to improve
  588. * accuracy--scale the parent rate by *both* the pre-divider
  589. * value and the divider before actually computing the
  590. * result of the pre-divider.
  591. *
  592. * If there's only one divider, just scale the parent rate.
  593. */
  594. if (pre_div && divider_exists(pre_div)) {
  595. u64 scaled_rate;
  596. scaled_rate = scale_rate(pre_div, parent_rate);
  597. scaled_rate = scale_rate(div, scaled_rate);
  598. scaled_div = divider_read_scaled(ccu, pre_div);
  599. scaled_parent_rate = do_div_round_closest(scaled_rate,
  600. scaled_div);
  601. } else {
  602. scaled_parent_rate = scale_rate(div, parent_rate);
  603. }
  604. /*
  605. * Get the scaled divisor value, and divide the scaled
  606. * parent rate by that to determine this clock's resulting
  607. * rate.
  608. */
  609. scaled_div = divider_read_scaled(ccu, div);
  610. result = do_div_round_closest(scaled_parent_rate, scaled_div);
  611. return (unsigned long)result;
  612. }
  613. /*
  614. * Compute the output rate produced when a given parent rate is fed
  615. * into two dividers. The pre-divider can be NULL, and even if it's
  616. * non-null it may be nonexistent. It's also OK for the divider to
  617. * be nonexistent, and in that case the pre-divider is also ignored.
  618. *
  619. * If scaled_div is non-null, it is used to return the scaled divisor
  620. * value used by the (downstream) divider to produce that rate.
  621. */
  622. static long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div,
  623. struct bcm_clk_div *pre_div,
  624. unsigned long rate, unsigned long parent_rate,
  625. u64 *scaled_div)
  626. {
  627. u64 scaled_parent_rate;
  628. u64 min_scaled_div;
  629. u64 max_scaled_div;
  630. u64 best_scaled_div;
  631. u64 result;
  632. BUG_ON(!divider_exists(div));
  633. BUG_ON(!rate);
  634. BUG_ON(parent_rate > (u64)LONG_MAX);
  635. /*
  636. * If there is a pre-divider, divide the scaled parent rate
  637. * by the pre-divider value first. In this case--to improve
  638. * accuracy--scale the parent rate by *both* the pre-divider
  639. * value and the divider before actually computing the
  640. * result of the pre-divider.
  641. *
  642. * If there's only one divider, just scale the parent rate.
  643. *
  644. * For simplicity we treat the pre-divider as fixed (for now).
  645. */
  646. if (divider_exists(pre_div)) {
  647. u64 scaled_rate;
  648. u64 scaled_pre_div;
  649. scaled_rate = scale_rate(pre_div, parent_rate);
  650. scaled_rate = scale_rate(div, scaled_rate);
  651. scaled_pre_div = divider_read_scaled(ccu, pre_div);
  652. scaled_parent_rate = do_div_round_closest(scaled_rate,
  653. scaled_pre_div);
  654. } else {
  655. scaled_parent_rate = scale_rate(div, parent_rate);
  656. }
  657. /*
  658. * Compute the best possible divider and ensure it is in
  659. * range. A fixed divider can't be changed, so just report
  660. * the best we can do.
  661. */
  662. if (!divider_is_fixed(div)) {
  663. best_scaled_div = do_div_round_closest(scaled_parent_rate,
  664. rate);
  665. min_scaled_div = scaled_div_min(div);
  666. max_scaled_div = scaled_div_max(div);
  667. if (best_scaled_div > max_scaled_div)
  668. best_scaled_div = max_scaled_div;
  669. else if (best_scaled_div < min_scaled_div)
  670. best_scaled_div = min_scaled_div;
  671. } else {
  672. best_scaled_div = divider_read_scaled(ccu, div);
  673. }
  674. /* OK, figure out the resulting rate */
  675. result = do_div_round_closest(scaled_parent_rate, best_scaled_div);
  676. if (scaled_div)
  677. *scaled_div = best_scaled_div;
  678. return (long)result;
  679. }
  680. /* Common clock parent helpers */
  681. /*
  682. * For a given parent selector (register field) value, find the
  683. * index into a selector's parent_sel array that contains it.
  684. * Returns the index, or BAD_CLK_INDEX if it's not found.
  685. */
  686. static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
  687. {
  688. u8 i;
  689. BUG_ON(sel->parent_count > (u32)U8_MAX);
  690. for (i = 0; i < sel->parent_count; i++)
  691. if (sel->parent_sel[i] == parent_sel)
  692. return i;
  693. return BAD_CLK_INDEX;
  694. }
  695. /*
  696. * Fetch the current value of the selector, and translate that into
  697. * its corresponding index in the parent array we registered with
  698. * the clock framework.
  699. *
  700. * Returns parent array index that corresponds with the value found,
  701. * or BAD_CLK_INDEX if the found value is out of range.
  702. */
  703. static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
  704. {
  705. unsigned long flags;
  706. u32 reg_val;
  707. u32 parent_sel;
  708. u8 index;
  709. /* If there's no selector, there's only one parent */
  710. if (!selector_exists(sel))
  711. return 0;
  712. /* Get the value in the selector register */
  713. flags = ccu_lock(ccu);
  714. reg_val = __ccu_read(ccu, sel->offset);
  715. ccu_unlock(ccu, flags);
  716. parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
  717. /* Look up that selector's parent array index and return it */
  718. index = parent_index(sel, parent_sel);
  719. if (index == BAD_CLK_INDEX)
  720. pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
  721. __func__, parent_sel, ccu->name, sel->offset);
  722. return index;
  723. }
  724. /*
  725. * Commit our desired selector value to the hardware.
  726. *
  727. * Returns 0 on success. Returns -EINVAL for invalid arguments.
  728. * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
  729. */
  730. static int
  731. __sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
  732. struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
  733. {
  734. u32 parent_sel;
  735. u32 reg_val;
  736. bool enabled;
  737. int ret = 0;
  738. BUG_ON(!selector_exists(sel));
  739. /*
  740. * If we're just initializing the selector, and no initial
  741. * state was defined in the device tree, we just find out
  742. * what its current value is rather than updating it.
  743. */
  744. if (sel->clk_index == BAD_CLK_INDEX) {
  745. u8 index;
  746. reg_val = __ccu_read(ccu, sel->offset);
  747. parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
  748. index = parent_index(sel, parent_sel);
  749. if (index == BAD_CLK_INDEX)
  750. return -EINVAL;
  751. sel->clk_index = index;
  752. return 0;
  753. }
  754. BUG_ON((u32)sel->clk_index >= sel->parent_count);
  755. parent_sel = sel->parent_sel[sel->clk_index];
  756. /* Clock needs to be enabled before changing the parent */
  757. enabled = __is_clk_gate_enabled(ccu, gate);
  758. if (!enabled && !__clk_gate(ccu, gate, true))
  759. return -ENXIO;
  760. /* Replace the selector value and record the result */
  761. reg_val = __ccu_read(ccu, sel->offset);
  762. reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel);
  763. __ccu_write(ccu, sel->offset, reg_val);
  764. /* If the trigger fails we still want to disable the gate */
  765. if (!__clk_trigger(ccu, trig))
  766. ret = -EIO;
  767. /* Disable the clock again if it was disabled to begin with */
  768. if (!enabled && !__clk_gate(ccu, gate, false))
  769. ret = ret ? ret : -ENXIO; /* return first error */
  770. return ret;
  771. }
  772. /*
  773. * Initialize a selector by committing our desired state to hardware
  774. * without the usual checks to see if it's already set up that way.
  775. * Returns true if successful, false otherwise.
  776. */
  777. static bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
  778. struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
  779. {
  780. if (!selector_exists(sel))
  781. return true;
  782. return !__sel_commit(ccu, gate, sel, trig);
  783. }
  784. /*
  785. * Write a new value into a selector register to switch to a
  786. * different parent clock. Returns 0 on success, or an error code
  787. * (from __sel_commit()) otherwise.
  788. */
  789. static int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
  790. struct bcm_clk_sel *sel, struct bcm_clk_trig *trig,
  791. u8 index)
  792. {
  793. unsigned long flags;
  794. u8 previous;
  795. int ret;
  796. previous = sel->clk_index;
  797. if (previous == index)
  798. return 0; /* No change */
  799. sel->clk_index = index;
  800. flags = ccu_lock(ccu);
  801. __ccu_write_enable(ccu);
  802. ret = __sel_commit(ccu, gate, sel, trig);
  803. __ccu_write_disable(ccu);
  804. ccu_unlock(ccu, flags);
  805. if (ret)
  806. sel->clk_index = previous; /* Revert the change */
  807. return ret;
  808. }
  809. /* Clock operations */
  810. static int kona_peri_clk_enable(struct clk_hw *hw)
  811. {
  812. struct kona_clk *bcm_clk = to_kona_clk(hw);
  813. struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
  814. return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
  815. }
  816. static void kona_peri_clk_disable(struct clk_hw *hw)
  817. {
  818. struct kona_clk *bcm_clk = to_kona_clk(hw);
  819. struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
  820. (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
  821. }
  822. static int kona_peri_clk_is_enabled(struct clk_hw *hw)
  823. {
  824. struct kona_clk *bcm_clk = to_kona_clk(hw);
  825. struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
  826. return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
  827. }
  828. static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
  829. unsigned long parent_rate)
  830. {
  831. struct kona_clk *bcm_clk = to_kona_clk(hw);
  832. struct peri_clk_data *data = bcm_clk->u.peri;
  833. return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
  834. parent_rate);
  835. }
  836. static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  837. unsigned long *parent_rate)
  838. {
  839. struct kona_clk *bcm_clk = to_kona_clk(hw);
  840. struct bcm_clk_div *div = &bcm_clk->u.peri->div;
  841. if (!divider_exists(div))
  842. return __clk_get_rate(hw->clk);
  843. /* Quietly avoid a zero rate */
  844. return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
  845. rate ? rate : 1, *parent_rate, NULL);
  846. }
  847. static long kona_peri_clk_determine_rate(struct clk_hw *hw, unsigned long rate,
  848. unsigned long *best_parent_rate, struct clk **best_parent)
  849. {
  850. struct kona_clk *bcm_clk = to_kona_clk(hw);
  851. struct clk *clk = hw->clk;
  852. struct clk *current_parent;
  853. unsigned long parent_rate;
  854. unsigned long best_delta;
  855. unsigned long best_rate;
  856. u32 parent_count;
  857. u32 which;
  858. /*
  859. * If there is no other parent to choose, use the current one.
  860. * Note: We don't honor (or use) CLK_SET_RATE_NO_REPARENT.
  861. */
  862. WARN_ON_ONCE(bcm_clk->init_data.flags & CLK_SET_RATE_NO_REPARENT);
  863. parent_count = (u32)bcm_clk->init_data.num_parents;
  864. if (parent_count < 2)
  865. return kona_peri_clk_round_rate(hw, rate, best_parent_rate);
  866. /* Unless we can do better, stick with current parent */
  867. current_parent = clk_get_parent(clk);
  868. parent_rate = __clk_get_rate(current_parent);
  869. best_rate = kona_peri_clk_round_rate(hw, rate, &parent_rate);
  870. best_delta = abs(best_rate - rate);
  871. /* Check whether any other parent clock can produce a better result */
  872. for (which = 0; which < parent_count; which++) {
  873. struct clk *parent = clk_get_parent_by_index(clk, which);
  874. unsigned long delta;
  875. unsigned long other_rate;
  876. BUG_ON(!parent);
  877. if (parent == current_parent)
  878. continue;
  879. /* We don't support CLK_SET_RATE_PARENT */
  880. parent_rate = __clk_get_rate(parent);
  881. other_rate = kona_peri_clk_round_rate(hw, rate, &parent_rate);
  882. delta = abs(other_rate - rate);
  883. if (delta < best_delta) {
  884. best_delta = delta;
  885. best_rate = other_rate;
  886. *best_parent = parent;
  887. *best_parent_rate = parent_rate;
  888. }
  889. }
  890. return best_rate;
  891. }
  892. static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
  893. {
  894. struct kona_clk *bcm_clk = to_kona_clk(hw);
  895. struct peri_clk_data *data = bcm_clk->u.peri;
  896. struct bcm_clk_sel *sel = &data->sel;
  897. struct bcm_clk_trig *trig;
  898. int ret;
  899. BUG_ON(index >= sel->parent_count);
  900. /* If there's only one parent we don't require a selector */
  901. if (!selector_exists(sel))
  902. return 0;
  903. /*
  904. * The regular trigger is used by default, but if there's a
  905. * pre-trigger we want to use that instead.
  906. */
  907. trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
  908. : &data->trig;
  909. ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index);
  910. if (ret == -ENXIO) {
  911. pr_err("%s: gating failure for %s\n", __func__,
  912. bcm_clk->init_data.name);
  913. ret = -EIO; /* Don't proliferate weird errors */
  914. } else if (ret == -EIO) {
  915. pr_err("%s: %strigger failed for %s\n", __func__,
  916. trig == &data->pre_trig ? "pre-" : "",
  917. bcm_clk->init_data.name);
  918. }
  919. return ret;
  920. }
  921. static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
  922. {
  923. struct kona_clk *bcm_clk = to_kona_clk(hw);
  924. struct peri_clk_data *data = bcm_clk->u.peri;
  925. u8 index;
  926. index = selector_read_index(bcm_clk->ccu, &data->sel);
  927. /* Not all callers would handle an out-of-range value gracefully */
  928. return index == BAD_CLK_INDEX ? 0 : index;
  929. }
  930. static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  931. unsigned long parent_rate)
  932. {
  933. struct kona_clk *bcm_clk = to_kona_clk(hw);
  934. struct peri_clk_data *data = bcm_clk->u.peri;
  935. struct bcm_clk_div *div = &data->div;
  936. u64 scaled_div = 0;
  937. int ret;
  938. if (parent_rate > (unsigned long)LONG_MAX)
  939. return -EINVAL;
  940. if (rate == __clk_get_rate(hw->clk))
  941. return 0;
  942. if (!divider_exists(div))
  943. return rate == parent_rate ? 0 : -EINVAL;
  944. /*
  945. * A fixed divider can't be changed. (Nor can a fixed
  946. * pre-divider be, but for now we never actually try to
  947. * change that.) Tolerate a request for a no-op change.
  948. */
  949. if (divider_is_fixed(&data->div))
  950. return rate == parent_rate ? 0 : -EINVAL;
  951. /*
  952. * Get the scaled divisor value needed to achieve a clock
  953. * rate as close as possible to what was requested, given
  954. * the parent clock rate supplied.
  955. */
  956. (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
  957. rate ? rate : 1, parent_rate, &scaled_div);
  958. /*
  959. * We aren't updating any pre-divider at this point, so
  960. * we'll use the regular trigger.
  961. */
  962. ret = divider_write(bcm_clk->ccu, &data->gate, &data->div,
  963. &data->trig, scaled_div);
  964. if (ret == -ENXIO) {
  965. pr_err("%s: gating failure for %s\n", __func__,
  966. bcm_clk->init_data.name);
  967. ret = -EIO; /* Don't proliferate weird errors */
  968. } else if (ret == -EIO) {
  969. pr_err("%s: trigger failed for %s\n", __func__,
  970. bcm_clk->init_data.name);
  971. }
  972. return ret;
  973. }
  974. struct clk_ops kona_peri_clk_ops = {
  975. .enable = kona_peri_clk_enable,
  976. .disable = kona_peri_clk_disable,
  977. .is_enabled = kona_peri_clk_is_enabled,
  978. .recalc_rate = kona_peri_clk_recalc_rate,
  979. .determine_rate = kona_peri_clk_determine_rate,
  980. .set_parent = kona_peri_clk_set_parent,
  981. .get_parent = kona_peri_clk_get_parent,
  982. .set_rate = kona_peri_clk_set_rate,
  983. };
  984. /* Put a peripheral clock into its initial state */
  985. static bool __peri_clk_init(struct kona_clk *bcm_clk)
  986. {
  987. struct ccu_data *ccu = bcm_clk->ccu;
  988. struct peri_clk_data *peri = bcm_clk->u.peri;
  989. const char *name = bcm_clk->init_data.name;
  990. struct bcm_clk_trig *trig;
  991. BUG_ON(bcm_clk->type != bcm_clk_peri);
  992. if (!policy_init(ccu, &peri->policy)) {
  993. pr_err("%s: error initializing policy for %s\n",
  994. __func__, name);
  995. return false;
  996. }
  997. if (!gate_init(ccu, &peri->gate)) {
  998. pr_err("%s: error initializing gate for %s\n", __func__, name);
  999. return false;
  1000. }
  1001. if (!hyst_init(ccu, &peri->hyst)) {
  1002. pr_err("%s: error initializing hyst for %s\n", __func__, name);
  1003. return false;
  1004. }
  1005. if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) {
  1006. pr_err("%s: error initializing divider for %s\n", __func__,
  1007. name);
  1008. return false;
  1009. }
  1010. /*
  1011. * For the pre-divider and selector, the pre-trigger is used
  1012. * if it's present, otherwise we just use the regular trigger.
  1013. */
  1014. trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
  1015. : &peri->trig;
  1016. if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) {
  1017. pr_err("%s: error initializing pre-divider for %s\n", __func__,
  1018. name);
  1019. return false;
  1020. }
  1021. if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) {
  1022. pr_err("%s: error initializing selector for %s\n", __func__,
  1023. name);
  1024. return false;
  1025. }
  1026. return true;
  1027. }
  1028. static bool __kona_clk_init(struct kona_clk *bcm_clk)
  1029. {
  1030. switch (bcm_clk->type) {
  1031. case bcm_clk_peri:
  1032. return __peri_clk_init(bcm_clk);
  1033. default:
  1034. BUG();
  1035. }
  1036. return -EINVAL;
  1037. }
  1038. /* Set a CCU and all its clocks into their desired initial state */
  1039. bool __init kona_ccu_init(struct ccu_data *ccu)
  1040. {
  1041. unsigned long flags;
  1042. unsigned int which;
  1043. struct clk **clks = ccu->clk_data.clks;
  1044. bool success = true;
  1045. flags = ccu_lock(ccu);
  1046. __ccu_write_enable(ccu);
  1047. for (which = 0; which < ccu->clk_data.clk_num; which++) {
  1048. struct kona_clk *bcm_clk;
  1049. if (!clks[which])
  1050. continue;
  1051. bcm_clk = to_kona_clk(__clk_get_hw(clks[which]));
  1052. success &= __kona_clk_init(bcm_clk);
  1053. }
  1054. __ccu_write_disable(ccu);
  1055. ccu_unlock(ccu, flags);
  1056. return success;
  1057. }