clk-kona.c 33 KB

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