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