brcmstb-avs-cpufreq.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * CPU frequency scaling for Broadcom SoCs with AVS firmware that
  3. * supports DVS or DVFS
  4. *
  5. * Copyright (c) 2016 Broadcom
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /*
  17. * "AVS" is the name of a firmware developed at Broadcom. It derives
  18. * its name from the technique called "Adaptive Voltage Scaling".
  19. * Adaptive voltage scaling was the original purpose of this firmware.
  20. * The AVS firmware still supports "AVS mode", where all it does is
  21. * adaptive voltage scaling. However, on some newer Broadcom SoCs, the
  22. * AVS Firmware, despite its unchanged name, also supports DFS mode and
  23. * DVFS mode.
  24. *
  25. * In the context of this document and the related driver, "AVS" by
  26. * itself always means the Broadcom firmware and never refers to the
  27. * technique called "Adaptive Voltage Scaling".
  28. *
  29. * The Broadcom STB AVS CPUfreq driver provides voltage and frequency
  30. * scaling on Broadcom SoCs using AVS firmware with support for DFS and
  31. * DVFS. The AVS firmware is running on its own co-processor. The
  32. * driver supports both uniprocessor (UP) and symmetric multiprocessor
  33. * (SMP) systems which share clock and voltage across all CPUs.
  34. *
  35. * Actual voltage and frequency scaling is done solely by the AVS
  36. * firmware. This driver does not change frequency or voltage itself.
  37. * It provides a standard CPUfreq interface to the rest of the kernel
  38. * and to userland. It interfaces with the AVS firmware to effect the
  39. * requested changes and to report back the current system status in a
  40. * way that is expected by existing tools.
  41. */
  42. #include <linux/cpufreq.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/io.h>
  45. #include <linux/module.h>
  46. #include <linux/of_address.h>
  47. #include <linux/platform_device.h>
  48. #include <linux/semaphore.h>
  49. #ifdef CONFIG_ARM_BRCMSTB_AVS_CPUFREQ_DEBUG
  50. #include <linux/ctype.h>
  51. #include <linux/debugfs.h>
  52. #include <linux/slab.h>
  53. #include <linux/uaccess.h>
  54. #endif
  55. /* Max number of arguments AVS calls take */
  56. #define AVS_MAX_CMD_ARGS 4
  57. /*
  58. * This macro is used to generate AVS parameter register offsets. For
  59. * x >= AVS_MAX_CMD_ARGS, it returns 0 to protect against accidental memory
  60. * access outside of the parameter range. (Offset 0 is the first parameter.)
  61. */
  62. #define AVS_PARAM_MULT(x) ((x) < AVS_MAX_CMD_ARGS ? (x) : 0)
  63. /* AVS Mailbox Register offsets */
  64. #define AVS_MBOX_COMMAND 0x00
  65. #define AVS_MBOX_STATUS 0x04
  66. #define AVS_MBOX_VOLTAGE0 0x08
  67. #define AVS_MBOX_TEMP0 0x0c
  68. #define AVS_MBOX_PV0 0x10
  69. #define AVS_MBOX_MV0 0x14
  70. #define AVS_MBOX_PARAM(x) (0x18 + AVS_PARAM_MULT(x) * sizeof(u32))
  71. #define AVS_MBOX_REVISION 0x28
  72. #define AVS_MBOX_PSTATE 0x2c
  73. #define AVS_MBOX_HEARTBEAT 0x30
  74. #define AVS_MBOX_MAGIC 0x34
  75. #define AVS_MBOX_SIGMA_HVT 0x38
  76. #define AVS_MBOX_SIGMA_SVT 0x3c
  77. #define AVS_MBOX_VOLTAGE1 0x40
  78. #define AVS_MBOX_TEMP1 0x44
  79. #define AVS_MBOX_PV1 0x48
  80. #define AVS_MBOX_MV1 0x4c
  81. #define AVS_MBOX_FREQUENCY 0x50
  82. /* AVS Commands */
  83. #define AVS_CMD_AVAILABLE 0x00
  84. #define AVS_CMD_DISABLE 0x10
  85. #define AVS_CMD_ENABLE 0x11
  86. #define AVS_CMD_S2_ENTER 0x12
  87. #define AVS_CMD_S2_EXIT 0x13
  88. #define AVS_CMD_BBM_ENTER 0x14
  89. #define AVS_CMD_BBM_EXIT 0x15
  90. #define AVS_CMD_S3_ENTER 0x16
  91. #define AVS_CMD_S3_EXIT 0x17
  92. #define AVS_CMD_BALANCE 0x18
  93. /* PMAP and P-STATE commands */
  94. #define AVS_CMD_GET_PMAP 0x30
  95. #define AVS_CMD_SET_PMAP 0x31
  96. #define AVS_CMD_GET_PSTATE 0x40
  97. #define AVS_CMD_SET_PSTATE 0x41
  98. /* Different modes AVS supports (for GET_PMAP/SET_PMAP) */
  99. #define AVS_MODE_AVS 0x0
  100. #define AVS_MODE_DFS 0x1
  101. #define AVS_MODE_DVS 0x2
  102. #define AVS_MODE_DVFS 0x3
  103. /*
  104. * PMAP parameter p1
  105. * unused:31-24, mdiv_p0:23-16, unused:15-14, pdiv:13-10 , ndiv_int:9-0
  106. */
  107. #define NDIV_INT_SHIFT 0
  108. #define NDIV_INT_MASK 0x3ff
  109. #define PDIV_SHIFT 10
  110. #define PDIV_MASK 0xf
  111. #define MDIV_P0_SHIFT 16
  112. #define MDIV_P0_MASK 0xff
  113. /*
  114. * PMAP parameter p2
  115. * mdiv_p4:31-24, mdiv_p3:23-16, mdiv_p2:15:8, mdiv_p1:7:0
  116. */
  117. #define MDIV_P1_SHIFT 0
  118. #define MDIV_P1_MASK 0xff
  119. #define MDIV_P2_SHIFT 8
  120. #define MDIV_P2_MASK 0xff
  121. #define MDIV_P3_SHIFT 16
  122. #define MDIV_P3_MASK 0xff
  123. #define MDIV_P4_SHIFT 24
  124. #define MDIV_P4_MASK 0xff
  125. /* Different P-STATES AVS supports (for GET_PSTATE/SET_PSTATE) */
  126. #define AVS_PSTATE_P0 0x0
  127. #define AVS_PSTATE_P1 0x1
  128. #define AVS_PSTATE_P2 0x2
  129. #define AVS_PSTATE_P3 0x3
  130. #define AVS_PSTATE_P4 0x4
  131. #define AVS_PSTATE_MAX AVS_PSTATE_P4
  132. /* CPU L2 Interrupt Controller Registers */
  133. #define AVS_CPU_L2_SET0 0x04
  134. #define AVS_CPU_L2_INT_MASK BIT(31)
  135. /* AVS Command Status Values */
  136. #define AVS_STATUS_CLEAR 0x00
  137. /* Command/notification accepted */
  138. #define AVS_STATUS_SUCCESS 0xf0
  139. /* Command/notification rejected */
  140. #define AVS_STATUS_FAILURE 0xff
  141. /* Invalid command/notification (unknown) */
  142. #define AVS_STATUS_INVALID 0xf1
  143. /* Non-AVS modes are not supported */
  144. #define AVS_STATUS_NO_SUPP 0xf2
  145. /* Cannot set P-State until P-Map supplied */
  146. #define AVS_STATUS_NO_MAP 0xf3
  147. /* Cannot change P-Map after initial P-Map set */
  148. #define AVS_STATUS_MAP_SET 0xf4
  149. /* Max AVS status; higher numbers are used for debugging */
  150. #define AVS_STATUS_MAX 0xff
  151. /* Other AVS related constants */
  152. #define AVS_LOOP_LIMIT 10000
  153. #define AVS_TIMEOUT 300 /* in ms; expected completion is < 10ms */
  154. #define AVS_FIRMWARE_MAGIC 0xa11600d1
  155. #define BRCM_AVS_CPUFREQ_PREFIX "brcmstb-avs"
  156. #define BRCM_AVS_CPUFREQ_NAME BRCM_AVS_CPUFREQ_PREFIX "-cpufreq"
  157. #define BRCM_AVS_CPU_DATA "brcm,avs-cpu-data-mem"
  158. #define BRCM_AVS_CPU_INTR "brcm,avs-cpu-l2-intr"
  159. #define BRCM_AVS_HOST_INTR "sw_intr"
  160. struct pmap {
  161. unsigned int mode;
  162. unsigned int p1;
  163. unsigned int p2;
  164. unsigned int state;
  165. };
  166. struct private_data {
  167. void __iomem *base;
  168. void __iomem *avs_intr_base;
  169. struct device *dev;
  170. #ifdef CONFIG_ARM_BRCMSTB_AVS_CPUFREQ_DEBUG
  171. struct dentry *debugfs;
  172. #endif
  173. struct completion done;
  174. struct semaphore sem;
  175. struct pmap pmap;
  176. };
  177. #ifdef CONFIG_ARM_BRCMSTB_AVS_CPUFREQ_DEBUG
  178. enum debugfs_format {
  179. DEBUGFS_NORMAL,
  180. DEBUGFS_FLOAT,
  181. DEBUGFS_REV,
  182. };
  183. struct debugfs_data {
  184. struct debugfs_entry *entry;
  185. struct private_data *priv;
  186. };
  187. struct debugfs_entry {
  188. char *name;
  189. u32 offset;
  190. fmode_t mode;
  191. enum debugfs_format format;
  192. };
  193. #define DEBUGFS_ENTRY(name, mode, format) { \
  194. #name, AVS_MBOX_##name, mode, format \
  195. }
  196. /*
  197. * These are used for debugfs only. Otherwise we use AVS_MBOX_PARAM() directly.
  198. */
  199. #define AVS_MBOX_PARAM1 AVS_MBOX_PARAM(0)
  200. #define AVS_MBOX_PARAM2 AVS_MBOX_PARAM(1)
  201. #define AVS_MBOX_PARAM3 AVS_MBOX_PARAM(2)
  202. #define AVS_MBOX_PARAM4 AVS_MBOX_PARAM(3)
  203. /*
  204. * This table stores the name, access permissions and offset for each hardware
  205. * register and is used to generate debugfs entries.
  206. */
  207. static struct debugfs_entry debugfs_entries[] = {
  208. DEBUGFS_ENTRY(COMMAND, S_IWUSR, DEBUGFS_NORMAL),
  209. DEBUGFS_ENTRY(STATUS, S_IWUSR, DEBUGFS_NORMAL),
  210. DEBUGFS_ENTRY(VOLTAGE0, 0, DEBUGFS_FLOAT),
  211. DEBUGFS_ENTRY(TEMP0, 0, DEBUGFS_FLOAT),
  212. DEBUGFS_ENTRY(PV0, 0, DEBUGFS_FLOAT),
  213. DEBUGFS_ENTRY(MV0, 0, DEBUGFS_FLOAT),
  214. DEBUGFS_ENTRY(PARAM1, S_IWUSR, DEBUGFS_NORMAL),
  215. DEBUGFS_ENTRY(PARAM2, S_IWUSR, DEBUGFS_NORMAL),
  216. DEBUGFS_ENTRY(PARAM3, S_IWUSR, DEBUGFS_NORMAL),
  217. DEBUGFS_ENTRY(PARAM4, S_IWUSR, DEBUGFS_NORMAL),
  218. DEBUGFS_ENTRY(REVISION, 0, DEBUGFS_REV),
  219. DEBUGFS_ENTRY(PSTATE, 0, DEBUGFS_NORMAL),
  220. DEBUGFS_ENTRY(HEARTBEAT, 0, DEBUGFS_NORMAL),
  221. DEBUGFS_ENTRY(MAGIC, S_IWUSR, DEBUGFS_NORMAL),
  222. DEBUGFS_ENTRY(SIGMA_HVT, 0, DEBUGFS_NORMAL),
  223. DEBUGFS_ENTRY(SIGMA_SVT, 0, DEBUGFS_NORMAL),
  224. DEBUGFS_ENTRY(VOLTAGE1, 0, DEBUGFS_FLOAT),
  225. DEBUGFS_ENTRY(TEMP1, 0, DEBUGFS_FLOAT),
  226. DEBUGFS_ENTRY(PV1, 0, DEBUGFS_FLOAT),
  227. DEBUGFS_ENTRY(MV1, 0, DEBUGFS_FLOAT),
  228. DEBUGFS_ENTRY(FREQUENCY, 0, DEBUGFS_NORMAL),
  229. };
  230. static int brcm_avs_target_index(struct cpufreq_policy *, unsigned int);
  231. static char *__strtolower(char *s)
  232. {
  233. char *p;
  234. for (p = s; *p; p++)
  235. *p = tolower(*p);
  236. return s;
  237. }
  238. #endif /* CONFIG_ARM_BRCMSTB_AVS_CPUFREQ_DEBUG */
  239. static void __iomem *__map_region(const char *name)
  240. {
  241. struct device_node *np;
  242. void __iomem *ptr;
  243. np = of_find_compatible_node(NULL, NULL, name);
  244. if (!np)
  245. return NULL;
  246. ptr = of_iomap(np, 0);
  247. of_node_put(np);
  248. return ptr;
  249. }
  250. static int __issue_avs_command(struct private_data *priv, int cmd, bool is_send,
  251. u32 args[])
  252. {
  253. unsigned long time_left = msecs_to_jiffies(AVS_TIMEOUT);
  254. void __iomem *base = priv->base;
  255. unsigned int i;
  256. int ret;
  257. u32 val;
  258. ret = down_interruptible(&priv->sem);
  259. if (ret)
  260. return ret;
  261. /*
  262. * Make sure no other command is currently running: cmd is 0 if AVS
  263. * co-processor is idle. Due to the guard above, we should almost never
  264. * have to wait here.
  265. */
  266. for (i = 0, val = 1; val != 0 && i < AVS_LOOP_LIMIT; i++)
  267. val = readl(base + AVS_MBOX_COMMAND);
  268. /* Give the caller a chance to retry if AVS is busy. */
  269. if (i == AVS_LOOP_LIMIT) {
  270. ret = -EAGAIN;
  271. goto out;
  272. }
  273. /* Clear status before we begin. */
  274. writel(AVS_STATUS_CLEAR, base + AVS_MBOX_STATUS);
  275. /* We need to send arguments for this command. */
  276. if (args && is_send) {
  277. for (i = 0; i < AVS_MAX_CMD_ARGS; i++)
  278. writel(args[i], base + AVS_MBOX_PARAM(i));
  279. }
  280. /* Protect from spurious interrupts. */
  281. reinit_completion(&priv->done);
  282. /* Now issue the command & tell firmware to wake up to process it. */
  283. writel(cmd, base + AVS_MBOX_COMMAND);
  284. writel(AVS_CPU_L2_INT_MASK, priv->avs_intr_base + AVS_CPU_L2_SET0);
  285. /* Wait for AVS co-processor to finish processing the command. */
  286. time_left = wait_for_completion_timeout(&priv->done, time_left);
  287. /*
  288. * If the AVS status is not in the expected range, it means AVS didn't
  289. * complete our command in time, and we return an error. Also, if there
  290. * is no "time left", we timed out waiting for the interrupt.
  291. */
  292. val = readl(base + AVS_MBOX_STATUS);
  293. if (time_left == 0 || val == 0 || val > AVS_STATUS_MAX) {
  294. dev_err(priv->dev, "AVS command %#x didn't complete in time\n",
  295. cmd);
  296. dev_err(priv->dev, " Time left: %u ms, AVS status: %#x\n",
  297. jiffies_to_msecs(time_left), val);
  298. ret = -ETIMEDOUT;
  299. goto out;
  300. }
  301. /* This command returned arguments, so we read them back. */
  302. if (args && !is_send) {
  303. for (i = 0; i < AVS_MAX_CMD_ARGS; i++)
  304. args[i] = readl(base + AVS_MBOX_PARAM(i));
  305. }
  306. /* Clear status to tell AVS co-processor we are done. */
  307. writel(AVS_STATUS_CLEAR, base + AVS_MBOX_STATUS);
  308. /* Convert firmware errors to errno's as much as possible. */
  309. switch (val) {
  310. case AVS_STATUS_INVALID:
  311. ret = -EINVAL;
  312. break;
  313. case AVS_STATUS_NO_SUPP:
  314. ret = -ENOTSUPP;
  315. break;
  316. case AVS_STATUS_NO_MAP:
  317. ret = -ENOENT;
  318. break;
  319. case AVS_STATUS_MAP_SET:
  320. ret = -EEXIST;
  321. break;
  322. case AVS_STATUS_FAILURE:
  323. ret = -EIO;
  324. break;
  325. }
  326. out:
  327. up(&priv->sem);
  328. return ret;
  329. }
  330. static irqreturn_t irq_handler(int irq, void *data)
  331. {
  332. struct private_data *priv = data;
  333. /* AVS command completed execution. Wake up __issue_avs_command(). */
  334. complete(&priv->done);
  335. return IRQ_HANDLED;
  336. }
  337. static char *brcm_avs_mode_to_string(unsigned int mode)
  338. {
  339. switch (mode) {
  340. case AVS_MODE_AVS:
  341. return "AVS";
  342. case AVS_MODE_DFS:
  343. return "DFS";
  344. case AVS_MODE_DVS:
  345. return "DVS";
  346. case AVS_MODE_DVFS:
  347. return "DVFS";
  348. }
  349. return NULL;
  350. }
  351. static void brcm_avs_parse_p1(u32 p1, unsigned int *mdiv_p0, unsigned int *pdiv,
  352. unsigned int *ndiv)
  353. {
  354. *mdiv_p0 = (p1 >> MDIV_P0_SHIFT) & MDIV_P0_MASK;
  355. *pdiv = (p1 >> PDIV_SHIFT) & PDIV_MASK;
  356. *ndiv = (p1 >> NDIV_INT_SHIFT) & NDIV_INT_MASK;
  357. }
  358. static void brcm_avs_parse_p2(u32 p2, unsigned int *mdiv_p1,
  359. unsigned int *mdiv_p2, unsigned int *mdiv_p3,
  360. unsigned int *mdiv_p4)
  361. {
  362. *mdiv_p4 = (p2 >> MDIV_P4_SHIFT) & MDIV_P4_MASK;
  363. *mdiv_p3 = (p2 >> MDIV_P3_SHIFT) & MDIV_P3_MASK;
  364. *mdiv_p2 = (p2 >> MDIV_P2_SHIFT) & MDIV_P2_MASK;
  365. *mdiv_p1 = (p2 >> MDIV_P1_SHIFT) & MDIV_P1_MASK;
  366. }
  367. static int brcm_avs_get_pmap(struct private_data *priv, struct pmap *pmap)
  368. {
  369. u32 args[AVS_MAX_CMD_ARGS];
  370. int ret;
  371. ret = __issue_avs_command(priv, AVS_CMD_GET_PMAP, false, args);
  372. if (ret || !pmap)
  373. return ret;
  374. pmap->mode = args[0];
  375. pmap->p1 = args[1];
  376. pmap->p2 = args[2];
  377. pmap->state = args[3];
  378. return 0;
  379. }
  380. static int brcm_avs_set_pmap(struct private_data *priv, struct pmap *pmap)
  381. {
  382. u32 args[AVS_MAX_CMD_ARGS];
  383. args[0] = pmap->mode;
  384. args[1] = pmap->p1;
  385. args[2] = pmap->p2;
  386. args[3] = pmap->state;
  387. return __issue_avs_command(priv, AVS_CMD_SET_PMAP, true, args);
  388. }
  389. static int brcm_avs_get_pstate(struct private_data *priv, unsigned int *pstate)
  390. {
  391. u32 args[AVS_MAX_CMD_ARGS];
  392. int ret;
  393. ret = __issue_avs_command(priv, AVS_CMD_GET_PSTATE, false, args);
  394. if (ret)
  395. return ret;
  396. *pstate = args[0];
  397. return 0;
  398. }
  399. static int brcm_avs_set_pstate(struct private_data *priv, unsigned int pstate)
  400. {
  401. u32 args[AVS_MAX_CMD_ARGS];
  402. args[0] = pstate;
  403. return __issue_avs_command(priv, AVS_CMD_SET_PSTATE, true, args);
  404. }
  405. static unsigned long brcm_avs_get_voltage(void __iomem *base)
  406. {
  407. return readl(base + AVS_MBOX_VOLTAGE1);
  408. }
  409. static unsigned long brcm_avs_get_frequency(void __iomem *base)
  410. {
  411. return readl(base + AVS_MBOX_FREQUENCY) * 1000; /* in kHz */
  412. }
  413. /*
  414. * We determine which frequencies are supported by cycling through all P-states
  415. * and reading back what frequency we are running at for each P-state.
  416. */
  417. static struct cpufreq_frequency_table *
  418. brcm_avs_get_freq_table(struct device *dev, struct private_data *priv)
  419. {
  420. struct cpufreq_frequency_table *table;
  421. unsigned int pstate;
  422. int i, ret;
  423. /* Remember P-state for later */
  424. ret = brcm_avs_get_pstate(priv, &pstate);
  425. if (ret)
  426. return ERR_PTR(ret);
  427. table = devm_kzalloc(dev, (AVS_PSTATE_MAX + 1) * sizeof(*table),
  428. GFP_KERNEL);
  429. if (!table)
  430. return ERR_PTR(-ENOMEM);
  431. for (i = AVS_PSTATE_P0; i <= AVS_PSTATE_MAX; i++) {
  432. ret = brcm_avs_set_pstate(priv, i);
  433. if (ret)
  434. return ERR_PTR(ret);
  435. table[i].frequency = brcm_avs_get_frequency(priv->base);
  436. table[i].driver_data = i;
  437. }
  438. table[i].frequency = CPUFREQ_TABLE_END;
  439. /* Restore P-state */
  440. ret = brcm_avs_set_pstate(priv, pstate);
  441. if (ret)
  442. return ERR_PTR(ret);
  443. return table;
  444. }
  445. #ifdef CONFIG_ARM_BRCMSTB_AVS_CPUFREQ_DEBUG
  446. #define MANT(x) (unsigned int)(abs((x)) / 1000)
  447. #define FRAC(x) (unsigned int)(abs((x)) - abs((x)) / 1000 * 1000)
  448. static int brcm_avs_debug_show(struct seq_file *s, void *data)
  449. {
  450. struct debugfs_data *dbgfs = s->private;
  451. void __iomem *base;
  452. u32 val, offset;
  453. if (!dbgfs) {
  454. seq_puts(s, "No device pointer\n");
  455. return 0;
  456. }
  457. base = dbgfs->priv->base;
  458. offset = dbgfs->entry->offset;
  459. val = readl(base + offset);
  460. switch (dbgfs->entry->format) {
  461. case DEBUGFS_NORMAL:
  462. seq_printf(s, "%u\n", val);
  463. break;
  464. case DEBUGFS_FLOAT:
  465. seq_printf(s, "%d.%03d\n", MANT(val), FRAC(val));
  466. break;
  467. case DEBUGFS_REV:
  468. seq_printf(s, "%c.%c.%c.%c\n", (val >> 24 & 0xff),
  469. (val >> 16 & 0xff), (val >> 8 & 0xff),
  470. val & 0xff);
  471. break;
  472. }
  473. seq_printf(s, "0x%08x\n", val);
  474. return 0;
  475. }
  476. #undef MANT
  477. #undef FRAC
  478. static ssize_t brcm_avs_seq_write(struct file *file, const char __user *buf,
  479. size_t size, loff_t *ppos)
  480. {
  481. struct seq_file *s = file->private_data;
  482. struct debugfs_data *dbgfs = s->private;
  483. struct private_data *priv = dbgfs->priv;
  484. void __iomem *base, *avs_intr_base;
  485. bool use_issue_command = false;
  486. unsigned long val, offset;
  487. char str[128];
  488. int ret;
  489. char *str_ptr = str;
  490. if (size >= sizeof(str))
  491. return -E2BIG;
  492. memset(str, 0, sizeof(str));
  493. ret = copy_from_user(str, buf, size);
  494. if (ret)
  495. return ret;
  496. base = priv->base;
  497. avs_intr_base = priv->avs_intr_base;
  498. offset = dbgfs->entry->offset;
  499. /*
  500. * Special case writing to "command" entry only: if the string starts
  501. * with a 'c', we use the driver's __issue_avs_command() function.
  502. * Otherwise, we perform a raw write. This should allow testing of raw
  503. * access as well as using the higher level function. (Raw access
  504. * doesn't clear the firmware return status after issuing the command.)
  505. */
  506. if (str_ptr[0] == 'c' && offset == AVS_MBOX_COMMAND) {
  507. use_issue_command = true;
  508. str_ptr++;
  509. }
  510. if (kstrtoul(str_ptr, 0, &val) != 0)
  511. return -EINVAL;
  512. /*
  513. * Setting the P-state is a special case. We need to update the CPU
  514. * frequency we report.
  515. */
  516. if (val == AVS_CMD_SET_PSTATE) {
  517. struct cpufreq_policy *policy;
  518. unsigned int pstate;
  519. policy = cpufreq_cpu_get(smp_processor_id());
  520. /* Read back the P-state we are about to set */
  521. pstate = readl(base + AVS_MBOX_PARAM(0));
  522. if (use_issue_command) {
  523. ret = brcm_avs_target_index(policy, pstate);
  524. return ret ? ret : size;
  525. }
  526. policy->cur = policy->freq_table[pstate].frequency;
  527. }
  528. if (use_issue_command) {
  529. ret = __issue_avs_command(priv, val, false, NULL);
  530. } else {
  531. /* Locking here is not perfect, but is only for debug. */
  532. ret = down_interruptible(&priv->sem);
  533. if (ret)
  534. return ret;
  535. writel(val, base + offset);
  536. /* We have to wake up the firmware to process a command. */
  537. if (offset == AVS_MBOX_COMMAND)
  538. writel(AVS_CPU_L2_INT_MASK,
  539. avs_intr_base + AVS_CPU_L2_SET0);
  540. up(&priv->sem);
  541. }
  542. return ret ? ret : size;
  543. }
  544. static struct debugfs_entry *__find_debugfs_entry(const char *name)
  545. {
  546. int i;
  547. for (i = 0; i < ARRAY_SIZE(debugfs_entries); i++)
  548. if (strcasecmp(debugfs_entries[i].name, name) == 0)
  549. return &debugfs_entries[i];
  550. return NULL;
  551. }
  552. static int brcm_avs_debug_open(struct inode *inode, struct file *file)
  553. {
  554. struct debugfs_data *data;
  555. fmode_t fmode;
  556. int ret;
  557. /*
  558. * seq_open(), which is called by single_open(), clears "write" access.
  559. * We need write access to some files, so we preserve our access mode
  560. * and restore it.
  561. */
  562. fmode = file->f_mode;
  563. /*
  564. * Check access permissions even for root. We don't want to be writing
  565. * to read-only registers. Access for regular users has already been
  566. * checked by the VFS layer.
  567. */
  568. if ((fmode & FMODE_WRITER) && !(inode->i_mode & S_IWUSR))
  569. return -EACCES;
  570. data = kmalloc(sizeof(*data), GFP_KERNEL);
  571. if (!data)
  572. return -ENOMEM;
  573. /*
  574. * We use the same file system operations for all our debug files. To
  575. * produce specific output, we look up the file name upon opening a
  576. * debugfs entry and map it to a memory offset. This offset is then used
  577. * in the generic "show" function to read a specific register.
  578. */
  579. data->entry = __find_debugfs_entry(file->f_path.dentry->d_iname);
  580. data->priv = inode->i_private;
  581. ret = single_open(file, brcm_avs_debug_show, data);
  582. if (ret)
  583. kfree(data);
  584. file->f_mode = fmode;
  585. return ret;
  586. }
  587. static int brcm_avs_debug_release(struct inode *inode, struct file *file)
  588. {
  589. struct seq_file *seq_priv = file->private_data;
  590. struct debugfs_data *data = seq_priv->private;
  591. kfree(data);
  592. return single_release(inode, file);
  593. }
  594. static const struct file_operations brcm_avs_debug_ops = {
  595. .open = brcm_avs_debug_open,
  596. .read = seq_read,
  597. .write = brcm_avs_seq_write,
  598. .llseek = seq_lseek,
  599. .release = brcm_avs_debug_release,
  600. };
  601. static void brcm_avs_cpufreq_debug_init(struct platform_device *pdev)
  602. {
  603. struct private_data *priv = platform_get_drvdata(pdev);
  604. struct dentry *dir;
  605. int i;
  606. if (!priv)
  607. return;
  608. dir = debugfs_create_dir(BRCM_AVS_CPUFREQ_NAME, NULL);
  609. if (IS_ERR_OR_NULL(dir))
  610. return;
  611. priv->debugfs = dir;
  612. for (i = 0; i < ARRAY_SIZE(debugfs_entries); i++) {
  613. /*
  614. * The DEBUGFS_ENTRY macro generates uppercase strings. We
  615. * convert them to lowercase before creating the debugfs
  616. * entries.
  617. */
  618. char *entry = __strtolower(debugfs_entries[i].name);
  619. fmode_t mode = debugfs_entries[i].mode;
  620. if (!debugfs_create_file(entry, S_IFREG | S_IRUGO | mode,
  621. dir, priv, &brcm_avs_debug_ops)) {
  622. priv->debugfs = NULL;
  623. debugfs_remove_recursive(dir);
  624. break;
  625. }
  626. }
  627. }
  628. static void brcm_avs_cpufreq_debug_exit(struct platform_device *pdev)
  629. {
  630. struct private_data *priv = platform_get_drvdata(pdev);
  631. if (priv && priv->debugfs) {
  632. debugfs_remove_recursive(priv->debugfs);
  633. priv->debugfs = NULL;
  634. }
  635. }
  636. #else
  637. static void brcm_avs_cpufreq_debug_init(struct platform_device *pdev) {}
  638. static void brcm_avs_cpufreq_debug_exit(struct platform_device *pdev) {}
  639. #endif /* CONFIG_ARM_BRCMSTB_AVS_CPUFREQ_DEBUG */
  640. /*
  641. * To ensure the right firmware is running we need to
  642. * - check the MAGIC matches what we expect
  643. * - brcm_avs_get_pmap() doesn't return -ENOTSUPP or -EINVAL
  644. * We need to set up our interrupt handling before calling brcm_avs_get_pmap()!
  645. */
  646. static bool brcm_avs_is_firmware_loaded(struct private_data *priv)
  647. {
  648. u32 magic;
  649. int rc;
  650. rc = brcm_avs_get_pmap(priv, NULL);
  651. magic = readl(priv->base + AVS_MBOX_MAGIC);
  652. return (magic == AVS_FIRMWARE_MAGIC) && (rc != -ENOTSUPP) &&
  653. (rc != -EINVAL);
  654. }
  655. static unsigned int brcm_avs_cpufreq_get(unsigned int cpu)
  656. {
  657. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  658. struct private_data *priv = policy->driver_data;
  659. return brcm_avs_get_frequency(priv->base);
  660. }
  661. static int brcm_avs_target_index(struct cpufreq_policy *policy,
  662. unsigned int index)
  663. {
  664. return brcm_avs_set_pstate(policy->driver_data,
  665. policy->freq_table[index].driver_data);
  666. }
  667. static int brcm_avs_suspend(struct cpufreq_policy *policy)
  668. {
  669. struct private_data *priv = policy->driver_data;
  670. return brcm_avs_get_pmap(priv, &priv->pmap);
  671. }
  672. static int brcm_avs_resume(struct cpufreq_policy *policy)
  673. {
  674. struct private_data *priv = policy->driver_data;
  675. int ret;
  676. ret = brcm_avs_set_pmap(priv, &priv->pmap);
  677. if (ret == -EEXIST) {
  678. struct platform_device *pdev = cpufreq_get_driver_data();
  679. struct device *dev = &pdev->dev;
  680. dev_warn(dev, "PMAP was already set\n");
  681. ret = 0;
  682. }
  683. return ret;
  684. }
  685. /*
  686. * All initialization code that we only want to execute once goes here. Setup
  687. * code that can be re-tried on every core (if it failed before) can go into
  688. * brcm_avs_cpufreq_init().
  689. */
  690. static int brcm_avs_prepare_init(struct platform_device *pdev)
  691. {
  692. struct private_data *priv;
  693. struct device *dev;
  694. int host_irq, ret;
  695. dev = &pdev->dev;
  696. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  697. if (!priv)
  698. return -ENOMEM;
  699. priv->dev = dev;
  700. sema_init(&priv->sem, 1);
  701. init_completion(&priv->done);
  702. platform_set_drvdata(pdev, priv);
  703. priv->base = __map_region(BRCM_AVS_CPU_DATA);
  704. if (!priv->base) {
  705. dev_err(dev, "Couldn't find property %s in device tree.\n",
  706. BRCM_AVS_CPU_DATA);
  707. return -ENOENT;
  708. }
  709. priv->avs_intr_base = __map_region(BRCM_AVS_CPU_INTR);
  710. if (!priv->avs_intr_base) {
  711. dev_err(dev, "Couldn't find property %s in device tree.\n",
  712. BRCM_AVS_CPU_INTR);
  713. ret = -ENOENT;
  714. goto unmap_base;
  715. }
  716. host_irq = platform_get_irq_byname(pdev, BRCM_AVS_HOST_INTR);
  717. if (host_irq < 0) {
  718. dev_err(dev, "Couldn't find interrupt %s -- %d\n",
  719. BRCM_AVS_HOST_INTR, host_irq);
  720. ret = host_irq;
  721. goto unmap_intr_base;
  722. }
  723. ret = devm_request_irq(dev, host_irq, irq_handler, IRQF_TRIGGER_RISING,
  724. BRCM_AVS_HOST_INTR, priv);
  725. if (ret) {
  726. dev_err(dev, "IRQ request failed: %s (%d) -- %d\n",
  727. BRCM_AVS_HOST_INTR, host_irq, ret);
  728. goto unmap_intr_base;
  729. }
  730. if (brcm_avs_is_firmware_loaded(priv))
  731. return 0;
  732. dev_err(dev, "AVS firmware is not loaded or doesn't support DVFS\n");
  733. ret = -ENODEV;
  734. unmap_intr_base:
  735. iounmap(priv->avs_intr_base);
  736. unmap_base:
  737. iounmap(priv->base);
  738. platform_set_drvdata(pdev, NULL);
  739. return ret;
  740. }
  741. static int brcm_avs_cpufreq_init(struct cpufreq_policy *policy)
  742. {
  743. struct cpufreq_frequency_table *freq_table;
  744. struct platform_device *pdev;
  745. struct private_data *priv;
  746. struct device *dev;
  747. int ret;
  748. pdev = cpufreq_get_driver_data();
  749. priv = platform_get_drvdata(pdev);
  750. policy->driver_data = priv;
  751. dev = &pdev->dev;
  752. freq_table = brcm_avs_get_freq_table(dev, priv);
  753. if (IS_ERR(freq_table)) {
  754. ret = PTR_ERR(freq_table);
  755. dev_err(dev, "Couldn't determine frequency table (%d).\n", ret);
  756. return ret;
  757. }
  758. ret = cpufreq_table_validate_and_show(policy, freq_table);
  759. if (ret) {
  760. dev_err(dev, "invalid frequency table: %d\n", ret);
  761. return ret;
  762. }
  763. /* All cores share the same clock and thus the same policy. */
  764. cpumask_setall(policy->cpus);
  765. ret = __issue_avs_command(priv, AVS_CMD_ENABLE, false, NULL);
  766. if (!ret) {
  767. unsigned int pstate;
  768. ret = brcm_avs_get_pstate(priv, &pstate);
  769. if (!ret) {
  770. policy->cur = freq_table[pstate].frequency;
  771. dev_info(dev, "registered\n");
  772. return 0;
  773. }
  774. }
  775. dev_err(dev, "couldn't initialize driver (%d)\n", ret);
  776. return ret;
  777. }
  778. static ssize_t show_brcm_avs_pstate(struct cpufreq_policy *policy, char *buf)
  779. {
  780. struct private_data *priv = policy->driver_data;
  781. unsigned int pstate;
  782. if (brcm_avs_get_pstate(priv, &pstate))
  783. return sprintf(buf, "<unknown>\n");
  784. return sprintf(buf, "%u\n", pstate);
  785. }
  786. static ssize_t show_brcm_avs_mode(struct cpufreq_policy *policy, char *buf)
  787. {
  788. struct private_data *priv = policy->driver_data;
  789. struct pmap pmap;
  790. if (brcm_avs_get_pmap(priv, &pmap))
  791. return sprintf(buf, "<unknown>\n");
  792. return sprintf(buf, "%s %u\n", brcm_avs_mode_to_string(pmap.mode),
  793. pmap.mode);
  794. }
  795. static ssize_t show_brcm_avs_pmap(struct cpufreq_policy *policy, char *buf)
  796. {
  797. unsigned int mdiv_p0, mdiv_p1, mdiv_p2, mdiv_p3, mdiv_p4;
  798. struct private_data *priv = policy->driver_data;
  799. unsigned int ndiv, pdiv;
  800. struct pmap pmap;
  801. if (brcm_avs_get_pmap(priv, &pmap))
  802. return sprintf(buf, "<unknown>\n");
  803. brcm_avs_parse_p1(pmap.p1, &mdiv_p0, &pdiv, &ndiv);
  804. brcm_avs_parse_p2(pmap.p2, &mdiv_p1, &mdiv_p2, &mdiv_p3, &mdiv_p4);
  805. return sprintf(buf, "0x%08x 0x%08x %u %u %u %u %u %u %u\n",
  806. pmap.p1, pmap.p2, ndiv, pdiv, mdiv_p0, mdiv_p1, mdiv_p2,
  807. mdiv_p3, mdiv_p4);
  808. }
  809. static ssize_t show_brcm_avs_voltage(struct cpufreq_policy *policy, char *buf)
  810. {
  811. struct private_data *priv = policy->driver_data;
  812. return sprintf(buf, "0x%08lx\n", brcm_avs_get_voltage(priv->base));
  813. }
  814. static ssize_t show_brcm_avs_frequency(struct cpufreq_policy *policy, char *buf)
  815. {
  816. struct private_data *priv = policy->driver_data;
  817. return sprintf(buf, "0x%08lx\n", brcm_avs_get_frequency(priv->base));
  818. }
  819. cpufreq_freq_attr_ro(brcm_avs_pstate);
  820. cpufreq_freq_attr_ro(brcm_avs_mode);
  821. cpufreq_freq_attr_ro(brcm_avs_pmap);
  822. cpufreq_freq_attr_ro(brcm_avs_voltage);
  823. cpufreq_freq_attr_ro(brcm_avs_frequency);
  824. static struct freq_attr *brcm_avs_cpufreq_attr[] = {
  825. &cpufreq_freq_attr_scaling_available_freqs,
  826. &brcm_avs_pstate,
  827. &brcm_avs_mode,
  828. &brcm_avs_pmap,
  829. &brcm_avs_voltage,
  830. &brcm_avs_frequency,
  831. NULL
  832. };
  833. static struct cpufreq_driver brcm_avs_driver = {
  834. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  835. .verify = cpufreq_generic_frequency_table_verify,
  836. .target_index = brcm_avs_target_index,
  837. .get = brcm_avs_cpufreq_get,
  838. .suspend = brcm_avs_suspend,
  839. .resume = brcm_avs_resume,
  840. .init = brcm_avs_cpufreq_init,
  841. .attr = brcm_avs_cpufreq_attr,
  842. .name = BRCM_AVS_CPUFREQ_PREFIX,
  843. };
  844. static int brcm_avs_cpufreq_probe(struct platform_device *pdev)
  845. {
  846. int ret;
  847. ret = brcm_avs_prepare_init(pdev);
  848. if (ret)
  849. return ret;
  850. brcm_avs_driver.driver_data = pdev;
  851. ret = cpufreq_register_driver(&brcm_avs_driver);
  852. if (!ret)
  853. brcm_avs_cpufreq_debug_init(pdev);
  854. return ret;
  855. }
  856. static int brcm_avs_cpufreq_remove(struct platform_device *pdev)
  857. {
  858. struct private_data *priv;
  859. int ret;
  860. ret = cpufreq_unregister_driver(&brcm_avs_driver);
  861. if (ret)
  862. return ret;
  863. brcm_avs_cpufreq_debug_exit(pdev);
  864. priv = platform_get_drvdata(pdev);
  865. iounmap(priv->base);
  866. iounmap(priv->avs_intr_base);
  867. platform_set_drvdata(pdev, NULL);
  868. return 0;
  869. }
  870. static const struct of_device_id brcm_avs_cpufreq_match[] = {
  871. { .compatible = BRCM_AVS_CPU_DATA },
  872. { }
  873. };
  874. MODULE_DEVICE_TABLE(of, brcm_avs_cpufreq_match);
  875. static struct platform_driver brcm_avs_cpufreq_platdrv = {
  876. .driver = {
  877. .name = BRCM_AVS_CPUFREQ_NAME,
  878. .of_match_table = brcm_avs_cpufreq_match,
  879. },
  880. .probe = brcm_avs_cpufreq_probe,
  881. .remove = brcm_avs_cpufreq_remove,
  882. };
  883. module_platform_driver(brcm_avs_cpufreq_platdrv);
  884. MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
  885. MODULE_DESCRIPTION("CPUfreq driver for Broadcom STB AVS");
  886. MODULE_LICENSE("GPL");