0001
0002
0003
0004
0005
0006 #include <linux/atomic.h>
0007 #include <linux/bt-bmc.h>
0008 #include <linux/errno.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/io.h>
0011 #include <linux/miscdevice.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/poll.h>
0016 #include <linux/sched.h>
0017 #include <linux/timer.h>
0018
0019
0020
0021
0022 #define DEVICE_NAME "ipmi-bt-host"
0023
0024 #define BT_IO_BASE 0xe4
0025 #define BT_IRQ 10
0026
0027 #define BT_CR0 0x0
0028 #define BT_CR0_IO_BASE 16
0029 #define BT_CR0_IRQ 12
0030 #define BT_CR0_EN_CLR_SLV_RDP 0x8
0031 #define BT_CR0_EN_CLR_SLV_WRP 0x4
0032 #define BT_CR0_ENABLE_IBT 0x1
0033 #define BT_CR1 0x4
0034 #define BT_CR1_IRQ_H2B 0x01
0035 #define BT_CR1_IRQ_HBUSY 0x40
0036 #define BT_CR2 0x8
0037 #define BT_CR2_IRQ_H2B 0x01
0038 #define BT_CR2_IRQ_HBUSY 0x40
0039 #define BT_CR3 0xc
0040 #define BT_CTRL 0x10
0041 #define BT_CTRL_B_BUSY 0x80
0042 #define BT_CTRL_H_BUSY 0x40
0043 #define BT_CTRL_OEM0 0x20
0044 #define BT_CTRL_SMS_ATN 0x10
0045 #define BT_CTRL_B2H_ATN 0x08
0046 #define BT_CTRL_H2B_ATN 0x04
0047 #define BT_CTRL_CLR_RD_PTR 0x02
0048 #define BT_CTRL_CLR_WR_PTR 0x01
0049 #define BT_BMC2HOST 0x14
0050 #define BT_INTMASK 0x18
0051 #define BT_INTMASK_B2H_IRQEN 0x01
0052 #define BT_INTMASK_B2H_IRQ 0x02
0053 #define BT_INTMASK_BMC_HWRST 0x80
0054
0055 #define BT_BMC_BUFFER_SIZE 256
0056
0057 struct bt_bmc {
0058 struct device dev;
0059 struct miscdevice miscdev;
0060 void __iomem *base;
0061 int irq;
0062 wait_queue_head_t queue;
0063 struct timer_list poll_timer;
0064 struct mutex mutex;
0065 };
0066
0067 static atomic_t open_count = ATOMIC_INIT(0);
0068
0069 static u8 bt_inb(struct bt_bmc *bt_bmc, int reg)
0070 {
0071 return readb(bt_bmc->base + reg);
0072 }
0073
0074 static void bt_outb(struct bt_bmc *bt_bmc, u8 data, int reg)
0075 {
0076 writeb(data, bt_bmc->base + reg);
0077 }
0078
0079 static void clr_rd_ptr(struct bt_bmc *bt_bmc)
0080 {
0081 bt_outb(bt_bmc, BT_CTRL_CLR_RD_PTR, BT_CTRL);
0082 }
0083
0084 static void clr_wr_ptr(struct bt_bmc *bt_bmc)
0085 {
0086 bt_outb(bt_bmc, BT_CTRL_CLR_WR_PTR, BT_CTRL);
0087 }
0088
0089 static void clr_h2b_atn(struct bt_bmc *bt_bmc)
0090 {
0091 bt_outb(bt_bmc, BT_CTRL_H2B_ATN, BT_CTRL);
0092 }
0093
0094 static void set_b_busy(struct bt_bmc *bt_bmc)
0095 {
0096 if (!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY))
0097 bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
0098 }
0099
0100 static void clr_b_busy(struct bt_bmc *bt_bmc)
0101 {
0102 if (bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY)
0103 bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
0104 }
0105
0106 static void set_b2h_atn(struct bt_bmc *bt_bmc)
0107 {
0108 bt_outb(bt_bmc, BT_CTRL_B2H_ATN, BT_CTRL);
0109 }
0110
0111 static u8 bt_read(struct bt_bmc *bt_bmc)
0112 {
0113 return bt_inb(bt_bmc, BT_BMC2HOST);
0114 }
0115
0116 static ssize_t bt_readn(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
0117 {
0118 int i;
0119
0120 for (i = 0; i < n; i++)
0121 buf[i] = bt_read(bt_bmc);
0122 return n;
0123 }
0124
0125 static void bt_write(struct bt_bmc *bt_bmc, u8 c)
0126 {
0127 bt_outb(bt_bmc, c, BT_BMC2HOST);
0128 }
0129
0130 static ssize_t bt_writen(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
0131 {
0132 int i;
0133
0134 for (i = 0; i < n; i++)
0135 bt_write(bt_bmc, buf[i]);
0136 return n;
0137 }
0138
0139 static void set_sms_atn(struct bt_bmc *bt_bmc)
0140 {
0141 bt_outb(bt_bmc, BT_CTRL_SMS_ATN, BT_CTRL);
0142 }
0143
0144 static struct bt_bmc *file_bt_bmc(struct file *file)
0145 {
0146 return container_of(file->private_data, struct bt_bmc, miscdev);
0147 }
0148
0149 static int bt_bmc_open(struct inode *inode, struct file *file)
0150 {
0151 struct bt_bmc *bt_bmc = file_bt_bmc(file);
0152
0153 if (atomic_inc_return(&open_count) == 1) {
0154 clr_b_busy(bt_bmc);
0155 return 0;
0156 }
0157
0158 atomic_dec(&open_count);
0159 return -EBUSY;
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 static ssize_t bt_bmc_read(struct file *file, char __user *buf,
0176 size_t count, loff_t *ppos)
0177 {
0178 struct bt_bmc *bt_bmc = file_bt_bmc(file);
0179 u8 len;
0180 int len_byte = 1;
0181 u8 kbuffer[BT_BMC_BUFFER_SIZE];
0182 ssize_t ret = 0;
0183 ssize_t nread;
0184
0185 WARN_ON(*ppos);
0186
0187 if (wait_event_interruptible(bt_bmc->queue,
0188 bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
0189 return -ERESTARTSYS;
0190
0191 mutex_lock(&bt_bmc->mutex);
0192
0193 if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
0194 ret = -EIO;
0195 goto out_unlock;
0196 }
0197
0198 set_b_busy(bt_bmc);
0199 clr_h2b_atn(bt_bmc);
0200 clr_rd_ptr(bt_bmc);
0201
0202
0203
0204
0205
0206 kbuffer[0] = bt_read(bt_bmc);
0207 len = kbuffer[0];
0208
0209
0210 if (len + 1 > count)
0211 len = count - 1;
0212
0213 while (len) {
0214 nread = min_t(ssize_t, len, sizeof(kbuffer) - len_byte);
0215
0216 bt_readn(bt_bmc, kbuffer + len_byte, nread);
0217
0218 if (copy_to_user(buf, kbuffer, nread + len_byte)) {
0219 ret = -EFAULT;
0220 break;
0221 }
0222 len -= nread;
0223 buf += nread + len_byte;
0224 ret += nread + len_byte;
0225 len_byte = 0;
0226 }
0227
0228 clr_b_busy(bt_bmc);
0229
0230 out_unlock:
0231 mutex_unlock(&bt_bmc->mutex);
0232 return ret;
0233 }
0234
0235
0236
0237
0238
0239
0240
0241 static ssize_t bt_bmc_write(struct file *file, const char __user *buf,
0242 size_t count, loff_t *ppos)
0243 {
0244 struct bt_bmc *bt_bmc = file_bt_bmc(file);
0245 u8 kbuffer[BT_BMC_BUFFER_SIZE];
0246 ssize_t ret = 0;
0247 ssize_t nwritten;
0248
0249
0250
0251
0252 if (count < 5)
0253 return -EINVAL;
0254
0255 WARN_ON(*ppos);
0256
0257
0258
0259
0260
0261 if (wait_event_interruptible(bt_bmc->queue,
0262 !(bt_inb(bt_bmc, BT_CTRL) &
0263 (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
0264 return -ERESTARTSYS;
0265
0266 mutex_lock(&bt_bmc->mutex);
0267
0268 if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
0269 (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
0270 ret = -EIO;
0271 goto out_unlock;
0272 }
0273
0274 clr_wr_ptr(bt_bmc);
0275
0276 while (count) {
0277 nwritten = min_t(ssize_t, count, sizeof(kbuffer));
0278 if (copy_from_user(&kbuffer, buf, nwritten)) {
0279 ret = -EFAULT;
0280 break;
0281 }
0282
0283 bt_writen(bt_bmc, kbuffer, nwritten);
0284
0285 count -= nwritten;
0286 buf += nwritten;
0287 ret += nwritten;
0288 }
0289
0290 set_b2h_atn(bt_bmc);
0291
0292 out_unlock:
0293 mutex_unlock(&bt_bmc->mutex);
0294 return ret;
0295 }
0296
0297 static long bt_bmc_ioctl(struct file *file, unsigned int cmd,
0298 unsigned long param)
0299 {
0300 struct bt_bmc *bt_bmc = file_bt_bmc(file);
0301
0302 switch (cmd) {
0303 case BT_BMC_IOCTL_SMS_ATN:
0304 set_sms_atn(bt_bmc);
0305 return 0;
0306 }
0307 return -EINVAL;
0308 }
0309
0310 static int bt_bmc_release(struct inode *inode, struct file *file)
0311 {
0312 struct bt_bmc *bt_bmc = file_bt_bmc(file);
0313
0314 atomic_dec(&open_count);
0315 set_b_busy(bt_bmc);
0316 return 0;
0317 }
0318
0319 static __poll_t bt_bmc_poll(struct file *file, poll_table *wait)
0320 {
0321 struct bt_bmc *bt_bmc = file_bt_bmc(file);
0322 __poll_t mask = 0;
0323 u8 ctrl;
0324
0325 poll_wait(file, &bt_bmc->queue, wait);
0326
0327 ctrl = bt_inb(bt_bmc, BT_CTRL);
0328
0329 if (ctrl & BT_CTRL_H2B_ATN)
0330 mask |= EPOLLIN;
0331
0332 if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
0333 mask |= EPOLLOUT;
0334
0335 return mask;
0336 }
0337
0338 static const struct file_operations bt_bmc_fops = {
0339 .owner = THIS_MODULE,
0340 .open = bt_bmc_open,
0341 .read = bt_bmc_read,
0342 .write = bt_bmc_write,
0343 .release = bt_bmc_release,
0344 .poll = bt_bmc_poll,
0345 .unlocked_ioctl = bt_bmc_ioctl,
0346 };
0347
0348 static void poll_timer(struct timer_list *t)
0349 {
0350 struct bt_bmc *bt_bmc = from_timer(bt_bmc, t, poll_timer);
0351
0352 bt_bmc->poll_timer.expires += msecs_to_jiffies(500);
0353 wake_up(&bt_bmc->queue);
0354 add_timer(&bt_bmc->poll_timer);
0355 }
0356
0357 static irqreturn_t bt_bmc_irq(int irq, void *arg)
0358 {
0359 struct bt_bmc *bt_bmc = arg;
0360 u32 reg;
0361
0362 reg = readl(bt_bmc->base + BT_CR2);
0363
0364 reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
0365 if (!reg)
0366 return IRQ_NONE;
0367
0368
0369 writel(reg, bt_bmc->base + BT_CR2);
0370
0371 wake_up(&bt_bmc->queue);
0372 return IRQ_HANDLED;
0373 }
0374
0375 static int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
0376 struct platform_device *pdev)
0377 {
0378 struct device *dev = &pdev->dev;
0379 int rc;
0380 u32 reg;
0381
0382 bt_bmc->irq = platform_get_irq_optional(pdev, 0);
0383 if (bt_bmc->irq < 0)
0384 return bt_bmc->irq;
0385
0386 rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
0387 DEVICE_NAME, bt_bmc);
0388 if (rc < 0) {
0389 dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
0390 bt_bmc->irq = rc;
0391 return rc;
0392 }
0393
0394
0395
0396
0397
0398
0399
0400 reg = readl(bt_bmc->base + BT_CR1);
0401 reg |= BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY;
0402 writel(reg, bt_bmc->base + BT_CR1);
0403
0404 return 0;
0405 }
0406
0407 static int bt_bmc_probe(struct platform_device *pdev)
0408 {
0409 struct bt_bmc *bt_bmc;
0410 struct device *dev;
0411 int rc;
0412
0413 dev = &pdev->dev;
0414 dev_info(dev, "Found bt bmc device\n");
0415
0416 bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
0417 if (!bt_bmc)
0418 return -ENOMEM;
0419
0420 dev_set_drvdata(&pdev->dev, bt_bmc);
0421
0422 bt_bmc->base = devm_platform_ioremap_resource(pdev, 0);
0423 if (IS_ERR(bt_bmc->base))
0424 return PTR_ERR(bt_bmc->base);
0425
0426 mutex_init(&bt_bmc->mutex);
0427 init_waitqueue_head(&bt_bmc->queue);
0428
0429 bt_bmc->miscdev.minor = MISC_DYNAMIC_MINOR;
0430 bt_bmc->miscdev.name = DEVICE_NAME;
0431 bt_bmc->miscdev.fops = &bt_bmc_fops;
0432 bt_bmc->miscdev.parent = dev;
0433 rc = misc_register(&bt_bmc->miscdev);
0434 if (rc) {
0435 dev_err(dev, "Unable to register misc device\n");
0436 return rc;
0437 }
0438
0439 bt_bmc_config_irq(bt_bmc, pdev);
0440
0441 if (bt_bmc->irq >= 0) {
0442 dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
0443 } else {
0444 dev_info(dev, "No IRQ; using timer\n");
0445 timer_setup(&bt_bmc->poll_timer, poll_timer, 0);
0446 bt_bmc->poll_timer.expires = jiffies + msecs_to_jiffies(10);
0447 add_timer(&bt_bmc->poll_timer);
0448 }
0449
0450 writel((BT_IO_BASE << BT_CR0_IO_BASE) |
0451 (BT_IRQ << BT_CR0_IRQ) |
0452 BT_CR0_EN_CLR_SLV_RDP |
0453 BT_CR0_EN_CLR_SLV_WRP |
0454 BT_CR0_ENABLE_IBT,
0455 bt_bmc->base + BT_CR0);
0456
0457 clr_b_busy(bt_bmc);
0458
0459 return 0;
0460 }
0461
0462 static int bt_bmc_remove(struct platform_device *pdev)
0463 {
0464 struct bt_bmc *bt_bmc = dev_get_drvdata(&pdev->dev);
0465
0466 misc_deregister(&bt_bmc->miscdev);
0467 if (bt_bmc->irq < 0)
0468 del_timer_sync(&bt_bmc->poll_timer);
0469 return 0;
0470 }
0471
0472 static const struct of_device_id bt_bmc_match[] = {
0473 { .compatible = "aspeed,ast2400-ibt-bmc" },
0474 { .compatible = "aspeed,ast2500-ibt-bmc" },
0475 { .compatible = "aspeed,ast2600-ibt-bmc" },
0476 { },
0477 };
0478
0479 static struct platform_driver bt_bmc_driver = {
0480 .driver = {
0481 .name = DEVICE_NAME,
0482 .of_match_table = bt_bmc_match,
0483 },
0484 .probe = bt_bmc_probe,
0485 .remove = bt_bmc_remove,
0486 };
0487
0488 module_platform_driver(bt_bmc_driver);
0489
0490 MODULE_DEVICE_TABLE(of, bt_bmc_match);
0491 MODULE_LICENSE("GPL");
0492 MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
0493 MODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");