bfin_sport_uart.c 22 KB

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