]> git.sur5r.net Git - openocd/blob - src/jtag/aice/aice_pipe.c
nds32: support multi-target debugging
[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(uint32_t coreid, 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(uint32_t coreid, 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(uint32_t coreid)
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(uint32_t coreid)
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 coreid, 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 coreid, 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 coreid, 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 coreid, 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(uint32_t coreid)
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 coreid, 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 coreid, 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 coreid, uint32_t addr,
594                 uint32_t length, uint8_t *buffer)
595 {
596         char line[AICE_PIPE_MAXLINE + 1];
597         char command[AICE_PIPE_MAXLINE];
598         uint32_t remain_len = length;
599         uint32_t prepare_len;
600         char *received_line;
601         uint32_t received_len;
602         int read_len;
603
604         command[0] = AICE_READ_MEM_BULK;
605         set_u32(command + 1, addr);
606         set_u32(command + 5, length);
607
608         if (aice_pipe_write(command, 9) < 0)
609                 return ERROR_FAIL;
610
611         while (remain_len > 0) {
612                 if (remain_len > AICE_PIPE_MAXLINE)
613                         prepare_len = AICE_PIPE_MAXLINE;
614                 else
615                         prepare_len = remain_len;
616
617                 prepare_len++;
618                 received_len = 0;
619                 received_line = line;
620                 do {
621                         read_len = aice_pipe_read(received_line, prepare_len - received_len);
622                         if (read_len < 0)
623                                 return ERROR_FAIL;
624                         received_line += read_len;
625                         received_len += read_len;
626                 } while (received_len < prepare_len);
627
628                 if (line[0] != AICE_OK)
629                         return ERROR_FAIL;
630
631                 prepare_len--;
632                 memcpy(buffer, line + 1, prepare_len);
633                 remain_len -= prepare_len;
634                 buffer += prepare_len;
635         }
636
637         return ERROR_OK;
638 }
639
640 static int aice_pipe_write_mem_bulk(uint32_t coreid, uint32_t addr,
641                 uint32_t length, const uint8_t *buffer)
642 {
643         char line[AICE_PIPE_MAXLINE];
644         char command[AICE_PIPE_MAXLINE + 4];
645         uint32_t remain_len = length;
646         uint32_t written_len = 0;
647         uint32_t write_len;
648
649         command[0] = AICE_WRITE_MEM_BULK;
650         set_u32(command + 1, addr);
651         set_u32(command + 5, length);
652
653         /* Send command first */
654         if (aice_pipe_write(command, 9) < 0)
655                 return ERROR_FAIL;
656
657         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
658                 return ERROR_FAIL;
659
660         if (line[0] == AICE_ERROR)
661                 return ERROR_FAIL;
662
663         while (remain_len > 0) {
664                 if (remain_len > AICE_PIPE_MAXLINE)
665                         write_len = AICE_PIPE_MAXLINE;
666                 else
667                         write_len = remain_len;
668
669                 set_u32(command, write_len);
670                 memcpy(command + 4, buffer + written_len, write_len); /* data only */
671
672                 if (aice_pipe_write(command, write_len + 4) < 0)
673                         return ERROR_FAIL;
674
675                 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
676                         return ERROR_FAIL;
677
678                 if (line[0] == AICE_ERROR)
679                         return ERROR_FAIL;
680
681                 remain_len -= write_len;
682                 written_len += write_len;
683         }
684
685         if (line[0] == AICE_OK)
686                 return ERROR_OK;
687         else
688                 return ERROR_FAIL;
689 }
690
691 static int aice_pipe_read_debug_reg(uint32_t coreid, uint32_t addr, uint32_t *val)
692 {
693         char line[AICE_PIPE_MAXLINE];
694         char command[AICE_PIPE_MAXLINE];
695
696         command[0] = AICE_READ_DEBUG_REG;
697         set_u32(command + 1, addr);
698
699         if (aice_pipe_write(command, 5) != 5)
700                 return ERROR_FAIL;
701
702         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
703                 return ERROR_FAIL;
704
705         *val = get_u32(line);
706
707         return ERROR_OK;
708 }
709
710 static int aice_pipe_write_debug_reg(uint32_t coreid, uint32_t addr, const uint32_t val)
711 {
712         char line[AICE_PIPE_MAXLINE];
713         char command[AICE_PIPE_MAXLINE];
714
715         command[0] = AICE_WRITE_DEBUG_REG;
716         set_u32(command + 1, addr);
717         set_u32(command + 5, val);
718
719         if (aice_pipe_write(command, 9) != 9)
720                 return ERROR_FAIL;
721
722         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
723                 return ERROR_FAIL;
724
725         if (line[0] == AICE_OK)
726                 return ERROR_OK;
727         else
728                 return ERROR_FAIL;
729 }
730
731 static int aice_pipe_set_jtag_clock(uint32_t a_clock)
732 {
733         char line[AICE_PIPE_MAXLINE];
734         char command[AICE_PIPE_MAXLINE];
735
736         command[0] = AICE_SET_JTAG_CLOCK;
737         set_u32(command + 1, a_clock);
738
739         if (aice_pipe_write(command, 5) != 5)
740                 return ERROR_FAIL;
741
742         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
743                 return ERROR_FAIL;
744
745         if (line[0] == AICE_OK)
746                 return ERROR_OK;
747         else
748                 return ERROR_FAIL;
749 }
750
751 static int aice_pipe_memory_access(uint32_t coreid, enum nds_memory_access access_channel)
752 {
753         char line[AICE_PIPE_MAXLINE];
754         char command[AICE_PIPE_MAXLINE];
755
756         command[0] = AICE_MEMORY_ACCESS;
757         set_u32(command + 1, access_channel);
758
759         if (aice_pipe_write(command, 5) != 5)
760                 return ERROR_FAIL;
761
762         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
763                 return ERROR_FAIL;
764
765         if (line[0] == AICE_OK)
766                 return ERROR_OK;
767         else
768                 return ERROR_FAIL;
769 }
770
771 static int aice_pipe_memory_mode(uint32_t coreid, enum nds_memory_select mem_select)
772 {
773         char line[AICE_PIPE_MAXLINE];
774         char command[AICE_PIPE_MAXLINE];
775
776         command[0] = AICE_MEMORY_MODE;
777         set_u32(command + 1, mem_select);
778
779         if (aice_pipe_write(command, 5) != 5)
780                 return ERROR_FAIL;
781
782         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
783                 return ERROR_FAIL;
784
785         if (line[0] == AICE_OK)
786                 return ERROR_OK;
787         else
788                 return ERROR_FAIL;
789 }
790
791 static int aice_pipe_read_tlb(uint32_t coreid, uint32_t virtual_address,
792                 uint32_t *physical_address)
793 {
794         char line[AICE_PIPE_MAXLINE];
795         char command[AICE_PIPE_MAXLINE];
796
797         command[0] = AICE_READ_TLB;
798         set_u32(command + 1, virtual_address);
799
800         if (aice_pipe_write(command, 5) != 5)
801                 return ERROR_FAIL;
802
803         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
804                 return ERROR_FAIL;
805
806         if (line[0] == AICE_OK) {
807                 *physical_address = get_u32(line + 1);
808                 return ERROR_OK;
809         } else
810                 return ERROR_FAIL;
811 }
812
813 static int aice_pipe_cache_ctl(uint32_t coreid, uint32_t subtype, uint32_t address)
814 {
815         char line[AICE_PIPE_MAXLINE];
816         char command[AICE_PIPE_MAXLINE];
817
818         command[0] = AICE_CACHE_CTL;
819         set_u32(command + 1, subtype);
820         set_u32(command + 5, address);
821
822         if (aice_pipe_write(command, 9) != 9)
823                 return ERROR_FAIL;
824
825         if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
826                 return ERROR_FAIL;
827
828         if (line[0] == AICE_OK)
829                 return ERROR_OK;
830         else
831                 return ERROR_FAIL;
832 }
833
834 static int aice_pipe_set_retry_times(uint32_t a_retry_times)
835 {
836         return ERROR_OK;
837 }
838
839 /** */
840 struct aice_port_api_s aice_pipe = {
841         /** */
842         .open = aice_pipe_open,
843         /** */
844         .close = aice_pipe_close,
845         /** */
846         .idcode = aice_pipe_idcode,
847         /** */
848         .set_jtag_clock = aice_pipe_set_jtag_clock,
849         /** */
850         .state = aice_pipe_state,
851         /** */
852         .reset = aice_pipe_reset,
853         /** */
854         .assert_srst = aice_pipe_assert_srst,
855         /** */
856         .run = aice_pipe_run,
857         /** */
858         .halt = aice_pipe_halt,
859         /** */
860         .step = aice_pipe_step,
861         /** */
862         .read_reg = aice_pipe_read_reg,
863         /** */
864         .write_reg = aice_pipe_write_reg,
865         /** */
866         .read_reg_64 = aice_pipe_read_reg_64,
867         /** */
868         .write_reg_64 = aice_pipe_write_reg_64,
869         /** */
870         .read_mem_unit = aice_pipe_read_mem_unit,
871         /** */
872         .write_mem_unit = aice_pipe_write_mem_unit,
873         /** */
874         .read_mem_bulk = aice_pipe_read_mem_bulk,
875         /** */
876         .write_mem_bulk = aice_pipe_write_mem_bulk,
877         /** */
878         .read_debug_reg = aice_pipe_read_debug_reg,
879         /** */
880         .write_debug_reg = aice_pipe_write_debug_reg,
881
882         /** */
883         .memory_access = aice_pipe_memory_access,
884         /** */
885         .memory_mode = aice_pipe_memory_mode,
886
887         /** */
888         .read_tlb = aice_pipe_read_tlb,
889
890         /** */
891         .cache_ctl = aice_pipe_cache_ctl,
892
893         /** */
894         .set_retry_times = aice_pipe_set_retry_times,
895 };