intel_pmc_bxt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Intel Broxton PMC
  4. *
  5. * (C) Copyright 2014 - 2020 Intel Corporation
  6. *
  7. * This driver is based on Intel SCU IPC driver (intel_scu_ipc.c) by
  8. * Sreedhara DS <[email protected]>
  9. *
  10. * The PMC (Power Management Controller) running on the ARC processor
  11. * communicates with another entity running in the IA (Intel Architecture)
  12. * core through an IPC (Intel Processor Communications) mechanism which in
  13. * turn sends messages between the IA and the PMC.
  14. */
  15. #include <linux/acpi.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io-64-nonatomic-lo-hi.h>
  20. #include <linux/mfd/core.h>
  21. #include <linux/mfd/intel_pmc_bxt.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/platform_data/itco_wdt.h>
  25. #include <asm/intel_scu_ipc.h>
  26. /* Residency with clock rate at 19.2MHz to usecs */
  27. #define S0IX_RESIDENCY_IN_USECS(d, s) \
  28. ({ \
  29. u64 result = 10ull * ((d) + (s)); \
  30. do_div(result, 192); \
  31. result; \
  32. })
  33. /* Resources exported from IFWI */
  34. #define PLAT_RESOURCE_IPC_INDEX 0
  35. #define PLAT_RESOURCE_IPC_SIZE 0x1000
  36. #define PLAT_RESOURCE_GCR_OFFSET 0x1000
  37. #define PLAT_RESOURCE_GCR_SIZE 0x1000
  38. #define PLAT_RESOURCE_BIOS_DATA_INDEX 1
  39. #define PLAT_RESOURCE_BIOS_IFACE_INDEX 2
  40. #define PLAT_RESOURCE_TELEM_SSRAM_INDEX 3
  41. #define PLAT_RESOURCE_ISP_DATA_INDEX 4
  42. #define PLAT_RESOURCE_ISP_IFACE_INDEX 5
  43. #define PLAT_RESOURCE_GTD_DATA_INDEX 6
  44. #define PLAT_RESOURCE_GTD_IFACE_INDEX 7
  45. #define PLAT_RESOURCE_ACPI_IO_INDEX 0
  46. /*
  47. * BIOS does not create an ACPI device for each PMC function, but
  48. * exports multiple resources from one ACPI device (IPC) for multiple
  49. * functions. This driver is responsible for creating a child device and
  50. * to export resources for those functions.
  51. */
  52. #define SMI_EN_OFFSET 0x0040
  53. #define SMI_EN_SIZE 4
  54. #define TCO_BASE_OFFSET 0x0060
  55. #define TCO_REGS_SIZE 16
  56. #define TELEM_SSRAM_SIZE 240
  57. #define TELEM_PMC_SSRAM_OFFSET 0x1b00
  58. #define TELEM_PUNIT_SSRAM_OFFSET 0x1a00
  59. /* Commands */
  60. #define PMC_NORTHPEAK_CTRL 0xed
  61. static inline bool is_gcr_valid(u32 offset)
  62. {
  63. return offset < PLAT_RESOURCE_GCR_SIZE - 8;
  64. }
  65. /**
  66. * intel_pmc_gcr_read64() - Read a 64-bit PMC GCR register
  67. * @pmc: PMC device pointer
  68. * @offset: offset of GCR register from GCR address base
  69. * @data: data pointer for storing the register output
  70. *
  71. * Reads the 64-bit PMC GCR register at given offset.
  72. *
  73. * Return: Negative value on error or 0 on success.
  74. */
  75. int intel_pmc_gcr_read64(struct intel_pmc_dev *pmc, u32 offset, u64 *data)
  76. {
  77. if (!is_gcr_valid(offset))
  78. return -EINVAL;
  79. spin_lock(&pmc->gcr_lock);
  80. *data = readq(pmc->gcr_mem_base + offset);
  81. spin_unlock(&pmc->gcr_lock);
  82. return 0;
  83. }
  84. EXPORT_SYMBOL_GPL(intel_pmc_gcr_read64);
  85. /**
  86. * intel_pmc_gcr_update() - Update PMC GCR register bits
  87. * @pmc: PMC device pointer
  88. * @offset: offset of GCR register from GCR address base
  89. * @mask: bit mask for update operation
  90. * @val: update value
  91. *
  92. * Updates the bits of given GCR register as specified by
  93. * @mask and @val.
  94. *
  95. * Return: Negative value on error or 0 on success.
  96. */
  97. int intel_pmc_gcr_update(struct intel_pmc_dev *pmc, u32 offset, u32 mask, u32 val)
  98. {
  99. u32 new_val;
  100. if (!is_gcr_valid(offset))
  101. return -EINVAL;
  102. spin_lock(&pmc->gcr_lock);
  103. new_val = readl(pmc->gcr_mem_base + offset);
  104. new_val = (new_val & ~mask) | (val & mask);
  105. writel(new_val, pmc->gcr_mem_base + offset);
  106. new_val = readl(pmc->gcr_mem_base + offset);
  107. spin_unlock(&pmc->gcr_lock);
  108. /* Check whether the bit update is successful */
  109. return (new_val & mask) != (val & mask) ? -EIO : 0;
  110. }
  111. EXPORT_SYMBOL_GPL(intel_pmc_gcr_update);
  112. /**
  113. * intel_pmc_s0ix_counter_read() - Read S0ix residency
  114. * @pmc: PMC device pointer
  115. * @data: Out param that contains current S0ix residency count.
  116. *
  117. * Writes to @data how many usecs the system has been in low-power S0ix
  118. * state.
  119. *
  120. * Return: An error code or 0 on success.
  121. */
  122. int intel_pmc_s0ix_counter_read(struct intel_pmc_dev *pmc, u64 *data)
  123. {
  124. u64 deep, shlw;
  125. spin_lock(&pmc->gcr_lock);
  126. deep = readq(pmc->gcr_mem_base + PMC_GCR_TELEM_DEEP_S0IX_REG);
  127. shlw = readq(pmc->gcr_mem_base + PMC_GCR_TELEM_SHLW_S0IX_REG);
  128. spin_unlock(&pmc->gcr_lock);
  129. *data = S0IX_RESIDENCY_IN_USECS(deep, shlw);
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(intel_pmc_s0ix_counter_read);
  133. /**
  134. * simplecmd_store() - Send a simple IPC command
  135. * @dev: Device under the attribute is
  136. * @attr: Attribute in question
  137. * @buf: Buffer holding data to be stored to the attribute
  138. * @count: Number of bytes in @buf
  139. *
  140. * Expects a string with two integers separated with space. These two
  141. * values hold command and subcommand that is send to PMC.
  142. *
  143. * Return: Number number of bytes written (@count) or negative errno in
  144. * case of error.
  145. */
  146. static ssize_t simplecmd_store(struct device *dev, struct device_attribute *attr,
  147. const char *buf, size_t count)
  148. {
  149. struct intel_pmc_dev *pmc = dev_get_drvdata(dev);
  150. struct intel_scu_ipc_dev *scu = pmc->scu;
  151. int subcmd;
  152. int cmd;
  153. int ret;
  154. ret = sscanf(buf, "%d %d", &cmd, &subcmd);
  155. if (ret != 2) {
  156. dev_err(dev, "Invalid values, expected: cmd subcmd\n");
  157. return -EINVAL;
  158. }
  159. ret = intel_scu_ipc_dev_simple_command(scu, cmd, subcmd);
  160. if (ret)
  161. return ret;
  162. return count;
  163. }
  164. static DEVICE_ATTR_WO(simplecmd);
  165. /**
  166. * northpeak_store() - Enable or disable Northpeak
  167. * @dev: Device under the attribute is
  168. * @attr: Attribute in question
  169. * @buf: Buffer holding data to be stored to the attribute
  170. * @count: Number of bytes in @buf
  171. *
  172. * Expects an unsigned integer. Non-zero enables Northpeak and zero
  173. * disables it.
  174. *
  175. * Return: Number number of bytes written (@count) or negative errno in
  176. * case of error.
  177. */
  178. static ssize_t northpeak_store(struct device *dev, struct device_attribute *attr,
  179. const char *buf, size_t count)
  180. {
  181. struct intel_pmc_dev *pmc = dev_get_drvdata(dev);
  182. struct intel_scu_ipc_dev *scu = pmc->scu;
  183. unsigned long val;
  184. int subcmd;
  185. int ret;
  186. ret = kstrtoul(buf, 0, &val);
  187. if (ret)
  188. return ret;
  189. /* Northpeak is enabled if subcmd == 1 and disabled if it is 0 */
  190. if (val)
  191. subcmd = 1;
  192. else
  193. subcmd = 0;
  194. ret = intel_scu_ipc_dev_simple_command(scu, PMC_NORTHPEAK_CTRL, subcmd);
  195. if (ret)
  196. return ret;
  197. return count;
  198. }
  199. static DEVICE_ATTR_WO(northpeak);
  200. static struct attribute *intel_pmc_attrs[] = {
  201. &dev_attr_northpeak.attr,
  202. &dev_attr_simplecmd.attr,
  203. NULL
  204. };
  205. static const struct attribute_group intel_pmc_group = {
  206. .attrs = intel_pmc_attrs,
  207. };
  208. static const struct attribute_group *intel_pmc_groups[] = {
  209. &intel_pmc_group,
  210. NULL
  211. };
  212. static struct resource punit_res[6];
  213. static struct mfd_cell punit = {
  214. .name = "intel_punit_ipc",
  215. .resources = punit_res,
  216. };
  217. static struct itco_wdt_platform_data tco_pdata = {
  218. .name = "Apollo Lake SoC",
  219. .version = 5,
  220. .no_reboot_use_pmc = true,
  221. };
  222. static struct resource tco_res[2];
  223. static const struct mfd_cell tco = {
  224. .name = "iTCO_wdt",
  225. .ignore_resource_conflicts = true,
  226. .resources = tco_res,
  227. .num_resources = ARRAY_SIZE(tco_res),
  228. .platform_data = &tco_pdata,
  229. .pdata_size = sizeof(tco_pdata),
  230. };
  231. static const struct resource telem_res[] = {
  232. DEFINE_RES_MEM(TELEM_PUNIT_SSRAM_OFFSET, TELEM_SSRAM_SIZE),
  233. DEFINE_RES_MEM(TELEM_PMC_SSRAM_OFFSET, TELEM_SSRAM_SIZE),
  234. };
  235. static const struct mfd_cell telem = {
  236. .name = "intel_telemetry",
  237. .resources = telem_res,
  238. .num_resources = ARRAY_SIZE(telem_res),
  239. };
  240. static int intel_pmc_get_tco_resources(struct platform_device *pdev)
  241. {
  242. struct resource *res;
  243. if (acpi_has_watchdog())
  244. return 0;
  245. res = platform_get_resource(pdev, IORESOURCE_IO,
  246. PLAT_RESOURCE_ACPI_IO_INDEX);
  247. if (!res) {
  248. dev_err(&pdev->dev, "Failed to get IO resource\n");
  249. return -EINVAL;
  250. }
  251. tco_res[0].flags = IORESOURCE_IO;
  252. tco_res[0].start = res->start + TCO_BASE_OFFSET;
  253. tco_res[0].end = tco_res[0].start + TCO_REGS_SIZE - 1;
  254. tco_res[1].flags = IORESOURCE_IO;
  255. tco_res[1].start = res->start + SMI_EN_OFFSET;
  256. tco_res[1].end = tco_res[1].start + SMI_EN_SIZE - 1;
  257. return 0;
  258. }
  259. static int intel_pmc_get_resources(struct platform_device *pdev,
  260. struct intel_pmc_dev *pmc,
  261. struct intel_scu_ipc_data *scu_data)
  262. {
  263. struct resource gcr_res;
  264. size_t npunit_res = 0;
  265. struct resource *res;
  266. int ret;
  267. scu_data->irq = platform_get_irq_optional(pdev, 0);
  268. res = platform_get_resource(pdev, IORESOURCE_MEM,
  269. PLAT_RESOURCE_IPC_INDEX);
  270. if (!res) {
  271. dev_err(&pdev->dev, "Failed to get IPC resource\n");
  272. return -EINVAL;
  273. }
  274. /* IPC registers */
  275. scu_data->mem.flags = res->flags;
  276. scu_data->mem.start = res->start;
  277. scu_data->mem.end = res->start + PLAT_RESOURCE_IPC_SIZE - 1;
  278. /* GCR registers */
  279. gcr_res.flags = res->flags;
  280. gcr_res.start = res->start + PLAT_RESOURCE_GCR_OFFSET;
  281. gcr_res.end = gcr_res.start + PLAT_RESOURCE_GCR_SIZE - 1;
  282. pmc->gcr_mem_base = devm_ioremap_resource(&pdev->dev, &gcr_res);
  283. if (IS_ERR(pmc->gcr_mem_base))
  284. return PTR_ERR(pmc->gcr_mem_base);
  285. /* Only register iTCO watchdog if there is no WDAT ACPI table */
  286. ret = intel_pmc_get_tco_resources(pdev);
  287. if (ret)
  288. return ret;
  289. /* BIOS data register */
  290. res = platform_get_resource(pdev, IORESOURCE_MEM,
  291. PLAT_RESOURCE_BIOS_DATA_INDEX);
  292. if (!res) {
  293. dev_err(&pdev->dev, "Failed to get resource of P-unit BIOS data\n");
  294. return -EINVAL;
  295. }
  296. punit_res[npunit_res++] = *res;
  297. /* BIOS interface register */
  298. res = platform_get_resource(pdev, IORESOURCE_MEM,
  299. PLAT_RESOURCE_BIOS_IFACE_INDEX);
  300. if (!res) {
  301. dev_err(&pdev->dev, "Failed to get resource of P-unit BIOS interface\n");
  302. return -EINVAL;
  303. }
  304. punit_res[npunit_res++] = *res;
  305. /* ISP data register, optional */
  306. res = platform_get_resource(pdev, IORESOURCE_MEM,
  307. PLAT_RESOURCE_ISP_DATA_INDEX);
  308. if (res)
  309. punit_res[npunit_res++] = *res;
  310. /* ISP interface register, optional */
  311. res = platform_get_resource(pdev, IORESOURCE_MEM,
  312. PLAT_RESOURCE_ISP_IFACE_INDEX);
  313. if (res)
  314. punit_res[npunit_res++] = *res;
  315. /* GTD data register, optional */
  316. res = platform_get_resource(pdev, IORESOURCE_MEM,
  317. PLAT_RESOURCE_GTD_DATA_INDEX);
  318. if (res)
  319. punit_res[npunit_res++] = *res;
  320. /* GTD interface register, optional */
  321. res = platform_get_resource(pdev, IORESOURCE_MEM,
  322. PLAT_RESOURCE_GTD_IFACE_INDEX);
  323. if (res)
  324. punit_res[npunit_res++] = *res;
  325. punit.num_resources = npunit_res;
  326. /* Telemetry SSRAM is optional */
  327. res = platform_get_resource(pdev, IORESOURCE_MEM,
  328. PLAT_RESOURCE_TELEM_SSRAM_INDEX);
  329. if (res)
  330. pmc->telem_base = res;
  331. return 0;
  332. }
  333. static int intel_pmc_create_devices(struct intel_pmc_dev *pmc)
  334. {
  335. int ret;
  336. if (!acpi_has_watchdog()) {
  337. ret = devm_mfd_add_devices(pmc->dev, PLATFORM_DEVID_AUTO, &tco,
  338. 1, NULL, 0, NULL);
  339. if (ret)
  340. return ret;
  341. }
  342. ret = devm_mfd_add_devices(pmc->dev, PLATFORM_DEVID_AUTO, &punit, 1,
  343. NULL, 0, NULL);
  344. if (ret)
  345. return ret;
  346. if (pmc->telem_base) {
  347. ret = devm_mfd_add_devices(pmc->dev, PLATFORM_DEVID_AUTO,
  348. &telem, 1, pmc->telem_base, 0, NULL);
  349. }
  350. return ret;
  351. }
  352. static const struct acpi_device_id intel_pmc_acpi_ids[] = {
  353. { "INT34D2" },
  354. { }
  355. };
  356. MODULE_DEVICE_TABLE(acpi, intel_pmc_acpi_ids);
  357. static int intel_pmc_probe(struct platform_device *pdev)
  358. {
  359. struct intel_scu_ipc_data scu_data = {};
  360. struct intel_pmc_dev *pmc;
  361. int ret;
  362. pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
  363. if (!pmc)
  364. return -ENOMEM;
  365. pmc->dev = &pdev->dev;
  366. spin_lock_init(&pmc->gcr_lock);
  367. ret = intel_pmc_get_resources(pdev, pmc, &scu_data);
  368. if (ret) {
  369. dev_err(&pdev->dev, "Failed to request resources\n");
  370. return ret;
  371. }
  372. pmc->scu = devm_intel_scu_ipc_register(&pdev->dev, &scu_data);
  373. if (IS_ERR(pmc->scu))
  374. return PTR_ERR(pmc->scu);
  375. platform_set_drvdata(pdev, pmc);
  376. ret = intel_pmc_create_devices(pmc);
  377. if (ret)
  378. dev_err(&pdev->dev, "Failed to create PMC devices\n");
  379. return ret;
  380. }
  381. static struct platform_driver intel_pmc_driver = {
  382. .probe = intel_pmc_probe,
  383. .driver = {
  384. .name = "intel_pmc_bxt",
  385. .acpi_match_table = intel_pmc_acpi_ids,
  386. .dev_groups = intel_pmc_groups,
  387. },
  388. };
  389. module_platform_driver(intel_pmc_driver);
  390. MODULE_AUTHOR("Mika Westerberg <[email protected]>");
  391. MODULE_AUTHOR("Zha Qipeng <[email protected]>");
  392. MODULE_DESCRIPTION("Intel Broxton PMC driver");
  393. MODULE_LICENSE("GPL v2");