imx.c 57 KB

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