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