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