8250_omap.c 38 KB

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