bfin_sport_uart.c 23 KB

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