am335x.c 6.86 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
Jeroen Vreeken's avatar
Jeroen Vreeken committed
2
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014, 2015
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include "am335x.h"

#include <log/log.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

struct map {
	size_t base;
	size_t size;
	void *map;
	
	struct map *next;
};

static struct map *mapped_list = NULL;

#define AM335X_MIN_SIZE 4096

void *am335x_mem(size_t base, size_t size)
{
	int fd;
	void *map;
	struct map *entry;
	
	size = (size + (AM335X_MIN_SIZE - 1)) & ~(AM335X_MIN_SIZE - 1);
	
	for (entry = mapped_list; entry; entry = entry->next) {
		if (base >= entry->base && 
		    (base + size) <= (entry->base + entry->size)) {
			char *tmp = entry->map;
			tmp += base - entry->base;
			
			return tmp;
		}
	}
	
	entry = calloc(1,sizeof(struct map));
	if (!entry) {
		log_send(LOG_T_ERROR, "am335x: calloc() failed");
		goto err_calloc;
	}
	
	if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0 ) {
		log_send(LOG_T_ERROR, "am335x: Error opening /dev/mem");
		goto err_open;
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
70
71
72
73
	map = mmap(NULL, size, 
	    PROT_READ | PROT_WRITE, 
	    MAP_SHARED | MAP_LOCKED,
	    fd, base);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
74
75
76
77
78
79
80
81
82
83
	close(fd);
	if (!map) {
		log_send(LOG_T_ERROR, "am335x: mmap 0x%zx bytes @ 0x%08zx failed",
		    size, base);
		goto err_mmap;
	}
	
	log_send(LOG_T_DEBUG, "am335x: 0x%zx bytes mapped @ 0x%08zx",
	    size, base);
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
84
85
86
	entry->base = base;
	entry->size = size;
	entry->map = map;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
87
88
89
90
91
92
93
94
95
96
	entry->next = mapped_list;
	mapped_list = entry;
	
	return map;
err_mmap:
err_open:
	free(entry);
err_calloc:
	return NULL;
}
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

static void *cm_base;

static int am335x_cm_init(void)
{
	cm_base = am335x_mem(AM335X_CM_BASE, AM335X_CM_SIZE);
	
	return cm_base == NULL;
}

