8250_omap.c 35 KB

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