imx.c 62 KB

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