Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / char / ipmi / bt-bmc.c
blobfc9e8891eae36458205624bce3bef9cc84be8f12
1 /*
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.
8 */
10 #include <linux/atomic.h>
11 #include <linux/bt-bmc.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.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
28 #define BT_IRQ 10
30 #define BT_CR0 0x0
31 #define BT_CR0_IO_BASE 16
32 #define BT_CR0_IRQ 12
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
36 #define BT_CR1 0x4
37 #define BT_CR1_IRQ_H2B 0x01
38 #define BT_CR1_IRQ_HBUSY 0x40
39 #define BT_CR2 0x8
40 #define BT_CR2_IRQ_H2B 0x01
41 #define BT_CR2_IRQ_HBUSY 0x40
42 #define BT_CR3 0xc
43 #define BT_CTRL 0x10
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
60 struct bt_bmc {
61 struct device dev;
62 struct miscdevice miscdev;
63 void __iomem *base;
64 int irq;
65 wait_queue_head_t queue;
66 struct timer_list poll_timer;
67 struct mutex mutex;
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)
121 int i;
123 for (i = 0; i < n; i++)
124 buf[i] = bt_read(bt_bmc);
125 return n;
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)
135 int i;
137 for (i = 0; i < n; i++)
138 bt_write(bt_bmc, buf[i]);
139 return n;
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) {
157 clr_b_busy(bt_bmc);
158 return 0;
161 atomic_dec(&open_count);
162 return -EBUSY;
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);
182 u8 len;
183 int len_byte = 1;
184 u8 kbuffer[BT_BMC_BUFFER_SIZE];
185 ssize_t ret = 0;
186 ssize_t nread;
188 if (!access_ok(VERIFY_WRITE, buf, count))
189 return -EFAULT;
191 WARN_ON(*ppos);
193 if (wait_event_interruptible(bt_bmc->queue,
194 bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
195 return -ERESTARTSYS;
197 mutex_lock(&bt_bmc->mutex);
199 if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
200 ret = -EIO;
201 goto out_unlock;
204 set_b_busy(bt_bmc);
205 clr_h2b_atn(bt_bmc);
206 clr_rd_ptr(bt_bmc);
209 * The BT frames start with the message length, which does not
210 * include the length byte.
212 kbuffer[0] = bt_read(bt_bmc);
213 len = kbuffer[0];
215 /* We pass the length back to userspace as well */
216 if (len + 1 > count)
217 len = count - 1;
219 while (len) {
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)) {
225 ret = -EFAULT;
226 break;
228 len -= nread;
229 buf += nread + len_byte;
230 ret += nread + len_byte;
231 len_byte = 0;
234 clr_b_busy(bt_bmc);
236 out_unlock:
237 mutex_unlock(&bt_bmc->mutex);
238 return ret;
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];
252 ssize_t ret = 0;
253 ssize_t nwritten;
256 * send a minimum response size
258 if (count < 5)
259 return -EINVAL;
261 if (!access_ok(VERIFY_READ, buf, count))
262 return -EFAULT;
264 WARN_ON(*ppos);
267 * There's no interrupt for clearing bmc busy so we have to
268 * poll
270 if (wait_event_interruptible(bt_bmc->queue,
271 !(bt_inb(bt_bmc, BT_CTRL) &
272 (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
273 return -ERESTARTSYS;
275 mutex_lock(&bt_bmc->mutex);
277 if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
278 (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
279 ret = -EIO;
280 goto out_unlock;
283 clr_wr_ptr(bt_bmc);
285 while (count) {
286 nwritten = min_t(ssize_t, count, sizeof(kbuffer));
287 if (copy_from_user(&kbuffer, buf, nwritten)) {
288 ret = -EFAULT;
289 break;
292 bt_writen(bt_bmc, kbuffer, nwritten);
294 count -= nwritten;
295 buf += nwritten;
296 ret += nwritten;
299 set_b2h_atn(bt_bmc);
301 out_unlock:
302 mutex_unlock(&bt_bmc->mutex);
303 return ret;
306 static long bt_bmc_ioctl(struct file *file, unsigned int cmd,
307 unsigned long param)
309 struct bt_bmc *bt_bmc = file_bt_bmc(file);
311 switch (cmd) {
312 case BT_BMC_IOCTL_SMS_ATN:
313 set_sms_atn(bt_bmc);
314 return 0;
316 return -EINVAL;
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);
324 set_b_busy(bt_bmc);
325 return 0;
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;
332 u8 ctrl;
334 poll_wait(file, &bt_bmc->queue, wait);
336 ctrl = bt_inb(bt_bmc, BT_CTRL);
338 if (ctrl & BT_CTRL_H2B_ATN)
339 mask |= POLLIN;
341 if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
342 mask |= POLLOUT;
344 return mask;
347 static const struct file_operations bt_bmc_fops = {
348 .owner = THIS_MODULE,
349 .open = bt_bmc_open,
350 .read = bt_bmc_read,
351 .write = bt_bmc_write,
352 .release = bt_bmc_release,
353 .poll = bt_bmc_poll,
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;
369 u32 reg;
371 reg = ioread32(bt_bmc->base + BT_CR2);
372 reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
373 if (!reg)
374 return IRQ_NONE;
376 /* ack pending IRQs */
377 iowrite32(reg, bt_bmc->base + BT_CR2);
379 wake_up(&bt_bmc->queue);
380 return IRQ_HANDLED;
383 static int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
384 struct platform_device *pdev)
386 struct device *dev = &pdev->dev;
387 u32 reg;
388 int rc;
390 bt_bmc->irq = platform_get_irq(pdev, 0);
391 if (!bt_bmc->irq)
392 return -ENODEV;
394 rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
395 DEVICE_NAME, bt_bmc);
396 if (rc < 0) {
397 dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
398 bt_bmc->irq = 0;
399 return rc;
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);
412 return 0;
415 static int bt_bmc_probe(struct platform_device *pdev)
417 struct bt_bmc *bt_bmc;
418 struct device *dev;
419 struct resource *res;
420 int rc;
422 if (!pdev || !pdev->dev.of_node)
423 return -ENODEV;
425 dev = &pdev->dev;
426 dev_info(dev, "Found bt bmc device\n");
428 bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
429 if (!bt_bmc)
430 return -ENOMEM;
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);
447 if (rc) {
448 dev_err(dev, "Unable to register misc device\n");
449 return rc;
452 bt_bmc_config_irq(bt_bmc, pdev);
454 if (bt_bmc->irq) {
455 dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
456 } else {
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 |
468 BT_CR0_ENABLE_IBT,
469 bt_bmc->base + BT_CR0);
471 clr_b_busy(bt_bmc);
473 return 0;
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);
481 if (!bt_bmc->irq)
482 del_timer_sync(&bt_bmc->poll_timer);
483 return 0;
486 static const struct of_device_id bt_bmc_match[] = {
487 { .compatible = "aspeed,ast2400-ibt-bmc" },
488 { },
491 static struct platform_driver bt_bmc_driver = {
492 .driver = {
493 .name = DEVICE_NAME,
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");