18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2015-2016, IBM Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/atomic.h>
78c2ecf20Sopenharmony_ci#include <linux/bt-bmc.h>
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
128c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/poll.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci#include <linux/sched.h>
198c2ecf20Sopenharmony_ci#include <linux/timer.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * This is a BMC device used to communicate to the host
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci#define DEVICE_NAME	"ipmi-bt-host"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define BT_IO_BASE	0xe4
278c2ecf20Sopenharmony_ci#define BT_IRQ		10
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define BT_CR0		0x0
308c2ecf20Sopenharmony_ci#define   BT_CR0_IO_BASE		16
318c2ecf20Sopenharmony_ci#define   BT_CR0_IRQ			12
328c2ecf20Sopenharmony_ci#define   BT_CR0_EN_CLR_SLV_RDP		0x8
338c2ecf20Sopenharmony_ci#define   BT_CR0_EN_CLR_SLV_WRP		0x4
348c2ecf20Sopenharmony_ci#define   BT_CR0_ENABLE_IBT		0x1
358c2ecf20Sopenharmony_ci#define BT_CR1		0x4
368c2ecf20Sopenharmony_ci#define   BT_CR1_IRQ_H2B	0x01
378c2ecf20Sopenharmony_ci#define   BT_CR1_IRQ_HBUSY	0x40
388c2ecf20Sopenharmony_ci#define BT_CR2		0x8
398c2ecf20Sopenharmony_ci#define   BT_CR2_IRQ_H2B	0x01
408c2ecf20Sopenharmony_ci#define   BT_CR2_IRQ_HBUSY	0x40
418c2ecf20Sopenharmony_ci#define BT_CR3		0xc
428c2ecf20Sopenharmony_ci#define BT_CTRL		0x10
438c2ecf20Sopenharmony_ci#define   BT_CTRL_B_BUSY		0x80
448c2ecf20Sopenharmony_ci#define   BT_CTRL_H_BUSY		0x40
458c2ecf20Sopenharmony_ci#define   BT_CTRL_OEM0			0x20
468c2ecf20Sopenharmony_ci#define   BT_CTRL_SMS_ATN		0x10
478c2ecf20Sopenharmony_ci#define   BT_CTRL_B2H_ATN		0x08
488c2ecf20Sopenharmony_ci#define   BT_CTRL_H2B_ATN		0x04
498c2ecf20Sopenharmony_ci#define   BT_CTRL_CLR_RD_PTR		0x02
508c2ecf20Sopenharmony_ci#define   BT_CTRL_CLR_WR_PTR		0x01
518c2ecf20Sopenharmony_ci#define BT_BMC2HOST	0x14
528c2ecf20Sopenharmony_ci#define BT_INTMASK	0x18
538c2ecf20Sopenharmony_ci#define   BT_INTMASK_B2H_IRQEN		0x01
548c2ecf20Sopenharmony_ci#define   BT_INTMASK_B2H_IRQ		0x02
558c2ecf20Sopenharmony_ci#define   BT_INTMASK_BMC_HWRST		0x80
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define BT_BMC_BUFFER_SIZE 256
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistruct bt_bmc {
608c2ecf20Sopenharmony_ci	struct device		dev;
618c2ecf20Sopenharmony_ci	struct miscdevice	miscdev;
628c2ecf20Sopenharmony_ci	struct regmap		*map;
638c2ecf20Sopenharmony_ci	int			offset;
648c2ecf20Sopenharmony_ci	int			irq;
658c2ecf20Sopenharmony_ci	wait_queue_head_t	queue;
668c2ecf20Sopenharmony_ci	struct timer_list	poll_timer;
678c2ecf20Sopenharmony_ci	struct mutex		mutex;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic atomic_t open_count = ATOMIC_INIT(0);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic const struct regmap_config bt_regmap_cfg = {
738c2ecf20Sopenharmony_ci	.reg_bits = 32,
748c2ecf20Sopenharmony_ci	.val_bits = 32,
758c2ecf20Sopenharmony_ci	.reg_stride = 4,
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic u8 bt_inb(struct bt_bmc *bt_bmc, int reg)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	uint32_t val = 0;
818c2ecf20Sopenharmony_ci	int rc;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	rc = regmap_read(bt_bmc->map, bt_bmc->offset + reg, &val);
848c2ecf20Sopenharmony_ci	WARN(rc != 0, "regmap_read() failed: %d\n", rc);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return rc == 0 ? (u8) val : 0;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic void bt_outb(struct bt_bmc *bt_bmc, u8 data, int reg)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	int rc;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	rc = regmap_write(bt_bmc->map, bt_bmc->offset + reg, data);
948c2ecf20Sopenharmony_ci	WARN(rc != 0, "regmap_write() failed: %d\n", rc);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic void clr_rd_ptr(struct bt_bmc *bt_bmc)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	bt_outb(bt_bmc, BT_CTRL_CLR_RD_PTR, BT_CTRL);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic void clr_wr_ptr(struct bt_bmc *bt_bmc)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	bt_outb(bt_bmc, BT_CTRL_CLR_WR_PTR, BT_CTRL);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void clr_h2b_atn(struct bt_bmc *bt_bmc)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	bt_outb(bt_bmc, BT_CTRL_H2B_ATN, BT_CTRL);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic void set_b_busy(struct bt_bmc *bt_bmc)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	if (!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY))
1158c2ecf20Sopenharmony_ci		bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic void clr_b_busy(struct bt_bmc *bt_bmc)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	if (bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY)
1218c2ecf20Sopenharmony_ci		bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void set_b2h_atn(struct bt_bmc *bt_bmc)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	bt_outb(bt_bmc, BT_CTRL_B2H_ATN, BT_CTRL);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic u8 bt_read(struct bt_bmc *bt_bmc)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	return bt_inb(bt_bmc, BT_BMC2HOST);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic ssize_t bt_readn(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	int i;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
1398c2ecf20Sopenharmony_ci		buf[i] = bt_read(bt_bmc);
1408c2ecf20Sopenharmony_ci	return n;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic void bt_write(struct bt_bmc *bt_bmc, u8 c)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	bt_outb(bt_bmc, c, BT_BMC2HOST);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic ssize_t bt_writen(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	int i;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
1538c2ecf20Sopenharmony_ci		bt_write(bt_bmc, buf[i]);
1548c2ecf20Sopenharmony_ci	return n;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic void set_sms_atn(struct bt_bmc *bt_bmc)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	bt_outb(bt_bmc, BT_CTRL_SMS_ATN, BT_CTRL);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic struct bt_bmc *file_bt_bmc(struct file *file)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return container_of(file->private_data, struct bt_bmc, miscdev);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int bt_bmc_open(struct inode *inode, struct file *file)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = file_bt_bmc(file);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (atomic_inc_return(&open_count) == 1) {
1728c2ecf20Sopenharmony_ci		clr_b_busy(bt_bmc);
1738c2ecf20Sopenharmony_ci		return 0;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	atomic_dec(&open_count);
1778c2ecf20Sopenharmony_ci	return -EBUSY;
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/*
1818c2ecf20Sopenharmony_ci * The BT (Block Transfer) interface means that entire messages are
1828c2ecf20Sopenharmony_ci * buffered by the host before a notification is sent to the BMC that
1838c2ecf20Sopenharmony_ci * there is data to be read. The first byte is the length and the
1848c2ecf20Sopenharmony_ci * message data follows. The read operation just tries to capture the
1858c2ecf20Sopenharmony_ci * whole before returning it to userspace.
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * BT Message format :
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci *    Byte 1  Byte 2     Byte 3  Byte 4  Byte 5:N
1908c2ecf20Sopenharmony_ci *    Length  NetFn/LUN  Seq     Cmd     Data
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci */
1938c2ecf20Sopenharmony_cistatic ssize_t bt_bmc_read(struct file *file, char __user *buf,
1948c2ecf20Sopenharmony_ci			   size_t count, loff_t *ppos)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = file_bt_bmc(file);
1978c2ecf20Sopenharmony_ci	u8 len;
1988c2ecf20Sopenharmony_ci	int len_byte = 1;
1998c2ecf20Sopenharmony_ci	u8 kbuffer[BT_BMC_BUFFER_SIZE];
2008c2ecf20Sopenharmony_ci	ssize_t ret = 0;
2018c2ecf20Sopenharmony_ci	ssize_t nread;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	WARN_ON(*ppos);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (wait_event_interruptible(bt_bmc->queue,
2068c2ecf20Sopenharmony_ci				     bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
2078c2ecf20Sopenharmony_ci		return -ERESTARTSYS;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	mutex_lock(&bt_bmc->mutex);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
2128c2ecf20Sopenharmony_ci		ret = -EIO;
2138c2ecf20Sopenharmony_ci		goto out_unlock;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	set_b_busy(bt_bmc);
2178c2ecf20Sopenharmony_ci	clr_h2b_atn(bt_bmc);
2188c2ecf20Sopenharmony_ci	clr_rd_ptr(bt_bmc);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/*
2218c2ecf20Sopenharmony_ci	 * The BT frames start with the message length, which does not
2228c2ecf20Sopenharmony_ci	 * include the length byte.
2238c2ecf20Sopenharmony_ci	 */
2248c2ecf20Sopenharmony_ci	kbuffer[0] = bt_read(bt_bmc);
2258c2ecf20Sopenharmony_ci	len = kbuffer[0];
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* We pass the length back to userspace as well */
2288c2ecf20Sopenharmony_ci	if (len + 1 > count)
2298c2ecf20Sopenharmony_ci		len = count - 1;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	while (len) {
2328c2ecf20Sopenharmony_ci		nread = min_t(ssize_t, len, sizeof(kbuffer) - len_byte);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		bt_readn(bt_bmc, kbuffer + len_byte, nread);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci		if (copy_to_user(buf, kbuffer, nread + len_byte)) {
2378c2ecf20Sopenharmony_ci			ret = -EFAULT;
2388c2ecf20Sopenharmony_ci			break;
2398c2ecf20Sopenharmony_ci		}
2408c2ecf20Sopenharmony_ci		len -= nread;
2418c2ecf20Sopenharmony_ci		buf += nread + len_byte;
2428c2ecf20Sopenharmony_ci		ret += nread + len_byte;
2438c2ecf20Sopenharmony_ci		len_byte = 0;
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	clr_b_busy(bt_bmc);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ciout_unlock:
2498c2ecf20Sopenharmony_ci	mutex_unlock(&bt_bmc->mutex);
2508c2ecf20Sopenharmony_ci	return ret;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/*
2548c2ecf20Sopenharmony_ci * BT Message response format :
2558c2ecf20Sopenharmony_ci *
2568c2ecf20Sopenharmony_ci *    Byte 1  Byte 2     Byte 3  Byte 4  Byte 5  Byte 6:N
2578c2ecf20Sopenharmony_ci *    Length  NetFn/LUN  Seq     Cmd     Code    Data
2588c2ecf20Sopenharmony_ci */
2598c2ecf20Sopenharmony_cistatic ssize_t bt_bmc_write(struct file *file, const char __user *buf,
2608c2ecf20Sopenharmony_ci			    size_t count, loff_t *ppos)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = file_bt_bmc(file);
2638c2ecf20Sopenharmony_ci	u8 kbuffer[BT_BMC_BUFFER_SIZE];
2648c2ecf20Sopenharmony_ci	ssize_t ret = 0;
2658c2ecf20Sopenharmony_ci	ssize_t nwritten;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/*
2688c2ecf20Sopenharmony_ci	 * send a minimum response size
2698c2ecf20Sopenharmony_ci	 */
2708c2ecf20Sopenharmony_ci	if (count < 5)
2718c2ecf20Sopenharmony_ci		return -EINVAL;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	WARN_ON(*ppos);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * There's no interrupt for clearing bmc busy so we have to
2778c2ecf20Sopenharmony_ci	 * poll
2788c2ecf20Sopenharmony_ci	 */
2798c2ecf20Sopenharmony_ci	if (wait_event_interruptible(bt_bmc->queue,
2808c2ecf20Sopenharmony_ci				     !(bt_inb(bt_bmc, BT_CTRL) &
2818c2ecf20Sopenharmony_ci				       (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
2828c2ecf20Sopenharmony_ci		return -ERESTARTSYS;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	mutex_lock(&bt_bmc->mutex);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
2878c2ecf20Sopenharmony_ci		     (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
2888c2ecf20Sopenharmony_ci		ret = -EIO;
2898c2ecf20Sopenharmony_ci		goto out_unlock;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	clr_wr_ptr(bt_bmc);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	while (count) {
2958c2ecf20Sopenharmony_ci		nwritten = min_t(ssize_t, count, sizeof(kbuffer));
2968c2ecf20Sopenharmony_ci		if (copy_from_user(&kbuffer, buf, nwritten)) {
2978c2ecf20Sopenharmony_ci			ret = -EFAULT;
2988c2ecf20Sopenharmony_ci			break;
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		bt_writen(bt_bmc, kbuffer, nwritten);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci		count -= nwritten;
3048c2ecf20Sopenharmony_ci		buf += nwritten;
3058c2ecf20Sopenharmony_ci		ret += nwritten;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	set_b2h_atn(bt_bmc);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ciout_unlock:
3118c2ecf20Sopenharmony_ci	mutex_unlock(&bt_bmc->mutex);
3128c2ecf20Sopenharmony_ci	return ret;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic long bt_bmc_ioctl(struct file *file, unsigned int cmd,
3168c2ecf20Sopenharmony_ci			 unsigned long param)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = file_bt_bmc(file);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	switch (cmd) {
3218c2ecf20Sopenharmony_ci	case BT_BMC_IOCTL_SMS_ATN:
3228c2ecf20Sopenharmony_ci		set_sms_atn(bt_bmc);
3238c2ecf20Sopenharmony_ci		return 0;
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci	return -EINVAL;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int bt_bmc_release(struct inode *inode, struct file *file)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = file_bt_bmc(file);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	atomic_dec(&open_count);
3338c2ecf20Sopenharmony_ci	set_b_busy(bt_bmc);
3348c2ecf20Sopenharmony_ci	return 0;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic __poll_t bt_bmc_poll(struct file *file, poll_table *wait)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = file_bt_bmc(file);
3408c2ecf20Sopenharmony_ci	__poll_t mask = 0;
3418c2ecf20Sopenharmony_ci	u8 ctrl;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	poll_wait(file, &bt_bmc->queue, wait);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	ctrl = bt_inb(bt_bmc, BT_CTRL);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (ctrl & BT_CTRL_H2B_ATN)
3488c2ecf20Sopenharmony_ci		mask |= EPOLLIN;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
3518c2ecf20Sopenharmony_ci		mask |= EPOLLOUT;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return mask;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic const struct file_operations bt_bmc_fops = {
3578c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
3588c2ecf20Sopenharmony_ci	.open		= bt_bmc_open,
3598c2ecf20Sopenharmony_ci	.read		= bt_bmc_read,
3608c2ecf20Sopenharmony_ci	.write		= bt_bmc_write,
3618c2ecf20Sopenharmony_ci	.release	= bt_bmc_release,
3628c2ecf20Sopenharmony_ci	.poll		= bt_bmc_poll,
3638c2ecf20Sopenharmony_ci	.unlocked_ioctl	= bt_bmc_ioctl,
3648c2ecf20Sopenharmony_ci};
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic void poll_timer(struct timer_list *t)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = from_timer(bt_bmc, t, poll_timer);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	bt_bmc->poll_timer.expires += msecs_to_jiffies(500);
3718c2ecf20Sopenharmony_ci	wake_up(&bt_bmc->queue);
3728c2ecf20Sopenharmony_ci	add_timer(&bt_bmc->poll_timer);
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic irqreturn_t bt_bmc_irq(int irq, void *arg)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = arg;
3788c2ecf20Sopenharmony_ci	u32 reg;
3798c2ecf20Sopenharmony_ci	int rc;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	rc = regmap_read(bt_bmc->map, bt_bmc->offset + BT_CR2, &reg);
3828c2ecf20Sopenharmony_ci	if (rc)
3838c2ecf20Sopenharmony_ci		return IRQ_NONE;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
3868c2ecf20Sopenharmony_ci	if (!reg)
3878c2ecf20Sopenharmony_ci		return IRQ_NONE;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	/* ack pending IRQs */
3908c2ecf20Sopenharmony_ci	regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR2, reg);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	wake_up(&bt_bmc->queue);
3938c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
3978c2ecf20Sopenharmony_ci			     struct platform_device *pdev)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
4008c2ecf20Sopenharmony_ci	int rc;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	bt_bmc->irq = platform_get_irq_optional(pdev, 0);
4038c2ecf20Sopenharmony_ci	if (bt_bmc->irq < 0)
4048c2ecf20Sopenharmony_ci		return bt_bmc->irq;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
4078c2ecf20Sopenharmony_ci			      DEVICE_NAME, bt_bmc);
4088c2ecf20Sopenharmony_ci	if (rc < 0) {
4098c2ecf20Sopenharmony_ci		dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
4108c2ecf20Sopenharmony_ci		bt_bmc->irq = rc;
4118c2ecf20Sopenharmony_ci		return rc;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/*
4158c2ecf20Sopenharmony_ci	 * Configure IRQs on the bmc clearing the H2B and HBUSY bits;
4168c2ecf20Sopenharmony_ci	 * H2B will be asserted when the bmc has data for us; HBUSY
4178c2ecf20Sopenharmony_ci	 * will be cleared (along with B2H) when we can write the next
4188c2ecf20Sopenharmony_ci	 * message to the BT buffer
4198c2ecf20Sopenharmony_ci	 */
4208c2ecf20Sopenharmony_ci	rc = regmap_update_bits(bt_bmc->map, bt_bmc->offset + BT_CR1,
4218c2ecf20Sopenharmony_ci				(BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY),
4228c2ecf20Sopenharmony_ci				(BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY));
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return rc;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic int bt_bmc_probe(struct platform_device *pdev)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc;
4308c2ecf20Sopenharmony_ci	struct device *dev;
4318c2ecf20Sopenharmony_ci	int rc;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	dev = &pdev->dev;
4348c2ecf20Sopenharmony_ci	dev_info(dev, "Found bt bmc device\n");
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
4378c2ecf20Sopenharmony_ci	if (!bt_bmc)
4388c2ecf20Sopenharmony_ci		return -ENOMEM;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	dev_set_drvdata(&pdev->dev, bt_bmc);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	bt_bmc->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
4438c2ecf20Sopenharmony_ci	if (IS_ERR(bt_bmc->map)) {
4448c2ecf20Sopenharmony_ci		void __iomem *base;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		/*
4478c2ecf20Sopenharmony_ci		 * Assume it's not the MFD-based devicetree description, in
4488c2ecf20Sopenharmony_ci		 * which case generate a regmap ourselves
4498c2ecf20Sopenharmony_ci		 */
4508c2ecf20Sopenharmony_ci		base = devm_platform_ioremap_resource(pdev, 0);
4518c2ecf20Sopenharmony_ci		if (IS_ERR(base))
4528c2ecf20Sopenharmony_ci			return PTR_ERR(base);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		bt_bmc->map = devm_regmap_init_mmio(dev, base, &bt_regmap_cfg);
4558c2ecf20Sopenharmony_ci		bt_bmc->offset = 0;
4568c2ecf20Sopenharmony_ci	} else {
4578c2ecf20Sopenharmony_ci		rc = of_property_read_u32(dev->of_node, "reg", &bt_bmc->offset);
4588c2ecf20Sopenharmony_ci		if (rc)
4598c2ecf20Sopenharmony_ci			return rc;
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	mutex_init(&bt_bmc->mutex);
4638c2ecf20Sopenharmony_ci	init_waitqueue_head(&bt_bmc->queue);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	bt_bmc->miscdev.minor	= MISC_DYNAMIC_MINOR,
4668c2ecf20Sopenharmony_ci	bt_bmc->miscdev.name	= DEVICE_NAME,
4678c2ecf20Sopenharmony_ci	bt_bmc->miscdev.fops	= &bt_bmc_fops,
4688c2ecf20Sopenharmony_ci	bt_bmc->miscdev.parent = dev;
4698c2ecf20Sopenharmony_ci	rc = misc_register(&bt_bmc->miscdev);
4708c2ecf20Sopenharmony_ci	if (rc) {
4718c2ecf20Sopenharmony_ci		dev_err(dev, "Unable to register misc device\n");
4728c2ecf20Sopenharmony_ci		return rc;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	bt_bmc_config_irq(bt_bmc, pdev);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	if (bt_bmc->irq >= 0) {
4788c2ecf20Sopenharmony_ci		dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
4798c2ecf20Sopenharmony_ci	} else {
4808c2ecf20Sopenharmony_ci		dev_info(dev, "No IRQ; using timer\n");
4818c2ecf20Sopenharmony_ci		timer_setup(&bt_bmc->poll_timer, poll_timer, 0);
4828c2ecf20Sopenharmony_ci		bt_bmc->poll_timer.expires = jiffies + msecs_to_jiffies(10);
4838c2ecf20Sopenharmony_ci		add_timer(&bt_bmc->poll_timer);
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR0,
4878c2ecf20Sopenharmony_ci		     (BT_IO_BASE << BT_CR0_IO_BASE) |
4888c2ecf20Sopenharmony_ci		     (BT_IRQ << BT_CR0_IRQ) |
4898c2ecf20Sopenharmony_ci		     BT_CR0_EN_CLR_SLV_RDP |
4908c2ecf20Sopenharmony_ci		     BT_CR0_EN_CLR_SLV_WRP |
4918c2ecf20Sopenharmony_ci		     BT_CR0_ENABLE_IBT);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	clr_b_busy(bt_bmc);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	return 0;
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cistatic int bt_bmc_remove(struct platform_device *pdev)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	struct bt_bmc *bt_bmc = dev_get_drvdata(&pdev->dev);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	misc_deregister(&bt_bmc->miscdev);
5038c2ecf20Sopenharmony_ci	if (bt_bmc->irq < 0)
5048c2ecf20Sopenharmony_ci		del_timer_sync(&bt_bmc->poll_timer);
5058c2ecf20Sopenharmony_ci	return 0;
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic const struct of_device_id bt_bmc_match[] = {
5098c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2400-ibt-bmc" },
5108c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2500-ibt-bmc" },
5118c2ecf20Sopenharmony_ci	{ },
5128c2ecf20Sopenharmony_ci};
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic struct platform_driver bt_bmc_driver = {
5158c2ecf20Sopenharmony_ci	.driver = {
5168c2ecf20Sopenharmony_ci		.name		= DEVICE_NAME,
5178c2ecf20Sopenharmony_ci		.of_match_table = bt_bmc_match,
5188c2ecf20Sopenharmony_ci	},
5198c2ecf20Sopenharmony_ci	.probe = bt_bmc_probe,
5208c2ecf20Sopenharmony_ci	.remove = bt_bmc_remove,
5218c2ecf20Sopenharmony_ci};
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cimodule_platform_driver(bt_bmc_driver);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bt_bmc_match);
5268c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
5288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");
529