omap-serial.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * Driver for OMAP-UART controller.
  3. * Based on drivers/serial/8250.c
  4. *
  5. * Copyright (C) 2010 Texas Instruments.
  6. *
  7. * Authors:
  8. * Govindraj R <govindraj.raja@ti.com>
  9. * Thara Gopinath <thara@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * Note: This driver is made seperate from 8250 driver as we cannot
  17. * over load 8250 driver with omap platform specific configuration for
  18. * features like DMA, it makes easier to implement features like DMA and
  19. * hardware flow control and software flow control configuration with
  20. * this driver as required for the omap-platform.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/console.h>
  25. #include <linux/serial_reg.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/io.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/clk.h>
  33. #include <linux/serial_core.h>
  34. #include <linux/irq.h>
  35. #include <plat/dma.h>
  36. #include <plat/dmtimer.h>
  37. #include <plat/omap-serial.h>
  38. static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
  39. /* Forward declaration of functions */
  40. static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
  41. static void serial_omap_rx_timeout(unsigned long uart_no);
  42. static int serial_omap_start_rxdma(struct uart_omap_port *up);
  43. static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
  44. {
  45. offset <<= up->port.regshift;
  46. return readw(up->port.membase + offset);
  47. }
  48. static inline void serial_out(struct uart_omap_port *up, int offset, int value)
  49. {
  50. offset <<= up->port.regshift;
  51. writew(value, up->port.membase + offset);
  52. }
  53. static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
  54. {
  55. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  56. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  57. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  58. serial_out(up, UART_FCR, 0);
  59. }
  60. /*
  61. * serial_omap_get_divisor - calculate divisor value
  62. * @port: uart port info
  63. * @baud: baudrate for which divisor needs to be calculated.
  64. *
  65. * We have written our own function to get the divisor so as to support
  66. * 13x mode. 3Mbps Baudrate as an different divisor.
  67. * Reference OMAP TRM Chapter 17:
  68. * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
  69. * referring to oversampling - divisor value
  70. * baudrate 460,800 to 3,686,400 all have divisor 13
  71. * except 3,000,000 which has divisor value 16
  72. */
  73. static unsigned int
  74. serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
  75. {
  76. unsigned int divisor;
  77. if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
  78. divisor = 13;
  79. else
  80. divisor = 16;
  81. return port->uartclk/(baud * divisor);
  82. }
  83. static void serial_omap_stop_rxdma(struct uart_omap_port *up)
  84. {
  85. if (up->uart_dma.rx_dma_used) {
  86. del_timer(&up->uart_dma.rx_timer);
  87. omap_stop_dma(up->uart_dma.rx_dma_channel);
  88. omap_free_dma(up->uart_dma.rx_dma_channel);
  89. up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
  90. up->uart_dma.rx_dma_used = false;
  91. }
  92. }
  93. static void serial_omap_enable_ms(struct uart_port *port)
  94. {
  95. struct uart_omap_port *up = (struct uart_omap_port *)port;
  96. dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->pdev->id);
  97. up->ier |= UART_IER_MSI;
  98. serial_out(up, UART_IER, up->ier);
  99. }
  100. static void serial_omap_stop_tx(struct uart_port *port)
  101. {
  102. struct uart_omap_port *up = (struct uart_omap_port *)port;
  103. if (up->use_dma &&
  104. up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
  105. /*
  106. * Check if dma is still active. If yes do nothing,
  107. * return. Else stop dma
  108. */
  109. if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
  110. return;
  111. omap_stop_dma(up->uart_dma.tx_dma_channel);
  112. omap_free_dma(up->uart_dma.tx_dma_channel);
  113. up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
  114. }
  115. if (up->ier & UART_IER_THRI) {
  116. up->ier &= ~UART_IER_THRI;
  117. serial_out(up, UART_IER, up->ier);
  118. }
  119. }
  120. static void serial_omap_stop_rx(struct uart_port *port)
  121. {
  122. struct uart_omap_port *up = (struct uart_omap_port *)port;
  123. if (up->use_dma)
  124. serial_omap_stop_rxdma(up);
  125. up->ier &= ~UART_IER_RLSI;
  126. up->port.read_status_mask &= ~UART_LSR_DR;
  127. serial_out(up, UART_IER, up->ier);
  128. }
  129. static inline void receive_chars(struct uart_omap_port *up, int *status)
  130. {
  131. struct tty_struct *tty = up->port.state->port.tty;
  132. unsigned int flag;
  133. unsigned char ch, lsr = *status;
  134. int max_count = 256;
  135. do {
  136. if (likely(lsr & UART_LSR_DR))
  137. ch = serial_in(up, UART_RX);
  138. flag = TTY_NORMAL;
  139. up->port.icount.rx++;
  140. if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
  141. /*
  142. * For statistics only
  143. */
  144. if (lsr & UART_LSR_BI) {
  145. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  146. up->port.icount.brk++;
  147. /*
  148. * We do the SysRQ and SAK checking
  149. * here because otherwise the break
  150. * may get masked by ignore_status_mask
  151. * or read_status_mask.
  152. */
  153. if (uart_handle_break(&up->port))
  154. goto ignore_char;
  155. } else if (lsr & UART_LSR_PE) {
  156. up->port.icount.parity++;
  157. } else if (lsr & UART_LSR_FE) {
  158. up->port.icount.frame++;
  159. }
  160. if (lsr & UART_LSR_OE)
  161. up->port.icount.overrun++;
  162. /*
  163. * Mask off conditions which should be ignored.
  164. */
  165. lsr &= up->port.read_status_mask;
  166. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  167. if (up->port.line == up->port.cons->index) {
  168. /* Recover the break flag from console xmit */
  169. lsr |= up->lsr_break_flag;
  170. up->lsr_break_flag = 0;
  171. }
  172. #endif
  173. if (lsr & UART_LSR_BI)
  174. flag = TTY_BREAK;
  175. else if (lsr & UART_LSR_PE)
  176. flag = TTY_PARITY;
  177. else if (lsr & UART_LSR_FE)
  178. flag = TTY_FRAME;
  179. }
  180. if (uart_handle_sysrq_char(&up->port, ch))
  181. goto ignore_char;
  182. uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
  183. ignore_char:
  184. lsr = serial_in(up, UART_LSR);
  185. } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
  186. spin_unlock(&up->port.lock);
  187. tty_flip_buffer_push(tty);
  188. spin_lock(&up->port.lock);
  189. }
  190. static void transmit_chars(struct uart_omap_port *up)
  191. {
  192. struct circ_buf *xmit = &up->port.state->xmit;
  193. int count;
  194. if (up->port.x_char) {
  195. serial_out(up, UART_TX, up->port.x_char);
  196. up->port.icount.tx++;
  197. up->port.x_char = 0;
  198. return;
  199. }
  200. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  201. serial_omap_stop_tx(&up->port);
  202. return;
  203. }
  204. count = up->port.fifosize / 4;
  205. do {
  206. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  207. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  208. up->port.icount.tx++;
  209. if (uart_circ_empty(xmit))
  210. break;
  211. } while (--count > 0);
  212. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  213. uart_write_wakeup(&up->port);
  214. if (uart_circ_empty(xmit))
  215. serial_omap_stop_tx(&up->port);
  216. }
  217. static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
  218. {
  219. if (!(up->ier & UART_IER_THRI)) {
  220. up->ier |= UART_IER_THRI;
  221. serial_out(up, UART_IER, up->ier);
  222. }
  223. }
  224. static void serial_omap_start_tx(struct uart_port *port)
  225. {
  226. struct uart_omap_port *up = (struct uart_omap_port *)port;
  227. struct circ_buf *xmit;
  228. unsigned int start;
  229. int ret = 0;
  230. if (!up->use_dma) {
  231. serial_omap_enable_ier_thri(up);
  232. return;
  233. }
  234. if (up->uart_dma.tx_dma_used)
  235. return;
  236. xmit = &up->port.state->xmit;
  237. if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
  238. ret = omap_request_dma(up->uart_dma.uart_dma_tx,
  239. "UART Tx DMA",
  240. (void *)uart_tx_dma_callback, up,
  241. &(up->uart_dma.tx_dma_channel));
  242. if (ret < 0) {
  243. serial_omap_enable_ier_thri(up);
  244. return;
  245. }
  246. }
  247. spin_lock(&(up->uart_dma.tx_lock));
  248. up->uart_dma.tx_dma_used = true;
  249. spin_unlock(&(up->uart_dma.tx_lock));
  250. start = up->uart_dma.tx_buf_dma_phys +
  251. (xmit->tail & (UART_XMIT_SIZE - 1));
  252. up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
  253. /*
  254. * It is a circular buffer. See if the buffer has wounded back.
  255. * If yes it will have to be transferred in two separate dma
  256. * transfers
  257. */
  258. if (start + up->uart_dma.tx_buf_size >=
  259. up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
  260. up->uart_dma.tx_buf_size =
  261. (up->uart_dma.tx_buf_dma_phys +
  262. UART_XMIT_SIZE) - start;
  263. omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
  264. OMAP_DMA_AMODE_CONSTANT,
  265. up->uart_dma.uart_base, 0, 0);
  266. omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
  267. OMAP_DMA_AMODE_POST_INC, start, 0, 0);
  268. omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
  269. OMAP_DMA_DATA_TYPE_S8,
  270. up->uart_dma.tx_buf_size, 1,
  271. OMAP_DMA_SYNC_ELEMENT,
  272. up->uart_dma.uart_dma_tx, 0);
  273. /* FIXME: Cache maintenance needed here? */
  274. omap_start_dma(up->uart_dma.tx_dma_channel);
  275. }
  276. static unsigned int check_modem_status(struct uart_omap_port *up)
  277. {
  278. unsigned int status;
  279. status = serial_in(up, UART_MSR);
  280. status |= up->msr_saved_flags;
  281. up->msr_saved_flags = 0;
  282. if ((status & UART_MSR_ANY_DELTA) == 0)
  283. return status;
  284. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  285. up->port.state != NULL) {
  286. if (status & UART_MSR_TERI)
  287. up->port.icount.rng++;
  288. if (status & UART_MSR_DDSR)
  289. up->port.icount.dsr++;
  290. if (status & UART_MSR_DDCD)
  291. uart_handle_dcd_change
  292. (&up->port, status & UART_MSR_DCD);
  293. if (status & UART_MSR_DCTS)
  294. uart_handle_cts_change
  295. (&up->port, status & UART_MSR_CTS);
  296. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  297. }
  298. return status;
  299. }
  300. /**
  301. * serial_omap_irq() - This handles the interrupt from one port
  302. * @irq: uart port irq number
  303. * @dev_id: uart port info
  304. */
  305. static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
  306. {
  307. struct uart_omap_port *up = dev_id;
  308. unsigned int iir, lsr;
  309. unsigned long flags;
  310. iir = serial_in(up, UART_IIR);
  311. if (iir & UART_IIR_NO_INT)
  312. return IRQ_NONE;
  313. spin_lock_irqsave(&up->port.lock, flags);
  314. lsr = serial_in(up, UART_LSR);
  315. if (iir & UART_IIR_RLSI) {
  316. if (!up->use_dma) {
  317. if (lsr & UART_LSR_DR)
  318. receive_chars(up, &lsr);
  319. } else {
  320. up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
  321. serial_out(up, UART_IER, up->ier);
  322. if ((serial_omap_start_rxdma(up) != 0) &&
  323. (lsr & UART_LSR_DR))
  324. receive_chars(up, &lsr);
  325. }
  326. }
  327. check_modem_status(up);
  328. if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
  329. transmit_chars(up);
  330. spin_unlock_irqrestore(&up->port.lock, flags);
  331. up->port_activity = jiffies;
  332. return IRQ_HANDLED;
  333. }
  334. static unsigned int serial_omap_tx_empty(struct uart_port *port)
  335. {
  336. struct uart_omap_port *up = (struct uart_omap_port *)port;
  337. unsigned long flags = 0;
  338. unsigned int ret = 0;
  339. dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
  340. spin_lock_irqsave(&up->port.lock, flags);
  341. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  342. spin_unlock_irqrestore(&up->port.lock, flags);
  343. return ret;
  344. }
  345. static unsigned int serial_omap_get_mctrl(struct uart_port *port)
  346. {
  347. struct uart_omap_port *up = (struct uart_omap_port *)port;
  348. unsigned char status;
  349. unsigned int ret = 0;
  350. status = check_modem_status(up);
  351. dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
  352. if (status & UART_MSR_DCD)
  353. ret |= TIOCM_CAR;
  354. if (status & UART_MSR_RI)
  355. ret |= TIOCM_RNG;
  356. if (status & UART_MSR_DSR)
  357. ret |= TIOCM_DSR;
  358. if (status & UART_MSR_CTS)
  359. ret |= TIOCM_CTS;
  360. return ret;
  361. }
  362. static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
  363. {
  364. struct uart_omap_port *up = (struct uart_omap_port *)port;
  365. unsigned char mcr = 0;
  366. dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
  367. if (mctrl & TIOCM_RTS)
  368. mcr |= UART_MCR_RTS;
  369. if (mctrl & TIOCM_DTR)
  370. mcr |= UART_MCR_DTR;
  371. if (mctrl & TIOCM_OUT1)
  372. mcr |= UART_MCR_OUT1;
  373. if (mctrl & TIOCM_OUT2)
  374. mcr |= UART_MCR_OUT2;
  375. if (mctrl & TIOCM_LOOP)
  376. mcr |= UART_MCR_LOOP;
  377. mcr |= up->mcr;
  378. serial_out(up, UART_MCR, mcr);
  379. }
  380. static void serial_omap_break_ctl(struct uart_port *port, int break_state)
  381. {
  382. struct uart_omap_port *up = (struct uart_omap_port *)port;
  383. unsigned long flags = 0;
  384. dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
  385. spin_lock_irqsave(&up->port.lock, flags);
  386. if (break_state == -1)
  387. up->lcr |= UART_LCR_SBC;
  388. else
  389. up->lcr &= ~UART_LCR_SBC;
  390. serial_out(up, UART_LCR, up->lcr);
  391. spin_unlock_irqrestore(&up->port.lock, flags);
  392. }
  393. static int serial_omap_startup(struct uart_port *port)
  394. {
  395. struct uart_omap_port *up = (struct uart_omap_port *)port;
  396. unsigned long flags = 0;
  397. int retval;
  398. /*
  399. * Allocate the IRQ
  400. */
  401. retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
  402. up->name, up);
  403. if (retval)
  404. return retval;
  405. dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
  406. /*
  407. * Clear the FIFO buffers and disable them.
  408. * (they will be reenabled in set_termios())
  409. */
  410. serial_omap_clear_fifos(up);
  411. /* For Hardware flow control */
  412. serial_out(up, UART_MCR, UART_MCR_RTS);
  413. /*
  414. * Clear the interrupt registers.
  415. */
  416. (void) serial_in(up, UART_LSR);
  417. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  418. (void) serial_in(up, UART_RX);
  419. (void) serial_in(up, UART_IIR);
  420. (void) serial_in(up, UART_MSR);
  421. /*
  422. * Now, initialize the UART
  423. */
  424. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  425. spin_lock_irqsave(&up->port.lock, flags);
  426. /*
  427. * Most PC uarts need OUT2 raised to enable interrupts.
  428. */
  429. up->port.mctrl |= TIOCM_OUT2;
  430. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  431. spin_unlock_irqrestore(&up->port.lock, flags);
  432. up->msr_saved_flags = 0;
  433. if (up->use_dma) {
  434. free_page((unsigned long)up->port.state->xmit.buf);
  435. up->port.state->xmit.buf = dma_alloc_coherent(NULL,
  436. UART_XMIT_SIZE,
  437. (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
  438. 0);
  439. init_timer(&(up->uart_dma.rx_timer));
  440. up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
  441. up->uart_dma.rx_timer.data = up->pdev->id;
  442. /* Currently the buffer size is 4KB. Can increase it */
  443. up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
  444. up->uart_dma.rx_buf_size,
  445. (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
  446. }
  447. /*
  448. * Finally, enable interrupts. Note: Modem status interrupts
  449. * are set via set_termios(), which will be occurring imminently
  450. * anyway, so we don't enable them here.
  451. */
  452. up->ier = UART_IER_RLSI | UART_IER_RDI;
  453. serial_out(up, UART_IER, up->ier);
  454. up->port_activity = jiffies;
  455. return 0;
  456. }
  457. static void serial_omap_shutdown(struct uart_port *port)
  458. {
  459. struct uart_omap_port *up = (struct uart_omap_port *)port;
  460. unsigned long flags = 0;
  461. dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
  462. /*
  463. * Disable interrupts from this port
  464. */
  465. up->ier = 0;
  466. serial_out(up, UART_IER, 0);
  467. spin_lock_irqsave(&up->port.lock, flags);
  468. up->port.mctrl &= ~TIOCM_OUT2;
  469. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  470. spin_unlock_irqrestore(&up->port.lock, flags);
  471. /*
  472. * Disable break condition and FIFOs
  473. */
  474. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  475. serial_omap_clear_fifos(up);
  476. /*
  477. * Read data port to reset things, and then free the irq
  478. */
  479. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  480. (void) serial_in(up, UART_RX);
  481. if (up->use_dma) {
  482. dma_free_coherent(up->port.dev,
  483. UART_XMIT_SIZE, up->port.state->xmit.buf,
  484. up->uart_dma.tx_buf_dma_phys);
  485. up->port.state->xmit.buf = NULL;
  486. serial_omap_stop_rx(port);
  487. dma_free_coherent(up->port.dev,
  488. up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
  489. up->uart_dma.rx_buf_dma_phys);
  490. up->uart_dma.rx_buf = NULL;
  491. }
  492. free_irq(up->port.irq, up);
  493. }
  494. static inline void
  495. serial_omap_configure_xonxoff
  496. (struct uart_omap_port *up, struct ktermios *termios)
  497. {
  498. unsigned char efr = 0;
  499. up->lcr = serial_in(up, UART_LCR);
  500. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  501. up->efr = serial_in(up, UART_EFR);
  502. serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
  503. serial_out(up, UART_XON1, termios->c_cc[VSTART]);
  504. serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
  505. /* clear SW control mode bits */
  506. efr = up->efr;
  507. efr &= OMAP_UART_SW_CLR;
  508. /*
  509. * IXON Flag:
  510. * Enable XON/XOFF flow control on output.
  511. * Transmit XON1, XOFF1
  512. */
  513. if (termios->c_iflag & IXON)
  514. efr |= OMAP_UART_SW_TX;
  515. /*
  516. * IXOFF Flag:
  517. * Enable XON/XOFF flow control on input.
  518. * Receiver compares XON1, XOFF1.
  519. */
  520. if (termios->c_iflag & IXOFF)
  521. efr |= OMAP_UART_SW_RX;
  522. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  523. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  524. up->mcr = serial_in(up, UART_MCR);
  525. /*
  526. * IXANY Flag:
  527. * Enable any character to restart output.
  528. * Operation resumes after receiving any
  529. * character after recognition of the XOFF character
  530. */
  531. if (termios->c_iflag & IXANY)
  532. up->mcr |= UART_MCR_XONANY;
  533. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  534. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  535. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
  536. /* Enable special char function UARTi.EFR_REG[5] and
  537. * load the new software flow control mode IXON or IXOFF
  538. * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
  539. */
  540. serial_out(up, UART_EFR, efr | UART_EFR_SCD);
  541. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  542. serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
  543. serial_out(up, UART_LCR, up->lcr);
  544. }
  545. static void
  546. serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
  547. struct ktermios *old)
  548. {
  549. struct uart_omap_port *up = (struct uart_omap_port *)port;
  550. unsigned char cval = 0;
  551. unsigned char efr = 0;
  552. unsigned long flags = 0;
  553. unsigned int baud, quot;
  554. switch (termios->c_cflag & CSIZE) {
  555. case CS5:
  556. cval = UART_LCR_WLEN5;
  557. break;
  558. case CS6:
  559. cval = UART_LCR_WLEN6;
  560. break;
  561. case CS7:
  562. cval = UART_LCR_WLEN7;
  563. break;
  564. default:
  565. case CS8:
  566. cval = UART_LCR_WLEN8;
  567. break;
  568. }
  569. if (termios->c_cflag & CSTOPB)
  570. cval |= UART_LCR_STOP;
  571. if (termios->c_cflag & PARENB)
  572. cval |= UART_LCR_PARITY;
  573. if (!(termios->c_cflag & PARODD))
  574. cval |= UART_LCR_EPAR;
  575. /*
  576. * Ask the core to calculate the divisor for us.
  577. */
  578. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
  579. quot = serial_omap_get_divisor(port, baud);
  580. up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
  581. UART_FCR_ENABLE_FIFO;
  582. if (up->use_dma)
  583. up->fcr |= UART_FCR_DMA_SELECT;
  584. /*
  585. * Ok, we're now changing the port state. Do it with
  586. * interrupts disabled.
  587. */
  588. spin_lock_irqsave(&up->port.lock, flags);
  589. /*
  590. * Update the per-port timeout.
  591. */
  592. uart_update_timeout(port, termios->c_cflag, baud);
  593. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  594. if (termios->c_iflag & INPCK)
  595. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  596. if (termios->c_iflag & (BRKINT | PARMRK))
  597. up->port.read_status_mask |= UART_LSR_BI;
  598. /*
  599. * Characters to ignore
  600. */
  601. up->port.ignore_status_mask = 0;
  602. if (termios->c_iflag & IGNPAR)
  603. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  604. if (termios->c_iflag & IGNBRK) {
  605. up->port.ignore_status_mask |= UART_LSR_BI;
  606. /*
  607. * If we're ignoring parity and break indicators,
  608. * ignore overruns too (for real raw support).
  609. */
  610. if (termios->c_iflag & IGNPAR)
  611. up->port.ignore_status_mask |= UART_LSR_OE;
  612. }
  613. /*
  614. * ignore all characters if CREAD is not set
  615. */
  616. if ((termios->c_cflag & CREAD) == 0)
  617. up->port.ignore_status_mask |= UART_LSR_DR;
  618. /*
  619. * Modem status interrupts
  620. */
  621. up->ier &= ~UART_IER_MSI;
  622. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  623. up->ier |= UART_IER_MSI;
  624. serial_out(up, UART_IER, up->ier);
  625. serial_out(up, UART_LCR, cval); /* reset DLAB */
  626. /* FIFOs and DMA Settings */
  627. /* FCR can be changed only when the
  628. * baud clock is not running
  629. * DLL_REG and DLH_REG set to 0.
  630. */
  631. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  632. serial_out(up, UART_DLL, 0);
  633. serial_out(up, UART_DLM, 0);
  634. serial_out(up, UART_LCR, 0);
  635. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  636. up->efr = serial_in(up, UART_EFR);
  637. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  638. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  639. up->mcr = serial_in(up, UART_MCR);
  640. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  641. /* FIFO ENABLE, DMA MODE */
  642. serial_out(up, UART_FCR, up->fcr);
  643. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  644. if (up->use_dma) {
  645. serial_out(up, UART_TI752_TLR, 0);
  646. serial_out(up, UART_OMAP_SCR,
  647. (UART_FCR_TRIGGER_4 | UART_FCR_TRIGGER_8));
  648. }
  649. serial_out(up, UART_EFR, up->efr);
  650. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  651. serial_out(up, UART_MCR, up->mcr);
  652. /* Protocol, Baud Rate, and Interrupt Settings */
  653. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
  654. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  655. up->efr = serial_in(up, UART_EFR);
  656. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  657. serial_out(up, UART_LCR, 0);
  658. serial_out(up, UART_IER, 0);
  659. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  660. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  661. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  662. serial_out(up, UART_LCR, 0);
  663. serial_out(up, UART_IER, up->ier);
  664. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  665. serial_out(up, UART_EFR, up->efr);
  666. serial_out(up, UART_LCR, cval);
  667. if (baud > 230400 && baud != 3000000)
  668. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
  669. else
  670. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
  671. /* Hardware Flow Control Configuration */
  672. if (termios->c_cflag & CRTSCTS) {
  673. efr |= (UART_EFR_CTS | UART_EFR_RTS);
  674. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  675. up->mcr = serial_in(up, UART_MCR);
  676. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  677. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  678. up->efr = serial_in(up, UART_EFR);
  679. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  680. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
  681. serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
  682. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  683. serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
  684. serial_out(up, UART_LCR, cval);
  685. }
  686. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  687. /* Software Flow Control Configuration */
  688. if (termios->c_iflag & (IXON | IXOFF))
  689. serial_omap_configure_xonxoff(up, termios);
  690. spin_unlock_irqrestore(&up->port.lock, flags);
  691. dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
  692. }
  693. static void
  694. serial_omap_pm(struct uart_port *port, unsigned int state,
  695. unsigned int oldstate)
  696. {
  697. struct uart_omap_port *up = (struct uart_omap_port *)port;
  698. unsigned char efr;
  699. dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
  700. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  701. efr = serial_in(up, UART_EFR);
  702. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  703. serial_out(up, UART_LCR, 0);
  704. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  705. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  706. serial_out(up, UART_EFR, efr);
  707. serial_out(up, UART_LCR, 0);
  708. /* Enable module level wake up */
  709. serial_out(up, UART_OMAP_WER,
  710. (state != 0) ? OMAP_UART_WER_MOD_WKUP : 0);
  711. }
  712. static void serial_omap_release_port(struct uart_port *port)
  713. {
  714. dev_dbg(port->dev, "serial_omap_release_port+\n");
  715. }
  716. static int serial_omap_request_port(struct uart_port *port)
  717. {
  718. dev_dbg(port->dev, "serial_omap_request_port+\n");
  719. return 0;
  720. }
  721. static void serial_omap_config_port(struct uart_port *port, int flags)
  722. {
  723. struct uart_omap_port *up = (struct uart_omap_port *)port;
  724. dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
  725. up->pdev->id);
  726. up->port.type = PORT_OMAP;
  727. }
  728. static int
  729. serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
  730. {
  731. /* we don't want the core code to modify any port params */
  732. dev_dbg(port->dev, "serial_omap_verify_port+\n");
  733. return -EINVAL;
  734. }
  735. static const char *
  736. serial_omap_type(struct uart_port *port)
  737. {
  738. struct uart_omap_port *up = (struct uart_omap_port *)port;
  739. dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
  740. return up->name;
  741. }
  742. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  743. static inline void wait_for_xmitr(struct uart_omap_port *up)
  744. {
  745. unsigned int status, tmout = 10000;
  746. /* Wait up to 10ms for the character(s) to be sent. */
  747. do {
  748. status = serial_in(up, UART_LSR);
  749. if (status & UART_LSR_BI)
  750. up->lsr_break_flag = UART_LSR_BI;
  751. if (--tmout == 0)
  752. break;
  753. udelay(1);
  754. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  755. /* Wait up to 1s for flow control if necessary */
  756. if (up->port.flags & UPF_CONS_FLOW) {
  757. tmout = 1000000;
  758. for (tmout = 1000000; tmout; tmout--) {
  759. unsigned int msr = serial_in(up, UART_MSR);
  760. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  761. if (msr & UART_MSR_CTS)
  762. break;
  763. udelay(1);
  764. }
  765. }
  766. }
  767. #ifdef CONFIG_CONSOLE_POLL
  768. static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
  769. {
  770. struct uart_omap_port *up = (struct uart_omap_port *)port;
  771. wait_for_xmitr(up);
  772. serial_out(up, UART_TX, ch);
  773. }
  774. static int serial_omap_poll_get_char(struct uart_port *port)
  775. {
  776. struct uart_omap_port *up = (struct uart_omap_port *)port;
  777. unsigned int status = serial_in(up, UART_LSR);
  778. if (!(status & UART_LSR_DR))
  779. return NO_POLL_CHAR;
  780. return serial_in(up, UART_RX);
  781. }
  782. #endif /* CONFIG_CONSOLE_POLL */
  783. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  784. static struct uart_omap_port *serial_omap_console_ports[4];
  785. static struct uart_driver serial_omap_reg;
  786. static void serial_omap_console_putchar(struct uart_port *port, int ch)
  787. {
  788. struct uart_omap_port *up = (struct uart_omap_port *)port;
  789. wait_for_xmitr(up);
  790. serial_out(up, UART_TX, ch);
  791. }
  792. static void
  793. serial_omap_console_write(struct console *co, const char *s,
  794. unsigned int count)
  795. {
  796. struct uart_omap_port *up = serial_omap_console_ports[co->index];
  797. unsigned long flags;
  798. unsigned int ier;
  799. int locked = 1;
  800. local_irq_save(flags);
  801. if (up->port.sysrq)
  802. locked = 0;
  803. else if (oops_in_progress)
  804. locked = spin_trylock(&up->port.lock);
  805. else
  806. spin_lock(&up->port.lock);
  807. /*
  808. * First save the IER then disable the interrupts
  809. */
  810. ier = serial_in(up, UART_IER);
  811. serial_out(up, UART_IER, 0);
  812. uart_console_write(&up->port, s, count, serial_omap_console_putchar);
  813. /*
  814. * Finally, wait for transmitter to become empty
  815. * and restore the IER
  816. */
  817. wait_for_xmitr(up);
  818. serial_out(up, UART_IER, ier);
  819. /*
  820. * The receive handling will happen properly because the
  821. * receive ready bit will still be set; it is not cleared
  822. * on read. However, modem control will not, we must
  823. * call it if we have saved something in the saved flags
  824. * while processing with interrupts off.
  825. */
  826. if (up->msr_saved_flags)
  827. check_modem_status(up);
  828. if (locked)
  829. spin_unlock(&up->port.lock);
  830. local_irq_restore(flags);
  831. }
  832. static int __init
  833. serial_omap_console_setup(struct console *co, char *options)
  834. {
  835. struct uart_omap_port *up;
  836. int baud = 115200;
  837. int bits = 8;
  838. int parity = 'n';
  839. int flow = 'n';
  840. if (serial_omap_console_ports[co->index] == NULL)
  841. return -ENODEV;
  842. up = serial_omap_console_ports[co->index];
  843. if (options)
  844. uart_parse_options(options, &baud, &parity, &bits, &flow);
  845. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  846. }
  847. static struct console serial_omap_console = {
  848. .name = OMAP_SERIAL_NAME,
  849. .write = serial_omap_console_write,
  850. .device = uart_console_device,
  851. .setup = serial_omap_console_setup,
  852. .flags = CON_PRINTBUFFER,
  853. .index = -1,
  854. .data = &serial_omap_reg,
  855. };
  856. static void serial_omap_add_console_port(struct uart_omap_port *up)
  857. {
  858. serial_omap_console_ports[up->pdev->id] = up;
  859. }
  860. #define OMAP_CONSOLE (&serial_omap_console)
  861. #else
  862. #define OMAP_CONSOLE NULL
  863. static inline void serial_omap_add_console_port(struct uart_omap_port *up)
  864. {}
  865. #endif
  866. static struct uart_ops serial_omap_pops = {
  867. .tx_empty = serial_omap_tx_empty,
  868. .set_mctrl = serial_omap_set_mctrl,
  869. .get_mctrl = serial_omap_get_mctrl,
  870. .stop_tx = serial_omap_stop_tx,
  871. .start_tx = serial_omap_start_tx,
  872. .stop_rx = serial_omap_stop_rx,
  873. .enable_ms = serial_omap_enable_ms,
  874. .break_ctl = serial_omap_break_ctl,
  875. .startup = serial_omap_startup,
  876. .shutdown = serial_omap_shutdown,
  877. .set_termios = serial_omap_set_termios,
  878. .pm = serial_omap_pm,
  879. .type = serial_omap_type,
  880. .release_port = serial_omap_release_port,
  881. .request_port = serial_omap_request_port,
  882. .config_port = serial_omap_config_port,
  883. .verify_port = serial_omap_verify_port,
  884. #ifdef CONFIG_CONSOLE_POLL
  885. .poll_put_char = serial_omap_poll_put_char,
  886. .poll_get_char = serial_omap_poll_get_char,
  887. #endif
  888. };
  889. static struct uart_driver serial_omap_reg = {
  890. .owner = THIS_MODULE,
  891. .driver_name = "OMAP-SERIAL",
  892. .dev_name = OMAP_SERIAL_NAME,
  893. .nr = OMAP_MAX_HSUART_PORTS,
  894. .cons = OMAP_CONSOLE,
  895. };
  896. static int
  897. serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
  898. {
  899. struct uart_omap_port *up = platform_get_drvdata(pdev);
  900. if (up)
  901. uart_suspend_port(&serial_omap_reg, &up->port);
  902. return 0;
  903. }
  904. static int serial_omap_resume(struct platform_device *dev)
  905. {
  906. struct uart_omap_port *up = platform_get_drvdata(dev);
  907. if (up)
  908. uart_resume_port(&serial_omap_reg, &up->port);
  909. return 0;
  910. }
  911. static void serial_omap_rx_timeout(unsigned long uart_no)
  912. {
  913. struct uart_omap_port *up = ui[uart_no];
  914. unsigned int curr_dma_pos, curr_transmitted_size;
  915. int ret = 0;
  916. curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
  917. if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
  918. (curr_dma_pos == 0)) {
  919. if (jiffies_to_msecs(jiffies - up->port_activity) <
  920. RX_TIMEOUT) {
  921. mod_timer(&up->uart_dma.rx_timer, jiffies +
  922. usecs_to_jiffies(up->uart_dma.rx_timeout));
  923. } else {
  924. serial_omap_stop_rxdma(up);
  925. up->ier |= (UART_IER_RDI | UART_IER_RLSI);
  926. serial_out(up, UART_IER, up->ier);
  927. }
  928. return;
  929. }
  930. curr_transmitted_size = curr_dma_pos -
  931. up->uart_dma.prev_rx_dma_pos;
  932. up->port.icount.rx += curr_transmitted_size;
  933. tty_insert_flip_string(up->port.state->port.tty,
  934. up->uart_dma.rx_buf +
  935. (up->uart_dma.prev_rx_dma_pos -
  936. up->uart_dma.rx_buf_dma_phys),
  937. curr_transmitted_size);
  938. tty_flip_buffer_push(up->port.state->port.tty);
  939. up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
  940. if (up->uart_dma.rx_buf_size +
  941. up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
  942. ret = serial_omap_start_rxdma(up);
  943. if (ret < 0) {
  944. serial_omap_stop_rxdma(up);
  945. up->ier |= (UART_IER_RDI | UART_IER_RLSI);
  946. serial_out(up, UART_IER, up->ier);
  947. }
  948. } else {
  949. mod_timer(&up->uart_dma.rx_timer, jiffies +
  950. usecs_to_jiffies(up->uart_dma.rx_timeout));
  951. }
  952. up->port_activity = jiffies;
  953. }
  954. static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
  955. {
  956. return;
  957. }
  958. static int serial_omap_start_rxdma(struct uart_omap_port *up)
  959. {
  960. int ret = 0;
  961. if (up->uart_dma.rx_dma_channel == -1) {
  962. ret = omap_request_dma(up->uart_dma.uart_dma_rx,
  963. "UART Rx DMA",
  964. (void *)uart_rx_dma_callback, up,
  965. &(up->uart_dma.rx_dma_channel));
  966. if (ret < 0)
  967. return ret;
  968. omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
  969. OMAP_DMA_AMODE_CONSTANT,
  970. up->uart_dma.uart_base, 0, 0);
  971. omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
  972. OMAP_DMA_AMODE_POST_INC,
  973. up->uart_dma.rx_buf_dma_phys, 0, 0);
  974. omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
  975. OMAP_DMA_DATA_TYPE_S8,
  976. up->uart_dma.rx_buf_size, 1,
  977. OMAP_DMA_SYNC_ELEMENT,
  978. up->uart_dma.uart_dma_rx, 0);
  979. }
  980. up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
  981. /* FIXME: Cache maintenance needed here? */
  982. omap_start_dma(up->uart_dma.rx_dma_channel);
  983. mod_timer(&up->uart_dma.rx_timer, jiffies +
  984. usecs_to_jiffies(up->uart_dma.rx_timeout));
  985. up->uart_dma.rx_dma_used = true;
  986. return ret;
  987. }
  988. static void serial_omap_continue_tx(struct uart_omap_port *up)
  989. {
  990. struct circ_buf *xmit = &up->port.state->xmit;
  991. unsigned int start = up->uart_dma.tx_buf_dma_phys
  992. + (xmit->tail & (UART_XMIT_SIZE - 1));
  993. if (uart_circ_empty(xmit))
  994. return;
  995. up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
  996. /*
  997. * It is a circular buffer. See if the buffer has wounded back.
  998. * If yes it will have to be transferred in two separate dma
  999. * transfers
  1000. */
  1001. if (start + up->uart_dma.tx_buf_size >=
  1002. up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
  1003. up->uart_dma.tx_buf_size =
  1004. (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
  1005. omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
  1006. OMAP_DMA_AMODE_CONSTANT,
  1007. up->uart_dma.uart_base, 0, 0);
  1008. omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
  1009. OMAP_DMA_AMODE_POST_INC, start, 0, 0);
  1010. omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
  1011. OMAP_DMA_DATA_TYPE_S8,
  1012. up->uart_dma.tx_buf_size, 1,
  1013. OMAP_DMA_SYNC_ELEMENT,
  1014. up->uart_dma.uart_dma_tx, 0);
  1015. /* FIXME: Cache maintenance needed here? */
  1016. omap_start_dma(up->uart_dma.tx_dma_channel);
  1017. }
  1018. static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
  1019. {
  1020. struct uart_omap_port *up = (struct uart_omap_port *)data;
  1021. struct circ_buf *xmit = &up->port.state->xmit;
  1022. xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
  1023. (UART_XMIT_SIZE - 1);
  1024. up->port.icount.tx += up->uart_dma.tx_buf_size;
  1025. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1026. uart_write_wakeup(&up->port);
  1027. if (uart_circ_empty(xmit)) {
  1028. spin_lock(&(up->uart_dma.tx_lock));
  1029. serial_omap_stop_tx(&up->port);
  1030. up->uart_dma.tx_dma_used = false;
  1031. spin_unlock(&(up->uart_dma.tx_lock));
  1032. } else {
  1033. omap_stop_dma(up->uart_dma.tx_dma_channel);
  1034. serial_omap_continue_tx(up);
  1035. }
  1036. up->port_activity = jiffies;
  1037. return;
  1038. }
  1039. static int serial_omap_probe(struct platform_device *pdev)
  1040. {
  1041. struct uart_omap_port *up;
  1042. struct resource *mem, *irq, *dma_tx, *dma_rx;
  1043. struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
  1044. int ret = -ENOSPC;
  1045. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1046. if (!mem) {
  1047. dev_err(&pdev->dev, "no mem resource?\n");
  1048. return -ENODEV;
  1049. }
  1050. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1051. if (!irq) {
  1052. dev_err(&pdev->dev, "no irq resource?\n");
  1053. return -ENODEV;
  1054. }
  1055. if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
  1056. pdev->dev.driver->name)) {
  1057. dev_err(&pdev->dev, "memory region already claimed\n");
  1058. return -EBUSY;
  1059. }
  1060. dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1061. if (!dma_rx) {
  1062. ret = -EINVAL;
  1063. goto err;
  1064. }
  1065. dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1066. if (!dma_tx) {
  1067. ret = -EINVAL;
  1068. goto err;
  1069. }
  1070. up = kzalloc(sizeof(*up), GFP_KERNEL);
  1071. if (up == NULL) {
  1072. ret = -ENOMEM;
  1073. goto do_release_region;
  1074. }
  1075. sprintf(up->name, "OMAP UART%d", pdev->id);
  1076. up->pdev = pdev;
  1077. up->port.dev = &pdev->dev;
  1078. up->port.type = PORT_OMAP;
  1079. up->port.iotype = UPIO_MEM;
  1080. up->port.irq = irq->start;
  1081. up->port.regshift = 2;
  1082. up->port.fifosize = 64;
  1083. up->port.ops = &serial_omap_pops;
  1084. up->port.line = pdev->id;
  1085. up->port.membase = omap_up_info->membase;
  1086. up->port.mapbase = omap_up_info->mapbase;
  1087. up->port.flags = omap_up_info->flags;
  1088. up->port.irqflags = omap_up_info->irqflags;
  1089. up->port.uartclk = omap_up_info->uartclk;
  1090. up->uart_dma.uart_base = mem->start;
  1091. if (omap_up_info->dma_enabled) {
  1092. up->uart_dma.uart_dma_tx = dma_tx->start;
  1093. up->uart_dma.uart_dma_rx = dma_rx->start;
  1094. up->use_dma = 1;
  1095. up->uart_dma.rx_buf_size = 4096;
  1096. up->uart_dma.rx_timeout = 2;
  1097. spin_lock_init(&(up->uart_dma.tx_lock));
  1098. spin_lock_init(&(up->uart_dma.rx_lock));
  1099. up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
  1100. up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
  1101. }
  1102. ui[pdev->id] = up;
  1103. serial_omap_add_console_port(up);
  1104. ret = uart_add_one_port(&serial_omap_reg, &up->port);
  1105. if (ret != 0)
  1106. goto do_release_region;
  1107. platform_set_drvdata(pdev, up);
  1108. return 0;
  1109. err:
  1110. dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
  1111. pdev->id, __func__, ret);
  1112. do_release_region:
  1113. release_mem_region(mem->start, (mem->end - mem->start) + 1);
  1114. return ret;
  1115. }
  1116. static int serial_omap_remove(struct platform_device *dev)
  1117. {
  1118. struct uart_omap_port *up = platform_get_drvdata(dev);
  1119. platform_set_drvdata(dev, NULL);
  1120. if (up) {
  1121. uart_remove_one_port(&serial_omap_reg, &up->port);
  1122. kfree(up);
  1123. }
  1124. return 0;
  1125. }
  1126. static struct platform_driver serial_omap_driver = {
  1127. .probe = serial_omap_probe,
  1128. .remove = serial_omap_remove,
  1129. .suspend = serial_omap_suspend,
  1130. .resume = serial_omap_resume,
  1131. .driver = {
  1132. .name = DRIVER_NAME,
  1133. },
  1134. };
  1135. static int __init serial_omap_init(void)
  1136. {
  1137. int ret;
  1138. ret = uart_register_driver(&serial_omap_reg);
  1139. if (ret != 0)
  1140. return ret;
  1141. ret = platform_driver_register(&serial_omap_driver);
  1142. if (ret != 0)
  1143. uart_unregister_driver(&serial_omap_reg);
  1144. return ret;
  1145. }
  1146. static void __exit serial_omap_exit(void)
  1147. {
  1148. platform_driver_unregister(&serial_omap_driver);
  1149. uart_unregister_driver(&serial_omap_reg);
  1150. }
  1151. module_init(serial_omap_init);
  1152. module_exit(serial_omap_exit);
  1153. MODULE_DESCRIPTION("OMAP High Speed UART driver");
  1154. MODULE_LICENSE("GPL");
  1155. MODULE_AUTHOR("Texas Instruments Inc");