1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Microchip's PolarFire SoC (MPFS) System Controller Driver
4 *
5 * Copyright (C) 2024 Microchip Technology Inc. All rights reserved.
6 *
7 * Author: Jamie Gibbons <jamie.gibbons@microchip.com>
8 *
9 */
10
11 #include <asm/system.h>
12 #include <dm.h>
13 #include <dm/device_compat.h>
14 #include <env.h>
15 #include <errno.h>
16 #include <linux/compat.h>
17 #include <linux/completion.h>
18 #include <linux/err.h>
19 #include <linux/mtd/mtd.h>
20 #include <log.h>
21 #include <mailbox.h>
22 #include <misc.h>
23 #include <mpfs-mailbox.h>
24
25 /* Descriptor table */
26 #define CMD_OPCODE 0x0u
27 #define CMD_DATA_SIZE 0U
28 #define CMD_DATA NULL
29 #define MBOX_OFFSET 0x0
30 #define RESP_OFFSET 0x0
31 #define RESP_BYTES 16U
32
33 /**
34 * struct mpfs_syscontroller_priv - Structure representing System Controller data.
35 * @chan: Mailbox channel
36 * @c: Completion signal
37 */
38 struct mpfs_syscontroller_priv {
39 struct mbox_chan chan;
40 struct completion c;
41 };
42
43 /**
44 * mpfs_syscontroller_run_service() - Run the MPFS system service
45 * @sys_controller: corresponding MPFS system service device
46 * @msg: Message to send
47 *
48 * Return: 0 if all goes good, else appropriate error message.
49 */
mpfs_syscontroller_run_service(struct mpfs_syscontroller_priv * sys_controller,struct mpfs_mss_msg * msg)50 int mpfs_syscontroller_run_service(struct mpfs_syscontroller_priv *sys_controller, struct mpfs_mss_msg *msg)
51 {
52 int ret;
53
54 reinit_completion(&sys_controller->c);
55
56 /* Run the System Service Request */
57 ret = mbox_send(&sys_controller->chan, msg);
58 if (ret < 0)
59 dev_warn(sys_controller->chan.dev, "MPFS sys controller service timeout\n");
60
61 debug("%s: Service successful %s\n",
62 __func__, sys_controller->chan.dev->name);
63
64 return ret;
65 }
66 EXPORT_SYMBOL_GPL(mpfs_syscontroller_run_service);
67
68 /**
69 * mpfs_syscontroller_read_sernum() - Use system service to read the device serial number
70 * @sys_serv_priv: system service private data
71 * @device_serial_number: device serial number
72 *
73 * Return: 0 if all went ok, else return appropriate error
74 */
mpfs_syscontroller_read_sernum(struct mpfs_sys_serv * sys_serv_priv,u8 * device_serial_number)75 int mpfs_syscontroller_read_sernum(struct mpfs_sys_serv *sys_serv_priv, u8 *device_serial_number)
76 {
77 unsigned long timeoutsecs = 300;
78 int ret;
79
80 struct mpfs_mss_response response = {
81 .resp_status = 0U,
82 .resp_msg = (u32 *)device_serial_number,
83 .resp_size = RESP_BYTES};
84 struct mpfs_mss_msg msg = {
85 .cmd_opcode = CMD_OPCODE,
86 .cmd_data_size = CMD_DATA_SIZE,
87 .response = &response,
88 .cmd_data = CMD_DATA,
89 .mbox_offset = MBOX_OFFSET,
90 .resp_offset = RESP_OFFSET};
91
92 ret = mpfs_syscontroller_run_service(sys_serv_priv->sys_controller, &msg);
93 if (ret) {
94 dev_err(sys_serv_priv->sys_controller->chan.dev, "Service failed: %d, abort\n", ret);
95 return ret;
96 }
97
98 /* Receive the response */
99 ret = mbox_recv(&sys_serv_priv->sys_controller->chan, &msg, timeoutsecs);
100 if (ret) {
101 dev_err(sys_serv_priv->sys_controller->chan.dev, "Service failed: %d, abort. Failure: %u\n", ret, msg.response->resp_status);
102 return ret;
103 }
104
105 debug("%s: Read successful %s\n",
106 __func__, sys_serv_priv->sys_controller->chan.dev->name);
107
108 return 0;
109 }
110 EXPORT_SYMBOL(mpfs_syscontroller_read_sernum);
111
mpfs_syscontroller_probe(struct udevice * dev)112 static int mpfs_syscontroller_probe(struct udevice *dev)
113 {
114 struct mpfs_syscontroller_priv *sys_controller = dev_get_priv(dev);
115 int ret;
116
117 ret = mbox_get_by_index(dev, 0, &sys_controller->chan);
118 if (ret) {
119 dev_err(dev, "%s: Acquiring mailbox channel failed. ret = %d\n",
120 __func__, ret);
121 return ret;
122 }
123
124 init_completion(&sys_controller->c);
125 dev_info(dev, "Registered MPFS system controller\n");
126
127 return 0;
128 }
129
130 static const struct udevice_id mpfs_syscontroller_ids[] = {
131 { .compatible = "microchip,mpfs-sys-controller" },
132 { }
133 };
134
mpfs_syscontroller_get(struct udevice * dev)135 struct mpfs_syscontroller_priv *mpfs_syscontroller_get(struct udevice *dev)
136 {
137 struct mpfs_syscontroller_priv *sys_controller;
138
139 sys_controller = dev_get_priv(dev);
140 if (!sys_controller) {
141 debug("%s: MPFS system controller found but could not register as a sub device %p\n",
142 __func__, sys_controller);
143 return ERR_PTR(-EPROBE_DEFER);
144 }
145
146 return sys_controller;
147 }
148 EXPORT_SYMBOL(mpfs_syscontroller_get);
149
150 U_BOOT_DRIVER(mpfs_syscontroller) = {
151 .name = "mpfs_syscontroller",
152 .id = UCLASS_MISC,
153 .of_match = mpfs_syscontroller_ids,
154 .probe = mpfs_syscontroller_probe,
155 .priv_auto = sizeof(struct mpfs_syscontroller_priv),
156 };
157