1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
// SPDX-License-Identifier: GPL-2.0+
/*
* NXP PCI ECAM driver based on RPMSG
*
* Copyright 2024 NXP
*/
/* The nxp-pci-ecam-rpmsg transfer protocol:
*
* +---------------+-------------------------------+
* | Byte Offset | Content |
* +---------------+---+---+---+---+---+---+---+---+
* | 0 | Category |
* +---------------+---+---+---+---+---+---+---+---+
* | 1 ~ 2 | Version |
* +---------------+---+---+---+---+---+---+---+---+
* | 3 | Type |
* +---------------+---+---+---+---+---+---+---+---+
* | 4 | Command |
* +---------------+---+---+---+---+---+---+---+---+
* | 5 | Reserved0 |
* +---------------+---+---+---+---+---+---+---+---+
* | 6 | Reserved1 |
* +---------------+---+---+---+---+---+---+---+---+
* | 7 | Reserved2 |
* +---------------+---+---+---+---+---+---+---+---+
* | 8 | Reserved3 |
* +---------------+---+---+---+---+---+---+---+---+
* | 9 | Reserved4 |
* +---------------+---+---+---+---+---+---+---+---+
* | 10 | PCIe BUS Number |
* +---------------+---+---+---+---+---+---+---+---+
* | 11 | PCIe devfn |
* +---------------+---+---+---+---+---+---+---+---+
* | 12 ~ 13 | Register Offset |
* +---------------+---+---+---+---+---+---+---+---+
* | 14 | Size |
* +---------------+---+---+---+---+---+---+---+---+
* | 15 | ERROR Code |
* +---------------+---+---+---+---+---+---+---+---+
* | 16 ~ 19 | Register Value |
* +---------------+---+---+---+---+---+---+---+---+
*
* Command:
* 0x01 = Write the register of PCIe config space
*
* The Size of reading/writing the register:
* 0x01 = 1 byte
* 0x02 = 2 bytes (word)
* 0x04 = 4 bytes (dword)
*
* The definition of ERROR Code:
* 0x00 = Success
* 0x01 = Failed
*/
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/imx_rpmsg.h>
#include <linux/module.h>
#include <linux/pci-ecam.h>
#include <linux/rpmsg.h>
#include "../pci.h"
#define NXP_RPMSG_TIMEOUT 500 /* unit: ms */
#define NXP_NETC_RPMSG_CATEGORY 0x0c
#define NXP_NETC_RPMSG_VERSION 0x0100
#define NXP_RPMSG_TYPE_REQUEST 0x00
#define NXP_RPMSG_TYPE_RESPONSE 0x01
#define NXP_RPMSG_COMMAND_WRITE 0x01
/* Serialize access to the virtual config space */
static DEFINE_MUTEX(nxp_rpmsg_lock);
struct nxp_rpmsg_msg {
struct imx_rpmsg_head header;
/* Payload Start*/
u8 bus;
u8 devfn;
u16 reg;
u8 size;
u8 err_code;
u32 reg_val;
} __packed;
struct nxp_rpmsg_info {
struct rpmsg_device *rpdev;
int probe_status;
struct nxp_rpmsg_msg *msg;
struct completion cmd_complete;
u8 cmd;
u8 bus;
u8 devfn;
u16 reg;
};
static struct nxp_rpmsg_info nxp_rpmsg;
static int nxp_rpmsg_xmit(struct nxp_rpmsg_msg *msg)
{
int err;
reinit_completion(&nxp_rpmsg.cmd_complete);
nxp_rpmsg.cmd = msg->header.cmd;
nxp_rpmsg.bus = msg->bus;
nxp_rpmsg.devfn = msg->devfn;
nxp_rpmsg.reg = msg->reg;
err = rpmsg_send(nxp_rpmsg.rpdev->ept, (void *)msg,
sizeof(struct nxp_rpmsg_msg));
if (err) {
dev_err(&nxp_rpmsg.rpdev->dev, "rpmsg_send failed: %d\n", err);
return err;
}
err = wait_for_completion_timeout(&nxp_rpmsg.cmd_complete,
msecs_to_jiffies(NXP_RPMSG_TIMEOUT));
if (!err) {
dev_err(&nxp_rpmsg.rpdev->dev,
"rpmsg timeout: cmd:%u bus:%u devfn:%u\n",
msg->header.cmd, msg->bus, msg->devfn);
return -ETIME;
}
if (nxp_rpmsg.msg->err_code) {
dev_err(&nxp_rpmsg.rpdev->dev,
"cmd:%u rpmsg error: %u\n",
nxp_rpmsg.cmd, nxp_rpmsg.msg->err_code);
return -EREMOTEIO;
}
return 0;
}
static int nxp_rpmsg_probe(struct rpmsg_device *rpdev)
{
if (!rpdev) {
dev_info(&rpdev->dev, "%s failed, rpdev=NULL\n", __func__);
nxp_rpmsg.probe_status = -EINVAL;
return -EINVAL;
}
init_completion(&nxp_rpmsg.cmd_complete);
nxp_rpmsg.rpdev = rpdev;
dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
rpdev->src, rpdev->dst);
return 0;
}
static void nxp_rpmsg_remove(struct rpmsg_device *rpdev)
{
nxp_rpmsg.rpdev = NULL;
nxp_rpmsg.probe_status = 0;
dev_info(&rpdev->dev, "NXP PCI ECAM RPMSG driver is removed\n");
}
static int nxp_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len,
void *priv, u32 src)
{
struct nxp_rpmsg_msg *msg = (struct nxp_rpmsg_msg *)data;
if (msg->header.type != NXP_RPMSG_TYPE_RESPONSE)
return -EINVAL;
if (msg->bus != nxp_rpmsg.bus || msg->devfn != nxp_rpmsg.devfn ||
msg->reg != nxp_rpmsg.reg || msg->header.cmd != nxp_rpmsg.cmd) {
dev_err(&rpdev->dev,
"expected bus:%u devfn:%u reg:%u cmd:%u, received bus:%u devfn:%u reg:%u cmd:%u\n",
nxp_rpmsg.bus, nxp_rpmsg.devfn, nxp_rpmsg.reg, nxp_rpmsg.cmd,
msg->bus, msg->devfn, msg->reg, msg->header.cmd);
return -EINVAL;
}
/* Receive Success */
nxp_rpmsg.msg = msg;
complete(&nxp_rpmsg.cmd_complete);
return 0;
}
static struct rpmsg_device_id nxp_rpmsg_id_table[] = {
{ .name = "rpmsg-pci-channel" },
{ },
};
static struct rpmsg_driver nxp_rpmsg_driver = {
.drv.name = "nxp-pci-rpmsg",
.drv.owner = THIS_MODULE,
.id_table = nxp_rpmsg_id_table,
.probe = nxp_rpmsg_probe,
.remove = nxp_rpmsg_remove,
.callback = nxp_rpmsg_cb,
};
static const bool per_bus_mapping = !IS_ENABLED(CONFIG_64BIT);
static int nxp_rpmsg_pci_ecam_add_bus(struct pci_bus *bus)
{
struct pci_config_window *cfg = bus->sysdata;
unsigned int bsz = 1 << cfg->bus_shift;
unsigned int busn = bus->number;
phys_addr_t start;
if (!per_bus_mapping)
return 0;
if (busn < cfg->busr.start || busn > cfg->busr.end)
return -EINVAL;
busn -= cfg->busr.start;
start = cfg->res.start + busn * bsz;
cfg->winp[busn] = pci_remap_cfgspace(start, bsz);
if (!cfg->winp[busn])
return -ENOMEM;
return 0;
}
static void nxp_rpmsg_pci_ecam_remove_bus(struct pci_bus *bus)
{
struct pci_config_window *cfg = bus->sysdata;
unsigned int busn = bus->number;
if (!per_bus_mapping || busn < cfg->busr.start || busn > cfg->busr.end)
return;
busn -= cfg->busr.start;
if (cfg->winp[busn]) {
iounmap(cfg->winp[busn]);
cfg->winp[busn] = NULL;
}
}
static void __iomem *nxp_rpmsg_pci_ecam_map_bus(struct pci_bus *bus,
unsigned int devfn,
int where)
{
return pci_ecam_map_bus(bus, devfn, where);
}
static int nxp_rpmsg_pci_config_read(struct pci_bus *bus, unsigned int devfn,
int where, int size, u32 *val)
__must_hold(&pci_lock)
{
int ret;
raw_spin_unlock_irq(&pci_lock);
scoped_guard(mutex, &nxp_rpmsg_lock) {
ret = pci_generic_config_read(bus, devfn, where, size, val);
}
raw_spin_lock_irq(&pci_lock);
return ret;
}
static int nxp_rpmsg_pci_config_write(struct pci_bus *bus, unsigned int devfn,
int where, int size, u32 val, int msg_cate,
int msg_ver)
__must_hold(&pci_lock)
{
struct nxp_rpmsg_msg *msg __free(kfree);
int err;
raw_spin_unlock_irq(&pci_lock);
msg = kzalloc(sizeof(*msg), GFP_KERNEL);
if (!msg) {
err = -ENOMEM;
goto end;
}
msg->header.cate = msg_cate;
msg->header.major = msg_ver >> 8;
msg->header.minor = msg_ver & 0xff;
msg->header.type = NXP_RPMSG_TYPE_REQUEST;
msg->header.cmd = NXP_RPMSG_COMMAND_WRITE;
msg->bus = bus->number;
msg->devfn = devfn;
msg->reg = where;
msg->size = size;
msg->reg_val = val;
scoped_guard(mutex, &nxp_rpmsg_lock) {
err = nxp_rpmsg_xmit(msg);
}
end:
raw_spin_lock_irq(&pci_lock);
return err;
}
static int netc_rpmsg_pci_config_write(struct pci_bus *bus, unsigned int devfn,
int where, int size, u32 val)
{
return nxp_rpmsg_pci_config_write(bus, devfn, where, size, val,
NXP_NETC_RPMSG_CATEGORY,
NXP_NETC_RPMSG_VERSION);
}
static const struct pci_ecam_ops netc_rpmsg_pci_ecam_ops = {
.pci_ops = {
.add_bus = nxp_rpmsg_pci_ecam_add_bus,
.remove_bus = nxp_rpmsg_pci_ecam_remove_bus,
.map_bus = nxp_rpmsg_pci_ecam_map_bus,
.read = nxp_rpmsg_pci_config_read,
.write = netc_rpmsg_pci_config_write,
}
};
static const struct of_device_id nxp_rpmsg_pci_of_match[] = {
{ .compatible = "nxp,netc-rpmsg-pci-host-ecam",
.data = &netc_rpmsg_pci_ecam_ops },
{ },
};
MODULE_DEVICE_TABLE(of, nxp_rpmsg_pci_of_match);
static int nxp_pci_host_probe(struct platform_device *pdev)
{
if (nxp_rpmsg.probe_status)
return nxp_rpmsg.probe_status;
if (!nxp_rpmsg.rpdev)
return -EPROBE_DEFER;
return pci_host_common_probe(pdev);
}
static int nxp_pci_host_remove(struct platform_device *pdev)
{
return pci_host_common_remove(pdev);
}
static struct platform_driver nxp_rpmsg_pci_driver = {
.driver = {
.name = "nxp-rpmsg-pci-ecam",
.of_match_table = nxp_rpmsg_pci_of_match,
},
.probe = nxp_pci_host_probe,
.remove = nxp_pci_host_remove,
};
static int __init nxp_rpmsg_driver_init(void)
{
int ret = 0;
ret = register_rpmsg_driver(&nxp_rpmsg_driver);
if (ret < 0)
return ret;
return platform_driver_register(&nxp_rpmsg_pci_driver);
}
static void __exit nxp_rpmsg_driver_exit(void)
{
platform_driver_unregister(&nxp_rpmsg_pci_driver);
unregister_rpmsg_driver(&nxp_rpmsg_driver);
}
module_init(nxp_rpmsg_driver_init);
module_exit(nxp_rpmsg_driver_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("NXP PCI ECAM driver over RPMSG");
|