imx.c 51 KB

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