ebedfdfec0314bbfadb40532d3ddeee3f143ce42
[fw/openocd] / src / jtag / ep93xx.c
1 /***************************************************************************\r
2  *   Copyright (C) 2005 by Dominic Rath                                    *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\r
5  *   This program is free software; you can redistribute it and/or modify  *\r
6  *   it under the terms of the GNU General Public License as published by  *\r
7  *   the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                   *\r
9  *                                                                         *\r
10  *   This program is distributed in the hope that it will be useful,       *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
13  *   GNU General Public License for more details.                          *\r
14  *                                                                         *\r
15  *   You should have received a copy of the GNU General Public License     *\r
16  *   along with this program; if not, write to the                         *\r
17  *   Free Software Foundation, Inc.,                                       *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
19  ***************************************************************************/\r
20 #ifdef HAVE_CONFIG_H\r
21 #include "config.h"\r
22 #endif\r
23 \r
24 #include "log.h"\r
25 #include "jtag.h"\r
26 #include "bitbang.h"\r
27 \r
28 #define TDO_BIT         1\r
29 #define TDI_BIT         2\r
30 #define TCK_BIT         4\r
31 #define TMS_BIT         8\r
32 #define TRST_BIT        16\r
33 #define SRST_BIT        32\r
34 #define VCC_BIT         64\r
35 \r
36 /* system includes */\r
37 #include <string.h>\r
38 #include <stdlib.h>\r
39 #include <stdio.h>\r
40 #include <sys/mman.h>\r
41 #include <unistd.h>\r
42 #include <fcntl.h>\r
43 \r
44 static u8 output_value = 0x0;\r
45 static int dev_mem_fd;\r
46 static void *gpio_controller;\r
47 static volatile u8 *gpio_data_register;\r
48 static volatile u8 *gpio_data_direction_register;\r
49 \r
50 /* low level command set\r
51  */\r
52 int ep93xx_read(void);\r
53 void ep93xx_write(int tck, int tms, int tdi);\r
54 void ep93xx_reset(int trst, int srst);\r
55 \r
56 int ep93xx_speed(int speed);\r
57 int ep93xx_register_commands(struct command_context_s *cmd_ctx);\r
58 int ep93xx_init(void);\r
59 int ep93xx_quit(void);\r
60 \r
61 struct timespec ep93xx_zzzz;\r
62 \r
63 jtag_interface_t ep93xx_interface = \r
64 {\r
65         .name = "ep93xx",\r
66         \r
67         .execute_queue = bitbang_execute_queue,\r
68 \r
69         .speed = ep93xx_speed,  \r
70         .register_commands = ep93xx_register_commands,\r
71         .init = ep93xx_init,\r
72         .quit = ep93xx_quit,\r
73 };\r
74 \r
75 bitbang_interface_t ep93xx_bitbang =\r
76 {\r
77         .read = ep93xx_read,\r
78         .write = ep93xx_write,\r
79         .reset = ep93xx_reset,\r
80         .blink = 0;\r
81 };\r
82 \r
83 int ep93xx_read(void)\r
84 {\r
85         return !!(*gpio_data_register & TDO_BIT);\r
86 }\r
87 \r
88 void ep93xx_write(int tck, int tms, int tdi)\r
89 {\r
90         if (tck)\r
91                 output_value |= TCK_BIT;\r
92         else\r
93                 output_value &= TCK_BIT;\r
94         \r
95         if (tms)\r
96                 output_value |= TMS_BIT;\r
97         else\r
98                 output_value &= TMS_BIT;\r
99         \r
100         if (tdi)\r
101                 output_value |= TDI_BIT;\r
102         else\r
103                 output_value &= TDI_BIT;\r
104 \r
105         *gpio_data_register = output_value;\r
106         nanosleep(ep93xx_zzzz);\r
107 }\r
108 \r
109 /* (1) assert or (0) deassert reset lines */\r
110 void ep93xx_reset(int trst, int srst)\r
111 {\r
112         if (trst == 0)\r
113                 output_value |= TRST_BIT;\r
114         else if (trst == 1)\r
115                 output_value &= TRST_BIT;\r
116 \r
117         if (srst == 0)\r
118                 output_value |= SRST_BIT;\r
119         else if (srst == 1)\r
120                 output_value &= SRST_BIT;\r
121         \r
122         *gpio_data_register = output_value;\r
123         nanosleep(ep93xx_zzzz);\r
124 }\r
125 \r
126 int ep93xx_speed(int speed)\r
127 {\r
128         \r
129         return ERROR_OK;\r
130 }\r
131 \r
132 int ep93xx_register_commands(struct command_context_s *cmd_ctx)\r
133 {\r
134 \r
135         return ERROR_OK;\r
136 }\r
137 \r
138 static int set_gonk_mode(void)\r
139 {\r
140         void *syscon;\r
141         u32 devicecfg;\r
142 \r
143         syscon = mmap(NULL, 4096, PROT_READ | PROT_WRITE,\r
144                         MAP_SHARED, dev_mem_fd, 0x80930000);\r
145         if (syscon == MAP_FAILED) {\r
146                 perror("mmap");\r
147                 return ERROR_JTAG_INIT_FAILED;\r
148         }\r
149 \r
150         devicecfg = *((volatile int *)(syscon + 0x80));\r
151         *((volatile int *)(syscon + 0xc0)) = 0xaa;\r
152         *((volatile int *)(syscon + 0x80)) = devicecfg | 0x08000000;\r
153 \r
154         munmap(syscon, 4096);\r
155 \r
156         return ERROR_OK;\r
157 }\r
158 \r
159 int ep93xx_init(void)\r
160 {\r
161         int ret;\r
162 \r
163         bitbang_interface = &ep93xx_bitbang;    \r
164 \r
165         ep93xx_zzzz.tv_sec = 0;\r
166         ep93xx_zzzz.tv_nsec = 10000000;\r
167 \r
168         dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);\r
169         if (dev_mem_fd < 0) {\r
170                 perror("open");\r
171                 return ERROR_JTAG_INIT_FAILED;\r
172         }\r
173 \r
174         gpio_controller = mmap(NULL, 4096, PROT_READ | PROT_WRITE,\r
175                                 MAP_SHARED, dev_mem_fd, 0x80840000);\r
176         if (gpio_controller == MAP_FAILED) {\r
177                 perror("mmap");\r
178                 close(dev_mem_fd);\r
179                 return ERROR_JTAG_INIT_FAILED;\r
180         }\r
181 \r
182         ret = set_gonk_mode();\r
183         if (ret != ERROR_OK) {\r
184                 munmap(gpio_controller, 4096);\r
185                 close(dev_mem_fd);\r
186                 return ret;\r
187         }\r
188 \r
189 #if 0\r
190         /* Use GPIO port A.  */\r
191         gpio_data_register = gpio_controller + 0x00;\r
192         gpio_data_direction_register = gpio_controller + 0x10;\r
193 \r
194 \r
195         /* Use GPIO port B.  */\r
196         gpio_data_register = gpio_controller + 0x04;\r
197         gpio_data_direction_register = gpio_controller + 0x14;\r
198 \r
199         /* Use GPIO port C.  */\r
200         gpio_data_register = gpio_controller + 0x08;\r
201         gpio_data_direction_register = gpio_controller + 0x18;\r
202 \r
203         /* Use GPIO port D.  */\r
204         gpio_data_register = gpio_controller + 0x0c;\r
205         gpio_data_direction_register = gpio_controller + 0x1c;\r
206 #endif\r
207 \r
208         /* Use GPIO port C.  */\r
209         gpio_data_register = gpio_controller + 0x08;\r
210         gpio_data_direction_register = gpio_controller + 0x18;\r
211 \r
212         INFO("gpio_data_register      = %p\n", gpio_data_register);\r
213         INFO("gpio_data_direction_reg = %p\n", gpio_data_direction_register); \r
214         /*\r
215          * Configure bit 0 (TDO) as an input, and bits 1-5 (TDI, TCK\r
216          * TMS, TRST, SRST) as outputs.  Drive TDI and TCK low, and\r
217          * TMS/TRST/SRST high.\r
218          */\r
219         output_value = TMS_BIT | TRST_BIT | SRST_BIT | VCC_BIT;\r
220         *gpio_data_register = output_value;\r
221         nanosleep(ep93xx_zzzz);\r
222 \r
223         /*\r
224          * Configure the direction register.  1 = output, 0 = input.\r
225          */\r
226         *gpio_data_direction_register =\r
227                 TDI_BIT | TCK_BIT | TMS_BIT | TRST_BIT | SRST_BIT | VCC_BIT;\r
228 \r
229         nanosleep(ep93xx_zzzz);\r
230         return ERROR_OK;\r
231 }\r
232 \r
233 int ep93xx_quit(void)\r
234 {\r
235 \r
236         return ERROR_OK;\r
237 }\r