imx.c 56 KB

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