summaryrefslogtreecommitdiff
path: root/drivers/misc/bt-host.c
blob: 105d3fc664a4549c5c4a8fcbbbe51fcbd6297436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/bt-host.h>

#define DEVICE_NAME	"bt-host"

#define BT_IO_BASE	0xe4
#define BT_IRQ		10

#define BT_CR0		0x0
#define   BT_CR0_IO_BASE		16
#define   BT_CR0_IRQ			12
#define   BT_CR0_EN_CLR_SLV_RDP		0x8
#define   BT_CR0_EN_CLR_SLV_WRP		0x4
#define   BT_CR0_ENABLE_IBT		0x1
#define BT_CR1		0x4
#define   BT_CR1_IRQ_H2B	0x01
#define   BT_CR1_IRQ_HBUSY	0x40
#define BT_CR2		0x8
#define   BT_CR2_IRQ_H2B	0x01
#define   BT_CR2_IRQ_HBUSY	0x40
#define BT_CR3		0xc
#define BT_CTRL		0x10
#define   BT_CTRL_B_BUSY		0x80
#define   BT_CTRL_H_BUSY		0x40
#define   BT_CTRL_OEM0			0x20
#define   BT_CTRL_SMS_ATN		0x10
#define   BT_CTRL_B2H_ATN		0x08
#define   BT_CTRL_H2B_ATN		0x04
#define   BT_CTRL_CLR_RD_PTR		0x02
#define   BT_CTRL_CLR_WR_PTR		0x01
#define BT_BMC2HOST	0x14
#define BT_INTMASK	0x18
#define   BT_INTMASK_B2H_IRQEN		0x01
#define   BT_INTMASK_B2H_IRQ		0x02
#define   BT_INTMASK_BMC_HWRST		0x80

struct bt_host {
	struct device		dev;
	struct miscdevice	miscdev;
	void			*base;
	int			open_count;
	int			irq;
	wait_queue_head_t	queue;
	struct timer_list	poll_timer;
};

static u8 bt_inb(struct bt_host *bt_host, int reg)
{
	return ioread8(bt_host->base + reg);
}

static void bt_outb(struct bt_host *bt_host, u8 data, int reg)
{
	iowrite8(data, bt_host->base + reg);
}

static void clr_rd_ptr(struct bt_host *bt_host)
{
	bt_outb(bt_host, BT_CTRL_CLR_RD_PTR, BT_CTRL);
}

static void clr_wr_ptr(struct bt_host *bt_host)
{
	bt_outb(bt_host, BT_CTRL_CLR_WR_PTR, BT_CTRL);
}

static void clr_h2b_atn(struct bt_host *bt_host)
{
	bt_outb(bt_host, BT_CTRL_H2B_ATN, BT_CTRL);
}

static void set_b_busy(struct bt_host *bt_host)
{
	if (!(bt_inb(bt_host, BT_CTRL) & BT_CTRL_B_BUSY))
		bt_outb(bt_host, BT_CTRL_B_BUSY, BT_CTRL);
}

static void clr_b_busy(struct bt_host *bt_host)
{
	if (bt_inb(bt_host, BT_CTRL) & BT_CTRL_B_BUSY)
		bt_outb(bt_host, BT_CTRL_B_BUSY, BT_CTRL);
}

static void set_b2h_atn(struct bt_host *bt_host)
{
	bt_outb(bt_host, BT_CTRL_B2H_ATN, BT_CTRL);
}

static u8 bt_read(struct bt_host *bt_host)
{
	return bt_inb(bt_host, BT_BMC2HOST);
}

static void bt_write(struct bt_host *bt_host, u8 c)
{
	bt_outb(bt_host, c, BT_BMC2HOST);
}

static void set_sms_atn(struct bt_host *bt_host)
{
	bt_outb(bt_host, BT_CTRL_SMS_ATN, BT_CTRL);
}

static struct bt_host *file_bt_host(struct file *file)
{
	return container_of(file->private_data, struct bt_host, miscdev);
}

static int bt_host_open(struct inode *inode, struct file *file)
{
	struct bt_host *bt_host = file_bt_host(file);

	clr_b_busy(bt_host);

	return 0;
}

