omap-serial.c 45 KB

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