pmc_atom.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Intel Atom SOC Power Management Controller Driver
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/pci.h>
  19. #include <linux/device.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/io.h>
  23. #include <asm/pmc_atom.h>
  24. struct pmc_dev {
  25. u32 base_addr;
  26. void __iomem *regmap;
  27. #ifdef CONFIG_DEBUG_FS
  28. struct dentry *dbgfs_dir;
  29. #endif /* CONFIG_DEBUG_FS */
  30. };
  31. static struct pmc_dev pmc_device;
  32. static u32 acpi_base_addr;
  33. struct pmc_bit_map {
  34. const char *name;
  35. u32 bit_mask;
  36. };
  37. static const struct pmc_bit_map dev_map[] = {
  38. {"0 - LPSS1_F0_DMA", BIT_LPSS1_F0_DMA},
  39. {"1 - LPSS1_F1_PWM1", BIT_LPSS1_F1_PWM1},
  40. {"2 - LPSS1_F2_PWM2", BIT_LPSS1_F2_PWM2},
  41. {"3 - LPSS1_F3_HSUART1", BIT_LPSS1_F3_HSUART1},
  42. {"4 - LPSS1_F4_HSUART2", BIT_LPSS1_F4_HSUART2},
  43. {"5 - LPSS1_F5_SPI", BIT_LPSS1_F5_SPI},
  44. {"6 - LPSS1_F6_Reserved", BIT_LPSS1_F6_XXX},
  45. {"7 - LPSS1_F7_Reserved", BIT_LPSS1_F7_XXX},
  46. {"8 - SCC_EMMC", BIT_SCC_EMMC},
  47. {"9 - SCC_SDIO", BIT_SCC_SDIO},
  48. {"10 - SCC_SDCARD", BIT_SCC_SDCARD},
  49. {"11 - SCC_MIPI", BIT_SCC_MIPI},
  50. {"12 - HDA", BIT_HDA},
  51. {"13 - LPE", BIT_LPE},
  52. {"14 - OTG", BIT_OTG},
  53. {"15 - USH", BIT_USH},
  54. {"16 - GBE", BIT_GBE},
  55. {"17 - SATA", BIT_SATA},
  56. {"18 - USB_EHCI", BIT_USB_EHCI},
  57. {"19 - SEC", BIT_SEC},
  58. {"20 - PCIE_PORT0", BIT_PCIE_PORT0},
  59. {"21 - PCIE_PORT1", BIT_PCIE_PORT1},
  60. {"22 - PCIE_PORT2", BIT_PCIE_PORT2},
  61. {"23 - PCIE_PORT3", BIT_PCIE_PORT3},
  62. {"24 - LPSS2_F0_DMA", BIT_LPSS2_F0_DMA},
  63. {"25 - LPSS2_F1_I2C1", BIT_LPSS2_F1_I2C1},
  64. {"26 - LPSS2_F2_I2C2", BIT_LPSS2_F2_I2C2},
  65. {"27 - LPSS2_F3_I2C3", BIT_LPSS2_F3_I2C3},
  66. {"28 - LPSS2_F3_I2C4", BIT_LPSS2_F4_I2C4},
  67. {"29 - LPSS2_F5_I2C5", BIT_LPSS2_F5_I2C5},
  68. {"30 - LPSS2_F6_I2C6", BIT_LPSS2_F6_I2C6},
  69. {"31 - LPSS2_F7_I2C7", BIT_LPSS2_F7_I2C7},
  70. {"32 - SMB", BIT_SMB},
  71. {"33 - OTG_SS_PHY", BIT_OTG_SS_PHY},
  72. {"34 - USH_SS_PHY", BIT_USH_SS_PHY},
  73. {"35 - DFX", BIT_DFX},
  74. };
  75. static const struct pmc_bit_map pss_map[] = {
  76. {"0 - GBE", PMC_PSS_BIT_GBE},
  77. {"1 - SATA", PMC_PSS_BIT_SATA},
  78. {"2 - HDA", PMC_PSS_BIT_HDA},
  79. {"3 - SEC", PMC_PSS_BIT_SEC},
  80. {"4 - PCIE", PMC_PSS_BIT_PCIE},
  81. {"5 - LPSS", PMC_PSS_BIT_LPSS},
  82. {"6 - LPE", PMC_PSS_BIT_LPE},
  83. {"7 - DFX", PMC_PSS_BIT_DFX},
  84. {"8 - USH_CTRL", PMC_PSS_BIT_USH_CTRL},
  85. {"9 - USH_SUS", PMC_PSS_BIT_USH_SUS},
  86. {"10 - USH_VCCS", PMC_PSS_BIT_USH_VCCS},
  87. {"11 - USH_VCCA", PMC_PSS_BIT_USH_VCCA},
  88. {"12 - OTG_CTRL", PMC_PSS_BIT_OTG_CTRL},
  89. {"13 - OTG_VCCS", PMC_PSS_BIT_OTG_VCCS},
  90. {"14 - OTG_VCCA_CLK", PMC_PSS_BIT_OTG_VCCA_CLK},
  91. {"15 - OTG_VCCA", PMC_PSS_BIT_OTG_VCCA},
  92. {"16 - USB", PMC_PSS_BIT_USB},
  93. {"17 - USB_SUS", PMC_PSS_BIT_USB_SUS},
  94. };
  95. static inline u32 pmc_reg_read(struct pmc_dev *pmc, int reg_offset)
  96. {
  97. return readl(pmc->regmap + reg_offset);
  98. }
  99. static inline void pmc_reg_write(struct pmc_dev *pmc, int reg_offset, u32 val)
  100. {
  101. writel(val, pmc->regmap + reg_offset);
  102. }
  103. static void pmc_power_off(void)
  104. {
  105. u16 pm1_cnt_port;
  106. u32 pm1_cnt_value;
  107. pr_info("Preparing to enter system sleep state S5\n");
  108. pm1_cnt_port = acpi_base_addr + PM1_CNT;
  109. pm1_cnt_value = inl(pm1_cnt_port);
  110. pm1_cnt_value &= SLEEP_TYPE_MASK;
  111. pm1_cnt_value |= SLEEP_TYPE_S5;
  112. pm1_cnt_value |= SLEEP_ENABLE;
  113. outl(pm1_cnt_value, pm1_cnt_port);
  114. }
  115. static void pmc_hw_reg_setup(struct pmc_dev *pmc)
  116. {
  117. /*
  118. * Disable PMC S0IX_WAKE_EN events coming from:
  119. * - LPC clock run
  120. * - GPIO_SUS ored dedicated IRQs
  121. * - GPIO_SCORE ored dedicated IRQs
  122. * - GPIO_SUS shared IRQ
  123. * - GPIO_SCORE shared IRQ
  124. */
  125. pmc_reg_write(pmc, PMC_S0IX_WAKE_EN, (u32)PMC_WAKE_EN_SETTING);
  126. }
  127. #ifdef CONFIG_DEBUG_FS
  128. static int pmc_dev_state_show(struct seq_file *s, void *unused)
  129. {
  130. struct pmc_dev *pmc = s->private;
  131. u32 func_dis, func_dis_2, func_dis_index;
  132. u32 d3_sts_0, d3_sts_1, d3_sts_index;
  133. int dev_num, dev_index, reg_index;
  134. func_dis = pmc_reg_read(pmc, PMC_FUNC_DIS);
  135. func_dis_2 = pmc_reg_read(pmc, PMC_FUNC_DIS_2);
  136. d3_sts_0 = pmc_reg_read(pmc, PMC_D3_STS_0);
  137. d3_sts_1 = pmc_reg_read(pmc, PMC_D3_STS_1);
  138. dev_num = ARRAY_SIZE(dev_map);
  139. for (dev_index = 0; dev_index < dev_num; dev_index++) {
  140. reg_index = dev_index / PMC_REG_BIT_WIDTH;
  141. if (reg_index) {
  142. func_dis_index = func_dis_2;
  143. d3_sts_index = d3_sts_1;
  144. } else {
  145. func_dis_index = func_dis;
  146. d3_sts_index = d3_sts_0;
  147. }
  148. seq_printf(s, "Dev: %-32s\tState: %s [%s]\n",
  149. dev_map[dev_index].name,
  150. dev_map[dev_index].bit_mask & func_dis_index ?
  151. "Disabled" : "Enabled ",
  152. dev_map[dev_index].bit_mask & d3_sts_index ?
  153. "D3" : "D0");
  154. }
  155. return 0;
  156. }
  157. static int pmc_dev_state_open(struct inode *inode, struct file *file)
  158. {
  159. return single_open(file, pmc_dev_state_show, inode->i_private);
  160. }
  161. static const struct file_operations pmc_dev_state_ops = {
  162. .open = pmc_dev_state_open,
  163. .read = seq_read,
  164. .llseek = seq_lseek,
  165. .release = single_release,
  166. };
  167. static int pmc_pss_state_show(struct seq_file *s, void *unused)
  168. {
  169. struct pmc_dev *pmc = s->private;
  170. u32 pss = pmc_reg_read(pmc, PMC_PSS);
  171. int pss_index;
  172. for (pss_index = 0; pss_index < ARRAY_SIZE(pss_map); pss_index++) {
  173. seq_printf(s, "Island: %-32s\tState: %s\n",
  174. pss_map[pss_index].name,
  175. pss_map[pss_index].bit_mask & pss ? "Off" : "On");
  176. }
  177. return 0;
  178. }
  179. static int pmc_pss_state_open(struct inode *inode, struct file *file)
  180. {
  181. return single_open(file, pmc_pss_state_show, inode->i_private);
  182. }
  183. static const struct file_operations pmc_pss_state_ops = {
  184. .open = pmc_pss_state_open,
  185. .read = seq_read,
  186. .llseek = seq_lseek,
  187. .release = single_release,
  188. };
  189. static int pmc_sleep_tmr_show(struct seq_file *s, void *unused)
  190. {
  191. struct pmc_dev *pmc = s->private;
  192. u64 s0ir_tmr, s0i1_tmr, s0i2_tmr, s0i3_tmr, s0_tmr;
  193. s0ir_tmr = (u64)pmc_reg_read(pmc, PMC_S0IR_TMR) << PMC_TMR_SHIFT;
  194. s0i1_tmr = (u64)pmc_reg_read(pmc, PMC_S0I1_TMR) << PMC_TMR_SHIFT;
  195. s0i2_tmr = (u64)pmc_reg_read(pmc, PMC_S0I2_TMR) << PMC_TMR_SHIFT;
  196. s0i3_tmr = (u64)pmc_reg_read(pmc, PMC_S0I3_TMR) << PMC_TMR_SHIFT;
  197. s0_tmr = (u64)pmc_reg_read(pmc, PMC_S0_TMR) << PMC_TMR_SHIFT;
  198. seq_printf(s, "S0IR Residency:\t%lldus\n", s0ir_tmr);
  199. seq_printf(s, "S0I1 Residency:\t%lldus\n", s0i1_tmr);
  200. seq_printf(s, "S0I2 Residency:\t%lldus\n", s0i2_tmr);
  201. seq_printf(s, "S0I3 Residency:\t%lldus\n", s0i3_tmr);
  202. seq_printf(s, "S0 Residency:\t%lldus\n", s0_tmr);
  203. return 0;
  204. }
  205. static int pmc_sleep_tmr_open(struct inode *inode, struct file *file)
  206. {
  207. return single_open(file, pmc_sleep_tmr_show, inode->i_private);
  208. }
  209. static const struct file_operations pmc_sleep_tmr_ops = {
  210. .open = pmc_sleep_tmr_open,
  211. .read = seq_read,
  212. .llseek = seq_lseek,
  213. .release = single_release,
  214. };
  215. static void pmc_dbgfs_unregister(struct pmc_dev *pmc)
  216. {
  217. debugfs_remove_recursive(pmc->dbgfs_dir);
  218. }
  219. static int pmc_dbgfs_register(struct pmc_dev *pmc, struct pci_dev *pdev)
  220. {
  221. struct dentry *dir, *f;
  222. dir = debugfs_create_dir("pmc_atom", NULL);
  223. if (!dir)
  224. return -ENOMEM;
  225. pmc->dbgfs_dir = dir;
  226. f = debugfs_create_file("dev_state", S_IFREG | S_IRUGO,
  227. dir, pmc, &pmc_dev_state_ops);
  228. if (!f) {
  229. dev_err(&pdev->dev, "dev_state register failed\n");
  230. goto err;
  231. }
  232. f = debugfs_create_file("pss_state", S_IFREG | S_IRUGO,
  233. dir, pmc, &pmc_pss_state_ops);
  234. if (!f) {
  235. dev_err(&pdev->dev, "pss_state register failed\n");
  236. goto err;
  237. }
  238. f = debugfs_create_file("sleep_state", S_IFREG | S_IRUGO,
  239. dir, pmc, &pmc_sleep_tmr_ops);
  240. if (!f) {
  241. dev_err(&pdev->dev, "sleep_state register failed\n");
  242. goto err;
  243. }
  244. return 0;
  245. err:
  246. pmc_dbgfs_unregister(pmc);
  247. return -ENODEV;
  248. }
  249. #else
  250. static int pmc_dbgfs_register(struct pmc_dev *pmc, struct pci_dev *pdev)
  251. {
  252. return 0;
  253. }
  254. #endif /* CONFIG_DEBUG_FS */
  255. static int pmc_setup_dev(struct pci_dev *pdev)
  256. {
  257. struct pmc_dev *pmc = &pmc_device;
  258. int ret;
  259. /* Obtain ACPI base address */
  260. pci_read_config_dword(pdev, ACPI_BASE_ADDR_OFFSET, &acpi_base_addr);
  261. acpi_base_addr &= ACPI_BASE_ADDR_MASK;
  262. /* Install power off function */
  263. if (acpi_base_addr != 0 && pm_power_off == NULL)
  264. pm_power_off = pmc_power_off;
  265. pci_read_config_dword(pdev, PMC_BASE_ADDR_OFFSET, &pmc->base_addr);
  266. pmc->base_addr &= PMC_BASE_ADDR_MASK;
  267. pmc->regmap = ioremap_nocache(pmc->base_addr, PMC_MMIO_REG_LEN);
  268. if (!pmc->regmap) {
  269. dev_err(&pdev->dev, "error: ioremap failed\n");
  270. return -ENOMEM;
  271. }
  272. /* PMC hardware registers setup */
  273. pmc_hw_reg_setup(pmc);
  274. ret = pmc_dbgfs_register(pmc, pdev);
  275. if (ret) {
  276. iounmap(pmc->regmap);
  277. }
  278. return ret;
  279. }
  280. /*
  281. * Data for PCI driver interface
  282. *
  283. * This data only exists for exporting the supported
  284. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  285. * register a pci_driver, because lpc_ich will register
  286. * a driver on the same PCI id.
  287. */
  288. static const struct pci_device_id pmc_pci_ids[] = {
  289. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VLV_PMC) },
  290. { 0, },
  291. };
  292. MODULE_DEVICE_TABLE(pci, pmc_pci_ids);
  293. static int __init pmc_atom_init(void)
  294. {
  295. struct pci_dev *pdev = NULL;
  296. const struct pci_device_id *ent;
  297. /* We look for our device - PCU PMC
  298. * we assume that there is max. one device.
  299. *
  300. * We can't use plain pci_driver mechanism,
  301. * as the device is really a multiple function device,
  302. * main driver that binds to the pci_device is lpc_ich
  303. * and have to find & bind to the device this way.
  304. */
  305. for_each_pci_dev(pdev) {
  306. ent = pci_match_id(pmc_pci_ids, pdev);
  307. if (ent)
  308. return pmc_setup_dev(pdev);
  309. }
  310. /* Device not found. */
  311. return -ENODEV;
  312. }
  313. module_init(pmc_atom_init);
  314. /* no module_exit, this driver shouldn't be unloaded */
  315. MODULE_AUTHOR("Aubrey Li <aubrey.li@linux.intel.com>");
  316. MODULE_DESCRIPTION("Intel Atom SOC Power Management Controller Interface");
  317. MODULE_LICENSE("GPL v2");