2 * (C) Copyright 2000 - 2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
23 * Based ont the MPC5200 PSC driver.
24 * Adapted for MPC512x by Jan Wrobel <wrr@semihalf.com>
28 * Minimal serial functions needed to use one of the PSC ports
29 * as serial console interface.
33 #include <linux/compiler.h>
35 #include <asm/processor.h>
38 DECLARE_GLOBAL_DATA_PTR
;
40 #if defined(CONFIG_PSC_CONSOLE)
42 static void fifo_init (volatile psc512x_t
*psc
)
44 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
47 /* reset Rx & Tx fifo slice */
48 out_be32(&psc
->rfcmd
, PSC_FIFO_RESET_SLICE
);
49 out_be32(&psc
->tfcmd
, PSC_FIFO_RESET_SLICE
);
51 /* disable Tx & Rx FIFO interrupts */
52 out_be32(&psc
->rfintmask
, 0);
53 out_be32(&psc
->tfintmask
, 0);
55 switch (((u32
)psc
& 0xf00) >> 8) {
57 tfsize
= FIFOC_PSC0_TX_SIZE
| (FIFOC_PSC0_TX_ADDR
<< 16);
58 rfsize
= FIFOC_PSC0_RX_SIZE
| (FIFOC_PSC0_RX_ADDR
<< 16);
61 tfsize
= FIFOC_PSC1_TX_SIZE
| (FIFOC_PSC1_TX_ADDR
<< 16);
62 rfsize
= FIFOC_PSC1_RX_SIZE
| (FIFOC_PSC1_RX_ADDR
<< 16);
65 tfsize
= FIFOC_PSC2_TX_SIZE
| (FIFOC_PSC2_TX_ADDR
<< 16);
66 rfsize
= FIFOC_PSC2_RX_SIZE
| (FIFOC_PSC2_RX_ADDR
<< 16);
69 tfsize
= FIFOC_PSC3_TX_SIZE
| (FIFOC_PSC3_TX_ADDR
<< 16);
70 rfsize
= FIFOC_PSC3_RX_SIZE
| (FIFOC_PSC3_RX_ADDR
<< 16);
73 tfsize
= FIFOC_PSC4_TX_SIZE
| (FIFOC_PSC4_TX_ADDR
<< 16);
74 rfsize
= FIFOC_PSC4_RX_SIZE
| (FIFOC_PSC4_RX_ADDR
<< 16);
77 tfsize
= FIFOC_PSC5_TX_SIZE
| (FIFOC_PSC5_TX_ADDR
<< 16);
78 rfsize
= FIFOC_PSC5_RX_SIZE
| (FIFOC_PSC5_RX_ADDR
<< 16);
81 tfsize
= FIFOC_PSC6_TX_SIZE
| (FIFOC_PSC6_TX_ADDR
<< 16);
82 rfsize
= FIFOC_PSC6_RX_SIZE
| (FIFOC_PSC6_RX_ADDR
<< 16);
85 tfsize
= FIFOC_PSC7_TX_SIZE
| (FIFOC_PSC7_TX_ADDR
<< 16);
86 rfsize
= FIFOC_PSC7_RX_SIZE
| (FIFOC_PSC7_RX_ADDR
<< 16);
89 tfsize
= FIFOC_PSC8_TX_SIZE
| (FIFOC_PSC8_TX_ADDR
<< 16);
90 rfsize
= FIFOC_PSC8_RX_SIZE
| (FIFOC_PSC8_RX_ADDR
<< 16);
93 tfsize
= FIFOC_PSC9_TX_SIZE
| (FIFOC_PSC9_TX_ADDR
<< 16);
94 rfsize
= FIFOC_PSC9_RX_SIZE
| (FIFOC_PSC9_RX_ADDR
<< 16);
97 tfsize
= FIFOC_PSC10_TX_SIZE
| (FIFOC_PSC10_TX_ADDR
<< 16);
98 rfsize
= FIFOC_PSC10_RX_SIZE
| (FIFOC_PSC10_RX_ADDR
<< 16);
101 tfsize
= FIFOC_PSC11_TX_SIZE
| (FIFOC_PSC11_TX_ADDR
<< 16);
102 rfsize
= FIFOC_PSC11_RX_SIZE
| (FIFOC_PSC11_RX_ADDR
<< 16);
108 out_be32(&psc
->tfsize
, tfsize
);
109 out_be32(&psc
->rfsize
, rfsize
);
111 /* enable Tx & Rx FIFO slice */
112 out_be32(&psc
->rfcmd
, PSC_FIFO_ENABLE_SLICE
);
113 out_be32(&psc
->tfcmd
, PSC_FIFO_ENABLE_SLICE
);
115 out_be32(&im
->fifoc
.fifoc_cmd
, FIFOC_DISABLE_CLOCK_GATE
);
116 __asm__
volatile ("sync");
119 void serial_setbrg_dev(unsigned int idx
)
121 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
122 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
123 unsigned long baseclk
, div
;
124 unsigned long baudrate
;
128 baudrate
= gd
->baudrate
;
129 if (idx
!= CONFIG_PSC_CONSOLE
) {
130 /* Allows setting baudrate for other serial devices
131 * on PSCx using environment. If not specified, use
132 * the same baudrate as for console.
134 sprintf(buf
, "psc%d_baudrate", idx
);
135 br_env
= getenv(buf
);
137 baudrate
= simple_strtoul(br_env
, NULL
, 10);
139 debug("%s: idx %d, baudrate %ld\n", __func__
, idx
, baudrate
);
142 /* calculate divisor for setting PSC CTUR and CTLR registers */
143 baseclk
= (gd
->arch
.ips_clk
+ 8) / 16;
144 div
= (baseclk
+ (baudrate
/ 2)) / baudrate
;
146 out_8(&psc
->ctur
, (div
>> 8) & 0xff);
147 out_8(&psc
->ctlr
, div
& 0xff); /* set baudrate */
150 int serial_init_dev(unsigned int idx
)
152 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
153 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
156 reg
= in_be32(&im
->clk
.sccr
[0]);
157 out_be32(&im
->clk
.sccr
[0], reg
| CLOCK_SCCR1_PSC_EN(idx
));
161 /* set MR register to point to MR1 */
162 out_8(&psc
->command
, PSC_SEL_MODE_REG_1
);
165 out_8(&psc
->command
, PSC_TX_DISABLE
| PSC_RX_DISABLE
);
167 /* choose the prescaler by 16 for the Tx/Rx clock generation */
168 out_be16(&psc
->psc_clock_select
, 0xdd00);
170 /* switch to UART mode */
171 out_be32(&psc
->sicr
, 0);
173 /* mode register points to mr1 */
174 /* configure parity, bit length and so on in mode register 1*/
175 out_8(&psc
->mode
, PSC_MODE_8_BITS
| PSC_MODE_PARNONE
);
176 /* now, mode register points to mr2 */
177 out_8(&psc
->mode
, PSC_MODE_1_STOPBIT
);
180 serial_setbrg_dev(idx
);
182 /* disable all interrupts */
183 out_be16(&psc
->psc_imr
, 0);
185 /* reset and enable Rx/Tx */
186 out_8(&psc
->command
, PSC_RST_RX
);
187 out_8(&psc
->command
, PSC_RST_TX
);
188 out_8(&psc
->command
, PSC_RX_ENABLE
| PSC_TX_ENABLE
);
193 int serial_uninit_dev(unsigned int idx
)
195 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
196 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
199 out_8(&psc
->command
, PSC_RX_DISABLE
| PSC_TX_DISABLE
);
200 reg
= in_be32(&im
->clk
.sccr
[0]);
201 reg
&= ~CLOCK_SCCR1_PSC_EN(idx
);
202 out_be32(&im
->clk
.sccr
[0], reg
);
207 void serial_putc_dev(unsigned int idx
, const char c
)
209 volatile immap_t
*im
= (immap_t
*)CONFIG_SYS_IMMR
;
210 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
213 serial_putc_dev(idx
, '\r');
215 /* Wait for last character to go. */
216 while (!(in_be16(&psc
->psc_status
) & PSC_SR_TXEMP
))
219 out_8(&psc
->tfdata_8
, c
);
222 void serial_putc_raw_dev(unsigned int idx
, const char c
)
224 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
225 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
227 /* Wait for last character to go. */
228 while (!(in_be16(&psc
->psc_status
) & PSC_SR_TXEMP
))
231 out_8(&psc
->tfdata_8
, c
);
234 void serial_puts_dev(unsigned int idx
, const char *s
)
237 serial_putc_dev(idx
, *s
++);
240 int serial_getc_dev(unsigned int idx
)
242 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
243 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
245 /* Wait for a character to arrive. */
246 while (in_be32(&psc
->rfstat
) & PSC_FIFO_EMPTY
)
249 return in_8(&psc
->rfdata_8
);
252 int serial_tstc_dev(unsigned int idx
)
254 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
255 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
257 return !(in_be32(&psc
->rfstat
) & PSC_FIFO_EMPTY
);
260 void serial_setrts_dev(unsigned int idx
, int s
)
262 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
263 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
266 /* Assert RTS (become LOW) */
267 out_8(&psc
->op1
, 0x1);
270 /* Negate RTS (become HIGH) */
271 out_8(&psc
->op0
, 0x1);
275 int serial_getcts_dev(unsigned int idx
)
277 volatile immap_t
*im
= (immap_t
*) CONFIG_SYS_IMMR
;
278 volatile psc512x_t
*psc
= (psc512x_t
*) &im
->psc
[idx
];
280 return (in_8(&psc
->ip
) & 0x1) ? 0 : 1;
282 #endif /* CONFIG_PSC_CONSOLE */
284 #define DECLARE_PSC_SERIAL_FUNCTIONS(port) \
285 int serial##port##_init(void) \
287 return serial_init_dev(port); \
289 int serial##port##_uninit(void) \
291 return serial_uninit_dev(port); \
293 void serial##port##_setbrg(void) \
295 serial_setbrg_dev(port); \
297 int serial##port##_getc(void) \
299 return serial_getc_dev(port); \
301 int serial##port##_tstc(void) \
303 return serial_tstc_dev(port); \
305 void serial##port##_putc(const char c) \
307 serial_putc_dev(port, c); \
309 void serial##port##_puts(const char *s) \
311 serial_puts_dev(port, s); \
314 #define INIT_PSC_SERIAL_STRUCTURE(port, __name) { \
316 .start = serial##port##_init, \
317 .stop = serial##port##_uninit, \
318 .setbrg = serial##port##_setbrg, \
319 .getc = serial##port##_getc, \
320 .tstc = serial##port##_tstc, \
321 .putc = serial##port##_putc, \
322 .puts = serial##port##_puts, \
325 #if defined(CONFIG_SYS_PSC1)
326 DECLARE_PSC_SERIAL_FUNCTIONS(1);
327 struct serial_device serial1_device
=
328 INIT_PSC_SERIAL_STRUCTURE(1, "psc1");
331 #if defined(CONFIG_SYS_PSC3)
332 DECLARE_PSC_SERIAL_FUNCTIONS(3);
333 struct serial_device serial3_device
=
334 INIT_PSC_SERIAL_STRUCTURE(3, "psc3");
337 #if defined(CONFIG_SYS_PSC4)
338 DECLARE_PSC_SERIAL_FUNCTIONS(4);
339 struct serial_device serial4_device
=
340 INIT_PSC_SERIAL_STRUCTURE(4, "psc4");
343 #if defined(CONFIG_SYS_PSC6)
344 DECLARE_PSC_SERIAL_FUNCTIONS(6);
345 struct serial_device serial6_device
=
346 INIT_PSC_SERIAL_STRUCTURE(6, "psc6");
349 __weak
struct serial_device
*default_serial_console(void)
351 #if (CONFIG_PSC_CONSOLE == 3)
352 return &serial3_device
;
353 #elif (CONFIG_PSC_CONSOLE == 6)
354 return &serial6_device
;
356 #error "invalid CONFIG_PSC_CONSOLE"
360 void mpc512x_serial_initialize(void)
362 #if defined(CONFIG_SYS_PSC1)
363 serial_register(&serial1_device
);
365 #if defined(CONFIG_SYS_PSC3)
366 serial_register(&serial3_device
);
368 #if defined(CONFIG_SYS_PSC4)
369 serial_register(&serial4_device
);
371 #if defined(CONFIG_SYS_PSC6)
372 serial_register(&serial6_device
);
376 #include <stdio_dev.h>
378 * Routines for communication with serial devices over PSC
380 /* Bitfield for initialized PSCs */
381 static unsigned int initialized
;
383 struct stdio_dev
*open_port(int num
, int baudrate
)
385 struct stdio_dev
*port
;
390 if (num
< 0 || num
> 11)
393 sprintf(name
, "psc%d", num
);
394 port
= stdio_get_by_name(name
);
398 if (!test_bit(num
, &initialized
)) {
399 sprintf(env_var
, "psc%d_baudrate", num
);
400 sprintf(env_val
, "%d", baudrate
);
401 setenv(env_var
, env_val
);
406 set_bit(num
, &initialized
);
412 int close_port(int num
)
414 struct stdio_dev
*port
;
418 if (num
< 0 || num
> 11)
421 sprintf(name
, "psc%d", num
);
422 port
= stdio_get_by_name(name
);
427 clear_bit(num
, &initialized
);
432 int write_port(struct stdio_dev
*port
, char *buf
)
442 int read_port(struct stdio_dev
*port
, char *buf
, int size
)
452 while (port
->tstc()) {
453 buf
[cnt
++] = port
->getc();