8250_omap.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
  377. if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
  378. /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
  379. up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  380. priv->efr |= UART_EFR_CTS;
  381. } else if (up->port.flags & UPF_SOFT_FLOW) {
  382. /*
  383. * OMAP rx s/w flow control is borked; the transmitter remains
  384. * stuck off even if rx flow control is subsequently disabled
  385. */
  386. /*
  387. * IXOFF Flag:
  388. * Enable XON/XOFF flow control on output.
  389. * Transmit XON1, XOFF1
  390. */
  391. if (termios->c_iflag & IXOFF) {
  392. up->port.status |= UPSTAT_AUTOXOFF;
  393. priv->efr |= OMAP_UART_SW_TX;
  394. }
  395. }
  396. omap8250_restore_regs(up);
  397. spin_unlock_irq(&up->port.lock);
  398. pm_runtime_mark_last_busy(port->dev);
  399. pm_runtime_put_autosuspend(port->dev);
  400. /* calculate wakeup latency constraint */
  401. priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
  402. priv->latency = priv->calc_latency;
  403. schedule_work(&priv->qos_work);
  404. /* Don't rewrite B0 */
  405. if (tty_termios_baud_rate(termios))
  406. tty_termios_encode_baud_rate(termios, baud, baud);
  407. }
  408. /* same as 8250 except that we may have extra flow bits set in EFR */
  409. static void omap_8250_pm(struct uart_port *port, unsigned int state,
  410. unsigned int oldstate)
  411. {
  412. struct uart_8250_port *up = up_to_u8250p(port);
  413. u8 efr;
  414. pm_runtime_get_sync(port->dev);
  415. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  416. efr = serial_in(up, UART_EFR);
  417. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  418. serial_out(up, UART_LCR, 0);
  419. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  420. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  421. serial_out(up, UART_EFR, efr);
  422. serial_out(up, UART_LCR, 0);
  423. pm_runtime_mark_last_busy(port->dev);
  424. pm_runtime_put_autosuspend(port->dev);
  425. }
  426. static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
  427. struct omap8250_priv *priv)
  428. {
  429. u32 mvr, scheme;
  430. u16 revision, major, minor;
  431. mvr = uart_read(up, UART_OMAP_MVER);
  432. /* Check revision register scheme */
  433. scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
  434. switch (scheme) {
  435. case 0: /* Legacy Scheme: OMAP2/3 */
  436. /* MINOR_REV[0:4], MAJOR_REV[4:7] */
  437. major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
  438. OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
  439. minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
  440. break;
  441. case 1:
  442. /* New Scheme: OMAP4+ */
  443. /* MINOR_REV[0:5], MAJOR_REV[8:10] */
  444. major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
  445. OMAP_UART_MVR_MAJ_SHIFT;
  446. minor = (mvr & OMAP_UART_MVR_MIN_MASK);
  447. break;
  448. default:
  449. dev_warn(up->port.dev,
  450. "Unknown revision, defaulting to highest\n");
  451. /* highest possible revision */
  452. major = 0xff;
  453. minor = 0xff;
  454. }
  455. /* normalize revision for the driver */
  456. revision = UART_BUILD_REVISION(major, minor);
  457. switch (revision) {
  458. case OMAP_UART_REV_46:
  459. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
  460. break;
  461. case OMAP_UART_REV_52:
  462. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
  463. OMAP_UART_WER_HAS_TX_WAKEUP;
  464. break;
  465. case OMAP_UART_REV_63:
  466. priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
  467. OMAP_UART_WER_HAS_TX_WAKEUP;
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. static void omap8250_uart_qos_work(struct work_struct *work)
  474. {
  475. struct omap8250_priv *priv;
  476. priv = container_of(work, struct omap8250_priv, qos_work);
  477. pm_qos_update_request(&priv->pm_qos_request, priv->latency);
  478. }
  479. #ifdef CONFIG_SERIAL_8250_DMA
  480. static int omap_8250_dma_handle_irq(struct uart_port *port);
  481. #endif
  482. static irqreturn_t omap8250_irq(int irq, void *dev_id)
  483. {
  484. struct uart_port *port = dev_id;
  485. struct uart_8250_port *up = up_to_u8250p(port);
  486. unsigned int iir;
  487. int ret;
  488. #ifdef CONFIG_SERIAL_8250_DMA
  489. if (up->dma) {
  490. ret = omap_8250_dma_handle_irq(port);
  491. return IRQ_RETVAL(ret);
  492. }
  493. #endif
  494. serial8250_rpm_get(up);
  495. iir = serial_port_in(port, UART_IIR);
  496. ret = serial8250_handle_irq(port, iir);
  497. serial8250_rpm_put(up);
  498. return IRQ_RETVAL(ret);
  499. }
  500. static int omap_8250_startup(struct uart_port *port)
  501. {
  502. struct uart_8250_port *up = up_to_u8250p(port);
  503. struct omap8250_priv *priv = port->private_data;
  504. int ret;
  505. if (priv->wakeirq) {
  506. ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
  507. if (ret)
  508. return ret;
  509. }
  510. pm_runtime_get_sync(port->dev);
  511. up->mcr = 0;
  512. serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  513. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  514. up->lsr_saved_flags = 0;
  515. up->msr_saved_flags = 0;
  516. /* Disable DMA for console UART */
  517. if (uart_console(port))
  518. up->dma = NULL;
  519. if (up->dma) {
  520. ret = serial8250_request_dma(up);
  521. if (ret) {
  522. dev_warn_ratelimited(port->dev,
  523. "failed to request DMA\n");
  524. up->dma = NULL;
  525. }
  526. }
  527. ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
  528. dev_name(port->dev), port);
  529. if (ret < 0)
  530. goto err;
  531. up->ier = UART_IER_RLSI | UART_IER_RDI;
  532. serial_out(up, UART_IER, up->ier);
  533. #ifdef CONFIG_PM
  534. up->capabilities |= UART_CAP_RPM;
  535. #endif
  536. /* Enable module level wake up */
  537. priv->wer = OMAP_UART_WER_MOD_WKUP;
  538. if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
  539. priv->wer |= OMAP_UART_TX_WAKEUP_EN;
  540. serial_out(up, UART_OMAP_WER, priv->wer);
  541. if (up->dma)
  542. up->dma->rx_dma(up, 0);
  543. pm_runtime_mark_last_busy(port->dev);
  544. pm_runtime_put_autosuspend(port->dev);
  545. return 0;
  546. err:
  547. pm_runtime_mark_last_busy(port->dev);
  548. pm_runtime_put_autosuspend(port->dev);
  549. dev_pm_clear_wake_irq(port->dev);
  550. return ret;
  551. }
  552. static void omap_8250_shutdown(struct uart_port *port)
  553. {
  554. struct uart_8250_port *up = up_to_u8250p(port);
  555. struct omap8250_priv *priv = port->private_data;
  556. flush_work(&priv->qos_work);
  557. if (up->dma)
  558. up->dma->rx_dma(up, UART_IIR_RX_TIMEOUT);
  559. pm_runtime_get_sync(port->dev);
  560. serial_out(up, UART_OMAP_WER, 0);
  561. up->ier = 0;
  562. serial_out(up, UART_IER, 0);
  563. if (up->dma)
  564. serial8250_release_dma(up);
  565. /*
  566. * Disable break condition and FIFOs
  567. */
  568. if (up->lcr & UART_LCR_SBC)
  569. serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
  570. serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  571. pm_runtime_mark_last_busy(port->dev);
  572. pm_runtime_put_autosuspend(port->dev);
  573. free_irq(port->irq, port);
  574. dev_pm_clear_wake_irq(port->dev);
  575. }
  576. static void omap_8250_throttle(struct uart_port *port)
  577. {
  578. unsigned long flags;
  579. struct uart_8250_port *up =
  580. container_of(port, struct uart_8250_port, port);
  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 void omap_8250_unthrottle(struct uart_port *port)
  590. {
  591. unsigned long flags;
  592. struct uart_8250_port *up =
  593. container_of(port, struct uart_8250_port, port);
  594. pm_runtime_get_sync(port->dev);
  595. spin_lock_irqsave(&port->lock, flags);
  596. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  597. serial_out(up, UART_IER, up->ier);
  598. spin_unlock_irqrestore(&port->lock, flags);
  599. pm_runtime_mark_last_busy(port->dev);
  600. pm_runtime_put_autosuspend(port->dev);
  601. }
  602. #ifdef CONFIG_SERIAL_8250_DMA
  603. static int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir);
  604. static void __dma_rx_do_complete(struct uart_8250_port *p, bool error)
  605. {
  606. struct omap8250_priv *priv = p->port.private_data;
  607. struct uart_8250_dma *dma = p->dma;
  608. struct tty_port *tty_port = &p->port.state->port;
  609. struct dma_tx_state state;
  610. int count;
  611. unsigned long flags;
  612. int ret;
  613. dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
  614. dma->rx_size, DMA_FROM_DEVICE);
  615. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  616. if (!dma->rx_running)
  617. goto unlock;
  618. dma->rx_running = 0;
  619. dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
  620. dmaengine_terminate_all(dma->rxchan);
  621. count = dma->rx_size - state.residue;
  622. ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
  623. p->port.icount.rx += ret;
  624. p->port.icount.buf_overrun += count - ret;
  625. unlock:
  626. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  627. if (!error)
  628. omap_8250_rx_dma(p, 0);
  629. tty_flip_buffer_push(tty_port);
  630. }
  631. static void __dma_rx_complete(void *param)
  632. {
  633. __dma_rx_do_complete(param, false);
  634. }
  635. static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
  636. {
  637. struct omap8250_priv *priv = p->port.private_data;
  638. struct uart_8250_dma *dma = p->dma;
  639. unsigned long flags;
  640. int ret;
  641. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  642. if (!dma->rx_running) {
  643. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  644. return;
  645. }
  646. ret = dmaengine_pause(dma->rxchan);
  647. if (WARN_ON_ONCE(ret))
  648. priv->rx_dma_broken = true;
  649. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  650. __dma_rx_do_complete(p, true);
  651. }
  652. static int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
  653. {
  654. struct omap8250_priv *priv = p->port.private_data;
  655. struct uart_8250_dma *dma = p->dma;
  656. int err = 0;
  657. struct dma_async_tx_descriptor *desc;
  658. unsigned long flags;
  659. switch (iir & 0x3f) {
  660. case UART_IIR_RLSI:
  661. /* 8250_core handles errors and break interrupts */
  662. omap_8250_rx_dma_flush(p);
  663. return -EIO;
  664. case UART_IIR_RX_TIMEOUT:
  665. /*
  666. * If RCVR FIFO trigger level was not reached, complete the
  667. * transfer and let 8250_core copy the remaining data.
  668. */
  669. omap_8250_rx_dma_flush(p);
  670. return -ETIMEDOUT;
  671. case UART_IIR_RDI:
  672. /*
  673. * The OMAP UART is a special BEAST. If we receive RDI we _have_
  674. * a DMA transfer programmed but it didn't work. One reason is
  675. * that we were too slow and there were too many bytes in the
  676. * FIFO, the UART counted wrong and never kicked the DMA engine
  677. * to do anything. That means once we receive RDI on OMAP then
  678. * the DMA won't do anything soon so we have to cancel the DMA
  679. * transfer and purge the FIFO manually.
  680. */
  681. omap_8250_rx_dma_flush(p);
  682. return -ETIMEDOUT;
  683. default:
  684. break;
  685. }
  686. if (priv->rx_dma_broken)
  687. return -EINVAL;
  688. spin_lock_irqsave(&priv->rx_dma_lock, flags);
  689. if (dma->rx_running)
  690. goto out;
  691. desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
  692. dma->rx_size, DMA_DEV_TO_MEM,
  693. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  694. if (!desc) {
  695. err = -EBUSY;
  696. goto out;
  697. }
  698. dma->rx_running = 1;
  699. desc->callback = __dma_rx_complete;
  700. desc->callback_param = p;
  701. dma->rx_cookie = dmaengine_submit(desc);
  702. dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
  703. dma->rx_size, DMA_FROM_DEVICE);
  704. dma_async_issue_pending(dma->rxchan);
  705. out:
  706. spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
  707. return err;
  708. }
  709. static int omap_8250_tx_dma(struct uart_8250_port *p);
  710. static void omap_8250_dma_tx_complete(void *param)
  711. {
  712. struct uart_8250_port *p = param;
  713. struct uart_8250_dma *dma = p->dma;
  714. struct circ_buf *xmit = &p->port.state->xmit;
  715. unsigned long flags;
  716. bool en_thri = false;
  717. struct omap8250_priv *priv = p->port.private_data;
  718. dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
  719. UART_XMIT_SIZE, DMA_TO_DEVICE);
  720. spin_lock_irqsave(&p->port.lock, flags);
  721. dma->tx_running = 0;
  722. xmit->tail += dma->tx_size;
  723. xmit->tail &= UART_XMIT_SIZE - 1;
  724. p->port.icount.tx += dma->tx_size;
  725. if (priv->delayed_restore) {
  726. priv->delayed_restore = 0;
  727. omap8250_restore_regs(p);
  728. }
  729. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  730. uart_write_wakeup(&p->port);
  731. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
  732. int ret;
  733. ret = omap_8250_tx_dma(p);
  734. if (ret)
  735. en_thri = true;
  736. } else if (p->capabilities & UART_CAP_RPM) {
  737. en_thri = true;
  738. }
  739. if (en_thri) {
  740. dma->tx_err = 1;
  741. p->ier |= UART_IER_THRI;
  742. serial_port_out(&p->port, UART_IER, p->ier);
  743. }
  744. spin_unlock_irqrestore(&p->port.lock, flags);
  745. }
  746. static int omap_8250_tx_dma(struct uart_8250_port *p)
  747. {
  748. struct uart_8250_dma *dma = p->dma;
  749. struct omap8250_priv *priv = p->port.private_data;
  750. struct circ_buf *xmit = &p->port.state->xmit;
  751. struct dma_async_tx_descriptor *desc;
  752. unsigned int skip_byte = 0;
  753. int ret;
  754. if (dma->tx_running)
  755. return 0;
  756. if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
  757. /*
  758. * Even if no data, we need to return an error for the two cases
  759. * below so serial8250_tx_chars() is invoked and properly clears
  760. * THRI and/or runtime suspend.
  761. */
  762. if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
  763. ret = -EBUSY;
  764. goto err;
  765. }
  766. if (p->ier & UART_IER_THRI) {
  767. p->ier &= ~UART_IER_THRI;
  768. serial_out(p, UART_IER, p->ier);
  769. }
  770. return 0;
  771. }
  772. dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  773. if (priv->habit & OMAP_DMA_TX_KICK) {
  774. u8 tx_lvl;
  775. /*
  776. * We need to put the first byte into the FIFO in order to start
  777. * the DMA transfer. For transfers smaller than four bytes we
  778. * don't bother doing DMA at all. It seem not matter if there
  779. * are still bytes in the FIFO from the last transfer (in case
  780. * we got here directly from omap_8250_dma_tx_complete()). Bytes
  781. * leaving the FIFO seem not to trigger the DMA transfer. It is
  782. * really the byte that we put into the FIFO.
  783. * If the FIFO is already full then we most likely got here from
  784. * omap_8250_dma_tx_complete(). And this means the DMA engine
  785. * just completed its work. We don't have to wait the complete
  786. * 86us at 115200,8n1 but around 60us (not to mention lower
  787. * baudrates). So in that case we take the interrupt and try
  788. * again with an empty FIFO.
  789. */
  790. tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
  791. if (tx_lvl == p->tx_loadsz) {
  792. ret = -EBUSY;
  793. goto err;
  794. }
  795. if (dma->tx_size < 4) {
  796. ret = -EINVAL;
  797. goto err;
  798. }
  799. skip_byte = 1;
  800. }
  801. desc = dmaengine_prep_slave_single(dma->txchan,
  802. dma->tx_addr + xmit->tail + skip_byte,
  803. dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
  804. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  805. if (!desc) {
  806. ret = -EBUSY;
  807. goto err;
  808. }
  809. dma->tx_running = 1;
  810. desc->callback = omap_8250_dma_tx_complete;
  811. desc->callback_param = p;
  812. dma->tx_cookie = dmaengine_submit(desc);
  813. dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
  814. UART_XMIT_SIZE, DMA_TO_DEVICE);
  815. dma_async_issue_pending(dma->txchan);
  816. if (dma->tx_err)
  817. dma->tx_err = 0;
  818. if (p->ier & UART_IER_THRI) {
  819. p->ier &= ~UART_IER_THRI;
  820. serial_out(p, UART_IER, p->ier);
  821. }
  822. if (skip_byte)
  823. serial_out(p, UART_TX, xmit->buf[xmit->tail]);
  824. return 0;
  825. err:
  826. dma->tx_err = 1;
  827. return ret;
  828. }
  829. /*
  830. * This is mostly serial8250_handle_irq(). We have a slightly different DMA
  831. * hoook for RX/TX and need different logic for them in the ISR. Therefore we
  832. * use the default routine in the non-DMA case and this one for with DMA.
  833. */
  834. static int omap_8250_dma_handle_irq(struct uart_port *port)
  835. {
  836. struct uart_8250_port *up = up_to_u8250p(port);
  837. unsigned char status;
  838. unsigned long flags;
  839. u8 iir;
  840. int dma_err = 0;
  841. serial8250_rpm_get(up);
  842. iir = serial_port_in(port, UART_IIR);
  843. if (iir & UART_IIR_NO_INT) {
  844. serial8250_rpm_put(up);
  845. return 0;
  846. }
  847. spin_lock_irqsave(&port->lock, flags);
  848. status = serial_port_in(port, UART_LSR);
  849. if (status & (UART_LSR_DR | UART_LSR_BI)) {
  850. dma_err = omap_8250_rx_dma(up, iir);
  851. if (dma_err) {
  852. status = serial8250_rx_chars(up, status);
  853. omap_8250_rx_dma(up, 0);
  854. }
  855. }
  856. serial8250_modem_status(up);
  857. if (status & UART_LSR_THRE && up->dma->tx_err) {
  858. if (uart_tx_stopped(&up->port) ||
  859. uart_circ_empty(&up->port.state->xmit)) {
  860. up->dma->tx_err = 0;
  861. serial8250_tx_chars(up);
  862. } else {
  863. /*
  864. * try again due to an earlier failer which
  865. * might have been resolved by now.
  866. */
  867. dma_err = omap_8250_tx_dma(up);
  868. if (dma_err)
  869. serial8250_tx_chars(up);
  870. }
  871. }
  872. spin_unlock_irqrestore(&port->lock, flags);
  873. serial8250_rpm_put(up);
  874. return 1;
  875. }
  876. static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
  877. {
  878. return false;
  879. }
  880. #else
  881. static inline int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
  882. {
  883. return -EINVAL;
  884. }
  885. #endif
  886. static int omap8250_no_handle_irq(struct uart_port *port)
  887. {
  888. /* IRQ has not been requested but handling irq? */
  889. WARN_ONCE(1, "Unexpected irq handling before port startup\n");
  890. return 0;
  891. }
  892. static const u8 am3352_habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE;
  893. static const u8 am4372_habit = UART_ERRATA_CLOCK_DISABLE;
  894. static const struct of_device_id omap8250_dt_ids[] = {
  895. { .compatible = "ti,omap2-uart" },
  896. { .compatible = "ti,omap3-uart" },
  897. { .compatible = "ti,omap4-uart" },
  898. { .compatible = "ti,am3352-uart", .data = &am3352_habit, },
  899. { .compatible = "ti,am4372-uart", .data = &am4372_habit, },
  900. { .compatible = "ti,dra742-uart", .data = &am4372_habit, },
  901. {},
  902. };
  903. MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
  904. static int omap8250_probe(struct platform_device *pdev)
  905. {
  906. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  907. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  908. struct omap8250_priv *priv;
  909. struct uart_8250_port up;
  910. int ret;
  911. void __iomem *membase;
  912. if (!regs || !irq) {
  913. dev_err(&pdev->dev, "missing registers or irq\n");
  914. return -EINVAL;
  915. }
  916. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  917. if (!priv)
  918. return -ENOMEM;
  919. membase = devm_ioremap_nocache(&pdev->dev, regs->start,
  920. resource_size(regs));
  921. if (!membase)
  922. return -ENODEV;
  923. memset(&up, 0, sizeof(up));
  924. up.port.dev = &pdev->dev;
  925. up.port.mapbase = regs->start;
  926. up.port.membase = membase;
  927. up.port.irq = irq->start;
  928. /*
  929. * It claims to be 16C750 compatible however it is a little different.
  930. * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
  931. * have) is enabled via EFR instead of MCR. The type is set here 8250
  932. * just to get things going. UNKNOWN does not work for a few reasons and
  933. * we don't need our own type since we don't use 8250's set_termios()
  934. * or pm callback.
  935. */
  936. up.port.type = PORT_8250;
  937. up.port.iotype = UPIO_MEM;
  938. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
  939. UPF_HARD_FLOW;
  940. up.port.private_data = priv;
  941. up.port.regshift = 2;
  942. up.port.fifosize = 64;
  943. up.tx_loadsz = 64;
  944. up.capabilities = UART_CAP_FIFO;
  945. #ifdef CONFIG_PM
  946. /*
  947. * Runtime PM is mostly transparent. However to do it right we need to a
  948. * TX empty interrupt before we can put the device to auto idle. So if
  949. * PM is not enabled we don't add that flag and can spare that one extra
  950. * interrupt in the TX path.
  951. */
  952. up.capabilities |= UART_CAP_RPM;
  953. #endif
  954. up.port.set_termios = omap_8250_set_termios;
  955. up.port.set_mctrl = omap8250_set_mctrl;
  956. up.port.pm = omap_8250_pm;
  957. up.port.startup = omap_8250_startup;
  958. up.port.shutdown = omap_8250_shutdown;
  959. up.port.throttle = omap_8250_throttle;
  960. up.port.unthrottle = omap_8250_unthrottle;
  961. if (pdev->dev.of_node) {
  962. const struct of_device_id *id;
  963. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  964. of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  965. &up.port.uartclk);
  966. priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  967. id = of_match_device(of_match_ptr(omap8250_dt_ids), &pdev->dev);
  968. if (id && id->data)
  969. priv->habit |= *(u8 *)id->data;
  970. } else {
  971. ret = pdev->id;
  972. }
  973. if (ret < 0) {
  974. dev_err(&pdev->dev, "failed to get alias/pdev id\n");
  975. return ret;
  976. }
  977. up.port.line = ret;
  978. if (!up.port.uartclk) {
  979. up.port.uartclk = DEFAULT_CLK_SPEED;
  980. dev_warn(&pdev->dev,
  981. "No clock speed specified: using default: %d\n",
  982. DEFAULT_CLK_SPEED);
  983. }
  984. priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  985. priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  986. pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
  987. priv->latency);
  988. INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
  989. spin_lock_init(&priv->rx_dma_lock);
  990. device_init_wakeup(&pdev->dev, true);
  991. pm_runtime_use_autosuspend(&pdev->dev);
  992. pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
  993. pm_runtime_irq_safe(&pdev->dev);
  994. pm_runtime_enable(&pdev->dev);
  995. pm_runtime_get_sync(&pdev->dev);
  996. omap_serial_fill_features_erratas(&up, priv);
  997. up.port.handle_irq = omap8250_no_handle_irq;
  998. #ifdef CONFIG_SERIAL_8250_DMA
  999. if (pdev->dev.of_node) {
  1000. /*
  1001. * Oh DMA support. If there are no DMA properties in the DT then
  1002. * we will fall back to a generic DMA channel which does not
  1003. * really work here. To ensure that we do not get a generic DMA
  1004. * channel assigned, we have the the_no_dma_filter_fn() here.
  1005. * To avoid "failed to request DMA" messages we check for DMA
  1006. * properties in DT.
  1007. */
  1008. ret = of_property_count_strings(pdev->dev.of_node, "dma-names");
  1009. if (ret == 2) {
  1010. up.dma = &priv->omap8250_dma;
  1011. priv->omap8250_dma.fn = the_no_dma_filter_fn;
  1012. priv->omap8250_dma.tx_dma = omap_8250_tx_dma;
  1013. priv->omap8250_dma.rx_dma = omap_8250_rx_dma;
  1014. priv->omap8250_dma.rx_size = RX_TRIGGER;
  1015. priv->omap8250_dma.rxconf.src_maxburst = RX_TRIGGER;
  1016. priv->omap8250_dma.txconf.dst_maxburst = TX_TRIGGER;
  1017. if (of_machine_is_compatible("ti,am33xx"))
  1018. priv->habit |= OMAP_DMA_TX_KICK;
  1019. /*
  1020. * pause is currently not supported atleast on omap-sdma
  1021. * and edma on most earlier kernels.
  1022. */
  1023. priv->rx_dma_broken = true;
  1024. }
  1025. }
  1026. #endif
  1027. ret = serial8250_register_8250_port(&up);
  1028. if (ret < 0) {
  1029. dev_err(&pdev->dev, "unable to register 8250 port\n");
  1030. goto err;
  1031. }
  1032. priv->line = ret;
  1033. platform_set_drvdata(pdev, priv);
  1034. pm_runtime_mark_last_busy(&pdev->dev);
  1035. pm_runtime_put_autosuspend(&pdev->dev);
  1036. return 0;
  1037. err:
  1038. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1039. pm_runtime_put_sync(&pdev->dev);
  1040. pm_runtime_disable(&pdev->dev);
  1041. return ret;
  1042. }
  1043. static int omap8250_remove(struct platform_device *pdev)
  1044. {
  1045. struct omap8250_priv *priv = platform_get_drvdata(pdev);
  1046. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1047. pm_runtime_put_sync(&pdev->dev);
  1048. pm_runtime_disable(&pdev->dev);
  1049. serial8250_unregister_port(priv->line);
  1050. pm_qos_remove_request(&priv->pm_qos_request);
  1051. device_init_wakeup(&pdev->dev, false);
  1052. return 0;
  1053. }
  1054. #ifdef CONFIG_PM_SLEEP
  1055. static int omap8250_prepare(struct device *dev)
  1056. {
  1057. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1058. if (!priv)
  1059. return 0;
  1060. priv->is_suspending = true;
  1061. return 0;
  1062. }
  1063. static void omap8250_complete(struct device *dev)
  1064. {
  1065. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1066. if (!priv)
  1067. return;
  1068. priv->is_suspending = false;
  1069. }
  1070. static int omap8250_suspend(struct device *dev)
  1071. {
  1072. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1073. serial8250_suspend_port(priv->line);
  1074. flush_work(&priv->qos_work);
  1075. return 0;
  1076. }
  1077. static int omap8250_resume(struct device *dev)
  1078. {
  1079. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1080. serial8250_resume_port(priv->line);
  1081. return 0;
  1082. }
  1083. #else
  1084. #define omap8250_prepare NULL
  1085. #define omap8250_complete NULL
  1086. #endif
  1087. #ifdef CONFIG_PM
  1088. static int omap8250_lost_context(struct uart_8250_port *up)
  1089. {
  1090. u32 val;
  1091. val = serial_in(up, UART_OMAP_SCR);
  1092. /*
  1093. * If we lose context, then SCR is set to its reset value of zero.
  1094. * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
  1095. * among other bits, to never set the register back to zero again.
  1096. */
  1097. if (!val)
  1098. return 1;
  1099. return 0;
  1100. }
  1101. /* TODO: in future, this should happen via API in drivers/reset/ */
  1102. static int omap8250_soft_reset(struct device *dev)
  1103. {
  1104. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1105. struct uart_8250_port *up = serial8250_get_port(priv->line);
  1106. int timeout = 100;
  1107. int sysc;
  1108. int syss;
  1109. sysc = serial_in(up, UART_OMAP_SYSC);
  1110. /* softreset the UART */
  1111. sysc |= OMAP_UART_SYSC_SOFTRESET;
  1112. serial_out(up, UART_OMAP_SYSC, sysc);
  1113. /* By experiments, 1us enough for reset complete on AM335x */
  1114. do {
  1115. udelay(1);
  1116. syss = serial_in(up, UART_OMAP_SYSS);
  1117. } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
  1118. if (!timeout) {
  1119. dev_err(dev, "timed out waiting for reset done\n");
  1120. return -ETIMEDOUT;
  1121. }
  1122. return 0;
  1123. }
  1124. static int omap8250_runtime_suspend(struct device *dev)
  1125. {
  1126. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1127. struct uart_8250_port *up;
  1128. /* In case runtime-pm tries this before we are setup */
  1129. if (!priv)
  1130. return 0;
  1131. up = serial8250_get_port(priv->line);
  1132. /*
  1133. * When using 'no_console_suspend', the console UART must not be
  1134. * suspended. Since driver suspend is managed by runtime suspend,
  1135. * preventing runtime suspend (by returning error) will keep device
  1136. * active during suspend.
  1137. */
  1138. if (priv->is_suspending && !console_suspend_enabled) {
  1139. if (uart_console(&up->port))
  1140. return -EBUSY;
  1141. }
  1142. if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
  1143. int ret;
  1144. ret = omap8250_soft_reset(dev);
  1145. if (ret)
  1146. return ret;
  1147. /* Restore to UART mode after reset (for wakeup) */
  1148. omap8250_update_mdr1(up, priv);
  1149. }
  1150. if (up->dma && up->dma->rxchan)
  1151. omap_8250_rx_dma(up, UART_IIR_RX_TIMEOUT);
  1152. priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1153. schedule_work(&priv->qos_work);
  1154. return 0;
  1155. }
  1156. static int omap8250_runtime_resume(struct device *dev)
  1157. {
  1158. struct omap8250_priv *priv = dev_get_drvdata(dev);
  1159. struct uart_8250_port *up;
  1160. int loss_cntx;
  1161. /* In case runtime-pm tries this before we are setup */
  1162. if (!priv)
  1163. return 0;
  1164. up = serial8250_get_port(priv->line);
  1165. loss_cntx = omap8250_lost_context(up);
  1166. if (loss_cntx)
  1167. omap8250_restore_regs(up);
  1168. if (up->dma && up->dma->rxchan)
  1169. omap_8250_rx_dma(up, 0);
  1170. priv->latency = priv->calc_latency;
  1171. schedule_work(&priv->qos_work);
  1172. return 0;
  1173. }
  1174. #endif
  1175. #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
  1176. static int __init omap8250_console_fixup(void)
  1177. {
  1178. char *omap_str;
  1179. char *options;
  1180. u8 idx;
  1181. if (strstr(boot_command_line, "console=ttyS"))
  1182. /* user set a ttyS based name for the console */
  1183. return 0;
  1184. omap_str = strstr(boot_command_line, "console=ttyO");
  1185. if (!omap_str)
  1186. /* user did not set ttyO based console, so we don't care */
  1187. return 0;
  1188. omap_str += 12;
  1189. if ('0' <= *omap_str && *omap_str <= '9')
  1190. idx = *omap_str - '0';
  1191. else
  1192. return 0;
  1193. omap_str++;
  1194. if (omap_str[0] == ',') {
  1195. omap_str++;
  1196. options = omap_str;
  1197. } else {
  1198. options = NULL;
  1199. }
  1200. add_preferred_console("ttyS", idx, options);
  1201. pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
  1202. idx, idx);
  1203. pr_err("This ensures that you still see kernel messages. Please\n");
  1204. pr_err("update your kernel commandline.\n");
  1205. return 0;
  1206. }
  1207. console_initcall(omap8250_console_fixup);
  1208. #endif
  1209. static const struct dev_pm_ops omap8250_dev_pm_ops = {
  1210. SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
  1211. SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
  1212. omap8250_runtime_resume, NULL)
  1213. .prepare = omap8250_prepare,
  1214. .complete = omap8250_complete,
  1215. };
  1216. static struct platform_driver omap8250_platform_driver = {
  1217. .driver = {
  1218. .name = "omap8250",
  1219. .pm = &omap8250_dev_pm_ops,
  1220. .of_match_table = omap8250_dt_ids,
  1221. },
  1222. .probe = omap8250_probe,
  1223. .remove = omap8250_remove,
  1224. };
  1225. module_platform_driver(omap8250_platform_driver);
  1226. MODULE_AUTHOR("Sebastian Andrzej Siewior");
  1227. MODULE_DESCRIPTION("OMAP 8250 Driver");
  1228. MODULE_LICENSE("GPL v2");