8250_omap.c 37 KB

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