blob: 97fb956bb6e04a328b12abdf37eb9db3fb321bb3 [file] [log] [blame]
Lee Jones8ea44842015-10-16 08:21:30 +01001/*
2 * Copyright (C) 2015 ST Microelectronics
3 *
4 * Author: Lee Jones <lee.jones@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/debugfs.h>
13#include <linux/err.h>
Sudeep Hollabaef9a32016-11-29 14:37:04 +000014#include <linux/fs.h>
Lee Jonesa133f8b2015-10-20 17:11:27 +010015#include <linux/io.h>
Lee Jones8ea44842015-10-16 08:21:30 +010016#include <linux/kernel.h>
17#include <linux/mailbox_client.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/platform_device.h>
Sudeep Hollabaef9a32016-11-29 14:37:04 +000021#include <linux/poll.h>
Lee Jones8ea44842015-10-16 08:21:30 +010022#include <linux/slab.h>
23#include <linux/uaccess.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Lee Jones8ea44842015-10-16 08:21:30 +010025
26#define MBOX_MAX_SIG_LEN 8
27#define MBOX_MAX_MSG_LEN 128
28#define MBOX_BYTES_PER_LINE 16
Lee Jones6c036632015-10-16 13:32:47 +010029#define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
Lee Jones8ea44842015-10-16 08:21:30 +010030#define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
31 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
32
33static struct dentry *root_debugfs_dir;
34
35struct mbox_test_device {
36 struct device *dev;
Sudeep Holla2d74ffd2016-02-19 16:01:18 +000037 void __iomem *tx_mmio;
38 void __iomem *rx_mmio;
Lee Jones8ea44842015-10-16 08:21:30 +010039 struct mbox_chan *tx_channel;
40 struct mbox_chan *rx_channel;
41 char *rx_buffer;
42 char *signal;
43 char *message;
44 spinlock_t lock;
Sudeep Hollabaef9a32016-11-29 14:37:04 +000045 wait_queue_head_t waitq;
46 struct fasync_struct *async_queue;
Lee Jones8ea44842015-10-16 08:21:30 +010047};
48
49static ssize_t mbox_test_signal_write(struct file *filp,
50 const char __user *userbuf,
51 size_t count, loff_t *ppos)
52{
53 struct mbox_test_device *tdev = filp->private_data;
Lee Jones8ea44842015-10-16 08:21:30 +010054
55 if (!tdev->tx_channel) {
56 dev_err(tdev->dev, "Channel cannot do Tx\n");
57 return -EINVAL;
58 }
59
60 if (count > MBOX_MAX_SIG_LEN) {
61 dev_err(tdev->dev,
Lee Jones6c036632015-10-16 13:32:47 +010062 "Signal length %zd greater than max allowed %d\n",
Lee Jones8ea44842015-10-16 08:21:30 +010063 count, MBOX_MAX_SIG_LEN);
64 return -EINVAL;
65 }
66
Lee Jonesd1c2f872016-03-23 14:43:42 +000067 /* Only allocate memory if we need to */
68 if (!tdev->signal) {
69 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
70 if (!tdev->signal)
71 return -ENOMEM;
72 }
Lee Jones8ea44842015-10-16 08:21:30 +010073
Lee Jones17f5f282016-03-23 14:43:41 +000074 if (copy_from_user(tdev->signal, userbuf, count)) {
Lee Jones8ea44842015-10-16 08:21:30 +010075 kfree(tdev->signal);
Lee Jones17f5f282016-03-23 14:43:41 +000076 tdev->signal = NULL;
Lee Jones8ea44842015-10-16 08:21:30 +010077 return -EFAULT;
78 }
79
Lee Jones17f5f282016-03-23 14:43:41 +000080 return count;
Lee Jones8ea44842015-10-16 08:21:30 +010081}
82
83static const struct file_operations mbox_test_signal_ops = {
84 .write = mbox_test_signal_write,
85 .open = simple_open,
86 .llseek = generic_file_llseek,
87};
88
Sudeep Hollabaef9a32016-11-29 14:37:04 +000089static int mbox_test_message_fasync(int fd, struct file *filp, int on)
90{
91 struct mbox_test_device *tdev = filp->private_data;
92
93 return fasync_helper(fd, filp, on, &tdev->async_queue);
94}
95
Lee Jones8ea44842015-10-16 08:21:30 +010096static ssize_t mbox_test_message_write(struct file *filp,
97 const char __user *userbuf,
98 size_t count, loff_t *ppos)
99{
100 struct mbox_test_device *tdev = filp->private_data;
101 void *data;
102 int ret;
103
104 if (!tdev->tx_channel) {
105 dev_err(tdev->dev, "Channel cannot do Tx\n");
106 return -EINVAL;
107 }
108
109 if (count > MBOX_MAX_MSG_LEN) {
110 dev_err(tdev->dev,
Lee Jones6c036632015-10-16 13:32:47 +0100111 "Message length %zd greater than max allowed %d\n",
Lee Jones8ea44842015-10-16 08:21:30 +0100112 count, MBOX_MAX_MSG_LEN);
113 return -EINVAL;
114 }
115
116 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
117 if (!tdev->message)
118 return -ENOMEM;
119
120 ret = copy_from_user(tdev->message, userbuf, count);
121 if (ret) {
122 ret = -EFAULT;
123 goto out;
124 }
125
126 /*
127 * A separate signal is only of use if there is
128 * MMIO to subsequently pass the message through
129 */
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000130 if (tdev->tx_mmio && tdev->signal) {
Sudeep Holla27fa6802016-02-19 16:01:17 +0000131 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
132 tdev->signal, MBOX_MAX_SIG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100133
134 data = tdev->signal;
135 } else
136 data = tdev->message;
137
Sudeep Holla27fa6802016-02-19 16:01:17 +0000138 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
139 tdev->message, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100140
141 ret = mbox_send_message(tdev->tx_channel, data);
142 if (ret < 0)
143 dev_err(tdev->dev, "Failed to send message via mailbox\n");
144
145out:
146 kfree(tdev->signal);
147 kfree(tdev->message);
Sudeep Holla9ef3c512016-05-24 17:12:04 +0100148 tdev->signal = NULL;
Lee Jones8ea44842015-10-16 08:21:30 +0100149
150 return ret < 0 ? ret : count;
151}
152
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000153static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
154{
155 unsigned char data;
156 unsigned long flags;
157
158 spin_lock_irqsave(&tdev->lock, flags);
159 data = tdev->rx_buffer[0];
160 spin_unlock_irqrestore(&tdev->lock, flags);
161
162 if (data != '\0')
163 return true;
164 return false;
165}
166
Lee Jones8ea44842015-10-16 08:21:30 +0100167static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
168 size_t count, loff_t *ppos)
169{
170 struct mbox_test_device *tdev = filp->private_data;
171 unsigned long flags;
172 char *touser, *ptr;
173 int l = 0;
174 int ret;
175
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000176 DECLARE_WAITQUEUE(wait, current);
177
Dan Carpenterc3ac54a2015-10-22 22:51:27 +0300178 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
Lee Jones8ea44842015-10-16 08:21:30 +0100179 if (!touser)
180 return -ENOMEM;
181
182 if (!tdev->rx_channel) {
183 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
184 ret = simple_read_from_buffer(userbuf, count, ppos,
185 touser, ret);
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000186 goto kfree_err;
Lee Jones8ea44842015-10-16 08:21:30 +0100187 }
188
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000189 add_wait_queue(&tdev->waitq, &wait);
190
191 do {
192 __set_current_state(TASK_INTERRUPTIBLE);
193
194 if (mbox_test_message_data_ready(tdev))
195 break;
196
197 if (filp->f_flags & O_NONBLOCK) {
198 ret = -EAGAIN;
199 goto waitq_err;
200 }
201
202 if (signal_pending(current)) {
203 ret = -ERESTARTSYS;
204 goto waitq_err;
205 }
206 schedule();
207
208 } while (1);
Lee Jones8ea44842015-10-16 08:21:30 +0100209
210 spin_lock_irqsave(&tdev->lock, flags);
211
212 ptr = tdev->rx_buffer;
213 while (l < MBOX_HEXDUMP_MAX_LEN) {
214 hex_dump_to_buffer(ptr,
215 MBOX_BYTES_PER_LINE,
216 MBOX_BYTES_PER_LINE, 1, touser + l,
217 MBOX_HEXDUMP_LINE_LEN, true);
218
219 ptr += MBOX_BYTES_PER_LINE;
220 l += MBOX_HEXDUMP_LINE_LEN;
221 *(touser + (l - 1)) = '\n';
222 }
223 *(touser + l) = '\0';
224
225 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
226
227 spin_unlock_irqrestore(&tdev->lock, flags);
228
229 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000230waitq_err:
231 __set_current_state(TASK_RUNNING);
232 remove_wait_queue(&tdev->waitq, &wait);
233kfree_err:
Lee Jones8ea44842015-10-16 08:21:30 +0100234 kfree(touser);
235 return ret;
236}
237
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000238static unsigned int
239mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
240{
241 struct mbox_test_device *tdev = filp->private_data;
242
243 poll_wait(filp, &tdev->waitq, wait);
244
245 if (mbox_test_message_data_ready(tdev))
246 return POLLIN | POLLRDNORM;
247 return 0;
248}
249
Lee Jones8ea44842015-10-16 08:21:30 +0100250static const struct file_operations mbox_test_message_ops = {
251 .write = mbox_test_message_write,
252 .read = mbox_test_message_read,
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000253 .fasync = mbox_test_message_fasync,
254 .poll = mbox_test_message_poll,
Lee Jones8ea44842015-10-16 08:21:30 +0100255 .open = simple_open,
256 .llseek = generic_file_llseek,
257};
258
259static int mbox_test_add_debugfs(struct platform_device *pdev,
260 struct mbox_test_device *tdev)
261{
262 if (!debugfs_initialized())
263 return 0;
264
265 root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
266 if (!root_debugfs_dir) {
267 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
268 return -EINVAL;
269 }
270
271 debugfs_create_file("message", 0600, root_debugfs_dir,
272 tdev, &mbox_test_message_ops);
273
274 debugfs_create_file("signal", 0200, root_debugfs_dir,
275 tdev, &mbox_test_signal_ops);
276
277 return 0;
278}
279
280static void mbox_test_receive_message(struct mbox_client *client, void *message)
281{
282 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
283 unsigned long flags;
284
285 spin_lock_irqsave(&tdev->lock, flags);
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000286 if (tdev->rx_mmio) {
287 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
Sudeep Holla27fa6802016-02-19 16:01:17 +0000288 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
289 tdev->rx_buffer, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100290 } else if (message) {
Sudeep Holla27fa6802016-02-19 16:01:17 +0000291 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
292 message, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100293 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
294 }
295 spin_unlock_irqrestore(&tdev->lock, flags);
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000296
297 wake_up_interruptible(&tdev->waitq);
298
299 kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
Lee Jones8ea44842015-10-16 08:21:30 +0100300}
301
302static void mbox_test_prepare_message(struct mbox_client *client, void *message)
303{
304 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
305
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000306 if (tdev->tx_mmio) {
Lee Jones8ea44842015-10-16 08:21:30 +0100307 if (tdev->signal)
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000308 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100309 else
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000310 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100311 }
312}
313
314static void mbox_test_message_sent(struct mbox_client *client,
315 void *message, int r)
316{
317 if (r)
318 dev_warn(client->dev,
319 "Client: Message could not be sent: %d\n", r);
320 else
321 dev_info(client->dev,
322 "Client: Message sent\n");
323}
324
325static struct mbox_chan *
326mbox_test_request_channel(struct platform_device *pdev, const char *name)
327{
328 struct mbox_client *client;
329 struct mbox_chan *channel;
330
331 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
332 if (!client)
333 return ERR_PTR(-ENOMEM);
334
335 client->dev = &pdev->dev;
336 client->rx_callback = mbox_test_receive_message;
337 client->tx_prepare = mbox_test_prepare_message;
338 client->tx_done = mbox_test_message_sent;
339 client->tx_block = true;
340 client->knows_txdone = false;
341 client->tx_tout = 500;
342
343 channel = mbox_request_channel_byname(client, name);
344 if (IS_ERR(channel)) {
345 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
346 return NULL;
347 }
348
349 return channel;
350}
351
352static int mbox_test_probe(struct platform_device *pdev)
353{
354 struct mbox_test_device *tdev;
355 struct resource *res;
Sudeep Holladb4d22c2016-11-29 14:37:05 +0000356 resource_size_t size;
Lee Jones8ea44842015-10-16 08:21:30 +0100357 int ret;
358
359 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
360 if (!tdev)
361 return -ENOMEM;
362
363 /* It's okay for MMIO to be NULL */
364 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sudeep Holladb4d22c2016-11-29 14:37:05 +0000365 size = resource_size(res);
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000366 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
Sudeep Holladb4d22c2016-11-29 14:37:05 +0000367 if (PTR_ERR(tdev->tx_mmio) == -EBUSY)
368 /* if reserved area in SRAM, try just ioremap */
369 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
370 else if (IS_ERR(tdev->tx_mmio))
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000371 tdev->tx_mmio = NULL;
372
373 /* If specified, second reg entry is Rx MMIO */
374 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sudeep Holladb4d22c2016-11-29 14:37:05 +0000375 size = resource_size(res);
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000376 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
Sudeep Holladb4d22c2016-11-29 14:37:05 +0000377 if (PTR_ERR(tdev->rx_mmio) == -EBUSY)
378 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
379 else if (IS_ERR(tdev->rx_mmio))
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000380 tdev->rx_mmio = tdev->tx_mmio;
Lee Jones8ea44842015-10-16 08:21:30 +0100381
382 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
383 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
384
Lee Jones6c036632015-10-16 13:32:47 +0100385 if (!tdev->tx_channel && !tdev->rx_channel)
Lee Jones8ea44842015-10-16 08:21:30 +0100386 return -EPROBE_DEFER;
387
Sudeep Holla2d74ffd2016-02-19 16:01:18 +0000388 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
389 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
390 tdev->rx_channel = tdev->tx_channel;
391
Lee Jones8ea44842015-10-16 08:21:30 +0100392 tdev->dev = &pdev->dev;
393 platform_set_drvdata(pdev, tdev);
394
395 spin_lock_init(&tdev->lock);
396
397 if (tdev->rx_channel) {
398 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
399 MBOX_MAX_MSG_LEN, GFP_KERNEL);
400 if (!tdev->rx_buffer)
401 return -ENOMEM;
402 }
403
404 ret = mbox_test_add_debugfs(pdev, tdev);
405 if (ret)
406 return ret;
407
Sudeep Hollabaef9a32016-11-29 14:37:04 +0000408 init_waitqueue_head(&tdev->waitq);
Lee Jones8ea44842015-10-16 08:21:30 +0100409 dev_info(&pdev->dev, "Successfully registered\n");
410
411 return 0;
412}
413
414static int mbox_test_remove(struct platform_device *pdev)
415{
416 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
417
418 debugfs_remove_recursive(root_debugfs_dir);
419
420 if (tdev->tx_channel)
421 mbox_free_channel(tdev->tx_channel);
422 if (tdev->rx_channel)
423 mbox_free_channel(tdev->rx_channel);
424
425 return 0;
426}
427
428static const struct of_device_id mbox_test_match[] = {
Sudeep Hollac4280132016-02-19 16:01:16 +0000429 { .compatible = "mailbox-test" },
Lee Jones8ea44842015-10-16 08:21:30 +0100430 {},
431};
Javier Martinez Canillasf42cce32016-10-20 00:34:04 -0300432MODULE_DEVICE_TABLE(of, mbox_test_match);
Lee Jones8ea44842015-10-16 08:21:30 +0100433
434static struct platform_driver mbox_test_driver = {
435 .driver = {
Sudeep Hollaadf06ba2016-02-19 16:01:15 +0000436 .name = "mailbox_test",
Lee Jones8ea44842015-10-16 08:21:30 +0100437 .of_match_table = mbox_test_match,
438 },
439 .probe = mbox_test_probe,
440 .remove = mbox_test_remove,
441};
442module_platform_driver(mbox_test_driver);
443
444MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
445MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
446MODULE_LICENSE("GPL v2");