Linux 2.6.31.6
[linux/fpc-iii.git] / arch / powerpc / sysdev / pmi.c
blobaaa915998eb630cdb0492427a67ac40816e67a68
1 /*
2 * pmi driver
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
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.
10 * Author: Christian Krafft <krafft@de.ibm.com>
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.
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.
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.
27 #include <linux/interrupt.h>
28 #include <linux/completion.h>
29 #include <linux/spinlock.h>
30 #include <linux/workqueue.h>
31 #include <linux/of_device.h>
32 #include <linux/of_platform.h>
34 #include <asm/io.h>
35 #include <asm/pmi.h>
36 #include <asm/prom.h>
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;
51 static struct pmi_data *data;
53 static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
55 u8 type;
56 int rc;
58 spin_lock(&data->pmi_spinlock);
60 type = ioread8(data->pmi_reg + PMI_READ_TYPE);
61 pr_debug("pmi: got message of type %d\n", type);
63 if (type & PMI_ACK && !data->completion) {
64 printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
65 rc = -EIO;
66 goto unlock;
69 if (data->completion && !(type & PMI_ACK)) {
70 printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
71 rc = -EIO;
72 goto unlock;
75 data->msg.type = type;
76 data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
77 data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
78 data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
79 rc = 0;
80 unlock:
81 spin_unlock(&data->pmi_spinlock);
83 if (rc == -EIO) {
84 rc = IRQ_HANDLED;
85 goto out;
88 if (data->msg.type & PMI_ACK) {
89 complete(data->completion);
90 rc = IRQ_HANDLED;
91 goto out;
94 schedule_work(&data->work);
96 rc = IRQ_HANDLED;
97 out:
98 return rc;
102 static struct of_device_id pmi_match[] = {
103 { .type = "ibm,pmi", .name = "ibm,pmi" },
104 { .type = "ibm,pmi" },
108 MODULE_DEVICE_TABLE(of, pmi_match);
110 static void pmi_notify_handlers(struct work_struct *work)
112 struct pmi_handler *handler;
114 spin_lock(&data->handler_spinlock);
115 list_for_each_entry(handler, &data->handler, node) {
116 pr_debug(KERN_INFO "pmi: notifying handler %p\n", handler);
117 if (handler->type == data->msg.type)
118 handler->handle_pmi_message(data->msg);
120 spin_unlock(&data->handler_spinlock);
123 static int pmi_of_probe(struct of_device *dev,
124 const struct of_device_id *match)
126 struct device_node *np = dev->node;
127 int rc;
129 if (data) {
130 printk(KERN_ERR "pmi: driver has already been initialized.\n");
131 rc = -EBUSY;
132 goto out;
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;
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;
149 INIT_LIST_HEAD(&data->handler);
151 mutex_init(&data->msg_mutex);
152 spin_lock_init(&data->pmi_spinlock);
153 spin_lock_init(&data->handler_spinlock);
155 INIT_WORK(&data->work, pmi_notify_handlers);
157 data->dev = dev;
159 data->irq = irq_of_parse_and_map(np, 0);
160 if (data->irq == NO_IRQ) {
161 printk(KERN_ERR "pmi: invalid interrupt.\n");
162 rc = -EFAULT;
163 goto error_cleanup_iomap;
166 rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
167 if (rc) {
168 printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
169 data->irq, rc);
170 goto error_cleanup_iomap;
173 printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
175 goto out;
177 error_cleanup_iomap:
178 iounmap(data->pmi_reg);
180 error_cleanup_data:
181 kfree(data);
183 out:
184 return rc;
187 static int pmi_of_remove(struct of_device *dev)
189 struct pmi_handler *handler, *tmp;
191 free_irq(data->irq, NULL);
192 iounmap(data->pmi_reg);
194 spin_lock(&data->handler_spinlock);
196 list_for_each_entry_safe(handler, tmp, &data->handler, node)
197 list_del(&handler->node);
199 spin_unlock(&data->handler_spinlock);
201 kfree(data);
202 data = NULL;
204 return 0;
207 static struct of_platform_driver pmi_of_platform_driver = {
208 .match_table = pmi_match,
209 .probe = pmi_of_probe,
210 .remove = pmi_of_remove,
211 .driver = {
212 .name = "pmi",
216 static int __init pmi_module_init(void)
218 return of_register_platform_driver(&pmi_of_platform_driver);
220 module_init(pmi_module_init);
222 static void __exit pmi_module_exit(void)
224 of_unregister_platform_driver(&pmi_of_platform_driver);
226 module_exit(pmi_module_exit);
228 int pmi_send_message(pmi_message_t msg)
230 unsigned long flags;
231 DECLARE_COMPLETION_ONSTACK(completion);
233 if (!data)
234 return -ENODEV;
236 mutex_lock(&data->msg_mutex);
238 data->msg = msg;
239 pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
241 data->completion = &completion;
243 spin_lock_irqsave(&data->pmi_spinlock, flags);
244 iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
245 iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
246 iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
247 iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
248 spin_unlock_irqrestore(&data->pmi_spinlock, flags);
250 pr_debug("pmi_send_message: wait for completion\n");
252 wait_for_completion_interruptible_timeout(data->completion,
253 PMI_TIMEOUT);
255 data->completion = NULL;
257 mutex_unlock(&data->msg_mutex);
259 return 0;
261 EXPORT_SYMBOL_GPL(pmi_send_message);
263 int pmi_register_handler(struct pmi_handler *handler)
265 if (!data)
266 return -ENODEV;
268 spin_lock(&data->handler_spinlock);
269 list_add_tail(&handler->node, &data->handler);
270 spin_unlock(&data->handler_spinlock);
272 return 0;
274 EXPORT_SYMBOL_GPL(pmi_register_handler);
276 void pmi_unregister_handler(struct pmi_handler *handler)
278 if (!data)
279 return;
281 pr_debug("pmi: unregistering handler %p\n", handler);
283 spin_lock(&data->handler_spinlock);
284 list_del(&handler->node);
285 spin_unlock(&data->handler_spinlock);
287 EXPORT_SYMBOL_GPL(pmi_unregister_handler);
289 MODULE_LICENSE("GPL");
290 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
291 MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");