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