1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD SoC Power Management Controller Driver
5 * Copyright (c) 2020, Advanced Micro Devices, Inc.
8 * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/acpi.h>
14 #include <linux/bitfield.h>
15 #include <linux/bits.h>
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
19 #include <linux/iopoll.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/suspend.h>
24 #include <linux/seq_file.h>
25 #include <linux/uaccess.h>
27 /* SMU communication registers */
28 #define AMD_PMC_REGISTER_MESSAGE 0x538
29 #define AMD_PMC_REGISTER_RESPONSE 0x980
30 #define AMD_PMC_REGISTER_ARGUMENT 0x9BC
32 /* Base address of SMU for mapping physical address to virtual address */
33 #define AMD_PMC_SMU_INDEX_ADDRESS 0xB8
34 #define AMD_PMC_SMU_INDEX_DATA 0xBC
35 #define AMD_PMC_MAPPING_SIZE 0x01000
36 #define AMD_PMC_BASE_ADDR_OFFSET 0x10000
37 #define AMD_PMC_BASE_ADDR_LO 0x13B102E8
38 #define AMD_PMC_BASE_ADDR_HI 0x13B102EC
39 #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0)
40 #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20)
42 /* SMU Response Codes */
43 #define AMD_PMC_RESULT_OK 0x01
44 #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC
45 #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD
46 #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE
47 #define AMD_PMC_RESULT_FAILED 0xFF
49 /* List of supported CPU ids */
50 #define AMD_CPU_ID_RV 0x15D0
51 #define AMD_CPU_ID_RN 0x1630
52 #define AMD_CPU_ID_PCO AMD_CPU_ID_RV
53 #define AMD_CPU_ID_CZN AMD_CPU_ID_RN
55 #define AMD_SMU_FW_VERSION 0x0
56 #define PMC_MSG_DELAY_MIN_US 100
57 #define RESPONSE_REGISTER_LOOP_MAX 200
66 void __iomem
*regbase
;
67 void __iomem
*smu_base
;
71 #if IS_ENABLED(CONFIG_DEBUG_FS)
72 struct dentry
*dbgfs_dir
;
73 #endif /* CONFIG_DEBUG_FS */
76 static struct amd_pmc_dev pmc
;
78 static inline u32
amd_pmc_reg_read(struct amd_pmc_dev
*dev
, int reg_offset
)
80 return ioread32(dev
->regbase
+ reg_offset
);
83 static inline void amd_pmc_reg_write(struct amd_pmc_dev
*dev
, int reg_offset
, u32 val
)
85 iowrite32(val
, dev
->regbase
+ reg_offset
);
89 static int smu_fw_info_show(struct seq_file
*s
, void *unused
)
91 struct amd_pmc_dev
*dev
= s
->private;
94 value
= ioread32(dev
->smu_base
+ AMD_SMU_FW_VERSION
);
95 seq_printf(s
, "SMU FW Info: %x\n", value
);
98 DEFINE_SHOW_ATTRIBUTE(smu_fw_info
);
100 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev
*dev
)
102 debugfs_remove_recursive(dev
->dbgfs_dir
);
105 static void amd_pmc_dbgfs_register(struct amd_pmc_dev
*dev
)
107 dev
->dbgfs_dir
= debugfs_create_dir("amd_pmc", NULL
);
108 debugfs_create_file("smu_fw_info", 0644, dev
->dbgfs_dir
, dev
,
112 static inline void amd_pmc_dbgfs_register(struct amd_pmc_dev
*dev
)
116 static inline void amd_pmc_dbgfs_unregister(struct amd_pmc_dev
*dev
)
119 #endif /* CONFIG_DEBUG_FS */
121 static void amd_pmc_dump_registers(struct amd_pmc_dev
*dev
)
125 value
= amd_pmc_reg_read(dev
, AMD_PMC_REGISTER_RESPONSE
);
126 dev_dbg(dev
->dev
, "AMD_PMC_REGISTER_RESPONSE:%x\n", value
);
128 value
= amd_pmc_reg_read(dev
, AMD_PMC_REGISTER_ARGUMENT
);
129 dev_dbg(dev
->dev
, "AMD_PMC_REGISTER_ARGUMENT:%x\n", value
);
131 value
= amd_pmc_reg_read(dev
, AMD_PMC_REGISTER_MESSAGE
);
132 dev_dbg(dev
->dev
, "AMD_PMC_REGISTER_MESSAGE:%x\n", value
);
135 static int amd_pmc_send_cmd(struct amd_pmc_dev
*dev
, bool set
)
141 /* Wait until we get a valid response */
142 rc
= readx_poll_timeout(ioread32
, dev
->regbase
+ AMD_PMC_REGISTER_RESPONSE
,
143 val
, val
> 0, PMC_MSG_DELAY_MIN_US
,
144 PMC_MSG_DELAY_MIN_US
* RESPONSE_REGISTER_LOOP_MAX
);
146 dev_err(dev
->dev
, "failed to talk to SMU\n");
150 /* Write zero to response register */
151 amd_pmc_reg_write(dev
, AMD_PMC_REGISTER_RESPONSE
, 0);
153 /* Write argument into response register */
154 amd_pmc_reg_write(dev
, AMD_PMC_REGISTER_ARGUMENT
, set
);
156 /* Write message ID to message ID register */
157 msg
= (dev
->cpu_id
== AMD_CPU_ID_RN
) ? MSG_OS_HINT_RN
: MSG_OS_HINT_PCO
;
158 amd_pmc_reg_write(dev
, AMD_PMC_REGISTER_MESSAGE
, msg
);
162 static int __maybe_unused
amd_pmc_suspend(struct device
*dev
)
164 struct amd_pmc_dev
*pdev
= dev_get_drvdata(dev
);
167 rc
= amd_pmc_send_cmd(pdev
, 1);
169 dev_err(pdev
->dev
, "suspend failed\n");
171 amd_pmc_dump_registers(pdev
);
175 static int __maybe_unused
amd_pmc_resume(struct device
*dev
)
177 struct amd_pmc_dev
*pdev
= dev_get_drvdata(dev
);
180 rc
= amd_pmc_send_cmd(pdev
, 0);
182 dev_err(pdev
->dev
, "resume failed\n");
184 amd_pmc_dump_registers(pdev
);
188 static const struct dev_pm_ops amd_pmc_pm_ops
= {
189 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(amd_pmc_suspend
, amd_pmc_resume
)
192 static const struct pci_device_id pmc_pci_ids
[] = {
193 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_CZN
) },
194 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_RN
) },
195 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_PCO
) },
196 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_RV
) },
200 static int amd_pmc_probe(struct platform_device
*pdev
)
202 struct amd_pmc_dev
*dev
= &pmc
;
203 struct pci_dev
*rdev
;
210 dev
->dev
= &pdev
->dev
;
212 rdev
= pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
213 if (!rdev
|| !pci_match_id(pmc_pci_ids
, rdev
))
216 dev
->cpu_id
= rdev
->device
;
217 err
= pci_write_config_dword(rdev
, AMD_PMC_SMU_INDEX_ADDRESS
, AMD_PMC_BASE_ADDR_LO
);
219 dev_err(dev
->dev
, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS
);
220 return pcibios_err_to_errno(err
);
223 err
= pci_read_config_dword(rdev
, AMD_PMC_SMU_INDEX_DATA
, &val
);
225 return pcibios_err_to_errno(err
);
227 base_addr_lo
= val
& AMD_PMC_BASE_ADDR_HI_MASK
;
229 err
= pci_write_config_dword(rdev
, AMD_PMC_SMU_INDEX_ADDRESS
, AMD_PMC_BASE_ADDR_HI
);
231 dev_err(dev
->dev
, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS
);
232 return pcibios_err_to_errno(err
);
235 err
= pci_read_config_dword(rdev
, AMD_PMC_SMU_INDEX_DATA
, &val
);
237 return pcibios_err_to_errno(err
);
239 base_addr_hi
= val
& AMD_PMC_BASE_ADDR_LO_MASK
;
241 base_addr
= ((u64
)base_addr_hi
<< 32 | base_addr_lo
);
243 dev
->smu_base
= devm_ioremap(dev
->dev
, base_addr
, AMD_PMC_MAPPING_SIZE
);
247 dev
->regbase
= devm_ioremap(dev
->dev
, base_addr
+ AMD_PMC_BASE_ADDR_OFFSET
,
248 AMD_PMC_MAPPING_SIZE
);
252 amd_pmc_dump_registers(dev
);
254 platform_set_drvdata(pdev
, dev
);
255 amd_pmc_dbgfs_register(dev
);
259 static int amd_pmc_remove(struct platform_device
*pdev
)
261 struct amd_pmc_dev
*dev
= platform_get_drvdata(pdev
);
263 amd_pmc_dbgfs_unregister(dev
);
267 static const struct acpi_device_id amd_pmc_acpi_ids
[] = {
272 MODULE_DEVICE_TABLE(acpi
, amd_pmc_acpi_ids
);
274 static struct platform_driver amd_pmc_driver
= {
277 .acpi_match_table
= amd_pmc_acpi_ids
,
278 .pm
= &amd_pmc_pm_ops
,
280 .probe
= amd_pmc_probe
,
281 .remove
= amd_pmc_remove
,
283 module_platform_driver(amd_pmc_driver
);
285 MODULE_LICENSE("GPL v2");
286 MODULE_DESCRIPTION("AMD PMC Driver");