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%08l" PRIx32
, 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%08l" PRIx32
, 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(uint32_t coreid
, 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(uint32_t coreid
, 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(uint32_t coreid
)
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(uint32_t coreid
)
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 coreid
, 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 coreid
, 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 coreid
, 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 coreid
, 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(uint32_t coreid
)
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 coreid
, 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 coreid
, 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 coreid
, uint32_t addr
,
594 uint32_t length
, uint8_t *buffer
)
596 char line
[AICE_PIPE_MAXLINE
+ 1];
597 char command
[AICE_PIPE_MAXLINE
];
598 uint32_t remain_len
= length
;
599 uint32_t prepare_len
;
601 uint32_t received_len
;
604 command
[0] = AICE_READ_MEM_BULK
;
605 set_u32(command
+ 1, addr
);
606 set_u32(command
+ 5, length
);
608 if (aice_pipe_write(command
, 9) < 0)
611 while (remain_len
> 0) {
612 if (remain_len
> AICE_PIPE_MAXLINE
)
613 prepare_len
= AICE_PIPE_MAXLINE
;
615 prepare_len
= remain_len
;
619 received_line
= line
;
621 read_len
= aice_pipe_read(received_line
, prepare_len
- received_len
);
624 received_line
+= read_len
;
625 received_len
+= read_len
;
626 } while (received_len
< prepare_len
);
628 if (line
[0] != AICE_OK
)
632 memcpy(buffer
, line
+ 1, prepare_len
);
633 remain_len
-= prepare_len
;
634 buffer
+= prepare_len
;
640 static int aice_pipe_write_mem_bulk(uint32_t coreid
, uint32_t addr
,
641 uint32_t length
, const uint8_t *buffer
)
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;
649 command
[0] = AICE_WRITE_MEM_BULK
;
650 set_u32(command
+ 1, addr
);
651 set_u32(command
+ 5, length
);
653 /* Send command first */
654 if (aice_pipe_write(command
, 9) < 0)
657 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
660 if (line
[0] == AICE_ERROR
)
663 while (remain_len
> 0) {
664 if (remain_len
> AICE_PIPE_MAXLINE
)
665 write_len
= AICE_PIPE_MAXLINE
;
667 write_len
= remain_len
;
669 set_u32(command
, write_len
);
670 memcpy(command
+ 4, buffer
+ written_len
, write_len
); /* data only */
672 if (aice_pipe_write(command
, write_len
+ 4) < 0)
675 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
678 if (line
[0] == AICE_ERROR
)
681 remain_len
-= write_len
;
682 written_len
+= write_len
;
685 if (line
[0] == AICE_OK
)
691 static int aice_pipe_read_debug_reg(uint32_t coreid
, uint32_t addr
, uint32_t *val
)
693 char line
[AICE_PIPE_MAXLINE
];
694 char command
[AICE_PIPE_MAXLINE
];
696 command
[0] = AICE_READ_DEBUG_REG
;
697 set_u32(command
+ 1, addr
);
699 if (aice_pipe_write(command
, 5) != 5)
702 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
705 *val
= get_u32(line
);
710 static int aice_pipe_write_debug_reg(uint32_t coreid
, uint32_t addr
, const uint32_t val
)
712 char line
[AICE_PIPE_MAXLINE
];
713 char command
[AICE_PIPE_MAXLINE
];
715 command
[0] = AICE_WRITE_DEBUG_REG
;
716 set_u32(command
+ 1, addr
);
717 set_u32(command
+ 5, val
);
719 if (aice_pipe_write(command
, 9) != 9)
722 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
725 if (line
[0] == AICE_OK
)
731 static int aice_pipe_set_jtag_clock(uint32_t a_clock
)
733 char line
[AICE_PIPE_MAXLINE
];
734 char command
[AICE_PIPE_MAXLINE
];
736 command
[0] = AICE_SET_JTAG_CLOCK
;
737 set_u32(command
+ 1, a_clock
);
739 if (aice_pipe_write(command
, 5) != 5)
742 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
745 if (line
[0] == AICE_OK
)
751 static int aice_pipe_memory_access(uint32_t coreid
, enum nds_memory_access access_channel
)
753 char line
[AICE_PIPE_MAXLINE
];
754 char command
[AICE_PIPE_MAXLINE
];
756 command
[0] = AICE_MEMORY_ACCESS
;
757 set_u32(command
+ 1, access_channel
);
759 if (aice_pipe_write(command
, 5) != 5)
762 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
765 if (line
[0] == AICE_OK
)
771 static int aice_pipe_memory_mode(uint32_t coreid
, enum nds_memory_select mem_select
)
773 char line
[AICE_PIPE_MAXLINE
];
774 char command
[AICE_PIPE_MAXLINE
];
776 command
[0] = AICE_MEMORY_MODE
;
777 set_u32(command
+ 1, mem_select
);
779 if (aice_pipe_write(command
, 5) != 5)
782 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
785 if (line
[0] == AICE_OK
)
791 static int aice_pipe_read_tlb(uint32_t coreid
, uint32_t virtual_address
,
792 uint32_t *physical_address
)
794 char line
[AICE_PIPE_MAXLINE
];
795 char command
[AICE_PIPE_MAXLINE
];
797 command
[0] = AICE_READ_TLB
;
798 set_u32(command
+ 1, virtual_address
);
800 if (aice_pipe_write(command
, 5) != 5)
803 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
806 if (line
[0] == AICE_OK
) {
807 *physical_address
= get_u32(line
+ 1);
813 static int aice_pipe_cache_ctl(uint32_t coreid
, uint32_t subtype
, uint32_t address
)
815 char line
[AICE_PIPE_MAXLINE
];
816 char command
[AICE_PIPE_MAXLINE
];
818 command
[0] = AICE_CACHE_CTL
;
819 set_u32(command
+ 1, subtype
);
820 set_u32(command
+ 5, address
);
822 if (aice_pipe_write(command
, 9) != 9)
825 if (aice_pipe_read(line
, AICE_PIPE_MAXLINE
) < 0)
828 if (line
[0] == AICE_OK
)
834 static int aice_pipe_set_retry_times(uint32_t a_retry_times
)
840 struct aice_port_api_s aice_pipe
= {
842 .open
= aice_pipe_open
,
844 .close
= aice_pipe_close
,
846 .idcode
= aice_pipe_idcode
,
848 .set_jtag_clock
= aice_pipe_set_jtag_clock
,
850 .state
= aice_pipe_state
,
852 .reset
= aice_pipe_reset
,
854 .assert_srst
= aice_pipe_assert_srst
,
856 .run
= aice_pipe_run
,
858 .halt
= aice_pipe_halt
,
860 .step
= aice_pipe_step
,
862 .read_reg
= aice_pipe_read_reg
,
864 .write_reg
= aice_pipe_write_reg
,
866 .read_reg_64
= aice_pipe_read_reg_64
,
868 .write_reg_64
= aice_pipe_write_reg_64
,
870 .read_mem_unit
= aice_pipe_read_mem_unit
,
872 .write_mem_unit
= aice_pipe_write_mem_unit
,
874 .read_mem_bulk
= aice_pipe_read_mem_bulk
,
876 .write_mem_bulk
= aice_pipe_write_mem_bulk
,
878 .read_debug_reg
= aice_pipe_read_debug_reg
,
880 .write_debug_reg
= aice_pipe_write_debug_reg
,
883 .memory_access
= aice_pipe_memory_access
,
885 .memory_mode
= aice_pipe_memory_mode
,
888 .read_tlb
= aice_pipe_read_tlb
,
891 .cache_ctl
= aice_pipe_cache_ctl
,
894 .set_retry_times
= aice_pipe_set_retry_times
,