3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Infineon Technologies AG, http://www.infineon.com
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/addrspace.h>
33 #define SET_BIT(reg, mask) asc_writel(reg, asc_readl(reg) | (mask))
34 #define CLEAR_BIT(reg, mask) asc_writel(reg, asc_readl(reg) & (~mask))
35 #define SET_BITFIELD(reg, mask, off, val) asc_writel(reg, (asc_readl(reg) & (~mask)) | (val << off) )
39 #define DEBUG_ASC_RAW_RX_BUF 0xA0800000
40 #define DEBUG_ASC_RAW_TX_BUF 0xA0900000
43 DECLARE_GLOBAL_DATA_PTR;
45 static IfxAsc_t *pAsc = (IfxAsc_t *)CKSEG1ADDR(CONFIG_SYS_IFX_ASC_BASE);
49 * BaudRate = ----- * --------------------
50 * 512 16 * (ReloadValue+1)
55 * ReloadValue = ( ----- * --------------- ) - 1
58 static void serial_divs(u32 baudrate, u32 fasc, u32 *pfdv, u32 *preload)
60 u32 clock = fasc / 16;
62 u32 fdv; /* best fdv */
63 u32 reload = 0; /* best reload */
64 u32 diff; /* smallest diff */
65 u32 idiff; /* current diff */
66 u32 ireload; /* current reload */
67 u32 i; /* current fdv */
68 u32 result; /* current resulting baudrate */
75 fdv = 512; /* start with 1:1 fraction */
76 diff = baudrate; /* highest possible */
78 /* i is the test fdv value -- start with the largest possible */
79 for (i = 512; i > 0; i--)
81 ireload = (clock * i) / baudrate;
83 break; /* already invalid */
84 result = (clock * i) / ireload;
86 idiff = (result > baudrate) ? (result - baudrate) : (baudrate - result);
91 break; /* can't do better */
93 else if (idiff < diff)
95 fdv = i; /* best so far */
97 diff = idiff; /* update lowest diff*/
101 *pfdv = (fdv == 512) ? 0 : fdv;
102 *preload = reload - 1;
106 void serial_setbrg (void)
108 u32 ReloadValue, fdv;
110 serial_divs(gd->baudrate, get_bus_freq(0), &fdv, &ReloadValue);
112 /* Disable Baud Rate Generator; BG should only be written when R=0 */
113 CLEAR_BIT(asc_con, ASCCON_R);
115 /* Enable Fractional Divider */
116 SET_BIT(asc_con, ASCCON_FDE); /* FDE = 1 */
118 /* Set fractional divider value */
119 asc_writel(asc_fdv, fdv & ASCFDV_VALUE_MASK);
121 /* Set reload value in BG */
122 asc_writel(asc_bg, ReloadValue);
124 /* Enable Baud Rate Generator */
125 SET_BIT(asc_con, ASCCON_R); /* R = 1 */
129 int serial_init (void)
132 /* and we have to set CLC register*/
133 CLEAR_BIT(asc_clc, ASCCLC_DISS);
134 SET_BITFIELD(asc_clc, ASCCLC_RMCMASK, ASCCLC_RMCOFFSET, 0x0001);
136 /* initialy we are in async mode */
137 asc_writel(asc_con, ASCCON_M_8ASYNC);
139 /* select input port */
140 asc_writel(asc_pisel, CONSOLE_TTY & 0x1);
142 /* TXFIFO's filling level */
143 SET_BITFIELD(asc_txfcon, ASCTXFCON_TXFITLMASK,
144 ASCTXFCON_TXFITLOFF, ASC_TXFIFO_FL);
146 SET_BIT(asc_txfcon, ASCTXFCON_TXFEN);
148 /* RXFIFO's filling level */
149 SET_BITFIELD(asc_txfcon, ASCRXFCON_RXFITLMASK,
150 ASCRXFCON_RXFITLOFF, ASC_RXFIFO_FL);
152 SET_BIT(asc_rxfcon, ASCRXFCON_RXFEN);
157 /* enable error signals & Receiver enable */
158 SET_BIT(asc_whbstate, ASCWHBSTATE_SETREN|ASCCON_FEN|ASCCON_TOEN|ASCCON_ROEN);
164 void serial_putc (const char c)
168 static u8 * debug = (u8 *) DEBUG_ASC_RAW_TX_BUF;
173 /* check do we have a free space in the TX FIFO */
174 /* get current filling level */
176 txFl = ( asc_readl(asc_fstat) & ASCFSTAT_TXFFLMASK ) >> ASCFSTAT_TXFFLOFF;
178 while ( txFl == ASC_TXFIFO_FULL );
180 asc_writel(asc_tbuf, c); /* write char to Transmit Buffer Register */
182 /* check for errors */
183 if ( asc_readl(asc_state) & ASCSTATE_TOE ) {
184 SET_BIT(asc_whbstate, ASCWHBSTATE_CLRTOE);
189 void serial_puts (const char *s)
196 int serial_getc (void)
199 while ((asc_readl(asc_fstat) & ASCFSTAT_RXFFLMASK) == 0 );
200 c = (char)(asc_readl(asc_rbuf) & 0xff);
203 static u8* debug=(u8*)(DEBUG_ASC_RAW_RX_BUF);
210 int serial_tstc (void)
214 if ( (asc_readl(asc_fstat) & ASCFSTAT_RXFFLMASK) == 0 ) {