spc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Versatile Express Serial Power Controller (SPC) support
  4. *
  5. * Copyright (C) 2013 ARM Ltd.
  6. *
  7. * Authors: Sudeep KarkadaNagesha <[email protected]>
  8. * Achin Gupta <[email protected]>
  9. * Lorenzo Pieralisi <[email protected]>
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/cpu.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_opp.h>
  20. #include <linux/slab.h>
  21. #include <linux/semaphore.h>
  22. #include <asm/cacheflush.h>
  23. #include "spc.h"
  24. #define SPCLOG "vexpress-spc: "
  25. #define PERF_LVL_A15 0x00
  26. #define PERF_REQ_A15 0x04
  27. #define PERF_LVL_A7 0x08
  28. #define PERF_REQ_A7 0x0c
  29. #define COMMS 0x10
  30. #define COMMS_REQ 0x14
  31. #define PWC_STATUS 0x18
  32. #define PWC_FLAG 0x1c
  33. /* SPC wake-up IRQs status and mask */
  34. #define WAKE_INT_MASK 0x24
  35. #define WAKE_INT_RAW 0x28
  36. #define WAKE_INT_STAT 0x2c
  37. /* SPC power down registers */
  38. #define A15_PWRDN_EN 0x30
  39. #define A7_PWRDN_EN 0x34
  40. /* SPC per-CPU mailboxes */
  41. #define A15_BX_ADDR0 0x68
  42. #define A7_BX_ADDR0 0x78
  43. /* SPC CPU/cluster reset statue */
  44. #define STANDBYWFI_STAT 0x3c
  45. #define STANDBYWFI_STAT_A15_CPU_MASK(cpu) (1 << (cpu))
  46. #define STANDBYWFI_STAT_A7_CPU_MASK(cpu) (1 << (3 + (cpu)))
  47. /* SPC system config interface registers */
  48. #define SYSCFG_WDATA 0x70
  49. #define SYSCFG_RDATA 0x74
  50. /* A15/A7 OPP virtual register base */
  51. #define A15_PERFVAL_BASE 0xC10
  52. #define A7_PERFVAL_BASE 0xC30
  53. /* Config interface control bits */
  54. #define SYSCFG_START BIT(31)
  55. #define SYSCFG_SCC (6 << 20)
  56. #define SYSCFG_STAT (14 << 20)
  57. /* wake-up interrupt masks */
  58. #define GBL_WAKEUP_INT_MSK (0x3 << 10)
  59. /* TC2 static dual-cluster configuration */
  60. #define MAX_CLUSTERS 2
  61. /*
  62. * Even though the SPC takes max 3-5 ms to complete any OPP/COMMS
  63. * operation, the operation could start just before jiffie is about
  64. * to be incremented. So setting timeout value of 20ms = 2jiffies@100Hz
  65. */
  66. #define TIMEOUT_US 20000
  67. #define MAX_OPPS 8
  68. #define CA15_DVFS 0
  69. #define CA7_DVFS 1
  70. #define SPC_SYS_CFG 2
  71. #define STAT_COMPLETE(type) ((1 << 0) << (type << 2))
  72. #define STAT_ERR(type) ((1 << 1) << (type << 2))
  73. #define RESPONSE_MASK(type) (STAT_COMPLETE(type) | STAT_ERR(type))
  74. struct ve_spc_opp {
  75. unsigned long freq;
  76. unsigned long u_volt;
  77. };
  78. struct ve_spc_drvdata {
  79. void __iomem *baseaddr;
  80. /*
  81. * A15s cluster identifier
  82. * It corresponds to A15 processors MPIDR[15:8] bitfield
  83. */
  84. u32 a15_clusid;
  85. uint32_t cur_rsp_mask;
  86. uint32_t cur_rsp_stat;
  87. struct semaphore sem;
  88. struct completion done;
  89. struct ve_spc_opp *opps[MAX_CLUSTERS];
  90. int num_opps[MAX_CLUSTERS];
  91. };
  92. static struct ve_spc_drvdata *info;
  93. static inline bool cluster_is_a15(u32 cluster)
  94. {
  95. return cluster == info->a15_clusid;
  96. }
  97. /**
  98. * ve_spc_global_wakeup_irq() - sets/clears global wakeup IRQs
  99. *
  100. * @set: if true, global wake-up IRQs are set, if false they are cleared
  101. *
  102. * Function to set/clear global wakeup IRQs. Not protected by locking since
  103. * it might be used in code paths where normal cacheable locks are not
  104. * working. Locking must be provided by the caller to ensure atomicity.
  105. */
  106. void ve_spc_global_wakeup_irq(bool set)
  107. {
  108. u32 reg;
  109. reg = readl_relaxed(info->baseaddr + WAKE_INT_MASK);
  110. if (set)
  111. reg |= GBL_WAKEUP_INT_MSK;
  112. else
  113. reg &= ~GBL_WAKEUP_INT_MSK;
  114. writel_relaxed(reg, info->baseaddr + WAKE_INT_MASK);
  115. }
  116. /**
  117. * ve_spc_cpu_wakeup_irq() - sets/clears per-CPU wake-up IRQs
  118. *
  119. * @cluster: mpidr[15:8] bitfield describing cluster affinity level
  120. * @cpu: mpidr[7:0] bitfield describing cpu affinity level
  121. * @set: if true, wake-up IRQs are set, if false they are cleared
  122. *
  123. * Function to set/clear per-CPU wake-up IRQs. Not protected by locking since
  124. * it might be used in code paths where normal cacheable locks are not
  125. * working. Locking must be provided by the caller to ensure atomicity.
  126. */
  127. void ve_spc_cpu_wakeup_irq(u32 cluster, u32 cpu, bool set)
  128. {
  129. u32 mask, reg;
  130. if (cluster >= MAX_CLUSTERS)
  131. return;
  132. mask = BIT(cpu);
  133. if (!cluster_is_a15(cluster))
  134. mask <<= 4;
  135. reg = readl_relaxed(info->baseaddr + WAKE_INT_MASK);
  136. if (set)
  137. reg |= mask;
  138. else
  139. reg &= ~mask;
  140. writel_relaxed(reg, info->baseaddr + WAKE_INT_MASK);
  141. }
  142. /**
  143. * ve_spc_set_resume_addr() - set the jump address used for warm boot
  144. *
  145. * @cluster: mpidr[15:8] bitfield describing cluster affinity level
  146. * @cpu: mpidr[7:0] bitfield describing cpu affinity level
  147. * @addr: physical resume address
  148. */
  149. void ve_spc_set_resume_addr(u32 cluster, u32 cpu, u32 addr)
  150. {
  151. void __iomem *baseaddr;
  152. if (cluster >= MAX_CLUSTERS)
  153. return;
  154. if (cluster_is_a15(cluster))
  155. baseaddr = info->baseaddr + A15_BX_ADDR0 + (cpu << 2);
  156. else
  157. baseaddr = info->baseaddr + A7_BX_ADDR0 + (cpu << 2);
  158. writel_relaxed(addr, baseaddr);
  159. }
  160. /**
  161. * ve_spc_powerdown() - enables/disables cluster powerdown
  162. *
  163. * @cluster: mpidr[15:8] bitfield describing cluster affinity level
  164. * @enable: if true enables powerdown, if false disables it
  165. *
  166. * Function to enable/disable cluster powerdown. Not protected by locking
  167. * since it might be used in code paths where normal cacheable locks are not
  168. * working. Locking must be provided by the caller to ensure atomicity.
  169. */
  170. void ve_spc_powerdown(u32 cluster, bool enable)
  171. {
  172. u32 pwdrn_reg;
  173. if (cluster >= MAX_CLUSTERS)
  174. return;
  175. pwdrn_reg = cluster_is_a15(cluster) ? A15_PWRDN_EN : A7_PWRDN_EN;
  176. writel_relaxed(enable, info->baseaddr + pwdrn_reg);
  177. }
  178. static u32 standbywfi_cpu_mask(u32 cpu, u32 cluster)
  179. {
  180. return cluster_is_a15(cluster) ?
  181. STANDBYWFI_STAT_A15_CPU_MASK(cpu)
  182. : STANDBYWFI_STAT_A7_CPU_MASK(cpu);
  183. }
  184. /**
  185. * ve_spc_cpu_in_wfi() - Checks if the specified CPU is in WFI or not
  186. *
  187. * @cpu: mpidr[7:0] bitfield describing CPU affinity level within cluster
  188. * @cluster: mpidr[15:8] bitfield describing cluster affinity level
  189. *
  190. * @return: non-zero if and only if the specified CPU is in WFI
  191. *
  192. * Take care when interpreting the result of this function: a CPU might
  193. * be in WFI temporarily due to idle, and is not necessarily safely
  194. * parked.
  195. */
  196. int ve_spc_cpu_in_wfi(u32 cpu, u32 cluster)
  197. {
  198. int ret;
  199. u32 mask = standbywfi_cpu_mask(cpu, cluster);
  200. if (cluster >= MAX_CLUSTERS)
  201. return 1;
  202. ret = readl_relaxed(info->baseaddr + STANDBYWFI_STAT);
  203. pr_debug("%s: PCFGREG[0x%X] = 0x%08X, mask = 0x%X\n",
  204. __func__, STANDBYWFI_STAT, ret, mask);
  205. return ret & mask;
  206. }
  207. static int ve_spc_get_performance(int cluster, u32 *freq)
  208. {
  209. struct ve_spc_opp *opps = info->opps[cluster];
  210. u32 perf_cfg_reg = 0;
  211. u32 perf;
  212. perf_cfg_reg = cluster_is_a15(cluster) ? PERF_LVL_A15 : PERF_LVL_A7;
  213. perf = readl_relaxed(info->baseaddr + perf_cfg_reg);
  214. if (perf >= info->num_opps[cluster])
  215. return -EINVAL;
  216. opps += perf;
  217. *freq = opps->freq;
  218. return 0;
  219. }
  220. /* find closest match to given frequency in OPP table */
  221. static int ve_spc_round_performance(int cluster, u32 freq)
  222. {
  223. int idx, max_opp = info->num_opps[cluster];
  224. struct ve_spc_opp *opps = info->opps[cluster];
  225. u32 fmin = 0, fmax = ~0, ftmp;
  226. freq /= 1000; /* OPP entries in kHz */
  227. for (idx = 0; idx < max_opp; idx++, opps++) {
  228. ftmp = opps->freq;
  229. if (ftmp >= freq) {
  230. if (ftmp <= fmax)
  231. fmax = ftmp;
  232. } else {
  233. if (ftmp >= fmin)
  234. fmin = ftmp;
  235. }
  236. }
  237. if (fmax != ~0)
  238. return fmax * 1000;
  239. else
  240. return fmin * 1000;
  241. }
  242. static int ve_spc_find_performance_index(int cluster, u32 freq)
  243. {
  244. int idx, max_opp = info->num_opps[cluster];
  245. struct ve_spc_opp *opps = info->opps[cluster];
  246. for (idx = 0; idx < max_opp; idx++, opps++)
  247. if (opps->freq == freq)
  248. break;
  249. return (idx == max_opp) ? -EINVAL : idx;
  250. }
  251. static int ve_spc_waitforcompletion(int req_type)
  252. {
  253. int ret = wait_for_completion_interruptible_timeout(
  254. &info->done, usecs_to_jiffies(TIMEOUT_US));
  255. if (ret == 0)
  256. ret = -ETIMEDOUT;
  257. else if (ret > 0)
  258. ret = info->cur_rsp_stat & STAT_COMPLETE(req_type) ? 0 : -EIO;
  259. return ret;
  260. }
  261. static int ve_spc_set_performance(int cluster, u32 freq)
  262. {
  263. u32 perf_cfg_reg;
  264. int ret, perf, req_type;
  265. if (cluster_is_a15(cluster)) {
  266. req_type = CA15_DVFS;
  267. perf_cfg_reg = PERF_LVL_A15;
  268. } else {
  269. req_type = CA7_DVFS;
  270. perf_cfg_reg = PERF_LVL_A7;
  271. }
  272. perf = ve_spc_find_performance_index(cluster, freq);
  273. if (perf < 0)
  274. return perf;
  275. if (down_timeout(&info->sem, usecs_to_jiffies(TIMEOUT_US)))
  276. return -ETIME;
  277. init_completion(&info->done);
  278. info->cur_rsp_mask = RESPONSE_MASK(req_type);
  279. writel(perf, info->baseaddr + perf_cfg_reg);
  280. ret = ve_spc_waitforcompletion(req_type);
  281. info->cur_rsp_mask = 0;
  282. up(&info->sem);
  283. return ret;
  284. }
  285. static int ve_spc_read_sys_cfg(int func, int offset, uint32_t *data)
  286. {
  287. int ret;
  288. if (down_timeout(&info->sem, usecs_to_jiffies(TIMEOUT_US)))
  289. return -ETIME;
  290. init_completion(&info->done);
  291. info->cur_rsp_mask = RESPONSE_MASK(SPC_SYS_CFG);
  292. /* Set the control value */
  293. writel(SYSCFG_START | func | offset >> 2, info->baseaddr + COMMS);
  294. ret = ve_spc_waitforcompletion(SPC_SYS_CFG);
  295. if (ret == 0)
  296. *data = readl(info->baseaddr + SYSCFG_RDATA);
  297. info->cur_rsp_mask = 0;
  298. up(&info->sem);
  299. return ret;
  300. }
  301. static irqreturn_t ve_spc_irq_handler(int irq, void *data)
  302. {
  303. struct ve_spc_drvdata *drv_data = data;
  304. uint32_t status = readl_relaxed(drv_data->baseaddr + PWC_STATUS);
  305. if (info->cur_rsp_mask & status) {
  306. info->cur_rsp_stat = status;
  307. complete(&drv_data->done);
  308. }
  309. return IRQ_HANDLED;
  310. }
  311. /*
  312. * +--------------------------+
  313. * | 31 20 | 19 0 |
  314. * +--------------------------+
  315. * | m_volt | freq(kHz) |
  316. * +--------------------------+
  317. */
  318. #define MULT_FACTOR 20
  319. #define VOLT_SHIFT 20
  320. #define FREQ_MASK (0xFFFFF)
  321. static int ve_spc_populate_opps(uint32_t cluster)
  322. {
  323. uint32_t data = 0, off, ret, idx;
  324. struct ve_spc_opp *opps;
  325. opps = kcalloc(MAX_OPPS, sizeof(*opps), GFP_KERNEL);
  326. if (!opps)
  327. return -ENOMEM;
  328. info->opps[cluster] = opps;
  329. off = cluster_is_a15(cluster) ? A15_PERFVAL_BASE : A7_PERFVAL_BASE;
  330. for (idx = 0; idx < MAX_OPPS; idx++, off += 4, opps++) {
  331. ret = ve_spc_read_sys_cfg(SYSCFG_SCC, off, &data);
  332. if (!ret) {
  333. opps->freq = (data & FREQ_MASK) * MULT_FACTOR;
  334. opps->u_volt = (data >> VOLT_SHIFT) * 1000;
  335. } else {
  336. break;
  337. }
  338. }
  339. info->num_opps[cluster] = idx;
  340. return ret;
  341. }
  342. static int ve_init_opp_table(struct device *cpu_dev)
  343. {
  344. int cluster;
  345. int idx, ret = 0, max_opp;
  346. struct ve_spc_opp *opps;
  347. cluster = topology_physical_package_id(cpu_dev->id);
  348. cluster = cluster < 0 ? 0 : cluster;
  349. max_opp = info->num_opps[cluster];
  350. opps = info->opps[cluster];
  351. for (idx = 0; idx < max_opp; idx++, opps++) {
  352. ret = dev_pm_opp_add(cpu_dev, opps->freq * 1000, opps->u_volt);
  353. if (ret) {
  354. dev_warn(cpu_dev, "failed to add opp %lu %lu\n",
  355. opps->freq, opps->u_volt);
  356. return ret;
  357. }
  358. }
  359. return ret;
  360. }
  361. int __init ve_spc_init(void __iomem *baseaddr, u32 a15_clusid, int irq)
  362. {
  363. int ret;
  364. info = kzalloc(sizeof(*info), GFP_KERNEL);
  365. if (!info)
  366. return -ENOMEM;
  367. info->baseaddr = baseaddr;
  368. info->a15_clusid = a15_clusid;
  369. if (irq <= 0) {
  370. pr_err(SPCLOG "Invalid IRQ %d\n", irq);
  371. kfree(info);
  372. return -EINVAL;
  373. }
  374. init_completion(&info->done);
  375. readl_relaxed(info->baseaddr + PWC_STATUS);
  376. ret = request_irq(irq, ve_spc_irq_handler, IRQF_TRIGGER_HIGH
  377. | IRQF_ONESHOT, "vexpress-spc", info);
  378. if (ret) {
  379. pr_err(SPCLOG "IRQ %d request failed\n", irq);
  380. kfree(info);
  381. return -ENODEV;
  382. }
  383. sema_init(&info->sem, 1);
  384. /*
  385. * Multi-cluster systems may need this data when non-coherent, during
  386. * cluster power-up/power-down. Make sure driver info reaches main
  387. * memory.
  388. */
  389. sync_cache_w(info);
  390. sync_cache_w(&info);
  391. return 0;
  392. }
  393. struct clk_spc {
  394. struct clk_hw hw;
  395. int cluster;
  396. };
  397. #define to_clk_spc(spc) container_of(spc, struct clk_spc, hw)
  398. static unsigned long spc_recalc_rate(struct clk_hw *hw,
  399. unsigned long parent_rate)
  400. {
  401. struct clk_spc *spc = to_clk_spc(hw);
  402. u32 freq;
  403. if (ve_spc_get_performance(spc->cluster, &freq))
  404. return -EIO;
  405. return freq * 1000;
  406. }
  407. static long spc_round_rate(struct clk_hw *hw, unsigned long drate,
  408. unsigned long *parent_rate)
  409. {
  410. struct clk_spc *spc = to_clk_spc(hw);
  411. return ve_spc_round_performance(spc->cluster, drate);
  412. }
  413. static int spc_set_rate(struct clk_hw *hw, unsigned long rate,
  414. unsigned long parent_rate)
  415. {
  416. struct clk_spc *spc = to_clk_spc(hw);
  417. return ve_spc_set_performance(spc->cluster, rate / 1000);
  418. }
  419. static struct clk_ops clk_spc_ops = {
  420. .recalc_rate = spc_recalc_rate,
  421. .round_rate = spc_round_rate,
  422. .set_rate = spc_set_rate,
  423. };
  424. static struct clk *ve_spc_clk_register(struct device *cpu_dev)
  425. {
  426. struct clk_init_data init;
  427. struct clk_spc *spc;
  428. spc = kzalloc(sizeof(*spc), GFP_KERNEL);
  429. if (!spc)
  430. return ERR_PTR(-ENOMEM);
  431. spc->hw.init = &init;
  432. spc->cluster = topology_physical_package_id(cpu_dev->id);
  433. spc->cluster = spc->cluster < 0 ? 0 : spc->cluster;
  434. init.name = dev_name(cpu_dev);
  435. init.ops = &clk_spc_ops;
  436. init.flags = CLK_GET_RATE_NOCACHE;
  437. init.num_parents = 0;
  438. return devm_clk_register(cpu_dev, &spc->hw);
  439. }
  440. static int __init ve_spc_clk_init(void)
  441. {
  442. int cpu, cluster;
  443. struct clk *clk;
  444. bool init_opp_table[MAX_CLUSTERS] = { false };
  445. if (!info)
  446. return 0; /* Continue only if SPC is initialised */
  447. if (ve_spc_populate_opps(0) || ve_spc_populate_opps(1)) {
  448. pr_err("failed to build OPP table\n");
  449. return -ENODEV;
  450. }
  451. for_each_possible_cpu(cpu) {
  452. struct device *cpu_dev = get_cpu_device(cpu);
  453. if (!cpu_dev) {
  454. pr_warn("failed to get cpu%d device\n", cpu);
  455. continue;
  456. }
  457. clk = ve_spc_clk_register(cpu_dev);
  458. if (IS_ERR(clk)) {
  459. pr_warn("failed to register cpu%d clock\n", cpu);
  460. continue;
  461. }
  462. if (clk_register_clkdev(clk, NULL, dev_name(cpu_dev))) {
  463. pr_warn("failed to register cpu%d clock lookup\n", cpu);
  464. continue;
  465. }
  466. cluster = topology_physical_package_id(cpu_dev->id);
  467. if (cluster < 0 || init_opp_table[cluster])
  468. continue;
  469. if (ve_init_opp_table(cpu_dev))
  470. pr_warn("failed to initialise cpu%d opp table\n", cpu);
  471. else if (dev_pm_opp_set_sharing_cpus(cpu_dev,
  472. topology_core_cpumask(cpu_dev->id)))
  473. pr_warn("failed to mark OPPs shared for cpu%d\n", cpu);
  474. else
  475. init_opp_table[cluster] = true;
  476. }
  477. platform_device_register_simple("vexpress-spc-cpufreq", -1, NULL, 0);
  478. return 0;
  479. }
  480. device_initcall(ve_spc_clk_init);