clk-eyeq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PLL clock driver for the Mobileye EyeQ5, EyeQ6L and EyeQ6H platforms.
  4. *
  5. * This controller handles:
  6. * - Read-only PLLs, all derived from the same main crystal clock.
  7. * - It also exposes divider clocks, those are children to PLLs.
  8. * - Fixed factor clocks, children to PLLs.
  9. *
  10. * Parent clock is expected to be constant. This driver's registers live in a
  11. * shared region called OLB. Some PLLs and fixed-factors are initialised early
  12. * by of_clk_init(); if so, two clk providers are registered.
  13. *
  14. * We use eqc_ as prefix, as-in "EyeQ Clock", but way shorter.
  15. *
  16. * Copyright (C) 2024 Mobileye Vision Technologies Ltd.
  17. */
  18. /*
  19. * Set pr_fmt() for printing from eqc_early_init().
  20. * It is called at of_clk_init() stage (read: really early).
  21. */
  22. #define pr_fmt(fmt) "clk-eyeq: " fmt
  23. #include <linux/array_size.h>
  24. #include <linux/auxiliary_bus.h>
  25. #include <linux/bitfield.h>
  26. #include <linux/bits.h>
  27. #include <linux/clk-provider.h>
  28. #include <linux/device.h>
  29. #include <linux/err.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/io-64-nonatomic-hi-lo.h>
  33. #include <linux/io.h>
  34. #include <linux/mod_devicetable.h>
  35. #include <linux/module.h>
  36. #include <linux/of.h>
  37. #include <linux/of_address.h>
  38. #include <linux/overflow.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/printk.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/types.h>
  44. #include <dt-bindings/clock/mobileye,eyeq5-clk.h>
  45. /* In frac mode, it enables fractional noise canceling DAC. Else, no function. */
  46. #define PCSR0_DAC_EN BIT(0)
  47. /* Fractional or integer mode */
  48. #define PCSR0_DSM_EN BIT(1)
  49. #define PCSR0_PLL_EN BIT(2)
  50. /* All clocks output held at 0 */
  51. #define PCSR0_FOUTPOSTDIV_EN BIT(3)
  52. #define PCSR0_POST_DIV1 GENMASK(6, 4)
  53. #define PCSR0_POST_DIV2 GENMASK(9, 7)
  54. #define PCSR0_REF_DIV GENMASK(15, 10)
  55. #define PCSR0_INTIN GENMASK(27, 16)
  56. #define PCSR0_BYPASS BIT(28)
  57. /* Bits 30..29 are reserved */
  58. #define PCSR0_PLL_LOCKED BIT(31)
  59. #define PCSR1_RESET BIT(0)
  60. #define PCSR1_SSGC_DIV GENMASK(4, 1)
  61. /* Spread amplitude (% = 0.1 * SPREAD[4:0]) */
  62. #define PCSR1_SPREAD GENMASK(9, 5)
  63. #define PCSR1_DIS_SSCG BIT(10)
  64. /* Down-spread or center-spread */
  65. #define PCSR1_DOWN_SPREAD BIT(11)
  66. #define PCSR1_FRAC_IN GENMASK(31, 12)
  67. struct eqc_pll {
  68. unsigned int index;
  69. const char *name;
  70. unsigned int reg64;
  71. };
  72. /*
  73. * Divider clock. Divider is 2*(v+1), with v the register value.
  74. * Min divider is 2, max is 2*(2^width).
  75. */
  76. struct eqc_div {
  77. unsigned int index;
  78. const char *name;
  79. unsigned int parent;
  80. unsigned int reg;
  81. u8 shift;
  82. u8 width;
  83. };
  84. struct eqc_fixed_factor {
  85. unsigned int index;
  86. const char *name;
  87. unsigned int mult;
  88. unsigned int div;
  89. unsigned int parent;
  90. };
  91. struct eqc_match_data {
  92. unsigned int pll_count;
  93. const struct eqc_pll *plls;
  94. unsigned int div_count;
  95. const struct eqc_div *divs;
  96. unsigned int fixed_factor_count;
  97. const struct eqc_fixed_factor *fixed_factors;
  98. const char *reset_auxdev_name;
  99. const char *pinctrl_auxdev_name;
  100. unsigned int early_clk_count;
  101. };
  102. struct eqc_early_match_data {
  103. unsigned int early_pll_count;
  104. const struct eqc_pll *early_plls;
  105. unsigned int early_fixed_factor_count;
  106. const struct eqc_fixed_factor *early_fixed_factors;
  107. /*
  108. * We want our of_xlate callback to EPROBE_DEFER instead of dev_err()
  109. * and EINVAL. For that, we must know the total clock count.
  110. */
  111. unsigned int late_clk_count;
  112. };
  113. /*
  114. * Both factors (mult and div) must fit in 32 bits. When an operation overflows,
  115. * this function throws away low bits so that factors still fit in 32 bits.
  116. *
  117. * Precision loss depends on amplitude of mult and div. Worst theoretical
  118. * loss is: (UINT_MAX+1) / UINT_MAX - 1 = 2.3e-10.
  119. * This is 1Hz every 4.3GHz.
  120. */
  121. static void eqc_pll_downshift_factors(unsigned long *mult, unsigned long *div)
  122. {
  123. unsigned long biggest;
  124. unsigned int shift;
  125. /* This function can be removed if mult/div switch to unsigned long. */
  126. static_assert(sizeof_field(struct clk_fixed_factor, mult) == sizeof(unsigned int));
  127. static_assert(sizeof_field(struct clk_fixed_factor, div) == sizeof(unsigned int));
  128. /* No overflow, nothing to be done. */
  129. if (*mult <= UINT_MAX && *div <= UINT_MAX)
  130. return;
  131. /*
  132. * Compute the shift required to bring the biggest factor into unsigned
  133. * int range. That is, shift its highest set bit to the unsigned int
  134. * most significant bit.
  135. */
  136. biggest = max(*mult, *div);
  137. shift = __fls(biggest) - (BITS_PER_BYTE * sizeof(unsigned int)) + 1;
  138. *mult >>= shift;
  139. *div >>= shift;
  140. }
  141. static int eqc_pll_parse_registers(u32 r0, u32 r1, unsigned long *mult,
  142. unsigned long *div, unsigned long *acc)
  143. {
  144. u32 spread;
  145. if (r0 & PCSR0_BYPASS) {
  146. *mult = 1;
  147. *div = 1;
  148. *acc = 0;
  149. return 0;
  150. }
  151. if (!(r0 & PCSR0_PLL_LOCKED))
  152. return -EINVAL;
  153. *mult = FIELD_GET(PCSR0_INTIN, r0);
  154. *div = FIELD_GET(PCSR0_REF_DIV, r0);
  155. if (r0 & PCSR0_FOUTPOSTDIV_EN)
  156. *div *= FIELD_GET(PCSR0_POST_DIV1, r0) * FIELD_GET(PCSR0_POST_DIV2, r0);
  157. /* Fractional mode, in 2^20 (0x100000) parts. */
  158. if (r0 & PCSR0_DSM_EN) {
  159. *div *= (1ULL << 20);
  160. *mult = *mult * (1ULL << 20) + FIELD_GET(PCSR1_FRAC_IN, r1);
  161. }
  162. if (!*mult || !*div)
  163. return -EINVAL;
  164. if (r1 & (PCSR1_RESET | PCSR1_DIS_SSCG)) {
  165. *acc = 0;
  166. return 0;
  167. }
  168. /*
  169. * Spread spectrum.
  170. *
  171. * Spread is 1/1000 parts of frequency, accuracy is half of
  172. * that. To get accuracy, convert to ppb (parts per billion).
  173. *
  174. * acc = spread * 1e6 / 2
  175. * with acc in parts per billion and,
  176. * spread in parts per thousand.
  177. */
  178. spread = FIELD_GET(PCSR1_SPREAD, r1);
  179. *acc = spread * 500000;
  180. if (r1 & PCSR1_DOWN_SPREAD) {
  181. /*
  182. * Downspreading: the central frequency is half a
  183. * spread lower.
  184. */
  185. *mult *= 2000 - spread;
  186. *div *= 2000;
  187. /*
  188. * Previous operation might overflow 32 bits. If it
  189. * does, throw away the least amount of low bits.
  190. */
  191. eqc_pll_downshift_factors(mult, div);
  192. }
  193. return 0;
  194. }
  195. static void eqc_probe_init_plls(struct device *dev, const struct eqc_match_data *data,
  196. void __iomem *base, struct clk_hw_onecell_data *cells)
  197. {
  198. unsigned long mult, div, acc;
  199. const struct eqc_pll *pll;
  200. struct clk_hw *hw;
  201. unsigned int i;
  202. u32 r0, r1;
  203. u64 val;
  204. int ret;
  205. for (i = 0; i < data->pll_count; i++) {
  206. pll = &data->plls[i];
  207. val = readq(base + pll->reg64);
  208. r0 = val;
  209. r1 = val >> 32;
  210. ret = eqc_pll_parse_registers(r0, r1, &mult, &div, &acc);
  211. if (ret) {
  212. dev_warn(dev, "failed parsing state of %s\n", pll->name);
  213. cells->hws[pll->index] = ERR_PTR(ret);
  214. continue;
  215. }
  216. hw = clk_hw_register_fixed_factor_with_accuracy_fwname(dev,
  217. dev->of_node, pll->name, "ref", 0, mult, div, acc);
  218. cells->hws[pll->index] = hw;
  219. if (IS_ERR(hw))
  220. dev_warn(dev, "failed registering %s: %pe\n", pll->name, hw);
  221. }
  222. }
  223. static void eqc_probe_init_divs(struct device *dev, const struct eqc_match_data *data,
  224. void __iomem *base, struct clk_hw_onecell_data *cells)
  225. {
  226. struct clk_parent_data parent_data = { };
  227. const struct eqc_div *div;
  228. struct clk_hw *parent;
  229. void __iomem *reg;
  230. struct clk_hw *hw;
  231. unsigned int i;
  232. for (i = 0; i < data->div_count; i++) {
  233. div = &data->divs[i];
  234. reg = base + div->reg;
  235. parent = cells->hws[div->parent];
  236. if (IS_ERR(parent)) {
  237. /* Parent is in early clk provider. */
  238. parent_data.index = div->parent;
  239. parent_data.hw = NULL;
  240. } else {
  241. /* Avoid clock lookup when we already have the hw reference. */
  242. parent_data.index = 0;
  243. parent_data.hw = parent;
  244. }
  245. hw = clk_hw_register_divider_table_parent_data(dev, div->name,
  246. &parent_data, 0, reg, div->shift, div->width,
  247. CLK_DIVIDER_EVEN_INTEGERS, NULL, NULL);
  248. cells->hws[div->index] = hw;
  249. if (IS_ERR(hw))
  250. dev_warn(dev, "failed registering %s: %pe\n",
  251. div->name, hw);
  252. }
  253. }
  254. static void eqc_probe_init_fixed_factors(struct device *dev,
  255. const struct eqc_match_data *data,
  256. struct clk_hw_onecell_data *cells)
  257. {
  258. const struct eqc_fixed_factor *ff;
  259. struct clk_hw *hw, *parent_hw;
  260. unsigned int i;
  261. for (i = 0; i < data->fixed_factor_count; i++) {
  262. ff = &data->fixed_factors[i];
  263. parent_hw = cells->hws[ff->parent];
  264. if (IS_ERR(parent_hw)) {
  265. /* Parent is in early clk provider. */
  266. hw = clk_hw_register_fixed_factor_index(dev, ff->name,
  267. ff->parent, 0, ff->mult, ff->div);
  268. } else {
  269. /* Avoid clock lookup when we already have the hw reference. */
  270. hw = clk_hw_register_fixed_factor_parent_hw(dev, ff->name,
  271. parent_hw, 0, ff->mult, ff->div);
  272. }
  273. cells->hws[ff->index] = hw;
  274. if (IS_ERR(hw))
  275. dev_warn(dev, "failed registering %s: %pe\n",
  276. ff->name, hw);
  277. }
  278. }
  279. static void eqc_auxdev_release(struct device *dev)
  280. {
  281. struct auxiliary_device *adev = to_auxiliary_dev(dev);
  282. kfree(adev);
  283. }
  284. static int eqc_auxdev_create(struct device *dev, void __iomem *base,
  285. const char *name, u32 id)
  286. {
  287. struct auxiliary_device *adev;
  288. int ret;
  289. adev = kzalloc_obj(*adev);
  290. if (!adev)
  291. return -ENOMEM;
  292. adev->name = name;
  293. adev->dev.parent = dev;
  294. adev->dev.platform_data = (void __force *)base;
  295. adev->dev.release = eqc_auxdev_release;
  296. adev->id = id;
  297. ret = auxiliary_device_init(adev);
  298. if (ret)
  299. return ret;
  300. ret = auxiliary_device_add(adev);
  301. if (ret)
  302. auxiliary_device_uninit(adev);
  303. return ret;
  304. }
  305. static int eqc_probe(struct platform_device *pdev)
  306. {
  307. struct device *dev = &pdev->dev;
  308. struct device_node *np = dev->of_node;
  309. const struct eqc_match_data *data;
  310. struct clk_hw_onecell_data *cells;
  311. unsigned int i, clk_count;
  312. struct resource *res;
  313. void __iomem *base;
  314. int ret;
  315. data = device_get_match_data(dev);
  316. if (!data)
  317. return 0; /* No clocks nor auxdevs, we are done. */
  318. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  319. if (!res)
  320. return -ENODEV;
  321. base = ioremap(res->start, resource_size(res));
  322. if (!base)
  323. return -ENOMEM;
  324. /* Init optional reset auxiliary device. */
  325. if (data->reset_auxdev_name) {
  326. ret = eqc_auxdev_create(dev, base, data->reset_auxdev_name, 0);
  327. if (ret)
  328. dev_warn(dev, "failed creating auxiliary device %s.%s: %d\n",
  329. KBUILD_MODNAME, data->reset_auxdev_name, ret);
  330. }
  331. /* Init optional pinctrl auxiliary device. */
  332. if (data->pinctrl_auxdev_name) {
  333. ret = eqc_auxdev_create(dev, base, data->pinctrl_auxdev_name, 0);
  334. if (ret)
  335. dev_warn(dev, "failed creating auxiliary device %s.%s: %d\n",
  336. KBUILD_MODNAME, data->pinctrl_auxdev_name, ret);
  337. }
  338. if (data->pll_count + data->div_count + data->fixed_factor_count == 0)
  339. return 0; /* Zero clocks, we are done. */
  340. clk_count = data->pll_count + data->div_count +
  341. data->fixed_factor_count + data->early_clk_count;
  342. cells = kzalloc_flex(*cells, hws, clk_count);
  343. if (!cells)
  344. return -ENOMEM;
  345. cells->num = clk_count;
  346. /* Early PLLs are marked as errors: the early provider will get queried. */
  347. for (i = 0; i < clk_count; i++)
  348. cells->hws[i] = ERR_PTR(-EINVAL);
  349. eqc_probe_init_plls(dev, data, base, cells);
  350. eqc_probe_init_divs(dev, data, base, cells);
  351. eqc_probe_init_fixed_factors(dev, data, cells);
  352. return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, cells);
  353. }
  354. /* Required early for GIC timer (pll-cpu) and UARTs (pll-per). */
  355. static const struct eqc_pll eqc_eyeq5_early_plls[] = {
  356. { .index = EQ5C_PLL_CPU, .name = "pll-cpu", .reg64 = 0x02C },
  357. { .index = EQ5C_PLL_PER, .name = "pll-per", .reg64 = 0x05C },
  358. };
  359. static const struct eqc_pll eqc_eyeq5_plls[] = {
  360. { .index = EQ5C_PLL_VMP, .name = "pll-vmp", .reg64 = 0x034 },
  361. { .index = EQ5C_PLL_PMA, .name = "pll-pma", .reg64 = 0x03C },
  362. { .index = EQ5C_PLL_VDI, .name = "pll-vdi", .reg64 = 0x044 },
  363. { .index = EQ5C_PLL_DDR0, .name = "pll-ddr0", .reg64 = 0x04C },
  364. { .index = EQ5C_PLL_PCI, .name = "pll-pci", .reg64 = 0x054 },
  365. { .index = EQ5C_PLL_PMAC, .name = "pll-pmac", .reg64 = 0x064 },
  366. { .index = EQ5C_PLL_MPC, .name = "pll-mpc", .reg64 = 0x06C },
  367. { .index = EQ5C_PLL_DDR1, .name = "pll-ddr1", .reg64 = 0x074 },
  368. };
  369. enum {
  370. /*
  371. * EQ5C_PLL_CPU children.
  372. * EQ5C_PER_OCC_PCI is the last clock exposed in dt-bindings.
  373. */
  374. EQ5C_CPU_OCC = EQ5C_PER_OCC_PCI + 1,
  375. EQ5C_CPU_SI_CSS0,
  376. EQ5C_CPU_CPC,
  377. EQ5C_CPU_CM,
  378. EQ5C_CPU_MEM,
  379. EQ5C_CPU_OCC_ISRAM,
  380. EQ5C_CPU_ISRAM,
  381. EQ5C_CPU_OCC_DBU,
  382. EQ5C_CPU_SI_DBU_TP,
  383. /*
  384. * EQ5C_PLL_VDI children.
  385. */
  386. EQ5C_VDI_OCC_VDI,
  387. EQ5C_VDI_VDI,
  388. EQ5C_VDI_OCC_CAN_SER,
  389. EQ5C_VDI_CAN_SER,
  390. EQ5C_VDI_I2C_SER,
  391. /*
  392. * EQ5C_PLL_PER children.
  393. */
  394. EQ5C_PER_PERIPH,
  395. EQ5C_PER_CAN,
  396. EQ5C_PER_TIMER,
  397. EQ5C_PER_CCF,
  398. EQ5C_PER_OCC_MJPEG,
  399. EQ5C_PER_HSM,
  400. EQ5C_PER_MJPEG,
  401. EQ5C_PER_FCMU_A,
  402. };
  403. static const struct eqc_fixed_factor eqc_eyeq5_early_fixed_factors[] = {
  404. /* EQ5C_PLL_CPU children */
  405. { EQ5C_CPU_OCC, "occ-cpu", 1, 1, EQ5C_PLL_CPU },
  406. { EQ5C_CPU_SI_CSS0, "si-css0", 1, 1, EQ5C_CPU_OCC },
  407. { EQ5C_CPU_CORE0, "core0", 1, 1, EQ5C_CPU_SI_CSS0 },
  408. { EQ5C_CPU_CORE1, "core1", 1, 1, EQ5C_CPU_SI_CSS0 },
  409. { EQ5C_CPU_CORE2, "core2", 1, 1, EQ5C_CPU_SI_CSS0 },
  410. { EQ5C_CPU_CORE3, "core3", 1, 1, EQ5C_CPU_SI_CSS0 },
  411. /* EQ5C_PLL_PER children */
  412. { EQ5C_PER_OCC, "occ-periph", 1, 16, EQ5C_PLL_PER },
  413. { EQ5C_PER_UART, "uart", 1, 1, EQ5C_PER_OCC },
  414. };
  415. static const struct eqc_fixed_factor eqc_eyeq5_fixed_factors[] = {
  416. /* EQ5C_PLL_CPU children */
  417. { EQ5C_CPU_CPC, "cpc", 1, 1, EQ5C_CPU_SI_CSS0 },
  418. { EQ5C_CPU_CM, "cm", 1, 1, EQ5C_CPU_SI_CSS0 },
  419. { EQ5C_CPU_MEM, "mem", 1, 1, EQ5C_CPU_SI_CSS0 },
  420. { EQ5C_CPU_OCC_ISRAM, "occ-isram", 1, 2, EQ5C_PLL_CPU },
  421. { EQ5C_CPU_ISRAM, "isram", 1, 1, EQ5C_CPU_OCC_ISRAM },
  422. { EQ5C_CPU_OCC_DBU, "occ-dbu", 1, 10, EQ5C_PLL_CPU },
  423. { EQ5C_CPU_SI_DBU_TP, "si-dbu-tp", 1, 1, EQ5C_CPU_OCC_DBU },
  424. /* EQ5C_PLL_VDI children */
  425. { EQ5C_VDI_OCC_VDI, "occ-vdi", 1, 2, EQ5C_PLL_VDI },
  426. { EQ5C_VDI_VDI, "vdi", 1, 1, EQ5C_VDI_OCC_VDI },
  427. { EQ5C_VDI_OCC_CAN_SER, "occ-can-ser", 1, 16, EQ5C_PLL_VDI },
  428. { EQ5C_VDI_CAN_SER, "can-ser", 1, 1, EQ5C_VDI_OCC_CAN_SER },
  429. { EQ5C_VDI_I2C_SER, "i2c-ser", 1, 20, EQ5C_PLL_VDI },
  430. /* EQ5C_PLL_PER children */
  431. { EQ5C_PER_PERIPH, "periph", 1, 1, EQ5C_PER_OCC },
  432. { EQ5C_PER_CAN, "can", 1, 1, EQ5C_PER_OCC },
  433. { EQ5C_PER_SPI, "spi", 1, 1, EQ5C_PER_OCC },
  434. { EQ5C_PER_I2C, "i2c", 1, 1, EQ5C_PER_OCC },
  435. { EQ5C_PER_TIMER, "timer", 1, 1, EQ5C_PER_OCC },
  436. { EQ5C_PER_GPIO, "gpio", 1, 1, EQ5C_PER_OCC },
  437. { EQ5C_PER_EMMC, "emmc-sys", 1, 10, EQ5C_PLL_PER },
  438. { EQ5C_PER_CCF, "ccf-ctrl", 1, 4, EQ5C_PLL_PER },
  439. { EQ5C_PER_OCC_MJPEG, "occ-mjpeg", 1, 2, EQ5C_PLL_PER },
  440. { EQ5C_PER_HSM, "hsm", 1, 1, EQ5C_PER_OCC_MJPEG },
  441. { EQ5C_PER_MJPEG, "mjpeg", 1, 1, EQ5C_PER_OCC_MJPEG },
  442. { EQ5C_PER_FCMU_A, "fcmu-a", 1, 20, EQ5C_PLL_PER },
  443. { EQ5C_PER_OCC_PCI, "occ-pci-sys", 1, 8, EQ5C_PLL_PER },
  444. };
  445. static const struct eqc_div eqc_eyeq5_divs[] = {
  446. {
  447. .index = EQ5C_DIV_OSPI,
  448. .name = "div-ospi",
  449. .parent = EQ5C_PLL_PER,
  450. .reg = 0x11C,
  451. .shift = 0,
  452. .width = 4,
  453. },
  454. };
  455. static const struct eqc_early_match_data eqc_eyeq5_early_match_data __initconst = {
  456. .early_pll_count = ARRAY_SIZE(eqc_eyeq5_early_plls),
  457. .early_plls = eqc_eyeq5_early_plls,
  458. .early_fixed_factor_count = ARRAY_SIZE(eqc_eyeq5_early_fixed_factors),
  459. .early_fixed_factors = eqc_eyeq5_early_fixed_factors,
  460. .late_clk_count = ARRAY_SIZE(eqc_eyeq5_plls) + ARRAY_SIZE(eqc_eyeq5_divs) +
  461. ARRAY_SIZE(eqc_eyeq5_fixed_factors),
  462. };
  463. static const struct eqc_match_data eqc_eyeq5_match_data = {
  464. .pll_count = ARRAY_SIZE(eqc_eyeq5_plls),
  465. .plls = eqc_eyeq5_plls,
  466. .div_count = ARRAY_SIZE(eqc_eyeq5_divs),
  467. .divs = eqc_eyeq5_divs,
  468. .fixed_factor_count = ARRAY_SIZE(eqc_eyeq5_fixed_factors),
  469. .fixed_factors = eqc_eyeq5_fixed_factors,
  470. .reset_auxdev_name = "reset",
  471. .pinctrl_auxdev_name = "pinctrl",
  472. .early_clk_count = ARRAY_SIZE(eqc_eyeq5_early_plls) +
  473. ARRAY_SIZE(eqc_eyeq5_early_fixed_factors),
  474. };
  475. static const struct eqc_pll eqc_eyeq6l_plls[] = {
  476. { .index = EQ6LC_PLL_DDR, .name = "pll-ddr", .reg64 = 0x02C },
  477. { .index = EQ6LC_PLL_CPU, .name = "pll-cpu", .reg64 = 0x034 }, /* also acc */
  478. { .index = EQ6LC_PLL_PER, .name = "pll-per", .reg64 = 0x03C },
  479. { .index = EQ6LC_PLL_VDI, .name = "pll-vdi", .reg64 = 0x044 },
  480. };
  481. static const struct eqc_match_data eqc_eyeq6l_match_data = {
  482. .pll_count = ARRAY_SIZE(eqc_eyeq6l_plls),
  483. .plls = eqc_eyeq6l_plls,
  484. .reset_auxdev_name = "reset",
  485. };
  486. static const struct eqc_match_data eqc_eyeq6h_west_match_data = {
  487. .reset_auxdev_name = "reset_west",
  488. };
  489. static const struct eqc_pll eqc_eyeq6h_east_plls[] = {
  490. { .index = 0, .name = "pll-east", .reg64 = 0x074 },
  491. };
  492. static const struct eqc_match_data eqc_eyeq6h_east_match_data = {
  493. .pll_count = ARRAY_SIZE(eqc_eyeq6h_east_plls),
  494. .plls = eqc_eyeq6h_east_plls,
  495. .reset_auxdev_name = "reset_east",
  496. };
  497. static const struct eqc_pll eqc_eyeq6h_south_plls[] = {
  498. { .index = EQ6HC_SOUTH_PLL_VDI, .name = "pll-vdi", .reg64 = 0x000 },
  499. { .index = EQ6HC_SOUTH_PLL_PCIE, .name = "pll-pcie", .reg64 = 0x008 },
  500. { .index = EQ6HC_SOUTH_PLL_PER, .name = "pll-per", .reg64 = 0x010 },
  501. { .index = EQ6HC_SOUTH_PLL_ISP, .name = "pll-isp", .reg64 = 0x018 },
  502. };
  503. static const struct eqc_div eqc_eyeq6h_south_divs[] = {
  504. {
  505. .index = EQ6HC_SOUTH_DIV_EMMC,
  506. .name = "div-emmc",
  507. .parent = EQ6HC_SOUTH_PLL_PER,
  508. .reg = 0x070,
  509. .shift = 4,
  510. .width = 4,
  511. },
  512. {
  513. .index = EQ6HC_SOUTH_DIV_OSPI_REF,
  514. .name = "div-ospi-ref",
  515. .parent = EQ6HC_SOUTH_PLL_PER,
  516. .reg = 0x090,
  517. .shift = 4,
  518. .width = 4,
  519. },
  520. {
  521. .index = EQ6HC_SOUTH_DIV_OSPI_SYS,
  522. .name = "div-ospi-sys",
  523. .parent = EQ6HC_SOUTH_PLL_PER,
  524. .reg = 0x090,
  525. .shift = 8,
  526. .width = 1,
  527. },
  528. {
  529. .index = EQ6HC_SOUTH_DIV_TSU,
  530. .name = "div-tsu",
  531. .parent = EQ6HC_SOUTH_PLL_PCIE,
  532. .reg = 0x098,
  533. .shift = 4,
  534. .width = 8,
  535. },
  536. };
  537. static const struct eqc_match_data eqc_eyeq6h_south_match_data = {
  538. .pll_count = ARRAY_SIZE(eqc_eyeq6h_south_plls),
  539. .plls = eqc_eyeq6h_south_plls,
  540. .div_count = ARRAY_SIZE(eqc_eyeq6h_south_divs),
  541. .divs = eqc_eyeq6h_south_divs,
  542. };
  543. static const struct eqc_pll eqc_eyeq6h_ddr0_plls[] = {
  544. { .index = 0, .name = "pll-ddr0", .reg64 = 0x074 },
  545. };
  546. static const struct eqc_match_data eqc_eyeq6h_ddr0_match_data = {
  547. .pll_count = ARRAY_SIZE(eqc_eyeq6h_ddr0_plls),
  548. .plls = eqc_eyeq6h_ddr0_plls,
  549. };
  550. static const struct eqc_pll eqc_eyeq6h_ddr1_plls[] = {
  551. { .index = 0, .name = "pll-ddr1", .reg64 = 0x074 },
  552. };
  553. static const struct eqc_match_data eqc_eyeq6h_ddr1_match_data = {
  554. .pll_count = ARRAY_SIZE(eqc_eyeq6h_ddr1_plls),
  555. .plls = eqc_eyeq6h_ddr1_plls,
  556. };
  557. static const struct eqc_pll eqc_eyeq6h_acc_plls[] = {
  558. { .index = EQ6HC_ACC_PLL_XNN, .name = "pll-xnn", .reg64 = 0x040 },
  559. { .index = EQ6HC_ACC_PLL_VMP, .name = "pll-vmp", .reg64 = 0x050 },
  560. { .index = EQ6HC_ACC_PLL_PMA, .name = "pll-pma", .reg64 = 0x05C },
  561. { .index = EQ6HC_ACC_PLL_MPC, .name = "pll-mpc", .reg64 = 0x068 },
  562. { .index = EQ6HC_ACC_PLL_NOC, .name = "pll-noc", .reg64 = 0x070 },
  563. };
  564. static const struct eqc_match_data eqc_eyeq6h_acc_match_data = {
  565. .pll_count = ARRAY_SIZE(eqc_eyeq6h_acc_plls),
  566. .plls = eqc_eyeq6h_acc_plls,
  567. .reset_auxdev_name = "reset_acc",
  568. };
  569. static const struct of_device_id eqc_match_table[] = {
  570. { .compatible = "mobileye,eyeq5-olb", .data = &eqc_eyeq5_match_data },
  571. { .compatible = "mobileye,eyeq6l-olb", .data = &eqc_eyeq6l_match_data },
  572. { .compatible = "mobileye,eyeq6h-west-olb", .data = &eqc_eyeq6h_west_match_data },
  573. { .compatible = "mobileye,eyeq6h-east-olb", .data = &eqc_eyeq6h_east_match_data },
  574. { .compatible = "mobileye,eyeq6h-south-olb", .data = &eqc_eyeq6h_south_match_data },
  575. { .compatible = "mobileye,eyeq6h-ddr0-olb", .data = &eqc_eyeq6h_ddr0_match_data },
  576. { .compatible = "mobileye,eyeq6h-ddr1-olb", .data = &eqc_eyeq6h_ddr1_match_data },
  577. { .compatible = "mobileye,eyeq6h-acc-olb", .data = &eqc_eyeq6h_acc_match_data },
  578. {}
  579. };
  580. static struct platform_driver eqc_driver = {
  581. .probe = eqc_probe,
  582. .driver = {
  583. .name = "clk-eyeq",
  584. .of_match_table = eqc_match_table,
  585. .suppress_bind_attrs = true,
  586. },
  587. };
  588. builtin_platform_driver(eqc_driver);
  589. /* Required early for GIC timer. */
  590. static const struct eqc_pll eqc_eyeq6h_central_early_plls[] = {
  591. { .index = EQ6HC_CENTRAL_PLL_CPU, .name = "pll-cpu", .reg64 = 0x02C },
  592. };
  593. static const struct eqc_fixed_factor eqc_eyeq6h_central_early_fixed_factors[] = {
  594. { EQ6HC_CENTRAL_CPU_OCC, "occ-cpu", 1, 1, EQ6HC_CENTRAL_PLL_CPU },
  595. };
  596. static const struct eqc_early_match_data eqc_eyeq6h_central_early_match_data __initconst = {
  597. .early_pll_count = ARRAY_SIZE(eqc_eyeq6h_central_early_plls),
  598. .early_plls = eqc_eyeq6h_central_early_plls,
  599. .early_fixed_factor_count = ARRAY_SIZE(eqc_eyeq6h_central_early_fixed_factors),
  600. .early_fixed_factors = eqc_eyeq6h_central_early_fixed_factors,
  601. };
  602. /* Required early for UART. */
  603. static const struct eqc_pll eqc_eyeq6h_west_early_plls[] = {
  604. { .index = EQ6HC_WEST_PLL_PER, .name = "pll-west", .reg64 = 0x074 },
  605. };
  606. static const struct eqc_fixed_factor eqc_eyeq6h_west_early_fixed_factors[] = {
  607. { EQ6HC_WEST_PER_OCC, "west-per-occ", 1, 10, EQ6HC_WEST_PLL_PER },
  608. { EQ6HC_WEST_PER_UART, "west-per-uart", 1, 1, EQ6HC_WEST_PER_OCC },
  609. };
  610. static const struct eqc_early_match_data eqc_eyeq6h_west_early_match_data __initconst = {
  611. .early_pll_count = ARRAY_SIZE(eqc_eyeq6h_west_early_plls),
  612. .early_plls = eqc_eyeq6h_west_early_plls,
  613. .early_fixed_factor_count = ARRAY_SIZE(eqc_eyeq6h_west_early_fixed_factors),
  614. .early_fixed_factors = eqc_eyeq6h_west_early_fixed_factors,
  615. };
  616. static void __init eqc_early_init(struct device_node *np,
  617. const struct eqc_early_match_data *early_data)
  618. {
  619. struct clk_hw_onecell_data *cells;
  620. unsigned int i, clk_count;
  621. void __iomem *base;
  622. int ret;
  623. clk_count = early_data->early_pll_count + early_data->early_fixed_factor_count +
  624. early_data->late_clk_count;
  625. cells = kzalloc_flex(*cells, hws, clk_count);
  626. if (!cells) {
  627. ret = -ENOMEM;
  628. goto err;
  629. }
  630. cells->num = clk_count;
  631. /*
  632. * Mark all clocks as deferred; some are registered here, the rest at
  633. * platform device probe.
  634. *
  635. * Once the platform device is probed, its provider will take priority
  636. * when looking up clocks.
  637. */
  638. for (i = 0; i < clk_count; i++)
  639. cells->hws[i] = ERR_PTR(-EPROBE_DEFER);
  640. /* Offsets (reg64) of early PLLs are relative to OLB block. */
  641. base = of_iomap(np, 0);
  642. if (!base) {
  643. ret = -ENODEV;
  644. goto err;
  645. }
  646. for (i = 0; i < early_data->early_pll_count; i++) {
  647. const struct eqc_pll *pll = &early_data->early_plls[i];
  648. unsigned long mult, div, acc;
  649. struct clk_hw *hw;
  650. u32 r0, r1;
  651. u64 val;
  652. val = readq(base + pll->reg64);
  653. r0 = val;
  654. r1 = val >> 32;
  655. ret = eqc_pll_parse_registers(r0, r1, &mult, &div, &acc);
  656. if (ret) {
  657. pr_err("failed parsing state of %s\n", pll->name);
  658. goto err;
  659. }
  660. hw = clk_hw_register_fixed_factor_with_accuracy_fwname(NULL,
  661. np, pll->name, "ref", 0, mult, div, acc);
  662. cells->hws[pll->index] = hw;
  663. if (IS_ERR(hw)) {
  664. pr_err("failed registering %s: %pe\n", pll->name, hw);
  665. ret = PTR_ERR(hw);
  666. goto err;
  667. }
  668. }
  669. for (i = 0; i < early_data->early_fixed_factor_count; i++) {
  670. const struct eqc_fixed_factor *ff = &early_data->early_fixed_factors[i];
  671. struct clk_hw *parent_hw = cells->hws[ff->parent];
  672. struct clk_hw *hw;
  673. hw = clk_hw_register_fixed_factor_parent_hw(NULL, ff->name,
  674. parent_hw, 0, ff->mult, ff->div);
  675. cells->hws[ff->index] = hw;
  676. if (IS_ERR(hw)) {
  677. pr_err("failed registering %s: %pe\n", ff->name, hw);
  678. ret = PTR_ERR(hw);
  679. goto err;
  680. }
  681. }
  682. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, cells);
  683. if (ret) {
  684. pr_err("failed registering clk provider: %d\n", ret);
  685. goto err;
  686. }
  687. return;
  688. err:
  689. /*
  690. * We are doomed. The system will not be able to boot.
  691. *
  692. * Let's still try to be good citizens by freeing resources and print
  693. * a last error message that might help debugging.
  694. */
  695. pr_err("failed clk init: %d\n", ret);
  696. if (cells) {
  697. of_clk_del_provider(np);
  698. for (i = 0; i < early_data->early_pll_count; i++) {
  699. const struct eqc_pll *pll = &early_data->early_plls[i];
  700. struct clk_hw *hw = cells->hws[pll->index];
  701. if (!IS_ERR_OR_NULL(hw))
  702. clk_hw_unregister_fixed_factor(hw);
  703. }
  704. kfree(cells);
  705. }
  706. }
  707. static void __init eqc_eyeq5_early_init(struct device_node *np)
  708. {
  709. eqc_early_init(np, &eqc_eyeq5_early_match_data);
  710. }
  711. CLK_OF_DECLARE_DRIVER(eqc_eyeq5, "mobileye,eyeq5-olb", eqc_eyeq5_early_init);
  712. static void __init eqc_eyeq6h_central_early_init(struct device_node *np)
  713. {
  714. eqc_early_init(np, &eqc_eyeq6h_central_early_match_data);
  715. }
  716. CLK_OF_DECLARE_DRIVER(eqc_eyeq6h_central, "mobileye,eyeq6h-central-olb",
  717. eqc_eyeq6h_central_early_init);
  718. static void __init eqc_eyeq6h_west_early_init(struct device_node *np)
  719. {
  720. eqc_early_init(np, &eqc_eyeq6h_west_early_match_data);
  721. }
  722. CLK_OF_DECLARE_DRIVER(eqc_eyeq6h_west, "mobileye,eyeq6h-west-olb",
  723. eqc_eyeq6h_west_early_init);