imx.c 54 KB

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