]> git.sur5r.net Git - openocd/blob - src/jtag/aice/aice_pipe.c
aice: add Andes AICE support
[openocd] / src / jtag / aice / aice_pipe.c
1 /***************************************************************************
2  *   Copyright (C) 2013 by Andes Technology                                *
3  *   Hsiangkai Wang <hkwang@andestech.com>                                 *
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  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef _WIN32
25 #include <windows.h>
26 #else
27 #include <signal.h>
28 #endif
29
30 #include <helper/log.h>
31 #include <helper/time_support.h>
32 #include "aice_port.h"
33 #include "aice_pipe.h"
34
35 #define AICE_PIPE_MAXLINE 8192
36
37 #ifdef _WIN32
38 PROCESS_INFORMATION proc_info;
39
40 HANDLE aice_pipe_output[2];
41 HANDLE aice_pipe_input[2];
42
43 static int aice_pipe_write(const void *buffer, int count)
44 {
45         BOOL success;
46         DWORD written;
47
48         success = WriteFile(aice_pipe_output[1], buffer, count, &written, NULL);
49         if (!success) {
50                 LOG_ERROR("(WIN32) write to pipe failed, error code: 0x%08lx", GetLastError());
51                 return -1;
52         }
53
54         return written;
55 }
56
57 static int aice_pipe_read(void *buffer, int count)
58 {
59         BOOL success;
60         DWORD has_read;
61
62         success = ReadFile(aice_pipe_input[0], buffer, count, &has_read, NULL);
63         if (!success || (has_read == 0)) {
64                 LOG_ERROR("(WIN32) read from pipe failed, error code: 0x%08lx", GetLastError());
65                 return -1;
66         }
67
68         return has_read;
69 }
70
71 static int aice_pipe_child_init(struct aice_port_param_s *param)
72 {
73         STARTUPINFO start_info;
74         BOOL success;
75
76         ZeroMemory(&proc_info, sizeof(PROCESS_INFORMATION));
77         ZeroMemory(&start_info, sizeof(STARTUPINFO));
78         start_info.cb = sizeof(STARTUPINFO);
79         start_info.hStdError = aice_pipe_input[1];
80         start_info.hStdOutput = aice_pipe_input[1];
81         start_info.hStdInput = aice_pipe_output[0];
82         start_info.dwFlags |= STARTF_USESTDHANDLES;
83
84         success = CreateProcess(NULL,
85                         param->adapter_name,
86                         NULL,
87                         NULL,
88                         TRUE,
89                         0,
90                         NULL,
91                         NULL,
92                         &start_info,
93                         &proc_info);
94
95         if (!success) {
96                 LOG_ERROR("Create new process failed");
97                 return ERROR_FAIL;
98         }
99
100         return ERROR_OK;
101 }
102
103 static int aice_pipe_parent_init(struct aice_port_param_s *param)
104 {
105         /* send open to adapter */
106         char line[AICE_PIPE_MAXLINE];
107         char command[AICE_PIPE_MAXLINE];
108
109         command[0] = AICE_OPEN;
110         set_u16(command + 1, param->vid);
111         set_u16(command + 3, param->pid);
112
113         if (aice_pipe_write(command, 5) != 5) {
114                 LOG_ERROR("write failed\n");
115                 return ERROR_FAIL;
116         }
117
118         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0) {
119                 LOG_ERROR("read failed\n");
120                 return ERROR_FAIL;
121         }
122
123         if (line[0] == AICE_OK)
124                 return ERROR_OK;
125         else
126                 return ERROR_FAIL;
127 }
128
129 static int aice_pipe_open(struct aice_port_param_s *param)
130 {
131         SECURITY_ATTRIBUTES attribute;
132
133         attribute.nLength = sizeof(SECURITY_ATTRIBUTES);
134         attribute.bInheritHandle = TRUE;
135         attribute.lpSecurityDescriptor = NULL;
136
137         if (!CreatePipe(&aice_pipe_output[0], &aice_pipe_output[1],
138                                 &attribute, AICE_PIPE_MAXLINE)) {
139                 LOG_ERROR("Create pipes failed");
140                 return ERROR_FAIL;
141         }
142         if (!CreatePipe(&aice_pipe_input[0], &aice_pipe_input[1],
143                                 &attribute, AICE_PIPE_MAXLINE)) {
144                 LOG_ERROR("Create pipes failed");
145                 return ERROR_FAIL;
146         }
147
148         /* do not inherit aice_pipe_output[1] & aice_pipe_input[0] to child process */
149         if (!SetHandleInformation(aice_pipe_output[1], HANDLE_FLAG_INHERIT, 0))
150                 return ERROR_FAIL;
151         if (!SetHandleInformation(aice_pipe_input[0], HANDLE_FLAG_INHERIT, 0))
152                 return ERROR_FAIL;
153
154         aice_pipe_child_init(param);
155
156         aice_pipe_parent_init(param);
157
158         return ERROR_OK;
159 }
160
161 #else
162
163 int aice_pipe_output[2];
164 int aice_pipe_input[2];
165
166 static int aice_pipe_write(const void *buffer, int count)
167 {
168         if (write(aice_pipe_output[1], buffer, count) != count) {
169                 LOG_ERROR("write to pipe failed");
170                 return -1;
171         }
172
173         return count;
174 }
175
176 static int aice_pipe_read(void *buffer, int count)
177 {
178         int n;
179         long long then, cur;
180
181         then = timeval_ms();
182
183         while (1) {
184                 n = read(aice_pipe_input[0], buffer, count);
185
186                 if ((n == -1) && (errno == EAGAIN)) {
187                         cur = timeval_ms();
188                         if (cur - then > 500)
189                                 keep_alive();
190                         continue;
191                 } else if (n > 0)
192                         break;
193                 else {
194                         LOG_ERROR("read from pipe failed");
195                         break;
196                 }
197         }
198
199         return n;
200 }
201
202 static int aice_pipe_child_init(struct aice_port_param_s *param)
203 {
204         close(aice_pipe_output[1]);
205         close(aice_pipe_input[0]);
206
207         if (aice_pipe_output[0] != STDIN_FILENO) {
208                 if (dup2(aice_pipe_output[0], STDIN_FILENO) != STDIN_FILENO) {
209                         LOG_ERROR("Map aice_pipe to STDIN failed");
210                         return ERROR_FAIL;
211                 }
212                 close(aice_pipe_output[0]);
213         }
214
215         if (aice_pipe_input[1] != STDOUT_FILENO) {
216                 if (dup2(aice_pipe_input[1], STDOUT_FILENO) != STDOUT_FILENO) {
217                         LOG_ERROR("Map aice_pipe to STDOUT failed");
218                         return ERROR_FAIL;
219                 }
220                 close(aice_pipe_input[1]);
221         }
222
223         if (execl(param->adapter_name, param->adapter_name, (char *)0) < 0) {
224                 LOG_ERROR("Execute aice_pipe failed");
225                 return ERROR_FAIL;
226         }
227
228         return ERROR_OK;
229 }
230
231 static int aice_pipe_parent_init(struct aice_port_param_s *param)
232 {
233         close(aice_pipe_output[0]);
234         close(aice_pipe_input[1]);
235
236         /* set read end of pipe as non-blocking */
237         if (fcntl(aice_pipe_input[0], F_SETFL, O_NONBLOCK))
238                 return ERROR_FAIL;
239
240         /* send open to adapter */
241         char line[AICE_PIPE_MAXLINE];
242         char command[AICE_PIPE_MAXLINE];
243
244         command[0] = AICE_OPEN;
245         set_u16(command + 1, param->vid);
246         set_u16(command + 3, param->pid);
247
248         if (aice_pipe_write(command, 5) != 5) {
249                 LOG_ERROR("write failed\n");
250                 return ERROR_FAIL;
251         }
252
253         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0) {
254                 LOG_ERROR("read failed\n");
255                 return ERROR_FAIL;
256         }
257
258         if (line[0] == AICE_OK)
259                 return ERROR_OK;
260         else
261                 return ERROR_FAIL;
262 }
263
264 static void sig_pipe(int signo)
265 {
266         exit(1);
267 }
268
269 static int aice_pipe_open(struct aice_port_param_s *param)
270 {
271         pid_t pid;
272
273         if (signal(SIGPIPE, sig_pipe) == SIG_ERR) {
274                 LOG_ERROR("Register SIGPIPE handler failed");
275                 return ERROR_FAIL;
276         }
277
278         if (pipe(aice_pipe_output) < 0 || pipe(aice_pipe_input) < 0) {
279                 LOG_ERROR("Create pipes failed");
280                 return ERROR_FAIL;
281         }
282
283         pid = fork();
284         if (pid < 0) {
285                 LOG_ERROR("Fork new process failed");
286                 return ERROR_FAIL;
287         } else if (pid == 0) {
288                 if (aice_pipe_child_init(param) != ERROR_OK) {
289                         LOG_ERROR("AICE_PIPE child process initial error");
290                         return ERROR_FAIL;
291                 } else {
292                         if (aice_pipe_parent_init(param) != ERROR_OK) {
293                                 LOG_ERROR("AICE_PIPE parent process initial error");
294                                 return ERROR_FAIL;
295                         }
296                 }
297         }
298
299         return ERROR_OK;
300 }
301 #endif
302
303 static int aice_pipe_close(void)
304 {
305         char line[AICE_PIPE_MAXLINE];
306         char command[AICE_PIPE_MAXLINE];
307
308         command[0] = AICE_CLOSE;
309
310         if (aice_pipe_write(command, 1) != 1)
311                 return ERROR_FAIL;
312
313         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
314                 return ERROR_FAIL;
315
316         if (line[0] == AICE_OK) {
317 #ifdef _WIN32
318                 WaitForSingleObject(proc_info.hProcess, INFINITE);
319                 CloseHandle(proc_info.hProcess);
320                 CloseHandle(proc_info.hThread);
321 #endif
322                 return ERROR_OK;
323         } else
324                 return ERROR_FAIL;
325 }
326
327 static int aice_pipe_idcode(uint32_t *idcode, uint8_t *num_of_idcode)
328 {
329         char line[AICE_PIPE_MAXLINE];
330         char command[AICE_PIPE_MAXLINE];
331
332         command[0] = AICE_IDCODE;
333
334         if (aice_pipe_write(command, 1) != 1)
335                 return ERROR_FAIL;
336
337         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
338                 return ERROR_FAIL;
339
340         *num_of_idcode = line[0];
341
342         if ((*num_of_idcode == 0) || (*num_of_idcode >= 16))
343                 return ERROR_FAIL;
344
345         for (int i = 0 ; i < *num_of_idcode ; i++)
346                 idcode[i] = get_u32(line + i * 4 + 1);
347
348         return ERROR_OK;
349 }
350
351 static int aice_pipe_state(enum aice_target_state_s *state)
352 {
353         char line[AICE_PIPE_MAXLINE];
354         char command[AICE_PIPE_MAXLINE];
355
356         command[0] = AICE_STATE;
357
358         if (aice_pipe_write(command, 1) != 1)
359                 return ERROR_FAIL;
360
361         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
362                 return ERROR_FAIL;
363
364         *state = (enum aice_target_state_s)line[0];
365
366         return ERROR_OK;
367 }
368
369 static int aice_pipe_reset(void)
370 {
371         char line[AICE_PIPE_MAXLINE];
372         char command[AICE_PIPE_MAXLINE];
373
374         command[0] = AICE_RESET;
375
376         if (aice_pipe_write(command, 1) != 1)
377                 return ERROR_FAIL;
378
379         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
380                 return ERROR_FAIL;
381
382         if (line[0] == AICE_OK)
383                 return ERROR_OK;
384         else
385                 return ERROR_FAIL;
386 }
387
388 static int aice_pipe_assert_srst(enum aice_srst_type_s srst)
389 {
390         char line[AICE_PIPE_MAXLINE];
391         char command[AICE_PIPE_MAXLINE];
392
393         command[0] = AICE_ASSERT_SRST;
394         command[1] = srst;
395
396         if (aice_pipe_write(command, 2) != 2)
397                 return ERROR_FAIL;
398
399         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
400                 return ERROR_FAIL;
401
402         if (line[0] == AICE_OK)
403                 return ERROR_OK;
404         else
405                 return ERROR_FAIL;
406 }
407
408 static int aice_pipe_run(void)
409 {
410         char line[AICE_PIPE_MAXLINE];
411         char command[AICE_PIPE_MAXLINE];
412
413         command[0] = AICE_RUN;
414
415         if (aice_pipe_write(command, 1) != 1)
416                 return ERROR_FAIL;
417
418         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
419                 return ERROR_FAIL;
420
421         if (line[0] == AICE_OK)
422                 return ERROR_OK;
423         else
424                 return ERROR_FAIL;
425 }
426
427 static int aice_pipe_halt(void)
428 {
429         char line[AICE_PIPE_MAXLINE];
430         char command[AICE_PIPE_MAXLINE];
431
432         command[0] = AICE_HALT;
433
434         if (aice_pipe_write(command, 1) != 1)
435                 return ERROR_FAIL;
436
437         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
438                 return ERROR_FAIL;
439
440         if (line[0] == AICE_OK)
441                 return ERROR_OK;
442         else
443                 return ERROR_FAIL;
444 }
445
446 static int aice_pipe_read_reg(uint32_t num, uint32_t *val)
447 {
448         char line[AICE_PIPE_MAXLINE];
449         char command[AICE_PIPE_MAXLINE];
450
451         command[0] = AICE_READ_REG;
452         set_u32(command + 1, num);
453
454         if (aice_pipe_write(command, 5) != 5)
455                 return ERROR_FAIL;
456
457         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
458                 return ERROR_FAIL;
459
460         *val = get_u32(line);
461
462         return ERROR_OK;
463 }
464
465 static int aice_pipe_write_reg(uint32_t num, uint32_t val)
466 {
467         char line[AICE_PIPE_MAXLINE];
468         char command[AICE_PIPE_MAXLINE];
469
470         command[0] = AICE_WRITE_REG;
471         set_u32(command + 1, num);
472         set_u32(command + 5, val);
473
474         if (aice_pipe_write(command, 9) != 9)
475                 return ERROR_FAIL;
476
477         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
478                 return ERROR_FAIL;
479
480         if (line[0] == AICE_OK)
481                 return ERROR_OK;
482         else
483                 return ERROR_FAIL;
484 }
485
486 static int aice_pipe_read_reg_64(uint32_t num, uint64_t *val)
487 {
488         char line[AICE_PIPE_MAXLINE];
489         char command[AICE_PIPE_MAXLINE];
490
491         command[0] = AICE_READ_REG_64;
492         set_u32(command + 1, num);
493
494         if (aice_pipe_write(command, 5) != 5)
495                 return ERROR_FAIL;
496
497         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
498                 return ERROR_FAIL;
499
500         *val = (((uint64_t)get_u32(line + 4)) << 32) | get_u32(line);
501
502         return ERROR_OK;
503 }
504
505 static int aice_pipe_write_reg_64(uint32_t num, uint64_t val)
506 {
507         char line[AICE_PIPE_MAXLINE];
508         char command[AICE_PIPE_MAXLINE];
509
510         command[0] = AICE_WRITE_REG_64;
511         set_u32(command + 1, num);
512         set_u32(command + 5, val & 0xFFFFFFFF);
513         set_u32(command + 9, (val >> 32) & 0xFFFFFFFF);
514
515         if (aice_pipe_write(command, 13) != 9)
516                 return ERROR_FAIL;
517
518         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
519                 return ERROR_FAIL;
520
521         if (line[0] == AICE_OK)
522                 return ERROR_OK;
523         else
524                 return ERROR_FAIL;
525 }
526
527 static int aice_pipe_step(void)
528 {
529         char line[AICE_PIPE_MAXLINE];
530         char command[AICE_PIPE_MAXLINE];
531
532         command[0] = AICE_STEP;
533
534         if (aice_pipe_write(command, 1) != 1)
535                 return ERROR_FAIL;
536
537         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
538                 return ERROR_FAIL;
539
540         if (line[0] == AICE_OK)
541                 return ERROR_OK;
542         else
543                 return ERROR_FAIL;
544 }
545
546 static int aice_pipe_read_mem_unit(uint32_t addr, uint32_t size,
547                 uint32_t count, uint8_t *buffer)
548 {
549         char command[AICE_PIPE_MAXLINE];
550
551         command[0] = AICE_READ_MEM_UNIT;
552         set_u32(command + 1, addr);
553         set_u32(command + 5, size);
554         set_u32(command + 9, count);
555
556         if (aice_pipe_write(command, 13) != 13)
557                 return ERROR_FAIL;
558
559         if (aice_pipe_read(buffer, size * count) < 0)
560                 return ERROR_FAIL;
561
562         return ERROR_OK;
563 }
564
565 static int aice_pipe_write_mem_unit(uint32_t addr, uint32_t size,
566                 uint32_t count, const uint8_t *buffer)
567 {
568         char line[AICE_PIPE_MAXLINE];
569         char command[AICE_PIPE_MAXLINE];
570
571         command[0] = AICE_WRITE_MEM_UNIT;
572         set_u32(command + 1, addr);
573         set_u32(command + 5, size);
574         set_u32(command + 9, count);
575
576         /* WRITE_MEM_UNIT|addr|size|count|data */
577         memcpy(command + 13, buffer, size * count);
578
579         if (aice_pipe_write(command, 13 + size * count) < 0)
580                 return ERROR_FAIL;
581
582         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
583                 return ERROR_FAIL;
584
585         if (line[0] == AICE_OK)
586                 return ERROR_OK;
587         else
588                 return ERROR_FAIL;
589
590         return ERROR_OK;
591 }
592
593 static int aice_pipe_read_mem_bulk(uint32_t addr, uint32_t length, uint8_t *buffer)
594 {
595         char line[AICE_PIPE_MAXLINE + 1];
596         char command[AICE_PIPE_MAXLINE];
597         uint32_t remain_len = length;
598         uint32_t prepare_len;
599         char *received_line;
600         uint32_t received_len;
601         int read_len;
602
603         command[0] = AICE_READ_MEM_BULK;
604         set_u32(command + 1, addr);
605         set_u32(command + 5, length);
606
607         if (aice_pipe_write(command, 9) < 0)
608                 return ERROR_FAIL;
609
610         while (remain_len > 0) {
611                 if (remain_len > AICE_PIPE_MAXLINE)
612                         prepare_len = AICE_PIPE_MAXLINE;
613                 else
614                         prepare_len = remain_len;
615
616                 prepare_len++;
617                 received_len = 0;
618                 received_line = line;
619                 do {
620                         read_len = aice_pipe_read(received_line, prepare_len - received_len);
621                         if (read_len < 0)
622                                 return ERROR_FAIL;
623                         received_line += read_len;
624                         received_len += read_len;
625                 } while (received_len < prepare_len);
626
627                 if (line[0] != AICE_OK)
628                         return ERROR_FAIL;
629
630                 prepare_len--;
631                 memcpy(buffer, line + 1, prepare_len);
632                 remain_len -= prepare_len;
633                 buffer += prepare_len;
634         }
635
636         return ERROR_OK;
637 }
638
639 static int aice_pipe_write_mem_bulk(uint32_t addr, uint32_t length, const uint8_t *buffer)
640 {
641         char line[AICE_PIPE_MAXLINE];
642         char command[AICE_PIPE_MAXLINE + 4];
643         uint32_t remain_len = length;
644         uint32_t written_len = 0;
645         uint32_t write_len;
646
647         command[0] = AICE_WRITE_MEM_BULK;
648         set_u32(command + 1, addr);
649         set_u32(command + 5, length);
650
651         /* Send command first */
652         if (aice_pipe_write(command, 9) < 0)
653                 return ERROR_FAIL;
654
655         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
656                 return ERROR_FAIL;
657
658         if (line[0] == AICE_ERROR)
659                 return ERROR_FAIL;
660
661         while (remain_len > 0) {
662                 if (remain_len > AICE_PIPE_MAXLINE)
663                         write_len = AICE_PIPE_MAXLINE;
664                 else
665                         write_len = remain_len;
666
667                 set_u32(command, write_len);
668                 memcpy(command + 4, buffer + written_len, write_len); /* data only */
669
670                 if (aice_pipe_write(command, write_len + 4) < 0)
671                         return ERROR_FAIL;
672
673                 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
674                         return ERROR_FAIL;
675
676                 if (line[0] == AICE_ERROR)
677                         return ERROR_FAIL;
678
679                 remain_len -= write_len;
680                 written_len += write_len;
681         }
682
683         if (line[0] == AICE_OK)
684                 return ERROR_OK;
685         else
686                 return ERROR_FAIL;
687 }
688
689 static int aice_pipe_read_debug_reg(uint32_t addr, uint32_t *val)
690 {
691         char line[AICE_PIPE_MAXLINE];
692         char command[AICE_PIPE_MAXLINE];
693
694         command[0] = AICE_READ_DEBUG_REG;
695         set_u32(command + 1, addr);
696
697         if (aice_pipe_write(command, 5) != 5)
698                 return ERROR_FAIL;
699
700         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
701                 return ERROR_FAIL;
702
703         *val = get_u32(line);
704
705         return ERROR_OK;
706 }
707
708 static int aice_pipe_write_debug_reg(uint32_t addr, const uint32_t val)
709 {
710         char line[AICE_PIPE_MAXLINE];
711         char command[AICE_PIPE_MAXLINE];
712
713         command[0] = AICE_WRITE_DEBUG_REG;
714         set_u32(command + 1, addr);
715         set_u32(command + 5, val);
716
717         if (aice_pipe_write(command, 9) != 9)
718                 return ERROR_FAIL;
719
720         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
721                 return ERROR_FAIL;
722
723         if (line[0] == AICE_OK)
724                 return ERROR_OK;
725         else
726                 return ERROR_FAIL;
727 }
728
729 static int aice_pipe_set_jtag_clock(uint32_t a_clock)
730 {
731         char line[AICE_PIPE_MAXLINE];
732         char command[AICE_PIPE_MAXLINE];
733
734         command[0] = AICE_SET_JTAG_CLOCK;
735         set_u32(command + 1, a_clock);
736
737         if (aice_pipe_write(command, 5) != 5)
738                 return ERROR_FAIL;
739
740         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
741                 return ERROR_FAIL;
742
743         if (line[0] == AICE_OK)
744                 return ERROR_OK;
745         else
746                 return ERROR_FAIL;
747 }
748
749 static int aice_pipe_select_target(uint32_t target_id)
750 {
751         char line[AICE_PIPE_MAXLINE];
752         char command[AICE_PIPE_MAXLINE];
753
754         command[0] = AICE_SELECT_TARGET;
755         set_u32(command + 1, target_id);
756
757         if (aice_pipe_write(command, 5) != 5)
758                 return ERROR_FAIL;
759
760         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
761                 return ERROR_FAIL;
762
763         if (line[0] == AICE_OK)
764                 return ERROR_OK;
765         else
766                 return ERROR_FAIL;
767 }
768
769 static int aice_pipe_memory_access(enum nds_memory_access access_channel)
770 {
771         char line[AICE_PIPE_MAXLINE];
772         char command[AICE_PIPE_MAXLINE];
773
774         command[0] = AICE_MEMORY_ACCESS;
775         set_u32(command + 1, access_channel);
776
777         if (aice_pipe_write(command, 5) != 5)
778                 return ERROR_FAIL;
779
780         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
781                 return ERROR_FAIL;
782
783         if (line[0] == AICE_OK)
784                 return ERROR_OK;
785         else
786                 return ERROR_FAIL;
787 }
788
789 static int aice_pipe_memory_mode(enum nds_memory_select mem_select)
790 {
791         char line[AICE_PIPE_MAXLINE];
792         char command[AICE_PIPE_MAXLINE];
793
794         command[0] = AICE_MEMORY_MODE;
795         set_u32(command + 1, mem_select);
796
797         if (aice_pipe_write(command, 5) != 5)
798                 return ERROR_FAIL;
799
800         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
801                 return ERROR_FAIL;
802
803         if (line[0] == AICE_OK)
804                 return ERROR_OK;
805         else
806                 return ERROR_FAIL;
807 }
808
809 static int aice_pipe_read_tlb(uint32_t virtual_address, uint32_t *physical_address)
810 {
811         char line[AICE_PIPE_MAXLINE];
812         char command[AICE_PIPE_MAXLINE];
813
814         command[0] = AICE_READ_TLB;
815         set_u32(command + 1, virtual_address);
816
817         if (aice_pipe_write(command, 5) != 5)
818                 return ERROR_FAIL;
819
820         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
821                 return ERROR_FAIL;
822
823         if (line[0] == AICE_OK) {
824                 *physical_address = get_u32(line + 1);
825                 return ERROR_OK;
826         } else
827                 return ERROR_FAIL;
828 }
829
830 static int aice_pipe_cache_ctl(uint32_t subtype, uint32_t address)
831 {
832         char line[AICE_PIPE_MAXLINE];
833         char command[AICE_PIPE_MAXLINE];
834
835         command[0] = AICE_CACHE_CTL;
836         set_u32(command + 1, subtype);
837         set_u32(command + 5, address);
838
839         if (aice_pipe_write(command, 9) != 9)
840                 return ERROR_FAIL;
841
842         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
843                 return ERROR_FAIL;
844
845         if (line[0] == AICE_OK)
846                 return ERROR_OK;
847         else
848                 return ERROR_FAIL;
849 }
850
851 static int aice_pipe_set_retry_times(uint32_t a_retry_times)
852 {
853         return ERROR_OK;
854 }
855
856 /** */
857 struct aice_port_api_s aice_pipe = {
858         /** */
859         .open = aice_pipe_open,
860         /** */
861         .close = aice_pipe_close,
862         /** */
863         .idcode = aice_pipe_idcode,
864         /** */
865         .state = aice_pipe_state,
866         /** */
867         .reset = aice_pipe_reset,
868         /** */
869         .assert_srst = aice_pipe_assert_srst,
870         /** */
871         .run = aice_pipe_run,
872         /** */
873         .halt = aice_pipe_halt,
874         /** */
875         .step = aice_pipe_step,
876         /** */
877         .read_reg = aice_pipe_read_reg,
878         /** */
879         .write_reg = aice_pipe_write_reg,
880         /** */
881         .read_reg_64 = aice_pipe_read_reg_64,
882         /** */
883         .write_reg_64 = aice_pipe_write_reg_64,
884         /** */
885         .read_mem_unit = aice_pipe_read_mem_unit,
886         /** */
887         .write_mem_unit = aice_pipe_write_mem_unit,
888         /** */
889         .read_mem_bulk = aice_pipe_read_mem_bulk,
890         /** */
891         .write_mem_bulk = aice_pipe_write_mem_bulk,
892         /** */
893         .read_debug_reg = aice_pipe_read_debug_reg,
894         /** */
895         .write_debug_reg = aice_pipe_write_debug_reg,
896
897         /** */
898         .set_jtag_clock = aice_pipe_set_jtag_clock,
899         /** */
900         .select_target = aice_pipe_select_target,
901
902         /** */
903         .memory_access = aice_pipe_memory_access,
904         /** */
905         .memory_mode = aice_pipe_memory_mode,
906
907         /** */
908         .read_tlb = aice_pipe_read_tlb,
909
910         /** */
911         .cache_ctl = aice_pipe_cache_ctl,
912
913         /** */
914         .set_retry_times = aice_pipe_set_retry_times,
915 };