blob: 15f13b584c9965f3daff8b0023e08421e4dd1942 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Mikael Olssond4ad9e52024-02-07 11:22:26 +01002 * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 * SPDX-License-Identifier: GPL-2.0-only
Kristofer Jonsson116a6352020-08-20 17:25:23 +02004 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
Kristofer Jonsson116a6352020-08-20 17:25:23 +020018 */
19
Mikael Olssond4ad9e52024-02-07 11:22:26 +010020#include <common/ethosu_device.h>
21#include <uapi/ethosu.h>
22
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020023#include <linux/bitmap.h>
24#include <linux/fs.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020025#include <linux/module.h>
26#include <linux/io.h>
27#include <linux/of.h>
28#include <linux/of_address.h>
Kristofer Jonssond779a082023-01-04 17:09:47 +010029#include <linux/rpmsg.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020030
Kristofer Jonsson116a6352020-08-20 17:25:23 +020031/****************************************************************************
32 * Defines
33 ****************************************************************************/
34
Mikael Olssonf1cfe192023-06-12 15:00:41 +020035#define ETHOSU_DRIVER_STR(s) #s
36#define ETHOSU_DRIVER_VERSION_STR(major, minor, patch) \
37 ETHOSU_DRIVER_STR(major) "." \
38 ETHOSU_DRIVER_STR(minor) "." \
39 ETHOSU_DRIVER_STR(patch)
40#define ETHOSU_DRIVER_VERSION ETHOSU_DRIVER_VERSION_STR( \
41 ETHOSU_KERNEL_DRIVER_VERSION_MAJOR, \
42 ETHOSU_KERNEL_DRIVER_VERSION_MINOR, \
43 ETHOSU_KERNEL_DRIVER_VERSION_PATCH)
44
Kristofer Jonsson116a6352020-08-20 17:25:23 +020045#define ETHOSU_DRIVER_NAME "ethosu"
46
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020047#define MINOR_BASE 0 /* Minor version starts at 0 */
48#define MINOR_COUNT 64 /* Allocate minor versions */
49
Kristofer Jonsson116a6352020-08-20 17:25:23 +020050/****************************************************************************
51 * Variables
52 ****************************************************************************/
53
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020054static struct class *ethosu_class;
55
56static dev_t devt;
57
Kristofer Jonsson116a6352020-08-20 17:25:23 +020058/****************************************************************************
Kristofer Jonssond779a082023-01-04 17:09:47 +010059 * Rpmsg driver
Kristofer Jonsson116a6352020-08-20 17:25:23 +020060 ****************************************************************************/
61
Kristofer Jonssond779a082023-01-04 17:09:47 +010062static int ethosu_rpmsg_probe(struct rpmsg_device *rpdev)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020063{
Kristofer Jonsson116a6352020-08-20 17:25:23 +020064 int ret;
65
Kristofer Jonsson116a6352020-08-20 17:25:23 +020066 /* Initialize device */
Kristofer Jonsson074ef902023-01-23 13:05:36 +010067 ret = ethosu_dev_init(rpdev, ethosu_class, devt);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020068 if (ret)
Kristofer Jonssond779a082023-01-04 17:09:47 +010069 return ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020070
71 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020072}
73
Kristofer Jonssond779a082023-01-04 17:09:47 +010074static void ethosu_rpmsg_remove(struct rpmsg_device *rpdev)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020075{
Kristofer Jonssonec477042023-01-20 13:38:13 +010076 ethosu_dev_deinit(rpdev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020077}
78
Kristofer Jonssond779a082023-01-04 17:09:47 +010079static int ethosu_rpmsg_cb(struct rpmsg_device *rpdev,
80 void *data,
81 int len,
82 void *priv,
83 u32 src)
84{
85 dev_err(&rpdev->dev, "%s", __FUNCTION__);
86
87 return -EINVAL;
88}
89
90static struct rpmsg_device_id ethosu_rpmsg_driver_id_table[] = {
91 { .name = "ethos-u-0.0" },
92 {},
Kristofer Jonsson116a6352020-08-20 17:25:23 +020093};
94
Kristofer Jonssond779a082023-01-04 17:09:47 +010095MODULE_DEVICE_TABLE(rpmsg, ethosu_rpmsg_driver_id_table);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020096
Kristofer Jonssond779a082023-01-04 17:09:47 +010097static struct rpmsg_driver ethosu_rpmsg_driver = {
Mikael Olssoneb5a3282023-06-21 14:46:01 +020098 .drv = {
99 .name = ETHOSU_DRIVER_NAME,
100 .owner = THIS_MODULE,
101 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200102 },
Mikael Olssoneb5a3282023-06-21 14:46:01 +0200103 .id_table = ethosu_rpmsg_driver_id_table,
104 .probe = ethosu_rpmsg_probe,
105 .callback = ethosu_rpmsg_cb,
106 .remove = ethosu_rpmsg_remove,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200107};
108
109/****************************************************************************
110 * Module init and exit
111 ****************************************************************************/
112
Kristofer Jonssond779a082023-01-04 17:09:47 +0100113static void __exit ethosu_exit(void)
114{
115 unregister_rpmsg_driver(&ethosu_rpmsg_driver);
116 unregister_chrdev_region(devt, MINOR_COUNT);
117 class_destroy(ethosu_class);
118}
119
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200120static int __init ethosu_init(void)
121{
122 int ret;
123
124 ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
125 if (IS_ERR(ethosu_class)) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100126 pr_err("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200127
128 return PTR_ERR(ethosu_class);
129 }
130
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200131 ret = alloc_chrdev_region(&devt, MINOR_BASE, MINOR_COUNT,
132 ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200133 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100134 pr_err("Failed to allocate chrdev region.\n");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200135 goto destroy_class;
136 }
137
Kristofer Jonssond779a082023-01-04 17:09:47 +0100138 ret = register_rpmsg_driver(&ethosu_rpmsg_driver);
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200139 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100140 pr_err("Failed to register Arm Ethos-U rpmsg driver.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200141 goto region_unregister;
142 }
143
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200144 return 0;
145
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200146region_unregister:
147 unregister_chrdev_region(devt, MINOR_COUNT);
148
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200149destroy_class:
150 class_destroy(ethosu_class);
151
152 return ret;
153}
154
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200155module_init(ethosu_init)
156module_exit(ethosu_exit)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100157
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200158MODULE_LICENSE("GPL v2");
159MODULE_AUTHOR("Arm Ltd");
160MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
161MODULE_VERSION(ETHOSU_DRIVER_VERSION);