8250_omap.c 37 KB

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