blob: 2f91b55b775475a8c839299801b390b670c4e183 [file] [log] [blame]
Christian Krafft0e826642007-02-14 14:09:45 +01001/*
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>
Christian Krafft6bf05fd2007-04-23 21:35:45 +020036#include <asm/prom.h>
Christian Krafft0e826642007-02-14 14:09:45 +010037
38struct 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
Christian Krafft813f9072007-07-20 21:39:18 +020051static struct pmi_data *data;
Christian Krafft0e826642007-02-14 14:09:45 +010052
Christian Krafft0e826642007-02-14 14:09:45 +010053static int pmi_irq_handler(int irq, void *dev_id)
54{
Christian Krafft0e826642007-02-14 14:09:45 +010055 u8 type;
56 int rc;
57
Christian Krafft0e826642007-02-14 14:09:45 +010058 spin_lock(&data->pmi_spinlock);
59
60 type = ioread8(data->pmi_reg + PMI_READ_TYPE);
61 pr_debug("pmi: got message of type %d\n", type);
62
63 if (type & PMI_ACK && !data->completion) {
64 printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
65 rc = -EIO;
66 goto unlock;
67 }
68
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;
73 }
74
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;
80unlock:
81 spin_unlock(&data->pmi_spinlock);
82
83 if (rc == -EIO) {
84 rc = IRQ_HANDLED;
85 goto out;
86 }
87
88 if (data->msg.type & PMI_ACK) {
89 complete(data->completion);
90 rc = IRQ_HANDLED;
91 goto out;
92 }
93
94 schedule_work(&data->work);
95
96 rc = IRQ_HANDLED;
97out:
98 return rc;
99}
100
101
102static struct of_device_id pmi_match[] = {
103 { .type = "ibm,pmi", .name = "ibm,pmi" },
Christian Krafft4a065f92007-04-23 21:35:44 +0200104 { .type = "ibm,pmi" },
Christian Krafft0e826642007-02-14 14:09:45 +0100105 {},
106};
107
108MODULE_DEVICE_TABLE(of, pmi_match);
109
110static void pmi_notify_handlers(struct work_struct *work)
111{
Christian Krafft0e826642007-02-14 14:09:45 +0100112 struct pmi_handler *handler;
113
Christian Krafft0e826642007-02-14 14:09:45 +0100114 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)
Christian Krafft813f9072007-07-20 21:39:18 +0200118 handler->handle_pmi_message(data->msg);
Christian Krafft0e826642007-02-14 14:09:45 +0100119 }
120 spin_unlock(&data->handler_spinlock);
121}
122
123static int pmi_of_probe(struct of_device *dev,
124 const struct of_device_id *match)
125{
126 struct device_node *np = dev->node;
Christian Krafft0e826642007-02-14 14:09:45 +0100127 int rc;
128
Christian Krafft813f9072007-07-20 21:39:18 +0200129 if (data) {
130 printk(KERN_ERR "pmi: driver has already been initialized.\n");
131 rc = -EBUSY;
132 goto out;
133 }
134
Christian Krafft0e826642007-02-14 14:09:45 +0100135 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
Christian Krafft6bf05fd2007-04-23 21:35:45 +0200142 data->pmi_reg = of_iomap(np, 0);
Christian Krafft0e826642007-02-14 14:09:45 +0100143 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
Christian Krafft0e826642007-02-14 14:09:45 +0100157 data->dev = dev;
158
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;
164 }
165
Christian Krafft813f9072007-07-20 21:39:18 +0200166 rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
Christian Krafft0e826642007-02-14 14:09:45 +0100167 if (rc) {
168 printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
169 data->irq, rc);
170 goto error_cleanup_iomap;
171 }
172
173 printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
174
175 goto out;
176
177error_cleanup_iomap:
178 iounmap(data->pmi_reg);
179
180error_cleanup_data:
181 kfree(data);
182
183out:
184 return rc;
185}
186
187static int pmi_of_remove(struct of_device *dev)
188{
Christian Krafft0e826642007-02-14 14:09:45 +0100189 struct pmi_handler *handler, *tmp;
190
Christian Krafft813f9072007-07-20 21:39:18 +0200191 free_irq(data->irq, NULL);
Christian Krafft0e826642007-02-14 14:09:45 +0100192 iounmap(data->pmi_reg);
193
194 spin_lock(&data->handler_spinlock);
195
196 list_for_each_entry_safe(handler, tmp, &data->handler, node)
197 list_del(&handler->node);
198
199 spin_unlock(&data->handler_spinlock);
200
Christian Krafft813f9072007-07-20 21:39:18 +0200201 kfree(data);
202 data = NULL;
Christian Krafft0e826642007-02-14 14:09:45 +0100203
204 return 0;
205}
206
207static struct of_platform_driver pmi_of_platform_driver = {
208 .name = "pmi",
209 .match_table = pmi_match,
210 .probe = pmi_of_probe,
211 .remove = pmi_of_remove
212};
213
214static int __init pmi_module_init(void)
215{
216 return of_register_platform_driver(&pmi_of_platform_driver);
217}
218module_init(pmi_module_init);
219
220static void __exit pmi_module_exit(void)
221{
222 of_unregister_platform_driver(&pmi_of_platform_driver);
223}
224module_exit(pmi_module_exit);
225
Christian Krafft813f9072007-07-20 21:39:18 +0200226int pmi_send_message(pmi_message_t msg)
Christian Krafft0e826642007-02-14 14:09:45 +0100227{
Christian Krafft0e826642007-02-14 14:09:45 +0100228 unsigned long flags;
229 DECLARE_COMPLETION_ONSTACK(completion);
230
Christian Krafft813f9072007-07-20 21:39:18 +0200231 if (!data)
232 return -ENODEV;
Christian Krafft0e826642007-02-14 14:09:45 +0100233
234 mutex_lock(&data->msg_mutex);
235
236 data->msg = msg;
237 pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
238
239 data->completion = &completion;
240
241 spin_lock_irqsave(&data->pmi_spinlock, flags);
242 iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
243 iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
244 iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
245 iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
246 spin_unlock_irqrestore(&data->pmi_spinlock, flags);
247
248 pr_debug("pmi_send_message: wait for completion\n");
249
250 wait_for_completion_interruptible_timeout(data->completion,
251 PMI_TIMEOUT);
252
253 data->completion = NULL;
254
255 mutex_unlock(&data->msg_mutex);
Christian Krafft813f9072007-07-20 21:39:18 +0200256
257 return 0;
Christian Krafft0e826642007-02-14 14:09:45 +0100258}
259EXPORT_SYMBOL_GPL(pmi_send_message);
260
Christian Krafft813f9072007-07-20 21:39:18 +0200261int pmi_register_handler(struct pmi_handler *handler)
Christian Krafft0e826642007-02-14 14:09:45 +0100262{
Christian Krafft79baf4a2007-04-23 21:35:43 +0200263 if (!data)
Christian Krafft813f9072007-07-20 21:39:18 +0200264 return -ENODEV;
Christian Krafft79baf4a2007-04-23 21:35:43 +0200265
Christian Krafft0e826642007-02-14 14:09:45 +0100266 spin_lock(&data->handler_spinlock);
267 list_add_tail(&handler->node, &data->handler);
268 spin_unlock(&data->handler_spinlock);
Christian Krafft813f9072007-07-20 21:39:18 +0200269
270 return 0;
Christian Krafft0e826642007-02-14 14:09:45 +0100271}
272EXPORT_SYMBOL_GPL(pmi_register_handler);
273
Christian Krafft813f9072007-07-20 21:39:18 +0200274void pmi_unregister_handler(struct pmi_handler *handler)
Christian Krafft0e826642007-02-14 14:09:45 +0100275{
Christian Krafft79baf4a2007-04-23 21:35:43 +0200276 if (!data)
277 return;
Christian Krafft0e826642007-02-14 14:09:45 +0100278
279 pr_debug("pmi: unregistering handler %p\n", handler);
280
Christian Krafft0e826642007-02-14 14:09:45 +0100281 spin_lock(&data->handler_spinlock);
282 list_del(&handler->node);
283 spin_unlock(&data->handler_spinlock);
284}
285EXPORT_SYMBOL_GPL(pmi_unregister_handler);
286
287MODULE_LICENSE("GPL");
288MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
289MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");