omap-serial.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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, OMAP_UART_LCR_CONF_MDB);
  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_DLAB);
  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, OMAP_UART_LCR_CONF_MDB);
  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_DLAB);
  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_DLAB);
  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, OMAP_UART_LCR_CONF_MDB);
  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_DLAB);
  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, OMAP_UART_LCR_CONF_MDB);
  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_DLAB);
  651. serial_out(up, UART_MCR, up->mcr);
  652. /* Protocol, Baud Rate, and Interrupt Settings */
  653. serial_out(up, UART_OMAP_MDR1, OMAP_MDR1_DISABLE);
  654. serial_out(up, UART_LCR, OMAP_UART_LCR_CONF_MDB);
  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, OMAP_UART_LCR_CONF_MDB);
  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, OMAP_UART_LCR_CONF_MDB);
  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, OMAP_MDR1_MODE13X);
  669. else
  670. serial_out(up, UART_OMAP_MDR1, OMAP_MDR1_MODE16X);
  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_DLAB);
  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, OMAP_UART_LCR_CONF_MDB);
  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_DLAB);
  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, OMAP_UART_LCR_CONF_MDB);
  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, OMAP_UART_LCR_CONF_MDB);
  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. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  743. static struct uart_omap_port *serial_omap_console_ports[4];
  744. static struct uart_driver serial_omap_reg;
  745. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  746. static inline void wait_for_xmitr(struct uart_omap_port *up)
  747. {
  748. unsigned int status, tmout = 10000;
  749. /* Wait up to 10ms for the character(s) to be sent. */
  750. do {
  751. status = serial_in(up, UART_LSR);
  752. if (status & UART_LSR_BI)
  753. up->lsr_break_flag = UART_LSR_BI;
  754. if (--tmout == 0)
  755. break;
  756. udelay(1);
  757. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  758. /* Wait up to 1s for flow control if necessary */
  759. if (up->port.flags & UPF_CONS_FLOW) {
  760. tmout = 1000000;
  761. for (tmout = 1000000; tmout; tmout--) {
  762. unsigned int msr = serial_in(up, UART_MSR);
  763. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  764. if (msr & UART_MSR_CTS)
  765. break;
  766. udelay(1);
  767. }
  768. }
  769. }
  770. static void serial_omap_console_putchar(struct uart_port *port, int ch)
  771. {
  772. struct uart_omap_port *up = (struct uart_omap_port *)port;
  773. wait_for_xmitr(up);
  774. serial_out(up, UART_TX, ch);
  775. }
  776. static void
  777. serial_omap_console_write(struct console *co, const char *s,
  778. unsigned int count)
  779. {
  780. struct uart_omap_port *up = serial_omap_console_ports[co->index];
  781. unsigned long flags;
  782. unsigned int ier;
  783. int locked = 1;
  784. local_irq_save(flags);
  785. if (up->port.sysrq)
  786. locked = 0;
  787. else if (oops_in_progress)
  788. locked = spin_trylock(&up->port.lock);
  789. else
  790. spin_lock(&up->port.lock);
  791. /*
  792. * First save the IER then disable the interrupts
  793. */
  794. ier = serial_in(up, UART_IER);
  795. serial_out(up, UART_IER, 0);
  796. uart_console_write(&up->port, s, count, serial_omap_console_putchar);
  797. /*
  798. * Finally, wait for transmitter to become empty
  799. * and restore the IER
  800. */
  801. wait_for_xmitr(up);
  802. serial_out(up, UART_IER, ier);
  803. /*
  804. * The receive handling will happen properly because the
  805. * receive ready bit will still be set; it is not cleared
  806. * on read. However, modem control will not, we must
  807. * call it if we have saved something in the saved flags
  808. * while processing with interrupts off.
  809. */
  810. if (up->msr_saved_flags)
  811. check_modem_status(up);
  812. if (locked)
  813. spin_unlock(&up->port.lock);
  814. local_irq_restore(flags);
  815. }
  816. static int __init
  817. serial_omap_console_setup(struct console *co, char *options)
  818. {
  819. struct uart_omap_port *up;
  820. int baud = 115200;
  821. int bits = 8;
  822. int parity = 'n';
  823. int flow = 'n';
  824. if (serial_omap_console_ports[co->index] == NULL)
  825. return -ENODEV;
  826. up = serial_omap_console_ports[co->index];
  827. if (options)
  828. uart_parse_options(options, &baud, &parity, &bits, &flow);
  829. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  830. }
  831. static struct console serial_omap_console = {
  832. .name = OMAP_SERIAL_NAME,
  833. .write = serial_omap_console_write,
  834. .device = uart_console_device,
  835. .setup = serial_omap_console_setup,
  836. .flags = CON_PRINTBUFFER,
  837. .index = -1,
  838. .data = &serial_omap_reg,
  839. };
  840. static void serial_omap_add_console_port(struct uart_omap_port *up)
  841. {
  842. serial_omap_console_ports[up->pdev->id] = up;
  843. }
  844. #define OMAP_CONSOLE (&serial_omap_console)
  845. #else
  846. #define OMAP_CONSOLE NULL
  847. static inline void serial_omap_add_console_port(struct uart_omap_port *up)
  848. {}
  849. #endif
  850. static struct uart_ops serial_omap_pops = {
  851. .tx_empty = serial_omap_tx_empty,
  852. .set_mctrl = serial_omap_set_mctrl,
  853. .get_mctrl = serial_omap_get_mctrl,
  854. .stop_tx = serial_omap_stop_tx,
  855. .start_tx = serial_omap_start_tx,
  856. .stop_rx = serial_omap_stop_rx,
  857. .enable_ms = serial_omap_enable_ms,
  858. .break_ctl = serial_omap_break_ctl,
  859. .startup = serial_omap_startup,
  860. .shutdown = serial_omap_shutdown,
  861. .set_termios = serial_omap_set_termios,
  862. .pm = serial_omap_pm,
  863. .type = serial_omap_type,
  864. .release_port = serial_omap_release_port,
  865. .request_port = serial_omap_request_port,
  866. .config_port = serial_omap_config_port,
  867. .verify_port = serial_omap_verify_port,
  868. };
  869. static struct uart_driver serial_omap_reg = {
  870. .owner = THIS_MODULE,
  871. .driver_name = "OMAP-SERIAL",
  872. .dev_name = OMAP_SERIAL_NAME,
  873. .nr = OMAP_MAX_HSUART_PORTS,
  874. .cons = OMAP_CONSOLE,
  875. };
  876. static int
  877. serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
  878. {
  879. struct uart_omap_port *up = platform_get_drvdata(pdev);
  880. if (up)
  881. uart_suspend_port(&serial_omap_reg, &up->port);
  882. return 0;
  883. }
  884. static int serial_omap_resume(struct platform_device *dev)
  885. {
  886. struct uart_omap_port *up = platform_get_drvdata(dev);
  887. if (up)
  888. uart_resume_port(&serial_omap_reg, &up->port);
  889. return 0;
  890. }
  891. static void serial_omap_rx_timeout(unsigned long uart_no)
  892. {
  893. struct uart_omap_port *up = ui[uart_no];
  894. unsigned int curr_dma_pos, curr_transmitted_size;
  895. int ret = 0;
  896. curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
  897. if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
  898. (curr_dma_pos == 0)) {
  899. if (jiffies_to_msecs(jiffies - up->port_activity) <
  900. RX_TIMEOUT) {
  901. mod_timer(&up->uart_dma.rx_timer, jiffies +
  902. usecs_to_jiffies(up->uart_dma.rx_timeout));
  903. } else {
  904. serial_omap_stop_rxdma(up);
  905. up->ier |= (UART_IER_RDI | UART_IER_RLSI);
  906. serial_out(up, UART_IER, up->ier);
  907. }
  908. return;
  909. }
  910. curr_transmitted_size = curr_dma_pos -
  911. up->uart_dma.prev_rx_dma_pos;
  912. up->port.icount.rx += curr_transmitted_size;
  913. tty_insert_flip_string(up->port.state->port.tty,
  914. up->uart_dma.rx_buf +
  915. (up->uart_dma.prev_rx_dma_pos -
  916. up->uart_dma.rx_buf_dma_phys),
  917. curr_transmitted_size);
  918. tty_flip_buffer_push(up->port.state->port.tty);
  919. up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
  920. if (up->uart_dma.rx_buf_size +
  921. up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
  922. ret = serial_omap_start_rxdma(up);
  923. if (ret < 0) {
  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. } else {
  929. mod_timer(&up->uart_dma.rx_timer, jiffies +
  930. usecs_to_jiffies(up->uart_dma.rx_timeout));
  931. }
  932. up->port_activity = jiffies;
  933. }
  934. static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
  935. {
  936. return;
  937. }
  938. static int serial_omap_start_rxdma(struct uart_omap_port *up)
  939. {
  940. int ret = 0;
  941. if (up->uart_dma.rx_dma_channel == -1) {
  942. ret = omap_request_dma(up->uart_dma.uart_dma_rx,
  943. "UART Rx DMA",
  944. (void *)uart_rx_dma_callback, up,
  945. &(up->uart_dma.rx_dma_channel));
  946. if (ret < 0)
  947. return ret;
  948. omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
  949. OMAP_DMA_AMODE_CONSTANT,
  950. up->uart_dma.uart_base, 0, 0);
  951. omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
  952. OMAP_DMA_AMODE_POST_INC,
  953. up->uart_dma.rx_buf_dma_phys, 0, 0);
  954. omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
  955. OMAP_DMA_DATA_TYPE_S8,
  956. up->uart_dma.rx_buf_size, 1,
  957. OMAP_DMA_SYNC_ELEMENT,
  958. up->uart_dma.uart_dma_rx, 0);
  959. }
  960. up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
  961. /* FIXME: Cache maintenance needed here? */
  962. omap_start_dma(up->uart_dma.rx_dma_channel);
  963. mod_timer(&up->uart_dma.rx_timer, jiffies +
  964. usecs_to_jiffies(up->uart_dma.rx_timeout));
  965. up->uart_dma.rx_dma_used = true;
  966. return ret;
  967. }
  968. static void serial_omap_continue_tx(struct uart_omap_port *up)
  969. {
  970. struct circ_buf *xmit = &up->port.state->xmit;
  971. unsigned int start = up->uart_dma.tx_buf_dma_phys
  972. + (xmit->tail & (UART_XMIT_SIZE - 1));
  973. if (uart_circ_empty(xmit))
  974. return;
  975. up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
  976. /*
  977. * It is a circular buffer. See if the buffer has wounded back.
  978. * If yes it will have to be transferred in two separate dma
  979. * transfers
  980. */
  981. if (start + up->uart_dma.tx_buf_size >=
  982. up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
  983. up->uart_dma.tx_buf_size =
  984. (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
  985. omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
  986. OMAP_DMA_AMODE_CONSTANT,
  987. up->uart_dma.uart_base, 0, 0);
  988. omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
  989. OMAP_DMA_AMODE_POST_INC, start, 0, 0);
  990. omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
  991. OMAP_DMA_DATA_TYPE_S8,
  992. up->uart_dma.tx_buf_size, 1,
  993. OMAP_DMA_SYNC_ELEMENT,
  994. up->uart_dma.uart_dma_tx, 0);
  995. /* FIXME: Cache maintenance needed here? */
  996. omap_start_dma(up->uart_dma.tx_dma_channel);
  997. }
  998. static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
  999. {
  1000. struct uart_omap_port *up = (struct uart_omap_port *)data;
  1001. struct circ_buf *xmit = &up->port.state->xmit;
  1002. xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
  1003. (UART_XMIT_SIZE - 1);
  1004. up->port.icount.tx += up->uart_dma.tx_buf_size;
  1005. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1006. uart_write_wakeup(&up->port);
  1007. if (uart_circ_empty(xmit)) {
  1008. spin_lock(&(up->uart_dma.tx_lock));
  1009. serial_omap_stop_tx(&up->port);
  1010. up->uart_dma.tx_dma_used = false;
  1011. spin_unlock(&(up->uart_dma.tx_lock));
  1012. } else {
  1013. omap_stop_dma(up->uart_dma.tx_dma_channel);
  1014. serial_omap_continue_tx(up);
  1015. }
  1016. up->port_activity = jiffies;
  1017. return;
  1018. }
  1019. static int serial_omap_probe(struct platform_device *pdev)
  1020. {
  1021. struct uart_omap_port *up;
  1022. struct resource *mem, *irq, *dma_tx, *dma_rx;
  1023. struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
  1024. int ret = -ENOSPC;
  1025. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1026. if (!mem) {
  1027. dev_err(&pdev->dev, "no mem resource?\n");
  1028. return -ENODEV;
  1029. }
  1030. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1031. if (!irq) {
  1032. dev_err(&pdev->dev, "no irq resource?\n");
  1033. return -ENODEV;
  1034. }
  1035. if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
  1036. pdev->dev.driver->name)) {
  1037. dev_err(&pdev->dev, "memory region already claimed\n");
  1038. return -EBUSY;
  1039. }
  1040. dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1041. if (!dma_rx) {
  1042. ret = -EINVAL;
  1043. goto err;
  1044. }
  1045. dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1046. if (!dma_tx) {
  1047. ret = -EINVAL;
  1048. goto err;
  1049. }
  1050. up = kzalloc(sizeof(*up), GFP_KERNEL);
  1051. if (up == NULL) {
  1052. ret = -ENOMEM;
  1053. goto do_release_region;
  1054. }
  1055. sprintf(up->name, "OMAP UART%d", pdev->id);
  1056. up->pdev = pdev;
  1057. up->port.dev = &pdev->dev;
  1058. up->port.type = PORT_OMAP;
  1059. up->port.iotype = UPIO_MEM;
  1060. up->port.irq = irq->start;
  1061. up->port.regshift = 2;
  1062. up->port.fifosize = 64;
  1063. up->port.ops = &serial_omap_pops;
  1064. up->port.line = pdev->id;
  1065. up->port.membase = omap_up_info->membase;
  1066. up->port.mapbase = omap_up_info->mapbase;
  1067. up->port.flags = omap_up_info->flags;
  1068. up->port.irqflags = omap_up_info->irqflags;
  1069. up->port.uartclk = omap_up_info->uartclk;
  1070. up->uart_dma.uart_base = mem->start;
  1071. if (omap_up_info->dma_enabled) {
  1072. up->uart_dma.uart_dma_tx = dma_tx->start;
  1073. up->uart_dma.uart_dma_rx = dma_rx->start;
  1074. up->use_dma = 1;
  1075. up->uart_dma.rx_buf_size = 4096;
  1076. up->uart_dma.rx_timeout = 2;
  1077. spin_lock_init(&(up->uart_dma.tx_lock));
  1078. spin_lock_init(&(up->uart_dma.rx_lock));
  1079. up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
  1080. up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
  1081. }
  1082. ui[pdev->id] = up;
  1083. serial_omap_add_console_port(up);
  1084. ret = uart_add_one_port(&serial_omap_reg, &up->port);
  1085. if (ret != 0)
  1086. goto do_release_region;
  1087. platform_set_drvdata(pdev, up);
  1088. return 0;
  1089. err:
  1090. dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
  1091. pdev->id, __func__, ret);
  1092. do_release_region:
  1093. release_mem_region(mem->start, (mem->end - mem->start) + 1);
  1094. return ret;
  1095. }
  1096. static int serial_omap_remove(struct platform_device *dev)
  1097. {
  1098. struct uart_omap_port *up = platform_get_drvdata(dev);
  1099. platform_set_drvdata(dev, NULL);
  1100. if (up) {
  1101. uart_remove_one_port(&serial_omap_reg, &up->port);
  1102. kfree(up);
  1103. }
  1104. return 0;
  1105. }
  1106. static struct platform_driver serial_omap_driver = {
  1107. .probe = serial_omap_probe,
  1108. .remove = serial_omap_remove,
  1109. .suspend = serial_omap_suspend,
  1110. .resume = serial_omap_resume,
  1111. .driver = {
  1112. .name = DRIVER_NAME,
  1113. },
  1114. };
  1115. static int __init serial_omap_init(void)
  1116. {
  1117. int ret;
  1118. ret = uart_register_driver(&serial_omap_reg);
  1119. if (ret != 0)
  1120. return ret;
  1121. ret = platform_driver_register(&serial_omap_driver);
  1122. if (ret != 0)
  1123. uart_unregister_driver(&serial_omap_reg);
  1124. return ret;
  1125. }
  1126. static void __exit serial_omap_exit(void)
  1127. {
  1128. platform_driver_unregister(&serial_omap_driver);
  1129. uart_unregister_driver(&serial_omap_reg);
  1130. }
  1131. module_init(serial_omap_init);
  1132. module_exit(serial_omap_exit);
  1133. MODULE_DESCRIPTION("OMAP High Speed UART driver");
  1134. MODULE_LICENSE("GPL");
  1135. MODULE_AUTHOR("Texas Instruments Inc");