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