1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IBM Real-Time Linux driver
5 * Copyright (C) IBM Corporation, 2010
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
17 #include <linux/dmi.h>
18 #include <linux/efi.h>
19 #include <linux/mutex.h>
20 #include <asm/bios_ebda.h>
22 #include <linux/io-64-nonatomic-lo-hi.h>
25 module_param(force, bool, 0);
26 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
29 module_param(debug, bool, 0644);
30 MODULE_PARM_DESC(debug, "Show debug output");
32 MODULE_DESCRIPTION("IBM Premium Real Time Mode (PRTM) driver");
33 MODULE_LICENSE("GPL");
37 #define RTL_ADDR_TYPE_IO 1
38 #define RTL_ADDR_TYPE_MMIO 2
40 #define RTL_CMD_ENTER_PRTM 1
41 #define RTL_CMD_EXIT_PRTM 2
43 /* The RTL table as presented by the EBDA: */
44 struct ibm_rtl_table {
45 char signature[5]; /* signature should be "_RTL_" */
54 u32 cmd_port_address; /* platform dependent address */
55 u32 cmd_port_value; /* platform dependent value */
56 } __attribute__((packed));
58 /* to locate "_RTL_" signature do a masked 5-byte integer compare */
59 #define RTL_SIGNATURE 0x0000005f4c54525fULL
60 #define RTL_MASK 0x000000ffffffffffULL
62 #define RTL_DEBUG(fmt, ...) \
65 pr_info(fmt, ##__VA_ARGS__); \
68 static DEFINE_MUTEX(rtl_lock);
69 static struct ibm_rtl_table __iomem *rtl_table;
70 static void __iomem *ebda_map;
71 static void __iomem *rtl_cmd_addr;
72 static u8 rtl_cmd_type;
73 static u8 rtl_cmd_width;
75 static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
77 if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
78 return ioremap(addr, len);
79 return ioport_map(addr, len);
82 static void rtl_port_unmap(void __iomem *addr)
84 if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
90 static int ibm_rtl_write(u8 value)
92 int ret = 0, count = 0;
95 RTL_DEBUG("%s(%d)\n", __func__, value);
97 value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
99 mutex_lock(&rtl_lock);
101 if (ioread8(&rtl_table->rt_status) != value) {
102 iowrite8(value, &rtl_table->command);
104 switch (rtl_cmd_width) {
106 cmd_port_val = ioread8(&rtl_table->cmd_port_value);
107 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
108 iowrite8((u8)cmd_port_val, rtl_cmd_addr);
111 cmd_port_val = ioread16(&rtl_table->cmd_port_value);
112 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
113 iowrite16((u16)cmd_port_val, rtl_cmd_addr);
116 cmd_port_val = ioread32(&rtl_table->cmd_port_value);
117 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
118 iowrite32(cmd_port_val, rtl_cmd_addr);
122 while (ioread8(&rtl_table->command)) {
125 pr_err("Hardware not responding to "
126 "mode switch request\n");
133 if (ioread8(&rtl_table->command_status)) {
134 RTL_DEBUG("command_status reports failed command\n");
139 mutex_unlock(&rtl_lock);
143 static ssize_t rtl_show_version(struct device *dev,
144 struct device_attribute *attr,
147 return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
150 static ssize_t rtl_show_state(struct device *dev,
151 struct device_attribute *attr,
154 return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
157 static ssize_t rtl_set_state(struct device *dev,
158 struct device_attribute *attr,
164 if (count < 1 || count > 2)
169 ret = ibm_rtl_write(0);
172 ret = ibm_rtl_write(1);
183 static const struct bus_type rtl_subsys = {
185 .dev_name = "ibm_rtl",
188 static DEVICE_ATTR(version, S_IRUGO, rtl_show_version, NULL);
189 static DEVICE_ATTR(state, 0600, rtl_show_state, rtl_set_state);
191 static struct device_attribute *rtl_attributes[] = {
198 static int rtl_setup_sysfs(void) {
201 ret = subsys_system_register(&rtl_subsys, NULL);
203 struct device *dev_root = bus_get_dev_root(&rtl_subsys);
206 for (i = 0; rtl_attributes[i]; i ++)
207 device_create_file(dev_root, rtl_attributes[i]);
208 put_device(dev_root);
214 static void rtl_teardown_sysfs(void) {
215 struct device *dev_root = bus_get_dev_root(&rtl_subsys);
219 for (i = 0; rtl_attributes[i]; i ++)
220 device_remove_file(dev_root, rtl_attributes[i]);
221 put_device(dev_root);
223 bus_unregister(&rtl_subsys);
227 static const struct dmi_system_id ibm_rtl_dmi_table[] __initconst = {
230 DMI_MATCH(DMI_SYS_VENDOR, "IBM"), \
236 static int __init ibm_rtl_init(void) {
237 unsigned long ebda_addr, ebda_size;
238 unsigned int ebda_kb;
239 int ret = -ENODEV, i;
242 pr_warn("module loaded by force\n");
243 /* first ensure that we are running on IBM HW */
244 else if (efi_enabled(EFI_BOOT) || !dmi_check_system(ibm_rtl_dmi_table))
247 /* Get the address for the Extended BIOS Data Area */
248 ebda_addr = get_bios_ebda();
250 RTL_DEBUG("no BIOS EBDA found\n");
254 ebda_map = ioremap(ebda_addr, 4);
258 /* First word in the EDBA is the Size in KB */
259 ebda_kb = ioread16(ebda_map);
260 RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
266 ebda_size = ebda_kb*1024;
268 /* Remap the whole table */
269 ebda_map = ioremap(ebda_addr, ebda_size);
273 /* search for the _RTL_ signature at the start of the table */
274 for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
275 struct ibm_rtl_table __iomem * tmp;
276 tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
277 if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
280 RTL_DEBUG("found RTL_SIGNATURE at %p\n", tmp);
282 /* The address, value, width and offset are platform
283 * dependent and found in the ibm_rtl_table */
284 rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
285 rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
286 RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
287 rtl_cmd_width, rtl_cmd_type);
288 addr = ioread32(&rtl_table->cmd_port_address);
289 RTL_DEBUG("addr = %#llx\n", (unsigned long long)addr);
290 plen = rtl_cmd_width/sizeof(char);
291 rtl_cmd_addr = rtl_port_map(addr, plen);
292 RTL_DEBUG("rtl_cmd_addr = %p\n", rtl_cmd_addr);
297 ret = rtl_setup_sysfs();
305 rtl_port_unmap(rtl_cmd_addr);
311 static void __exit ibm_rtl_exit(void)
314 RTL_DEBUG("cleaning up");
315 /* do not leave the machine in SMI-free mode */
317 /* unmap, unlink and remove all traces */
318 rtl_teardown_sysfs();
320 rtl_port_unmap(rtl_cmd_addr);
324 module_init(ibm_rtl_init);
325 module_exit(ibm_rtl_exit);