1
|
/* (C) 2011-2012 by Harald Welte <laforge@gnumonks.org>
|
2
|
*
|
3
|
* All Rights Reserved
|
4
|
*
|
5
|
* This program is free software; you can redistribute it and/or modify
|
6
|
* it under the terms of the GNU General Public License as published by
|
7
|
* the Free Software Foundation; either version 3 of the License, or
|
8
|
* (at your option) any later version.
|
9
|
*
|
10
|
* This program is distributed in the hope that it will be useful,
|
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
* GNU General Public License for more details.
|
14
|
*
|
15
|
* You should have received a copy of the GNU General Public License
|
16
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
*/
|
18
|
|
19
|
|
20
|
#include <stdint.h>
|
21
|
#include <errno.h>
|
22
|
#include <string.h>
|
23
|
|
24
|
#include <uart_cmd.h>
|
25
|
#include <reg_field.h>
|
26
|
|
27
|
uint32_t reg_field_read(struct reg_field_ops *ops, struct reg_field *field)
|
28
|
{
|
29
|
uint32_t rc = ops->read_cb(ops->data, field->reg);
|
30
|
|
31
|
return (rc >> field->shift) & ((1 << field->width)-1);
|
32
|
}
|
33
|
|
34
|
int reg_field_write(struct reg_field_ops *ops, struct reg_field *field, uint32_t val)
|
35
|
{
|
36
|
uint32_t old = ops->read_cb(ops->data, field->reg);
|
37
|
uint32_t mask, newreg;
|
38
|
|
39
|
mask = ((1 << field->width)-1) << field->shift;
|
40
|
newreg = (old & ~mask) | ((val << field->shift) & mask);
|
41
|
|
42
|
return ops->write_cb(ops->data, field->reg, newreg);
|
43
|
}
|
44
|
|
45
|
int reg_field_cmd(struct cmd_state *cs, enum cmd_op op,
|
46
|
const char *cmd, int argc, char **argv,
|
47
|
struct reg_field_ops *ops)
|
48
|
{
|
49
|
uint32_t tmp;
|
50
|
int i;
|
51
|
|
52
|
for (i = 0; i < ops->num_fields; i++) {
|
53
|
if (strcmp(ops->field_names[i], cmd))
|
54
|
continue;
|
55
|
|
56
|
switch (op) {
|
57
|
case CMD_OP_SET:
|
58
|
if (argc < 1)
|
59
|
return -EINVAL;
|
60
|
|
61
|
reg_field_write(ops, &ops->fields[i], atoi(argv[0]));
|
62
|
return 0;
|
63
|
break;
|
64
|
case CMD_OP_GET:
|
65
|
tmp = reg_field_read(ops, &ops->fields[i]);
|
66
|
uart_cmd_out(cs, "%s:%u\n\r", cmd, tmp);
|
67
|
return 0;
|
68
|
break;
|
69
|
default:
|
70
|
return -EINVAL;
|
71
|
break;
|
72
|
}
|
73
|
}
|
74
|
return -EINVAL;
|
75
|
}
|