omap-serial.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. /*
  2. * Driver for OMAP-UART controller.
  3. * Based on drivers/serial/8250.c
  4. *
  5. * Copyright (C) 2010 Texas Instruments.
  6. *
  7. * Authors:
  8. * Govindraj R <govindraj.raja@ti.com>
  9. * Thara Gopinath <thara@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * Note: This driver is made separate from 8250 driver as we cannot
  17. * over load 8250 driver with omap platform specific configuration for
  18. * features like DMA, it makes easier to implement features like DMA and
  19. * hardware flow control and software flow control configuration with
  20. * this driver as required for the omap-platform.
  21. */
  22. #if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  23. #define SUPPORT_SYSRQ
  24. #endif
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/console.h>
  28. #include <linux/serial_reg.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/io.h>
  35. #include <linux/clk.h>
  36. #include <linux/serial_core.h>
  37. #include <linux/irq.h>
  38. #include <linux/pm_runtime.h>
  39. #include <linux/pm_wakeirq.h>
  40. #include <linux/of.h>
  41. #include <linux/of_irq.h>
  42. #include <linux/gpio.h>
  43. #include <linux/of_gpio.h>
  44. #include <linux/platform_data/serial-omap.h>
  45. #include <dt-bindings/gpio/gpio.h>
  46. #define OMAP_MAX_HSUART_PORTS 10
  47. #define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
  48. #define OMAP_UART_REV_42 0x0402
  49. #define OMAP_UART_REV_46 0x0406
  50. #define OMAP_UART_REV_52 0x0502
  51. #define OMAP_UART_REV_63 0x0603
  52. #define OMAP_UART_TX_WAKEUP_EN BIT(7)
  53. /* Feature flags */
  54. #define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
  55. #define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
  56. #define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
  57. #define DEFAULT_CLK_SPEED 48000000 /* 48Mhz */
  58. /* SCR register bitmasks */
  59. #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
  60. #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
  61. #define OMAP_UART_SCR_TX_EMPTY (1 << 3)
  62. /* FCR register bitmasks */
  63. #define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
  64. #define OMAP_UART_FCR_TX_FIFO_TRIG_MASK (0x3 << 4)
  65. /* MVR register bitmasks */
  66. #define OMAP_UART_MVR_SCHEME_SHIFT 30
  67. #define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
  68. #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
  69. #define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
  70. #define OMAP_UART_MVR_MAJ_MASK 0x700
  71. #define OMAP_UART_MVR_MAJ_SHIFT 8
  72. #define OMAP_UART_MVR_MIN_MASK 0x3f
  73. #define OMAP_UART_DMA_CH_FREE -1
  74. #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
  75. #define OMAP_MODE13X_SPEED 230400
  76. /* WER = 0x7F
  77. * Enable module level wakeup in WER reg
  78. */
  79. #define OMAP_UART_WER_MOD_WKUP 0x7F
  80. /* Enable XON/XOFF flow control on output */
  81. #define OMAP_UART_SW_TX 0x08
  82. /* Enable XON/XOFF flow control on input */
  83. #define OMAP_UART_SW_RX 0x02
  84. #define OMAP_UART_SW_CLR 0xF0
  85. #define OMAP_UART_TCR_TRIG 0x0F
  86. struct uart_omap_dma {
  87. u8 uart_dma_tx;
  88. u8 uart_dma_rx;
  89. int rx_dma_channel;
  90. int tx_dma_channel;
  91. dma_addr_t rx_buf_dma_phys;
  92. dma_addr_t tx_buf_dma_phys;
  93. unsigned int uart_base;
  94. /*
  95. * Buffer for rx dma. It is not required for tx because the buffer
  96. * comes from port structure.
  97. */
  98. unsigned char *rx_buf;
  99. unsigned int prev_rx_dma_pos;
  100. int tx_buf_size;
  101. int tx_dma_used;
  102. int rx_dma_used;
  103. spinlock_t tx_lock;
  104. spinlock_t rx_lock;
  105. /* timer to poll activity on rx dma */
  106. struct timer_list rx_timer;
  107. unsigned int rx_buf_size;
  108. unsigned int rx_poll_rate;
  109. unsigned int rx_timeout;
  110. };
  111. struct uart_omap_port {
  112. struct uart_port port;
  113. struct uart_omap_dma uart_dma;
  114. struct device *dev;
  115. int wakeirq;
  116. unsigned char ier;
  117. unsigned char lcr;
  118. unsigned char mcr;
  119. unsigned char fcr;
  120. unsigned char efr;
  121. unsigned char dll;
  122. unsigned char dlh;
  123. unsigned char mdr1;
  124. unsigned char scr;
  125. unsigned char wer;
  126. int use_dma;
  127. /*
  128. * Some bits in registers are cleared on a read, so they must
  129. * be saved whenever the register is read, but the bits will not
  130. * be immediately processed.
  131. */
  132. unsigned int lsr_break_flag;
  133. unsigned char msr_saved_flags;
  134. char name[20];
  135. unsigned long port_activity;
  136. int context_loss_cnt;
  137. u32 errata;
  138. u32 features;
  139. int rts_gpio;
  140. struct pm_qos_request pm_qos_request;
  141. u32 latency;
  142. u32 calc_latency;
  143. struct work_struct qos_work;
  144. bool is_suspending;
  145. };
  146. #define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port)))
  147. static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
  148. /* Forward declaration of functions */
  149. static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
  150. static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
  151. {
  152. offset <<= up->port.regshift;
  153. return readw(up->port.membase + offset);
  154. }
  155. static inline void serial_out(struct uart_omap_port *up, int offset, int value)
  156. {
  157. offset <<= up->port.regshift;
  158. writew(value, up->port.membase + offset);
  159. }
  160. static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
  161. {
  162. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  163. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  164. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  165. serial_out(up, UART_FCR, 0);
  166. }
  167. static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
  168. {
  169. struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
  170. if (!pdata || !pdata->get_context_loss_count)
  171. return -EINVAL;
  172. return pdata->get_context_loss_count(up->dev);
  173. }
  174. /* REVISIT: Remove this when omap3 boots in device tree only mode */
  175. static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
  176. {
  177. struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
  178. if (!pdata || !pdata->enable_wakeup)
  179. return;
  180. pdata->enable_wakeup(up->dev, enable);
  181. }
  182. /*
  183. * Calculate the absolute difference between the desired and actual baud
  184. * rate for the given mode.
  185. */
  186. static inline int calculate_baud_abs_diff(struct uart_port *port,
  187. unsigned int baud, unsigned int mode)
  188. {
  189. unsigned int n = port->uartclk / (mode * baud);
  190. int abs_diff;
  191. if (n == 0)
  192. n = 1;
  193. abs_diff = baud - (port->uartclk / (mode * n));
  194. if (abs_diff < 0)
  195. abs_diff = -abs_diff;
  196. return abs_diff;
  197. }
  198. /*
  199. * serial_omap_baud_is_mode16 - check if baud rate is MODE16X
  200. * @port: uart port info
  201. * @baud: baudrate for which mode needs to be determined
  202. *
  203. * Returns true if baud rate is MODE16X and false if MODE13X
  204. * Original table in OMAP TRM named "UART Mode Baud Rates, Divisor Values,
  205. * and Error Rates" determines modes not for all common baud rates.
  206. * E.g. for 1000000 baud rate mode must be 16x, but according to that
  207. * table it's determined as 13x.
  208. */
  209. static bool
  210. serial_omap_baud_is_mode16(struct uart_port *port, unsigned int baud)
  211. {
  212. int abs_diff_13 = calculate_baud_abs_diff(port, baud, 13);
  213. int abs_diff_16 = calculate_baud_abs_diff(port, baud, 16);
  214. return (abs_diff_13 >= abs_diff_16);
  215. }
  216. /*
  217. * serial_omap_get_divisor - calculate divisor value
  218. * @port: uart port info
  219. * @baud: baudrate for which divisor needs to be calculated.
  220. */
  221. static unsigned int
  222. serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
  223. {
  224. unsigned int mode;
  225. if (!serial_omap_baud_is_mode16(port, baud))
  226. mode = 13;
  227. else
  228. mode = 16;
  229. return port->uartclk/(mode * baud);
  230. }
  231. static void serial_omap_enable_ms(struct uart_port *port)
  232. {
  233. struct uart_omap_port *up = to_uart_omap_port(port);
  234. dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
  235. pm_runtime_get_sync(up->dev);
  236. up->ier |= UART_IER_MSI;
  237. serial_out(up, UART_IER, up->ier);
  238. pm_runtime_mark_last_busy(up->dev);
  239. pm_runtime_put_autosuspend(up->dev);
  240. }
  241. static void serial_omap_stop_tx(struct uart_port *port)
  242. {
  243. struct uart_omap_port *up = to_uart_omap_port(port);
  244. int res;
  245. pm_runtime_get_sync(up->dev);
  246. /* Handle RS-485 */
  247. if (port->rs485.flags & SER_RS485_ENABLED) {
  248. if (up->scr & OMAP_UART_SCR_TX_EMPTY) {
  249. /* THR interrupt is fired when both TX FIFO and TX
  250. * shift register are empty. This means there's nothing
  251. * left to transmit now, so make sure the THR interrupt
  252. * is fired when TX FIFO is below the trigger level,
  253. * disable THR interrupts and toggle the RS-485 GPIO
  254. * data direction pin if needed.
  255. */
  256. up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
  257. serial_out(up, UART_OMAP_SCR, up->scr);
  258. res = (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) ?
  259. 1 : 0;
  260. if (gpio_get_value(up->rts_gpio) != res) {
  261. if (port->rs485.delay_rts_after_send > 0)
  262. mdelay(
  263. port->rs485.delay_rts_after_send);
  264. gpio_set_value(up->rts_gpio, res);
  265. }
  266. } else {
  267. /* We're asked to stop, but there's still stuff in the
  268. * UART FIFO, so make sure the THR interrupt is fired
  269. * when both TX FIFO and TX shift register are empty.
  270. * The next THR interrupt (if no transmission is started
  271. * in the meantime) will indicate the end of a
  272. * transmission. Therefore we _don't_ disable THR
  273. * interrupts in this situation.
  274. */
  275. up->scr |= OMAP_UART_SCR_TX_EMPTY;
  276. serial_out(up, UART_OMAP_SCR, up->scr);
  277. return;
  278. }
  279. }
  280. if (up->ier & UART_IER_THRI) {
  281. up->ier &= ~UART_IER_THRI;
  282. serial_out(up, UART_IER, up->ier);
  283. }
  284. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  285. !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
  286. /*
  287. * Empty the RX FIFO, we are not interested in anything
  288. * received during the half-duplex transmission.
  289. */
  290. serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_RCVR);
  291. /* Re-enable RX interrupts */
  292. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  293. up->port.read_status_mask |= UART_LSR_DR;
  294. serial_out(up, UART_IER, up->ier);
  295. }
  296. pm_runtime_mark_last_busy(up->dev);
  297. pm_runtime_put_autosuspend(up->dev);
  298. }
  299. static void serial_omap_stop_rx(struct uart_port *port)
  300. {
  301. struct uart_omap_port *up = to_uart_omap_port(port);
  302. pm_runtime_get_sync(up->dev);
  303. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  304. up->port.read_status_mask &= ~UART_LSR_DR;
  305. serial_out(up, UART_IER, up->ier);
  306. pm_runtime_mark_last_busy(up->dev);
  307. pm_runtime_put_autosuspend(up->dev);
  308. }
  309. static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
  310. {
  311. struct circ_buf *xmit = &up->port.state->xmit;
  312. int count;
  313. if (up->port.x_char) {
  314. serial_out(up, UART_TX, up->port.x_char);
  315. up->port.icount.tx++;
  316. up->port.x_char = 0;
  317. return;
  318. }
  319. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  320. serial_omap_stop_tx(&up->port);
  321. return;
  322. }
  323. count = up->port.fifosize / 4;
  324. do {
  325. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  326. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  327. up->port.icount.tx++;
  328. if (uart_circ_empty(xmit))
  329. break;
  330. } while (--count > 0);
  331. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  332. uart_write_wakeup(&up->port);
  333. if (uart_circ_empty(xmit))
  334. serial_omap_stop_tx(&up->port);
  335. }
  336. static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
  337. {
  338. if (!(up->ier & UART_IER_THRI)) {
  339. up->ier |= UART_IER_THRI;
  340. serial_out(up, UART_IER, up->ier);
  341. }
  342. }
  343. static void serial_omap_start_tx(struct uart_port *port)
  344. {
  345. struct uart_omap_port *up = to_uart_omap_port(port);
  346. int res;
  347. pm_runtime_get_sync(up->dev);
  348. /* Handle RS-485 */
  349. if (port->rs485.flags & SER_RS485_ENABLED) {
  350. /* Fire THR interrupts when FIFO is below trigger level */
  351. up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
  352. serial_out(up, UART_OMAP_SCR, up->scr);
  353. /* if rts not already enabled */
  354. res = (port->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
  355. if (gpio_get_value(up->rts_gpio) != res) {
  356. gpio_set_value(up->rts_gpio, res);
  357. if (port->rs485.delay_rts_before_send > 0)
  358. mdelay(port->rs485.delay_rts_before_send);
  359. }
  360. }
  361. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  362. !(port->rs485.flags & SER_RS485_RX_DURING_TX))
  363. serial_omap_stop_rx(port);
  364. serial_omap_enable_ier_thri(up);
  365. pm_runtime_mark_last_busy(up->dev);
  366. pm_runtime_put_autosuspend(up->dev);
  367. }
  368. static void serial_omap_throttle(struct uart_port *port)
  369. {
  370. struct uart_omap_port *up = to_uart_omap_port(port);
  371. unsigned long flags;
  372. pm_runtime_get_sync(up->dev);
  373. spin_lock_irqsave(&up->port.lock, flags);
  374. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  375. serial_out(up, UART_IER, up->ier);
  376. spin_unlock_irqrestore(&up->port.lock, flags);
  377. pm_runtime_mark_last_busy(up->dev);
  378. pm_runtime_put_autosuspend(up->dev);
  379. }
  380. static void serial_omap_unthrottle(struct uart_port *port)
  381. {
  382. struct uart_omap_port *up = to_uart_omap_port(port);
  383. unsigned long flags;
  384. pm_runtime_get_sync(up->dev);
  385. spin_lock_irqsave(&up->port.lock, flags);
  386. up->ier |= UART_IER_RLSI | UART_IER_RDI;
  387. serial_out(up, UART_IER, up->ier);
  388. spin_unlock_irqrestore(&up->port.lock, flags);
  389. pm_runtime_mark_last_busy(up->dev);
  390. pm_runtime_put_autosuspend(up->dev);
  391. }
  392. static unsigned int check_modem_status(struct uart_omap_port *up)
  393. {
  394. unsigned int status;
  395. status = serial_in(up, UART_MSR);
  396. status |= up->msr_saved_flags;
  397. up->msr_saved_flags = 0;
  398. if ((status & UART_MSR_ANY_DELTA) == 0)
  399. return status;
  400. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  401. up->port.state != NULL) {
  402. if (status & UART_MSR_TERI)
  403. up->port.icount.rng++;
  404. if (status & UART_MSR_DDSR)
  405. up->port.icount.dsr++;
  406. if (status & UART_MSR_DDCD)
  407. uart_handle_dcd_change
  408. (&up->port, status & UART_MSR_DCD);
  409. if (status & UART_MSR_DCTS)
  410. uart_handle_cts_change
  411. (&up->port, status & UART_MSR_CTS);
  412. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  413. }
  414. return status;
  415. }
  416. static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
  417. {
  418. unsigned int flag;
  419. unsigned char ch = 0;
  420. if (likely(lsr & UART_LSR_DR))
  421. ch = serial_in(up, UART_RX);
  422. up->port.icount.rx++;
  423. flag = TTY_NORMAL;
  424. if (lsr & UART_LSR_BI) {
  425. flag = TTY_BREAK;
  426. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  427. up->port.icount.brk++;
  428. /*
  429. * We do the SysRQ and SAK checking
  430. * here because otherwise the break
  431. * may get masked by ignore_status_mask
  432. * or read_status_mask.
  433. */
  434. if (uart_handle_break(&up->port))
  435. return;
  436. }
  437. if (lsr & UART_LSR_PE) {
  438. flag = TTY_PARITY;
  439. up->port.icount.parity++;
  440. }
  441. if (lsr & UART_LSR_FE) {
  442. flag = TTY_FRAME;
  443. up->port.icount.frame++;
  444. }
  445. if (lsr & UART_LSR_OE)
  446. up->port.icount.overrun++;
  447. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  448. if (up->port.line == up->port.cons->index) {
  449. /* Recover the break flag from console xmit */
  450. lsr |= up->lsr_break_flag;
  451. }
  452. #endif
  453. uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
  454. }
  455. static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
  456. {
  457. unsigned char ch = 0;
  458. unsigned int flag;
  459. if (!(lsr & UART_LSR_DR))
  460. return;
  461. ch = serial_in(up, UART_RX);
  462. flag = TTY_NORMAL;
  463. up->port.icount.rx++;
  464. if (uart_handle_sysrq_char(&up->port, ch))
  465. return;
  466. uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
  467. }
  468. /**
  469. * serial_omap_irq() - This handles the interrupt from one port
  470. * @irq: uart port irq number
  471. * @dev_id: uart port info
  472. */
  473. static irqreturn_t serial_omap_irq(int irq, void *dev_id)
  474. {
  475. struct uart_omap_port *up = dev_id;
  476. unsigned int iir, lsr;
  477. unsigned int type;
  478. irqreturn_t ret = IRQ_NONE;
  479. int max_count = 256;
  480. spin_lock(&up->port.lock);
  481. pm_runtime_get_sync(up->dev);
  482. do {
  483. iir = serial_in(up, UART_IIR);
  484. if (iir & UART_IIR_NO_INT)
  485. break;
  486. ret = IRQ_HANDLED;
  487. lsr = serial_in(up, UART_LSR);
  488. /* extract IRQ type from IIR register */
  489. type = iir & 0x3e;
  490. switch (type) {
  491. case UART_IIR_MSI:
  492. check_modem_status(up);
  493. break;
  494. case UART_IIR_THRI:
  495. transmit_chars(up, lsr);
  496. break;
  497. case UART_IIR_RX_TIMEOUT:
  498. /* FALLTHROUGH */
  499. case UART_IIR_RDI:
  500. serial_omap_rdi(up, lsr);
  501. break;
  502. case UART_IIR_RLSI:
  503. serial_omap_rlsi(up, lsr);
  504. break;
  505. case UART_IIR_CTS_RTS_DSR:
  506. /* simply try again */
  507. break;
  508. case UART_IIR_XOFF:
  509. /* FALLTHROUGH */
  510. default:
  511. break;
  512. }
  513. } while (!(iir & UART_IIR_NO_INT) && max_count--);
  514. spin_unlock(&up->port.lock);
  515. tty_flip_buffer_push(&up->port.state->port);
  516. pm_runtime_mark_last_busy(up->dev);
  517. pm_runtime_put_autosuspend(up->dev);
  518. up->port_activity = jiffies;
  519. return ret;
  520. }
  521. static unsigned int serial_omap_tx_empty(struct uart_port *port)
  522. {
  523. struct uart_omap_port *up = to_uart_omap_port(port);
  524. unsigned long flags = 0;
  525. unsigned int ret = 0;
  526. pm_runtime_get_sync(up->dev);
  527. dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
  528. spin_lock_irqsave(&up->port.lock, flags);
  529. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  530. spin_unlock_irqrestore(&up->port.lock, flags);
  531. pm_runtime_mark_last_busy(up->dev);
  532. pm_runtime_put_autosuspend(up->dev);
  533. return ret;
  534. }
  535. static unsigned int serial_omap_get_mctrl(struct uart_port *port)
  536. {
  537. struct uart_omap_port *up = to_uart_omap_port(port);
  538. unsigned int status;
  539. unsigned int ret = 0;
  540. pm_runtime_get_sync(up->dev);
  541. status = check_modem_status(up);
  542. pm_runtime_mark_last_busy(up->dev);
  543. pm_runtime_put_autosuspend(up->dev);
  544. dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
  545. if (status & UART_MSR_DCD)
  546. ret |= TIOCM_CAR;
  547. if (status & UART_MSR_RI)
  548. ret |= TIOCM_RNG;
  549. if (status & UART_MSR_DSR)
  550. ret |= TIOCM_DSR;
  551. if (status & UART_MSR_CTS)
  552. ret |= TIOCM_CTS;
  553. return ret;
  554. }
  555. static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
  556. {
  557. struct uart_omap_port *up = to_uart_omap_port(port);
  558. unsigned char mcr = 0, old_mcr, lcr;
  559. dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
  560. if (mctrl & TIOCM_RTS)
  561. mcr |= UART_MCR_RTS;
  562. if (mctrl & TIOCM_DTR)
  563. mcr |= UART_MCR_DTR;
  564. if (mctrl & TIOCM_OUT1)
  565. mcr |= UART_MCR_OUT1;
  566. if (mctrl & TIOCM_OUT2)
  567. mcr |= UART_MCR_OUT2;
  568. if (mctrl & TIOCM_LOOP)
  569. mcr |= UART_MCR_LOOP;
  570. pm_runtime_get_sync(up->dev);
  571. old_mcr = serial_in(up, UART_MCR);
  572. old_mcr &= ~(UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_OUT1 |
  573. UART_MCR_DTR | UART_MCR_RTS);
  574. up->mcr = old_mcr | mcr;
  575. serial_out(up, UART_MCR, up->mcr);
  576. /* Turn off autoRTS if RTS is lowered; restore autoRTS if RTS raised */
  577. lcr = serial_in(up, UART_LCR);
  578. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  579. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  580. up->efr |= UART_EFR_RTS;
  581. else
  582. up->efr &= UART_EFR_RTS;
  583. serial_out(up, UART_EFR, up->efr);
  584. serial_out(up, UART_LCR, lcr);
  585. pm_runtime_mark_last_busy(up->dev);
  586. pm_runtime_put_autosuspend(up->dev);
  587. }
  588. static void serial_omap_break_ctl(struct uart_port *port, int break_state)
  589. {
  590. struct uart_omap_port *up = to_uart_omap_port(port);
  591. unsigned long flags = 0;
  592. dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
  593. pm_runtime_get_sync(up->dev);
  594. spin_lock_irqsave(&up->port.lock, flags);
  595. if (break_state == -1)
  596. up->lcr |= UART_LCR_SBC;
  597. else
  598. up->lcr &= ~UART_LCR_SBC;
  599. serial_out(up, UART_LCR, up->lcr);
  600. spin_unlock_irqrestore(&up->port.lock, flags);
  601. pm_runtime_mark_last_busy(up->dev);
  602. pm_runtime_put_autosuspend(up->dev);
  603. }
  604. static int serial_omap_startup(struct uart_port *port)
  605. {
  606. struct uart_omap_port *up = to_uart_omap_port(port);
  607. unsigned long flags = 0;
  608. int retval;
  609. /*
  610. * Allocate the IRQ
  611. */
  612. retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
  613. up->name, up);
  614. if (retval)
  615. return retval;
  616. /* Optional wake-up IRQ */
  617. if (up->wakeirq) {
  618. retval = dev_pm_set_dedicated_wake_irq(up->dev, up->wakeirq);
  619. if (retval) {
  620. free_irq(up->port.irq, up);
  621. return retval;
  622. }
  623. }
  624. dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
  625. pm_runtime_get_sync(up->dev);
  626. /*
  627. * Clear the FIFO buffers and disable them.
  628. * (they will be reenabled in set_termios())
  629. */
  630. serial_omap_clear_fifos(up);
  631. /*
  632. * Clear the interrupt registers.
  633. */
  634. (void) serial_in(up, UART_LSR);
  635. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  636. (void) serial_in(up, UART_RX);
  637. (void) serial_in(up, UART_IIR);
  638. (void) serial_in(up, UART_MSR);
  639. /*
  640. * Now, initialize the UART
  641. */
  642. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  643. spin_lock_irqsave(&up->port.lock, flags);
  644. /*
  645. * Most PC uarts need OUT2 raised to enable interrupts.
  646. */
  647. up->port.mctrl |= TIOCM_OUT2;
  648. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  649. spin_unlock_irqrestore(&up->port.lock, flags);
  650. up->msr_saved_flags = 0;
  651. /*
  652. * Finally, enable interrupts. Note: Modem status interrupts
  653. * are set via set_termios(), which will be occurring imminently
  654. * anyway, so we don't enable them here.
  655. */
  656. up->ier = UART_IER_RLSI | UART_IER_RDI;
  657. serial_out(up, UART_IER, up->ier);
  658. /* Enable module level wake up */
  659. up->wer = OMAP_UART_WER_MOD_WKUP;
  660. if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
  661. up->wer |= OMAP_UART_TX_WAKEUP_EN;
  662. serial_out(up, UART_OMAP_WER, up->wer);
  663. pm_runtime_mark_last_busy(up->dev);
  664. pm_runtime_put_autosuspend(up->dev);
  665. up->port_activity = jiffies;
  666. return 0;
  667. }
  668. static void serial_omap_shutdown(struct uart_port *port)
  669. {
  670. struct uart_omap_port *up = to_uart_omap_port(port);
  671. unsigned long flags = 0;
  672. dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
  673. pm_runtime_get_sync(up->dev);
  674. /*
  675. * Disable interrupts from this port
  676. */
  677. up->ier = 0;
  678. serial_out(up, UART_IER, 0);
  679. spin_lock_irqsave(&up->port.lock, flags);
  680. up->port.mctrl &= ~TIOCM_OUT2;
  681. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  682. spin_unlock_irqrestore(&up->port.lock, flags);
  683. /*
  684. * Disable break condition and FIFOs
  685. */
  686. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  687. serial_omap_clear_fifos(up);
  688. /*
  689. * Read data port to reset things, and then free the irq
  690. */
  691. if (serial_in(up, UART_LSR) & UART_LSR_DR)
  692. (void) serial_in(up, UART_RX);
  693. pm_runtime_mark_last_busy(up->dev);
  694. pm_runtime_put_autosuspend(up->dev);
  695. free_irq(up->port.irq, up);
  696. dev_pm_clear_wake_irq(up->dev);
  697. }
  698. static void serial_omap_uart_qos_work(struct work_struct *work)
  699. {
  700. struct uart_omap_port *up = container_of(work, struct uart_omap_port,
  701. qos_work);
  702. pm_qos_update_request(&up->pm_qos_request, up->latency);
  703. }
  704. static void
  705. serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
  706. struct ktermios *old)
  707. {
  708. struct uart_omap_port *up = to_uart_omap_port(port);
  709. unsigned char cval = 0;
  710. unsigned long flags = 0;
  711. unsigned int baud, quot;
  712. switch (termios->c_cflag & CSIZE) {
  713. case CS5:
  714. cval = UART_LCR_WLEN5;
  715. break;
  716. case CS6:
  717. cval = UART_LCR_WLEN6;
  718. break;
  719. case CS7:
  720. cval = UART_LCR_WLEN7;
  721. break;
  722. default:
  723. case CS8:
  724. cval = UART_LCR_WLEN8;
  725. break;
  726. }
  727. if (termios->c_cflag & CSTOPB)
  728. cval |= UART_LCR_STOP;
  729. if (termios->c_cflag & PARENB)
  730. cval |= UART_LCR_PARITY;
  731. if (!(termios->c_cflag & PARODD))
  732. cval |= UART_LCR_EPAR;
  733. if (termios->c_cflag & CMSPAR)
  734. cval |= UART_LCR_SPAR;
  735. /*
  736. * Ask the core to calculate the divisor for us.
  737. */
  738. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
  739. quot = serial_omap_get_divisor(port, baud);
  740. /* calculate wakeup latency constraint */
  741. up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
  742. up->latency = up->calc_latency;
  743. schedule_work(&up->qos_work);
  744. up->dll = quot & 0xff;
  745. up->dlh = quot >> 8;
  746. up->mdr1 = UART_OMAP_MDR1_DISABLE;
  747. up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
  748. UART_FCR_ENABLE_FIFO;
  749. /*
  750. * Ok, we're now changing the port state. Do it with
  751. * interrupts disabled.
  752. */
  753. pm_runtime_get_sync(up->dev);
  754. spin_lock_irqsave(&up->port.lock, flags);
  755. /*
  756. * Update the per-port timeout.
  757. */
  758. uart_update_timeout(port, termios->c_cflag, baud);
  759. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  760. if (termios->c_iflag & INPCK)
  761. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  762. if (termios->c_iflag & (BRKINT | PARMRK))
  763. up->port.read_status_mask |= UART_LSR_BI;
  764. /*
  765. * Characters to ignore
  766. */
  767. up->port.ignore_status_mask = 0;
  768. if (termios->c_iflag & IGNPAR)
  769. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  770. if (termios->c_iflag & IGNBRK) {
  771. up->port.ignore_status_mask |= UART_LSR_BI;
  772. /*
  773. * If we're ignoring parity and break indicators,
  774. * ignore overruns too (for real raw support).
  775. */
  776. if (termios->c_iflag & IGNPAR)
  777. up->port.ignore_status_mask |= UART_LSR_OE;
  778. }
  779. /*
  780. * ignore all characters if CREAD is not set
  781. */
  782. if ((termios->c_cflag & CREAD) == 0)
  783. up->port.ignore_status_mask |= UART_LSR_DR;
  784. /*
  785. * Modem status interrupts
  786. */
  787. up->ier &= ~UART_IER_MSI;
  788. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  789. up->ier |= UART_IER_MSI;
  790. serial_out(up, UART_IER, up->ier);
  791. serial_out(up, UART_LCR, cval); /* reset DLAB */
  792. up->lcr = cval;
  793. up->scr = 0;
  794. /* FIFOs and DMA Settings */
  795. /* FCR can be changed only when the
  796. * baud clock is not running
  797. * DLL_REG and DLH_REG set to 0.
  798. */
  799. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  800. serial_out(up, UART_DLL, 0);
  801. serial_out(up, UART_DLM, 0);
  802. serial_out(up, UART_LCR, 0);
  803. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  804. up->efr = serial_in(up, UART_EFR) & ~UART_EFR_ECB;
  805. up->efr &= ~UART_EFR_SCD;
  806. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  807. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  808. up->mcr = serial_in(up, UART_MCR) & ~UART_MCR_TCRTLR;
  809. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  810. /* FIFO ENABLE, DMA MODE */
  811. up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
  812. /*
  813. * NOTE: Setting OMAP_UART_SCR_RX_TRIG_GRANU1_MASK
  814. * sets Enables the granularity of 1 for TRIGGER RX
  815. * level. Along with setting RX FIFO trigger level
  816. * to 1 (as noted below, 16 characters) and TLR[3:0]
  817. * to zero this will result RX FIFO threshold level
  818. * to 1 character, instead of 16 as noted in comment
  819. * below.
  820. */
  821. /* Set receive FIFO threshold to 16 characters and
  822. * transmit FIFO threshold to 32 spaces
  823. */
  824. up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
  825. up->fcr &= ~OMAP_UART_FCR_TX_FIFO_TRIG_MASK;
  826. up->fcr |= UART_FCR6_R_TRIGGER_16 | UART_FCR6_T_TRIGGER_24 |
  827. UART_FCR_ENABLE_FIFO;
  828. serial_out(up, UART_FCR, up->fcr);
  829. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  830. serial_out(up, UART_OMAP_SCR, up->scr);
  831. /* Reset UART_MCR_TCRTLR: this must be done with the EFR_ECB bit set */
  832. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  833. serial_out(up, UART_MCR, up->mcr);
  834. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  835. serial_out(up, UART_EFR, up->efr);
  836. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  837. /* Protocol, Baud Rate, and Interrupt Settings */
  838. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  839. serial_omap_mdr1_errataset(up, up->mdr1);
  840. else
  841. serial_out(up, UART_OMAP_MDR1, up->mdr1);
  842. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  843. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  844. serial_out(up, UART_LCR, 0);
  845. serial_out(up, UART_IER, 0);
  846. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  847. serial_out(up, UART_DLL, up->dll); /* LS of divisor */
  848. serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
  849. serial_out(up, UART_LCR, 0);
  850. serial_out(up, UART_IER, up->ier);
  851. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  852. serial_out(up, UART_EFR, up->efr);
  853. serial_out(up, UART_LCR, cval);
  854. if (!serial_omap_baud_is_mode16(port, baud))
  855. up->mdr1 = UART_OMAP_MDR1_13X_MODE;
  856. else
  857. up->mdr1 = UART_OMAP_MDR1_16X_MODE;
  858. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  859. serial_omap_mdr1_errataset(up, up->mdr1);
  860. else
  861. serial_out(up, UART_OMAP_MDR1, up->mdr1);
  862. /* Configure flow control */
  863. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  864. /* XON1/XOFF1 accessible mode B, TCRTLR=0, ECB=0 */
  865. serial_out(up, UART_XON1, termios->c_cc[VSTART]);
  866. serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
  867. /* Enable access to TCR/TLR */
  868. serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
  869. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  870. serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
  871. serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
  872. up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
  873. if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
  874. /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
  875. up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  876. up->efr |= UART_EFR_CTS;
  877. } else {
  878. /* Disable AUTORTS and AUTOCTS */
  879. up->efr &= ~(UART_EFR_CTS | UART_EFR_RTS);
  880. }
  881. if (up->port.flags & UPF_SOFT_FLOW) {
  882. /* clear SW control mode bits */
  883. up->efr &= OMAP_UART_SW_CLR;
  884. /*
  885. * IXON Flag:
  886. * Enable XON/XOFF flow control on input.
  887. * Receiver compares XON1, XOFF1.
  888. */
  889. if (termios->c_iflag & IXON)
  890. up->efr |= OMAP_UART_SW_RX;
  891. /*
  892. * IXOFF Flag:
  893. * Enable XON/XOFF flow control on output.
  894. * Transmit XON1, XOFF1
  895. */
  896. if (termios->c_iflag & IXOFF) {
  897. up->port.status |= UPSTAT_AUTOXOFF;
  898. up->efr |= OMAP_UART_SW_TX;
  899. }
  900. /*
  901. * IXANY Flag:
  902. * Enable any character to restart output.
  903. * Operation resumes after receiving any
  904. * character after recognition of the XOFF character
  905. */
  906. if (termios->c_iflag & IXANY)
  907. up->mcr |= UART_MCR_XONANY;
  908. else
  909. up->mcr &= ~UART_MCR_XONANY;
  910. }
  911. serial_out(up, UART_MCR, up->mcr);
  912. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  913. serial_out(up, UART_EFR, up->efr);
  914. serial_out(up, UART_LCR, up->lcr);
  915. serial_omap_set_mctrl(&up->port, up->port.mctrl);
  916. spin_unlock_irqrestore(&up->port.lock, flags);
  917. pm_runtime_mark_last_busy(up->dev);
  918. pm_runtime_put_autosuspend(up->dev);
  919. dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
  920. }
  921. static void
  922. serial_omap_pm(struct uart_port *port, unsigned int state,
  923. unsigned int oldstate)
  924. {
  925. struct uart_omap_port *up = to_uart_omap_port(port);
  926. unsigned char efr;
  927. dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
  928. pm_runtime_get_sync(up->dev);
  929. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  930. efr = serial_in(up, UART_EFR);
  931. serial_out(up, UART_EFR, efr | UART_EFR_ECB);
  932. serial_out(up, UART_LCR, 0);
  933. serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
  934. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  935. serial_out(up, UART_EFR, efr);
  936. serial_out(up, UART_LCR, 0);
  937. pm_runtime_mark_last_busy(up->dev);
  938. pm_runtime_put_autosuspend(up->dev);
  939. }
  940. static void serial_omap_release_port(struct uart_port *port)
  941. {
  942. dev_dbg(port->dev, "serial_omap_release_port+\n");
  943. }
  944. static int serial_omap_request_port(struct uart_port *port)
  945. {
  946. dev_dbg(port->dev, "serial_omap_request_port+\n");
  947. return 0;
  948. }
  949. static void serial_omap_config_port(struct uart_port *port, int flags)
  950. {
  951. struct uart_omap_port *up = to_uart_omap_port(port);
  952. dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
  953. up->port.line);
  954. up->port.type = PORT_OMAP;
  955. up->port.flags |= UPF_SOFT_FLOW | UPF_HARD_FLOW;
  956. }
  957. static int
  958. serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
  959. {
  960. /* we don't want the core code to modify any port params */
  961. dev_dbg(port->dev, "serial_omap_verify_port+\n");
  962. return -EINVAL;
  963. }
  964. static const char *
  965. serial_omap_type(struct uart_port *port)
  966. {
  967. struct uart_omap_port *up = to_uart_omap_port(port);
  968. dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
  969. return up->name;
  970. }
  971. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  972. static inline void wait_for_xmitr(struct uart_omap_port *up)
  973. {
  974. unsigned int status, tmout = 10000;
  975. /* Wait up to 10ms for the character(s) to be sent. */
  976. do {
  977. status = serial_in(up, UART_LSR);
  978. if (status & UART_LSR_BI)
  979. up->lsr_break_flag = UART_LSR_BI;
  980. if (--tmout == 0)
  981. break;
  982. udelay(1);
  983. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  984. /* Wait up to 1s for flow control if necessary */
  985. if (up->port.flags & UPF_CONS_FLOW) {
  986. tmout = 1000000;
  987. for (tmout = 1000000; tmout; tmout--) {
  988. unsigned int msr = serial_in(up, UART_MSR);
  989. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  990. if (msr & UART_MSR_CTS)
  991. break;
  992. udelay(1);
  993. }
  994. }
  995. }
  996. #ifdef CONFIG_CONSOLE_POLL
  997. static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
  998. {
  999. struct uart_omap_port *up = to_uart_omap_port(port);
  1000. pm_runtime_get_sync(up->dev);
  1001. wait_for_xmitr(up);
  1002. serial_out(up, UART_TX, ch);
  1003. pm_runtime_mark_last_busy(up->dev);
  1004. pm_runtime_put_autosuspend(up->dev);
  1005. }
  1006. static int serial_omap_poll_get_char(struct uart_port *port)
  1007. {
  1008. struct uart_omap_port *up = to_uart_omap_port(port);
  1009. unsigned int status;
  1010. pm_runtime_get_sync(up->dev);
  1011. status = serial_in(up, UART_LSR);
  1012. if (!(status & UART_LSR_DR)) {
  1013. status = NO_POLL_CHAR;
  1014. goto out;
  1015. }
  1016. status = serial_in(up, UART_RX);
  1017. out:
  1018. pm_runtime_mark_last_busy(up->dev);
  1019. pm_runtime_put_autosuspend(up->dev);
  1020. return status;
  1021. }
  1022. #endif /* CONFIG_CONSOLE_POLL */
  1023. #ifdef CONFIG_SERIAL_OMAP_CONSOLE
  1024. static struct uart_omap_port *serial_omap_console_ports[OMAP_MAX_HSUART_PORTS];
  1025. static struct uart_driver serial_omap_reg;
  1026. static void serial_omap_console_putchar(struct uart_port *port, int ch)
  1027. {
  1028. struct uart_omap_port *up = to_uart_omap_port(port);
  1029. wait_for_xmitr(up);
  1030. serial_out(up, UART_TX, ch);
  1031. }
  1032. static void
  1033. serial_omap_console_write(struct console *co, const char *s,
  1034. unsigned int count)
  1035. {
  1036. struct uart_omap_port *up = serial_omap_console_ports[co->index];
  1037. unsigned long flags;
  1038. unsigned int ier;
  1039. int locked = 1;
  1040. pm_runtime_get_sync(up->dev);
  1041. local_irq_save(flags);
  1042. if (up->port.sysrq)
  1043. locked = 0;
  1044. else if (oops_in_progress)
  1045. locked = spin_trylock(&up->port.lock);
  1046. else
  1047. spin_lock(&up->port.lock);
  1048. /*
  1049. * First save the IER then disable the interrupts
  1050. */
  1051. ier = serial_in(up, UART_IER);
  1052. serial_out(up, UART_IER, 0);
  1053. uart_console_write(&up->port, s, count, serial_omap_console_putchar);
  1054. /*
  1055. * Finally, wait for transmitter to become empty
  1056. * and restore the IER
  1057. */
  1058. wait_for_xmitr(up);
  1059. serial_out(up, UART_IER, ier);
  1060. /*
  1061. * The receive handling will happen properly because the
  1062. * receive ready bit will still be set; it is not cleared
  1063. * on read. However, modem control will not, we must
  1064. * call it if we have saved something in the saved flags
  1065. * while processing with interrupts off.
  1066. */
  1067. if (up->msr_saved_flags)
  1068. check_modem_status(up);
  1069. pm_runtime_mark_last_busy(up->dev);
  1070. pm_runtime_put_autosuspend(up->dev);
  1071. if (locked)
  1072. spin_unlock(&up->port.lock);
  1073. local_irq_restore(flags);
  1074. }
  1075. static int __init
  1076. serial_omap_console_setup(struct console *co, char *options)
  1077. {
  1078. struct uart_omap_port *up;
  1079. int baud = 115200;
  1080. int bits = 8;
  1081. int parity = 'n';
  1082. int flow = 'n';
  1083. if (serial_omap_console_ports[co->index] == NULL)
  1084. return -ENODEV;
  1085. up = serial_omap_console_ports[co->index];
  1086. if (options)
  1087. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1088. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  1089. }
  1090. static struct console serial_omap_console = {
  1091. .name = OMAP_SERIAL_NAME,
  1092. .write = serial_omap_console_write,
  1093. .device = uart_console_device,
  1094. .setup = serial_omap_console_setup,
  1095. .flags = CON_PRINTBUFFER,
  1096. .index = -1,
  1097. .data = &serial_omap_reg,
  1098. };
  1099. static void serial_omap_add_console_port(struct uart_omap_port *up)
  1100. {
  1101. serial_omap_console_ports[up->port.line] = up;
  1102. }
  1103. #define OMAP_CONSOLE (&serial_omap_console)
  1104. #else
  1105. #define OMAP_CONSOLE NULL
  1106. static inline void serial_omap_add_console_port(struct uart_omap_port *up)
  1107. {}
  1108. #endif
  1109. /* Enable or disable the rs485 support */
  1110. static int
  1111. serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
  1112. {
  1113. struct uart_omap_port *up = to_uart_omap_port(port);
  1114. unsigned int mode;
  1115. int val;
  1116. pm_runtime_get_sync(up->dev);
  1117. /* Disable interrupts from this port */
  1118. mode = up->ier;
  1119. up->ier = 0;
  1120. serial_out(up, UART_IER, 0);
  1121. /* store new config */
  1122. port->rs485 = *rs485conf;
  1123. /*
  1124. * Just as a precaution, only allow rs485
  1125. * to be enabled if the gpio pin is valid
  1126. */
  1127. if (gpio_is_valid(up->rts_gpio)) {
  1128. /* enable / disable rts */
  1129. val = (port->rs485.flags & SER_RS485_ENABLED) ?
  1130. SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND;
  1131. val = (port->rs485.flags & val) ? 1 : 0;
  1132. gpio_set_value(up->rts_gpio, val);
  1133. } else
  1134. port->rs485.flags &= ~SER_RS485_ENABLED;
  1135. /* Enable interrupts */
  1136. up->ier = mode;
  1137. serial_out(up, UART_IER, up->ier);
  1138. /* If RS-485 is disabled, make sure the THR interrupt is fired when
  1139. * TX FIFO is below the trigger level.
  1140. */
  1141. if (!(port->rs485.flags & SER_RS485_ENABLED) &&
  1142. (up->scr & OMAP_UART_SCR_TX_EMPTY)) {
  1143. up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
  1144. serial_out(up, UART_OMAP_SCR, up->scr);
  1145. }
  1146. pm_runtime_mark_last_busy(up->dev);
  1147. pm_runtime_put_autosuspend(up->dev);
  1148. return 0;
  1149. }
  1150. static struct uart_ops serial_omap_pops = {
  1151. .tx_empty = serial_omap_tx_empty,
  1152. .set_mctrl = serial_omap_set_mctrl,
  1153. .get_mctrl = serial_omap_get_mctrl,
  1154. .stop_tx = serial_omap_stop_tx,
  1155. .start_tx = serial_omap_start_tx,
  1156. .throttle = serial_omap_throttle,
  1157. .unthrottle = serial_omap_unthrottle,
  1158. .stop_rx = serial_omap_stop_rx,
  1159. .enable_ms = serial_omap_enable_ms,
  1160. .break_ctl = serial_omap_break_ctl,
  1161. .startup = serial_omap_startup,
  1162. .shutdown = serial_omap_shutdown,
  1163. .set_termios = serial_omap_set_termios,
  1164. .pm = serial_omap_pm,
  1165. .type = serial_omap_type,
  1166. .release_port = serial_omap_release_port,
  1167. .request_port = serial_omap_request_port,
  1168. .config_port = serial_omap_config_port,
  1169. .verify_port = serial_omap_verify_port,
  1170. #ifdef CONFIG_CONSOLE_POLL
  1171. .poll_put_char = serial_omap_poll_put_char,
  1172. .poll_get_char = serial_omap_poll_get_char,
  1173. #endif
  1174. };
  1175. static struct uart_driver serial_omap_reg = {
  1176. .owner = THIS_MODULE,
  1177. .driver_name = "OMAP-SERIAL",
  1178. .dev_name = OMAP_SERIAL_NAME,
  1179. .nr = OMAP_MAX_HSUART_PORTS,
  1180. .cons = OMAP_CONSOLE,
  1181. };
  1182. #ifdef CONFIG_PM_SLEEP
  1183. static int serial_omap_prepare(struct device *dev)
  1184. {
  1185. struct uart_omap_port *up = dev_get_drvdata(dev);
  1186. up->is_suspending = true;
  1187. return 0;
  1188. }
  1189. static void serial_omap_complete(struct device *dev)
  1190. {
  1191. struct uart_omap_port *up = dev_get_drvdata(dev);
  1192. up->is_suspending = false;
  1193. }
  1194. static int serial_omap_suspend(struct device *dev)
  1195. {
  1196. struct uart_omap_port *up = dev_get_drvdata(dev);
  1197. uart_suspend_port(&serial_omap_reg, &up->port);
  1198. flush_work(&up->qos_work);
  1199. if (device_may_wakeup(dev))
  1200. serial_omap_enable_wakeup(up, true);
  1201. else
  1202. serial_omap_enable_wakeup(up, false);
  1203. return 0;
  1204. }
  1205. static int serial_omap_resume(struct device *dev)
  1206. {
  1207. struct uart_omap_port *up = dev_get_drvdata(dev);
  1208. if (device_may_wakeup(dev))
  1209. serial_omap_enable_wakeup(up, false);
  1210. uart_resume_port(&serial_omap_reg, &up->port);
  1211. return 0;
  1212. }
  1213. #else
  1214. #define serial_omap_prepare NULL
  1215. #define serial_omap_complete NULL
  1216. #endif /* CONFIG_PM_SLEEP */
  1217. static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
  1218. {
  1219. u32 mvr, scheme;
  1220. u16 revision, major, minor;
  1221. mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
  1222. /* Check revision register scheme */
  1223. scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
  1224. switch (scheme) {
  1225. case 0: /* Legacy Scheme: OMAP2/3 */
  1226. /* MINOR_REV[0:4], MAJOR_REV[4:7] */
  1227. major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
  1228. OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
  1229. minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
  1230. break;
  1231. case 1:
  1232. /* New Scheme: OMAP4+ */
  1233. /* MINOR_REV[0:5], MAJOR_REV[8:10] */
  1234. major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
  1235. OMAP_UART_MVR_MAJ_SHIFT;
  1236. minor = (mvr & OMAP_UART_MVR_MIN_MASK);
  1237. break;
  1238. default:
  1239. dev_warn(up->dev,
  1240. "Unknown %s revision, defaulting to highest\n",
  1241. up->name);
  1242. /* highest possible revision */
  1243. major = 0xff;
  1244. minor = 0xff;
  1245. }
  1246. /* normalize revision for the driver */
  1247. revision = UART_BUILD_REVISION(major, minor);
  1248. switch (revision) {
  1249. case OMAP_UART_REV_46:
  1250. up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
  1251. UART_ERRATA_i291_DMA_FORCEIDLE);
  1252. break;
  1253. case OMAP_UART_REV_52:
  1254. up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
  1255. UART_ERRATA_i291_DMA_FORCEIDLE);
  1256. up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
  1257. break;
  1258. case OMAP_UART_REV_63:
  1259. up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
  1260. up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
  1261. break;
  1262. default:
  1263. break;
  1264. }
  1265. }
  1266. static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
  1267. {
  1268. struct omap_uart_port_info *omap_up_info;
  1269. omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
  1270. if (!omap_up_info)
  1271. return NULL; /* out of memory */
  1272. of_property_read_u32(dev->of_node, "clock-frequency",
  1273. &omap_up_info->uartclk);
  1274. return omap_up_info;
  1275. }
  1276. static int serial_omap_probe_rs485(struct uart_omap_port *up,
  1277. struct device_node *np)
  1278. {
  1279. struct serial_rs485 *rs485conf = &up->port.rs485;
  1280. u32 rs485_delay[2];
  1281. enum of_gpio_flags flags;
  1282. int ret;
  1283. rs485conf->flags = 0;
  1284. up->rts_gpio = -EINVAL;
  1285. if (!np)
  1286. return 0;
  1287. if (of_property_read_bool(np, "rs485-rts-active-high"))
  1288. rs485conf->flags |= SER_RS485_RTS_ON_SEND;
  1289. else
  1290. rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  1291. /* check for tx enable gpio */
  1292. up->rts_gpio = of_get_named_gpio_flags(np, "rts-gpio", 0, &flags);
  1293. if (gpio_is_valid(up->rts_gpio)) {
  1294. ret = devm_gpio_request(up->dev, up->rts_gpio, "omap-serial");
  1295. if (ret < 0)
  1296. return ret;
  1297. ret = gpio_direction_output(up->rts_gpio,
  1298. flags & SER_RS485_RTS_AFTER_SEND);
  1299. if (ret < 0)
  1300. return ret;
  1301. } else if (up->rts_gpio == -EPROBE_DEFER) {
  1302. return -EPROBE_DEFER;
  1303. } else {
  1304. up->rts_gpio = -EINVAL;
  1305. }
  1306. if (of_property_read_u32_array(np, "rs485-rts-delay",
  1307. rs485_delay, 2) == 0) {
  1308. rs485conf->delay_rts_before_send = rs485_delay[0];
  1309. rs485conf->delay_rts_after_send = rs485_delay[1];
  1310. }
  1311. if (of_property_read_bool(np, "rs485-rx-during-tx"))
  1312. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1313. if (of_property_read_bool(np, "linux,rs485-enabled-at-boot-time"))
  1314. rs485conf->flags |= SER_RS485_ENABLED;
  1315. return 0;
  1316. }
  1317. static int serial_omap_probe(struct platform_device *pdev)
  1318. {
  1319. struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
  1320. struct uart_omap_port *up;
  1321. struct resource *mem;
  1322. void __iomem *base;
  1323. int uartirq = 0;
  1324. int wakeirq = 0;
  1325. int ret;
  1326. /* The optional wakeirq may be specified in the board dts file */
  1327. if (pdev->dev.of_node) {
  1328. uartirq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1329. if (!uartirq)
  1330. return -EPROBE_DEFER;
  1331. wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1332. omap_up_info = of_get_uart_port_info(&pdev->dev);
  1333. pdev->dev.platform_data = omap_up_info;
  1334. } else {
  1335. uartirq = platform_get_irq(pdev, 0);
  1336. if (uartirq < 0)
  1337. return -EPROBE_DEFER;
  1338. }
  1339. up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
  1340. if (!up)
  1341. return -ENOMEM;
  1342. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1343. base = devm_ioremap_resource(&pdev->dev, mem);
  1344. if (IS_ERR(base))
  1345. return PTR_ERR(base);
  1346. up->dev = &pdev->dev;
  1347. up->port.dev = &pdev->dev;
  1348. up->port.type = PORT_OMAP;
  1349. up->port.iotype = UPIO_MEM;
  1350. up->port.irq = uartirq;
  1351. up->port.regshift = 2;
  1352. up->port.fifosize = 64;
  1353. up->port.ops = &serial_omap_pops;
  1354. if (pdev->dev.of_node)
  1355. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  1356. else
  1357. ret = pdev->id;
  1358. if (ret < 0) {
  1359. dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
  1360. ret);
  1361. goto err_port_line;
  1362. }
  1363. up->port.line = ret;
  1364. if (up->port.line >= OMAP_MAX_HSUART_PORTS) {
  1365. dev_err(&pdev->dev, "uart ID %d > MAX %d.\n", up->port.line,
  1366. OMAP_MAX_HSUART_PORTS);
  1367. ret = -ENXIO;
  1368. goto err_port_line;
  1369. }
  1370. up->wakeirq = wakeirq;
  1371. if (!up->wakeirq)
  1372. dev_info(up->port.dev, "no wakeirq for uart%d\n",
  1373. up->port.line);
  1374. ret = serial_omap_probe_rs485(up, pdev->dev.of_node);
  1375. if (ret < 0)
  1376. goto err_rs485;
  1377. sprintf(up->name, "OMAP UART%d", up->port.line);
  1378. up->port.mapbase = mem->start;
  1379. up->port.membase = base;
  1380. up->port.flags = omap_up_info->flags;
  1381. up->port.uartclk = omap_up_info->uartclk;
  1382. up->port.rs485_config = serial_omap_config_rs485;
  1383. if (!up->port.uartclk) {
  1384. up->port.uartclk = DEFAULT_CLK_SPEED;
  1385. dev_warn(&pdev->dev,
  1386. "No clock speed specified: using default: %d\n",
  1387. DEFAULT_CLK_SPEED);
  1388. }
  1389. up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1390. up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1391. pm_qos_add_request(&up->pm_qos_request,
  1392. PM_QOS_CPU_DMA_LATENCY, up->latency);
  1393. INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
  1394. platform_set_drvdata(pdev, up);
  1395. if (omap_up_info->autosuspend_timeout == 0)
  1396. omap_up_info->autosuspend_timeout = -1;
  1397. device_init_wakeup(up->dev, true);
  1398. pm_runtime_use_autosuspend(&pdev->dev);
  1399. pm_runtime_set_autosuspend_delay(&pdev->dev,
  1400. omap_up_info->autosuspend_timeout);
  1401. pm_runtime_irq_safe(&pdev->dev);
  1402. pm_runtime_enable(&pdev->dev);
  1403. pm_runtime_get_sync(&pdev->dev);
  1404. omap_serial_fill_features_erratas(up);
  1405. ui[up->port.line] = up;
  1406. serial_omap_add_console_port(up);
  1407. ret = uart_add_one_port(&serial_omap_reg, &up->port);
  1408. if (ret != 0)
  1409. goto err_add_port;
  1410. pm_runtime_mark_last_busy(up->dev);
  1411. pm_runtime_put_autosuspend(up->dev);
  1412. return 0;
  1413. err_add_port:
  1414. pm_runtime_put(&pdev->dev);
  1415. pm_runtime_disable(&pdev->dev);
  1416. pm_qos_remove_request(&up->pm_qos_request);
  1417. device_init_wakeup(up->dev, false);
  1418. err_rs485:
  1419. err_port_line:
  1420. return ret;
  1421. }
  1422. static int serial_omap_remove(struct platform_device *dev)
  1423. {
  1424. struct uart_omap_port *up = platform_get_drvdata(dev);
  1425. pm_runtime_put_sync(up->dev);
  1426. pm_runtime_disable(up->dev);
  1427. uart_remove_one_port(&serial_omap_reg, &up->port);
  1428. pm_qos_remove_request(&up->pm_qos_request);
  1429. device_init_wakeup(&dev->dev, false);
  1430. return 0;
  1431. }
  1432. /*
  1433. * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
  1434. * The access to uart register after MDR1 Access
  1435. * causes UART to corrupt data.
  1436. *
  1437. * Need a delay =
  1438. * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
  1439. * give 10 times as much
  1440. */
  1441. static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
  1442. {
  1443. u8 timeout = 255;
  1444. serial_out(up, UART_OMAP_MDR1, mdr1);
  1445. udelay(2);
  1446. serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
  1447. UART_FCR_CLEAR_RCVR);
  1448. /*
  1449. * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
  1450. * TX_FIFO_E bit is 1.
  1451. */
  1452. while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
  1453. (UART_LSR_THRE | UART_LSR_DR))) {
  1454. timeout--;
  1455. if (!timeout) {
  1456. /* Should *never* happen. we warn and carry on */
  1457. dev_crit(up->dev, "Errata i202: timedout %x\n",
  1458. serial_in(up, UART_LSR));
  1459. break;
  1460. }
  1461. udelay(1);
  1462. }
  1463. }
  1464. #ifdef CONFIG_PM
  1465. static void serial_omap_restore_context(struct uart_omap_port *up)
  1466. {
  1467. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  1468. serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
  1469. else
  1470. serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
  1471. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
  1472. serial_out(up, UART_EFR, UART_EFR_ECB);
  1473. serial_out(up, UART_LCR, 0x0); /* Operational mode */
  1474. serial_out(up, UART_IER, 0x0);
  1475. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
  1476. serial_out(up, UART_DLL, up->dll);
  1477. serial_out(up, UART_DLM, up->dlh);
  1478. serial_out(up, UART_LCR, 0x0); /* Operational mode */
  1479. serial_out(up, UART_IER, up->ier);
  1480. serial_out(up, UART_FCR, up->fcr);
  1481. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  1482. serial_out(up, UART_MCR, up->mcr);
  1483. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
  1484. serial_out(up, UART_OMAP_SCR, up->scr);
  1485. serial_out(up, UART_EFR, up->efr);
  1486. serial_out(up, UART_LCR, up->lcr);
  1487. if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
  1488. serial_omap_mdr1_errataset(up, up->mdr1);
  1489. else
  1490. serial_out(up, UART_OMAP_MDR1, up->mdr1);
  1491. serial_out(up, UART_OMAP_WER, up->wer);
  1492. }
  1493. static int serial_omap_runtime_suspend(struct device *dev)
  1494. {
  1495. struct uart_omap_port *up = dev_get_drvdata(dev);
  1496. if (!up)
  1497. return -EINVAL;
  1498. /*
  1499. * When using 'no_console_suspend', the console UART must not be
  1500. * suspended. Since driver suspend is managed by runtime suspend,
  1501. * preventing runtime suspend (by returning error) will keep device
  1502. * active during suspend.
  1503. */
  1504. if (up->is_suspending && !console_suspend_enabled &&
  1505. uart_console(&up->port))
  1506. return -EBUSY;
  1507. up->context_loss_cnt = serial_omap_get_context_loss_count(up);
  1508. serial_omap_enable_wakeup(up, true);
  1509. up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
  1510. schedule_work(&up->qos_work);
  1511. return 0;
  1512. }
  1513. static int serial_omap_runtime_resume(struct device *dev)
  1514. {
  1515. struct uart_omap_port *up = dev_get_drvdata(dev);
  1516. int loss_cnt = serial_omap_get_context_loss_count(up);
  1517. serial_omap_enable_wakeup(up, false);
  1518. if (loss_cnt < 0) {
  1519. dev_dbg(dev, "serial_omap_get_context_loss_count failed : %d\n",
  1520. loss_cnt);
  1521. serial_omap_restore_context(up);
  1522. } else if (up->context_loss_cnt != loss_cnt) {
  1523. serial_omap_restore_context(up);
  1524. }
  1525. up->latency = up->calc_latency;
  1526. schedule_work(&up->qos_work);
  1527. return 0;
  1528. }
  1529. #endif
  1530. static const struct dev_pm_ops serial_omap_dev_pm_ops = {
  1531. SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
  1532. SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
  1533. serial_omap_runtime_resume, NULL)
  1534. .prepare = serial_omap_prepare,
  1535. .complete = serial_omap_complete,
  1536. };
  1537. #if defined(CONFIG_OF)
  1538. static const struct of_device_id omap_serial_of_match[] = {
  1539. { .compatible = "ti,omap2-uart" },
  1540. { .compatible = "ti,omap3-uart" },
  1541. { .compatible = "ti,omap4-uart" },
  1542. {},
  1543. };
  1544. MODULE_DEVICE_TABLE(of, omap_serial_of_match);
  1545. #endif
  1546. static struct platform_driver serial_omap_driver = {
  1547. .probe = serial_omap_probe,
  1548. .remove = serial_omap_remove,
  1549. .driver = {
  1550. .name = DRIVER_NAME,
  1551. .pm = &serial_omap_dev_pm_ops,
  1552. .of_match_table = of_match_ptr(omap_serial_of_match),
  1553. },
  1554. };
  1555. static int __init serial_omap_init(void)
  1556. {
  1557. int ret;
  1558. ret = uart_register_driver(&serial_omap_reg);
  1559. if (ret != 0)
  1560. return ret;
  1561. ret = platform_driver_register(&serial_omap_driver);
  1562. if (ret != 0)
  1563. uart_unregister_driver(&serial_omap_reg);
  1564. return ret;
  1565. }
  1566. static void __exit serial_omap_exit(void)
  1567. {
  1568. platform_driver_unregister(&serial_omap_driver);
  1569. uart_unregister_driver(&serial_omap_reg);
  1570. }
  1571. module_init(serial_omap_init);
  1572. module_exit(serial_omap_exit);
  1573. MODULE_DESCRIPTION("OMAP High Speed UART driver");
  1574. MODULE_LICENSE("GPL");
  1575. MODULE_AUTHOR("Texas Instruments Inc");