imx.c 57 KB

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