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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
// SPDX-License-Identifier: GPL-2.0
/*
* PRU-ICSS platform driver for various TI SoCs
*
* Copyright (C) 2014-2018 Texas Instruments Incorporated - http://www.ti.com/
* Suman Anna <s-anna@ti.com>
* Andrew F. Davis <afd@ti.com>
* Tero Kristo <t-kristo@ti.com>
*/
#include <linux/dma-mapping.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/regmap.h>
#include <linux/mfd/syscon.h>
#include "pruss.h"
/**
* struct pruss_private_data - PRUSS driver private data
* @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
*/
struct pruss_private_data {
bool has_no_sharedram;
};
/**
* struct pruss_match_private_data - private data to handle multiple instances
* @device_name: device name of the PRUSS instance
* @priv_data: PRUSS driver private data for this PRUSS instance
*/
struct pruss_match_private_data {
const char *device_name;
const struct pruss_private_data *priv_data;
};
/**
* pruss_get() - get the pruss for a given PRU remoteproc
* @rproc: remoteproc handle of a PRU instance
*
* Finds the parent pruss device for a PRU given the @rproc handle of the
* PRU remote processor. This function increments the pruss device's refcount,
* so always use pruss_put() to decrement it back once pruss isn't needed
* anymore.
*
* Returns the pruss handle on success, and an ERR_PTR on failure using one
* of the following error values
* -EINVAL if invalid parameter
* -ENODEV if PRU device or PRUSS device is not found
*/
struct pruss *pruss_get(struct rproc *rproc)
{
struct pruss *pruss;
struct device *dev;
struct platform_device *ppdev;
if (IS_ERR_OR_NULL(rproc))
return ERR_PTR(-EINVAL);
dev = &rproc->dev;
if (!dev->parent)
return ERR_PTR(-ENODEV);
/* rudimentary check to make sure rproc handle is for a PRU or RTU */
if (!strstr(dev_name(dev->parent), "pru") &&
!strstr(dev_name(dev->parent), "rtu"))
return ERR_PTR(-ENODEV);
ppdev = to_platform_device(dev->parent->parent);
pruss = platform_get_drvdata(ppdev);
if (pruss)
get_device(pruss->dev);
return pruss ? pruss : ERR_PTR(-ENODEV);
}
EXPORT_SYMBOL_GPL(pruss_get);
/**
* pruss_put() - decrement pruss device's usecount
* @pruss: pruss handle
*
* Complimentary function for pruss_get(). Needs to be called
* after the PRUSS is used, and only if the pruss_get() succeeds.
*/
void pruss_put(struct pruss *pruss)
{
if (IS_ERR_OR_NULL(pruss))
return;
put_device(pruss->dev);
}
EXPORT_SYMBOL_GPL(pruss_put);
/**
* pruss_regmap_read() - read a PRUSS syscon sub-module register
* @pruss: the pruss instance handle
* @mod: the pruss syscon sub-module identifier
* @reg: register offset within the sub-module
* @val: pointer to return the value in
*
* Reads a given register within one of the PRUSS sub-modules represented
* by a syscon and returns it through the passed-in @val pointer
*
* Returns 0 on success, or an error code otherwise
*/
int pruss_regmap_read(struct pruss *pruss, enum pruss_syscon mod,
unsigned int reg, unsigned int *val)
{
struct regmap *map;
if (IS_ERR_OR_NULL(pruss) || mod < PRUSS_SYSCON_CFG ||
mod >= PRUSS_SYSCON_MAX)
return -EINVAL;
map = (mod == PRUSS_SYSCON_CFG) ? pruss->cfg :
((mod == PRUSS_SYSCON_IEP ? pruss->iep : pruss->mii_rt));
return regmap_read(map, reg, val);
}
EXPORT_SYMBOL_GPL(pruss_regmap_read);
/**
* pruss_regmap_update() - configure a PRUSS syscon sub-module register
* @pruss: the pruss instance handle
* @mod: the pruss syscon sub-module identifier
* @reg: register offset within the sub-module
* @mask: bit mask to use for programming the @val
* @val: value to write
*
* Programs a given register within one of the PRUSS sub-modules represented
* by a syscon
*
* Returns 0 on success, or an error code otherwise
*/
int pruss_regmap_update(struct pruss *pruss, enum pruss_syscon mod,
unsigned int reg, unsigned int mask, unsigned int val)
{
struct regmap *map;
if (IS_ERR_OR_NULL(pruss) || mod < PRUSS_SYSCON_CFG ||
mod >= PRUSS_SYSCON_MAX)
return -EINVAL;
map = (mod == PRUSS_SYSCON_CFG) ? pruss->cfg :
((mod == PRUSS_SYSCON_IEP ? pruss->iep : pruss->mii_rt));
return regmap_update_bits(map, reg, mask, val);
}
EXPORT_SYMBOL_GPL(pruss_regmap_update);
/**
* pruss_request_mem_region() - request a memory resource
* @pruss: the pruss instance
* @mem_id: the memory resource id
* @region: pointer to memory region structure to be filled in
*
* This function allows a client driver to request a memory resource,
* and if successful, will let the client driver own the particular
* memory region until released using the pruss_release_mem_region()
* API.
*
* Returns the memory region if requested resource is available, an
* error otherwise
*/
int pruss_request_mem_region(struct pruss *pruss, enum pruss_mem mem_id,
struct pruss_mem_region *region)
{
if (!pruss || !region)
return -EINVAL;
if (mem_id >= PRUSS_MEM_MAX)
return -EINVAL;
mutex_lock(&pruss->lock);
if (pruss->mem_in_use[mem_id]) {
mutex_unlock(&pruss->lock);
return -EBUSY;
}
*region = pruss->mem_regions[mem_id];
pruss->mem_in_use[mem_id] = region;
mutex_unlock(&pruss->lock);
return 0;
}
EXPORT_SYMBOL_GPL(pruss_request_mem_region);
/**
* pruss_release_mem_region() - release a memory resource
* @pruss: the pruss instance
* @region: the memory region to release
*
* This function is the complimentary function to
* pruss_request_mem_region(), and allows the client drivers to
* release back a memory resource.
*
* Returns 0 on success, an error code otherwise
*/
int pruss_release_mem_region(struct pruss *pruss,
struct pruss_mem_region *region)
{
int id;
if (!pruss || !region)
return -EINVAL;
mutex_lock(&pruss->lock);
/* find out the memory region being released */
for (id = 0; id < PRUSS_MEM_MAX; id++) {
if (pruss->mem_in_use[id] == region)
break;
}
if (id == PRUSS_MEM_MAX) {
mutex_unlock(&pruss->lock);
return -EINVAL;
}
pruss->mem_in_use[id] = NULL;
mutex_unlock(&pruss->lock);
return 0;
}
EXPORT_SYMBOL_GPL(pruss_release_mem_region);
/* Custom configuration of couple of PRUSS clocks only on AM65x SoCs */
static int pruss_configure_clocks(struct platform_device *pdev,
struct pruss *pruss)
{
int ret;
if (!of_device_is_compatible(pdev->dev.of_node, "ti,am654-icssg"))
return 0;
ret = pruss_regmap_update(pruss, PRUSS_SYSCON_CFG, ICSSG_CFG_CORE_SYNC,
ICSSG_CORE_VBUSP_SYNC_EN,
ICSSG_CORE_VBUSP_SYNC_EN);
if (ret)
return ret;
ret = pruss_regmap_update(pruss, PRUSS_SYSCON_CFG, PRUSS_CFG_IEPCLK,
PRUSS_IEPCLK_IEP_OCP_CLK_EN,
PRUSS_IEPCLK_IEP_OCP_CLK_EN);
if (ret)
return ret;
return 0;
}
static const
struct pruss_private_data *pruss_get_private_data(struct platform_device *pdev)
{
const struct pruss_match_private_data *data;
if (!of_device_is_compatible(pdev->dev.of_node, "ti,am4376-pruss"))
return NULL;
data = of_device_get_match_data(&pdev->dev);
for (; data && data->device_name; data++) {
if (!strcmp(dev_name(&pdev->dev), data->device_name))
return data->priv_data;
}
return ERR_PTR(-ENODEV);
}
static int pruss_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
struct device_node *np;
struct pruss *pruss;
struct resource res;
int ret, i, index;
const struct pruss_private_data *data;
const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
if (!node) {
dev_err(dev, "Non-DT platform device not supported\n");
return -ENODEV;
}
data = pruss_get_private_data(pdev);
if (IS_ERR(data)) {
dev_err(dev, "missing private data\n");
return -ENODEV;
}
ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
if (ret) {
dev_err(dev, "dma_set_coherent_mask: %d\n", ret);
return ret;
}
pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
if (!pruss)
return -ENOMEM;
pruss->dev = dev;
mutex_init(&pruss->lock);
np = of_get_child_by_name(node, "cfg");
if (!np)
return -ENODEV;
pruss->cfg = syscon_node_to_regmap(np);
of_node_put(np);
if (IS_ERR(pruss->cfg))
return -ENODEV;
np = of_get_child_by_name(node, "iep");
if (!np)
return -ENODEV;
pruss->iep = syscon_node_to_regmap(np);
of_node_put(np);
if (IS_ERR(pruss->iep))
return -ENODEV;
np = of_get_child_by_name(node, "mii_rt");
if (!np)
return -ENODEV;
pruss->mii_rt = syscon_node_to_regmap(np);
of_node_put(np);
if (IS_ERR(pruss->mii_rt))
return -ENODEV;
np = of_get_child_by_name(node, "memories");
if (!np)
return -ENODEV;
for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
if (data && data->has_no_sharedram &&
!strcmp(mem_names[i], "shrdram2"))
continue;
index = of_property_match_string(np, "reg-names", mem_names[i]);
if (index < 0) {
of_node_put(np);
return index;
}
if (of_address_to_resource(np, index, &res)) {
of_node_put(np);
return -EINVAL;
}
pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
resource_size(&res));
if (!pruss->mem_regions[i].va) {
dev_err(dev, "failed to parse and map memory resource %d %s\n",
i, mem_names[i]);
of_node_put(np);
return -ENOMEM;
}
pruss->mem_regions[i].pa = res.start;
pruss->mem_regions[i].size = resource_size(&res);
dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %p\n",
mem_names[i], &pruss->mem_regions[i].pa,
pruss->mem_regions[i].size, pruss->mem_regions[i].va);
}
of_node_put(np);
platform_set_drvdata(pdev, pruss);
ret = pruss_configure_clocks(pdev, pruss);
if (ret) {
dev_err(dev, "clock frequency config failed, ret = %d\n", ret);
return ret;
}
dev_info(&pdev->dev, "creating PRU cores and other child platform devices\n");
ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
if (ret)
dev_err(dev, "of_platform_populate failed\n");
return ret;
}
static int pruss_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
dev_info(dev, "remove PRU cores and other child platform devices\n");
of_platform_depopulate(dev);
return 0;
}
/* instance-specific driver private data */
static const struct pruss_private_data am437x_pruss1_priv_data = {
.has_no_sharedram = false,
};
static const struct pruss_private_data am437x_pruss0_priv_data = {
.has_no_sharedram = true,
};
static const struct pruss_match_private_data am437x_match_data[] = {
{
.device_name = "54400000.pruss",
.priv_data = &am437x_pruss1_priv_data,
},
{
.device_name = "54440000.pruss",
.priv_data = &am437x_pruss0_priv_data,
},
{
/* sentinel */
},
};
static const struct of_device_id pruss_of_match[] = {
{ .compatible = "ti,am3356-pruss", .data = NULL, },
{ .compatible = "ti,am4376-pruss", .data = &am437x_match_data, },
{ .compatible = "ti,am5728-pruss", .data = NULL, },
{ .compatible = "ti,k2g-pruss", .data = NULL, },
{ .compatible = "ti,am654-icssg", .data = NULL, },
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, pruss_of_match);
static struct platform_driver pruss_driver = {
.driver = {
.name = "pruss",
.of_match_table = pruss_of_match,
},
.probe = pruss_probe,
.remove = pruss_remove,
};
module_platform_driver(pruss_driver);
MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
MODULE_LICENSE("GPL v2");
|