imx.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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. dev_err(dev, "cannot alloc DMA buffer.\n");
  847. ret = -ENOMEM;
  848. goto err;
  849. }
  850. /* Prepare for TX : */
  851. sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
  852. if (!sport->dma_chan_tx) {
  853. dev_err(dev, "cannot get the TX DMA channel!\n");
  854. ret = -EINVAL;
  855. goto err;
  856. }
  857. slave_config.direction = DMA_MEM_TO_DEV;
  858. slave_config.dst_addr = sport->port.mapbase + URTX0;
  859. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  860. slave_config.dst_maxburst = TXTL;
  861. ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
  862. if (ret) {
  863. dev_err(dev, "error in TX dma configuration.");
  864. goto err;
  865. }
  866. sport->dma_is_inited = 1;
  867. return 0;
  868. err:
  869. imx_uart_dma_exit(sport);
  870. return ret;
  871. }
  872. static void imx_enable_dma(struct imx_port *sport)
  873. {
  874. unsigned long temp;
  875. init_waitqueue_head(&sport->dma_wait);
  876. /* set UCR1 */
  877. temp = readl(sport->port.membase + UCR1);
  878. temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN |
  879. /* wait for 32 idle frames for IDDMA interrupt */
  880. UCR1_ICD_REG(3);
  881. writel(temp, sport->port.membase + UCR1);
  882. /* set UCR4 */
  883. temp = readl(sport->port.membase + UCR4);
  884. temp |= UCR4_IDDMAEN;
  885. writel(temp, sport->port.membase + UCR4);
  886. sport->dma_is_enabled = 1;
  887. }
  888. static void imx_disable_dma(struct imx_port *sport)
  889. {
  890. unsigned long temp;
  891. /* clear UCR1 */
  892. temp = readl(sport->port.membase + UCR1);
  893. temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
  894. writel(temp, sport->port.membase + UCR1);
  895. /* clear UCR2 */
  896. temp = readl(sport->port.membase + UCR2);
  897. temp &= ~(UCR2_CTSC | UCR2_CTS);
  898. writel(temp, sport->port.membase + UCR2);
  899. /* clear UCR4 */
  900. temp = readl(sport->port.membase + UCR4);
  901. temp &= ~UCR4_IDDMAEN;
  902. writel(temp, sport->port.membase + UCR4);
  903. sport->dma_is_enabled = 0;
  904. }
  905. /* half the RX buffer size */
  906. #define CTSTL 16
  907. static int imx_startup(struct uart_port *port)
  908. {
  909. struct imx_port *sport = (struct imx_port *)port;
  910. int retval, i;
  911. unsigned long flags, temp;
  912. retval = clk_prepare_enable(sport->clk_per);
  913. if (retval)
  914. goto error_out1;
  915. retval = clk_prepare_enable(sport->clk_ipg);
  916. if (retval) {
  917. clk_disable_unprepare(sport->clk_per);
  918. goto error_out1;
  919. }
  920. imx_setup_ufcr(sport, 0);
  921. /* disable the DREN bit (Data Ready interrupt enable) before
  922. * requesting IRQs
  923. */
  924. temp = readl(sport->port.membase + UCR4);
  925. if (USE_IRDA(sport))
  926. temp |= UCR4_IRSC;
  927. /* set the trigger level for CTS */
  928. temp &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
  929. temp |= CTSTL << UCR4_CTSTL_SHF;
  930. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  931. /* Reset fifo's and state machines */
  932. i = 100;
  933. temp = readl(sport->port.membase + UCR2);
  934. temp &= ~UCR2_SRST;
  935. writel(temp, sport->port.membase + UCR2);
  936. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
  937. udelay(1);
  938. /*
  939. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  940. * chips only have one interrupt.
  941. */
  942. if (sport->txirq > 0) {
  943. retval = request_irq(sport->rxirq, imx_rxint, 0,
  944. dev_name(port->dev), sport);
  945. if (retval)
  946. goto error_out1;
  947. retval = request_irq(sport->txirq, imx_txint, 0,
  948. dev_name(port->dev), sport);
  949. if (retval)
  950. goto error_out2;
  951. /* do not use RTS IRQ on IrDA */
  952. if (!USE_IRDA(sport)) {
  953. retval = request_irq(sport->rtsirq, imx_rtsint, 0,
  954. dev_name(port->dev), sport);
  955. if (retval)
  956. goto error_out3;
  957. }
  958. } else {
  959. retval = request_irq(sport->port.irq, imx_int, 0,
  960. dev_name(port->dev), sport);
  961. if (retval) {
  962. free_irq(sport->port.irq, sport);
  963. goto error_out1;
  964. }
  965. }
  966. spin_lock_irqsave(&sport->port.lock, flags);
  967. /*
  968. * Finally, clear and enable interrupts
  969. */
  970. writel(USR1_RTSD, sport->port.membase + USR1);
  971. temp = readl(sport->port.membase + UCR1);
  972. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  973. if (USE_IRDA(sport)) {
  974. temp |= UCR1_IREN;
  975. temp &= ~(UCR1_RTSDEN);
  976. }
  977. writel(temp, sport->port.membase + UCR1);
  978. temp = readl(sport->port.membase + UCR2);
  979. temp |= (UCR2_RXEN | UCR2_TXEN);
  980. if (!sport->have_rtscts)
  981. temp |= UCR2_IRTS;
  982. writel(temp, sport->port.membase + UCR2);
  983. if (!is_imx1_uart(sport)) {
  984. temp = readl(sport->port.membase + UCR3);
  985. temp |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
  986. writel(temp, sport->port.membase + UCR3);
  987. }
  988. if (USE_IRDA(sport)) {
  989. temp = readl(sport->port.membase + UCR4);
  990. if (sport->irda_inv_rx)
  991. temp |= UCR4_INVR;
  992. else
  993. temp &= ~(UCR4_INVR);
  994. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  995. temp = readl(sport->port.membase + UCR3);
  996. if (sport->irda_inv_tx)
  997. temp |= UCR3_INVT;
  998. else
  999. temp &= ~(UCR3_INVT);
  1000. writel(temp, sport->port.membase + UCR3);
  1001. }
  1002. /*
  1003. * Enable modem status interrupts
  1004. */
  1005. imx_enable_ms(&sport->port);
  1006. spin_unlock_irqrestore(&sport->port.lock, flags);
  1007. if (USE_IRDA(sport)) {
  1008. struct imxuart_platform_data *pdata;
  1009. pdata = dev_get_platdata(sport->port.dev);
  1010. sport->irda_inv_rx = pdata->irda_inv_rx;
  1011. sport->irda_inv_tx = pdata->irda_inv_tx;
  1012. sport->trcv_delay = pdata->transceiver_delay;
  1013. if (pdata->irda_enable)
  1014. pdata->irda_enable(1);
  1015. }
  1016. return 0;
  1017. error_out3:
  1018. if (sport->txirq)
  1019. free_irq(sport->txirq, sport);
  1020. error_out2:
  1021. if (sport->rxirq)
  1022. free_irq(sport->rxirq, sport);
  1023. error_out1:
  1024. return retval;
  1025. }
  1026. static void imx_shutdown(struct uart_port *port)
  1027. {
  1028. struct imx_port *sport = (struct imx_port *)port;
  1029. unsigned long temp;
  1030. unsigned long flags;
  1031. if (sport->dma_is_enabled) {
  1032. int ret;
  1033. /* We have to wait for the DMA to finish. */
  1034. ret = wait_event_interruptible(sport->dma_wait,
  1035. !sport->dma_is_rxing && !sport->dma_is_txing);
  1036. if (ret != 0) {
  1037. sport->dma_is_rxing = 0;
  1038. sport->dma_is_txing = 0;
  1039. dmaengine_terminate_all(sport->dma_chan_tx);
  1040. dmaengine_terminate_all(sport->dma_chan_rx);
  1041. }
  1042. imx_stop_tx(port);
  1043. imx_stop_rx(port);
  1044. imx_disable_dma(sport);
  1045. imx_uart_dma_exit(sport);
  1046. }
  1047. spin_lock_irqsave(&sport->port.lock, flags);
  1048. temp = readl(sport->port.membase + UCR2);
  1049. temp &= ~(UCR2_TXEN);
  1050. writel(temp, sport->port.membase + UCR2);
  1051. spin_unlock_irqrestore(&sport->port.lock, flags);
  1052. if (USE_IRDA(sport)) {
  1053. struct imxuart_platform_data *pdata;
  1054. pdata = dev_get_platdata(sport->port.dev);
  1055. if (pdata->irda_enable)
  1056. pdata->irda_enable(0);
  1057. }
  1058. /*
  1059. * Stop our timer.
  1060. */
  1061. del_timer_sync(&sport->timer);
  1062. /*
  1063. * Free the interrupts
  1064. */
  1065. if (sport->txirq > 0) {
  1066. if (!USE_IRDA(sport))
  1067. free_irq(sport->rtsirq, sport);
  1068. free_irq(sport->txirq, sport);
  1069. free_irq(sport->rxirq, sport);
  1070. } else
  1071. free_irq(sport->port.irq, sport);
  1072. /*
  1073. * Disable all interrupts, port and break condition.
  1074. */
  1075. spin_lock_irqsave(&sport->port.lock, flags);
  1076. temp = readl(sport->port.membase + UCR1);
  1077. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  1078. if (USE_IRDA(sport))
  1079. temp &= ~(UCR1_IREN);
  1080. writel(temp, sport->port.membase + UCR1);
  1081. spin_unlock_irqrestore(&sport->port.lock, flags);
  1082. clk_disable_unprepare(sport->clk_per);
  1083. clk_disable_unprepare(sport->clk_ipg);
  1084. }
  1085. static void imx_flush_buffer(struct uart_port *port)
  1086. {
  1087. struct imx_port *sport = (struct imx_port *)port;
  1088. if (sport->dma_is_enabled) {
  1089. sport->tx_bytes = 0;
  1090. dmaengine_terminate_all(sport->dma_chan_tx);
  1091. }
  1092. }
  1093. static void
  1094. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  1095. struct ktermios *old)
  1096. {
  1097. struct imx_port *sport = (struct imx_port *)port;
  1098. unsigned long flags;
  1099. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  1100. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1101. unsigned int div, ufcr;
  1102. unsigned long num, denom;
  1103. uint64_t tdiv64;
  1104. /*
  1105. * If we don't support modem control lines, don't allow
  1106. * these to be set.
  1107. */
  1108. if (0) {
  1109. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  1110. termios->c_cflag |= CLOCAL;
  1111. }
  1112. /*
  1113. * We only support CS7 and CS8.
  1114. */
  1115. while ((termios->c_cflag & CSIZE) != CS7 &&
  1116. (termios->c_cflag & CSIZE) != CS8) {
  1117. termios->c_cflag &= ~CSIZE;
  1118. termios->c_cflag |= old_csize;
  1119. old_csize = CS8;
  1120. }
  1121. if ((termios->c_cflag & CSIZE) == CS8)
  1122. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  1123. else
  1124. ucr2 = UCR2_SRST | UCR2_IRTS;
  1125. if (termios->c_cflag & CRTSCTS) {
  1126. if (sport->have_rtscts) {
  1127. ucr2 &= ~UCR2_IRTS;
  1128. ucr2 |= UCR2_CTSC;
  1129. /* Can we enable the DMA support? */
  1130. if (is_imx6q_uart(sport) && !uart_console(port)
  1131. && !sport->dma_is_inited)
  1132. imx_uart_dma_init(sport);
  1133. } else {
  1134. termios->c_cflag &= ~CRTSCTS;
  1135. }
  1136. }
  1137. if (termios->c_cflag & CSTOPB)
  1138. ucr2 |= UCR2_STPB;
  1139. if (termios->c_cflag & PARENB) {
  1140. ucr2 |= UCR2_PREN;
  1141. if (termios->c_cflag & PARODD)
  1142. ucr2 |= UCR2_PROE;
  1143. }
  1144. del_timer_sync(&sport->timer);
  1145. /*
  1146. * Ask the core to calculate the divisor for us.
  1147. */
  1148. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1149. quot = uart_get_divisor(port, baud);
  1150. spin_lock_irqsave(&sport->port.lock, flags);
  1151. sport->port.read_status_mask = 0;
  1152. if (termios->c_iflag & INPCK)
  1153. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  1154. if (termios->c_iflag & (BRKINT | PARMRK))
  1155. sport->port.read_status_mask |= URXD_BRK;
  1156. /*
  1157. * Characters to ignore
  1158. */
  1159. sport->port.ignore_status_mask = 0;
  1160. if (termios->c_iflag & IGNPAR)
  1161. sport->port.ignore_status_mask |= URXD_PRERR;
  1162. if (termios->c_iflag & IGNBRK) {
  1163. sport->port.ignore_status_mask |= URXD_BRK;
  1164. /*
  1165. * If we're ignoring parity and break indicators,
  1166. * ignore overruns too (for real raw support).
  1167. */
  1168. if (termios->c_iflag & IGNPAR)
  1169. sport->port.ignore_status_mask |= URXD_OVRRUN;
  1170. }
  1171. /*
  1172. * Update the per-port timeout.
  1173. */
  1174. uart_update_timeout(port, termios->c_cflag, baud);
  1175. /*
  1176. * disable interrupts and drain transmitter
  1177. */
  1178. old_ucr1 = readl(sport->port.membase + UCR1);
  1179. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  1180. sport->port.membase + UCR1);
  1181. while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
  1182. barrier();
  1183. /* then, disable everything */
  1184. old_txrxen = readl(sport->port.membase + UCR2);
  1185. writel(old_txrxen & ~(UCR2_TXEN | UCR2_RXEN),
  1186. sport->port.membase + UCR2);
  1187. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  1188. if (USE_IRDA(sport)) {
  1189. /*
  1190. * use maximum available submodule frequency to
  1191. * avoid missing short pulses due to low sampling rate
  1192. */
  1193. div = 1;
  1194. } else {
  1195. /* custom-baudrate handling */
  1196. div = sport->port.uartclk / (baud * 16);
  1197. if (baud == 38400 && quot != div)
  1198. baud = sport->port.uartclk / (quot * 16);
  1199. div = sport->port.uartclk / (baud * 16);
  1200. if (div > 7)
  1201. div = 7;
  1202. if (!div)
  1203. div = 1;
  1204. }
  1205. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  1206. 1 << 16, 1 << 16, &num, &denom);
  1207. tdiv64 = sport->port.uartclk;
  1208. tdiv64 *= num;
  1209. do_div(tdiv64, denom * 16 * div);
  1210. tty_termios_encode_baud_rate(termios,
  1211. (speed_t)tdiv64, (speed_t)tdiv64);
  1212. num -= 1;
  1213. denom -= 1;
  1214. ufcr = readl(sport->port.membase + UFCR);
  1215. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  1216. if (sport->dte_mode)
  1217. ufcr |= UFCR_DCEDTE;
  1218. writel(ufcr, sport->port.membase + UFCR);
  1219. writel(num, sport->port.membase + UBIR);
  1220. writel(denom, sport->port.membase + UBMR);
  1221. if (!is_imx1_uart(sport))
  1222. writel(sport->port.uartclk / div / 1000,
  1223. sport->port.membase + IMX21_ONEMS);
  1224. writel(old_ucr1, sport->port.membase + UCR1);
  1225. /* set the parity, stop bits and data size */
  1226. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  1227. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  1228. imx_enable_ms(&sport->port);
  1229. if (sport->dma_is_inited && !sport->dma_is_enabled)
  1230. imx_enable_dma(sport);
  1231. spin_unlock_irqrestore(&sport->port.lock, flags);
  1232. }
  1233. static const char *imx_type(struct uart_port *port)
  1234. {
  1235. struct imx_port *sport = (struct imx_port *)port;
  1236. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  1237. }
  1238. /*
  1239. * Configure/autoconfigure the port.
  1240. */
  1241. static void imx_config_port(struct uart_port *port, int flags)
  1242. {
  1243. struct imx_port *sport = (struct imx_port *)port;
  1244. if (flags & UART_CONFIG_TYPE)
  1245. sport->port.type = PORT_IMX;
  1246. }
  1247. /*
  1248. * Verify the new serial_struct (for TIOCSSERIAL).
  1249. * The only change we allow are to the flags and type, and
  1250. * even then only between PORT_IMX and PORT_UNKNOWN
  1251. */
  1252. static int
  1253. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  1254. {
  1255. struct imx_port *sport = (struct imx_port *)port;
  1256. int ret = 0;
  1257. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  1258. ret = -EINVAL;
  1259. if (sport->port.irq != ser->irq)
  1260. ret = -EINVAL;
  1261. if (ser->io_type != UPIO_MEM)
  1262. ret = -EINVAL;
  1263. if (sport->port.uartclk / 16 != ser->baud_base)
  1264. ret = -EINVAL;
  1265. if (sport->port.mapbase != (unsigned long)ser->iomem_base)
  1266. ret = -EINVAL;
  1267. if (sport->port.iobase != ser->port)
  1268. ret = -EINVAL;
  1269. if (ser->hub6 != 0)
  1270. ret = -EINVAL;
  1271. return ret;
  1272. }
  1273. #if defined(CONFIG_CONSOLE_POLL)
  1274. static int imx_poll_get_char(struct uart_port *port)
  1275. {
  1276. if (!(readl(port->membase + USR2) & USR2_RDR))
  1277. return NO_POLL_CHAR;
  1278. return readl(port->membase + URXD0) & URXD_RX_DATA;
  1279. }
  1280. static void imx_poll_put_char(struct uart_port *port, unsigned char c)
  1281. {
  1282. struct imx_port_ucrs old_ucr;
  1283. unsigned int status;
  1284. /* save control registers */
  1285. imx_port_ucrs_save(port, &old_ucr);
  1286. /* disable interrupts */
  1287. writel(UCR1_UARTEN, port->membase + UCR1);
  1288. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  1289. port->membase + UCR2);
  1290. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  1291. port->membase + UCR3);
  1292. /* drain */
  1293. do {
  1294. status = readl(port->membase + USR1);
  1295. } while (~status & USR1_TRDY);
  1296. /* write */
  1297. writel(c, port->membase + URTX0);
  1298. /* flush */
  1299. do {
  1300. status = readl(port->membase + USR2);
  1301. } while (~status & USR2_TXDC);
  1302. /* restore control registers */
  1303. imx_port_ucrs_restore(port, &old_ucr);
  1304. }
  1305. #endif
  1306. static struct uart_ops imx_pops = {
  1307. .tx_empty = imx_tx_empty,
  1308. .set_mctrl = imx_set_mctrl,
  1309. .get_mctrl = imx_get_mctrl,
  1310. .stop_tx = imx_stop_tx,
  1311. .start_tx = imx_start_tx,
  1312. .stop_rx = imx_stop_rx,
  1313. .enable_ms = imx_enable_ms,
  1314. .break_ctl = imx_break_ctl,
  1315. .startup = imx_startup,
  1316. .shutdown = imx_shutdown,
  1317. .flush_buffer = imx_flush_buffer,
  1318. .set_termios = imx_set_termios,
  1319. .type = imx_type,
  1320. .config_port = imx_config_port,
  1321. .verify_port = imx_verify_port,
  1322. #if defined(CONFIG_CONSOLE_POLL)
  1323. .poll_get_char = imx_poll_get_char,
  1324. .poll_put_char = imx_poll_put_char,
  1325. #endif
  1326. };
  1327. static struct imx_port *imx_ports[UART_NR];
  1328. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  1329. static void imx_console_putchar(struct uart_port *port, int ch)
  1330. {
  1331. struct imx_port *sport = (struct imx_port *)port;
  1332. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  1333. barrier();
  1334. writel(ch, sport->port.membase + URTX0);
  1335. }
  1336. /*
  1337. * Interrupts are disabled on entering
  1338. */
  1339. static void
  1340. imx_console_write(struct console *co, const char *s, unsigned int count)
  1341. {
  1342. struct imx_port *sport = imx_ports[co->index];
  1343. struct imx_port_ucrs old_ucr;
  1344. unsigned int ucr1;
  1345. unsigned long flags = 0;
  1346. int locked = 1;
  1347. int retval;
  1348. retval = clk_enable(sport->clk_per);
  1349. if (retval)
  1350. return;
  1351. retval = clk_enable(sport->clk_ipg);
  1352. if (retval) {
  1353. clk_disable(sport->clk_per);
  1354. return;
  1355. }
  1356. if (sport->port.sysrq)
  1357. locked = 0;
  1358. else if (oops_in_progress)
  1359. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1360. else
  1361. spin_lock_irqsave(&sport->port.lock, flags);
  1362. /*
  1363. * First, save UCR1/2/3 and then disable interrupts
  1364. */
  1365. imx_port_ucrs_save(&sport->port, &old_ucr);
  1366. ucr1 = old_ucr.ucr1;
  1367. if (is_imx1_uart(sport))
  1368. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1369. ucr1 |= UCR1_UARTEN;
  1370. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1371. writel(ucr1, sport->port.membase + UCR1);
  1372. writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  1373. uart_console_write(&sport->port, s, count, imx_console_putchar);
  1374. /*
  1375. * Finally, wait for transmitter to become empty
  1376. * and restore UCR1/2/3
  1377. */
  1378. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  1379. imx_port_ucrs_restore(&sport->port, &old_ucr);
  1380. if (locked)
  1381. spin_unlock_irqrestore(&sport->port.lock, flags);
  1382. clk_disable(sport->clk_ipg);
  1383. clk_disable(sport->clk_per);
  1384. }
  1385. /*
  1386. * If the port was already initialised (eg, by a boot loader),
  1387. * try to determine the current setup.
  1388. */
  1389. static void __init
  1390. imx_console_get_options(struct imx_port *sport, int *baud,
  1391. int *parity, int *bits)
  1392. {
  1393. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  1394. /* ok, the port was enabled */
  1395. unsigned int ucr2, ubir, ubmr, uartclk;
  1396. unsigned int baud_raw;
  1397. unsigned int ucfr_rfdiv;
  1398. ucr2 = readl(sport->port.membase + UCR2);
  1399. *parity = 'n';
  1400. if (ucr2 & UCR2_PREN) {
  1401. if (ucr2 & UCR2_PROE)
  1402. *parity = 'o';
  1403. else
  1404. *parity = 'e';
  1405. }
  1406. if (ucr2 & UCR2_WS)
  1407. *bits = 8;
  1408. else
  1409. *bits = 7;
  1410. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  1411. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  1412. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  1413. if (ucfr_rfdiv == 6)
  1414. ucfr_rfdiv = 7;
  1415. else
  1416. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1417. uartclk = clk_get_rate(sport->clk_per);
  1418. uartclk /= ucfr_rfdiv;
  1419. { /*
  1420. * The next code provides exact computation of
  1421. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1422. * without need of float support or long long division,
  1423. * which would be required to prevent 32bit arithmetic overflow
  1424. */
  1425. unsigned int mul = ubir + 1;
  1426. unsigned int div = 16 * (ubmr + 1);
  1427. unsigned int rem = uartclk % div;
  1428. baud_raw = (uartclk / div) * mul;
  1429. baud_raw += (rem * mul + div / 2) / div;
  1430. *baud = (baud_raw + 50) / 100 * 100;
  1431. }
  1432. if (*baud != baud_raw)
  1433. pr_info("Console IMX rounded baud rate from %d to %d\n",
  1434. baud_raw, *baud);
  1435. }
  1436. }
  1437. static int __init
  1438. imx_console_setup(struct console *co, char *options)
  1439. {
  1440. struct imx_port *sport;
  1441. int baud = 9600;
  1442. int bits = 8;
  1443. int parity = 'n';
  1444. int flow = 'n';
  1445. int retval;
  1446. /*
  1447. * Check whether an invalid uart number has been specified, and
  1448. * if so, search for the first available port that does have
  1449. * console support.
  1450. */
  1451. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1452. co->index = 0;
  1453. sport = imx_ports[co->index];
  1454. if (sport == NULL)
  1455. return -ENODEV;
  1456. /* For setting the registers, we only need to enable the ipg clock. */
  1457. retval = clk_prepare_enable(sport->clk_ipg);
  1458. if (retval)
  1459. goto error_console;
  1460. if (options)
  1461. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1462. else
  1463. imx_console_get_options(sport, &baud, &parity, &bits);
  1464. imx_setup_ufcr(sport, 0);
  1465. retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1466. clk_disable(sport->clk_ipg);
  1467. if (retval) {
  1468. clk_unprepare(sport->clk_ipg);
  1469. goto error_console;
  1470. }
  1471. retval = clk_prepare(sport->clk_per);
  1472. if (retval)
  1473. clk_disable_unprepare(sport->clk_ipg);
  1474. error_console:
  1475. return retval;
  1476. }
  1477. static struct uart_driver imx_reg;
  1478. static struct console imx_console = {
  1479. .name = DEV_NAME,
  1480. .write = imx_console_write,
  1481. .device = uart_console_device,
  1482. .setup = imx_console_setup,
  1483. .flags = CON_PRINTBUFFER,
  1484. .index = -1,
  1485. .data = &imx_reg,
  1486. };
  1487. #define IMX_CONSOLE &imx_console
  1488. #else
  1489. #define IMX_CONSOLE NULL
  1490. #endif
  1491. static struct uart_driver imx_reg = {
  1492. .owner = THIS_MODULE,
  1493. .driver_name = DRIVER_NAME,
  1494. .dev_name = DEV_NAME,
  1495. .major = SERIAL_IMX_MAJOR,
  1496. .minor = MINOR_START,
  1497. .nr = ARRAY_SIZE(imx_ports),
  1498. .cons = IMX_CONSOLE,
  1499. };
  1500. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1501. {
  1502. struct imx_port *sport = platform_get_drvdata(dev);
  1503. unsigned int val;
  1504. /* enable wakeup from i.MX UART */
  1505. val = readl(sport->port.membase + UCR3);
  1506. val |= UCR3_AWAKEN;
  1507. writel(val, sport->port.membase + UCR3);
  1508. uart_suspend_port(&imx_reg, &sport->port);
  1509. return 0;
  1510. }
  1511. static int serial_imx_resume(struct platform_device *dev)
  1512. {
  1513. struct imx_port *sport = platform_get_drvdata(dev);
  1514. unsigned int val;
  1515. /* disable wakeup from i.MX UART */
  1516. val = readl(sport->port.membase + UCR3);
  1517. val &= ~UCR3_AWAKEN;
  1518. writel(val, sport->port.membase + UCR3);
  1519. uart_resume_port(&imx_reg, &sport->port);
  1520. return 0;
  1521. }
  1522. #ifdef CONFIG_OF
  1523. /*
  1524. * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
  1525. * could successfully get all information from dt or a negative errno.
  1526. */
  1527. static int serial_imx_probe_dt(struct imx_port *sport,
  1528. struct platform_device *pdev)
  1529. {
  1530. struct device_node *np = pdev->dev.of_node;
  1531. const struct of_device_id *of_id =
  1532. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1533. int ret;
  1534. if (!np)
  1535. /* no device tree device */
  1536. return 1;
  1537. ret = of_alias_get_id(np, "serial");
  1538. if (ret < 0) {
  1539. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1540. return ret;
  1541. }
  1542. sport->port.line = ret;
  1543. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1544. sport->have_rtscts = 1;
  1545. if (of_get_property(np, "fsl,irda-mode", NULL))
  1546. sport->use_irda = 1;
  1547. if (of_get_property(np, "fsl,dte-mode", NULL))
  1548. sport->dte_mode = 1;
  1549. sport->devdata = of_id->data;
  1550. return 0;
  1551. }
  1552. #else
  1553. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1554. struct platform_device *pdev)
  1555. {
  1556. return 1;
  1557. }
  1558. #endif
  1559. static void serial_imx_probe_pdata(struct imx_port *sport,
  1560. struct platform_device *pdev)
  1561. {
  1562. struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1563. sport->port.line = pdev->id;
  1564. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1565. if (!pdata)
  1566. return;
  1567. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1568. sport->have_rtscts = 1;
  1569. if (pdata->flags & IMXUART_IRDA)
  1570. sport->use_irda = 1;
  1571. }
  1572. static int serial_imx_probe(struct platform_device *pdev)
  1573. {
  1574. struct imx_port *sport;
  1575. void __iomem *base;
  1576. int ret = 0;
  1577. struct resource *res;
  1578. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1579. if (!sport)
  1580. return -ENOMEM;
  1581. ret = serial_imx_probe_dt(sport, pdev);
  1582. if (ret > 0)
  1583. serial_imx_probe_pdata(sport, pdev);
  1584. else if (ret < 0)
  1585. return ret;
  1586. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1587. base = devm_ioremap_resource(&pdev->dev, res);
  1588. if (IS_ERR(base))
  1589. return PTR_ERR(base);
  1590. sport->port.dev = &pdev->dev;
  1591. sport->port.mapbase = res->start;
  1592. sport->port.membase = base;
  1593. sport->port.type = PORT_IMX,
  1594. sport->port.iotype = UPIO_MEM;
  1595. sport->port.irq = platform_get_irq(pdev, 0);
  1596. sport->rxirq = platform_get_irq(pdev, 0);
  1597. sport->txirq = platform_get_irq(pdev, 1);
  1598. sport->rtsirq = platform_get_irq(pdev, 2);
  1599. sport->port.fifosize = 32;
  1600. sport->port.ops = &imx_pops;
  1601. sport->port.flags = UPF_BOOT_AUTOCONF;
  1602. init_timer(&sport->timer);
  1603. sport->timer.function = imx_timeout;
  1604. sport->timer.data = (unsigned long)sport;
  1605. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1606. if (IS_ERR(sport->clk_ipg)) {
  1607. ret = PTR_ERR(sport->clk_ipg);
  1608. dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
  1609. return ret;
  1610. }
  1611. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  1612. if (IS_ERR(sport->clk_per)) {
  1613. ret = PTR_ERR(sport->clk_per);
  1614. dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
  1615. return ret;
  1616. }
  1617. sport->port.uartclk = clk_get_rate(sport->clk_per);
  1618. imx_ports[sport->port.line] = sport;
  1619. platform_set_drvdata(pdev, sport);
  1620. return uart_add_one_port(&imx_reg, &sport->port);
  1621. }
  1622. static int serial_imx_remove(struct platform_device *pdev)
  1623. {
  1624. struct imx_port *sport = platform_get_drvdata(pdev);
  1625. return uart_remove_one_port(&imx_reg, &sport->port);
  1626. }
  1627. static struct platform_driver serial_imx_driver = {
  1628. .probe = serial_imx_probe,
  1629. .remove = serial_imx_remove,
  1630. .suspend = serial_imx_suspend,
  1631. .resume = serial_imx_resume,
  1632. .id_table = imx_uart_devtype,
  1633. .driver = {
  1634. .name = "imx-uart",
  1635. .owner = THIS_MODULE,
  1636. .of_match_table = imx_uart_dt_ids,
  1637. },
  1638. };
  1639. static int __init imx_serial_init(void)
  1640. {
  1641. int ret;
  1642. pr_info("Serial: IMX driver\n");
  1643. ret = uart_register_driver(&imx_reg);
  1644. if (ret)
  1645. return ret;
  1646. ret = platform_driver_register(&serial_imx_driver);
  1647. if (ret != 0)
  1648. uart_unregister_driver(&imx_reg);
  1649. return ret;
  1650. }
  1651. static void __exit imx_serial_exit(void)
  1652. {
  1653. platform_driver_unregister(&serial_imx_driver);
  1654. uart_unregister_driver(&imx_reg);
  1655. }
  1656. module_init(imx_serial_init);
  1657. module_exit(imx_serial_exit);
  1658. MODULE_AUTHOR("Sascha Hauer");
  1659. MODULE_DESCRIPTION("IMX generic serial port driver");
  1660. MODULE_LICENSE("GPL");
  1661. MODULE_ALIAS("platform:imx-uart");