omap-serial.c 45 KB

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