8250_omap.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * 8250-core based driver for the OMAP internal UART
  4. *
  5. * based on omap-serial.c, Copyright (C) 2010 Texas Instruments.
  6. *
  7. * Copyright (C) 2014 Sebastian Andrzej Siewior
  8. *
  9. */
  10. #include <linux/atomic.h>
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/serial_8250.h>
  16. #include <linux/serial_reg.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/delay.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/console.h>
  25. #include <linux/pm_qos.h>
  26. #include <linux/pm_wakeirq.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/sys_soc.h>
  29. #include <linux/reboot.h>
  30. #include <linux/pinctrl/consumer.h>
  31. #include "8250.h"
  32. #define DEFAULT_CLK_SPEED 48000000
  33. #define OMAP_UART_REGSHIFT 2
  34. #define UART_ERRATA_i202_MDR1_ACCESS (1 << 0)
  35. #define OMAP_UART_WER_HAS_TX_WAKEUP (1 << 1)
  36. #define OMAP_DMA_TX_KICK (1 << 2)
  37. /*
  38. * See Advisory 21 in AM437x errata SPRZ408B, updated April 2015.
  39. * The same errata is applicable to AM335x and DRA7x processors too.
  40. */
  41. #define UART_ERRATA_CLOCK_DISABLE (1 << 3)
  42. #define UART_HAS_EFR2 BIT(4)
  43. #define UART_HAS_RHR_IT_DIS BIT(5)
  44. #define UART_RX_TIMEOUT_QUIRK BIT(6)
  45. #define UART_HAS_NATIVE_RS485 BIT(7)
  46. #define OMAP_UART_FCR_RX_TRIG 6
  47. #define OMAP_UART_FCR_TX_TRIG 4
  48. /* SCR register bitmasks */
  49. #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
  50. #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
  51. #define OMAP_UART_SCR_TX_EMPTY (1 << 3)
  52. #define OMAP_UART_SCR_DMAMODE_MASK (3 << 1)
  53. #define OMAP_UART_SCR_DMAMODE_1 (1 << 1)
  54. #define OMAP_UART_SCR_DMAMODE_CTL (1 << 0)
  55. /* MVR register bitmasks */
  56. #define OMAP_UART_MVR_SCHEME_SHIFT 30
  57. #define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
  58. #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
  59. #define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
  60. #define OMAP_UART_MVR_MAJ_MASK 0x700
  61. #define OMAP_UART_MVR_MAJ_SHIFT 8
  62. #define OMAP_UART_MVR_MIN_MASK 0x3f
  63. /* SYSC register bitmasks */
  64. #define OMAP_UART_SYSC_SOFTRESET (1 << 1)
  65. /* SYSS register bitmasks */
  66. #define OMAP_UART_SYSS_RESETDONE (1 << 0)
  67. #define UART_TI752_TLR_TX 0
  68. #define UART_TI752_TLR_RX 4
  69. #define TRIGGER_TLR_MASK(x) ((x & 0x3c) >> 2)
  70. #define TRIGGER_FCR_MASK(x) (x & 3)
  71. /* Enable XON/XOFF flow control on output */
  72. #define OMAP_UART_SW_TX 0x08
  73. /* Enable XON/XOFF flow control on input */
  74. #define OMAP_UART_SW_RX 0x02
  75. #define OMAP_UART_WER_MOD_WKUP 0x7f
  76. #define OMAP_UART_TX_WAKEUP_EN (1 << 7)
  77. #define TX_TRIGGER 1
  78. #define RX_TRIGGER 48
  79. #define OMAP_UART_TCR_RESTORE(x) ((x / 4) << 4)
  80. #define OMAP_UART_TCR_HALT(x) ((x / 4) << 0)
  81. #define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
  82. #define OMAP_UART_REV_46 0x0406
  83. #define OMAP_UART_REV_52 0x0502
  84. #define OMAP_UART_REV_63 0x0603
  85. /* Resume register */
  86. #define UART_OMAP_RESUME 0x0B
  87. /* Interrupt Enable Register 2 */
  88. #define UART_OMAP_IER2 0x1B
  89. #define UART_OMAP_IER2_RHR_IT_DIS BIT(2)
  90. /* Mode Definition Register 3 */
  91. #define UART_OMAP_MDR3 0x20
  92. #define UART_OMAP_MDR3_DIR_POL BIT(3)
  93. #define UART_OMAP_MDR3_DIR_EN BIT(4)
  94. /* Enhanced features register 2 */
  95. #define UART_OMAP_EFR2 0x23
  96. #define UART_OMAP_EFR2_TIMEOUT_BEHAVE BIT(6)
  97. /* RX FIFO occupancy indicator */
  98. #define UART_OMAP_RX_LVL 0x19
  99. /* Timeout low and High */
  100. #define UART_OMAP_TO_L 0x26
  101. #define UART_OMAP_TO_H 0x27
  102. struct omap8250_priv {
  103. void __iomem *membase;
  104. int line;
  105. u8 habit;
  106. u8 mdr1;
  107. u8 mdr3;
  108. u8 efr;
  109. u8 scr;
  110. u8 wer;
  111. u8 xon;
  112. u8 xoff;
  113. u8 delayed_restore;
  114. u16 quot;
  115. u8 tx_trigger;
  116. u8 rx_trigger;
  117. atomic_t active;
  118. bool is_suspending;
  119. int wakeirq;
  120. u32 latency;
  121. u32 calc_latency;
  122. struct pm_qos_request pm_qos_request;
  123. struct work_struct qos_work;
  124. struct uart_8250_dma omap8250_dma;
  125. spinlock_t rx_dma_lock;
  126. bool rx_dma_broken;
  127. bool throttled;
  128. struct pinctrl *pinctrl;
  129. struct pinctrl_state *pinctrl_wakeup;
  130. };
  131. struct omap8250_dma_params {
  132. u32 rx_size;
  133. u8 rx_trigger;
  134. u8 tx_trigger;
  135. };
  136. struct omap8250_platdata {
  137. struct omap8250_dma_params *dma_params;
  138. u8 habit;
  139. };
  140. #ifdef CONFIG_SERIAL_8250_DMA
  141. static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
  142. #else
  143. static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
  144. #endif
  145. static u32 uart_read(struct omap8250_priv *priv, u32 reg)
  146. {
  147. return readl(priv->membase + (reg << OMAP_UART_REGSHIFT));
  148. }
  149. /*
  150. * Called on runtime PM resume path from omap8250_restore_regs(), and
  151. * omap8250_set_mctrl().
  152. */
  153. static void __omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  154. {
  155. struct uart_8250_port *up = up_to_u8250p(port);
  156. struct omap8250_priv *priv = port->private_data;
  157. u8 lcr;
  158. serial8250_do_set_mctrl(port, mctrl);
  159. if (!mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS)) {
  160. /*
  161. * Turn off autoRTS if RTS is lowered and restore autoRTS
  162. * setting if RTS is raised
  163. */
  164. lcr = serial_in(up, UART_LCR);
  165. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  166. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  167. priv->efr |= UART_EFR_RTS;
  168. else
  169. priv->efr &= ~UART_EFR_RTS;
  170. serial_out(up, UART_EFR, priv->efr);
  171. serial_out(up, UART_LCR, lcr);
  172. }
  173. }
  174. static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  175. {
  176. int err;
  177. err = pm_runtime_resume_and_get(port->dev);
  178. if (err)
  179. return;
  180. __omap8250_set_mctrl(port, mctrl);
  181. pm_runtime_mark_last_busy(port->dev);
  182. pm_runtime_put_autosuspend(port->dev);
  183. }
  184. /*
  185. * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
  186. * The access to uart register after MDR1 Access
  187. * causes UART to corrupt data.
  188. *
  189. * Need a delay =
  190. * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
  191. * give 10 times as much
  192. */
  193. static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
  194. struct omap8250_priv *priv)
  195. {
  196. serial_out(up, UART_OMAP_MDR1, priv->mdr1);
  197. udelay(2);
  198. serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
  199. UART_FCR_CLEAR_RCVR);
  200. }
  201. static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
  202. struct omap8250_priv *priv)
  203. {
  204. unsigned int uartclk = port->uartclk;
  205. unsigned int div_13, div_16;
  206. unsigned int abs_d13, abs_d16;
  207. div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
  208. div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
  209. if (!div_13)
  210. div_13 = 1;
  211. if (!div_16)
  212. div_16 = 1;
  213. abs_d13 = abs(baud - uartclk / 13 / div_13);
  214. abs_d16 = abs(baud - uartclk / 16 / div_16);
  215. if (abs_d13 >= abs_d16) {
  216. priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
  217. priv->quot = div_16;
  218. } else {
  219. priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
  220. priv->quot = div_13;
  221. }
  222. }
  223. static void omap8250_update_scr(struct uart_8250_port *up,
  224. struct omap8250_priv *priv)
  225. {
  226. u8 old_scr;
  227. old_scr = serial_in(up, UART_OMAP_SCR);
  228. if (old_scr == priv->scr)
  229. return;
  230. /*
  231. * The manual recommends not to enable the DMA mode selector in the SCR
  232. * (instead of the FCR) register _and_ selecting the DMA mode as one
  233. * register write because this may lead to malfunction.
  234. */
  235. if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
  236. serial_out(up, UART_OMAP_SCR,
  237. priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
  238. serial_out(up, UART_OMAP_SCR, priv->scr);
  239. }
  240. static void omap8250_update_mdr1(struct uart_8250_port *up,
  241. struct omap8250_priv *priv)
  242. {
  243. if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
  244. omap_8250_mdr1_errataset(up, priv);
  245. else
  246. serial_out(up, UART_OMAP_MDR1, priv->mdr1);
  247. }
  248. static void omap8250_restore_regs(struct uart_8250_port *up)
  249. {
  250. struct uart_port *port = &up->port;
  251. struct omap8250_priv *priv = port->private_data;
  252. struct uart_8250_dma *dma = up->dma;
  253. u8 mcr = serial8250_in_MCR(up);
  254. /* Port locked to synchronize UART_IER access against the console. */
  255. lockdep_assert_held_once(&port->lock);
  256. if (dma && dma->tx_running) {
  257. /*
  258. * TCSANOW requests the change to occur immediately however if
  259. * we have a TX-DMA operation in progress then it has been
  260. * observed that it might stall and never complete. Therefore we
  261. * delay DMA completes to prevent this hang from happen.
  262. */
  263. priv->delayed_restore = 1;
  264. return;
  265. }
  266. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  267. serial_out(up, UART_EFR, UART_EFR_ECB);
  268. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  269. serial8250_out_MCR(up, mcr | UART_MCR_TCRTLR);
  270. serial_out(up, UART_FCR, up->fcr);
  271. omap8250_update_scr(up, priv);
  272. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  273. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
  274. OMAP_UART_TCR_HALT(52));
  275. serial_out(up, UART_TI752_TLR,
  276. TRIGGER_TLR_MASK(priv->tx_trigger) << UART_TI752_TLR_TX |
  277. TRIGGER_TLR_MASK(priv->rx_trigger) << UART_TI752_TLR_RX);
  278. serial_out(up, UART_LCR, 0);
  279. /* drop TCR + TLR access, we setup XON/XOFF later */
  280. serial8250_out_MCR(up, mcr);
  281. serial_out(up, UART_IER, up->ier);
  282. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  283. serial_dl_write(up, priv->quot);
  284. serial_out(up, UART_EFR, priv->efr);
  285. /* Configure flow control */
  286. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  287. serial_out(up, UART_XON1, priv->xon);
  288. serial_out(up, UART_XOFF1, priv->xoff);
  289. serial_out(up, UART_LCR, up->lcr);
  290. omap8250_update_mdr1(up, priv);
  291. __omap8250_set_mctrl(port, port->mctrl);
  292. serial_out(up, UART_OMAP_MDR3, priv->mdr3);
  293. if (port->rs485.flags & SER_RS485_ENABLED &&
  294. port->rs485_config == serial8250_em485_config)
  295. serial8250_em485_stop_tx(up, true);
  296. }
  297. static void omap_8250_set_termios_atomic(struct uart_port *port, struct ktermios *termios,
  298. const struct ktermios *old, unsigned int baud)
  299. {
  300. struct uart_8250_port *up = up_to_u8250p(port);
  301. struct omap8250_priv *priv = port->private_data;
  302. u8 cval;
  303. cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
  304. if (termios->c_cflag & CSTOPB)
  305. cval |= UART_LCR_STOP;
  306. if (termios->c_cflag & PARENB)
  307. cval |= UART_LCR_PARITY;
  308. if (!(termios->c_cflag & PARODD))
  309. cval |= UART_LCR_EPAR;
  310. if (termios->c_cflag & CMSPAR)
  311. cval |= UART_LCR_SPAR;
  312. omap_8250_get_divisor(port, baud, priv);
  313. /*
  314. * Ok, we're now changing the port state. Do it with
  315. * interrupts disabled.
  316. */
  317. guard(serial8250_rpm)(up);
  318. guard(uart_port_lock_irq)(port);
  319. /*
  320. * Update the per-port timeout.
  321. */
  322. uart_update_timeout(port, termios->c_cflag, baud);
  323. /*
  324. * Specify which conditions may be considered for error
  325. * handling and the ignoring of characters. The actual
  326. * ignoring of characters only occurs if the bit is set
  327. * in @ignore_status_mask as well.
  328. */
  329. port->read_status_mask = UART_LSR_OE | UART_LSR_DR;
  330. if (termios->c_iflag & INPCK)
  331. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  332. if (termios->c_iflag & (IGNBRK | PARMRK))
  333. port->read_status_mask |= UART_LSR_BI;
  334. /*
  335. * Characters to ignore
  336. */
  337. port->ignore_status_mask = 0;
  338. if (termios->c_iflag & IGNPAR)
  339. port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  340. if (termios->c_iflag & IGNBRK) {
  341. port->ignore_status_mask |= UART_LSR_BI;
  342. /*
  343. * If we're ignoring parity and break indicators,
  344. * ignore overruns too (for real raw support).
  345. */
  346. if (termios->c_iflag & IGNPAR)
  347. port->ignore_status_mask |= UART_LSR_OE;
  348. }
  349. /*
  350. * ignore all characters if CREAD is not set
  351. */
  352. if ((termios->c_cflag & CREAD) == 0)
  353. port->ignore_status_mask |= UART_LSR_DR;
  354. /*
  355. * Modem status interrupts
  356. */
  357. up->ier &= ~UART_IER_MSI;
  358. if (UART_ENABLE_MS(port, termios->c_cflag))
  359. up->ier |= UART_IER_MSI;
  360. up->lcr = cval;
  361. /* Up to here it was mostly serial8250_do_set_termios() */
  362. /*
  363. * We enable TRIG_GRANU for RX and TX and additionally we set
  364. * SCR_TX_EMPTY bit. The result is the following:
  365. * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
  366. * - less than RX_TRIGGER number of bytes will also cause an interrupt
  367. * once the UART decides that there no new bytes arriving.
  368. * - Once THRE is enabled, the interrupt will be fired once the FIFO is
  369. * empty - the trigger level is ignored here.
  370. *
  371. * Once DMA is enabled:
  372. * - UART will assert the TX DMA line once there is room for TX_TRIGGER
  373. * bytes in the TX FIFO. On each assert the DMA engine will move
  374. * TX_TRIGGER bytes into the FIFO.
  375. * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
  376. * the FIFO and move RX_TRIGGER bytes.
  377. * This is because threshold and trigger values are the same.
  378. */
  379. up->fcr = UART_FCR_ENABLE_FIFO;
  380. up->fcr |= TRIGGER_FCR_MASK(priv->tx_trigger) << OMAP_UART_FCR_TX_TRIG;
  381. up->fcr |= TRIGGER_FCR_MASK(priv->rx_trigger) << OMAP_UART_FCR_RX_TRIG;
  382. priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
  383. OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
  384. if (up->dma)
  385. priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
  386. OMAP_UART_SCR_DMAMODE_CTL;
  387. priv->xon = termios->c_cc[VSTART];
  388. priv->xoff = termios->c_cc[VSTOP];
  389. priv->efr = 0;
  390. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
  391. if (termios->c_cflag & CRTSCTS && port->flags & UPF_HARD_FLOW &&
  392. !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) &&
  393. !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_CTS)) {
  394. /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
  395. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  396. priv->efr |= UART_EFR_CTS;
  397. } else if (port->flags & UPF_SOFT_FLOW) {
  398. /*
  399. * OMAP rx s/w flow control is borked; the transmitter remains
  400. * stuck off even if rx flow control is subsequently disabled
  401. */
  402. /*
  403. * IXOFF Flag:
  404. * Enable XON/XOFF flow control on output.
  405. * Transmit XON1, XOFF1
  406. */
  407. if (termios->c_iflag & IXOFF) {
  408. port->status |= UPSTAT_AUTOXOFF;
  409. priv->efr |= OMAP_UART_SW_TX;
  410. }
  411. }
  412. omap8250_restore_regs(up);
  413. }
  414. /*
  415. * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
  416. * some differences in how we want to handle flow control.
  417. */
  418. static void omap_8250_set_termios(struct uart_port *port,
  419. struct ktermios *termios,
  420. const struct ktermios *old)
  421. {
  422. struct omap8250_priv *priv = port->private_data;
  423. unsigned int baud;
  424. /*
  425. * Ask the core to calculate the divisor for us.
  426. */
  427. baud = uart_get_baud_rate(port, termios, old,
  428. port->uartclk / 16 / UART_DIV_MAX,
  429. port->uartclk / 13);
  430. omap_8250_set_termios_atomic(port, termios, old, baud);
  431. /* calculate wakeup latency constraint */
  432. priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
  433. priv->latency = priv->calc_latency;
  434. schedule_work(&priv->qos_work);
  435. /* Don't rewrite B0 */
  436. if (tty_termios_baud_rate(termios))
  437. tty_termios_encode_baud_rate(termios, baud, baud);
  438. }
  439. /* same as 8250 except that we may have extra flow bits set in EFR */
  440. static void omap_8250_pm(struct uart_port *port, unsigned int state,
  441. unsigned int oldstate)
  442. {
  443. struct uart_8250_port *up = up_to_u8250p(port);
  444. u8 efr;
  445. guard(serial8250_rpm)(up);
  446. /* Synchronize UART_IER access against the console. */
  447. guard(uart_port_lock_irq)(port);
  448. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  449. efr = serial_in(up, UART_EFR);
  450. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  451. serial_out(up, UART_LCR, 0);
  452. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  453. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  454. serial_out(up, UART_EFR, efr);
  455. serial_out(up, UART_LCR, 0);
  456. }
  457. static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
  458. struct omap8250_priv *priv)
  459. {
  460. static const struct soc_device_attribute k3_soc_devices[] = {
  461. { .family = "AM65X", },
  462. { .family = "J721E", .revision = "SR1.0" },
  463. { /* sentinel */ }
  464. };
  465. u32 mvr, scheme;
  466. u16 revision, major, minor;
  467. mvr = uart_read(priv, UART_OMAP_MVER);
  468. /* Check revision register scheme */
  469. scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
  470. switch (scheme) {
  471. case 0: /* Legacy Scheme: OMAP2/3 */
  472. /* MINOR_REV[0:4], MAJOR_REV[4:7] */
  473. major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
  474. OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
  475. minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
  476. break;
  477. case 1:
  478. /* New Scheme: OMAP4+ */
  479. /* MINOR_REV[0:5], MAJOR_REV[8:10] */
  480. major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
  481. OMAP_UART_MVR_MAJ_SHIFT;
  482. minor = (mvr & OMAP_UART_MVR_MIN_MASK);
  483. break;
  484. default:
  485. dev_warn(up->port.dev,
  486. "Unknown revision, defaulting to highest\n");
  487. /* highest possible revision */
  488. major = 0xff;
  489. minor = 0xff;
  490. }
  491. /* normalize revision for the driver */
  492. revision = UART_BUILD_REVISION(major, minor);
  493. switch (revision) {
  494. case OMAP_UART_REV_46:
  495. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
  496. break;
  497. case OMAP_UART_REV_52:
  498. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
  499. OMAP_UART_WER_HAS_TX_WAKEUP;
  500. break;
  501. case OMAP_UART_REV_63:
  502. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
  503. OMAP_UART_WER_HAS_TX_WAKEUP;
  504. break;
  505. default:
  506. break;
  507. }
  508. /*
  509. * AM65x SR1.0, AM65x SR2.0 and J721e SR1.0 don't
  510. * don't have RHR_IT_DIS bit in IER2 register. So drop to flag
  511. * to enable errata workaround.
  512. */
  513. if (soc_device_match(k3_soc_devices))
  514. priv->habit &= ~UART_HAS_RHR_IT_DIS;
  515. }
  516. static void omap8250_uart_qos_work(struct work_struct *work)
  517. {
  518. struct omap8250_priv *priv;
  519. priv = container_of(work, struct omap8250_priv, qos_work);
  520. cpu_latency_qos_update_request(&priv->pm_qos_request, priv->latency);
  521. }
  522. #ifdef CONFIG_SERIAL_8250_DMA
  523. static int omap_8250_dma_handle_irq(struct uart_port *port);
  524. #endif
  525. static irqreturn_t omap8250_irq(int irq, void *dev_id)
  526. {
  527. struct omap8250_priv *priv = dev_id;
  528. struct uart_8250_port *up = serial8250_get_port(priv->line);
  529. struct uart_port *port = &up->port;
  530. unsigned int iir, lsr;
  531. int ret;
  532. pm_runtime_get_noresume(port->dev);
  533. /* Shallow idle state wake-up to an IO interrupt? */
  534. if (atomic_add_unless(&priv->active, 1, 1)) {
  535. priv->latency = priv->calc_latency;
  536. schedule_work(&priv->qos_work);
  537. }
  538. #ifdef CONFIG_SERIAL_8250_DMA
  539. if (up->dma) {
  540. ret = omap_8250_dma_handle_irq(port);
  541. pm_runtime_mark_last_busy(port->dev);
  542. pm_runtime_put(port->dev);
  543. return IRQ_RETVAL(ret);
  544. }
  545. #endif
  546. lsr = serial_port_in(port, UART_LSR);
  547. iir = serial_port_in(port, UART_IIR);
  548. ret = serial8250_handle_irq(port, iir);
  549. /*
  550. * On K3 SoCs, it is observed that RX TIMEOUT is signalled after
  551. * FIFO has been drained or erroneously.
  552. * So apply solution of Errata i2310 as mentioned in
  553. * https://www.ti.com/lit/pdf/sprz536
  554. */
  555. if (priv->habit & UART_RX_TIMEOUT_QUIRK &&
  556. (iir & UART_IIR_RX_TIMEOUT) == UART_IIR_RX_TIMEOUT &&
  557. serial_port_in(port, UART_OMAP_RX_LVL) == 0) {
  558. unsigned char efr2, timeout_h, timeout_l;
  559. efr2 = serial_in(up, UART_OMAP_EFR2);
  560. timeout_h = serial_in(up, UART_OMAP_TO_H);
  561. timeout_l = serial_in(up, UART_OMAP_TO_L);
  562. serial_out(up, UART_OMAP_TO_H, 0xFF);
  563. serial_out(up, UART_OMAP_TO_L, 0xFF);
  564. serial_out(up, UART_OMAP_EFR2, UART_OMAP_EFR2_TIMEOUT_BEHAVE);
  565. serial_in(up, UART_IIR);
  566. serial_out(up, UART_OMAP_EFR2, efr2);
  567. serial_out(up, UART_OMAP_TO_H, timeout_h);
  568. serial_out(up, UART_OMAP_TO_L, timeout_l);
  569. }
  570. /* Stop processing interrupts on input overrun */
  571. if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
  572. unsigned long delay;
  573. /* Synchronize UART_IER access against the console. */
  574. uart_port_lock(port);
  575. up->ier = serial_port_in(port, UART_IER);
  576. if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
  577. port->ops->stop_rx(port);
  578. } else {
  579. /* Keep restarting the timer until
  580. * the input overrun subsides.
  581. */
  582. cancel_delayed_work(&up->overrun_backoff);
  583. }
  584. uart_port_unlock(port);
  585. delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
  586. schedule_delayed_work(&up->overrun_backoff, delay);
  587. }
  588. pm_runtime_mark_last_busy(port->dev);
  589. pm_runtime_put(port->dev);
  590. return IRQ_RETVAL(ret);
  591. }
  592. static int omap_8250_startup(struct uart_port *port)
  593. {
  594. struct uart_8250_port *up = up_to_u8250p(port);
  595. struct omap8250_priv *priv = port->private_data;
  596. struct uart_8250_dma *dma = &priv->omap8250_dma;
  597. int ret;
  598. if (priv->wakeirq) {
  599. ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
  600. if (ret)
  601. return ret;
  602. }
  603. #ifdef CONFIG_PM
  604. up->capabilities |= UART_CAP_RPM;
  605. #endif
  606. guard(serial8250_rpm)(up);
  607. serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  608. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  609. up->lsr_saved_flags = 0;
  610. up->msr_saved_flags = 0;
  611. /* Disable DMA for console UART */
  612. if (dma->fn && !uart_console(port)) {
  613. up->dma = &priv->omap8250_dma;
  614. ret = serial8250_request_dma(up);
  615. if (ret) {
  616. dev_warn_ratelimited(port->dev,
  617. "failed to request DMA\n");
  618. up->dma = NULL;
  619. }
  620. } else {
  621. up->dma = NULL;
  622. }
  623. /* Synchronize UART_IER access against the console. */
  624. scoped_guard(uart_port_lock_irq, port) {
  625. up->ier = UART_IER_RLSI | UART_IER_RDI;
  626. serial_out(up, UART_IER, up->ier);
  627. }
  628. /* Enable module level wake up */
  629. priv->wer = OMAP_UART_WER_MOD_WKUP;
  630. if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
  631. priv->wer |= OMAP_UART_TX_WAKEUP_EN;
  632. serial_out(up, UART_OMAP_WER, priv->wer);
  633. if (up->dma && !(priv->habit & UART_HAS_EFR2)) {
  634. guard(uart_port_lock_irq)(port);
  635. up->dma->rx_dma(up);
  636. }
  637. enable_irq(port->irq);
  638. return 0;
  639. }
  640. static void omap_8250_shutdown(struct uart_port *port)
  641. {
  642. struct uart_8250_port *up = up_to_u8250p(port);
  643. struct omap8250_priv *priv = port->private_data;
  644. guard(serial8250_rpm)(up);
  645. flush_work(&priv->qos_work);
  646. if (up->dma)
  647. omap_8250_rx_dma_flush(up);
  648. serial_out(up, UART_OMAP_WER, 0);
  649. if (priv->habit & UART_HAS_EFR2)
  650. serial_out(up, UART_OMAP_EFR2, 0x0);
  651. /* Synchronize UART_IER access against the console. */
  652. scoped_guard(uart_port_lock_irq, port) {
  653. up->ier = 0;
  654. serial_out(up, UART_IER, 0);
  655. }
  656. disable_irq_nosync(port->irq);
  657. dev_pm_clear_wake_irq(port->dev);
  658. serial8250_release_dma(up);
  659. up->dma = NULL;
  660. /*
  661. * Disable break condition and FIFOs
  662. */
  663. if (up->lcr & UART_LCR_SBC)
  664. serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
  665. serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  666. }
  667. static void omap_8250_throttle(struct uart_port *port)
  668. {
  669. struct omap8250_priv *priv = port->private_data;
  670. guard(serial8250_rpm)(up_to_u8250p(port));
  671. guard(uart_port_lock_irqsave)(port);
  672. port->ops->stop_rx(port);
  673. priv->throttled = true;
  674. }
  675. static void omap_8250_unthrottle(struct uart_port *port)
  676. {
  677. struct omap8250_priv *priv = port->private_data;
  678. struct uart_8250_port *up = up_to_u8250p(port);
  679. guard(serial8250_rpm)(up);
  680. /* Synchronize UART_IER access against the console. */
  681. guard(uart_port_lock_irqsave)(port);
  682. priv->throttled = false;
  683. if (up->dma)
  684. up->dma->rx_dma(up);
  685. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  686. serial_out(up, UART_IER, up->ier);
  687. }
  688. static int omap8250_rs485_config(struct uart_port *port,
  689. struct ktermios *termios,
  690. struct serial_rs485 *rs485)
  691. {
  692. struct omap8250_priv *priv = port->private_data;
  693. struct uart_8250_port *up = up_to_u8250p(port);
  694. u32 fixed_delay_rts_before_send = 0;
  695. u32 fixed_delay_rts_after_send = 0;
  696. unsigned int baud;
  697. /*
  698. * There is a fixed delay of 3 bit clock cycles after the TX shift
  699. * register is going empty to allow time for the stop bit to transition
  700. * through the transceiver before direction is changed to receive.
  701. *
  702. * Additionally there appears to be a 1 bit clock delay between writing
  703. * to the THR register and transmission of the start bit, per page 8783
  704. * of the AM65 TRM: https://www.ti.com/lit/ug/spruid7e/spruid7e.pdf
  705. */
  706. if (priv->quot) {
  707. if (priv->mdr1 == UART_OMAP_MDR1_16X_MODE)
  708. baud = port->uartclk / (16 * priv->quot);
  709. else
  710. baud = port->uartclk / (13 * priv->quot);
  711. fixed_delay_rts_after_send = 3 * MSEC_PER_SEC / baud;
  712. fixed_delay_rts_before_send = 1 * MSEC_PER_SEC / baud;
  713. }
  714. /*
  715. * Fall back to RS485 software emulation if the UART is missing
  716. * hardware support, if the device tree specifies an mctrl_gpio
  717. * (indicates that RTS is unavailable due to a pinmux conflict)
  718. * or if the requested delays exceed the fixed hardware delays.
  719. */
  720. if (!(priv->habit & UART_HAS_NATIVE_RS485) ||
  721. mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) ||
  722. rs485->delay_rts_after_send > fixed_delay_rts_after_send ||
  723. rs485->delay_rts_before_send > fixed_delay_rts_before_send) {
  724. priv->mdr3 &= ~UART_OMAP_MDR3_DIR_EN;
  725. serial_out(up, UART_OMAP_MDR3, priv->mdr3);
  726. port->rs485_config = serial8250_em485_config;
  727. return serial8250_em485_config(port, termios, rs485);
  728. }
  729. rs485->delay_rts_after_send = fixed_delay_rts_after_send;
  730. rs485->delay_rts_before_send = fixed_delay_rts_before_send;
  731. if (rs485->flags & SER_RS485_ENABLED)
  732. priv->mdr3 |= UART_OMAP_MDR3_DIR_EN;
  733. else
  734. priv->mdr3 &= ~UART_OMAP_MDR3_DIR_EN;
  735. /*
  736. * Retain same polarity semantics as RS485 software emulation,
  737. * i.e. SER_RS485_RTS_ON_SEND means driving RTS low on send.
  738. */
  739. if (rs485->flags & SER_RS485_RTS_ON_SEND)
  740. priv->mdr3 &= ~UART_OMAP_MDR3_DIR_POL;
  741. else
  742. priv->mdr3 |= UART_OMAP_MDR3_DIR_POL;
  743. serial_out(up, UART_OMAP_MDR3, priv->mdr3);
  744. return 0;
  745. }
  746. #ifdef CONFIG_SERIAL_8250_DMA
  747. static int omap_8250_rx_dma(struct uart_8250_port *p);
  748. /* Must be called while priv->rx_dma_lock is held */
  749. static void __dma_rx_do_complete(struct uart_8250_port *p)
  750. {
  751. struct uart_8250_dma *dma = p->dma;
  752. struct tty_port *tty_port = &p->port.state->port;
  753. struct omap8250_priv *priv = p->port.private_data;
  754. struct dma_chan *rxchan = dma->rxchan;
  755. dma_cookie_t cookie;
  756. struct dma_tx_state state;
  757. int count;
  758. int ret;
  759. u32 reg;
  760. if (!dma->rx_running)
  761. goto out;
  762. cookie = dma->rx_cookie;
  763. /* Re-enable RX FIFO interrupt now that transfer is complete */
  764. if (priv->habit & UART_HAS_RHR_IT_DIS) {
  765. reg = serial_in(p, UART_OMAP_IER2);
  766. reg &= ~UART_OMAP_IER2_RHR_IT_DIS;
  767. serial_out(p, UART_OMAP_IER2, reg);
  768. }
  769. dmaengine_tx_status(rxchan, cookie, &state);
  770. count = dma->rx_size - state.residue + state.in_flight_bytes;
  771. if (count < dma->rx_size) {
  772. dmaengine_terminate_async(rxchan);
  773. /*
  774. * Poll for teardown to complete which guarantees in
  775. * flight data is drained.
  776. */
  777. if (state.in_flight_bytes) {
  778. int poll_count = 25;
  779. while (dmaengine_tx_status(rxchan, cookie, NULL) &&
  780. poll_count--)
  781. cpu_relax();
  782. if (poll_count == -1)
  783. dev_err(p->port.dev, "teardown incomplete\n");
  784. }
  785. }
  786. if (!count)
  787. goto out;
  788. ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
  789. dma->rx_running = 0;
  790. p->port.icount.rx += ret;
  791. p->port.icount.buf_overrun += count - ret;
  792. out:
  793. tty_flip_buffer_push(tty_port);
  794. }
  795. static void __dma_rx_complete(void *param)
  796. {
  797. struct uart_8250_port *p = param;
  798. struct omap8250_priv *priv = p->port.private_data;
  799. struct uart_8250_dma *dma = p->dma;
  800. struct dma_tx_state state;
  801. /* Synchronize UART_IER access against the console. */
  802. guard(uart_port_lock_irqsave)(&p->port);
  803. /*
  804. * If the tx status is not DMA_COMPLETE, then this is a delayed
  805. * completion callback. A previous RX timeout flush would have
  806. * already pushed the data, so exit.
  807. */
  808. if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) != DMA_COMPLETE)
  809. return;
  810. __dma_rx_do_complete(p);
  811. if (priv->throttled)
  812. return;
  813. p->ier |= UART_IER_RLSI | UART_IER_RDI;
  814. serial_out(p, UART_IER, p->ier);
  815. if (!(priv->habit & UART_HAS_EFR2))
  816. omap_8250_rx_dma(p);
  817. }
  818. static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
  819. {
  820. struct omap8250_priv *priv = p->port.private_data;
  821. struct uart_8250_dma *dma = p->dma;
  822. struct dma_tx_state state;
  823. unsigned long flags;
  824. int ret;
  825. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  826. if (!dma->rx_running) {
  827. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  828. return;
  829. }
  830. ret = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
  831. if (ret == DMA_IN_PROGRESS) {
  832. ret = dmaengine_pause(dma->rxchan);
  833. if (WARN_ON_ONCE(ret))
  834. priv->rx_dma_broken = true;
  835. }
  836. __dma_rx_do_complete(p);
  837. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  838. }
  839. static int omap_8250_rx_dma(struct uart_8250_port *p)
  840. {
  841. struct omap8250_priv *priv = p->port.private_data;
  842. struct uart_8250_dma *dma = p->dma;
  843. int err = 0;
  844. struct dma_async_tx_descriptor *desc;
  845. unsigned long flags;
  846. u32 reg;
  847. /* Port locked to synchronize UART_IER access against the console. */
  848. lockdep_assert_held_once(&p->port.lock);
  849. if (priv->rx_dma_broken)
  850. return -EINVAL;
  851. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  852. if (dma->rx_running) {
  853. enum dma_status state;
  854. state = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, NULL);
  855. if (state == DMA_COMPLETE) {
  856. /*
  857. * Disable RX interrupts to allow RX DMA completion
  858. * callback to run.
  859. */
  860. p->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  861. serial_out(p, UART_IER, p->ier);
  862. }
  863. goto out;
  864. }
  865. desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
  866. dma->rx_size, DMA_DEV_TO_MEM,
  867. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  868. if (!desc) {
  869. err = -EBUSY;
  870. goto out;
  871. }
  872. dma->rx_running = 1;
  873. desc->callback = __dma_rx_complete;
  874. desc->callback_param = p;
  875. dma->rx_cookie = dmaengine_submit(desc);
  876. /*
  877. * Disable RX FIFO interrupt while RX DMA is enabled, else
  878. * spurious interrupt may be raised when data is in the RX FIFO
  879. * but is yet to be drained by DMA.
  880. */
  881. if (priv->habit & UART_HAS_RHR_IT_DIS) {
  882. reg = serial_in(p, UART_OMAP_IER2);
  883. reg |= UART_OMAP_IER2_RHR_IT_DIS;
  884. serial_out(p, UART_OMAP_IER2, reg);
  885. }
  886. dma_async_issue_pending(dma->rxchan);
  887. out:
  888. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  889. return err;
  890. }
  891. static int omap_8250_tx_dma(struct uart_8250_port *p);
  892. static void omap_8250_dma_tx_complete(void *param)
  893. {
  894. struct uart_8250_port *p = param;
  895. struct uart_8250_dma *dma = p->dma;
  896. struct tty_port *tport = &p->port.state->port;
  897. bool en_thri = false;
  898. struct omap8250_priv *priv = p->port.private_data;
  899. dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
  900. UART_XMIT_SIZE, DMA_TO_DEVICE);
  901. guard(uart_port_lock_irqsave)(&p->port);
  902. dma->tx_running = 0;
  903. uart_xmit_advance(&p->port, dma->tx_size);
  904. if (priv->delayed_restore) {
  905. priv->delayed_restore = 0;
  906. omap8250_restore_regs(p);
  907. }
  908. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  909. uart_write_wakeup(&p->port);
  910. if (!kfifo_is_empty(&tport->xmit_fifo) && !uart_tx_stopped(&p->port)) {
  911. int ret;
  912. ret = omap_8250_tx_dma(p);
  913. if (ret)
  914. en_thri = true;
  915. } else if (p->capabilities & UART_CAP_RPM) {
  916. en_thri = true;
  917. }
  918. if (en_thri) {
  919. dma->tx_err = 1;
  920. serial8250_set_THRI(p);
  921. }
  922. }
  923. static int omap_8250_tx_dma(struct uart_8250_port *p)
  924. {
  925. struct uart_8250_dma *dma = p->dma;
  926. struct omap8250_priv *priv = p->port.private_data;
  927. struct tty_port *tport = &p->port.state->port;
  928. struct dma_async_tx_descriptor *desc;
  929. struct scatterlist sg;
  930. int skip_byte = -1;
  931. int ret;
  932. if (dma->tx_running)
  933. return 0;
  934. if (uart_tx_stopped(&p->port) || kfifo_is_empty(&tport->xmit_fifo)) {
  935. /*
  936. * Even if no data, we need to return an error for the two cases
  937. * below so serial8250_tx_chars() is invoked and properly clears
  938. * THRI and/or runtime suspend.
  939. */
  940. if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
  941. ret = -EBUSY;
  942. goto err;
  943. }
  944. serial8250_clear_THRI(p);
  945. return 0;
  946. }
  947. if (priv->habit & OMAP_DMA_TX_KICK) {
  948. unsigned char c;
  949. u8 tx_lvl;
  950. /*
  951. * We need to put the first byte into the FIFO in order to start
  952. * the DMA transfer. For transfers smaller than four bytes we
  953. * don't bother doing DMA at all. It seem not matter if there
  954. * are still bytes in the FIFO from the last transfer (in case
  955. * we got here directly from omap_8250_dma_tx_complete()). Bytes
  956. * leaving the FIFO seem not to trigger the DMA transfer. It is
  957. * really the byte that we put into the FIFO.
  958. * If the FIFO is already full then we most likely got here from
  959. * omap_8250_dma_tx_complete(). And this means the DMA engine
  960. * just completed its work. We don't have to wait the complete
  961. * 86us at 115200,8n1 but around 60us (not to mention lower
  962. * baudrates). So in that case we take the interrupt and try
  963. * again with an empty FIFO.
  964. */
  965. tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
  966. if (tx_lvl == p->tx_loadsz) {
  967. ret = -EBUSY;
  968. goto err;
  969. }
  970. if (kfifo_len(&tport->xmit_fifo) < 4) {
  971. ret = -EINVAL;
  972. goto err;
  973. }
  974. if (!uart_fifo_out(&p->port, &c, 1)) {
  975. ret = -EINVAL;
  976. goto err;
  977. }
  978. skip_byte = c;
  979. }
  980. sg_init_table(&sg, 1);
  981. ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1, UART_XMIT_SIZE, dma->tx_addr);
  982. if (ret != 1) {
  983. ret = -EINVAL;
  984. goto err;
  985. }
  986. desc = dmaengine_prep_slave_sg(dma->txchan, &sg, 1, DMA_MEM_TO_DEV,
  987. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  988. if (!desc) {
  989. ret = -EBUSY;
  990. goto err;
  991. }
  992. dma->tx_size = sg_dma_len(&sg);
  993. dma->tx_running = 1;
  994. desc->callback = omap_8250_dma_tx_complete;
  995. desc->callback_param = p;
  996. dma->tx_cookie = dmaengine_submit(desc);
  997. dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
  998. UART_XMIT_SIZE, DMA_TO_DEVICE);
  999. dma_async_issue_pending(dma->txchan);
  1000. if (dma->tx_err)
  1001. dma->tx_err = 0;
  1002. serial8250_clear_THRI(p);
  1003. ret = 0;
  1004. goto out_skip;
  1005. err:
  1006. dma->tx_err = 1;
  1007. out_skip:
  1008. if (skip_byte >= 0)
  1009. serial_out(p, UART_TX, skip_byte);
  1010. return ret;
  1011. }
  1012. static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
  1013. {
  1014. switch (iir & 0x3f) {
  1015. case UART_IIR_RLSI:
  1016. case UART_IIR_RX_TIMEOUT:
  1017. case UART_IIR_RDI:
  1018. omap_8250_rx_dma_flush(up);
  1019. return true;
  1020. }
  1021. return omap_8250_rx_dma(up);
  1022. }
  1023. static u16 omap_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir, u16 status)
  1024. {
  1025. if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
  1026. (iir & UART_IIR_RDI)) {
  1027. if (handle_rx_dma(up, iir)) {
  1028. status = serial8250_rx_chars(up, status);
  1029. omap_8250_rx_dma(up);
  1030. }
  1031. }
  1032. return status;
  1033. }
  1034. static void am654_8250_handle_uart_errors(struct uart_8250_port *up, u8 iir, u16 status)
  1035. {
  1036. if (status & UART_LSR_OE) {
  1037. serial8250_clear_and_reinit_fifos(up);
  1038. serial_in(up, UART_LSR);
  1039. serial_in(up, UART_OMAP_RESUME);
  1040. } else {
  1041. if (status & (UART_LSR_FE | UART_LSR_PE | UART_LSR_BI))
  1042. serial_in(up, UART_RX);
  1043. if (iir & UART_IIR_XOFF)
  1044. serial_in(up, UART_IIR);
  1045. }
  1046. }
  1047. static void am654_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir,
  1048. u16 status)
  1049. {
  1050. /* Port locked to synchronize UART_IER access against the console. */
  1051. lockdep_assert_held_once(&up->port.lock);
  1052. /*
  1053. * Queue a new transfer if FIFO has data.
  1054. */
  1055. if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
  1056. (up->ier & UART_IER_RDI) && !(status & UART_LSR_OE)) {
  1057. am654_8250_handle_uart_errors(up, iir, status);
  1058. omap_8250_rx_dma(up);
  1059. serial_out(up, UART_OMAP_EFR2, UART_OMAP_EFR2_TIMEOUT_BEHAVE);
  1060. } else if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
  1061. /*
  1062. * Disable RX timeout, read IIR to clear
  1063. * current timeout condition, clear EFR2 to
  1064. * periodic timeouts, re-enable interrupts.
  1065. */
  1066. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  1067. serial_out(up, UART_IER, up->ier);
  1068. omap_8250_rx_dma_flush(up);
  1069. serial_in(up, UART_IIR);
  1070. serial_out(up, UART_OMAP_EFR2, 0x0);
  1071. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  1072. serial_out(up, UART_IER, up->ier);
  1073. } else {
  1074. am654_8250_handle_uart_errors(up, iir, status);
  1075. }
  1076. }
  1077. /*
  1078. * This is mostly serial8250_handle_irq(). We have a slightly different DMA
  1079. * hook for RX/TX and need different logic for them in the ISR. Therefore we
  1080. * use the default routine in the non-DMA case and this one for with DMA.
  1081. */
  1082. static int omap_8250_dma_handle_irq(struct uart_port *port)
  1083. {
  1084. struct uart_8250_port *up = up_to_u8250p(port);
  1085. struct omap8250_priv *priv = port->private_data;
  1086. u16 status;
  1087. u8 iir;
  1088. iir = serial_port_in(port, UART_IIR);
  1089. if (iir & UART_IIR_NO_INT) {
  1090. return IRQ_HANDLED;
  1091. }
  1092. uart_port_lock(port);
  1093. status = serial_port_in(port, UART_LSR);
  1094. if ((iir & 0x3f) != UART_IIR_THRI) {
  1095. if (priv->habit & UART_HAS_EFR2)
  1096. am654_8250_handle_rx_dma(up, iir, status);
  1097. else
  1098. status = omap_8250_handle_rx_dma(up, iir, status);
  1099. }
  1100. serial8250_modem_status(up);
  1101. if (status & UART_LSR_THRE && up->dma->tx_err) {
  1102. if (uart_tx_stopped(port) ||
  1103. kfifo_is_empty(&port->state->port.xmit_fifo)) {
  1104. up->dma->tx_err = 0;
  1105. serial8250_tx_chars(up);
  1106. } else {
  1107. /*
  1108. * try again due to an earlier failure which
  1109. * might have been resolved by now.
  1110. */
  1111. if (omap_8250_tx_dma(up))
  1112. serial8250_tx_chars(up);
  1113. }
  1114. }
  1115. uart_unlock_and_check_sysrq(port);
  1116. return 1;
  1117. }
  1118. static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
  1119. {
  1120. return false;
  1121. }
  1122. #else
  1123. static inline int omap_8250_rx_dma(struct uart_8250_port *p)
  1124. {
  1125. return -EINVAL;
  1126. }
  1127. #endif
  1128. static int omap8250_no_handle_irq(struct uart_port *port)
  1129. {
  1130. /* IRQ has not been requested but handling irq? */
  1131. WARN_ONCE(1, "Unexpected irq handling before port startup\n");
  1132. return 0;
  1133. }
  1134. static int omap8250_select_wakeup_pinctrl(struct device *dev,
  1135. struct omap8250_priv *priv)
  1136. {
  1137. if (IS_ERR_OR_NULL(priv->pinctrl_wakeup))
  1138. return 0;
  1139. if (!device_may_wakeup(dev))
  1140. return 0;
  1141. device_set_out_band_wakeup(dev);
  1142. return pinctrl_select_state(priv->pinctrl, priv->pinctrl_wakeup);
  1143. }
  1144. static struct omap8250_dma_params am654_dma = {
  1145. .rx_size = SZ_2K,
  1146. .rx_trigger = 1,
  1147. .tx_trigger = TX_TRIGGER,
  1148. };
  1149. static struct omap8250_dma_params am33xx_dma = {
  1150. .rx_size = RX_TRIGGER,
  1151. .rx_trigger = RX_TRIGGER,
  1152. .tx_trigger = TX_TRIGGER,
  1153. };
  1154. static struct omap8250_platdata am654_platdata = {
  1155. .dma_params = &am654_dma,
  1156. .habit = UART_HAS_EFR2 | UART_HAS_RHR_IT_DIS |
  1157. UART_RX_TIMEOUT_QUIRK | UART_HAS_NATIVE_RS485,
  1158. };
  1159. static struct omap8250_platdata am33xx_platdata = {
  1160. .dma_params = &am33xx_dma,
  1161. .habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE,
  1162. };
  1163. static struct omap8250_platdata omap4_platdata = {
  1164. .dma_params = &am33xx_dma,
  1165. .habit = UART_ERRATA_CLOCK_DISABLE,
  1166. };
  1167. static const struct of_device_id omap8250_dt_ids[] = {
  1168. { .compatible = "ti,am654-uart", .data = &am654_platdata, },
  1169. { .compatible = "ti,omap2-uart" },
  1170. { .compatible = "ti,omap3-uart" },
  1171. { .compatible = "ti,omap4-uart", .data = &omap4_platdata, },
  1172. { .compatible = "ti,am3352-uart", .data = &am33xx_platdata, },
  1173. { .compatible = "ti,am4372-uart", .data = &am33xx_platdata, },
  1174. { .compatible = "ti,dra742-uart", .data = &omap4_platdata, },
  1175. {},
  1176. };
  1177. MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
  1178. static int omap8250_probe(struct platform_device *pdev)
  1179. {
  1180. struct device_node *np = pdev->dev.of_node;
  1181. struct omap8250_priv *priv;
  1182. const struct omap8250_platdata *pdata;
  1183. struct uart_8250_port up;
  1184. struct resource *regs;
  1185. void __iomem *membase;
  1186. int ret;
  1187. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1188. if (!regs) {
  1189. dev_err(&pdev->dev, "missing registers\n");
  1190. return -EINVAL;
  1191. }
  1192. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  1193. if (!priv)
  1194. return -ENOMEM;
  1195. membase = devm_ioremap(&pdev->dev, regs->start,
  1196. resource_size(regs));
  1197. if (!membase)
  1198. return -ENODEV;
  1199. memset(&up, 0, sizeof(up));
  1200. up.port.dev = &pdev->dev;
  1201. up.port.mapbase = regs->start;
  1202. up.port.membase = membase;
  1203. /*
  1204. * It claims to be 16C750 compatible however it is a little different.
  1205. * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
  1206. * have) is enabled via EFR instead of MCR. The type is set here 8250
  1207. * just to get things going. UNKNOWN does not work for a few reasons and
  1208. * we don't need our own type since we don't use 8250's set_termios()
  1209. * or pm callback.
  1210. */
  1211. up.port.type = PORT_8250;
  1212. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW | UPF_HARD_FLOW;
  1213. up.port.private_data = priv;
  1214. up.tx_loadsz = 64;
  1215. up.capabilities = UART_CAP_FIFO;
  1216. #ifdef CONFIG_PM
  1217. /*
  1218. * Runtime PM is mostly transparent. However to do it right we need to a
  1219. * TX empty interrupt before we can put the device to auto idle. So if
  1220. * PM is not enabled we don't add that flag and can spare that one extra
  1221. * interrupt in the TX path.
  1222. */
  1223. up.capabilities |= UART_CAP_RPM;
  1224. #endif
  1225. up.port.set_termios = omap_8250_set_termios;
  1226. up.port.set_mctrl = omap8250_set_mctrl;
  1227. up.port.pm = omap_8250_pm;
  1228. up.port.startup = omap_8250_startup;
  1229. up.port.shutdown = omap_8250_shutdown;
  1230. up.port.throttle = omap_8250_throttle;
  1231. up.port.unthrottle = omap_8250_unthrottle;
  1232. up.port.rs485_config = omap8250_rs485_config;
  1233. /* same rs485_supported for software emulation and native RS485 */
  1234. up.port.rs485_supported = serial8250_em485_supported;
  1235. up.rs485_start_tx = serial8250_em485_start_tx;
  1236. up.rs485_stop_tx = serial8250_em485_stop_tx;
  1237. up.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
  1238. ret = uart_read_port_properties(&up.port);
  1239. if (ret)
  1240. return ret;
  1241. up.port.regshift = OMAP_UART_REGSHIFT;
  1242. up.port.fifosize = 64;
  1243. if (!up.port.uartclk) {
  1244. struct clk *clk;
  1245. clk = devm_clk_get(&pdev->dev, NULL);
  1246. if (IS_ERR(clk)) {
  1247. if (PTR_ERR(clk) == -EPROBE_DEFER)
  1248. return -EPROBE_DEFER;
  1249. } else {
  1250. up.port.uartclk = clk_get_rate(clk);
  1251. }
  1252. }
  1253. if (of_property_read_u32(np, "overrun-throttle-ms",
  1254. &up.overrun_backoff_time_ms) != 0)
  1255. up.overrun_backoff_time_ms = 0;
  1256. pdata = of_device_get_match_data(&pdev->dev);
  1257. if (pdata)
  1258. priv->habit |= pdata->habit;
  1259. if (!up.port.uartclk) {
  1260. up.port.uartclk = DEFAULT_CLK_SPEED;
  1261. dev_warn(&pdev->dev,
  1262. "No clock speed specified: using default: %d\n",
  1263. DEFAULT_CLK_SPEED);
  1264. }
  1265. priv->membase = membase;
  1266. priv->line = -ENODEV;
  1267. priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
  1268. priv->calc_latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
  1269. cpu_latency_qos_add_request(&priv->pm_qos_request, priv->latency);
  1270. INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
  1271. spin_lock_init(&priv->rx_dma_lock);
  1272. platform_set_drvdata(pdev, priv);
  1273. device_set_wakeup_capable(&pdev->dev, true);
  1274. if (of_property_read_bool(np, "wakeup-source"))
  1275. device_set_wakeup_enable(&pdev->dev, true);
  1276. pm_runtime_enable(&pdev->dev);
  1277. pm_runtime_use_autosuspend(&pdev->dev);
  1278. /*
  1279. * Disable runtime PM until autosuspend delay unless specifically
  1280. * enabled by the user via sysfs. This is the historic way to
  1281. * prevent an unsafe default policy with lossy characters on wake-up.
  1282. * For serdev devices this is not needed, the policy can be managed by
  1283. * the serdev driver.
  1284. */
  1285. if (!of_get_available_child_count(pdev->dev.of_node))
  1286. pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
  1287. pm_runtime_get_sync(&pdev->dev);
  1288. omap_serial_fill_features_erratas(&up, priv);
  1289. up.port.handle_irq = omap8250_no_handle_irq;
  1290. priv->rx_trigger = RX_TRIGGER;
  1291. priv->tx_trigger = TX_TRIGGER;
  1292. #ifdef CONFIG_SERIAL_8250_DMA
  1293. /*
  1294. * Oh DMA support. If there are no DMA properties in the DT then
  1295. * we will fall back to a generic DMA channel which does not
  1296. * really work here. To ensure that we do not get a generic DMA
  1297. * channel assigned, we have the the_no_dma_filter_fn() here.
  1298. * To avoid "failed to request DMA" messages we check for DMA
  1299. * properties in DT.
  1300. */
  1301. ret = of_property_count_strings(np, "dma-names");
  1302. if (ret == 2) {
  1303. struct omap8250_dma_params *dma_params = NULL;
  1304. struct uart_8250_dma *dma = &priv->omap8250_dma;
  1305. dma->fn = the_no_dma_filter_fn;
  1306. dma->tx_dma = omap_8250_tx_dma;
  1307. dma->rx_dma = omap_8250_rx_dma;
  1308. if (pdata)
  1309. dma_params = pdata->dma_params;
  1310. if (dma_params) {
  1311. dma->rx_size = dma_params->rx_size;
  1312. dma->rxconf.src_maxburst = dma_params->rx_trigger;
  1313. dma->txconf.dst_maxburst = dma_params->tx_trigger;
  1314. priv->rx_trigger = dma_params->rx_trigger;
  1315. priv->tx_trigger = dma_params->tx_trigger;
  1316. } else {
  1317. dma->rx_size = RX_TRIGGER;
  1318. dma->rxconf.src_maxburst = RX_TRIGGER;
  1319. dma->txconf.dst_maxburst = TX_TRIGGER;
  1320. }
  1321. }
  1322. #endif
  1323. irq_set_status_flags(up.port.irq, IRQ_NOAUTOEN);
  1324. ret = devm_request_irq(&pdev->dev, up.port.irq, omap8250_irq, 0,
  1325. dev_name(&pdev->dev), priv);
  1326. if (ret < 0)
  1327. goto err;
  1328. priv->wakeirq = irq_of_parse_and_map(np, 1);
  1329. ret = serial8250_register_8250_port(&up);
  1330. if (ret < 0) {
  1331. dev_err(&pdev->dev, "unable to register 8250 port\n");
  1332. goto err;
  1333. }
  1334. priv->line = ret;
  1335. pm_runtime_mark_last_busy(&pdev->dev);
  1336. pm_runtime_put_autosuspend(&pdev->dev);
  1337. priv->pinctrl = devm_pinctrl_get(&pdev->dev);
  1338. if (!IS_ERR_OR_NULL(priv->pinctrl))
  1339. priv->pinctrl_wakeup = pinctrl_lookup_state(priv->pinctrl, "wakeup");
  1340. return 0;
  1341. err:
  1342. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1343. pm_runtime_put_sync(&pdev->dev);
  1344. flush_work(&priv->qos_work);
  1345. pm_runtime_disable(&pdev->dev);
  1346. cpu_latency_qos_remove_request(&priv->pm_qos_request);
  1347. return ret;
  1348. }
  1349. static void omap8250_remove(struct platform_device *pdev)
  1350. {
  1351. struct omap8250_priv *priv = platform_get_drvdata(pdev);
  1352. struct uart_8250_port *up;
  1353. int err;
  1354. err = pm_runtime_resume_and_get(&pdev->dev);
  1355. if (err)
  1356. dev_err(&pdev->dev, "Failed to resume hardware\n");
  1357. up = serial8250_get_port(priv->line);
  1358. omap_8250_shutdown(&up->port);
  1359. serial8250_unregister_port(priv->line);
  1360. priv->line = -ENODEV;
  1361. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1362. pm_runtime_put_sync(&pdev->dev);
  1363. flush_work(&priv->qos_work);
  1364. pm_runtime_disable(&pdev->dev);
  1365. cpu_latency_qos_remove_request(&priv->pm_qos_request);
  1366. device_set_wakeup_capable(&pdev->dev, false);
  1367. }
  1368. static int omap8250_prepare(struct device *dev)
  1369. {
  1370. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1371. if (!priv)
  1372. return 0;
  1373. priv->is_suspending = true;
  1374. return 0;
  1375. }
  1376. static void omap8250_complete(struct device *dev)
  1377. {
  1378. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1379. if (!priv)
  1380. return;
  1381. priv->is_suspending = false;
  1382. }
  1383. static int omap8250_suspend(struct device *dev)
  1384. {
  1385. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1386. struct uart_8250_port *up = serial8250_get_port(priv->line);
  1387. int err = 0;
  1388. err = omap8250_select_wakeup_pinctrl(dev, priv);
  1389. if (err) {
  1390. dev_err(dev, "Failed to select wakeup pinctrl, aborting suspend %pe\n",
  1391. ERR_PTR(err));
  1392. return err;
  1393. }
  1394. serial8250_suspend_port(priv->line);
  1395. err = pm_runtime_resume_and_get(dev);
  1396. if (err)
  1397. return err;
  1398. if (!device_may_wakeup(dev))
  1399. priv->wer = 0;
  1400. serial_out(up, UART_OMAP_WER, priv->wer);
  1401. if (uart_console(&up->port) && console_suspend_enabled)
  1402. err = pm_runtime_force_suspend(dev);
  1403. flush_work(&priv->qos_work);
  1404. return err;
  1405. }
  1406. static int omap8250_resume(struct device *dev)
  1407. {
  1408. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1409. struct uart_8250_port *up = serial8250_get_port(priv->line);
  1410. int err;
  1411. err = pinctrl_select_default_state(dev);
  1412. if (err) {
  1413. dev_err(dev, "Failed to select default pinctrl state on resume: %pe\n",
  1414. ERR_PTR(err));
  1415. return err;
  1416. }
  1417. if (uart_console(&up->port) && console_suspend_enabled) {
  1418. err = pm_runtime_force_resume(dev);
  1419. if (err)
  1420. return err;
  1421. }
  1422. serial8250_resume_port(priv->line);
  1423. /* Paired with pm_runtime_resume_and_get() in omap8250_suspend() */
  1424. pm_runtime_mark_last_busy(dev);
  1425. pm_runtime_put_autosuspend(dev);
  1426. return 0;
  1427. }
  1428. static int omap8250_lost_context(struct uart_8250_port *up)
  1429. {
  1430. u32 val;
  1431. val = serial_in(up, UART_OMAP_SCR);
  1432. /*
  1433. * If we lose context, then SCR is set to its reset value of zero.
  1434. * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
  1435. * among other bits, to never set the register back to zero again.
  1436. */
  1437. if (!val)
  1438. return 1;
  1439. return 0;
  1440. }
  1441. static void uart_write(struct omap8250_priv *priv, u32 reg, u32 val)
  1442. {
  1443. writel(val, priv->membase + (reg << OMAP_UART_REGSHIFT));
  1444. }
  1445. /* TODO: in future, this should happen via API in drivers/reset/ */
  1446. static int omap8250_soft_reset(struct device *dev)
  1447. {
  1448. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1449. int timeout = 100;
  1450. int sysc;
  1451. int syss;
  1452. /*
  1453. * At least on omap4, unused uarts may not idle after reset without
  1454. * a basic scr dma configuration even with no dma in use. The
  1455. * module clkctrl status bits will be 1 instead of 3 blocking idle
  1456. * for the whole clockdomain. The softreset below will clear scr,
  1457. * and we restore it on resume so this is safe to do on all SoCs
  1458. * needing omap8250_soft_reset() quirk. Do it in two writes as
  1459. * recommended in the comment for omap8250_update_scr().
  1460. */
  1461. uart_write(priv, UART_OMAP_SCR, OMAP_UART_SCR_DMAMODE_1);
  1462. uart_write(priv, UART_OMAP_SCR,
  1463. OMAP_UART_SCR_DMAMODE_1 | OMAP_UART_SCR_DMAMODE_CTL);
  1464. sysc = uart_read(priv, UART_OMAP_SYSC);
  1465. /* softreset the UART */
  1466. sysc |= OMAP_UART_SYSC_SOFTRESET;
  1467. uart_write(priv, UART_OMAP_SYSC, sysc);
  1468. /* By experiments, 1us enough for reset complete on AM335x */
  1469. do {
  1470. udelay(1);
  1471. syss = uart_read(priv, UART_OMAP_SYSS);
  1472. } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
  1473. if (!timeout) {
  1474. dev_err(dev, "timed out waiting for reset done\n");
  1475. return -ETIMEDOUT;
  1476. }
  1477. return 0;
  1478. }
  1479. static int omap8250_runtime_suspend(struct device *dev)
  1480. {
  1481. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1482. struct uart_8250_port *up = NULL;
  1483. if (priv->line >= 0)
  1484. up = serial8250_get_port(priv->line);
  1485. if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
  1486. int ret;
  1487. ret = omap8250_soft_reset(dev);
  1488. if (ret)
  1489. return ret;
  1490. if (up) {
  1491. /* Restore to UART mode after reset (for wakeup) */
  1492. omap8250_update_mdr1(up, priv);
  1493. /* Restore wakeup enable register */
  1494. serial_out(up, UART_OMAP_WER, priv->wer);
  1495. }
  1496. }
  1497. if (up && up->dma && up->dma->rxchan)
  1498. omap_8250_rx_dma_flush(up);
  1499. priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
  1500. schedule_work(&priv->qos_work);
  1501. atomic_set(&priv->active, 0);
  1502. return 0;
  1503. }
  1504. static int omap8250_runtime_resume(struct device *dev)
  1505. {
  1506. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1507. struct uart_8250_port *up = NULL;
  1508. /* Did the hardware wake to a device IO interrupt before a wakeirq? */
  1509. if (atomic_read(&priv->active))
  1510. return 0;
  1511. if (priv->line >= 0)
  1512. up = serial8250_get_port(priv->line);
  1513. if (up && omap8250_lost_context(up)) {
  1514. guard(uart_port_lock_irq)(&up->port);
  1515. omap8250_restore_regs(up);
  1516. }
  1517. if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2)) {
  1518. guard(uart_port_lock_irq)(&up->port);
  1519. omap_8250_rx_dma(up);
  1520. }
  1521. atomic_set(&priv->active, 1);
  1522. priv->latency = priv->calc_latency;
  1523. schedule_work(&priv->qos_work);
  1524. return 0;
  1525. }
  1526. #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
  1527. static int __init omap8250_console_fixup(void)
  1528. {
  1529. char *omap_str;
  1530. char *options;
  1531. u8 idx;
  1532. if (strstr(boot_command_line, "console=ttyS"))
  1533. /* user set a ttyS based name for the console */
  1534. return 0;
  1535. omap_str = strstr(boot_command_line, "console=ttyO");
  1536. if (!omap_str)
  1537. /* user did not set ttyO based console, so we don't care */
  1538. return 0;
  1539. omap_str += 12;
  1540. if ('0' <= *omap_str && *omap_str <= '9')
  1541. idx = *omap_str - '0';
  1542. else
  1543. return 0;
  1544. omap_str++;
  1545. if (omap_str[0] == ',') {
  1546. omap_str++;
  1547. options = omap_str;
  1548. } else {
  1549. options = NULL;
  1550. }
  1551. add_preferred_console("ttyS", idx, options);
  1552. pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
  1553. idx, idx);
  1554. pr_err("This ensures that you still see kernel messages. Please\n");
  1555. pr_err("update your kernel commandline.\n");
  1556. return 0;
  1557. }
  1558. console_initcall(omap8250_console_fixup);
  1559. #endif
  1560. static const struct dev_pm_ops omap8250_dev_pm_ops = {
  1561. SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
  1562. RUNTIME_PM_OPS(omap8250_runtime_suspend,
  1563. omap8250_runtime_resume, NULL)
  1564. .prepare = pm_sleep_ptr(omap8250_prepare),
  1565. .complete = pm_sleep_ptr(omap8250_complete),
  1566. };
  1567. static struct platform_driver omap8250_platform_driver = {
  1568. .driver = {
  1569. .name = "omap8250",
  1570. .pm = pm_ptr(&omap8250_dev_pm_ops),
  1571. .of_match_table = omap8250_dt_ids,
  1572. },
  1573. .probe = omap8250_probe,
  1574. .remove = omap8250_remove,
  1575. };
  1576. module_platform_driver(omap8250_platform_driver);
  1577. MODULE_AUTHOR("Sebastian Andrzej Siewior");
  1578. MODULE_DESCRIPTION("OMAP 8250 Driver");
  1579. MODULE_LICENSE("GPL v2");