omap-serial.c 40 KB

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