imx.c 60 KB

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