blob: f35c5e5ba4582e428b9f4268add7d4f611f95a99 [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"
30
31/****************************************************************************
32 * Defines
33 ****************************************************************************/
34
35#define ETHOSU_DRIVER_VERSION "1.0"
36#define ETHOSU_DRIVER_NAME "ethosu"
37
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020038#define MINOR_BASE 0 /* Minor version starts at 0 */
39#define MINOR_COUNT 64 /* Allocate minor versions */
40
Kristofer Jonsson116a6352020-08-20 17:25:23 +020041/****************************************************************************
42 * Variables
43 ****************************************************************************/
44
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020045static struct class *ethosu_class;
46
47static dev_t devt;
48
Kristofer Jonsson116a6352020-08-20 17:25:23 +020049/****************************************************************************
Kristofer Jonssond779a082023-01-04 17:09:47 +010050 * Rpmsg driver
Kristofer Jonsson116a6352020-08-20 17:25:23 +020051 ****************************************************************************/
52
Kristofer Jonssond779a082023-01-04 17:09:47 +010053static int ethosu_rpmsg_probe(struct rpmsg_device *rpdev)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020054{
Kristofer Jonssond779a082023-01-04 17:09:47 +010055 struct device *dev = &rpdev->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020056 int ret;
57
Kristofer Jonsson074ef902023-01-23 13:05:36 +010058 dev_info(dev, "%s", __FUNCTION__);
Kristofer Jonsson4aec3762021-10-13 17:09:27 +020059
Kristofer Jonsson116a6352020-08-20 17:25:23 +020060 /* Initialize device */
Kristofer Jonsson074ef902023-01-23 13:05:36 +010061 ret = ethosu_dev_init(rpdev, ethosu_class, devt);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020062 if (ret)
Kristofer Jonssond779a082023-01-04 17:09:47 +010063 return ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020064
65 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020066}
67
Kristofer Jonssond779a082023-01-04 17:09:47 +010068static void ethosu_rpmsg_remove(struct rpmsg_device *rpdev)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020069{
Kristofer Jonssonec477042023-01-20 13:38:13 +010070 struct device *dev = &rpdev->dev;
Kristofer Jonssond779a082023-01-04 17:09:47 +010071
Kristofer Jonssonec477042023-01-20 13:38:13 +010072 dev_info(dev, "%s", __FUNCTION__);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020073
Kristofer Jonssonec477042023-01-20 13:38:13 +010074 ethosu_dev_deinit(rpdev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020075}
76
Kristofer Jonssond779a082023-01-04 17:09:47 +010077static int ethosu_rpmsg_cb(struct rpmsg_device *rpdev,
78 void *data,
79 int len,
80 void *priv,
81 u32 src)
82{
83 dev_err(&rpdev->dev, "%s", __FUNCTION__);
84
85 return -EINVAL;
86}
87
88static struct rpmsg_device_id ethosu_rpmsg_driver_id_table[] = {
89 { .name = "ethos-u-0.0" },
90 {},
Kristofer Jonsson116a6352020-08-20 17:25:23 +020091};
92
Kristofer Jonssond779a082023-01-04 17:09:47 +010093MODULE_DEVICE_TABLE(rpmsg, ethosu_rpmsg_driver_id_table);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020094
Kristofer Jonssond779a082023-01-04 17:09:47 +010095static struct rpmsg_driver ethosu_rpmsg_driver = {
Mikael Olssoneb5a3282023-06-21 14:46:01 +020096 .drv = {
97 .name = ETHOSU_DRIVER_NAME,
98 .owner = THIS_MODULE,
99 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200100 },
Mikael Olssoneb5a3282023-06-21 14:46:01 +0200101 .id_table = ethosu_rpmsg_driver_id_table,
102 .probe = ethosu_rpmsg_probe,
103 .callback = ethosu_rpmsg_cb,
104 .remove = ethosu_rpmsg_remove,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200105};
106
107/****************************************************************************
108 * Module init and exit
109 ****************************************************************************/
110
Kristofer Jonssond779a082023-01-04 17:09:47 +0100111static void __exit ethosu_exit(void)
112{
113 unregister_rpmsg_driver(&ethosu_rpmsg_driver);
114 unregister_chrdev_region(devt, MINOR_COUNT);
115 class_destroy(ethosu_class);
116}
117
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200118static int __init ethosu_init(void)
119{
120 int ret;
121
122 ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
123 if (IS_ERR(ethosu_class)) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100124 pr_err("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200125
126 return PTR_ERR(ethosu_class);
127 }
128
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200129 ret = alloc_chrdev_region(&devt, MINOR_BASE, MINOR_COUNT,
130 ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200131 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100132 pr_err("Failed to allocate chrdev region.\n");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200133 goto destroy_class;
134 }
135
Kristofer Jonssond779a082023-01-04 17:09:47 +0100136 ret = register_rpmsg_driver(&ethosu_rpmsg_driver);
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200137 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100138 pr_err("Failed to register Arm Ethos-U rpmsg driver.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200139 goto region_unregister;
140 }
141
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200142 return 0;
143
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200144region_unregister:
145 unregister_chrdev_region(devt, MINOR_COUNT);
146
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200147destroy_class:
148 class_destroy(ethosu_class);
149
150 return ret;
151}
152
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200153module_init(ethosu_init)
154module_exit(ethosu_exit)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100155
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156MODULE_LICENSE("GPL v2");
157MODULE_AUTHOR("Arm Ltd");
158MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
159MODULE_VERSION(ETHOSU_DRIVER_VERSION);