spc.c 14 KB

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