blob: 4260f368db5233391769204de3da3f67db1cf07c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Christian Krafft0e826642007-02-14 14:09:45 +010029#include <linux/completion.h>
30#include <linux/spinlock.h>
31#include <linux/workqueue.h>
Jon Loeligereb8f2762007-11-14 04:13:09 +110032#include <linux/of_device.h>
33#include <linux/of_platform.h>
Christian Krafft0e826642007-02-14 14:09:45 +010034
Christian Krafft0e826642007-02-14 14:09:45 +010035#include <asm/io.h>
36#include <asm/pmi.h>
Christian Krafft6bf05fd2007-04-23 21:35:45 +020037#include <asm/prom.h>
Christian Krafft0e826642007-02-14 14:09:45 +010038
39struct pmi_data {
40 struct list_head handler;
41 spinlock_t handler_spinlock;
42 spinlock_t pmi_spinlock;
43 struct mutex msg_mutex;
44 pmi_message_t msg;
45 struct completion *completion;
Grant Likelya454dc52010-07-22 15:52:34 -060046 struct platform_device *dev;
Christian Krafft0e826642007-02-14 14:09:45 +010047 int irq;
48 u8 __iomem *pmi_reg;
49 struct work_struct work;
50};
51
Christian Krafft813f9072007-07-20 21:39:18 +020052static struct pmi_data *data;
Christian Krafft0e826642007-02-14 14:09:45 +010053
Stephen Rothwellfb247442009-03-18 17:08:52 +000054static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
Christian Krafft0e826642007-02-14 14:09:45 +010055{
Christian Krafft0e826642007-02-14 14:09:45 +010056 u8 type;
57 int rc;
58
Christian Krafft0e826642007-02-14 14:09:45 +010059 spin_lock(&data->pmi_spinlock);
60
61 type = ioread8(data->pmi_reg + PMI_READ_TYPE);
62 pr_debug("pmi: got message of type %d\n", type);
63
64 if (type & PMI_ACK && !data->completion) {
65 printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
66 rc = -EIO;
67 goto unlock;
68 }
69
70 if (data->completion && !(type & PMI_ACK)) {
71 printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
72 rc = -EIO;
73 goto unlock;
74 }
75
76 data->msg.type = type;
77 data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
78 data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
79 data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
80 rc = 0;
81unlock:
82 spin_unlock(&data->pmi_spinlock);
83
84 if (rc == -EIO) {
85 rc = IRQ_HANDLED;
86 goto out;
87 }
88
89 if (data->msg.type & PMI_ACK) {
90 complete(data->completion);
91 rc = IRQ_HANDLED;
92 goto out;
93 }
94
95 schedule_work(&data->work);
96
97 rc = IRQ_HANDLED;
98out:
99 return rc;
100}
101
102
103static struct of_device_id pmi_match[] = {
104 { .type = "ibm,pmi", .name = "ibm,pmi" },
Christian Krafft4a065f92007-04-23 21:35:44 +0200105 { .type = "ibm,pmi" },
Christian Krafft0e826642007-02-14 14:09:45 +0100106 {},
107};
108
109MODULE_DEVICE_TABLE(of, pmi_match);
110
111static void pmi_notify_handlers(struct work_struct *work)
112{
Christian Krafft0e826642007-02-14 14:09:45 +0100113 struct pmi_handler *handler;
114
Christian Krafft0e826642007-02-14 14:09:45 +0100115 spin_lock(&data->handler_spinlock);
116 list_for_each_entry(handler, &data->handler, node) {
Joe Perches689fd142010-09-11 19:10:53 +0000117 pr_debug("pmi: notifying handler %p\n", handler);
Christian Krafft0e826642007-02-14 14:09:45 +0100118 if (handler->type == data->msg.type)
Christian Krafft813f9072007-07-20 21:39:18 +0200119 handler->handle_pmi_message(data->msg);
Christian Krafft0e826642007-02-14 14:09:45 +0100120 }
121 spin_unlock(&data->handler_spinlock);
122}
123
Grant Likelya454dc52010-07-22 15:52:34 -0600124static int pmi_of_probe(struct platform_device *dev,
Christian Krafft0e826642007-02-14 14:09:45 +0100125 const struct of_device_id *match)
126{
Grant Likely61c7a082010-04-13 16:12:29 -0700127 struct device_node *np = dev->dev.of_node;
Christian Krafft0e826642007-02-14 14:09:45 +0100128 int rc;
129
Christian Krafft813f9072007-07-20 21:39:18 +0200130 if (data) {
131 printk(KERN_ERR "pmi: driver has already been initialized.\n");
132 rc = -EBUSY;
133 goto out;
134 }
135
Christian Krafft0e826642007-02-14 14:09:45 +0100136 data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
137 if (!data) {
138 printk(KERN_ERR "pmi: could not allocate memory.\n");
139 rc = -ENOMEM;
140 goto out;
141 }
142
Christian Krafft6bf05fd2007-04-23 21:35:45 +0200143 data->pmi_reg = of_iomap(np, 0);
Christian Krafft0e826642007-02-14 14:09:45 +0100144 if (!data->pmi_reg) {
145 printk(KERN_ERR "pmi: invalid register address.\n");
146 rc = -EFAULT;
147 goto error_cleanup_data;
148 }
149
150 INIT_LIST_HEAD(&data->handler);
151
152 mutex_init(&data->msg_mutex);
153 spin_lock_init(&data->pmi_spinlock);
154 spin_lock_init(&data->handler_spinlock);
155
156 INIT_WORK(&data->work, pmi_notify_handlers);
157
Christian Krafft0e826642007-02-14 14:09:45 +0100158 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
Christian Krafft813f9072007-07-20 21:39:18 +0200167 rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
Christian Krafft0e826642007-02-14 14:09:45 +0100168 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
178error_cleanup_iomap:
179 iounmap(data->pmi_reg);
180
181error_cleanup_data:
182 kfree(data);
183
184out:
185 return rc;
186}
187
Grant Likelya454dc52010-07-22 15:52:34 -0600188static int pmi_of_remove(struct platform_device *dev)
Christian Krafft0e826642007-02-14 14:09:45 +0100189{
Christian Krafft0e826642007-02-14 14:09:45 +0100190 struct pmi_handler *handler, *tmp;
191
Christian Krafft813f9072007-07-20 21:39:18 +0200192 free_irq(data->irq, NULL);
Christian Krafft0e826642007-02-14 14:09:45 +0100193 iounmap(data->pmi_reg);
194
195 spin_lock(&data->handler_spinlock);
196
197 list_for_each_entry_safe(handler, tmp, &data->handler, node)
198 list_del(&handler->node);
199
200 spin_unlock(&data->handler_spinlock);
201
Christian Krafft813f9072007-07-20 21:39:18 +0200202 kfree(data);
203 data = NULL;
Christian Krafft0e826642007-02-14 14:09:45 +0100204
205 return 0;
206}
207
208static struct of_platform_driver pmi_of_platform_driver = {
Christian Krafft0e826642007-02-14 14:09:45 +0100209 .probe = pmi_of_probe,
Stephen Rothwell84dd4672007-10-11 15:19:03 +1000210 .remove = pmi_of_remove,
Grant Likely40182942010-04-13 16:13:02 -0700211 .driver = {
212 .name = "pmi",
213 .owner = THIS_MODULE,
214 .of_match_table = pmi_match,
Stephen Rothwell84dd4672007-10-11 15:19:03 +1000215 },
Christian Krafft0e826642007-02-14 14:09:45 +0100216};
217
218static int __init pmi_module_init(void)
219{
220 return of_register_platform_driver(&pmi_of_platform_driver);
221}
222module_init(pmi_module_init);
223
224static void __exit pmi_module_exit(void)
225{
226 of_unregister_platform_driver(&pmi_of_platform_driver);
227}
228module_exit(pmi_module_exit);
229
Christian Krafft813f9072007-07-20 21:39:18 +0200230int pmi_send_message(pmi_message_t msg)
Christian Krafft0e826642007-02-14 14:09:45 +0100231{
Christian Krafft0e826642007-02-14 14:09:45 +0100232 unsigned long flags;
233 DECLARE_COMPLETION_ONSTACK(completion);
234
Christian Krafft813f9072007-07-20 21:39:18 +0200235 if (!data)
236 return -ENODEV;
Christian Krafft0e826642007-02-14 14:09:45 +0100237
238 mutex_lock(&data->msg_mutex);
239
240 data->msg = msg;
241 pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
242
243 data->completion = &completion;
244
245 spin_lock_irqsave(&data->pmi_spinlock, flags);
246 iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
247 iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
248 iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
249 iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
250 spin_unlock_irqrestore(&data->pmi_spinlock, flags);
251
252 pr_debug("pmi_send_message: wait for completion\n");
253
254 wait_for_completion_interruptible_timeout(data->completion,
255 PMI_TIMEOUT);
256
257 data->completion = NULL;
258
259 mutex_unlock(&data->msg_mutex);
Christian Krafft813f9072007-07-20 21:39:18 +0200260
261 return 0;
Christian Krafft0e826642007-02-14 14:09:45 +0100262}
263EXPORT_SYMBOL_GPL(pmi_send_message);
264
Christian Krafft813f9072007-07-20 21:39:18 +0200265int pmi_register_handler(struct pmi_handler *handler)
Christian Krafft0e826642007-02-14 14:09:45 +0100266{
Christian Krafft79baf4a2007-04-23 21:35:43 +0200267 if (!data)
Christian Krafft813f9072007-07-20 21:39:18 +0200268 return -ENODEV;
Christian Krafft79baf4a2007-04-23 21:35:43 +0200269
Christian Krafft0e826642007-02-14 14:09:45 +0100270 spin_lock(&data->handler_spinlock);
271 list_add_tail(&handler->node, &data->handler);
272 spin_unlock(&data->handler_spinlock);
Christian Krafft813f9072007-07-20 21:39:18 +0200273
274 return 0;
Christian Krafft0e826642007-02-14 14:09:45 +0100275}
276EXPORT_SYMBOL_GPL(pmi_register_handler);
277
Christian Krafft813f9072007-07-20 21:39:18 +0200278void pmi_unregister_handler(struct pmi_handler *handler)
Christian Krafft0e826642007-02-14 14:09:45 +0100279{
Christian Krafft79baf4a2007-04-23 21:35:43 +0200280 if (!data)
281 return;
Christian Krafft0e826642007-02-14 14:09:45 +0100282
283 pr_debug("pmi: unregistering handler %p\n", handler);
284
Christian Krafft0e826642007-02-14 14:09:45 +0100285 spin_lock(&data->handler_spinlock);
286 list_del(&handler->node);
287 spin_unlock(&data->handler_spinlock);
288}
289EXPORT_SYMBOL_GPL(pmi_unregister_handler);
290
291MODULE_LICENSE("GPL");
292MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
293MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");