GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / reset / tegra / reset-bpmp.c
1 /*
2  * Copyright (C) 2016 NVIDIA Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/reset-controller.h>
10
11 #include <soc/tegra/bpmp.h>
12 #include <soc/tegra/bpmp-abi.h>
13
14 static struct tegra_bpmp *to_tegra_bpmp(struct reset_controller_dev *rstc)
15 {
16         return container_of(rstc, struct tegra_bpmp, rstc);
17 }
18
19 static int tegra_bpmp_reset_common(struct reset_controller_dev *rstc,
20                                    enum mrq_reset_commands command,
21                                    unsigned int id)
22 {
23         struct tegra_bpmp *bpmp = to_tegra_bpmp(rstc);
24         struct mrq_reset_request request;
25         struct tegra_bpmp_message msg;
26         int err;
27
28         memset(&request, 0, sizeof(request));
29         request.cmd = command;
30         request.reset_id = id;
31
32         memset(&msg, 0, sizeof(msg));
33         msg.mrq = MRQ_RESET;
34         msg.tx.data = &request;
35         msg.tx.size = sizeof(request);
36
37         err = tegra_bpmp_transfer(bpmp, &msg);
38         if (err)
39                 return err;
40         if (msg.rx.ret)
41                 return -EINVAL;
42
43         return 0;
44 }
45
46 static int tegra_bpmp_reset_module(struct reset_controller_dev *rstc,
47                                    unsigned long id)
48 {
49         return tegra_bpmp_reset_common(rstc, CMD_RESET_MODULE, id);
50 }
51
52 static int tegra_bpmp_reset_assert(struct reset_controller_dev *rstc,
53                                    unsigned long id)
54 {
55         return tegra_bpmp_reset_common(rstc, CMD_RESET_ASSERT, id);
56 }
57
58 static int tegra_bpmp_reset_deassert(struct reset_controller_dev *rstc,
59                                      unsigned long id)
60 {
61         return tegra_bpmp_reset_common(rstc, CMD_RESET_DEASSERT, id);
62 }
63
64 static const struct reset_control_ops tegra_bpmp_reset_ops = {
65         .reset = tegra_bpmp_reset_module,
66         .assert = tegra_bpmp_reset_assert,
67         .deassert = tegra_bpmp_reset_deassert,
68 };
69
70 int tegra_bpmp_init_resets(struct tegra_bpmp *bpmp)
71 {
72         bpmp->rstc.ops = &tegra_bpmp_reset_ops;
73         bpmp->rstc.owner = THIS_MODULE;
74         bpmp->rstc.of_node = bpmp->dev->of_node;
75         bpmp->rstc.nr_resets = bpmp->soc->num_resets;
76
77         return devm_reset_controller_register(bpmp->dev, &bpmp->rstc);
78 }