1243ac210SCorey Minyard // SPDX-License-Identifier: GPL-2.0+
254f9c4d0SAlistair Popple /*
354f9c4d0SAlistair Popple * Copyright (c) 2015-2016, IBM Corporation.
454f9c4d0SAlistair Popple */
554f9c4d0SAlistair Popple
654f9c4d0SAlistair Popple #include <linux/atomic.h>
754f9c4d0SAlistair Popple #include <linux/bt-bmc.h>
854f9c4d0SAlistair Popple #include <linux/errno.h>
954f9c4d0SAlistair Popple #include <linux/interrupt.h>
1054f9c4d0SAlistair Popple #include <linux/io.h>
1154f9c4d0SAlistair Popple #include <linux/miscdevice.h>
1254f9c4d0SAlistair Popple #include <linux/module.h>
13eb994594SAndrew Jeffery #include <linux/of.h>
1454f9c4d0SAlistair Popple #include <linux/platform_device.h>
1554f9c4d0SAlistair Popple #include <linux/poll.h>
1654f9c4d0SAlistair Popple #include <linux/sched.h>
1754f9c4d0SAlistair Popple #include <linux/timer.h>
1854f9c4d0SAlistair Popple
1954f9c4d0SAlistair Popple /*
2054f9c4d0SAlistair Popple * This is a BMC device used to communicate to the host
2154f9c4d0SAlistair Popple */
2254f9c4d0SAlistair Popple #define DEVICE_NAME "ipmi-bt-host"
2354f9c4d0SAlistair Popple
2454f9c4d0SAlistair Popple #define BT_IO_BASE 0xe4
2554f9c4d0SAlistair Popple #define BT_IRQ 10
2654f9c4d0SAlistair Popple
2754f9c4d0SAlistair Popple #define BT_CR0 0x0
2854f9c4d0SAlistair Popple #define BT_CR0_IO_BASE 16
2954f9c4d0SAlistair Popple #define BT_CR0_IRQ 12
3054f9c4d0SAlistair Popple #define BT_CR0_EN_CLR_SLV_RDP 0x8
3154f9c4d0SAlistair Popple #define BT_CR0_EN_CLR_SLV_WRP 0x4
3254f9c4d0SAlistair Popple #define BT_CR0_ENABLE_IBT 0x1
3354f9c4d0SAlistair Popple #define BT_CR1 0x4
3454f9c4d0SAlistair Popple #define BT_CR1_IRQ_H2B 0x01
3554f9c4d0SAlistair Popple #define BT_CR1_IRQ_HBUSY 0x40
3654f9c4d0SAlistair Popple #define BT_CR2 0x8
3754f9c4d0SAlistair Popple #define BT_CR2_IRQ_H2B 0x01
3854f9c4d0SAlistair Popple #define BT_CR2_IRQ_HBUSY 0x40
3954f9c4d0SAlistair Popple #define BT_CR3 0xc
4054f9c4d0SAlistair Popple #define BT_CTRL 0x10
4154f9c4d0SAlistair Popple #define BT_CTRL_B_BUSY 0x80
4254f9c4d0SAlistair Popple #define BT_CTRL_H_BUSY 0x40
4354f9c4d0SAlistair Popple #define BT_CTRL_OEM0 0x20
4454f9c4d0SAlistair Popple #define BT_CTRL_SMS_ATN 0x10
4554f9c4d0SAlistair Popple #define BT_CTRL_B2H_ATN 0x08
4654f9c4d0SAlistair Popple #define BT_CTRL_H2B_ATN 0x04
4754f9c4d0SAlistair Popple #define BT_CTRL_CLR_RD_PTR 0x02
4854f9c4d0SAlistair Popple #define BT_CTRL_CLR_WR_PTR 0x01
4954f9c4d0SAlistair Popple #define BT_BMC2HOST 0x14
5054f9c4d0SAlistair Popple #define BT_INTMASK 0x18
5154f9c4d0SAlistair Popple #define BT_INTMASK_B2H_IRQEN 0x01
5254f9c4d0SAlistair Popple #define BT_INTMASK_B2H_IRQ 0x02
5354f9c4d0SAlistair Popple #define BT_INTMASK_BMC_HWRST 0x80
5454f9c4d0SAlistair Popple
5554f9c4d0SAlistair Popple #define BT_BMC_BUFFER_SIZE 256
5654f9c4d0SAlistair Popple
5754f9c4d0SAlistair Popple struct bt_bmc {
5854f9c4d0SAlistair Popple struct device dev;
5954f9c4d0SAlistair Popple struct miscdevice miscdev;
60396c84bbSJoel Stanley void __iomem *base;
6154f9c4d0SAlistair Popple int irq;
6254f9c4d0SAlistair Popple wait_queue_head_t queue;
6354f9c4d0SAlistair Popple struct timer_list poll_timer;
6454f9c4d0SAlistair Popple struct mutex mutex;
6554f9c4d0SAlistair Popple };
6654f9c4d0SAlistair Popple
6754f9c4d0SAlistair Popple static atomic_t open_count = ATOMIC_INIT(0);
6854f9c4d0SAlistair Popple
bt_inb(struct bt_bmc * bt_bmc,int reg)6954f9c4d0SAlistair Popple static u8 bt_inb(struct bt_bmc *bt_bmc, int reg)
7054f9c4d0SAlistair Popple {
71396c84bbSJoel Stanley return readb(bt_bmc->base + reg);
7254f9c4d0SAlistair Popple }
7354f9c4d0SAlistair Popple
bt_outb(struct bt_bmc * bt_bmc,u8 data,int reg)7454f9c4d0SAlistair Popple static void bt_outb(struct bt_bmc *bt_bmc, u8 data, int reg)
7554f9c4d0SAlistair Popple {
76396c84bbSJoel Stanley writeb(data, bt_bmc->base + reg);
7754f9c4d0SAlistair Popple }
7854f9c4d0SAlistair Popple
clr_rd_ptr(struct bt_bmc * bt_bmc)7954f9c4d0SAlistair Popple static void clr_rd_ptr(struct bt_bmc *bt_bmc)
8054f9c4d0SAlistair Popple {
8154f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_CLR_RD_PTR, BT_CTRL);
8254f9c4d0SAlistair Popple }
8354f9c4d0SAlistair Popple
clr_wr_ptr(struct bt_bmc * bt_bmc)8454f9c4d0SAlistair Popple static void clr_wr_ptr(struct bt_bmc *bt_bmc)
8554f9c4d0SAlistair Popple {
8654f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_CLR_WR_PTR, BT_CTRL);
8754f9c4d0SAlistair Popple }
8854f9c4d0SAlistair Popple
clr_h2b_atn(struct bt_bmc * bt_bmc)8954f9c4d0SAlistair Popple static void clr_h2b_atn(struct bt_bmc *bt_bmc)
9054f9c4d0SAlistair Popple {
9154f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_H2B_ATN, BT_CTRL);
9254f9c4d0SAlistair Popple }
9354f9c4d0SAlistair Popple
set_b_busy(struct bt_bmc * bt_bmc)9454f9c4d0SAlistair Popple static void set_b_busy(struct bt_bmc *bt_bmc)
9554f9c4d0SAlistair Popple {
9654f9c4d0SAlistair Popple if (!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY))
9754f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
9854f9c4d0SAlistair Popple }
9954f9c4d0SAlistair Popple
clr_b_busy(struct bt_bmc * bt_bmc)10054f9c4d0SAlistair Popple static void clr_b_busy(struct bt_bmc *bt_bmc)
10154f9c4d0SAlistair Popple {
10254f9c4d0SAlistair Popple if (bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY)
10354f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
10454f9c4d0SAlistair Popple }
10554f9c4d0SAlistair Popple
set_b2h_atn(struct bt_bmc * bt_bmc)10654f9c4d0SAlistair Popple static void set_b2h_atn(struct bt_bmc *bt_bmc)
10754f9c4d0SAlistair Popple {
10854f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_B2H_ATN, BT_CTRL);
10954f9c4d0SAlistair Popple }
11054f9c4d0SAlistair Popple
bt_read(struct bt_bmc * bt_bmc)11154f9c4d0SAlistair Popple static u8 bt_read(struct bt_bmc *bt_bmc)
11254f9c4d0SAlistair Popple {
11354f9c4d0SAlistair Popple return bt_inb(bt_bmc, BT_BMC2HOST);
11454f9c4d0SAlistair Popple }
11554f9c4d0SAlistair Popple
bt_readn(struct bt_bmc * bt_bmc,u8 * buf,size_t n)11654f9c4d0SAlistair Popple static ssize_t bt_readn(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
11754f9c4d0SAlistair Popple {
11854f9c4d0SAlistair Popple int i;
11954f9c4d0SAlistair Popple
12054f9c4d0SAlistair Popple for (i = 0; i < n; i++)
12154f9c4d0SAlistair Popple buf[i] = bt_read(bt_bmc);
12254f9c4d0SAlistair Popple return n;
12354f9c4d0SAlistair Popple }
12454f9c4d0SAlistair Popple
bt_write(struct bt_bmc * bt_bmc,u8 c)12554f9c4d0SAlistair Popple static void bt_write(struct bt_bmc *bt_bmc, u8 c)
12654f9c4d0SAlistair Popple {
12754f9c4d0SAlistair Popple bt_outb(bt_bmc, c, BT_BMC2HOST);
12854f9c4d0SAlistair Popple }
12954f9c4d0SAlistair Popple
bt_writen(struct bt_bmc * bt_bmc,u8 * buf,size_t n)13054f9c4d0SAlistair Popple static ssize_t bt_writen(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
13154f9c4d0SAlistair Popple {
13254f9c4d0SAlistair Popple int i;
13354f9c4d0SAlistair Popple
13454f9c4d0SAlistair Popple for (i = 0; i < n; i++)
13554f9c4d0SAlistair Popple bt_write(bt_bmc, buf[i]);
13654f9c4d0SAlistair Popple return n;
13754f9c4d0SAlistair Popple }
13854f9c4d0SAlistair Popple
set_sms_atn(struct bt_bmc * bt_bmc)13954f9c4d0SAlistair Popple static void set_sms_atn(struct bt_bmc *bt_bmc)
14054f9c4d0SAlistair Popple {
14154f9c4d0SAlistair Popple bt_outb(bt_bmc, BT_CTRL_SMS_ATN, BT_CTRL);
14254f9c4d0SAlistair Popple }
14354f9c4d0SAlistair Popple
file_bt_bmc(struct file * file)14454f9c4d0SAlistair Popple static struct bt_bmc *file_bt_bmc(struct file *file)
14554f9c4d0SAlistair Popple {
14654f9c4d0SAlistair Popple return container_of(file->private_data, struct bt_bmc, miscdev);
14754f9c4d0SAlistair Popple }
14854f9c4d0SAlistair Popple
bt_bmc_open(struct inode * inode,struct file * file)14954f9c4d0SAlistair Popple static int bt_bmc_open(struct inode *inode, struct file *file)
15054f9c4d0SAlistair Popple {
15154f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = file_bt_bmc(file);
15254f9c4d0SAlistair Popple
15354f9c4d0SAlistair Popple if (atomic_inc_return(&open_count) == 1) {
15454f9c4d0SAlistair Popple clr_b_busy(bt_bmc);
15554f9c4d0SAlistair Popple return 0;
15654f9c4d0SAlistair Popple }
15754f9c4d0SAlistair Popple
15854f9c4d0SAlistair Popple atomic_dec(&open_count);
15954f9c4d0SAlistair Popple return -EBUSY;
16054f9c4d0SAlistair Popple }
16154f9c4d0SAlistair Popple
16254f9c4d0SAlistair Popple /*
16354f9c4d0SAlistair Popple * The BT (Block Transfer) interface means that entire messages are
16454f9c4d0SAlistair Popple * buffered by the host before a notification is sent to the BMC that
16554f9c4d0SAlistair Popple * there is data to be read. The first byte is the length and the
16654f9c4d0SAlistair Popple * message data follows. The read operation just tries to capture the
16754f9c4d0SAlistair Popple * whole before returning it to userspace.
16854f9c4d0SAlistair Popple *
16954f9c4d0SAlistair Popple * BT Message format :
17054f9c4d0SAlistair Popple *
17154f9c4d0SAlistair Popple * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5:N
17254f9c4d0SAlistair Popple * Length NetFn/LUN Seq Cmd Data
17354f9c4d0SAlistair Popple *
17454f9c4d0SAlistair Popple */
bt_bmc_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)17554f9c4d0SAlistair Popple static ssize_t bt_bmc_read(struct file *file, char __user *buf,
17654f9c4d0SAlistair Popple size_t count, loff_t *ppos)
17754f9c4d0SAlistair Popple {
17854f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = file_bt_bmc(file);
17954f9c4d0SAlistair Popple u8 len;
18054f9c4d0SAlistair Popple int len_byte = 1;
18154f9c4d0SAlistair Popple u8 kbuffer[BT_BMC_BUFFER_SIZE];
18254f9c4d0SAlistair Popple ssize_t ret = 0;
18354f9c4d0SAlistair Popple ssize_t nread;
18454f9c4d0SAlistair Popple
18554f9c4d0SAlistair Popple WARN_ON(*ppos);
18654f9c4d0SAlistair Popple
18754f9c4d0SAlistair Popple if (wait_event_interruptible(bt_bmc->queue,
18854f9c4d0SAlistair Popple bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
18954f9c4d0SAlistair Popple return -ERESTARTSYS;
19054f9c4d0SAlistair Popple
19154f9c4d0SAlistair Popple mutex_lock(&bt_bmc->mutex);
19254f9c4d0SAlistair Popple
19354f9c4d0SAlistair Popple if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
19454f9c4d0SAlistair Popple ret = -EIO;
19554f9c4d0SAlistair Popple goto out_unlock;
19654f9c4d0SAlistair Popple }
19754f9c4d0SAlistair Popple
19854f9c4d0SAlistair Popple set_b_busy(bt_bmc);
19954f9c4d0SAlistair Popple clr_h2b_atn(bt_bmc);
20054f9c4d0SAlistair Popple clr_rd_ptr(bt_bmc);
20154f9c4d0SAlistair Popple
20254f9c4d0SAlistair Popple /*
20354f9c4d0SAlistair Popple * The BT frames start with the message length, which does not
20454f9c4d0SAlistair Popple * include the length byte.
20554f9c4d0SAlistair Popple */
20654f9c4d0SAlistair Popple kbuffer[0] = bt_read(bt_bmc);
20754f9c4d0SAlistair Popple len = kbuffer[0];
20854f9c4d0SAlistair Popple
20954f9c4d0SAlistair Popple /* We pass the length back to userspace as well */
21054f9c4d0SAlistair Popple if (len + 1 > count)
21154f9c4d0SAlistair Popple len = count - 1;
21254f9c4d0SAlistair Popple
21354f9c4d0SAlistair Popple while (len) {
21454f9c4d0SAlistair Popple nread = min_t(ssize_t, len, sizeof(kbuffer) - len_byte);
21554f9c4d0SAlistair Popple
21654f9c4d0SAlistair Popple bt_readn(bt_bmc, kbuffer + len_byte, nread);
21754f9c4d0SAlistair Popple
21854f9c4d0SAlistair Popple if (copy_to_user(buf, kbuffer, nread + len_byte)) {
21954f9c4d0SAlistair Popple ret = -EFAULT;
22054f9c4d0SAlistair Popple break;
22154f9c4d0SAlistair Popple }
22254f9c4d0SAlistair Popple len -= nread;
22354f9c4d0SAlistair Popple buf += nread + len_byte;
22454f9c4d0SAlistair Popple ret += nread + len_byte;
22554f9c4d0SAlistair Popple len_byte = 0;
22654f9c4d0SAlistair Popple }
22754f9c4d0SAlistair Popple
22854f9c4d0SAlistair Popple clr_b_busy(bt_bmc);
22954f9c4d0SAlistair Popple
23054f9c4d0SAlistair Popple out_unlock:
23154f9c4d0SAlistair Popple mutex_unlock(&bt_bmc->mutex);
23254f9c4d0SAlistair Popple return ret;
23354f9c4d0SAlistair Popple }
23454f9c4d0SAlistair Popple
23554f9c4d0SAlistair Popple /*
23654f9c4d0SAlistair Popple * BT Message response format :
23754f9c4d0SAlistair Popple *
23854f9c4d0SAlistair Popple * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6:N
23954f9c4d0SAlistair Popple * Length NetFn/LUN Seq Cmd Code Data
24054f9c4d0SAlistair Popple */
bt_bmc_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)24154f9c4d0SAlistair Popple static ssize_t bt_bmc_write(struct file *file, const char __user *buf,
24254f9c4d0SAlistair Popple size_t count, loff_t *ppos)
24354f9c4d0SAlistair Popple {
24454f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = file_bt_bmc(file);
24554f9c4d0SAlistair Popple u8 kbuffer[BT_BMC_BUFFER_SIZE];
24654f9c4d0SAlistair Popple ssize_t ret = 0;
24754f9c4d0SAlistair Popple ssize_t nwritten;
24854f9c4d0SAlistair Popple
24954f9c4d0SAlistair Popple /*
25054f9c4d0SAlistair Popple * send a minimum response size
25154f9c4d0SAlistair Popple */
25254f9c4d0SAlistair Popple if (count < 5)
25354f9c4d0SAlistair Popple return -EINVAL;
25454f9c4d0SAlistair Popple
25554f9c4d0SAlistair Popple WARN_ON(*ppos);
25654f9c4d0SAlistair Popple
25754f9c4d0SAlistair Popple /*
25854f9c4d0SAlistair Popple * There's no interrupt for clearing bmc busy so we have to
25954f9c4d0SAlistair Popple * poll
26054f9c4d0SAlistair Popple */
26154f9c4d0SAlistair Popple if (wait_event_interruptible(bt_bmc->queue,
26254f9c4d0SAlistair Popple !(bt_inb(bt_bmc, BT_CTRL) &
26354f9c4d0SAlistair Popple (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
26454f9c4d0SAlistair Popple return -ERESTARTSYS;
26554f9c4d0SAlistair Popple
26654f9c4d0SAlistair Popple mutex_lock(&bt_bmc->mutex);
26754f9c4d0SAlistair Popple
26854f9c4d0SAlistair Popple if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
26954f9c4d0SAlistair Popple (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
27054f9c4d0SAlistair Popple ret = -EIO;
27154f9c4d0SAlistair Popple goto out_unlock;
27254f9c4d0SAlistair Popple }
27354f9c4d0SAlistair Popple
27454f9c4d0SAlistair Popple clr_wr_ptr(bt_bmc);
27554f9c4d0SAlistair Popple
27654f9c4d0SAlistair Popple while (count) {
27754f9c4d0SAlistair Popple nwritten = min_t(ssize_t, count, sizeof(kbuffer));
27854f9c4d0SAlistair Popple if (copy_from_user(&kbuffer, buf, nwritten)) {
27954f9c4d0SAlistair Popple ret = -EFAULT;
28054f9c4d0SAlistair Popple break;
28154f9c4d0SAlistair Popple }
28254f9c4d0SAlistair Popple
28354f9c4d0SAlistair Popple bt_writen(bt_bmc, kbuffer, nwritten);
28454f9c4d0SAlistair Popple
28554f9c4d0SAlistair Popple count -= nwritten;
28654f9c4d0SAlistair Popple buf += nwritten;
28754f9c4d0SAlistair Popple ret += nwritten;
28854f9c4d0SAlistair Popple }
28954f9c4d0SAlistair Popple
29054f9c4d0SAlistair Popple set_b2h_atn(bt_bmc);
29154f9c4d0SAlistair Popple
29254f9c4d0SAlistair Popple out_unlock:
29354f9c4d0SAlistair Popple mutex_unlock(&bt_bmc->mutex);
29454f9c4d0SAlistair Popple return ret;
29554f9c4d0SAlistair Popple }
29654f9c4d0SAlistair Popple
bt_bmc_ioctl(struct file * file,unsigned int cmd,unsigned long param)29754f9c4d0SAlistair Popple static long bt_bmc_ioctl(struct file *file, unsigned int cmd,
29854f9c4d0SAlistair Popple unsigned long param)
29954f9c4d0SAlistair Popple {
30054f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = file_bt_bmc(file);
30154f9c4d0SAlistair Popple
30254f9c4d0SAlistair Popple switch (cmd) {
30354f9c4d0SAlistair Popple case BT_BMC_IOCTL_SMS_ATN:
30454f9c4d0SAlistair Popple set_sms_atn(bt_bmc);
30554f9c4d0SAlistair Popple return 0;
30654f9c4d0SAlistair Popple }
30754f9c4d0SAlistair Popple return -EINVAL;
30854f9c4d0SAlistair Popple }
30954f9c4d0SAlistair Popple
bt_bmc_release(struct inode * inode,struct file * file)31054f9c4d0SAlistair Popple static int bt_bmc_release(struct inode *inode, struct file *file)
31154f9c4d0SAlistair Popple {
31254f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = file_bt_bmc(file);
31354f9c4d0SAlistair Popple
31454f9c4d0SAlistair Popple atomic_dec(&open_count);
31554f9c4d0SAlistair Popple set_b_busy(bt_bmc);
31654f9c4d0SAlistair Popple return 0;
31754f9c4d0SAlistair Popple }
31854f9c4d0SAlistair Popple
bt_bmc_poll(struct file * file,poll_table * wait)319afc9a42bSAl Viro static __poll_t bt_bmc_poll(struct file *file, poll_table *wait)
32054f9c4d0SAlistair Popple {
32154f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = file_bt_bmc(file);
322afc9a42bSAl Viro __poll_t mask = 0;
32354f9c4d0SAlistair Popple u8 ctrl;
32454f9c4d0SAlistair Popple
32554f9c4d0SAlistair Popple poll_wait(file, &bt_bmc->queue, wait);
32654f9c4d0SAlistair Popple
32754f9c4d0SAlistair Popple ctrl = bt_inb(bt_bmc, BT_CTRL);
32854f9c4d0SAlistair Popple
32954f9c4d0SAlistair Popple if (ctrl & BT_CTRL_H2B_ATN)
330a9a08845SLinus Torvalds mask |= EPOLLIN;
33154f9c4d0SAlistair Popple
33254f9c4d0SAlistair Popple if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
333a9a08845SLinus Torvalds mask |= EPOLLOUT;
33454f9c4d0SAlistair Popple
33554f9c4d0SAlistair Popple return mask;
33654f9c4d0SAlistair Popple }
33754f9c4d0SAlistair Popple
33854f9c4d0SAlistair Popple static const struct file_operations bt_bmc_fops = {
33954f9c4d0SAlistair Popple .owner = THIS_MODULE,
34054f9c4d0SAlistair Popple .open = bt_bmc_open,
34154f9c4d0SAlistair Popple .read = bt_bmc_read,
34254f9c4d0SAlistair Popple .write = bt_bmc_write,
34354f9c4d0SAlistair Popple .release = bt_bmc_release,
34454f9c4d0SAlistair Popple .poll = bt_bmc_poll,
34554f9c4d0SAlistair Popple .unlocked_ioctl = bt_bmc_ioctl,
34654f9c4d0SAlistair Popple };
34754f9c4d0SAlistair Popple
poll_timer(struct timer_list * t)348e99e88a9SKees Cook static void poll_timer(struct timer_list *t)
34954f9c4d0SAlistair Popple {
350e99e88a9SKees Cook struct bt_bmc *bt_bmc = from_timer(bt_bmc, t, poll_timer);
35154f9c4d0SAlistair Popple
35254f9c4d0SAlistair Popple bt_bmc->poll_timer.expires += msecs_to_jiffies(500);
35354f9c4d0SAlistair Popple wake_up(&bt_bmc->queue);
35454f9c4d0SAlistair Popple add_timer(&bt_bmc->poll_timer);
35554f9c4d0SAlistair Popple }
35654f9c4d0SAlistair Popple
bt_bmc_irq(int irq,void * arg)35754f9c4d0SAlistair Popple static irqreturn_t bt_bmc_irq(int irq, void *arg)
35854f9c4d0SAlistair Popple {
35954f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = arg;
36054f9c4d0SAlistair Popple u32 reg;
36154f9c4d0SAlistair Popple
362396c84bbSJoel Stanley reg = readl(bt_bmc->base + BT_CR2);
363eb994594SAndrew Jeffery
36454f9c4d0SAlistair Popple reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
36554f9c4d0SAlistair Popple if (!reg)
36654f9c4d0SAlistair Popple return IRQ_NONE;
36754f9c4d0SAlistair Popple
36854f9c4d0SAlistair Popple /* ack pending IRQs */
369396c84bbSJoel Stanley writel(reg, bt_bmc->base + BT_CR2);
37054f9c4d0SAlistair Popple
37154f9c4d0SAlistair Popple wake_up(&bt_bmc->queue);
37254f9c4d0SAlistair Popple return IRQ_HANDLED;
37354f9c4d0SAlistair Popple }
37454f9c4d0SAlistair Popple
bt_bmc_config_irq(struct bt_bmc * bt_bmc,struct platform_device * pdev)37554f9c4d0SAlistair Popple static int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
37654f9c4d0SAlistair Popple struct platform_device *pdev)
37754f9c4d0SAlistair Popple {
37854f9c4d0SAlistair Popple struct device *dev = &pdev->dev;
37954f9c4d0SAlistair Popple int rc;
380396c84bbSJoel Stanley u32 reg;
38154f9c4d0SAlistair Popple
38249826937STang Bin bt_bmc->irq = platform_get_irq_optional(pdev, 0);
38349826937STang Bin if (bt_bmc->irq < 0)
38449826937STang Bin return bt_bmc->irq;
38554f9c4d0SAlistair Popple
38654f9c4d0SAlistair Popple rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
38754f9c4d0SAlistair Popple DEVICE_NAME, bt_bmc);
38854f9c4d0SAlistair Popple if (rc < 0) {
38954f9c4d0SAlistair Popple dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
39049826937STang Bin bt_bmc->irq = rc;
39154f9c4d0SAlistair Popple return rc;
39254f9c4d0SAlistair Popple }
39354f9c4d0SAlistair Popple
39454f9c4d0SAlistair Popple /*
39554f9c4d0SAlistair Popple * Configure IRQs on the bmc clearing the H2B and HBUSY bits;
39654f9c4d0SAlistair Popple * H2B will be asserted when the bmc has data for us; HBUSY
39754f9c4d0SAlistair Popple * will be cleared (along with B2H) when we can write the next
39854f9c4d0SAlistair Popple * message to the BT buffer
39954f9c4d0SAlistair Popple */
400396c84bbSJoel Stanley reg = readl(bt_bmc->base + BT_CR1);
401396c84bbSJoel Stanley reg |= BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY;
402396c84bbSJoel Stanley writel(reg, bt_bmc->base + BT_CR1);
40354f9c4d0SAlistair Popple
404396c84bbSJoel Stanley return 0;
40554f9c4d0SAlistair Popple }
40654f9c4d0SAlistair Popple
bt_bmc_probe(struct platform_device * pdev)40754f9c4d0SAlistair Popple static int bt_bmc_probe(struct platform_device *pdev)
40854f9c4d0SAlistair Popple {
40954f9c4d0SAlistair Popple struct bt_bmc *bt_bmc;
41054f9c4d0SAlistair Popple struct device *dev;
41154f9c4d0SAlistair Popple int rc;
41254f9c4d0SAlistair Popple
41354f9c4d0SAlistair Popple dev = &pdev->dev;
41454f9c4d0SAlistair Popple dev_info(dev, "Found bt bmc device\n");
41554f9c4d0SAlistair Popple
41654f9c4d0SAlistair Popple bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
41754f9c4d0SAlistair Popple if (!bt_bmc)
41854f9c4d0SAlistair Popple return -ENOMEM;
41954f9c4d0SAlistair Popple
42054f9c4d0SAlistair Popple dev_set_drvdata(&pdev->dev, bt_bmc);
42154f9c4d0SAlistair Popple
422396c84bbSJoel Stanley bt_bmc->base = devm_platform_ioremap_resource(pdev, 0);
423396c84bbSJoel Stanley if (IS_ERR(bt_bmc->base))
424396c84bbSJoel Stanley return PTR_ERR(bt_bmc->base);
42554f9c4d0SAlistair Popple
42654f9c4d0SAlistair Popple mutex_init(&bt_bmc->mutex);
42754f9c4d0SAlistair Popple init_waitqueue_head(&bt_bmc->queue);
42854f9c4d0SAlistair Popple
429fad0319cSZheng Yongjun bt_bmc->miscdev.minor = MISC_DYNAMIC_MINOR;
430fad0319cSZheng Yongjun bt_bmc->miscdev.name = DEVICE_NAME;
431fad0319cSZheng Yongjun bt_bmc->miscdev.fops = &bt_bmc_fops;
43254f9c4d0SAlistair Popple bt_bmc->miscdev.parent = dev;
43354f9c4d0SAlistair Popple rc = misc_register(&bt_bmc->miscdev);
43454f9c4d0SAlistair Popple if (rc) {
43554f9c4d0SAlistair Popple dev_err(dev, "Unable to register misc device\n");
43654f9c4d0SAlistair Popple return rc;
43754f9c4d0SAlistair Popple }
43854f9c4d0SAlistair Popple
43954f9c4d0SAlistair Popple bt_bmc_config_irq(bt_bmc, pdev);
44054f9c4d0SAlistair Popple
44149826937STang Bin if (bt_bmc->irq >= 0) {
44254f9c4d0SAlistair Popple dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
44354f9c4d0SAlistair Popple } else {
44454f9c4d0SAlistair Popple dev_info(dev, "No IRQ; using timer\n");
445e99e88a9SKees Cook timer_setup(&bt_bmc->poll_timer, poll_timer, 0);
44654f9c4d0SAlistair Popple bt_bmc->poll_timer.expires = jiffies + msecs_to_jiffies(10);
44754f9c4d0SAlistair Popple add_timer(&bt_bmc->poll_timer);
44854f9c4d0SAlistair Popple }
44954f9c4d0SAlistair Popple
450396c84bbSJoel Stanley writel((BT_IO_BASE << BT_CR0_IO_BASE) |
45154f9c4d0SAlistair Popple (BT_IRQ << BT_CR0_IRQ) |
45254f9c4d0SAlistair Popple BT_CR0_EN_CLR_SLV_RDP |
45354f9c4d0SAlistair Popple BT_CR0_EN_CLR_SLV_WRP |
454396c84bbSJoel Stanley BT_CR0_ENABLE_IBT,
455396c84bbSJoel Stanley bt_bmc->base + BT_CR0);
45654f9c4d0SAlistair Popple
45754f9c4d0SAlistair Popple clr_b_busy(bt_bmc);
45854f9c4d0SAlistair Popple
45954f9c4d0SAlistair Popple return 0;
46054f9c4d0SAlistair Popple }
46154f9c4d0SAlistair Popple
bt_bmc_remove(struct platform_device * pdev)46254f9c4d0SAlistair Popple static int bt_bmc_remove(struct platform_device *pdev)
46354f9c4d0SAlistair Popple {
46454f9c4d0SAlistair Popple struct bt_bmc *bt_bmc = dev_get_drvdata(&pdev->dev);
46554f9c4d0SAlistair Popple
46654f9c4d0SAlistair Popple misc_deregister(&bt_bmc->miscdev);
46749826937STang Bin if (bt_bmc->irq < 0)
46854f9c4d0SAlistair Popple del_timer_sync(&bt_bmc->poll_timer);
46954f9c4d0SAlistair Popple return 0;
47054f9c4d0SAlistair Popple }
47154f9c4d0SAlistair Popple
47254f9c4d0SAlistair Popple static const struct of_device_id bt_bmc_match[] = {
4731c8018f7SCédric Le Goater { .compatible = "aspeed,ast2400-ibt-bmc" },
4747ecaff77SJoel Stanley { .compatible = "aspeed,ast2500-ibt-bmc" },
475*cd921b9fSJoel Stanley { .compatible = "aspeed,ast2600-ibt-bmc" },
47654f9c4d0SAlistair Popple { },
47754f9c4d0SAlistair Popple };
47854f9c4d0SAlistair Popple
47954f9c4d0SAlistair Popple static struct platform_driver bt_bmc_driver = {
48054f9c4d0SAlistair Popple .driver = {
48154f9c4d0SAlistair Popple .name = DEVICE_NAME,
48254f9c4d0SAlistair Popple .of_match_table = bt_bmc_match,
48354f9c4d0SAlistair Popple },
48454f9c4d0SAlistair Popple .probe = bt_bmc_probe,
48554f9c4d0SAlistair Popple .remove = bt_bmc_remove,
48654f9c4d0SAlistair Popple };
48754f9c4d0SAlistair Popple
48854f9c4d0SAlistair Popple module_platform_driver(bt_bmc_driver);
48954f9c4d0SAlistair Popple
49054f9c4d0SAlistair Popple MODULE_DEVICE_TABLE(of, bt_bmc_match);
49154f9c4d0SAlistair Popple MODULE_LICENSE("GPL");
49254f9c4d0SAlistair Popple MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
4931c8018f7SCédric Le Goater MODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");
494