imx.c 51 KB

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