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, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
28 #include <helper/log.h>
29 #include <helper/time_support.h>
30 #include "aice_port.h"
31 #include "aice_pipe.h"
33 #define AICE_PIPE_MAXLINE 8192
36 PROCESS_INFORMATION proc_info;
38 HANDLE aice_pipe_output[2];
39 HANDLE aice_pipe_input[2];
41 static int aice_pipe_write(const void *buffer, int count)
46 success = WriteFile(aice_pipe_output[1], buffer, count, &written, NULL);
48 LOG_ERROR("(WIN32) write to pipe failed, error code: 0x%08l" PRIx32, GetLastError());
55 static int aice_pipe_read(void *buffer, int count)
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());
69 static int aice_pipe_child_init(struct aice_port_param_s *param)
71 STARTUPINFO start_info;
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;
82 success = CreateProcess(NULL,
94 LOG_ERROR("Create new process failed");
101 static int aice_pipe_parent_init(struct aice_port_param_s *param)
103 /* send open to adapter */
104 char line[AICE_PIPE_MAXLINE];
105 char command[AICE_PIPE_MAXLINE];
107 command[0] = AICE_OPEN;
108 set_u16(command + 1, param->vid);
109 set_u16(command + 3, param->pid);
111 if (aice_pipe_write(command, 5) != 5) {
112 LOG_ERROR("write failed\n");
116 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0) {
117 LOG_ERROR("read failed\n");
121 if (line[0] == AICE_OK)
127 static int aice_pipe_open(struct aice_port_param_s *param)
129 SECURITY_ATTRIBUTES attribute;
131 attribute.nLength = sizeof(SECURITY_ATTRIBUTES);
132 attribute.bInheritHandle = TRUE;
133 attribute.lpSecurityDescriptor = NULL;
135 if (!CreatePipe(&aice_pipe_output[0], &aice_pipe_output[1],
136 &attribute, AICE_PIPE_MAXLINE)) {
137 LOG_ERROR("Create pipes failed");
140 if (!CreatePipe(&aice_pipe_input[0], &aice_pipe_input[1],
141 &attribute, AICE_PIPE_MAXLINE)) {
142 LOG_ERROR("Create pipes failed");
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))
149 if (!SetHandleInformation(aice_pipe_input[0], HANDLE_FLAG_INHERIT, 0))
152 aice_pipe_child_init(param);
154 aice_pipe_parent_init(param);
161 int aice_pipe_output[2];
162 int aice_pipe_input[2];
164 static int aice_pipe_write(const void *buffer, int count)
166 if (write(aice_pipe_output[1], buffer, count) != count) {
167 LOG_ERROR("write to pipe failed");
174 static int aice_pipe_read(void *buffer, int count)
182 n = read(aice_pipe_input[0], buffer, count);
184 if ((n == -1) && (errno == EAGAIN)) {
186 if (cur - then > 500)
192 LOG_ERROR("read from pipe failed");
200 static int aice_pipe_child_init(struct aice_port_param_s *param)
202 close(aice_pipe_output[1]);
203 close(aice_pipe_input[0]);
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");
210 close(aice_pipe_output[0]);
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");
218 close(aice_pipe_input[1]);
221 if (execl(param->adapter_name, param->adapter_name, (char *)0) < 0) {
222 LOG_ERROR("Execute aice_pipe failed");
229 static int aice_pipe_parent_init(struct aice_port_param_s *param)
231 close(aice_pipe_output[0]);
232 close(aice_pipe_input[1]);
234 /* set read end of pipe as non-blocking */
235 if (fcntl(aice_pipe_input[0], F_SETFL, O_NONBLOCK))
238 /* send open to adapter */
239 char line[AICE_PIPE_MAXLINE];
240 char command[AICE_PIPE_MAXLINE];
242 command[0] = AICE_OPEN;
243 set_u16(command + 1, param->vid);
244 set_u16(command + 3, param->pid);
246 if (aice_pipe_write(command, 5) != 5) {
247 LOG_ERROR("write failed\n");
251 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0) {
252 LOG_ERROR("read failed\n");
256 if (line[0] == AICE_OK)
262 static void sig_pipe(int signo)
267 static int aice_pipe_open(struct aice_port_param_s *param)
271 if (signal(SIGPIPE, sig_pipe) == SIG_ERR) {
272 LOG_ERROR("Register SIGPIPE handler failed");
276 if (pipe(aice_pipe_output) < 0 || pipe(aice_pipe_input) < 0) {
277 LOG_ERROR("Create pipes failed");
283 LOG_ERROR("Fork new process failed");
285 } else if (pid == 0) {
286 if (aice_pipe_child_init(param) != ERROR_OK) {
287 LOG_ERROR("AICE_PIPE child process initial error");
290 if (aice_pipe_parent_init(param) != ERROR_OK) {
291 LOG_ERROR("AICE_PIPE parent process initial error");
301 static int aice_pipe_close(void)
303 char line[AICE_PIPE_MAXLINE];
304 char command[AICE_PIPE_MAXLINE];
306 command[0] = AICE_CLOSE;
308 if (aice_pipe_write(command, 1) != 1)
311 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
314 if (line[0] == AICE_OK) {
316 WaitForSingleObject(proc_info.hProcess, INFINITE);
317 CloseHandle(proc_info.hProcess);
318 CloseHandle(proc_info.hThread);
325 static int aice_pipe_idcode(uint32_t *idcode, uint8_t *num_of_idcode)
327 char line[AICE_PIPE_MAXLINE];
328 char command[AICE_PIPE_MAXLINE];
330 command[0] = AICE_IDCODE;
332 if (aice_pipe_write(command, 1) != 1)
335 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
338 *num_of_idcode = line[0];
340 if ((*num_of_idcode == 0) || (*num_of_idcode >= 16))
343 for (int i = 0 ; i < *num_of_idcode ; i++)
344 idcode[i] = get_u32(line + i * 4 + 1);
349 static int aice_pipe_state(uint32_t coreid, enum aice_target_state_s *state)
351 char line[AICE_PIPE_MAXLINE];
352 char command[AICE_PIPE_MAXLINE];
354 command[0] = AICE_STATE;
356 if (aice_pipe_write(command, 1) != 1)
359 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
362 *state = (enum aice_target_state_s)line[0];
367 static int aice_pipe_reset(void)
369 char line[AICE_PIPE_MAXLINE];
370 char command[AICE_PIPE_MAXLINE];
372 command[0] = AICE_RESET;
374 if (aice_pipe_write(command, 1) != 1)
377 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
380 if (line[0] == AICE_OK)
386 static int aice_pipe_assert_srst(uint32_t coreid, enum aice_srst_type_s srst)
388 char line[AICE_PIPE_MAXLINE];
389 char command[AICE_PIPE_MAXLINE];
391 command[0] = AICE_ASSERT_SRST;
394 if (aice_pipe_write(command, 2) != 2)
397 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
400 if (line[0] == AICE_OK)
406 static int aice_pipe_run(uint32_t coreid)
408 char line[AICE_PIPE_MAXLINE];
409 char command[AICE_PIPE_MAXLINE];
411 command[0] = AICE_RUN;
413 if (aice_pipe_write(command, 1) != 1)
416 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
419 if (line[0] == AICE_OK)
425 static int aice_pipe_halt(uint32_t coreid)
427 char line[AICE_PIPE_MAXLINE];
428 char command[AICE_PIPE_MAXLINE];
430 command[0] = AICE_HALT;
432 if (aice_pipe_write(command, 1) != 1)
435 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
438 if (line[0] == AICE_OK)
444 static int aice_pipe_read_reg(uint32_t coreid, uint32_t num, uint32_t *val)
446 char line[AICE_PIPE_MAXLINE];
447 char command[AICE_PIPE_MAXLINE];
449 command[0] = AICE_READ_REG;
450 set_u32(command + 1, num);
452 if (aice_pipe_write(command, 5) != 5)
455 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
458 *val = get_u32(line);
463 static int aice_pipe_write_reg(uint32_t coreid, uint32_t num, uint32_t val)
465 char line[AICE_PIPE_MAXLINE];
466 char command[AICE_PIPE_MAXLINE];
468 command[0] = AICE_WRITE_REG;
469 set_u32(command + 1, num);
470 set_u32(command + 5, val);
472 if (aice_pipe_write(command, 9) != 9)
475 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
478 if (line[0] == AICE_OK)
484 static int aice_pipe_read_reg_64(uint32_t coreid, uint32_t num, uint64_t *val)
486 char line[AICE_PIPE_MAXLINE];
487 char command[AICE_PIPE_MAXLINE];
489 command[0] = AICE_READ_REG_64;
490 set_u32(command + 1, num);
492 if (aice_pipe_write(command, 5) != 5)
495 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
498 *val = (((uint64_t)get_u32(line + 4)) << 32) | get_u32(line);
503 static int aice_pipe_write_reg_64(uint32_t coreid, uint32_t num, uint64_t val)
505 char line[AICE_PIPE_MAXLINE];
506 char command[AICE_PIPE_MAXLINE];
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);
513 if (aice_pipe_write(command, 13) != 9)
516 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
519 if (line[0] == AICE_OK)
525 static int aice_pipe_step(uint32_t coreid)
527 char line[AICE_PIPE_MAXLINE];
528 char command[AICE_PIPE_MAXLINE];
530 command[0] = AICE_STEP;
532 if (aice_pipe_write(command, 1) != 1)
535 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
538 if (line[0] == AICE_OK)
544 static int aice_pipe_read_mem_unit(uint32_t coreid, uint32_t addr, uint32_t size,
545 uint32_t count, uint8_t *buffer)
547 char command[AICE_PIPE_MAXLINE];
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);
554 if (aice_pipe_write(command, 13) != 13)
557 if (aice_pipe_read(buffer, size * count) < 0)
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)
566 char line[AICE_PIPE_MAXLINE];
567 char command[AICE_PIPE_MAXLINE];
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);
574 /* WRITE_MEM_UNIT|addr|size|count|data */
575 memcpy(command + 13, buffer, size * count);
577 if (aice_pipe_write(command, 13 + size * count) < 0)
580 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
583 if (line[0] == AICE_OK)
591 static int aice_pipe_read_mem_bulk(uint32_t coreid, uint32_t addr,
592 uint32_t length, uint8_t *buffer)
594 char line[AICE_PIPE_MAXLINE + 1];
595 char command[AICE_PIPE_MAXLINE];
596 uint32_t remain_len = length;
597 uint32_t prepare_len;
599 uint32_t received_len;
602 command[0] = AICE_READ_MEM_BULK;
603 set_u32(command + 1, addr);
604 set_u32(command + 5, length);
606 if (aice_pipe_write(command, 9) < 0)
609 while (remain_len > 0) {
610 if (remain_len > AICE_PIPE_MAXLINE)
611 prepare_len = AICE_PIPE_MAXLINE;
613 prepare_len = remain_len;
617 received_line = line;
619 read_len = aice_pipe_read(received_line, prepare_len - received_len);
622 received_line += read_len;
623 received_len += read_len;
624 } while (received_len < prepare_len);
626 if (line[0] != AICE_OK)
630 memcpy(buffer, line + 1, prepare_len);
631 remain_len -= prepare_len;
632 buffer += prepare_len;
638 static int aice_pipe_write_mem_bulk(uint32_t coreid, uint32_t addr,
639 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 coreid, 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 coreid, 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_memory_access(uint32_t coreid, enum nds_memory_access access_channel)
751 char line[AICE_PIPE_MAXLINE];
752 char command[AICE_PIPE_MAXLINE];
754 command[0] = AICE_MEMORY_ACCESS;
755 set_u32(command + 1, access_channel);
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_mode(uint32_t coreid, enum nds_memory_select mem_select)
771 char line[AICE_PIPE_MAXLINE];
772 char command[AICE_PIPE_MAXLINE];
774 command[0] = AICE_MEMORY_MODE;
775 set_u32(command + 1, mem_select);
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_read_tlb(uint32_t coreid, uint32_t virtual_address,
790 uint32_t *physical_address)
792 char line[AICE_PIPE_MAXLINE];
793 char command[AICE_PIPE_MAXLINE];
795 command[0] = AICE_READ_TLB;
796 set_u32(command + 1, virtual_address);
798 if (aice_pipe_write(command, 5) != 5)
801 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
804 if (line[0] == AICE_OK) {
805 *physical_address = get_u32(line + 1);
811 static int aice_pipe_cache_ctl(uint32_t coreid, uint32_t subtype, uint32_t address)
813 char line[AICE_PIPE_MAXLINE];
814 char command[AICE_PIPE_MAXLINE];
816 command[0] = AICE_CACHE_CTL;
817 set_u32(command + 1, subtype);
818 set_u32(command + 5, address);
820 if (aice_pipe_write(command, 9) != 9)
823 if (aice_pipe_read(line, AICE_PIPE_MAXLINE) < 0)
826 if (line[0] == AICE_OK)
832 static int aice_pipe_set_retry_times(uint32_t a_retry_times)
838 struct aice_port_api_s aice_pipe = {
840 .open = aice_pipe_open,
842 .close = aice_pipe_close,
844 .idcode = aice_pipe_idcode,
846 .set_jtag_clock = aice_pipe_set_jtag_clock,
848 .state = aice_pipe_state,
850 .reset = aice_pipe_reset,
852 .assert_srst = aice_pipe_assert_srst,
854 .run = aice_pipe_run,
856 .halt = aice_pipe_halt,
858 .step = aice_pipe_step,
860 .read_reg = aice_pipe_read_reg,
862 .write_reg = aice_pipe_write_reg,
864 .read_reg_64 = aice_pipe_read_reg_64,
866 .write_reg_64 = aice_pipe_write_reg_64,
868 .read_mem_unit = aice_pipe_read_mem_unit,
870 .write_mem_unit = aice_pipe_write_mem_unit,
872 .read_mem_bulk = aice_pipe_read_mem_bulk,
874 .write_mem_bulk = aice_pipe_write_mem_bulk,
876 .read_debug_reg = aice_pipe_read_debug_reg,
878 .write_debug_reg = aice_pipe_write_debug_reg,
881 .memory_access = aice_pipe_memory_access,
883 .memory_mode = aice_pipe_memory_mode,
886 .read_tlb = aice_pipe_read_tlb,
889 .cache_ctl = aice_pipe_cache_ctl,
892 .set_retry_times = aice_pipe_set_retry_times,