omap-serial.c 43 KB

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