spc.c 14 KB

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