bfin_sport_uart.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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_warning("requested word length not supported\n");
  433. }
  434. if (termios->c_cflag & CSTOPB) {
  435. up->stopb = 1;
  436. }
  437. if (termios->c_cflag & PARENB) {
  438. pr_warning("PAREN bits is not supported yet\n");
  439. /* up->parib = 1; */
  440. }
  441. spin_lock_irqsave(&up->port.lock, flags);
  442. port->read_status_mask = 0;
  443. /*
  444. * Characters to ignore
  445. */
  446. port->ignore_status_mask = 0;
  447. /* RX extract mask */
  448. up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
  449. /* TX masks, 8 bit data and 1 bit stop for example:
  450. * mask1 = b#0111111110
  451. * mask2 = b#1000000000
  452. */
  453. for (i = 0, up->txmask1 = 0; i < up->csize; i++)
  454. up->txmask1 |= (1<<i);
  455. up->txmask2 = (1<<i);
  456. if (up->stopb) {
  457. ++i;
  458. up->txmask2 |= (1<<i);
  459. }
  460. up->txmask1 <<= 1;
  461. up->txmask2 <<= 1;
  462. /* uart baud rate */
  463. port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
  464. /* Disable UART */
  465. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  466. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
  467. sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
  468. /* driver TX line high after config, one dummy data is
  469. * necessary to stop sport after shift one byte
  470. */
  471. SPORT_PUT_TX(up, 0xffff);
  472. SPORT_PUT_TX(up, 0xffff);
  473. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  474. SSYNC();
  475. while (!(SPORT_GET_STAT(up) & TXHRE))
  476. cpu_relax();
  477. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  478. SSYNC();
  479. /* Port speed changed, update the per-port timeout. */
  480. uart_update_timeout(port, termios->c_cflag, port->uartclk);
  481. /* Enable sport rx */
  482. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
  483. SSYNC();
  484. spin_unlock_irqrestore(&up->port.lock, flags);
  485. }
  486. struct uart_ops sport_uart_ops = {
  487. .tx_empty = sport_tx_empty,
  488. .set_mctrl = sport_set_mctrl,
  489. .get_mctrl = sport_get_mctrl,
  490. .stop_tx = sport_stop_tx,
  491. .start_tx = sport_start_tx,
  492. .stop_rx = sport_stop_rx,
  493. .break_ctl = sport_break_ctl,
  494. .startup = sport_startup,
  495. .shutdown = sport_shutdown,
  496. .set_termios = sport_set_termios,
  497. .type = sport_type,
  498. .release_port = sport_release_port,
  499. .request_port = sport_request_port,
  500. .config_port = sport_config_port,
  501. .verify_port = sport_verify_port,
  502. };
  503. #define BFIN_SPORT_UART_MAX_PORTS 4
  504. static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
  505. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  506. #define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
  507. static int __init
  508. sport_uart_console_setup(struct console *co, char *options)
  509. {
  510. struct sport_uart_port *up;
  511. int baud = 57600;
  512. int bits = 8;
  513. int parity = 'n';
  514. # ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  515. int flow = 'r';
  516. # else
  517. int flow = 'n';
  518. # endif
  519. /* Check whether an invalid uart number has been specified */
  520. if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
  521. return -ENODEV;
  522. up = bfin_sport_uart_ports[co->index];
  523. if (!up)
  524. return -ENODEV;
  525. if (options)
  526. uart_parse_options(options, &baud, &parity, &bits, &flow);
  527. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  528. }
  529. static void sport_uart_console_putchar(struct uart_port *port, int ch)
  530. {
  531. struct sport_uart_port *up = (struct sport_uart_port *)port;
  532. while (SPORT_GET_STAT(up) & TXF)
  533. barrier();
  534. tx_one_byte(up, ch);
  535. }
  536. /*
  537. * Interrupts are disabled on entering
  538. */
  539. static void
  540. sport_uart_console_write(struct console *co, const char *s, unsigned int count)
  541. {
  542. struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
  543. unsigned long flags;
  544. spin_lock_irqsave(&up->port.lock, flags);
  545. if (SPORT_GET_TCR1(up) & TSPEN)
  546. uart_console_write(&up->port, s, count, sport_uart_console_putchar);
  547. else {
  548. /* dummy data to start sport */
  549. while (SPORT_GET_STAT(up) & TXF)
  550. barrier();
  551. SPORT_PUT_TX(up, 0xffff);
  552. /* Enable transmit, then an interrupt will generated */
  553. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  554. SSYNC();
  555. uart_console_write(&up->port, s, count, sport_uart_console_putchar);
  556. /* Although the hold register is empty, last byte is still in shift
  557. * register and not sent out yet. So, put a dummy data into TX FIFO.
  558. * Then, sport tx stops when last byte is shift out and the dummy
  559. * data is moved into the shift register.
  560. */
  561. while (SPORT_GET_STAT(up) & TXF)
  562. barrier();
  563. SPORT_PUT_TX(up, 0xffff);
  564. while (!(SPORT_GET_STAT(up) & TXHRE))
  565. barrier();
  566. /* Stop sport tx transfer */
  567. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  568. SSYNC();
  569. }
  570. spin_unlock_irqrestore(&up->port.lock, flags);
  571. }
  572. static struct uart_driver sport_uart_reg;
  573. static struct console sport_uart_console = {
  574. .name = DEVICE_NAME,
  575. .write = sport_uart_console_write,
  576. .device = uart_console_device,
  577. .setup = sport_uart_console_setup,
  578. .flags = CON_PRINTBUFFER,
  579. .index = -1,
  580. .data = &sport_uart_reg,
  581. };
  582. #define SPORT_UART_CONSOLE (&sport_uart_console)
  583. #else
  584. #define SPORT_UART_CONSOLE NULL
  585. #endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
  586. static struct uart_driver sport_uart_reg = {
  587. .owner = THIS_MODULE,
  588. .driver_name = DRV_NAME,
  589. .dev_name = DEVICE_NAME,
  590. .major = 204,
  591. .minor = 84,
  592. .nr = BFIN_SPORT_UART_MAX_PORTS,
  593. .cons = SPORT_UART_CONSOLE,
  594. };
  595. #ifdef CONFIG_PM
  596. static int sport_uart_suspend(struct device *dev)
  597. {
  598. struct sport_uart_port *sport = dev_get_drvdata(dev);
  599. dev_dbg(dev, "%s enter\n", __func__);
  600. if (sport)
  601. uart_suspend_port(&sport_uart_reg, &sport->port);
  602. return 0;
  603. }
  604. static int sport_uart_resume(struct device *dev)
  605. {
  606. struct sport_uart_port *sport = dev_get_drvdata(dev);
  607. dev_dbg(dev, "%s enter\n", __func__);
  608. if (sport)
  609. uart_resume_port(&sport_uart_reg, &sport->port);
  610. return 0;
  611. }
  612. static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
  613. .suspend = sport_uart_suspend,
  614. .resume = sport_uart_resume,
  615. };
  616. #endif
  617. static int sport_uart_probe(struct platform_device *pdev)
  618. {
  619. struct resource *res;
  620. struct sport_uart_port *sport;
  621. int ret = 0;
  622. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  623. if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
  624. dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
  625. return -ENOENT;
  626. }
  627. if (bfin_sport_uart_ports[pdev->id] == NULL) {
  628. bfin_sport_uart_ports[pdev->id] =
  629. kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
  630. sport = bfin_sport_uart_ports[pdev->id];
  631. if (!sport) {
  632. dev_err(&pdev->dev,
  633. "Fail to malloc sport_uart_port\n");
  634. return -ENOMEM;
  635. }
  636. ret = peripheral_request_list(dev_get_platdata(&pdev->dev),
  637. DRV_NAME);
  638. if (ret) {
  639. dev_err(&pdev->dev,
  640. "Fail to request SPORT peripherals\n");
  641. goto out_error_free_mem;
  642. }
  643. spin_lock_init(&sport->port.lock);
  644. sport->port.fifosize = SPORT_TX_FIFO_SIZE,
  645. sport->port.ops = &sport_uart_ops;
  646. sport->port.line = pdev->id;
  647. sport->port.iotype = UPIO_MEM;
  648. sport->port.flags = UPF_BOOT_AUTOCONF;
  649. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  650. if (res == NULL) {
  651. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  652. ret = -ENOENT;
  653. goto out_error_free_peripherals;
  654. }
  655. sport->port.membase = ioremap(res->start, resource_size(res));
  656. if (!sport->port.membase) {
  657. dev_err(&pdev->dev, "Cannot map sport IO\n");
  658. ret = -ENXIO;
  659. goto out_error_free_peripherals;
  660. }
  661. sport->port.mapbase = res->start;
  662. sport->port.irq = platform_get_irq(pdev, 0);
  663. if ((int)sport->port.irq < 0) {
  664. dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
  665. ret = -ENOENT;
  666. goto out_error_unmap;
  667. }
  668. sport->err_irq = platform_get_irq(pdev, 1);
  669. if (sport->err_irq < 0) {
  670. dev_err(&pdev->dev, "No sport status IRQ specified\n");
  671. ret = -ENOENT;
  672. goto out_error_unmap;
  673. }
  674. #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  675. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  676. if (res == NULL)
  677. sport->cts_pin = -1;
  678. else
  679. sport->cts_pin = res->start;
  680. res = platform_get_resource(pdev, IORESOURCE_IO, 1);
  681. if (res == NULL)
  682. sport->rts_pin = -1;
  683. else
  684. sport->rts_pin = res->start;
  685. #endif
  686. }
  687. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  688. if (!is_early_platform_device(pdev)) {
  689. #endif
  690. sport = bfin_sport_uart_ports[pdev->id];
  691. sport->port.dev = &pdev->dev;
  692. dev_set_drvdata(&pdev->dev, sport);
  693. ret = uart_add_one_port(&sport_uart_reg, &sport->port);
  694. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  695. }
  696. #endif
  697. if (!ret)
  698. return 0;
  699. if (sport) {
  700. out_error_unmap:
  701. iounmap(sport->port.membase);
  702. out_error_free_peripherals:
  703. peripheral_free_list(dev_get_platdata(&pdev->dev));
  704. out_error_free_mem:
  705. kfree(sport);
  706. bfin_sport_uart_ports[pdev->id] = NULL;
  707. }
  708. return ret;
  709. }
  710. static int sport_uart_remove(struct platform_device *pdev)
  711. {
  712. struct sport_uart_port *sport = platform_get_drvdata(pdev);
  713. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  714. dev_set_drvdata(&pdev->dev, NULL);
  715. if (sport) {
  716. uart_remove_one_port(&sport_uart_reg, &sport->port);
  717. iounmap(sport->port.membase);
  718. peripheral_free_list(dev_get_platdata(&pdev->dev));
  719. kfree(sport);
  720. bfin_sport_uart_ports[pdev->id] = NULL;
  721. }
  722. return 0;
  723. }
  724. static struct platform_driver sport_uart_driver = {
  725. .probe = sport_uart_probe,
  726. .remove = sport_uart_remove,
  727. .driver = {
  728. .name = DRV_NAME,
  729. #ifdef CONFIG_PM
  730. .pm = &bfin_sport_uart_dev_pm_ops,
  731. #endif
  732. },
  733. };
  734. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  735. static struct early_platform_driver early_sport_uart_driver __initdata = {
  736. .class_str = CLASS_BFIN_SPORT_CONSOLE,
  737. .pdrv = &sport_uart_driver,
  738. .requested_id = EARLY_PLATFORM_ID_UNSET,
  739. };
  740. static int __init sport_uart_rs_console_init(void)
  741. {
  742. early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
  743. early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
  744. BFIN_SPORT_UART_MAX_PORTS, 0);
  745. register_console(&sport_uart_console);
  746. return 0;
  747. }
  748. console_initcall(sport_uart_rs_console_init);
  749. #endif
  750. static int __init sport_uart_init(void)
  751. {
  752. int ret;
  753. pr_info("Blackfin uart over sport driver\n");
  754. ret = uart_register_driver(&sport_uart_reg);
  755. if (ret) {
  756. pr_err("failed to register %s:%d\n",
  757. sport_uart_reg.driver_name, ret);
  758. return ret;
  759. }
  760. ret = platform_driver_register(&sport_uart_driver);
  761. if (ret) {
  762. pr_err("failed to register sport uart driver:%d\n", ret);
  763. uart_unregister_driver(&sport_uart_reg);
  764. }
  765. return ret;
  766. }
  767. module_init(sport_uart_init);
  768. static void __exit sport_uart_exit(void)
  769. {
  770. platform_driver_unregister(&sport_uart_driver);
  771. uart_unregister_driver(&sport_uart_reg);
  772. }
  773. module_exit(sport_uart_exit);
  774. MODULE_AUTHOR("Sonic Zhang, Roy Huang");
  775. MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
  776. MODULE_LICENSE("GPL");