1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Alan Korr & Nick Robinson
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
33 #if CONFIG_CPU == IMX31L
34 #include "serial-imx31.h"
37 #if CONFIG_CPU == SH7034
39 /* FIX: this doesn't work on iRiver or iPod yet */
40 /* iFP7xx has no remote */
42 /* Received byte identifiers */
50 void serial_setup (void)
52 /* Set PB10 function to serial Rx */
53 PBCR1
= (PBCR1
& 0xffcf) | 0x0020;
57 BRR1
= (FREQ
/(32*9600))-1;
58 and_b(0, &SSR1
); /* The status bits must be read before they are cleared,
59 so we do an AND operation */
61 /* Let the hardware settle. The serial port needs to wait "at least
62 the interval required to transmit or receive one bit" before it
66 SCR1
= 0x10; /* Enable the receiver, no interrupt */
83 void tx_writec(unsigned char c
)
89 unsigned char rx_readc(void)
92 /* Read byte and clear the Rx Full bit */
94 and_b(~SCI_RDRF
, &SSR1
);
99 /* This function returns the received remote control code only if it is
100 received without errors before or after the reception.
101 It therefore returns the received code on the second call after the
102 code has been received. */
103 int remote_control_rx(void)
105 static int last_valid_button
= BUTTON_NONE
;
106 static int last_was_error
= false;
108 int ret
= BUTTON_NONE
;
110 /* Errors? Just clear'em. The receiver stops if we don't */
111 if(SSR1
& (SCI_ORER
| SCI_FER
| SCI_PER
)) {
112 and_b(~(SCI_ORER
| SCI_FER
| SCI_PER
), &SSR1
);
113 last_valid_button
= BUTTON_NONE
;
114 last_was_error
= true;
123 last_valid_button
= BUTTON_NONE
;
131 last_valid_button
= BUTTON_RC_STOP
;
135 last_valid_button
= BUTTON_RC_PLAY
;
139 last_valid_button
= BUTTON_RC_VOL_UP
;
143 last_valid_button
= BUTTON_RC_VOL_DOWN
;
147 last_valid_button
= BUTTON_RC_LEFT
;
151 last_valid_button
= BUTTON_RC_RIGHT
;
155 last_valid_button
= BUTTON_NONE
;
162 /* This means that a valid remote control character was received
163 the last time we were called, with no receiver errors either before
164 or after. Then we can assume that there really is a remote control
165 attached, and return the button code. */
166 ret
= last_valid_button
;
167 last_valid_button
= BUTTON_NONE
;
170 last_was_error
= false;
175 #elif defined(CPU_COLDFIRE)
177 void serial_setup (void)
179 UCR0
= 0x30; /* Reset transmitter */
180 UCSR0
= 0xdd; /* Timer mode */
182 UCR0
= 0x10; /* Reset pointer */
183 UMR0
= 0x13; /* No parity, 8 bits */
184 UMR0
= 0x07; /* 1 stop bit */
186 UCR0
= 0x04; /* Tx enable */
203 void tx_writec(unsigned char c
)
208 #elif (CONFIG_CPU == IMX31L)
210 void serial_setup(void)
212 #ifdef UART_INT /*enable UART Interrupts */
213 UCR1_1
|= (EUARTUCR1_TRDYEN
| EUARTUCR1_RRDYEN
| EUARTUCR1_TXMPTYEN
);
214 UCR4_1
|= (EUARTUCR4_TCEN
);
215 #else /*disable UART Interrupts*/
216 UCR1_1
&= ~(EUARTUCR1_TRDYEN
| EUARTUCR1_RRDYEN
| EUARTUCR1_TXMPTYEN
);
217 UCR4_1
&= ~(EUARTUCR4_TCEN
);
219 UCR1_1
|= EUARTUCR1_UARTEN
;
220 UCR2_1
|= (EUARTUCR2_TXEN
| EUARTUCR2_RXEN
| EUARTUCR2_IRTS
);
222 /* Tx,Rx Interrupt Trigger levels, Disable for now*/
223 /*UFCR1 |= (UFCR1_TXTL_32 | UFCR1_RXTL_32);*/
228 if((UTS1
& EUARTUTS_TXEMPTY
))
234 /*Not ready...After first Rx, UTS1 & UTS1_RXEMPTY
235 keeps returning true*/
238 if(!(UTS1
& EUARTUTS_RXEMPTY
))
244 void tx_writec(unsigned char c
)
249 #elif defined(IPOD_ACCESSORY_PROTOCOL)
250 static int autobaud
= 0;
251 void serial_setup (void)
255 #if (MODEL_NUMBER == 3) || (MODEL_NUMBER == 8)
257 /* Route the Tx/Rx pins. 4G Ipod??? */
258 outl(0x70000018, inl(0x70000018) & ~0xc00);
259 #elif (MODEL_NUMBER == 4) || (MODEL_NUMBER == 5)
261 /* Route the Tx/Rx pins. 5G Ipod */
262 (*(volatile unsigned long *)(0x7000008C)) &= ~0x0C;
263 GPO32_ENABLE
&= ~0x0C;
266 DEV_EN
= DEV_EN
| DEV_SER0
;
267 CPU_HI_INT_DIS
= SER0_MASK
;
273 SER0_LCR
= 0x80; /* Divisor latch enable */
275 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
278 SER0_FCR
= 0x07; /* Tx+Rx FIFO reset and FIFO enable */
280 CPU_INT_EN
|= HI_MASK
;
281 CPU_HI_INT_EN
|= SER0_MASK
;
287 void serial_bitrate(int rate
)
292 SER0_LCR
= 0x80; /* Divisor latch enable */
293 SER0_DLL
= 0x0D; /* 24000000/13/16 = 115384 baud */
294 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
299 SER0_LCR
= 0x80; /* Divisor latch enable */
300 SER0_DLL
= 24000000L / rate
/ 16;
301 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
306 if((SER0_LSR
& 0x20))
320 void tx_writec(unsigned char c
)
325 unsigned char rx_readc(void)
327 return (SER0_RBR
& 0xFF);
332 static int badbaud
= 0;
333 static bool newpkt
= true;
339 if (newpkt
&& autobaud
> 0)
349 SER0_LCR
= 0x80; /* Divisor latch enable */
350 SER0_DLL
= 0x4E; /* 24000000/78/16 = 19230 baud */
351 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
355 SER0_LCR
= 0x80; /* Divisor latch enable */
356 SER0_DLL
= 0x9C; /* 24000000/156/16 = 9615 baud */
357 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
362 if (badbaud
>= 6) /* Switch baud detection mode */
365 SER0_LCR
= 0x80; /* Divisor latch enable */
366 SER0_DLL
= 0x0D; /* 24000000/13/16 = 115384 baud */
367 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
370 SER0_LCR
= 0x80; /* Divisor latch enable */
371 SER0_DLL
= 0x1A; /* 24000000/26/16 = 57692 baud */
372 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
383 SER0_LCR
= 0x80; /* Divisor latch enable */
384 SER0_DLL
= 0x1A; /* 24000000/26/16 = 57692 baud */
385 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
389 SER0_LCR
= 0x80; /* Divisor latch enable */
390 SER0_DLL
= 0x27; /* 24000000/39/16 = 38461 baud */
391 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
395 SER0_LCR
= 0x80; /* Divisor latch enable */
396 SER0_DLL
= 0x4E; /* 24000000/78/16 = 19230 baud */
397 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
402 if (badbaud
>= 6) /* Switch baud detection */
405 SER0_LCR
= 0x80; /* Divisor latch enable */
406 SER0_DLL
= 0x1A; /* 24000000/26/16 = 57692 baud */
407 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
410 SER0_LCR
= 0x80; /* Divisor latch enable */
411 SER0_DLL
= 0x0D; /* 24000000/13/16 = 115384 baud */
412 SER0_LCR
= 0x03; /* Divisor latch disable, 8-N-1 */
418 bool pkt
= iap_getc(temp
);
420 autobaud
= 0; /* Found good baud */
427 void dprintf(const char * str
, ... )
429 char dprintfbuff
[256];
436 vsnprintf(ptr
,sizeof(dprintfbuff
),str
,ap
);
439 serial_tx((unsigned char *)ptr
);
442 void serial_tx(const unsigned char * buf
)