bfin_sport_uart.c 21 KB

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