1 /***************************************************************************
2 * Copyright (C) 2013 by Andes Technology *
3 * Hsiangkai Wang <hkwang@andestech.com> *
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. *
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. *
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 ***************************************************************************/
30 #include <helper/log.h>
31 #include <helper/time_support.h>
32 #include "aice_port.h"
33 #include "aice_pipe.h"
35 #define AICE_PIPE_MAXLINE 8192
38 PROCESS_INFORMATION proc_info
;
40 HANDLE aice_pipe_output
[2];
41 HANDLE aice_pipe_input
[2];
43 static int aice_pipe_write(const void *buffer
, int count
)
48 success
= WriteFile(aice_pipe_output
[1], buffer
, count
, &written
, NULL
);
50 LOG_ERROR("(WIN32) write to pipe failed, error code: 0x%08lx", GetLastError());
57 static int aice_pipe_read(void *buffer
, int count
)
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());
71 static int aice_pipe_child_init(struct aice_port_param_s
*param
)
73 STARTUPINFO start_info
;
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
;
84 success
= CreateProcess(NULL
,
96 LOG_ERROR("Create new process failed");
103 static int aice_pipe_parent_init(struct aice_port_param_s
*param
)
105 /* send open to adapter */
106 char line
[AICE_PIPE_MAXLINE
];
107 char command
[AICE_PIPE_MAXLINE
];
109 command
[0] = AICE_OPEN
;
110 set_u16(command
+ 1, param
->vid
);
111 set_u16(command
+ 3, param
->pid
);
113 if (aice_pipe_write(command
, 5) != 5) {
114 LOG_ERROR("write failed\n");
118 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0) {
119 LOG_ERROR("read failed\n");
123 if (line
[0] == AICE_OK
)
129 static int aice_pipe_open(struct aice_port_param_s
*param
)
131 SECURITY_ATTRIBUTES attribute
;
133 attribute
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
134 attribute
.bInheritHandle
= TRUE
;
135 attribute
.lpSecurityDescriptor
= NULL
;
137 if (!CreatePipe(&aice_pipe_output
[0], &aice_pipe_output
[1],
138 &attribute
, AICE_PIPE_MAXLINE
)) {
139 LOG_ERROR("Create pipes failed");
142 if (!CreatePipe(&aice_pipe_input
[0], &aice_pipe_input
[1],
143 &attribute
, AICE_PIPE_MAXLINE
)) {
144 LOG_ERROR("Create pipes failed");
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))
151 if (!SetHandleInformation(aice_pipe_input
[0], HANDLE_FLAG_INHERIT
, 0))
154 aice_pipe_child_init(param
);
156 aice_pipe_parent_init(param
);
163 int aice_pipe_output
[2];
164 int aice_pipe_input
[2];
166 static int aice_pipe_write(const void *buffer
, int count
)
168 if (write(aice_pipe_output
[1], buffer
, count
) != count
) {
169 LOG_ERROR("write to pipe failed");
176 static int aice_pipe_read(void *buffer
, int count
)
184 n
= read(aice_pipe_input
[0], buffer
, count
);
186 if ((n
== -1) && (errno
== EAGAIN
)) {
188 if (cur
- then
> 500)
194 LOG_ERROR("read from pipe failed");
202 static int aice_pipe_child_init(struct aice_port_param_s
*param
)
204 close(aice_pipe_output
[1]);
205 close(aice_pipe_input
[0]);
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");
212 close(aice_pipe_output
[0]);
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");
220 close(aice_pipe_input
[1]);
223 if (execl(param
->adapter_name
, param
->adapter_name
, (char *)0) < 0) {
224 LOG_ERROR("Execute aice_pipe failed");
231 static int aice_pipe_parent_init(struct aice_port_param_s
*param
)
233 close(aice_pipe_output
[0]);
234 close(aice_pipe_input
[1]);
236 /* set read end of pipe as non-blocking */
237 if (fcntl(aice_pipe_input
[0], F_SETFL
, O_NONBLOCK
))
240 /* send open to adapter */
241 char line
[AICE_PIPE_MAXLINE
];
242 char command
[AICE_PIPE_MAXLINE
];
244 command
[0] = AICE_OPEN
;
245 set_u16(command
+ 1, param
->vid
);
246 set_u16(command
+ 3, param
->pid
);
248 if (aice_pipe_write(command
, 5) != 5) {
249 LOG_ERROR("write failed\n");
253 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0) {
254 LOG_ERROR("read failed\n");
258 if (line
[0] == AICE_OK
)
264 static void sig_pipe(int signo
)
269 static int aice_pipe_open(struct aice_port_param_s
*param
)
273 if (signal(SIGPIPE
, sig_pipe
) == SIG_ERR
) {
274 LOG_ERROR("Register SIGPIPE handler failed");
278 if (pipe(aice_pipe_output
) < 0 || pipe(aice_pipe_input
) < 0) {
279 LOG_ERROR("Create pipes failed");
285 LOG_ERROR("Fork new process failed");
287 } else if (pid
== 0) {
288 if (aice_pipe_child_init(param
) != ERROR_OK
) {
289 LOG_ERROR("AICE_PIPE child process initial error");
292 if (aice_pipe_parent_init(param
) != ERROR_OK
) {
293 LOG_ERROR("AICE_PIPE parent process initial error");
303 static int aice_pipe_close(void)
305 char line
[AICE_PIPE_MAXLINE
];
306 char command
[AICE_PIPE_MAXLINE
];
308 command
[0] = AICE_CLOSE
;
310 if (aice_pipe_write(command
, 1) != 1)
313 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
316 if (line
[0] == AICE_OK
) {
318 WaitForSingleObject(proc_info
.hProcess
, INFINITE
);
319 CloseHandle(proc_info
.hProcess
);
320 CloseHandle(proc_info
.hThread
);
327 static int aice_pipe_idcode(uint32_t *idcode
, uint8_t *num_of_idcode
)
329 char line
[AICE_PIPE_MAXLINE
];
330 char command
[AICE_PIPE_MAXLINE
];
332 command
[0] = AICE_IDCODE
;
334 if (aice_pipe_write(command
, 1) != 1)
337 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
340 *num_of_idcode
= line
[0];
342 if ((*num_of_idcode
== 0) || (*num_of_idcode
>= 16))
345 for (int i
= 0 ; i
< *num_of_idcode
; i
++)
346 idcode
[i
] = get_u32(line
+ i
* 4 + 1);
351 static int aice_pipe_state(enum aice_target_state_s
*state
)
353 char line
[AICE_PIPE_MAXLINE
];
354 char command
[AICE_PIPE_MAXLINE
];
356 command
[0] = AICE_STATE
;
358 if (aice_pipe_write(command
, 1) != 1)
361 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
364 *state
= (enum aice_target_state_s
)line
[0];
369 static int aice_pipe_reset(void)
371 char line
[AICE_PIPE_MAXLINE
];
372 char command
[AICE_PIPE_MAXLINE
];
374 command
[0] = AICE_RESET
;
376 if (aice_pipe_write(command
, 1) != 1)
379 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
382 if (line
[0] == AICE_OK
)
388 static int aice_pipe_assert_srst(enum aice_srst_type_s srst
)
390 char line
[AICE_PIPE_MAXLINE
];
391 char command
[AICE_PIPE_MAXLINE
];
393 command
[0] = AICE_ASSERT_SRST
;
396 if (aice_pipe_write(command
, 2) != 2)
399 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
402 if (line
[0] == AICE_OK
)
408 static int aice_pipe_run(void)
410 char line
[AICE_PIPE_MAXLINE
];
411 char command
[AICE_PIPE_MAXLINE
];
413 command
[0] = AICE_RUN
;
415 if (aice_pipe_write(command
, 1) != 1)
418 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
421 if (line
[0] == AICE_OK
)
427 static int aice_pipe_halt(void)
429 char line
[AICE_PIPE_MAXLINE
];
430 char command
[AICE_PIPE_MAXLINE
];
432 command
[0] = AICE_HALT
;
434 if (aice_pipe_write(command
, 1) != 1)
437 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
440 if (line
[0] == AICE_OK
)
446 static int aice_pipe_read_reg(uint32_t num
, uint32_t *val
)
448 char line
[AICE_PIPE_MAXLINE
];
449 char command
[AICE_PIPE_MAXLINE
];
451 command
[0] = AICE_READ_REG
;
452 set_u32(command
+ 1, num
);
454 if (aice_pipe_write(command
, 5) != 5)
457 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
460 *val
= get_u32(line
);
465 static int aice_pipe_write_reg(uint32_t num
, uint32_t val
)
467 char line
[AICE_PIPE_MAXLINE
];
468 char command
[AICE_PIPE_MAXLINE
];
470 command
[0] = AICE_WRITE_REG
;
471 set_u32(command
+ 1, num
);
472 set_u32(command
+ 5, val
);
474 if (aice_pipe_write(command
, 9) != 9)
477 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
480 if (line
[0] == AICE_OK
)
486 static int aice_pipe_read_reg_64(uint32_t num
, uint64_t *val
)
488 char line
[AICE_PIPE_MAXLINE
];
489 char command
[AICE_PIPE_MAXLINE
];
491 command
[0] = AICE_READ_REG_64
;
492 set_u32(command
+ 1, num
);
494 if (aice_pipe_write(command
, 5) != 5)
497 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
500 *val
= (((uint64_t)get_u32(line
+ 4)) << 32) | get_u32(line
);
505 static int aice_pipe_write_reg_64(uint32_t num
, uint64_t val
)
507 char line
[AICE_PIPE_MAXLINE
];
508 char command
[AICE_PIPE_MAXLINE
];
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);
515 if (aice_pipe_write(command
, 13) != 9)
518 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
521 if (line
[0] == AICE_OK
)
527 static int aice_pipe_step(void)
529 char line
[AICE_PIPE_MAXLINE
];
530 char command
[AICE_PIPE_MAXLINE
];
532 command
[0] = AICE_STEP
;
534 if (aice_pipe_write(command
, 1) != 1)
537 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
540 if (line
[0] == AICE_OK
)
546 static int aice_pipe_read_mem_unit(uint32_t addr
, uint32_t size
,
547 uint32_t count
, uint8_t *buffer
)
549 char command
[AICE_PIPE_MAXLINE
];
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
);
556 if (aice_pipe_write(command
, 13) != 13)
559 if (aice_pipe_read(buffer
, size
* count
) < 0)
565 static int aice_pipe_write_mem_unit(uint32_t addr
, uint32_t size
,
566 uint32_t count
, const uint8_t *buffer
)
568 char line
[AICE_PIPE_MAXLINE
];
569 char command
[AICE_PIPE_MAXLINE
];
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
);
576 /* WRITE_MEM_UNIT|addr|size|count|data */
577 memcpy(command
+ 13, buffer
, size
* count
);
579 if (aice_pipe_write(command
, 13 + size
* count
) < 0)
582 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
585 if (line
[0] == AICE_OK
)
593 static int aice_pipe_read_mem_bulk(uint32_t addr
, uint32_t length
, uint8_t *buffer
)
595 char line
[AICE_PIPE_MAXLINE
+ 1];
596 char command
[AICE_PIPE_MAXLINE
];
597 uint32_t remain_len
= length
;
598 uint32_t prepare_len
;
600 uint32_t received_len
;
603 command
[0] = AICE_READ_MEM_BULK
;
604 set_u32(command
+ 1, addr
);
605 set_u32(command
+ 5, length
);
607 if (aice_pipe_write(command
, 9) < 0)
610 while (remain_len
> 0) {
611 if (remain_len
> AICE_PIPE_MAXLINE
)
612 prepare_len
= AICE_PIPE_MAXLINE
;
614 prepare_len
= remain_len
;
618 received_line
= line
;
620 read_len
= aice_pipe_read(received_line
, prepare_len
- received_len
);
623 received_line
+= read_len
;
624 received_len
+= read_len
;
625 } while (received_len
< prepare_len
);
627 if (line
[0] != AICE_OK
)
631 memcpy(buffer
, line
+ 1, prepare_len
);
632 remain_len
-= prepare_len
;
633 buffer
+= prepare_len
;
639 static int aice_pipe_write_mem_bulk(uint32_t addr
, uint32_t length
, const uint8_t *buffer
)
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;
647 command
[0] = AICE_WRITE_MEM_BULK
;
648 set_u32(command
+ 1, addr
);
649 set_u32(command
+ 5, length
);
651 /* Send command first */
652 if (aice_pipe_write(command
, 9) < 0)
655 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
658 if (line
[0] == AICE_ERROR
)
661 while (remain_len
> 0) {
662 if (remain_len
> AICE_PIPE_MAXLINE
)
663 write_len
= AICE_PIPE_MAXLINE
;
665 write_len
= remain_len
;
667 set_u32(command
, write_len
);
668 memcpy(command
+ 4, buffer
+ written_len
, write_len
); /* data only */
670 if (aice_pipe_write(command
, write_len
+ 4) < 0)
673 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
676 if (line
[0] == AICE_ERROR
)
679 remain_len
-= write_len
;
680 written_len
+= write_len
;
683 if (line
[0] == AICE_OK
)
689 static int aice_pipe_read_debug_reg(uint32_t addr
, uint32_t *val
)
691 char line
[AICE_PIPE_MAXLINE
];
692 char command
[AICE_PIPE_MAXLINE
];
694 command
[0] = AICE_READ_DEBUG_REG
;
695 set_u32(command
+ 1, addr
);
697 if (aice_pipe_write(command
, 5) != 5)
700 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
703 *val
= get_u32(line
);
708 static int aice_pipe_write_debug_reg(uint32_t addr
, const uint32_t val
)
710 char line
[AICE_PIPE_MAXLINE
];
711 char command
[AICE_PIPE_MAXLINE
];
713 command
[0] = AICE_WRITE_DEBUG_REG
;
714 set_u32(command
+ 1, addr
);
715 set_u32(command
+ 5, val
);
717 if (aice_pipe_write(command
, 9) != 9)
720 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
723 if (line
[0] == AICE_OK
)
729 static int aice_pipe_set_jtag_clock(uint32_t a_clock
)
731 char line
[AICE_PIPE_MAXLINE
];
732 char command
[AICE_PIPE_MAXLINE
];
734 command
[0] = AICE_SET_JTAG_CLOCK
;
735 set_u32(command
+ 1, a_clock
);
737 if (aice_pipe_write(command
, 5) != 5)
740 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
743 if (line
[0] == AICE_OK
)
749 static int aice_pipe_select_target(uint32_t target_id
)
751 char line
[AICE_PIPE_MAXLINE
];
752 char command
[AICE_PIPE_MAXLINE
];
754 command
[0] = AICE_SELECT_TARGET
;
755 set_u32(command
+ 1, target_id
);
757 if (aice_pipe_write(command
, 5) != 5)
760 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
763 if (line
[0] == AICE_OK
)
769 static int aice_pipe_memory_access(enum nds_memory_access access_channel
)
771 char line
[AICE_PIPE_MAXLINE
];
772 char command
[AICE_PIPE_MAXLINE
];
774 command
[0] = AICE_MEMORY_ACCESS
;
775 set_u32(command
+ 1, access_channel
);
777 if (aice_pipe_write(command
, 5) != 5)
780 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
783 if (line
[0] == AICE_OK
)
789 static int aice_pipe_memory_mode(enum nds_memory_select mem_select
)
791 char line
[AICE_PIPE_MAXLINE
];
792 char command
[AICE_PIPE_MAXLINE
];
794 command
[0] = AICE_MEMORY_MODE
;
795 set_u32(command
+ 1, mem_select
);
797 if (aice_pipe_write(command
, 5) != 5)
800 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
803 if (line
[0] == AICE_OK
)
809 static int aice_pipe_read_tlb(uint32_t virtual_address
, uint32_t *physical_address
)
811 char line
[AICE_PIPE_MAXLINE
];
812 char command
[AICE_PIPE_MAXLINE
];
814 command
[0] = AICE_READ_TLB
;
815 set_u32(command
+ 1, virtual_address
);
817 if (aice_pipe_write(command
, 5) != 5)
820 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
823 if (line
[0] == AICE_OK
) {
824 *physical_address
= get_u32(line
+ 1);
830 static int aice_pipe_cache_ctl(uint32_t subtype
, uint32_t address
)
832 char line
[AICE_PIPE_MAXLINE
];
833 char command
[AICE_PIPE_MAXLINE
];
835 command
[0] = AICE_CACHE_CTL
;
836 set_u32(command
+ 1, subtype
);
837 set_u32(command
+ 5, address
);
839 if (aice_pipe_write(command
, 9) != 9)
842 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
845 if (line
[0] == AICE_OK
)
851 static int aice_pipe_set_retry_times(uint32_t a_retry_times
)
857 struct aice_port_api_s aice_pipe
= {
859 .open
= aice_pipe_open
,
861 .close
= aice_pipe_close
,
863 .idcode
= aice_pipe_idcode
,
865 .state
= aice_pipe_state
,
867 .reset
= aice_pipe_reset
,
869 .assert_srst
= aice_pipe_assert_srst
,
871 .run
= aice_pipe_run
,
873 .halt
= aice_pipe_halt
,
875 .step
= aice_pipe_step
,
877 .read_reg
= aice_pipe_read_reg
,
879 .write_reg
= aice_pipe_write_reg
,
881 .read_reg_64
= aice_pipe_read_reg_64
,
883 .write_reg_64
= aice_pipe_write_reg_64
,
885 .read_mem_unit
= aice_pipe_read_mem_unit
,
887 .write_mem_unit
= aice_pipe_write_mem_unit
,
889 .read_mem_bulk
= aice_pipe_read_mem_bulk
,
891 .write_mem_bulk
= aice_pipe_write_mem_bulk
,
893 .read_debug_reg
= aice_pipe_read_debug_reg
,
895 .write_debug_reg
= aice_pipe_write_debug_reg
,
898 .set_jtag_clock
= aice_pipe_set_jtag_clock
,
900 .select_target
= aice_pipe_select_target
,
903 .memory_access
= aice_pipe_memory_access
,
905 .memory_mode
= aice_pipe_memory_mode
,
908 .read_tlb
= aice_pipe_read_tlb
,
911 .cache_ctl
= aice_pipe_cache_ctl
,
914 .set_retry_times
= aice_pipe_set_retry_times
,