This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / serial / cpm_uart / cpm_uart_core.c
1 /*
2  *  linux/drivers/serial/cpm_uart.c
3  *
4  *  Driver for CPM (SCC/SMC) serial ports; core driver
5  *
6  *  Based on arch/ppc/cpm2_io/uart.c by Dan Malek
7  *  Based on ppc8xx.c by Thomas Gleixner
8  *  Based on drivers/serial/amba.c by Russell King
9  *
10  *  Maintainer: Kumar Gala (kumar.gala@freescale.com) (CPM2)
11  *              Pantelis Antoniou (panto@intracom.gr) (CPM1)
12  * 
13  *  Copyright (C) 2004 Freescale Semiconductor, Inc.
14  *            (C) 2004 Intracom, S.A.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *
30  */
31
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/tty.h>
35 #include <linux/ioport.h>
36 #include <linux/init.h>
37 #include <linux/serial.h>
38 #include <linux/console.h>
39 #include <linux/sysrq.h>
40 #include <linux/device.h>
41 #include <linux/bootmem.h>
42 #include <linux/dma-mapping.h>
43
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/delay.h>
47
48 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
49 #define SUPPORT_SYSRQ
50 #endif
51
52 #include <linux/serial_core.h>
53 #include <linux/kernel.h>
54
55 #include "cpm_uart.h"
56
57 /***********************************************************************/
58
59 /* Track which ports are configured as uarts */
60 int cpm_uart_port_map[UART_NR];
61 /* How many ports did we config as uarts */
62 int cpm_uart_nr;
63
64 /**************************************************************/
65
66 static int  cpm_uart_tx_pump(struct uart_port *port);
67 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo, int bits, u16 cval);
68 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo, int sbits, u16 sval);
69
70 /**************************************************************/
71
72 /*
73  * Check, if transmit buffers are processed             
74 */
75 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
76 {
77         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
78         volatile cbd_t *bdp = pinfo->tx_bd_base;
79         int ret = 0;
80
81         while (1) {
82                 if (bdp->cbd_sc & BD_SC_READY)
83                         break;
84
85                 if (bdp->cbd_sc & BD_SC_WRAP) {
86                         ret = TIOCSER_TEMT;
87                         break;
88                 }
89                 bdp++;
90         }
91
92         pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
93
94         return ret;
95 }
96
97 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
98 {
99         /* Whee. Do nothing. */
100 }
101
102 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
103 {
104         /* Whee. Do nothing. */
105         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
106 }
107
108 /*
109  * Stop transmitter
110  */
111 static void cpm_uart_stop_tx(struct uart_port *port, unsigned int tty_stop)
112 {
113         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
114         volatile smc_t *smcp = pinfo->smcp;
115         volatile scc_t *sccp = pinfo->sccp;
116
117         pr_debug("CPM uart[%d]:stop tx\n", port->line);
118
119         if (IS_SMC(pinfo))
120                 smcp->smc_smcm &= ~SMCM_TX;
121         else
122                 sccp->scc_sccm &= ~UART_SCCM_TX;
123 }
124
125 /*
126  * Start transmitter
127  */
128 static void cpm_uart_start_tx(struct uart_port *port, unsigned int tty_start)
129 {
130         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
131         volatile smc_t *smcp = pinfo->smcp;
132         volatile scc_t *sccp = pinfo->sccp;
133
134         pr_debug("CPM uart[%d]:start tx\n", port->line);
135
136         /* if in the middle of discarding return */
137         if (IS_DISCARDING(pinfo))
138                 return;
139
140         if (IS_SMC(pinfo)) {
141                 if (smcp->smc_smcm & SMCM_TX)
142                         return;
143         } else {
144                 if (sccp->scc_sccm & UART_SCCM_TX)
145                         return;
146         }
147
148         if (cpm_uart_tx_pump(port) != 0) {
149                 if (IS_SMC(pinfo))
150                         smcp->smc_smcm |= SMCM_TX;
151                 else
152                         sccp->scc_sccm |= UART_SCCM_TX;
153         }
154 }
155
156 /*
157  * Stop receiver 
158  */
159 static void cpm_uart_stop_rx(struct uart_port *port)
160 {
161         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
162         volatile smc_t *smcp = pinfo->smcp;
163         volatile scc_t *sccp = pinfo->sccp;
164
165         pr_debug("CPM uart[%d]:stop rx\n", port->line);
166
167         if (IS_SMC(pinfo))
168                 smcp->smc_smcm &= ~SMCM_RX;
169         else
170                 sccp->scc_sccm &= ~UART_SCCM_RX;
171 }
172
173 /*
174  * Enable Modem status interrupts
175  */
176 static void cpm_uart_enable_ms(struct uart_port *port)
177 {
178         pr_debug("CPM uart[%d]:enable ms\n", port->line);
179 }
180
181 /*
182  * Generate a break. 
183  */
184 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
185 {
186         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
187         int line = pinfo - cpm_uart_ports;
188
189         pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
190                 break_state);
191
192         if (break_state)
193                 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
194         else
195                 cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
196 }
197
198 /*
199  * Transmit characters, refill buffer descriptor, if possible
200  */
201 static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
202 {
203         pr_debug("CPM uart[%d]:TX INT\n", port->line);
204
205         cpm_uart_tx_pump(port);
206 }
207
208 /*
209  * Receive characters
210  */
211 static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
212 {
213         int i;
214         unsigned char ch, *cp;
215         struct tty_struct *tty = port->info->tty;
216         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
217         volatile cbd_t *bdp;
218         u16 status;
219         unsigned int flg;
220
221         pr_debug("CPM uart[%d]:RX INT\n", port->line);
222
223         /* Just loop through the closed BDs and copy the characters into
224          * the buffer.
225          */
226         bdp = pinfo->rx_cur;
227         for (;;) {
228                 /* get status */
229                 status = bdp->cbd_sc;
230                 /* If this one is empty, return happy */
231                 if (status & BD_SC_EMPTY)
232                         break;
233
234                 /* get number of characters, and check spce in flip-buffer */
235                 i = bdp->cbd_datlen;
236
237                 /* If we have not enough room in tty flip buffer, then we try 
238                  * later, which will be the next rx-interrupt or a timeout
239                  */
240                 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE) {
241                         tty->flip.work.func((void *)tty);
242                         if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE) {
243                                 printk(KERN_WARNING "TTY_DONT_FLIP set\n");
244                                 return;
245                         }
246                 }
247
248                 /* get pointer */
249                 cp = (unsigned char *)bus_to_virt(bdp->cbd_bufaddr);
250
251                 /* loop through the buffer */
252                 while (i-- > 0) {
253                         ch = *cp++;
254                         port->icount.rx++;
255                         flg = TTY_NORMAL;
256
257                         if (status &
258                             (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
259                                 goto handle_error;
260                         if (uart_handle_sysrq_char(port, ch, regs))
261                                 continue;
262
263                       error_return:
264                         *tty->flip.char_buf_ptr++ = ch;
265                         *tty->flip.flag_buf_ptr++ = flg;
266                         tty->flip.count++;
267
268                 }               /* End while (i--) */
269
270                 /* This BD is ready to be used again. Clear status. get next */
271                 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
272                 bdp->cbd_sc |= BD_SC_EMPTY;
273
274                 if (bdp->cbd_sc & BD_SC_WRAP)
275                         bdp = pinfo->rx_bd_base;
276                 else
277                         bdp++;
278         } /* End for (;;) */
279
280         /* Write back buffer pointer */
281         pinfo->rx_cur = (volatile cbd_t *) bdp;
282
283         /* activate BH processing */
284         tty_flip_buffer_push(tty);
285
286         return;
287
288         /* Error processing */
289
290       handle_error:
291         /* Statistics */
292         if (status & BD_SC_BR)
293                 port->icount.brk++;
294         if (status & BD_SC_PR)
295                 port->icount.parity++;
296         if (status & BD_SC_FR)
297                 port->icount.frame++;
298         if (status & BD_SC_OV)
299                 port->icount.overrun++;
300
301         /* Mask out ignored conditions */
302         status &= port->read_status_mask;
303
304         /* Handle the remaining ones */
305         if (status & BD_SC_BR)
306                 flg = TTY_BREAK;
307         else if (status & BD_SC_PR)
308                 flg = TTY_PARITY;
309         else if (status & BD_SC_FR)
310                 flg = TTY_FRAME;
311
312         /* overrun does not affect the current character ! */
313         if (status & BD_SC_OV) {
314                 ch = 0;
315                 flg = TTY_OVERRUN;
316                 /* We skip this buffer */
317                 /* CHECK: Is really nothing senseful there */
318                 /* ASSUMPTION: it contains nothing valid */
319                 i = 0;
320         }
321 #ifdef SUPPORT_SYSRQ
322         port->sysrq = 0;
323 #endif
324         goto error_return;
325 }
326
327 /*
328  * Asynchron mode interrupt handler
329  */
330 static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
331 {
332         u8 events;
333         struct uart_port *port = (struct uart_port *)data;
334         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
335         volatile smc_t *smcp = pinfo->smcp;
336         volatile scc_t *sccp = pinfo->sccp;
337
338         pr_debug("CPM uart[%d]:IRQ\n", port->line);
339
340         if (IS_SMC(pinfo)) {
341                 events = smcp->smc_smce;
342                 if (events & SMCM_BRKE)
343                         uart_handle_break(port);
344                 if (events & SMCM_RX)
345                         cpm_uart_int_rx(port, regs);
346                 if (events & SMCM_TX)
347                         cpm_uart_int_tx(port, regs);
348                 smcp->smc_smce = events;
349         } else {
350                 events = sccp->scc_scce;
351                 if (events & UART_SCCM_BRKE)
352                         uart_handle_break(port);
353                 if (events & UART_SCCM_RX)
354                         cpm_uart_int_rx(port, regs);
355                 if (events & UART_SCCM_TX)
356                         cpm_uart_int_tx(port, regs);
357                 sccp->scc_scce = events;
358         }
359         return (events) ? IRQ_HANDLED : IRQ_NONE;
360 }
361
362 static int cpm_uart_startup(struct uart_port *port)
363 {
364         int retval;
365
366         pr_debug("CPM uart[%d]:startup\n", port->line);
367
368         /* Install interrupt handler. */
369         retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
370         if (retval)
371                 return retval;
372
373         return 0;
374 }
375
376 /*
377  * Shutdown the uart
378  */
379 static void cpm_uart_shutdown(struct uart_port *port)
380 {
381         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
382         int line = pinfo - cpm_uart_ports;
383
384         pr_debug("CPM uart[%d]:shutdown\n", port->line);
385
386         /* free interrupt handler */
387         free_irq(port->irq, port);
388
389         /* If the port is not the console, disable Rx and Tx. */
390         if (!(pinfo->flags & FLAG_CONSOLE)) {
391                 /* Stop uarts */
392                 if (IS_SMC(pinfo)) {
393                         volatile smc_t *smcp = pinfo->smcp;
394                         smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
395                         smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
396                 } else {
397                         volatile scc_t *sccp = pinfo->sccp;
398                         sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
399                         sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
400                 }
401
402                 /* Shut them really down and reinit buffer descriptors */
403                 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
404         }
405 }
406
407 static void cpm_uart_set_termios(struct uart_port *port,
408                                  struct termios *termios, struct termios *old)
409 {
410         int baud;
411         unsigned long flags;
412         u16 cval, scval;
413         int bits, sbits;
414         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
415         int line = pinfo - cpm_uart_ports;
416         volatile cbd_t *bdp;
417
418         pr_debug("CPM uart[%d]:set_termios\n", port->line);
419
420         spin_lock_irqsave(&port->lock, flags);
421         /* disable uart interrupts */
422         if (IS_SMC(pinfo))
423                 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
424         else
425                 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
426         pinfo->flags |= FLAG_DISCARDING;
427         spin_unlock_irqrestore(&port->lock, flags);
428
429         /* if previous configuration exists wait for tx to finish */
430         if (pinfo->baud != 0 && pinfo->bits != 0) {
431
432                 /* point to the last txed bd */
433                 bdp = pinfo->tx_cur;
434                 if (bdp == pinfo->tx_bd_base)
435                         bdp = pinfo->tx_bd_base + (pinfo->tx_nrfifos - 1);
436                 else
437                         bdp--;
438
439                 /* wait for it to be transmitted */
440                 while ((bdp->cbd_sc & BD_SC_READY) != 0)
441                         schedule();
442
443                 /* and delay for the hw fifo to drain */
444                 udelay((3 * 1000000 * pinfo->bits) / pinfo->baud);
445         }
446
447         spin_lock_irqsave(&port->lock, flags);
448
449         /* Send the CPM an initialize command. */
450         cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
451
452         /* Stop uart */
453         if (IS_SMC(pinfo))
454                 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
455         else
456                 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
457
458         /* Send the CPM an initialize command. */
459         cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
460
461         spin_unlock_irqrestore(&port->lock, flags);
462
463         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
464
465         /* Character length programmed into the mode register is the
466          * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
467          * 1 or 2 stop bits, minus 1.
468          * The value 'bits' counts this for us.
469          */
470         cval = 0;
471         scval = 0;
472
473         /* byte size */
474         switch (termios->c_cflag & CSIZE) {
475         case CS5:
476                 bits = 5;
477                 break;
478         case CS6:
479                 bits = 6;
480                 break;
481         case CS7:
482                 bits = 7;
483                 break;
484         case CS8:
485                 bits = 8;
486                 break;
487                 /* Never happens, but GCC is too dumb to figure it out */
488         default:
489                 bits = 8;
490                 break;
491         }
492         sbits = bits - 5;
493
494         if (termios->c_cflag & CSTOPB) {
495                 cval |= SMCMR_SL;       /* Two stops */
496                 scval |= SCU_PSMR_SL;
497                 bits++;
498         }
499
500         if (termios->c_cflag & PARENB) {
501                 cval |= SMCMR_PEN;
502                 scval |= SCU_PSMR_PEN;
503                 bits++;
504                 if (!(termios->c_cflag & PARODD)) {
505                         cval |= SMCMR_PM_EVEN;
506                         scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
507                 }
508         }
509
510         /*
511          * Set up parity check flag
512          */
513 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
514
515         port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
516         if (termios->c_iflag & INPCK)
517                 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
518         if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
519                 port->read_status_mask |= BD_SC_BR;
520
521         /*
522          * Characters to ignore
523          */
524         port->ignore_status_mask = 0;
525         if (termios->c_iflag & IGNPAR)
526                 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
527         if (termios->c_iflag & IGNBRK) {
528                 port->ignore_status_mask |= BD_SC_BR;
529                 /*
530                  * If we're ignore parity and break indicators, ignore
531                  * overruns too.  (For real raw support).
532                  */
533                 if (termios->c_iflag & IGNPAR)
534                         port->ignore_status_mask |= BD_SC_OV;
535         }
536         /*
537          * !!! ignore all characters if CREAD is not set
538          */
539         if ((termios->c_cflag & CREAD) == 0)
540                 port->read_status_mask &= ~BD_SC_EMPTY;
541         
542         spin_lock_irqsave(&port->lock, flags);
543
544         cpm_set_brg(pinfo->brg - 1, baud);
545
546         /* Start bit has not been added (so don't, because we would just
547          * subtract it later), and we need to add one for the number of
548          * stops bits (there is always at least one).
549          */
550         bits++;
551
552         /* re-init */
553         if (IS_SMC(pinfo))
554                 cpm_uart_init_smc(pinfo, bits, cval);
555         else
556                 cpm_uart_init_scc(pinfo, sbits, scval);
557
558         pinfo->baud = baud;
559         pinfo->bits = bits;
560
561         pinfo->flags &= ~FLAG_DISCARDING;
562         spin_unlock_irqrestore(&port->lock, flags);
563
564 }
565
566 static const char *cpm_uart_type(struct uart_port *port)
567 {
568         pr_debug("CPM uart[%d]:uart_type\n", port->line);
569
570         return port->type == PORT_CPM ? "CPM UART" : NULL;
571 }
572
573 /*
574  * verify the new serial_struct (for TIOCSSERIAL).
575  */
576 static int cpm_uart_verify_port(struct uart_port *port,
577                                 struct serial_struct *ser)
578 {
579         int ret = 0;
580
581         pr_debug("CPM uart[%d]:verify_port\n", port->line);
582
583         if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
584                 ret = -EINVAL;
585         if (ser->irq < 0 || ser->irq >= NR_IRQS)
586                 ret = -EINVAL;
587         if (ser->baud_base < 9600)
588                 ret = -EINVAL;
589         return ret;
590 }
591
592 /*
593  * Transmit characters, refill buffer descriptor, if possible
594  */
595 static int cpm_uart_tx_pump(struct uart_port *port)
596 {
597         volatile cbd_t *bdp;
598         unsigned char *p;
599         int count;
600         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
601         struct circ_buf *xmit = &port->info->xmit;
602
603         /* Handle xon/xoff */
604         if (port->x_char) {
605                 /* Pick next descriptor and fill from buffer */
606                 bdp = pinfo->tx_cur;
607
608                 p = bus_to_virt(bdp->cbd_bufaddr);
609                 *p++ = xmit->buf[xmit->tail];
610                 bdp->cbd_datlen = 1;
611                 bdp->cbd_sc |= BD_SC_READY;
612                 /* Get next BD. */
613                 if (bdp->cbd_sc & BD_SC_WRAP)
614                         bdp = pinfo->tx_bd_base;
615                 else
616                         bdp++;
617                 pinfo->tx_cur = bdp;
618
619                 port->icount.tx++;
620                 port->x_char = 0;
621                 return 1;
622         }
623
624         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
625                 cpm_uart_stop_tx(port, 0);
626                 return 0;
627         }
628
629         /* Pick next descriptor and fill from buffer */
630         bdp = pinfo->tx_cur;
631
632         while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
633                 count = 0;
634                 p = bus_to_virt(bdp->cbd_bufaddr);
635                 while (count < pinfo->tx_fifosize) {
636                         *p++ = xmit->buf[xmit->tail];
637                         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
638                         port->icount.tx++;
639                         count++;
640                         if (xmit->head == xmit->tail)
641                                 break;
642                 }
643                 bdp->cbd_datlen = count;
644                 bdp->cbd_sc |= BD_SC_READY;
645                 /* Get next BD. */
646                 if (bdp->cbd_sc & BD_SC_WRAP)
647                         bdp = pinfo->tx_bd_base;
648                 else
649                         bdp++;
650         }
651         pinfo->tx_cur = bdp;
652
653         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
654                 uart_write_wakeup(port);
655
656         if (uart_circ_empty(xmit)) {
657                 cpm_uart_stop_tx(port, 0);
658                 return 0;
659         }
660
661         return 1;
662 }
663
664 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo, int bits, u16 scval)
665 {
666         int line = pinfo - cpm_uart_ports;
667         volatile scc_t *scp;
668         volatile scc_uart_t *sup;
669         u8 *mem_addr;
670         volatile cbd_t *bdp;
671         int i;
672
673         pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
674
675         scp = pinfo->sccp;
676         sup = pinfo->sccup;
677
678         /* Set the physical address of the host memory
679          * buffers in the buffer descriptors, and the
680          * virtual address for us to work with.
681          */
682         pinfo->rx_cur = pinfo->rx_bd_base;
683         mem_addr = pinfo->mem_addr;
684         for (bdp = pinfo->rx_bd_base, i = 0; i < pinfo->rx_nrfifos; i++, bdp++) {
685                 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
686                 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT | (i < (pinfo->rx_nrfifos - 1) ? 0 : BD_SC_WRAP);
687                 mem_addr += pinfo->rx_fifosize;
688         }
689
690         /* Set the physical address of the host memory
691          * buffers in the buffer descriptors, and the
692          * virtual address for us to work with.
693          */
694         mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
695         pinfo->tx_cur = pinfo->tx_bd_base;
696         for (bdp = pinfo->tx_bd_base, i = 0; i < pinfo->tx_nrfifos; i++, bdp++) {
697                 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
698                 bdp->cbd_sc = BD_SC_INTRPT | (i < (pinfo->tx_nrfifos - 1) ? 0 : BD_SC_WRAP);
699                 mem_addr += pinfo->tx_fifosize;
700                 bdp++;
701         }
702
703         /* Store address */
704         pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
705         pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
706
707         /* Set up the uart parameters in the
708          * parameter ram.
709          */
710
711         cpm_set_scc_fcr(sup);
712
713         sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
714         sup->scc_maxidl = pinfo->rx_fifosize;
715         sup->scc_brkcr = 1;
716         sup->scc_parec = 0;
717         sup->scc_frmec = 0;
718         sup->scc_nosec = 0;
719         sup->scc_brkec = 0;
720         sup->scc_uaddr1 = 0;
721         sup->scc_uaddr2 = 0;
722         sup->scc_toseq = 0;
723         sup->scc_char1 = 0x8000;
724         sup->scc_char2 = 0x8000;
725         sup->scc_char3 = 0x8000;
726         sup->scc_char4 = 0x8000;
727         sup->scc_char5 = 0x8000;
728         sup->scc_char6 = 0x8000;
729         sup->scc_char7 = 0x8000;
730         sup->scc_char8 = 0x8000;
731         sup->scc_rccm = 0xc0ff;
732
733         /* Send the CPM an initialize command.
734          */
735         cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
736
737         /* Set UART mode, 8 bit, no parity, one stop.
738          * Enable receive and transmit.
739          */
740         scp->scc_gsmrh = 0;
741         scp->scc_gsmrl =
742             (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
743
744         /* Enable rx interrupts  and clear all pending events.  */
745         scp->scc_sccm = UART_SCCM_RX;
746         scp->scc_scce = 0xffff;
747         scp->scc_dsr = 0x7e7e;
748         scp->scc_psmr = (bits << 12) | scval;
749
750         scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
751 }
752
753 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo, int bits, u16 cval)
754 {
755         int line = pinfo - cpm_uart_ports;
756         volatile smc_t *sp;
757         volatile smc_uart_t *up;
758         volatile u8 *mem_addr;
759         volatile cbd_t *bdp;
760         int i;
761
762         pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
763
764         sp = pinfo->smcp;
765         up = pinfo->smcup;
766
767         /* Set the physical address of the host memory
768          * buffers in the buffer descriptors, and the
769          * virtual address for us to work with.
770          */
771         mem_addr = pinfo->mem_addr;
772         pinfo->rx_cur = pinfo->rx_bd_base;
773         for (bdp = pinfo->rx_bd_base, i = 0; i < pinfo->rx_nrfifos; i++, bdp++) {
774                 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
775                 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT | (i < (pinfo->rx_nrfifos - 1) ? 0 : BD_SC_WRAP);
776                 mem_addr += pinfo->rx_fifosize;
777         }
778
779         /* Set the physical address of the host memory
780          * buffers in the buffer descriptors, and the
781          * virtual address for us to work with.
782          */
783         mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
784         pinfo->tx_cur = pinfo->tx_bd_base;
785         for (bdp = pinfo->tx_bd_base, i = 0; i < pinfo->tx_nrfifos; i++, bdp++) {
786                 bdp->cbd_bufaddr = virt_to_bus(mem_addr);
787                 bdp->cbd_sc = BD_SC_INTRPT | (i < (pinfo->tx_nrfifos - 1) ? 0 : BD_SC_WRAP);
788                 mem_addr += pinfo->tx_fifosize;
789         }
790
791         /* Store address */
792         pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
793         pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
794
795         /* Set up the uart parameters in the
796          * parameter ram.
797          */
798         cpm_set_smc_fcr(up);
799
800         /* Using idle charater time requires some additional tuning.  */
801         up->smc_mrblr = pinfo->rx_fifosize;
802         up->smc_maxidl = pinfo->rx_fifosize;
803         up->smc_brkcr = 1;
804
805         cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
806
807         /* Set UART mode, according to the parameters */
808         sp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
809
810         /* Enable only rx interrupts clear all pending events. */
811         sp->smc_smcm = SMCM_RX;
812         sp->smc_smce = 0xff;
813
814         sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
815 }
816
817 /*
818  * Initialize port. This is called from early_console stuff
819  * so we have to be careful here !
820  */
821 static int cpm_uart_request_port(struct uart_port *port)
822 {
823         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
824         int ret;
825
826         pr_debug("CPM uart[%d]:request port\n", port->line);
827
828         if (pinfo->flags & FLAG_CONSOLE)
829                 return 0;
830
831         /*
832          * Setup any port IO, connect any baud rate generators,
833          * etc.  This is expected to be handled by board
834          * dependant code 
835          */
836         if (pinfo->set_lineif)
837                 pinfo->set_lineif(pinfo);
838
839         ret = cpm_uart_allocbuf(pinfo, 0);
840         if (ret)
841                 return ret;
842
843         return 0;
844 }
845
846 static void cpm_uart_release_port(struct uart_port *port)
847 {
848         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
849
850         if (!(pinfo->flags & FLAG_CONSOLE))
851                 cpm_uart_freebuf(pinfo);
852 }
853
854 /*
855  * Configure/autoconfigure the port.
856  */
857 static void cpm_uart_config_port(struct uart_port *port, int flags)
858 {
859         pr_debug("CPM uart[%d]:config_port\n", port->line);
860
861         if (flags & UART_CONFIG_TYPE) {
862                 port->type = PORT_CPM;
863                 cpm_uart_request_port(port);
864         }
865 }
866 static struct uart_ops cpm_uart_pops = {
867         .tx_empty       = cpm_uart_tx_empty,
868         .set_mctrl      = cpm_uart_set_mctrl,
869         .get_mctrl      = cpm_uart_get_mctrl,
870         .stop_tx        = cpm_uart_stop_tx,
871         .start_tx       = cpm_uart_start_tx,
872         .stop_rx        = cpm_uart_stop_rx,
873         .enable_ms      = cpm_uart_enable_ms,
874         .break_ctl      = cpm_uart_break_ctl,
875         .startup        = cpm_uart_startup,
876         .shutdown       = cpm_uart_shutdown,
877         .set_termios    = cpm_uart_set_termios,
878         .type           = cpm_uart_type,
879         .release_port   = cpm_uart_release_port,
880         .request_port   = cpm_uart_request_port,
881         .config_port    = cpm_uart_config_port,
882         .verify_port    = cpm_uart_verify_port,
883 };
884
885 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
886         [UART_SMC1] = {
887                 .port = {
888                         .irq            = SMC1_IRQ,
889                         .ops            = &cpm_uart_pops,
890                         .iotype         = SERIAL_IO_MEM,
891                 },
892                 .flags = FLAG_SMC,
893                 .tx_nrfifos = TX_NUM_FIFO,
894                 .tx_fifosize = TX_BUF_SIZE,
895                 .rx_nrfifos = RX_NUM_FIFO, 
896                 .rx_fifosize = RX_BUF_SIZE,
897                 .set_lineif = smc1_lineif,
898         },
899         [UART_SMC2] = {
900                 .port = {
901                         .irq            = SMC2_IRQ,
902                         .ops            = &cpm_uart_pops,
903                         .iotype         = SERIAL_IO_MEM,
904                 },
905                 .flags = FLAG_SMC,
906                 .tx_nrfifos = TX_NUM_FIFO,
907                 .tx_fifosize = TX_BUF_SIZE,
908                 .rx_nrfifos = RX_NUM_FIFO, 
909                 .rx_fifosize = RX_BUF_SIZE,
910                 .set_lineif = smc2_lineif,
911         },
912         [UART_SCC1] = {
913                 .port = {
914                         .irq            = SCC1_IRQ,
915                         .ops            = &cpm_uart_pops,
916                         .iotype         = SERIAL_IO_MEM,
917                 },
918                 .tx_nrfifos = TX_NUM_FIFO,
919                 .tx_fifosize = TX_BUF_SIZE,
920                 .rx_nrfifos = RX_NUM_FIFO, 
921                 .rx_fifosize = RX_BUF_SIZE,
922                 .set_lineif = scc1_lineif,
923         },
924         [UART_SCC2] = {
925                 .port = {
926                         .irq            = SCC2_IRQ,
927                         .ops            = &cpm_uart_pops,
928                         .iotype         = SERIAL_IO_MEM,
929                 },
930                 .tx_nrfifos = TX_NUM_FIFO,
931                 .tx_fifosize = TX_BUF_SIZE,
932                 .rx_nrfifos = RX_NUM_FIFO, 
933                 .rx_fifosize = RX_BUF_SIZE,
934                 .set_lineif = scc2_lineif,
935         },
936         [UART_SCC3] = {
937                 .port = {
938                         .irq            = SCC3_IRQ,
939                         .ops            = &cpm_uart_pops,
940                         .iotype         = SERIAL_IO_MEM,
941                 },
942                 .tx_nrfifos = TX_NUM_FIFO,
943                 .tx_fifosize = TX_BUF_SIZE,
944                 .rx_nrfifos = RX_NUM_FIFO, 
945                 .rx_fifosize = RX_BUF_SIZE,
946                 .set_lineif = scc3_lineif,
947         },
948         [UART_SCC4] = {
949                 .port = {
950                         .irq            = SCC4_IRQ,
951                         .ops            = &cpm_uart_pops,
952                         .iotype         = SERIAL_IO_MEM,
953                 },
954                 .tx_nrfifos = TX_NUM_FIFO,
955                 .tx_fifosize = TX_BUF_SIZE,
956                 .rx_nrfifos = RX_NUM_FIFO, 
957                 .rx_fifosize = RX_BUF_SIZE,
958                 .set_lineif = scc4_lineif,
959         },
960 };
961
962 #ifdef CONFIG_SERIAL_CPM_CONSOLE
963 /*
964  *      Print a string to the serial port trying not to disturb
965  *      any possible real use of the port...
966  *
967  *      Note that this is called with interrupts already disabled
968  */
969 static void cpm_uart_console_write(struct console *co, const char *s,
970                                    u_int count)
971 {
972         struct uart_cpm_port *pinfo =
973             &cpm_uart_ports[cpm_uart_port_map[co->index]];
974         unsigned int i;
975         volatile cbd_t *bdp, *bdbase;
976         volatile unsigned char *cp;
977
978         if (IS_DISCARDING(pinfo))
979                 return;
980
981         /* Get the address of the host memory buffer.
982          */
983         bdp = pinfo->tx_cur;
984         bdbase = pinfo->tx_bd_base;
985
986         /*
987          * Now, do each character.  This is not as bad as it looks
988          * since this is a holding FIFO and not a transmitting FIFO.
989          * We could add the complexity of filling the entire transmit
990          * buffer, but we would just wait longer between accesses......
991          */
992         for (i = 0; i < count; i++, s++) {
993                 /* Wait for transmitter fifo to empty.
994                  * Ready indicates output is ready, and xmt is doing
995                  * that, not that it is ready for us to send.
996                  */
997                 while ((bdp->cbd_sc & BD_SC_READY) != 0)
998                         ;
999
1000                 /* Send the character out.
1001                  * If the buffer address is in the CPM DPRAM, don't
1002                  * convert it.
1003                  */
1004                 if ((uint) (bdp->cbd_bufaddr) > (uint) CPM_ADDR)
1005                         cp = (unsigned char *) (bdp->cbd_bufaddr);
1006                 else
1007                         cp = bus_to_virt(bdp->cbd_bufaddr);
1008                 
1009                 *cp = *s;
1010
1011                 bdp->cbd_datlen = 1;
1012                 bdp->cbd_sc |= BD_SC_READY;
1013
1014                 if (bdp->cbd_sc & BD_SC_WRAP)
1015                         bdp = bdbase;
1016                 else
1017                         bdp++;
1018
1019                 /* if a LF, also do CR... */
1020                 if (*s == 10) {
1021                         while ((bdp->cbd_sc & BD_SC_READY) != 0)
1022                                 ;
1023
1024                         if ((uint) (bdp->cbd_bufaddr) > (uint) CPM_ADDR)
1025                                 cp = (unsigned char *) (bdp->cbd_bufaddr);
1026                         else
1027                                 cp = bus_to_virt(bdp->cbd_bufaddr);
1028
1029                         *cp = 13;
1030                         bdp->cbd_datlen = 1;
1031                         bdp->cbd_sc |= BD_SC_READY;
1032
1033                         if (bdp->cbd_sc & BD_SC_WRAP)
1034                                 bdp = bdbase;
1035                         else
1036                                 bdp++;
1037                 }
1038         }
1039
1040         /*
1041          * Finally, Wait for transmitter & holding register to empty
1042          *  and restore the IER
1043          */
1044         while ((bdp->cbd_sc & BD_SC_READY) != 0)
1045                 ;
1046
1047         pinfo->tx_cur = (volatile cbd_t *) bdp;
1048 }
1049
1050 /*
1051  * Setup console. Be careful is called early !
1052  */
1053 static int __init cpm_uart_console_setup(struct console *co, char *options)
1054 {
1055         struct uart_port *port;
1056         struct uart_cpm_port *pinfo;
1057         int baud = 38400;
1058         int bits = 8;
1059         int parity = 'n';
1060         int flow = 'n';
1061         int ret;
1062
1063         port =
1064             (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1065         pinfo = (struct uart_cpm_port *)port;
1066         
1067         pinfo->flags |= FLAG_CONSOLE;
1068
1069         if (options) {
1070                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1071         } else {
1072                 bd_t *bd = (bd_t *) __res;
1073
1074                 if (bd->bi_baudrate)
1075                         baud = bd->bi_baudrate;
1076                 else
1077                         baud = 9600;
1078         }
1079
1080         /*
1081          * Setup any port IO, connect any baud rate generators,
1082          * etc.  This is expected to be handled by board
1083          * dependant code 
1084          */
1085         if (pinfo->set_lineif)
1086                 pinfo->set_lineif(pinfo);
1087
1088         ret = cpm_uart_allocbuf(pinfo, 1);
1089         if (ret)
1090                 return ret;
1091
1092         uart_set_options(port, co, baud, parity, bits, flow);
1093
1094         return 0;
1095 }
1096
1097 extern struct uart_driver cpm_reg;
1098 static struct console cpm_scc_uart_console = {
1099         .name           "ttyCPM",
1100         .write          cpm_uart_console_write,
1101         .device         uart_console_device,
1102         .setup          cpm_uart_console_setup,
1103         .flags          CON_PRINTBUFFER,
1104         .index          -1,
1105         .data           = &cpm_reg,
1106 };
1107
1108 int __init cpm_uart_console_init(void)
1109 {
1110         int ret = cpm_uart_init_portdesc();
1111
1112         if (!ret)
1113                 register_console(&cpm_scc_uart_console);
1114         return ret;
1115 }
1116
1117 console_initcall(cpm_uart_console_init);
1118
1119 #define CPM_UART_CONSOLE        &cpm_scc_uart_console
1120 #else
1121 #define CPM_UART_CONSOLE        NULL
1122 #endif
1123
1124 static struct uart_driver cpm_reg = {
1125         .owner          = THIS_MODULE,
1126         .driver_name    = "ttyCPM",
1127         .dev_name       = "ttyCPM",
1128         .major          = SERIAL_CPM_MAJOR,
1129         .minor          = SERIAL_CPM_MINOR,
1130         .cons           = CPM_UART_CONSOLE,
1131 };
1132
1133 static int __init cpm_uart_init(void)
1134 {
1135         int ret, i;
1136
1137         printk(KERN_INFO "Serial: CPM driver $Revision: 0.01 $\n");
1138
1139 #ifndef CONFIG_SERIAL_CPM_CONSOLE
1140         ret = cpm_uart_init_portdesc();
1141         if (ret)
1142                 return ret;
1143 #endif
1144
1145         cpm_reg.nr = cpm_uart_nr;
1146         ret = uart_register_driver(&cpm_reg);
1147
1148         if (ret)
1149                 return ret;
1150
1151         for (i = 0; i < cpm_uart_nr; i++) {
1152                 int con = cpm_uart_port_map[i];
1153                 cpm_uart_ports[con].port.line = i;
1154                 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1155                 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1156         }
1157
1158         return ret;
1159 }
1160
1161 static void __exit cpm_uart_exit(void)
1162 {
1163         int i;
1164
1165         for (i = 0; i < cpm_uart_nr; i++) {
1166                 int con = cpm_uart_port_map[i];
1167                 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1168         }
1169
1170         uart_unregister_driver(&cpm_reg);
1171 }
1172
1173 module_init(cpm_uart_init);
1174 module_exit(cpm_uart_exit);
1175
1176 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1177 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1178 MODULE_LICENSE("GPL");
1179 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);