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
20 * Author: Keith Mannthey <kmannth@us.ibm.com>
21 * Vernon Mauery <vernux@us.ibm.com>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/module.h>
29 #include <linux/sysdev.h>
30 #include <linux/dmi.h>
31 #include <linux/mutex.h>
32 #include <asm/bios_ebda.h>
35 module_param(force, bool, 0);
36 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
39 module_param(debug, bool, 0644);
40 MODULE_PARM_DESC(debug, "Show debug output");
42 MODULE_LICENSE("GPL");
43 MODULE_AUTHOR("Keith Mannthey <kmmanth@us.ibm.com>");
44 MODULE_AUTHOR("Vernon Mauery <vernux@us.ibm.com>");
46 #define RTL_ADDR_TYPE_IO 1
47 #define RTL_ADDR_TYPE_MMIO 2
49 #define RTL_CMD_ENTER_PRTM 1
50 #define RTL_CMD_EXIT_PRTM 2
52 /* The RTL table as presented by the EBDA: */
53 struct ibm_rtl_table {
54 char signature[5]; /* signature should be "_RTL_" */
63 u32 cmd_port_address; /* platform dependent address */
64 u32 cmd_port_value; /* platform dependent value */
65 } __attribute__((packed));
67 /* to locate "_RTL_" signature do a masked 5-byte integer compare */
68 #define RTL_SIGNATURE 0x0000005f4c54525fULL
69 #define RTL_MASK 0x000000ffffffffffULL
71 #define RTL_DEBUG(A, ...) do { \
73 pr_info("ibm-rtl: " A, ##__VA_ARGS__ ); \
76 static DEFINE_MUTEX(rtl_lock);
77 static struct ibm_rtl_table __iomem *rtl_table;
78 static void __iomem *ebda_map;
79 static void __iomem *rtl_cmd_addr;
80 static u8 rtl_cmd_type;
81 static u8 rtl_cmd_width;
83 static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
85 if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
86 return ioremap(addr, len);
87 return ioport_map(addr, len);
90 static void rtl_port_unmap(void __iomem *addr)
92 if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
98 static int ibm_rtl_write(u8 value)
100 int ret = 0, count = 0;
101 static u32 cmd_port_val;
103 RTL_DEBUG("%s(%d)\n", __FUNCTION__, value);
105 value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
107 mutex_lock(&rtl_lock);
109 if (ioread8(&rtl_table->rt_status) != value) {
110 iowrite8(value, &rtl_table->command);
112 switch (rtl_cmd_width) {
114 cmd_port_val = ioread8(&rtl_table->cmd_port_value);
115 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
116 iowrite8((u8)cmd_port_val, rtl_cmd_addr);
119 cmd_port_val = ioread16(&rtl_table->cmd_port_value);
120 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
121 iowrite16((u16)cmd_port_val, rtl_cmd_addr);
124 cmd_port_val = ioread32(&rtl_table->cmd_port_value);
125 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
126 iowrite32(cmd_port_val, rtl_cmd_addr);
130 while (ioread8(&rtl_table->command)) {
133 pr_err("ibm-rtl: Hardware not responding to "
134 "mode switch request\n");
141 if (ioread8(&rtl_table->command_status)) {
142 RTL_DEBUG("command_status reports failed command\n");
147 mutex_unlock(&rtl_lock);
151 static ssize_t rtl_show_version(struct sysdev_class * dev,
152 struct sysdev_class_attribute *attr,
155 return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
158 static ssize_t rtl_show_state(struct sysdev_class *dev,
159 struct sysdev_class_attribute *attr,
162 return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
165 static ssize_t rtl_set_state(struct sysdev_class *dev,
166 struct sysdev_class_attribute *attr,
172 if (count < 1 || count > 2)
177 ret = ibm_rtl_write(0);
180 ret = ibm_rtl_write(1);
191 static struct sysdev_class class_rtl = {
195 static SYSDEV_CLASS_ATTR(version, S_IRUGO, rtl_show_version, NULL);
196 static SYSDEV_CLASS_ATTR(state, 0600, rtl_show_state, rtl_set_state);
198 static struct sysdev_class_attribute *rtl_attributes[] = {
205 static int rtl_setup_sysfs(void) {
207 ret = sysdev_class_register(&class_rtl);
210 for (i = 0; rtl_attributes[i]; i ++)
211 sysdev_class_create_file(&class_rtl, rtl_attributes[i]);
216 static void rtl_teardown_sysfs(void) {
218 for (i = 0; rtl_attributes[i]; i ++)
219 sysdev_class_remove_file(&class_rtl, rtl_attributes[i]);
220 sysdev_class_unregister(&class_rtl);
223 static int dmi_check_cb(const struct dmi_system_id *id)
225 RTL_DEBUG("found IBM server '%s'\n", id->ident);
229 #define ibm_dmi_entry(NAME, TYPE) \
233 DMI_MATCH(DMI_SYS_VENDOR, "IBM"), \
234 DMI_MATCH(DMI_PRODUCT_NAME, TYPE), \
236 .callback = dmi_check_cb \
239 static struct dmi_system_id __initdata ibm_rtl_dmi_table[] = {
240 ibm_dmi_entry("BladeCenter LS21", "7971"),
241 ibm_dmi_entry("BladeCenter LS22", "7901"),
242 ibm_dmi_entry("BladeCenter HS21 XM", "7995"),
243 ibm_dmi_entry("BladeCenter HS22", "7870"),
244 ibm_dmi_entry("BladeCenter HS22V", "7871"),
245 ibm_dmi_entry("System x3550 M2", "7946"),
246 ibm_dmi_entry("System x3650 M2", "7947"),
247 ibm_dmi_entry("System x3550 M3", "7944"),
248 ibm_dmi_entry("System x3650 M3", "7945"),
252 static int __init ibm_rtl_init(void) {
253 unsigned long ebda_addr, ebda_size;
254 unsigned int ebda_kb;
255 int ret = -ENODEV, i;
258 pr_warning("ibm-rtl: module loaded by force\n");
259 /* first ensure that we are running on IBM HW */
260 else if (!dmi_check_system(ibm_rtl_dmi_table))
263 /* Get the address for the Extended BIOS Data Area */
264 ebda_addr = get_bios_ebda();
266 RTL_DEBUG("no BIOS EBDA found\n");
270 ebda_map = ioremap(ebda_addr, 4);
274 /* First word in the EDBA is the Size in KB */
275 ebda_kb = ioread16(ebda_map);
276 RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
282 ebda_size = ebda_kb*1024;
284 /* Remap the whole table */
285 ebda_map = ioremap(ebda_addr, ebda_size);
289 /* search for the _RTL_ signature at the start of the table */
290 for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
291 struct ibm_rtl_table __iomem * tmp;
292 tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
293 if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
296 RTL_DEBUG("found RTL_SIGNATURE at %#llx\n", (u64)tmp);
298 /* The address, value, width and offset are platform
299 * dependent and found in the ibm_rtl_table */
300 rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
301 rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
302 RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
303 rtl_cmd_width, rtl_cmd_type);
304 addr = ioread32(&rtl_table->cmd_port_address);
305 RTL_DEBUG("addr = %#llx\n", addr);
306 plen = rtl_cmd_width/sizeof(char);
307 rtl_cmd_addr = rtl_port_map(addr, plen);
308 RTL_DEBUG("rtl_cmd_addr = %#llx\n", (u64)rtl_cmd_addr);
313 ret = rtl_setup_sysfs();
321 rtl_port_unmap(rtl_cmd_addr);
327 static void __exit ibm_rtl_exit(void)
330 RTL_DEBUG("cleaning up");
331 /* do not leave the machine in SMI-free mode */
333 /* unmap, unlink and remove all traces */
334 rtl_teardown_sysfs();
336 rtl_port_unmap(rtl_cmd_addr);
340 module_init(ibm_rtl_init);
341 module_exit(ibm_rtl_exit);