blob: bc10c501aac2c53f7e736cae1168decc31b69eb3 [file] [log] [blame]
Rafal Jaworowski8993e542007-07-27 14:43:59 +02001/*
Detlev Zundel57ae8a52010-01-21 17:55:58 +01002 * (C) Copyright 2000 - 2010
Rafal Jaworowski8993e542007-07-27 14:43:59 +02003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 * Based ont the MPC5200 PSC driver.
24 * Adapted for MPC512x by Jan Wrobel <wrr@semihalf.com>
25 */
26
27/*
28 * Minimal serial functions needed to use one of the PSC ports
29 * as serial console interface.
30 */
31
32#include <common.h>
Mike Frysinger6c768ca2011-04-29 18:03:29 +000033#include <linux/compiler.h>
Wolfgang Denk843efb12009-05-16 10:47:43 +020034#include <asm/io.h>
35#include <asm/processor.h>
Anatolij Gustschine3b28e62010-04-24 19:27:05 +020036#include <serial.h>
Rafal Jaworowski8993e542007-07-27 14:43:59 +020037
38DECLARE_GLOBAL_DATA_PTR;
39
Anatolij Gustschine3b28e62010-04-24 19:27:05 +020040#if defined(CONFIG_PSC_CONSOLE) || defined(CONFIG_SERIAL_MULTI)
Rafal Jaworowski8993e542007-07-27 14:43:59 +020041
42static void fifo_init (volatile psc512x_t *psc)
43{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020044 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +020045 u32 tfsize, rfsize;
Rafal Jaworowski8993e542007-07-27 14:43:59 +020046
47 /* reset Rx & Tx fifo slice */
Wolfgang Denk843efb12009-05-16 10:47:43 +020048 out_be32(&psc->rfcmd, PSC_FIFO_RESET_SLICE);
49 out_be32(&psc->tfcmd, PSC_FIFO_RESET_SLICE);
Rafal Jaworowski8993e542007-07-27 14:43:59 +020050
51 /* disable Tx & Rx FIFO interrupts */
Wolfgang Denk843efb12009-05-16 10:47:43 +020052 out_be32(&psc->rfintmask, 0);
53 out_be32(&psc->tfintmask, 0);
Rafal Jaworowski8993e542007-07-27 14:43:59 +020054
Anatolij Gustschine3b28e62010-04-24 19:27:05 +020055#if defined(CONFIG_SERIAL_MULTI)
56 switch (((u32)psc & 0xf00) >> 8) {
57 case 0:
58 tfsize = FIFOC_PSC0_TX_SIZE | (FIFOC_PSC0_TX_ADDR << 16);
59 rfsize = FIFOC_PSC0_RX_SIZE | (FIFOC_PSC0_RX_ADDR << 16);
60 break;
61 case 1:
62 tfsize = FIFOC_PSC1_TX_SIZE | (FIFOC_PSC1_TX_ADDR << 16);
63 rfsize = FIFOC_PSC1_RX_SIZE | (FIFOC_PSC1_RX_ADDR << 16);
64 break;
65 case 2:
66 tfsize = FIFOC_PSC2_TX_SIZE | (FIFOC_PSC2_TX_ADDR << 16);
67 rfsize = FIFOC_PSC2_RX_SIZE | (FIFOC_PSC2_RX_ADDR << 16);
68 break;
69 case 3:
70 tfsize = FIFOC_PSC3_TX_SIZE | (FIFOC_PSC3_TX_ADDR << 16);
71 rfsize = FIFOC_PSC3_RX_SIZE | (FIFOC_PSC3_RX_ADDR << 16);
72 break;
73 case 4:
74 tfsize = FIFOC_PSC4_TX_SIZE | (FIFOC_PSC4_TX_ADDR << 16);
75 rfsize = FIFOC_PSC4_RX_SIZE | (FIFOC_PSC4_RX_ADDR << 16);
76 break;
77 case 5:
78 tfsize = FIFOC_PSC5_TX_SIZE | (FIFOC_PSC5_TX_ADDR << 16);
79 rfsize = FIFOC_PSC5_RX_SIZE | (FIFOC_PSC5_RX_ADDR << 16);
80 break;
81 case 6:
82 tfsize = FIFOC_PSC6_TX_SIZE | (FIFOC_PSC6_TX_ADDR << 16);
83 rfsize = FIFOC_PSC6_RX_SIZE | (FIFOC_PSC6_RX_ADDR << 16);
84 break;
85 case 7:
86 tfsize = FIFOC_PSC7_TX_SIZE | (FIFOC_PSC7_TX_ADDR << 16);
87 rfsize = FIFOC_PSC7_RX_SIZE | (FIFOC_PSC7_RX_ADDR << 16);
88 break;
89 case 8:
90 tfsize = FIFOC_PSC8_TX_SIZE | (FIFOC_PSC8_TX_ADDR << 16);
91 rfsize = FIFOC_PSC8_RX_SIZE | (FIFOC_PSC8_RX_ADDR << 16);
92 break;
93 case 9:
94 tfsize = FIFOC_PSC9_TX_SIZE | (FIFOC_PSC9_TX_ADDR << 16);
95 rfsize = FIFOC_PSC9_RX_SIZE | (FIFOC_PSC9_RX_ADDR << 16);
96 break;
97 case 10:
98 tfsize = FIFOC_PSC10_TX_SIZE | (FIFOC_PSC10_TX_ADDR << 16);
99 rfsize = FIFOC_PSC10_RX_SIZE | (FIFOC_PSC10_RX_ADDR << 16);
100 break;
101 case 11:
102 tfsize = FIFOC_PSC11_TX_SIZE | (FIFOC_PSC11_TX_ADDR << 16);
103 rfsize = FIFOC_PSC11_RX_SIZE | (FIFOC_PSC11_RX_ADDR << 16);
104 break;
105 default:
106 return;
107 }
108#else
109 tfsize = CONSOLE_FIFO_TX_SIZE | (CONSOLE_FIFO_TX_ADDR << 16);
110 rfsize = CONSOLE_FIFO_RX_SIZE | (CONSOLE_FIFO_RX_ADDR << 16);
111#endif
112 out_be32(&psc->tfsize, tfsize);
113 out_be32(&psc->rfsize, rfsize);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200114
115 /* enable Tx & Rx FIFO slice */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200116 out_be32(&psc->rfcmd, PSC_FIFO_ENABLE_SLICE);
117 out_be32(&psc->tfcmd, PSC_FIFO_ENABLE_SLICE);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200118
Wolfgang Denk843efb12009-05-16 10:47:43 +0200119 out_be32(&im->fifoc.fifoc_cmd, FIFOC_DISABLE_CLOCK_GATE);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200120 __asm__ volatile ("sync");
121}
122
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200123void serial_setbrg_dev(unsigned int idx)
Stefan Roeseba4feae2009-06-02 16:53:16 +0200124{
125 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200126 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Stefan Roeseba4feae2009-06-02 16:53:16 +0200127 unsigned long baseclk, div;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200128 unsigned long baudrate;
129 char buf[16];
130 char *br_env;
Stefan Roeseba4feae2009-06-02 16:53:16 +0200131
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200132 baudrate = gd->baudrate;
133 if (idx != CONFIG_PSC_CONSOLE) {
134 /* Allows setting baudrate for other serial devices
135 * on PSCx using environment. If not specified, use
136 * the same baudrate as for console.
137 */
138 sprintf(buf, "psc%d_baudrate", idx);
139 br_env = getenv(buf);
140 if (br_env)
141 baudrate = simple_strtoul(br_env, NULL, 10);
142
Marek Vasut506b9f22011-10-21 14:17:02 +0000143 debug("%s: idx %d, baudrate %ld\n", __func__, idx, baudrate);
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200144 }
145
146 /* calculate divisor for setting PSC CTUR and CTLR registers */
Stefan Roeseba4feae2009-06-02 16:53:16 +0200147 baseclk = (gd->ips_clk + 8) / 16;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200148 div = (baseclk + (baudrate / 2)) / baudrate;
Stefan Roeseba4feae2009-06-02 16:53:16 +0200149
150 out_8(&psc->ctur, (div >> 8) & 0xff);
151 out_8(&psc->ctlr, div & 0xff); /* set baudrate */
152}
153
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200154int serial_init_dev(unsigned int idx)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200155{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200156 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200157 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
158#if defined(CONFIG_SERIAL_MULTI)
159 u32 reg;
160
161 reg = in_be32(&im->clk.sccr[0]);
162 out_be32(&im->clk.sccr[0], reg | CLOCK_SCCR1_PSC_EN(idx));
163#endif
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200164
165 fifo_init (psc);
166
167 /* set MR register to point to MR1 */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200168 out_8(&psc->command, PSC_SEL_MODE_REG_1);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200169
170 /* disable Tx/Rx */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200171 out_8(&psc->command, PSC_TX_DISABLE | PSC_RX_DISABLE);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200172
173 /* choose the prescaler by 16 for the Tx/Rx clock generation */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200174 out_be16(&psc->psc_clock_select, 0xdd00);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200175
176 /* switch to UART mode */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200177 out_be32(&psc->sicr, 0);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200178
179 /* mode register points to mr1 */
180 /* configure parity, bit length and so on in mode register 1*/
Wolfgang Denk843efb12009-05-16 10:47:43 +0200181 out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200182 /* now, mode register points to mr2 */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200183 out_8(&psc->mode, PSC_MODE_1_STOPBIT);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200184
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200185 /* set baudrate */
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200186 serial_setbrg_dev(idx);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200187
188 /* disable all interrupts */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200189 out_be16(&psc->psc_imr, 0);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200190
191 /* reset and enable Rx/Tx */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200192 out_8(&psc->command, PSC_RST_RX);
193 out_8(&psc->command, PSC_RST_TX);
194 out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200195
196 return 0;
197}
198
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200199int serial_uninit_dev(unsigned int idx)
200{
201 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
202 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
203 u32 reg;
204
205 out_8(&psc->command, PSC_RX_DISABLE | PSC_TX_DISABLE);
206 reg = in_be32(&im->clk.sccr[0]);
207 reg &= ~CLOCK_SCCR1_PSC_EN(idx);
208 out_be32(&im->clk.sccr[0], reg);
209
210 return 0;
211}
212
213void serial_putc_dev(unsigned int idx, const char c)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200214{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200215 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200216 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200217
218 if (c == '\n')
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200219 serial_putc_dev(idx, '\r');
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200220
221 /* Wait for last character to go. */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200222 while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP))
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200223 ;
224
Detlev Zundel57ae8a52010-01-21 17:55:58 +0100225 out_8(&psc->tfdata_8, c);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200226}
227
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200228void serial_putc_raw_dev(unsigned int idx, const char c)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200229{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200230 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200231 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200232
233 /* Wait for last character to go. */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200234 while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP))
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200235 ;
236
Detlev Zundel57ae8a52010-01-21 17:55:58 +0100237 out_8(&psc->tfdata_8, c);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200238}
239
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200240void serial_puts_dev(unsigned int idx, const char *s)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200241{
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200242 while (*s)
243 serial_putc_dev(idx, *s++);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200244}
245
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200246int serial_getc_dev(unsigned int idx)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200247{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200248 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200249 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200250
251 /* Wait for a character to arrive. */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200252 while (in_be32(&psc->rfstat) & PSC_FIFO_EMPTY)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200253 ;
254
Detlev Zundel57ae8a52010-01-21 17:55:58 +0100255 return in_8(&psc->rfdata_8);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200256}
257
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200258int serial_tstc_dev(unsigned int idx)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200259{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200260 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200261 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200262
Wolfgang Denk843efb12009-05-16 10:47:43 +0200263 return !(in_be32(&psc->rfstat) & PSC_FIFO_EMPTY);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200264}
265
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200266void serial_setrts_dev(unsigned int idx, int s)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200267{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200268 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200269 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200270
271 if (s) {
272 /* Assert RTS (become LOW) */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200273 out_8(&psc->op1, 0x1);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200274 }
275 else {
276 /* Negate RTS (become HIGH) */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200277 out_8(&psc->op0, 0x1);
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200278 }
279}
280
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200281int serial_getcts_dev(unsigned int idx)
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200282{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200283 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200284 volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200285
Wolfgang Denk843efb12009-05-16 10:47:43 +0200286 return (in_8(&psc->ip) & 0x1) ? 0 : 1;
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200287}
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200288#endif /* CONFIG_PSC_CONSOLE || CONFIG_SERIAL_MULTI */
289
290#if defined(CONFIG_SERIAL_MULTI)
291
292#define DECLARE_PSC_SERIAL_FUNCTIONS(port) \
293 int serial##port##_init(void) \
294 { \
295 return serial_init_dev(port); \
296 } \
297 int serial##port##_uninit(void) \
298 { \
299 return serial_uninit_dev(port); \
300 } \
301 void serial##port##_setbrg(void) \
302 { \
303 serial_setbrg_dev(port); \
304 } \
305 int serial##port##_getc(void) \
306 { \
307 return serial_getc_dev(port); \
308 } \
309 int serial##port##_tstc(void) \
310 { \
311 return serial_tstc_dev(port); \
312 } \
313 void serial##port##_putc(const char c) \
314 { \
315 serial_putc_dev(port, c); \
316 } \
317 void serial##port##_puts(const char *s) \
318 { \
319 serial_puts_dev(port, s); \
320 }
321
Marek Vasut90bad892012-09-09 18:48:28 +0200322#define INIT_PSC_SERIAL_STRUCTURE(port, __name) { \
323 .name = __name, \
324 .start = serial##port##_init, \
325 .stop = serial##port##_uninit, \
326 .setbrg = serial##port##_setbrg, \
327 .getc = serial##port##_getc, \
328 .tstc = serial##port##_tstc, \
329 .putc = serial##port##_putc, \
330 .puts = serial##port##_puts, \
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200331}
332
333#if defined(CONFIG_SYS_PSC1)
334DECLARE_PSC_SERIAL_FUNCTIONS(1);
335struct serial_device serial1_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000336INIT_PSC_SERIAL_STRUCTURE(1, "psc1");
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200337#endif
338
339#if defined(CONFIG_SYS_PSC3)
340DECLARE_PSC_SERIAL_FUNCTIONS(3);
341struct serial_device serial3_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000342INIT_PSC_SERIAL_STRUCTURE(3, "psc3");
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200343#endif
344
345#if defined(CONFIG_SYS_PSC4)
346DECLARE_PSC_SERIAL_FUNCTIONS(4);
347struct serial_device serial4_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000348INIT_PSC_SERIAL_STRUCTURE(4, "psc4");
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200349#endif
350
351#if defined(CONFIG_SYS_PSC6)
352DECLARE_PSC_SERIAL_FUNCTIONS(6);
353struct serial_device serial6_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000354INIT_PSC_SERIAL_STRUCTURE(6, "psc6");
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200355#endif
356
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000357__weak struct serial_device *default_serial_console(void)
358{
359#if (CONFIG_PSC_CONSOLE == 3)
360 return &serial3_device;
361#elif (CONFIG_PSC_CONSOLE == 6)
362 return &serial6_device;
363#else
364#error "invalid CONFIG_PSC_CONSOLE"
365#endif
366}
367
Marek Vasut918327c2012-09-12 19:50:18 +0200368void mpc512x_serial_initialize(void)
369{
370#if defined(CONFIG_SYS_PSC1)
371 serial_register(&serial1_device);
372#endif
373#if defined(CONFIG_SYS_PSC3)
374 serial_register(&serial3_device);
375#endif
376#if defined(CONFIG_SYS_PSC4)
377 serial_register(&serial4_device);
378#endif
379#if defined(CONFIG_SYS_PSC6)
380 serial_register(&serial6_device);
381#endif
382}
383
Anatolij Gustschine3b28e62010-04-24 19:27:05 +0200384#else
385
386void serial_setbrg(void)
387{
388 serial_setbrg_dev(CONFIG_PSC_CONSOLE);
389}
390
391int serial_init(void)
392{
393 return serial_init_dev(CONFIG_PSC_CONSOLE);
394}
395
396void serial_putc(const char c)
397{
398 serial_putc_dev(CONFIG_PSC_CONSOLE, c);
399}
400
401void serial_putc_raw(const char c)
402{
403 serial_putc_raw_dev(CONFIG_PSC_CONSOLE, c);
404}
405
406void serial_puts(const char *s)
407{
408 serial_puts_dev(CONFIG_PSC_CONSOLE, s);
409}
410
411int serial_getc(void)
412{
413 return serial_getc_dev(CONFIG_PSC_CONSOLE);
414}
415
416int serial_tstc(void)
417{
418 return serial_tstc_dev(CONFIG_PSC_CONSOLE);
419}
420
421void serial_setrts(int s)
422{
423 return serial_setrts_dev(CONFIG_PSC_CONSOLE, s);
424}
425
426int serial_getcts(void)
427{
428 return serial_getcts_dev(CONFIG_PSC_CONSOLE);
429}
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200430#endif /* CONFIG_PSC_CONSOLE */
Anatolij Gustschin8e234e32010-04-24 19:27:06 +0200431
432#if defined(CONFIG_SERIAL_MULTI)
433#include <stdio_dev.h>
434/*
435 * Routines for communication with serial devices over PSC
436 */
437/* Bitfield for initialized PSCs */
438static unsigned int initialized;
439
440struct stdio_dev *open_port(int num, int baudrate)
441{
442 struct stdio_dev *port;
443 char env_var[16];
444 char env_val[10];
445 char name[7];
446
447 if (num < 0 || num > 11)
448 return NULL;
449
450 sprintf(name, "psc%d", num);
451 port = stdio_get_by_name(name);
452 if (!port)
453 return NULL;
454
455 if (!test_bit(num, &initialized)) {
456 sprintf(env_var, "psc%d_baudrate", num);
457 sprintf(env_val, "%d", baudrate);
458 setenv(env_var, env_val);
459
460 if (port->start())
461 return NULL;
462
463 set_bit(num, &initialized);
464 }
465
466 return port;
467}
468
469int close_port(int num)
470{
471 struct stdio_dev *port;
472 int ret;
473 char name[7];
474
475 if (num < 0 || num > 11)
476 return -1;
477
478 sprintf(name, "psc%d", num);
479 port = stdio_get_by_name(name);
480 if (!port)
481 return -1;
482
483 ret = port->stop();
484 clear_bit(num, &initialized);
485
486 return ret;
487}
488
489int write_port(struct stdio_dev *port, char *buf)
490{
491 if (!port || !buf)
492 return -1;
493
494 port->puts(buf);
495
496 return 0;
497}
498
499int read_port(struct stdio_dev *port, char *buf, int size)
500{
501 int cnt = 0;
502
503 if (!port || !buf)
504 return -1;
505
506 if (!size)
507 return 0;
508
509 while (port->tstc()) {
510 buf[cnt++] = port->getc();
511 if (cnt > size)
512 break;
513 }
514
515 return cnt;
516}
517#endif /* CONFIG_SERIAL_MULTI */