2 * Copyright (c) 2015-2016, IBM Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/atomic.h>
11 #include <linux/bt-bmc.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
15 #include <linux/miscdevice.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
23 * This is a BMC device used to communicate to the host
25 #define DEVICE_NAME "ipmi-bt-host"
27 #define BT_IO_BASE 0xe4
31 #define BT_CR0_IO_BASE 16
33 #define BT_CR0_EN_CLR_SLV_RDP 0x8
34 #define BT_CR0_EN_CLR_SLV_WRP 0x4
35 #define BT_CR0_ENABLE_IBT 0x1
37 #define BT_CR1_IRQ_H2B 0x01
38 #define BT_CR1_IRQ_HBUSY 0x40
40 #define BT_CR2_IRQ_H2B 0x01
41 #define BT_CR2_IRQ_HBUSY 0x40
44 #define BT_CTRL_B_BUSY 0x80
45 #define BT_CTRL_H_BUSY 0x40
46 #define BT_CTRL_OEM0 0x20
47 #define BT_CTRL_SMS_ATN 0x10
48 #define BT_CTRL_B2H_ATN 0x08
49 #define BT_CTRL_H2B_ATN 0x04
50 #define BT_CTRL_CLR_RD_PTR 0x02
51 #define BT_CTRL_CLR_WR_PTR 0x01
52 #define BT_BMC2HOST 0x14
53 #define BT_INTMASK 0x18
54 #define BT_INTMASK_B2H_IRQEN 0x01
55 #define BT_INTMASK_B2H_IRQ 0x02
56 #define BT_INTMASK_BMC_HWRST 0x80
58 #define BT_BMC_BUFFER_SIZE 256
62 struct miscdevice miscdev
;
65 wait_queue_head_t queue
;
66 struct timer_list poll_timer
;
70 static atomic_t open_count
= ATOMIC_INIT(0);
72 static u8
bt_inb(struct bt_bmc
*bt_bmc
, int reg
)
74 return ioread8(bt_bmc
->base
+ reg
);
77 static void bt_outb(struct bt_bmc
*bt_bmc
, u8 data
, int reg
)
79 iowrite8(data
, bt_bmc
->base
+ reg
);
82 static void clr_rd_ptr(struct bt_bmc
*bt_bmc
)
84 bt_outb(bt_bmc
, BT_CTRL_CLR_RD_PTR
, BT_CTRL
);
87 static void clr_wr_ptr(struct bt_bmc
*bt_bmc
)
89 bt_outb(bt_bmc
, BT_CTRL_CLR_WR_PTR
, BT_CTRL
);
92 static void clr_h2b_atn(struct bt_bmc
*bt_bmc
)
94 bt_outb(bt_bmc
, BT_CTRL_H2B_ATN
, BT_CTRL
);
97 static void set_b_busy(struct bt_bmc
*bt_bmc
)
99 if (!(bt_inb(bt_bmc
, BT_CTRL
) & BT_CTRL_B_BUSY
))
100 bt_outb(bt_bmc
, BT_CTRL_B_BUSY
, BT_CTRL
);
103 static void clr_b_busy(struct bt_bmc
*bt_bmc
)
105 if (bt_inb(bt_bmc
, BT_CTRL
) & BT_CTRL_B_BUSY
)
106 bt_outb(bt_bmc
, BT_CTRL_B_BUSY
, BT_CTRL
);
109 static void set_b2h_atn(struct bt_bmc
*bt_bmc
)
111 bt_outb(bt_bmc
, BT_CTRL_B2H_ATN
, BT_CTRL
);
114 static u8
bt_read(struct bt_bmc
*bt_bmc
)
116 return bt_inb(bt_bmc
, BT_BMC2HOST
);
119 static ssize_t
bt_readn(struct bt_bmc
*bt_bmc
, u8
*buf
, size_t n
)
123 for (i
= 0; i
< n
; i
++)
124 buf
[i
] = bt_read(bt_bmc
);
128 static void bt_write(struct bt_bmc
*bt_bmc
, u8 c
)
130 bt_outb(bt_bmc
, c
, BT_BMC2HOST
);
133 static ssize_t
bt_writen(struct bt_bmc
*bt_bmc
, u8
*buf
, size_t n
)
137 for (i
= 0; i
< n
; i
++)
138 bt_write(bt_bmc
, buf
[i
]);
142 static void set_sms_atn(struct bt_bmc
*bt_bmc
)
144 bt_outb(bt_bmc
, BT_CTRL_SMS_ATN
, BT_CTRL
);
147 static struct bt_bmc
*file_bt_bmc(struct file
*file
)
149 return container_of(file
->private_data
, struct bt_bmc
, miscdev
);
152 static int bt_bmc_open(struct inode
*inode
, struct file
*file
)
154 struct bt_bmc
*bt_bmc
= file_bt_bmc(file
);
156 if (atomic_inc_return(&open_count
) == 1) {
161 atomic_dec(&open_count
);
166 * The BT (Block Transfer) interface means that entire messages are
167 * buffered by the host before a notification is sent to the BMC that
168 * there is data to be read. The first byte is the length and the
169 * message data follows. The read operation just tries to capture the
170 * whole before returning it to userspace.
172 * BT Message format :
174 * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5:N
175 * Length NetFn/LUN Seq Cmd Data
178 static ssize_t
bt_bmc_read(struct file
*file
, char __user
*buf
,
179 size_t count
, loff_t
*ppos
)
181 struct bt_bmc
*bt_bmc
= file_bt_bmc(file
);
184 u8 kbuffer
[BT_BMC_BUFFER_SIZE
];
188 if (!access_ok(VERIFY_WRITE
, buf
, count
))
193 if (wait_event_interruptible(bt_bmc
->queue
,
194 bt_inb(bt_bmc
, BT_CTRL
) & BT_CTRL_H2B_ATN
))
197 mutex_lock(&bt_bmc
->mutex
);
199 if (unlikely(!(bt_inb(bt_bmc
, BT_CTRL
) & BT_CTRL_H2B_ATN
))) {
209 * The BT frames start with the message length, which does not
210 * include the length byte.
212 kbuffer
[0] = bt_read(bt_bmc
);
215 /* We pass the length back to userspace as well */
220 nread
= min_t(ssize_t
, len
, sizeof(kbuffer
) - len_byte
);
222 bt_readn(bt_bmc
, kbuffer
+ len_byte
, nread
);
224 if (copy_to_user(buf
, kbuffer
, nread
+ len_byte
)) {
229 buf
+= nread
+ len_byte
;
230 ret
+= nread
+ len_byte
;
237 mutex_unlock(&bt_bmc
->mutex
);
242 * BT Message response format :
244 * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6:N
245 * Length NetFn/LUN Seq Cmd Code Data
247 static ssize_t
bt_bmc_write(struct file
*file
, const char __user
*buf
,
248 size_t count
, loff_t
*ppos
)
250 struct bt_bmc
*bt_bmc
= file_bt_bmc(file
);
251 u8 kbuffer
[BT_BMC_BUFFER_SIZE
];
256 * send a minimum response size
261 if (!access_ok(VERIFY_READ
, buf
, count
))
267 * There's no interrupt for clearing bmc busy so we have to
270 if (wait_event_interruptible(bt_bmc
->queue
,
271 !(bt_inb(bt_bmc
, BT_CTRL
) &
272 (BT_CTRL_H_BUSY
| BT_CTRL_B2H_ATN
))))
275 mutex_lock(&bt_bmc
->mutex
);
277 if (unlikely(bt_inb(bt_bmc
, BT_CTRL
) &
278 (BT_CTRL_H_BUSY
| BT_CTRL_B2H_ATN
))) {
286 nwritten
= min_t(ssize_t
, count
, sizeof(kbuffer
));
287 if (copy_from_user(&kbuffer
, buf
, nwritten
)) {
292 bt_writen(bt_bmc
, kbuffer
, nwritten
);
302 mutex_unlock(&bt_bmc
->mutex
);
306 static long bt_bmc_ioctl(struct file
*file
, unsigned int cmd
,
309 struct bt_bmc
*bt_bmc
= file_bt_bmc(file
);
312 case BT_BMC_IOCTL_SMS_ATN
:
319 static int bt_bmc_release(struct inode
*inode
, struct file
*file
)
321 struct bt_bmc
*bt_bmc
= file_bt_bmc(file
);
323 atomic_dec(&open_count
);
328 static unsigned int bt_bmc_poll(struct file
*file
, poll_table
*wait
)
330 struct bt_bmc
*bt_bmc
= file_bt_bmc(file
);
331 unsigned int mask
= 0;
334 poll_wait(file
, &bt_bmc
->queue
, wait
);
336 ctrl
= bt_inb(bt_bmc
, BT_CTRL
);
338 if (ctrl
& BT_CTRL_H2B_ATN
)
341 if (!(ctrl
& (BT_CTRL_H_BUSY
| BT_CTRL_B2H_ATN
)))
347 static const struct file_operations bt_bmc_fops
= {
348 .owner
= THIS_MODULE
,
351 .write
= bt_bmc_write
,
352 .release
= bt_bmc_release
,
354 .unlocked_ioctl
= bt_bmc_ioctl
,
357 static void poll_timer(unsigned long data
)
359 struct bt_bmc
*bt_bmc
= (void *)data
;
361 bt_bmc
->poll_timer
.expires
+= msecs_to_jiffies(500);
362 wake_up(&bt_bmc
->queue
);
363 add_timer(&bt_bmc
->poll_timer
);
366 static irqreturn_t
bt_bmc_irq(int irq
, void *arg
)
368 struct bt_bmc
*bt_bmc
= arg
;
371 reg
= ioread32(bt_bmc
->base
+ BT_CR2
);
372 reg
&= BT_CR2_IRQ_H2B
| BT_CR2_IRQ_HBUSY
;
376 /* ack pending IRQs */
377 iowrite32(reg
, bt_bmc
->base
+ BT_CR2
);
379 wake_up(&bt_bmc
->queue
);
383 static int bt_bmc_config_irq(struct bt_bmc
*bt_bmc
,
384 struct platform_device
*pdev
)
386 struct device
*dev
= &pdev
->dev
;
390 bt_bmc
->irq
= platform_get_irq(pdev
, 0);
394 rc
= devm_request_irq(dev
, bt_bmc
->irq
, bt_bmc_irq
, IRQF_SHARED
,
395 DEVICE_NAME
, bt_bmc
);
397 dev_warn(dev
, "Unable to request IRQ %d\n", bt_bmc
->irq
);
403 * Configure IRQs on the bmc clearing the H2B and HBUSY bits;
404 * H2B will be asserted when the bmc has data for us; HBUSY
405 * will be cleared (along with B2H) when we can write the next
406 * message to the BT buffer
408 reg
= ioread32(bt_bmc
->base
+ BT_CR1
);
409 reg
|= BT_CR1_IRQ_H2B
| BT_CR1_IRQ_HBUSY
;
410 iowrite32(reg
, bt_bmc
->base
+ BT_CR1
);
415 static int bt_bmc_probe(struct platform_device
*pdev
)
417 struct bt_bmc
*bt_bmc
;
419 struct resource
*res
;
422 if (!pdev
|| !pdev
->dev
.of_node
)
426 dev_info(dev
, "Found bt bmc device\n");
428 bt_bmc
= devm_kzalloc(dev
, sizeof(*bt_bmc
), GFP_KERNEL
);
432 dev_set_drvdata(&pdev
->dev
, bt_bmc
);
434 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
435 bt_bmc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
436 if (IS_ERR(bt_bmc
->base
))
437 return PTR_ERR(bt_bmc
->base
);
439 mutex_init(&bt_bmc
->mutex
);
440 init_waitqueue_head(&bt_bmc
->queue
);
442 bt_bmc
->miscdev
.minor
= MISC_DYNAMIC_MINOR
,
443 bt_bmc
->miscdev
.name
= DEVICE_NAME
,
444 bt_bmc
->miscdev
.fops
= &bt_bmc_fops
,
445 bt_bmc
->miscdev
.parent
= dev
;
446 rc
= misc_register(&bt_bmc
->miscdev
);
448 dev_err(dev
, "Unable to register misc device\n");
452 bt_bmc_config_irq(bt_bmc
, pdev
);
455 dev_info(dev
, "Using IRQ %d\n", bt_bmc
->irq
);
457 dev_info(dev
, "No IRQ; using timer\n");
458 setup_timer(&bt_bmc
->poll_timer
, poll_timer
,
459 (unsigned long)bt_bmc
);
460 bt_bmc
->poll_timer
.expires
= jiffies
+ msecs_to_jiffies(10);
461 add_timer(&bt_bmc
->poll_timer
);
464 iowrite32((BT_IO_BASE
<< BT_CR0_IO_BASE
) |
465 (BT_IRQ
<< BT_CR0_IRQ
) |
466 BT_CR0_EN_CLR_SLV_RDP
|
467 BT_CR0_EN_CLR_SLV_WRP
|
469 bt_bmc
->base
+ BT_CR0
);
476 static int bt_bmc_remove(struct platform_device
*pdev
)
478 struct bt_bmc
*bt_bmc
= dev_get_drvdata(&pdev
->dev
);
480 misc_deregister(&bt_bmc
->miscdev
);
482 del_timer_sync(&bt_bmc
->poll_timer
);
486 static const struct of_device_id bt_bmc_match
[] = {
487 { .compatible
= "aspeed,ast2400-ibt-bmc" },
491 static struct platform_driver bt_bmc_driver
= {
494 .of_match_table
= bt_bmc_match
,
496 .probe
= bt_bmc_probe
,
497 .remove
= bt_bmc_remove
,
500 module_platform_driver(bt_bmc_driver
);
502 MODULE_DEVICE_TABLE(of
, bt_bmc_match
);
503 MODULE_LICENSE("GPL");
504 MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
505 MODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");