blob: e57fb3d228e2dd0881ab10f3410a457b46e0e405 [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
Mike Frysinger2ce1efc2010-03-09 12:25:38 -050037#include <asm/bfin_sport.h>
Bryan Wu2f351742008-04-30 00:52:12 -070038#include <asm/delay.h>
39#include <asm/portmux.h>
40
41#include "bfin_sport_uart.h"
42
Bryan Wu2f351742008-04-30 00:52:12 -070043struct sport_uart_port {
44 struct uart_port port;
Bryan Wu2f351742008-04-30 00:52:12 -070045 int err_irq;
sonic zhangccf68e52009-12-09 12:31:28 -080046 unsigned short csize;
47 unsigned short rxmask;
48 unsigned short txmask1;
49 unsigned short txmask2;
50 unsigned char stopb;
51/* unsigned char parib; */
Sonic Zhang1f7d1c82010-03-09 12:25:33 -050052#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
53 int cts_pin;
54 int rts_pin;
55#endif
Bryan Wu2f351742008-04-30 00:52:12 -070056};
57
Sonic Zhang9356c462010-03-09 12:25:37 -050058static int sport_uart_tx_chars(struct sport_uart_port *up);
Bryan Wu2f351742008-04-30 00:52:12 -070059static void sport_stop_tx(struct uart_port *port);
60
61static inline void tx_one_byte(struct sport_uart_port *up, unsigned int value)
62{
sonic zhangccf68e52009-12-09 12:31:28 -080063 pr_debug("%s value:%x, mask1=0x%x, mask2=0x%x\n", __func__, value,
64 up->txmask1, up->txmask2);
65
66 /* Place Start and Stop bits */
Mike Frysinger4328e3e2009-06-11 13:37:11 +010067 __asm__ __volatile__ (
sonic zhangccf68e52009-12-09 12:31:28 -080068 "%[val] <<= 1;"
69 "%[val] = %[val] & %[mask1];"
70 "%[val] = %[val] | %[mask2];"
71 : [val]"+d"(value)
72 : [mask1]"d"(up->txmask1), [mask2]"d"(up->txmask2)
73 : "ASTAT"
Mike Frysinger4328e3e2009-06-11 13:37:11 +010074 );
Harvey Harrison6ef53062009-01-02 13:49:13 +000075 pr_debug("%s value:%x\n", __func__, value);
Bryan Wu2f351742008-04-30 00:52:12 -070076
77 SPORT_PUT_TX(up, value);
78}
79
sonic zhangccf68e52009-12-09 12:31:28 -080080static inline unsigned char rx_one_byte(struct sport_uart_port *up)
Bryan Wu2f351742008-04-30 00:52:12 -070081{
sonic zhangccf68e52009-12-09 12:31:28 -080082 unsigned int value;
83 unsigned char extract;
Mike Frysinger4328e3e2009-06-11 13:37:11 +010084 u32 tmp_mask1, tmp_mask2, tmp_shift, tmp;
Bryan Wu2f351742008-04-30 00:52:12 -070085
sonic zhangccf68e52009-12-09 12:31:28 -080086 if ((up->csize + up->stopb) > 7)
87 value = SPORT_GET_RX32(up);
88 else
89 value = SPORT_GET_RX(up);
Bryan Wu2f351742008-04-30 00:52:12 -070090
sonic zhangccf68e52009-12-09 12:31:28 -080091 pr_debug("%s value:%x, cs=%d, mask=0x%x\n", __func__, value,
92 up->csize, up->rxmask);
93
94 /* Extract data */
Mike Frysinger4328e3e2009-06-11 13:37:11 +010095 __asm__ __volatile__ (
96 "%[extr] = 0;"
sonic zhangccf68e52009-12-09 12:31:28 -080097 "%[mask1] = %[rxmask];"
98 "%[mask2] = 0x0200(Z);"
Mike Frysinger4328e3e2009-06-11 13:37:11 +010099 "%[shift] = 0;"
100 "LSETUP(.Lloop_s, .Lloop_e) LC0 = %[lc];"
101 ".Lloop_s:"
102 "%[tmp] = extract(%[val], %[mask1].L)(Z);"
103 "%[tmp] <<= %[shift];"
104 "%[extr] = %[extr] | %[tmp];"
105 "%[mask1] = %[mask1] - %[mask2];"
106 ".Lloop_e:"
107 "%[shift] += 1;"
sonic zhangccf68e52009-12-09 12:31:28 -0800108 : [extr]"=&d"(extract), [shift]"=&d"(tmp_shift), [tmp]"=&d"(tmp),
109 [mask1]"=&d"(tmp_mask1), [mask2]"=&d"(tmp_mask2)
110 : [val]"d"(value), [rxmask]"d"(up->rxmask), [lc]"a"(up->csize)
Mike Frysinger4328e3e2009-06-11 13:37:11 +0100111 : "ASTAT", "LB0", "LC0", "LT0"
112 );
Bryan Wu2f351742008-04-30 00:52:12 -0700113
114 pr_debug(" extract:%x\n", extract);
115 return extract;
116}
117
sonic zhangccf68e52009-12-09 12:31:28 -0800118static int sport_uart_setup(struct sport_uart_port *up, int size, int baud_rate)
Bryan Wu2f351742008-04-30 00:52:12 -0700119{
sonic zhangccf68e52009-12-09 12:31:28 -0800120 int tclkdiv, rclkdiv;
121 unsigned int sclk = get_sclk();
Bryan Wu2f351742008-04-30 00:52:12 -0700122
sonic zhangccf68e52009-12-09 12:31:28 -0800123 /* Set TCR1 and TCR2, TFSR is not enabled for uart */
124 SPORT_PUT_TCR1(up, (ITFS | TLSBIT | ITCLK));
125 SPORT_PUT_TCR2(up, size + 1);
Harvey Harrison6ef53062009-01-02 13:49:13 +0000126 pr_debug("%s TCR1:%x, TCR2:%x\n", __func__, SPORT_GET_TCR1(up), SPORT_GET_TCR2(up));
Bryan Wu2f351742008-04-30 00:52:12 -0700127
128 /* Set RCR1 and RCR2 */
129 SPORT_PUT_RCR1(up, (RCKFE | LARFS | LRFS | RFSR | IRCLK));
sonic zhangccf68e52009-12-09 12:31:28 -0800130 SPORT_PUT_RCR2(up, (size + 1) * 2 - 1);
Harvey Harrison6ef53062009-01-02 13:49:13 +0000131 pr_debug("%s RCR1:%x, RCR2:%x\n", __func__, SPORT_GET_RCR1(up), SPORT_GET_RCR2(up));
Bryan Wu2f351742008-04-30 00:52:12 -0700132
sonic zhangccf68e52009-12-09 12:31:28 -0800133 tclkdiv = sclk / (2 * baud_rate) - 1;
134 rclkdiv = sclk / (2 * baud_rate * 2) - 1;
Bryan Wu2f351742008-04-30 00:52:12 -0700135 SPORT_PUT_TCLKDIV(up, tclkdiv);
Bryan Wu2f351742008-04-30 00:52:12 -0700136 SPORT_PUT_RCLKDIV(up, rclkdiv);
137 SSYNC();
sonic zhangccf68e52009-12-09 12:31:28 -0800138 pr_debug("%s sclk:%d, baud_rate:%d, tclkdiv:%d, rclkdiv:%d\n",
139 __func__, sclk, baud_rate, tclkdiv, rclkdiv);
Bryan Wu2f351742008-04-30 00:52:12 -0700140
141 return 0;
142}
143
144static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
145{
146 struct sport_uart_port *up = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700147 struct tty_struct *tty = up->port.state->port.tty;
Bryan Wu2f351742008-04-30 00:52:12 -0700148 unsigned int ch;
149
sonic zhangccf68e52009-12-09 12:31:28 -0800150 spin_lock(&up->port.lock);
151
152 while (SPORT_GET_STAT(up) & RXNE) {
Bryan Wu2f351742008-04-30 00:52:12 -0700153 ch = rx_one_byte(up);
154 up->port.icount.rx++;
155
sonic zhangccf68e52009-12-09 12:31:28 -0800156 if (!uart_handle_sysrq_char(&up->port, ch))
Bryan Wu2f351742008-04-30 00:52:12 -0700157 tty_insert_flip_char(tty, ch, TTY_NORMAL);
sonic zhangccf68e52009-12-09 12:31:28 -0800158 }
Bryan Wu2f351742008-04-30 00:52:12 -0700159 tty_flip_buffer_push(tty);
160
sonic zhangccf68e52009-12-09 12:31:28 -0800161 spin_unlock(&up->port.lock);
162
Bryan Wu2f351742008-04-30 00:52:12 -0700163 return IRQ_HANDLED;
164}
165
166static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
167{
sonic zhangccf68e52009-12-09 12:31:28 -0800168 struct sport_uart_port *up = dev_id;
169
170 spin_lock(&up->port.lock);
171 sport_uart_tx_chars(up);
172 spin_unlock(&up->port.lock);
Bryan Wu2f351742008-04-30 00:52:12 -0700173
174 return IRQ_HANDLED;
175}
176
177static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
178{
179 struct sport_uart_port *up = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700180 struct tty_struct *tty = up->port.state->port.tty;
Bryan Wu2f351742008-04-30 00:52:12 -0700181 unsigned int stat = SPORT_GET_STAT(up);
182
sonic zhangccf68e52009-12-09 12:31:28 -0800183 spin_lock(&up->port.lock);
184
Bryan Wu2f351742008-04-30 00:52:12 -0700185 /* Overflow in RX FIFO */
186 if (stat & ROVF) {
187 up->port.icount.overrun++;
188 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
189 SPORT_PUT_STAT(up, ROVF); /* Clear ROVF bit */
190 }
191 /* These should not happen */
192 if (stat & (TOVF | TUVF | RUVF)) {
sonic zhangccf68e52009-12-09 12:31:28 -0800193 pr_err("SPORT Error:%s %s %s\n",
194 (stat & TOVF) ? "TX overflow" : "",
195 (stat & TUVF) ? "TX underflow" : "",
196 (stat & RUVF) ? "RX underflow" : "");
Bryan Wu2f351742008-04-30 00:52:12 -0700197 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
198 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
199 }
200 SSYNC();
201
sonic zhangccf68e52009-12-09 12:31:28 -0800202 spin_unlock(&up->port.lock);
Bryan Wu2f351742008-04-30 00:52:12 -0700203 return IRQ_HANDLED;
204}
205
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500206#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
207static unsigned int sport_get_mctrl(struct uart_port *port)
208{
209 struct sport_uart_port *up = (struct sport_uart_port *)port;
210 if (up->cts_pin < 0)
211 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
212
213 /* CTS PIN is negative assertive. */
214 if (SPORT_UART_GET_CTS(up))
215 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
216 else
217 return TIOCM_DSR | TIOCM_CAR;
218}
219
220static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
221{
222 struct sport_uart_port *up = (struct sport_uart_port *)port;
223 if (up->rts_pin < 0)
224 return;
225
226 /* RTS PIN is negative assertive. */
227 if (mctrl & TIOCM_RTS)
228 SPORT_UART_ENABLE_RTS(up);
229 else
230 SPORT_UART_DISABLE_RTS(up);
231}
232
233/*
234 * Handle any change of modem status signal.
235 */
236static irqreturn_t sport_mctrl_cts_int(int irq, void *dev_id)
237{
238 struct sport_uart_port *up = (struct sport_uart_port *)dev_id;
239 unsigned int status;
240
241 status = sport_get_mctrl(&up->port);
242 uart_handle_cts_change(&up->port, status & TIOCM_CTS);
243
244 return IRQ_HANDLED;
245}
246#else
247static unsigned int sport_get_mctrl(struct uart_port *port)
248{
249 pr_debug("%s enter\n", __func__);
250 return TIOCM_CTS | TIOCM_CD | TIOCM_DSR;
251}
252
253static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
254{
255 pr_debug("%s enter\n", __func__);
256}
257#endif
258
Bryan Wu2f351742008-04-30 00:52:12 -0700259/* Reqeust IRQ, Setup clock */
260static int sport_startup(struct uart_port *port)
261{
262 struct sport_uart_port *up = (struct sport_uart_port *)port;
sonic zhangccf68e52009-12-09 12:31:28 -0800263 int ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700264
Harvey Harrison6ef53062009-01-02 13:49:13 +0000265 pr_debug("%s enter\n", __func__);
sonic zhangccf68e52009-12-09 12:31:28 -0800266 ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
267 "SPORT_UART_RX", up);
268 if (ret) {
269 dev_err(port->dev, "unable to request SPORT RX interrupt\n");
270 return ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700271 }
272
sonic zhangccf68e52009-12-09 12:31:28 -0800273 ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
274 "SPORT_UART_TX", up);
275 if (ret) {
276 dev_err(port->dev, "unable to request SPORT TX interrupt\n");
Bryan Wu2f351742008-04-30 00:52:12 -0700277 goto fail1;
278 }
279
sonic zhangccf68e52009-12-09 12:31:28 -0800280 ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
281 "SPORT_UART_STATUS", up);
282 if (ret) {
283 dev_err(port->dev, "unable to request SPORT status interrupt\n");
Bryan Wu2f351742008-04-30 00:52:12 -0700284 goto fail2;
285 }
286
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500287#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
288 if (up->cts_pin >= 0) {
289 if (request_irq(gpio_to_irq(up->cts_pin),
290 sport_mctrl_cts_int,
291 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
292 IRQF_DISABLED, "BFIN_SPORT_UART_CTS", up)) {
293 up->cts_pin = -1;
294 dev_info(port->dev, "Unable to attach BlackFin UART \
295 over SPORT CTS interrupt. So, disable it.\n");
296 }
297 }
298 if (up->rts_pin >= 0)
299 gpio_direction_output(up->rts_pin, 0);
300#endif
301
Bryan Wu2f351742008-04-30 00:52:12 -0700302 return 0;
sonic zhangccf68e52009-12-09 12:31:28 -0800303 fail2:
304 free_irq(up->port.irq+1, up);
305 fail1:
306 free_irq(up->port.irq, up);
Bryan Wu2f351742008-04-30 00:52:12 -0700307
sonic zhangccf68e52009-12-09 12:31:28 -0800308 return ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700309}
310
Sonic Zhang9356c462010-03-09 12:25:37 -0500311/*
312 * sport_uart_tx_chars
313 *
314 * ret 1 means need to enable sport.
315 * ret 0 means do nothing.
316 */
317static int sport_uart_tx_chars(struct sport_uart_port *up)
Bryan Wu2f351742008-04-30 00:52:12 -0700318{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700319 struct circ_buf *xmit = &up->port.state->xmit;
Bryan Wu2f351742008-04-30 00:52:12 -0700320
321 if (SPORT_GET_STAT(up) & TXF)
Sonic Zhang9356c462010-03-09 12:25:37 -0500322 return 0;
Bryan Wu2f351742008-04-30 00:52:12 -0700323
324 if (up->port.x_char) {
325 tx_one_byte(up, up->port.x_char);
326 up->port.icount.tx++;
327 up->port.x_char = 0;
Sonic Zhang9356c462010-03-09 12:25:37 -0500328 return 1;
Bryan Wu2f351742008-04-30 00:52:12 -0700329 }
330
331 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Sonic Zhang3f3a9782010-03-09 12:25:29 -0500332 /* The waiting loop to stop SPORT TX from TX interrupt is
333 * too long. This may block SPORT RX interrupts and cause
334 * RX FIFO overflow. So, do stop sport TX only after the last
335 * char in TX FIFO is moved into the shift register.
336 */
337 if (SPORT_GET_STAT(up) & TXHRE)
338 sport_stop_tx(&up->port);
Sonic Zhang9356c462010-03-09 12:25:37 -0500339 return 0;
Bryan Wu2f351742008-04-30 00:52:12 -0700340 }
341
342 while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
343 tx_one_byte(up, xmit->buf[xmit->tail]);
344 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
345 up->port.icount.tx++;
346 }
347
348 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
349 uart_write_wakeup(&up->port);
Sonic Zhang9356c462010-03-09 12:25:37 -0500350
351 return 1;
Bryan Wu2f351742008-04-30 00:52:12 -0700352}
353
354static unsigned int sport_tx_empty(struct uart_port *port)
355{
356 struct sport_uart_port *up = (struct sport_uart_port *)port;
357 unsigned int stat;
358
359 stat = SPORT_GET_STAT(up);
Harvey Harrison6ef53062009-01-02 13:49:13 +0000360 pr_debug("%s stat:%04x\n", __func__, stat);
Bryan Wu2f351742008-04-30 00:52:12 -0700361 if (stat & TXHRE) {
362 return TIOCSER_TEMT;
363 } else
364 return 0;
365}
366
Bryan Wu2f351742008-04-30 00:52:12 -0700367static void sport_stop_tx(struct uart_port *port)
368{
369 struct sport_uart_port *up = (struct sport_uart_port *)port;
Bryan Wu2f351742008-04-30 00:52:12 -0700370
Harvey Harrison6ef53062009-01-02 13:49:13 +0000371 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700372
Sonic Zhang9356c462010-03-09 12:25:37 -0500373 if (!(SPORT_GET_TCR1(up) & TSPEN))
374 return;
375
Bryan Wu2f351742008-04-30 00:52:12 -0700376 /* Although the hold register is empty, last byte is still in shift
sonic zhangccf68e52009-12-09 12:31:28 -0800377 * register and not sent out yet. So, put a dummy data into TX FIFO.
378 * Then, sport tx stops when last byte is shift out and the dummy
379 * data is moved into the shift register.
380 */
381 SPORT_PUT_TX(up, 0xffff);
382 while (!(SPORT_GET_STAT(up) & TXHRE))
383 cpu_relax();
Bryan Wu2f351742008-04-30 00:52:12 -0700384
385 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
386 SSYNC();
387
388 return;
389}
390
391static void sport_start_tx(struct uart_port *port)
392{
393 struct sport_uart_port *up = (struct sport_uart_port *)port;
394
Harvey Harrison6ef53062009-01-02 13:49:13 +0000395 pr_debug("%s enter\n", __func__);
sonic zhangccf68e52009-12-09 12:31:28 -0800396
Bryan Wu2f351742008-04-30 00:52:12 -0700397 /* Write data into SPORT FIFO before enable SPROT to transmit */
Sonic Zhang9356c462010-03-09 12:25:37 -0500398 if (sport_uart_tx_chars(up)) {
399 /* Enable transmit, then an interrupt will generated */
400 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
401 SSYNC();
402 }
Bryan Wu2f351742008-04-30 00:52:12 -0700403
Harvey Harrison6ef53062009-01-02 13:49:13 +0000404 pr_debug("%s exit\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700405}
406
407static void sport_stop_rx(struct uart_port *port)
408{
409 struct sport_uart_port *up = (struct sport_uart_port *)port;
410
Harvey Harrison6ef53062009-01-02 13:49:13 +0000411 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700412 /* Disable sport to stop rx */
413 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
414 SSYNC();
415}
416
417static void sport_enable_ms(struct uart_port *port)
418{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000419 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700420}
421
422static void sport_break_ctl(struct uart_port *port, int break_state)
423{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000424 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700425}
426
427static void sport_shutdown(struct uart_port *port)
428{
429 struct sport_uart_port *up = (struct sport_uart_port *)port;
430
sonic zhangccf68e52009-12-09 12:31:28 -0800431 dev_dbg(port->dev, "%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700432
433 /* Disable sport */
434 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
435 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
436 SSYNC();
437
sonic zhangccf68e52009-12-09 12:31:28 -0800438 free_irq(up->port.irq, up);
439 free_irq(up->port.irq+1, up);
Bryan Wu2f351742008-04-30 00:52:12 -0700440 free_irq(up->err_irq, up);
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500441#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
442 if (up->cts_pin >= 0)
443 free_irq(gpio_to_irq(up->cts_pin), up);
444#endif
Bryan Wu2f351742008-04-30 00:52:12 -0700445}
446
Bryan Wu2f351742008-04-30 00:52:12 -0700447static const char *sport_type(struct uart_port *port)
448{
449 struct sport_uart_port *up = (struct sport_uart_port *)port;
450
Harvey Harrison6ef53062009-01-02 13:49:13 +0000451 pr_debug("%s enter\n", __func__);
sonic zhangccf68e52009-12-09 12:31:28 -0800452 return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
Bryan Wu2f351742008-04-30 00:52:12 -0700453}
454
455static void sport_release_port(struct uart_port *port)
456{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000457 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700458}
459
460static int sport_request_port(struct uart_port *port)
461{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000462 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700463 return 0;
464}
465
466static void sport_config_port(struct uart_port *port, int flags)
467{
468 struct sport_uart_port *up = (struct sport_uart_port *)port;
469
Harvey Harrison6ef53062009-01-02 13:49:13 +0000470 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700471 up->port.type = PORT_BFIN_SPORT;
472}
473
474static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
475{
Harvey Harrison6ef53062009-01-02 13:49:13 +0000476 pr_debug("%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700477 return 0;
478}
479
sonic zhangccf68e52009-12-09 12:31:28 -0800480static void sport_set_termios(struct uart_port *port,
481 struct ktermios *termios, struct ktermios *old)
482{
483 struct sport_uart_port *up = (struct sport_uart_port *)port;
484 unsigned long flags;
485 int i;
486
487 pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
488
489 switch (termios->c_cflag & CSIZE) {
490 case CS8:
491 up->csize = 8;
492 break;
493 case CS7:
494 up->csize = 7;
495 break;
496 case CS6:
497 up->csize = 6;
498 break;
499 case CS5:
500 up->csize = 5;
501 break;
502 default:
503 pr_warning("requested word length not supported\n");
504 }
505
506 if (termios->c_cflag & CSTOPB) {
507 up->stopb = 1;
508 }
509 if (termios->c_cflag & PARENB) {
510 pr_warning("PAREN bits is not supported yet\n");
511 /* up->parib = 1; */
512 }
513
Sonic Zhang9498dc92010-03-09 12:25:34 -0500514 spin_lock_irqsave(&up->port.lock, flags);
515
Mike Frysinger60bd9402010-03-09 12:25:36 -0500516 port->read_status_mask = 0;
sonic zhangccf68e52009-12-09 12:31:28 -0800517
518 /*
519 * Characters to ignore
520 */
521 port->ignore_status_mask = 0;
sonic zhangccf68e52009-12-09 12:31:28 -0800522
523 /* RX extract mask */
524 up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
525 /* TX masks, 8 bit data and 1 bit stop for example:
526 * mask1 = b#0111111110
527 * mask2 = b#1000000000
528 */
529 for (i = 0, up->txmask1 = 0; i < up->csize; i++)
530 up->txmask1 |= (1<<i);
531 up->txmask2 = (1<<i);
532 if (up->stopb) {
533 ++i;
534 up->txmask2 |= (1<<i);
535 }
536 up->txmask1 <<= 1;
537 up->txmask2 <<= 1;
538 /* uart baud rate */
539 port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
540
sonic zhangccf68e52009-12-09 12:31:28 -0800541 /* Disable UART */
542 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
543 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
544
545 sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
546
547 /* driver TX line high after config, one dummy data is
548 * necessary to stop sport after shift one byte
549 */
550 SPORT_PUT_TX(up, 0xffff);
551 SPORT_PUT_TX(up, 0xffff);
552 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
553 SSYNC();
554 while (!(SPORT_GET_STAT(up) & TXHRE))
555 cpu_relax();
556 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
557 SSYNC();
558
559 /* Port speed changed, update the per-port timeout. */
560 uart_update_timeout(port, termios->c_cflag, port->uartclk);
561
562 /* Enable sport rx */
563 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
564 SSYNC();
565
566 spin_unlock_irqrestore(&up->port.lock, flags);
567}
568
Bryan Wu2f351742008-04-30 00:52:12 -0700569struct uart_ops sport_uart_ops = {
570 .tx_empty = sport_tx_empty,
571 .set_mctrl = sport_set_mctrl,
572 .get_mctrl = sport_get_mctrl,
573 .stop_tx = sport_stop_tx,
574 .start_tx = sport_start_tx,
575 .stop_rx = sport_stop_rx,
576 .enable_ms = sport_enable_ms,
577 .break_ctl = sport_break_ctl,
578 .startup = sport_startup,
579 .shutdown = sport_shutdown,
580 .set_termios = sport_set_termios,
581 .type = sport_type,
582 .release_port = sport_release_port,
583 .request_port = sport_request_port,
584 .config_port = sport_config_port,
585 .verify_port = sport_verify_port,
586};
587
sonic zhangccf68e52009-12-09 12:31:28 -0800588#define BFIN_SPORT_UART_MAX_PORTS 4
589
590static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
591
592#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
Sonic Zhangb59588a2010-03-09 12:25:32 -0500593#define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
594
sonic zhangccf68e52009-12-09 12:31:28 -0800595static int __init
596sport_uart_console_setup(struct console *co, char *options)
597{
598 struct sport_uart_port *up;
599 int baud = 57600;
600 int bits = 8;
601 int parity = 'n';
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500602# ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
603 int flow = 'r';
604# else
sonic zhangccf68e52009-12-09 12:31:28 -0800605 int flow = 'n';
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500606# endif
sonic zhangccf68e52009-12-09 12:31:28 -0800607
608 /* Check whether an invalid uart number has been specified */
609 if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
610 return -ENODEV;
611
612 up = bfin_sport_uart_ports[co->index];
613 if (!up)
614 return -ENODEV;
615
616 if (options)
617 uart_parse_options(options, &baud, &parity, &bits, &flow);
618
619 return uart_set_options(&up->port, co, baud, parity, bits, flow);
620}
621
622static void sport_uart_console_putchar(struct uart_port *port, int ch)
623{
624 struct sport_uart_port *up = (struct sport_uart_port *)port;
625
626 while (SPORT_GET_STAT(up) & TXF)
627 barrier();
628
629 tx_one_byte(up, ch);
630}
631
632/*
633 * Interrupts are disabled on entering
634 */
635static void
636sport_uart_console_write(struct console *co, const char *s, unsigned int count)
637{
638 struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
639 unsigned long flags;
640
641 spin_lock_irqsave(&up->port.lock, flags);
642
643 if (SPORT_GET_TCR1(up) & TSPEN)
644 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
645 else {
646 /* dummy data to start sport */
647 while (SPORT_GET_STAT(up) & TXF)
648 barrier();
649 SPORT_PUT_TX(up, 0xffff);
650 /* Enable transmit, then an interrupt will generated */
651 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
652 SSYNC();
653
654 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
655
656 /* Although the hold register is empty, last byte is still in shift
657 * register and not sent out yet. So, put a dummy data into TX FIFO.
658 * Then, sport tx stops when last byte is shift out and the dummy
659 * data is moved into the shift register.
660 */
661 while (SPORT_GET_STAT(up) & TXF)
662 barrier();
663 SPORT_PUT_TX(up, 0xffff);
664 while (!(SPORT_GET_STAT(up) & TXHRE))
665 barrier();
666
667 /* Stop sport tx transfer */
668 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
669 SSYNC();
Bryan Wu2f351742008-04-30 00:52:12 -0700670 }
sonic zhangccf68e52009-12-09 12:31:28 -0800671
672 spin_unlock_irqrestore(&up->port.lock, flags);
673}
674
675static struct uart_driver sport_uart_reg;
676
677static struct console sport_uart_console = {
678 .name = DEVICE_NAME,
679 .write = sport_uart_console_write,
680 .device = uart_console_device,
681 .setup = sport_uart_console_setup,
682 .flags = CON_PRINTBUFFER,
683 .index = -1,
684 .data = &sport_uart_reg,
Bryan Wu2f351742008-04-30 00:52:12 -0700685};
686
sonic zhangccf68e52009-12-09 12:31:28 -0800687#define SPORT_UART_CONSOLE (&sport_uart_console)
688#else
689#define SPORT_UART_CONSOLE NULL
690#endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
691
692
Bryan Wu2f351742008-04-30 00:52:12 -0700693static struct uart_driver sport_uart_reg = {
694 .owner = THIS_MODULE,
sonic zhangccf68e52009-12-09 12:31:28 -0800695 .driver_name = DRV_NAME,
696 .dev_name = DEVICE_NAME,
Bryan Wu2f351742008-04-30 00:52:12 -0700697 .major = 204,
698 .minor = 84,
sonic zhangccf68e52009-12-09 12:31:28 -0800699 .nr = BFIN_SPORT_UART_MAX_PORTS,
700 .cons = SPORT_UART_CONSOLE,
Bryan Wu2f351742008-04-30 00:52:12 -0700701};
702
sonic zhangccf68e52009-12-09 12:31:28 -0800703#ifdef CONFIG_PM
704static int sport_uart_suspend(struct device *dev)
Bryan Wu2f351742008-04-30 00:52:12 -0700705{
sonic zhangccf68e52009-12-09 12:31:28 -0800706 struct sport_uart_port *sport = dev_get_drvdata(dev);
Bryan Wu2f351742008-04-30 00:52:12 -0700707
sonic zhangccf68e52009-12-09 12:31:28 -0800708 dev_dbg(dev, "%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700709 if (sport)
710 uart_suspend_port(&sport_uart_reg, &sport->port);
711
712 return 0;
713}
714
sonic zhangccf68e52009-12-09 12:31:28 -0800715static int sport_uart_resume(struct device *dev)
Bryan Wu2f351742008-04-30 00:52:12 -0700716{
sonic zhangccf68e52009-12-09 12:31:28 -0800717 struct sport_uart_port *sport = dev_get_drvdata(dev);
Bryan Wu2f351742008-04-30 00:52:12 -0700718
sonic zhangccf68e52009-12-09 12:31:28 -0800719 dev_dbg(dev, "%s enter\n", __func__);
Bryan Wu2f351742008-04-30 00:52:12 -0700720 if (sport)
721 uart_resume_port(&sport_uart_reg, &sport->port);
722
723 return 0;
724}
725
sonic zhangccf68e52009-12-09 12:31:28 -0800726static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
727 .suspend = sport_uart_suspend,
728 .resume = sport_uart_resume,
729};
730#endif
Bryan Wu2f351742008-04-30 00:52:12 -0700731
sonic zhangccf68e52009-12-09 12:31:28 -0800732static int __devinit sport_uart_probe(struct platform_device *pdev)
733{
734 struct resource *res;
735 struct sport_uart_port *sport;
736 int ret = 0;
737
738 dev_dbg(&pdev->dev, "%s enter\n", __func__);
739
740 if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
741 dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
742 return -ENOENT;
743 }
744
745 if (bfin_sport_uart_ports[pdev->id] == NULL) {
746 bfin_sport_uart_ports[pdev->id] =
Sonic Zhangf4d10ca2010-03-09 12:25:35 -0500747 kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
sonic zhangccf68e52009-12-09 12:31:28 -0800748 sport = bfin_sport_uart_ports[pdev->id];
749 if (!sport) {
750 dev_err(&pdev->dev,
Sonic Zhangf4d10ca2010-03-09 12:25:35 -0500751 "Fail to malloc sport_uart_port\n");
sonic zhangccf68e52009-12-09 12:31:28 -0800752 return -ENOMEM;
753 }
754
755 ret = peripheral_request_list(
756 (unsigned short *)pdev->dev.platform_data, DRV_NAME);
757 if (ret) {
758 dev_err(&pdev->dev,
759 "Fail to request SPORT peripherals\n");
760 goto out_error_free_mem;
761 }
762
763 spin_lock_init(&sport->port.lock);
764 sport->port.fifosize = SPORT_TX_FIFO_SIZE,
765 sport->port.ops = &sport_uart_ops;
766 sport->port.line = pdev->id;
767 sport->port.iotype = UPIO_MEM;
768 sport->port.flags = UPF_BOOT_AUTOCONF;
769
770 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
771 if (res == NULL) {
772 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
773 ret = -ENOENT;
774 goto out_error_free_peripherals;
775 }
776
Tobias Klausere1144742010-05-11 09:10:23 +0200777 sport->port.membase = ioremap(res->start, resource_size(res));
sonic zhangccf68e52009-12-09 12:31:28 -0800778 if (!sport->port.membase) {
779 dev_err(&pdev->dev, "Cannot map sport IO\n");
780 ret = -ENXIO;
781 goto out_error_free_peripherals;
782 }
Sonic Zhange8126b32010-03-09 12:25:31 -0500783 sport->port.mapbase = res->start;
sonic zhangccf68e52009-12-09 12:31:28 -0800784
785 sport->port.irq = platform_get_irq(pdev, 0);
786 if (sport->port.irq < 0) {
787 dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
788 ret = -ENOENT;
789 goto out_error_unmap;
790 }
791
792 sport->err_irq = platform_get_irq(pdev, 1);
793 if (sport->err_irq < 0) {
794 dev_err(&pdev->dev, "No sport status IRQ specified\n");
795 ret = -ENOENT;
796 goto out_error_unmap;
797 }
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500798#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
799 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
800 if (res == NULL)
801 sport->cts_pin = -1;
802 else
803 sport->cts_pin = res->start;
804
805 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
806 if (res == NULL)
807 sport->rts_pin = -1;
808 else
809 sport->rts_pin = res->start;
810
811 if (sport->rts_pin >= 0)
812 gpio_request(sport->rts_pin, DRV_NAME);
813#endif
sonic zhangccf68e52009-12-09 12:31:28 -0800814 }
815
816#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
817 if (!is_early_platform_device(pdev)) {
818#endif
819 sport = bfin_sport_uart_ports[pdev->id];
820 sport->port.dev = &pdev->dev;
821 dev_set_drvdata(&pdev->dev, sport);
822 ret = uart_add_one_port(&sport_uart_reg, &sport->port);
823#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
824 }
825#endif
826 if (!ret)
827 return 0;
828
829 if (sport) {
830out_error_unmap:
831 iounmap(sport->port.membase);
832out_error_free_peripherals:
833 peripheral_free_list(
834 (unsigned short *)pdev->dev.platform_data);
835out_error_free_mem:
836 kfree(sport);
837 bfin_sport_uart_ports[pdev->id] = NULL;
838 }
839
840 return ret;
Bryan Wu2f351742008-04-30 00:52:12 -0700841}
842
sonic zhangccf68e52009-12-09 12:31:28 -0800843static int __devexit sport_uart_remove(struct platform_device *pdev)
Bryan Wu2f351742008-04-30 00:52:12 -0700844{
sonic zhangccf68e52009-12-09 12:31:28 -0800845 struct sport_uart_port *sport = platform_get_drvdata(pdev);
Bryan Wu2f351742008-04-30 00:52:12 -0700846
sonic zhangccf68e52009-12-09 12:31:28 -0800847 dev_dbg(&pdev->dev, "%s enter\n", __func__);
848 dev_set_drvdata(&pdev->dev, NULL);
Bryan Wu2f351742008-04-30 00:52:12 -0700849
sonic zhangccf68e52009-12-09 12:31:28 -0800850 if (sport) {
Bryan Wu2f351742008-04-30 00:52:12 -0700851 uart_remove_one_port(&sport_uart_reg, &sport->port);
Sonic Zhang1f7d1c82010-03-09 12:25:33 -0500852#ifdef CONFIG_SERIAL_BFIN_CTSRTS
853 if (sport->rts_pin >= 0)
854 gpio_free(sport->rts_pin);
855#endif
sonic zhangccf68e52009-12-09 12:31:28 -0800856 iounmap(sport->port.membase);
857 peripheral_free_list(
858 (unsigned short *)pdev->dev.platform_data);
859 kfree(sport);
860 bfin_sport_uart_ports[pdev->id] = NULL;
861 }
Bryan Wu2f351742008-04-30 00:52:12 -0700862
863 return 0;
864}
865
866static struct platform_driver sport_uart_driver = {
867 .probe = sport_uart_probe,
sonic zhangccf68e52009-12-09 12:31:28 -0800868 .remove = __devexit_p(sport_uart_remove),
Bryan Wu2f351742008-04-30 00:52:12 -0700869 .driver = {
870 .name = DRV_NAME,
sonic zhangccf68e52009-12-09 12:31:28 -0800871#ifdef CONFIG_PM
872 .pm = &bfin_sport_uart_dev_pm_ops,
873#endif
Bryan Wu2f351742008-04-30 00:52:12 -0700874 },
875};
876
sonic zhangccf68e52009-12-09 12:31:28 -0800877#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
878static __initdata struct early_platform_driver early_sport_uart_driver = {
Sonic Zhangb59588a2010-03-09 12:25:32 -0500879 .class_str = CLASS_BFIN_SPORT_CONSOLE,
sonic zhangccf68e52009-12-09 12:31:28 -0800880 .pdrv = &sport_uart_driver,
881 .requested_id = EARLY_PLATFORM_ID_UNSET,
882};
883
884static int __init sport_uart_rs_console_init(void)
885{
886 early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
887
Sonic Zhangb59588a2010-03-09 12:25:32 -0500888 early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
889 BFIN_SPORT_UART_MAX_PORTS, 0);
sonic zhangccf68e52009-12-09 12:31:28 -0800890
891 register_console(&sport_uart_console);
892
893 return 0;
894}
895console_initcall(sport_uart_rs_console_init);
896#endif
897
Bryan Wu2f351742008-04-30 00:52:12 -0700898static int __init sport_uart_init(void)
899{
900 int ret;
901
Sonic Zhange8126b32010-03-09 12:25:31 -0500902 pr_info("Blackfin uart over sport driver\n");
sonic zhangccf68e52009-12-09 12:31:28 -0800903
Bryan Wu2f351742008-04-30 00:52:12 -0700904 ret = uart_register_driver(&sport_uart_reg);
sonic zhangccf68e52009-12-09 12:31:28 -0800905 if (ret) {
906 pr_err("failed to register %s:%d\n",
Bryan Wu2f351742008-04-30 00:52:12 -0700907 sport_uart_reg.driver_name, ret);
908 return ret;
909 }
910
911 ret = platform_driver_register(&sport_uart_driver);
sonic zhangccf68e52009-12-09 12:31:28 -0800912 if (ret) {
913 pr_err("failed to register sport uart driver:%d\n", ret);
Bryan Wu2f351742008-04-30 00:52:12 -0700914 uart_unregister_driver(&sport_uart_reg);
915 }
916
Bryan Wu2f351742008-04-30 00:52:12 -0700917 return ret;
918}
sonic zhangccf68e52009-12-09 12:31:28 -0800919module_init(sport_uart_init);
Bryan Wu2f351742008-04-30 00:52:12 -0700920
921static void __exit sport_uart_exit(void)
922{
Bryan Wu2f351742008-04-30 00:52:12 -0700923 platform_driver_unregister(&sport_uart_driver);
924 uart_unregister_driver(&sport_uart_reg);
925}
Bryan Wu2f351742008-04-30 00:52:12 -0700926module_exit(sport_uart_exit);
927
sonic zhangccf68e52009-12-09 12:31:28 -0800928MODULE_AUTHOR("Sonic Zhang, Roy Huang");
929MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
Bryan Wu2f351742008-04-30 00:52:12 -0700930MODULE_LICENSE("GPL");