blob: 42a7d603c870bb259a93a1630065de34d5c35a01 [file] [log] [blame]
Vernon Mauery35f0ce02010-10-05 15:47:18 -07001/*
2 * IBM Real-Time Linux driver
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Copyright (C) IBM Corporation, 2010
19 *
20 * Author: Keith Mannthey <kmannth@us.ibm.com>
21 * Vernon Mauery <vernux@us.ibm.com>
22 *
23 */
24
Joe Perches323623a2011-03-29 15:21:42 -070025#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
Vernon Mauery35f0ce02010-10-05 15:47:18 -070027#include <linux/kernel.h>
28#include <linux/delay.h>
29#include <linux/module.h>
30#include <linux/io.h>
Vernon Mauery35f0ce02010-10-05 15:47:18 -070031#include <linux/dmi.h>
Vernon Mauery1d37db72010-11-02 13:08:11 -070032#include <linux/efi.h>
Vernon Mauery35f0ce02010-10-05 15:47:18 -070033#include <linux/mutex.h>
34#include <asm/bios_ebda.h>
35
36static bool force;
37module_param(force, bool, 0);
38MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
39
40static bool debug;
41module_param(debug, bool, 0644);
42MODULE_PARM_DESC(debug, "Show debug output");
43
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Keith Mannthey <kmmanth@us.ibm.com>");
46MODULE_AUTHOR("Vernon Mauery <vernux@us.ibm.com>");
47
48#define RTL_ADDR_TYPE_IO 1
49#define RTL_ADDR_TYPE_MMIO 2
50
51#define RTL_CMD_ENTER_PRTM 1
52#define RTL_CMD_EXIT_PRTM 2
53
54/* The RTL table as presented by the EBDA: */
55struct ibm_rtl_table {
56 char signature[5]; /* signature should be "_RTL_" */
57 u8 version;
58 u8 rt_status;
59 u8 command;
60 u8 command_status;
61 u8 cmd_address_type;
62 u8 cmd_granularity;
63 u8 cmd_offset;
64 u16 reserve1;
65 u32 cmd_port_address; /* platform dependent address */
66 u32 cmd_port_value; /* platform dependent value */
67} __attribute__((packed));
68
69/* to locate "_RTL_" signature do a masked 5-byte integer compare */
70#define RTL_SIGNATURE 0x0000005f4c54525fULL
71#define RTL_MASK 0x000000ffffffffffULL
72
Joe Perches323623a2011-03-29 15:21:42 -070073#define RTL_DEBUG(fmt, ...) \
74do { \
75 if (debug) \
76 pr_info(fmt, ##__VA_ARGS__); \
Vernon Mauery35f0ce02010-10-05 15:47:18 -070077} while (0)
78
79static DEFINE_MUTEX(rtl_lock);
80static struct ibm_rtl_table __iomem *rtl_table;
81static void __iomem *ebda_map;
82static void __iomem *rtl_cmd_addr;
83static u8 rtl_cmd_type;
84static u8 rtl_cmd_width;
85
Roland Dreierdbee8a02011-05-24 17:13:09 -070086#ifndef readq
87static inline __u64 readq(const volatile void __iomem *addr)
88{
89 const volatile u32 __iomem *p = addr;
90 u32 low, high;
91
92 low = readl(p);
93 high = readl(p + 1);
94
95 return low + ((u64)high << 32);
96}
97#endif
98
Vernon Mauery35f0ce02010-10-05 15:47:18 -070099static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
100{
101 if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
102 return ioremap(addr, len);
103 return ioport_map(addr, len);
104}
105
106static void rtl_port_unmap(void __iomem *addr)
107{
108 if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
109 iounmap(addr);
110 else
111 ioport_unmap(addr);
112}
113
114static int ibm_rtl_write(u8 value)
115{
116 int ret = 0, count = 0;
117 static u32 cmd_port_val;
118
Joe Perches323623a2011-03-29 15:21:42 -0700119 RTL_DEBUG("%s(%d)\n", __func__, value);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700120
121 value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
122
123 mutex_lock(&rtl_lock);
124
125 if (ioread8(&rtl_table->rt_status) != value) {
126 iowrite8(value, &rtl_table->command);
127
128 switch (rtl_cmd_width) {
129 case 8:
130 cmd_port_val = ioread8(&rtl_table->cmd_port_value);
131 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
132 iowrite8((u8)cmd_port_val, rtl_cmd_addr);
133 break;
134 case 16:
135 cmd_port_val = ioread16(&rtl_table->cmd_port_value);
136 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
137 iowrite16((u16)cmd_port_val, rtl_cmd_addr);
138 break;
139 case 32:
140 cmd_port_val = ioread32(&rtl_table->cmd_port_value);
141 RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
142 iowrite32(cmd_port_val, rtl_cmd_addr);
143 break;
144 }
145
146 while (ioread8(&rtl_table->command)) {
147 msleep(10);
148 if (count++ > 500) {
Joe Perches323623a2011-03-29 15:21:42 -0700149 pr_err("Hardware not responding to "
150 "mode switch request\n");
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700151 ret = -EIO;
152 break;
153 }
154
155 }
156
157 if (ioread8(&rtl_table->command_status)) {
158 RTL_DEBUG("command_status reports failed command\n");
159 ret = -EIO;
160 }
161 }
162
163 mutex_unlock(&rtl_lock);
164 return ret;
165}
166
Kay Sievers15916a12011-12-14 15:26:15 -0800167static ssize_t rtl_show_version(struct device *dev,
168 struct device_attribute *attr,
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700169 char *buf)
170{
171 return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
172}
173
Kay Sievers15916a12011-12-14 15:26:15 -0800174static ssize_t rtl_show_state(struct device *dev,
175 struct device_attribute *attr,
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700176 char *buf)
177{
178 return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
179}
180
Kay Sievers15916a12011-12-14 15:26:15 -0800181static ssize_t rtl_set_state(struct device *dev,
182 struct device_attribute *attr,
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700183 const char *buf,
184 size_t count)
185{
186 ssize_t ret;
187
188 if (count < 1 || count > 2)
189 return -EINVAL;
190
191 switch (buf[0]) {
192 case '0':
193 ret = ibm_rtl_write(0);
194 break;
195 case '1':
196 ret = ibm_rtl_write(1);
197 break;
198 default:
199 ret = -EINVAL;
200 }
201 if (ret >= 0)
202 ret = count;
203
204 return ret;
205}
206
Kay Sievers15916a12011-12-14 15:26:15 -0800207static struct bus_type rtl_subsys = {
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700208 .name = "ibm_rtl",
Kay Sievers15916a12011-12-14 15:26:15 -0800209 .dev_name = "ibm_rtl",
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700210};
211
Kay Sievers15916a12011-12-14 15:26:15 -0800212static DEVICE_ATTR(version, S_IRUGO, rtl_show_version, NULL);
213static DEVICE_ATTR(state, 0600, rtl_show_state, rtl_set_state);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700214
Kay Sievers15916a12011-12-14 15:26:15 -0800215static struct device_attribute *rtl_attributes[] = {
216 &dev_attr_version,
217 &dev_attr_state,
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700218 NULL
219};
220
221
222static int rtl_setup_sysfs(void) {
223 int ret, i;
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700224
Kay Sievers15916a12011-12-14 15:26:15 -0800225 ret = subsys_system_register(&rtl_subsys, NULL);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700226 if (!ret) {
227 for (i = 0; rtl_attributes[i]; i ++)
Kay Sievers15916a12011-12-14 15:26:15 -0800228 device_create_file(rtl_subsys.dev_root, rtl_attributes[i]);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700229 }
230 return ret;
231}
232
233static void rtl_teardown_sysfs(void) {
234 int i;
235 for (i = 0; rtl_attributes[i]; i ++)
Kay Sievers15916a12011-12-14 15:26:15 -0800236 device_remove_file(rtl_subsys.dev_root, rtl_attributes[i]);
237 bus_unregister(&rtl_subsys);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700238}
239
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700240
241static struct dmi_system_id __initdata ibm_rtl_dmi_table[] = {
Vernon Mauerya2262262010-11-02 13:08:10 -0700242 { \
243 .matches = { \
244 DMI_MATCH(DMI_SYS_VENDOR, "IBM"), \
245 }, \
246 },
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700247 { }
248};
249
250static int __init ibm_rtl_init(void) {
251 unsigned long ebda_addr, ebda_size;
252 unsigned int ebda_kb;
253 int ret = -ENODEV, i;
254
255 if (force)
Joe Perches323623a2011-03-29 15:21:42 -0700256 pr_warn("module loaded by force\n");
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700257 /* first ensure that we are running on IBM HW */
Vernon Mauery1d37db72010-11-02 13:08:11 -0700258 else if (efi_enabled || !dmi_check_system(ibm_rtl_dmi_table))
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700259 return -ENODEV;
260
261 /* Get the address for the Extended BIOS Data Area */
262 ebda_addr = get_bios_ebda();
263 if (!ebda_addr) {
264 RTL_DEBUG("no BIOS EBDA found\n");
265 return -ENODEV;
266 }
267
268 ebda_map = ioremap(ebda_addr, 4);
269 if (!ebda_map)
270 return -ENOMEM;
271
272 /* First word in the EDBA is the Size in KB */
273 ebda_kb = ioread16(ebda_map);
274 RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
275
276 if (ebda_kb == 0)
277 goto out;
278
279 iounmap(ebda_map);
280 ebda_size = ebda_kb*1024;
281
282 /* Remap the whole table */
283 ebda_map = ioremap(ebda_addr, ebda_size);
284 if (!ebda_map)
285 return -ENOMEM;
286
287 /* search for the _RTL_ signature at the start of the table */
288 for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
289 struct ibm_rtl_table __iomem * tmp;
290 tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
291 if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
292 phys_addr_t addr;
293 unsigned int plen;
Joe Perches3a351252011-03-29 15:21:33 -0700294 RTL_DEBUG("found RTL_SIGNATURE at %p\n", tmp);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700295 rtl_table = tmp;
296 /* The address, value, width and offset are platform
297 * dependent and found in the ibm_rtl_table */
298 rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
299 rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
300 RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
Joe Perches323623a2011-03-29 15:21:42 -0700301 rtl_cmd_width, rtl_cmd_type);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700302 addr = ioread32(&rtl_table->cmd_port_address);
Randy Dunlapc72b8442010-10-22 16:18:47 -0700303 RTL_DEBUG("addr = %#llx\n", (unsigned long long)addr);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700304 plen = rtl_cmd_width/sizeof(char);
305 rtl_cmd_addr = rtl_port_map(addr, plen);
Joe Perches3a351252011-03-29 15:21:33 -0700306 RTL_DEBUG("rtl_cmd_addr = %p\n", rtl_cmd_addr);
Vernon Mauery35f0ce02010-10-05 15:47:18 -0700307 if (!rtl_cmd_addr) {
308 ret = -ENOMEM;
309 break;
310 }
311 ret = rtl_setup_sysfs();
312 break;
313 }
314 }
315
316out:
317 if (ret) {
318 iounmap(ebda_map);
319 rtl_port_unmap(rtl_cmd_addr);
320 }
321
322 return ret;
323}
324
325static void __exit ibm_rtl_exit(void)
326{
327 if (rtl_table) {
328 RTL_DEBUG("cleaning up");
329 /* do not leave the machine in SMI-free mode */
330 ibm_rtl_write(0);
331 /* unmap, unlink and remove all traces */
332 rtl_teardown_sysfs();
333 iounmap(ebda_map);
334 rtl_port_unmap(rtl_cmd_addr);
335 }
336}
337
338module_init(ibm_rtl_init);
339module_exit(ibm_rtl_exit);