]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/char/ipmi/bt-bmc.c
treewide: setup_timer() -> timer_setup()
[mirror_ubuntu-bionic-kernel.git] / drivers / char / ipmi / bt-bmc.c
CommitLineData
54f9c4d0
AP
1/*
2 * Copyright (c) 2015-2016, IBM Corporation.
3 *
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 */
9
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>
eb994594 15#include <linux/mfd/syscon.h>
54f9c4d0
AP
16#include <linux/miscdevice.h>
17#include <linux/module.h>
eb994594 18#include <linux/of.h>
54f9c4d0
AP
19#include <linux/platform_device.h>
20#include <linux/poll.h>
eb994594 21#include <linux/regmap.h>
54f9c4d0
AP
22#include <linux/sched.h>
23#include <linux/timer.h>
24
25/*
26 * This is a BMC device used to communicate to the host
27 */
28#define DEVICE_NAME "ipmi-bt-host"
29
30#define BT_IO_BASE 0xe4
31#define BT_IRQ 10
32
33#define BT_CR0 0x0
34#define BT_CR0_IO_BASE 16
35#define BT_CR0_IRQ 12
36#define BT_CR0_EN_CLR_SLV_RDP 0x8
37#define BT_CR0_EN_CLR_SLV_WRP 0x4
38#define BT_CR0_ENABLE_IBT 0x1
39#define BT_CR1 0x4
40#define BT_CR1_IRQ_H2B 0x01
41#define BT_CR1_IRQ_HBUSY 0x40
42#define BT_CR2 0x8
43#define BT_CR2_IRQ_H2B 0x01
44#define BT_CR2_IRQ_HBUSY 0x40
45#define BT_CR3 0xc
46#define BT_CTRL 0x10
47#define BT_CTRL_B_BUSY 0x80
48#define BT_CTRL_H_BUSY 0x40
49#define BT_CTRL_OEM0 0x20
50#define BT_CTRL_SMS_ATN 0x10
51#define BT_CTRL_B2H_ATN 0x08
52#define BT_CTRL_H2B_ATN 0x04
53#define BT_CTRL_CLR_RD_PTR 0x02
54#define BT_CTRL_CLR_WR_PTR 0x01
55#define BT_BMC2HOST 0x14
56#define BT_INTMASK 0x18
57#define BT_INTMASK_B2H_IRQEN 0x01
58#define BT_INTMASK_B2H_IRQ 0x02
59#define BT_INTMASK_BMC_HWRST 0x80
60
61#define BT_BMC_BUFFER_SIZE 256
62
63struct bt_bmc {
64 struct device dev;
65 struct miscdevice miscdev;
eb994594
AJ
66 struct regmap *map;
67 int offset;
54f9c4d0
AP
68 int irq;
69 wait_queue_head_t queue;
70 struct timer_list poll_timer;
71 struct mutex mutex;
72};
73
74static atomic_t open_count = ATOMIC_INIT(0);
75
eb994594
AJ
76static const struct regmap_config bt_regmap_cfg = {
77 .reg_bits = 32,
78 .val_bits = 32,
79 .reg_stride = 4,
80};
81
54f9c4d0
AP
82static u8 bt_inb(struct bt_bmc *bt_bmc, int reg)
83{
eb994594
AJ
84 uint32_t val = 0;
85 int rc;
86
87 rc = regmap_read(bt_bmc->map, bt_bmc->offset + reg, &val);
88 WARN(rc != 0, "regmap_read() failed: %d\n", rc);
89
90 return rc == 0 ? (u8) val : 0;
54f9c4d0
AP
91}
92
93static void bt_outb(struct bt_bmc *bt_bmc, u8 data, int reg)
94{
eb994594
AJ
95 int rc;
96
97 rc = regmap_write(bt_bmc->map, bt_bmc->offset + reg, data);
98 WARN(rc != 0, "regmap_write() failed: %d\n", rc);
54f9c4d0
AP
99}
100
101static void clr_rd_ptr(struct bt_bmc *bt_bmc)
102{
103 bt_outb(bt_bmc, BT_CTRL_CLR_RD_PTR, BT_CTRL);
104}
105
106static void clr_wr_ptr(struct bt_bmc *bt_bmc)
107{
108 bt_outb(bt_bmc, BT_CTRL_CLR_WR_PTR, BT_CTRL);
109}
110
111static void clr_h2b_atn(struct bt_bmc *bt_bmc)
112{
113 bt_outb(bt_bmc, BT_CTRL_H2B_ATN, BT_CTRL);
114}
115
116static void set_b_busy(struct bt_bmc *bt_bmc)
117{
118 if (!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY))
119 bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
120}
121
122static void clr_b_busy(struct bt_bmc *bt_bmc)
123{
124 if (bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY)
125 bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
126}
127
128static void set_b2h_atn(struct bt_bmc *bt_bmc)
129{
130 bt_outb(bt_bmc, BT_CTRL_B2H_ATN, BT_CTRL);
131}
132
133static u8 bt_read(struct bt_bmc *bt_bmc)
134{
135 return bt_inb(bt_bmc, BT_BMC2HOST);
136}
137
138static ssize_t bt_readn(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
139{
140 int i;
141
142 for (i = 0; i < n; i++)
143 buf[i] = bt_read(bt_bmc);
144 return n;
145}
146
147static void bt_write(struct bt_bmc *bt_bmc, u8 c)
148{
149 bt_outb(bt_bmc, c, BT_BMC2HOST);
150}
151
152static ssize_t bt_writen(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
153{
154 int i;
155
156 for (i = 0; i < n; i++)
157 bt_write(bt_bmc, buf[i]);
158 return n;
159}
160
161static void set_sms_atn(struct bt_bmc *bt_bmc)
162{
163 bt_outb(bt_bmc, BT_CTRL_SMS_ATN, BT_CTRL);
164}
165
166static struct bt_bmc *file_bt_bmc(struct file *file)
167{
168 return container_of(file->private_data, struct bt_bmc, miscdev);
169}
170
171static int bt_bmc_open(struct inode *inode, struct file *file)
172{
173 struct bt_bmc *bt_bmc = file_bt_bmc(file);
174
175 if (atomic_inc_return(&open_count) == 1) {
176 clr_b_busy(bt_bmc);
177 return 0;
178 }
179
180 atomic_dec(&open_count);
181 return -EBUSY;
182}
183
184/*
185 * The BT (Block Transfer) interface means that entire messages are
186 * buffered by the host before a notification is sent to the BMC that
187 * there is data to be read. The first byte is the length and the
188 * message data follows. The read operation just tries to capture the
189 * whole before returning it to userspace.
190 *
191 * BT Message format :
192 *
193 * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5:N
194 * Length NetFn/LUN Seq Cmd Data
195 *
196 */
197static ssize_t bt_bmc_read(struct file *file, char __user *buf,
198 size_t count, loff_t *ppos)
199{
200 struct bt_bmc *bt_bmc = file_bt_bmc(file);
201 u8 len;
202 int len_byte = 1;
203 u8 kbuffer[BT_BMC_BUFFER_SIZE];
204 ssize_t ret = 0;
205 ssize_t nread;
206
54f9c4d0
AP
207 WARN_ON(*ppos);
208
209 if (wait_event_interruptible(bt_bmc->queue,
210 bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
211 return -ERESTARTSYS;
212
213 mutex_lock(&bt_bmc->mutex);
214
215 if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
216 ret = -EIO;
217 goto out_unlock;
218 }
219
220 set_b_busy(bt_bmc);
221 clr_h2b_atn(bt_bmc);
222 clr_rd_ptr(bt_bmc);
223
224 /*
225 * The BT frames start with the message length, which does not
226 * include the length byte.
227 */
228 kbuffer[0] = bt_read(bt_bmc);
229 len = kbuffer[0];
230
231 /* We pass the length back to userspace as well */
232 if (len + 1 > count)
233 len = count - 1;
234
235 while (len) {
236 nread = min_t(ssize_t, len, sizeof(kbuffer) - len_byte);
237
238 bt_readn(bt_bmc, kbuffer + len_byte, nread);
239
240 if (copy_to_user(buf, kbuffer, nread + len_byte)) {
241 ret = -EFAULT;
242 break;
243 }
244 len -= nread;
245 buf += nread + len_byte;
246 ret += nread + len_byte;
247 len_byte = 0;
248 }
249
250 clr_b_busy(bt_bmc);
251
252out_unlock:
253 mutex_unlock(&bt_bmc->mutex);
254 return ret;
255}
256
257/*
258 * BT Message response format :
259 *
260 * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6:N
261 * Length NetFn/LUN Seq Cmd Code Data
262 */
263static ssize_t bt_bmc_write(struct file *file, const char __user *buf,
264 size_t count, loff_t *ppos)
265{
266 struct bt_bmc *bt_bmc = file_bt_bmc(file);
267 u8 kbuffer[BT_BMC_BUFFER_SIZE];
268 ssize_t ret = 0;
269 ssize_t nwritten;
270
271 /*
272 * send a minimum response size
273 */
274 if (count < 5)
275 return -EINVAL;
276
54f9c4d0
AP
277 WARN_ON(*ppos);
278
279 /*
280 * There's no interrupt for clearing bmc busy so we have to
281 * poll
282 */
283 if (wait_event_interruptible(bt_bmc->queue,
284 !(bt_inb(bt_bmc, BT_CTRL) &
285 (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
286 return -ERESTARTSYS;
287
288 mutex_lock(&bt_bmc->mutex);
289
290 if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
291 (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
292 ret = -EIO;
293 goto out_unlock;
294 }
295
296 clr_wr_ptr(bt_bmc);
297
298 while (count) {
299 nwritten = min_t(ssize_t, count, sizeof(kbuffer));
300 if (copy_from_user(&kbuffer, buf, nwritten)) {
301 ret = -EFAULT;
302 break;
303 }
304
305 bt_writen(bt_bmc, kbuffer, nwritten);
306
307 count -= nwritten;
308 buf += nwritten;
309 ret += nwritten;
310 }
311
312 set_b2h_atn(bt_bmc);
313
314out_unlock:
315 mutex_unlock(&bt_bmc->mutex);
316 return ret;
317}
318
319static long bt_bmc_ioctl(struct file *file, unsigned int cmd,
320 unsigned long param)
321{
322 struct bt_bmc *bt_bmc = file_bt_bmc(file);
323
324 switch (cmd) {
325 case BT_BMC_IOCTL_SMS_ATN:
326 set_sms_atn(bt_bmc);
327 return 0;
328 }
329 return -EINVAL;
330}
331
332static int bt_bmc_release(struct inode *inode, struct file *file)
333{
334 struct bt_bmc *bt_bmc = file_bt_bmc(file);
335
336 atomic_dec(&open_count);
337 set_b_busy(bt_bmc);
338 return 0;
339}
340
341static unsigned int bt_bmc_poll(struct file *file, poll_table *wait)
342{
343 struct bt_bmc *bt_bmc = file_bt_bmc(file);
344 unsigned int mask = 0;
345 u8 ctrl;
346
347 poll_wait(file, &bt_bmc->queue, wait);
348
349 ctrl = bt_inb(bt_bmc, BT_CTRL);
350
351 if (ctrl & BT_CTRL_H2B_ATN)
352 mask |= POLLIN;
353
354 if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
355 mask |= POLLOUT;
356
357 return mask;
358}
359
360static const struct file_operations bt_bmc_fops = {
361 .owner = THIS_MODULE,
362 .open = bt_bmc_open,
363 .read = bt_bmc_read,
364 .write = bt_bmc_write,
365 .release = bt_bmc_release,
366 .poll = bt_bmc_poll,
367 .unlocked_ioctl = bt_bmc_ioctl,
368};
369
e99e88a9 370static void poll_timer(struct timer_list *t)
54f9c4d0 371{
e99e88a9 372 struct bt_bmc *bt_bmc = from_timer(bt_bmc, t, poll_timer);
54f9c4d0
AP
373
374 bt_bmc->poll_timer.expires += msecs_to_jiffies(500);
375 wake_up(&bt_bmc->queue);
376 add_timer(&bt_bmc->poll_timer);
377}
378
379static irqreturn_t bt_bmc_irq(int irq, void *arg)
380{
381 struct bt_bmc *bt_bmc = arg;
382 u32 reg;
eb994594
AJ
383 int rc;
384
385 rc = regmap_read(bt_bmc->map, bt_bmc->offset + BT_CR2, &reg);
386 if (rc)
387 return IRQ_NONE;
54f9c4d0 388
54f9c4d0
AP
389 reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
390 if (!reg)
391 return IRQ_NONE;
392
393 /* ack pending IRQs */
eb994594 394 regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR2, reg);
54f9c4d0
AP
395
396 wake_up(&bt_bmc->queue);
397 return IRQ_HANDLED;
398}
399
400static int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
401 struct platform_device *pdev)
402{
403 struct device *dev = &pdev->dev;
54f9c4d0
AP
404 int rc;
405
406 bt_bmc->irq = platform_get_irq(pdev, 0);
407 if (!bt_bmc->irq)
408 return -ENODEV;
409
410 rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
411 DEVICE_NAME, bt_bmc);
412 if (rc < 0) {
413 dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
414 bt_bmc->irq = 0;
415 return rc;
416 }
417
418 /*
419 * Configure IRQs on the bmc clearing the H2B and HBUSY bits;
420 * H2B will be asserted when the bmc has data for us; HBUSY
421 * will be cleared (along with B2H) when we can write the next
422 * message to the BT buffer
423 */
eb994594
AJ
424 rc = regmap_update_bits(bt_bmc->map, bt_bmc->offset + BT_CR1,
425 (BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY),
426 (BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY));
54f9c4d0 427
eb994594 428 return rc;
54f9c4d0
AP
429}
430
431static int bt_bmc_probe(struct platform_device *pdev)
432{
433 struct bt_bmc *bt_bmc;
434 struct device *dev;
54f9c4d0
AP
435 int rc;
436
437 if (!pdev || !pdev->dev.of_node)
438 return -ENODEV;
439
440 dev = &pdev->dev;
441 dev_info(dev, "Found bt bmc device\n");
442
443 bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
444 if (!bt_bmc)
445 return -ENOMEM;
446
447 dev_set_drvdata(&pdev->dev, bt_bmc);
448
eb994594
AJ
449 bt_bmc->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
450 if (IS_ERR(bt_bmc->map)) {
451 struct resource *res;
452 void __iomem *base;
453
454 /*
455 * Assume it's not the MFD-based devicetree description, in
456 * which case generate a regmap ourselves
457 */
458 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
459 base = devm_ioremap_resource(&pdev->dev, res);
460 if (IS_ERR(base))
461 return PTR_ERR(base);
462
463 bt_bmc->map = devm_regmap_init_mmio(dev, base, &bt_regmap_cfg);
464 bt_bmc->offset = 0;
465 } else {
466 rc = of_property_read_u32(dev->of_node, "reg", &bt_bmc->offset);
467 if (rc)
468 return rc;
469 }
54f9c4d0
AP
470
471 mutex_init(&bt_bmc->mutex);
472 init_waitqueue_head(&bt_bmc->queue);
473
474 bt_bmc->miscdev.minor = MISC_DYNAMIC_MINOR,
475 bt_bmc->miscdev.name = DEVICE_NAME,
476 bt_bmc->miscdev.fops = &bt_bmc_fops,
477 bt_bmc->miscdev.parent = dev;
478 rc = misc_register(&bt_bmc->miscdev);
479 if (rc) {
480 dev_err(dev, "Unable to register misc device\n");
481 return rc;
482 }
483
484 bt_bmc_config_irq(bt_bmc, pdev);
485
486 if (bt_bmc->irq) {
487 dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
488 } else {
489 dev_info(dev, "No IRQ; using timer\n");
e99e88a9 490 timer_setup(&bt_bmc->poll_timer, poll_timer, 0);
54f9c4d0
AP
491 bt_bmc->poll_timer.expires = jiffies + msecs_to_jiffies(10);
492 add_timer(&bt_bmc->poll_timer);
493 }
494
eb994594
AJ
495 regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR0,
496 (BT_IO_BASE << BT_CR0_IO_BASE) |
497 (BT_IRQ << BT_CR0_IRQ) |
498 BT_CR0_EN_CLR_SLV_RDP |
499 BT_CR0_EN_CLR_SLV_WRP |
500 BT_CR0_ENABLE_IBT);
54f9c4d0
AP
501
502 clr_b_busy(bt_bmc);
503
504 return 0;
505}
506
507static int bt_bmc_remove(struct platform_device *pdev)
508{
509 struct bt_bmc *bt_bmc = dev_get_drvdata(&pdev->dev);
510
511 misc_deregister(&bt_bmc->miscdev);
512 if (!bt_bmc->irq)
513 del_timer_sync(&bt_bmc->poll_timer);
514 return 0;
515}
516
517static const struct of_device_id bt_bmc_match[] = {
1c8018f7 518 { .compatible = "aspeed,ast2400-ibt-bmc" },
7ecaff77 519 { .compatible = "aspeed,ast2500-ibt-bmc" },
54f9c4d0
AP
520 { },
521};
522
523static struct platform_driver bt_bmc_driver = {
524 .driver = {
525 .name = DEVICE_NAME,
526 .of_match_table = bt_bmc_match,
527 },
528 .probe = bt_bmc_probe,
529 .remove = bt_bmc_remove,
530};
531
532module_platform_driver(bt_bmc_driver);
533
534MODULE_DEVICE_TABLE(of, bt_bmc_match);
535MODULE_LICENSE("GPL");
536MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
1c8018f7 537MODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");