omap-serial.c 45 KB

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