45fb089bc92a752b6c4ca7a9678c496811f35927
[openocd.git] / src / jtag / at91rm9200.c
1 /***************************************************************************
2 * Copyright (C) 2006 by Anders Larsen *
3 * al@alarsen.net *
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 2 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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "log.h"
25 #include "jtag.h"
26 #include "bitbang.h"
27
28 /* system includes */
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/mman.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35
36 /* AT91RM9200 */
37 #define AT91C_BASE_SYS (0xfffff000)
38
39 /* GPIO assignment */
40 #define PIOA (0 << 7)
41 #define PIOB (1 << 7)
42 #define PIOC (2 << 7)
43 #define PIOD (3 << 7)
44
45 #define PIO_PER (0) /* PIO enable */
46 #define PIO_OER (4) /* output enable */
47 #define PIO_ODR (5) /* output disable */
48 #define PIO_SODR (12) /* set output data */
49 #define PIO_CODR (13) /* clear output data */
50 #define PIO_PDSR (15) /* pin data status */
51 #define PIO_PPUER (25) /* pull-up enable */
52
53 #define NC (0) /* not connected */
54 #define P0 (1 << 0)
55 #define P1 (1 << 1)
56 #define P2 (1 << 2)
57 #define P3 (1 << 3)
58 #define P4 (1 << 4)
59 #define P5 (1 << 5)
60 #define P6 (1 << 6)
61 #define P7 (1 << 7)
62 #define P8 (1 << 8)
63 #define P9 (1 << 9)
64 #define P10 (1 << 10)
65 #define P11 (1 << 11)
66 #define P12 (1 << 12)
67 #define P13 (1 << 13)
68 #define P14 (1 << 14)
69 #define P15 (1 << 15)
70 #define P16 (1 << 16)
71 #define P17 (1 << 17)
72 #define P18 (1 << 18)
73 #define P19 (1 << 19)
74 #define P20 (1 << 20)
75 #define P21 (1 << 21)
76 #define P22 (1 << 22)
77 #define P23 (1 << 23)
78 #define P24 (1 << 24)
79 #define P25 (1 << 25)
80 #define P26 (1 << 26)
81 #define P27 (1 << 27)
82 #define P28 (1 << 28)
83 #define P29 (1 << 29)
84 #define P30 (1 << 30)
85 #define P31 (1 << 31)
86
87 struct device_t
88 {
89 char* name;
90 int TDO_PIO; /* PIO holding TDO */
91 u32 TDO_MASK; /* TDO bitmask */
92 int TRST_PIO; /* PIO holding TRST */
93 u32 TRST_MASK; /* TRST bitmask */
94 int TMS_PIO; /* PIO holding TMS */
95 u32 TMS_MASK; /* TMS bitmask */
96 int TCK_PIO; /* PIO holding TCK */
97 u32 TCK_MASK; /* TCK bitmask */
98 int TDI_PIO; /* PIO holding TDI */
99 u32 TDI_MASK; /* TDI bitmask */
100 int SRST_PIO; /* PIO holding SRST */
101 u32 SRST_MASK; /* SRST bitmask */
102 };
103
104 static struct device_t devices[] =
105 {
106 { "rea_ecr", PIOD, P27, PIOA, NC, PIOD, P23, PIOD, P24, PIOD, P26, PIOC, P5 },
107 { .name = NULL },
108 };
109
110 /* configuration */
111 static char* at91rm9200_device;
112
113 /* interface variables
114 */
115 static struct device_t* device;
116 static int dev_mem_fd;
117 static void *sys_controller;
118 static u32* pio_base;
119
120 /* low level command set
121 */
122 static int at91rm9200_read(void);
123 static void at91rm9200_write(int tck, int tms, int tdi);
124 static void at91rm9200_reset(int trst, int srst);
125
126 static int at91rm9200_speed(int speed);
127 static int at91rm9200_register_commands(struct command_context_s *cmd_ctx);
128 static int at91rm9200_init(void);
129 static int at91rm9200_quit(void);
130
131 jtag_interface_t at91rm9200_interface =
132 {
133 .name = "at91rm9200",
134
135 .execute_queue = bitbang_execute_queue,
136
137 .speed = at91rm9200_speed,
138 .register_commands = at91rm9200_register_commands,
139 .init = at91rm9200_init,
140 .quit = at91rm9200_quit,
141 };
142
143 static bitbang_interface_t at91rm9200_bitbang =
144 {
145 .read = at91rm9200_read,
146 .write = at91rm9200_write,
147 .reset = at91rm9200_reset,
148 .blink = 0
149 };
150
151 static int at91rm9200_read(void)
152 {
153 return (pio_base[device->TDO_PIO + PIO_PDSR] & device->TDO_MASK) != 0;
154 }
155
156 static void at91rm9200_write(int tck, int tms, int tdi)
157 {
158 if (tck)
159 pio_base[device->TCK_PIO + PIO_SODR] = device->TCK_MASK;
160 else
161 pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
162
163 if (tms)
164 pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
165 else
166 pio_base[device->TMS_PIO + PIO_CODR] = device->TMS_MASK;
167
168 if (tdi)
169 pio_base[device->TDI_PIO + PIO_SODR] = device->TDI_MASK;
170 else
171 pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
172 }
173
174 /* (1) assert or (0) deassert reset lines */
175 static void at91rm9200_reset(int trst, int srst)
176 {
177 if (trst == 0)
178 pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
179 else if (trst == 1)
180 pio_base[device->TRST_PIO + PIO_CODR] = device->TRST_MASK;
181
182 if (srst == 0)
183 pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
184 else if (srst == 1)
185 pio_base[device->SRST_PIO + PIO_CODR] = device->SRST_MASK;
186 }
187
188 static int at91rm9200_speed(int speed)
189 {
190
191 return ERROR_OK;
192 }
193
194 static int at91rm9200_handle_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
195 {
196 if (argc == 0)
197 return ERROR_OK;
198
199 /* only if the device name wasn't overwritten by cmdline */
200 if (at91rm9200_device == 0)
201 {
202 at91rm9200_device = malloc(strlen(args[0]) + sizeof(char));
203 strcpy(at91rm9200_device, args[0]);
204 }
205
206 return ERROR_OK;
207 }
208
209 static int at91rm9200_register_commands(struct command_context_s *cmd_ctx)
210 {
211 register_command(cmd_ctx, NULL, "at91rm9200_device", at91rm9200_handle_device_command,
212 COMMAND_CONFIG, NULL);
213 return ERROR_OK;
214 }
215
216 static int at91rm9200_init(void)
217 {
218 struct device_t *cur_device;
219
220 cur_device = devices;
221
222 if (at91rm9200_device == NULL || at91rm9200_device[0] == 0)
223 {
224 at91rm9200_device = "rea_ecr";
225 LOG_WARNING("No at91rm9200 device specified, using default 'rea_ecr'");
226 }
227
228 while (cur_device->name)
229 {
230 if (strcmp(cur_device->name, at91rm9200_device) == 0)
231 {
232 device = cur_device;
233 break;
234 }
235 cur_device++;
236 }
237
238 if (!device)
239 {
240 LOG_ERROR("No matching device found for %s", at91rm9200_device);
241 return ERROR_JTAG_INIT_FAILED;
242 }
243
244 bitbang_interface = &at91rm9200_bitbang;
245
246 dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
247 if (dev_mem_fd < 0) {
248 perror("open");
249 return ERROR_JTAG_INIT_FAILED;
250 }
251
252 sys_controller = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
253 MAP_SHARED, dev_mem_fd, AT91C_BASE_SYS);
254 if (sys_controller == MAP_FAILED) {
255 perror("mmap");
256 close(dev_mem_fd);
257 return ERROR_JTAG_INIT_FAILED;
258 }
259 pio_base = (u32*)sys_controller + 0x100;
260
261 /*
262 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
263 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
264 */
265 pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
266 pio_base[device->TDI_PIO + PIO_OER] = device->TDI_MASK;
267 pio_base[device->TDI_PIO + PIO_PER] = device->TDI_MASK;
268 pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
269 pio_base[device->TCK_PIO + PIO_OER] = device->TCK_MASK;
270 pio_base[device->TCK_PIO + PIO_PER] = device->TCK_MASK;
271 pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
272 pio_base[device->TMS_PIO + PIO_OER] = device->TMS_MASK;
273 pio_base[device->TMS_PIO + PIO_PER] = device->TMS_MASK;
274 pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
275 pio_base[device->TRST_PIO + PIO_OER] = device->TRST_MASK;
276 pio_base[device->TRST_PIO + PIO_PER] = device->TRST_MASK;
277 pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
278 pio_base[device->SRST_PIO + PIO_OER] = device->SRST_MASK;
279 pio_base[device->SRST_PIO + PIO_PER] = device->SRST_MASK;
280 pio_base[device->TDO_PIO + PIO_ODR] = device->TDO_MASK;
281 pio_base[device->TDO_PIO + PIO_PPUER] = device->TDO_MASK;
282 pio_base[device->TDO_PIO + PIO_PER] = device->TDO_MASK;
283
284 return ERROR_OK;
285 }
286
287 static int at91rm9200_quit(void)
288 {
289
290 return ERROR_OK;
291 }