Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-2.6] / arch / powerpc / sysdev / pmi.c
1 /*
2  * pmi driver
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *
6  * PMI (Platform Management Interrupt) is a way to communicate
7  * with the BMC (Baseboard Management Controller) via interrupts.
8  * Unlike IPMI it is bidirectional and has a low latency.
9  *
10  * Author: Christian Krafft <krafft@de.ibm.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #include <linux/interrupt.h>
28 #include <linux/completion.h>
29 #include <linux/spinlock.h>
30 #include <linux/workqueue.h>
31
32 #include <asm/of_device.h>
33 #include <asm/of_platform.h>
34 #include <asm/io.h>
35 #include <asm/pmi.h>
36 #include <asm/prom.h>
37
38 struct pmi_data {
39         struct list_head        handler;
40         spinlock_t              handler_spinlock;
41         spinlock_t              pmi_spinlock;
42         struct mutex            msg_mutex;
43         pmi_message_t           msg;
44         struct completion       *completion;
45         struct of_device        *dev;
46         int                     irq;
47         u8 __iomem              *pmi_reg;
48         struct work_struct      work;
49 };
50
51
52 static int pmi_irq_handler(int irq, void *dev_id)
53 {
54         struct pmi_data *data;
55         u8 type;
56         int rc;
57
58         data = dev_id;
59
60         spin_lock(&data->pmi_spinlock);
61
62         type = ioread8(data->pmi_reg + PMI_READ_TYPE);
63         pr_debug("pmi: got message of type %d\n", type);
64
65         if (type & PMI_ACK && !data->completion) {
66                 printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
67                 rc = -EIO;
68                 goto unlock;
69         }
70
71         if (data->completion && !(type & PMI_ACK)) {
72                 printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
73                 rc = -EIO;
74                 goto unlock;
75         }
76
77         data->msg.type = type;
78         data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
79         data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
80         data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
81         rc = 0;
82 unlock:
83         spin_unlock(&data->pmi_spinlock);
84
85         if (rc == -EIO) {
86                 rc = IRQ_HANDLED;
87                 goto out;
88         }
89
90         if (data->msg.type & PMI_ACK) {
91                 complete(data->completion);
92                 rc = IRQ_HANDLED;
93                 goto out;
94         }
95
96         schedule_work(&data->work);
97
98         rc = IRQ_HANDLED;
99 out:
100         return rc;
101 }
102
103
104 static struct of_device_id pmi_match[] = {
105         { .type = "ibm,pmi", .name = "ibm,pmi" },
106         { .type = "ibm,pmi" },
107         {},
108 };
109
110 MODULE_DEVICE_TABLE(of, pmi_match);
111
112 static void pmi_notify_handlers(struct work_struct *work)
113 {
114         struct pmi_data *data;
115         struct pmi_handler *handler;
116
117         data = container_of(work, struct pmi_data, work);
118
119         spin_lock(&data->handler_spinlock);
120         list_for_each_entry(handler, &data->handler, node) {
121                 pr_debug(KERN_INFO "pmi: notifying handler %p\n", handler);
122                 if (handler->type == data->msg.type)
123                         handler->handle_pmi_message(data->dev, data->msg);
124         }
125         spin_unlock(&data->handler_spinlock);
126 }
127
128 static int pmi_of_probe(struct of_device *dev,
129                         const struct of_device_id *match)
130 {
131         struct device_node *np = dev->node;
132         struct pmi_data *data;
133         int rc;
134
135         data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
136         if (!data) {
137                 printk(KERN_ERR "pmi: could not allocate memory.\n");
138                 rc = -ENOMEM;
139                 goto out;
140         }
141
142         data->pmi_reg = of_iomap(np, 0);
143         if (!data->pmi_reg) {
144                 printk(KERN_ERR "pmi: invalid register address.\n");
145                 rc = -EFAULT;
146                 goto error_cleanup_data;
147         }
148
149         INIT_LIST_HEAD(&data->handler);
150
151         mutex_init(&data->msg_mutex);
152         spin_lock_init(&data->pmi_spinlock);
153         spin_lock_init(&data->handler_spinlock);
154
155         INIT_WORK(&data->work, pmi_notify_handlers);
156
157         dev->dev.driver_data = data;
158         data->dev = dev;
159
160         data->irq = irq_of_parse_and_map(np, 0);
161         if (data->irq == NO_IRQ) {
162                 printk(KERN_ERR "pmi: invalid interrupt.\n");
163                 rc = -EFAULT;
164                 goto error_cleanup_iomap;
165         }
166
167         rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", data);
168         if (rc) {
169                 printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
170                                 data->irq, rc);
171                 goto error_cleanup_iomap;
172         }
173
174         printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
175
176         goto out;
177
178 error_cleanup_iomap:
179         iounmap(data->pmi_reg);
180
181 error_cleanup_data:
182         kfree(data);
183
184 out:
185         return rc;
186 }
187
188 static int pmi_of_remove(struct of_device *dev)
189 {
190         struct pmi_data *data;
191         struct pmi_handler *handler, *tmp;
192
193         data = dev->dev.driver_data;
194
195         free_irq(data->irq, data);
196         iounmap(data->pmi_reg);
197
198         spin_lock(&data->handler_spinlock);
199
200         list_for_each_entry_safe(handler, tmp, &data->handler, node)
201                 list_del(&handler->node);
202
203         spin_unlock(&data->handler_spinlock);
204
205         kfree(dev->dev.driver_data);
206
207         return 0;
208 }
209
210 static struct of_platform_driver pmi_of_platform_driver = {
211         .name           = "pmi",
212         .match_table    = pmi_match,
213         .probe          = pmi_of_probe,
214         .remove         = pmi_of_remove
215 };
216
217 static int __init pmi_module_init(void)
218 {
219         return of_register_platform_driver(&pmi_of_platform_driver);
220 }
221 module_init(pmi_module_init);
222
223 static void __exit pmi_module_exit(void)
224 {
225         of_unregister_platform_driver(&pmi_of_platform_driver);
226 }
227 module_exit(pmi_module_exit);
228
229 void pmi_send_message(struct of_device *device, pmi_message_t msg)
230 {
231         struct pmi_data *data;
232         unsigned long flags;
233         DECLARE_COMPLETION_ONSTACK(completion);
234
235         data = device->dev.driver_data;
236
237         mutex_lock(&data->msg_mutex);
238
239         data->msg = msg;
240         pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
241
242         data->completion = &completion;
243
244         spin_lock_irqsave(&data->pmi_spinlock, flags);
245         iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
246         iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
247         iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
248         iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
249         spin_unlock_irqrestore(&data->pmi_spinlock, flags);
250
251         pr_debug("pmi_send_message: wait for completion\n");
252
253         wait_for_completion_interruptible_timeout(data->completion,
254                                                   PMI_TIMEOUT);
255
256         data->completion = NULL;
257
258         mutex_unlock(&data->msg_mutex);
259 }
260 EXPORT_SYMBOL_GPL(pmi_send_message);
261
262 void pmi_register_handler(struct of_device *device,
263                           struct pmi_handler *handler)
264 {
265         struct pmi_data *data;
266         data = device->dev.driver_data;
267
268         if (!data)
269                 return;
270
271         spin_lock(&data->handler_spinlock);
272         list_add_tail(&handler->node, &data->handler);
273         spin_unlock(&data->handler_spinlock);
274 }
275 EXPORT_SYMBOL_GPL(pmi_register_handler);
276
277 void pmi_unregister_handler(struct of_device *device,
278                             struct pmi_handler *handler)
279 {
280         struct pmi_data *data;
281         data = device->dev.driver_data;
282
283         if (!data)
284                 return;
285
286         pr_debug("pmi: unregistering handler %p\n", handler);
287
288         spin_lock(&data->handler_spinlock);
289         list_del(&handler->node);
290         spin_unlock(&data->handler_spinlock);
291 }
292 EXPORT_SYMBOL_GPL(pmi_unregister_handler);
293
294 MODULE_LICENSE("GPL");
295 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
296 MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");