summaryrefslogtreecommitdiff
path: root/drivers/misc/npcm7xx-pci-mbox.c
blob: 1a80661a42969fcd8f57f8ba1630f6e148914fd5 (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
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2014-2018 Nuvoton Technology corporation.

#include <linux/interrupt.h>
#include <linux/mfd/syscon.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#define DEVICE_NAME	"npcm7xx-pci-mbox"

#define NPCM7XX_MBOX_BMBXSTAT	0x0
#define NPCM7XX_MBOX_BMBXCTL	0x4
#define NPCM7XX_MBOX_BMBXCMD	0x8

#define NPCM7XX_MBOX_CIF_0	BIT(0)
#define NPCM7XX_MBOX_CIE_0	BIT(0)
#define NPCM7XX_MBOX_HIF_0	BIT(0)

#define NPCM7XX_MBOX_ALL_CIF	GENMASK(7, 0)
#define NPCM7XX_MBOX_ALL_CIE	GENMASK(7, 0)
#define NPCM7XX_MBOX_ALL_HIF	GENMASK(7, 0)

struct npcm7xx_mbox {
	struct miscdevice	miscdev;
	struct regmap		*regmap;
	void __iomem		*memory;
	wait_queue_head_t	queue;
	spinlock_t		lock;	/* mbox access mutex */
	bool			cif0;
	u32			max_buf_size;
};

static atomic_t npcm7xx_mbox_open_count = ATOMIC_INIT(0);

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

static int npcm7xx_mbox_open(struct inode *inode, struct file *file)
{
	struct npcm7xx_mbox *mbox = file_mbox(file);

	if (atomic_inc_return(&npcm7xx_mbox_open_count) == 1) {
		/* enable mailbox interrupt */
		regmap_update_bits(mbox->regmap, NPCM7XX_MBOX_BMBXCTL,
				   NPCM7XX_MBOX_ALL_CIE, NPCM7XX_MBOX_CIE_0);
		return 0;
	}

	atomic_dec(&npcm7xx_mbox_open_count);
	return -EBUSY;
}

static ssize_t npcm7xx_mbox_read(struct file *file, char __user *buf,
				 size_t count, loff_t *ppos)
{
	struct npcm7xx_mbox *mbox = file_mbox(file);
	unsigned long flags;

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

	if ((*ppos + count) > mbox->max_buf_size)
		return -EINVAL;

	if (file->f_flags & O_NONBLOCK) {
		if (!mbox->cif0)
			return -EAGAIN;
	} else if (wait_event_interruptible(mbox->queue, mbox->cif0)) {
		return -ERESTARTSYS;
	}

	spin_lock_irqsave(&mbox->lock, flags);

	if (copy_to_user((void __user *)buf,
			 (const void *)(mbox->memory + *ppos), count)) {
		spin_unlock_irqrestore(&mbox->lock, flags);
		return -EFAULT;
	}

	mbox->cif0 = false;
	spin_unlock_irqrestore(&mbox->lock, flags);
	return count;
}

static ssize_t npcm7xx_mbox_write(struct file *file, const char __user *buf,
				  size_t count, loff_t *ppos)
{
	struct npcm7xx_mbox *mbox = file_mbox(file);
	unsigned long flags;

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

	if ((*ppos + count) > mbox->max_buf_size)
		return -EINVAL;

	spin_lock_irqsave(&mbox->lock, flags);

	if (copy_from_user((void *)(mbox->memory + *ppos),
			   (void __user *)buf, count)) {
		spin_unlock_irqrestore(&mbox->lock, flags);
		return -EFAULT;
	}

	regmap_update_bits(mbox->regmap, NPCM7XX_MBOX_BMBXCMD,
			   NPCM7XX_MBOX_ALL_HIF, NPCM7XX_MBOX_HIF_0);

	spin_unlock_irqrestore(&mbox->lock, flags);
	return count;
}

static unsigned int npcm7xx_mbox_poll(struct file *file, poll_table *wait)
{
	struct npcm7xx_mbox *mbox = file_mbox(file);
	unsigned int mask = 0;

	poll_wait(file, &mbox->queue, wait);
	if (mbox->cif0)
		mask |= POLLIN;

	return mask;
}

static int npcm7xx_mbox_release(struct inode *inode, struct file *file)
{
	atomic_dec(&npcm7xx_mbox_open_count);
	return 0;
}

static const struct file_operations npcm7xx_mbox_fops = {
	.owner		= THIS_MODULE,
	.llseek		= no_seek_end_llseek,
	.read		= npcm7xx_mbox_read,
	.write		= npcm7xx_mbox_write,
	.open		= npcm7xx_mbox_open,
	.release	= npcm7xx_mbox_release,
	.poll		= npcm7xx_mbox_poll,
};

static irqreturn_t npcm7xx_mbox_irq(int irq, void *arg)
{
	struct npcm7xx_mbox *mbox = arg;
	u32 val;

	regmap_read(mbox->regmap, NPCM7XX_MBOX_BMBXSTAT, &val);
	if ((val & NPCM7XX_MBOX_CIF_0) != NPCM7XX_MBOX_CIF_0)
		return IRQ_NONE;

	/*
	 * Leave the status bit set so that we know the data is for us,
	 * clear it once it has been read.
	 */
	mbox->cif0 = true;

	/* Mask it off, we'll clear it when we the data gets read */
	regmap_write_bits(mbox->regmap, NPCM7XX_MBOX_BMBXSTAT,
			  NPCM7XX_MBOX_ALL_CIF, NPCM7XX_MBOX_CIF_0);

	wake_up(&mbox->queue);

	return IRQ_HANDLED;
}

static int npcm7xx_mbox_config_irq(struct npcm7xx_mbox *mbox,
				   struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	int rc, irq;
	u32 val;

	/* Disable all register based interrupts */
	regmap_update_bits(mbox->regmap, NPCM7XX_MBOX_BMBXCTL,
			   NPCM7XX_MBOX_ALL_CIE, 0);
/*
 * These registers are write one to clear. Clear them.
 * Per spec, cleared bits should not be re-cleared.
 * Need to read and clear needed bits only, instead of blindly clearing all.
 */
	regmap_read(mbox->regmap, NPCM7XX_MBOX_BMBXSTAT, &val);
	val &= NPCM7XX_MBOX_ALL_CIF;

	/* If any bit is set, write back to clear */
	if (val)
		regmap_write_bits(mbox->regmap, NPCM7XX_MBOX_BMBXSTAT,
				  NPCM7XX_MBOX_ALL_CIF, val);

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

	rc = devm_request_irq(dev, irq, npcm7xx_mbox_irq, 0, DEVICE_NAME, mbox);
	if (rc < 0) {
		dev_err(dev, "Unable to request IRQ %d\n", irq);
		return rc;
	}

	return 0;
}

static int npcm7xx_mbox_probe(struct platform_device *pdev)
{
	struct npcm7xx_mbox *mbox;
	struct device *dev;
	struct resource *res;
	int rc;

	dev = &pdev->dev;

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

	dev_set_drvdata(&pdev->dev, mbox);

	mbox->regmap = syscon_node_to_regmap(dev->of_node);
	if (IS_ERR(mbox->regmap)) {
		dev_err(dev, "Couldn't get regmap\n");
		return -ENODEV;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	mbox->memory = devm_ioremap_resource(dev, res);
	if (IS_ERR(mbox->memory))
		return PTR_ERR(mbox->memory);
	mbox->max_buf_size = resource_size(res);

	spin_lock_init(&mbox->lock);
	init_waitqueue_head(&mbox->queue);

	mbox->miscdev.minor = MISC_DYNAMIC_MINOR;
	mbox->miscdev.name = DEVICE_NAME;
	mbox->miscdev.fops = &npcm7xx_mbox_fops;
	mbox->miscdev.parent = dev;
	mbox->cif0 = false;
	rc = misc_register(&mbox->miscdev);
	if (rc) {
		dev_err(dev, "Unable to register device\n");
		return rc;
	}

	rc = npcm7xx_mbox_config_irq(mbox, pdev);
	if (rc) {
		dev_err(dev, "Failed to configure IRQ\n");
		misc_deregister(&mbox->miscdev);
		return rc;
	}

	pr_info("NPCM7xx PCI Mailbox probed\n");

	return 0;
}

static int npcm7xx_mbox_remove(struct platform_device *pdev)
{
	struct npcm7xx_mbox *mbox = dev_get_drvdata(&pdev->dev);

	misc_deregister(&mbox->miscdev);

	return 0;
}

static const struct of_device_id npcm7xx_mbox_match[] = {
	{ .compatible = "nuvoton,npcm750-pci-mbox" },
	{ },
};

static struct platform_driver npcm7xx_mbox_driver = {
	.driver = {
		.name		= DEVICE_NAME,
		.of_match_table = npcm7xx_mbox_match,
	},
	.probe = npcm7xx_mbox_probe,
	.remove = npcm7xx_mbox_remove,
};

module_platform_driver(npcm7xx_mbox_driver);

MODULE_DEVICE_TABLE(of, npcm7xx_mbox_match);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
MODULE_DESCRIPTION("NPCM7XX mailbox device driver");