11#include "acuart_internal.h"
15static int uart_eve_alloc()
23 ret = CreateEventFlag(¶m);
26 printf(
"acuart:eve_alloc: error %d\n", ret);
31static int uart_xmit(
struct uart_buf *xmit, acUartReg uartreg)
50 size = xmit->ub_size - tail;
62 for ( size_v6 = size - 1; size_v6 >= 0; --size_v6 )
71 size_v8 = head - tail;
74 const acUint8 *src_v9;
78 if (
count < size_v8 )
88 for ( size_v10 = size_v8 - 1; size_v10 >= 0; --size_v10 )
95 return 2 * (
count < 16);
98int acUartWrite(
void *buf,
int count)
110 if ( !buf ||
count < 0 )
114 src = (acUint8 *)buf;
120 head = Uartc.xmit.ub_head;
121 tail = Uartc.xmit.ub_tail;
123 ub_buf = Uartc.xmit.ub_buf;
124 bufsize = Uartc.xmit.ub_size;
131 xlen = Uartc.xmit.ub_size - head;
132 dst = &Uartc.xmit.ub_buf[head];
133 if (
count < (
signed int)(Uartc.xmit.ub_size - head) )
143 for ( xlen_v9 = xlen - 1; xlen_v9 >= 0; ++dst )
149 if ( v6 && head < tail )
155 xlen_v11 = tail - head;
156 dst_v12 = &ub_buf[head];
157 if ( v6 < tail - head )
167 for ( xlen_v13 = xlen_v11 - 1; xlen_v13 >= 0; ++dst_v12 )
181 SetEventFlag(eve, 2u);
183 Uartc.xmit.ub_head = head;
187 if ( (*((
volatile acUint16 *)0xB241800A) & 0x20) != 0 )
189 uart_xmit(&Uartc.xmit, (acUartReg)0xB2418000);
190 *((
volatile acUint16 *)0xB2418002) = *((
volatile acUint16 *)0xB2418002) | 2;
196int acUartRead(
void *buf,
int count)
206 if ( !buf ||
count < 0 )
212 dst = (acUint8 *)buf;
213 if ( Uartc.eve == 0 )
219 head = Uartc.recv.ub_head;
220 tail = Uartc.recv.ub_tail;
226 ub_buf = Uartc.recv.ub_buf;
234 bufsize = Uartc.recv.ub_size - tail;
235 src = &Uartc.recv.ub_buf[tail];
236 if (
count < (
signed int)(Uartc.recv.ub_size - tail) )
245 v9 =
count - bufsize;
246 for ( bufsize_v9 = bufsize - 1; bufsize_v9 >= 0; ++dst )
252 if ( v9 && tail < head )
255 const acUint8 *src_v12;
258 bufsize_v11 = head - tail;
259 src_v12 = &ub_buf[tail];
260 if ( v9 < head - tail )
270 for ( bufsize_v13 = bufsize_v11 - 1; bufsize_v13 >= 0; ++dst )
276 Uartc.recv.ub_tail = tail;
278 SetEventFlag(eve, 1u);
282static unsigned int uart_timedout(
void *arg)
284 iReleaseWaitThread((
int)arg);
288int acUartWait(acUartFlag rw,
int usec)
300 if ( Uartc.eve == 0 )
304 thid = GetThreadId();
307 USec2SysClock(usec, &tmout);
308 SetAlarm(&tmout, uart_timedout, (
void *)thid);
311 v7 = (usec > 0 ? WaitEventFlag : PollEventFlag)(Uartc.eve, rw, 17, &result_v7);
316 else if ( v7 >= -417 )
328 CancelAlarm(uart_timedout, (
void *)thid);
333 ret_v5 = result_v7 & rw;
336 SetEventFlag(Uartc.eve, result_v7);
355 *((
volatile acUint16 *)0xB3100000) = 0;
356 event = arg->recv.ub_head;
357 ub_tail = arg->recv.ub_tail;
358 buf = arg->recv.ub_buf;
359 bufsize = arg->recv.ub_size;
360 sr = *((
volatile acUint16 *)0xB241800A) & 0xFF;
361 while ( (sr & 0x1F) != 0 )
363 if ( (sr & 0x10) != 0 )
365 Kprintf(
"acuart:intr: BREAK\n");
371 buf[
event] = *((
volatile acUint16 *)0xB2418000);
373 if (
event >= bufsize )
375 if ( (acUint32)
event == ub_tail && (
int)++ub_tail >= bufsize )
380 sr = *((
volatile acUint16 *)0xB241800A) & 0xFF;
382 p_recv->ub_head =
event;
383 p_recv->ub_tail = ub_tail;
385 if ( (*((
volatile acUint16 *)0xB241800A) & 0x20) != 0 )
387 if ( !uart_xmit(&arg->xmit, (acUartReg)0xB2418000) )
388 *((
volatile acUint16 *)0xB2418002) = *((
volatile acUint16 *)0xB2418002) & 0xFD;
392 iSetEventFlag(arg->eve, event_v9);
396static void uart_attr_set(
struct uart_softc *uartc, acUartReg uartreg)
404 v3 = 36864000 / (
signed int)(16 * uartc->speed);
405 trigger = uartc->fifo;
409 v6 = ((trigger >= 4) << 6 & 0xFFFF);
417 v7 = 16 * (uartc->loopback != 0);
421 *uartreg = (v3 + 1) >> 1;
422 uartreg[1] = (v3 + 1) >> 9;
424 uartreg[4] = v7 | 0xF;
427 uartc->xmit.ub_tail = 0;
428 uartc->xmit.ub_head = uartc->xmit.ub_tail;
429 uartc->recv.ub_tail = 0;
430 uartc->recv.ub_head = uartc->recv.ub_tail;
444 if ( Uartc.eve == 0 )
448 fifo = attr->ua_fifo;
449 Uartc.speed = attr->ua_speed;
450 Uartc.loopback = attr->ua_loopback != 0;
468 Uartc.fifo = fifo_v2;
469 uart_attr_set(&Uartc, (acUartReg)0xB2418000);
479 if ( Uartc.eve == 0 )
483 attr->ua_speed = Uartc.speed;
484 attr->ua_fifo = Uartc.fifo;
485 attr->ua_loopback = Uartc.loopback;
489int acUartModuleStatus()
500static int uart_optarg(
const char *str,
int default_value)
505 result = strtol(str, &next, 0);
507 return default_value;
511int acUartModuleStart(
int argc,
char **argv)
522 if ( acUartModuleStatus() != 0 )
531 if ( !Uartc.xmit.ub_size )
532 Uartc.xmit.ub_size = 256;
534 if ( !Uartc.recv.ub_size )
535 Uartc.recv.ub_size = 512;
537 while ( index < argc )
547 Uartc.speed = uart_optarg(opt + 2, Uartc.speed);
550 Uartc.fifo = uart_optarg(opt + 2, Uartc.fifo);
556 Uartc.recv.ub_size = uart_optarg(opt + 2, Uartc.recv.ub_size);
559 Uartc.xmit.ub_size = uart_optarg(opt + 2, Uartc.xmit.ub_size);
568 Uartc.xmit.ub_buf = 0;
569 uartc = Uartc.recv.ub_size + Uartc.xmit.ub_size;
570 v9 = uart_eve_alloc();
580 buf = (acUint8 *)AllocSysMemory(0, uartc, 0);
588 size = acIntrRegister(AC_INTR_NUM_UART, (acIntrHandler)uart_intr, &Uartc);
591 msg =
"intr_register";
595 size = acIntrEnable(AC_INTR_NUM_UART);
598 acIntrRelease(AC_INTR_NUM_UART);
603 Uartc.xmit.ub_buf = buf;
604 Uartc.recv.ub_buf = &buf[Uartc.xmit.ub_size];
605 uart_attr_set(&Uartc, (acUartReg)0xB2418000);
612 printf(
"acuart:init:%s: error %d\n", msg, size);
617 DeleteEventFlag(eve);
622int acUartModuleStop()
624 if ( !acUartModuleStatus() )
626 *((
volatile acUint16 *)0xB2418002) = 0;
627 *((
volatile acUint16 *)0xB2418004) = 7;
628 acIntrDisable(AC_INTR_NUM_UART);
629 acIntrRelease(AC_INTR_NUM_UART);
630 if ( Uartc.xmit.ub_buf )
631 FreeSysMemory(Uartc.xmit.ub_buf);
634 DeleteEventFlag(Uartc.eve);
639int acUartModuleRestart(
int argc,
char **argv)
int CpuResumeIntr(int state)
int CpuSuspendIntr(int *state)
u32 count
start sector of fragmented bd/file