bfin_sport_uart.c 23 KB

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