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