omap-serial.c 42 KB

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