bfin_sport_uart.c 21 KB

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