]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/powerpc/cpu/mpc8260/serial_smc.c
serial: Remove CONFIG_SERIAL_MULTI from serial drivers
[karo-tx-uboot.git] / arch / powerpc / cpu / mpc8260 / serial_smc.c
1 /*
2  * (C) Copyright 2000, 2001, 2002
3  * 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  * 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).
26  */
27
28 /*
29  * Minimal serial functions needed to use one of the SMC ports
30  * as serial console interface.
31  */
32
33 #include <common.h>
34 #include <mpc8260.h>
35 #include <asm/cpm_8260.h>
36 #include <serial.h>
37 #include <linux/compiler.h>
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 #if defined(CONFIG_CONS_ON_SMC)
42
43 #if CONFIG_CONS_INDEX == 1      /* Console on SMC1 */
44
45 #define SMC_INDEX               0
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
52
53 #elif CONFIG_CONS_INDEX == 2    /* Console on SMC2 */
54
55 #define SMC_INDEX               1
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
62
63 #else
64
65 #error "console not correctly defined"
66
67 #endif
68
69 #if !defined(CONFIG_SYS_SMC_RXBUFLEN)
70 #define CONFIG_SYS_SMC_RXBUFLEN 1
71 #define CONFIG_SYS_MAXIDLE      0
72 #else
73 #if !defined(CONFIG_SYS_MAXIDLE)
74 #error "you must define CONFIG_SYS_MAXIDLE"
75 #endif
76 #endif
77
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 */
84 } serialbuffer_t;
85
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 */
94 };
95
96 static int mpc8260_smc_serial_init(void)
97 {
98         volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
99         volatile smc_t *sp;
100         volatile smc_uart_t *up;
101         volatile cpm8260_t *cp = &(im->im_cpm);
102         uint    dpaddr;
103         volatile serialbuffer_t *rtx;
104
105         /* initialize pointers to SMC */
106
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];
110
111         /* Disable transmitter/receiver. */
112         sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
113
114         /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
115
116         /* Allocate space for two buffer descriptors in the DP ram.
117          * damm: allocating space after the two buffers for rx/tx data
118          */
119
120         /* allocate size of struct serialbuffer with bd rx/tx,
121          * buffer rx/tx and rx index
122          */
123         dpaddr = m8260_cpm_dpalloc((sizeof(serialbuffer_t)), 16);
124
125         rtx = (serialbuffer_t *)&im->im_dprambase[dpaddr];
126
127         /* Set the physical address of the host memory buffers in
128          * the buffer descriptors.
129          */
130         rtx->rxbd.cbd_bufaddr = (uint) &rtx->rxbuf;
131         rtx->rxbd.cbd_sc      = 0;
132
133         rtx->txbd.cbd_bufaddr = (uint) &rtx->txbuf;
134         rtx->txbd.cbd_sc      = 0;
135
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;
141         up->smc_brklen = 0;
142         up->smc_brkec = 0;
143         up->smc_brkcr = 0;
144
145         /* Set UART mode, 8 bit, no parity, one stop.
146          * Enable receive and transmit.
147          */
148         sp->smc_smcmr = smcr_mk_clen(9) |  SMCMR_SM_UART;
149
150         /* Mask all interrupts and remove anything pending. */
151         sp->smc_smcm = 0;
152         sp->smc_smce = 0xff;
153
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).
156          */
157         im->im_cpmux.cmx_smr = (im->im_cpmux.cmx_smr&~CMXSMR_MASK)|CMXSMR_VALUE;
158
159         /* Set up the baud rate generator. */
160         serial_setbrg ();
161
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;
165
166         /* single/multi character receive. */
167         up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
168         up->smc_maxidl = CONFIG_SYS_MAXIDLE;
169         rtx->rxindex = 0;
170
171         /* Initialize Tx/Rx parameters. */
172
173         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
174           ;
175
176         cp->cp_cpcr = mk_cr_cmd(CPM_CR_SMC_PAGE, CPM_CR_SMC_SBLOCK,
177                                         0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
178
179         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
180           ;
181
182         /* Enable transmitter/receiver. */
183         sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
184
185         return (0);
186 }
187
188 static void mpc8260_smc_serial_setbrg(void)
189 {
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);
193 #else
194         m8260_cpm_setbrg(brg_map[SMC_INDEX], gd->baudrate);
195 #endif
196 }
197
198 static void mpc8260_smc_serial_putc(const char c)
199 {
200         volatile smc_uart_t     *up;
201         volatile immap_t        *im = (immap_t *)CONFIG_SYS_IMMR;
202         volatile serialbuffer_t *rtx;
203
204         if (c == '\n')
205                 serial_putc ('\r');
206
207         up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
208
209         rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
210
211         /* Wait for last character to go. */
212         while (rtx->txbd.cbd_sc & BD_SC_READY & BD_SC_READY)
213                 ;
214         rtx->txbuf = c;
215         rtx->txbd.cbd_datlen = 1;
216         rtx->txbd.cbd_sc |= BD_SC_READY;
217 }
218
219 static void mpc8260_smc_serial_puts(const char *s)
220 {
221         while (*s) {
222                 serial_putc (*s++);
223         }
224 }
225
226 static int mpc8260_smc_serial_getc(void)
227 {
228         volatile smc_uart_t     *up;
229         volatile immap_t        *im = (immap_t *)CONFIG_SYS_IMMR;
230         volatile serialbuffer_t *rtx;
231         unsigned char  c;
232
233         up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
234
235         rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
236
237         /* Wait for character to show up. */
238         while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
239                 ;
240
241         /* the characters are read one by one,
242          * use the rxindex to know the next char to deliver
243          */
244         c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr + rtx->rxindex);
245         rtx->rxindex++;
246
247         /* check if all char are readout, then make prepare for next receive */
248         if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
249                 rtx->rxindex = 0;
250                 rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
251         }
252         return(c);
253 }
254
255 static int mpc8260_smc_serial_tstc(void)
256 {
257         volatile smc_uart_t     *up;
258         volatile immap_t        *im = (immap_t *)CONFIG_SYS_IMMR;
259         volatile serialbuffer_t *rtx;
260
261         up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
262         rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
263
264         return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
265 }
266
267 static struct serial_device mpc8260_smc_serial_drv = {
268         .name   = "mpc8260_smc_uart",
269         .start  = mpc8260_smc_serial_init,
270         .stop   = NULL,
271         .setbrg = mpc8260_smc_serial_setbrg,
272         .putc   = mpc8260_smc_serial_putc,
273         .puts   = mpc8260_smc_serial_puts,
274         .getc   = mpc8260_smc_serial_getc,
275         .tstc   = mpc8260_smc_serial_tstc,
276 };
277
278 void mpc8260_smc_serial_initialize(void)
279 {
280         serial_register(&mpc8260_smc_serial_drv);
281 }
282
283 __weak struct serial_device *default_serial_console(void)
284 {
285         return &mpc8260_smc_serial_drv;
286 }
287 #endif  /* CONFIG_CONS_ON_SMC */
288
289 #if defined(CONFIG_KGDB_ON_SMC)
290
291 #if defined(CONFIG_CONS_ON_SMC) && CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX
292 #error Whoops! serial console and kgdb are on the same smc serial port
293 #endif
294
295 #if CONFIG_KGDB_INDEX == 1      /* KGDB Port on SMC1 */
296
297 #define KGDB_SMC_INDEX          0
298 #define KGDB_PROFF_SMC_BASE     PROFF_SMC1_BASE
299 #define KGDB_PROFF_SMC          PROFF_SMC1
300 #define KGDB_CPM_CR_SMC_PAGE    CPM_CR_SMC1_PAGE
301 #define KGDB_CPM_CR_SMC_SBLOCK  CPM_CR_SMC1_SBLOCK
302 #define KGDB_CMXSMR_MASK        (CMXSMR_SMC1|CMXSMR_SMC1CS_MSK)
303 #define KGDB_CMXSMR_VALUE       CMXSMR_SMC1CS_BRG7
304
305 #elif CONFIG_KGDB_INDEX == 2    /* KGDB Port on SMC2 */
306
307 #define KGDB_SMC_INDEX          1
308 #define KGDB_PROFF_SMC_BASE     PROFF_SMC2_BASE
309 #define KGDB_PROFF_SMC          PROFF_SMC2
310 #define KGDB_CPM_CR_SMC_PAGE    CPM_CR_SMC2_PAGE
311 #define KGDB_CPM_CR_SMC_SBLOCK  CPM_CR_SMC2_SBLOCK
312 #define KGDB_CMXSMR_MASK        (CMXSMR_SMC2|CMXSMR_SMC2CS_MSK)
313 #define KGDB_CMXSMR_VALUE       CMXSMR_SMC2CS_BRG8
314
315 #else
316
317 #error "console not correctly defined"
318
319 #endif
320
321 void
322 kgdb_serial_init (void)
323 {
324         volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
325         volatile smc_t *sp;
326         volatile smc_uart_t *up;
327         volatile cbd_t *tbdf, *rbdf;
328         volatile cpm8260_t *cp = &(im->im_cpm);
329         uint dpaddr, speed = CONFIG_KGDB_BAUDRATE;
330         char *s, *e;
331
332         if ((s = getenv("kgdbrate")) != NULL && *s != '\0') {
333                 ulong rate = simple_strtoul(s, &e, 10);
334                 if (e > s && *e == '\0')
335                         speed = rate;
336         }
337
338         /* initialize pointers to SMC */
339
340         sp = (smc_t *) &(im->im_smc[KGDB_SMC_INDEX]);
341         *(ushort *)(&im->im_dprambase[KGDB_PROFF_SMC_BASE]) = KGDB_PROFF_SMC;
342         up = (smc_uart_t *)&im->im_dprambase[KGDB_PROFF_SMC];
343
344         /* Disable transmitter/receiver. */
345         sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
346
347         /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
348
349         /* Allocate space for two buffer descriptors in the DP ram.
350          * damm: allocating space after the two buffers for rx/tx data
351          */
352
353         dpaddr = m8260_cpm_dpalloc((2 * sizeof (cbd_t)) + 2, 16);
354
355         /* Set the physical address of the host memory buffers in
356          * the buffer descriptors.
357          */
358         rbdf = (cbd_t *)&im->im_dprambase[dpaddr];
359         rbdf->cbd_bufaddr = (uint) (rbdf+2);
360         rbdf->cbd_sc = 0;
361         tbdf = rbdf + 1;
362         tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
363         tbdf->cbd_sc = 0;
364
365         /* Set up the uart parameters in the parameter ram. */
366         up->smc_rbase = dpaddr;
367         up->smc_tbase = dpaddr+sizeof(cbd_t);
368         up->smc_rfcr = CPMFCR_EB;
369         up->smc_tfcr = CPMFCR_EB;
370         up->smc_brklen = 0;
371         up->smc_brkec = 0;
372         up->smc_brkcr = 0;
373
374         /* Set UART mode, 8 bit, no parity, one stop.
375          * Enable receive and transmit.
376          */
377         sp->smc_smcmr = smcr_mk_clen(9) |  SMCMR_SM_UART;
378
379         /* Mask all interrupts and remove anything pending. */
380         sp->smc_smcm = 0;
381         sp->smc_smce = 0xff;
382
383         /* put the SMC channel into NMSI (non multiplexd serial interface)
384          * mode and wire either BRG7 to SMC1 or BRG8 to SMC2 (15-17).
385          */
386         im->im_cpmux.cmx_smr =
387                 (im->im_cpmux.cmx_smr & ~KGDB_CMXSMR_MASK) | KGDB_CMXSMR_VALUE;
388
389         /* Set up the baud rate generator. */
390 #if defined(CONFIG_KGDB_USE_EXTC)
391         m8260_cpm_extcbrg(brg_map[KGDB_SMC_INDEX], speed,
392                 CONFIG_KGDB_EXTC_RATE, CONFIG_KGDB_EXTC_PINSEL);
393 #else
394         m8260_cpm_setbrg(brg_map[KGDB_SMC_INDEX], speed);
395 #endif
396
397         /* Make the first buffer the only buffer. */
398         tbdf->cbd_sc |= BD_SC_WRAP;
399         rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
400
401         /* Single character receive. */
402         up->smc_mrblr = 1;
403         up->smc_maxidl = 0;
404
405         /* Initialize Tx/Rx parameters. */
406
407         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
408           ;
409
410         cp->cp_cpcr = mk_cr_cmd(KGDB_CPM_CR_SMC_PAGE, KGDB_CPM_CR_SMC_SBLOCK,
411                                         0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
412
413         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
414           ;
415
416         /* Enable transmitter/receiver. */
417         sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
418
419         printf("SMC%d at %dbps ", CONFIG_KGDB_INDEX, speed);
420 }
421
422 void
423 putDebugChar(const char c)
424 {
425         volatile cbd_t          *tbdf;
426         volatile char           *buf;
427         volatile smc_uart_t     *up;
428         volatile immap_t        *im = (immap_t *)CONFIG_SYS_IMMR;
429
430         if (c == '\n')
431                 putDebugChar ('\r');
432
433         up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
434
435         tbdf = (cbd_t *)&im->im_dprambase[up->smc_tbase];
436
437         /* Wait for last character to go. */
438         buf = (char *)tbdf->cbd_bufaddr;
439         while (tbdf->cbd_sc & BD_SC_READY)
440                 ;
441
442         *buf = c;
443         tbdf->cbd_datlen = 1;
444         tbdf->cbd_sc |= BD_SC_READY;
445 }
446
447 void
448 putDebugStr (const char *s)
449 {
450         while (*s) {
451                 putDebugChar (*s++);
452         }
453 }
454
455 int
456 getDebugChar(void)
457 {
458         volatile cbd_t          *rbdf;
459         volatile unsigned char  *buf;
460         volatile smc_uart_t     *up;
461         volatile immap_t        *im = (immap_t *)CONFIG_SYS_IMMR;
462         unsigned char           c;
463
464         up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
465
466         rbdf = (cbd_t *)&im->im_dprambase[up->smc_rbase];
467
468         /* Wait for character to show up. */
469         buf = (unsigned char *)rbdf->cbd_bufaddr;
470         while (rbdf->cbd_sc & BD_SC_EMPTY)
471                 ;
472         c = *buf;
473         rbdf->cbd_sc |= BD_SC_EMPTY;
474
475         return(c);
476 }
477
478 void
479 kgdb_interruptible(int yes)
480 {
481         return;
482 }
483
484 #endif  /* CONFIG_KGDB_ON_SMC */