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