blob: 25efca5a7a1ffe423b0757f2a89f660390537e20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Kumar Gala4c8d3d92005-11-13 16:06:30 -080010 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
Kumar Gala311c4622005-08-09 10:08:00 -070012 *
Scott Wood7ae87032007-07-17 17:59:06 -050013 * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * (C) 2004 Intracom, S.A.
Vitaly Bordug6e1976962006-04-29 23:06:00 +040015 * (C) 2005-2006 MontaVista Software, Inc.
Kumar Gala0d844062008-06-12 07:53:48 -050016 * Vitaly Bordug <vbordug@ru.mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 *
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/tty.h>
36#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/serial.h>
39#include <linux/console.h>
40#include <linux/sysrq.h>
41#include <linux/device.h>
42#include <linux/bootmem.h>
43#include <linux/dma-mapping.h>
Vitaly Borduge27987c2006-04-25 20:26:41 +040044#include <linux/fs_uart_pd.h>
Kumar Gala0b2a2e52008-06-12 08:05:09 -050045#include <linux/of_platform.h>
Laurent Pinchart7485d262008-07-24 18:36:37 +020046#include <linux/gpio.h>
47#include <linux/of_gpio.h>
Laurent Pinchart80776552008-07-28 10:42:16 +020048#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/io.h>
51#include <asm/irq.h>
52#include <asm/delay.h>
Vitaly Bordug3dd0dcb2006-09-21 17:27:15 +040053#include <asm/fs_pd.h>
Scott Wood7ae87032007-07-17 17:59:06 -050054#include <asm/udbg.h>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57#define SUPPORT_SYSRQ
58#endif
59
60#include <linux/serial_core.h>
61#include <linux/kernel.h>
62
63#include "cpm_uart.h"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/**************************************************************/
67
68static int cpm_uart_tx_pump(struct uart_port *port);
69static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
70static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
71static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
72
73/**************************************************************/
74
75/*
Kumar Gala311c4622005-08-09 10:08:00 -070076 * Check, if transmit buffers are processed
Linus Torvalds1da177e2005-04-16 15:20:36 -070077*/
78static unsigned int cpm_uart_tx_empty(struct uart_port *port)
79{
80 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -050081 cbd_t __iomem *bdp = pinfo->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int ret = 0;
83
84 while (1) {
Scott Woodc1dcfd92007-07-24 15:53:07 -050085 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
87
Scott Woodc1dcfd92007-07-24 15:53:07 -050088 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ret = TIOCSER_TEMT;
90 break;
91 }
92 bdp++;
93 }
94
95 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
96
97 return ret;
98}
99
100static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
101{
Laurent Pinchart7485d262008-07-24 18:36:37 +0200102 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
103
104 if (pinfo->gpios[GPIO_RTS] >= 0)
105 gpio_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
106
107 if (pinfo->gpios[GPIO_DTR] >= 0)
108 gpio_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
112{
Laurent Pinchart7485d262008-07-24 18:36:37 +0200113 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
114 unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
115
116 if (pinfo->gpios[GPIO_CTS] >= 0) {
117 if (gpio_get_value(pinfo->gpios[GPIO_CTS]))
118 mctrl &= ~TIOCM_CTS;
119 }
120
121 if (pinfo->gpios[GPIO_DSR] >= 0) {
122 if (gpio_get_value(pinfo->gpios[GPIO_DSR]))
123 mctrl &= ~TIOCM_DSR;
124 }
125
126 if (pinfo->gpios[GPIO_DCD] >= 0) {
127 if (gpio_get_value(pinfo->gpios[GPIO_DCD]))
128 mctrl &= ~TIOCM_CAR;
129 }
130
131 if (pinfo->gpios[GPIO_RI] >= 0) {
132 if (!gpio_get_value(pinfo->gpios[GPIO_RI]))
133 mctrl |= TIOCM_RNG;
134 }
135
136 return mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/*
140 * Stop transmitter
141 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100142static void cpm_uart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500145 smc_t __iomem *smcp = pinfo->smcp;
146 scc_t __iomem *sccp = pinfo->sccp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 pr_debug("CPM uart[%d]:stop tx\n", port->line);
149
150 if (IS_SMC(pinfo))
Scott Woodc1dcfd92007-07-24 15:53:07 -0500151 clrbits8(&smcp->smc_smcm, SMCM_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 else
Scott Woodc1dcfd92007-07-24 15:53:07 -0500153 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/*
157 * Start transmitter
158 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100159static void cpm_uart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500162 smc_t __iomem *smcp = pinfo->smcp;
163 scc_t __iomem *sccp = pinfo->sccp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 pr_debug("CPM uart[%d]:start tx\n", port->line);
166
167 if (IS_SMC(pinfo)) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500168 if (in_8(&smcp->smc_smcm) & SMCM_TX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return;
170 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500171 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return;
173 }
174
175 if (cpm_uart_tx_pump(port) != 0) {
Kumar Gala311c4622005-08-09 10:08:00 -0700176 if (IS_SMC(pinfo)) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500177 setbits8(&smcp->smc_smcm, SMCM_TX);
Kumar Gala311c4622005-08-09 10:08:00 -0700178 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500179 setbits16(&sccp->scc_sccm, UART_SCCM_TX);
Kumar Gala311c4622005-08-09 10:08:00 -0700180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182}
183
184/*
Kumar Gala311c4622005-08-09 10:08:00 -0700185 * Stop receiver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
187static void cpm_uart_stop_rx(struct uart_port *port)
188{
189 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500190 smc_t __iomem *smcp = pinfo->smcp;
191 scc_t __iomem *sccp = pinfo->sccp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 pr_debug("CPM uart[%d]:stop rx\n", port->line);
194
195 if (IS_SMC(pinfo))
Scott Woodc1dcfd92007-07-24 15:53:07 -0500196 clrbits8(&smcp->smc_smcm, SMCM_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 else
Scott Woodc1dcfd92007-07-24 15:53:07 -0500198 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201/*
202 * Enable Modem status interrupts
203 */
204static void cpm_uart_enable_ms(struct uart_port *port)
205{
206 pr_debug("CPM uart[%d]:enable ms\n", port->line);
207}
208
209/*
Kumar Gala311c4622005-08-09 10:08:00 -0700210 * Generate a break.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
212static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
213{
214 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
217 break_state);
218
219 if (break_state)
Scott Wood7ae87032007-07-17 17:59:06 -0500220 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 else
Scott Wood7ae87032007-07-17 17:59:06 -0500222 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
226 * Transmit characters, refill buffer descriptor, if possible
227 */
David Howells7d12e782006-10-05 14:55:46 +0100228static void cpm_uart_int_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 pr_debug("CPM uart[%d]:TX INT\n", port->line);
231
232 cpm_uart_tx_pump(port);
233}
234
Jason Wessel8e21d042008-07-23 11:30:16 -0500235#ifdef CONFIG_CONSOLE_POLL
236static int serial_polled;
237#endif
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Receive characters
241 */
David Howells7d12e782006-10-05 14:55:46 +0100242static void cpm_uart_int_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int i;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500245 unsigned char ch;
246 u8 *cp;
Alan Coxf10140f2008-07-22 15:25:07 +0100247 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500249 cbd_t __iomem *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u16 status;
251 unsigned int flg;
252
253 pr_debug("CPM uart[%d]:RX INT\n", port->line);
254
255 /* Just loop through the closed BDs and copy the characters into
256 * the buffer.
257 */
258 bdp = pinfo->rx_cur;
259 for (;;) {
Jason Wessel8e21d042008-07-23 11:30:16 -0500260#ifdef CONFIG_CONSOLE_POLL
261 if (unlikely(serial_polled)) {
262 serial_polled = 0;
263 return;
264 }
265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* get status */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500267 status = in_be16(&bdp->cbd_sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* If this one is empty, return happy */
269 if (status & BD_SC_EMPTY)
270 break;
271
272 /* get number of characters, and check spce in flip-buffer */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500273 i = in_be16(&bdp->cbd_datlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Kumar Gala311c4622005-08-09 10:08:00 -0700275 /* If we have not enough room in tty flip buffer, then we try
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * later, which will be the next rx-interrupt or a timeout
277 */
Vitaly Bordug76a55432006-02-08 21:40:13 +0000278 if(tty_buffer_request_room(tty, i) < i) {
279 printk(KERN_WARNING "No room in flip buffer\n");
280 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 /* get pointer */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500284 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* loop through the buffer */
287 while (i-- > 0) {
288 ch = *cp++;
289 port->icount.rx++;
290 flg = TTY_NORMAL;
291
292 if (status &
293 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
294 goto handle_error;
David Howells7d12e782006-10-05 14:55:46 +0100295 if (uart_handle_sysrq_char(port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 continue;
Jason Wessel8e21d042008-07-23 11:30:16 -0500297#ifdef CONFIG_CONSOLE_POLL
298 if (unlikely(serial_polled)) {
299 serial_polled = 0;
300 return;
301 }
302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 error_return:
Vitaly Bordug76a55432006-02-08 21:40:13 +0000304 tty_insert_flip_char(tty, ch, flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 } /* End while (i--) */
307
308 /* This BD is ready to be used again. Clear status. get next */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500309 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
310 BD_SC_OV | BD_SC_ID);
311 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Scott Woodc1dcfd92007-07-24 15:53:07 -0500313 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 bdp = pinfo->rx_bd_base;
315 else
316 bdp++;
Kumar Gala311c4622005-08-09 10:08:00 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } /* End for (;;) */
319
320 /* Write back buffer pointer */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500321 pinfo->rx_cur = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* activate BH processing */
324 tty_flip_buffer_push(tty);
325
326 return;
327
328 /* Error processing */
329
330 handle_error:
331 /* Statistics */
332 if (status & BD_SC_BR)
333 port->icount.brk++;
334 if (status & BD_SC_PR)
335 port->icount.parity++;
336 if (status & BD_SC_FR)
337 port->icount.frame++;
338 if (status & BD_SC_OV)
339 port->icount.overrun++;
340
341 /* Mask out ignored conditions */
342 status &= port->read_status_mask;
343
344 /* Handle the remaining ones */
345 if (status & BD_SC_BR)
346 flg = TTY_BREAK;
347 else if (status & BD_SC_PR)
348 flg = TTY_PARITY;
349 else if (status & BD_SC_FR)
350 flg = TTY_FRAME;
351
352 /* overrun does not affect the current character ! */
353 if (status & BD_SC_OV) {
354 ch = 0;
355 flg = TTY_OVERRUN;
356 /* We skip this buffer */
357 /* CHECK: Is really nothing senseful there */
358 /* ASSUMPTION: it contains nothing valid */
359 i = 0;
360 }
361#ifdef SUPPORT_SYSRQ
362 port->sysrq = 0;
363#endif
364 goto error_return;
365}
366
367/*
368 * Asynchron mode interrupt handler
369 */
David Howells7d12e782006-10-05 14:55:46 +0100370static irqreturn_t cpm_uart_int(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 u8 events;
Jeff Garzik15aafa22008-02-06 01:36:20 -0800373 struct uart_port *port = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500375 smc_t __iomem *smcp = pinfo->smcp;
376 scc_t __iomem *sccp = pinfo->sccp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 pr_debug("CPM uart[%d]:IRQ\n", port->line);
379
380 if (IS_SMC(pinfo)) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500381 events = in_8(&smcp->smc_smce);
382 out_8(&smcp->smc_smce, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (events & SMCM_BRKE)
384 uart_handle_break(port);
385 if (events & SMCM_RX)
David Howells7d12e782006-10-05 14:55:46 +0100386 cpm_uart_int_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (events & SMCM_TX)
David Howells7d12e782006-10-05 14:55:46 +0100388 cpm_uart_int_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500390 events = in_be16(&sccp->scc_scce);
391 out_be16(&sccp->scc_scce, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (events & UART_SCCM_BRKE)
393 uart_handle_break(port);
394 if (events & UART_SCCM_RX)
David Howells7d12e782006-10-05 14:55:46 +0100395 cpm_uart_int_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (events & UART_SCCM_TX)
David Howells7d12e782006-10-05 14:55:46 +0100397 cpm_uart_int_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 return (events) ? IRQ_HANDLED : IRQ_NONE;
400}
401
402static int cpm_uart_startup(struct uart_port *port)
403{
404 int retval;
405 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
406
407 pr_debug("CPM uart[%d]:startup\n", port->line);
408
409 /* Install interrupt handler. */
410 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
411 if (retval)
412 return retval;
413
414 /* Startup rx-int */
415 if (IS_SMC(pinfo)) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500416 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
417 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500419 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
420 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Kumar Gala311c4622005-08-09 10:08:00 -0700423 if (!(pinfo->flags & FLAG_CONSOLE))
Scott Wood7ae87032007-07-17 17:59:06 -0500424 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426}
427
Kumar Gala311c4622005-08-09 10:08:00 -0700428inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
429{
Kumar Gala638861d2005-09-03 15:55:37 -0700430 set_current_state(TASK_UNINTERRUPTIBLE);
431 schedule_timeout(pinfo->wait_closing);
Kumar Gala311c4622005-08-09 10:08:00 -0700432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Shutdown the uart
436 */
437static void cpm_uart_shutdown(struct uart_port *port)
438{
439 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 pr_debug("CPM uart[%d]:shutdown\n", port->line);
442
443 /* free interrupt handler */
444 free_irq(port->irq, port);
445
446 /* If the port is not the console, disable Rx and Tx. */
447 if (!(pinfo->flags & FLAG_CONSOLE)) {
Kumar Gala311c4622005-08-09 10:08:00 -0700448 /* Wait for all the BDs marked sent */
Kumar Gala638861d2005-09-03 15:55:37 -0700449 while(!cpm_uart_tx_empty(port)) {
450 set_current_state(TASK_UNINTERRUPTIBLE);
Kumar Gala311c4622005-08-09 10:08:00 -0700451 schedule_timeout(2);
Kumar Gala638861d2005-09-03 15:55:37 -0700452 }
453
454 if (pinfo->wait_closing)
Kumar Gala311c4622005-08-09 10:08:00 -0700455 cpm_uart_wait_until_send(pinfo);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Stop uarts */
458 if (IS_SMC(pinfo)) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500459 smc_t __iomem *smcp = pinfo->smcp;
460 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
461 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500463 scc_t __iomem *sccp = pinfo->sccp;
464 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
465 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 /* Shut them really down and reinit buffer descriptors */
Nye Liuae2d4c32008-07-23 21:29:50 -0700469 if (IS_SMC(pinfo)) {
470 out_be16(&pinfo->smcup->smc_brkcr, 0);
Scott Wood7ae87032007-07-17 17:59:06 -0500471 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
Nye Liuae2d4c32008-07-23 21:29:50 -0700472 } else {
473 out_be16(&pinfo->sccup->scc_brkcr, 0);
Scott Wood7ae87032007-07-17 17:59:06 -0500474 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
Nye Liuae2d4c32008-07-23 21:29:50 -0700475 }
Vitaly Bordug61f56572006-04-29 22:32:44 +0400476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 cpm_uart_initbd(pinfo);
478 }
479}
480
481static void cpm_uart_set_termios(struct uart_port *port,
Scott Wood1bda8f32007-05-08 12:19:21 -0500482 struct ktermios *termios,
483 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 int baud;
486 unsigned long flags;
487 u16 cval, scval, prev_mode;
488 int bits, sbits;
489 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500490 smc_t __iomem *smcp = pinfo->smcp;
491 scc_t __iomem *sccp = pinfo->sccp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 pr_debug("CPM uart[%d]:set_termios\n", port->line);
494
495 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
496
497 /* Character length programmed into the mode register is the
498 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
499 * 1 or 2 stop bits, minus 1.
500 * The value 'bits' counts this for us.
501 */
502 cval = 0;
503 scval = 0;
504
505 /* byte size */
506 switch (termios->c_cflag & CSIZE) {
507 case CS5:
508 bits = 5;
509 break;
510 case CS6:
511 bits = 6;
512 break;
513 case CS7:
514 bits = 7;
515 break;
516 case CS8:
517 bits = 8;
518 break;
519 /* Never happens, but GCC is too dumb to figure it out */
520 default:
521 bits = 8;
522 break;
523 }
524 sbits = bits - 5;
525
526 if (termios->c_cflag & CSTOPB) {
527 cval |= SMCMR_SL; /* Two stops */
528 scval |= SCU_PSMR_SL;
529 bits++;
530 }
531
532 if (termios->c_cflag & PARENB) {
533 cval |= SMCMR_PEN;
534 scval |= SCU_PSMR_PEN;
535 bits++;
536 if (!(termios->c_cflag & PARODD)) {
537 cval |= SMCMR_PM_EVEN;
538 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
539 }
540 }
541
542 /*
Laurent Pinchartdc320812008-07-02 10:58:45 +0200543 * Update the timeout
544 */
545 uart_update_timeout(port, termios->c_cflag, baud);
546
547 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * Set up parity check flag
549 */
550#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
551
552 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
553 if (termios->c_iflag & INPCK)
554 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
555 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
556 port->read_status_mask |= BD_SC_BR;
557
558 /*
559 * Characters to ignore
560 */
561 port->ignore_status_mask = 0;
562 if (termios->c_iflag & IGNPAR)
563 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
564 if (termios->c_iflag & IGNBRK) {
565 port->ignore_status_mask |= BD_SC_BR;
566 /*
567 * If we're ignore parity and break indicators, ignore
568 * overruns too. (For real raw support).
569 */
570 if (termios->c_iflag & IGNPAR)
571 port->ignore_status_mask |= BD_SC_OV;
572 }
573 /*
574 * !!! ignore all characters if CREAD is not set
575 */
576 if ((termios->c_cflag & CREAD) == 0)
577 port->read_status_mask &= ~BD_SC_EMPTY;
Kumar Gala311c4622005-08-09 10:08:00 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 spin_lock_irqsave(&port->lock, flags);
580
581 /* Start bit has not been added (so don't, because we would just
582 * subtract it later), and we need to add one for the number of
583 * stops bits (there is always at least one).
584 */
585 bits++;
586 if (IS_SMC(pinfo)) {
587 /* Set the mode register. We want to keep a copy of the
588 * enables, because we want to put them back if they were
589 * present.
590 */
Nye Liuae2d4c32008-07-23 21:29:50 -0700591 prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
592 /* Output in *one* operation, so we don't interrupt RX/TX if they
593 * were already enabled. */
594 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
595 SMCMR_SM_UART | prev_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500597 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Laurent Pinchart80776552008-07-28 10:42:16 +0200600 if (pinfo->clk)
601 clk_set_rate(pinfo->clk, baud);
602 else
603 cpm_set_brg(pinfo->brg - 1, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static const char *cpm_uart_type(struct uart_port *port)
608{
609 pr_debug("CPM uart[%d]:uart_type\n", port->line);
610
611 return port->type == PORT_CPM ? "CPM UART" : NULL;
612}
613
614/*
615 * verify the new serial_struct (for TIOCSSERIAL).
616 */
617static int cpm_uart_verify_port(struct uart_port *port,
618 struct serial_struct *ser)
619{
620 int ret = 0;
621
622 pr_debug("CPM uart[%d]:verify_port\n", port->line);
623
624 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
625 ret = -EINVAL;
626 if (ser->irq < 0 || ser->irq >= NR_IRQS)
627 ret = -EINVAL;
628 if (ser->baud_base < 9600)
629 ret = -EINVAL;
630 return ret;
631}
632
633/*
634 * Transmit characters, refill buffer descriptor, if possible
635 */
636static int cpm_uart_tx_pump(struct uart_port *port)
637{
Scott Woodc1dcfd92007-07-24 15:53:07 -0500638 cbd_t __iomem *bdp;
639 u8 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int count;
641 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
642 struct circ_buf *xmit = &port->info->xmit;
643
644 /* Handle xon/xoff */
645 if (port->x_char) {
646 /* Pick next descriptor and fill from buffer */
647 bdp = pinfo->tx_cur;
648
Scott Woodc1dcfd92007-07-24 15:53:07 -0500649 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
Kumar Gala311c4622005-08-09 10:08:00 -0700650
Aristeu Sergio Rozanski Filho03929c72005-12-29 19:18:49 -0200651 *p++ = port->x_char;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500652
653 out_be16(&bdp->cbd_datlen, 1);
654 setbits16(&bdp->cbd_sc, BD_SC_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* Get next BD. */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500656 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 bdp = pinfo->tx_bd_base;
658 else
659 bdp++;
660 pinfo->tx_cur = bdp;
661
662 port->icount.tx++;
663 port->x_char = 0;
664 return 1;
665 }
666
667 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100668 cpm_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 0;
670 }
671
672 /* Pick next descriptor and fill from buffer */
673 bdp = pinfo->tx_cur;
674
Scott Woodc1dcfd92007-07-24 15:53:07 -0500675 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
676 xmit->tail != xmit->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 count = 0;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500678 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 while (count < pinfo->tx_fifosize) {
680 *p++ = xmit->buf[xmit->tail];
681 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
682 port->icount.tx++;
683 count++;
684 if (xmit->head == xmit->tail)
685 break;
686 }
Scott Woodc1dcfd92007-07-24 15:53:07 -0500687 out_be16(&bdp->cbd_datlen, count);
688 setbits16(&bdp->cbd_sc, BD_SC_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Get next BD. */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500690 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 bdp = pinfo->tx_bd_base;
692 else
693 bdp++;
694 }
695 pinfo->tx_cur = bdp;
696
697 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
698 uart_write_wakeup(port);
699
700 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100701 cpm_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703 }
704
705 return 1;
706}
707
708/*
709 * init buffer descriptors
710 */
711static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
712{
713 int i;
714 u8 *mem_addr;
Scott Woodc1dcfd92007-07-24 15:53:07 -0500715 cbd_t __iomem *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
718
719 /* Set the physical address of the host memory
720 * buffers in the buffer descriptors, and the
721 * virtual address for us to work with.
722 */
723 mem_addr = pinfo->mem_addr;
724 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
725 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500726 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
727 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mem_addr += pinfo->rx_fifosize;
729 }
Kumar Gala311c4622005-08-09 10:08:00 -0700730
Scott Woodc1dcfd92007-07-24 15:53:07 -0500731 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
732 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* Set the physical address of the host memory
735 * buffers in the buffer descriptors, and the
736 * virtual address for us to work with.
737 */
738 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
739 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
740 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500741 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
742 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 mem_addr += pinfo->tx_fifosize;
744 }
Kumar Gala311c4622005-08-09 10:08:00 -0700745
Scott Woodc1dcfd92007-07-24 15:53:07 -0500746 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
747 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
751{
Scott Woodc1dcfd92007-07-24 15:53:07 -0500752 scc_t __iomem *scp;
753 scc_uart_t __iomem *sup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
756
757 scp = pinfo->sccp;
758 sup = pinfo->sccup;
759
760 /* Store address */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500761 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
762 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
763 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
764 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* Set up the uart parameters in the
767 * parameter ram.
768 */
769
770 cpm_set_scc_fcr(sup);
771
Scott Woodc1dcfd92007-07-24 15:53:07 -0500772 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
773 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
774 out_be16(&sup->scc_brkcr, 1);
775 out_be16(&sup->scc_parec, 0);
776 out_be16(&sup->scc_frmec, 0);
777 out_be16(&sup->scc_nosec, 0);
778 out_be16(&sup->scc_brkec, 0);
779 out_be16(&sup->scc_uaddr1, 0);
780 out_be16(&sup->scc_uaddr2, 0);
781 out_be16(&sup->scc_toseq, 0);
782 out_be16(&sup->scc_char1, 0x8000);
783 out_be16(&sup->scc_char2, 0x8000);
784 out_be16(&sup->scc_char3, 0x8000);
785 out_be16(&sup->scc_char4, 0x8000);
786 out_be16(&sup->scc_char5, 0x8000);
787 out_be16(&sup->scc_char6, 0x8000);
788 out_be16(&sup->scc_char7, 0x8000);
789 out_be16(&sup->scc_char8, 0x8000);
790 out_be16(&sup->scc_rccm, 0xc0ff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* Send the CPM an initialize command.
793 */
Scott Wood7ae87032007-07-17 17:59:06 -0500794 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* Set UART mode, 8 bit, no parity, one stop.
797 * Enable receive and transmit.
798 */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500799 out_be32(&scp->scc_gsmrh, 0);
800 out_be32(&scp->scc_gsmrl,
801 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /* Enable rx interrupts and clear all pending events. */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500804 out_be16(&scp->scc_sccm, 0);
805 out_be16(&scp->scc_scce, 0xffff);
806 out_be16(&scp->scc_dsr, 0x7e7e);
807 out_be16(&scp->scc_psmr, 0x3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Scott Woodc1dcfd92007-07-24 15:53:07 -0500809 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
813{
Scott Woodc1dcfd92007-07-24 15:53:07 -0500814 smc_t __iomem *sp;
815 smc_uart_t __iomem *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
818
819 sp = pinfo->smcp;
820 up = pinfo->smcup;
821
822 /* Store address */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500823 out_be16(&pinfo->smcup->smc_rbase,
824 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
825 out_be16(&pinfo->smcup->smc_tbase,
826 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828/*
829 * In case SMC1 is being relocated...
830 */
831#if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
Scott Woodc1dcfd92007-07-24 15:53:07 -0500832 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
833 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
834 out_be32(&up->smc_rstate, 0);
835 out_be32(&up->smc_tstate, 0);
836 out_be16(&up->smc_brkcr, 1); /* number of break chars */
837 out_be16(&up->smc_brkec, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838#endif
839
840 /* Set up the uart parameters in the
841 * parameter ram.
842 */
843 cpm_set_smc_fcr(up);
844
845 /* Using idle charater time requires some additional tuning. */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500846 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
847 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
848 out_be16(&up->smc_brklen, 0);
849 out_be16(&up->smc_brkec, 0);
850 out_be16(&up->smc_brkcr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Scott Wood7ae87032007-07-17 17:59:06 -0500852 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* Set UART mode, 8 bit, no parity, one stop.
855 * Enable receive and transmit.
856 */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500857 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* Enable only rx interrupts clear all pending events. */
Scott Woodc1dcfd92007-07-24 15:53:07 -0500860 out_8(&sp->smc_smcm, 0);
861 out_8(&sp->smc_smce, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Scott Woodc1dcfd92007-07-24 15:53:07 -0500863 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866/*
867 * Initialize port. This is called from early_console stuff
868 * so we have to be careful here !
869 */
870static int cpm_uart_request_port(struct uart_port *port)
871{
872 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
873 int ret;
874
875 pr_debug("CPM uart[%d]:request port\n", port->line);
876
877 if (pinfo->flags & FLAG_CONSOLE)
878 return 0;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (IS_SMC(pinfo)) {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500881 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
882 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 } else {
Scott Woodc1dcfd92007-07-24 15:53:07 -0500884 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
885 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 ret = cpm_uart_allocbuf(pinfo, 0);
889
890 if (ret)
891 return ret;
892
893 cpm_uart_initbd(pinfo);
Kumar Gala311c4622005-08-09 10:08:00 -0700894 if (IS_SMC(pinfo))
895 cpm_uart_init_smc(pinfo);
896 else
897 cpm_uart_init_scc(pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return 0;
900}
901
902static void cpm_uart_release_port(struct uart_port *port)
903{
904 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
905
906 if (!(pinfo->flags & FLAG_CONSOLE))
907 cpm_uart_freebuf(pinfo);
908}
909
910/*
911 * Configure/autoconfigure the port.
912 */
913static void cpm_uart_config_port(struct uart_port *port, int flags)
914{
915 pr_debug("CPM uart[%d]:config_port\n", port->line);
916
917 if (flags & UART_CONFIG_TYPE) {
918 port->type = PORT_CPM;
919 cpm_uart_request_port(port);
920 }
921}
Jason Wessel8e21d042008-07-23 11:30:16 -0500922
923#ifdef CONFIG_CONSOLE_POLL
924/* Serial polling routines for writing and reading from the uart while
925 * in an interrupt or debug context.
926 */
927
928#define GDB_BUF_SIZE 512 /* power of 2, please */
929
930static char poll_buf[GDB_BUF_SIZE];
931static char *pollp;
932static int poll_chars;
933
934static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
935{
936 u_char c, *cp;
937 volatile cbd_t *bdp;
938 int i;
939
940 /* Get the address of the host memory buffer.
941 */
942 bdp = pinfo->rx_cur;
943 while (bdp->cbd_sc & BD_SC_EMPTY)
944 ;
945
946 /* If the buffer address is in the CPM DPRAM, don't
947 * convert it.
948 */
949 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
950
951 if (obuf) {
952 i = c = bdp->cbd_datlen;
953 while (i-- > 0)
954 *obuf++ = *cp++;
955 } else
956 c = *cp;
957 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
958 bdp->cbd_sc |= BD_SC_EMPTY;
959
960 if (bdp->cbd_sc & BD_SC_WRAP)
961 bdp = pinfo->rx_bd_base;
962 else
963 bdp++;
964 pinfo->rx_cur = (cbd_t *)bdp;
965
966 return (int)c;
967}
968
969static int cpm_get_poll_char(struct uart_port *port)
970{
971 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
972
973 if (!serial_polled) {
974 serial_polled = 1;
975 poll_chars = 0;
976 }
977 if (poll_chars <= 0) {
978 poll_chars = poll_wait_key(poll_buf, pinfo);
979 pollp = poll_buf;
980 }
981 poll_chars--;
982 return *pollp++;
983}
984
985static void cpm_put_poll_char(struct uart_port *port,
986 unsigned char c)
987{
988 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
989 static char ch[2];
990
991 ch[0] = (char)c;
992 cpm_uart_early_write(pinfo->port.line, ch, 1);
993}
994#endif /* CONFIG_CONSOLE_POLL */
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static struct uart_ops cpm_uart_pops = {
997 .tx_empty = cpm_uart_tx_empty,
998 .set_mctrl = cpm_uart_set_mctrl,
999 .get_mctrl = cpm_uart_get_mctrl,
1000 .stop_tx = cpm_uart_stop_tx,
1001 .start_tx = cpm_uart_start_tx,
1002 .stop_rx = cpm_uart_stop_rx,
1003 .enable_ms = cpm_uart_enable_ms,
1004 .break_ctl = cpm_uart_break_ctl,
1005 .startup = cpm_uart_startup,
1006 .shutdown = cpm_uart_shutdown,
1007 .set_termios = cpm_uart_set_termios,
1008 .type = cpm_uart_type,
1009 .release_port = cpm_uart_release_port,
1010 .request_port = cpm_uart_request_port,
1011 .config_port = cpm_uart_config_port,
1012 .verify_port = cpm_uart_verify_port,
Jason Wessel8e21d042008-07-23 11:30:16 -05001013#ifdef CONFIG_CONSOLE_POLL
1014 .poll_get_char = cpm_get_poll_char,
1015 .poll_put_char = cpm_put_poll_char,
1016#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017};
1018
Scott Wood7ae87032007-07-17 17:59:06 -05001019struct uart_cpm_port cpm_uart_ports[UART_NR];
1020
Scott Woodc1dcfd92007-07-24 15:53:07 -05001021static int cpm_uart_init_port(struct device_node *np,
1022 struct uart_cpm_port *pinfo)
Scott Wood7ae87032007-07-17 17:59:06 -05001023{
1024 const u32 *data;
Scott Woodc1dcfd92007-07-24 15:53:07 -05001025 void __iomem *mem, *pram;
Scott Wood7ae87032007-07-17 17:59:06 -05001026 int len;
1027 int ret;
Laurent Pinchart7485d262008-07-24 18:36:37 +02001028 int i;
Scott Wood7ae87032007-07-17 17:59:06 -05001029
Laurent Pinchart80776552008-07-28 10:42:16 +02001030 data = of_get_property(np, "clock", NULL);
1031 if (data) {
1032 struct clk *clk = clk_get(NULL, (const char*)data);
1033 if (!IS_ERR(clk))
1034 pinfo->clk = clk;
Scott Wood7ae87032007-07-17 17:59:06 -05001035 }
Laurent Pinchart80776552008-07-28 10:42:16 +02001036 if (!pinfo->clk) {
1037 data = of_get_property(np, "fsl,cpm-brg", &len);
1038 if (!data || len != 4) {
1039 printk(KERN_ERR "CPM UART %s has no/invalid "
1040 "fsl,cpm-brg property.\n", np->name);
1041 return -EINVAL;
1042 }
1043 pinfo->brg = *data;
1044 }
Scott Wood7ae87032007-07-17 17:59:06 -05001045
1046 data = of_get_property(np, "fsl,cpm-command", &len);
1047 if (!data || len != 4) {
1048 printk(KERN_ERR "CPM UART %s has no/invalid "
1049 "fsl,cpm-command property.\n", np->name);
1050 return -EINVAL;
1051 }
1052 pinfo->command = *data;
1053
1054 mem = of_iomap(np, 0);
1055 if (!mem)
1056 return -ENOMEM;
1057
Scott Wood7ae87032007-07-17 17:59:06 -05001058 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1059 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1060 pinfo->sccp = mem;
Laurent Pinchartd464df22008-04-10 17:01:27 +02001061 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
Scott Wood7ae87032007-07-17 17:59:06 -05001062 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1063 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1064 pinfo->flags |= FLAG_SMC;
1065 pinfo->smcp = mem;
Laurent Pinchartd464df22008-04-10 17:01:27 +02001066 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
Scott Wood7ae87032007-07-17 17:59:06 -05001067 } else {
1068 ret = -ENODEV;
Laurent Pinchartd464df22008-04-10 17:01:27 +02001069 goto out_mem;
1070 }
1071
1072 if (!pram) {
1073 ret = -ENOMEM;
1074 goto out_mem;
Scott Wood7ae87032007-07-17 17:59:06 -05001075 }
1076
1077 pinfo->tx_nrfifos = TX_NUM_FIFO;
1078 pinfo->tx_fifosize = TX_BUF_SIZE;
1079 pinfo->rx_nrfifos = RX_NUM_FIFO;
1080 pinfo->rx_fifosize = RX_BUF_SIZE;
1081
1082 pinfo->port.uartclk = ppc_proc_freq;
1083 pinfo->port.mapbase = (unsigned long)mem;
1084 pinfo->port.type = PORT_CPM;
1085 pinfo->port.ops = &cpm_uart_pops,
1086 pinfo->port.iotype = UPIO_MEM;
Laurent Pinchartdc320812008-07-02 10:58:45 +02001087 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
Scott Wood7ae87032007-07-17 17:59:06 -05001088 spin_lock_init(&pinfo->port.lock);
1089
1090 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
1091 if (pinfo->port.irq == NO_IRQ) {
1092 ret = -EINVAL;
1093 goto out_pram;
1094 }
1095
Laurent Pinchart7485d262008-07-24 18:36:37 +02001096 for (i = 0; i < NUM_GPIOS; i++)
1097 pinfo->gpios[i] = of_get_gpio(np, i);
1098
Scott Wood7ae87032007-07-17 17:59:06 -05001099 return cpm_uart_request_port(&pinfo->port);
1100
1101out_pram:
Laurent Pinchartd464df22008-04-10 17:01:27 +02001102 cpm_uart_unmap_pram(pinfo, pram);
Scott Wood7ae87032007-07-17 17:59:06 -05001103out_mem:
1104 iounmap(mem);
1105 return ret;
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108#ifdef CONFIG_SERIAL_CPM_CONSOLE
1109/*
1110 * Print a string to the serial port trying not to disturb
1111 * any possible real use of the port...
1112 *
1113 * Note that this is called with interrupts already disabled
1114 */
1115static void cpm_uart_console_write(struct console *co, const char *s,
1116 u_int count)
1117{
Scott Wood7ae87032007-07-17 17:59:06 -05001118 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 unsigned int i;
Scott Woodc1dcfd92007-07-24 15:53:07 -05001120 cbd_t __iomem *bdp, *bdbase;
1121 unsigned char *cp;
Rune Torgersen491a7a42008-05-20 14:39:17 -05001122 unsigned long flags;
1123 int nolock = oops_in_progress;
1124
1125 if (unlikely(nolock)) {
1126 local_irq_save(flags);
1127 } else {
1128 spin_lock_irqsave(&pinfo->port.lock, flags);
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 /* Get the address of the host memory buffer.
1132 */
1133 bdp = pinfo->tx_cur;
1134 bdbase = pinfo->tx_bd_base;
1135
1136 /*
1137 * Now, do each character. This is not as bad as it looks
1138 * since this is a holding FIFO and not a transmitting FIFO.
1139 * We could add the complexity of filling the entire transmit
1140 * buffer, but we would just wait longer between accesses......
1141 */
1142 for (i = 0; i < count; i++, s++) {
1143 /* Wait for transmitter fifo to empty.
1144 * Ready indicates output is ready, and xmt is doing
1145 * that, not that it is ready for us to send.
1146 */
Scott Woodc1dcfd92007-07-24 15:53:07 -05001147 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 ;
1149
1150 /* Send the character out.
1151 * If the buffer address is in the CPM DPRAM, don't
1152 * convert it.
1153 */
Scott Woodc1dcfd92007-07-24 15:53:07 -05001154 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 *cp = *s;
1156
Scott Woodc1dcfd92007-07-24 15:53:07 -05001157 out_be16(&bdp->cbd_datlen, 1);
1158 setbits16(&bdp->cbd_sc, BD_SC_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Scott Woodc1dcfd92007-07-24 15:53:07 -05001160 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 bdp = bdbase;
1162 else
1163 bdp++;
1164
1165 /* if a LF, also do CR... */
1166 if (*s == 10) {
Scott Woodc1dcfd92007-07-24 15:53:07 -05001167 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ;
1169
Scott Woodc1dcfd92007-07-24 15:53:07 -05001170 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *cp = 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Scott Woodc1dcfd92007-07-24 15:53:07 -05001173 out_be16(&bdp->cbd_datlen, 1);
1174 setbits16(&bdp->cbd_sc, BD_SC_READY);
1175
1176 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 bdp = bdbase;
1178 else
1179 bdp++;
1180 }
1181 }
1182
1183 /*
1184 * Finally, Wait for transmitter & holding register to empty
1185 * and restore the IER
1186 */
Scott Woodc1dcfd92007-07-24 15:53:07 -05001187 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ;
1189
Scott Woodc1dcfd92007-07-24 15:53:07 -05001190 pinfo->tx_cur = bdp;
Rune Torgersen491a7a42008-05-20 14:39:17 -05001191
1192 if (unlikely(nolock)) {
1193 local_irq_restore(flags);
1194 } else {
1195 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Vitaly Borduge27987c2006-04-25 20:26:41 +04001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static int __init cpm_uart_console_setup(struct console *co, char *options)
1201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int baud = 38400;
1203 int bits = 8;
1204 int parity = 'n';
1205 int flow = 'n';
1206 int ret;
Scott Wood7ae87032007-07-17 17:59:06 -05001207 struct uart_cpm_port *pinfo;
1208 struct uart_port *port;
1209
Scott Wood7ae87032007-07-17 17:59:06 -05001210 struct device_node *np = NULL;
1211 int i = 0;
1212
1213 if (co->index >= UART_NR) {
1214 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1215 co->index);
1216 return -ENODEV;
1217 }
1218
1219 do {
1220 np = of_find_node_by_type(np, "serial");
1221 if (!np)
1222 return -ENODEV;
1223
1224 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1225 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1226 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1227 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1228 i--;
1229 } while (i++ != co->index);
1230
1231 pinfo = &cpm_uart_ports[co->index];
1232
1233 pinfo->flags |= FLAG_CONSOLE;
1234 port = &pinfo->port;
1235
1236 ret = cpm_uart_init_port(np, pinfo);
1237 of_node_put(np);
1238 if (ret)
1239 return ret;
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (options) {
1242 uart_parse_options(options, &baud, &parity, &bits, &flow);
1243 } else {
Vitaly Bordug3dd0dcb2006-09-21 17:27:15 +04001244 if ((baud = uart_baudrate()) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 baud = 9600;
1246 }
1247
Scott Wood7ae87032007-07-17 17:59:06 -05001248#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1249 udbg_putc = NULL;
1250#endif
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (IS_SMC(pinfo)) {
Nye Liuae2d4c32008-07-23 21:29:50 -07001253 out_be16(&pinfo->smcup->smc_brkcr, 0);
1254 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
Scott Woodc1dcfd92007-07-24 15:53:07 -05001255 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1256 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 } else {
Nye Liuae2d4c32008-07-23 21:29:50 -07001258 out_be16(&pinfo->sccup->scc_brkcr, 0);
1259 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
Scott Woodc1dcfd92007-07-24 15:53:07 -05001260 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1261 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 ret = cpm_uart_allocbuf(pinfo, 1);
1265
1266 if (ret)
1267 return ret;
1268
1269 cpm_uart_initbd(pinfo);
1270
1271 if (IS_SMC(pinfo))
1272 cpm_uart_init_smc(pinfo);
1273 else
1274 cpm_uart_init_scc(pinfo);
1275
1276 uart_set_options(port, co, baud, parity, bits, flow);
Scott Woodd948a292007-07-17 18:09:33 -05001277 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 return 0;
1280}
1281
Kumar Gala36d2f5a2005-08-09 10:08:02 -07001282static struct uart_driver cpm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static struct console cpm_scc_uart_console = {
Kumar Gala36d2f5a2005-08-09 10:08:02 -07001284 .name = "ttyCPM",
1285 .write = cpm_uart_console_write,
1286 .device = uart_console_device,
1287 .setup = cpm_uart_console_setup,
1288 .flags = CON_PRINTBUFFER,
1289 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 .data = &cpm_reg,
1291};
1292
Scott Woodc1dcfd92007-07-24 15:53:07 -05001293static int __init cpm_uart_console_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Vitaly Borduge27987c2006-04-25 20:26:41 +04001295 register_console(&cpm_scc_uart_console);
1296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299console_initcall(cpm_uart_console_init);
1300
1301#define CPM_UART_CONSOLE &cpm_scc_uart_console
1302#else
1303#define CPM_UART_CONSOLE NULL
1304#endif
1305
1306static struct uart_driver cpm_reg = {
1307 .owner = THIS_MODULE,
1308 .driver_name = "ttyCPM",
1309 .dev_name = "ttyCPM",
1310 .major = SERIAL_CPM_MAJOR,
1311 .minor = SERIAL_CPM_MINOR,
1312 .cons = CPM_UART_CONSOLE,
Scott Wood7ae87032007-07-17 17:59:06 -05001313 .nr = UART_NR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314};
Scott Wood7ae87032007-07-17 17:59:06 -05001315
Scott Wood7ae87032007-07-17 17:59:06 -05001316static int probe_index;
1317
1318static int __devinit cpm_uart_probe(struct of_device *ofdev,
1319 const struct of_device_id *match)
1320{
1321 int index = probe_index++;
1322 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1323 int ret;
1324
1325 pinfo->port.line = index;
1326
1327 if (index >= UART_NR)
1328 return -ENODEV;
1329
1330 dev_set_drvdata(&ofdev->dev, pinfo);
1331
1332 ret = cpm_uart_init_port(ofdev->node, pinfo);
1333 if (ret)
1334 return ret;
1335
1336 return uart_add_one_port(&cpm_reg, &pinfo->port);
1337}
1338
1339static int __devexit cpm_uart_remove(struct of_device *ofdev)
1340{
1341 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1342 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1343}
1344
1345static struct of_device_id cpm_uart_match[] = {
1346 {
1347 .compatible = "fsl,cpm1-smc-uart",
1348 },
1349 {
1350 .compatible = "fsl,cpm1-scc-uart",
1351 },
1352 {
1353 .compatible = "fsl,cpm2-smc-uart",
1354 },
1355 {
1356 .compatible = "fsl,cpm2-scc-uart",
1357 },
1358 {}
1359};
1360
1361static struct of_platform_driver cpm_uart_driver = {
1362 .name = "cpm_uart",
1363 .match_table = cpm_uart_match,
1364 .probe = cpm_uart_probe,
1365 .remove = cpm_uart_remove,
1366 };
1367
1368static int __init cpm_uart_init(void)
1369{
1370 int ret = uart_register_driver(&cpm_reg);
1371 if (ret)
1372 return ret;
1373
1374 ret = of_register_platform_driver(&cpm_uart_driver);
1375 if (ret)
1376 uart_unregister_driver(&cpm_reg);
1377
1378 return ret;
1379}
1380
1381static void __exit cpm_uart_exit(void)
1382{
1383 of_unregister_platform_driver(&cpm_uart_driver);
1384 uart_unregister_driver(&cpm_reg);
1385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387module_init(cpm_uart_init);
1388module_exit(cpm_uart_exit);
1389
1390MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1391MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1392MODULE_LICENSE("GPL");
1393MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);