bfin_sport_uart.c 23 KB

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