pmc_atom.c 12 KB

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