2 * IBM Real-Time Linux driver
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2010
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/module.h>
31 #include <linux/dmi.h>
32 #include <linux/efi.h>
33 #include <linux/mutex.h>
34 #include <asm/bios_ebda.h>
36 #include <asm-generic/io-64-nonatomic-lo-hi.h>
39 module_param(force, bool, 0);
40 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
43 module_param(debug, bool, 0644);
44 MODULE_PARM_DESC(debug, "Show debug output");
46 MODULE_LICENSE("GPL");
50 #define RTL_ADDR_TYPE_IO 1
51 #define RTL_ADDR_TYPE_MMIO 2
53 #define RTL_CMD_ENTER_PRTM 1
54 #define RTL_CMD_EXIT_PRTM 2
56 /* The RTL table as presented by the EBDA: */
57 struct ibm_rtl_table {
58 char signature[5]; /* signature should be "_RTL_" */
67 u32 cmd_port_address; /* platform dependent address */
68 u32 cmd_port_value; /* platform dependent value */
69 } __attribute__((packed));
71 /* to locate "_RTL_" signature do a masked 5-byte integer compare */
72 #define RTL_SIGNATURE 0x0000005f4c54525fULL
73 #define RTL_MASK 0x000000ffffffffffULL
75 #define RTL_DEBUG(fmt, ...) \
78 pr_info(fmt, ##__VA_ARGS__); \
81 static DEFINE_MUTEX(rtl_lock);
82 static struct ibm_rtl_table __iomem *rtl_table;
83 static void __iomem *ebda_map;
84 static void __iomem *rtl_cmd_addr;
85 static u8 rtl_cmd_type;
86 static u8 rtl_cmd_width;
88 static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
90 if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
91 return ioremap(addr, len);
92 return ioport_map(addr, len);
95 static void rtl_port_unmap(void __iomem *addr)
97 if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
103 static int ibm_rtl_write(u8 value)
105 int ret = 0, count = 0;
106 static u32 cmd_port_val;
108 RTL_DEBUG("%s(%d)\n", __func__, value);
110 value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
112 mutex_lock(&rtl_lock);
114 if (ioread8(&rtl_table->rt_status) != value) {
115 iowrite8(value, &rtl_table->command);
117 switch (rtl_cmd_width) {
119 cmd_port_val = ioread8(&rtl_table->cmd_port_value);
120 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
121 iowrite8((u8)cmd_port_val, rtl_cmd_addr);
124 cmd_port_val = ioread16(&rtl_table->cmd_port_value);
125 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
126 iowrite16((u16)cmd_port_val, rtl_cmd_addr);
129 cmd_port_val = ioread32(&rtl_table->cmd_port_value);
130 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
131 iowrite32(cmd_port_val, rtl_cmd_addr);
135 while (ioread8(&rtl_table->command)) {
138 pr_err("Hardware not responding to "
139 "mode switch request\n");
146 if (ioread8(&rtl_table->command_status)) {
147 RTL_DEBUG("command_status reports failed command\n");
152 mutex_unlock(&rtl_lock);
156 static ssize_t rtl_show_version(struct device *dev,
157 struct device_attribute *attr,
160 return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
163 static ssize_t rtl_show_state(struct device *dev,
164 struct device_attribute *attr,
167 return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
170 static ssize_t rtl_set_state(struct device *dev,
171 struct device_attribute *attr,
177 if (count < 1 || count > 2)
182 ret = ibm_rtl_write(0);
185 ret = ibm_rtl_write(1);
196 static struct bus_type rtl_subsys = {
198 .dev_name = "ibm_rtl",
201 static DEVICE_ATTR(version, S_IRUGO, rtl_show_version, NULL);
202 static DEVICE_ATTR(state, 0600, rtl_show_state, rtl_set_state);
204 static struct device_attribute *rtl_attributes[] = {
211 static int rtl_setup_sysfs(void) {
214 ret = subsys_system_register(&rtl_subsys, NULL);
216 for (i = 0; rtl_attributes[i]; i ++)
217 device_create_file(rtl_subsys.dev_root, rtl_attributes[i]);
222 static void rtl_teardown_sysfs(void) {
224 for (i = 0; rtl_attributes[i]; i ++)
225 device_remove_file(rtl_subsys.dev_root, rtl_attributes[i]);
226 bus_unregister(&rtl_subsys);
230 static struct dmi_system_id __initdata ibm_rtl_dmi_table[] = {
233 DMI_MATCH(DMI_SYS_VENDOR, "IBM"), \
239 static int __init ibm_rtl_init(void) {
240 unsigned long ebda_addr, ebda_size;
241 unsigned int ebda_kb;
242 int ret = -ENODEV, i;
245 pr_warn("module loaded by force\n");
246 /* first ensure that we are running on IBM HW */
247 else if (efi_enabled(EFI_BOOT) || !dmi_check_system(ibm_rtl_dmi_table))
250 /* Get the address for the Extended BIOS Data Area */
251 ebda_addr = get_bios_ebda();
253 RTL_DEBUG("no BIOS EBDA found\n");
257 ebda_map = ioremap(ebda_addr, 4);
261 /* First word in the EDBA is the Size in KB */
262 ebda_kb = ioread16(ebda_map);
263 RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
269 ebda_size = ebda_kb*1024;
271 /* Remap the whole table */
272 ebda_map = ioremap(ebda_addr, ebda_size);
276 /* search for the _RTL_ signature at the start of the table */
277 for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
278 struct ibm_rtl_table __iomem * tmp;
279 tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
280 if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
283 RTL_DEBUG("found RTL_SIGNATURE at %p\n", tmp);
285 /* The address, value, width and offset are platform
286 * dependent and found in the ibm_rtl_table */
287 rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
288 rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
289 RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
290 rtl_cmd_width, rtl_cmd_type);
291 addr = ioread32(&rtl_table->cmd_port_address);
292 RTL_DEBUG("addr = %#llx\n", (unsigned long long)addr);
293 plen = rtl_cmd_width/sizeof(char);
294 rtl_cmd_addr = rtl_port_map(addr, plen);
295 RTL_DEBUG("rtl_cmd_addr = %p\n", rtl_cmd_addr);
300 ret = rtl_setup_sysfs();
308 rtl_port_unmap(rtl_cmd_addr);
314 static void __exit ibm_rtl_exit(void)
317 RTL_DEBUG("cleaning up");
318 /* do not leave the machine in SMI-free mode */
320 /* unmap, unlink and remove all traces */
321 rtl_teardown_sysfs();
323 rtl_port_unmap(rtl_cmd_addr);
327 module_init(ibm_rtl_init);
328 module_exit(ibm_rtl_exit);