]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/tty/serial/ucc_uart.c
TTY: switch tty_buffer_request_room to tty_port
[karo-tx-linux.git] / drivers / tty / serial / ucc_uart.c
1 /*
2  * Freescale QUICC Engine UART device driver
3  *
4  * Author: Timur Tabi <timur@freescale.com>
5  *
6  * Copyright 2007 Freescale Semiconductor, Inc.  This file is licensed under
7  * the terms of the GNU General Public License version 2.  This program
8  * is licensed "as is" without any warranty of any kind, whether express
9  * or implied.
10  *
11  * This driver adds support for UART devices via Freescale's QUICC Engine
12  * found on some Freescale SOCs.
13  *
14  * If Soft-UART support is needed but not already present, then this driver
15  * will request and upload the "Soft-UART" microcode upon probe.  The
16  * filename of the microcode should be fsl_qe_ucode_uart_X_YZ.bin, where "X"
17  * is the name of the SOC (e.g. 8323), and YZ is the revision of the SOC,
18  * (e.g. "11" for 1.1).
19  */
20
21 #include <linux/module.h>
22 #include <linux/serial.h>
23 #include <linux/serial_core.h>
24 #include <linux/slab.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/io.h>
28 #include <linux/of_platform.h>
29 #include <linux/dma-mapping.h>
30
31 #include <linux/fs_uart_pd.h>
32 #include <asm/ucc_slow.h>
33
34 #include <linux/firmware.h>
35 #include <asm/reg.h>
36
37 /*
38  * The GUMR flag for Soft UART.  This would normally be defined in qe.h,
39  * but Soft-UART is a hack and we want to keep everything related to it in
40  * this file.
41  */
42 #define UCC_SLOW_GUMR_H_SUART           0x00004000      /* Soft-UART */
43
44 /*
45  * soft_uart is 1 if we need to use Soft-UART mode
46  */
47 static int soft_uart;
48 /*
49  * firmware_loaded is 1 if the firmware has been loaded, 0 otherwise.
50  */
51 static int firmware_loaded;
52
53 /* Enable this macro to configure all serial ports in internal loopback
54    mode */
55 /* #define LOOPBACK */
56
57 /* The major and minor device numbers are defined in
58  * http://www.lanana.org/docs/device-list/devices-2.6+.txt.  For the QE
59  * UART, we have major number 204 and minor numbers 46 - 49, which are the
60  * same as for the CPM2.  This decision was made because no Freescale part
61  * has both a CPM and a QE.
62  */
63 #define SERIAL_QE_MAJOR 204
64 #define SERIAL_QE_MINOR 46
65
66 /* Since we only have minor numbers 46 - 49, there is a hard limit of 4 ports */
67 #define UCC_MAX_UART    4
68
69 /* The number of buffer descriptors for receiving characters. */
70 #define RX_NUM_FIFO     4
71
72 /* The number of buffer descriptors for transmitting characters. */
73 #define TX_NUM_FIFO     4
74
75 /* The maximum size of the character buffer for a single RX BD. */
76 #define RX_BUF_SIZE     32
77
78 /* The maximum size of the character buffer for a single TX BD. */
79 #define TX_BUF_SIZE     32
80
81 /*
82  * The number of jiffies to wait after receiving a close command before the
83  * device is actually closed.  This allows the last few characters to be
84  * sent over the wire.
85  */
86 #define UCC_WAIT_CLOSING 100
87
88 struct ucc_uart_pram {
89         struct ucc_slow_pram common;
90         u8 res1[8];             /* reserved */
91         __be16 maxidl;          /* Maximum idle chars */
92         __be16 idlc;            /* temp idle counter */
93         __be16 brkcr;           /* Break count register */
94         __be16 parec;           /* receive parity error counter */
95         __be16 frmec;           /* receive framing error counter */
96         __be16 nosec;           /* receive noise counter */
97         __be16 brkec;           /* receive break condition counter */
98         __be16 brkln;           /* last received break length */
99         __be16 uaddr[2];        /* UART address character 1 & 2 */
100         __be16 rtemp;           /* Temp storage */
101         __be16 toseq;           /* Transmit out of sequence char */
102         __be16 cchars[8];       /* control characters 1-8 */
103         __be16 rccm;            /* receive control character mask */
104         __be16 rccr;            /* receive control character register */
105         __be16 rlbc;            /* receive last break character */
106         __be16 res2;            /* reserved */
107         __be32 res3;            /* reserved, should be cleared */
108         u8 res4;                /* reserved, should be cleared */
109         u8 res5[3];             /* reserved, should be cleared */
110         __be32 res6;            /* reserved, should be cleared */
111         __be32 res7;            /* reserved, should be cleared */
112         __be32 res8;            /* reserved, should be cleared */
113         __be32 res9;            /* reserved, should be cleared */
114         __be32 res10;           /* reserved, should be cleared */
115         __be32 res11;           /* reserved, should be cleared */
116         __be32 res12;           /* reserved, should be cleared */
117         __be32 res13;           /* reserved, should be cleared */
118 /* The rest is for Soft-UART only */
119         __be16 supsmr;          /* 0x90, Shadow UPSMR */
120         __be16 res92;           /* 0x92, reserved, initialize to 0 */
121         __be32 rx_state;        /* 0x94, RX state, initialize to 0 */
122         __be32 rx_cnt;          /* 0x98, RX count, initialize to 0 */
123         u8 rx_length;           /* 0x9C, Char length, set to 1+CL+PEN+1+SL */
124         u8 rx_bitmark;          /* 0x9D, reserved, initialize to 0 */
125         u8 rx_temp_dlst_qe;     /* 0x9E, reserved, initialize to 0 */
126         u8 res14[0xBC - 0x9F];  /* reserved */
127         __be32 dump_ptr;        /* 0xBC, Dump pointer */
128         __be32 rx_frame_rem;    /* 0xC0, reserved, initialize to 0 */
129         u8 rx_frame_rem_size;   /* 0xC4, reserved, initialize to 0 */
130         u8 tx_mode;             /* 0xC5, mode, 0=AHDLC, 1=UART */
131         __be16 tx_state;        /* 0xC6, TX state */
132         u8 res15[0xD0 - 0xC8];  /* reserved */
133         __be32 resD0;           /* 0xD0, reserved, initialize to 0 */
134         u8 resD4;               /* 0xD4, reserved, initialize to 0 */
135         __be16 resD5;           /* 0xD5, reserved, initialize to 0 */
136 } __attribute__ ((packed));
137
138 /* SUPSMR definitions, for Soft-UART only */
139 #define UCC_UART_SUPSMR_SL              0x8000
140 #define UCC_UART_SUPSMR_RPM_MASK        0x6000
141 #define UCC_UART_SUPSMR_RPM_ODD         0x0000
142 #define UCC_UART_SUPSMR_RPM_LOW         0x2000
143 #define UCC_UART_SUPSMR_RPM_EVEN        0x4000
144 #define UCC_UART_SUPSMR_RPM_HIGH        0x6000
145 #define UCC_UART_SUPSMR_PEN             0x1000
146 #define UCC_UART_SUPSMR_TPM_MASK        0x0C00
147 #define UCC_UART_SUPSMR_TPM_ODD         0x0000
148 #define UCC_UART_SUPSMR_TPM_LOW         0x0400
149 #define UCC_UART_SUPSMR_TPM_EVEN        0x0800
150 #define UCC_UART_SUPSMR_TPM_HIGH        0x0C00
151 #define UCC_UART_SUPSMR_FRZ             0x0100
152 #define UCC_UART_SUPSMR_UM_MASK         0x00c0
153 #define UCC_UART_SUPSMR_UM_NORMAL       0x0000
154 #define UCC_UART_SUPSMR_UM_MAN_MULTI    0x0040
155 #define UCC_UART_SUPSMR_UM_AUTO_MULTI   0x00c0
156 #define UCC_UART_SUPSMR_CL_MASK         0x0030
157 #define UCC_UART_SUPSMR_CL_8            0x0030
158 #define UCC_UART_SUPSMR_CL_7            0x0020
159 #define UCC_UART_SUPSMR_CL_6            0x0010
160 #define UCC_UART_SUPSMR_CL_5            0x0000
161
162 #define UCC_UART_TX_STATE_AHDLC         0x00
163 #define UCC_UART_TX_STATE_UART          0x01
164 #define UCC_UART_TX_STATE_X1            0x00
165 #define UCC_UART_TX_STATE_X16           0x80
166
167 #define UCC_UART_PRAM_ALIGNMENT 0x100
168
169 #define UCC_UART_SIZE_OF_BD     UCC_SLOW_SIZE_OF_BD
170 #define NUM_CONTROL_CHARS       8
171
172 /* Private per-port data structure */
173 struct uart_qe_port {
174         struct uart_port port;
175         struct ucc_slow __iomem *uccp;
176         struct ucc_uart_pram __iomem *uccup;
177         struct ucc_slow_info us_info;
178         struct ucc_slow_private *us_private;
179         struct device_node *np;
180         unsigned int ucc_num;   /* First ucc is 0, not 1 */
181
182         u16 rx_nrfifos;
183         u16 rx_fifosize;
184         u16 tx_nrfifos;
185         u16 tx_fifosize;
186         int wait_closing;
187         u32 flags;
188         struct qe_bd *rx_bd_base;
189         struct qe_bd *rx_cur;
190         struct qe_bd *tx_bd_base;
191         struct qe_bd *tx_cur;
192         unsigned char *tx_buf;
193         unsigned char *rx_buf;
194         void *bd_virt;          /* virtual address of the BD buffers */
195         dma_addr_t bd_dma_addr; /* bus address of the BD buffers */
196         unsigned int bd_size;   /* size of BD buffer space */
197 };
198
199 static struct uart_driver ucc_uart_driver = {
200         .owner          = THIS_MODULE,
201         .driver_name    = "ucc_uart",
202         .dev_name       = "ttyQE",
203         .major          = SERIAL_QE_MAJOR,
204         .minor          = SERIAL_QE_MINOR,
205         .nr             = UCC_MAX_UART,
206 };
207
208 /*
209  * Virtual to physical address translation.
210  *
211  * Given the virtual address for a character buffer, this function returns
212  * the physical (DMA) equivalent.
213  */
214 static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
215 {
216         if (likely((addr >= qe_port->bd_virt)) &&
217             (addr < (qe_port->bd_virt + qe_port->bd_size)))
218                 return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
219
220         /* something nasty happened */
221         printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
222         BUG();
223         return 0;
224 }
225
226 /*
227  * Physical to virtual address translation.
228  *
229  * Given the physical (DMA) address for a character buffer, this function
230  * returns the virtual equivalent.
231  */
232 static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
233 {
234         /* sanity check */
235         if (likely((addr >= qe_port->bd_dma_addr) &&
236                    (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
237                 return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
238
239         /* something nasty happened */
240         printk(KERN_ERR "%s: addr=%llx\n", __func__, (u64)addr);
241         BUG();
242         return NULL;
243 }
244
245 /*
246  * Return 1 if the QE is done transmitting all buffers for this port
247  *
248  * This function scans each BD in sequence.  If we find a BD that is not
249  * ready (READY=1), then we return 0 indicating that the QE is still sending
250  * data.  If we reach the last BD (WRAP=1), then we know we've scanned
251  * the entire list, and all BDs are done.
252  */
253 static unsigned int qe_uart_tx_empty(struct uart_port *port)
254 {
255         struct uart_qe_port *qe_port =
256                 container_of(port, struct uart_qe_port, port);
257         struct qe_bd *bdp = qe_port->tx_bd_base;
258
259         while (1) {
260                 if (in_be16(&bdp->status) & BD_SC_READY)
261                         /* This BD is not done, so return "not done" */
262                         return 0;
263
264                 if (in_be16(&bdp->status) & BD_SC_WRAP)
265                         /*
266                          * This BD is done and it's the last one, so return
267                          * "done"
268                          */
269                         return 1;
270
271                 bdp++;
272         };
273 }
274
275 /*
276  * Set the modem control lines
277  *
278  * Although the QE can control the modem control lines (e.g. CTS), we
279  * don't need that support. This function must exist, however, otherwise
280  * the kernel will panic.
281  */
282 void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
283 {
284 }
285
286 /*
287  * Get the current modem control line status
288  *
289  * Although the QE can control the modem control lines (e.g. CTS), this
290  * driver currently doesn't support that, so we always return Carrier
291  * Detect, Data Set Ready, and Clear To Send.
292  */
293 static unsigned int qe_uart_get_mctrl(struct uart_port *port)
294 {
295         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
296 }
297
298 /*
299  * Disable the transmit interrupt.
300  *
301  * Although this function is called "stop_tx", it does not actually stop
302  * transmission of data.  Instead, it tells the QE to not generate an
303  * interrupt when the UCC is finished sending characters.
304  */
305 static void qe_uart_stop_tx(struct uart_port *port)
306 {
307         struct uart_qe_port *qe_port =
308                 container_of(port, struct uart_qe_port, port);
309
310         clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
311 }
312
313 /*
314  * Transmit as many characters to the HW as possible.
315  *
316  * This function will attempt to stuff of all the characters from the
317  * kernel's transmit buffer into TX BDs.
318  *
319  * A return value of non-zero indicates that it successfully stuffed all
320  * characters from the kernel buffer.
321  *
322  * A return value of zero indicates that there are still characters in the
323  * kernel's buffer that have not been transmitted, but there are no more BDs
324  * available.  This function should be called again after a BD has been made
325  * available.
326  */
327 static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
328 {
329         struct qe_bd *bdp;
330         unsigned char *p;
331         unsigned int count;
332         struct uart_port *port = &qe_port->port;
333         struct circ_buf *xmit = &port->state->xmit;
334
335         bdp = qe_port->rx_cur;
336
337         /* Handle xon/xoff */
338         if (port->x_char) {
339                 /* Pick next descriptor and fill from buffer */
340                 bdp = qe_port->tx_cur;
341
342                 p = qe2cpu_addr(bdp->buf, qe_port);
343
344                 *p++ = port->x_char;
345                 out_be16(&bdp->length, 1);
346                 setbits16(&bdp->status, BD_SC_READY);
347                 /* Get next BD. */
348                 if (in_be16(&bdp->status) & BD_SC_WRAP)
349                         bdp = qe_port->tx_bd_base;
350                 else
351                         bdp++;
352                 qe_port->tx_cur = bdp;
353
354                 port->icount.tx++;
355                 port->x_char = 0;
356                 return 1;
357         }
358
359         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
360                 qe_uart_stop_tx(port);
361                 return 0;
362         }
363
364         /* Pick next descriptor and fill from buffer */
365         bdp = qe_port->tx_cur;
366
367         while (!(in_be16(&bdp->status) & BD_SC_READY) &&
368                (xmit->tail != xmit->head)) {
369                 count = 0;
370                 p = qe2cpu_addr(bdp->buf, qe_port);
371                 while (count < qe_port->tx_fifosize) {
372                         *p++ = xmit->buf[xmit->tail];
373                         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
374                         port->icount.tx++;
375                         count++;
376                         if (xmit->head == xmit->tail)
377                                 break;
378                 }
379
380                 out_be16(&bdp->length, count);
381                 setbits16(&bdp->status, BD_SC_READY);
382
383                 /* Get next BD. */
384                 if (in_be16(&bdp->status) & BD_SC_WRAP)
385                         bdp = qe_port->tx_bd_base;
386                 else
387                         bdp++;
388         }
389         qe_port->tx_cur = bdp;
390
391         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
392                 uart_write_wakeup(port);
393
394         if (uart_circ_empty(xmit)) {
395                 /* The kernel buffer is empty, so turn off TX interrupts.  We
396                    don't need to be told when the QE is finished transmitting
397                    the data. */
398                 qe_uart_stop_tx(port);
399                 return 0;
400         }
401
402         return 1;
403 }
404
405 /*
406  * Start transmitting data
407  *
408  * This function will start transmitting any available data, if the port
409  * isn't already transmitting data.
410  */
411 static void qe_uart_start_tx(struct uart_port *port)
412 {
413         struct uart_qe_port *qe_port =
414                 container_of(port, struct uart_qe_port, port);
415
416         /* If we currently are transmitting, then just return */
417         if (in_be16(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
418                 return;
419
420         /* Otherwise, pump the port and start transmission */
421         if (qe_uart_tx_pump(qe_port))
422                 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
423 }
424
425 /*
426  * Stop transmitting data
427  */
428 static void qe_uart_stop_rx(struct uart_port *port)
429 {
430         struct uart_qe_port *qe_port =
431                 container_of(port, struct uart_qe_port, port);
432
433         clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
434 }
435
436 /*
437  * Enable status change interrupts
438  *
439  * We don't support status change interrupts, but we need to define this
440  * function otherwise the kernel will panic.
441  */
442 static void qe_uart_enable_ms(struct uart_port *port)
443 {
444 }
445
446 /* Start or stop sending  break signal
447  *
448  * This function controls the sending of a break signal.  If break_state=1,
449  * then we start sending a break signal.  If break_state=0, then we stop
450  * sending the break signal.
451  */
452 static void qe_uart_break_ctl(struct uart_port *port, int break_state)
453 {
454         struct uart_qe_port *qe_port =
455                 container_of(port, struct uart_qe_port, port);
456
457         if (break_state)
458                 ucc_slow_stop_tx(qe_port->us_private);
459         else
460                 ucc_slow_restart_tx(qe_port->us_private);
461 }
462
463 /* ISR helper function for receiving character.
464  *
465  * This function is called by the ISR to handling receiving characters
466  */
467 static void qe_uart_int_rx(struct uart_qe_port *qe_port)
468 {
469         int i;
470         unsigned char ch, *cp;
471         struct uart_port *port = &qe_port->port;
472         struct tty_port *tport = &port->state->port;
473         struct tty_struct *tty = tport->tty;
474         struct qe_bd *bdp;
475         u16 status;
476         unsigned int flg;
477
478         /* Just loop through the closed BDs and copy the characters into
479          * the buffer.
480          */
481         bdp = qe_port->rx_cur;
482         while (1) {
483                 status = in_be16(&bdp->status);
484
485                 /* If this one is empty, then we assume we've read them all */
486                 if (status & BD_SC_EMPTY)
487                         break;
488
489                 /* get number of characters, and check space in RX buffer */
490                 i = in_be16(&bdp->length);
491
492                 /* If we don't have enough room in RX buffer for the entire BD,
493                  * then we try later, which will be the next RX interrupt.
494                  */
495                 if (tty_buffer_request_room(tport, i) < i) {
496                         dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
497                         return;
498                 }
499
500                 /* get pointer */
501                 cp = qe2cpu_addr(bdp->buf, qe_port);
502
503                 /* loop through the buffer */
504                 while (i-- > 0) {
505                         ch = *cp++;
506                         port->icount.rx++;
507                         flg = TTY_NORMAL;
508
509                         if (!i && status &
510                             (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
511                                 goto handle_error;
512                         if (uart_handle_sysrq_char(port, ch))
513                                 continue;
514
515 error_return:
516                         tty_insert_flip_char(tty, ch, flg);
517
518                 }
519
520                 /* This BD is ready to be used again. Clear status. get next */
521                 clrsetbits_be16(&bdp->status, BD_SC_BR | BD_SC_FR | BD_SC_PR |
522                         BD_SC_OV | BD_SC_ID, BD_SC_EMPTY);
523                 if (in_be16(&bdp->status) & BD_SC_WRAP)
524                         bdp = qe_port->rx_bd_base;
525                 else
526                         bdp++;
527
528         }
529
530         /* Write back buffer pointer */
531         qe_port->rx_cur = bdp;
532
533         /* Activate BH processing */
534         tty_flip_buffer_push(tty);
535
536         return;
537
538         /* Error processing */
539
540 handle_error:
541         /* Statistics */
542         if (status & BD_SC_BR)
543                 port->icount.brk++;
544         if (status & BD_SC_PR)
545                 port->icount.parity++;
546         if (status & BD_SC_FR)
547                 port->icount.frame++;
548         if (status & BD_SC_OV)
549                 port->icount.overrun++;
550
551         /* Mask out ignored conditions */
552         status &= port->read_status_mask;
553
554         /* Handle the remaining ones */
555         if (status & BD_SC_BR)
556                 flg = TTY_BREAK;
557         else if (status & BD_SC_PR)
558                 flg = TTY_PARITY;
559         else if (status & BD_SC_FR)
560                 flg = TTY_FRAME;
561
562         /* Overrun does not affect the current character ! */
563         if (status & BD_SC_OV)
564                 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
565 #ifdef SUPPORT_SYSRQ
566         port->sysrq = 0;
567 #endif
568         goto error_return;
569 }
570
571 /* Interrupt handler
572  *
573  * This interrupt handler is called after a BD is processed.
574  */
575 static irqreturn_t qe_uart_int(int irq, void *data)
576 {
577         struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
578         struct ucc_slow __iomem *uccp = qe_port->uccp;
579         u16 events;
580
581         /* Clear the interrupts */
582         events = in_be16(&uccp->ucce);
583         out_be16(&uccp->ucce, events);
584
585         if (events & UCC_UART_UCCE_BRKE)
586                 uart_handle_break(&qe_port->port);
587
588         if (events & UCC_UART_UCCE_RX)
589                 qe_uart_int_rx(qe_port);
590
591         if (events & UCC_UART_UCCE_TX)
592                 qe_uart_tx_pump(qe_port);
593
594         return events ? IRQ_HANDLED : IRQ_NONE;
595 }
596
597 /* Initialize buffer descriptors
598  *
599  * This function initializes all of the RX and TX buffer descriptors.
600  */
601 static void qe_uart_initbd(struct uart_qe_port *qe_port)
602 {
603         int i;
604         void *bd_virt;
605         struct qe_bd *bdp;
606
607         /* Set the physical address of the host memory buffers in the buffer
608          * descriptors, and the virtual address for us to work with.
609          */
610         bd_virt = qe_port->bd_virt;
611         bdp = qe_port->rx_bd_base;
612         qe_port->rx_cur = qe_port->rx_bd_base;
613         for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
614                 out_be16(&bdp->status, BD_SC_EMPTY | BD_SC_INTRPT);
615                 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
616                 out_be16(&bdp->length, 0);
617                 bd_virt += qe_port->rx_fifosize;
618                 bdp++;
619         }
620
621         /* */
622         out_be16(&bdp->status, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
623         out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
624         out_be16(&bdp->length, 0);
625
626         /* Set the physical address of the host memory
627          * buffers in the buffer descriptors, and the
628          * virtual address for us to work with.
629          */
630         bd_virt = qe_port->bd_virt +
631                 L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
632         qe_port->tx_cur = qe_port->tx_bd_base;
633         bdp = qe_port->tx_bd_base;
634         for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
635                 out_be16(&bdp->status, BD_SC_INTRPT);
636                 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
637                 out_be16(&bdp->length, 0);
638                 bd_virt += qe_port->tx_fifosize;
639                 bdp++;
640         }
641
642         /* Loopback requires the preamble bit to be set on the first TX BD */
643 #ifdef LOOPBACK
644         setbits16(&qe_port->tx_cur->status, BD_SC_P);
645 #endif
646
647         out_be16(&bdp->status, BD_SC_WRAP | BD_SC_INTRPT);
648         out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
649         out_be16(&bdp->length, 0);
650 }
651
652 /*
653  * Initialize a UCC for UART.
654  *
655  * This function configures a given UCC to be used as a UART device. Basic
656  * UCC initialization is handled in qe_uart_request_port().  This function
657  * does all the UART-specific stuff.
658  */
659 static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
660 {
661         u32 cecr_subblock;
662         struct ucc_slow __iomem *uccp = qe_port->uccp;
663         struct ucc_uart_pram *uccup = qe_port->uccup;
664
665         unsigned int i;
666
667         /* First, disable TX and RX in the UCC */
668         ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
669
670         /* Program the UCC UART parameter RAM */
671         out_8(&uccup->common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
672         out_8(&uccup->common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
673         out_be16(&uccup->common.mrblr, qe_port->rx_fifosize);
674         out_be16(&uccup->maxidl, 0x10);
675         out_be16(&uccup->brkcr, 1);
676         out_be16(&uccup->parec, 0);
677         out_be16(&uccup->frmec, 0);
678         out_be16(&uccup->nosec, 0);
679         out_be16(&uccup->brkec, 0);
680         out_be16(&uccup->uaddr[0], 0);
681         out_be16(&uccup->uaddr[1], 0);
682         out_be16(&uccup->toseq, 0);
683         for (i = 0; i < 8; i++)
684                 out_be16(&uccup->cchars[i], 0xC000);
685         out_be16(&uccup->rccm, 0xc0ff);
686
687         /* Configure the GUMR registers for UART */
688         if (soft_uart) {
689                 /* Soft-UART requires a 1X multiplier for TX */
690                 clrsetbits_be32(&uccp->gumr_l,
691                         UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
692                         UCC_SLOW_GUMR_L_RDCR_MASK,
693                         UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
694                         UCC_SLOW_GUMR_L_RDCR_16);
695
696                 clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
697                         UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
698         } else {
699                 clrsetbits_be32(&uccp->gumr_l,
700                         UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
701                         UCC_SLOW_GUMR_L_RDCR_MASK,
702                         UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
703                         UCC_SLOW_GUMR_L_RDCR_16);
704
705                 clrsetbits_be32(&uccp->gumr_h,
706                         UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
707                         UCC_SLOW_GUMR_H_RFW);
708         }
709
710 #ifdef LOOPBACK
711         clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
712                 UCC_SLOW_GUMR_L_DIAG_LOOP);
713         clrsetbits_be32(&uccp->gumr_h,
714                 UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
715                 UCC_SLOW_GUMR_H_CDS);
716 #endif
717
718         /* Disable rx interrupts  and clear all pending events.  */
719         out_be16(&uccp->uccm, 0);
720         out_be16(&uccp->ucce, 0xffff);
721         out_be16(&uccp->udsr, 0x7e7e);
722
723         /* Initialize UPSMR */
724         out_be16(&uccp->upsmr, 0);
725
726         if (soft_uart) {
727                 out_be16(&uccup->supsmr, 0x30);
728                 out_be16(&uccup->res92, 0);
729                 out_be32(&uccup->rx_state, 0);
730                 out_be32(&uccup->rx_cnt, 0);
731                 out_8(&uccup->rx_bitmark, 0);
732                 out_8(&uccup->rx_length, 10);
733                 out_be32(&uccup->dump_ptr, 0x4000);
734                 out_8(&uccup->rx_temp_dlst_qe, 0);
735                 out_be32(&uccup->rx_frame_rem, 0);
736                 out_8(&uccup->rx_frame_rem_size, 0);
737                 /* Soft-UART requires TX to be 1X */
738                 out_8(&uccup->tx_mode,
739                         UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
740                 out_be16(&uccup->tx_state, 0);
741                 out_8(&uccup->resD4, 0);
742                 out_be16(&uccup->resD5, 0);
743
744                 /* Set UART mode.
745                  * Enable receive and transmit.
746                  */
747
748                 /* From the microcode errata:
749                  * 1.GUMR_L register, set mode=0010 (QMC).
750                  * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
751                  * 3.Set GUMR_H[19:20] (Transparent mode)
752                  * 4.Clear GUMR_H[26] (RFW)
753                  * ...
754                  * 6.Receiver must use 16x over sampling
755                  */
756                 clrsetbits_be32(&uccp->gumr_l,
757                         UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
758                         UCC_SLOW_GUMR_L_RDCR_MASK,
759                         UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
760                         UCC_SLOW_GUMR_L_RDCR_16);
761
762                 clrsetbits_be32(&uccp->gumr_h,
763                         UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
764                         UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
765                         UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
766
767 #ifdef LOOPBACK
768                 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
769                                 UCC_SLOW_GUMR_L_DIAG_LOOP);
770                 clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
771                           UCC_SLOW_GUMR_H_CDS);
772 #endif
773
774                 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
775                 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
776                         QE_CR_PROTOCOL_UNSPECIFIED, 0);
777         } else {
778                 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
779                 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
780                         QE_CR_PROTOCOL_UART, 0);
781         }
782 }
783
784 /*
785  * Initialize the port.
786  */
787 static int qe_uart_startup(struct uart_port *port)
788 {
789         struct uart_qe_port *qe_port =
790                 container_of(port, struct uart_qe_port, port);
791         int ret;
792
793         /*
794          * If we're using Soft-UART mode, then we need to make sure the
795          * firmware has been uploaded first.
796          */
797         if (soft_uart && !firmware_loaded) {
798                 dev_err(port->dev, "Soft-UART firmware not uploaded\n");
799                 return -ENODEV;
800         }
801
802         qe_uart_initbd(qe_port);
803         qe_uart_init_ucc(qe_port);
804
805         /* Install interrupt handler. */
806         ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
807                 qe_port);
808         if (ret) {
809                 dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
810                 return ret;
811         }
812
813         /* Startup rx-int */
814         setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
815         ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
816
817         return 0;
818 }
819
820 /*
821  * Shutdown the port.
822  */
823 static void qe_uart_shutdown(struct uart_port *port)
824 {
825         struct uart_qe_port *qe_port =
826                 container_of(port, struct uart_qe_port, port);
827         struct ucc_slow __iomem *uccp = qe_port->uccp;
828         unsigned int timeout = 20;
829
830         /* Disable RX and TX */
831
832         /* Wait for all the BDs marked sent */
833         while (!qe_uart_tx_empty(port)) {
834                 if (!--timeout) {
835                         dev_warn(port->dev, "shutdown timeout\n");
836                         break;
837                 }
838                 set_current_state(TASK_UNINTERRUPTIBLE);
839                 schedule_timeout(2);
840         }
841
842         if (qe_port->wait_closing) {
843                 /* Wait a bit longer */
844                 set_current_state(TASK_UNINTERRUPTIBLE);
845                 schedule_timeout(qe_port->wait_closing);
846         }
847
848         /* Stop uarts */
849         ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
850         clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
851
852         /* Shut them really down and reinit buffer descriptors */
853         ucc_slow_graceful_stop_tx(qe_port->us_private);
854         qe_uart_initbd(qe_port);
855
856         free_irq(port->irq, qe_port);
857 }
858
859 /*
860  * Set the serial port parameters.
861  */
862 static void qe_uart_set_termios(struct uart_port *port,
863                                 struct ktermios *termios, struct ktermios *old)
864 {
865         struct uart_qe_port *qe_port =
866                 container_of(port, struct uart_qe_port, port);
867         struct ucc_slow __iomem *uccp = qe_port->uccp;
868         unsigned int baud;
869         unsigned long flags;
870         u16 upsmr = in_be16(&uccp->upsmr);
871         struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
872         u16 supsmr = in_be16(&uccup->supsmr);
873         u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
874
875         /* Character length programmed into the mode register is the
876          * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
877          * 1 or 2 stop bits, minus 1.
878          * The value 'bits' counts this for us.
879          */
880
881         /* byte size */
882         upsmr &= UCC_UART_UPSMR_CL_MASK;
883         supsmr &= UCC_UART_SUPSMR_CL_MASK;
884
885         switch (termios->c_cflag & CSIZE) {
886         case CS5:
887                 upsmr |= UCC_UART_UPSMR_CL_5;
888                 supsmr |= UCC_UART_SUPSMR_CL_5;
889                 char_length += 5;
890                 break;
891         case CS6:
892                 upsmr |= UCC_UART_UPSMR_CL_6;
893                 supsmr |= UCC_UART_SUPSMR_CL_6;
894                 char_length += 6;
895                 break;
896         case CS7:
897                 upsmr |= UCC_UART_UPSMR_CL_7;
898                 supsmr |= UCC_UART_SUPSMR_CL_7;
899                 char_length += 7;
900                 break;
901         default:        /* case CS8 */
902                 upsmr |= UCC_UART_UPSMR_CL_8;
903                 supsmr |= UCC_UART_SUPSMR_CL_8;
904                 char_length += 8;
905                 break;
906         }
907
908         /* If CSTOPB is set, we want two stop bits */
909         if (termios->c_cflag & CSTOPB) {
910                 upsmr |= UCC_UART_UPSMR_SL;
911                 supsmr |= UCC_UART_SUPSMR_SL;
912                 char_length++;  /* + SL */
913         }
914
915         if (termios->c_cflag & PARENB) {
916                 upsmr |= UCC_UART_UPSMR_PEN;
917                 supsmr |= UCC_UART_SUPSMR_PEN;
918                 char_length++;  /* + PEN */
919
920                 if (!(termios->c_cflag & PARODD)) {
921                         upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
922                                    UCC_UART_UPSMR_TPM_MASK);
923                         upsmr |= UCC_UART_UPSMR_RPM_EVEN |
924                                 UCC_UART_UPSMR_TPM_EVEN;
925                         supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
926                                     UCC_UART_SUPSMR_TPM_MASK);
927                         supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
928                                 UCC_UART_SUPSMR_TPM_EVEN;
929                 }
930         }
931
932         /*
933          * Set up parity check flag
934          */
935         port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
936         if (termios->c_iflag & INPCK)
937                 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
938         if (termios->c_iflag & (BRKINT | PARMRK))
939                 port->read_status_mask |= BD_SC_BR;
940
941         /*
942          * Characters to ignore
943          */
944         port->ignore_status_mask = 0;
945         if (termios->c_iflag & IGNPAR)
946                 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
947         if (termios->c_iflag & IGNBRK) {
948                 port->ignore_status_mask |= BD_SC_BR;
949                 /*
950                  * If we're ignore parity and break indicators, ignore
951                  * overruns too.  (For real raw support).
952                  */
953                 if (termios->c_iflag & IGNPAR)
954                         port->ignore_status_mask |= BD_SC_OV;
955         }
956         /*
957          * !!! ignore all characters if CREAD is not set
958          */
959         if ((termios->c_cflag & CREAD) == 0)
960                 port->read_status_mask &= ~BD_SC_EMPTY;
961
962         baud = uart_get_baud_rate(port, termios, old, 0, 115200);
963
964         /* Do we really need a spinlock here? */
965         spin_lock_irqsave(&port->lock, flags);
966
967         /* Update the per-port timeout. */
968         uart_update_timeout(port, termios->c_cflag, baud);
969
970         out_be16(&uccp->upsmr, upsmr);
971         if (soft_uart) {
972                 out_be16(&uccup->supsmr, supsmr);
973                 out_8(&uccup->rx_length, char_length);
974
975                 /* Soft-UART requires a 1X multiplier for TX */
976                 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
977                 qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
978         } else {
979                 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
980                 qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
981         }
982
983         spin_unlock_irqrestore(&port->lock, flags);
984 }
985
986 /*
987  * Return a pointer to a string that describes what kind of port this is.
988  */
989 static const char *qe_uart_type(struct uart_port *port)
990 {
991         return "QE";
992 }
993
994 /*
995  * Allocate any memory and I/O resources required by the port.
996  */
997 static int qe_uart_request_port(struct uart_port *port)
998 {
999         int ret;
1000         struct uart_qe_port *qe_port =
1001                 container_of(port, struct uart_qe_port, port);
1002         struct ucc_slow_info *us_info = &qe_port->us_info;
1003         struct ucc_slow_private *uccs;
1004         unsigned int rx_size, tx_size;
1005         void *bd_virt;
1006         dma_addr_t bd_dma_addr = 0;
1007
1008         ret = ucc_slow_init(us_info, &uccs);
1009         if (ret) {
1010                 dev_err(port->dev, "could not initialize UCC%u\n",
1011                        qe_port->ucc_num);
1012                 return ret;
1013         }
1014
1015         qe_port->us_private = uccs;
1016         qe_port->uccp = uccs->us_regs;
1017         qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
1018         qe_port->rx_bd_base = uccs->rx_bd;
1019         qe_port->tx_bd_base = uccs->tx_bd;
1020
1021         /*
1022          * Allocate the transmit and receive data buffers.
1023          */
1024
1025         rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
1026         tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
1027
1028         bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
1029                 GFP_KERNEL);
1030         if (!bd_virt) {
1031                 dev_err(port->dev, "could not allocate buffer descriptors\n");
1032                 return -ENOMEM;
1033         }
1034
1035         qe_port->bd_virt = bd_virt;
1036         qe_port->bd_dma_addr = bd_dma_addr;
1037         qe_port->bd_size = rx_size + tx_size;
1038
1039         qe_port->rx_buf = bd_virt;
1040         qe_port->tx_buf = qe_port->rx_buf + rx_size;
1041
1042         return 0;
1043 }
1044
1045 /*
1046  * Configure the port.
1047  *
1048  * We say we're a CPM-type port because that's mostly true.  Once the device
1049  * is configured, this driver operates almost identically to the CPM serial
1050  * driver.
1051  */
1052 static void qe_uart_config_port(struct uart_port *port, int flags)
1053 {
1054         if (flags & UART_CONFIG_TYPE) {
1055                 port->type = PORT_CPM;
1056                 qe_uart_request_port(port);
1057         }
1058 }
1059
1060 /*
1061  * Release any memory and I/O resources that were allocated in
1062  * qe_uart_request_port().
1063  */
1064 static void qe_uart_release_port(struct uart_port *port)
1065 {
1066         struct uart_qe_port *qe_port =
1067                 container_of(port, struct uart_qe_port, port);
1068         struct ucc_slow_private *uccs = qe_port->us_private;
1069
1070         dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
1071                           qe_port->bd_dma_addr);
1072
1073         ucc_slow_free(uccs);
1074 }
1075
1076 /*
1077  * Verify that the data in serial_struct is suitable for this device.
1078  */
1079 static int qe_uart_verify_port(struct uart_port *port,
1080                                struct serial_struct *ser)
1081 {
1082         if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
1083                 return -EINVAL;
1084
1085         if (ser->irq < 0 || ser->irq >= nr_irqs)
1086                 return -EINVAL;
1087
1088         if (ser->baud_base < 9600)
1089                 return -EINVAL;
1090
1091         return 0;
1092 }
1093 /* UART operations
1094  *
1095  * Details on these functions can be found in Documentation/serial/driver
1096  */
1097 static struct uart_ops qe_uart_pops = {
1098         .tx_empty       = qe_uart_tx_empty,
1099         .set_mctrl      = qe_uart_set_mctrl,
1100         .get_mctrl      = qe_uart_get_mctrl,
1101         .stop_tx        = qe_uart_stop_tx,
1102         .start_tx       = qe_uart_start_tx,
1103         .stop_rx        = qe_uart_stop_rx,
1104         .enable_ms      = qe_uart_enable_ms,
1105         .break_ctl      = qe_uart_break_ctl,
1106         .startup        = qe_uart_startup,
1107         .shutdown       = qe_uart_shutdown,
1108         .set_termios    = qe_uart_set_termios,
1109         .type           = qe_uart_type,
1110         .release_port   = qe_uart_release_port,
1111         .request_port   = qe_uart_request_port,
1112         .config_port    = qe_uart_config_port,
1113         .verify_port    = qe_uart_verify_port,
1114 };
1115
1116 /*
1117  * Obtain the SOC model number and revision level
1118  *
1119  * This function parses the device tree to obtain the SOC model.  It then
1120  * reads the SVR register to the revision.
1121  *
1122  * The device tree stores the SOC model two different ways.
1123  *
1124  * The new way is:
1125  *
1126  *              cpu@0 {
1127  *                      compatible = "PowerPC,8323";
1128  *                      device_type = "cpu";
1129  *                      ...
1130  *
1131  *
1132  * The old way is:
1133  *               PowerPC,8323@0 {
1134  *                      device_type = "cpu";
1135  *                      ...
1136  *
1137  * This code first checks the new way, and then the old way.
1138  */
1139 static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
1140 {
1141         struct device_node *np;
1142         const char *soc_string;
1143         unsigned int svr;
1144         unsigned int soc;
1145
1146         /* Find the CPU node */
1147         np = of_find_node_by_type(NULL, "cpu");
1148         if (!np)
1149                 return 0;
1150         /* Find the compatible property */
1151         soc_string = of_get_property(np, "compatible", NULL);
1152         if (!soc_string)
1153                 /* No compatible property, so try the name. */
1154                 soc_string = np->name;
1155
1156         /* Extract the SOC number from the "PowerPC," string */
1157         if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
1158                 return 0;
1159
1160         /* Get the revision from the SVR */
1161         svr = mfspr(SPRN_SVR);
1162         *rev_h = (svr >> 4) & 0xf;
1163         *rev_l = svr & 0xf;
1164
1165         return soc;
1166 }
1167
1168 /*
1169  * requst_firmware_nowait() callback function
1170  *
1171  * This function is called by the kernel when a firmware is made available,
1172  * or if it times out waiting for the firmware.
1173  */
1174 static void uart_firmware_cont(const struct firmware *fw, void *context)
1175 {
1176         struct qe_firmware *firmware;
1177         struct device *dev = context;
1178         int ret;
1179
1180         if (!fw) {
1181                 dev_err(dev, "firmware not found\n");
1182                 return;
1183         }
1184
1185         firmware = (struct qe_firmware *) fw->data;
1186
1187         if (firmware->header.length != fw->size) {
1188                 dev_err(dev, "invalid firmware\n");
1189                 goto out;
1190         }
1191
1192         ret = qe_upload_firmware(firmware);
1193         if (ret) {
1194                 dev_err(dev, "could not load firmware\n");
1195                 goto out;
1196         }
1197
1198         firmware_loaded = 1;
1199  out:
1200         release_firmware(fw);
1201 }
1202
1203 static int ucc_uart_probe(struct platform_device *ofdev)
1204 {
1205         struct device_node *np = ofdev->dev.of_node;
1206         const unsigned int *iprop;      /* Integer OF properties */
1207         const char *sprop;      /* String OF properties */
1208         struct uart_qe_port *qe_port = NULL;
1209         struct resource res;
1210         int ret;
1211
1212         /*
1213          * Determine if we need Soft-UART mode
1214          */
1215         if (of_find_property(np, "soft-uart", NULL)) {
1216                 dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
1217                 soft_uart = 1;
1218         }
1219
1220         /*
1221          * If we are using Soft-UART, determine if we need to upload the
1222          * firmware, too.
1223          */
1224         if (soft_uart) {
1225                 struct qe_firmware_info *qe_fw_info;
1226
1227                 qe_fw_info = qe_get_firmware_info();
1228
1229                 /* Check if the firmware has been uploaded. */
1230                 if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
1231                         firmware_loaded = 1;
1232                 } else {
1233                         char filename[32];
1234                         unsigned int soc;
1235                         unsigned int rev_h;
1236                         unsigned int rev_l;
1237
1238                         soc = soc_info(&rev_h, &rev_l);
1239                         if (!soc) {
1240                                 dev_err(&ofdev->dev, "unknown CPU model\n");
1241                                 return -ENXIO;
1242                         }
1243                         sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
1244                                 soc, rev_h, rev_l);
1245
1246                         dev_info(&ofdev->dev, "waiting for firmware %s\n",
1247                                 filename);
1248
1249                         /*
1250                          * We call request_firmware_nowait instead of
1251                          * request_firmware so that the driver can load and
1252                          * initialize the ports without holding up the rest of
1253                          * the kernel.  If hotplug support is enabled in the
1254                          * kernel, then we use it.
1255                          */
1256                         ret = request_firmware_nowait(THIS_MODULE,
1257                                 FW_ACTION_HOTPLUG, filename, &ofdev->dev,
1258                                 GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
1259                         if (ret) {
1260                                 dev_err(&ofdev->dev,
1261                                         "could not load firmware %s\n",
1262                                         filename);
1263                                 return ret;
1264                         }
1265                 }
1266         }
1267
1268         qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
1269         if (!qe_port) {
1270                 dev_err(&ofdev->dev, "can't allocate QE port structure\n");
1271                 return -ENOMEM;
1272         }
1273
1274         /* Search for IRQ and mapbase */
1275         ret = of_address_to_resource(np, 0, &res);
1276         if (ret) {
1277                 dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
1278                 goto out_free;
1279         }
1280         if (!res.start) {
1281                 dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
1282                 ret = -EINVAL;
1283                 goto out_free;
1284         }
1285         qe_port->port.mapbase = res.start;
1286
1287         /* Get the UCC number (device ID) */
1288         /* UCCs are numbered 1-7 */
1289         iprop = of_get_property(np, "cell-index", NULL);
1290         if (!iprop) {
1291                 iprop = of_get_property(np, "device-id", NULL);
1292                 if (!iprop) {
1293                         dev_err(&ofdev->dev, "UCC is unspecified in "
1294                                 "device tree\n");
1295                         ret = -EINVAL;
1296                         goto out_free;
1297                 }
1298         }
1299
1300         if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
1301                 dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
1302                 ret = -ENODEV;
1303                 goto out_free;
1304         }
1305         qe_port->ucc_num = *iprop - 1;
1306
1307         /*
1308          * In the future, we should not require the BRG to be specified in the
1309          * device tree.  If no clock-source is specified, then just pick a BRG
1310          * to use.  This requires a new QE library function that manages BRG
1311          * assignments.
1312          */
1313
1314         sprop = of_get_property(np, "rx-clock-name", NULL);
1315         if (!sprop) {
1316                 dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
1317                 ret = -ENODEV;
1318                 goto out_free;
1319         }
1320
1321         qe_port->us_info.rx_clock = qe_clock_source(sprop);
1322         if ((qe_port->us_info.rx_clock < QE_BRG1) ||
1323             (qe_port->us_info.rx_clock > QE_BRG16)) {
1324                 dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
1325                 ret = -ENODEV;
1326                 goto out_free;
1327         }
1328
1329 #ifdef LOOPBACK
1330         /* In internal loopback mode, TX and RX must use the same clock */
1331         qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
1332 #else
1333         sprop = of_get_property(np, "tx-clock-name", NULL);
1334         if (!sprop) {
1335                 dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
1336                 ret = -ENODEV;
1337                 goto out_free;
1338         }
1339         qe_port->us_info.tx_clock = qe_clock_source(sprop);
1340 #endif
1341         if ((qe_port->us_info.tx_clock < QE_BRG1) ||
1342             (qe_port->us_info.tx_clock > QE_BRG16)) {
1343                 dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
1344                 ret = -ENODEV;
1345                 goto out_free;
1346         }
1347
1348         /* Get the port number, numbered 0-3 */
1349         iprop = of_get_property(np, "port-number", NULL);
1350         if (!iprop) {
1351                 dev_err(&ofdev->dev, "missing port-number in device tree\n");
1352                 ret = -EINVAL;
1353                 goto out_free;
1354         }
1355         qe_port->port.line = *iprop;
1356         if (qe_port->port.line >= UCC_MAX_UART) {
1357                 dev_err(&ofdev->dev, "port-number must be 0-%u\n",
1358                         UCC_MAX_UART - 1);
1359                 ret = -EINVAL;
1360                 goto out_free;
1361         }
1362
1363         qe_port->port.irq = irq_of_parse_and_map(np, 0);
1364         if (qe_port->port.irq == 0) {
1365                 dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
1366                        qe_port->ucc_num + 1);
1367                 ret = -EINVAL;
1368                 goto out_free;
1369         }
1370
1371         /*
1372          * Newer device trees have an "fsl,qe" compatible property for the QE
1373          * node, but we still need to support older device trees.
1374          */
1375         np = of_find_compatible_node(NULL, NULL, "fsl,qe");
1376         if (!np) {
1377                 np = of_find_node_by_type(NULL, "qe");
1378                 if (!np) {
1379                         dev_err(&ofdev->dev, "could not find 'qe' node\n");
1380                         ret = -EINVAL;
1381                         goto out_free;
1382                 }
1383         }
1384
1385         iprop = of_get_property(np, "brg-frequency", NULL);
1386         if (!iprop) {
1387                 dev_err(&ofdev->dev,
1388                        "missing brg-frequency in device tree\n");
1389                 ret = -EINVAL;
1390                 goto out_np;
1391         }
1392
1393         if (*iprop)
1394                 qe_port->port.uartclk = *iprop;
1395         else {
1396                 /*
1397                  * Older versions of U-Boot do not initialize the brg-frequency
1398                  * property, so in this case we assume the BRG frequency is
1399                  * half the QE bus frequency.
1400                  */
1401                 iprop = of_get_property(np, "bus-frequency", NULL);
1402                 if (!iprop) {
1403                         dev_err(&ofdev->dev,
1404                                 "missing QE bus-frequency in device tree\n");
1405                         ret = -EINVAL;
1406                         goto out_np;
1407                 }
1408                 if (*iprop)
1409                         qe_port->port.uartclk = *iprop / 2;
1410                 else {
1411                         dev_err(&ofdev->dev,
1412                                 "invalid QE bus-frequency in device tree\n");
1413                         ret = -EINVAL;
1414                         goto out_np;
1415                 }
1416         }
1417
1418         spin_lock_init(&qe_port->port.lock);
1419         qe_port->np = np;
1420         qe_port->port.dev = &ofdev->dev;
1421         qe_port->port.ops = &qe_uart_pops;
1422         qe_port->port.iotype = UPIO_MEM;
1423
1424         qe_port->tx_nrfifos = TX_NUM_FIFO;
1425         qe_port->tx_fifosize = TX_BUF_SIZE;
1426         qe_port->rx_nrfifos = RX_NUM_FIFO;
1427         qe_port->rx_fifosize = RX_BUF_SIZE;
1428
1429         qe_port->wait_closing = UCC_WAIT_CLOSING;
1430         qe_port->port.fifosize = 512;
1431         qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1432
1433         qe_port->us_info.ucc_num = qe_port->ucc_num;
1434         qe_port->us_info.regs = (phys_addr_t) res.start;
1435         qe_port->us_info.irq = qe_port->port.irq;
1436
1437         qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
1438         qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
1439
1440         /* Make sure ucc_slow_init() initializes both TX and RX */
1441         qe_port->us_info.init_tx = 1;
1442         qe_port->us_info.init_rx = 1;
1443
1444         /* Add the port to the uart sub-system.  This will cause
1445          * qe_uart_config_port() to be called, so the us_info structure must
1446          * be initialized.
1447          */
1448         ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
1449         if (ret) {
1450                 dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
1451                        qe_port->port.line);
1452                 goto out_np;
1453         }
1454
1455         dev_set_drvdata(&ofdev->dev, qe_port);
1456
1457         dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
1458                 qe_port->ucc_num + 1, qe_port->port.line);
1459
1460         /* Display the mknod command for this device */
1461         dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1462                qe_port->port.line, SERIAL_QE_MAJOR,
1463                SERIAL_QE_MINOR + qe_port->port.line);
1464
1465         return 0;
1466 out_np:
1467         of_node_put(np);
1468 out_free:
1469         kfree(qe_port);
1470         return ret;
1471 }
1472
1473 static int ucc_uart_remove(struct platform_device *ofdev)
1474 {
1475         struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
1476
1477         dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
1478
1479         uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
1480
1481         dev_set_drvdata(&ofdev->dev, NULL);
1482         kfree(qe_port);
1483
1484         return 0;
1485 }
1486
1487 static struct of_device_id ucc_uart_match[] = {
1488         {
1489                 .type = "serial",
1490                 .compatible = "ucc_uart",
1491         },
1492         {},
1493 };
1494 MODULE_DEVICE_TABLE(of, ucc_uart_match);
1495
1496 static struct platform_driver ucc_uart_of_driver = {
1497         .driver = {
1498                 .name = "ucc_uart",
1499                 .owner = THIS_MODULE,
1500                 .of_match_table    = ucc_uart_match,
1501         },
1502         .probe          = ucc_uart_probe,
1503         .remove         = ucc_uart_remove,
1504 };
1505
1506 static int __init ucc_uart_init(void)
1507 {
1508         int ret;
1509
1510         printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
1511 #ifdef LOOPBACK
1512         printk(KERN_INFO "ucc-uart: Using loopback mode\n");
1513 #endif
1514
1515         ret = uart_register_driver(&ucc_uart_driver);
1516         if (ret) {
1517                 printk(KERN_ERR "ucc-uart: could not register UART driver\n");
1518                 return ret;
1519         }
1520
1521         ret = platform_driver_register(&ucc_uart_of_driver);
1522         if (ret)
1523                 printk(KERN_ERR
1524                        "ucc-uart: could not register platform driver\n");
1525
1526         return ret;
1527 }
1528
1529 static void __exit ucc_uart_exit(void)
1530 {
1531         printk(KERN_INFO
1532                "Freescale QUICC Engine UART device driver unloading\n");
1533
1534         platform_driver_unregister(&ucc_uart_of_driver);
1535         uart_unregister_driver(&ucc_uart_driver);
1536 }
1537
1538 module_init(ucc_uart_init);
1539 module_exit(ucc_uart_exit);
1540
1541 MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
1542 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1543 MODULE_LICENSE("GPL v2");
1544 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);
1545