static ssize_t bt_host_read(struct file *file, char __user *buf,
				size_t count, loff_t *ppos)
{
	struct bt_host *bt_host = file_bt_host(file);
	char __user *p = buf;
	u8 len;

	if (!access_ok(VERIFY_WRITE, buf, count))
		return -EFAULT;

	WARN_ON(*ppos);

	if (wait_event_interruptible(bt_host->queue,
				bt_inb(bt_host, BT_CTRL) & BT_CTRL_H2B_ATN))
		return -ERESTARTSYS;

	set_b_busy(bt_host);
	clr_h2b_atn(bt_host);
	clr_rd_ptr(bt_host);

	len = bt_read(bt_host);
	__put_user(len, p++);

	/* We pass the length back as well */
	if (len + 1 > count)
		len = count - 1;

	while(len) {
		if (__put_user(bt_read(bt_host), p))
			return -EFAULT;
		len--; p++;
	}

	clr_b_busy(bt_host);

	return p - buf;
}

static ssize_t bt_host_write(struct file *file, const char __user *buf,
				size_t count, loff_t *ppos)
{
	struct bt_host *bt_host = file_bt_host(file);
	const char __user *p = buf;
	u8 c;

	if (!access_ok(VERIFY_READ, buf, count))
		return -EFAULT;

	WARN_ON(*ppos);

	/* There's no interrupt for clearing host busy so we have to
	 * poll */
	if (wait_event_interruptible(bt_host->queue,
				!(bt_inb(bt_host, BT_CTRL) &
					(BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
		return -ERESTARTSYS;

	clr_wr_ptr(bt_host);

	while (count) {
		if (__get_user(c, p))
			return -EFAULT;

		bt_write(bt_host, c);
		count--; p++;
	}

	set_b2h_atn(bt_host);

	return p - buf;
}

static long bt_host_ioctl(struct file *file, unsigned int cmd,
		unsigned long param)
{
	struct bt_host *bt_host = file_bt_host(file);
	switch (cmd) {
	case BT_HOST_IOCTL_SMS_ATN:
		set_sms_atn(bt_host);
		return 0;
	}
	return -EINVAL;
}

static int bt_host_release(struct inode *inode, struct file *file)
{
	struct bt_host *bt_host = file_bt_host(file);
	set_b_busy(bt_host);
	return 0;
}

static unsigned int bt_host_poll(struct file *file, poll_table *wait)
{
	struct bt_host *bt_host = file_bt_host(file);
	unsigned int mask = 0;
	uint8_t ctrl;

	poll_wait(file, &bt_host->queue, wait);

	ctrl = bt_inb(bt_host, BT_CTRL);

	if (ctrl & BT_CTRL_H2B_ATN)
		mask |= POLLIN;

	if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
		mask |= POLLOUT;

	return mask;
}

static const struct file_operations bt_host_fops = {
	.owner		= THIS_MODULE,
	.open		= bt_host_open,
	.read		= bt_host_read,
	.write		= bt_host_write,
	.release	= bt_host_release,
	.poll		= bt_host_poll,
	.unlocked_ioctl	= bt_host_ioctl,
};

static void poll_timer(unsigned long data)
{
	struct bt_host *bt_host = (void *)data;
	bt_host->poll_timer.expires += msecs_to_jiffies(500);
	wake_up(&bt_host->queue);
	add_timer(&bt_host->poll_timer);
}

irqreturn_t bt_host_irq(int irq, void *arg)
{
	struct bt_host *bt_host = arg;
	uint32_t reg;

	reg = ioread32(bt_host->base + BT_CR2);
	reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
	if (!reg)
		return IRQ_NONE;

	/* ack pending IRQs */
	iowrite32(reg, bt_host->base + BT_CR2);

	wake_up(&bt_host->queue);
	return IRQ_HANDLED;
}

static int bt_host_config_irq(struct bt_host *bt_host,
		struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	uint32_t reg;
	int rc;

	bt_host->irq = irq_of_parse_and_map(dev->of_node, 0);
	if (!bt_host->irq)
		return -ENODEV;

	rc = devm_request_irq(dev, bt_host->irq, bt_host_irq, IRQF_SHARED,
			DEVICE_NAME, bt_host);
	if (rc < 0) {
		dev_warn(dev, "Unable to request IRQ %d\n", bt_host->irq);
		bt_host->irq = 0;
		return rc;
	}

	/* Configure IRQs on the host clearing the H2B and HBUSY bits;
	 * H2B will be asserted when the host has data for us; HBUSY
	 * will be cleared (along with B2H) when we can write the next
	 * message to the BT buffer */
	reg = ioread32(bt_host->base + BT_CR1);
	reg |= BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY;
	iowrite32(reg, bt_host->base + BT_CR1);

	return 0;
}

static int bt_host_probe(struct platform_device *pdev)
{
	struct bt_host *bt_host;
	struct device *dev;
	struct resource *res;
	int rc;

	if (!pdev || !pdev->dev.of_node)
		return -ENODEV;

	dev = &pdev->dev;
	dev_info(dev, "Found bt host device\n");

	bt_host = devm_kzalloc(dev, sizeof(*bt_host), GFP_KERNEL);
	if (!bt_host)
		return -ENOMEM;

	dev_set_drvdata(&pdev->dev, bt_host);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(dev, "Unable to find resources\n");
		rc = -ENXIO;
		goto out_free;
	}

	bt_host->base = devm_ioremap_resource(&pdev->dev, res);
	if (!bt_host->base) {
		rc = -ENOMEM;
		goto out_free;
	}

	init_waitqueue_head(&bt_host->queue);

	bt_host->miscdev.minor	= MISC_DYNAMIC_MINOR,
	bt_host->miscdev.name	= DEVICE_NAME,
	bt_host->miscdev.fops	= &bt_host_fops,
	bt_host->miscdev.parent = dev;
	rc = misc_register(&bt_host->miscdev);
	if (rc) {
		dev_err(dev, "Unable to register device\n");
		goto out_unmap;
	}

	bt_host_config_irq(bt_host, pdev);

	if (bt_host->irq) {
		dev_info(dev, "Using IRQ %d\n", bt_host->irq);
	} else {
		dev_info(dev, "No IRQ; using timer\n");
		init_timer(&bt_host->poll_timer);
		bt_host->poll_timer.function = poll_timer;
		bt_host->poll_timer.data = (unsigned long)bt_host;
		bt_host->poll_timer.expires = jiffies + msecs_to_jiffies(10);
		add_timer(&bt_host->poll_timer);
	}

	iowrite32((BT_IO_BASE << BT_CR0_IO_BASE) |
		  (BT_IRQ << BT_CR0_IRQ) |
		  BT_CR0_EN_CLR_SLV_RDP |
		  BT_CR0_EN_CLR_SLV_WRP |
		  BT_CR0_ENABLE_IBT,
		  bt_host->base + BT_CR0);

	clr_b_busy(bt_host);

	return 0;

out_unmap:
	devm_iounmap(&pdev->dev, bt_host->base);

out_free:
	devm_kfree(dev, bt_host);
	return rc;

}

static int bt_host_remove(struct platform_device *pdev)
{
	struct bt_host *bt_host = dev_get_drvdata(&pdev->dev);
	misc_deregister(&bt_host->miscdev);
	if (!bt_host->irq)
		del_timer_sync(&bt_host->poll_timer);
	devm_iounmap(&pdev->dev, bt_host->base);
	devm_kfree(&pdev->dev, bt_host);
	bt_host = NULL;

	return 0;
}

static const struct of_device_id bt_host_match[] = {
	{ .compatible = "aspeed,bt-host" },
	{ },
};

static struct platform_driver bt_host_driver = {
	.driver = {
		.name		= DEVICE_NAME,
		.owner		= THIS_MODULE,
		.of_match_table = bt_host_match,
	},
	.probe = bt_host_probe,
	.remove = bt_host_remove,
};

module_platform_driver(bt_host_driver);

MODULE_DEVICE_TABLE(of, bt_host_match);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
MODULE_DESCRIPTION("Linux device interface to the BT interface");