blob: 342f501254a1a5d1d084189282d3663727d30948 [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 = {
96 .drv = {
97 .name = ETHOSU_DRIVER_NAME,
98 .owner = THIS_MODULE,
Kristofer Jonsson116a6352020-08-20 17:25:23 +020099 },
Kristofer Jonssond779a082023-01-04 17:09:47 +0100100 .id_table = ethosu_rpmsg_driver_id_table,
101 .probe = ethosu_rpmsg_probe,
102 .callback = ethosu_rpmsg_cb,
103 .remove = ethosu_rpmsg_remove,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200104};
105
106/****************************************************************************
107 * Module init and exit
108 ****************************************************************************/
109
Kristofer Jonssond779a082023-01-04 17:09:47 +0100110static void __exit ethosu_exit(void)
111{
112 unregister_rpmsg_driver(&ethosu_rpmsg_driver);
113 unregister_chrdev_region(devt, MINOR_COUNT);
114 class_destroy(ethosu_class);
115}
116
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200117static int __init ethosu_init(void)
118{
119 int ret;
120
121 ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
122 if (IS_ERR(ethosu_class)) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100123 pr_err("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200124
125 return PTR_ERR(ethosu_class);
126 }
127
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200128 ret = alloc_chrdev_region(&devt, MINOR_BASE, MINOR_COUNT,
129 ETHOSU_DRIVER_NAME);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200130 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100131 pr_err("Failed to allocate chrdev region.\n");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200132 goto destroy_class;
133 }
134
Kristofer Jonssond779a082023-01-04 17:09:47 +0100135 ret = register_rpmsg_driver(&ethosu_rpmsg_driver);
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200136 if (ret) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100137 pr_err("Failed to register Arm Ethos-U rpmsg driver.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200138 goto region_unregister;
139 }
140
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141 return 0;
142
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200143region_unregister:
144 unregister_chrdev_region(devt, MINOR_COUNT);
145
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200146destroy_class:
147 class_destroy(ethosu_class);
148
149 return ret;
150}
151
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200152module_init(ethosu_init)
153module_exit(ethosu_exit)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100154
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200155MODULE_LICENSE("GPL v2");
156MODULE_AUTHOR("Arm Ltd");
157MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
158MODULE_VERSION(ETHOSU_DRIVER_VERSION);