blob: 48d215508dc54939b3c3290f496fb5ce6107e38e [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonssond779a082023-01-04 17:09:47 +01002 * Copyright 2020-2023 Arm Limited and/or its affiliates
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020021#include <linux/bitmap.h>
22#include <linux/fs.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020023#include <linux/module.h>
24#include <linux/io.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
Kristofer Jonssond779a082023-01-04 17:09:47 +010027#include <linux/rpmsg.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020028
29#include "ethosu_device.h"
Mikael Olssonf1cfe192023-06-12 15:00:41 +020030#include "uapi/ethosu.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020031
32/****************************************************************************
33 * Defines
34 ****************************************************************************/
35
Mikael Olssonf1cfe192023-06-12 15:00:41 +020036#define ETHOSU_DRIVER_STR(s) #s
37#define ETHOSU_DRIVER_VERSION_STR(major, minor, patch) \
38 ETHOSU_DRIVER_STR(major) "." \
39 ETHOSU_DRIVER_STR(minor) "." \
40 ETHOSU_DRIVER_STR(patch)
41#define ETHOSU_DRIVER_VERSION ETHOSU_DRIVER_VERSION_STR( \
42 ETHOSU_KERNEL_DRIVER_VERSION_MAJOR, \
43 ETHOSU_KERNEL_DRIVER_VERSION_MINOR, \
44 ETHOSU_KERNEL_DRIVER_VERSION_PATCH)
45
Kristofer Jonsson116a6352020-08-20 17:25:23 +020046#define ETHOSU_DRIVER_NAME "ethosu"
47
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020048#define MINOR_BASE 0 /* Minor version starts at 0 */
49#define MINOR_COUNT 64 /* Allocate minor versions */
50
Kristofer Jonsson116a6352020-08-20 17:25:23 +020051/****************************************************************************
52 * Variables
53 ****************************************************************************/
54
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020055static struct class *ethosu_class;
56
57static dev_t devt;
58
Kristofer Jonsson116a6352020-08-20 17:25:23 +020059/****************************************************************************
Kristofer Jonssond779a082023-01-04 17:09:47 +010060 * Rpmsg driver
Kristofer Jonsson116a6352020-08-20 17:25:23 +020061 ****************************************************************************/
62
Kristofer Jonssond779a082023-01-04 17:09:47 +010063static int ethosu_rpmsg_probe(struct rpmsg_device *rpdev)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020064{
Kristofer Jonsson116a6352020-08-20 17:25:23 +020065 int ret;
66
Kristofer Jonsson116a6352020-08-20 17:25:23 +020067 /* Initialize device */
Kristofer Jonsson074ef902023-01-23 13:05:36 +010068 ret = ethosu_dev_init(rpdev, ethosu_class, devt);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020069 if (ret)
Kristofer Jonssond779a082023-01-04 17:09:47 +010070 return ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020071
72 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020073}
74
Kristofer Jonssond779a082023-01-04 17:09:47 +010075static void ethosu_rpmsg_remove(struct rpmsg_device *rpdev)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020076{
Kristofer Jonssonec477042023-01-20 13:38:13 +010077 ethosu_dev_deinit(rpdev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020078}
79
Kristofer Jonssond779a082023-01-04 17:09:47 +010080static int ethosu_rpmsg_cb(struct rpmsg_device *rpdev,
81 void *data,
82 int len,
83 void *priv,
84 u32 src)
85{
86 dev_err(&rpdev->dev, "%s", __FUNCTION__);
87
88 return -EINVAL;
89}
90
91static struct rpmsg_device_id ethosu_rpmsg_driver_id_table[] = {
92 { .name = "ethos-u-0.0" },
93 {},
Kristofer Jonsson116a6352020-08-20 17:25:23 +020094};
95
Kristofer Jonssond779a082023-01-04 17:09:47 +010096MODULE_DEVICE_TABLE(rpmsg, ethosu_rpmsg_driver_id_table);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020097
Kristofer Jonssond779a082023-01-04 17:09:47 +010098static struct rpmsg_driver ethosu_rpmsg_driver = {
Mikael Olssoneb5a3282023-06-21 14:46:01 +020099 .drv = {
100 .name = ETHOSU_DRIVER_NAME,
101 .owner = THIS_MODULE,
102 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200103 },
Mikael Olssoneb5a3282023-06-21 14:46:01 +0200104 .id_table = ethosu_rpmsg_driver_id_table,
105 .probe = ethosu_rpmsg_probe,
106 .callback = ethosu_rpmsg_cb,
107 .remove = ethosu_rpmsg_remove,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200108};
109
110/****************************************************************************
111 * Module init and exit
112 ****************************************************************************/
113
Kristofer Jonssond779a082023-01-04 17:09:47 +0100114static void __exit ethosu_exit(void)
115{
116 unregister_rpmsg_driver(&ethosu_rpmsg_driver);
117 unregister_chrdev_region(devt, MINOR_COUNT);
118 class_destroy(ethosu_class);
119}
120
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200121static int __init ethosu_init(void)
122{
123 int ret;
124
125 ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
126 if (IS_ERR(ethosu_class)) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100127 pr_err("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200128
129 return PTR_ERR(ethosu_class);
130 }
131
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200132 ret = alloc_chrdev_region(&devt, MINOR_BASE, MINOR_COUNT,
133 ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200134 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100135 pr_err("Failed to allocate chrdev region.\n");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200136 goto destroy_class;
137 }
138
Kristofer Jonssond779a082023-01-04 17:09:47 +0100139 ret = register_rpmsg_driver(&ethosu_rpmsg_driver);
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200140 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100141 pr_err("Failed to register Arm Ethos-U rpmsg driver.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200142 goto region_unregister;
143 }
144
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200145 return 0;
146
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200147region_unregister:
148 unregister_chrdev_region(devt, MINOR_COUNT);
149
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200150destroy_class:
151 class_destroy(ethosu_class);
152
153 return ret;
154}
155
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156module_init(ethosu_init)
157module_exit(ethosu_exit)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100158
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200159MODULE_LICENSE("GPL v2");
160MODULE_AUTHOR("Arm Ltd");
161MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
162MODULE_VERSION(ETHOSU_DRIVER_VERSION);