imx.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  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_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
  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 rx_bytes, tx_bytes;
  214. struct work_struct tsk_dma_rx, tsk_dma_tx;
  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. /*
  397. * We are maybe in the SMP context, so if the DMA TX thread is running
  398. * on other cpu, we have to wait for it to finish.
  399. */
  400. if (sport->dma_is_enabled && sport->dma_is_rxing)
  401. return;
  402. temp = readl(sport->port.membase + UCR2);
  403. writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
  404. }
  405. /*
  406. * Set the modem control timer to fire immediately.
  407. */
  408. static void imx_enable_ms(struct uart_port *port)
  409. {
  410. struct imx_port *sport = (struct imx_port *)port;
  411. mod_timer(&sport->timer, jiffies);
  412. }
  413. static inline void imx_transmit_buffer(struct imx_port *sport)
  414. {
  415. struct circ_buf *xmit = &sport->port.state->xmit;
  416. while (!uart_circ_empty(xmit) &&
  417. !(readl(sport->port.membase + uts_reg(sport))
  418. & UTS_TXFULL)) {
  419. /* send xmit->buf[xmit->tail]
  420. * out the port here */
  421. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  422. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  423. sport->port.icount.tx++;
  424. }
  425. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  426. uart_write_wakeup(&sport->port);
  427. if (uart_circ_empty(xmit))
  428. imx_stop_tx(&sport->port);
  429. }
  430. static void dma_tx_callback(void *data)
  431. {
  432. struct imx_port *sport = data;
  433. struct scatterlist *sgl = &sport->tx_sgl[0];
  434. struct circ_buf *xmit = &sport->port.state->xmit;
  435. unsigned long flags;
  436. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  437. sport->dma_is_txing = 0;
  438. /* update the stat */
  439. spin_lock_irqsave(&sport->port.lock, flags);
  440. xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
  441. sport->port.icount.tx += sport->tx_bytes;
  442. spin_unlock_irqrestore(&sport->port.lock, flags);
  443. dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
  444. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  445. uart_write_wakeup(&sport->port);
  446. if (waitqueue_active(&sport->dma_wait)) {
  447. wake_up(&sport->dma_wait);
  448. dev_dbg(sport->port.dev, "exit in %s.\n", __func__);
  449. return;
  450. }
  451. schedule_work(&sport->tsk_dma_tx);
  452. }
  453. static void dma_tx_work(struct work_struct *w)
  454. {
  455. struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_tx);
  456. struct circ_buf *xmit = &sport->port.state->xmit;
  457. struct scatterlist *sgl = sport->tx_sgl;
  458. struct dma_async_tx_descriptor *desc;
  459. struct dma_chan *chan = sport->dma_chan_tx;
  460. struct device *dev = sport->port.dev;
  461. enum dma_status status;
  462. unsigned long flags;
  463. int ret;
  464. status = chan->device->device_tx_status(chan, (dma_cookie_t)0, NULL);
  465. if (DMA_IN_PROGRESS == status)
  466. return;
  467. spin_lock_irqsave(&sport->port.lock, flags);
  468. sport->tx_bytes = uart_circ_chars_pending(xmit);
  469. if (sport->tx_bytes == 0) {
  470. spin_unlock_irqrestore(&sport->port.lock, flags);
  471. return;
  472. }
  473. if (xmit->tail > xmit->head) {
  474. sport->dma_tx_nents = 2;
  475. sg_init_table(sgl, 2);
  476. sg_set_buf(sgl, xmit->buf + xmit->tail,
  477. UART_XMIT_SIZE - xmit->tail);
  478. sg_set_buf(sgl + 1, xmit->buf, xmit->head);
  479. } else {
  480. sport->dma_tx_nents = 1;
  481. sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
  482. }
  483. spin_unlock_irqrestore(&sport->port.lock, flags);
  484. ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  485. if (ret == 0) {
  486. dev_err(dev, "DMA mapping error for TX.\n");
  487. return;
  488. }
  489. desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
  490. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  491. if (!desc) {
  492. dev_err(dev, "We cannot prepare for the TX slave dma!\n");
  493. return;
  494. }
  495. desc->callback = dma_tx_callback;
  496. desc->callback_param = sport;
  497. dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
  498. uart_circ_chars_pending(xmit));
  499. /* fire it */
  500. sport->dma_is_txing = 1;
  501. dmaengine_submit(desc);
  502. dma_async_issue_pending(chan);
  503. return;
  504. }
  505. /*
  506. * interrupts disabled on entry
  507. */
  508. static void imx_start_tx(struct uart_port *port)
  509. {
  510. struct imx_port *sport = (struct imx_port *)port;
  511. unsigned long temp;
  512. if (USE_IRDA(sport)) {
  513. /* half duplex in IrDA mode; have to disable receive mode */
  514. temp = readl(sport->port.membase + UCR4);
  515. temp &= ~(UCR4_DREN);
  516. writel(temp, sport->port.membase + UCR4);
  517. temp = readl(sport->port.membase + UCR1);
  518. temp &= ~(UCR1_RRDYEN);
  519. writel(temp, sport->port.membase + UCR1);
  520. }
  521. /* Clear any pending ORE flag before enabling interrupt */
  522. temp = readl(sport->port.membase + USR2);
  523. writel(temp | USR2_ORE, sport->port.membase + USR2);
  524. temp = readl(sport->port.membase + UCR4);
  525. temp |= UCR4_OREN;
  526. writel(temp, sport->port.membase + UCR4);
  527. if (!sport->dma_is_enabled) {
  528. temp = readl(sport->port.membase + UCR1);
  529. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  530. }
  531. if (USE_IRDA(sport)) {
  532. temp = readl(sport->port.membase + UCR1);
  533. temp |= UCR1_TRDYEN;
  534. writel(temp, sport->port.membase + UCR1);
  535. temp = readl(sport->port.membase + UCR4);
  536. temp |= UCR4_TCEN;
  537. writel(temp, sport->port.membase + UCR4);
  538. }
  539. if (sport->dma_is_enabled) {
  540. /*
  541. * We may in the interrupt context, so arise a work_struct to
  542. * do the real job.
  543. */
  544. schedule_work(&sport->tsk_dma_tx);
  545. return;
  546. }
  547. if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY)
  548. imx_transmit_buffer(sport);
  549. }
  550. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  551. {
  552. struct imx_port *sport = dev_id;
  553. unsigned int val;
  554. unsigned long flags;
  555. spin_lock_irqsave(&sport->port.lock, flags);
  556. writel(USR1_RTSD, sport->port.membase + USR1);
  557. val = readl(sport->port.membase + USR1) & USR1_RTSS;
  558. uart_handle_cts_change(&sport->port, !!val);
  559. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  560. spin_unlock_irqrestore(&sport->port.lock, flags);
  561. return IRQ_HANDLED;
  562. }
  563. static irqreturn_t imx_txint(int irq, void *dev_id)
  564. {
  565. struct imx_port *sport = dev_id;
  566. struct circ_buf *xmit = &sport->port.state->xmit;
  567. unsigned long flags;
  568. spin_lock_irqsave(&sport->port.lock, flags);
  569. if (sport->port.x_char) {
  570. /* Send next char */
  571. writel(sport->port.x_char, sport->port.membase + URTX0);
  572. goto out;
  573. }
  574. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  575. imx_stop_tx(&sport->port);
  576. goto out;
  577. }
  578. imx_transmit_buffer(sport);
  579. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  580. uart_write_wakeup(&sport->port);
  581. out:
  582. spin_unlock_irqrestore(&sport->port.lock, flags);
  583. return IRQ_HANDLED;
  584. }
  585. static irqreturn_t imx_rxint(int irq, void *dev_id)
  586. {
  587. struct imx_port *sport = dev_id;
  588. unsigned int rx, flg, ignored = 0;
  589. struct tty_port *port = &sport->port.state->port;
  590. unsigned long flags, temp;
  591. spin_lock_irqsave(&sport->port.lock, flags);
  592. while (readl(sport->port.membase + USR2) & USR2_RDR) {
  593. flg = TTY_NORMAL;
  594. sport->port.icount.rx++;
  595. rx = readl(sport->port.membase + URXD0);
  596. temp = readl(sport->port.membase + USR2);
  597. if (temp & USR2_BRCD) {
  598. writel(USR2_BRCD, sport->port.membase + USR2);
  599. if (uart_handle_break(&sport->port))
  600. continue;
  601. }
  602. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  603. continue;
  604. if (unlikely(rx & URXD_ERR)) {
  605. if (rx & URXD_BRK)
  606. sport->port.icount.brk++;
  607. else if (rx & URXD_PRERR)
  608. sport->port.icount.parity++;
  609. else if (rx & URXD_FRMERR)
  610. sport->port.icount.frame++;
  611. if (rx & URXD_OVRRUN)
  612. sport->port.icount.overrun++;
  613. if (rx & sport->port.ignore_status_mask) {
  614. if (++ignored > 100)
  615. goto out;
  616. continue;
  617. }
  618. rx &= sport->port.read_status_mask;
  619. if (rx & URXD_BRK)
  620. flg = TTY_BREAK;
  621. else if (rx & URXD_PRERR)
  622. flg = TTY_PARITY;
  623. else if (rx & URXD_FRMERR)
  624. flg = TTY_FRAME;
  625. if (rx & URXD_OVRRUN)
  626. flg = TTY_OVERRUN;
  627. #ifdef SUPPORT_SYSRQ
  628. sport->port.sysrq = 0;
  629. #endif
  630. }
  631. tty_insert_flip_char(port, rx, flg);
  632. }
  633. out:
  634. spin_unlock_irqrestore(&sport->port.lock, flags);
  635. tty_flip_buffer_push(port);
  636. return IRQ_HANDLED;
  637. }
  638. /*
  639. * If the RXFIFO is filled with some data, and then we
  640. * arise a DMA operation to receive them.
  641. */
  642. static void imx_dma_rxint(struct imx_port *sport)
  643. {
  644. unsigned long temp;
  645. temp = readl(sport->port.membase + USR2);
  646. if ((temp & USR2_RDR) && !sport->dma_is_rxing) {
  647. sport->dma_is_rxing = 1;
  648. /* disable the `Recerver Ready Interrrupt` */
  649. temp = readl(sport->port.membase + UCR1);
  650. temp &= ~(UCR1_RRDYEN);
  651. writel(temp, sport->port.membase + UCR1);
  652. /* tell the DMA to receive the data. */
  653. schedule_work(&sport->tsk_dma_rx);
  654. }
  655. }
  656. static irqreturn_t imx_int(int irq, void *dev_id)
  657. {
  658. struct imx_port *sport = dev_id;
  659. unsigned int sts;
  660. unsigned int sts2;
  661. sts = readl(sport->port.membase + USR1);
  662. if (sts & USR1_RRDY) {
  663. if (sport->dma_is_enabled)
  664. imx_dma_rxint(sport);
  665. else
  666. imx_rxint(irq, dev_id);
  667. }
  668. if (sts & USR1_TRDY &&
  669. readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
  670. imx_txint(irq, dev_id);
  671. if (sts & USR1_RTSD)
  672. imx_rtsint(irq, dev_id);
  673. if (sts & USR1_AWAKE)
  674. writel(USR1_AWAKE, sport->port.membase + USR1);
  675. sts2 = readl(sport->port.membase + USR2);
  676. if (sts2 & USR2_ORE) {
  677. dev_err(sport->port.dev, "Rx FIFO overrun\n");
  678. sport->port.icount.overrun++;
  679. writel(sts2 | USR2_ORE, sport->port.membase + USR2);
  680. }
  681. return IRQ_HANDLED;
  682. }
  683. /*
  684. * Return TIOCSER_TEMT when transmitter is not busy.
  685. */
  686. static unsigned int imx_tx_empty(struct uart_port *port)
  687. {
  688. struct imx_port *sport = (struct imx_port *)port;
  689. return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  690. }
  691. /*
  692. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  693. */
  694. static unsigned int imx_get_mctrl(struct uart_port *port)
  695. {
  696. struct imx_port *sport = (struct imx_port *)port;
  697. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  698. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  699. tmp |= TIOCM_CTS;
  700. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  701. tmp |= TIOCM_RTS;
  702. return tmp;
  703. }
  704. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  705. {
  706. struct imx_port *sport = (struct imx_port *)port;
  707. unsigned long temp;
  708. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  709. if (mctrl & TIOCM_RTS)
  710. if (!sport->dma_is_enabled)
  711. temp |= UCR2_CTS;
  712. writel(temp, sport->port.membase + UCR2);
  713. }
  714. /*
  715. * Interrupts always disabled.
  716. */
  717. static void imx_break_ctl(struct uart_port *port, int break_state)
  718. {
  719. struct imx_port *sport = (struct imx_port *)port;
  720. unsigned long flags, temp;
  721. spin_lock_irqsave(&sport->port.lock, flags);
  722. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  723. if (break_state != 0)
  724. temp |= UCR1_SNDBRK;
  725. writel(temp, sport->port.membase + UCR1);
  726. spin_unlock_irqrestore(&sport->port.lock, flags);
  727. }
  728. #define TXTL 2 /* reset default */
  729. #define RXTL 1 /* reset default */
  730. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  731. {
  732. unsigned int val;
  733. /* set receiver / transmitter trigger level */
  734. val = readl(sport->port.membase + UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
  735. val |= TXTL << UFCR_TXTL_SHF | RXTL;
  736. writel(val, sport->port.membase + UFCR);
  737. return 0;
  738. }
  739. #define RX_BUF_SIZE (PAGE_SIZE)
  740. static int start_rx_dma(struct imx_port *sport);
  741. static void dma_rx_work(struct work_struct *w)
  742. {
  743. struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_rx);
  744. struct tty_port *port = &sport->port.state->port;
  745. if (sport->rx_bytes) {
  746. tty_insert_flip_string(port, sport->rx_buf, sport->rx_bytes);
  747. tty_flip_buffer_push(port);
  748. sport->rx_bytes = 0;
  749. }
  750. if (sport->dma_is_rxing)
  751. start_rx_dma(sport);
  752. }
  753. static void imx_rx_dma_done(struct imx_port *sport)
  754. {
  755. unsigned long temp;
  756. /* Enable this interrupt when the RXFIFO is empty. */
  757. temp = readl(sport->port.membase + UCR1);
  758. temp |= UCR1_RRDYEN;
  759. writel(temp, sport->port.membase + UCR1);
  760. sport->dma_is_rxing = 0;
  761. /* Is the shutdown waiting for us? */
  762. if (waitqueue_active(&sport->dma_wait))
  763. wake_up(&sport->dma_wait);
  764. }
  765. /*
  766. * There are three kinds of RX DMA interrupts(such as in the MX6Q):
  767. * [1] the RX DMA buffer is full.
  768. * [2] the Aging timer expires(wait for 8 bytes long)
  769. * [3] the Idle Condition Detect(enabled the UCR4_IDDMAEN).
  770. *
  771. * The [2] is trigger when a character was been sitting in the FIFO
  772. * meanwhile [3] can wait for 32 bytes long when the RX line is
  773. * on IDLE state and RxFIFO is empty.
  774. */
  775. static void dma_rx_callback(void *data)
  776. {
  777. struct imx_port *sport = data;
  778. struct dma_chan *chan = sport->dma_chan_rx;
  779. struct scatterlist *sgl = &sport->rx_sgl;
  780. struct dma_tx_state state;
  781. enum dma_status status;
  782. unsigned int count;
  783. /* unmap it first */
  784. dma_unmap_sg(sport->port.dev, sgl, 1, DMA_FROM_DEVICE);
  785. status = chan->device->device_tx_status(chan, (dma_cookie_t)0, &state);
  786. count = RX_BUF_SIZE - state.residue;
  787. dev_dbg(sport->port.dev, "We get %d bytes.\n", count);
  788. if (count) {
  789. sport->rx_bytes = count;
  790. schedule_work(&sport->tsk_dma_rx);
  791. } else
  792. imx_rx_dma_done(sport);
  793. }
  794. static int start_rx_dma(struct imx_port *sport)
  795. {
  796. struct scatterlist *sgl = &sport->rx_sgl;
  797. struct dma_chan *chan = sport->dma_chan_rx;
  798. struct device *dev = sport->port.dev;
  799. struct dma_async_tx_descriptor *desc;
  800. int ret;
  801. sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
  802. ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
  803. if (ret == 0) {
  804. dev_err(dev, "DMA mapping error for RX.\n");
  805. return -EINVAL;
  806. }
  807. desc = dmaengine_prep_slave_sg(chan, sgl, 1, DMA_DEV_TO_MEM,
  808. DMA_PREP_INTERRUPT);
  809. if (!desc) {
  810. dev_err(dev, "We cannot prepare for the RX slave dma!\n");
  811. return -EINVAL;
  812. }
  813. desc->callback = dma_rx_callback;
  814. desc->callback_param = sport;
  815. dev_dbg(dev, "RX: prepare for the DMA.\n");
  816. dmaengine_submit(desc);
  817. dma_async_issue_pending(chan);
  818. return 0;
  819. }
  820. static void imx_uart_dma_exit(struct imx_port *sport)
  821. {
  822. if (sport->dma_chan_rx) {
  823. dma_release_channel(sport->dma_chan_rx);
  824. sport->dma_chan_rx = NULL;
  825. kfree(sport->rx_buf);
  826. sport->rx_buf = NULL;
  827. }
  828. if (sport->dma_chan_tx) {
  829. dma_release_channel(sport->dma_chan_tx);
  830. sport->dma_chan_tx = NULL;
  831. }
  832. sport->dma_is_inited = 0;
  833. }
  834. static int imx_uart_dma_init(struct imx_port *sport)
  835. {
  836. struct dma_slave_config slave_config = {};
  837. struct device *dev = sport->port.dev;
  838. int ret;
  839. /* Prepare for RX : */
  840. sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
  841. if (!sport->dma_chan_rx) {
  842. dev_dbg(dev, "cannot get the DMA channel.\n");
  843. ret = -EINVAL;
  844. goto err;
  845. }
  846. slave_config.direction = DMA_DEV_TO_MEM;
  847. slave_config.src_addr = sport->port.mapbase + URXD0;
  848. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  849. slave_config.src_maxburst = RXTL;
  850. ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
  851. if (ret) {
  852. dev_err(dev, "error in RX dma configuration.\n");
  853. goto err;
  854. }
  855. sport->rx_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  856. if (!sport->rx_buf) {
  857. dev_err(dev, "cannot alloc DMA buffer.\n");
  858. ret = -ENOMEM;
  859. goto err;
  860. }
  861. sport->rx_bytes = 0;
  862. /* Prepare for TX : */
  863. sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
  864. if (!sport->dma_chan_tx) {
  865. dev_err(dev, "cannot get the TX DMA channel!\n");
  866. ret = -EINVAL;
  867. goto err;
  868. }
  869. slave_config.direction = DMA_MEM_TO_DEV;
  870. slave_config.dst_addr = sport->port.mapbase + URTX0;
  871. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  872. slave_config.dst_maxburst = TXTL;
  873. ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
  874. if (ret) {
  875. dev_err(dev, "error in TX dma configuration.");
  876. goto err;
  877. }
  878. sport->dma_is_inited = 1;
  879. return 0;
  880. err:
  881. imx_uart_dma_exit(sport);
  882. return ret;
  883. }
  884. static void imx_enable_dma(struct imx_port *sport)
  885. {
  886. unsigned long temp;
  887. struct tty_port *port = &sport->port.state->port;
  888. port->low_latency = 1;
  889. INIT_WORK(&sport->tsk_dma_tx, dma_tx_work);
  890. INIT_WORK(&sport->tsk_dma_rx, dma_rx_work);
  891. init_waitqueue_head(&sport->dma_wait);
  892. /* set UCR1 */
  893. temp = readl(sport->port.membase + UCR1);
  894. temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN |
  895. /* wait for 32 idle frames for IDDMA interrupt */
  896. UCR1_ICD_REG(3);
  897. writel(temp, sport->port.membase + UCR1);
  898. /* set UCR4 */
  899. temp = readl(sport->port.membase + UCR4);
  900. temp |= UCR4_IDDMAEN;
  901. writel(temp, sport->port.membase + UCR4);
  902. sport->dma_is_enabled = 1;
  903. }
  904. static void imx_disable_dma(struct imx_port *sport)
  905. {
  906. unsigned long temp;
  907. struct tty_port *port = &sport->port.state->port;
  908. /* clear UCR1 */
  909. temp = readl(sport->port.membase + UCR1);
  910. temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
  911. writel(temp, sport->port.membase + UCR1);
  912. /* clear UCR2 */
  913. temp = readl(sport->port.membase + UCR2);
  914. temp &= ~(UCR2_CTSC | UCR2_CTS);
  915. writel(temp, sport->port.membase + UCR2);
  916. /* clear UCR4 */
  917. temp = readl(sport->port.membase + UCR4);
  918. temp &= ~UCR4_IDDMAEN;
  919. writel(temp, sport->port.membase + UCR4);
  920. sport->dma_is_enabled = 0;
  921. port->low_latency = 0;
  922. }
  923. /* half the RX buffer size */
  924. #define CTSTL 16
  925. static int imx_startup(struct uart_port *port)
  926. {
  927. struct imx_port *sport = (struct imx_port *)port;
  928. int retval;
  929. unsigned long flags, temp;
  930. retval = clk_prepare_enable(sport->clk_per);
  931. if (retval)
  932. goto error_out1;
  933. retval = clk_prepare_enable(sport->clk_ipg);
  934. if (retval) {
  935. clk_disable_unprepare(sport->clk_per);
  936. goto error_out1;
  937. }
  938. imx_setup_ufcr(sport, 0);
  939. /* disable the DREN bit (Data Ready interrupt enable) before
  940. * requesting IRQs
  941. */
  942. temp = readl(sport->port.membase + UCR4);
  943. if (USE_IRDA(sport))
  944. temp |= UCR4_IRSC;
  945. /* set the trigger level for CTS */
  946. temp &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
  947. temp |= CTSTL << UCR4_CTSTL_SHF;
  948. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  949. if (USE_IRDA(sport)) {
  950. /* reset fifo's and state machines */
  951. int i = 100;
  952. temp = readl(sport->port.membase + UCR2);
  953. temp &= ~UCR2_SRST;
  954. writel(temp, sport->port.membase + UCR2);
  955. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  956. (--i > 0)) {
  957. udelay(1);
  958. }
  959. }
  960. /*
  961. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  962. * chips only have one interrupt.
  963. */
  964. if (sport->txirq > 0) {
  965. retval = request_irq(sport->rxirq, imx_rxint, 0,
  966. DRIVER_NAME, sport);
  967. if (retval)
  968. goto error_out1;
  969. retval = request_irq(sport->txirq, imx_txint, 0,
  970. DRIVER_NAME, sport);
  971. if (retval)
  972. goto error_out2;
  973. /* do not use RTS IRQ on IrDA */
  974. if (!USE_IRDA(sport)) {
  975. retval = request_irq(sport->rtsirq, imx_rtsint, 0,
  976. DRIVER_NAME, sport);
  977. if (retval)
  978. goto error_out3;
  979. }
  980. } else {
  981. retval = request_irq(sport->port.irq, imx_int, 0,
  982. DRIVER_NAME, sport);
  983. if (retval) {
  984. free_irq(sport->port.irq, sport);
  985. goto error_out1;
  986. }
  987. }
  988. spin_lock_irqsave(&sport->port.lock, flags);
  989. /*
  990. * Finally, clear and enable interrupts
  991. */
  992. writel(USR1_RTSD, sport->port.membase + USR1);
  993. temp = readl(sport->port.membase + UCR1);
  994. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  995. if (USE_IRDA(sport)) {
  996. temp |= UCR1_IREN;
  997. temp &= ~(UCR1_RTSDEN);
  998. }
  999. writel(temp, sport->port.membase + UCR1);
  1000. temp = readl(sport->port.membase + UCR2);
  1001. temp |= (UCR2_RXEN | UCR2_TXEN);
  1002. if (!sport->have_rtscts)
  1003. temp |= UCR2_IRTS;
  1004. writel(temp, sport->port.membase + UCR2);
  1005. if (USE_IRDA(sport)) {
  1006. /* clear RX-FIFO */
  1007. int i = 64;
  1008. while ((--i > 0) &&
  1009. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  1010. barrier();
  1011. }
  1012. }
  1013. if (!is_imx1_uart(sport)) {
  1014. temp = readl(sport->port.membase + UCR3);
  1015. temp |= IMX21_UCR3_RXDMUXSEL;
  1016. writel(temp, sport->port.membase + UCR3);
  1017. }
  1018. if (USE_IRDA(sport)) {
  1019. temp = readl(sport->port.membase + UCR4);
  1020. if (sport->irda_inv_rx)
  1021. temp |= UCR4_INVR;
  1022. else
  1023. temp &= ~(UCR4_INVR);
  1024. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  1025. temp = readl(sport->port.membase + UCR3);
  1026. if (sport->irda_inv_tx)
  1027. temp |= UCR3_INVT;
  1028. else
  1029. temp &= ~(UCR3_INVT);
  1030. writel(temp, sport->port.membase + UCR3);
  1031. }
  1032. /*
  1033. * Enable modem status interrupts
  1034. */
  1035. imx_enable_ms(&sport->port);
  1036. spin_unlock_irqrestore(&sport->port.lock, flags);
  1037. if (USE_IRDA(sport)) {
  1038. struct imxuart_platform_data *pdata;
  1039. pdata = dev_get_platdata(sport->port.dev);
  1040. sport->irda_inv_rx = pdata->irda_inv_rx;
  1041. sport->irda_inv_tx = pdata->irda_inv_tx;
  1042. sport->trcv_delay = pdata->transceiver_delay;
  1043. if (pdata->irda_enable)
  1044. pdata->irda_enable(1);
  1045. }
  1046. return 0;
  1047. error_out3:
  1048. if (sport->txirq)
  1049. free_irq(sport->txirq, sport);
  1050. error_out2:
  1051. if (sport->rxirq)
  1052. free_irq(sport->rxirq, sport);
  1053. error_out1:
  1054. return retval;
  1055. }
  1056. static void imx_shutdown(struct uart_port *port)
  1057. {
  1058. struct imx_port *sport = (struct imx_port *)port;
  1059. unsigned long temp;
  1060. unsigned long flags;
  1061. if (sport->dma_is_enabled) {
  1062. /* We have to wait for the DMA to finish. */
  1063. wait_event(sport->dma_wait,
  1064. !sport->dma_is_rxing && !sport->dma_is_txing);
  1065. imx_stop_rx(port);
  1066. imx_disable_dma(sport);
  1067. imx_uart_dma_exit(sport);
  1068. }
  1069. spin_lock_irqsave(&sport->port.lock, flags);
  1070. temp = readl(sport->port.membase + UCR2);
  1071. temp &= ~(UCR2_TXEN);
  1072. writel(temp, sport->port.membase + UCR2);
  1073. spin_unlock_irqrestore(&sport->port.lock, flags);
  1074. if (USE_IRDA(sport)) {
  1075. struct imxuart_platform_data *pdata;
  1076. pdata = dev_get_platdata(sport->port.dev);
  1077. if (pdata->irda_enable)
  1078. pdata->irda_enable(0);
  1079. }
  1080. /*
  1081. * Stop our timer.
  1082. */
  1083. del_timer_sync(&sport->timer);
  1084. /*
  1085. * Free the interrupts
  1086. */
  1087. if (sport->txirq > 0) {
  1088. if (!USE_IRDA(sport))
  1089. free_irq(sport->rtsirq, sport);
  1090. free_irq(sport->txirq, sport);
  1091. free_irq(sport->rxirq, sport);
  1092. } else
  1093. free_irq(sport->port.irq, sport);
  1094. /*
  1095. * Disable all interrupts, port and break condition.
  1096. */
  1097. spin_lock_irqsave(&sport->port.lock, flags);
  1098. temp = readl(sport->port.membase + UCR1);
  1099. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  1100. if (USE_IRDA(sport))
  1101. temp &= ~(UCR1_IREN);
  1102. writel(temp, sport->port.membase + UCR1);
  1103. spin_unlock_irqrestore(&sport->port.lock, flags);
  1104. clk_disable_unprepare(sport->clk_per);
  1105. clk_disable_unprepare(sport->clk_ipg);
  1106. }
  1107. static void
  1108. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  1109. struct ktermios *old)
  1110. {
  1111. struct imx_port *sport = (struct imx_port *)port;
  1112. unsigned long flags;
  1113. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  1114. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1115. unsigned int div, ufcr;
  1116. unsigned long num, denom;
  1117. uint64_t tdiv64;
  1118. /*
  1119. * If we don't support modem control lines, don't allow
  1120. * these to be set.
  1121. */
  1122. if (0) {
  1123. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  1124. termios->c_cflag |= CLOCAL;
  1125. }
  1126. /*
  1127. * We only support CS7 and CS8.
  1128. */
  1129. while ((termios->c_cflag & CSIZE) != CS7 &&
  1130. (termios->c_cflag & CSIZE) != CS8) {
  1131. termios->c_cflag &= ~CSIZE;
  1132. termios->c_cflag |= old_csize;
  1133. old_csize = CS8;
  1134. }
  1135. if ((termios->c_cflag & CSIZE) == CS8)
  1136. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  1137. else
  1138. ucr2 = UCR2_SRST | UCR2_IRTS;
  1139. if (termios->c_cflag & CRTSCTS) {
  1140. if (sport->have_rtscts) {
  1141. ucr2 &= ~UCR2_IRTS;
  1142. ucr2 |= UCR2_CTSC;
  1143. /* Can we enable the DMA support? */
  1144. if (is_imx6q_uart(sport) && !uart_console(port)
  1145. && !sport->dma_is_inited)
  1146. imx_uart_dma_init(sport);
  1147. } else {
  1148. termios->c_cflag &= ~CRTSCTS;
  1149. }
  1150. }
  1151. if (termios->c_cflag & CSTOPB)
  1152. ucr2 |= UCR2_STPB;
  1153. if (termios->c_cflag & PARENB) {
  1154. ucr2 |= UCR2_PREN;
  1155. if (termios->c_cflag & PARODD)
  1156. ucr2 |= UCR2_PROE;
  1157. }
  1158. del_timer_sync(&sport->timer);
  1159. /*
  1160. * Ask the core to calculate the divisor for us.
  1161. */
  1162. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1163. quot = uart_get_divisor(port, baud);
  1164. spin_lock_irqsave(&sport->port.lock, flags);
  1165. sport->port.read_status_mask = 0;
  1166. if (termios->c_iflag & INPCK)
  1167. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  1168. if (termios->c_iflag & (BRKINT | PARMRK))
  1169. sport->port.read_status_mask |= URXD_BRK;
  1170. /*
  1171. * Characters to ignore
  1172. */
  1173. sport->port.ignore_status_mask = 0;
  1174. if (termios->c_iflag & IGNPAR)
  1175. sport->port.ignore_status_mask |= URXD_PRERR;
  1176. if (termios->c_iflag & IGNBRK) {
  1177. sport->port.ignore_status_mask |= URXD_BRK;
  1178. /*
  1179. * If we're ignoring parity and break indicators,
  1180. * ignore overruns too (for real raw support).
  1181. */
  1182. if (termios->c_iflag & IGNPAR)
  1183. sport->port.ignore_status_mask |= URXD_OVRRUN;
  1184. }
  1185. /*
  1186. * Update the per-port timeout.
  1187. */
  1188. uart_update_timeout(port, termios->c_cflag, baud);
  1189. /*
  1190. * disable interrupts and drain transmitter
  1191. */
  1192. old_ucr1 = readl(sport->port.membase + UCR1);
  1193. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  1194. sport->port.membase + UCR1);
  1195. while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
  1196. barrier();
  1197. /* then, disable everything */
  1198. old_txrxen = readl(sport->port.membase + UCR2);
  1199. writel(old_txrxen & ~(UCR2_TXEN | UCR2_RXEN),
  1200. sport->port.membase + UCR2);
  1201. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  1202. if (USE_IRDA(sport)) {
  1203. /*
  1204. * use maximum available submodule frequency to
  1205. * avoid missing short pulses due to low sampling rate
  1206. */
  1207. div = 1;
  1208. } else {
  1209. /* custom-baudrate handling */
  1210. div = sport->port.uartclk / (baud * 16);
  1211. if (baud == 38400 && quot != div)
  1212. baud = sport->port.uartclk / (quot * 16);
  1213. div = sport->port.uartclk / (baud * 16);
  1214. if (div > 7)
  1215. div = 7;
  1216. if (!div)
  1217. div = 1;
  1218. }
  1219. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  1220. 1 << 16, 1 << 16, &num, &denom);
  1221. tdiv64 = sport->port.uartclk;
  1222. tdiv64 *= num;
  1223. do_div(tdiv64, denom * 16 * div);
  1224. tty_termios_encode_baud_rate(termios,
  1225. (speed_t)tdiv64, (speed_t)tdiv64);
  1226. num -= 1;
  1227. denom -= 1;
  1228. ufcr = readl(sport->port.membase + UFCR);
  1229. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  1230. if (sport->dte_mode)
  1231. ufcr |= UFCR_DCEDTE;
  1232. writel(ufcr, sport->port.membase + UFCR);
  1233. writel(num, sport->port.membase + UBIR);
  1234. writel(denom, sport->port.membase + UBMR);
  1235. if (!is_imx1_uart(sport))
  1236. writel(sport->port.uartclk / div / 1000,
  1237. sport->port.membase + IMX21_ONEMS);
  1238. writel(old_ucr1, sport->port.membase + UCR1);
  1239. /* set the parity, stop bits and data size */
  1240. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  1241. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  1242. imx_enable_ms(&sport->port);
  1243. if (sport->dma_is_inited && !sport->dma_is_enabled)
  1244. imx_enable_dma(sport);
  1245. spin_unlock_irqrestore(&sport->port.lock, flags);
  1246. }
  1247. static const char *imx_type(struct uart_port *port)
  1248. {
  1249. struct imx_port *sport = (struct imx_port *)port;
  1250. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  1251. }
  1252. /*
  1253. * Release the memory region(s) being used by 'port'.
  1254. */
  1255. static void imx_release_port(struct uart_port *port)
  1256. {
  1257. struct platform_device *pdev = to_platform_device(port->dev);
  1258. struct resource *mmres;
  1259. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1260. release_mem_region(mmres->start, resource_size(mmres));
  1261. }
  1262. /*
  1263. * Request the memory region(s) being used by 'port'.
  1264. */
  1265. static int imx_request_port(struct uart_port *port)
  1266. {
  1267. struct platform_device *pdev = to_platform_device(port->dev);
  1268. struct resource *mmres;
  1269. void *ret;
  1270. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1271. if (!mmres)
  1272. return -ENODEV;
  1273. ret = request_mem_region(mmres->start, resource_size(mmres), "imx-uart");
  1274. return ret ? 0 : -EBUSY;
  1275. }
  1276. /*
  1277. * Configure/autoconfigure the port.
  1278. */
  1279. static void imx_config_port(struct uart_port *port, int flags)
  1280. {
  1281. struct imx_port *sport = (struct imx_port *)port;
  1282. if (flags & UART_CONFIG_TYPE &&
  1283. imx_request_port(&sport->port) == 0)
  1284. sport->port.type = PORT_IMX;
  1285. }
  1286. /*
  1287. * Verify the new serial_struct (for TIOCSSERIAL).
  1288. * The only change we allow are to the flags and type, and
  1289. * even then only between PORT_IMX and PORT_UNKNOWN
  1290. */
  1291. static int
  1292. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  1293. {
  1294. struct imx_port *sport = (struct imx_port *)port;
  1295. int ret = 0;
  1296. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  1297. ret = -EINVAL;
  1298. if (sport->port.irq != ser->irq)
  1299. ret = -EINVAL;
  1300. if (ser->io_type != UPIO_MEM)
  1301. ret = -EINVAL;
  1302. if (sport->port.uartclk / 16 != ser->baud_base)
  1303. ret = -EINVAL;
  1304. if ((void *)sport->port.mapbase != ser->iomem_base)
  1305. ret = -EINVAL;
  1306. if (sport->port.iobase != ser->port)
  1307. ret = -EINVAL;
  1308. if (ser->hub6 != 0)
  1309. ret = -EINVAL;
  1310. return ret;
  1311. }
  1312. #if defined(CONFIG_CONSOLE_POLL)
  1313. static int imx_poll_get_char(struct uart_port *port)
  1314. {
  1315. struct imx_port_ucrs old_ucr;
  1316. unsigned int status;
  1317. unsigned char c;
  1318. /* save control registers */
  1319. imx_port_ucrs_save(port, &old_ucr);
  1320. /* disable interrupts */
  1321. writel(UCR1_UARTEN, port->membase + UCR1);
  1322. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  1323. port->membase + UCR2);
  1324. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  1325. port->membase + UCR3);
  1326. /* poll */
  1327. do {
  1328. status = readl(port->membase + USR2);
  1329. } while (~status & USR2_RDR);
  1330. /* read */
  1331. c = readl(port->membase + URXD0);
  1332. /* restore control registers */
  1333. imx_port_ucrs_restore(port, &old_ucr);
  1334. return c;
  1335. }
  1336. static void imx_poll_put_char(struct uart_port *port, unsigned char c)
  1337. {
  1338. struct imx_port_ucrs old_ucr;
  1339. unsigned int status;
  1340. /* save control registers */
  1341. imx_port_ucrs_save(port, &old_ucr);
  1342. /* disable interrupts */
  1343. writel(UCR1_UARTEN, port->membase + UCR1);
  1344. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  1345. port->membase + UCR2);
  1346. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  1347. port->membase + UCR3);
  1348. /* drain */
  1349. do {
  1350. status = readl(port->membase + USR1);
  1351. } while (~status & USR1_TRDY);
  1352. /* write */
  1353. writel(c, port->membase + URTX0);
  1354. /* flush */
  1355. do {
  1356. status = readl(port->membase + USR2);
  1357. } while (~status & USR2_TXDC);
  1358. /* restore control registers */
  1359. imx_port_ucrs_restore(port, &old_ucr);
  1360. }
  1361. #endif
  1362. static struct uart_ops imx_pops = {
  1363. .tx_empty = imx_tx_empty,
  1364. .set_mctrl = imx_set_mctrl,
  1365. .get_mctrl = imx_get_mctrl,
  1366. .stop_tx = imx_stop_tx,
  1367. .start_tx = imx_start_tx,
  1368. .stop_rx = imx_stop_rx,
  1369. .enable_ms = imx_enable_ms,
  1370. .break_ctl = imx_break_ctl,
  1371. .startup = imx_startup,
  1372. .shutdown = imx_shutdown,
  1373. .set_termios = imx_set_termios,
  1374. .type = imx_type,
  1375. .release_port = imx_release_port,
  1376. .request_port = imx_request_port,
  1377. .config_port = imx_config_port,
  1378. .verify_port = imx_verify_port,
  1379. #if defined(CONFIG_CONSOLE_POLL)
  1380. .poll_get_char = imx_poll_get_char,
  1381. .poll_put_char = imx_poll_put_char,
  1382. #endif
  1383. };
  1384. static struct imx_port *imx_ports[UART_NR];
  1385. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  1386. static void imx_console_putchar(struct uart_port *port, int ch)
  1387. {
  1388. struct imx_port *sport = (struct imx_port *)port;
  1389. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  1390. barrier();
  1391. writel(ch, sport->port.membase + URTX0);
  1392. }
  1393. /*
  1394. * Interrupts are disabled on entering
  1395. */
  1396. static void
  1397. imx_console_write(struct console *co, const char *s, unsigned int count)
  1398. {
  1399. struct imx_port *sport = imx_ports[co->index];
  1400. struct imx_port_ucrs old_ucr;
  1401. unsigned int ucr1;
  1402. unsigned long flags = 0;
  1403. int locked = 1;
  1404. int retval;
  1405. retval = clk_enable(sport->clk_per);
  1406. if (retval)
  1407. return;
  1408. retval = clk_enable(sport->clk_ipg);
  1409. if (retval) {
  1410. clk_disable(sport->clk_per);
  1411. return;
  1412. }
  1413. if (sport->port.sysrq)
  1414. locked = 0;
  1415. else if (oops_in_progress)
  1416. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1417. else
  1418. spin_lock_irqsave(&sport->port.lock, flags);
  1419. /*
  1420. * First, save UCR1/2/3 and then disable interrupts
  1421. */
  1422. imx_port_ucrs_save(&sport->port, &old_ucr);
  1423. ucr1 = old_ucr.ucr1;
  1424. if (is_imx1_uart(sport))
  1425. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1426. ucr1 |= UCR1_UARTEN;
  1427. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1428. writel(ucr1, sport->port.membase + UCR1);
  1429. writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  1430. uart_console_write(&sport->port, s, count, imx_console_putchar);
  1431. /*
  1432. * Finally, wait for transmitter to become empty
  1433. * and restore UCR1/2/3
  1434. */
  1435. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  1436. imx_port_ucrs_restore(&sport->port, &old_ucr);
  1437. if (locked)
  1438. spin_unlock_irqrestore(&sport->port.lock, flags);
  1439. clk_disable(sport->clk_ipg);
  1440. clk_disable(sport->clk_per);
  1441. }
  1442. /*
  1443. * If the port was already initialised (eg, by a boot loader),
  1444. * try to determine the current setup.
  1445. */
  1446. static void __init
  1447. imx_console_get_options(struct imx_port *sport, int *baud,
  1448. int *parity, int *bits)
  1449. {
  1450. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  1451. /* ok, the port was enabled */
  1452. unsigned int ucr2, ubir, ubmr, uartclk;
  1453. unsigned int baud_raw;
  1454. unsigned int ucfr_rfdiv;
  1455. ucr2 = readl(sport->port.membase + UCR2);
  1456. *parity = 'n';
  1457. if (ucr2 & UCR2_PREN) {
  1458. if (ucr2 & UCR2_PROE)
  1459. *parity = 'o';
  1460. else
  1461. *parity = 'e';
  1462. }
  1463. if (ucr2 & UCR2_WS)
  1464. *bits = 8;
  1465. else
  1466. *bits = 7;
  1467. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  1468. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  1469. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  1470. if (ucfr_rfdiv == 6)
  1471. ucfr_rfdiv = 7;
  1472. else
  1473. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1474. uartclk = clk_get_rate(sport->clk_per);
  1475. uartclk /= ucfr_rfdiv;
  1476. { /*
  1477. * The next code provides exact computation of
  1478. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1479. * without need of float support or long long division,
  1480. * which would be required to prevent 32bit arithmetic overflow
  1481. */
  1482. unsigned int mul = ubir + 1;
  1483. unsigned int div = 16 * (ubmr + 1);
  1484. unsigned int rem = uartclk % div;
  1485. baud_raw = (uartclk / div) * mul;
  1486. baud_raw += (rem * mul + div / 2) / div;
  1487. *baud = (baud_raw + 50) / 100 * 100;
  1488. }
  1489. if (*baud != baud_raw)
  1490. pr_info("Console IMX rounded baud rate from %d to %d\n",
  1491. baud_raw, *baud);
  1492. }
  1493. }
  1494. static int __init
  1495. imx_console_setup(struct console *co, char *options)
  1496. {
  1497. struct imx_port *sport;
  1498. int baud = 9600;
  1499. int bits = 8;
  1500. int parity = 'n';
  1501. int flow = 'n';
  1502. int retval;
  1503. /*
  1504. * Check whether an invalid uart number has been specified, and
  1505. * if so, search for the first available port that does have
  1506. * console support.
  1507. */
  1508. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1509. co->index = 0;
  1510. sport = imx_ports[co->index];
  1511. if (sport == NULL)
  1512. return -ENODEV;
  1513. /* For setting the registers, we only need to enable the ipg clock. */
  1514. retval = clk_prepare_enable(sport->clk_ipg);
  1515. if (retval)
  1516. goto error_console;
  1517. if (options)
  1518. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1519. else
  1520. imx_console_get_options(sport, &baud, &parity, &bits);
  1521. imx_setup_ufcr(sport, 0);
  1522. retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1523. clk_disable(sport->clk_ipg);
  1524. if (retval) {
  1525. clk_unprepare(sport->clk_ipg);
  1526. goto error_console;
  1527. }
  1528. retval = clk_prepare(sport->clk_per);
  1529. if (retval)
  1530. clk_disable_unprepare(sport->clk_ipg);
  1531. error_console:
  1532. return retval;
  1533. }
  1534. static struct uart_driver imx_reg;
  1535. static struct console imx_console = {
  1536. .name = DEV_NAME,
  1537. .write = imx_console_write,
  1538. .device = uart_console_device,
  1539. .setup = imx_console_setup,
  1540. .flags = CON_PRINTBUFFER,
  1541. .index = -1,
  1542. .data = &imx_reg,
  1543. };
  1544. #define IMX_CONSOLE &imx_console
  1545. #else
  1546. #define IMX_CONSOLE NULL
  1547. #endif
  1548. static struct uart_driver imx_reg = {
  1549. .owner = THIS_MODULE,
  1550. .driver_name = DRIVER_NAME,
  1551. .dev_name = DEV_NAME,
  1552. .major = SERIAL_IMX_MAJOR,
  1553. .minor = MINOR_START,
  1554. .nr = ARRAY_SIZE(imx_ports),
  1555. .cons = IMX_CONSOLE,
  1556. };
  1557. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1558. {
  1559. struct imx_port *sport = platform_get_drvdata(dev);
  1560. unsigned int val;
  1561. /* enable wakeup from i.MX UART */
  1562. val = readl(sport->port.membase + UCR3);
  1563. val |= UCR3_AWAKEN;
  1564. writel(val, sport->port.membase + UCR3);
  1565. uart_suspend_port(&imx_reg, &sport->port);
  1566. return 0;
  1567. }
  1568. static int serial_imx_resume(struct platform_device *dev)
  1569. {
  1570. struct imx_port *sport = platform_get_drvdata(dev);
  1571. unsigned int val;
  1572. /* disable wakeup from i.MX UART */
  1573. val = readl(sport->port.membase + UCR3);
  1574. val &= ~UCR3_AWAKEN;
  1575. writel(val, sport->port.membase + UCR3);
  1576. uart_resume_port(&imx_reg, &sport->port);
  1577. return 0;
  1578. }
  1579. #ifdef CONFIG_OF
  1580. /*
  1581. * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
  1582. * could successfully get all information from dt or a negative errno.
  1583. */
  1584. static int serial_imx_probe_dt(struct imx_port *sport,
  1585. struct platform_device *pdev)
  1586. {
  1587. struct device_node *np = pdev->dev.of_node;
  1588. const struct of_device_id *of_id =
  1589. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1590. int ret;
  1591. if (!np)
  1592. /* no device tree device */
  1593. return 1;
  1594. ret = of_alias_get_id(np, "serial");
  1595. if (ret < 0) {
  1596. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1597. return ret;
  1598. }
  1599. sport->port.line = ret;
  1600. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1601. sport->have_rtscts = 1;
  1602. if (of_get_property(np, "fsl,irda-mode", NULL))
  1603. sport->use_irda = 1;
  1604. if (of_get_property(np, "fsl,dte-mode", NULL))
  1605. sport->dte_mode = 1;
  1606. sport->devdata = of_id->data;
  1607. if (of_device_is_stdout_path(np))
  1608. add_preferred_console(imx_reg.cons->name, sport->port.line, 0);
  1609. return 0;
  1610. }
  1611. #else
  1612. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1613. struct platform_device *pdev)
  1614. {
  1615. return 1;
  1616. }
  1617. #endif
  1618. static void serial_imx_probe_pdata(struct imx_port *sport,
  1619. struct platform_device *pdev)
  1620. {
  1621. struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1622. sport->port.line = pdev->id;
  1623. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1624. if (!pdata)
  1625. return;
  1626. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1627. sport->have_rtscts = 1;
  1628. if (pdata->flags & IMXUART_IRDA)
  1629. sport->use_irda = 1;
  1630. }
  1631. static int serial_imx_probe(struct platform_device *pdev)
  1632. {
  1633. struct imx_port *sport;
  1634. struct imxuart_platform_data *pdata;
  1635. void __iomem *base;
  1636. int ret = 0;
  1637. struct resource *res;
  1638. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1639. if (!sport)
  1640. return -ENOMEM;
  1641. ret = serial_imx_probe_dt(sport, pdev);
  1642. if (ret > 0)
  1643. serial_imx_probe_pdata(sport, pdev);
  1644. else if (ret < 0)
  1645. return ret;
  1646. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1647. if (!res)
  1648. return -ENODEV;
  1649. base = devm_ioremap(&pdev->dev, res->start, PAGE_SIZE);
  1650. if (!base)
  1651. return -ENOMEM;
  1652. sport->port.dev = &pdev->dev;
  1653. sport->port.mapbase = res->start;
  1654. sport->port.membase = base;
  1655. sport->port.type = PORT_IMX,
  1656. sport->port.iotype = UPIO_MEM;
  1657. sport->port.irq = platform_get_irq(pdev, 0);
  1658. sport->rxirq = platform_get_irq(pdev, 0);
  1659. sport->txirq = platform_get_irq(pdev, 1);
  1660. sport->rtsirq = platform_get_irq(pdev, 2);
  1661. sport->port.fifosize = 32;
  1662. sport->port.ops = &imx_pops;
  1663. sport->port.flags = UPF_BOOT_AUTOCONF;
  1664. init_timer(&sport->timer);
  1665. sport->timer.function = imx_timeout;
  1666. sport->timer.data = (unsigned long)sport;
  1667. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1668. if (IS_ERR(sport->clk_ipg)) {
  1669. ret = PTR_ERR(sport->clk_ipg);
  1670. dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
  1671. return ret;
  1672. }
  1673. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  1674. if (IS_ERR(sport->clk_per)) {
  1675. ret = PTR_ERR(sport->clk_per);
  1676. dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
  1677. return ret;
  1678. }
  1679. sport->port.uartclk = clk_get_rate(sport->clk_per);
  1680. imx_ports[sport->port.line] = sport;
  1681. pdata = dev_get_platdata(&pdev->dev);
  1682. if (pdata && pdata->init) {
  1683. ret = pdata->init(pdev);
  1684. if (ret)
  1685. return ret;
  1686. }
  1687. ret = uart_add_one_port(&imx_reg, &sport->port);
  1688. if (ret)
  1689. goto deinit;
  1690. platform_set_drvdata(pdev, sport);
  1691. return 0;
  1692. deinit:
  1693. if (pdata && pdata->exit)
  1694. pdata->exit(pdev);
  1695. return ret;
  1696. }
  1697. static int serial_imx_remove(struct platform_device *pdev)
  1698. {
  1699. struct imxuart_platform_data *pdata;
  1700. struct imx_port *sport = platform_get_drvdata(pdev);
  1701. pdata = dev_get_platdata(&pdev->dev);
  1702. uart_remove_one_port(&imx_reg, &sport->port);
  1703. if (pdata && pdata->exit)
  1704. pdata->exit(pdev);
  1705. return 0;
  1706. }
  1707. static struct platform_driver serial_imx_driver = {
  1708. .probe = serial_imx_probe,
  1709. .remove = serial_imx_remove,
  1710. .suspend = serial_imx_suspend,
  1711. .resume = serial_imx_resume,
  1712. .id_table = imx_uart_devtype,
  1713. .driver = {
  1714. .name = "imx-uart",
  1715. .owner = THIS_MODULE,
  1716. .of_match_table = imx_uart_dt_ids,
  1717. },
  1718. };
  1719. static int __init imx_serial_init(void)
  1720. {
  1721. int ret;
  1722. pr_info("Serial: IMX driver\n");
  1723. ret = uart_register_driver(&imx_reg);
  1724. if (ret)
  1725. return ret;
  1726. ret = platform_driver_register(&serial_imx_driver);
  1727. if (ret != 0)
  1728. uart_unregister_driver(&imx_reg);
  1729. return ret;
  1730. }
  1731. static void __exit imx_serial_exit(void)
  1732. {
  1733. platform_driver_unregister(&serial_imx_driver);
  1734. uart_unregister_driver(&imx_reg);
  1735. }
  1736. module_init(imx_serial_init);
  1737. module_exit(imx_serial_exit);
  1738. MODULE_AUTHOR("Sascha Hauer");
  1739. MODULE_DESCRIPTION("IMX generic serial port driver");
  1740. MODULE_LICENSE("GPL");
  1741. MODULE_ALIAS("platform:imx-uart");