blob: c88f8ad3ff821d16fef218c5021adede8c9c6cf1 [file] [log] [blame]
Bryan Wu2f351742008-04-30 00:52:12 -07001/*
sonic zhangccf68e52009-12-09 12:31:28 -08002 * Blackfin On-Chip Sport Emulated UART Driver
Bryan Wu2f351742008-04-30 00:52:12 -07003 *
sonic zhangccf68e52009-12-09 12:31:28 -08004 * Copyright 2006-2009 Analog Devices Inc.
Bryan Wu2f351742008-04-30 00:52:12 -07005 *
sonic zhangccf68e52009-12-09 12:31:28 -08006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wu2f351742008-04-30 00:52:12 -07007 *
sonic zhangccf68e52009-12-09 12:31:28 -08008 * Licensed under the GPL-2 or later.
Bryan Wu2f351742008-04-30 00:52:12 -07009 */
10
11/*
12 * This driver and the hardware supported are in term of EE-191 of ADI.
13 * http://www.analog.com/UploadedFiles/Application_Notes/399447663EE191.pdf
14 * This application note describe how to implement a UART on a Sharc DSP,
15 * but this driver is implemented on Blackfin Processor.
sonic zhangccf68e52009-12-09 12:31:28 -080016 * Transmit Frame Sync is not used by this driver to transfer data out.
Bryan Wu2f351742008-04-30 00:52:12 -070017 */
18
sonic zhangccf68e52009-12-09 12:31:28 -080019/* #define DEBUG */
Bryan Wu2f351742008-04-30 00:52:12 -070020
sonic zhangccf68e52009-12-09 12:31:28 -080021#define DRV_NAME "bfin-sport-uart"
22#define DEVICE_NAME "ttySS"
23#define pr_fmt(fmt) DRV_NAME ": " fmt
Bryan Wu2f351742008-04-30 00:52:12 -070024
25#include <linux/module.h>
26#include <linux/ioport.h>
sonic zhangccf68e52009-12-09 12:31:28 -080027#include <linux/io.h>
Bryan Wu2f351742008-04-30 00:52:12 -070028#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Bryan Wu2f351742008-04-30 00:52:12 -070032#include <linux/platform_device.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36
37#include <asm/delay.h>
38#include <asm/portmux.h>
39
40#include "bfin_sport_uart.h"
41
sonic zhangccf68e52009-12-09 12:31:28 -080042#ifdef CONFIG_SERIAL_BFIN_SPORT0_UART
Bryan Wu2f351742008-04-30 00:52:12 -070043unsigned short bfin_uart_pin_req_sport0[] =
44 {P_SPORT0_TFS, P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS, \
45 P_SPORT0_DRPRI, P_SPORT0_RSCLK, P_SPORT0_DRSEC, P_SPORT0_DTSEC, 0};
sonic zhangccf68e52009-12-09 12:31:28 -080046#endif
47#ifdef CONFIG_SERIAL_BFIN_SPORT1_UART
Bryan Wu2f351742008-04-30 00:52:12 -070048unsigned short bfin_uart_pin_req_sport1[] =
49 {P_SPORT1_TFS, P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, \
50 P_SPORT1_DRPRI, P_SPORT1_RSCLK, P_SPORT1_DRSEC, P_SPORT1_DTSEC, 0};
sonic zhangccf68e52009-12-09 12:31:28 -080051#endif
52#ifdef CONFIG_SERIAL_BFIN_SPORT2_UART
53unsigned short bfin_uart_pin_req_sport2[] =
54 {P_SPORT2_TFS, P_SPORT2_DTPRI, P_SPORT2_TSCLK, P_SPORT2_RFS, \
55 P_SPORT2_DRPRI, P_SPORT2_RSCLK, P_SPORT2_DRSEC, P_SPORT2_DTSEC, 0};
56#endif
57#ifdef CONFIG_SERIAL_BFIN_SPORT3_UART
58unsigned short bfin_uart_pin_req_sport3[] =
59 {P_SPORT3_TFS, P_SPORT3_DTPRI, P_SPORT3_TSCLK, P_SPORT3_RFS, \
60 P_SPORT3_DRPRI, P_SPORT3_RSCLK, P_SPORT3_DRSEC, P_SPORT3_DTSEC, 0};
61#endif
Bryan Wu2f351742008-04-30 00:52:12 -070062
63struct sport_uart_port {
64 struct uart_port port;
Bryan Wu2f351742008-04-30 00:52:12 -070065 int err_irq;
sonic zhangccf68e52009-12-09 12:31:28 -080066 unsigned short csize;
67 unsigned short rxmask;
68 unsigned short txmask1;
69 unsigned short txmask2;
70 unsigned char stopb;
71/* unsigned char parib; */
Bryan Wu2f351742008-04-30 00:52:12 -070072};
73
74static void sport_uart_tx_chars(struct sport_uart_port *up);
75static void sport_stop_tx(struct uart_port *port);
76
77static inline void tx_one_byte(struct sport_uart_port *up, unsigned int value)
78{
sonic zhangccf68e52009-12-09 12:31:28 -080079 pr_debug("%s value:%x, mask1=0x%x, mask2=0x%x\n", __func__, value,
80 up->txmask1, up->txmask2);
81
82 /* Place Start and Stop bits */
Mike Frysinger4328e3e2009-06-11 13:37:11 +010083 __asm__ __volatile__ (
sonic zhangccf68e52009-12-09 12:31:28 -080084 "%[val] <<= 1;"
85 "%[val] = %[val] & %[mask1];"
86 "%[val] = %[val] | %[mask2];"
87 : [val]"+d"(value)
88 : [mask1]"d"(up->txmask1), [mask2]"d"(up->txmask2)
89 : "ASTAT"
Mike Frysinger4328e3e2009-06-11 13:37:11 +010090 );
Harvey Harrison6ef53062009-01-02 13:49:13 +000091 pr_debug("%s value:%x\n", __func__, value);
Bryan Wu2f351742008-04-30 00:52:12 -070092
93 SPORT_PUT_TX(up, value);
94}
95
sonic zhangccf68e52009-12-09 12:31:28 -080096static inline unsigned char rx_one_byte(struct sport_uart_port *up)
Bryan Wu2f351742008-04-30 00:52:12 -070097{
sonic zhangccf68e52009-12-09 12:31:28 -080098 unsigned int value;
99 unsigned char extract;
Mike Frysinger4328e3e2009-06-11 13:37:11 +0100100 u32 tmp_mask1, tmp_mask2, tmp_shift, tmp;
Bryan Wu2f351742008-04-30 00:52:12 -0700101
sonic zhangccf68e52009-12-09 12:31:28 -0800102 if ((up->csize + up->stopb) > 7)
103 value = SPORT_GET_RX32(up);
104 else
105 value = SPORT_GET_RX(up);
Bryan Wu2f351742008-04-30 00:52:12 -0700106
sonic zhangccf68e52009-12-09 12:31:28 -0800107 pr_debug("%s value:%x, cs=%d, mask=0x%x\n", __func__, value,
108 up->csize, up->rxmask);
109
110 /* Extract data */
Mike Frysinger4328e3e2009-06-11 13:37:11 +0100111 __asm__ __volatile__ (
112 "%[extr] = 0;"
sonic zhangccf68e52009-12-09 12:31:28 -0800113 "%[mask1] = %[rxmask];"
114 "%[mask2] = 0x0200(Z);"
Mike Frysinger4328e3e2009-06-11 13:37:11 +0100115 "%[shift] = 0;"
116 "LSETUP(.Lloop_s, .Lloop_e) LC0 = %[lc];"
117 ".Lloop_s:"
118 "%[tmp] = extract(%[val], %[mask1].L)(Z);"
119 "%[tmp] <<= %[shift];"
120 "%[extr] = %[extr] | %[tmp];"
121 "%[mask1] = %[mask1] - %[mask2];"
122 ".Lloop_e:"
123 "%[shift] += 1;"
sonic zhangccf68e52009-12-09 12:31:28 -0800124 : [extr]"=&d"(extract), [shift]"=&d"(tmp_shift), [tmp]"=&d"(tmp),
125 [mask1]"=&d"(tmp_mask1), [mask2]"=&d"(tmp_mask2)
126 : [val]"d"(value), [rxmask]"d"(up->rxmask), [lc]"a"(up->csize)
Mike Frysinger4328e3e2009-06-11 13:37:11 +0100127 : "ASTAT", "LB0", "LC0", "LT0"
128 );
Bryan Wu2f351742008-04-30 00:52:12 -0700129
130 pr_debug(" extract:%x\n", extract);
131 return extract;
132}
133
sonic zhangccf68e52009-12-09 12:31:28 -0800134static int sport_uart_setup(struct sport_uart_port *up, int size, int baud_rate)
Bryan Wu2f351742008-04-30 00:52:12 -0700135{
sonic zhangccf68e52009-12-09 12:31:28 -0800136 int tclkdiv, rclkdiv;
137 unsigned int sclk = get_sclk();
Bryan Wu2f351742008-04-30 00:52:12 -0700138
sonic zhangccf68e52009-12-09 12:31:28 -0800139 /* Set TCR1 and TCR2, TFSR is not enabled for uart */
140 SPORT_PUT_TCR1(up, (ITFS | TLSBIT | ITCLK));
141 SPORT_PUT_TCR2(up, size + 1);
Harvey Harrison6ef53062009-01-02 13:49:13 +0000142 pr_debug("%s TCR1:%x, TCR2:%x\n", __func__, SPORT_GET_TCR1(up), SPORT_GET_TCR2(up));
Bryan Wu2f351742008-04-30 00:52:12 -0700143
144 /* Set RCR1 and RCR2 */
145 SPORT_PUT_RCR1(up, (RCKFE | LARFS | LRFS | RFSR | IRCLK));
sonic zhangccf68e52009-12-09 12:31:28 -0800146 SPORT_PUT_RCR2(up, (size + 1) * 2 - 1);
Harvey Harrison6ef53062009-01-02 13:49:13 +0000147 pr_debug("%s RCR1:%x, RCR2:%x\n", __func__, SPORT_GET_RCR1(up), SPORT_GET_RCR2(up));
Bryan Wu2f351742008-04-30 00:52:12 -0700148
sonic zhangccf68e52009-12-09 12:31:28 -0800149 tclkdiv = sclk / (2 * baud_rate) - 1;
150 rclkdiv = sclk / (2 * baud_rate * 2) - 1;
Bryan Wu2f351742008-04-30 00:52:12 -0700151 SPORT_PUT_TCLKDIV(up, tclkdiv);
Bryan Wu2f351742008-04-30 00:52:12 -0700152 SPORT_PUT_RCLKDIV(up, rclkdiv);
153 SSYNC();
sonic zhangccf68e52009-12-09 12:31:28 -0800154 pr_debug("%s sclk:%d, baud_rate:%d, tclkdiv:%d, rclkdiv:%d\n",
155 __func__, sclk, baud_rate, tclkdiv, rclkdiv);
Bryan Wu2f351742008-04-30 00:52:12 -0700156
157 return 0;
158}
159
160static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
161{
162 struct sport_uart_port *up = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700163 struct tty_struct *tty = up->port.state->port.tty;
Bryan Wu2f351742008-04-30 00:52:12 -0700164 unsigned int ch;
165
sonic zhangccf68e52009-12-09 12:31:28 -0800166 spin_lock(&up->port.lock);
167
168 while (SPORT_GET_STAT(up) & RXNE) {
Bryan Wu2f351742008-04-30 00:52:12 -0700169 ch = rx_one_byte(up);
170 up->port.icount.rx++;
171
sonic zhangccf68e52009-12-09 12:31:28 -0800172 if (!uart_handle_sysrq_char(&up->port, ch))
Bryan Wu2f351742008-04-30 00:52:12 -0700173 tty_insert_flip_char(tty, ch, TTY_NORMAL);
sonic zhangccf68e52009-12-09 12:31:28 -0800174 }
Bryan Wu2f351742008-04-30 00:52:12 -0700175 tty_flip_buffer_push(tty);
176
sonic zhangccf68e52009-12-09 12:31:28 -0800177 spin_unlock(&up->port.lock);
178
Bryan Wu2f351742008-04-30 00:52:12 -0700179 return IRQ_HANDLED;
180}
181
182static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
183{
sonic zhangccf68e52009-12-09 12:31:28 -0800184 struct sport_uart_port *up = dev_id;
185
186 spin_lock(&up->port.lock);
187 sport_uart_tx_chars(up);
188 spin_unlock(&up->port.lock);
Bryan Wu2f351742008-04-30 00:52:12 -0700189
190 return IRQ_HANDLED;
191}
192
193static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
194{
195 struct sport_uart_port *up = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700196 struct tty_struct *tty = up->port.state->port.tty;
Bryan Wu2f351742008-04-30 00:52:12 -0700197 unsigned int stat = SPORT_GET_STAT(up);
198
sonic zhangccf68e52009-12-09 12:31:28 -0800199 spin_lock(&up->port.lock);
200
Bryan Wu2f351742008-04-30 00:52:12 -0700201 /* Overflow in RX FIFO */
202 if (stat & ROVF) {
203 up->port.icount.overrun++;
204 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
205 SPORT_PUT_STAT(up, ROVF); /* Clear ROVF bit */
206 }
207 /* These should not happen */
208 if (stat & (TOVF | TUVF | RUVF)) {
sonic zhangccf68e52009-12-09 12:31:28 -0800209 pr_err("SPORT Error:%s %s %s\n",
210 (stat & TOVF) ? "TX overflow" : "",
211 (stat & TUVF) ? "TX underflow" : "",
212 (stat & RUVF) ? "RX underflow" : "");
Bryan Wu2f351742008-04-30 00:52:12 -0700213 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
214 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
215 }
216 SSYNC();
217
sonic zhangccf68e52009-12-09 12:31:28 -0800218 spin_unlock(&up->port.lock);
Bryan Wu2f351742008-04-30 00:52:12 -0700219 return IRQ_HANDLED;
220}
221
222/* Reqeust IRQ, Setup clock */
223static int sport_startup(struct uart_port *port)
224{
225 struct sport_uart_port *up = (struct sport_uart_port *)port;
sonic zhangccf68e52009-12-09 12:31:28 -0800226 int ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700227
Harvey Harrison6ef53062009-01-02 13:49:13 +0000228 pr_debug("%s enter\n", __func__);
sonic zhangccf68e52009-12-09 12:31:28 -0800229 ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
230 "SPORT_UART_RX", up);
231 if (ret) {
232 dev_err(port->dev, "unable to request SPORT RX interrupt\n");
233 return ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700234 }
235
sonic zhangccf68e52009-12-09 12:31:28 -0800236 ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
237 "SPORT_UART_TX", up);
238 if (ret) {
239 dev_err(port->dev, "unable to request SPORT TX interrupt\n");
Bryan Wu2f351742008-04-30 00:52:12 -0700240 goto fail1;
241 }
242
sonic zhangccf68e52009-12-09 12:31:28 -0800243 ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
244 "SPORT_UART_STATUS", up);
245 if (ret) {
246 dev_err(port->dev, "unable to request SPORT status interrupt\n");
Bryan Wu2f351742008-04-30 00:52:12 -0700247 goto fail2;
248 }
249
Bryan Wu2f351742008-04-30 00:52:12 -0700250 return 0;
sonic zhangccf68e52009-12-09 12:31:28 -0800251 fail2:
252 free_irq(up->port.irq+1, up);
253 fail1:
254 free_irq(up->port.irq, up);
Bryan Wu2f351742008-04-30 00:52:12 -0700255
sonic zhangccf68e52009-12-09 12:31:28 -0800256 return ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700257}
258
259static void sport_uart_tx_chars(struct sport_uart_port *up)
260{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700261 struct circ_buf *xmit = &up->port.state->xmit;
Bryan Wu2f351742008-04-30 00:52:12 -0700262
263 if (SPORT_GET_STAT(up) & TXF)
264 return;
265
266 if (up->port.x_char) {
267 tx_one_byte(up, up->port.x_char);
268 up->port.icount.tx++;
269 up->port.x_char = 0;
270 return;
271 }
272
273 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
274 sport_stop_tx(&up->port);
275 return;
276 }
277
278 while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
279 tx_one_byte(up, xmit->buf[xmit->tail]);
280 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
281 up->port.icount.tx++;
282 }
283
284 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
285 uart_write_wakeup(&up->port);
286}
287
288static unsigned int sport_tx_empty(struct uart_port *port)
289{
290 struct sport_uart_port *up = (struct sport_uart_port *)port;
291 unsigned int stat;
292
293 stat = SPORT_GET_STAT(up);
Harvey Harrison6ef53062009-01-02 13:49:13 +0000294 pr_debug("%s stat:%04x\n", __func__, stat);
Bryan Wu2f351742008-04-30 00:52:12 -0700295 if (stat & TXHRE) {
296 return TIOCSER_TEMT;
297 } else
298 return 0;
299}
300
301static unsigned int sport_get_mctrl(struct uart_port *port)
302{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000303 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700304 return (TIOCM_CTS | TIOCM_CD | TIOCM_DSR);
305}
306
307static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
308{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000309 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700310}
311
312static void sport_stop_tx(struct uart_port *port)
313{
314 struct sport_uart_port *up = (struct sport_uart_port *)port;
Bryan Wu2f351742008-04-30 00:52:12 -0700315
Harvey Harrison6ef53062009-01-02 13:49:13 +0000316 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700317
Bryan Wu2f351742008-04-30 00:52:12 -0700318 /* Although the hold register is empty, last byte is still in shift
sonic zhangccf68e52009-12-09 12:31:28 -0800319 * register and not sent out yet. So, put a dummy data into TX FIFO.
320 * Then, sport tx stops when last byte is shift out and the dummy
321 * data is moved into the shift register.
322 */
323 SPORT_PUT_TX(up, 0xffff);
324 while (!(SPORT_GET_STAT(up) & TXHRE))
325 cpu_relax();
Bryan Wu2f351742008-04-30 00:52:12 -0700326
327 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
328 SSYNC();
329
330 return;
331}
332
333static void sport_start_tx(struct uart_port *port)
334{
335 struct sport_uart_port *up = (struct sport_uart_port *)port;
336
Harvey Harrison6ef53062009-01-02 13:49:13 +0000337 pr_debug("%s enter\n", __func__);
sonic zhangccf68e52009-12-09 12:31:28 -0800338
Bryan Wu2f351742008-04-30 00:52:12 -0700339 /* Write data into SPORT FIFO before enable SPROT to transmit */
340 sport_uart_tx_chars(up);
341
342 /* Enable transmit, then an interrupt will generated */
343 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
344 SSYNC();
Harvey Harrison6ef53062009-01-02 13:49:13 +0000345 pr_debug("%s exit\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700346}
347
348static void sport_stop_rx(struct uart_port *port)
349{
350 struct sport_uart_port *up = (struct sport_uart_port *)port;
351
Harvey Harrison6ef53062009-01-02 13:49:13 +0000352 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700353 /* Disable sport to stop rx */
354 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
355 SSYNC();
356}
357
358static void sport_enable_ms(struct uart_port *port)
359{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000360 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700361}
362
363static void sport_break_ctl(struct uart_port *port, int break_state)
364{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000365 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700366}
367
368static void sport_shutdown(struct uart_port *port)
369{
370 struct sport_uart_port *up = (struct sport_uart_port *)port;
371
sonic zhangccf68e52009-12-09 12:31:28 -0800372 dev_dbg(port->dev, "%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700373
374 /* Disable sport */
375 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
376 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
377 SSYNC();
378
sonic zhangccf68e52009-12-09 12:31:28 -0800379 free_irq(up->port.irq, up);
380 free_irq(up->port.irq+1, up);
Bryan Wu2f351742008-04-30 00:52:12 -0700381 free_irq(up->err_irq, up);
382}
383
Bryan Wu2f351742008-04-30 00:52:12 -0700384static const char *sport_type(struct uart_port *port)
385{
386 struct sport_uart_port *up = (struct sport_uart_port *)port;
387
Harvey Harrison6ef53062009-01-02 13:49:13 +0000388 pr_debug("%s enter\n", __func__);
sonic zhangccf68e52009-12-09 12:31:28 -0800389 return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
Bryan Wu2f351742008-04-30 00:52:12 -0700390}
391
392static void sport_release_port(struct uart_port *port)
393{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000394 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700395}
396
397static int sport_request_port(struct uart_port *port)
398{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000399 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700400 return 0;
401}
402
403static void sport_config_port(struct uart_port *port, int flags)
404{
405 struct sport_uart_port *up = (struct sport_uart_port *)port;
406
Harvey Harrison6ef53062009-01-02 13:49:13 +0000407 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700408 up->port.type = PORT_BFIN_SPORT;
409}
410
411static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
412{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000413 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700414 return 0;
415}
416
sonic zhangccf68e52009-12-09 12:31:28 -0800417static void sport_set_termios(struct uart_port *port,
418 struct ktermios *termios, struct ktermios *old)
419{
420 struct sport_uart_port *up = (struct sport_uart_port *)port;
421 unsigned long flags;
422 int i;
423
424 pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
425
426 switch (termios->c_cflag & CSIZE) {
427 case CS8:
428 up->csize = 8;
429 break;
430 case CS7:
431 up->csize = 7;
432 break;
433 case CS6:
434 up->csize = 6;
435 break;
436 case CS5:
437 up->csize = 5;
438 break;
439 default:
440 pr_warning("requested word length not supported\n");
441 }
442
443 if (termios->c_cflag & CSTOPB) {
444 up->stopb = 1;
445 }
446 if (termios->c_cflag & PARENB) {
447 pr_warning("PAREN bits is not supported yet\n");
448 /* up->parib = 1; */
449 }
450
451 port->read_status_mask = OE;
452 if (termios->c_iflag & INPCK)
453 port->read_status_mask |= (FE | PE);
454 if (termios->c_iflag & (BRKINT | PARMRK))
455 port->read_status_mask |= BI;
456
457 /*
458 * Characters to ignore
459 */
460 port->ignore_status_mask = 0;
461 if (termios->c_iflag & IGNPAR)
462 port->ignore_status_mask |= FE | PE;
463 if (termios->c_iflag & IGNBRK) {
464 port->ignore_status_mask |= BI;
465 /*
466 * If we're ignoring parity and break indicators,
467 * ignore overruns too (for real raw support).
468 */
469 if (termios->c_iflag & IGNPAR)
470 port->ignore_status_mask |= OE;
471 }
472
473 /* RX extract mask */
474 up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
475 /* TX masks, 8 bit data and 1 bit stop for example:
476 * mask1 = b#0111111110
477 * mask2 = b#1000000000
478 */
479 for (i = 0, up->txmask1 = 0; i < up->csize; i++)
480 up->txmask1 |= (1<<i);
481 up->txmask2 = (1<<i);
482 if (up->stopb) {
483 ++i;
484 up->txmask2 |= (1<<i);
485 }
486 up->txmask1 <<= 1;
487 up->txmask2 <<= 1;
488 /* uart baud rate */
489 port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
490
491 spin_lock_irqsave(&up->port.lock, flags);
492
493 /* Disable UART */
494 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
495 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
496
497 sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
498
499 /* driver TX line high after config, one dummy data is
500 * necessary to stop sport after shift one byte
501 */
502 SPORT_PUT_TX(up, 0xffff);
503 SPORT_PUT_TX(up, 0xffff);
504 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
505 SSYNC();
506 while (!(SPORT_GET_STAT(up) & TXHRE))
507 cpu_relax();
508 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
509 SSYNC();
510
511 /* Port speed changed, update the per-port timeout. */
512 uart_update_timeout(port, termios->c_cflag, port->uartclk);
513
514 /* Enable sport rx */
515 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
516 SSYNC();
517
518 spin_unlock_irqrestore(&up->port.lock, flags);
519}
520
Bryan Wu2f351742008-04-30 00:52:12 -0700521struct uart_ops sport_uart_ops = {
522 .tx_empty = sport_tx_empty,
523 .set_mctrl = sport_set_mctrl,
524 .get_mctrl = sport_get_mctrl,
525 .stop_tx = sport_stop_tx,
526 .start_tx = sport_start_tx,
527 .stop_rx = sport_stop_rx,
528 .enable_ms = sport_enable_ms,
529 .break_ctl = sport_break_ctl,
530 .startup = sport_startup,
531 .shutdown = sport_shutdown,
532 .set_termios = sport_set_termios,
533 .type = sport_type,
534 .release_port = sport_release_port,
535 .request_port = sport_request_port,
536 .config_port = sport_config_port,
537 .verify_port = sport_verify_port,
538};
539
sonic zhangccf68e52009-12-09 12:31:28 -0800540#define BFIN_SPORT_UART_MAX_PORTS 4
541
542static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
543
544#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
545static int __init
546sport_uart_console_setup(struct console *co, char *options)
547{
548 struct sport_uart_port *up;
549 int baud = 57600;
550 int bits = 8;
551 int parity = 'n';
552 int flow = 'n';
553
554 /* Check whether an invalid uart number has been specified */
555 if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
556 return -ENODEV;
557
558 up = bfin_sport_uart_ports[co->index];
559 if (!up)
560 return -ENODEV;
561
562 if (options)
563 uart_parse_options(options, &baud, &parity, &bits, &flow);
564
565 return uart_set_options(&up->port, co, baud, parity, bits, flow);
566}
567
568static void sport_uart_console_putchar(struct uart_port *port, int ch)
569{
570 struct sport_uart_port *up = (struct sport_uart_port *)port;
571
572 while (SPORT_GET_STAT(up) & TXF)
573 barrier();
574
575 tx_one_byte(up, ch);
576}
577
578/*
579 * Interrupts are disabled on entering
580 */
581static void
582sport_uart_console_write(struct console *co, const char *s, unsigned int count)
583{
584 struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
585 unsigned long flags;
586
587 spin_lock_irqsave(&up->port.lock, flags);
588
589 if (SPORT_GET_TCR1(up) & TSPEN)
590 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
591 else {
592 /* dummy data to start sport */
593 while (SPORT_GET_STAT(up) & TXF)
594 barrier();
595 SPORT_PUT_TX(up, 0xffff);
596 /* Enable transmit, then an interrupt will generated */
597 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
598 SSYNC();
599
600 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
601
602 /* Although the hold register is empty, last byte is still in shift
603 * register and not sent out yet. So, put a dummy data into TX FIFO.
604 * Then, sport tx stops when last byte is shift out and the dummy
605 * data is moved into the shift register.
606 */
607 while (SPORT_GET_STAT(up) & TXF)
608 barrier();
609 SPORT_PUT_TX(up, 0xffff);
610 while (!(SPORT_GET_STAT(up) & TXHRE))
611 barrier();
612
613 /* Stop sport tx transfer */
614 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
615 SSYNC();
Bryan Wu2f351742008-04-30 00:52:12 -0700616 }
sonic zhangccf68e52009-12-09 12:31:28 -0800617
618 spin_unlock_irqrestore(&up->port.lock, flags);
619}
620
621static struct uart_driver sport_uart_reg;
622
623static struct console sport_uart_console = {
624 .name = DEVICE_NAME,
625 .write = sport_uart_console_write,
626 .device = uart_console_device,
627 .setup = sport_uart_console_setup,
628 .flags = CON_PRINTBUFFER,
629 .index = -1,
630 .data = &sport_uart_reg,
Bryan Wu2f351742008-04-30 00:52:12 -0700631};
632
sonic zhangccf68e52009-12-09 12:31:28 -0800633#define SPORT_UART_CONSOLE (&sport_uart_console)
634#else
635#define SPORT_UART_CONSOLE NULL
636#endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
637
638
Bryan Wu2f351742008-04-30 00:52:12 -0700639static struct uart_driver sport_uart_reg = {
640 .owner = THIS_MODULE,
sonic zhangccf68e52009-12-09 12:31:28 -0800641 .driver_name = DRV_NAME,
642 .dev_name = DEVICE_NAME,
Bryan Wu2f351742008-04-30 00:52:12 -0700643 .major = 204,
644 .minor = 84,
sonic zhangccf68e52009-12-09 12:31:28 -0800645 .nr = BFIN_SPORT_UART_MAX_PORTS,
646 .cons = SPORT_UART_CONSOLE,
Bryan Wu2f351742008-04-30 00:52:12 -0700647};
648
sonic zhangccf68e52009-12-09 12:31:28 -0800649#ifdef CONFIG_PM
650static int sport_uart_suspend(struct device *dev)
Bryan Wu2f351742008-04-30 00:52:12 -0700651{
sonic zhangccf68e52009-12-09 12:31:28 -0800652 struct sport_uart_port *sport = dev_get_drvdata(dev);
Bryan Wu2f351742008-04-30 00:52:12 -0700653
sonic zhangccf68e52009-12-09 12:31:28 -0800654 dev_dbg(dev, "%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700655 if (sport)
656 uart_suspend_port(&sport_uart_reg, &sport->port);
657
658 return 0;
659}
660
sonic zhangccf68e52009-12-09 12:31:28 -0800661static int sport_uart_resume(struct device *dev)
Bryan Wu2f351742008-04-30 00:52:12 -0700662{
sonic zhangccf68e52009-12-09 12:31:28 -0800663 struct sport_uart_port *sport = dev_get_drvdata(dev);
Bryan Wu2f351742008-04-30 00:52:12 -0700664
sonic zhangccf68e52009-12-09 12:31:28 -0800665 dev_dbg(dev, "%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700666 if (sport)
667 uart_resume_port(&sport_uart_reg, &sport->port);
668
669 return 0;
670}
671
sonic zhangccf68e52009-12-09 12:31:28 -0800672static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
673 .suspend = sport_uart_suspend,
674 .resume = sport_uart_resume,
675};
676#endif
Bryan Wu2f351742008-04-30 00:52:12 -0700677
sonic zhangccf68e52009-12-09 12:31:28 -0800678static int __devinit sport_uart_probe(struct platform_device *pdev)
679{
680 struct resource *res;
681 struct sport_uart_port *sport;
682 int ret = 0;
683
684 dev_dbg(&pdev->dev, "%s enter\n", __func__);
685
686 if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
687 dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
688 return -ENOENT;
689 }
690
691 if (bfin_sport_uart_ports[pdev->id] == NULL) {
692 bfin_sport_uart_ports[pdev->id] =
693 kmalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
694 sport = bfin_sport_uart_ports[pdev->id];
695 if (!sport) {
696 dev_err(&pdev->dev,
697 "Fail to kmalloc sport_uart_port\n");
698 return -ENOMEM;
699 }
700
701 ret = peripheral_request_list(
702 (unsigned short *)pdev->dev.platform_data, DRV_NAME);
703 if (ret) {
704 dev_err(&pdev->dev,
705 "Fail to request SPORT peripherals\n");
706 goto out_error_free_mem;
707 }
708
709 spin_lock_init(&sport->port.lock);
710 sport->port.fifosize = SPORT_TX_FIFO_SIZE,
711 sport->port.ops = &sport_uart_ops;
712 sport->port.line = pdev->id;
713 sport->port.iotype = UPIO_MEM;
714 sport->port.flags = UPF_BOOT_AUTOCONF;
715
716 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
717 if (res == NULL) {
718 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
719 ret = -ENOENT;
720 goto out_error_free_peripherals;
721 }
722
723 sport->port.membase = ioremap(res->start,
724 res->end - res->start);
725 if (!sport->port.membase) {
726 dev_err(&pdev->dev, "Cannot map sport IO\n");
727 ret = -ENXIO;
728 goto out_error_free_peripherals;
729 }
730
731 sport->port.irq = platform_get_irq(pdev, 0);
732 if (sport->port.irq < 0) {
733 dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
734 ret = -ENOENT;
735 goto out_error_unmap;
736 }
737
738 sport->err_irq = platform_get_irq(pdev, 1);
739 if (sport->err_irq < 0) {
740 dev_err(&pdev->dev, "No sport status IRQ specified\n");
741 ret = -ENOENT;
742 goto out_error_unmap;
743 }
744 }
745
746#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
747 if (!is_early_platform_device(pdev)) {
748#endif
749 sport = bfin_sport_uart_ports[pdev->id];
750 sport->port.dev = &pdev->dev;
751 dev_set_drvdata(&pdev->dev, sport);
752 ret = uart_add_one_port(&sport_uart_reg, &sport->port);
753#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
754 }
755#endif
756 if (!ret)
757 return 0;
758
759 if (sport) {
760out_error_unmap:
761 iounmap(sport->port.membase);
762out_error_free_peripherals:
763 peripheral_free_list(
764 (unsigned short *)pdev->dev.platform_data);
765out_error_free_mem:
766 kfree(sport);
767 bfin_sport_uart_ports[pdev->id] = NULL;
768 }
769
770 return ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700771}
772
sonic zhangccf68e52009-12-09 12:31:28 -0800773static int __devexit sport_uart_remove(struct platform_device *pdev)
Bryan Wu2f351742008-04-30 00:52:12 -0700774{
sonic zhangccf68e52009-12-09 12:31:28 -0800775 struct sport_uart_port *sport = platform_get_drvdata(pdev);
Bryan Wu2f351742008-04-30 00:52:12 -0700776
sonic zhangccf68e52009-12-09 12:31:28 -0800777 dev_dbg(&pdev->dev, "%s enter\n", __func__);
778 dev_set_drvdata(&pdev->dev, NULL);
Bryan Wu2f351742008-04-30 00:52:12 -0700779
sonic zhangccf68e52009-12-09 12:31:28 -0800780 if (sport) {
Bryan Wu2f351742008-04-30 00:52:12 -0700781 uart_remove_one_port(&sport_uart_reg, &sport->port);
sonic zhangccf68e52009-12-09 12:31:28 -0800782 iounmap(sport->port.membase);
783 peripheral_free_list(
784 (unsigned short *)pdev->dev.platform_data);
785 kfree(sport);
786 bfin_sport_uart_ports[pdev->id] = NULL;
787 }
Bryan Wu2f351742008-04-30 00:52:12 -0700788
789 return 0;
790}
791
792static struct platform_driver sport_uart_driver = {
793 .probe = sport_uart_probe,
sonic zhangccf68e52009-12-09 12:31:28 -0800794 .remove = __devexit_p(sport_uart_remove),
Bryan Wu2f351742008-04-30 00:52:12 -0700795 .driver = {
796 .name = DRV_NAME,
sonic zhangccf68e52009-12-09 12:31:28 -0800797#ifdef CONFIG_PM
798 .pm = &bfin_sport_uart_dev_pm_ops,
799#endif
Bryan Wu2f351742008-04-30 00:52:12 -0700800 },
801};
802
sonic zhangccf68e52009-12-09 12:31:28 -0800803#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
804static __initdata struct early_platform_driver early_sport_uart_driver = {
805 .class_str = DRV_NAME,
806 .pdrv = &sport_uart_driver,
807 .requested_id = EARLY_PLATFORM_ID_UNSET,
808};
809
810static int __init sport_uart_rs_console_init(void)
811{
812 early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
813
814 early_platform_driver_probe(DRV_NAME, BFIN_SPORT_UART_MAX_PORTS, 0);
815
816 register_console(&sport_uart_console);
817
818 return 0;
819}
820console_initcall(sport_uart_rs_console_init);
821#endif
822
Bryan Wu2f351742008-04-30 00:52:12 -0700823static int __init sport_uart_init(void)
824{
825 int ret;
826
sonic zhangccf68e52009-12-09 12:31:28 -0800827 pr_info("Serial: Blackfin uart over sport driver\n");
828
Bryan Wu2f351742008-04-30 00:52:12 -0700829 ret = uart_register_driver(&sport_uart_reg);
sonic zhangccf68e52009-12-09 12:31:28 -0800830 if (ret) {
831 pr_err("failed to register %s:%d\n",
Bryan Wu2f351742008-04-30 00:52:12 -0700832 sport_uart_reg.driver_name, ret);
833 return ret;
834 }
835
836 ret = platform_driver_register(&sport_uart_driver);
sonic zhangccf68e52009-12-09 12:31:28 -0800837 if (ret) {
838 pr_err("failed to register sport uart driver:%d\n", ret);
Bryan Wu2f351742008-04-30 00:52:12 -0700839 uart_unregister_driver(&sport_uart_reg);
840 }
841
Bryan Wu2f351742008-04-30 00:52:12 -0700842 return ret;
843}
sonic zhangccf68e52009-12-09 12:31:28 -0800844module_init(sport_uart_init);
Bryan Wu2f351742008-04-30 00:52:12 -0700845
846static void __exit sport_uart_exit(void)
847{
Bryan Wu2f351742008-04-30 00:52:12 -0700848 platform_driver_unregister(&sport_uart_driver);
849 uart_unregister_driver(&sport_uart_reg);
850}
Bryan Wu2f351742008-04-30 00:52:12 -0700851module_exit(sport_uart_exit);
852
sonic zhangccf68e52009-12-09 12:31:28 -0800853MODULE_AUTHOR("Sonic Zhang, Roy Huang");
854MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
Bryan Wu2f351742008-04-30 00:52:12 -0700855MODULE_LICENSE("GPL");