bfin_sport_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * Blackfin On-Chip Sport Emulated UART Driver
  3. *
  4. * Copyright 2006-2009 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. /*
  11. * This driver and the hardware supported are in term of EE-191 of ADI.
  12. * http://www.analog.com/UploadedFiles/Application_Notes/399447663EE191.pdf
  13. * This application note describe how to implement a UART on a Sharc DSP,
  14. * but this driver is implemented on Blackfin Processor.
  15. * Transmit Frame Sync is not used by this driver to transfer data out.
  16. */
  17. /* #define DEBUG */
  18. #define DRV_NAME "bfin-sport-uart"
  19. #define DEVICE_NAME "ttySS"
  20. #define pr_fmt(fmt) DRV_NAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/ioport.h>
  23. #include <linux/io.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/sysrq.h>
  27. #include <linux/slab.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/serial_core.h>
  32. #include <asm/delay.h>
  33. #include <asm/portmux.h>
  34. #include "bfin_sport_uart.h"
  35. struct sport_uart_port {
  36. struct uart_port port;
  37. int err_irq;
  38. unsigned short csize;
  39. unsigned short rxmask;
  40. unsigned short txmask1;
  41. unsigned short txmask2;
  42. unsigned char stopb;
  43. /* unsigned char parib; */
  44. };
  45. static void sport_uart_tx_chars(struct sport_uart_port *up);
  46. static void sport_stop_tx(struct uart_port *port);
  47. static inline void tx_one_byte(struct sport_uart_port *up, unsigned int value)
  48. {
  49. pr_debug("%s value:%x, mask1=0x%x, mask2=0x%x\n", __func__, value,
  50. up->txmask1, up->txmask2);
  51. /* Place Start and Stop bits */
  52. __asm__ __volatile__ (
  53. "%[val] <<= 1;"
  54. "%[val] = %[val] & %[mask1];"
  55. "%[val] = %[val] | %[mask2];"
  56. : [val]"+d"(value)
  57. : [mask1]"d"(up->txmask1), [mask2]"d"(up->txmask2)
  58. : "ASTAT"
  59. );
  60. pr_debug("%s value:%x\n", __func__, value);
  61. SPORT_PUT_TX(up, value);
  62. }
  63. static inline unsigned char rx_one_byte(struct sport_uart_port *up)
  64. {
  65. unsigned int value;
  66. unsigned char extract;
  67. u32 tmp_mask1, tmp_mask2, tmp_shift, tmp;
  68. if ((up->csize + up->stopb) > 7)
  69. value = SPORT_GET_RX32(up);
  70. else
  71. value = SPORT_GET_RX(up);
  72. pr_debug("%s value:%x, cs=%d, mask=0x%x\n", __func__, value,
  73. up->csize, up->rxmask);
  74. /* Extract data */
  75. __asm__ __volatile__ (
  76. "%[extr] = 0;"
  77. "%[mask1] = %[rxmask];"
  78. "%[mask2] = 0x0200(Z);"
  79. "%[shift] = 0;"
  80. "LSETUP(.Lloop_s, .Lloop_e) LC0 = %[lc];"
  81. ".Lloop_s:"
  82. "%[tmp] = extract(%[val], %[mask1].L)(Z);"
  83. "%[tmp] <<= %[shift];"
  84. "%[extr] = %[extr] | %[tmp];"
  85. "%[mask1] = %[mask1] - %[mask2];"
  86. ".Lloop_e:"
  87. "%[shift] += 1;"
  88. : [extr]"=&d"(extract), [shift]"=&d"(tmp_shift), [tmp]"=&d"(tmp),
  89. [mask1]"=&d"(tmp_mask1), [mask2]"=&d"(tmp_mask2)
  90. : [val]"d"(value), [rxmask]"d"(up->rxmask), [lc]"a"(up->csize)
  91. : "ASTAT", "LB0", "LC0", "LT0"
  92. );
  93. pr_debug(" extract:%x\n", extract);
  94. return extract;
  95. }
  96. static int sport_uart_setup(struct sport_uart_port *up, int size, int baud_rate)
  97. {
  98. int tclkdiv, rclkdiv;
  99. unsigned int sclk = get_sclk();
  100. /* Set TCR1 and TCR2, TFSR is not enabled for uart */
  101. SPORT_PUT_TCR1(up, (ITFS | TLSBIT | ITCLK));
  102. SPORT_PUT_TCR2(up, size + 1);
  103. pr_debug("%s TCR1:%x, TCR2:%x\n", __func__, SPORT_GET_TCR1(up), SPORT_GET_TCR2(up));
  104. /* Set RCR1 and RCR2 */
  105. SPORT_PUT_RCR1(up, (RCKFE | LARFS | LRFS | RFSR | IRCLK));
  106. SPORT_PUT_RCR2(up, (size + 1) * 2 - 1);
  107. pr_debug("%s RCR1:%x, RCR2:%x\n", __func__, SPORT_GET_RCR1(up), SPORT_GET_RCR2(up));
  108. tclkdiv = sclk / (2 * baud_rate) - 1;
  109. rclkdiv = sclk / (2 * baud_rate * 2) - 1;
  110. SPORT_PUT_TCLKDIV(up, tclkdiv);
  111. SPORT_PUT_RCLKDIV(up, rclkdiv);
  112. SSYNC();
  113. pr_debug("%s sclk:%d, baud_rate:%d, tclkdiv:%d, rclkdiv:%d\n",
  114. __func__, sclk, baud_rate, tclkdiv, rclkdiv);
  115. return 0;
  116. }
  117. static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
  118. {
  119. struct sport_uart_port *up = dev_id;
  120. struct tty_struct *tty = up->port.state->port.tty;
  121. unsigned int ch;
  122. spin_lock(&up->port.lock);
  123. while (SPORT_GET_STAT(up) & RXNE) {
  124. ch = rx_one_byte(up);
  125. up->port.icount.rx++;
  126. if (!uart_handle_sysrq_char(&up->port, ch))
  127. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  128. }
  129. tty_flip_buffer_push(tty);
  130. spin_unlock(&up->port.lock);
  131. return IRQ_HANDLED;
  132. }
  133. static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
  134. {
  135. struct sport_uart_port *up = dev_id;
  136. spin_lock(&up->port.lock);
  137. sport_uart_tx_chars(up);
  138. spin_unlock(&up->port.lock);
  139. return IRQ_HANDLED;
  140. }
  141. static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
  142. {
  143. struct sport_uart_port *up = dev_id;
  144. struct tty_struct *tty = up->port.state->port.tty;
  145. unsigned int stat = SPORT_GET_STAT(up);
  146. spin_lock(&up->port.lock);
  147. /* Overflow in RX FIFO */
  148. if (stat & ROVF) {
  149. up->port.icount.overrun++;
  150. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  151. SPORT_PUT_STAT(up, ROVF); /* Clear ROVF bit */
  152. }
  153. /* These should not happen */
  154. if (stat & (TOVF | TUVF | RUVF)) {
  155. pr_err("SPORT Error:%s %s %s\n",
  156. (stat & TOVF) ? "TX overflow" : "",
  157. (stat & TUVF) ? "TX underflow" : "",
  158. (stat & RUVF) ? "RX underflow" : "");
  159. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  160. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
  161. }
  162. SSYNC();
  163. spin_unlock(&up->port.lock);
  164. return IRQ_HANDLED;
  165. }
  166. /* Reqeust IRQ, Setup clock */
  167. static int sport_startup(struct uart_port *port)
  168. {
  169. struct sport_uart_port *up = (struct sport_uart_port *)port;
  170. int ret;
  171. pr_debug("%s enter\n", __func__);
  172. ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
  173. "SPORT_UART_RX", up);
  174. if (ret) {
  175. dev_err(port->dev, "unable to request SPORT RX interrupt\n");
  176. return ret;
  177. }
  178. ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
  179. "SPORT_UART_TX", up);
  180. if (ret) {
  181. dev_err(port->dev, "unable to request SPORT TX interrupt\n");
  182. goto fail1;
  183. }
  184. ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
  185. "SPORT_UART_STATUS", up);
  186. if (ret) {
  187. dev_err(port->dev, "unable to request SPORT status interrupt\n");
  188. goto fail2;
  189. }
  190. return 0;
  191. fail2:
  192. free_irq(up->port.irq+1, up);
  193. fail1:
  194. free_irq(up->port.irq, up);
  195. return ret;
  196. }
  197. static void sport_uart_tx_chars(struct sport_uart_port *up)
  198. {
  199. struct circ_buf *xmit = &up->port.state->xmit;
  200. if (SPORT_GET_STAT(up) & TXF)
  201. return;
  202. if (up->port.x_char) {
  203. tx_one_byte(up, up->port.x_char);
  204. up->port.icount.tx++;
  205. up->port.x_char = 0;
  206. return;
  207. }
  208. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  209. /* The waiting loop to stop SPORT TX from TX interrupt is
  210. * too long. This may block SPORT RX interrupts and cause
  211. * RX FIFO overflow. So, do stop sport TX only after the last
  212. * char in TX FIFO is moved into the shift register.
  213. */
  214. if (SPORT_GET_STAT(up) & TXHRE)
  215. sport_stop_tx(&up->port);
  216. return;
  217. }
  218. while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
  219. tx_one_byte(up, xmit->buf[xmit->tail]);
  220. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
  221. up->port.icount.tx++;
  222. }
  223. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  224. uart_write_wakeup(&up->port);
  225. }
  226. static unsigned int sport_tx_empty(struct uart_port *port)
  227. {
  228. struct sport_uart_port *up = (struct sport_uart_port *)port;
  229. unsigned int stat;
  230. stat = SPORT_GET_STAT(up);
  231. pr_debug("%s stat:%04x\n", __func__, stat);
  232. if (stat & TXHRE) {
  233. return TIOCSER_TEMT;
  234. } else
  235. return 0;
  236. }
  237. static unsigned int sport_get_mctrl(struct uart_port *port)
  238. {
  239. pr_debug("%s enter\n", __func__);
  240. return (TIOCM_CTS | TIOCM_CD | TIOCM_DSR);
  241. }
  242. static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
  243. {
  244. pr_debug("%s enter\n", __func__);
  245. }
  246. static void sport_stop_tx(struct uart_port *port)
  247. {
  248. struct sport_uart_port *up = (struct sport_uart_port *)port;
  249. pr_debug("%s enter\n", __func__);
  250. /* Although the hold register is empty, last byte is still in shift
  251. * register and not sent out yet. So, put a dummy data into TX FIFO.
  252. * Then, sport tx stops when last byte is shift out and the dummy
  253. * data is moved into the shift register.
  254. */
  255. SPORT_PUT_TX(up, 0xffff);
  256. while (!(SPORT_GET_STAT(up) & TXHRE))
  257. cpu_relax();
  258. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  259. SSYNC();
  260. return;
  261. }
  262. static void sport_start_tx(struct uart_port *port)
  263. {
  264. struct sport_uart_port *up = (struct sport_uart_port *)port;
  265. pr_debug("%s enter\n", __func__);
  266. /* Write data into SPORT FIFO before enable SPROT to transmit */
  267. sport_uart_tx_chars(up);
  268. /* Enable transmit, then an interrupt will generated */
  269. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  270. SSYNC();
  271. pr_debug("%s exit\n", __func__);
  272. }
  273. static void sport_stop_rx(struct uart_port *port)
  274. {
  275. struct sport_uart_port *up = (struct sport_uart_port *)port;
  276. pr_debug("%s enter\n", __func__);
  277. /* Disable sport to stop rx */
  278. SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
  279. SSYNC();
  280. }
  281. static void sport_enable_ms(struct uart_port *port)
  282. {
  283. pr_debug("%s enter\n", __func__);
  284. }
  285. static void sport_break_ctl(struct uart_port *port, int break_state)
  286. {
  287. pr_debug("%s enter\n", __func__);
  288. }
  289. static void sport_shutdown(struct uart_port *port)
  290. {
  291. struct sport_uart_port *up = (struct sport_uart_port *)port;
  292. dev_dbg(port->dev, "%s enter\n", __func__);
  293. /* Disable sport */
  294. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  295. SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
  296. SSYNC();
  297. free_irq(up->port.irq, up);
  298. free_irq(up->port.irq+1, up);
  299. free_irq(up->err_irq, up);
  300. }
  301. static const char *sport_type(struct uart_port *port)
  302. {
  303. struct sport_uart_port *up = (struct sport_uart_port *)port;
  304. pr_debug("%s enter\n", __func__);
  305. return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
  306. }
  307. static void sport_release_port(struct uart_port *port)
  308. {
  309. pr_debug("%s enter\n", __func__);
  310. }
  311. static int sport_request_port(struct uart_port *port)
  312. {
  313. pr_debug("%s enter\n", __func__);
  314. return 0;
  315. }
  316. static void sport_config_port(struct uart_port *port, int flags)
  317. {
  318. struct sport_uart_port *up = (struct sport_uart_port *)port;
  319. pr_debug("%s enter\n", __func__);
  320. up->port.type = PORT_BFIN_SPORT;
  321. }
  322. static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
  323. {
  324. pr_debug("%s enter\n", __func__);
  325. return 0;
  326. }
  327. static void sport_set_termios(struct uart_port *port,
  328. struct ktermios *termios, struct ktermios *old)
  329. {
  330. struct sport_uart_port *up = (struct sport_uart_port *)port;
  331. unsigned long flags;
  332. int i;
  333. pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
  334. switch (termios->c_cflag & CSIZE) {
  335. case CS8:
  336. up->csize = 8;
  337. break;
  338. case CS7:
  339. up->csize = 7;
  340. break;
  341. case CS6:
  342. up->csize = 6;
  343. break;
  344. case CS5:
  345. up->csize = 5;
  346. break;
  347. default:
  348. pr_warning("requested word length not supported\n");
  349. }
  350. if (termios->c_cflag & CSTOPB) {
  351. up->stopb = 1;
  352. }
  353. if (termios->c_cflag & PARENB) {
  354. pr_warning("PAREN bits is not supported yet\n");
  355. /* up->parib = 1; */
  356. }
  357. port->read_status_mask = OE;
  358. if (termios->c_iflag & INPCK)
  359. port->read_status_mask |= (FE | PE);
  360. if (termios->c_iflag & (BRKINT | PARMRK))
  361. port->read_status_mask |= BI;
  362. /*
  363. * Characters to ignore
  364. */
  365. port->ignore_status_mask = 0;
  366. if (termios->c_iflag & IGNPAR)
  367. port->ignore_status_mask |= FE | PE;
  368. if (termios->c_iflag & IGNBRK) {
  369. port->ignore_status_mask |= BI;
  370. /*
  371. * If we're ignoring parity and break indicators,
  372. * ignore overruns too (for real raw support).
  373. */
  374. if (termios->c_iflag & IGNPAR)
  375. port->ignore_status_mask |= OE;
  376. }
  377. /* RX extract mask */
  378. up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
  379. /* TX masks, 8 bit data and 1 bit stop for example:
  380. * mask1 = b#0111111110
  381. * mask2 = b#1000000000
  382. */
  383. for (i = 0, up->txmask1 = 0; i < up->csize; i++)
  384. up->txmask1 |= (1<<i);
  385. up->txmask2 = (1<<i);
  386. if (up->stopb) {
  387. ++i;
  388. up->txmask2 |= (1<<i);
  389. }
  390. up->txmask1 <<= 1;
  391. up->txmask2 <<= 1;
  392. /* uart baud rate */
  393. port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
  394. spin_lock_irqsave(&up->port.lock, flags);
  395. /* Disable UART */
  396. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  397. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
  398. sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
  399. /* driver TX line high after config, one dummy data is
  400. * necessary to stop sport after shift one byte
  401. */
  402. SPORT_PUT_TX(up, 0xffff);
  403. SPORT_PUT_TX(up, 0xffff);
  404. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  405. SSYNC();
  406. while (!(SPORT_GET_STAT(up) & TXHRE))
  407. cpu_relax();
  408. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  409. SSYNC();
  410. /* Port speed changed, update the per-port timeout. */
  411. uart_update_timeout(port, termios->c_cflag, port->uartclk);
  412. /* Enable sport rx */
  413. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
  414. SSYNC();
  415. spin_unlock_irqrestore(&up->port.lock, flags);
  416. }
  417. struct uart_ops sport_uart_ops = {
  418. .tx_empty = sport_tx_empty,
  419. .set_mctrl = sport_set_mctrl,
  420. .get_mctrl = sport_get_mctrl,
  421. .stop_tx = sport_stop_tx,
  422. .start_tx = sport_start_tx,
  423. .stop_rx = sport_stop_rx,
  424. .enable_ms = sport_enable_ms,
  425. .break_ctl = sport_break_ctl,
  426. .startup = sport_startup,
  427. .shutdown = sport_shutdown,
  428. .set_termios = sport_set_termios,
  429. .type = sport_type,
  430. .release_port = sport_release_port,
  431. .request_port = sport_request_port,
  432. .config_port = sport_config_port,
  433. .verify_port = sport_verify_port,
  434. };
  435. #define BFIN_SPORT_UART_MAX_PORTS 4
  436. static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
  437. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  438. #define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
  439. static int __init
  440. sport_uart_console_setup(struct console *co, char *options)
  441. {
  442. struct sport_uart_port *up;
  443. int baud = 57600;
  444. int bits = 8;
  445. int parity = 'n';
  446. int flow = 'n';
  447. /* Check whether an invalid uart number has been specified */
  448. if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
  449. return -ENODEV;
  450. up = bfin_sport_uart_ports[co->index];
  451. if (!up)
  452. return -ENODEV;
  453. if (options)
  454. uart_parse_options(options, &baud, &parity, &bits, &flow);
  455. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  456. }
  457. static void sport_uart_console_putchar(struct uart_port *port, int ch)
  458. {
  459. struct sport_uart_port *up = (struct sport_uart_port *)port;
  460. while (SPORT_GET_STAT(up) & TXF)
  461. barrier();
  462. tx_one_byte(up, ch);
  463. }
  464. /*
  465. * Interrupts are disabled on entering
  466. */
  467. static void
  468. sport_uart_console_write(struct console *co, const char *s, unsigned int count)
  469. {
  470. struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
  471. unsigned long flags;
  472. spin_lock_irqsave(&up->port.lock, flags);
  473. if (SPORT_GET_TCR1(up) & TSPEN)
  474. uart_console_write(&up->port, s, count, sport_uart_console_putchar);
  475. else {
  476. /* dummy data to start sport */
  477. while (SPORT_GET_STAT(up) & TXF)
  478. barrier();
  479. SPORT_PUT_TX(up, 0xffff);
  480. /* Enable transmit, then an interrupt will generated */
  481. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  482. SSYNC();
  483. uart_console_write(&up->port, s, count, sport_uart_console_putchar);
  484. /* Although the hold register is empty, last byte is still in shift
  485. * register and not sent out yet. So, put a dummy data into TX FIFO.
  486. * Then, sport tx stops when last byte is shift out and the dummy
  487. * data is moved into the shift register.
  488. */
  489. while (SPORT_GET_STAT(up) & TXF)
  490. barrier();
  491. SPORT_PUT_TX(up, 0xffff);
  492. while (!(SPORT_GET_STAT(up) & TXHRE))
  493. barrier();
  494. /* Stop sport tx transfer */
  495. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  496. SSYNC();
  497. }
  498. spin_unlock_irqrestore(&up->port.lock, flags);
  499. }
  500. static struct uart_driver sport_uart_reg;
  501. static struct console sport_uart_console = {
  502. .name = DEVICE_NAME,
  503. .write = sport_uart_console_write,
  504. .device = uart_console_device,
  505. .setup = sport_uart_console_setup,
  506. .flags = CON_PRINTBUFFER,
  507. .index = -1,
  508. .data = &sport_uart_reg,
  509. };
  510. #define SPORT_UART_CONSOLE (&sport_uart_console)
  511. #else
  512. #define SPORT_UART_CONSOLE NULL
  513. #endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
  514. static struct uart_driver sport_uart_reg = {
  515. .owner = THIS_MODULE,
  516. .driver_name = DRV_NAME,
  517. .dev_name = DEVICE_NAME,
  518. .major = 204,
  519. .minor = 84,
  520. .nr = BFIN_SPORT_UART_MAX_PORTS,
  521. .cons = SPORT_UART_CONSOLE,
  522. };
  523. #ifdef CONFIG_PM
  524. static int sport_uart_suspend(struct device *dev)
  525. {
  526. struct sport_uart_port *sport = dev_get_drvdata(dev);
  527. dev_dbg(dev, "%s enter\n", __func__);
  528. if (sport)
  529. uart_suspend_port(&sport_uart_reg, &sport->port);
  530. return 0;
  531. }
  532. static int sport_uart_resume(struct device *dev)
  533. {
  534. struct sport_uart_port *sport = dev_get_drvdata(dev);
  535. dev_dbg(dev, "%s enter\n", __func__);
  536. if (sport)
  537. uart_resume_port(&sport_uart_reg, &sport->port);
  538. return 0;
  539. }
  540. static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
  541. .suspend = sport_uart_suspend,
  542. .resume = sport_uart_resume,
  543. };
  544. #endif
  545. static int __devinit sport_uart_probe(struct platform_device *pdev)
  546. {
  547. struct resource *res;
  548. struct sport_uart_port *sport;
  549. int ret = 0;
  550. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  551. if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
  552. dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
  553. return -ENOENT;
  554. }
  555. if (bfin_sport_uart_ports[pdev->id] == NULL) {
  556. bfin_sport_uart_ports[pdev->id] =
  557. kmalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
  558. sport = bfin_sport_uart_ports[pdev->id];
  559. if (!sport) {
  560. dev_err(&pdev->dev,
  561. "Fail to kmalloc sport_uart_port\n");
  562. return -ENOMEM;
  563. }
  564. ret = peripheral_request_list(
  565. (unsigned short *)pdev->dev.platform_data, DRV_NAME);
  566. if (ret) {
  567. dev_err(&pdev->dev,
  568. "Fail to request SPORT peripherals\n");
  569. goto out_error_free_mem;
  570. }
  571. spin_lock_init(&sport->port.lock);
  572. sport->port.fifosize = SPORT_TX_FIFO_SIZE,
  573. sport->port.ops = &sport_uart_ops;
  574. sport->port.line = pdev->id;
  575. sport->port.iotype = UPIO_MEM;
  576. sport->port.flags = UPF_BOOT_AUTOCONF;
  577. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  578. if (res == NULL) {
  579. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  580. ret = -ENOENT;
  581. goto out_error_free_peripherals;
  582. }
  583. sport->port.membase = ioremap(res->start,
  584. res->end - res->start);
  585. if (!sport->port.membase) {
  586. dev_err(&pdev->dev, "Cannot map sport IO\n");
  587. ret = -ENXIO;
  588. goto out_error_free_peripherals;
  589. }
  590. sport->port.mapbase = res->start;
  591. sport->port.irq = platform_get_irq(pdev, 0);
  592. if (sport->port.irq < 0) {
  593. dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
  594. ret = -ENOENT;
  595. goto out_error_unmap;
  596. }
  597. sport->err_irq = platform_get_irq(pdev, 1);
  598. if (sport->err_irq < 0) {
  599. dev_err(&pdev->dev, "No sport status IRQ specified\n");
  600. ret = -ENOENT;
  601. goto out_error_unmap;
  602. }
  603. }
  604. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  605. if (!is_early_platform_device(pdev)) {
  606. #endif
  607. sport = bfin_sport_uart_ports[pdev->id];
  608. sport->port.dev = &pdev->dev;
  609. dev_set_drvdata(&pdev->dev, sport);
  610. ret = uart_add_one_port(&sport_uart_reg, &sport->port);
  611. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  612. }
  613. #endif
  614. if (!ret)
  615. return 0;
  616. if (sport) {
  617. out_error_unmap:
  618. iounmap(sport->port.membase);
  619. out_error_free_peripherals:
  620. peripheral_free_list(
  621. (unsigned short *)pdev->dev.platform_data);
  622. out_error_free_mem:
  623. kfree(sport);
  624. bfin_sport_uart_ports[pdev->id] = NULL;
  625. }
  626. return ret;
  627. }
  628. static int __devexit sport_uart_remove(struct platform_device *pdev)
  629. {
  630. struct sport_uart_port *sport = platform_get_drvdata(pdev);
  631. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  632. dev_set_drvdata(&pdev->dev, NULL);
  633. if (sport) {
  634. uart_remove_one_port(&sport_uart_reg, &sport->port);
  635. iounmap(sport->port.membase);
  636. peripheral_free_list(
  637. (unsigned short *)pdev->dev.platform_data);
  638. kfree(sport);
  639. bfin_sport_uart_ports[pdev->id] = NULL;
  640. }
  641. return 0;
  642. }
  643. static struct platform_driver sport_uart_driver = {
  644. .probe = sport_uart_probe,
  645. .remove = __devexit_p(sport_uart_remove),
  646. .driver = {
  647. .name = DRV_NAME,
  648. #ifdef CONFIG_PM
  649. .pm = &bfin_sport_uart_dev_pm_ops,
  650. #endif
  651. },
  652. };
  653. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  654. static __initdata struct early_platform_driver early_sport_uart_driver = {
  655. .class_str = CLASS_BFIN_SPORT_CONSOLE,
  656. .pdrv = &sport_uart_driver,
  657. .requested_id = EARLY_PLATFORM_ID_UNSET,
  658. };
  659. static int __init sport_uart_rs_console_init(void)
  660. {
  661. early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
  662. early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
  663. BFIN_SPORT_UART_MAX_PORTS, 0);
  664. register_console(&sport_uart_console);
  665. return 0;
  666. }
  667. console_initcall(sport_uart_rs_console_init);
  668. #endif
  669. static int __init sport_uart_init(void)
  670. {
  671. int ret;
  672. pr_info("Blackfin uart over sport driver\n");
  673. ret = uart_register_driver(&sport_uart_reg);
  674. if (ret) {
  675. pr_err("failed to register %s:%d\n",
  676. sport_uart_reg.driver_name, ret);
  677. return ret;
  678. }
  679. ret = platform_driver_register(&sport_uart_driver);
  680. if (ret) {
  681. pr_err("failed to register sport uart driver:%d\n", ret);
  682. uart_unregister_driver(&sport_uart_reg);
  683. }
  684. return ret;
  685. }
  686. module_init(sport_uart_init);
  687. static void __exit sport_uart_exit(void)
  688. {
  689. platform_driver_unregister(&sport_uart_driver);
  690. uart_unregister_driver(&sport_uart_reg);
  691. }
  692. module_exit(sport_uart_exit);
  693. MODULE_AUTHOR("Sonic Zhang, Roy Huang");
  694. MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
  695. MODULE_LICENSE("GPL");