bfin_sport_uart.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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_struct *tty = up->port.state->port.tty;
  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(tty, ch, TTY_NORMAL);
  138. }
  139. tty_flip_buffer_push(tty);
  140. spin_unlock(&up->port.lock);
  141. return IRQ_HANDLED;
  142. }
  143. static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
  144. {
  145. struct sport_uart_port *up = dev_id;
  146. spin_lock(&up->port.lock);
  147. sport_uart_tx_chars(up);
  148. spin_unlock(&up->port.lock);
  149. return IRQ_HANDLED;
  150. }
  151. static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
  152. {
  153. struct sport_uart_port *up = dev_id;
  154. struct tty_struct *tty = up->port.state->port.tty;
  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(tty, 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. return IRQ_HANDLED;
  175. }
  176. #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  177. static unsigned int sport_get_mctrl(struct uart_port *port)
  178. {
  179. struct sport_uart_port *up = (struct sport_uart_port *)port;
  180. if (up->cts_pin < 0)
  181. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  182. /* CTS PIN is negative assertive. */
  183. if (SPORT_UART_GET_CTS(up))
  184. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  185. else
  186. return TIOCM_DSR | TIOCM_CAR;
  187. }
  188. static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
  189. {
  190. struct sport_uart_port *up = (struct sport_uart_port *)port;
  191. if (up->rts_pin < 0)
  192. return;
  193. /* RTS PIN is negative assertive. */
  194. if (mctrl & TIOCM_RTS)
  195. SPORT_UART_ENABLE_RTS(up);
  196. else
  197. SPORT_UART_DISABLE_RTS(up);
  198. }
  199. /*
  200. * Handle any change of modem status signal.
  201. */
  202. static irqreturn_t sport_mctrl_cts_int(int irq, void *dev_id)
  203. {
  204. struct sport_uart_port *up = (struct sport_uart_port *)dev_id;
  205. unsigned int status;
  206. status = sport_get_mctrl(&up->port);
  207. uart_handle_cts_change(&up->port, status & TIOCM_CTS);
  208. return IRQ_HANDLED;
  209. }
  210. #else
  211. static unsigned int sport_get_mctrl(struct uart_port *port)
  212. {
  213. pr_debug("%s enter\n", __func__);
  214. return TIOCM_CTS | TIOCM_CD | TIOCM_DSR;
  215. }
  216. static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
  217. {
  218. pr_debug("%s enter\n", __func__);
  219. }
  220. #endif
  221. /* Reqeust IRQ, Setup clock */
  222. static int sport_startup(struct uart_port *port)
  223. {
  224. struct sport_uart_port *up = (struct sport_uart_port *)port;
  225. int ret;
  226. pr_debug("%s enter\n", __func__);
  227. ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
  228. "SPORT_UART_RX", up);
  229. if (ret) {
  230. dev_err(port->dev, "unable to request SPORT RX interrupt\n");
  231. return ret;
  232. }
  233. ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
  234. "SPORT_UART_TX", up);
  235. if (ret) {
  236. dev_err(port->dev, "unable to request SPORT TX interrupt\n");
  237. goto fail1;
  238. }
  239. ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
  240. "SPORT_UART_STATUS", up);
  241. if (ret) {
  242. dev_err(port->dev, "unable to request SPORT status interrupt\n");
  243. goto fail2;
  244. }
  245. #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  246. if (up->cts_pin >= 0) {
  247. if (request_irq(gpio_to_irq(up->cts_pin),
  248. sport_mctrl_cts_int,
  249. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  250. 0, "BFIN_SPORT_UART_CTS", up)) {
  251. up->cts_pin = -1;
  252. dev_info(port->dev, "Unable to attach BlackFin UART over SPORT CTS interrupt. So, disable it.\n");
  253. }
  254. }
  255. if (up->rts_pin >= 0) {
  256. if (gpio_request(up->rts_pin, DRV_NAME)) {
  257. dev_info(port->dev, "fail to request RTS PIN at GPIO_%d\n", up->rts_pin);
  258. up->rts_pin = -1;
  259. } else
  260. gpio_direction_output(up->rts_pin, 0);
  261. }
  262. #endif
  263. return 0;
  264. fail2:
  265. free_irq(up->port.irq+1, up);
  266. fail1:
  267. free_irq(up->port.irq, up);
  268. return ret;
  269. }
  270. /*
  271. * sport_uart_tx_chars
  272. *
  273. * ret 1 means need to enable sport.
  274. * ret 0 means do nothing.
  275. */
  276. static int sport_uart_tx_chars(struct sport_uart_port *up)
  277. {
  278. struct circ_buf *xmit = &up->port.state->xmit;
  279. if (SPORT_GET_STAT(up) & TXF)
  280. return 0;
  281. if (up->port.x_char) {
  282. tx_one_byte(up, up->port.x_char);
  283. up->port.icount.tx++;
  284. up->port.x_char = 0;
  285. return 1;
  286. }
  287. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  288. /* The waiting loop to stop SPORT TX from TX interrupt is
  289. * too long. This may block SPORT RX interrupts and cause
  290. * RX FIFO overflow. So, do stop sport TX only after the last
  291. * char in TX FIFO is moved into the shift register.
  292. */
  293. if (SPORT_GET_STAT(up) & TXHRE)
  294. sport_stop_tx(&up->port);
  295. return 0;
  296. }
  297. while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
  298. tx_one_byte(up, xmit->buf[xmit->tail]);
  299. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
  300. up->port.icount.tx++;
  301. }
  302. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  303. uart_write_wakeup(&up->port);
  304. return 1;
  305. }
  306. static unsigned int sport_tx_empty(struct uart_port *port)
  307. {
  308. struct sport_uart_port *up = (struct sport_uart_port *)port;
  309. unsigned int stat;
  310. stat = SPORT_GET_STAT(up);
  311. pr_debug("%s stat:%04x\n", __func__, stat);
  312. if (stat & TXHRE) {
  313. return TIOCSER_TEMT;
  314. } else
  315. return 0;
  316. }
  317. static void sport_stop_tx(struct uart_port *port)
  318. {
  319. struct sport_uart_port *up = (struct sport_uart_port *)port;
  320. pr_debug("%s enter\n", __func__);
  321. if (!(SPORT_GET_TCR1(up) & TSPEN))
  322. return;
  323. /* Although the hold register is empty, last byte is still in shift
  324. * register and not sent out yet. So, put a dummy data into TX FIFO.
  325. * Then, sport tx stops when last byte is shift out and the dummy
  326. * data is moved into the shift register.
  327. */
  328. SPORT_PUT_TX(up, 0xffff);
  329. while (!(SPORT_GET_STAT(up) & TXHRE))
  330. cpu_relax();
  331. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  332. SSYNC();
  333. return;
  334. }
  335. static void sport_start_tx(struct uart_port *port)
  336. {
  337. struct sport_uart_port *up = (struct sport_uart_port *)port;
  338. pr_debug("%s enter\n", __func__);
  339. /* Write data into SPORT FIFO before enable SPROT to transmit */
  340. if (sport_uart_tx_chars(up)) {
  341. /* Enable transmit, then an interrupt will generated */
  342. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  343. SSYNC();
  344. }
  345. pr_debug("%s exit\n", __func__);
  346. }
  347. static void sport_stop_rx(struct uart_port *port)
  348. {
  349. struct sport_uart_port *up = (struct sport_uart_port *)port;
  350. pr_debug("%s enter\n", __func__);
  351. /* Disable sport to stop rx */
  352. SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
  353. SSYNC();
  354. }
  355. static void sport_enable_ms(struct uart_port *port)
  356. {
  357. pr_debug("%s enter\n", __func__);
  358. }
  359. static void sport_break_ctl(struct uart_port *port, int break_state)
  360. {
  361. pr_debug("%s enter\n", __func__);
  362. }
  363. static void sport_shutdown(struct uart_port *port)
  364. {
  365. struct sport_uart_port *up = (struct sport_uart_port *)port;
  366. dev_dbg(port->dev, "%s enter\n", __func__);
  367. /* Disable sport */
  368. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  369. SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
  370. SSYNC();
  371. free_irq(up->port.irq, up);
  372. free_irq(up->port.irq+1, up);
  373. free_irq(up->err_irq, up);
  374. #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  375. if (up->cts_pin >= 0)
  376. free_irq(gpio_to_irq(up->cts_pin), up);
  377. if (up->rts_pin >= 0)
  378. gpio_free(up->rts_pin);
  379. #endif
  380. }
  381. static const char *sport_type(struct uart_port *port)
  382. {
  383. struct sport_uart_port *up = (struct sport_uart_port *)port;
  384. pr_debug("%s enter\n", __func__);
  385. return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
  386. }
  387. static void sport_release_port(struct uart_port *port)
  388. {
  389. pr_debug("%s enter\n", __func__);
  390. }
  391. static int sport_request_port(struct uart_port *port)
  392. {
  393. pr_debug("%s enter\n", __func__);
  394. return 0;
  395. }
  396. static void sport_config_port(struct uart_port *port, int flags)
  397. {
  398. struct sport_uart_port *up = (struct sport_uart_port *)port;
  399. pr_debug("%s enter\n", __func__);
  400. up->port.type = PORT_BFIN_SPORT;
  401. }
  402. static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
  403. {
  404. pr_debug("%s enter\n", __func__);
  405. return 0;
  406. }
  407. static void sport_set_termios(struct uart_port *port,
  408. struct ktermios *termios, struct ktermios *old)
  409. {
  410. struct sport_uart_port *up = (struct sport_uart_port *)port;
  411. unsigned long flags;
  412. int i;
  413. pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
  414. switch (termios->c_cflag & CSIZE) {
  415. case CS8:
  416. up->csize = 8;
  417. break;
  418. case CS7:
  419. up->csize = 7;
  420. break;
  421. case CS6:
  422. up->csize = 6;
  423. break;
  424. case CS5:
  425. up->csize = 5;
  426. break;
  427. default:
  428. pr_warning("requested word length not supported\n");
  429. }
  430. if (termios->c_cflag & CSTOPB) {
  431. up->stopb = 1;
  432. }
  433. if (termios->c_cflag & PARENB) {
  434. pr_warning("PAREN bits is not supported yet\n");
  435. /* up->parib = 1; */
  436. }
  437. spin_lock_irqsave(&up->port.lock, flags);
  438. port->read_status_mask = 0;
  439. /*
  440. * Characters to ignore
  441. */
  442. port->ignore_status_mask = 0;
  443. /* RX extract mask */
  444. up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
  445. /* TX masks, 8 bit data and 1 bit stop for example:
  446. * mask1 = b#0111111110
  447. * mask2 = b#1000000000
  448. */
  449. for (i = 0, up->txmask1 = 0; i < up->csize; i++)
  450. up->txmask1 |= (1<<i);
  451. up->txmask2 = (1<<i);
  452. if (up->stopb) {
  453. ++i;
  454. up->txmask2 |= (1<<i);
  455. }
  456. up->txmask1 <<= 1;
  457. up->txmask2 <<= 1;
  458. /* uart baud rate */
  459. port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
  460. /* Disable UART */
  461. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  462. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
  463. sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
  464. /* driver TX line high after config, one dummy data is
  465. * necessary to stop sport after shift one byte
  466. */
  467. SPORT_PUT_TX(up, 0xffff);
  468. SPORT_PUT_TX(up, 0xffff);
  469. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  470. SSYNC();
  471. while (!(SPORT_GET_STAT(up) & TXHRE))
  472. cpu_relax();
  473. SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
  474. SSYNC();
  475. /* Port speed changed, update the per-port timeout. */
  476. uart_update_timeout(port, termios->c_cflag, port->uartclk);
  477. /* Enable sport rx */
  478. SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
  479. SSYNC();
  480. spin_unlock_irqrestore(&up->port.lock, flags);
  481. }
  482. struct uart_ops sport_uart_ops = {
  483. .tx_empty = sport_tx_empty,
  484. .set_mctrl = sport_set_mctrl,
  485. .get_mctrl = sport_get_mctrl,
  486. .stop_tx = sport_stop_tx,
  487. .start_tx = sport_start_tx,
  488. .stop_rx = sport_stop_rx,
  489. .enable_ms = sport_enable_ms,
  490. .break_ctl = sport_break_ctl,
  491. .startup = sport_startup,
  492. .shutdown = sport_shutdown,
  493. .set_termios = sport_set_termios,
  494. .type = sport_type,
  495. .release_port = sport_release_port,
  496. .request_port = sport_request_port,
  497. .config_port = sport_config_port,
  498. .verify_port = sport_verify_port,
  499. };
  500. #define BFIN_SPORT_UART_MAX_PORTS 4
  501. static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
  502. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  503. #define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
  504. static int __init
  505. sport_uart_console_setup(struct console *co, char *options)
  506. {
  507. struct sport_uart_port *up;
  508. int baud = 57600;
  509. int bits = 8;
  510. int parity = 'n';
  511. # ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  512. int flow = 'r';
  513. # else
  514. int flow = 'n';
  515. # endif
  516. /* Check whether an invalid uart number has been specified */
  517. if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
  518. return -ENODEV;
  519. up = bfin_sport_uart_ports[co->index];
  520. if (!up)
  521. return -ENODEV;
  522. if (options)
  523. uart_parse_options(options, &baud, &parity, &bits, &flow);
  524. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  525. }
  526. static void sport_uart_console_putchar(struct uart_port *port, int ch)
  527. {
  528. struct sport_uart_port *up = (struct sport_uart_port *)port;
  529. while (SPORT_GET_STAT(up) & TXF)
  530. barrier();
  531. tx_one_byte(up, ch);
  532. }
  533. /*
  534. * Interrupts are disabled on entering
  535. */
  536. static void
  537. sport_uart_console_write(struct console *co, const char *s, unsigned int count)
  538. {
  539. struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
  540. unsigned long flags;
  541. spin_lock_irqsave(&up->port.lock, flags);
  542. if (SPORT_GET_TCR1(up) & TSPEN)
  543. uart_console_write(&up->port, s, count, sport_uart_console_putchar);
  544. else {
  545. /* dummy data to start sport */
  546. while (SPORT_GET_STAT(up) & TXF)
  547. barrier();
  548. SPORT_PUT_TX(up, 0xffff);
  549. /* Enable transmit, then an interrupt will generated */
  550. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
  551. SSYNC();
  552. uart_console_write(&up->port, s, count, sport_uart_console_putchar);
  553. /* Although the hold register is empty, last byte is still in shift
  554. * register and not sent out yet. So, put a dummy data into TX FIFO.
  555. * Then, sport tx stops when last byte is shift out and the dummy
  556. * data is moved into the shift register.
  557. */
  558. while (SPORT_GET_STAT(up) & TXF)
  559. barrier();
  560. SPORT_PUT_TX(up, 0xffff);
  561. while (!(SPORT_GET_STAT(up) & TXHRE))
  562. barrier();
  563. /* Stop sport tx transfer */
  564. SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
  565. SSYNC();
  566. }
  567. spin_unlock_irqrestore(&up->port.lock, flags);
  568. }
  569. static struct uart_driver sport_uart_reg;
  570. static struct console sport_uart_console = {
  571. .name = DEVICE_NAME,
  572. .write = sport_uart_console_write,
  573. .device = uart_console_device,
  574. .setup = sport_uart_console_setup,
  575. .flags = CON_PRINTBUFFER,
  576. .index = -1,
  577. .data = &sport_uart_reg,
  578. };
  579. #define SPORT_UART_CONSOLE (&sport_uart_console)
  580. #else
  581. #define SPORT_UART_CONSOLE NULL
  582. #endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
  583. static struct uart_driver sport_uart_reg = {
  584. .owner = THIS_MODULE,
  585. .driver_name = DRV_NAME,
  586. .dev_name = DEVICE_NAME,
  587. .major = 204,
  588. .minor = 84,
  589. .nr = BFIN_SPORT_UART_MAX_PORTS,
  590. .cons = SPORT_UART_CONSOLE,
  591. };
  592. #ifdef CONFIG_PM
  593. static int sport_uart_suspend(struct device *dev)
  594. {
  595. struct sport_uart_port *sport = dev_get_drvdata(dev);
  596. dev_dbg(dev, "%s enter\n", __func__);
  597. if (sport)
  598. uart_suspend_port(&sport_uart_reg, &sport->port);
  599. return 0;
  600. }
  601. static int sport_uart_resume(struct device *dev)
  602. {
  603. struct sport_uart_port *sport = dev_get_drvdata(dev);
  604. dev_dbg(dev, "%s enter\n", __func__);
  605. if (sport)
  606. uart_resume_port(&sport_uart_reg, &sport->port);
  607. return 0;
  608. }
  609. static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
  610. .suspend = sport_uart_suspend,
  611. .resume = sport_uart_resume,
  612. };
  613. #endif
  614. static int __devinit sport_uart_probe(struct platform_device *pdev)
  615. {
  616. struct resource *res;
  617. struct sport_uart_port *sport;
  618. int ret = 0;
  619. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  620. if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
  621. dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
  622. return -ENOENT;
  623. }
  624. if (bfin_sport_uart_ports[pdev->id] == NULL) {
  625. bfin_sport_uart_ports[pdev->id] =
  626. kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
  627. sport = bfin_sport_uart_ports[pdev->id];
  628. if (!sport) {
  629. dev_err(&pdev->dev,
  630. "Fail to malloc sport_uart_port\n");
  631. return -ENOMEM;
  632. }
  633. ret = peripheral_request_list(
  634. (unsigned short *)pdev->dev.platform_data, DRV_NAME);
  635. if (ret) {
  636. dev_err(&pdev->dev,
  637. "Fail to request SPORT peripherals\n");
  638. goto out_error_free_mem;
  639. }
  640. spin_lock_init(&sport->port.lock);
  641. sport->port.fifosize = SPORT_TX_FIFO_SIZE,
  642. sport->port.ops = &sport_uart_ops;
  643. sport->port.line = pdev->id;
  644. sport->port.iotype = UPIO_MEM;
  645. sport->port.flags = UPF_BOOT_AUTOCONF;
  646. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  647. if (res == NULL) {
  648. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  649. ret = -ENOENT;
  650. goto out_error_free_peripherals;
  651. }
  652. sport->port.membase = ioremap(res->start, resource_size(res));
  653. if (!sport->port.membase) {
  654. dev_err(&pdev->dev, "Cannot map sport IO\n");
  655. ret = -ENXIO;
  656. goto out_error_free_peripherals;
  657. }
  658. sport->port.mapbase = res->start;
  659. sport->port.irq = platform_get_irq(pdev, 0);
  660. if ((int)sport->port.irq < 0) {
  661. dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
  662. ret = -ENOENT;
  663. goto out_error_unmap;
  664. }
  665. sport->err_irq = platform_get_irq(pdev, 1);
  666. if (sport->err_irq < 0) {
  667. dev_err(&pdev->dev, "No sport status IRQ specified\n");
  668. ret = -ENOENT;
  669. goto out_error_unmap;
  670. }
  671. #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
  672. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  673. if (res == NULL)
  674. sport->cts_pin = -1;
  675. else {
  676. sport->cts_pin = res->start;
  677. sport->port.flags |= ASYNC_CTS_FLOW;
  678. }
  679. res = platform_get_resource(pdev, IORESOURCE_IO, 1);
  680. if (res == NULL)
  681. sport->rts_pin = -1;
  682. else
  683. sport->rts_pin = res->start;
  684. #endif
  685. }
  686. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  687. if (!is_early_platform_device(pdev)) {
  688. #endif
  689. sport = bfin_sport_uart_ports[pdev->id];
  690. sport->port.dev = &pdev->dev;
  691. dev_set_drvdata(&pdev->dev, sport);
  692. ret = uart_add_one_port(&sport_uart_reg, &sport->port);
  693. #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
  694. }
  695. #endif
  696. if (!ret)
  697. return 0;
  698. if (sport) {
  699. out_error_unmap:
  700. iounmap(sport->port.membase);
  701. out_error_free_peripherals:
  702. peripheral_free_list(
  703. (unsigned short *)pdev->dev.platform_data);
  704. out_error_free_mem:
  705. kfree(sport);
  706. bfin_sport_uart_ports[pdev->id] = NULL;
  707. }
  708. return ret;
  709. }
  710. static int __devexit 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(
  719. (unsigned short *)pdev->dev.platform_data);
  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 = __devexit_p(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 __initdata struct early_platform_driver early_sport_uart_driver = {
  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");