spc.c 13 KB

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