clk-kona.c 32 KB

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