pmc_atom.c 9.9 KB

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