omap-serial.c 42 KB

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