OpenOCD
at91rm9200.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Anders Larsen *
5  * al@alarsen.net *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include <jtag/interface.h>
13 #include "bitbang.h"
14 
15 #include <sys/mman.h>
16 
17 /* AT91RM9200 */
18 #define AT91C_BASE_SYS (0xfffff000)
19 
20 /* GPIO assignment */
21 #define PIOA (0 << 7)
22 #define PIOB (1 << 7)
23 #define PIOC (2 << 7)
24 #define PIOD (3 << 7)
25 
26 #define PIO_PER (0) /* PIO enable */
27 #define PIO_OER (4) /* output enable */
28 #define PIO_ODR (5) /* output disable */
29 #define PIO_SODR (12) /* set output data */
30 #define PIO_CODR (13) /* clear output data */
31 #define PIO_PDSR (15) /* pin data status */
32 #define PIO_PPUER (25) /* pull-up enable */
33 
34 #define NC (0) /* not connected */
35 #define P0 (1 << 0)
36 #define P1 (1 << 1)
37 #define P2 (1 << 2)
38 #define P3 (1 << 3)
39 #define P4 (1 << 4)
40 #define P5 (1 << 5)
41 #define P6 (1 << 6)
42 #define P7 (1 << 7)
43 #define P8 (1 << 8)
44 #define P9 (1 << 9)
45 #define P10 (1 << 10)
46 #define P11 (1 << 11)
47 #define P12 (1 << 12)
48 #define P13 (1 << 13)
49 #define P14 (1 << 14)
50 #define P15 (1 << 15)
51 #define P16 (1 << 16)
52 #define P17 (1 << 17)
53 #define P18 (1 << 18)
54 #define P19 (1 << 19)
55 #define P20 (1 << 20)
56 #define P21 (1 << 21)
57 #define P22 (1 << 22)
58 #define P23 (1 << 23)
59 #define P24 (1 << 24)
60 #define P25 (1 << 25)
61 #define P26 (1 << 26)
62 #define P27 (1 << 27)
63 #define P28 (1 << 28)
64 #define P29 (1 << 29)
65 #define P30 (1 << 30)
66 #define P31 (1 << 31)
67 
68 struct device_t {
69  const char *name;
70  int TDO_PIO; /* PIO holding TDO */
71  uint32_t TDO_MASK; /* TDO bitmask */
72  int TRST_PIO; /* PIO holding TRST */
73  uint32_t TRST_MASK; /* TRST bitmask */
74  int TMS_PIO; /* PIO holding TMS */
75  uint32_t TMS_MASK; /* TMS bitmask */
76  int TCK_PIO; /* PIO holding TCK */
77  uint32_t TCK_MASK; /* TCK bitmask */
78  int TDI_PIO; /* PIO holding TDI */
79  uint32_t TDI_MASK; /* TDI bitmask */
80  int SRST_PIO; /* PIO holding SRST */
81  uint32_t SRST_MASK; /* SRST bitmask */
82 };
83 
84 static const struct device_t devices[] = {
85  { "rea_ecr", PIOD, P27, PIOA, NC, PIOD, P23, PIOD, P24, PIOD, P26, PIOC, P5 },
86  { .name = NULL },
87 };
88 
89 /* configuration */
90 static char *at91rm9200_device;
91 
92 /* interface variables
93  */
94 static const struct device_t *device;
95 static int dev_mem_fd;
96 static void *sys_controller;
97 static uint32_t *pio_base;
98 
99 /* low level command set
100  */
101 static bb_value_t at91rm9200_read(void);
102 static int at91rm9200_write(int tck, int tms, int tdi);
103 
104 static int at91rm9200_init(void);
105 static int at91rm9200_quit(void);
106 
107 static struct bitbang_interface at91rm9200_bitbang = {
109  .write = at91rm9200_write,
110  .blink = 0
111 };
112 
114 {
116 }
117 
118 static int at91rm9200_write(int tck, int tms, int tdi)
119 {
120  if (tck)
122  else
124 
125  if (tms)
127  else
129 
130  if (tdi)
132  else
134 
135  return ERROR_OK;
136 }
137 
138 /* (1) assert or (0) deassert reset lines */
139 static int at91rm9200_reset(int trst, int srst)
140 {
141  if (trst == 0)
143  else if (trst == 1)
145 
146  if (srst == 0)
148  else if (srst == 1)
150 
151  return ERROR_OK;
152 }
153 
154 COMMAND_HANDLER(at91rm9200_handle_device_command)
155 {
156  if (CMD_ARGC == 0)
158 
159  /* only if the device name wasn't overwritten by cmdline */
160  if (at91rm9200_device == 0) {
161  at91rm9200_device = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
162  strcpy(at91rm9200_device, CMD_ARGV[0]);
163  }
164 
165  return ERROR_OK;
166 }
167 
168 static const struct command_registration at91rm9200_command_handlers[] = {
169  {
170  .name = "at91rm9200_device",
171  .handler = &at91rm9200_handle_device_command,
172  .mode = COMMAND_CONFIG,
173  .help = "Set at91rm9200 device [default \"rea_ecr\"]",
174  .usage = "<device>",
175  },
177 };
178 
179 static struct jtag_interface at91rm9200_interface = {
181 };
182 
184  .name = "at91rm9200",
185  .transports = jtag_only,
186  .commands = at91rm9200_command_handlers,
187 
188  .init = at91rm9200_init,
189  .quit = at91rm9200_quit,
190  .reset = at91rm9200_reset,
191 
192  .jtag_ops = &at91rm9200_interface,
193 };
194 
195 static int at91rm9200_init(void)
196 {
197  const struct device_t *cur_device;
198 
199  cur_device = devices;
200 
201  if (!at91rm9200_device || at91rm9200_device[0] == 0) {
202  at91rm9200_device = "rea_ecr";
203  LOG_WARNING("No at91rm9200 device specified, using default 'rea_ecr'");
204  }
205 
206  while (cur_device->name) {
207  if (strcmp(cur_device->name, at91rm9200_device) == 0) {
208  device = cur_device;
209  break;
210  }
211  cur_device++;
212  }
213 
214  if (!device) {
215  LOG_ERROR("No matching device found for %s", at91rm9200_device);
216  return ERROR_JTAG_INIT_FAILED;
217  }
218 
220 
221  dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
222  if (dev_mem_fd < 0) {
223  LOG_ERROR("open: %s", strerror(errno));
224  return ERROR_JTAG_INIT_FAILED;
225  }
226 
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) {
230  LOG_ERROR("mmap: %s", strerror(errno));
231  close(dev_mem_fd);
232  return ERROR_JTAG_INIT_FAILED;
233  }
234  pio_base = (uint32_t *)sys_controller + 0x100;
235 
236  /*
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.
239  */
258 
259  return ERROR_OK;
260 }
261 
262 static int at91rm9200_quit(void)
263 {
264 
265  return ERROR_OK;
266 }
const char *const jtag_only[]
Definition: adapter.c:31
#define PIO_CODR
Definition: at91rm9200.c:30
#define PIOA
Definition: at91rm9200.c:21
#define NC
Definition: at91rm9200.c:34
#define PIO_PDSR
Definition: at91rm9200.c:31
static const struct command_registration at91rm9200_command_handlers[]
Definition: at91rm9200.c:168
static int at91rm9200_quit(void)
Definition: at91rm9200.c:262
#define P5
Definition: at91rm9200.c:40
static struct bitbang_interface at91rm9200_bitbang
Definition: at91rm9200.c:107
static char * at91rm9200_device
Definition: at91rm9200.c:90
#define AT91C_BASE_SYS
Definition: at91rm9200.c:18
#define P26
Definition: at91rm9200.c:61
#define PIO_OER
Definition: at91rm9200.c:27
#define P23
Definition: at91rm9200.c:58
#define PIO_PER
Definition: at91rm9200.c:26
#define P24
Definition: at91rm9200.c:59
COMMAND_HANDLER(at91rm9200_handle_device_command)
Definition: at91rm9200.c:154
static uint32_t * pio_base
Definition: at91rm9200.c:97
static int at91rm9200_init(void)
Definition: at91rm9200.c:195
struct adapter_driver at91rm9200_adapter_driver
Definition: at91rm9200.c:183
static const struct device_t devices[]
Definition: at91rm9200.c:84
static int dev_mem_fd
Definition: at91rm9200.c:95
static int at91rm9200_reset(int trst, int srst)
Definition: at91rm9200.c:139
#define P27
Definition: at91rm9200.c:62
static const struct device_t * device
Definition: at91rm9200.c:94
#define PIOC
Definition: at91rm9200.c:23
#define PIOD
Definition: at91rm9200.c:24
static int at91rm9200_write(int tck, int tms, int tdi)
Definition: at91rm9200.c:118
static struct jtag_interface at91rm9200_interface
Definition: at91rm9200.c:179
#define PIO_PPUER
Definition: at91rm9200.c:32
#define PIO_SODR
Definition: at91rm9200.c:29
static void * sys_controller
Definition: at91rm9200.c:96
#define PIO_ODR
Definition: at91rm9200.c:28
static bb_value_t at91rm9200_read(void)
Definition: at91rm9200.c:113
int bitbang_execute_queue(void)
Definition: bitbang.c:281
bb_value_t
Definition: bitbang.h:16
@ BB_LOW
Definition: bitbang.h:17
@ BB_HIGH
Definition: bitbang.h:18
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_CONFIG
Definition: command.h:41
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:549
#define LOG_WARNING(expr ...)
Definition: log.h:120
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define ERROR_OK
Definition: log.h:155
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
Low level callbacks (for bitbang).
Definition: bitbang.h:29
bb_value_t(* read)(void)
Sample TDO and return the value.
Definition: bitbang.h:31
const char * name
Definition: command.h:229
uint32_t TDO_MASK
Definition: at91rm9200.c:71
int TDO_PIO
Definition: at91rm9200.c:70
uint32_t TDI_MASK
Definition: at91rm9200.c:79
uint32_t SRST_MASK
Definition: at91rm9200.c:81
int TRST_PIO
Definition: at91rm9200.c:72
uint32_t TRST_MASK
Definition: at91rm9200.c:73
int SRST_PIO
Definition: at91rm9200.c:80
const char * name
Definition: at91rm9200.c:69
uint32_t TMS_MASK
Definition: at91rm9200.c:75
int TCK_PIO
Definition: at91rm9200.c:76
int TDI_PIO
Definition: at91rm9200.c:78
int TMS_PIO
Definition: at91rm9200.c:74
uint32_t TCK_MASK
Definition: at91rm9200.c:77
Represents a driver for a debugging interface.
Definition: interface.h:184
int(* execute_queue)(void)
Execute queued commands.
Definition: interface.h:195
#define NULL
Definition: usb.h:16