imx.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. * Driver for Motorola/Freescale IMX serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Author: Sascha Hauer <sascha@saschahauer.de>
  7. * Copyright (C) 2004 Pengutronix
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  20. #define SUPPORT_SYSRQ
  21. #endif
  22. #include <linux/module.h>
  23. #include <linux/ioport.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/sysrq.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial_core.h>
  31. #include <linux/serial.h>
  32. #include <linux/clk.h>
  33. #include <linux/delay.h>
  34. #include <linux/rational.h>
  35. #include <linux/slab.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/io.h>
  39. #include <linux/dma-mapping.h>
  40. #include <asm/irq.h>
  41. #include <linux/platform_data/serial-imx.h>
  42. #include <linux/platform_data/dma-imx.h>
  43. /* Register definitions */
  44. #define URXD0 0x0 /* Receiver Register */
  45. #define URTX0 0x40 /* Transmitter Register */
  46. #define UCR1 0x80 /* Control Register 1 */
  47. #define UCR2 0x84 /* Control Register 2 */
  48. #define UCR3 0x88 /* Control Register 3 */
  49. #define UCR4 0x8c /* Control Register 4 */
  50. #define UFCR 0x90 /* FIFO Control Register */
  51. #define USR1 0x94 /* Status Register 1 */
  52. #define USR2 0x98 /* Status Register 2 */
  53. #define UESC 0x9c /* Escape Character Register */
  54. #define UTIM 0xa0 /* Escape Timer Register */
  55. #define UBIR 0xa4 /* BRM Incremental Register */
  56. #define UBMR 0xa8 /* BRM Modulator Register */
  57. #define UBRC 0xac /* Baud Rate Count Register */
  58. #define IMX21_ONEMS 0xb0 /* One Millisecond register */
  59. #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
  60. #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
  61. /* UART Control Register Bit Fields.*/
  62. #define URXD_DUMMY_READ (1<<16)
  63. #define URXD_CHARRDY (1<<15)
  64. #define URXD_ERR (1<<14)
  65. #define URXD_OVRRUN (1<<13)
  66. #define URXD_FRMERR (1<<12)
  67. #define URXD_BRK (1<<11)
  68. #define URXD_PRERR (1<<10)
  69. #define URXD_RX_DATA (0xFF<<0)
  70. #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
  71. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  72. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  73. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  74. #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
  75. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  76. #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
  77. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  78. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  79. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  80. #define UCR1_SNDBRK (1<<4) /* Send break */
  81. #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
  82. #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
  83. #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
  84. #define UCR1_DOZE (1<<1) /* Doze */
  85. #define UCR1_UARTEN (1<<0) /* UART enabled */
  86. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  87. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  88. #define UCR2_CTSC (1<<13) /* CTS pin control */
  89. #define UCR2_CTS (1<<12) /* Clear to send */
  90. #define UCR2_ESCEN (1<<11) /* Escape enable */
  91. #define UCR2_PREN (1<<8) /* Parity enable */
  92. #define UCR2_PROE (1<<7) /* Parity odd/even */
  93. #define UCR2_STPB (1<<6) /* Stop */
  94. #define UCR2_WS (1<<5) /* Word size */
  95. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  96. #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
  97. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  98. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  99. #define UCR2_SRST (1<<0) /* SW reset */
  100. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  101. #define UCR3_PARERREN (1<<12) /* Parity enable */
  102. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  103. #define UCR3_DSR (1<<10) /* Data set ready */
  104. #define UCR3_DCD (1<<9) /* Data carrier detect */
  105. #define UCR3_RI (1<<8) /* Ring indicator */
  106. #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
  107. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  108. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  109. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  110. #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
  111. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  112. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  113. #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
  114. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  115. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  116. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  117. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  118. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  119. #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
  120. #define UCR4_IRSC (1<<5) /* IR special case */
  121. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  122. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  123. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  124. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  125. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  126. #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
  127. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  128. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  129. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  130. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  131. #define USR1_RTSS (1<<14) /* RTS pin status */
  132. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  133. #define USR1_RTSD (1<<12) /* RTS delta */
  134. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  135. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  136. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  137. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  138. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  139. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  140. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  141. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  142. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  143. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  144. #define USR2_IDLE (1<<12) /* Idle condition */
  145. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  146. #define USR2_WAKE (1<<7) /* Wake */
  147. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  148. #define USR2_TXDC (1<<3) /* Transmitter complete */
  149. #define USR2_BRCD (1<<2) /* Break condition */
  150. #define USR2_ORE (1<<1) /* Overrun error */
  151. #define USR2_RDR (1<<0) /* Recv data ready */
  152. #define UTS_FRCPERR (1<<13) /* Force parity error */
  153. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  154. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  155. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  156. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  157. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  158. #define UTS_SOFTRST (1<<0) /* Software reset */
  159. /* We've been assigned a range on the "Low-density serial ports" major */
  160. #define SERIAL_IMX_MAJOR 207
  161. #define MINOR_START 16
  162. #define DEV_NAME "ttymxc"
  163. /*
  164. * This determines how often we check the modem status signals
  165. * for any change. They generally aren't connected to an IRQ
  166. * so we have to poll them. We also check immediately before
  167. * filling the TX fifo incase CTS has been dropped.
  168. */
  169. #define MCTRL_TIMEOUT (250*HZ/1000)
  170. #define DRIVER_NAME "IMX-uart"
  171. #define UART_NR 8
  172. /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
  173. enum imx_uart_type {
  174. IMX1_UART,
  175. IMX21_UART,
  176. IMX6Q_UART,
  177. };
  178. /* device type dependent stuff */
  179. struct imx_uart_data {
  180. unsigned uts_reg;
  181. enum imx_uart_type devtype;
  182. };
  183. struct imx_port {
  184. struct uart_port port;
  185. struct timer_list timer;
  186. unsigned int old_status;
  187. unsigned int have_rtscts:1;
  188. unsigned int dte_mode:1;
  189. unsigned int irda_inv_rx:1;
  190. unsigned int irda_inv_tx:1;
  191. unsigned short trcv_delay; /* transceiver delay */
  192. struct clk *clk_ipg;
  193. struct clk *clk_per;
  194. const struct imx_uart_data *devdata;
  195. /* DMA fields */
  196. unsigned int dma_is_inited:1;
  197. unsigned int dma_is_enabled:1;
  198. unsigned int dma_is_rxing:1;
  199. unsigned int dma_is_txing:1;
  200. struct dma_chan *dma_chan_rx, *dma_chan_tx;
  201. struct scatterlist rx_sgl, tx_sgl[2];
  202. void *rx_buf;
  203. unsigned int tx_bytes;
  204. unsigned int dma_tx_nents;
  205. wait_queue_head_t dma_wait;
  206. };
  207. struct imx_port_ucrs {
  208. unsigned int ucr1;
  209. unsigned int ucr2;
  210. unsigned int ucr3;
  211. };
  212. static struct imx_uart_data imx_uart_devdata[] = {
  213. [IMX1_UART] = {
  214. .uts_reg = IMX1_UTS,
  215. .devtype = IMX1_UART,
  216. },
  217. [IMX21_UART] = {
  218. .uts_reg = IMX21_UTS,
  219. .devtype = IMX21_UART,
  220. },
  221. [IMX6Q_UART] = {
  222. .uts_reg = IMX21_UTS,
  223. .devtype = IMX6Q_UART,
  224. },
  225. };
  226. static struct platform_device_id imx_uart_devtype[] = {
  227. {
  228. .name = "imx1-uart",
  229. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX1_UART],
  230. }, {
  231. .name = "imx21-uart",
  232. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
  233. }, {
  234. .name = "imx6q-uart",
  235. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX6Q_UART],
  236. }, {
  237. /* sentinel */
  238. }
  239. };
  240. MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
  241. static const struct of_device_id imx_uart_dt_ids[] = {
  242. { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
  243. { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
  244. { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
  245. { /* sentinel */ }
  246. };
  247. MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
  248. static inline unsigned uts_reg(struct imx_port *sport)
  249. {
  250. return sport->devdata->uts_reg;
  251. }
  252. static inline int is_imx1_uart(struct imx_port *sport)
  253. {
  254. return sport->devdata->devtype == IMX1_UART;
  255. }
  256. static inline int is_imx21_uart(struct imx_port *sport)
  257. {
  258. return sport->devdata->devtype == IMX21_UART;
  259. }
  260. static inline int is_imx6q_uart(struct imx_port *sport)
  261. {
  262. return sport->devdata->devtype == IMX6Q_UART;
  263. }
  264. /*
  265. * Save and restore functions for UCR1, UCR2 and UCR3 registers
  266. */
  267. #if defined(CONFIG_SERIAL_IMX_CONSOLE)
  268. static void imx_port_ucrs_save(struct uart_port *port,
  269. struct imx_port_ucrs *ucr)
  270. {
  271. /* save control registers */
  272. ucr->ucr1 = readl(port->membase + UCR1);
  273. ucr->ucr2 = readl(port->membase + UCR2);
  274. ucr->ucr3 = readl(port->membase + UCR3);
  275. }
  276. static void imx_port_ucrs_restore(struct uart_port *port,
  277. struct imx_port_ucrs *ucr)
  278. {
  279. /* restore control registers */
  280. writel(ucr->ucr1, port->membase + UCR1);
  281. writel(ucr->ucr2, port->membase + UCR2);
  282. writel(ucr->ucr3, port->membase + UCR3);
  283. }
  284. #endif
  285. /*
  286. * Handle any change of modem status signal since we were last called.
  287. */
  288. static void imx_mctrl_check(struct imx_port *sport)
  289. {
  290. unsigned int status, changed;
  291. status = sport->port.ops->get_mctrl(&sport->port);
  292. changed = status ^ sport->old_status;
  293. if (changed == 0)
  294. return;
  295. sport->old_status = status;
  296. if (changed & TIOCM_RI)
  297. sport->port.icount.rng++;
  298. if (changed & TIOCM_DSR)
  299. sport->port.icount.dsr++;
  300. if (changed & TIOCM_CAR)
  301. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  302. if (changed & TIOCM_CTS)
  303. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  304. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  305. }
  306. /*
  307. * This is our per-port timeout handler, for checking the
  308. * modem status signals.
  309. */
  310. static void imx_timeout(unsigned long data)
  311. {
  312. struct imx_port *sport = (struct imx_port *)data;
  313. unsigned long flags;
  314. if (sport->port.state) {
  315. spin_lock_irqsave(&sport->port.lock, flags);
  316. imx_mctrl_check(sport);
  317. spin_unlock_irqrestore(&sport->port.lock, flags);
  318. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  319. }
  320. }
  321. /*
  322. * interrupts disabled on entry
  323. */
  324. static void imx_stop_tx(struct uart_port *port)
  325. {
  326. struct imx_port *sport = (struct imx_port *)port;
  327. unsigned long temp;
  328. /*
  329. * We are maybe in the SMP context, so if the DMA TX thread is running
  330. * on other cpu, we have to wait for it to finish.
  331. */
  332. if (sport->dma_is_enabled && sport->dma_is_txing)
  333. return;
  334. temp = readl(port->membase + UCR1);
  335. writel(temp & ~UCR1_TXMPTYEN, port->membase + UCR1);
  336. /* in rs485 mode disable transmitter if shifter is empty */
  337. if (port->rs485.flags & SER_RS485_ENABLED &&
  338. readl(port->membase + USR2) & USR2_TXDC) {
  339. temp = readl(port->membase + UCR2);
  340. if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
  341. temp &= ~UCR2_CTS;
  342. else
  343. temp |= UCR2_CTS;
  344. writel(temp, port->membase + UCR2);
  345. temp = readl(port->membase + UCR4);
  346. temp &= ~UCR4_TCEN;
  347. writel(temp, port->membase + UCR4);
  348. }
  349. }
  350. /*
  351. * interrupts disabled on entry
  352. */
  353. static void imx_stop_rx(struct uart_port *port)
  354. {
  355. struct imx_port *sport = (struct imx_port *)port;
  356. unsigned long temp;
  357. if (sport->dma_is_enabled && sport->dma_is_rxing) {
  358. if (sport->port.suspended) {
  359. dmaengine_terminate_all(sport->dma_chan_rx);
  360. sport->dma_is_rxing = 0;
  361. } else {
  362. return;
  363. }
  364. }
  365. temp = readl(sport->port.membase + UCR2);
  366. writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
  367. /* disable the `Receiver Ready Interrrupt` */
  368. temp = readl(sport->port.membase + UCR1);
  369. writel(temp & ~UCR1_RRDYEN, sport->port.membase + UCR1);
  370. }
  371. /*
  372. * Set the modem control timer to fire immediately.
  373. */
  374. static void imx_enable_ms(struct uart_port *port)
  375. {
  376. struct imx_port *sport = (struct imx_port *)port;
  377. mod_timer(&sport->timer, jiffies);
  378. }
  379. static void imx_dma_tx(struct imx_port *sport);
  380. static inline void imx_transmit_buffer(struct imx_port *sport)
  381. {
  382. struct circ_buf *xmit = &sport->port.state->xmit;
  383. unsigned long temp;
  384. if (sport->port.x_char) {
  385. /* Send next char */
  386. writel(sport->port.x_char, sport->port.membase + URTX0);
  387. sport->port.icount.tx++;
  388. sport->port.x_char = 0;
  389. return;
  390. }
  391. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  392. imx_stop_tx(&sport->port);
  393. return;
  394. }
  395. if (sport->dma_is_enabled) {
  396. /*
  397. * We've just sent a X-char Ensure the TX DMA is enabled
  398. * and the TX IRQ is disabled.
  399. **/
  400. temp = readl(sport->port.membase + UCR1);
  401. temp &= ~UCR1_TXMPTYEN;
  402. if (sport->dma_is_txing) {
  403. temp |= UCR1_TDMAEN;
  404. writel(temp, sport->port.membase + UCR1);
  405. } else {
  406. writel(temp, sport->port.membase + UCR1);
  407. imx_dma_tx(sport);
  408. }
  409. }
  410. while (!uart_circ_empty(xmit) &&
  411. !(readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)) {
  412. /* send xmit->buf[xmit->tail]
  413. * out the port here */
  414. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  415. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  416. sport->port.icount.tx++;
  417. }
  418. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  419. uart_write_wakeup(&sport->port);
  420. if (uart_circ_empty(xmit))
  421. imx_stop_tx(&sport->port);
  422. }
  423. static void dma_tx_callback(void *data)
  424. {
  425. struct imx_port *sport = data;
  426. struct scatterlist *sgl = &sport->tx_sgl[0];
  427. struct circ_buf *xmit = &sport->port.state->xmit;
  428. unsigned long flags;
  429. unsigned long temp;
  430. spin_lock_irqsave(&sport->port.lock, flags);
  431. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  432. temp = readl(sport->port.membase + UCR1);
  433. temp &= ~UCR1_TDMAEN;
  434. writel(temp, sport->port.membase + UCR1);
  435. /* update the stat */
  436. xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
  437. sport->port.icount.tx += sport->tx_bytes;
  438. dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
  439. sport->dma_is_txing = 0;
  440. spin_unlock_irqrestore(&sport->port.lock, flags);
  441. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  442. uart_write_wakeup(&sport->port);
  443. if (waitqueue_active(&sport->dma_wait)) {
  444. wake_up(&sport->dma_wait);
  445. dev_dbg(sport->port.dev, "exit in %s.\n", __func__);
  446. return;
  447. }
  448. spin_lock_irqsave(&sport->port.lock, flags);
  449. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
  450. imx_dma_tx(sport);
  451. spin_unlock_irqrestore(&sport->port.lock, flags);
  452. }
  453. static void imx_dma_tx(struct imx_port *sport)
  454. {
  455. struct circ_buf *xmit = &sport->port.state->xmit;
  456. struct scatterlist *sgl = sport->tx_sgl;
  457. struct dma_async_tx_descriptor *desc;
  458. struct dma_chan *chan = sport->dma_chan_tx;
  459. struct device *dev = sport->port.dev;
  460. unsigned long temp;
  461. int ret;
  462. if (sport->dma_is_txing)
  463. return;
  464. sport->tx_bytes = uart_circ_chars_pending(xmit);
  465. if (xmit->tail < xmit->head) {
  466. sport->dma_tx_nents = 1;
  467. sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
  468. } else {
  469. sport->dma_tx_nents = 2;
  470. sg_init_table(sgl, 2);
  471. sg_set_buf(sgl, xmit->buf + xmit->tail,
  472. UART_XMIT_SIZE - xmit->tail);
  473. sg_set_buf(sgl + 1, xmit->buf, xmit->head);
  474. }
  475. ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  476. if (ret == 0) {
  477. dev_err(dev, "DMA mapping error for TX.\n");
  478. return;
  479. }
  480. desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
  481. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  482. if (!desc) {
  483. dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
  484. DMA_TO_DEVICE);
  485. dev_err(dev, "We cannot prepare for the TX slave dma!\n");
  486. return;
  487. }
  488. desc->callback = dma_tx_callback;
  489. desc->callback_param = sport;
  490. dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
  491. uart_circ_chars_pending(xmit));
  492. temp = readl(sport->port.membase + UCR1);
  493. temp |= UCR1_TDMAEN;
  494. writel(temp, sport->port.membase + UCR1);
  495. /* fire it */
  496. sport->dma_is_txing = 1;
  497. dmaengine_submit(desc);
  498. dma_async_issue_pending(chan);
  499. return;
  500. }
  501. /*
  502. * interrupts disabled on entry
  503. */
  504. static void imx_start_tx(struct uart_port *port)
  505. {
  506. struct imx_port *sport = (struct imx_port *)port;
  507. unsigned long temp;
  508. if (port->rs485.flags & SER_RS485_ENABLED) {
  509. /* enable transmitter and shifter empty irq */
  510. temp = readl(port->membase + UCR2);
  511. if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
  512. temp &= ~UCR2_CTS;
  513. else
  514. temp |= UCR2_CTS;
  515. writel(temp, port->membase + UCR2);
  516. temp = readl(port->membase + UCR4);
  517. temp |= UCR4_TCEN;
  518. writel(temp, port->membase + UCR4);
  519. }
  520. if (!sport->dma_is_enabled) {
  521. temp = readl(sport->port.membase + UCR1);
  522. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  523. }
  524. if (sport->dma_is_enabled) {
  525. if (sport->port.x_char) {
  526. /* We have X-char to send, so enable TX IRQ and
  527. * disable TX DMA to let TX interrupt to send X-char */
  528. temp = readl(sport->port.membase + UCR1);
  529. temp &= ~UCR1_TDMAEN;
  530. temp |= UCR1_TXMPTYEN;
  531. writel(temp, sport->port.membase + UCR1);
  532. return;
  533. }
  534. if (!uart_circ_empty(&port->state->xmit) &&
  535. !uart_tx_stopped(port))
  536. imx_dma_tx(sport);
  537. return;
  538. }
  539. }
  540. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  541. {
  542. struct imx_port *sport = dev_id;
  543. unsigned int val;
  544. unsigned long flags;
  545. spin_lock_irqsave(&sport->port.lock, flags);
  546. writel(USR1_RTSD, sport->port.membase + USR1);
  547. val = readl(sport->port.membase + USR1) & USR1_RTSS;
  548. uart_handle_cts_change(&sport->port, !!val);
  549. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  550. spin_unlock_irqrestore(&sport->port.lock, flags);
  551. return IRQ_HANDLED;
  552. }
  553. static irqreturn_t imx_txint(int irq, void *dev_id)
  554. {
  555. struct imx_port *sport = dev_id;
  556. unsigned long flags;
  557. spin_lock_irqsave(&sport->port.lock, flags);
  558. imx_transmit_buffer(sport);
  559. spin_unlock_irqrestore(&sport->port.lock, flags);
  560. return IRQ_HANDLED;
  561. }
  562. static irqreturn_t imx_rxint(int irq, void *dev_id)
  563. {
  564. struct imx_port *sport = dev_id;
  565. unsigned int rx, flg, ignored = 0;
  566. struct tty_port *port = &sport->port.state->port;
  567. unsigned long flags, temp;
  568. spin_lock_irqsave(&sport->port.lock, flags);
  569. while (readl(sport->port.membase + USR2) & USR2_RDR) {
  570. flg = TTY_NORMAL;
  571. sport->port.icount.rx++;
  572. rx = readl(sport->port.membase + URXD0);
  573. temp = readl(sport->port.membase + USR2);
  574. if (temp & USR2_BRCD) {
  575. writel(USR2_BRCD, sport->port.membase + USR2);
  576. if (uart_handle_break(&sport->port))
  577. continue;
  578. }
  579. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  580. continue;
  581. if (unlikely(rx & URXD_ERR)) {
  582. if (rx & URXD_BRK)
  583. sport->port.icount.brk++;
  584. else if (rx & URXD_PRERR)
  585. sport->port.icount.parity++;
  586. else if (rx & URXD_FRMERR)
  587. sport->port.icount.frame++;
  588. if (rx & URXD_OVRRUN)
  589. sport->port.icount.overrun++;
  590. if (rx & sport->port.ignore_status_mask) {
  591. if (++ignored > 100)
  592. goto out;
  593. continue;
  594. }
  595. rx &= (sport->port.read_status_mask | 0xFF);
  596. if (rx & URXD_BRK)
  597. flg = TTY_BREAK;
  598. else if (rx & URXD_PRERR)
  599. flg = TTY_PARITY;
  600. else if (rx & URXD_FRMERR)
  601. flg = TTY_FRAME;
  602. if (rx & URXD_OVRRUN)
  603. flg = TTY_OVERRUN;
  604. #ifdef SUPPORT_SYSRQ
  605. sport->port.sysrq = 0;
  606. #endif
  607. }
  608. if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
  609. goto out;
  610. tty_insert_flip_char(port, rx, flg);
  611. }
  612. out:
  613. spin_unlock_irqrestore(&sport->port.lock, flags);
  614. tty_flip_buffer_push(port);
  615. return IRQ_HANDLED;
  616. }
  617. static int start_rx_dma(struct imx_port *sport);
  618. /*
  619. * If the RXFIFO is filled with some data, and then we
  620. * arise a DMA operation to receive them.
  621. */
  622. static void imx_dma_rxint(struct imx_port *sport)
  623. {
  624. unsigned long temp;
  625. unsigned long flags;
  626. spin_lock_irqsave(&sport->port.lock, flags);
  627. temp = readl(sport->port.membase + USR2);
  628. if ((temp & USR2_RDR) && !sport->dma_is_rxing) {
  629. sport->dma_is_rxing = 1;
  630. /* disable the `Recerver Ready Interrrupt` */
  631. temp = readl(sport->port.membase + UCR1);
  632. temp &= ~(UCR1_RRDYEN);
  633. writel(temp, sport->port.membase + UCR1);
  634. /* tell the DMA to receive the data. */
  635. start_rx_dma(sport);
  636. }
  637. spin_unlock_irqrestore(&sport->port.lock, flags);
  638. }
  639. static irqreturn_t imx_int(int irq, void *dev_id)
  640. {
  641. struct imx_port *sport = dev_id;
  642. unsigned int sts;
  643. unsigned int sts2;
  644. sts = readl(sport->port.membase + USR1);
  645. sts2 = readl(sport->port.membase + USR2);
  646. if (sts & USR1_RRDY) {
  647. if (sport->dma_is_enabled)
  648. imx_dma_rxint(sport);
  649. else
  650. imx_rxint(irq, dev_id);
  651. }
  652. if ((sts & USR1_TRDY &&
  653. readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN) ||
  654. (sts2 & USR2_TXDC &&
  655. readl(sport->port.membase + UCR4) & UCR4_TCEN))
  656. imx_txint(irq, dev_id);
  657. if (sts & USR1_RTSD)
  658. imx_rtsint(irq, dev_id);
  659. if (sts & USR1_AWAKE)
  660. writel(USR1_AWAKE, sport->port.membase + USR1);
  661. if (sts2 & USR2_ORE) {
  662. dev_err(sport->port.dev, "Rx FIFO overrun\n");
  663. sport->port.icount.overrun++;
  664. writel(USR2_ORE, sport->port.membase + USR2);
  665. }
  666. return IRQ_HANDLED;
  667. }
  668. /*
  669. * Return TIOCSER_TEMT when transmitter is not busy.
  670. */
  671. static unsigned int imx_tx_empty(struct uart_port *port)
  672. {
  673. struct imx_port *sport = (struct imx_port *)port;
  674. unsigned int ret;
  675. ret = (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  676. /* If the TX DMA is working, return 0. */
  677. if (sport->dma_is_enabled && sport->dma_is_txing)
  678. ret = 0;
  679. return ret;
  680. }
  681. /*
  682. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  683. */
  684. static unsigned int imx_get_mctrl(struct uart_port *port)
  685. {
  686. struct imx_port *sport = (struct imx_port *)port;
  687. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  688. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  689. tmp |= TIOCM_CTS;
  690. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  691. tmp |= TIOCM_RTS;
  692. if (readl(sport->port.membase + uts_reg(sport)) & UTS_LOOP)
  693. tmp |= TIOCM_LOOP;
  694. return tmp;
  695. }
  696. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  697. {
  698. struct imx_port *sport = (struct imx_port *)port;
  699. unsigned long temp;
  700. if (!(port->rs485.flags & SER_RS485_ENABLED)) {
  701. temp = readl(sport->port.membase + UCR2);
  702. temp &= ~(UCR2_CTS | UCR2_CTSC);
  703. if (mctrl & TIOCM_RTS)
  704. temp |= UCR2_CTS | UCR2_CTSC;
  705. writel(temp, sport->port.membase + UCR2);
  706. }
  707. temp = readl(sport->port.membase + uts_reg(sport)) & ~UTS_LOOP;
  708. if (mctrl & TIOCM_LOOP)
  709. temp |= UTS_LOOP;
  710. writel(temp, sport->port.membase + uts_reg(sport));
  711. }
  712. /*
  713. * Interrupts always disabled.
  714. */
  715. static void imx_break_ctl(struct uart_port *port, int break_state)
  716. {
  717. struct imx_port *sport = (struct imx_port *)port;
  718. unsigned long flags, temp;
  719. spin_lock_irqsave(&sport->port.lock, flags);
  720. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  721. if (break_state != 0)
  722. temp |= UCR1_SNDBRK;
  723. writel(temp, sport->port.membase + UCR1);
  724. spin_unlock_irqrestore(&sport->port.lock, flags);
  725. }
  726. #define TXTL 2 /* reset default */
  727. #define RXTL 1 /* reset default */
  728. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  729. {
  730. unsigned int val;
  731. /* set receiver / transmitter trigger level */
  732. val = readl(sport->port.membase + UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
  733. val |= TXTL << UFCR_TXTL_SHF | RXTL;
  734. writel(val, sport->port.membase + UFCR);
  735. return 0;
  736. }
  737. #define RX_BUF_SIZE (PAGE_SIZE)
  738. static void imx_rx_dma_done(struct imx_port *sport)
  739. {
  740. unsigned long temp;
  741. unsigned long flags;
  742. spin_lock_irqsave(&sport->port.lock, flags);
  743. /* Enable this interrupt when the RXFIFO is empty. */
  744. temp = readl(sport->port.membase + UCR1);
  745. temp |= UCR1_RRDYEN;
  746. writel(temp, sport->port.membase + UCR1);
  747. sport->dma_is_rxing = 0;
  748. /* Is the shutdown waiting for us? */
  749. if (waitqueue_active(&sport->dma_wait))
  750. wake_up(&sport->dma_wait);
  751. spin_unlock_irqrestore(&sport->port.lock, flags);
  752. }
  753. /*
  754. * There are three kinds of RX DMA interrupts(such as in the MX6Q):
  755. * [1] the RX DMA buffer is full.
  756. * [2] the Aging timer expires(wait for 8 bytes long)
  757. * [3] the Idle Condition Detect(enabled the UCR4_IDDMAEN).
  758. *
  759. * The [2] is trigger when a character was been sitting in the FIFO
  760. * meanwhile [3] can wait for 32 bytes long when the RX line is
  761. * on IDLE state and RxFIFO is empty.
  762. */
  763. static void dma_rx_callback(void *data)
  764. {
  765. struct imx_port *sport = data;
  766. struct dma_chan *chan = sport->dma_chan_rx;
  767. struct scatterlist *sgl = &sport->rx_sgl;
  768. struct tty_port *port = &sport->port.state->port;
  769. struct dma_tx_state state;
  770. enum dma_status status;
  771. unsigned int count;
  772. /* unmap it first */
  773. dma_unmap_sg(sport->port.dev, sgl, 1, DMA_FROM_DEVICE);
  774. status = dmaengine_tx_status(chan, (dma_cookie_t)0, &state);
  775. count = RX_BUF_SIZE - state.residue;
  776. if (readl(sport->port.membase + USR2) & USR2_IDLE) {
  777. /* In condition [3] the SDMA counted up too early */
  778. count--;
  779. writel(USR2_IDLE, sport->port.membase + USR2);
  780. }
  781. dev_dbg(sport->port.dev, "We get %d bytes.\n", count);
  782. if (count) {
  783. if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ))
  784. tty_insert_flip_string(port, sport->rx_buf, count);
  785. tty_flip_buffer_push(port);
  786. start_rx_dma(sport);
  787. } else if (readl(sport->port.membase + USR2) & USR2_RDR) {
  788. /*
  789. * start rx_dma directly once data in RXFIFO, more efficient
  790. * than before:
  791. * 1. call imx_rx_dma_done to stop dma if no data received
  792. * 2. wait next RDR interrupt to start dma transfer.
  793. */
  794. start_rx_dma(sport);
  795. } else {
  796. /*
  797. * stop dma to prevent too many IDLE event trigged if no data
  798. * in RXFIFO
  799. */
  800. imx_rx_dma_done(sport);
  801. }
  802. }
  803. static int start_rx_dma(struct imx_port *sport)
  804. {
  805. struct scatterlist *sgl = &sport->rx_sgl;
  806. struct dma_chan *chan = sport->dma_chan_rx;
  807. struct device *dev = sport->port.dev;
  808. struct dma_async_tx_descriptor *desc;
  809. int ret;
  810. sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
  811. ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
  812. if (ret == 0) {
  813. dev_err(dev, "DMA mapping error for RX.\n");
  814. return -EINVAL;
  815. }
  816. desc = dmaengine_prep_slave_sg(chan, sgl, 1, DMA_DEV_TO_MEM,
  817. DMA_PREP_INTERRUPT);
  818. if (!desc) {
  819. dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
  820. dev_err(dev, "We cannot prepare for the RX slave dma!\n");
  821. return -EINVAL;
  822. }
  823. desc->callback = dma_rx_callback;
  824. desc->callback_param = sport;
  825. dev_dbg(dev, "RX: prepare for the DMA.\n");
  826. dmaengine_submit(desc);
  827. dma_async_issue_pending(chan);
  828. return 0;
  829. }
  830. static void imx_uart_dma_exit(struct imx_port *sport)
  831. {
  832. if (sport->dma_chan_rx) {
  833. dma_release_channel(sport->dma_chan_rx);
  834. sport->dma_chan_rx = NULL;
  835. kfree(sport->rx_buf);
  836. sport->rx_buf = NULL;
  837. }
  838. if (sport->dma_chan_tx) {
  839. dma_release_channel(sport->dma_chan_tx);
  840. sport->dma_chan_tx = NULL;
  841. }
  842. sport->dma_is_inited = 0;
  843. }
  844. static int imx_uart_dma_init(struct imx_port *sport)
  845. {
  846. struct dma_slave_config slave_config = {};
  847. struct device *dev = sport->port.dev;
  848. int ret;
  849. /* Prepare for RX : */
  850. sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
  851. if (!sport->dma_chan_rx) {
  852. dev_dbg(dev, "cannot get the DMA channel.\n");
  853. ret = -EINVAL;
  854. goto err;
  855. }
  856. slave_config.direction = DMA_DEV_TO_MEM;
  857. slave_config.src_addr = sport->port.mapbase + URXD0;
  858. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  859. slave_config.src_maxburst = RXTL;
  860. ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
  861. if (ret) {
  862. dev_err(dev, "error in RX dma configuration.\n");
  863. goto err;
  864. }
  865. sport->rx_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  866. if (!sport->rx_buf) {
  867. ret = -ENOMEM;
  868. goto err;
  869. }
  870. /* Prepare for TX : */
  871. sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
  872. if (!sport->dma_chan_tx) {
  873. dev_err(dev, "cannot get the TX DMA channel!\n");
  874. ret = -EINVAL;
  875. goto err;
  876. }
  877. slave_config.direction = DMA_MEM_TO_DEV;
  878. slave_config.dst_addr = sport->port.mapbase + URTX0;
  879. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  880. slave_config.dst_maxburst = TXTL;
  881. ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
  882. if (ret) {
  883. dev_err(dev, "error in TX dma configuration.");
  884. goto err;
  885. }
  886. sport->dma_is_inited = 1;
  887. return 0;
  888. err:
  889. imx_uart_dma_exit(sport);
  890. return ret;
  891. }
  892. static void imx_enable_dma(struct imx_port *sport)
  893. {
  894. unsigned long temp;
  895. init_waitqueue_head(&sport->dma_wait);
  896. /* set UCR1 */
  897. temp = readl(sport->port.membase + UCR1);
  898. temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN |
  899. /* wait for 32 idle frames for IDDMA interrupt */
  900. UCR1_ICD_REG(3);
  901. writel(temp, sport->port.membase + UCR1);
  902. /* set UCR4 */
  903. temp = readl(sport->port.membase + UCR4);
  904. temp |= UCR4_IDDMAEN;
  905. writel(temp, sport->port.membase + UCR4);
  906. sport->dma_is_enabled = 1;
  907. }
  908. static void imx_disable_dma(struct imx_port *sport)
  909. {
  910. unsigned long temp;
  911. /* clear UCR1 */
  912. temp = readl(sport->port.membase + UCR1);
  913. temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
  914. writel(temp, sport->port.membase + UCR1);
  915. /* clear UCR2 */
  916. temp = readl(sport->port.membase + UCR2);
  917. temp &= ~(UCR2_CTSC | UCR2_CTS);
  918. writel(temp, sport->port.membase + UCR2);
  919. /* clear UCR4 */
  920. temp = readl(sport->port.membase + UCR4);
  921. temp &= ~UCR4_IDDMAEN;
  922. writel(temp, sport->port.membase + UCR4);
  923. sport->dma_is_enabled = 0;
  924. }
  925. /* half the RX buffer size */
  926. #define CTSTL 16
  927. static int imx_startup(struct uart_port *port)
  928. {
  929. struct imx_port *sport = (struct imx_port *)port;
  930. int retval, i;
  931. unsigned long flags, temp;
  932. retval = clk_prepare_enable(sport->clk_per);
  933. if (retval)
  934. return retval;
  935. retval = clk_prepare_enable(sport->clk_ipg);
  936. if (retval) {
  937. clk_disable_unprepare(sport->clk_per);
  938. return retval;
  939. }
  940. imx_setup_ufcr(sport, 0);
  941. /* disable the DREN bit (Data Ready interrupt enable) before
  942. * requesting IRQs
  943. */
  944. temp = readl(sport->port.membase + UCR4);
  945. /* set the trigger level for CTS */
  946. temp &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
  947. temp |= CTSTL << UCR4_CTSTL_SHF;
  948. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  949. /* Reset fifo's and state machines */
  950. i = 100;
  951. temp = readl(sport->port.membase + UCR2);
  952. temp &= ~UCR2_SRST;
  953. writel(temp, sport->port.membase + UCR2);
  954. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
  955. udelay(1);
  956. /* Can we enable the DMA support? */
  957. if (is_imx6q_uart(sport) && !uart_console(port) &&
  958. !sport->dma_is_inited)
  959. imx_uart_dma_init(sport);
  960. spin_lock_irqsave(&sport->port.lock, flags);
  961. /*
  962. * Finally, clear and enable interrupts
  963. */
  964. writel(USR1_RTSD, sport->port.membase + USR1);
  965. writel(USR2_ORE, sport->port.membase + USR2);
  966. if (sport->dma_is_inited && !sport->dma_is_enabled)
  967. imx_enable_dma(sport);
  968. temp = readl(sport->port.membase + UCR1);
  969. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  970. writel(temp, sport->port.membase + UCR1);
  971. temp = readl(sport->port.membase + UCR4);
  972. temp |= UCR4_OREN;
  973. writel(temp, sport->port.membase + UCR4);
  974. temp = readl(sport->port.membase + UCR2);
  975. temp |= (UCR2_RXEN | UCR2_TXEN);
  976. if (!sport->have_rtscts)
  977. temp |= UCR2_IRTS;
  978. writel(temp, sport->port.membase + UCR2);
  979. if (!is_imx1_uart(sport)) {
  980. temp = readl(sport->port.membase + UCR3);
  981. temp |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
  982. writel(temp, sport->port.membase + UCR3);
  983. }
  984. /*
  985. * Enable modem status interrupts
  986. */
  987. imx_enable_ms(&sport->port);
  988. spin_unlock_irqrestore(&sport->port.lock, flags);
  989. return 0;
  990. }
  991. static void imx_shutdown(struct uart_port *port)
  992. {
  993. struct imx_port *sport = (struct imx_port *)port;
  994. unsigned long temp;
  995. unsigned long flags;
  996. if (sport->dma_is_enabled) {
  997. int ret;
  998. /* We have to wait for the DMA to finish. */
  999. ret = wait_event_interruptible(sport->dma_wait,
  1000. !sport->dma_is_rxing && !sport->dma_is_txing);
  1001. if (ret != 0) {
  1002. sport->dma_is_rxing = 0;
  1003. sport->dma_is_txing = 0;
  1004. dmaengine_terminate_all(sport->dma_chan_tx);
  1005. dmaengine_terminate_all(sport->dma_chan_rx);
  1006. }
  1007. spin_lock_irqsave(&sport->port.lock, flags);
  1008. imx_stop_tx(port);
  1009. imx_stop_rx(port);
  1010. imx_disable_dma(sport);
  1011. spin_unlock_irqrestore(&sport->port.lock, flags);
  1012. imx_uart_dma_exit(sport);
  1013. }
  1014. spin_lock_irqsave(&sport->port.lock, flags);
  1015. temp = readl(sport->port.membase + UCR2);
  1016. temp &= ~(UCR2_TXEN);
  1017. writel(temp, sport->port.membase + UCR2);
  1018. spin_unlock_irqrestore(&sport->port.lock, flags);
  1019. /*
  1020. * Stop our timer.
  1021. */
  1022. del_timer_sync(&sport->timer);
  1023. /*
  1024. * Disable all interrupts, port and break condition.
  1025. */
  1026. spin_lock_irqsave(&sport->port.lock, flags);
  1027. temp = readl(sport->port.membase + UCR1);
  1028. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  1029. writel(temp, sport->port.membase + UCR1);
  1030. spin_unlock_irqrestore(&sport->port.lock, flags);
  1031. clk_disable_unprepare(sport->clk_per);
  1032. clk_disable_unprepare(sport->clk_ipg);
  1033. }
  1034. static void imx_flush_buffer(struct uart_port *port)
  1035. {
  1036. struct imx_port *sport = (struct imx_port *)port;
  1037. struct scatterlist *sgl = &sport->tx_sgl[0];
  1038. unsigned long temp;
  1039. int i = 100, ubir, ubmr, uts;
  1040. if (!sport->dma_chan_tx)
  1041. return;
  1042. sport->tx_bytes = 0;
  1043. dmaengine_terminate_all(sport->dma_chan_tx);
  1044. if (sport->dma_is_txing) {
  1045. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
  1046. DMA_TO_DEVICE);
  1047. temp = readl(sport->port.membase + UCR1);
  1048. temp &= ~UCR1_TDMAEN;
  1049. writel(temp, sport->port.membase + UCR1);
  1050. sport->dma_is_txing = false;
  1051. }
  1052. /*
  1053. * According to the Reference Manual description of the UART SRST bit:
  1054. * "Reset the transmit and receive state machines,
  1055. * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
  1056. * and UTS[6-3]". As we don't need to restore the old values from
  1057. * USR1, USR2, URXD, UTXD, only save/restore the other four registers
  1058. */
  1059. ubir = readl(sport->port.membase + UBIR);
  1060. ubmr = readl(sport->port.membase + UBMR);
  1061. uts = readl(sport->port.membase + IMX21_UTS);
  1062. temp = readl(sport->port.membase + UCR2);
  1063. temp &= ~UCR2_SRST;
  1064. writel(temp, sport->port.membase + UCR2);
  1065. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
  1066. udelay(1);
  1067. /* Restore the registers */
  1068. writel(ubir, sport->port.membase + UBIR);
  1069. writel(ubmr, sport->port.membase + UBMR);
  1070. writel(uts, sport->port.membase + IMX21_UTS);
  1071. }
  1072. static void
  1073. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  1074. struct ktermios *old)
  1075. {
  1076. struct imx_port *sport = (struct imx_port *)port;
  1077. unsigned long flags;
  1078. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  1079. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1080. unsigned int div, ufcr;
  1081. unsigned long num, denom;
  1082. uint64_t tdiv64;
  1083. /*
  1084. * We only support CS7 and CS8.
  1085. */
  1086. while ((termios->c_cflag & CSIZE) != CS7 &&
  1087. (termios->c_cflag & CSIZE) != CS8) {
  1088. termios->c_cflag &= ~CSIZE;
  1089. termios->c_cflag |= old_csize;
  1090. old_csize = CS8;
  1091. }
  1092. if ((termios->c_cflag & CSIZE) == CS8)
  1093. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  1094. else
  1095. ucr2 = UCR2_SRST | UCR2_IRTS;
  1096. if (termios->c_cflag & CRTSCTS) {
  1097. if (sport->have_rtscts) {
  1098. ucr2 &= ~UCR2_IRTS;
  1099. if (port->rs485.flags & SER_RS485_ENABLED) {
  1100. /*
  1101. * RTS is mandatory for rs485 operation, so keep
  1102. * it under manual control and keep transmitter
  1103. * disabled.
  1104. */
  1105. if (!(port->rs485.flags &
  1106. SER_RS485_RTS_AFTER_SEND))
  1107. ucr2 |= UCR2_CTS;
  1108. } else {
  1109. ucr2 |= UCR2_CTSC;
  1110. }
  1111. } else {
  1112. termios->c_cflag &= ~CRTSCTS;
  1113. }
  1114. } else if (port->rs485.flags & SER_RS485_ENABLED)
  1115. /* disable transmitter */
  1116. if (!(port->rs485.flags & SER_RS485_RTS_AFTER_SEND))
  1117. ucr2 |= UCR2_CTS;
  1118. if (termios->c_cflag & CSTOPB)
  1119. ucr2 |= UCR2_STPB;
  1120. if (termios->c_cflag & PARENB) {
  1121. ucr2 |= UCR2_PREN;
  1122. if (termios->c_cflag & PARODD)
  1123. ucr2 |= UCR2_PROE;
  1124. }
  1125. del_timer_sync(&sport->timer);
  1126. /*
  1127. * Ask the core to calculate the divisor for us.
  1128. */
  1129. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1130. quot = uart_get_divisor(port, baud);
  1131. spin_lock_irqsave(&sport->port.lock, flags);
  1132. sport->port.read_status_mask = 0;
  1133. if (termios->c_iflag & INPCK)
  1134. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  1135. if (termios->c_iflag & (BRKINT | PARMRK))
  1136. sport->port.read_status_mask |= URXD_BRK;
  1137. /*
  1138. * Characters to ignore
  1139. */
  1140. sport->port.ignore_status_mask = 0;
  1141. if (termios->c_iflag & IGNPAR)
  1142. sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
  1143. if (termios->c_iflag & IGNBRK) {
  1144. sport->port.ignore_status_mask |= URXD_BRK;
  1145. /*
  1146. * If we're ignoring parity and break indicators,
  1147. * ignore overruns too (for real raw support).
  1148. */
  1149. if (termios->c_iflag & IGNPAR)
  1150. sport->port.ignore_status_mask |= URXD_OVRRUN;
  1151. }
  1152. if ((termios->c_cflag & CREAD) == 0)
  1153. sport->port.ignore_status_mask |= URXD_DUMMY_READ;
  1154. /*
  1155. * Update the per-port timeout.
  1156. */
  1157. uart_update_timeout(port, termios->c_cflag, baud);
  1158. /*
  1159. * disable interrupts and drain transmitter
  1160. */
  1161. old_ucr1 = readl(sport->port.membase + UCR1);
  1162. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  1163. sport->port.membase + UCR1);
  1164. while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
  1165. barrier();
  1166. /* then, disable everything */
  1167. old_txrxen = readl(sport->port.membase + UCR2);
  1168. writel(old_txrxen & ~(UCR2_TXEN | UCR2_RXEN),
  1169. sport->port.membase + UCR2);
  1170. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  1171. /* custom-baudrate handling */
  1172. div = sport->port.uartclk / (baud * 16);
  1173. if (baud == 38400 && quot != div)
  1174. baud = sport->port.uartclk / (quot * 16);
  1175. div = sport->port.uartclk / (baud * 16);
  1176. if (div > 7)
  1177. div = 7;
  1178. if (!div)
  1179. div = 1;
  1180. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  1181. 1 << 16, 1 << 16, &num, &denom);
  1182. tdiv64 = sport->port.uartclk;
  1183. tdiv64 *= num;
  1184. do_div(tdiv64, denom * 16 * div);
  1185. tty_termios_encode_baud_rate(termios,
  1186. (speed_t)tdiv64, (speed_t)tdiv64);
  1187. num -= 1;
  1188. denom -= 1;
  1189. ufcr = readl(sport->port.membase + UFCR);
  1190. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  1191. if (sport->dte_mode)
  1192. ufcr |= UFCR_DCEDTE;
  1193. writel(ufcr, sport->port.membase + UFCR);
  1194. writel(num, sport->port.membase + UBIR);
  1195. writel(denom, sport->port.membase + UBMR);
  1196. if (!is_imx1_uart(sport))
  1197. writel(sport->port.uartclk / div / 1000,
  1198. sport->port.membase + IMX21_ONEMS);
  1199. writel(old_ucr1, sport->port.membase + UCR1);
  1200. /* set the parity, stop bits and data size */
  1201. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  1202. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  1203. imx_enable_ms(&sport->port);
  1204. spin_unlock_irqrestore(&sport->port.lock, flags);
  1205. }
  1206. static const char *imx_type(struct uart_port *port)
  1207. {
  1208. struct imx_port *sport = (struct imx_port *)port;
  1209. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  1210. }
  1211. /*
  1212. * Configure/autoconfigure the port.
  1213. */
  1214. static void imx_config_port(struct uart_port *port, int flags)
  1215. {
  1216. struct imx_port *sport = (struct imx_port *)port;
  1217. if (flags & UART_CONFIG_TYPE)
  1218. sport->port.type = PORT_IMX;
  1219. }
  1220. /*
  1221. * Verify the new serial_struct (for TIOCSSERIAL).
  1222. * The only change we allow are to the flags and type, and
  1223. * even then only between PORT_IMX and PORT_UNKNOWN
  1224. */
  1225. static int
  1226. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  1227. {
  1228. struct imx_port *sport = (struct imx_port *)port;
  1229. int ret = 0;
  1230. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  1231. ret = -EINVAL;
  1232. if (sport->port.irq != ser->irq)
  1233. ret = -EINVAL;
  1234. if (ser->io_type != UPIO_MEM)
  1235. ret = -EINVAL;
  1236. if (sport->port.uartclk / 16 != ser->baud_base)
  1237. ret = -EINVAL;
  1238. if (sport->port.mapbase != (unsigned long)ser->iomem_base)
  1239. ret = -EINVAL;
  1240. if (sport->port.iobase != ser->port)
  1241. ret = -EINVAL;
  1242. if (ser->hub6 != 0)
  1243. ret = -EINVAL;
  1244. return ret;
  1245. }
  1246. #if defined(CONFIG_CONSOLE_POLL)
  1247. static int imx_poll_init(struct uart_port *port)
  1248. {
  1249. struct imx_port *sport = (struct imx_port *)port;
  1250. unsigned long flags;
  1251. unsigned long temp;
  1252. int retval;
  1253. retval = clk_prepare_enable(sport->clk_ipg);
  1254. if (retval)
  1255. return retval;
  1256. retval = clk_prepare_enable(sport->clk_per);
  1257. if (retval)
  1258. clk_disable_unprepare(sport->clk_ipg);
  1259. imx_setup_ufcr(sport, 0);
  1260. spin_lock_irqsave(&sport->port.lock, flags);
  1261. temp = readl(sport->port.membase + UCR1);
  1262. if (is_imx1_uart(sport))
  1263. temp |= IMX1_UCR1_UARTCLKEN;
  1264. temp |= UCR1_UARTEN | UCR1_RRDYEN;
  1265. temp &= ~(UCR1_TXMPTYEN | UCR1_RTSDEN);
  1266. writel(temp, sport->port.membase + UCR1);
  1267. temp = readl(sport->port.membase + UCR2);
  1268. temp |= UCR2_RXEN;
  1269. writel(temp, sport->port.membase + UCR2);
  1270. spin_unlock_irqrestore(&sport->port.lock, flags);
  1271. return 0;
  1272. }
  1273. static int imx_poll_get_char(struct uart_port *port)
  1274. {
  1275. if (!(readl_relaxed(port->membase + USR2) & USR2_RDR))
  1276. return NO_POLL_CHAR;
  1277. return readl_relaxed(port->membase + URXD0) & URXD_RX_DATA;
  1278. }
  1279. static void imx_poll_put_char(struct uart_port *port, unsigned char c)
  1280. {
  1281. unsigned int status;
  1282. /* drain */
  1283. do {
  1284. status = readl_relaxed(port->membase + USR1);
  1285. } while (~status & USR1_TRDY);
  1286. /* write */
  1287. writel_relaxed(c, port->membase + URTX0);
  1288. /* flush */
  1289. do {
  1290. status = readl_relaxed(port->membase + USR2);
  1291. } while (~status & USR2_TXDC);
  1292. }
  1293. #endif
  1294. static int imx_rs485_config(struct uart_port *port,
  1295. struct serial_rs485 *rs485conf)
  1296. {
  1297. struct imx_port *sport = (struct imx_port *)port;
  1298. /* unimplemented */
  1299. rs485conf->delay_rts_before_send = 0;
  1300. rs485conf->delay_rts_after_send = 0;
  1301. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1302. /* RTS is required to control the transmitter */
  1303. if (!sport->have_rtscts)
  1304. rs485conf->flags &= ~SER_RS485_ENABLED;
  1305. if (rs485conf->flags & SER_RS485_ENABLED) {
  1306. unsigned long temp;
  1307. /* disable transmitter */
  1308. temp = readl(sport->port.membase + UCR2);
  1309. temp &= ~UCR2_CTSC;
  1310. if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
  1311. temp &= ~UCR2_CTS;
  1312. else
  1313. temp |= UCR2_CTS;
  1314. writel(temp, sport->port.membase + UCR2);
  1315. }
  1316. port->rs485 = *rs485conf;
  1317. return 0;
  1318. }
  1319. static struct uart_ops imx_pops = {
  1320. .tx_empty = imx_tx_empty,
  1321. .set_mctrl = imx_set_mctrl,
  1322. .get_mctrl = imx_get_mctrl,
  1323. .stop_tx = imx_stop_tx,
  1324. .start_tx = imx_start_tx,
  1325. .stop_rx = imx_stop_rx,
  1326. .enable_ms = imx_enable_ms,
  1327. .break_ctl = imx_break_ctl,
  1328. .startup = imx_startup,
  1329. .shutdown = imx_shutdown,
  1330. .flush_buffer = imx_flush_buffer,
  1331. .set_termios = imx_set_termios,
  1332. .type = imx_type,
  1333. .config_port = imx_config_port,
  1334. .verify_port = imx_verify_port,
  1335. #if defined(CONFIG_CONSOLE_POLL)
  1336. .poll_init = imx_poll_init,
  1337. .poll_get_char = imx_poll_get_char,
  1338. .poll_put_char = imx_poll_put_char,
  1339. #endif
  1340. };
  1341. static struct imx_port *imx_ports[UART_NR];
  1342. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  1343. static void imx_console_putchar(struct uart_port *port, int ch)
  1344. {
  1345. struct imx_port *sport = (struct imx_port *)port;
  1346. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  1347. barrier();
  1348. writel(ch, sport->port.membase + URTX0);
  1349. }
  1350. /*
  1351. * Interrupts are disabled on entering
  1352. */
  1353. static void
  1354. imx_console_write(struct console *co, const char *s, unsigned int count)
  1355. {
  1356. struct imx_port *sport = imx_ports[co->index];
  1357. struct imx_port_ucrs old_ucr;
  1358. unsigned int ucr1;
  1359. unsigned long flags = 0;
  1360. int locked = 1;
  1361. int retval;
  1362. retval = clk_enable(sport->clk_per);
  1363. if (retval)
  1364. return;
  1365. retval = clk_enable(sport->clk_ipg);
  1366. if (retval) {
  1367. clk_disable(sport->clk_per);
  1368. return;
  1369. }
  1370. if (sport->port.sysrq)
  1371. locked = 0;
  1372. else if (oops_in_progress)
  1373. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1374. else
  1375. spin_lock_irqsave(&sport->port.lock, flags);
  1376. /*
  1377. * First, save UCR1/2/3 and then disable interrupts
  1378. */
  1379. imx_port_ucrs_save(&sport->port, &old_ucr);
  1380. ucr1 = old_ucr.ucr1;
  1381. if (is_imx1_uart(sport))
  1382. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1383. ucr1 |= UCR1_UARTEN;
  1384. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1385. writel(ucr1, sport->port.membase + UCR1);
  1386. writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  1387. uart_console_write(&sport->port, s, count, imx_console_putchar);
  1388. /*
  1389. * Finally, wait for transmitter to become empty
  1390. * and restore UCR1/2/3
  1391. */
  1392. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  1393. imx_port_ucrs_restore(&sport->port, &old_ucr);
  1394. if (locked)
  1395. spin_unlock_irqrestore(&sport->port.lock, flags);
  1396. clk_disable(sport->clk_ipg);
  1397. clk_disable(sport->clk_per);
  1398. }
  1399. /*
  1400. * If the port was already initialised (eg, by a boot loader),
  1401. * try to determine the current setup.
  1402. */
  1403. static void __init
  1404. imx_console_get_options(struct imx_port *sport, int *baud,
  1405. int *parity, int *bits)
  1406. {
  1407. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  1408. /* ok, the port was enabled */
  1409. unsigned int ucr2, ubir, ubmr, uartclk;
  1410. unsigned int baud_raw;
  1411. unsigned int ucfr_rfdiv;
  1412. ucr2 = readl(sport->port.membase + UCR2);
  1413. *parity = 'n';
  1414. if (ucr2 & UCR2_PREN) {
  1415. if (ucr2 & UCR2_PROE)
  1416. *parity = 'o';
  1417. else
  1418. *parity = 'e';
  1419. }
  1420. if (ucr2 & UCR2_WS)
  1421. *bits = 8;
  1422. else
  1423. *bits = 7;
  1424. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  1425. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  1426. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  1427. if (ucfr_rfdiv == 6)
  1428. ucfr_rfdiv = 7;
  1429. else
  1430. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1431. uartclk = clk_get_rate(sport->clk_per);
  1432. uartclk /= ucfr_rfdiv;
  1433. { /*
  1434. * The next code provides exact computation of
  1435. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1436. * without need of float support or long long division,
  1437. * which would be required to prevent 32bit arithmetic overflow
  1438. */
  1439. unsigned int mul = ubir + 1;
  1440. unsigned int div = 16 * (ubmr + 1);
  1441. unsigned int rem = uartclk % div;
  1442. baud_raw = (uartclk / div) * mul;
  1443. baud_raw += (rem * mul + div / 2) / div;
  1444. *baud = (baud_raw + 50) / 100 * 100;
  1445. }
  1446. if (*baud != baud_raw)
  1447. pr_info("Console IMX rounded baud rate from %d to %d\n",
  1448. baud_raw, *baud);
  1449. }
  1450. }
  1451. static int __init
  1452. imx_console_setup(struct console *co, char *options)
  1453. {
  1454. struct imx_port *sport;
  1455. int baud = 9600;
  1456. int bits = 8;
  1457. int parity = 'n';
  1458. int flow = 'n';
  1459. int retval;
  1460. /*
  1461. * Check whether an invalid uart number has been specified, and
  1462. * if so, search for the first available port that does have
  1463. * console support.
  1464. */
  1465. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1466. co->index = 0;
  1467. sport = imx_ports[co->index];
  1468. if (sport == NULL)
  1469. return -ENODEV;
  1470. /* For setting the registers, we only need to enable the ipg clock. */
  1471. retval = clk_prepare_enable(sport->clk_ipg);
  1472. if (retval)
  1473. goto error_console;
  1474. if (options)
  1475. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1476. else
  1477. imx_console_get_options(sport, &baud, &parity, &bits);
  1478. imx_setup_ufcr(sport, 0);
  1479. retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1480. clk_disable(sport->clk_ipg);
  1481. if (retval) {
  1482. clk_unprepare(sport->clk_ipg);
  1483. goto error_console;
  1484. }
  1485. retval = clk_prepare(sport->clk_per);
  1486. if (retval)
  1487. clk_disable_unprepare(sport->clk_ipg);
  1488. error_console:
  1489. return retval;
  1490. }
  1491. static struct uart_driver imx_reg;
  1492. static struct console imx_console = {
  1493. .name = DEV_NAME,
  1494. .write = imx_console_write,
  1495. .device = uart_console_device,
  1496. .setup = imx_console_setup,
  1497. .flags = CON_PRINTBUFFER,
  1498. .index = -1,
  1499. .data = &imx_reg,
  1500. };
  1501. #define IMX_CONSOLE &imx_console
  1502. #else
  1503. #define IMX_CONSOLE NULL
  1504. #endif
  1505. static struct uart_driver imx_reg = {
  1506. .owner = THIS_MODULE,
  1507. .driver_name = DRIVER_NAME,
  1508. .dev_name = DEV_NAME,
  1509. .major = SERIAL_IMX_MAJOR,
  1510. .minor = MINOR_START,
  1511. .nr = ARRAY_SIZE(imx_ports),
  1512. .cons = IMX_CONSOLE,
  1513. };
  1514. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1515. {
  1516. struct imx_port *sport = platform_get_drvdata(dev);
  1517. unsigned int val;
  1518. /* enable wakeup from i.MX UART */
  1519. val = readl(sport->port.membase + UCR3);
  1520. val |= UCR3_AWAKEN;
  1521. writel(val, sport->port.membase + UCR3);
  1522. uart_suspend_port(&imx_reg, &sport->port);
  1523. return 0;
  1524. }
  1525. static int serial_imx_resume(struct platform_device *dev)
  1526. {
  1527. struct imx_port *sport = platform_get_drvdata(dev);
  1528. unsigned int val;
  1529. /* disable wakeup from i.MX UART */
  1530. val = readl(sport->port.membase + UCR3);
  1531. val &= ~UCR3_AWAKEN;
  1532. writel(val, sport->port.membase + UCR3);
  1533. uart_resume_port(&imx_reg, &sport->port);
  1534. return 0;
  1535. }
  1536. #ifdef CONFIG_OF
  1537. /*
  1538. * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
  1539. * could successfully get all information from dt or a negative errno.
  1540. */
  1541. static int serial_imx_probe_dt(struct imx_port *sport,
  1542. struct platform_device *pdev)
  1543. {
  1544. struct device_node *np = pdev->dev.of_node;
  1545. const struct of_device_id *of_id =
  1546. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1547. int ret;
  1548. if (!np)
  1549. /* no device tree device */
  1550. return 1;
  1551. ret = of_alias_get_id(np, "serial");
  1552. if (ret < 0) {
  1553. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1554. return ret;
  1555. }
  1556. sport->port.line = ret;
  1557. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1558. sport->have_rtscts = 1;
  1559. if (of_get_property(np, "fsl,dte-mode", NULL))
  1560. sport->dte_mode = 1;
  1561. sport->devdata = of_id->data;
  1562. return 0;
  1563. }
  1564. #else
  1565. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1566. struct platform_device *pdev)
  1567. {
  1568. return 1;
  1569. }
  1570. #endif
  1571. static void serial_imx_probe_pdata(struct imx_port *sport,
  1572. struct platform_device *pdev)
  1573. {
  1574. struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1575. sport->port.line = pdev->id;
  1576. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1577. if (!pdata)
  1578. return;
  1579. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1580. sport->have_rtscts = 1;
  1581. }
  1582. static int serial_imx_probe(struct platform_device *pdev)
  1583. {
  1584. struct imx_port *sport;
  1585. void __iomem *base;
  1586. int ret = 0;
  1587. struct resource *res;
  1588. int txirq, rxirq, rtsirq;
  1589. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1590. if (!sport)
  1591. return -ENOMEM;
  1592. ret = serial_imx_probe_dt(sport, pdev);
  1593. if (ret > 0)
  1594. serial_imx_probe_pdata(sport, pdev);
  1595. else if (ret < 0)
  1596. return ret;
  1597. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1598. base = devm_ioremap_resource(&pdev->dev, res);
  1599. if (IS_ERR(base))
  1600. return PTR_ERR(base);
  1601. rxirq = platform_get_irq(pdev, 0);
  1602. txirq = platform_get_irq(pdev, 1);
  1603. rtsirq = platform_get_irq(pdev, 2);
  1604. sport->port.dev = &pdev->dev;
  1605. sport->port.mapbase = res->start;
  1606. sport->port.membase = base;
  1607. sport->port.type = PORT_IMX,
  1608. sport->port.iotype = UPIO_MEM;
  1609. sport->port.irq = rxirq;
  1610. sport->port.fifosize = 32;
  1611. sport->port.ops = &imx_pops;
  1612. sport->port.rs485_config = imx_rs485_config;
  1613. sport->port.rs485.flags =
  1614. SER_RS485_RTS_ON_SEND | SER_RS485_RX_DURING_TX;
  1615. sport->port.flags = UPF_BOOT_AUTOCONF;
  1616. init_timer(&sport->timer);
  1617. sport->timer.function = imx_timeout;
  1618. sport->timer.data = (unsigned long)sport;
  1619. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1620. if (IS_ERR(sport->clk_ipg)) {
  1621. ret = PTR_ERR(sport->clk_ipg);
  1622. dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
  1623. return ret;
  1624. }
  1625. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  1626. if (IS_ERR(sport->clk_per)) {
  1627. ret = PTR_ERR(sport->clk_per);
  1628. dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
  1629. return ret;
  1630. }
  1631. sport->port.uartclk = clk_get_rate(sport->clk_per);
  1632. /*
  1633. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  1634. * chips only have one interrupt.
  1635. */
  1636. if (txirq > 0) {
  1637. ret = devm_request_irq(&pdev->dev, rxirq, imx_rxint, 0,
  1638. dev_name(&pdev->dev), sport);
  1639. if (ret)
  1640. return ret;
  1641. ret = devm_request_irq(&pdev->dev, txirq, imx_txint, 0,
  1642. dev_name(&pdev->dev), sport);
  1643. if (ret)
  1644. return ret;
  1645. } else {
  1646. ret = devm_request_irq(&pdev->dev, rxirq, imx_int, 0,
  1647. dev_name(&pdev->dev), sport);
  1648. if (ret)
  1649. return ret;
  1650. }
  1651. imx_ports[sport->port.line] = sport;
  1652. platform_set_drvdata(pdev, sport);
  1653. return uart_add_one_port(&imx_reg, &sport->port);
  1654. }
  1655. static int serial_imx_remove(struct platform_device *pdev)
  1656. {
  1657. struct imx_port *sport = platform_get_drvdata(pdev);
  1658. return uart_remove_one_port(&imx_reg, &sport->port);
  1659. }
  1660. static struct platform_driver serial_imx_driver = {
  1661. .probe = serial_imx_probe,
  1662. .remove = serial_imx_remove,
  1663. .suspend = serial_imx_suspend,
  1664. .resume = serial_imx_resume,
  1665. .id_table = imx_uart_devtype,
  1666. .driver = {
  1667. .name = "imx-uart",
  1668. .of_match_table = imx_uart_dt_ids,
  1669. },
  1670. };
  1671. static int __init imx_serial_init(void)
  1672. {
  1673. int ret = uart_register_driver(&imx_reg);
  1674. if (ret)
  1675. return ret;
  1676. ret = platform_driver_register(&serial_imx_driver);
  1677. if (ret != 0)
  1678. uart_unregister_driver(&imx_reg);
  1679. return ret;
  1680. }
  1681. static void __exit imx_serial_exit(void)
  1682. {
  1683. platform_driver_unregister(&serial_imx_driver);
  1684. uart_unregister_driver(&imx_reg);
  1685. }
  1686. module_init(imx_serial_init);
  1687. module_exit(imx_serial_exit);
  1688. MODULE_AUTHOR("Sascha Hauer");
  1689. MODULE_DESCRIPTION("IMX generic serial port driver");
  1690. MODULE_LICENSE("GPL");
  1691. MODULE_ALIAS("platform:imx-uart");