omap-serial.c 36 KB

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