2 * (C) Copyright 2000, 2001, 2002
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 * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00, with
24 * changes based on the file arch/powerpc/mbxboot/m8260_tty.c from the
25 * Linux/PPC sources (m8260_tty.c had no copyright info in it).
29 * Minimal serial functions needed to use one of the SMC ports
30 * as serial console interface.
35 #include <asm/cpm_8260.h>
37 #include <linux/compiler.h>
39 DECLARE_GLOBAL_DATA_PTR;
41 #if defined(CONFIG_CONS_ON_SMC)
43 #if CONFIG_CONS_INDEX == 1 /* Console on SMC1 */
46 #define PROFF_SMC_BASE PROFF_SMC1_BASE
47 #define PROFF_SMC PROFF_SMC1
48 #define CPM_CR_SMC_PAGE CPM_CR_SMC1_PAGE
49 #define CPM_CR_SMC_SBLOCK CPM_CR_SMC1_SBLOCK
50 #define CMXSMR_MASK (CMXSMR_SMC1|CMXSMR_SMC1CS_MSK)
51 #define CMXSMR_VALUE CMXSMR_SMC1CS_BRG7
53 #elif CONFIG_CONS_INDEX == 2 /* Console on SMC2 */
56 #define PROFF_SMC_BASE PROFF_SMC2_BASE
57 #define PROFF_SMC PROFF_SMC2
58 #define CPM_CR_SMC_PAGE CPM_CR_SMC2_PAGE
59 #define CPM_CR_SMC_SBLOCK CPM_CR_SMC2_SBLOCK
60 #define CMXSMR_MASK (CMXSMR_SMC2|CMXSMR_SMC2CS_MSK)
61 #define CMXSMR_VALUE CMXSMR_SMC2CS_BRG8
65 #error "console not correctly defined"
69 #if !defined(CONFIG_SYS_SMC_RXBUFLEN)
70 #define CONFIG_SYS_SMC_RXBUFLEN 1
71 #define CONFIG_SYS_MAXIDLE 0
73 #if !defined(CONFIG_SYS_MAXIDLE)
74 #error "you must define CONFIG_SYS_MAXIDLE"
78 typedef volatile struct serialbuffer {
79 cbd_t rxbd; /* Rx BD */
80 cbd_t txbd; /* Tx BD */
81 uint rxindex; /* index for next character to read */
82 volatile uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
83 volatile uchar txbuf; /* tx buffers */
86 /* map rs_table index to baud rate generator index */
87 static unsigned char brg_map[] = {
88 6, /* BRG7 for SMC1 */
89 7, /* BRG8 for SMC2 */
90 0, /* BRG1 for SCC1 */
91 1, /* BRG1 for SCC2 */
92 2, /* BRG1 for SCC3 */
93 3, /* BRG1 for SCC4 */
96 static int mpc8260_smc_serial_init(void)
98 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
100 volatile smc_uart_t *up;
101 volatile cpm8260_t *cp = &(im->im_cpm);
103 volatile serialbuffer_t *rtx;
105 /* initialize pointers to SMC */
107 sp = (smc_t *) &(im->im_smc[SMC_INDEX]);
108 *(ushort *)(&im->im_dprambase[PROFF_SMC_BASE]) = PROFF_SMC;
109 up = (smc_uart_t *)&im->im_dprambase[PROFF_SMC];
111 /* Disable transmitter/receiver. */
112 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
114 /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
116 /* Allocate space for two buffer descriptors in the DP ram.
117 * damm: allocating space after the two buffers for rx/tx data
120 /* allocate size of struct serialbuffer with bd rx/tx,
121 * buffer rx/tx and rx index
123 dpaddr = m8260_cpm_dpalloc((sizeof(serialbuffer_t)), 16);
125 rtx = (serialbuffer_t *)&im->im_dprambase[dpaddr];
127 /* Set the physical address of the host memory buffers in
128 * the buffer descriptors.
130 rtx->rxbd.cbd_bufaddr = (uint) &rtx->rxbuf;
131 rtx->rxbd.cbd_sc = 0;
133 rtx->txbd.cbd_bufaddr = (uint) &rtx->txbuf;
134 rtx->txbd.cbd_sc = 0;
136 /* Set up the uart parameters in the parameter ram. */
137 up->smc_rbase = dpaddr;
138 up->smc_tbase = dpaddr+sizeof(cbd_t);
139 up->smc_rfcr = CPMFCR_EB;
140 up->smc_tfcr = CPMFCR_EB;
145 /* Set UART mode, 8 bit, no parity, one stop.
146 * Enable receive and transmit.
148 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
150 /* Mask all interrupts and remove anything pending. */
154 /* put the SMC channel into NMSI (non multiplexd serial interface)
155 * mode and wire either BRG7 to SMC1 or BRG8 to SMC2 (15-17).
157 im->im_cpmux.cmx_smr = (im->im_cpmux.cmx_smr&~CMXSMR_MASK)|CMXSMR_VALUE;
159 /* Set up the baud rate generator. */
162 /* Make the first buffer the only buffer. */
163 rtx->txbd.cbd_sc |= BD_SC_WRAP;
164 rtx->rxbd.cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
166 /* single/multi character receive. */
167 up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
168 up->smc_maxidl = CONFIG_SYS_MAXIDLE;
171 /* Initialize Tx/Rx parameters. */
173 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
176 cp->cp_cpcr = mk_cr_cmd(CPM_CR_SMC_PAGE, CPM_CR_SMC_SBLOCK,
177 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
179 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
182 /* Enable transmitter/receiver. */
183 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
188 static void mpc8260_smc_serial_setbrg(void)
190 #if defined(CONFIG_CONS_USE_EXTC)
191 m8260_cpm_extcbrg(brg_map[SMC_INDEX], gd->baudrate,
192 CONFIG_CONS_EXTC_RATE, CONFIG_CONS_EXTC_PINSEL);
194 m8260_cpm_setbrg(brg_map[SMC_INDEX], gd->baudrate);
198 static void mpc8260_smc_serial_putc(const char c)
200 volatile smc_uart_t *up;
201 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
202 volatile serialbuffer_t *rtx;
207 up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
209 rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
211 /* Wait for last character to go. */
212 while (rtx->txbd.cbd_sc & BD_SC_READY & BD_SC_READY)
215 rtx->txbd.cbd_datlen = 1;
216 rtx->txbd.cbd_sc |= BD_SC_READY;
219 static void mpc8260_smc_serial_puts(const char *s)
226 static int mpc8260_smc_serial_getc(void)
228 volatile smc_uart_t *up;
229 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
230 volatile serialbuffer_t *rtx;
233 up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
235 rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
237 /* Wait for character to show up. */
238 while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
241 /* the characters are read one by one,
242 * use the rxindex to know the next char to deliver
244 c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr + rtx->rxindex);
247 /* check if all char are readout, then make prepare for next receive */
248 if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
250 rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
255 static int mpc8260_smc_serial_tstc(void)
257 volatile smc_uart_t *up;
258 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
259 volatile serialbuffer_t *rtx;
261 up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
262 rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
264 return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
267 #ifdef CONFIG_SERIAL_MULTI
268 static struct serial_device mpc8260_smc_serial_drv = {
269 .name = "mpc8260_smc_uart",
270 .start = mpc8260_smc_serial_init,
272 .setbrg = mpc8260_smc_serial_setbrg,
273 .putc = mpc8260_smc_serial_putc,
274 .puts = mpc8260_smc_serial_puts,
275 .getc = mpc8260_smc_serial_getc,
276 .tstc = mpc8260_smc_serial_tstc,
279 void mpc8260_smc_serial_initialize(void)
281 serial_register(&mpc8260_smc_serial_drv);
284 __weak struct serial_device *default_serial_console(void)
286 return &mpc8260_smc_serial_drv;
289 int serial_init(void)
291 return mpc8260_smc_serial_init();
294 void serial_setbrg(void)
296 mpc8260_smc_serial_setbrg();
299 void serial_putc(const char c)
301 mpc8260_smc_serial_putc(c);
304 void serial_puts(const char *s)
306 mpc8260_smc_serial_puts(s);
309 int serial_getc(void)
311 return mpc8260_smc_serial_getc();
314 int serial_tstc(void)
316 return mpc8260_smc_serial_tstc();
319 #endif /* CONFIG_CONS_ON_SMC */
321 #if defined(CONFIG_KGDB_ON_SMC)
323 #if defined(CONFIG_CONS_ON_SMC) && CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX
324 #error Whoops! serial console and kgdb are on the same smc serial port
327 #if CONFIG_KGDB_INDEX == 1 /* KGDB Port on SMC1 */
329 #define KGDB_SMC_INDEX 0
330 #define KGDB_PROFF_SMC_BASE PROFF_SMC1_BASE
331 #define KGDB_PROFF_SMC PROFF_SMC1
332 #define KGDB_CPM_CR_SMC_PAGE CPM_CR_SMC1_PAGE
333 #define KGDB_CPM_CR_SMC_SBLOCK CPM_CR_SMC1_SBLOCK
334 #define KGDB_CMXSMR_MASK (CMXSMR_SMC1|CMXSMR_SMC1CS_MSK)
335 #define KGDB_CMXSMR_VALUE CMXSMR_SMC1CS_BRG7
337 #elif CONFIG_KGDB_INDEX == 2 /* KGDB Port on SMC2 */
339 #define KGDB_SMC_INDEX 1
340 #define KGDB_PROFF_SMC_BASE PROFF_SMC2_BASE
341 #define KGDB_PROFF_SMC PROFF_SMC2
342 #define KGDB_CPM_CR_SMC_PAGE CPM_CR_SMC2_PAGE
343 #define KGDB_CPM_CR_SMC_SBLOCK CPM_CR_SMC2_SBLOCK
344 #define KGDB_CMXSMR_MASK (CMXSMR_SMC2|CMXSMR_SMC2CS_MSK)
345 #define KGDB_CMXSMR_VALUE CMXSMR_SMC2CS_BRG8
349 #error "console not correctly defined"
354 kgdb_serial_init (void)
356 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
358 volatile smc_uart_t *up;
359 volatile cbd_t *tbdf, *rbdf;
360 volatile cpm8260_t *cp = &(im->im_cpm);
361 uint dpaddr, speed = CONFIG_KGDB_BAUDRATE;
364 if ((s = getenv("kgdbrate")) != NULL && *s != '\0') {
365 ulong rate = simple_strtoul(s, &e, 10);
366 if (e > s && *e == '\0')
370 /* initialize pointers to SMC */
372 sp = (smc_t *) &(im->im_smc[KGDB_SMC_INDEX]);
373 *(ushort *)(&im->im_dprambase[KGDB_PROFF_SMC_BASE]) = KGDB_PROFF_SMC;
374 up = (smc_uart_t *)&im->im_dprambase[KGDB_PROFF_SMC];
376 /* Disable transmitter/receiver. */
377 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
379 /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
381 /* Allocate space for two buffer descriptors in the DP ram.
382 * damm: allocating space after the two buffers for rx/tx data
385 dpaddr = m8260_cpm_dpalloc((2 * sizeof (cbd_t)) + 2, 16);
387 /* Set the physical address of the host memory buffers in
388 * the buffer descriptors.
390 rbdf = (cbd_t *)&im->im_dprambase[dpaddr];
391 rbdf->cbd_bufaddr = (uint) (rbdf+2);
394 tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
397 /* Set up the uart parameters in the parameter ram. */
398 up->smc_rbase = dpaddr;
399 up->smc_tbase = dpaddr+sizeof(cbd_t);
400 up->smc_rfcr = CPMFCR_EB;
401 up->smc_tfcr = CPMFCR_EB;
406 /* Set UART mode, 8 bit, no parity, one stop.
407 * Enable receive and transmit.
409 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
411 /* Mask all interrupts and remove anything pending. */
415 /* put the SMC channel into NMSI (non multiplexd serial interface)
416 * mode and wire either BRG7 to SMC1 or BRG8 to SMC2 (15-17).
418 im->im_cpmux.cmx_smr =
419 (im->im_cpmux.cmx_smr & ~KGDB_CMXSMR_MASK) | KGDB_CMXSMR_VALUE;
421 /* Set up the baud rate generator. */
422 #if defined(CONFIG_KGDB_USE_EXTC)
423 m8260_cpm_extcbrg(brg_map[KGDB_SMC_INDEX], speed,
424 CONFIG_KGDB_EXTC_RATE, CONFIG_KGDB_EXTC_PINSEL);
426 m8260_cpm_setbrg(brg_map[KGDB_SMC_INDEX], speed);
429 /* Make the first buffer the only buffer. */
430 tbdf->cbd_sc |= BD_SC_WRAP;
431 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
433 /* Single character receive. */
437 /* Initialize Tx/Rx parameters. */
439 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
442 cp->cp_cpcr = mk_cr_cmd(KGDB_CPM_CR_SMC_PAGE, KGDB_CPM_CR_SMC_SBLOCK,
443 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
445 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
448 /* Enable transmitter/receiver. */
449 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
451 printf("SMC%d at %dbps ", CONFIG_KGDB_INDEX, speed);
455 putDebugChar(const char c)
457 volatile cbd_t *tbdf;
459 volatile smc_uart_t *up;
460 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
465 up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
467 tbdf = (cbd_t *)&im->im_dprambase[up->smc_tbase];
469 /* Wait for last character to go. */
470 buf = (char *)tbdf->cbd_bufaddr;
471 while (tbdf->cbd_sc & BD_SC_READY)
475 tbdf->cbd_datlen = 1;
476 tbdf->cbd_sc |= BD_SC_READY;
480 putDebugStr (const char *s)
490 volatile cbd_t *rbdf;
491 volatile unsigned char *buf;
492 volatile smc_uart_t *up;
493 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
496 up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
498 rbdf = (cbd_t *)&im->im_dprambase[up->smc_rbase];
500 /* Wait for character to show up. */
501 buf = (unsigned char *)rbdf->cbd_bufaddr;
502 while (rbdf->cbd_sc & BD_SC_EMPTY)
505 rbdf->cbd_sc |= BD_SC_EMPTY;
511 kgdb_interruptible(int yes)
516 #endif /* CONFIG_KGDB_ON_SMC */