omap-serial.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. #define OMAP_UART_AUTOSUSPEND_DELAY -1
  43. static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
  44. /* Forward declaration of functions */
  45. static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
  46. static void serial_omap_rx_timeout(unsigned long uart_no);
  47. static int serial_omap_start_rxdma(struct uart_omap_port *up);
  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. mcr |= up->mcr;
  409. serial_out(up, UART_MCR, mcr);
  410. pm_runtime_put(&up->pdev->dev);
  411. }
  412. static void serial_omap_break_ctl(struct uart_port *port, int break_state)
  413. {
  414. struct uart_omap_port *up = (struct uart_omap_port *)port;
  415. unsigned long flags = 0;
  416. dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
  417. pm_runtime_get_sync(&up->pdev->dev);
  418. spin_lock_irqsave(&up->port.lock, flags);
  419. if (break_state == -1)
  420. up->lcr |= UART_LCR_SBC;
  421. else
  422. up->lcr &= ~UART_LCR_SBC;
  423. serial_out(up, UART_LCR, up->lcr);
  424. spin_unlock_irqrestore(&up->port.lock, flags);
  425. pm_runtime_put(&up->pdev->dev);
  426. }
  427. static int serial_omap_startup(struct uart_port *port)
  428. {
  429. struct uart_omap_port *up = (struct uart_omap_port *)port;
  430. unsigned long flags = 0;
  431. int retval;
  432. /*
  433. * Allocate the IRQ
  434. */
  435. retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
  436. up->name, up);
  437. if (retval)
  438. return retval;
  439. dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
  440. pm_runtime_get_sync(&up->pdev->dev);
  441. /*
  442. * Clear the FIFO buffers and disable them.
  443. * (they will be reenabled in set_termios())
  444. */
  445. serial_omap_clear_fifos(up);
  446. /* For Hardware flow control */
  447. serial_out(up, UART_MCR, UART_MCR_RTS);
  448. /*
  449. * Clear the interrupt registers.
  450. */
  451. (void) serial_in(up, UART_LSR);
  452. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  453. (void) serial_in(up, UART_RX);
  454. (void) serial_in(up, UART_IIR);
  455. (void) serial_in(up, UART_MSR);
  456. /*
  457. * Now, initialize the UART
  458. */
  459. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  460. spin_lock_irqsave(&up->port.lock, flags);
  461. /*
  462. * Most PC uarts need OUT2 raised to enable interrupts.
  463. */
  464. up->port.mctrl |= TIOCM_OUT2;
  465. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  466. spin_unlock_irqrestore(&up->port.lock, flags);
  467. up->msr_saved_flags = 0;
  468. if (up->use_dma) {
  469. free_page((unsigned long)up->port.state->xmit.buf);
  470. up->port.state->xmit.buf = dma_alloc_coherent(NULL,
  471. UART_XMIT_SIZE,
  472. (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
  473. 0);
  474. init_timer(&(up->uart_dma.rx_timer));
  475. up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
  476. up->uart_dma.rx_timer.data = up->pdev->id;
  477. /* Currently the buffer size is 4KB. Can increase it */
  478. up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
  479. up->uart_dma.rx_buf_size,
  480. (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
  481. }
  482. /*
  483. * Finally, enable interrupts. Note: Modem status interrupts
  484. * are set via set_termios(), which will be occurring imminently
  485. * anyway, so we don't enable them here.
  486. */
  487. up->ier = UART_IER_RLSI | UART_IER_RDI;
  488. serial_out(up, UART_IER, up->ier);
  489. /* Enable module level wake up */
  490. serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
  491. pm_runtime_mark_last_busy(&up->pdev->dev);
  492. pm_runtime_put_autosuspend(&up->pdev->dev);
  493. up->port_activity = jiffies;
  494. return 0;
  495. }
  496. static void serial_omap_shutdown(struct uart_port *port)
  497. {
  498. struct uart_omap_port *up = (struct uart_omap_port *)port;
  499. unsigned long flags = 0;
  500. dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
  501. pm_runtime_get_sync(&up->pdev->dev);
  502. /*
  503. * Disable interrupts from this port
  504. */
  505. up->ier = 0;
  506. serial_out(up, UART_IER, 0);
  507. spin_lock_irqsave(&up->port.lock, flags);
  508. up->port.mctrl &= ~TIOCM_OUT2;
  509. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  510. spin_unlock_irqrestore(&up->port.lock, flags);
  511. /*
  512. * Disable break condition and FIFOs
  513. */
  514. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  515. serial_omap_clear_fifos(up);
  516. /*
  517. * Read data port to reset things, and then free the irq
  518. */
  519. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  520. (void) serial_in(up, UART_RX);
  521. if (up->use_dma) {
  522. dma_free_coherent(up->port.dev,
  523. UART_XMIT_SIZE, up->port.state->xmit.buf,
  524. up->uart_dma.tx_buf_dma_phys);
  525. up->port.state->xmit.buf = NULL;
  526. serial_omap_stop_rx(port);
  527. dma_free_coherent(up->port.dev,
  528. up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
  529. up->uart_dma.rx_buf_dma_phys);
  530. up->uart_dma.rx_buf = NULL;
  531. }
  532. pm_runtime_put(&up->pdev->dev);
  533. free_irq(up->port.irq, up);
  534. }
  535. static inline void
  536. serial_omap_configure_xonxoff
  537. (struct uart_omap_port *up, struct ktermios *termios)
  538. {
  539. unsigned char efr = 0;
  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. efr = up->efr;
  548. efr &= OMAP_UART_SW_CLR;
  549. /*
  550. * IXON Flag:
  551. * Enable XON/XOFF flow control on output.
  552. * Transmit XON1, XOFF1
  553. */
  554. if (termios->c_iflag & IXON)
  555. efr |= OMAP_UART_SW_TX;
  556. /*
  557. * IXOFF Flag:
  558. * Enable XON/XOFF flow control on input.
  559. * Receiver compares XON1, XOFF1.
  560. */
  561. if (termios->c_iflag & IXOFF)
  562. efr |= OMAP_UART_SW_RX;
  563. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  564. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  565. up->mcr = serial_in(up, UART_MCR);
  566. /*
  567. * IXANY Flag:
  568. * Enable any character to restart output.
  569. * Operation resumes after receiving any
  570. * character after recognition of the XOFF character
  571. */
  572. if (termios->c_iflag & IXANY)
  573. up->mcr |= UART_MCR_XONANY;
  574. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  575. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  576. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
  577. /* Enable special char function UARTi.EFR_REG[5] and
  578. * load the new software flow control mode IXON or IXOFF
  579. * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
  580. */
  581. serial_out(up, UART_EFR, efr | UART_EFR_SCD);
  582. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  583. serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
  584. serial_out(up, UART_LCR, up->lcr);
  585. }
  586. static void
  587. serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
  588. struct ktermios *old)
  589. {
  590. struct uart_omap_port *up = (struct uart_omap_port *)port;
  591. unsigned char cval = 0;
  592. unsigned char efr = 0;
  593. unsigned long flags = 0;
  594. unsigned int baud, quot;
  595. switch (termios->c_cflag & CSIZE) {
  596. case CS5:
  597. cval = UART_LCR_WLEN5;
  598. break;
  599. case CS6:
  600. cval = UART_LCR_WLEN6;
  601. break;
  602. case CS7:
  603. cval = UART_LCR_WLEN7;
  604. break;
  605. default:
  606. case CS8:
  607. cval = UART_LCR_WLEN8;
  608. break;
  609. }
  610. if (termios->c_cflag & CSTOPB)
  611. cval |= UART_LCR_STOP;
  612. if (termios->c_cflag & PARENB)
  613. cval |= UART_LCR_PARITY;
  614. if (!(termios->c_cflag & PARODD))
  615. cval |= UART_LCR_EPAR;
  616. /*
  617. * Ask the core to calculate the divisor for us.
  618. */
  619. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
  620. quot = serial_omap_get_divisor(port, baud);
  621. up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
  622. UART_FCR_ENABLE_FIFO;
  623. if (up->use_dma)
  624. up->fcr |= UART_FCR_DMA_SELECT;
  625. /*
  626. * Ok, we're now changing the port state. Do it with
  627. * interrupts disabled.
  628. */
  629. pm_runtime_get_sync(&up->pdev->dev);
  630. spin_lock_irqsave(&up->port.lock, flags);
  631. /*
  632. * Update the per-port timeout.
  633. */
  634. uart_update_timeout(port, termios->c_cflag, baud);
  635. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  636. if (termios->c_iflag & INPCK)
  637. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  638. if (termios->c_iflag & (BRKINT | PARMRK))
  639. up->port.read_status_mask |= UART_LSR_BI;
  640. /*
  641. * Characters to ignore
  642. */
  643. up->port.ignore_status_mask = 0;
  644. if (termios->c_iflag & IGNPAR)
  645. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  646. if (termios->c_iflag & IGNBRK) {
  647. up->port.ignore_status_mask |= UART_LSR_BI;
  648. /*
  649. * If we're ignoring parity and break indicators,
  650. * ignore overruns too (for real raw support).
  651. */
  652. if (termios->c_iflag & IGNPAR)
  653. up->port.ignore_status_mask |= UART_LSR_OE;
  654. }
  655. /*
  656. * ignore all characters if CREAD is not set
  657. */
  658. if ((termios->c_cflag & CREAD) == 0)
  659. up->port.ignore_status_mask |= UART_LSR_DR;
  660. /*
  661. * Modem status interrupts
  662. */
  663. up->ier &= ~UART_IER_MSI;
  664. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  665. up->ier |= UART_IER_MSI;
  666. serial_out(up, UART_IER, up->ier);
  667. serial_out(up, UART_LCR, cval); /* reset DLAB */
  668. /* FIFOs and DMA Settings */
  669. /* FCR can be changed only when the
  670. * baud clock is not running
  671. * DLL_REG and DLH_REG set to 0.
  672. */
  673. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  674. serial_out(up, UART_DLL, 0);
  675. serial_out(up, UART_DLM, 0);
  676. serial_out(up, UART_LCR, 0);
  677. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  678. up->efr = serial_in(up, UART_EFR);
  679. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  680. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  681. up->mcr = serial_in(up, UART_MCR);
  682. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  683. /* FIFO ENABLE, DMA MODE */
  684. serial_out(up, UART_FCR, up->fcr);
  685. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  686. if (up->use_dma) {
  687. serial_out(up, UART_TI752_TLR, 0);
  688. serial_out(up, UART_OMAP_SCR,
  689. (UART_FCR_TRIGGER_4 | UART_FCR_TRIGGER_8));
  690. }
  691. serial_out(up, UART_EFR, up->efr);
  692. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  693. serial_out(up, UART_MCR, up->mcr);
  694. /* Protocol, Baud Rate, and Interrupt Settings */
  695. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
  696. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  697. up->efr = serial_in(up, UART_EFR);
  698. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  699. serial_out(up, UART_LCR, 0);
  700. serial_out(up, UART_IER, 0);
  701. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  702. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  703. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  704. serial_out(up, UART_LCR, 0);
  705. serial_out(up, UART_IER, up->ier);
  706. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  707. serial_out(up, UART_EFR, up->efr);
  708. serial_out(up, UART_LCR, cval);
  709. if (baud > 230400 && baud != 3000000)
  710. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
  711. else
  712. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
  713. /* Hardware Flow Control Configuration */
  714. if (termios->c_cflag & CRTSCTS) {
  715. efr |= (UART_EFR_CTS | UART_EFR_RTS);
  716. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  717. up->mcr = serial_in(up, UART_MCR);
  718. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  719. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  720. up->efr = serial_in(up, UART_EFR);
  721. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  722. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
  723. serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
  724. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  725. serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
  726. serial_out(up, UART_LCR, cval);
  727. }
  728. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  729. /* Software Flow Control Configuration */
  730. serial_omap_configure_xonxoff(up, termios);
  731. spin_unlock_irqrestore(&up->port.lock, flags);
  732. pm_runtime_put(&up->pdev->dev);
  733. dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
  734. }
  735. static void
  736. serial_omap_pm(struct uart_port *port, unsigned int state,
  737. unsigned int oldstate)
  738. {
  739. struct uart_omap_port *up = (struct uart_omap_port *)port;
  740. unsigned char efr;
  741. dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
  742. pm_runtime_get_sync(&up->pdev->dev);
  743. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  744. efr = serial_in(up, UART_EFR);
  745. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  746. serial_out(up, UART_LCR, 0);
  747. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  748. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  749. serial_out(up, UART_EFR, efr);
  750. serial_out(up, UART_LCR, 0);
  751. if (!device_may_wakeup(&up->pdev->dev)) {
  752. if (!state)
  753. pm_runtime_forbid(&up->pdev->dev);
  754. else
  755. pm_runtime_allow(&up->pdev->dev);
  756. }
  757. pm_runtime_put(&up->pdev->dev);
  758. }
  759. static void serial_omap_release_port(struct uart_port *port)
  760. {
  761. dev_dbg(port->dev, "serial_omap_release_port+\n");
  762. }
  763. static int serial_omap_request_port(struct uart_port *port)
  764. {
  765. dev_dbg(port->dev, "serial_omap_request_port+\n");
  766. return 0;
  767. }
  768. static void serial_omap_config_port(struct uart_port *port, int flags)
  769. {
  770. struct uart_omap_port *up = (struct uart_omap_port *)port;
  771. dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
  772. up->pdev->id);
  773. up->port.type = PORT_OMAP;
  774. }
  775. static int
  776. serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
  777. {
  778. /* we don't want the core code to modify any port params */
  779. dev_dbg(port->dev, "serial_omap_verify_port+\n");
  780. return -EINVAL;
  781. }
  782. static const char *
  783. serial_omap_type(struct uart_port *port)
  784. {
  785. struct uart_omap_port *up = (struct uart_omap_port *)port;
  786. dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
  787. return up->name;
  788. }
  789. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  790. static inline void wait_for_xmitr(struct uart_omap_port *up)
  791. {
  792. unsigned int status, tmout = 10000;
  793. /* Wait up to 10ms for the character(s) to be sent. */
  794. do {
  795. status = serial_in(up, UART_LSR);
  796. if (status & UART_LSR_BI)
  797. up->lsr_break_flag = UART_LSR_BI;
  798. if (--tmout == 0)
  799. break;
  800. udelay(1);
  801. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  802. /* Wait up to 1s for flow control if necessary */
  803. if (up->port.flags & UPF_CONS_FLOW) {
  804. tmout = 1000000;
  805. for (tmout = 1000000; tmout; tmout--) {
  806. unsigned int msr = serial_in(up, UART_MSR);
  807. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  808. if (msr & UART_MSR_CTS)
  809. break;
  810. udelay(1);
  811. }
  812. }
  813. }
  814. #ifdef CONFIG_CONSOLE_POLL
  815. static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
  816. {
  817. struct uart_omap_port *up = (struct uart_omap_port *)port;
  818. pm_runtime_get_sync(&up->pdev->dev);
  819. wait_for_xmitr(up);
  820. serial_out(up, UART_TX, ch);
  821. pm_runtime_put(&up->pdev->dev);
  822. }
  823. static int serial_omap_poll_get_char(struct uart_port *port)
  824. {
  825. struct uart_omap_port *up = (struct uart_omap_port *)port;
  826. unsigned int status;
  827. pm_runtime_get_sync(&up->pdev->dev);
  828. status = serial_in(up, UART_LSR);
  829. if (!(status & UART_LSR_DR))
  830. return NO_POLL_CHAR;
  831. status = serial_in(up, UART_RX);
  832. pm_runtime_put(&up->pdev->dev);
  833. return status;
  834. }
  835. #endif /* CONFIG_CONSOLE_POLL */
  836. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  837. static struct uart_omap_port *serial_omap_console_ports[4];
  838. static struct uart_driver serial_omap_reg;
  839. static void serial_omap_console_putchar(struct uart_port *port, int ch)
  840. {
  841. struct uart_omap_port *up = (struct uart_omap_port *)port;
  842. wait_for_xmitr(up);
  843. serial_out(up, UART_TX, ch);
  844. }
  845. static void
  846. serial_omap_console_write(struct console *co, const char *s,
  847. unsigned int count)
  848. {
  849. struct uart_omap_port *up = serial_omap_console_ports[co->index];
  850. unsigned long flags;
  851. unsigned int ier;
  852. int locked = 1;
  853. pm_runtime_get_sync(&up->pdev->dev);
  854. local_irq_save(flags);
  855. if (up->port.sysrq)
  856. locked = 0;
  857. else if (oops_in_progress)
  858. locked = spin_trylock(&up->port.lock);
  859. else
  860. spin_lock(&up->port.lock);
  861. /*
  862. * First save the IER then disable the interrupts
  863. */
  864. ier = serial_in(up, UART_IER);
  865. serial_out(up, UART_IER, 0);
  866. uart_console_write(&up->port, s, count, serial_omap_console_putchar);
  867. /*
  868. * Finally, wait for transmitter to become empty
  869. * and restore the IER
  870. */
  871. wait_for_xmitr(up);
  872. serial_out(up, UART_IER, ier);
  873. /*
  874. * The receive handling will happen properly because the
  875. * receive ready bit will still be set; it is not cleared
  876. * on read. However, modem control will not, we must
  877. * call it if we have saved something in the saved flags
  878. * while processing with interrupts off.
  879. */
  880. if (up->msr_saved_flags)
  881. check_modem_status(up);
  882. pm_runtime_mark_last_busy(&up->pdev->dev);
  883. pm_runtime_put_autosuspend(&up->pdev->dev);
  884. if (locked)
  885. spin_unlock(&up->port.lock);
  886. local_irq_restore(flags);
  887. }
  888. static int __init
  889. serial_omap_console_setup(struct console *co, char *options)
  890. {
  891. struct uart_omap_port *up;
  892. int baud = 115200;
  893. int bits = 8;
  894. int parity = 'n';
  895. int flow = 'n';
  896. if (serial_omap_console_ports[co->index] == NULL)
  897. return -ENODEV;
  898. up = serial_omap_console_ports[co->index];
  899. if (options)
  900. uart_parse_options(options, &baud, &parity, &bits, &flow);
  901. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  902. }
  903. static struct console serial_omap_console = {
  904. .name = OMAP_SERIAL_NAME,
  905. .write = serial_omap_console_write,
  906. .device = uart_console_device,
  907. .setup = serial_omap_console_setup,
  908. .flags = CON_PRINTBUFFER,
  909. .index = -1,
  910. .data = &serial_omap_reg,
  911. };
  912. static void serial_omap_add_console_port(struct uart_omap_port *up)
  913. {
  914. serial_omap_console_ports[up->pdev->id] = up;
  915. }
  916. #define OMAP_CONSOLE (&serial_omap_console)
  917. #else
  918. #define OMAP_CONSOLE NULL
  919. static inline void serial_omap_add_console_port(struct uart_omap_port *up)
  920. {}
  921. #endif
  922. static struct uart_ops serial_omap_pops = {
  923. .tx_empty = serial_omap_tx_empty,
  924. .set_mctrl = serial_omap_set_mctrl,
  925. .get_mctrl = serial_omap_get_mctrl,
  926. .stop_tx = serial_omap_stop_tx,
  927. .start_tx = serial_omap_start_tx,
  928. .stop_rx = serial_omap_stop_rx,
  929. .enable_ms = serial_omap_enable_ms,
  930. .break_ctl = serial_omap_break_ctl,
  931. .startup = serial_omap_startup,
  932. .shutdown = serial_omap_shutdown,
  933. .set_termios = serial_omap_set_termios,
  934. .pm = serial_omap_pm,
  935. .type = serial_omap_type,
  936. .release_port = serial_omap_release_port,
  937. .request_port = serial_omap_request_port,
  938. .config_port = serial_omap_config_port,
  939. .verify_port = serial_omap_verify_port,
  940. #ifdef CONFIG_CONSOLE_POLL
  941. .poll_put_char = serial_omap_poll_put_char,
  942. .poll_get_char = serial_omap_poll_get_char,
  943. #endif
  944. };
  945. static struct uart_driver serial_omap_reg = {
  946. .owner = THIS_MODULE,
  947. .driver_name = "OMAP-SERIAL",
  948. .dev_name = OMAP_SERIAL_NAME,
  949. .nr = OMAP_MAX_HSUART_PORTS,
  950. .cons = OMAP_CONSOLE,
  951. };
  952. #ifdef CONFIG_SUSPEND
  953. static int serial_omap_suspend(struct device *dev)
  954. {
  955. struct uart_omap_port *up = dev_get_drvdata(dev);
  956. if (up)
  957. uart_suspend_port(&serial_omap_reg, &up->port);
  958. return 0;
  959. }
  960. static int serial_omap_resume(struct device *dev)
  961. {
  962. struct uart_omap_port *up = dev_get_drvdata(dev);
  963. if (up)
  964. uart_resume_port(&serial_omap_reg, &up->port);
  965. return 0;
  966. }
  967. #endif
  968. static void serial_omap_rx_timeout(unsigned long uart_no)
  969. {
  970. struct uart_omap_port *up = ui[uart_no];
  971. unsigned int curr_dma_pos, curr_transmitted_size;
  972. int ret = 0;
  973. curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
  974. if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
  975. (curr_dma_pos == 0)) {
  976. if (jiffies_to_msecs(jiffies - up->port_activity) <
  977. RX_TIMEOUT) {
  978. mod_timer(&up->uart_dma.rx_timer, jiffies +
  979. usecs_to_jiffies(up->uart_dma.rx_timeout));
  980. } else {
  981. serial_omap_stop_rxdma(up);
  982. up->ier |= (UART_IER_RDI | UART_IER_RLSI);
  983. serial_out(up, UART_IER, up->ier);
  984. }
  985. return;
  986. }
  987. curr_transmitted_size = curr_dma_pos -
  988. up->uart_dma.prev_rx_dma_pos;
  989. up->port.icount.rx += curr_transmitted_size;
  990. tty_insert_flip_string(up->port.state->port.tty,
  991. up->uart_dma.rx_buf +
  992. (up->uart_dma.prev_rx_dma_pos -
  993. up->uart_dma.rx_buf_dma_phys),
  994. curr_transmitted_size);
  995. tty_flip_buffer_push(up->port.state->port.tty);
  996. up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
  997. if (up->uart_dma.rx_buf_size +
  998. up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
  999. ret = serial_omap_start_rxdma(up);
  1000. if (ret < 0) {
  1001. serial_omap_stop_rxdma(up);
  1002. up->ier |= (UART_IER_RDI | UART_IER_RLSI);
  1003. serial_out(up, UART_IER, up->ier);
  1004. }
  1005. } else {
  1006. mod_timer(&up->uart_dma.rx_timer, jiffies +
  1007. usecs_to_jiffies(up->uart_dma.rx_timeout));
  1008. }
  1009. up->port_activity = jiffies;
  1010. }
  1011. static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
  1012. {
  1013. return;
  1014. }
  1015. static int serial_omap_start_rxdma(struct uart_omap_port *up)
  1016. {
  1017. int ret = 0;
  1018. if (up->uart_dma.rx_dma_channel == -1) {
  1019. pm_runtime_get_sync(&up->pdev->dev);
  1020. ret = omap_request_dma(up->uart_dma.uart_dma_rx,
  1021. "UART Rx DMA",
  1022. (void *)uart_rx_dma_callback, up,
  1023. &(up->uart_dma.rx_dma_channel));
  1024. if (ret < 0)
  1025. return ret;
  1026. omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
  1027. OMAP_DMA_AMODE_CONSTANT,
  1028. up->uart_dma.uart_base, 0, 0);
  1029. omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
  1030. OMAP_DMA_AMODE_POST_INC,
  1031. up->uart_dma.rx_buf_dma_phys, 0, 0);
  1032. omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
  1033. OMAP_DMA_DATA_TYPE_S8,
  1034. up->uart_dma.rx_buf_size, 1,
  1035. OMAP_DMA_SYNC_ELEMENT,
  1036. up->uart_dma.uart_dma_rx, 0);
  1037. }
  1038. up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
  1039. /* FIXME: Cache maintenance needed here? */
  1040. omap_start_dma(up->uart_dma.rx_dma_channel);
  1041. mod_timer(&up->uart_dma.rx_timer, jiffies +
  1042. usecs_to_jiffies(up->uart_dma.rx_timeout));
  1043. up->uart_dma.rx_dma_used = true;
  1044. return ret;
  1045. }
  1046. static void serial_omap_continue_tx(struct uart_omap_port *up)
  1047. {
  1048. struct circ_buf *xmit = &up->port.state->xmit;
  1049. unsigned int start = up->uart_dma.tx_buf_dma_phys
  1050. + (xmit->tail & (UART_XMIT_SIZE - 1));
  1051. if (uart_circ_empty(xmit))
  1052. return;
  1053. up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
  1054. /*
  1055. * It is a circular buffer. See if the buffer has wounded back.
  1056. * If yes it will have to be transferred in two separate dma
  1057. * transfers
  1058. */
  1059. if (start + up->uart_dma.tx_buf_size >=
  1060. up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
  1061. up->uart_dma.tx_buf_size =
  1062. (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
  1063. omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
  1064. OMAP_DMA_AMODE_CONSTANT,
  1065. up->uart_dma.uart_base, 0, 0);
  1066. omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
  1067. OMAP_DMA_AMODE_POST_INC, start, 0, 0);
  1068. omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
  1069. OMAP_DMA_DATA_TYPE_S8,
  1070. up->uart_dma.tx_buf_size, 1,
  1071. OMAP_DMA_SYNC_ELEMENT,
  1072. up->uart_dma.uart_dma_tx, 0);
  1073. /* FIXME: Cache maintenance needed here? */
  1074. omap_start_dma(up->uart_dma.tx_dma_channel);
  1075. }
  1076. static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
  1077. {
  1078. struct uart_omap_port *up = (struct uart_omap_port *)data;
  1079. struct circ_buf *xmit = &up->port.state->xmit;
  1080. xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
  1081. (UART_XMIT_SIZE - 1);
  1082. up->port.icount.tx += up->uart_dma.tx_buf_size;
  1083. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1084. uart_write_wakeup(&up->port);
  1085. if (uart_circ_empty(xmit)) {
  1086. spin_lock(&(up->uart_dma.tx_lock));
  1087. serial_omap_stop_tx(&up->port);
  1088. up->uart_dma.tx_dma_used = false;
  1089. spin_unlock(&(up->uart_dma.tx_lock));
  1090. } else {
  1091. omap_stop_dma(up->uart_dma.tx_dma_channel);
  1092. serial_omap_continue_tx(up);
  1093. }
  1094. up->port_activity = jiffies;
  1095. return;
  1096. }
  1097. static int serial_omap_probe(struct platform_device *pdev)
  1098. {
  1099. struct uart_omap_port *up;
  1100. struct resource *mem, *irq, *dma_tx, *dma_rx;
  1101. struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
  1102. int ret = -ENOSPC;
  1103. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1104. if (!mem) {
  1105. dev_err(&pdev->dev, "no mem resource?\n");
  1106. return -ENODEV;
  1107. }
  1108. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1109. if (!irq) {
  1110. dev_err(&pdev->dev, "no irq resource?\n");
  1111. return -ENODEV;
  1112. }
  1113. if (!request_mem_region(mem->start, resource_size(mem),
  1114. pdev->dev.driver->name)) {
  1115. dev_err(&pdev->dev, "memory region already claimed\n");
  1116. return -EBUSY;
  1117. }
  1118. dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1119. if (!dma_rx) {
  1120. ret = -EINVAL;
  1121. goto err;
  1122. }
  1123. dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1124. if (!dma_tx) {
  1125. ret = -EINVAL;
  1126. goto err;
  1127. }
  1128. up = kzalloc(sizeof(*up), GFP_KERNEL);
  1129. if (up == NULL) {
  1130. ret = -ENOMEM;
  1131. goto do_release_region;
  1132. }
  1133. sprintf(up->name, "OMAP UART%d", pdev->id);
  1134. up->pdev = pdev;
  1135. up->port.dev = &pdev->dev;
  1136. up->port.type = PORT_OMAP;
  1137. up->port.iotype = UPIO_MEM;
  1138. up->port.irq = irq->start;
  1139. up->port.regshift = 2;
  1140. up->port.fifosize = 64;
  1141. up->port.ops = &serial_omap_pops;
  1142. up->port.line = pdev->id;
  1143. up->port.mapbase = mem->start;
  1144. up->port.membase = ioremap(mem->start, resource_size(mem));
  1145. if (!up->port.membase) {
  1146. dev_err(&pdev->dev, "can't ioremap UART\n");
  1147. ret = -ENOMEM;
  1148. goto err;
  1149. }
  1150. up->port.flags = omap_up_info->flags;
  1151. up->port.uartclk = omap_up_info->uartclk;
  1152. up->uart_dma.uart_base = mem->start;
  1153. if (omap_up_info->dma_enabled) {
  1154. up->uart_dma.uart_dma_tx = dma_tx->start;
  1155. up->uart_dma.uart_dma_rx = dma_rx->start;
  1156. up->use_dma = 1;
  1157. up->uart_dma.rx_buf_size = 4096;
  1158. up->uart_dma.rx_timeout = 2;
  1159. spin_lock_init(&(up->uart_dma.tx_lock));
  1160. spin_lock_init(&(up->uart_dma.rx_lock));
  1161. up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
  1162. up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
  1163. }
  1164. pm_runtime_use_autosuspend(&pdev->dev);
  1165. pm_runtime_set_autosuspend_delay(&pdev->dev,
  1166. OMAP_UART_AUTOSUSPEND_DELAY);
  1167. pm_runtime_irq_safe(&pdev->dev);
  1168. pm_runtime_enable(&pdev->dev);
  1169. pm_runtime_get_sync(&pdev->dev);
  1170. ui[pdev->id] = up;
  1171. serial_omap_add_console_port(up);
  1172. ret = uart_add_one_port(&serial_omap_reg, &up->port);
  1173. if (ret != 0)
  1174. goto do_release_region;
  1175. pm_runtime_put(&pdev->dev);
  1176. platform_set_drvdata(pdev, up);
  1177. return 0;
  1178. err:
  1179. dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
  1180. pdev->id, __func__, ret);
  1181. do_release_region:
  1182. release_mem_region(mem->start, resource_size(mem));
  1183. return ret;
  1184. }
  1185. static int serial_omap_remove(struct platform_device *dev)
  1186. {
  1187. struct uart_omap_port *up = platform_get_drvdata(dev);
  1188. if (up) {
  1189. pm_runtime_disable(&up->pdev->dev);
  1190. uart_remove_one_port(&serial_omap_reg, &up->port);
  1191. kfree(up);
  1192. }
  1193. platform_set_drvdata(dev, NULL);
  1194. return 0;
  1195. }
  1196. #ifdef CONFIG_PM_RUNTIME
  1197. static int serial_omap_runtime_suspend(struct device *dev)
  1198. {
  1199. return 0;
  1200. }
  1201. static int serial_omap_runtime_resume(struct device *dev)
  1202. {
  1203. return 0;
  1204. }
  1205. #endif
  1206. static const struct dev_pm_ops serial_omap_dev_pm_ops = {
  1207. SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
  1208. SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
  1209. serial_omap_runtime_resume, NULL)
  1210. };
  1211. static struct platform_driver serial_omap_driver = {
  1212. .probe = serial_omap_probe,
  1213. .remove = serial_omap_remove,
  1214. .driver = {
  1215. .name = DRIVER_NAME,
  1216. .pm = &serial_omap_dev_pm_ops,
  1217. },
  1218. };
  1219. static int __init serial_omap_init(void)
  1220. {
  1221. int ret;
  1222. ret = uart_register_driver(&serial_omap_reg);
  1223. if (ret != 0)
  1224. return ret;
  1225. ret = platform_driver_register(&serial_omap_driver);
  1226. if (ret != 0)
  1227. uart_unregister_driver(&serial_omap_reg);
  1228. return ret;
  1229. }
  1230. static void __exit serial_omap_exit(void)
  1231. {
  1232. platform_driver_unregister(&serial_omap_driver);
  1233. uart_unregister_driver(&serial_omap_reg);
  1234. }
  1235. module_init(serial_omap_init);
  1236. module_exit(serial_omap_exit);
  1237. MODULE_DESCRIPTION("OMAP High Speed UART driver");
  1238. MODULE_LICENSE("GPL");
  1239. MODULE_AUTHOR("Texas Instruments Inc");