imx.c 58 KB

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