clk-icst.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the ICST307 VCO clock found in the ARM Reference designs.
  4. * We wrap the custom interface from <asm/hardware/icst.h> into the generic
  5. * clock framework.
  6. *
  7. * Copyright (C) 2012-2015 Linus Walleij
  8. *
  9. * TODO: when all ARM reference designs are migrated to generic clocks, the
  10. * ICST clock code from the ARM tree should probably be merged into this
  11. * file.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/export.h>
  16. #include <linux/err.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/regmap.h>
  20. #include <linux/mfd/syscon.h>
  21. #include "icst.h"
  22. #include "clk-icst.h"
  23. /* Magic unlocking token used on all Versatile boards */
  24. #define VERSATILE_LOCK_VAL 0xA05F
  25. #define VERSATILE_AUX_OSC_BITS 0x7FFFF
  26. #define INTEGRATOR_AP_CM_BITS 0xFF
  27. #define INTEGRATOR_AP_SYS_BITS 0xFF
  28. #define INTEGRATOR_CP_CM_CORE_BITS 0x7FF
  29. #define INTEGRATOR_CP_CM_MEM_BITS 0x7FF000
  30. #define INTEGRATOR_AP_PCI_25_33_MHZ BIT(8)
  31. /**
  32. * struct clk_icst - ICST VCO clock wrapper
  33. * @hw: corresponding clock hardware entry
  34. * @map: register map
  35. * @vcoreg_off: VCO register address
  36. * @lockreg_off: VCO lock register address
  37. * @params: parameters for this ICST instance
  38. * @rate: current rate
  39. * @ctype: the type of control register for the ICST
  40. */
  41. struct clk_icst {
  42. struct clk_hw hw;
  43. struct regmap *map;
  44. u32 vcoreg_off;
  45. u32 lockreg_off;
  46. struct icst_params *params;
  47. unsigned long rate;
  48. enum icst_control_type ctype;
  49. };
  50. #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
  51. /**
  52. * vco_get() - get ICST VCO settings from a certain ICST
  53. * @icst: the ICST clock to get
  54. * @vco: the VCO struct to return the value in
  55. */
  56. static int vco_get(struct clk_icst *icst, struct icst_vco *vco)
  57. {
  58. u32 val;
  59. int ret;
  60. ret = regmap_read(icst->map, icst->vcoreg_off, &val);
  61. if (ret)
  62. return ret;
  63. /*
  64. * The Integrator/AP core clock can only access the low eight
  65. * bits of the v PLL divider. Bit 8 is tied low and always zero,
  66. * r is hardwired to 22 and output divider s is hardwired to 1
  67. * (divide by 2) according to the document
  68. * "Integrator CM926EJ-S, CM946E-S, CM966E-S, CM1026EJ-S and
  69. * CM1136JF-S User Guide" ARM DUI 0138E, page 3-13 thru 3-14.
  70. */
  71. if (icst->ctype == ICST_INTEGRATOR_AP_CM) {
  72. vco->v = val & INTEGRATOR_AP_CM_BITS;
  73. vco->r = 22;
  74. vco->s = 1;
  75. return 0;
  76. }
  77. /*
  78. * The Integrator/AP system clock on the base board can only
  79. * access the low eight bits of the v PLL divider. Bit 8 is tied low
  80. * and always zero, r is hardwired to 46, and the output divider is
  81. * hardwired to 3 (divide by 4) according to the document
  82. * "Integrator AP ASIC Development Motherboard" ARM DUI 0098B,
  83. * page 3-16.
  84. */
  85. if (icst->ctype == ICST_INTEGRATOR_AP_SYS) {
  86. vco->v = val & INTEGRATOR_AP_SYS_BITS;
  87. vco->r = 46;
  88. vco->s = 3;
  89. return 0;
  90. }
  91. /*
  92. * The Integrator/AP PCI clock is using an odd pattern to create
  93. * the child clock, basically a single bit called DIVX/Y is used
  94. * to select between two different hardwired values: setting the
  95. * bit to 0 yields v = 17, r = 22 and OD = 1, whereas setting the
  96. * bit to 1 yields v = 14, r = 14 and OD = 1 giving the frequencies
  97. * 33 or 25 MHz respectively.
  98. */
  99. if (icst->ctype == ICST_INTEGRATOR_AP_PCI) {
  100. bool divxy = !!(val & INTEGRATOR_AP_PCI_25_33_MHZ);
  101. vco->v = divxy ? 17 : 14;
  102. vco->r = divxy ? 22 : 14;
  103. vco->s = 1;
  104. return 0;
  105. }
  106. /*
  107. * The Integrator/CP core clock can access the low eight bits
  108. * of the v PLL divider. Bit 8 is tied low and always zero,
  109. * r is hardwired to 22 and the output divider s is accessible
  110. * in bits 8 thru 10 according to the document
  111. * "Integrator/CM940T, CM920T, CM740T, and CM720T User Guide"
  112. * ARM DUI 0157A, page 3-20 thru 3-23 and 4-10.
  113. */
  114. if (icst->ctype == ICST_INTEGRATOR_CP_CM_CORE) {
  115. vco->v = val & 0xFF;
  116. vco->r = 22;
  117. vco->s = (val >> 8) & 7;
  118. return 0;
  119. }
  120. if (icst->ctype == ICST_INTEGRATOR_CP_CM_MEM) {
  121. vco->v = (val >> 12) & 0xFF;
  122. vco->r = 22;
  123. vco->s = (val >> 20) & 7;
  124. return 0;
  125. }
  126. vco->v = val & 0x1ff;
  127. vco->r = (val >> 9) & 0x7f;
  128. vco->s = (val >> 16) & 03;
  129. return 0;
  130. }
  131. /**
  132. * vco_set() - commit changes to an ICST VCO
  133. * @icst: the ICST clock to set
  134. * @vco: the VCO struct to set the changes from
  135. */
  136. static int vco_set(struct clk_icst *icst, struct icst_vco vco)
  137. {
  138. u32 mask;
  139. u32 val;
  140. int ret;
  141. /* Mask the bits used by the VCO */
  142. switch (icst->ctype) {
  143. case ICST_INTEGRATOR_AP_CM:
  144. mask = INTEGRATOR_AP_CM_BITS;
  145. val = vco.v & 0xFF;
  146. if (vco.v & 0x100)
  147. pr_err("ICST error: tried to set bit 8 of VDW\n");
  148. if (vco.s != 1)
  149. pr_err("ICST error: tried to use VOD != 1\n");
  150. if (vco.r != 22)
  151. pr_err("ICST error: tried to use RDW != 22\n");
  152. break;
  153. case ICST_INTEGRATOR_AP_SYS:
  154. mask = INTEGRATOR_AP_SYS_BITS;
  155. val = vco.v & 0xFF;
  156. if (vco.v & 0x100)
  157. pr_err("ICST error: tried to set bit 8 of VDW\n");
  158. if (vco.s != 3)
  159. pr_err("ICST error: tried to use VOD != 1\n");
  160. if (vco.r != 46)
  161. pr_err("ICST error: tried to use RDW != 22\n");
  162. break;
  163. case ICST_INTEGRATOR_CP_CM_CORE:
  164. mask = INTEGRATOR_CP_CM_CORE_BITS; /* Uses 12 bits */
  165. val = (vco.v & 0xFF) | vco.s << 8;
  166. if (vco.v & 0x100)
  167. pr_err("ICST error: tried to set bit 8 of VDW\n");
  168. if (vco.r != 22)
  169. pr_err("ICST error: tried to use RDW != 22\n");
  170. break;
  171. case ICST_INTEGRATOR_CP_CM_MEM:
  172. mask = INTEGRATOR_CP_CM_MEM_BITS; /* Uses 12 bits */
  173. val = ((vco.v & 0xFF) << 12) | (vco.s << 20);
  174. if (vco.v & 0x100)
  175. pr_err("ICST error: tried to set bit 8 of VDW\n");
  176. if (vco.r != 22)
  177. pr_err("ICST error: tried to use RDW != 22\n");
  178. break;
  179. default:
  180. /* Regular auxilary oscillator */
  181. mask = VERSATILE_AUX_OSC_BITS;
  182. val = vco.v | (vco.r << 9) | (vco.s << 16);
  183. break;
  184. }
  185. pr_debug("ICST: new val = 0x%08x\n", val);
  186. /* This magic unlocks the VCO so it can be controlled */
  187. ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL);
  188. if (ret)
  189. return ret;
  190. ret = regmap_update_bits(icst->map, icst->vcoreg_off, mask, val);
  191. if (ret)
  192. return ret;
  193. /* This locks the VCO again */
  194. ret = regmap_write(icst->map, icst->lockreg_off, 0);
  195. if (ret)
  196. return ret;
  197. return 0;
  198. }
  199. static unsigned long icst_recalc_rate(struct clk_hw *hw,
  200. unsigned long parent_rate)
  201. {
  202. struct clk_icst *icst = to_icst(hw);
  203. struct icst_vco vco;
  204. int ret;
  205. if (parent_rate)
  206. icst->params->ref = parent_rate;
  207. ret = vco_get(icst, &vco);
  208. if (ret) {
  209. pr_err("ICST: could not get VCO setting\n");
  210. return 0;
  211. }
  212. icst->rate = icst_hz(icst->params, vco);
  213. return icst->rate;
  214. }
  215. static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
  216. unsigned long *prate)
  217. {
  218. struct clk_icst *icst = to_icst(hw);
  219. struct icst_vco vco;
  220. if (icst->ctype == ICST_INTEGRATOR_AP_CM ||
  221. icst->ctype == ICST_INTEGRATOR_CP_CM_CORE) {
  222. if (rate <= 12000000)
  223. return 12000000;
  224. if (rate >= 160000000)
  225. return 160000000;
  226. /* Slam to closest megahertz */
  227. return DIV_ROUND_CLOSEST(rate, 1000000) * 1000000;
  228. }
  229. if (icst->ctype == ICST_INTEGRATOR_CP_CM_MEM) {
  230. if (rate <= 6000000)
  231. return 6000000;
  232. if (rate >= 66000000)
  233. return 66000000;
  234. /* Slam to closest 0.5 megahertz */
  235. return DIV_ROUND_CLOSEST(rate, 500000) * 500000;
  236. }
  237. if (icst->ctype == ICST_INTEGRATOR_AP_SYS) {
  238. /* Divides between 3 and 50 MHz in steps of 0.25 MHz */
  239. if (rate <= 3000000)
  240. return 3000000;
  241. if (rate >= 50000000)
  242. return 5000000;
  243. /* Slam to closest 0.25 MHz */
  244. return DIV_ROUND_CLOSEST(rate, 250000) * 250000;
  245. }
  246. if (icst->ctype == ICST_INTEGRATOR_AP_PCI) {
  247. /*
  248. * If we're below or less than halfway from 25 to 33 MHz
  249. * select 25 MHz
  250. */
  251. if (rate <= 25000000 || rate < 29000000)
  252. return 25000000;
  253. /* Else just return the default frequency */
  254. return 33000000;
  255. }
  256. vco = icst_hz_to_vco(icst->params, rate);
  257. return icst_hz(icst->params, vco);
  258. }
  259. static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
  260. unsigned long parent_rate)
  261. {
  262. struct clk_icst *icst = to_icst(hw);
  263. struct icst_vco vco;
  264. if (icst->ctype == ICST_INTEGRATOR_AP_PCI) {
  265. /* This clock is especially primitive */
  266. unsigned int val;
  267. int ret;
  268. if (rate == 25000000) {
  269. val = 0;
  270. } else if (rate == 33000000) {
  271. val = INTEGRATOR_AP_PCI_25_33_MHZ;
  272. } else {
  273. pr_err("ICST: cannot set PCI frequency %lu\n",
  274. rate);
  275. return -EINVAL;
  276. }
  277. ret = regmap_write(icst->map, icst->lockreg_off,
  278. VERSATILE_LOCK_VAL);
  279. if (ret)
  280. return ret;
  281. ret = regmap_update_bits(icst->map, icst->vcoreg_off,
  282. INTEGRATOR_AP_PCI_25_33_MHZ,
  283. val);
  284. if (ret)
  285. return ret;
  286. /* This locks the VCO again */
  287. ret = regmap_write(icst->map, icst->lockreg_off, 0);
  288. if (ret)
  289. return ret;
  290. return 0;
  291. }
  292. if (parent_rate)
  293. icst->params->ref = parent_rate;
  294. vco = icst_hz_to_vco(icst->params, rate);
  295. icst->rate = icst_hz(icst->params, vco);
  296. return vco_set(icst, vco);
  297. }
  298. static const struct clk_ops icst_ops = {
  299. .recalc_rate = icst_recalc_rate,
  300. .round_rate = icst_round_rate,
  301. .set_rate = icst_set_rate,
  302. };
  303. struct clk *icst_clk_setup(struct device *dev,
  304. const struct clk_icst_desc *desc,
  305. const char *name,
  306. const char *parent_name,
  307. struct regmap *map,
  308. enum icst_control_type ctype)
  309. {
  310. struct clk *clk;
  311. struct clk_icst *icst;
  312. struct clk_init_data init;
  313. struct icst_params *pclone;
  314. icst = kzalloc(sizeof(*icst), GFP_KERNEL);
  315. if (!icst)
  316. return ERR_PTR(-ENOMEM);
  317. pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
  318. if (!pclone) {
  319. kfree(icst);
  320. return ERR_PTR(-ENOMEM);
  321. }
  322. init.name = name;
  323. init.ops = &icst_ops;
  324. init.flags = 0;
  325. init.parent_names = (parent_name ? &parent_name : NULL);
  326. init.num_parents = (parent_name ? 1 : 0);
  327. icst->map = map;
  328. icst->hw.init = &init;
  329. icst->params = pclone;
  330. icst->vcoreg_off = desc->vco_offset;
  331. icst->lockreg_off = desc->lock_offset;
  332. icst->ctype = ctype;
  333. clk = clk_register(dev, &icst->hw);
  334. if (IS_ERR(clk)) {
  335. kfree(pclone);
  336. kfree(icst);
  337. }
  338. return clk;
  339. }
  340. EXPORT_SYMBOL_GPL(icst_clk_setup);
  341. struct clk *icst_clk_register(struct device *dev,
  342. const struct clk_icst_desc *desc,
  343. const char *name,
  344. const char *parent_name,
  345. void __iomem *base)
  346. {
  347. struct regmap_config icst_regmap_conf = {
  348. .reg_bits = 32,
  349. .val_bits = 32,
  350. .reg_stride = 4,
  351. };
  352. struct regmap *map;
  353. map = regmap_init_mmio(dev, base, &icst_regmap_conf);
  354. if (IS_ERR(map)) {
  355. pr_err("could not initialize ICST regmap\n");
  356. return ERR_CAST(map);
  357. }
  358. return icst_clk_setup(dev, desc, name, parent_name, map,
  359. ICST_VERSATILE);
  360. }
  361. EXPORT_SYMBOL_GPL(icst_clk_register);
  362. #ifdef CONFIG_OF
  363. /*
  364. * In a device tree, an memory-mapped ICST clock appear as a child
  365. * of a syscon node. Assume this and probe it only as a child of a
  366. * syscon.
  367. */
  368. static const struct icst_params icst525_params = {
  369. .vco_max = ICST525_VCO_MAX_5V,
  370. .vco_min = ICST525_VCO_MIN,
  371. .vd_min = 8,
  372. .vd_max = 263,
  373. .rd_min = 3,
  374. .rd_max = 65,
  375. .s2div = icst525_s2div,
  376. .idx2s = icst525_idx2s,
  377. };
  378. static const struct icst_params icst307_params = {
  379. .vco_max = ICST307_VCO_MAX,
  380. .vco_min = ICST307_VCO_MIN,
  381. .vd_min = 4 + 8,
  382. .vd_max = 511 + 8,
  383. .rd_min = 1 + 2,
  384. .rd_max = 127 + 2,
  385. .s2div = icst307_s2div,
  386. .idx2s = icst307_idx2s,
  387. };
  388. /*
  389. * The core modules on the Integrator/AP and Integrator/CP have
  390. * especially crippled ICST525 control.
  391. */
  392. static const struct icst_params icst525_apcp_cm_params = {
  393. .vco_max = ICST525_VCO_MAX_5V,
  394. .vco_min = ICST525_VCO_MIN,
  395. /* Minimum 12 MHz, VDW = 4 */
  396. .vd_min = 12,
  397. /*
  398. * Maximum 160 MHz, VDW = 152 for all core modules, but
  399. * CM926EJ-S, CM1026EJ-S and CM1136JF-S can actually
  400. * go to 200 MHz (max VDW = 192).
  401. */
  402. .vd_max = 192,
  403. /* r is hardcoded to 22 and this is the actual divisor, +2 */
  404. .rd_min = 24,
  405. .rd_max = 24,
  406. .s2div = icst525_s2div,
  407. .idx2s = icst525_idx2s,
  408. };
  409. static const struct icst_params icst525_ap_sys_params = {
  410. .vco_max = ICST525_VCO_MAX_5V,
  411. .vco_min = ICST525_VCO_MIN,
  412. /* Minimum 3 MHz, VDW = 4 */
  413. .vd_min = 3,
  414. /* Maximum 50 MHz, VDW = 192 */
  415. .vd_max = 50,
  416. /* r is hardcoded to 46 and this is the actual divisor, +2 */
  417. .rd_min = 48,
  418. .rd_max = 48,
  419. .s2div = icst525_s2div,
  420. .idx2s = icst525_idx2s,
  421. };
  422. static const struct icst_params icst525_ap_pci_params = {
  423. .vco_max = ICST525_VCO_MAX_5V,
  424. .vco_min = ICST525_VCO_MIN,
  425. /* Minimum 25 MHz */
  426. .vd_min = 25,
  427. /* Maximum 33 MHz */
  428. .vd_max = 33,
  429. /* r is hardcoded to 14 or 22 and this is the actual divisors +2 */
  430. .rd_min = 16,
  431. .rd_max = 24,
  432. .s2div = icst525_s2div,
  433. .idx2s = icst525_idx2s,
  434. };
  435. static void __init of_syscon_icst_setup(struct device_node *np)
  436. {
  437. struct device_node *parent;
  438. struct regmap *map;
  439. struct clk_icst_desc icst_desc;
  440. const char *name;
  441. const char *parent_name;
  442. struct clk *regclk;
  443. enum icst_control_type ctype;
  444. /* We do not release this reference, we are using it perpetually */
  445. parent = of_get_parent(np);
  446. if (!parent) {
  447. pr_err("no parent node for syscon ICST clock\n");
  448. return;
  449. }
  450. map = syscon_node_to_regmap(parent);
  451. if (IS_ERR(map)) {
  452. pr_err("no regmap for syscon ICST clock parent\n");
  453. return;
  454. }
  455. if (of_property_read_u32(np, "reg", &icst_desc.vco_offset) &&
  456. of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) {
  457. pr_err("no VCO register offset for ICST clock\n");
  458. return;
  459. }
  460. if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) {
  461. pr_err("no lock register offset for ICST clock\n");
  462. return;
  463. }
  464. if (of_device_is_compatible(np, "arm,syscon-icst525")) {
  465. icst_desc.params = &icst525_params;
  466. ctype = ICST_VERSATILE;
  467. } else if (of_device_is_compatible(np, "arm,syscon-icst307")) {
  468. icst_desc.params = &icst307_params;
  469. ctype = ICST_VERSATILE;
  470. } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-cm")) {
  471. icst_desc.params = &icst525_apcp_cm_params;
  472. ctype = ICST_INTEGRATOR_AP_CM;
  473. } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-sys")) {
  474. icst_desc.params = &icst525_ap_sys_params;
  475. ctype = ICST_INTEGRATOR_AP_SYS;
  476. } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-pci")) {
  477. icst_desc.params = &icst525_ap_pci_params;
  478. ctype = ICST_INTEGRATOR_AP_PCI;
  479. } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorcp-cm-core")) {
  480. icst_desc.params = &icst525_apcp_cm_params;
  481. ctype = ICST_INTEGRATOR_CP_CM_CORE;
  482. } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorcp-cm-mem")) {
  483. icst_desc.params = &icst525_apcp_cm_params;
  484. ctype = ICST_INTEGRATOR_CP_CM_MEM;
  485. } else {
  486. pr_err("unknown ICST clock %pOF\n", np);
  487. return;
  488. }
  489. /* Parent clock name is not the same as node parent */
  490. parent_name = of_clk_get_parent_name(np, 0);
  491. name = kasprintf(GFP_KERNEL, "%pOFP", np);
  492. regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map, ctype);
  493. if (IS_ERR(regclk)) {
  494. pr_err("error setting up syscon ICST clock %s\n", name);
  495. kfree(name);
  496. return;
  497. }
  498. of_clk_add_provider(np, of_clk_src_simple_get, regclk);
  499. pr_debug("registered syscon ICST clock %s\n", name);
  500. }
  501. CLK_OF_DECLARE(arm_syscon_icst525_clk,
  502. "arm,syscon-icst525", of_syscon_icst_setup);
  503. CLK_OF_DECLARE(arm_syscon_icst307_clk,
  504. "arm,syscon-icst307", of_syscon_icst_setup);
  505. CLK_OF_DECLARE(arm_syscon_integratorap_cm_clk,
  506. "arm,syscon-icst525-integratorap-cm", of_syscon_icst_setup);
  507. CLK_OF_DECLARE(arm_syscon_integratorap_sys_clk,
  508. "arm,syscon-icst525-integratorap-sys", of_syscon_icst_setup);
  509. CLK_OF_DECLARE(arm_syscon_integratorap_pci_clk,
  510. "arm,syscon-icst525-integratorap-pci", of_syscon_icst_setup);
  511. CLK_OF_DECLARE(arm_syscon_integratorcp_cm_core_clk,
  512. "arm,syscon-icst525-integratorcp-cm-core", of_syscon_icst_setup);
  513. CLK_OF_DECLARE(arm_syscon_integratorcp_cm_mem_clk,
  514. "arm,syscon-icst525-integratorcp-cm-mem", of_syscon_icst_setup);
  515. #endif