1 /***************************************************************************
2 * Copyright (C) 2006 by Anders Larsen *
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. *
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. *
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 ***************************************************************************/
23 #include <jtag/interface.h>
29 #define AT91C_BASE_SYS (0xfffff000)
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 */
45 #define NC (0) /* not connected */
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 */
95 static const struct device_t devices[] = {
96 { "rea_ecr", PIOD, P27, PIOA, NC, PIOD, P23, PIOD, P24, PIOD, P26, PIOC, P5 },
101 static char *at91rm9200_device;
103 /* interface variables
105 static const struct device_t *device;
106 static int dev_mem_fd;
107 static void *sys_controller;
108 static uint32_t *pio_base;
110 /* low level command set
112 static int at91rm9200_read(void);
113 static void at91rm9200_write(int tck, int tms, int tdi);
114 static void at91rm9200_reset(int trst, int srst);
116 static int at91rm9200_init(void);
117 static int at91rm9200_quit(void);
119 static struct bitbang_interface at91rm9200_bitbang = {
120 .read = at91rm9200_read,
121 .write = at91rm9200_write,
122 .reset = at91rm9200_reset,
126 static int at91rm9200_read(void)
128 return (pio_base[device->TDO_PIO + PIO_PDSR] & device->TDO_MASK) != 0;
131 static void at91rm9200_write(int tck, int tms, int tdi)
134 pio_base[device->TCK_PIO + PIO_SODR] = device->TCK_MASK;
136 pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
139 pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
141 pio_base[device->TMS_PIO + PIO_CODR] = device->TMS_MASK;
144 pio_base[device->TDI_PIO + PIO_SODR] = device->TDI_MASK;
146 pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
149 /* (1) assert or (0) deassert reset lines */
150 static void at91rm9200_reset(int trst, int srst)
153 pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
155 pio_base[device->TRST_PIO + PIO_CODR] = device->TRST_MASK;
158 pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
160 pio_base[device->SRST_PIO + PIO_CODR] = device->SRST_MASK;
163 COMMAND_HANDLER(at91rm9200_handle_device_command)
166 return ERROR_COMMAND_SYNTAX_ERROR;
168 /* only if the device name wasn't overwritten by cmdline */
169 if (at91rm9200_device == 0) {
170 at91rm9200_device = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
171 strcpy(at91rm9200_device, CMD_ARGV[0]);
177 static const struct command_registration at91rm9200_command_handlers[] = {
179 .name = "at91rm9200_device",
180 .handler = &at91rm9200_handle_device_command,
181 .mode = COMMAND_CONFIG,
182 .help = "query armjtagew info",
184 COMMAND_REGISTRATION_DONE
187 struct jtag_interface at91rm9200_interface = {
188 .name = "at91rm9200",
189 .execute_queue = bitbang_execute_queue,
190 .commands = at91rm9200_command_handlers,
191 .init = at91rm9200_init,
192 .quit = at91rm9200_quit,
195 static int at91rm9200_init(void)
197 const struct device_t *cur_device;
199 cur_device = devices;
201 if (at91rm9200_device == NULL || at91rm9200_device[0] == 0) {
202 at91rm9200_device = "rea_ecr";
203 LOG_WARNING("No at91rm9200 device specified, using default 'rea_ecr'");
206 while (cur_device->name) {
207 if (strcmp(cur_device->name, at91rm9200_device) == 0) {
215 LOG_ERROR("No matching device found for %s", at91rm9200_device);
216 return ERROR_JTAG_INIT_FAILED;
219 bitbang_interface = &at91rm9200_bitbang;
221 dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
222 if (dev_mem_fd < 0) {
224 return ERROR_JTAG_INIT_FAILED;
227 sys_controller = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
228 MAP_SHARED, dev_mem_fd, AT91C_BASE_SYS);
229 if (sys_controller == MAP_FAILED) {
232 return ERROR_JTAG_INIT_FAILED;
234 pio_base = (uint32_t *)sys_controller + 0x100;
237 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
238 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
240 pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
241 pio_base[device->TDI_PIO + PIO_OER] = device->TDI_MASK;
242 pio_base[device->TDI_PIO + PIO_PER] = device->TDI_MASK;
243 pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
244 pio_base[device->TCK_PIO + PIO_OER] = device->TCK_MASK;
245 pio_base[device->TCK_PIO + PIO_PER] = device->TCK_MASK;
246 pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
247 pio_base[device->TMS_PIO + PIO_OER] = device->TMS_MASK;
248 pio_base[device->TMS_PIO + PIO_PER] = device->TMS_MASK;
249 pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
250 pio_base[device->TRST_PIO + PIO_OER] = device->TRST_MASK;
251 pio_base[device->TRST_PIO + PIO_PER] = device->TRST_MASK;
252 pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
253 pio_base[device->SRST_PIO + PIO_OER] = device->SRST_MASK;
254 pio_base[device->SRST_PIO + PIO_PER] = device->SRST_MASK;
255 pio_base[device->TDO_PIO + PIO_ODR] = device->TDO_MASK;
256 pio_base[device->TDO_PIO + PIO_PPUER] = device->TDO_MASK;
257 pio_base[device->TDO_PIO + PIO_PER] = device->TDO_MASK;
262 static int at91rm9200_quit(void)