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