int am335x_cm_enable(size_t cm_off)
{
	uint32_t reg;
	
	if (!cm_base) {
		if (am335x_cm_init()) {
			log_send(LOG_T_ERROR, "am335x: CM init failed");
			return -1;
		}
	}
	
	reg = am335x_read32(cm_base, cm_off);
	log_send(LOG_T_DEBUG, "am335x: CM %x: 0x%08x", cm_off, reg);
	reg &= AM335X_CM_IDLEST_MASK;
	if (reg == AM335X_CM_IDLEST_FUNCTIONAL)
122
		log_send(LOG_T_DEBUG, "am335x: CM 0x%x: state: functional", cm_off);
123
	if (reg == AM335X_CM_IDLEST_TRANSITION)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
124
		log_send(LOG_T_DEBUG, "am335x: CM 0x%x: state: transitioning", cm_off);
125
	if (reg == AM335X_CM_IDLEST_IDLE)
126
		log_send(LOG_T_DEBUG, "am335x: CM 0x%x: state: idle", cm_off);
127
	if (reg == AM335X_CM_IDLEST_DISABLED)
128
		log_send(LOG_T_DEBUG, "am335x: CM 0x%x: state: disabled", cm_off);
129
130
131

	log_send(LOG_T_DEBUG, "am335x: CM %x: enabling", cm_off);
	am335x_write32(cm_base, cm_off, AM335X_CM_MODULEMODE_ENABLE);
132
133
	
	return 0;
134
135
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
136
137
138
139
140
141
142
143
144
145
146

static void *control_module_base;

static int am335x_control_module_init(void)
{
	control_module_base = 
	    am335x_mem(AM335X_CONTROL_MODULE_BASE, AM335X_CONTROL_MODULE_SIZE);
	
	return control_module_base == NULL;
}

147
148
149
150
151
152
153
154
155
156
157
158
159
int am335x_control_module_set(size_t offset, uint32_t bits)
{
	uint32_t reg;
	
	if (!control_module_base) {
		if (am335x_control_module_init()) {
			log_send(LOG_T_ERROR, "am335x: CONTROL_MODULE init failed");
			return -1;
		}
	}

	reg = am335x_read32(control_module_base, offset);
	am335x_write32(control_module_base, offset, reg | bits);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
160
161
	
	return 0;
162
163
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
int am335x_pinmux_debug(size_t offset)
{
	uint32_t reg;
	
	if (!control_module_base) {
		if (am335x_control_module_init()) {
			log_send(LOG_T_ERROR, "am335x: CONTROL_MODULE init failed");
			return -1;
		}
	}

	reg = am335x_read32(control_module_base, 
	    offset + AM335X_CONTROL_MODULE_CONF);
	
	log_send(LOG_T_DEBUG, "am335x: pin @0x%x: 0x%08x: %s, %s, %s, mode %d",
	    offset, reg,
	    reg & AM335X_CONTROL_MODULE_PINMUX_SLEWCTRL_SLOW ? "slow" : "fast",
	    reg & AM335X_CONTROL_MODULE_PINMUX_RX ? "input" : "output",
	    reg & AM335X_CONTROL_MODULE_PINMUX_PUEN ? 
	    (reg & AM335X_CONTROL_MODULE_PINMUX_PU ? "pull-up" : "pull-down") :
	    "open",
	    reg & AM335X_CONTROL_MODULE_PINMUX_MODE_MASK);

	return 0;
}

int am335x_pinmux_set(size_t offset, unsigned mode, unsigned flags)
{
	uint32_t reg, regr;
	int ret = 0;
	
	if (!control_module_base) {
		if (am335x_control_module_init()) {
			log_send(LOG_T_ERROR, "am335x: CONTROL_MODULE init failed");
			return -1;
		}
	}

	reg = (mode & AM335X_CONTROL_MODULE_PINMUX_MODE_MASK) | flags;
//	log_send(LOG_T_DEBUG, "am335x: mode: %d reg: 0x%08x", mode, reg);
	am335x_write32(control_module_base, offset + AM335X_CONTROL_MODULE_CONF,
	    reg);
	regr = am335x_read32(control_module_base, offset + AM335X_CONTROL_MODULE_CONF);
	if (regr != reg) {
		log_send(LOG_T_ERROR, "am335x: pinmux cannot be set to 0x%08x: 0x%08x",
		    reg, regr);
		ret = -1;
	}
	am335x_pinmux_debug(offset);
	
	return ret;
}
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

struct am335x_pinmux_gpio_entry {
	int gpio;
	int pin;
	size_t offset;
};

static struct am335x_pinmux_gpio_entry am335x_pinmux_gpio_table[] = {
	{ 0, 2, 0x150 },
	{ 0, 3, 0x154 },
	{ 0, 4, 0x158 },
	{ 0, 5, 0x15c },
	{ 0, 7, 0x164 },
	{ 0, 8, 0x0d0 },
	{ 0, 9, 0x0d4 },
	{ 0, 10, 0x0d8 },
	{ 0, 11, 0x0dc },
	{ 0, 12, 0x178 },
	{ 0, 13, 0x17c },
	{ 0, 14, 0x180 },
	{ 0, 15, 0x184 },
	{ 0, 20, 0x1b4 },
	{ 0, 22, 0x020 },
	{ 0, 23, 0x024 },
	{ 0, 26, 0x028 },
	{ 0, 27, 0x02c },
	{ 0, 30, 0x070 },
	{ 0, 31, 0x074 },
	{ 1, 0, 0x000 },
	{ 1, 1, 0x004 },
	{ 1, 2, 0x008 },
	{ 1, 3, 0x00c },
	{ 1, 4, 0x010 },
	{ 1, 5, 0x014 },
	{ 1, 6, 0x018 },
	{ 1, 7, 0x01c },
	{ 1, 12, 0x030 },
	{ 1, 13, 0x034 },
	{ 1, 14, 0x038 },
	{ 1, 15, 0x03c },
	{ 1, 16, 0x040 },
	{ 1, 17, 0x044 },
	{ 1, 18, 0x048 },
	{ 1, 19, 0x04c },
	{ 1, 29, 0x07c },
	{ 1, 30, 0x080 },
	{ 1, 31, 0x084 },
	{ 2, 1, 0x08c },
	{ 2, 2, 0x090 },
	{ 2, 3, 0x094 },
	{ 2, 4, 0x098 },
	{ 2, 5, 0x09c },
	{ 2, 6, 0x0a0 },
	{ 2, 7, 0x0a4 },
	{ 2, 8, 0x0a8 },
	{ 2, 9, 0x0ac },
	{ 2, 10, 0x0b0 },
	{ 2, 11, 0x0b4 },
	{ 2, 12, 0x0b8 },
	{ 2, 13, 0x0bc },
	{ 2, 14, 0x0c0 },
	{ 2, 15, 0x0c4 },
	{ 2, 16, 0x0c8 },
	{ 2, 17, 0x0cc },
	{ 2, 22, 0x0e0 },
	{ 2, 23, 0x0e4 },
	{ 2, 24, 0x0e8 },
	{ 2, 25, 0x0ec },
	{ 3, 14, 0x190 },
	{ 3, 15, 0x194 },
	{ 3, 16, 0x198 },
	{ 3, 17, 0x19c },
	{ 3, 18, 0x1a0 },
	{ 3, 19, 0x1a4 },
	{ 3, 20, 0x1a8 },
	{ 3, 21, 0x1ac },
};

int am335x_pinmux_gpio_offset(int gpio, int pin, size_t *offset)
{
	int ret = -1;
	int i;
	
	for (i = 0; i < sizeof(am335x_pinmux_gpio_table)/sizeof(struct am335x_pinmux_gpio_entry); i++) {
		if (am335x_pinmux_gpio_table[i].gpio == gpio &&
		    am335x_pinmux_gpio_table[i].pin == pin) {
			*offset = am335x_pinmux_gpio_table[i].offset;
			ret = 0;
			break;
		}
	}
	
	return ret;
}