omap-serial.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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 <[email protected]>
  9. * Thara Gopinath <[email protected]>
  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/platform_device.h>
  34. #include <linux/io.h>
  35. #include <linux/clk.h>
  36. #include <linux/serial_core.h>
  37. #include <linux/irq.h>
  38. #include <linux/pm_runtime.h>
  39. #include <linux/pm_wakeirq.h>
  40. #include <linux/of.h>
  41. #include <linux/of_irq.h>
  42. #include <linux/gpio.h>
  43. #include <linux/of_gpio.h>
  44. #include <linux/platform_data/serial-omap.h>
  45. #include <dt-bindings/gpio/gpio.h>
  46. #define OMAP_MAX_HSUART_PORTS 10
  47. #define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
  48. #define OMAP_UART_REV_42 0x0402
  49. #define OMAP_UART_REV_46 0x0406
  50. #define OMAP_UART_REV_52 0x0502
  51. #define OMAP_UART_REV_63 0x0603
  52. #define OMAP_UART_TX_WAKEUP_EN BIT(7)
  53. /* Feature flags */
  54. #define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
  55. #define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
  56. #define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
  57. #define DEFAULT_CLK_SPEED 48000000 /* 48Mhz */
  58. /* SCR register bitmasks */
  59. #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
  60. #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
  61. #define OMAP_UART_SCR_TX_EMPTY (1 << 3)
  62. /* FCR register bitmasks */
  63. #define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
  64. #define OMAP_UART_FCR_TX_FIFO_TRIG_MASK (0x3 << 4)
  65. /* MVR register bitmasks */
  66. #define OMAP_UART_MVR_SCHEME_SHIFT 30
  67. #define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
  68. #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
  69. #define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
  70. #define OMAP_UART_MVR_MAJ_MASK 0x700
  71. #define OMAP_UART_MVR_MAJ_SHIFT 8
  72. #define OMAP_UART_MVR_MIN_MASK 0x3f
  73. #define OMAP_UART_DMA_CH_FREE -1
  74. #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
  75. #define OMAP_MODE13X_SPEED 230400
  76. /* WER = 0x7F
  77. * Enable module level wakeup in WER reg
  78. */
  79. #define OMAP_UART_WER_MOD_WKUP 0x7F
  80. /* Enable XON/XOFF flow control on output */
  81. #define OMAP_UART_SW_TX 0x08
  82. /* Enable XON/XOFF flow control on input */
  83. #define OMAP_UART_SW_RX 0x02
  84. #define OMAP_UART_SW_CLR 0xF0
  85. #define OMAP_UART_TCR_TRIG 0x0F
  86. struct uart_omap_dma {
  87. u8 uart_dma_tx;
  88. u8 uart_dma_rx;
  89. int rx_dma_channel;
  90. int tx_dma_channel;
  91. dma_addr_t rx_buf_dma_phys;
  92. dma_addr_t tx_buf_dma_phys;
  93. unsigned int uart_base;
  94. /*
  95. * Buffer for rx dma. It is not required for tx because the buffer
  96. * comes from port structure.
  97. */
  98. unsigned char *rx_buf;
  99. unsigned int prev_rx_dma_pos;
  100. int tx_buf_size;
  101. int tx_dma_used;
  102. int rx_dma_used;
  103. spinlock_t tx_lock;
  104. spinlock_t rx_lock;
  105. /* timer to poll activity on rx dma */
  106. struct timer_list rx_timer;
  107. unsigned int rx_buf_size;
  108. unsigned int rx_poll_rate;
  109. unsigned int rx_timeout;
  110. };
  111. struct uart_omap_port {
  112. struct uart_port port;
  113. struct uart_omap_dma uart_dma;
  114. struct device *dev;
  115. int wakeirq;
  116. unsigned char ier;
  117. unsigned char lcr;
  118. unsigned char mcr;
  119. unsigned char fcr;
  120. unsigned char efr;
  121. unsigned char dll;
  122. unsigned char dlh;
  123. unsigned char mdr1;
  124. unsigned char scr;
  125. unsigned char wer;
  126. int use_dma;
  127. /*
  128. * Some bits in registers are cleared on a read, so they must
  129. * be saved whenever the register is read, but the bits will not
  130. * be immediately processed.
  131. */
  132. unsigned int lsr_break_flag;
  133. unsigned char msr_saved_flags;
  134. char name[20];
  135. unsigned long port_activity;
  136. int context_loss_cnt;
  137. u32 errata;
  138. u32 features;
  139. int rts_gpio;
  140. struct pm_qos_request pm_qos_request;
  141. u32 latency;
  142. u32 calc_latency;
  143. struct work_struct qos_work;
  144. bool is_suspending;
  145. };
  146. #define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port)))
  147. static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
  148. /* Forward declaration of functions */
  149. static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
  150. static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
  151. {
  152. offset <<= up->port.regshift;
  153. return readw(up->port.membase + offset);
  154. }
  155. static inline void serial_out(struct uart_omap_port *up, int offset, int value)
  156. {
  157. offset <<= up->port.regshift;
  158. writew(value, up->port.membase + offset);
  159. }
  160. static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
  161. {
  162. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  163. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  164. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  165. serial_out(up, UART_FCR, 0);
  166. }
  167. #ifdef CONFIG_PM
  168. static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
  169. {
  170. struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
  171. if (!pdata || !pdata->get_context_loss_count)
  172. return -EINVAL;
  173. return pdata->get_context_loss_count(up->dev);
  174. }
  175. /* REVISIT: Remove this when omap3 boots in device tree only mode */
  176. static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
  177. {
  178. struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
  179. if (!pdata || !pdata->enable_wakeup)
  180. return;
  181. pdata->enable_wakeup(up->dev, enable);
  182. }
  183. #endif /* CONFIG_PM */
  184. /*
  185. * Calculate the absolute difference between the desired and actual baud
  186. * rate for the given mode.
  187. */
  188. static inline int calculate_baud_abs_diff(struct uart_port *port,
  189. unsigned int baud, unsigned int mode)
  190. {
  191. unsigned int n = port->uartclk / (mode * baud);
  192. int abs_diff;
  193. if (n == 0)
  194. n = 1;
  195. abs_diff = baud - (port->uartclk / (mode * n));
  196. if (abs_diff < 0)
  197. abs_diff = -abs_diff;
  198. return abs_diff;
  199. }
  200. /*
  201. * serial_omap_baud_is_mode16 - check if baud rate is MODE16X
  202. * @port: uart port info
  203. * @baud: baudrate for which mode needs to be determined
  204. *
  205. * Returns true if baud rate is MODE16X and false if MODE13X
  206. * Original table in OMAP TRM named "UART Mode Baud Rates, Divisor Values,
  207. * and Error Rates" determines modes not for all common baud rates.
  208. * E.g. for 1000000 baud rate mode must be 16x, but according to that
  209. * table it's determined as 13x.
  210. */
  211. static bool
  212. serial_omap_baud_is_mode16(struct uart_port *port, unsigned int baud)
  213. {
  214. int abs_diff_13 = calculate_baud_abs_diff(port, baud, 13);
  215. int abs_diff_16 = calculate_baud_abs_diff(port, baud, 16);
  216. return (abs_diff_13 >= abs_diff_16);
  217. }
  218. /*
  219. * serial_omap_get_divisor - calculate divisor value
  220. * @port: uart port info
  221. * @baud: baudrate for which divisor needs to be calculated.
  222. */
  223. static unsigned int
  224. serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
  225. {
  226. unsigned int mode;
  227. if (!serial_omap_baud_is_mode16(port, baud))
  228. mode = 13;
  229. else
  230. mode = 16;
  231. return port->uartclk/(mode * baud);
  232. }
  233. static void serial_omap_enable_ms(struct uart_port *port)
  234. {
  235. struct uart_omap_port *up = to_uart_omap_port(port);
  236. dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
  237. pm_runtime_get_sync(up->dev);
  238. up->ier |= UART_IER_MSI;
  239. serial_out(up, UART_IER, up->ier);
  240. pm_runtime_mark_last_busy(up->dev);
  241. pm_runtime_put_autosuspend(up->dev);
  242. }
  243. static void serial_omap_stop_tx(struct uart_port *port)
  244. {
  245. struct uart_omap_port *up = to_uart_omap_port(port);
  246. int res;
  247. pm_runtime_get_sync(up->dev);
  248. /* Handle RS-485 */
  249. if (port->rs485.flags & SER_RS485_ENABLED) {
  250. if (up->scr & OMAP_UART_SCR_TX_EMPTY) {
  251. /* THR interrupt is fired when both TX FIFO and TX
  252. * shift register are empty. This means there's nothing
  253. * left to transmit now, so make sure the THR interrupt
  254. * is fired when TX FIFO is below the trigger level,
  255. * disable THR interrupts and toggle the RS-485 GPIO
  256. * data direction pin if needed.
  257. */
  258. up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
  259. serial_out(up, UART_OMAP_SCR, up->scr);
  260. res = (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) ?
  261. 1 : 0;
  262. if (gpio_get_value(up->rts_gpio) != res) {
  263. if (port->rs485.delay_rts_after_send > 0)
  264. mdelay(
  265. port->rs485.delay_rts_after_send);
  266. gpio_set_value(up->rts_gpio, res);
  267. }
  268. } else {
  269. /* We're asked to stop, but there's still stuff in the
  270. * UART FIFO, so make sure the THR interrupt is fired
  271. * when both TX FIFO and TX shift register are empty.
  272. * The next THR interrupt (if no transmission is started
  273. * in the meantime) will indicate the end of a
  274. * transmission. Therefore we _don't_ disable THR
  275. * interrupts in this situation.
  276. */
  277. up->scr |= OMAP_UART_SCR_TX_EMPTY;
  278. serial_out(up, UART_OMAP_SCR, up->scr);
  279. return;
  280. }
  281. }
  282. if (up->ier & UART_IER_THRI) {
  283. up->ier &= ~UART_IER_THRI;
  284. serial_out(up, UART_IER, up->ier);
  285. }
  286. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  287. !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
  288. /*
  289. * Empty the RX FIFO, we are not interested in anything
  290. * received during the half-duplex transmission.
  291. */
  292. serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_RCVR);
  293. /* Re-enable RX interrupts */
  294. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  295. up->port.read_status_mask |= UART_LSR_DR;
  296. serial_out(up, UART_IER, up->ier);
  297. }
  298. pm_runtime_mark_last_busy(up->dev);
  299. pm_runtime_put_autosuspend(up->dev);
  300. }
  301. static void serial_omap_stop_rx(struct uart_port *port)
  302. {
  303. struct uart_omap_port *up = to_uart_omap_port(port);
  304. pm_runtime_get_sync(up->dev);
  305. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  306. up->port.read_status_mask &= ~UART_LSR_DR;
  307. serial_out(up, UART_IER, up->ier);
  308. pm_runtime_mark_last_busy(up->dev);
  309. pm_runtime_put_autosuspend(up->dev);
  310. }
  311. static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
  312. {
  313. struct circ_buf *xmit = &up->port.state->xmit;
  314. int count;
  315. if (up->port.x_char) {
  316. serial_out(up, UART_TX, up->port.x_char);
  317. up->port.icount.tx++;
  318. up->port.x_char = 0;
  319. return;
  320. }
  321. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  322. serial_omap_stop_tx(&up->port);
  323. return;
  324. }
  325. count = up->port.fifosize / 4;
  326. do {
  327. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  328. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  329. up->port.icount.tx++;
  330. if (uart_circ_empty(xmit))
  331. break;
  332. } while (--count > 0);
  333. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  334. uart_write_wakeup(&up->port);
  335. if (uart_circ_empty(xmit))
  336. serial_omap_stop_tx(&up->port);
  337. }
  338. static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
  339. {
  340. if (!(up->ier & UART_IER_THRI)) {
  341. up->ier |= UART_IER_THRI;
  342. serial_out(up, UART_IER, up->ier);
  343. }
  344. }
  345. static void serial_omap_start_tx(struct uart_port *port)
  346. {
  347. struct uart_omap_port *up = to_uart_omap_port(port);
  348. int res;
  349. pm_runtime_get_sync(up->dev);
  350. /* Handle RS-485 */
  351. if (port->rs485.flags & SER_RS485_ENABLED) {
  352. /* Fire THR interrupts when FIFO is below trigger level */
  353. up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
  354. serial_out(up, UART_OMAP_SCR, up->scr);
  355. /* if rts not already enabled */
  356. res = (port->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
  357. if (gpio_get_value(up->rts_gpio) != res) {
  358. gpio_set_value(up->rts_gpio, res);
  359. if (port->rs485.delay_rts_before_send > 0)
  360. mdelay(port->rs485.delay_rts_before_send);
  361. }
  362. }
  363. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  364. !(port->rs485.flags & SER_RS485_RX_DURING_TX))
  365. serial_omap_stop_rx(port);
  366. serial_omap_enable_ier_thri(up);
  367. pm_runtime_mark_last_busy(up->dev);
  368. pm_runtime_put_autosuspend(up->dev);
  369. }
  370. static void serial_omap_throttle(struct uart_port *port)
  371. {
  372. struct uart_omap_port *up = to_uart_omap_port(port);
  373. unsigned long flags;
  374. pm_runtime_get_sync(up->dev);
  375. spin_lock_irqsave(&up->port.lock, flags);
  376. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  377. serial_out(up, UART_IER, up->ier);
  378. spin_unlock_irqrestore(&up->port.lock, flags);
  379. pm_runtime_mark_last_busy(up->dev);
  380. pm_runtime_put_autosuspend(up->dev);
  381. }
  382. static void serial_omap_unthrottle(struct uart_port *port)
  383. {
  384. struct uart_omap_port *up = to_uart_omap_port(port);
  385. unsigned long flags;
  386. pm_runtime_get_sync(up->dev);
  387. spin_lock_irqsave(&up->port.lock, flags);
  388. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  389. serial_out(up, UART_IER, up->ier);
  390. spin_unlock_irqrestore(&up->port.lock, flags);
  391. pm_runtime_mark_last_busy(up->dev);
  392. pm_runtime_put_autosuspend(up->dev);
  393. }
  394. static unsigned int check_modem_status(struct uart_omap_port *up)
  395. {
  396. unsigned int status;
  397. status = serial_in(up, UART_MSR);
  398. status |= up->msr_saved_flags;
  399. up->msr_saved_flags = 0;
  400. if ((status & UART_MSR_ANY_DELTA) == 0)
  401. return status;
  402. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  403. up->port.state != NULL) {
  404. if (status & UART_MSR_TERI)
  405. up->port.icount.rng++;
  406. if (status & UART_MSR_DDSR)
  407. up->port.icount.dsr++;
  408. if (status & UART_MSR_DDCD)
  409. uart_handle_dcd_change
  410. (&up->port, status & UART_MSR_DCD);
  411. if (status & UART_MSR_DCTS)
  412. uart_handle_cts_change
  413. (&up->port, status & UART_MSR_CTS);
  414. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  415. }
  416. return status;
  417. }
  418. static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
  419. {
  420. unsigned int flag;
  421. unsigned char ch = 0;
  422. if (likely(lsr & UART_LSR_DR))
  423. ch = serial_in(up, UART_RX);
  424. up->port.icount.rx++;
  425. flag = TTY_NORMAL;
  426. if (lsr & UART_LSR_BI) {
  427. flag = TTY_BREAK;
  428. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  429. up->port.icount.brk++;
  430. /*
  431. * We do the SysRQ and SAK checking
  432. * here because otherwise the break
  433. * may get masked by ignore_status_mask
  434. * or read_status_mask.
  435. */
  436. if (uart_handle_break(&up->port))
  437. return;
  438. }
  439. if (lsr & UART_LSR_PE) {
  440. flag = TTY_PARITY;
  441. up->port.icount.parity++;
  442. }
  443. if (lsr & UART_LSR_FE) {
  444. flag = TTY_FRAME;
  445. up->port.icount.frame++;
  446. }
  447. if (lsr & UART_LSR_OE)
  448. up->port.icount.overrun++;
  449. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  450. if (up->port.line == up->port.cons->index) {
  451. /* Recover the break flag from console xmit */
  452. lsr |= up->lsr_break_flag;
  453. }
  454. #endif
  455. uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
  456. }
  457. static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
  458. {
  459. unsigned char ch = 0;
  460. unsigned int flag;
  461. if (!(lsr & UART_LSR_DR))
  462. return;
  463. ch = serial_in(up, UART_RX);
  464. flag = TTY_NORMAL;
  465. up->port.icount.rx++;
  466. if (uart_handle_sysrq_char(&up->port, ch))
  467. return;
  468. uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
  469. }
  470. /**
  471. * serial_omap_irq() - This handles the interrupt from one port
  472. * @irq: uart port irq number
  473. * @dev_id: uart port info
  474. */
  475. static irqreturn_t serial_omap_irq(int irq, void *dev_id)
  476. {
  477. struct uart_omap_port *up = dev_id;
  478. unsigned int iir, lsr;
  479. unsigned int type;
  480. irqreturn_t ret = IRQ_NONE;
  481. int max_count = 256;
  482. spin_lock(&up->port.lock);
  483. pm_runtime_get_sync(up->dev);
  484. do {
  485. iir = serial_in(up, UART_IIR);
  486. if (iir & UART_IIR_NO_INT)
  487. break;
  488. ret = IRQ_HANDLED;
  489. lsr = serial_in(up, UART_LSR);
  490. /* extract IRQ type from IIR register */
  491. type = iir & 0x3e;
  492. switch (type) {
  493. case UART_IIR_MSI:
  494. check_modem_status(up);
  495. break;
  496. case UART_IIR_THRI:
  497. transmit_chars(up, lsr);
  498. break;
  499. case UART_IIR_RX_TIMEOUT:
  500. /* FALLTHROUGH */
  501. case UART_IIR_RDI:
  502. serial_omap_rdi(up, lsr);
  503. break;
  504. case UART_IIR_RLSI:
  505. serial_omap_rlsi(up, lsr);
  506. break;
  507. case UART_IIR_CTS_RTS_DSR:
  508. /* simply try again */
  509. break;
  510. case UART_IIR_XOFF:
  511. /* FALLTHROUGH */
  512. default:
  513. break;
  514. }
  515. } while (!(iir & UART_IIR_NO_INT) && max_count--);
  516. spin_unlock(&up->port.lock);
  517. tty_flip_buffer_push(&up->port.state->port);
  518. pm_runtime_mark_last_busy(up->dev);
  519. pm_runtime_put_autosuspend(up->dev);
  520. up->port_activity = jiffies;
  521. return ret;
  522. }
  523. static unsigned int serial_omap_tx_empty(struct uart_port *port)
  524. {
  525. struct uart_omap_port *up = to_uart_omap_port(port);
  526. unsigned long flags = 0;
  527. unsigned int ret = 0;
  528. pm_runtime_get_sync(up->dev);
  529. dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
  530. spin_lock_irqsave(&up->port.lock, flags);
  531. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  532. spin_unlock_irqrestore(&up->port.lock, flags);
  533. pm_runtime_mark_last_busy(up->dev);
  534. pm_runtime_put_autosuspend(up->dev);
  535. return ret;
  536. }
  537. static unsigned int serial_omap_get_mctrl(struct uart_port *port)
  538. {
  539. struct uart_omap_port *up = to_uart_omap_port(port);
  540. unsigned int status;
  541. unsigned int ret = 0;
  542. pm_runtime_get_sync(up->dev);
  543. status = check_modem_status(up);
  544. pm_runtime_mark_last_busy(up->dev);
  545. pm_runtime_put_autosuspend(up->dev);
  546. dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
  547. if (status & UART_MSR_DCD)
  548. ret |= TIOCM_CAR;
  549. if (status & UART_MSR_RI)
  550. ret |= TIOCM_RNG;
  551. if (status & UART_MSR_DSR)
  552. ret |= TIOCM_DSR;
  553. if (status & UART_MSR_CTS)
  554. ret |= TIOCM_CTS;
  555. return ret;
  556. }
  557. static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
  558. {
  559. struct uart_omap_port *up = to_uart_omap_port(port);
  560. unsigned char mcr = 0, old_mcr, lcr;
  561. dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
  562. if (mctrl & TIOCM_RTS)
  563. mcr |= UART_MCR_RTS;
  564. if (mctrl & TIOCM_DTR)
  565. mcr |= UART_MCR_DTR;
  566. if (mctrl & TIOCM_OUT1)
  567. mcr |= UART_MCR_OUT1;
  568. if (mctrl & TIOCM_OUT2)
  569. mcr |= UART_MCR_OUT2;
  570. if (mctrl & TIOCM_LOOP)
  571. mcr |= UART_MCR_LOOP;
  572. pm_runtime_get_sync(up->dev);
  573. old_mcr = serial_in(up, UART_MCR);
  574. old_mcr &= ~(UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_OUT1 |
  575. UART_MCR_DTR | UART_MCR_RTS);
  576. up->mcr = old_mcr | mcr;
  577. serial_out(up, UART_MCR, up->mcr);
  578. /* Turn off autoRTS if RTS is lowered; restore autoRTS if RTS raised */
  579. lcr = serial_in(up, UART_LCR);
  580. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  581. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  582. up->efr |= UART_EFR_RTS;
  583. else
  584. up->efr &= ~UART_EFR_RTS;
  585. serial_out(up, UART_EFR, up->efr);
  586. serial_out(up, UART_LCR, lcr);
  587. pm_runtime_mark_last_busy(up->dev);
  588. pm_runtime_put_autosuspend(up->dev);
  589. }
  590. static void serial_omap_break_ctl(struct uart_port *port, int break_state)
  591. {
  592. struct uart_omap_port *up = to_uart_omap_port(port);
  593. unsigned long flags = 0;
  594. dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
  595. pm_runtime_get_sync(up->dev);
  596. spin_lock_irqsave(&up->port.lock, flags);
  597. if (break_state == -1)
  598. up->lcr |= UART_LCR_SBC;
  599. else
  600. up->lcr &= ~UART_LCR_SBC;
  601. serial_out(up, UART_LCR, up->lcr);
  602. spin_unlock_irqrestore(&up->port.lock, flags);
  603. pm_runtime_mark_last_busy(up->dev);
  604. pm_runtime_put_autosuspend(up->dev);
  605. }
  606. static int serial_omap_startup(struct uart_port *port)
  607. {
  608. struct uart_omap_port *up = to_uart_omap_port(port);
  609. unsigned long flags = 0;
  610. int retval;
  611. /*
  612. * Allocate the IRQ
  613. */
  614. retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
  615. up->name, up);
  616. if (retval)
  617. return retval;
  618. /* Optional wake-up IRQ */
  619. if (up->wakeirq) {
  620. retval = dev_pm_set_dedicated_wake_irq(up->dev, up->wakeirq);
  621. if (retval) {
  622. free_irq(up->port.irq, up);
  623. return retval;
  624. }
  625. }
  626. dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
  627. pm_runtime_get_sync(up->dev);
  628. /*
  629. * Clear the FIFO buffers and disable them.
  630. * (they will be reenabled in set_termios())
  631. */
  632. serial_omap_clear_fifos(up);
  633. /*
  634. * Clear the interrupt registers.
  635. */
  636. (void) serial_in(up, UART_LSR);
  637. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  638. (void) serial_in(up, UART_RX);
  639. (void) serial_in(up, UART_IIR);
  640. (void) serial_in(up, UART_MSR);
  641. /*
  642. * Now, initialize the UART
  643. */
  644. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  645. spin_lock_irqsave(&up->port.lock, flags);
  646. /*
  647. * Most PC uarts need OUT2 raised to enable interrupts.
  648. */
  649. up->port.mctrl |= TIOCM_OUT2;
  650. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  651. spin_unlock_irqrestore(&up->port.lock, flags);
  652. up->msr_saved_flags = 0;
  653. /*
  654. * Finally, enable interrupts. Note: Modem status interrupts
  655. * are set via set_termios(), which will be occurring imminently
  656. * anyway, so we don't enable them here.
  657. */
  658. up->ier = UART_IER_RLSI | UART_IER_RDI;
  659. serial_out(up, UART_IER, up->ier);
  660. /* Enable module level wake up */
  661. up->wer = OMAP_UART_WER_MOD_WKUP;
  662. if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
  663. up->wer |= OMAP_UART_TX_WAKEUP_EN;
  664. serial_out(up, UART_OMAP_WER, up->wer);
  665. pm_runtime_mark_last_busy(up->dev);
  666. pm_runtime_put_autosuspend(up->dev);
  667. up->port_activity = jiffies;
  668. return 0;
  669. }
  670. static void serial_omap_shutdown(struct uart_port *port)
  671. {
  672. struct uart_omap_port *up = to_uart_omap_port(port);
  673. unsigned long flags = 0;
  674. dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
  675. pm_runtime_get_sync(up->dev);
  676. /*
  677. * Disable interrupts from this port
  678. */
  679. up->ier = 0;
  680. serial_out(up, UART_IER, 0);
  681. spin_lock_irqsave(&up->port.lock, flags);
  682. up->port.mctrl &= ~TIOCM_OUT2;
  683. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  684. spin_unlock_irqrestore(&up->port.lock, flags);
  685. /*
  686. * Disable break condition and FIFOs
  687. */
  688. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  689. serial_omap_clear_fifos(up);
  690. /*
  691. * Read data port to reset things, and then free the irq
  692. */
  693. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  694. (void) serial_in(up, UART_RX);
  695. pm_runtime_mark_last_busy(up->dev);
  696. pm_runtime_put_autosuspend(up->dev);
  697. free_irq(up->port.irq, up);
  698. dev_pm_clear_wake_irq(up->dev);
  699. }
  700. static void serial_omap_uart_qos_work(struct work_struct *work)
  701. {
  702. struct uart_omap_port *up = container_of(work, struct uart_omap_port,
  703. qos_work);
  704. pm_qos_update_request(&up->pm_qos_request, up->latency);
  705. }
  706. static void
  707. serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
  708. struct ktermios *old)
  709. {
  710. struct uart_omap_port *up = to_uart_omap_port(port);
  711. unsigned char cval = 0;
  712. unsigned long flags = 0;
  713. unsigned int baud, quot;
  714. switch (termios->c_cflag & CSIZE) {
  715. case CS5:
  716. cval = UART_LCR_WLEN5;
  717. break;
  718. case CS6:
  719. cval = UART_LCR_WLEN6;
  720. break;
  721. case CS7:
  722. cval = UART_LCR_WLEN7;
  723. break;
  724. default:
  725. case CS8:
  726. cval = UART_LCR_WLEN8;
  727. break;
  728. }
  729. if (termios->c_cflag & CSTOPB)
  730. cval |= UART_LCR_STOP;
  731. if (termios->c_cflag & PARENB)
  732. cval |= UART_LCR_PARITY;
  733. if (!(termios->c_cflag & PARODD))
  734. cval |= UART_LCR_EPAR;
  735. if (termios->c_cflag & CMSPAR)
  736. cval |= UART_LCR_SPAR;
  737. /*
  738. * Ask the core to calculate the divisor for us.
  739. */
  740. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
  741. quot = serial_omap_get_divisor(port, baud);
  742. /* calculate wakeup latency constraint */
  743. up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
  744. up->latency = up->calc_latency;
  745. schedule_work(&up->qos_work);
  746. up->dll = quot & 0xff;
  747. up->dlh = quot >> 8;
  748. up->mdr1 = UART_OMAP_MDR1_DISABLE;
  749. up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
  750. UART_FCR_ENABLE_FIFO;
  751. /*
  752. * Ok, we're now changing the port state. Do it with
  753. * interrupts disabled.
  754. */
  755. pm_runtime_get_sync(up->dev);
  756. spin_lock_irqsave(&up->port.lock, flags);
  757. /*
  758. * Update the per-port timeout.
  759. */
  760. uart_update_timeout(port, termios->c_cflag, baud);
  761. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  762. if (termios->c_iflag & INPCK)
  763. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  764. if (termios->c_iflag & (BRKINT | PARMRK))
  765. up->port.read_status_mask |= UART_LSR_BI;
  766. /*
  767. * Characters to ignore
  768. */
  769. up->port.ignore_status_mask = 0;
  770. if (termios->c_iflag & IGNPAR)
  771. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  772. if (termios->c_iflag & IGNBRK) {
  773. up->port.ignore_status_mask |= UART_LSR_BI;
  774. /*
  775. * If we're ignoring parity and break indicators,
  776. * ignore overruns too (for real raw support).
  777. */
  778. if (termios->c_iflag & IGNPAR)
  779. up->port.ignore_status_mask |= UART_LSR_OE;
  780. }
  781. /*
  782. * ignore all characters if CREAD is not set
  783. */
  784. if ((termios->c_cflag & CREAD) == 0)
  785. up->port.ignore_status_mask |= UART_LSR_DR;
  786. /*
  787. * Modem status interrupts
  788. */
  789. up->ier &= ~UART_IER_MSI;
  790. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  791. up->ier |= UART_IER_MSI;
  792. serial_out(up, UART_IER, up->ier);
  793. serial_out(up, UART_LCR, cval); /* reset DLAB */
  794. up->lcr = cval;
  795. up->scr = 0;
  796. /* FIFOs and DMA Settings */
  797. /* FCR can be changed only when the
  798. * baud clock is not running
  799. * DLL_REG and DLH_REG set to 0.
  800. */
  801. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  802. serial_out(up, UART_DLL, 0);
  803. serial_out(up, UART_DLM, 0);
  804. serial_out(up, UART_LCR, 0);
  805. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  806. up->efr = serial_in(up, UART_EFR) & ~UART_EFR_ECB;
  807. up->efr &= ~UART_EFR_SCD;
  808. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  809. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  810. up->mcr = serial_in(up, UART_MCR) & ~UART_MCR_TCRTLR;
  811. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  812. /* FIFO ENABLE, DMA MODE */
  813. up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
  814. /*
  815. * NOTE: Setting OMAP_UART_SCR_RX_TRIG_GRANU1_MASK
  816. * sets Enables the granularity of 1 for TRIGGER RX
  817. * level. Along with setting RX FIFO trigger level
  818. * to 1 (as noted below, 16 characters) and TLR[3:0]
  819. * to zero this will result RX FIFO threshold level
  820. * to 1 character, instead of 16 as noted in comment
  821. * below.
  822. */
  823. /* Set receive FIFO threshold to 16 characters and
  824. * transmit FIFO threshold to 32 spaces
  825. */
  826. up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
  827. up->fcr &= ~OMAP_UART_FCR_TX_FIFO_TRIG_MASK;
  828. up->fcr |= UART_FCR6_R_TRIGGER_16 | UART_FCR6_T_TRIGGER_24 |
  829. UART_FCR_ENABLE_FIFO;
  830. serial_out(up, UART_FCR, up->fcr);
  831. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  832. serial_out(up, UART_OMAP_SCR, up->scr);
  833. /* Reset UART_MCR_TCRTLR: this must be done with the EFR_ECB bit set */
  834. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  835. serial_out(up, UART_MCR, up->mcr);
  836. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  837. serial_out(up, UART_EFR, up->efr);
  838. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  839. /* Protocol, Baud Rate, and Interrupt Settings */
  840. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  841. serial_omap_mdr1_errataset(up, up->mdr1);
  842. else
  843. serial_out(up, UART_OMAP_MDR1, up->mdr1);
  844. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  845. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  846. serial_out(up, UART_LCR, 0);
  847. serial_out(up, UART_IER, 0);
  848. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  849. serial_out(up, UART_DLL, up->dll); /* LS of divisor */
  850. serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
  851. serial_out(up, UART_LCR, 0);
  852. serial_out(up, UART_IER, up->ier);
  853. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  854. serial_out(up, UART_EFR, up->efr);
  855. serial_out(up, UART_LCR, cval);
  856. if (!serial_omap_baud_is_mode16(port, baud))
  857. up->mdr1 = UART_OMAP_MDR1_13X_MODE;
  858. else
  859. up->mdr1 = UART_OMAP_MDR1_16X_MODE;
  860. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  861. serial_omap_mdr1_errataset(up, up->mdr1);
  862. else
  863. serial_out(up, UART_OMAP_MDR1, up->mdr1);
  864. /* Configure flow control */
  865. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  866. /* XON1/XOFF1 accessible mode B, TCRTLR=0, ECB=0 */
  867. serial_out(up, UART_XON1, termios->c_cc[VSTART]);
  868. serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
  869. /* Enable access to TCR/TLR */
  870. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  871. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  872. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  873. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
  874. up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
  875. if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
  876. /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
  877. up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  878. up->efr |= UART_EFR_CTS;
  879. } else {
  880. /* Disable AUTORTS and AUTOCTS */
  881. up->efr &= ~(UART_EFR_CTS | UART_EFR_RTS);
  882. }
  883. if (up->port.flags & UPF_SOFT_FLOW) {
  884. /* clear SW control mode bits */
  885. up->efr &= OMAP_UART_SW_CLR;
  886. /*
  887. * IXON Flag:
  888. * Enable XON/XOFF flow control on input.
  889. * Receiver compares XON1, XOFF1.
  890. */
  891. if (termios->c_iflag & IXON)
  892. up->efr |= OMAP_UART_SW_RX;
  893. /*
  894. * IXOFF Flag:
  895. * Enable XON/XOFF flow control on output.
  896. * Transmit XON1, XOFF1
  897. */
  898. if (termios->c_iflag & IXOFF) {
  899. up->port.status |= UPSTAT_AUTOXOFF;
  900. up->efr |= OMAP_UART_SW_TX;
  901. }
  902. /*
  903. * IXANY Flag:
  904. * Enable any character to restart output.
  905. * Operation resumes after receiving any
  906. * character after recognition of the XOFF character
  907. */
  908. if (termios->c_iflag & IXANY)
  909. up->mcr |= UART_MCR_XONANY;
  910. else
  911. up->mcr &= ~UART_MCR_XONANY;
  912. }
  913. serial_out(up, UART_MCR, up->mcr);
  914. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  915. serial_out(up, UART_EFR, up->efr);
  916. serial_out(up, UART_LCR, up->lcr);
  917. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  918. spin_unlock_irqrestore(&up->port.lock, flags);
  919. pm_runtime_mark_last_busy(up->dev);
  920. pm_runtime_put_autosuspend(up->dev);
  921. dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
  922. }
  923. static void
  924. serial_omap_pm(struct uart_port *port, unsigned int state,
  925. unsigned int oldstate)
  926. {
  927. struct uart_omap_port *up = to_uart_omap_port(port);
  928. unsigned char efr;
  929. dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
  930. pm_runtime_get_sync(up->dev);
  931. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  932. efr = serial_in(up, UART_EFR);
  933. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  934. serial_out(up, UART_LCR, 0);
  935. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  936. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  937. serial_out(up, UART_EFR, efr);
  938. serial_out(up, UART_LCR, 0);
  939. pm_runtime_mark_last_busy(up->dev);
  940. pm_runtime_put_autosuspend(up->dev);
  941. }
  942. static void serial_omap_release_port(struct uart_port *port)
  943. {
  944. dev_dbg(port->dev, "serial_omap_release_port+\n");
  945. }
  946. static int serial_omap_request_port(struct uart_port *port)
  947. {
  948. dev_dbg(port->dev, "serial_omap_request_port+\n");
  949. return 0;
  950. }
  951. static void serial_omap_config_port(struct uart_port *port, int flags)
  952. {
  953. struct uart_omap_port *up = to_uart_omap_port(port);
  954. dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
  955. up->port.line);
  956. up->port.type = PORT_OMAP;
  957. up->port.flags |= UPF_SOFT_FLOW | UPF_HARD_FLOW;
  958. }
  959. static int
  960. serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
  961. {
  962. /* we don't want the core code to modify any port params */
  963. dev_dbg(port->dev, "serial_omap_verify_port+\n");
  964. return -EINVAL;
  965. }
  966. static const char *
  967. serial_omap_type(struct uart_port *port)
  968. {
  969. struct uart_omap_port *up = to_uart_omap_port(port);
  970. dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
  971. return up->name;
  972. }
  973. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  974. static void __maybe_unused wait_for_xmitr(struct uart_omap_port *up)
  975. {
  976. unsigned int status, tmout = 10000;
  977. /* Wait up to 10ms for the character(s) to be sent. */
  978. do {
  979. status = serial_in(up, UART_LSR);
  980. if (status & UART_LSR_BI)
  981. up->lsr_break_flag = UART_LSR_BI;
  982. if (--tmout == 0)
  983. break;
  984. udelay(1);
  985. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  986. /* Wait up to 1s for flow control if necessary */
  987. if (up->port.flags & UPF_CONS_FLOW) {
  988. tmout = 1000000;
  989. for (tmout = 1000000; tmout; tmout--) {
  990. unsigned int msr = serial_in(up, UART_MSR);
  991. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  992. if (msr & UART_MSR_CTS)
  993. break;
  994. udelay(1);
  995. }
  996. }
  997. }
  998. #ifdef CONFIG_CONSOLE_POLL
  999. static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
  1000. {
  1001. struct uart_omap_port *up = to_uart_omap_port(port);
  1002. pm_runtime_get_sync(up->dev);
  1003. wait_for_xmitr(up);
  1004. serial_out(up, UART_TX, ch);
  1005. pm_runtime_mark_last_busy(up->dev);
  1006. pm_runtime_put_autosuspend(up->dev);
  1007. }
  1008. static int serial_omap_poll_get_char(struct uart_port *port)
  1009. {
  1010. struct uart_omap_port *up = to_uart_omap_port(port);
  1011. unsigned int status;
  1012. pm_runtime_get_sync(up->dev);
  1013. status = serial_in(up, UART_LSR);
  1014. if (!(status & UART_LSR_DR)) {
  1015. status = NO_POLL_CHAR;
  1016. goto out;
  1017. }
  1018. status = serial_in(up, UART_RX);
  1019. out:
  1020. pm_runtime_mark_last_busy(up->dev);
  1021. pm_runtime_put_autosuspend(up->dev);
  1022. return status;
  1023. }
  1024. #endif /* CONFIG_CONSOLE_POLL */
  1025. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  1026. static struct uart_omap_port *serial_omap_console_ports[OMAP_MAX_HSUART_PORTS];
  1027. static struct uart_driver serial_omap_reg;
  1028. static void serial_omap_console_putchar(struct uart_port *port, int ch)
  1029. {
  1030. struct uart_omap_port *up = to_uart_omap_port(port);
  1031. wait_for_xmitr(up);
  1032. serial_out(up, UART_TX, ch);
  1033. }
  1034. static void
  1035. serial_omap_console_write(struct console *co, const char *s,
  1036. unsigned int count)
  1037. {
  1038. struct uart_omap_port *up = serial_omap_console_ports[co->index];
  1039. unsigned long flags;
  1040. unsigned int ier;
  1041. int locked = 1;
  1042. pm_runtime_get_sync(up->dev);
  1043. local_irq_save(flags);
  1044. if (up->port.sysrq)
  1045. locked = 0;
  1046. else if (oops_in_progress)
  1047. locked = spin_trylock(&up->port.lock);
  1048. else
  1049. spin_lock(&up->port.lock);
  1050. /*
  1051. * First save the IER then disable the interrupts
  1052. */
  1053. ier = serial_in(up, UART_IER);
  1054. serial_out(up, UART_IER, 0);
  1055. uart_console_write(&up->port, s, count, serial_omap_console_putchar);
  1056. /*
  1057. * Finally, wait for transmitter to become empty
  1058. * and restore the IER
  1059. */
  1060. wait_for_xmitr(up);
  1061. serial_out(up, UART_IER, ier);
  1062. /*
  1063. * The receive handling will happen properly because the
  1064. * receive ready bit will still be set; it is not cleared
  1065. * on read. However, modem control will not, we must
  1066. * call it if we have saved something in the saved flags
  1067. * while processing with interrupts off.
  1068. */
  1069. if (up->msr_saved_flags)
  1070. check_modem_status(up);
  1071. pm_runtime_mark_last_busy(up->dev);
  1072. pm_runtime_put_autosuspend(up->dev);
  1073. if (locked)
  1074. spin_unlock(&up->port.lock);
  1075. local_irq_restore(flags);
  1076. }
  1077. static int __init
  1078. serial_omap_console_setup(struct console *co, char *options)
  1079. {
  1080. struct uart_omap_port *up;
  1081. int baud = 115200;
  1082. int bits = 8;
  1083. int parity = 'n';
  1084. int flow = 'n';
  1085. if (serial_omap_console_ports[co->index] == NULL)
  1086. return -ENODEV;
  1087. up = serial_omap_console_ports[co->index];
  1088. if (options)
  1089. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1090. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  1091. }
  1092. static struct console serial_omap_console = {
  1093. .name = OMAP_SERIAL_NAME,
  1094. .write = serial_omap_console_write,
  1095. .device = uart_console_device,
  1096. .setup = serial_omap_console_setup,
  1097. .flags = CON_PRINTBUFFER,
  1098. .index = -1,
  1099. .data = &serial_omap_reg,
  1100. };
  1101. static void serial_omap_add_console_port(struct uart_omap_port *up)
  1102. {
  1103. serial_omap_console_ports[up->port.line] = up;
  1104. }
  1105. #define OMAP_CONSOLE (&serial_omap_console)
  1106. #else
  1107. #define OMAP_CONSOLE NULL
  1108. static inline void serial_omap_add_console_port(struct uart_omap_port *up)
  1109. {}
  1110. #endif
  1111. /* Enable or disable the rs485 support */
  1112. static int
  1113. serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485)
  1114. {
  1115. struct uart_omap_port *up = to_uart_omap_port(port);
  1116. unsigned int mode;
  1117. int val;
  1118. pm_runtime_get_sync(up->dev);
  1119. /* Disable interrupts from this port */
  1120. mode = up->ier;
  1121. up->ier = 0;
  1122. serial_out(up, UART_IER, 0);
  1123. /* Clamp the delays to [0, 100ms] */
  1124. rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
  1125. rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U);
  1126. /* store new config */
  1127. port->rs485 = *rs485;
  1128. /*
  1129. * Just as a precaution, only allow rs485
  1130. * to be enabled if the gpio pin is valid
  1131. */
  1132. if (gpio_is_valid(up->rts_gpio)) {
  1133. /* enable / disable rts */
  1134. val = (port->rs485.flags & SER_RS485_ENABLED) ?
  1135. SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND;
  1136. val = (port->rs485.flags & val) ? 1 : 0;
  1137. gpio_set_value(up->rts_gpio, val);
  1138. } else
  1139. port->rs485.flags &= ~SER_RS485_ENABLED;
  1140. /* Enable interrupts */
  1141. up->ier = mode;
  1142. serial_out(up, UART_IER, up->ier);
  1143. /* If RS-485 is disabled, make sure the THR interrupt is fired when
  1144. * TX FIFO is below the trigger level.
  1145. */
  1146. if (!(port->rs485.flags & SER_RS485_ENABLED) &&
  1147. (up->scr & OMAP_UART_SCR_TX_EMPTY)) {
  1148. up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
  1149. serial_out(up, UART_OMAP_SCR, up->scr);
  1150. }
  1151. pm_runtime_mark_last_busy(up->dev);
  1152. pm_runtime_put_autosuspend(up->dev);
  1153. return 0;
  1154. }
  1155. static struct uart_ops serial_omap_pops = {
  1156. .tx_empty = serial_omap_tx_empty,
  1157. .set_mctrl = serial_omap_set_mctrl,
  1158. .get_mctrl = serial_omap_get_mctrl,
  1159. .stop_tx = serial_omap_stop_tx,
  1160. .start_tx = serial_omap_start_tx,
  1161. .throttle = serial_omap_throttle,
  1162. .unthrottle = serial_omap_unthrottle,
  1163. .stop_rx = serial_omap_stop_rx,
  1164. .enable_ms = serial_omap_enable_ms,
  1165. .break_ctl = serial_omap_break_ctl,
  1166. .startup = serial_omap_startup,
  1167. .shutdown = serial_omap_shutdown,
  1168. .set_termios = serial_omap_set_termios,
  1169. .pm = serial_omap_pm,
  1170. .type = serial_omap_type,
  1171. .release_port = serial_omap_release_port,
  1172. .request_port = serial_omap_request_port,
  1173. .config_port = serial_omap_config_port,
  1174. .verify_port = serial_omap_verify_port,
  1175. #ifdef CONFIG_CONSOLE_POLL
  1176. .poll_put_char = serial_omap_poll_put_char,
  1177. .poll_get_char = serial_omap_poll_get_char,
  1178. #endif
  1179. };
  1180. static struct uart_driver serial_omap_reg = {
  1181. .owner = THIS_MODULE,
  1182. .driver_name = "OMAP-SERIAL",
  1183. .dev_name = OMAP_SERIAL_NAME,
  1184. .nr = OMAP_MAX_HSUART_PORTS,
  1185. .cons = OMAP_CONSOLE,
  1186. };
  1187. #ifdef CONFIG_PM_SLEEP
  1188. static int serial_omap_prepare(struct device *dev)
  1189. {
  1190. struct uart_omap_port *up = dev_get_drvdata(dev);
  1191. up->is_suspending = true;
  1192. return 0;
  1193. }
  1194. static void serial_omap_complete(struct device *dev)
  1195. {
  1196. struct uart_omap_port *up = dev_get_drvdata(dev);
  1197. up->is_suspending = false;
  1198. }
  1199. static int serial_omap_suspend(struct device *dev)
  1200. {
  1201. struct uart_omap_port *up = dev_get_drvdata(dev);
  1202. uart_suspend_port(&serial_omap_reg, &up->port);
  1203. flush_work(&up->qos_work);
  1204. if (device_may_wakeup(dev))
  1205. serial_omap_enable_wakeup(up, true);
  1206. else
  1207. serial_omap_enable_wakeup(up, false);
  1208. return 0;
  1209. }
  1210. static int serial_omap_resume(struct device *dev)
  1211. {
  1212. struct uart_omap_port *up = dev_get_drvdata(dev);
  1213. if (device_may_wakeup(dev))
  1214. serial_omap_enable_wakeup(up, false);
  1215. uart_resume_port(&serial_omap_reg, &up->port);
  1216. return 0;
  1217. }
  1218. #else
  1219. #define serial_omap_prepare NULL
  1220. #define serial_omap_complete NULL
  1221. #endif /* CONFIG_PM_SLEEP */
  1222. static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
  1223. {
  1224. u32 mvr, scheme;
  1225. u16 revision, major, minor;
  1226. mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
  1227. /* Check revision register scheme */
  1228. scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
  1229. switch (scheme) {
  1230. case 0: /* Legacy Scheme: OMAP2/3 */
  1231. /* MINOR_REV[0:4], MAJOR_REV[4:7] */
  1232. major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
  1233. OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
  1234. minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
  1235. break;
  1236. case 1:
  1237. /* New Scheme: OMAP4+ */
  1238. /* MINOR_REV[0:5], MAJOR_REV[8:10] */
  1239. major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
  1240. OMAP_UART_MVR_MAJ_SHIFT;
  1241. minor = (mvr & OMAP_UART_MVR_MIN_MASK);
  1242. break;
  1243. default:
  1244. dev_warn(up->dev,
  1245. "Unknown %s revision, defaulting to highest\n",
  1246. up->name);
  1247. /* highest possible revision */
  1248. major = 0xff;
  1249. minor = 0xff;
  1250. }
  1251. /* normalize revision for the driver */
  1252. revision = UART_BUILD_REVISION(major, minor);
  1253. switch (revision) {
  1254. case OMAP_UART_REV_46:
  1255. up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
  1256. UART_ERRATA_i291_DMA_FORCEIDLE);
  1257. break;
  1258. case OMAP_UART_REV_52:
  1259. up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
  1260. UART_ERRATA_i291_DMA_FORCEIDLE);
  1261. up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
  1262. break;
  1263. case OMAP_UART_REV_63:
  1264. up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
  1265. up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
  1266. break;
  1267. default:
  1268. break;
  1269. }
  1270. }
  1271. static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
  1272. {
  1273. struct omap_uart_port_info *omap_up_info;
  1274. omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
  1275. if (!omap_up_info)
  1276. return NULL; /* out of memory */
  1277. of_property_read_u32(dev->of_node, "clock-frequency",
  1278. &omap_up_info->uartclk);
  1279. return omap_up_info;
  1280. }
  1281. static int serial_omap_probe_rs485(struct uart_omap_port *up,
  1282. struct device_node *np)
  1283. {
  1284. struct serial_rs485 *rs485conf = &up->port.rs485;
  1285. u32 rs485_delay[2];
  1286. enum of_gpio_flags flags;
  1287. int ret;
  1288. rs485conf->flags = 0;
  1289. up->rts_gpio = -EINVAL;
  1290. if (!np)
  1291. return 0;
  1292. if (of_property_read_bool(np, "rs485-rts-active-high"))
  1293. rs485conf->flags |= SER_RS485_RTS_ON_SEND;
  1294. else
  1295. rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  1296. /* check for tx enable gpio */
  1297. up->rts_gpio = of_get_named_gpio_flags(np, "rts-gpio", 0, &flags);
  1298. if (gpio_is_valid(up->rts_gpio)) {
  1299. ret = devm_gpio_request(up->dev, up->rts_gpio, "omap-serial");
  1300. if (ret < 0)
  1301. return ret;
  1302. ret = gpio_direction_output(up->rts_gpio,
  1303. flags & SER_RS485_RTS_AFTER_SEND);
  1304. if (ret < 0)
  1305. return ret;
  1306. } else if (up->rts_gpio == -EPROBE_DEFER) {
  1307. return -EPROBE_DEFER;
  1308. } else {
  1309. up->rts_gpio = -EINVAL;
  1310. }
  1311. if (of_property_read_u32_array(np, "rs485-rts-delay",
  1312. rs485_delay, 2) == 0) {
  1313. rs485conf->delay_rts_before_send = rs485_delay[0];
  1314. rs485conf->delay_rts_after_send = rs485_delay[1];
  1315. }
  1316. if (of_property_read_bool(np, "rs485-rx-during-tx"))
  1317. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1318. if (of_property_read_bool(np, "linux,rs485-enabled-at-boot-time"))
  1319. rs485conf->flags |= SER_RS485_ENABLED;
  1320. return 0;
  1321. }
  1322. static int serial_omap_probe(struct platform_device *pdev)
  1323. {
  1324. struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
  1325. struct uart_omap_port *up;
  1326. struct resource *mem;
  1327. void __iomem *base;
  1328. int uartirq = 0;
  1329. int wakeirq = 0;
  1330. int ret;
  1331. /* The optional wakeirq may be specified in the board dts file */
  1332. if (pdev->dev.of_node) {
  1333. uartirq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1334. if (!uartirq)
  1335. return -EPROBE_DEFER;
  1336. wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1337. omap_up_info = of_get_uart_port_info(&pdev->dev);
  1338. pdev->dev.platform_data = omap_up_info;
  1339. } else {
  1340. uartirq = platform_get_irq(pdev, 0);
  1341. if (uartirq < 0)
  1342. return -EPROBE_DEFER;
  1343. }
  1344. up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
  1345. if (!up)
  1346. return -ENOMEM;
  1347. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1348. base = devm_ioremap_resource(&pdev->dev, mem);
  1349. if (IS_ERR(base))
  1350. return PTR_ERR(base);
  1351. up->dev = &pdev->dev;
  1352. up->port.dev = &pdev->dev;
  1353. up->port.type = PORT_OMAP;
  1354. up->port.iotype = UPIO_MEM;
  1355. up->port.irq = uartirq;
  1356. up->port.regshift = 2;
  1357. up->port.fifosize = 64;
  1358. up->port.ops = &serial_omap_pops;
  1359. if (pdev->dev.of_node)
  1360. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  1361. else
  1362. ret = pdev->id;
  1363. if (ret < 0) {
  1364. dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
  1365. ret);
  1366. goto err_port_line;
  1367. }
  1368. up->port.line = ret;
  1369. if (up->port.line >= OMAP_MAX_HSUART_PORTS) {
  1370. dev_err(&pdev->dev, "uart ID %d > MAX %d.\n", up->port.line,
  1371. OMAP_MAX_HSUART_PORTS);
  1372. ret = -ENXIO;
  1373. goto err_port_line;
  1374. }
  1375. up->wakeirq = wakeirq;
  1376. if (!up->wakeirq)
  1377. dev_info(up->port.dev, "no wakeirq for uart%d\n",
  1378. up->port.line);
  1379. ret = serial_omap_probe_rs485(up, pdev->dev.of_node);
  1380. if (ret < 0)
  1381. goto err_rs485;
  1382. sprintf(up->name, "OMAP UART%d", up->port.line);
  1383. up->port.mapbase = mem->start;
  1384. up->port.membase = base;
  1385. up->port.flags = omap_up_info->flags;
  1386. up->port.uartclk = omap_up_info->uartclk;
  1387. up->port.rs485_config = serial_omap_config_rs485;
  1388. if (!up->port.uartclk) {
  1389. up->port.uartclk = DEFAULT_CLK_SPEED;
  1390. dev_warn(&pdev->dev,
  1391. "No clock speed specified: using default: %d\n",
  1392. DEFAULT_CLK_SPEED);
  1393. }
  1394. up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1395. up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1396. pm_qos_add_request(&up->pm_qos_request,
  1397. PM_QOS_CPU_DMA_LATENCY, up->latency);
  1398. INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
  1399. platform_set_drvdata(pdev, up);
  1400. if (omap_up_info->autosuspend_timeout == 0)
  1401. omap_up_info->autosuspend_timeout = -1;
  1402. device_init_wakeup(up->dev, true);
  1403. pm_runtime_use_autosuspend(&pdev->dev);
  1404. pm_runtime_set_autosuspend_delay(&pdev->dev,
  1405. omap_up_info->autosuspend_timeout);
  1406. pm_runtime_irq_safe(&pdev->dev);
  1407. pm_runtime_enable(&pdev->dev);
  1408. pm_runtime_get_sync(&pdev->dev);
  1409. omap_serial_fill_features_erratas(up);
  1410. ui[up->port.line] = up;
  1411. serial_omap_add_console_port(up);
  1412. ret = uart_add_one_port(&serial_omap_reg, &up->port);
  1413. if (ret != 0)
  1414. goto err_add_port;
  1415. pm_runtime_mark_last_busy(up->dev);
  1416. pm_runtime_put_autosuspend(up->dev);
  1417. return 0;
  1418. err_add_port:
  1419. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1420. pm_runtime_put_sync(&pdev->dev);
  1421. pm_runtime_disable(&pdev->dev);
  1422. pm_qos_remove_request(&up->pm_qos_request);
  1423. device_init_wakeup(up->dev, false);
  1424. err_rs485:
  1425. err_port_line:
  1426. return ret;
  1427. }
  1428. static int serial_omap_remove(struct platform_device *dev)
  1429. {
  1430. struct uart_omap_port *up = platform_get_drvdata(dev);
  1431. pm_runtime_get_sync(up->dev);
  1432. uart_remove_one_port(&serial_omap_reg, &up->port);
  1433. pm_runtime_dont_use_autosuspend(up->dev);
  1434. pm_runtime_put_sync(up->dev);
  1435. pm_runtime_disable(up->dev);
  1436. pm_qos_remove_request(&up->pm_qos_request);
  1437. device_init_wakeup(&dev->dev, false);
  1438. return 0;
  1439. }
  1440. /*
  1441. * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
  1442. * The access to uart register after MDR1 Access
  1443. * causes UART to corrupt data.
  1444. *
  1445. * Need a delay =
  1446. * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
  1447. * give 10 times as much
  1448. */
  1449. static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
  1450. {
  1451. u8 timeout = 255;
  1452. serial_out(up, UART_OMAP_MDR1, mdr1);
  1453. udelay(2);
  1454. serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
  1455. UART_FCR_CLEAR_RCVR);
  1456. /*
  1457. * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
  1458. * TX_FIFO_E bit is 1.
  1459. */
  1460. while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
  1461. (UART_LSR_THRE | UART_LSR_DR))) {
  1462. timeout--;
  1463. if (!timeout) {
  1464. /* Should *never* happen. we warn and carry on */
  1465. dev_crit(up->dev, "Errata i202: timedout %x\n",
  1466. serial_in(up, UART_LSR));
  1467. break;
  1468. }
  1469. udelay(1);
  1470. }
  1471. }
  1472. #ifdef CONFIG_PM
  1473. static void serial_omap_restore_context(struct uart_omap_port *up)
  1474. {
  1475. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  1476. serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
  1477. else
  1478. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
  1479. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
  1480. serial_out(up, UART_EFR, UART_EFR_ECB);
  1481. serial_out(up, UART_LCR, 0x0); /* Operational mode */
  1482. serial_out(up, UART_IER, 0x0);
  1483. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
  1484. serial_out(up, UART_DLL, up->dll);
  1485. serial_out(up, UART_DLM, up->dlh);
  1486. serial_out(up, UART_LCR, 0x0); /* Operational mode */
  1487. serial_out(up, UART_IER, up->ier);
  1488. serial_out(up, UART_FCR, up->fcr);
  1489. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  1490. serial_out(up, UART_MCR, up->mcr);
  1491. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
  1492. serial_out(up, UART_OMAP_SCR, up->scr);
  1493. serial_out(up, UART_EFR, up->efr);
  1494. serial_out(up, UART_LCR, up->lcr);
  1495. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  1496. serial_omap_mdr1_errataset(up, up->mdr1);
  1497. else
  1498. serial_out(up, UART_OMAP_MDR1, up->mdr1);
  1499. serial_out(up, UART_OMAP_WER, up->wer);
  1500. }
  1501. static int serial_omap_runtime_suspend(struct device *dev)
  1502. {
  1503. struct uart_omap_port *up = dev_get_drvdata(dev);
  1504. if (!up)
  1505. return -EINVAL;
  1506. /*
  1507. * When using 'no_console_suspend', the console UART must not be
  1508. * suspended. Since driver suspend is managed by runtime suspend,
  1509. * preventing runtime suspend (by returning error) will keep device
  1510. * active during suspend.
  1511. */
  1512. if (up->is_suspending && !console_suspend_enabled &&
  1513. uart_console(&up->port))
  1514. return -EBUSY;
  1515. up->context_loss_cnt = serial_omap_get_context_loss_count(up);
  1516. serial_omap_enable_wakeup(up, true);
  1517. up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1518. schedule_work(&up->qos_work);
  1519. return 0;
  1520. }
  1521. static int serial_omap_runtime_resume(struct device *dev)
  1522. {
  1523. struct uart_omap_port *up = dev_get_drvdata(dev);
  1524. int loss_cnt = serial_omap_get_context_loss_count(up);
  1525. serial_omap_enable_wakeup(up, false);
  1526. if (loss_cnt < 0) {
  1527. dev_dbg(dev, "serial_omap_get_context_loss_count failed : %d\n",
  1528. loss_cnt);
  1529. serial_omap_restore_context(up);
  1530. } else if (up->context_loss_cnt != loss_cnt) {
  1531. serial_omap_restore_context(up);
  1532. }
  1533. up->latency = up->calc_latency;
  1534. schedule_work(&up->qos_work);
  1535. return 0;
  1536. }
  1537. #endif
  1538. static const struct dev_pm_ops serial_omap_dev_pm_ops = {
  1539. SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
  1540. SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
  1541. serial_omap_runtime_resume, NULL)
  1542. .prepare = serial_omap_prepare,
  1543. .complete = serial_omap_complete,
  1544. };
  1545. #if defined(CONFIG_OF)
  1546. static const struct of_device_id omap_serial_of_match[] = {
  1547. { .compatible = "ti,omap2-uart" },
  1548. { .compatible = "ti,omap3-uart" },
  1549. { .compatible = "ti,omap4-uart" },
  1550. {},
  1551. };
  1552. MODULE_DEVICE_TABLE(of, omap_serial_of_match);
  1553. #endif
  1554. static struct platform_driver serial_omap_driver = {
  1555. .probe = serial_omap_probe,
  1556. .remove = serial_omap_remove,
  1557. .driver = {
  1558. .name = OMAP_SERIAL_DRIVER_NAME,
  1559. .pm = &serial_omap_dev_pm_ops,
  1560. .of_match_table = of_match_ptr(omap_serial_of_match),
  1561. },
  1562. };
  1563. static int __init serial_omap_init(void)
  1564. {
  1565. int ret;
  1566. ret = uart_register_driver(&serial_omap_reg);
  1567. if (ret != 0)
  1568. return ret;
  1569. ret = platform_driver_register(&serial_omap_driver);
  1570. if (ret != 0)
  1571. uart_unregister_driver(&serial_omap_reg);
  1572. return ret;
  1573. }
  1574. static void __exit serial_omap_exit(void)
  1575. {
  1576. platform_driver_unregister(&serial_omap_driver);
  1577. uart_unregister_driver(&serial_omap_reg);
  1578. }
  1579. module_init(serial_omap_init);
  1580. module_exit(serial_omap_exit);
  1581. MODULE_DESCRIPTION("OMAP High Speed UART driver");
  1582. MODULE_LICENSE("GPL");
  1583. MODULE_AUTHOR("Texas Instruments Inc");