8250_omap.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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/device.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/serial_8250.h>
  14. #include <linux/serial_reg.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_gpio.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 "8250.h"
  29. #define DEFAULT_CLK_SPEED 48000000
  30. #define UART_ERRATA_i202_MDR1_ACCESS (1 << 0)
  31. #define OMAP_UART_WER_HAS_TX_WAKEUP (1 << 1)
  32. #define OMAP_DMA_TX_KICK (1 << 2)
  33. /*
  34. * See Advisory 21 in AM437x errata SPRZ408B, updated April 2015.
  35. * The same errata is applicable to AM335x and DRA7x processors too.
  36. */
  37. #define UART_ERRATA_CLOCK_DISABLE (1 << 3)
  38. #define OMAP_UART_FCR_RX_TRIG 6
  39. #define OMAP_UART_FCR_TX_TRIG 4
  40. /* SCR register bitmasks */
  41. #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
  42. #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
  43. #define OMAP_UART_SCR_TX_EMPTY (1 << 3)
  44. #define OMAP_UART_SCR_DMAMODE_MASK (3 << 1)
  45. #define OMAP_UART_SCR_DMAMODE_1 (1 << 1)
  46. #define OMAP_UART_SCR_DMAMODE_CTL (1 << 0)
  47. /* MVR register bitmasks */
  48. #define OMAP_UART_MVR_SCHEME_SHIFT 30
  49. #define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
  50. #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
  51. #define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
  52. #define OMAP_UART_MVR_MAJ_MASK 0x700
  53. #define OMAP_UART_MVR_MAJ_SHIFT 8
  54. #define OMAP_UART_MVR_MIN_MASK 0x3f
  55. /* SYSC register bitmasks */
  56. #define OMAP_UART_SYSC_SOFTRESET (1 << 1)
  57. /* SYSS register bitmasks */
  58. #define OMAP_UART_SYSS_RESETDONE (1 << 0)
  59. #define UART_TI752_TLR_TX 0
  60. #define UART_TI752_TLR_RX 4
  61. #define TRIGGER_TLR_MASK(x) ((x & 0x3c) >> 2)
  62. #define TRIGGER_FCR_MASK(x) (x & 3)
  63. /* Enable XON/XOFF flow control on output */
  64. #define OMAP_UART_SW_TX 0x08
  65. /* Enable XON/XOFF flow control on input */
  66. #define OMAP_UART_SW_RX 0x02
  67. #define OMAP_UART_WER_MOD_WKUP 0x7f
  68. #define OMAP_UART_TX_WAKEUP_EN (1 << 7)
  69. #define TX_TRIGGER 1
  70. #define RX_TRIGGER 48
  71. #define OMAP_UART_TCR_RESTORE(x) ((x / 4) << 4)
  72. #define OMAP_UART_TCR_HALT(x) ((x / 4) << 0)
  73. #define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
  74. #define OMAP_UART_REV_46 0x0406
  75. #define OMAP_UART_REV_52 0x0502
  76. #define OMAP_UART_REV_63 0x0603
  77. struct omap8250_priv {
  78. int line;
  79. u8 habit;
  80. u8 mdr1;
  81. u8 efr;
  82. u8 scr;
  83. u8 wer;
  84. u8 xon;
  85. u8 xoff;
  86. u8 delayed_restore;
  87. u16 quot;
  88. bool is_suspending;
  89. int wakeirq;
  90. int wakeups_enabled;
  91. u32 latency;
  92. u32 calc_latency;
  93. struct pm_qos_request pm_qos_request;
  94. struct work_struct qos_work;
  95. struct uart_8250_dma omap8250_dma;
  96. spinlock_t rx_dma_lock;
  97. bool rx_dma_broken;
  98. bool throttled;
  99. };
  100. #ifdef CONFIG_SERIAL_8250_DMA
  101. static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
  102. #else
  103. static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
  104. #endif
  105. static u32 uart_read(struct uart_8250_port *up, u32 reg)
  106. {
  107. return readl(up->port.membase + (reg << up->port.regshift));
  108. }
  109. static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  110. {
  111. struct uart_8250_port *up = up_to_u8250p(port);
  112. struct omap8250_priv *priv = up->port.private_data;
  113. u8 lcr;
  114. serial8250_do_set_mctrl(port, mctrl);
  115. /*
  116. * Turn off autoRTS if RTS is lowered and restore autoRTS setting
  117. * if RTS is raised
  118. */
  119. lcr = serial_in(up, UART_LCR);
  120. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  121. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  122. priv->efr |= UART_EFR_RTS;
  123. else
  124. priv->efr &= ~UART_EFR_RTS;
  125. serial_out(up, UART_EFR, priv->efr);
  126. serial_out(up, UART_LCR, lcr);
  127. }
  128. /*
  129. * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
  130. * The access to uart register after MDR1 Access
  131. * causes UART to corrupt data.
  132. *
  133. * Need a delay =
  134. * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
  135. * give 10 times as much
  136. */
  137. static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
  138. struct omap8250_priv *priv)
  139. {
  140. u8 timeout = 255;
  141. u8 old_mdr1;
  142. old_mdr1 = serial_in(up, UART_OMAP_MDR1);
  143. if (old_mdr1 == priv->mdr1)
  144. return;
  145. serial_out(up, UART_OMAP_MDR1, priv->mdr1);
  146. udelay(2);
  147. serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
  148. UART_FCR_CLEAR_RCVR);
  149. /*
  150. * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
  151. * TX_FIFO_E bit is 1.
  152. */
  153. while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
  154. (UART_LSR_THRE | UART_LSR_DR))) {
  155. timeout--;
  156. if (!timeout) {
  157. /* Should *never* happen. we warn and carry on */
  158. dev_crit(up->port.dev, "Errata i202: timedout %x\n",
  159. serial_in(up, UART_LSR));
  160. break;
  161. }
  162. udelay(1);
  163. }
  164. }
  165. static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
  166. struct omap8250_priv *priv)
  167. {
  168. unsigned int uartclk = port->uartclk;
  169. unsigned int div_13, div_16;
  170. unsigned int abs_d13, abs_d16;
  171. /*
  172. * Old custom speed handling.
  173. */
  174. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
  175. priv->quot = port->custom_divisor & UART_DIV_MAX;
  176. /*
  177. * I assume that nobody is using this. But hey, if somebody
  178. * would like to specify the divisor _and_ the mode then the
  179. * driver is ready and waiting for it.
  180. */
  181. if (port->custom_divisor & (1 << 16))
  182. priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
  183. else
  184. priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
  185. return;
  186. }
  187. div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
  188. div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
  189. if (!div_13)
  190. div_13 = 1;
  191. if (!div_16)
  192. div_16 = 1;
  193. abs_d13 = abs(baud - uartclk / 13 / div_13);
  194. abs_d16 = abs(baud - uartclk / 16 / div_16);
  195. if (abs_d13 >= abs_d16) {
  196. priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
  197. priv->quot = div_16;
  198. } else {
  199. priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
  200. priv->quot = div_13;
  201. }
  202. }
  203. static void omap8250_update_scr(struct uart_8250_port *up,
  204. struct omap8250_priv *priv)
  205. {
  206. u8 old_scr;
  207. old_scr = serial_in(up, UART_OMAP_SCR);
  208. if (old_scr == priv->scr)
  209. return;
  210. /*
  211. * The manual recommends not to enable the DMA mode selector in the SCR
  212. * (instead of the FCR) register _and_ selecting the DMA mode as one
  213. * register write because this may lead to malfunction.
  214. */
  215. if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
  216. serial_out(up, UART_OMAP_SCR,
  217. priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
  218. serial_out(up, UART_OMAP_SCR, priv->scr);
  219. }
  220. static void omap8250_update_mdr1(struct uart_8250_port *up,
  221. struct omap8250_priv *priv)
  222. {
  223. if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
  224. omap_8250_mdr1_errataset(up, priv);
  225. else
  226. serial_out(up, UART_OMAP_MDR1, priv->mdr1);
  227. }
  228. static void omap8250_restore_regs(struct uart_8250_port *up)
  229. {
  230. struct omap8250_priv *priv = up->port.private_data;
  231. struct uart_8250_dma *dma = up->dma;
  232. if (dma && dma->tx_running) {
  233. /*
  234. * TCSANOW requests the change to occur immediately however if
  235. * we have a TX-DMA operation in progress then it has been
  236. * observed that it might stall and never complete. Therefore we
  237. * delay DMA completes to prevent this hang from happen.
  238. */
  239. priv->delayed_restore = 1;
  240. return;
  241. }
  242. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  243. serial_out(up, UART_EFR, UART_EFR_ECB);
  244. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  245. serial8250_out_MCR(up, UART_MCR_TCRTLR);
  246. serial_out(up, UART_FCR, up->fcr);
  247. omap8250_update_scr(up, priv);
  248. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  249. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
  250. OMAP_UART_TCR_HALT(52));
  251. serial_out(up, UART_TI752_TLR,
  252. TRIGGER_TLR_MASK(TX_TRIGGER) << UART_TI752_TLR_TX |
  253. TRIGGER_TLR_MASK(RX_TRIGGER) << UART_TI752_TLR_RX);
  254. serial_out(up, UART_LCR, 0);
  255. /* drop TCR + TLR access, we setup XON/XOFF later */
  256. serial8250_out_MCR(up, up->mcr);
  257. serial_out(up, UART_IER, up->ier);
  258. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  259. serial_dl_write(up, priv->quot);
  260. serial_out(up, UART_EFR, priv->efr);
  261. /* Configure flow control */
  262. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  263. serial_out(up, UART_XON1, priv->xon);
  264. serial_out(up, UART_XOFF1, priv->xoff);
  265. serial_out(up, UART_LCR, up->lcr);
  266. omap8250_update_mdr1(up, priv);
  267. up->port.ops->set_mctrl(&up->port, up->port.mctrl);
  268. }
  269. /*
  270. * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
  271. * some differences in how we want to handle flow control.
  272. */
  273. static void omap_8250_set_termios(struct uart_port *port,
  274. struct ktermios *termios,
  275. struct ktermios *old)
  276. {
  277. struct uart_8250_port *up = up_to_u8250p(port);
  278. struct omap8250_priv *priv = up->port.private_data;
  279. unsigned char cval = 0;
  280. unsigned int baud;
  281. switch (termios->c_cflag & CSIZE) {
  282. case CS5:
  283. cval = UART_LCR_WLEN5;
  284. break;
  285. case CS6:
  286. cval = UART_LCR_WLEN6;
  287. break;
  288. case CS7:
  289. cval = UART_LCR_WLEN7;
  290. break;
  291. default:
  292. case CS8:
  293. cval = UART_LCR_WLEN8;
  294. break;
  295. }
  296. if (termios->c_cflag & CSTOPB)
  297. cval |= UART_LCR_STOP;
  298. if (termios->c_cflag & PARENB)
  299. cval |= UART_LCR_PARITY;
  300. if (!(termios->c_cflag & PARODD))
  301. cval |= UART_LCR_EPAR;
  302. if (termios->c_cflag & CMSPAR)
  303. cval |= UART_LCR_SPAR;
  304. /*
  305. * Ask the core to calculate the divisor for us.
  306. */
  307. baud = uart_get_baud_rate(port, termios, old,
  308. port->uartclk / 16 / UART_DIV_MAX,
  309. port->uartclk / 13);
  310. omap_8250_get_divisor(port, baud, priv);
  311. /*
  312. * Ok, we're now changing the port state. Do it with
  313. * interrupts disabled.
  314. */
  315. pm_runtime_get_sync(port->dev);
  316. spin_lock_irq(&port->lock);
  317. /*
  318. * Update the per-port timeout.
  319. */
  320. uart_update_timeout(port, termios->c_cflag, baud);
  321. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  322. if (termios->c_iflag & INPCK)
  323. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  324. if (termios->c_iflag & (IGNBRK | PARMRK))
  325. up->port.read_status_mask |= UART_LSR_BI;
  326. /*
  327. * Characters to ignore
  328. */
  329. up->port.ignore_status_mask = 0;
  330. if (termios->c_iflag & IGNPAR)
  331. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  332. if (termios->c_iflag & IGNBRK) {
  333. up->port.ignore_status_mask |= UART_LSR_BI;
  334. /*
  335. * If we're ignoring parity and break indicators,
  336. * ignore overruns too (for real raw support).
  337. */
  338. if (termios->c_iflag & IGNPAR)
  339. up->port.ignore_status_mask |= UART_LSR_OE;
  340. }
  341. /*
  342. * ignore all characters if CREAD is not set
  343. */
  344. if ((termios->c_cflag & CREAD) == 0)
  345. up->port.ignore_status_mask |= UART_LSR_DR;
  346. /*
  347. * Modem status interrupts
  348. */
  349. up->ier &= ~UART_IER_MSI;
  350. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  351. up->ier |= UART_IER_MSI;
  352. up->lcr = cval;
  353. /* Up to here it was mostly serial8250_do_set_termios() */
  354. /*
  355. * We enable TRIG_GRANU for RX and TX and additionally we set
  356. * SCR_TX_EMPTY bit. The result is the following:
  357. * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
  358. * - less than RX_TRIGGER number of bytes will also cause an interrupt
  359. * once the UART decides that there no new bytes arriving.
  360. * - Once THRE is enabled, the interrupt will be fired once the FIFO is
  361. * empty - the trigger level is ignored here.
  362. *
  363. * Once DMA is enabled:
  364. * - UART will assert the TX DMA line once there is room for TX_TRIGGER
  365. * bytes in the TX FIFO. On each assert the DMA engine will move
  366. * TX_TRIGGER bytes into the FIFO.
  367. * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
  368. * the FIFO and move RX_TRIGGER bytes.
  369. * This is because threshold and trigger values are the same.
  370. */
  371. up->fcr = UART_FCR_ENABLE_FIFO;
  372. up->fcr |= TRIGGER_FCR_MASK(TX_TRIGGER) << OMAP_UART_FCR_TX_TRIG;
  373. up->fcr |= TRIGGER_FCR_MASK(RX_TRIGGER) << OMAP_UART_FCR_RX_TRIG;
  374. priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
  375. OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
  376. if (up->dma)
  377. priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
  378. OMAP_UART_SCR_DMAMODE_CTL;
  379. priv->xon = termios->c_cc[VSTART];
  380. priv->xoff = termios->c_cc[VSTOP];
  381. priv->efr = 0;
  382. up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
  383. if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
  384. /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
  385. up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  386. priv->efr |= UART_EFR_CTS;
  387. } else if (up->port.flags & UPF_SOFT_FLOW) {
  388. /*
  389. * OMAP rx s/w flow control is borked; the transmitter remains
  390. * stuck off even if rx flow control is subsequently disabled
  391. */
  392. /*
  393. * IXOFF Flag:
  394. * Enable XON/XOFF flow control on output.
  395. * Transmit XON1, XOFF1
  396. */
  397. if (termios->c_iflag & IXOFF) {
  398. up->port.status |= UPSTAT_AUTOXOFF;
  399. priv->efr |= OMAP_UART_SW_TX;
  400. }
  401. }
  402. omap8250_restore_regs(up);
  403. spin_unlock_irq(&up->port.lock);
  404. pm_runtime_mark_last_busy(port->dev);
  405. pm_runtime_put_autosuspend(port->dev);
  406. /* calculate wakeup latency constraint */
  407. priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
  408. priv->latency = priv->calc_latency;
  409. schedule_work(&priv->qos_work);
  410. /* Don't rewrite B0 */
  411. if (tty_termios_baud_rate(termios))
  412. tty_termios_encode_baud_rate(termios, baud, baud);
  413. }
  414. /* same as 8250 except that we may have extra flow bits set in EFR */
  415. static void omap_8250_pm(struct uart_port *port, unsigned int state,
  416. unsigned int oldstate)
  417. {
  418. struct uart_8250_port *up = up_to_u8250p(port);
  419. u8 efr;
  420. pm_runtime_get_sync(port->dev);
  421. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  422. efr = serial_in(up, UART_EFR);
  423. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  424. serial_out(up, UART_LCR, 0);
  425. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  426. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  427. serial_out(up, UART_EFR, efr);
  428. serial_out(up, UART_LCR, 0);
  429. pm_runtime_mark_last_busy(port->dev);
  430. pm_runtime_put_autosuspend(port->dev);
  431. }
  432. static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
  433. struct omap8250_priv *priv)
  434. {
  435. u32 mvr, scheme;
  436. u16 revision, major, minor;
  437. mvr = uart_read(up, UART_OMAP_MVER);
  438. /* Check revision register scheme */
  439. scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
  440. switch (scheme) {
  441. case 0: /* Legacy Scheme: OMAP2/3 */
  442. /* MINOR_REV[0:4], MAJOR_REV[4:7] */
  443. major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
  444. OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
  445. minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
  446. break;
  447. case 1:
  448. /* New Scheme: OMAP4+ */
  449. /* MINOR_REV[0:5], MAJOR_REV[8:10] */
  450. major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
  451. OMAP_UART_MVR_MAJ_SHIFT;
  452. minor = (mvr & OMAP_UART_MVR_MIN_MASK);
  453. break;
  454. default:
  455. dev_warn(up->port.dev,
  456. "Unknown revision, defaulting to highest\n");
  457. /* highest possible revision */
  458. major = 0xff;
  459. minor = 0xff;
  460. }
  461. /* normalize revision for the driver */
  462. revision = UART_BUILD_REVISION(major, minor);
  463. switch (revision) {
  464. case OMAP_UART_REV_46:
  465. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
  466. break;
  467. case OMAP_UART_REV_52:
  468. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
  469. OMAP_UART_WER_HAS_TX_WAKEUP;
  470. break;
  471. case OMAP_UART_REV_63:
  472. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
  473. OMAP_UART_WER_HAS_TX_WAKEUP;
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. static void omap8250_uart_qos_work(struct work_struct *work)
  480. {
  481. struct omap8250_priv *priv;
  482. priv = container_of(work, struct omap8250_priv, qos_work);
  483. pm_qos_update_request(&priv->pm_qos_request, priv->latency);
  484. }
  485. #ifdef CONFIG_SERIAL_8250_DMA
  486. static int omap_8250_dma_handle_irq(struct uart_port *port);
  487. #endif
  488. static irqreturn_t omap8250_irq(int irq, void *dev_id)
  489. {
  490. struct uart_port *port = dev_id;
  491. struct uart_8250_port *up = up_to_u8250p(port);
  492. unsigned int iir;
  493. int ret;
  494. #ifdef CONFIG_SERIAL_8250_DMA
  495. if (up->dma) {
  496. ret = omap_8250_dma_handle_irq(port);
  497. return IRQ_RETVAL(ret);
  498. }
  499. #endif
  500. serial8250_rpm_get(up);
  501. iir = serial_port_in(port, UART_IIR);
  502. ret = serial8250_handle_irq(port, iir);
  503. serial8250_rpm_put(up);
  504. return IRQ_RETVAL(ret);
  505. }
  506. static int omap_8250_startup(struct uart_port *port)
  507. {
  508. struct uart_8250_port *up = up_to_u8250p(port);
  509. struct omap8250_priv *priv = port->private_data;
  510. int ret;
  511. if (priv->wakeirq) {
  512. ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
  513. if (ret)
  514. return ret;
  515. }
  516. pm_runtime_get_sync(port->dev);
  517. up->mcr = 0;
  518. serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  519. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  520. up->lsr_saved_flags = 0;
  521. up->msr_saved_flags = 0;
  522. /* Disable DMA for console UART */
  523. if (uart_console(port))
  524. up->dma = NULL;
  525. if (up->dma) {
  526. ret = serial8250_request_dma(up);
  527. if (ret) {
  528. dev_warn_ratelimited(port->dev,
  529. "failed to request DMA\n");
  530. up->dma = NULL;
  531. }
  532. }
  533. ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
  534. dev_name(port->dev), port);
  535. if (ret < 0)
  536. goto err;
  537. up->ier = UART_IER_RLSI | UART_IER_RDI;
  538. serial_out(up, UART_IER, up->ier);
  539. #ifdef CONFIG_PM
  540. up->capabilities |= UART_CAP_RPM;
  541. #endif
  542. /* Enable module level wake up */
  543. priv->wer = OMAP_UART_WER_MOD_WKUP;
  544. if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
  545. priv->wer |= OMAP_UART_TX_WAKEUP_EN;
  546. serial_out(up, UART_OMAP_WER, priv->wer);
  547. if (up->dma)
  548. up->dma->rx_dma(up);
  549. pm_runtime_mark_last_busy(port->dev);
  550. pm_runtime_put_autosuspend(port->dev);
  551. return 0;
  552. err:
  553. pm_runtime_mark_last_busy(port->dev);
  554. pm_runtime_put_autosuspend(port->dev);
  555. dev_pm_clear_wake_irq(port->dev);
  556. return ret;
  557. }
  558. static void omap_8250_shutdown(struct uart_port *port)
  559. {
  560. struct uart_8250_port *up = up_to_u8250p(port);
  561. struct omap8250_priv *priv = port->private_data;
  562. flush_work(&priv->qos_work);
  563. if (up->dma)
  564. omap_8250_rx_dma_flush(up);
  565. pm_runtime_get_sync(port->dev);
  566. serial_out(up, UART_OMAP_WER, 0);
  567. up->ier = 0;
  568. serial_out(up, UART_IER, 0);
  569. if (up->dma)
  570. serial8250_release_dma(up);
  571. /*
  572. * Disable break condition and FIFOs
  573. */
  574. if (up->lcr & UART_LCR_SBC)
  575. serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
  576. serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  577. pm_runtime_mark_last_busy(port->dev);
  578. pm_runtime_put_autosuspend(port->dev);
  579. free_irq(port->irq, port);
  580. dev_pm_clear_wake_irq(port->dev);
  581. }
  582. static void omap_8250_throttle(struct uart_port *port)
  583. {
  584. struct omap8250_priv *priv = port->private_data;
  585. struct uart_8250_port *up = up_to_u8250p(port);
  586. unsigned long flags;
  587. pm_runtime_get_sync(port->dev);
  588. spin_lock_irqsave(&port->lock, flags);
  589. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  590. serial_out(up, UART_IER, up->ier);
  591. priv->throttled = true;
  592. spin_unlock_irqrestore(&port->lock, flags);
  593. pm_runtime_mark_last_busy(port->dev);
  594. pm_runtime_put_autosuspend(port->dev);
  595. }
  596. static int omap_8250_rs485_config(struct uart_port *port,
  597. struct serial_rs485 *rs485)
  598. {
  599. struct uart_8250_port *up = up_to_u8250p(port);
  600. /* Clamp the delays to [0, 100ms] */
  601. rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
  602. rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U);
  603. port->rs485 = *rs485;
  604. /*
  605. * Both serial8250_em485_init and serial8250_em485_destroy
  606. * are idempotent
  607. */
  608. if (rs485->flags & SER_RS485_ENABLED) {
  609. int ret = serial8250_em485_init(up);
  610. if (ret) {
  611. rs485->flags &= ~SER_RS485_ENABLED;
  612. port->rs485.flags &= ~SER_RS485_ENABLED;
  613. }
  614. return ret;
  615. }
  616. serial8250_em485_destroy(up);
  617. return 0;
  618. }
  619. static void omap_8250_unthrottle(struct uart_port *port)
  620. {
  621. struct omap8250_priv *priv = port->private_data;
  622. struct uart_8250_port *up = up_to_u8250p(port);
  623. unsigned long flags;
  624. pm_runtime_get_sync(port->dev);
  625. spin_lock_irqsave(&port->lock, flags);
  626. priv->throttled = false;
  627. if (up->dma)
  628. up->dma->rx_dma(up);
  629. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  630. serial_out(up, UART_IER, up->ier);
  631. spin_unlock_irqrestore(&port->lock, flags);
  632. pm_runtime_mark_last_busy(port->dev);
  633. pm_runtime_put_autosuspend(port->dev);
  634. }
  635. #ifdef CONFIG_SERIAL_8250_DMA
  636. static int omap_8250_rx_dma(struct uart_8250_port *p);
  637. static void __dma_rx_do_complete(struct uart_8250_port *p)
  638. {
  639. struct omap8250_priv *priv = p->port.private_data;
  640. struct uart_8250_dma *dma = p->dma;
  641. struct tty_port *tty_port = &p->port.state->port;
  642. struct dma_tx_state state;
  643. int count;
  644. unsigned long flags;
  645. int ret;
  646. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  647. if (!dma->rx_running)
  648. goto unlock;
  649. dma->rx_running = 0;
  650. dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
  651. count = dma->rx_size - state.residue;
  652. ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
  653. p->port.icount.rx += ret;
  654. p->port.icount.buf_overrun += count - ret;
  655. unlock:
  656. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  657. tty_flip_buffer_push(tty_port);
  658. }
  659. static void __dma_rx_complete(void *param)
  660. {
  661. struct uart_8250_port *p = param;
  662. struct omap8250_priv *priv = p->port.private_data;
  663. struct uart_8250_dma *dma = p->dma;
  664. struct dma_tx_state state;
  665. unsigned long flags;
  666. spin_lock_irqsave(&p->port.lock, flags);
  667. /*
  668. * If the tx status is not DMA_COMPLETE, then this is a delayed
  669. * completion callback. A previous RX timeout flush would have
  670. * already pushed the data, so exit.
  671. */
  672. if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) !=
  673. DMA_COMPLETE) {
  674. spin_unlock_irqrestore(&p->port.lock, flags);
  675. return;
  676. }
  677. __dma_rx_do_complete(p);
  678. if (!priv->throttled)
  679. omap_8250_rx_dma(p);
  680. spin_unlock_irqrestore(&p->port.lock, flags);
  681. }
  682. static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
  683. {
  684. struct omap8250_priv *priv = p->port.private_data;
  685. struct uart_8250_dma *dma = p->dma;
  686. struct dma_tx_state state;
  687. unsigned long flags;
  688. int ret;
  689. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  690. if (!dma->rx_running) {
  691. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  692. return;
  693. }
  694. ret = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
  695. if (ret == DMA_IN_PROGRESS) {
  696. ret = dmaengine_pause(dma->rxchan);
  697. if (WARN_ON_ONCE(ret))
  698. priv->rx_dma_broken = true;
  699. }
  700. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  701. __dma_rx_do_complete(p);
  702. dmaengine_terminate_all(dma->rxchan);
  703. }
  704. static int omap_8250_rx_dma(struct uart_8250_port *p)
  705. {
  706. struct omap8250_priv *priv = p->port.private_data;
  707. struct uart_8250_dma *dma = p->dma;
  708. int err = 0;
  709. struct dma_async_tx_descriptor *desc;
  710. unsigned long flags;
  711. if (priv->rx_dma_broken)
  712. return -EINVAL;
  713. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  714. if (dma->rx_running)
  715. goto out;
  716. desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
  717. dma->rx_size, DMA_DEV_TO_MEM,
  718. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  719. if (!desc) {
  720. err = -EBUSY;
  721. goto out;
  722. }
  723. dma->rx_running = 1;
  724. desc->callback = __dma_rx_complete;
  725. desc->callback_param = p;
  726. dma->rx_cookie = dmaengine_submit(desc);
  727. dma_async_issue_pending(dma->rxchan);
  728. out:
  729. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  730. return err;
  731. }
  732. static int omap_8250_tx_dma(struct uart_8250_port *p);
  733. static void omap_8250_dma_tx_complete(void *param)
  734. {
  735. struct uart_8250_port *p = param;
  736. struct uart_8250_dma *dma = p->dma;
  737. struct circ_buf *xmit = &p->port.state->xmit;
  738. unsigned long flags;
  739. bool en_thri = false;
  740. struct omap8250_priv *priv = p->port.private_data;
  741. dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
  742. UART_XMIT_SIZE, DMA_TO_DEVICE);
  743. spin_lock_irqsave(&p->port.lock, flags);
  744. dma->tx_running = 0;
  745. xmit->tail += dma->tx_size;
  746. xmit->tail &= UART_XMIT_SIZE - 1;
  747. p->port.icount.tx += dma->tx_size;
  748. if (priv->delayed_restore) {
  749. priv->delayed_restore = 0;
  750. omap8250_restore_regs(p);
  751. }
  752. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  753. uart_write_wakeup(&p->port);
  754. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
  755. int ret;
  756. ret = omap_8250_tx_dma(p);
  757. if (ret)
  758. en_thri = true;
  759. } else if (p->capabilities & UART_CAP_RPM) {
  760. en_thri = true;
  761. }
  762. if (en_thri) {
  763. dma->tx_err = 1;
  764. p->ier |= UART_IER_THRI;
  765. serial_port_out(&p->port, UART_IER, p->ier);
  766. }
  767. spin_unlock_irqrestore(&p->port.lock, flags);
  768. }
  769. static int omap_8250_tx_dma(struct uart_8250_port *p)
  770. {
  771. struct uart_8250_dma *dma = p->dma;
  772. struct omap8250_priv *priv = p->port.private_data;
  773. struct circ_buf *xmit = &p->port.state->xmit;
  774. struct dma_async_tx_descriptor *desc;
  775. unsigned int skip_byte = 0;
  776. int ret;
  777. if (dma->tx_running)
  778. return 0;
  779. if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
  780. /*
  781. * Even if no data, we need to return an error for the two cases
  782. * below so serial8250_tx_chars() is invoked and properly clears
  783. * THRI and/or runtime suspend.
  784. */
  785. if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
  786. ret = -EBUSY;
  787. goto err;
  788. }
  789. if (p->ier & UART_IER_THRI) {
  790. p->ier &= ~UART_IER_THRI;
  791. serial_out(p, UART_IER, p->ier);
  792. }
  793. return 0;
  794. }
  795. dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  796. if (priv->habit & OMAP_DMA_TX_KICK) {
  797. u8 tx_lvl;
  798. /*
  799. * We need to put the first byte into the FIFO in order to start
  800. * the DMA transfer. For transfers smaller than four bytes we
  801. * don't bother doing DMA at all. It seem not matter if there
  802. * are still bytes in the FIFO from the last transfer (in case
  803. * we got here directly from omap_8250_dma_tx_complete()). Bytes
  804. * leaving the FIFO seem not to trigger the DMA transfer. It is
  805. * really the byte that we put into the FIFO.
  806. * If the FIFO is already full then we most likely got here from
  807. * omap_8250_dma_tx_complete(). And this means the DMA engine
  808. * just completed its work. We don't have to wait the complete
  809. * 86us at 115200,8n1 but around 60us (not to mention lower
  810. * baudrates). So in that case we take the interrupt and try
  811. * again with an empty FIFO.
  812. */
  813. tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
  814. if (tx_lvl == p->tx_loadsz) {
  815. ret = -EBUSY;
  816. goto err;
  817. }
  818. if (dma->tx_size < 4) {
  819. ret = -EINVAL;
  820. goto err;
  821. }
  822. skip_byte = 1;
  823. }
  824. desc = dmaengine_prep_slave_single(dma->txchan,
  825. dma->tx_addr + xmit->tail + skip_byte,
  826. dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
  827. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  828. if (!desc) {
  829. ret = -EBUSY;
  830. goto err;
  831. }
  832. dma->tx_running = 1;
  833. desc->callback = omap_8250_dma_tx_complete;
  834. desc->callback_param = p;
  835. dma->tx_cookie = dmaengine_submit(desc);
  836. dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
  837. UART_XMIT_SIZE, DMA_TO_DEVICE);
  838. dma_async_issue_pending(dma->txchan);
  839. if (dma->tx_err)
  840. dma->tx_err = 0;
  841. if (p->ier & UART_IER_THRI) {
  842. p->ier &= ~UART_IER_THRI;
  843. serial_out(p, UART_IER, p->ier);
  844. }
  845. if (skip_byte)
  846. serial_out(p, UART_TX, xmit->buf[xmit->tail]);
  847. return 0;
  848. err:
  849. dma->tx_err = 1;
  850. return ret;
  851. }
  852. static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
  853. {
  854. switch (iir & 0x3f) {
  855. case UART_IIR_RLSI:
  856. case UART_IIR_RX_TIMEOUT:
  857. case UART_IIR_RDI:
  858. omap_8250_rx_dma_flush(up);
  859. return true;
  860. }
  861. return omap_8250_rx_dma(up);
  862. }
  863. /*
  864. * This is mostly serial8250_handle_irq(). We have a slightly different DMA
  865. * hoook for RX/TX and need different logic for them in the ISR. Therefore we
  866. * use the default routine in the non-DMA case and this one for with DMA.
  867. */
  868. static int omap_8250_dma_handle_irq(struct uart_port *port)
  869. {
  870. struct uart_8250_port *up = up_to_u8250p(port);
  871. unsigned char status;
  872. unsigned long flags;
  873. u8 iir;
  874. serial8250_rpm_get(up);
  875. iir = serial_port_in(port, UART_IIR);
  876. if (iir & UART_IIR_NO_INT) {
  877. serial8250_rpm_put(up);
  878. return 0;
  879. }
  880. spin_lock_irqsave(&port->lock, flags);
  881. status = serial_port_in(port, UART_LSR);
  882. if (status & (UART_LSR_DR | UART_LSR_BI)) {
  883. if (handle_rx_dma(up, iir)) {
  884. status = serial8250_rx_chars(up, status);
  885. omap_8250_rx_dma(up);
  886. }
  887. }
  888. serial8250_modem_status(up);
  889. if (status & UART_LSR_THRE && up->dma->tx_err) {
  890. if (uart_tx_stopped(&up->port) ||
  891. uart_circ_empty(&up->port.state->xmit)) {
  892. up->dma->tx_err = 0;
  893. serial8250_tx_chars(up);
  894. } else {
  895. /*
  896. * try again due to an earlier failer which
  897. * might have been resolved by now.
  898. */
  899. if (omap_8250_tx_dma(up))
  900. serial8250_tx_chars(up);
  901. }
  902. }
  903. spin_unlock_irqrestore(&port->lock, flags);
  904. serial8250_rpm_put(up);
  905. return 1;
  906. }
  907. static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
  908. {
  909. return false;
  910. }
  911. #else
  912. static inline int omap_8250_rx_dma(struct uart_8250_port *p)
  913. {
  914. return -EINVAL;
  915. }
  916. #endif
  917. static int omap8250_no_handle_irq(struct uart_port *port)
  918. {
  919. /* IRQ has not been requested but handling irq? */
  920. WARN_ONCE(1, "Unexpected irq handling before port startup\n");
  921. return 0;
  922. }
  923. static const u8 am3352_habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE;
  924. static const u8 dra742_habit = UART_ERRATA_CLOCK_DISABLE;
  925. static const struct of_device_id omap8250_dt_ids[] = {
  926. { .compatible = "ti,omap2-uart" },
  927. { .compatible = "ti,omap3-uart" },
  928. { .compatible = "ti,omap4-uart" },
  929. { .compatible = "ti,am3352-uart", .data = &am3352_habit, },
  930. { .compatible = "ti,am4372-uart", .data = &am3352_habit, },
  931. { .compatible = "ti,dra742-uart", .data = &dra742_habit, },
  932. {},
  933. };
  934. MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
  935. static int omap8250_probe(struct platform_device *pdev)
  936. {
  937. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  938. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  939. struct omap8250_priv *priv;
  940. struct uart_8250_port up;
  941. int ret;
  942. void __iomem *membase;
  943. if (!regs || !irq) {
  944. dev_err(&pdev->dev, "missing registers or irq\n");
  945. return -EINVAL;
  946. }
  947. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  948. if (!priv)
  949. return -ENOMEM;
  950. membase = devm_ioremap_nocache(&pdev->dev, regs->start,
  951. resource_size(regs));
  952. if (!membase)
  953. return -ENODEV;
  954. memset(&up, 0, sizeof(up));
  955. up.port.dev = &pdev->dev;
  956. up.port.mapbase = regs->start;
  957. up.port.membase = membase;
  958. up.port.irq = irq->start;
  959. /*
  960. * It claims to be 16C750 compatible however it is a little different.
  961. * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
  962. * have) is enabled via EFR instead of MCR. The type is set here 8250
  963. * just to get things going. UNKNOWN does not work for a few reasons and
  964. * we don't need our own type since we don't use 8250's set_termios()
  965. * or pm callback.
  966. */
  967. up.port.type = PORT_8250;
  968. up.port.iotype = UPIO_MEM;
  969. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
  970. UPF_HARD_FLOW;
  971. up.port.private_data = priv;
  972. up.port.regshift = 2;
  973. up.port.fifosize = 64;
  974. up.tx_loadsz = 64;
  975. up.capabilities = UART_CAP_FIFO;
  976. #ifdef CONFIG_PM
  977. /*
  978. * Runtime PM is mostly transparent. However to do it right we need to a
  979. * TX empty interrupt before we can put the device to auto idle. So if
  980. * PM is not enabled we don't add that flag and can spare that one extra
  981. * interrupt in the TX path.
  982. */
  983. up.capabilities |= UART_CAP_RPM;
  984. #endif
  985. up.port.set_termios = omap_8250_set_termios;
  986. up.port.set_mctrl = omap8250_set_mctrl;
  987. up.port.pm = omap_8250_pm;
  988. up.port.startup = omap_8250_startup;
  989. up.port.shutdown = omap_8250_shutdown;
  990. up.port.throttle = omap_8250_throttle;
  991. up.port.unthrottle = omap_8250_unthrottle;
  992. up.port.rs485_config = omap_8250_rs485_config;
  993. if (pdev->dev.of_node) {
  994. const struct of_device_id *id;
  995. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  996. of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  997. &up.port.uartclk);
  998. priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  999. id = of_match_device(of_match_ptr(omap8250_dt_ids), &pdev->dev);
  1000. if (id && id->data)
  1001. priv->habit |= *(u8 *)id->data;
  1002. } else {
  1003. ret = pdev->id;
  1004. }
  1005. if (ret < 0) {
  1006. dev_err(&pdev->dev, "failed to get alias/pdev id\n");
  1007. return ret;
  1008. }
  1009. up.port.line = ret;
  1010. if (!up.port.uartclk) {
  1011. up.port.uartclk = DEFAULT_CLK_SPEED;
  1012. dev_warn(&pdev->dev,
  1013. "No clock speed specified: using default: %d\n",
  1014. DEFAULT_CLK_SPEED);
  1015. }
  1016. priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1017. priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1018. pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
  1019. priv->latency);
  1020. INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
  1021. spin_lock_init(&priv->rx_dma_lock);
  1022. device_init_wakeup(&pdev->dev, true);
  1023. pm_runtime_use_autosuspend(&pdev->dev);
  1024. pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
  1025. pm_runtime_irq_safe(&pdev->dev);
  1026. pm_runtime_enable(&pdev->dev);
  1027. pm_runtime_get_sync(&pdev->dev);
  1028. omap_serial_fill_features_erratas(&up, priv);
  1029. up.port.handle_irq = omap8250_no_handle_irq;
  1030. #ifdef CONFIG_SERIAL_8250_DMA
  1031. if (pdev->dev.of_node) {
  1032. /*
  1033. * Oh DMA support. If there are no DMA properties in the DT then
  1034. * we will fall back to a generic DMA channel which does not
  1035. * really work here. To ensure that we do not get a generic DMA
  1036. * channel assigned, we have the the_no_dma_filter_fn() here.
  1037. * To avoid "failed to request DMA" messages we check for DMA
  1038. * properties in DT.
  1039. */
  1040. ret = of_property_count_strings(pdev->dev.of_node, "dma-names");
  1041. if (ret == 2) {
  1042. up.dma = &priv->omap8250_dma;
  1043. priv->omap8250_dma.fn = the_no_dma_filter_fn;
  1044. priv->omap8250_dma.tx_dma = omap_8250_tx_dma;
  1045. priv->omap8250_dma.rx_dma = omap_8250_rx_dma;
  1046. priv->omap8250_dma.rx_size = RX_TRIGGER;
  1047. priv->omap8250_dma.rxconf.src_maxburst = RX_TRIGGER;
  1048. priv->omap8250_dma.txconf.dst_maxburst = TX_TRIGGER;
  1049. }
  1050. }
  1051. #endif
  1052. ret = serial8250_register_8250_port(&up);
  1053. if (ret < 0) {
  1054. dev_err(&pdev->dev, "unable to register 8250 port\n");
  1055. goto err;
  1056. }
  1057. priv->line = ret;
  1058. platform_set_drvdata(pdev, priv);
  1059. pm_runtime_mark_last_busy(&pdev->dev);
  1060. pm_runtime_put_autosuspend(&pdev->dev);
  1061. return 0;
  1062. err:
  1063. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1064. pm_runtime_put_sync(&pdev->dev);
  1065. pm_runtime_disable(&pdev->dev);
  1066. return ret;
  1067. }
  1068. static int omap8250_remove(struct platform_device *pdev)
  1069. {
  1070. struct omap8250_priv *priv = platform_get_drvdata(pdev);
  1071. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1072. pm_runtime_put_sync(&pdev->dev);
  1073. pm_runtime_disable(&pdev->dev);
  1074. serial8250_unregister_port(priv->line);
  1075. pm_qos_remove_request(&priv->pm_qos_request);
  1076. device_init_wakeup(&pdev->dev, false);
  1077. return 0;
  1078. }
  1079. #ifdef CONFIG_PM_SLEEP
  1080. static int omap8250_prepare(struct device *dev)
  1081. {
  1082. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1083. if (!priv)
  1084. return 0;
  1085. priv->is_suspending = true;
  1086. return 0;
  1087. }
  1088. static void omap8250_complete(struct device *dev)
  1089. {
  1090. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1091. if (!priv)
  1092. return;
  1093. priv->is_suspending = false;
  1094. }
  1095. static int omap8250_suspend(struct device *dev)
  1096. {
  1097. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1098. serial8250_suspend_port(priv->line);
  1099. flush_work(&priv->qos_work);
  1100. return 0;
  1101. }
  1102. static int omap8250_resume(struct device *dev)
  1103. {
  1104. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1105. serial8250_resume_port(priv->line);
  1106. return 0;
  1107. }
  1108. #else
  1109. #define omap8250_prepare NULL
  1110. #define omap8250_complete NULL
  1111. #endif
  1112. #ifdef CONFIG_PM
  1113. static int omap8250_lost_context(struct uart_8250_port *up)
  1114. {
  1115. u32 val;
  1116. val = serial_in(up, UART_OMAP_SCR);
  1117. /*
  1118. * If we lose context, then SCR is set to its reset value of zero.
  1119. * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
  1120. * among other bits, to never set the register back to zero again.
  1121. */
  1122. if (!val)
  1123. return 1;
  1124. return 0;
  1125. }
  1126. /* TODO: in future, this should happen via API in drivers/reset/ */
  1127. static int omap8250_soft_reset(struct device *dev)
  1128. {
  1129. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1130. struct uart_8250_port *up = serial8250_get_port(priv->line);
  1131. int timeout = 100;
  1132. int sysc;
  1133. int syss;
  1134. sysc = serial_in(up, UART_OMAP_SYSC);
  1135. /* softreset the UART */
  1136. sysc |= OMAP_UART_SYSC_SOFTRESET;
  1137. serial_out(up, UART_OMAP_SYSC, sysc);
  1138. /* By experiments, 1us enough for reset complete on AM335x */
  1139. do {
  1140. udelay(1);
  1141. syss = serial_in(up, UART_OMAP_SYSS);
  1142. } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
  1143. if (!timeout) {
  1144. dev_err(dev, "timed out waiting for reset done\n");
  1145. return -ETIMEDOUT;
  1146. }
  1147. return 0;
  1148. }
  1149. static int omap8250_runtime_suspend(struct device *dev)
  1150. {
  1151. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1152. struct uart_8250_port *up;
  1153. /* In case runtime-pm tries this before we are setup */
  1154. if (!priv)
  1155. return 0;
  1156. up = serial8250_get_port(priv->line);
  1157. /*
  1158. * When using 'no_console_suspend', the console UART must not be
  1159. * suspended. Since driver suspend is managed by runtime suspend,
  1160. * preventing runtime suspend (by returning error) will keep device
  1161. * active during suspend.
  1162. */
  1163. if (priv->is_suspending && !console_suspend_enabled) {
  1164. if (uart_console(&up->port))
  1165. return -EBUSY;
  1166. }
  1167. if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
  1168. int ret;
  1169. ret = omap8250_soft_reset(dev);
  1170. if (ret)
  1171. return ret;
  1172. /* Restore to UART mode after reset (for wakeup) */
  1173. omap8250_update_mdr1(up, priv);
  1174. }
  1175. if (up->dma && up->dma->rxchan)
  1176. omap_8250_rx_dma_flush(up);
  1177. priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1178. schedule_work(&priv->qos_work);
  1179. return 0;
  1180. }
  1181. static int omap8250_runtime_resume(struct device *dev)
  1182. {
  1183. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1184. struct uart_8250_port *up;
  1185. /* In case runtime-pm tries this before we are setup */
  1186. if (!priv)
  1187. return 0;
  1188. up = serial8250_get_port(priv->line);
  1189. if (omap8250_lost_context(up))
  1190. omap8250_restore_regs(up);
  1191. if (up->dma && up->dma->rxchan)
  1192. omap_8250_rx_dma(up);
  1193. priv->latency = priv->calc_latency;
  1194. schedule_work(&priv->qos_work);
  1195. return 0;
  1196. }
  1197. #endif
  1198. #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
  1199. static int __init omap8250_console_fixup(void)
  1200. {
  1201. char *omap_str;
  1202. char *options;
  1203. u8 idx;
  1204. if (strstr(boot_command_line, "console=ttyS"))
  1205. /* user set a ttyS based name for the console */
  1206. return 0;
  1207. omap_str = strstr(boot_command_line, "console=ttyO");
  1208. if (!omap_str)
  1209. /* user did not set ttyO based console, so we don't care */
  1210. return 0;
  1211. omap_str += 12;
  1212. if ('0' <= *omap_str && *omap_str <= '9')
  1213. idx = *omap_str - '0';
  1214. else
  1215. return 0;
  1216. omap_str++;
  1217. if (omap_str[0] == ',') {
  1218. omap_str++;
  1219. options = omap_str;
  1220. } else {
  1221. options = NULL;
  1222. }
  1223. add_preferred_console("ttyS", idx, options);
  1224. pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
  1225. idx, idx);
  1226. pr_err("This ensures that you still see kernel messages. Please\n");
  1227. pr_err("update your kernel commandline.\n");
  1228. return 0;
  1229. }
  1230. console_initcall(omap8250_console_fixup);
  1231. #endif
  1232. static const struct dev_pm_ops omap8250_dev_pm_ops = {
  1233. SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
  1234. SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
  1235. omap8250_runtime_resume, NULL)
  1236. .prepare = omap8250_prepare,
  1237. .complete = omap8250_complete,
  1238. };
  1239. static struct platform_driver omap8250_platform_driver = {
  1240. .driver = {
  1241. .name = "omap8250",
  1242. .pm = &omap8250_dev_pm_ops,
  1243. .of_match_table = omap8250_dt_ids,
  1244. },
  1245. .probe = omap8250_probe,
  1246. .remove = omap8250_remove,
  1247. };
  1248. module_platform_driver(omap8250_platform_driver);
  1249. MODULE_AUTHOR("Sebastian Andrzej Siewior");
  1250. MODULE_DESCRIPTION("OMAP 8250 Driver");
  1251. MODULE_LICENSE("GPL v2");