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)
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>
32 #include <asm/of_device.h>
33 #include <asm/of_platform.h>
39 struct list_head handler
;
40 spinlock_t handler_spinlock
;
41 spinlock_t pmi_spinlock
;
42 struct mutex msg_mutex
;
44 struct completion
*completion
;
45 struct of_device
*dev
;
48 struct work_struct work
;
53 static void __iomem
*of_iomap(struct device_node
*np
)
57 if (of_address_to_resource(np
, 0, &res
))
60 pr_debug("Resource start: 0x%lx\n", res
.start
);
61 pr_debug("Resource end: 0x%lx\n", res
.end
);
63 return ioremap(res
.start
, 1 + res
.end
- res
.start
);
67 static int pmi_irq_handler(int irq
, void *dev_id
)
69 struct pmi_data
*data
;
75 spin_lock(&data
->pmi_spinlock
);
77 type
= ioread8(data
->pmi_reg
+ PMI_READ_TYPE
);
78 pr_debug("pmi: got message of type %d\n", type
);
80 if (type
& PMI_ACK
&& !data
->completion
) {
81 printk(KERN_WARNING
"pmi: got unexpected ACK message.\n");
86 if (data
->completion
&& !(type
& PMI_ACK
)) {
87 printk(KERN_WARNING
"pmi: expected ACK, but got %d\n", type
);
92 data
->msg
.type
= type
;
93 data
->msg
.data0
= ioread8(data
->pmi_reg
+ PMI_READ_DATA0
);
94 data
->msg
.data1
= ioread8(data
->pmi_reg
+ PMI_READ_DATA1
);
95 data
->msg
.data2
= ioread8(data
->pmi_reg
+ PMI_READ_DATA2
);
98 spin_unlock(&data
->pmi_spinlock
);
105 if (data
->msg
.type
& PMI_ACK
) {
106 complete(data
->completion
);
111 schedule_work(&data
->work
);
119 static struct of_device_id pmi_match
[] = {
120 { .type
= "ibm,pmi", .name
= "ibm,pmi" },
124 MODULE_DEVICE_TABLE(of
, pmi_match
);
126 static void pmi_notify_handlers(struct work_struct
*work
)
128 struct pmi_data
*data
;
129 struct pmi_handler
*handler
;
131 data
= container_of(work
, struct pmi_data
, work
);
133 spin_lock(&data
->handler_spinlock
);
134 list_for_each_entry(handler
, &data
->handler
, node
) {
135 pr_debug(KERN_INFO
"pmi: notifying handler %p\n", handler
);
136 if (handler
->type
== data
->msg
.type
)
137 handler
->handle_pmi_message(data
->dev
, data
->msg
);
139 spin_unlock(&data
->handler_spinlock
);
142 static int pmi_of_probe(struct of_device
*dev
,
143 const struct of_device_id
*match
)
145 struct device_node
*np
= dev
->node
;
146 struct pmi_data
*data
;
149 data
= kzalloc(sizeof(struct pmi_data
), GFP_KERNEL
);
151 printk(KERN_ERR
"pmi: could not allocate memory.\n");
156 data
->pmi_reg
= of_iomap(np
);
157 if (!data
->pmi_reg
) {
158 printk(KERN_ERR
"pmi: invalid register address.\n");
160 goto error_cleanup_data
;
163 INIT_LIST_HEAD(&data
->handler
);
165 mutex_init(&data
->msg_mutex
);
166 spin_lock_init(&data
->pmi_spinlock
);
167 spin_lock_init(&data
->handler_spinlock
);
169 INIT_WORK(&data
->work
, pmi_notify_handlers
);
171 dev
->dev
.driver_data
= data
;
174 data
->irq
= irq_of_parse_and_map(np
, 0);
175 if (data
->irq
== NO_IRQ
) {
176 printk(KERN_ERR
"pmi: invalid interrupt.\n");
178 goto error_cleanup_iomap
;
181 rc
= request_irq(data
->irq
, pmi_irq_handler
, 0, "pmi", data
);
183 printk(KERN_ERR
"pmi: can't request IRQ %d: returned %d\n",
185 goto error_cleanup_iomap
;
188 printk(KERN_INFO
"pmi: found pmi device at addr %p.\n", data
->pmi_reg
);
193 iounmap(data
->pmi_reg
);
202 static int pmi_of_remove(struct of_device
*dev
)
204 struct pmi_data
*data
;
205 struct pmi_handler
*handler
, *tmp
;
207 data
= dev
->dev
.driver_data
;
209 free_irq(data
->irq
, data
);
210 iounmap(data
->pmi_reg
);
212 spin_lock(&data
->handler_spinlock
);
214 list_for_each_entry_safe(handler
, tmp
, &data
->handler
, node
)
215 list_del(&handler
->node
);
217 spin_unlock(&data
->handler_spinlock
);
219 kfree(dev
->dev
.driver_data
);
224 static struct of_platform_driver pmi_of_platform_driver
= {
226 .match_table
= pmi_match
,
227 .probe
= pmi_of_probe
,
228 .remove
= pmi_of_remove
231 static int __init
pmi_module_init(void)
233 return of_register_platform_driver(&pmi_of_platform_driver
);
235 module_init(pmi_module_init
);
237 static void __exit
pmi_module_exit(void)
239 of_unregister_platform_driver(&pmi_of_platform_driver
);
241 module_exit(pmi_module_exit
);
243 void pmi_send_message(struct of_device
*device
, pmi_message_t msg
)
245 struct pmi_data
*data
;
247 DECLARE_COMPLETION_ONSTACK(completion
);
249 data
= device
->dev
.driver_data
;
251 mutex_lock(&data
->msg_mutex
);
254 pr_debug("pmi_send_message: msg is %08x\n", *(u32
*)&msg
);
256 data
->completion
= &completion
;
258 spin_lock_irqsave(&data
->pmi_spinlock
, flags
);
259 iowrite8(msg
.data0
, data
->pmi_reg
+ PMI_WRITE_DATA0
);
260 iowrite8(msg
.data1
, data
->pmi_reg
+ PMI_WRITE_DATA1
);
261 iowrite8(msg
.data2
, data
->pmi_reg
+ PMI_WRITE_DATA2
);
262 iowrite8(msg
.type
, data
->pmi_reg
+ PMI_WRITE_TYPE
);
263 spin_unlock_irqrestore(&data
->pmi_spinlock
, flags
);
265 pr_debug("pmi_send_message: wait for completion\n");
267 wait_for_completion_interruptible_timeout(data
->completion
,
270 data
->completion
= NULL
;
272 mutex_unlock(&data
->msg_mutex
);
274 EXPORT_SYMBOL_GPL(pmi_send_message
);
276 void pmi_register_handler(struct of_device
*device
,
277 struct pmi_handler
*handler
)
279 struct pmi_data
*data
;
280 data
= device
->dev
.driver_data
;
285 spin_lock(&data
->handler_spinlock
);
286 list_add_tail(&handler
->node
, &data
->handler
);
287 spin_unlock(&data
->handler_spinlock
);
289 EXPORT_SYMBOL_GPL(pmi_register_handler
);
291 void pmi_unregister_handler(struct of_device
*device
,
292 struct pmi_handler
*handler
)
294 struct pmi_data
*data
;
295 data
= device
->dev
.driver_data
;
300 pr_debug("pmi: unregistering handler %p\n", handler
);
302 spin_lock(&data
->handler_spinlock
);
303 list_del(&handler
->node
);
304 spin_unlock(&data
->handler_spinlock
);
306 EXPORT_SYMBOL_GPL(pmi_unregister_handler
);
308 MODULE_LICENSE("GPL");
309 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
310 MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");