1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 ST Microelectronics
5 * Author: Lee Jones <lee.jones@linaro.org>
8 #include <linux/debugfs.h>
12 #include <linux/kernel.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <linux/sched/signal.h>
22 #include <linux/mailbox_controller.h>
24 #define MBOX_MAX_SIG_LEN 8
25 #define MBOX_MAX_MSG_LEN 16
26 #define MBOX_BYTES_PER_LINE 16
27 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
28 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
30 static bool mbox_data_ready;
32 struct mbox_test_device {
34 void __iomem *tx_mmio;
35 void __iomem *rx_mmio;
36 struct mbox_chan *tx_channel;
37 struct mbox_chan *rx_channel;
42 wait_queue_head_t waitq;
43 struct fasync_struct *async_queue;
44 struct dentry *root_debugfs_dir;
47 static ssize_t mbox_test_signal_write(struct file *filp,
48 const char __user *userbuf,
49 size_t count, loff_t *ppos)
51 struct mbox_test_device *tdev = filp->private_data;
53 if (!tdev->tx_channel) {
54 dev_err(tdev->dev, "Channel cannot do Tx\n");
58 if (count > MBOX_MAX_SIG_LEN) {
60 "Signal length %zd greater than max allowed %d\n",
61 count, MBOX_MAX_SIG_LEN);
65 /* Only allocate memory if we need to */
67 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
72 if (copy_from_user(tdev->signal, userbuf, count)) {
81 static const struct file_operations mbox_test_signal_ops = {
82 .write = mbox_test_signal_write,
84 .llseek = generic_file_llseek,
87 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
89 struct mbox_test_device *tdev = filp->private_data;
91 return fasync_helper(fd, filp, on, &tdev->async_queue);
94 static ssize_t mbox_test_message_write(struct file *filp,
95 const char __user *userbuf,
96 size_t count, loff_t *ppos)
98 struct mbox_test_device *tdev = filp->private_data;
102 if (!tdev->tx_channel) {
103 dev_err(tdev->dev, "Channel cannot do Tx\n");
107 if (count > MBOX_MAX_MSG_LEN) {
109 "Message length %zd greater than max allowed %d\n",
110 count, MBOX_MAX_MSG_LEN);
114 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
118 ret = copy_from_user(tdev->message, userbuf, count);
124 if (tdev->tx_mmio && tdev->signal) {
125 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
126 tdev->signal, MBOX_MAX_SIG_LEN);
130 data = tdev->message;
132 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
133 tdev->message, MBOX_MAX_MSG_LEN);
135 ret = mbox_send_message(tdev->tx_channel, data);
136 mbox_chan_txdone(tdev->tx_channel, ret);
138 dev_err(tdev->dev, "Failed to send message via mailbox\n");
142 kfree(tdev->message);
145 return ret < 0 ? ret : count;
148 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
153 spin_lock_irqsave(&tdev->lock, flags);
154 data_ready = mbox_data_ready;
155 spin_unlock_irqrestore(&tdev->lock, flags);
160 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
161 size_t count, loff_t *ppos)
163 struct mbox_test_device *tdev = filp->private_data;
168 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
172 if (!tdev->rx_channel) {
173 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
174 ret = simple_read_from_buffer(userbuf, count, ppos,
180 if (mbox_test_message_data_ready(tdev))
183 if (filp->f_flags & O_NONBLOCK) {
188 if (signal_pending(current)) {
196 spin_lock_irqsave(&tdev->lock, flags);
198 ptr = tdev->rx_buffer;
200 mbox_data_ready = false;
202 spin_unlock_irqrestore(&tdev->lock, flags);
203 if (copy_to_user((void __user *)userbuf, ptr, 4))
207 __set_current_state(TASK_RUNNING);
214 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
216 struct mbox_test_device *tdev = filp->private_data;
218 poll_wait(filp, &tdev->waitq, wait);
220 if (mbox_test_message_data_ready(tdev))
221 return EPOLLIN | EPOLLRDNORM;
225 static const struct file_operations mbox_test_message_ops = {
226 .write = mbox_test_message_write,
227 .read = mbox_test_message_read,
228 .fasync = mbox_test_message_fasync,
229 .poll = mbox_test_message_poll,
231 .llseek = generic_file_llseek,
234 static int mbox_test_add_debugfs(struct platform_device *pdev,
235 struct mbox_test_device *tdev)
237 if (!debugfs_initialized())
240 tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
241 if (!tdev->root_debugfs_dir) {
242 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
246 debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
247 tdev, &mbox_test_message_ops);
249 debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
250 tdev, &mbox_test_signal_ops);
255 static void mbox_test_receive_message(struct mbox_client *client, void *message)
257 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
260 spin_lock_irqsave(&tdev->lock, flags);
262 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
263 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
264 tdev->rx_buffer, MBOX_MAX_MSG_LEN);
265 } else if (message) {
266 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
267 message, MBOX_MAX_MSG_LEN);
268 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
270 mbox_data_ready = true;
271 spin_unlock_irqrestore(&tdev->lock, flags);
274 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
276 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
280 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
282 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
286 static void mbox_test_message_sent(struct mbox_client *client,
287 void *message, int r)
290 dev_warn(client->dev,
291 "Client: Message could not be sent: %d\n", r);
293 dev_info(client->dev,
294 "Client: Message sent\n");
297 static struct mbox_chan *
298 mbox_test_request_channel(struct platform_device *pdev, const char *name)
300 struct mbox_client *client;
301 struct mbox_chan *channel;
303 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
305 return ERR_PTR(-ENOMEM);
307 client->dev = &pdev->dev;
308 client->rx_callback = mbox_test_receive_message;
309 client->tx_prepare = mbox_test_prepare_message;
310 client->tx_done = mbox_test_message_sent;
311 client->tx_block = false;
312 client->knows_txdone = false;
313 client->tx_tout = 500;
315 channel = mbox_request_channel_byname(client, name);
316 if (IS_ERR(channel)) {
317 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
324 static int mbox_test_probe(struct platform_device *pdev)
326 struct mbox_test_device *tdev;
327 struct resource *res;
328 resource_size_t size;
331 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
335 /* It's okay for MMIO to be NULL */
336 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
337 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
338 if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
339 /* if reserved area in SRAM, try just ioremap */
340 size = resource_size(res);
341 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
342 } else if (IS_ERR(tdev->tx_mmio)) {
343 tdev->tx_mmio = NULL;
346 /* If specified, second reg entry is Rx MMIO */
347 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
348 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
349 if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
350 size = resource_size(res);
351 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
352 } else if (IS_ERR(tdev->rx_mmio)) {
353 tdev->rx_mmio = tdev->tx_mmio;
356 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
357 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
359 if (!tdev->tx_channel && !tdev->rx_channel)
360 return -EPROBE_DEFER;
362 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
363 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
364 tdev->rx_channel = tdev->tx_channel;
366 tdev->dev = &pdev->dev;
367 platform_set_drvdata(pdev, tdev);
369 spin_lock_init(&tdev->lock);
371 if (tdev->rx_channel) {
372 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
373 MBOX_MAX_MSG_LEN, GFP_KERNEL);
374 if (!tdev->rx_buffer)
378 ret = mbox_test_add_debugfs(pdev, tdev);
382 dev_info(&pdev->dev, "Successfully registered\n");
387 static int mbox_test_remove(struct platform_device *pdev)
389 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
391 debugfs_remove_recursive(tdev->root_debugfs_dir);
393 if (tdev->tx_channel)
394 mbox_free_channel(tdev->tx_channel);
395 if (tdev->rx_channel)
396 mbox_free_channel(tdev->rx_channel);
401 static const struct of_device_id mbox_test_match[] = {
402 { .compatible = "starfive,mailbox-test" },
405 MODULE_DEVICE_TABLE(of, mbox_test_match);
407 static struct platform_driver mbox_test_driver = {
409 .name = "mailbox_test",
410 .of_match_table = mbox_test_match,
412 .probe = mbox_test_probe,
413 .remove = mbox_test_remove,
415 module_platform_driver(mbox_test_driver);
417 MODULE_DESCRIPTION("mailbox test driver");
418 MODULE_AUTHOR("StarFive Technology Co. Ltd.");
419 MODULE_LICENSE("GPL");