imx.c 51 KB

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