]> Pileus Git - ~andy/linux/blob - drivers/platform/x86/ibm_rtl.c
asus: Add pr_fmt and convert printks to pr_<level>
[~andy/linux] / drivers / platform / x86 / ibm_rtl.c
1 /*
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
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/module.h>
28 #include <linux/io.h>
29 #include <linux/sysdev.h>
30 #include <linux/dmi.h>
31 #include <linux/efi.h>
32 #include <linux/mutex.h>
33 #include <asm/bios_ebda.h>
34
35 static bool force;
36 module_param(force, bool, 0);
37 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
38
39 static bool debug;
40 module_param(debug, bool, 0644);
41 MODULE_PARM_DESC(debug, "Show debug output");
42
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Keith Mannthey <kmmanth@us.ibm.com>");
45 MODULE_AUTHOR("Vernon Mauery <vernux@us.ibm.com>");
46
47 #define RTL_ADDR_TYPE_IO    1
48 #define RTL_ADDR_TYPE_MMIO  2
49
50 #define RTL_CMD_ENTER_PRTM  1
51 #define RTL_CMD_EXIT_PRTM   2
52
53 /* The RTL table as presented by the EBDA: */
54 struct ibm_rtl_table {
55         char signature[5]; /* signature should be "_RTL_" */
56         u8 version;
57         u8 rt_status;
58         u8 command;
59         u8 command_status;
60         u8 cmd_address_type;
61         u8 cmd_granularity;
62         u8 cmd_offset;
63         u16 reserve1;
64         u32 cmd_port_address; /* platform dependent address */
65         u32 cmd_port_value;   /* platform dependent value */
66 } __attribute__((packed));
67
68 /* to locate "_RTL_" signature do a masked 5-byte integer compare */
69 #define RTL_SIGNATURE 0x0000005f4c54525fULL
70 #define RTL_MASK      0x000000ffffffffffULL
71
72 #define RTL_DEBUG(A, ...) do { \
73         if (debug) \
74                 pr_info("ibm-rtl: " A, ##__VA_ARGS__ ); \
75 } while (0)
76
77 static DEFINE_MUTEX(rtl_lock);
78 static struct ibm_rtl_table __iomem *rtl_table;
79 static void __iomem *ebda_map;
80 static void __iomem *rtl_cmd_addr;
81 static u8 rtl_cmd_type;
82 static u8 rtl_cmd_width;
83
84 #ifndef readq
85 static inline __u64 readq(const volatile void __iomem *addr)
86 {
87         const volatile u32 __iomem *p = addr;
88         u32 low, high;
89
90         low = readl(p);
91         high = readl(p + 1);
92
93         return low + ((u64)high << 32);
94 }
95 #endif
96
97 static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
98 {
99         if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
100                 return ioremap(addr, len);
101         return ioport_map(addr, len);
102 }
103
104 static void rtl_port_unmap(void __iomem *addr)
105 {
106         if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
107                 iounmap(addr);
108         else
109                 ioport_unmap(addr);
110 }
111
112 static int ibm_rtl_write(u8 value)
113 {
114         int ret = 0, count = 0;
115         static u32 cmd_port_val;
116
117         RTL_DEBUG("%s(%d)\n", __FUNCTION__, value);
118
119         value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
120
121         mutex_lock(&rtl_lock);
122
123         if (ioread8(&rtl_table->rt_status) != value) {
124                 iowrite8(value, &rtl_table->command);
125
126                 switch (rtl_cmd_width) {
127                 case 8:
128                         cmd_port_val = ioread8(&rtl_table->cmd_port_value);
129                         RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
130                         iowrite8((u8)cmd_port_val, rtl_cmd_addr);
131                         break;
132                 case 16:
133                         cmd_port_val = ioread16(&rtl_table->cmd_port_value);
134                         RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
135                         iowrite16((u16)cmd_port_val, rtl_cmd_addr);
136                         break;
137                 case 32:
138                         cmd_port_val = ioread32(&rtl_table->cmd_port_value);
139                         RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
140                         iowrite32(cmd_port_val, rtl_cmd_addr);
141                         break;
142                 }
143
144                 while (ioread8(&rtl_table->command)) {
145                         msleep(10);
146                         if (count++ > 500) {
147                                 pr_err("ibm-rtl: Hardware not responding to "
148                                         "mode switch request\n");
149                                 ret = -EIO;
150                                 break;
151                         }
152
153                 }
154
155                 if (ioread8(&rtl_table->command_status)) {
156                         RTL_DEBUG("command_status reports failed command\n");
157                         ret = -EIO;
158                 }
159         }
160
161         mutex_unlock(&rtl_lock);
162         return ret;
163 }
164
165 static ssize_t rtl_show_version(struct sysdev_class * dev,
166                                 struct sysdev_class_attribute *attr,
167                                 char *buf)
168 {
169         return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
170 }
171
172 static ssize_t rtl_show_state(struct sysdev_class *dev,
173                               struct sysdev_class_attribute *attr,
174                               char *buf)
175 {
176         return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
177 }
178
179 static ssize_t rtl_set_state(struct sysdev_class *dev,
180                              struct sysdev_class_attribute *attr,
181                              const char *buf,
182                              size_t count)
183 {
184         ssize_t ret;
185
186         if (count < 1 || count > 2)
187                 return -EINVAL;
188
189         switch (buf[0]) {
190         case '0':
191                 ret = ibm_rtl_write(0);
192                 break;
193         case '1':
194                 ret = ibm_rtl_write(1);
195                 break;
196         default:
197                 ret = -EINVAL;
198         }
199         if (ret >= 0)
200                 ret = count;
201
202         return ret;
203 }
204
205 static struct sysdev_class class_rtl = {
206         .name = "ibm_rtl",
207 };
208
209 static SYSDEV_CLASS_ATTR(version, S_IRUGO, rtl_show_version, NULL);
210 static SYSDEV_CLASS_ATTR(state, 0600, rtl_show_state, rtl_set_state);
211
212 static struct sysdev_class_attribute *rtl_attributes[] = {
213         &attr_version,
214         &attr_state,
215         NULL
216 };
217
218
219 static int rtl_setup_sysfs(void) {
220         int ret, i;
221         ret = sysdev_class_register(&class_rtl);
222
223         if (!ret) {
224                 for (i = 0; rtl_attributes[i]; i ++)
225                         sysdev_class_create_file(&class_rtl, rtl_attributes[i]);
226         }
227         return ret;
228 }
229
230 static void rtl_teardown_sysfs(void) {
231         int i;
232         for (i = 0; rtl_attributes[i]; i ++)
233                 sysdev_class_remove_file(&class_rtl, rtl_attributes[i]);
234         sysdev_class_unregister(&class_rtl);
235 }
236
237
238 static struct dmi_system_id __initdata ibm_rtl_dmi_table[] = {
239         {                                                  \
240                 .matches = {                               \
241                         DMI_MATCH(DMI_SYS_VENDOR, "IBM"),  \
242                 },                                         \
243         },
244         { }
245 };
246
247 static int __init ibm_rtl_init(void) {
248         unsigned long ebda_addr, ebda_size;
249         unsigned int ebda_kb;
250         int ret = -ENODEV, i;
251
252         if (force)
253                 pr_warning("ibm-rtl: module loaded by force\n");
254         /* first ensure that we are running on IBM HW */
255         else if (efi_enabled || !dmi_check_system(ibm_rtl_dmi_table))
256                 return -ENODEV;
257
258         /* Get the address for the Extended BIOS Data Area */
259         ebda_addr = get_bios_ebda();
260         if (!ebda_addr) {
261                 RTL_DEBUG("no BIOS EBDA found\n");
262                 return -ENODEV;
263         }
264
265         ebda_map = ioremap(ebda_addr, 4);
266         if (!ebda_map)
267                 return -ENOMEM;
268
269         /* First word in the EDBA is the Size in KB */
270         ebda_kb = ioread16(ebda_map);
271         RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
272
273         if (ebda_kb == 0)
274                 goto out;
275
276         iounmap(ebda_map);
277         ebda_size = ebda_kb*1024;
278
279         /* Remap the whole table */
280         ebda_map = ioremap(ebda_addr, ebda_size);
281         if (!ebda_map)
282                 return -ENOMEM;
283
284         /* search for the _RTL_ signature at the start of the table */
285         for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
286                 struct ibm_rtl_table __iomem * tmp;
287                 tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
288                 if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
289                         phys_addr_t addr;
290                         unsigned int plen;
291                         RTL_DEBUG("found RTL_SIGNATURE at %p\n", tmp);
292                         rtl_table = tmp;
293                         /* The address, value, width and offset are platform
294                          * dependent and found in the ibm_rtl_table */
295                         rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
296                         rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
297                         RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
298                               rtl_cmd_width, rtl_cmd_type);
299                         addr = ioread32(&rtl_table->cmd_port_address);
300                         RTL_DEBUG("addr = %#llx\n", (unsigned long long)addr);
301                         plen = rtl_cmd_width/sizeof(char);
302                         rtl_cmd_addr = rtl_port_map(addr, plen);
303                         RTL_DEBUG("rtl_cmd_addr = %p\n", rtl_cmd_addr);
304                         if (!rtl_cmd_addr) {
305                                 ret = -ENOMEM;
306                                 break;
307                         }
308                         ret = rtl_setup_sysfs();
309                         break;
310                 }
311         }
312
313 out:
314         if (ret) {
315                 iounmap(ebda_map);
316                 rtl_port_unmap(rtl_cmd_addr);
317         }
318
319         return ret;
320 }
321
322 static void __exit ibm_rtl_exit(void)
323 {
324         if (rtl_table) {
325                 RTL_DEBUG("cleaning up");
326                 /* do not leave the machine in SMI-free mode */
327                 ibm_rtl_write(0);
328                 /* unmap, unlink and remove all traces */
329                 rtl_teardown_sysfs();
330                 iounmap(ebda_map);
331                 rtl_port_unmap(rtl_cmd_addr);
332         }
333 }
334
335 module_init(ibm_rtl_init);
336 module_exit(ibm_rtl_exit);