imx.c 52 KB

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