omap-serial.c 36 KB

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