imx.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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/pinctrl/consumer.h>
  49. #include <asm/io.h>
  50. #include <asm/irq.h>
  51. #include <mach/imx-uart.h>
  52. /* Register definitions */
  53. #define URXD0 0x0 /* Receiver Register */
  54. #define URTX0 0x40 /* Transmitter Register */
  55. #define UCR1 0x80 /* Control Register 1 */
  56. #define UCR2 0x84 /* Control Register 2 */
  57. #define UCR3 0x88 /* Control Register 3 */
  58. #define UCR4 0x8c /* Control Register 4 */
  59. #define UFCR 0x90 /* FIFO Control Register */
  60. #define USR1 0x94 /* Status Register 1 */
  61. #define USR2 0x98 /* Status Register 2 */
  62. #define UESC 0x9c /* Escape Character Register */
  63. #define UTIM 0xa0 /* Escape Timer Register */
  64. #define UBIR 0xa4 /* BRM Incremental Register */
  65. #define UBMR 0xa8 /* BRM Modulator Register */
  66. #define UBRC 0xac /* Baud Rate Count Register */
  67. #define IMX21_ONEMS 0xb0 /* One Millisecond register */
  68. #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
  69. #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
  70. /* UART Control Register Bit Fields.*/
  71. #define URXD_CHARRDY (1<<15)
  72. #define URXD_ERR (1<<14)
  73. #define URXD_OVRRUN (1<<13)
  74. #define URXD_FRMERR (1<<12)
  75. #define URXD_BRK (1<<11)
  76. #define URXD_PRERR (1<<10)
  77. #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
  78. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  79. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  80. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  81. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  82. #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
  83. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  84. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  85. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  86. #define UCR1_SNDBRK (1<<4) /* Send break */
  87. #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
  88. #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
  89. #define UCR1_DOZE (1<<1) /* Doze */
  90. #define UCR1_UARTEN (1<<0) /* UART enabled */
  91. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  92. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  93. #define UCR2_CTSC (1<<13) /* CTS pin control */
  94. #define UCR2_CTS (1<<12) /* Clear to send */
  95. #define UCR2_ESCEN (1<<11) /* Escape enable */
  96. #define UCR2_PREN (1<<8) /* Parity enable */
  97. #define UCR2_PROE (1<<7) /* Parity odd/even */
  98. #define UCR2_STPB (1<<6) /* Stop */
  99. #define UCR2_WS (1<<5) /* Word size */
  100. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  101. #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
  102. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  103. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  104. #define UCR2_SRST (1<<0) /* SW reset */
  105. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  106. #define UCR3_PARERREN (1<<12) /* Parity enable */
  107. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  108. #define UCR3_DSR (1<<10) /* Data set ready */
  109. #define UCR3_DCD (1<<9) /* Data carrier detect */
  110. #define UCR3_RI (1<<8) /* Ring indicator */
  111. #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
  112. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  113. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  114. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  115. #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
  116. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  117. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  118. #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
  119. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  120. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  121. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  122. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  123. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  124. #define UCR4_IRSC (1<<5) /* IR special case */
  125. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  126. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  127. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  128. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  129. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  130. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  131. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  132. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  133. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  134. #define USR1_RTSS (1<<14) /* RTS pin status */
  135. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  136. #define USR1_RTSD (1<<12) /* RTS delta */
  137. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  138. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  139. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  140. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  141. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  142. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  143. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  144. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  145. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  146. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  147. #define USR2_IDLE (1<<12) /* Idle condition */
  148. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  149. #define USR2_WAKE (1<<7) /* Wake */
  150. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  151. #define USR2_TXDC (1<<3) /* Transmitter complete */
  152. #define USR2_BRCD (1<<2) /* Break condition */
  153. #define USR2_ORE (1<<1) /* Overrun error */
  154. #define USR2_RDR (1<<0) /* Recv data ready */
  155. #define UTS_FRCPERR (1<<13) /* Force parity error */
  156. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  157. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  158. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  159. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  160. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  161. #define UTS_SOFTRST (1<<0) /* Software reset */
  162. /* We've been assigned a range on the "Low-density serial ports" major */
  163. #define SERIAL_IMX_MAJOR 207
  164. #define MINOR_START 16
  165. #define DEV_NAME "ttymxc"
  166. #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
  167. /*
  168. * This determines how often we check the modem status signals
  169. * for any change. They generally aren't connected to an IRQ
  170. * so we have to poll them. We also check immediately before
  171. * filling the TX fifo incase CTS has been dropped.
  172. */
  173. #define MCTRL_TIMEOUT (250*HZ/1000)
  174. #define DRIVER_NAME "IMX-uart"
  175. #define UART_NR 8
  176. /* i.mx21 type uart runs on all i.mx except i.mx1 */
  177. enum imx_uart_type {
  178. IMX1_UART,
  179. IMX21_UART,
  180. };
  181. /* device type dependent stuff */
  182. struct imx_uart_data {
  183. unsigned uts_reg;
  184. enum imx_uart_type devtype;
  185. };
  186. struct imx_port {
  187. struct uart_port port;
  188. struct timer_list timer;
  189. unsigned int old_status;
  190. int txirq,rxirq,rtsirq;
  191. unsigned int have_rtscts:1;
  192. unsigned int use_irda:1;
  193. unsigned int irda_inv_rx:1;
  194. unsigned int irda_inv_tx:1;
  195. unsigned short trcv_delay; /* transceiver delay */
  196. struct clk *clk_ipg;
  197. struct clk *clk_per;
  198. struct imx_uart_data *devdata;
  199. };
  200. struct imx_port_ucrs {
  201. unsigned int ucr1;
  202. unsigned int ucr2;
  203. unsigned int ucr3;
  204. };
  205. #ifdef CONFIG_IRDA
  206. #define USE_IRDA(sport) ((sport)->use_irda)
  207. #else
  208. #define USE_IRDA(sport) (0)
  209. #endif
  210. static struct imx_uart_data imx_uart_devdata[] = {
  211. [IMX1_UART] = {
  212. .uts_reg = IMX1_UTS,
  213. .devtype = IMX1_UART,
  214. },
  215. [IMX21_UART] = {
  216. .uts_reg = IMX21_UTS,
  217. .devtype = IMX21_UART,
  218. },
  219. };
  220. static struct platform_device_id imx_uart_devtype[] = {
  221. {
  222. .name = "imx1-uart",
  223. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX1_UART],
  224. }, {
  225. .name = "imx21-uart",
  226. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
  227. }, {
  228. /* sentinel */
  229. }
  230. };
  231. MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
  232. static struct of_device_id imx_uart_dt_ids[] = {
  233. { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
  234. { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
  235. { /* sentinel */ }
  236. };
  237. MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
  238. static inline unsigned uts_reg(struct imx_port *sport)
  239. {
  240. return sport->devdata->uts_reg;
  241. }
  242. static inline int is_imx1_uart(struct imx_port *sport)
  243. {
  244. return sport->devdata->devtype == IMX1_UART;
  245. }
  246. static inline int is_imx21_uart(struct imx_port *sport)
  247. {
  248. return sport->devdata->devtype == IMX21_UART;
  249. }
  250. /*
  251. * Save and restore functions for UCR1, UCR2 and UCR3 registers
  252. */
  253. static void imx_port_ucrs_save(struct uart_port *port,
  254. struct imx_port_ucrs *ucr)
  255. {
  256. /* save control registers */
  257. ucr->ucr1 = readl(port->membase + UCR1);
  258. ucr->ucr2 = readl(port->membase + UCR2);
  259. ucr->ucr3 = readl(port->membase + UCR3);
  260. }
  261. static void imx_port_ucrs_restore(struct uart_port *port,
  262. struct imx_port_ucrs *ucr)
  263. {
  264. /* restore control registers */
  265. writel(ucr->ucr1, port->membase + UCR1);
  266. writel(ucr->ucr2, port->membase + UCR2);
  267. writel(ucr->ucr3, port->membase + UCR3);
  268. }
  269. /*
  270. * Handle any change of modem status signal since we were last called.
  271. */
  272. static void imx_mctrl_check(struct imx_port *sport)
  273. {
  274. unsigned int status, changed;
  275. status = sport->port.ops->get_mctrl(&sport->port);
  276. changed = status ^ sport->old_status;
  277. if (changed == 0)
  278. return;
  279. sport->old_status = status;
  280. if (changed & TIOCM_RI)
  281. sport->port.icount.rng++;
  282. if (changed & TIOCM_DSR)
  283. sport->port.icount.dsr++;
  284. if (changed & TIOCM_CAR)
  285. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  286. if (changed & TIOCM_CTS)
  287. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  288. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  289. }
  290. /*
  291. * This is our per-port timeout handler, for checking the
  292. * modem status signals.
  293. */
  294. static void imx_timeout(unsigned long data)
  295. {
  296. struct imx_port *sport = (struct imx_port *)data;
  297. unsigned long flags;
  298. if (sport->port.state) {
  299. spin_lock_irqsave(&sport->port.lock, flags);
  300. imx_mctrl_check(sport);
  301. spin_unlock_irqrestore(&sport->port.lock, flags);
  302. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  303. }
  304. }
  305. /*
  306. * interrupts disabled on entry
  307. */
  308. static void imx_stop_tx(struct uart_port *port)
  309. {
  310. struct imx_port *sport = (struct imx_port *)port;
  311. unsigned long temp;
  312. if (USE_IRDA(sport)) {
  313. /* half duplex - wait for end of transmission */
  314. int n = 256;
  315. while ((--n > 0) &&
  316. !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
  317. udelay(5);
  318. barrier();
  319. }
  320. /*
  321. * irda transceiver - wait a bit more to avoid
  322. * cutoff, hardware dependent
  323. */
  324. udelay(sport->trcv_delay);
  325. /*
  326. * half duplex - reactivate receive mode,
  327. * flush receive pipe echo crap
  328. */
  329. if (readl(sport->port.membase + USR2) & USR2_TXDC) {
  330. temp = readl(sport->port.membase + UCR1);
  331. temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
  332. writel(temp, sport->port.membase + UCR1);
  333. temp = readl(sport->port.membase + UCR4);
  334. temp &= ~(UCR4_TCEN);
  335. writel(temp, sport->port.membase + UCR4);
  336. while (readl(sport->port.membase + URXD0) &
  337. URXD_CHARRDY)
  338. barrier();
  339. temp = readl(sport->port.membase + UCR1);
  340. temp |= UCR1_RRDYEN;
  341. writel(temp, sport->port.membase + UCR1);
  342. temp = readl(sport->port.membase + UCR4);
  343. temp |= UCR4_DREN;
  344. writel(temp, sport->port.membase + UCR4);
  345. }
  346. return;
  347. }
  348. temp = readl(sport->port.membase + UCR1);
  349. writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
  350. }
  351. /*
  352. * interrupts disabled on entry
  353. */
  354. static void imx_stop_rx(struct uart_port *port)
  355. {
  356. struct imx_port *sport = (struct imx_port *)port;
  357. unsigned long temp;
  358. temp = readl(sport->port.membase + UCR2);
  359. writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
  360. }
  361. /*
  362. * Set the modem control timer to fire immediately.
  363. */
  364. static void imx_enable_ms(struct uart_port *port)
  365. {
  366. struct imx_port *sport = (struct imx_port *)port;
  367. mod_timer(&sport->timer, jiffies);
  368. }
  369. static inline void imx_transmit_buffer(struct imx_port *sport)
  370. {
  371. struct circ_buf *xmit = &sport->port.state->xmit;
  372. while (!uart_circ_empty(xmit) &&
  373. !(readl(sport->port.membase + uts_reg(sport))
  374. & UTS_TXFULL)) {
  375. /* send xmit->buf[xmit->tail]
  376. * out the port here */
  377. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  378. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  379. sport->port.icount.tx++;
  380. }
  381. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  382. uart_write_wakeup(&sport->port);
  383. if (uart_circ_empty(xmit))
  384. imx_stop_tx(&sport->port);
  385. }
  386. /*
  387. * interrupts disabled on entry
  388. */
  389. static void imx_start_tx(struct uart_port *port)
  390. {
  391. struct imx_port *sport = (struct imx_port *)port;
  392. unsigned long temp;
  393. if (USE_IRDA(sport)) {
  394. /* half duplex in IrDA mode; have to disable receive mode */
  395. temp = readl(sport->port.membase + UCR4);
  396. temp &= ~(UCR4_DREN);
  397. writel(temp, sport->port.membase + UCR4);
  398. temp = readl(sport->port.membase + UCR1);
  399. temp &= ~(UCR1_RRDYEN);
  400. writel(temp, sport->port.membase + UCR1);
  401. }
  402. temp = readl(sport->port.membase + UCR1);
  403. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  404. if (USE_IRDA(sport)) {
  405. temp = readl(sport->port.membase + UCR1);
  406. temp |= UCR1_TRDYEN;
  407. writel(temp, sport->port.membase + UCR1);
  408. temp = readl(sport->port.membase + UCR4);
  409. temp |= UCR4_TCEN;
  410. writel(temp, sport->port.membase + UCR4);
  411. }
  412. if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY)
  413. imx_transmit_buffer(sport);
  414. }
  415. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  416. {
  417. struct imx_port *sport = dev_id;
  418. unsigned int val;
  419. unsigned long flags;
  420. spin_lock_irqsave(&sport->port.lock, flags);
  421. writel(USR1_RTSD, sport->port.membase + USR1);
  422. val = readl(sport->port.membase + USR1) & USR1_RTSS;
  423. uart_handle_cts_change(&sport->port, !!val);
  424. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  425. spin_unlock_irqrestore(&sport->port.lock, flags);
  426. return IRQ_HANDLED;
  427. }
  428. static irqreturn_t imx_txint(int irq, void *dev_id)
  429. {
  430. struct imx_port *sport = dev_id;
  431. struct circ_buf *xmit = &sport->port.state->xmit;
  432. unsigned long flags;
  433. spin_lock_irqsave(&sport->port.lock,flags);
  434. if (sport->port.x_char)
  435. {
  436. /* Send next char */
  437. writel(sport->port.x_char, sport->port.membase + URTX0);
  438. goto out;
  439. }
  440. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  441. imx_stop_tx(&sport->port);
  442. goto out;
  443. }
  444. imx_transmit_buffer(sport);
  445. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  446. uart_write_wakeup(&sport->port);
  447. out:
  448. spin_unlock_irqrestore(&sport->port.lock,flags);
  449. return IRQ_HANDLED;
  450. }
  451. static irqreturn_t imx_rxint(int irq, void *dev_id)
  452. {
  453. struct imx_port *sport = dev_id;
  454. unsigned int rx,flg,ignored = 0;
  455. struct tty_struct *tty = sport->port.state->port.tty;
  456. unsigned long flags, temp;
  457. spin_lock_irqsave(&sport->port.lock,flags);
  458. while (readl(sport->port.membase + USR2) & USR2_RDR) {
  459. flg = TTY_NORMAL;
  460. sport->port.icount.rx++;
  461. rx = readl(sport->port.membase + URXD0);
  462. temp = readl(sport->port.membase + USR2);
  463. if (temp & USR2_BRCD) {
  464. writel(USR2_BRCD, sport->port.membase + USR2);
  465. if (uart_handle_break(&sport->port))
  466. continue;
  467. }
  468. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  469. continue;
  470. if (unlikely(rx & URXD_ERR)) {
  471. if (rx & URXD_BRK)
  472. sport->port.icount.brk++;
  473. else if (rx & URXD_PRERR)
  474. sport->port.icount.parity++;
  475. else if (rx & URXD_FRMERR)
  476. sport->port.icount.frame++;
  477. if (rx & URXD_OVRRUN)
  478. sport->port.icount.overrun++;
  479. if (rx & sport->port.ignore_status_mask) {
  480. if (++ignored > 100)
  481. goto out;
  482. continue;
  483. }
  484. rx &= sport->port.read_status_mask;
  485. if (rx & URXD_BRK)
  486. flg = TTY_BREAK;
  487. else if (rx & URXD_PRERR)
  488. flg = TTY_PARITY;
  489. else if (rx & URXD_FRMERR)
  490. flg = TTY_FRAME;
  491. if (rx & URXD_OVRRUN)
  492. flg = TTY_OVERRUN;
  493. #ifdef SUPPORT_SYSRQ
  494. sport->port.sysrq = 0;
  495. #endif
  496. }
  497. tty_insert_flip_char(tty, rx, flg);
  498. }
  499. out:
  500. spin_unlock_irqrestore(&sport->port.lock,flags);
  501. tty_flip_buffer_push(tty);
  502. return IRQ_HANDLED;
  503. }
  504. static irqreturn_t imx_int(int irq, void *dev_id)
  505. {
  506. struct imx_port *sport = dev_id;
  507. unsigned int sts;
  508. sts = readl(sport->port.membase + USR1);
  509. if (sts & USR1_RRDY)
  510. imx_rxint(irq, dev_id);
  511. if (sts & USR1_TRDY &&
  512. readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
  513. imx_txint(irq, dev_id);
  514. if (sts & USR1_RTSD)
  515. imx_rtsint(irq, dev_id);
  516. if (sts & USR1_AWAKE)
  517. writel(USR1_AWAKE, sport->port.membase + USR1);
  518. return IRQ_HANDLED;
  519. }
  520. /*
  521. * Return TIOCSER_TEMT when transmitter is not busy.
  522. */
  523. static unsigned int imx_tx_empty(struct uart_port *port)
  524. {
  525. struct imx_port *sport = (struct imx_port *)port;
  526. return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  527. }
  528. /*
  529. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  530. */
  531. static unsigned int imx_get_mctrl(struct uart_port *port)
  532. {
  533. struct imx_port *sport = (struct imx_port *)port;
  534. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  535. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  536. tmp |= TIOCM_CTS;
  537. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  538. tmp |= TIOCM_RTS;
  539. return tmp;
  540. }
  541. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  542. {
  543. struct imx_port *sport = (struct imx_port *)port;
  544. unsigned long temp;
  545. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  546. if (mctrl & TIOCM_RTS)
  547. temp |= UCR2_CTS;
  548. writel(temp, sport->port.membase + UCR2);
  549. }
  550. /*
  551. * Interrupts always disabled.
  552. */
  553. static void imx_break_ctl(struct uart_port *port, int break_state)
  554. {
  555. struct imx_port *sport = (struct imx_port *)port;
  556. unsigned long flags, temp;
  557. spin_lock_irqsave(&sport->port.lock, flags);
  558. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  559. if ( break_state != 0 )
  560. temp |= UCR1_SNDBRK;
  561. writel(temp, sport->port.membase + UCR1);
  562. spin_unlock_irqrestore(&sport->port.lock, flags);
  563. }
  564. #define TXTL 2 /* reset default */
  565. #define RXTL 1 /* reset default */
  566. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  567. {
  568. unsigned int val;
  569. unsigned int ufcr_rfdiv;
  570. /* set receiver / transmitter trigger level.
  571. * RFDIV is set such way to satisfy requested uartclk value
  572. */
  573. val = TXTL << 10 | RXTL;
  574. ufcr_rfdiv = (clk_get_rate(sport->clk_per) + sport->port.uartclk / 2)
  575. / sport->port.uartclk;
  576. if(!ufcr_rfdiv)
  577. ufcr_rfdiv = 1;
  578. val |= UFCR_RFDIV_REG(ufcr_rfdiv);
  579. writel(val, sport->port.membase + UFCR);
  580. return 0;
  581. }
  582. /* half the RX buffer size */
  583. #define CTSTL 16
  584. static int imx_startup(struct uart_port *port)
  585. {
  586. struct imx_port *sport = (struct imx_port *)port;
  587. int retval;
  588. unsigned long flags, temp;
  589. imx_setup_ufcr(sport, 0);
  590. /* disable the DREN bit (Data Ready interrupt enable) before
  591. * requesting IRQs
  592. */
  593. temp = readl(sport->port.membase + UCR4);
  594. if (USE_IRDA(sport))
  595. temp |= UCR4_IRSC;
  596. /* set the trigger level for CTS */
  597. temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
  598. temp |= CTSTL<< UCR4_CTSTL_SHF;
  599. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  600. if (USE_IRDA(sport)) {
  601. /* reset fifo's and state machines */
  602. int i = 100;
  603. temp = readl(sport->port.membase + UCR2);
  604. temp &= ~UCR2_SRST;
  605. writel(temp, sport->port.membase + UCR2);
  606. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  607. (--i > 0)) {
  608. udelay(1);
  609. }
  610. }
  611. /*
  612. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  613. * chips only have one interrupt.
  614. */
  615. if (sport->txirq > 0) {
  616. retval = request_irq(sport->rxirq, imx_rxint, 0,
  617. DRIVER_NAME, sport);
  618. if (retval)
  619. goto error_out1;
  620. retval = request_irq(sport->txirq, imx_txint, 0,
  621. DRIVER_NAME, sport);
  622. if (retval)
  623. goto error_out2;
  624. /* do not use RTS IRQ on IrDA */
  625. if (!USE_IRDA(sport)) {
  626. retval = request_irq(sport->rtsirq, imx_rtsint,
  627. (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
  628. IRQF_TRIGGER_FALLING |
  629. IRQF_TRIGGER_RISING,
  630. DRIVER_NAME, sport);
  631. if (retval)
  632. goto error_out3;
  633. }
  634. } else {
  635. retval = request_irq(sport->port.irq, imx_int, 0,
  636. DRIVER_NAME, sport);
  637. if (retval) {
  638. free_irq(sport->port.irq, sport);
  639. goto error_out1;
  640. }
  641. }
  642. /*
  643. * Finally, clear and enable interrupts
  644. */
  645. writel(USR1_RTSD, sport->port.membase + USR1);
  646. temp = readl(sport->port.membase + UCR1);
  647. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  648. if (USE_IRDA(sport)) {
  649. temp |= UCR1_IREN;
  650. temp &= ~(UCR1_RTSDEN);
  651. }
  652. writel(temp, sport->port.membase + UCR1);
  653. temp = readl(sport->port.membase + UCR2);
  654. temp |= (UCR2_RXEN | UCR2_TXEN);
  655. writel(temp, sport->port.membase + UCR2);
  656. if (USE_IRDA(sport)) {
  657. /* clear RX-FIFO */
  658. int i = 64;
  659. while ((--i > 0) &&
  660. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  661. barrier();
  662. }
  663. }
  664. if (is_imx21_uart(sport)) {
  665. temp = readl(sport->port.membase + UCR3);
  666. temp |= IMX21_UCR3_RXDMUXSEL;
  667. writel(temp, sport->port.membase + UCR3);
  668. }
  669. if (USE_IRDA(sport)) {
  670. temp = readl(sport->port.membase + UCR4);
  671. if (sport->irda_inv_rx)
  672. temp |= UCR4_INVR;
  673. else
  674. temp &= ~(UCR4_INVR);
  675. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  676. temp = readl(sport->port.membase + UCR3);
  677. if (sport->irda_inv_tx)
  678. temp |= UCR3_INVT;
  679. else
  680. temp &= ~(UCR3_INVT);
  681. writel(temp, sport->port.membase + UCR3);
  682. }
  683. /*
  684. * Enable modem status interrupts
  685. */
  686. spin_lock_irqsave(&sport->port.lock,flags);
  687. imx_enable_ms(&sport->port);
  688. spin_unlock_irqrestore(&sport->port.lock,flags);
  689. if (USE_IRDA(sport)) {
  690. struct imxuart_platform_data *pdata;
  691. pdata = sport->port.dev->platform_data;
  692. sport->irda_inv_rx = pdata->irda_inv_rx;
  693. sport->irda_inv_tx = pdata->irda_inv_tx;
  694. sport->trcv_delay = pdata->transceiver_delay;
  695. if (pdata->irda_enable)
  696. pdata->irda_enable(1);
  697. }
  698. return 0;
  699. error_out3:
  700. if (sport->txirq)
  701. free_irq(sport->txirq, sport);
  702. error_out2:
  703. if (sport->rxirq)
  704. free_irq(sport->rxirq, sport);
  705. error_out1:
  706. return retval;
  707. }
  708. static void imx_shutdown(struct uart_port *port)
  709. {
  710. struct imx_port *sport = (struct imx_port *)port;
  711. unsigned long temp;
  712. temp = readl(sport->port.membase + UCR2);
  713. temp &= ~(UCR2_TXEN);
  714. writel(temp, sport->port.membase + UCR2);
  715. if (USE_IRDA(sport)) {
  716. struct imxuart_platform_data *pdata;
  717. pdata = sport->port.dev->platform_data;
  718. if (pdata->irda_enable)
  719. pdata->irda_enable(0);
  720. }
  721. /*
  722. * Stop our timer.
  723. */
  724. del_timer_sync(&sport->timer);
  725. /*
  726. * Free the interrupts
  727. */
  728. if (sport->txirq > 0) {
  729. if (!USE_IRDA(sport))
  730. free_irq(sport->rtsirq, sport);
  731. free_irq(sport->txirq, sport);
  732. free_irq(sport->rxirq, sport);
  733. } else
  734. free_irq(sport->port.irq, sport);
  735. /*
  736. * Disable all interrupts, port and break condition.
  737. */
  738. temp = readl(sport->port.membase + UCR1);
  739. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  740. if (USE_IRDA(sport))
  741. temp &= ~(UCR1_IREN);
  742. writel(temp, sport->port.membase + UCR1);
  743. }
  744. static void
  745. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  746. struct ktermios *old)
  747. {
  748. struct imx_port *sport = (struct imx_port *)port;
  749. unsigned long flags;
  750. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  751. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  752. unsigned int div, ufcr;
  753. unsigned long num, denom;
  754. uint64_t tdiv64;
  755. /*
  756. * If we don't support modem control lines, don't allow
  757. * these to be set.
  758. */
  759. if (0) {
  760. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  761. termios->c_cflag |= CLOCAL;
  762. }
  763. /*
  764. * We only support CS7 and CS8.
  765. */
  766. while ((termios->c_cflag & CSIZE) != CS7 &&
  767. (termios->c_cflag & CSIZE) != CS8) {
  768. termios->c_cflag &= ~CSIZE;
  769. termios->c_cflag |= old_csize;
  770. old_csize = CS8;
  771. }
  772. if ((termios->c_cflag & CSIZE) == CS8)
  773. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  774. else
  775. ucr2 = UCR2_SRST | UCR2_IRTS;
  776. if (termios->c_cflag & CRTSCTS) {
  777. if( sport->have_rtscts ) {
  778. ucr2 &= ~UCR2_IRTS;
  779. ucr2 |= UCR2_CTSC;
  780. } else {
  781. termios->c_cflag &= ~CRTSCTS;
  782. }
  783. }
  784. if (termios->c_cflag & CSTOPB)
  785. ucr2 |= UCR2_STPB;
  786. if (termios->c_cflag & PARENB) {
  787. ucr2 |= UCR2_PREN;
  788. if (termios->c_cflag & PARODD)
  789. ucr2 |= UCR2_PROE;
  790. }
  791. del_timer_sync(&sport->timer);
  792. /*
  793. * Ask the core to calculate the divisor for us.
  794. */
  795. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  796. quot = uart_get_divisor(port, baud);
  797. spin_lock_irqsave(&sport->port.lock, flags);
  798. sport->port.read_status_mask = 0;
  799. if (termios->c_iflag & INPCK)
  800. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  801. if (termios->c_iflag & (BRKINT | PARMRK))
  802. sport->port.read_status_mask |= URXD_BRK;
  803. /*
  804. * Characters to ignore
  805. */
  806. sport->port.ignore_status_mask = 0;
  807. if (termios->c_iflag & IGNPAR)
  808. sport->port.ignore_status_mask |= URXD_PRERR;
  809. if (termios->c_iflag & IGNBRK) {
  810. sport->port.ignore_status_mask |= URXD_BRK;
  811. /*
  812. * If we're ignoring parity and break indicators,
  813. * ignore overruns too (for real raw support).
  814. */
  815. if (termios->c_iflag & IGNPAR)
  816. sport->port.ignore_status_mask |= URXD_OVRRUN;
  817. }
  818. /*
  819. * Update the per-port timeout.
  820. */
  821. uart_update_timeout(port, termios->c_cflag, baud);
  822. /*
  823. * disable interrupts and drain transmitter
  824. */
  825. old_ucr1 = readl(sport->port.membase + UCR1);
  826. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  827. sport->port.membase + UCR1);
  828. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  829. barrier();
  830. /* then, disable everything */
  831. old_txrxen = readl(sport->port.membase + UCR2);
  832. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  833. sport->port.membase + UCR2);
  834. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  835. if (USE_IRDA(sport)) {
  836. /*
  837. * use maximum available submodule frequency to
  838. * avoid missing short pulses due to low sampling rate
  839. */
  840. div = 1;
  841. } else {
  842. div = sport->port.uartclk / (baud * 16);
  843. if (div > 7)
  844. div = 7;
  845. if (!div)
  846. div = 1;
  847. }
  848. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  849. 1 << 16, 1 << 16, &num, &denom);
  850. tdiv64 = sport->port.uartclk;
  851. tdiv64 *= num;
  852. do_div(tdiv64, denom * 16 * div);
  853. tty_termios_encode_baud_rate(termios,
  854. (speed_t)tdiv64, (speed_t)tdiv64);
  855. num -= 1;
  856. denom -= 1;
  857. ufcr = readl(sport->port.membase + UFCR);
  858. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  859. writel(ufcr, sport->port.membase + UFCR);
  860. writel(num, sport->port.membase + UBIR);
  861. writel(denom, sport->port.membase + UBMR);
  862. if (is_imx21_uart(sport))
  863. writel(sport->port.uartclk / div / 1000,
  864. sport->port.membase + IMX21_ONEMS);
  865. writel(old_ucr1, sport->port.membase + UCR1);
  866. /* set the parity, stop bits and data size */
  867. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  868. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  869. imx_enable_ms(&sport->port);
  870. spin_unlock_irqrestore(&sport->port.lock, flags);
  871. }
  872. static const char *imx_type(struct uart_port *port)
  873. {
  874. struct imx_port *sport = (struct imx_port *)port;
  875. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  876. }
  877. /*
  878. * Release the memory region(s) being used by 'port'.
  879. */
  880. static void imx_release_port(struct uart_port *port)
  881. {
  882. struct platform_device *pdev = to_platform_device(port->dev);
  883. struct resource *mmres;
  884. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  885. release_mem_region(mmres->start, resource_size(mmres));
  886. }
  887. /*
  888. * Request the memory region(s) being used by 'port'.
  889. */
  890. static int imx_request_port(struct uart_port *port)
  891. {
  892. struct platform_device *pdev = to_platform_device(port->dev);
  893. struct resource *mmres;
  894. void *ret;
  895. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  896. if (!mmres)
  897. return -ENODEV;
  898. ret = request_mem_region(mmres->start, resource_size(mmres), "imx-uart");
  899. return ret ? 0 : -EBUSY;
  900. }
  901. /*
  902. * Configure/autoconfigure the port.
  903. */
  904. static void imx_config_port(struct uart_port *port, int flags)
  905. {
  906. struct imx_port *sport = (struct imx_port *)port;
  907. if (flags & UART_CONFIG_TYPE &&
  908. imx_request_port(&sport->port) == 0)
  909. sport->port.type = PORT_IMX;
  910. }
  911. /*
  912. * Verify the new serial_struct (for TIOCSSERIAL).
  913. * The only change we allow are to the flags and type, and
  914. * even then only between PORT_IMX and PORT_UNKNOWN
  915. */
  916. static int
  917. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  918. {
  919. struct imx_port *sport = (struct imx_port *)port;
  920. int ret = 0;
  921. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  922. ret = -EINVAL;
  923. if (sport->port.irq != ser->irq)
  924. ret = -EINVAL;
  925. if (ser->io_type != UPIO_MEM)
  926. ret = -EINVAL;
  927. if (sport->port.uartclk / 16 != ser->baud_base)
  928. ret = -EINVAL;
  929. if ((void *)sport->port.mapbase != ser->iomem_base)
  930. ret = -EINVAL;
  931. if (sport->port.iobase != ser->port)
  932. ret = -EINVAL;
  933. if (ser->hub6 != 0)
  934. ret = -EINVAL;
  935. return ret;
  936. }
  937. #if defined(CONFIG_CONSOLE_POLL)
  938. static int imx_poll_get_char(struct uart_port *port)
  939. {
  940. struct imx_port_ucrs old_ucr;
  941. unsigned int status;
  942. unsigned char c;
  943. /* save control registers */
  944. imx_port_ucrs_save(port, &old_ucr);
  945. /* disable interrupts */
  946. writel(UCR1_UARTEN, port->membase + UCR1);
  947. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  948. port->membase + UCR2);
  949. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  950. port->membase + UCR3);
  951. /* poll */
  952. do {
  953. status = readl(port->membase + USR2);
  954. } while (~status & USR2_RDR);
  955. /* read */
  956. c = readl(port->membase + URXD0);
  957. /* restore control registers */
  958. imx_port_ucrs_restore(port, &old_ucr);
  959. return c;
  960. }
  961. static void imx_poll_put_char(struct uart_port *port, unsigned char c)
  962. {
  963. struct imx_port_ucrs old_ucr;
  964. unsigned int status;
  965. /* save control registers */
  966. imx_port_ucrs_save(port, &old_ucr);
  967. /* disable interrupts */
  968. writel(UCR1_UARTEN, port->membase + UCR1);
  969. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  970. port->membase + UCR2);
  971. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  972. port->membase + UCR3);
  973. /* drain */
  974. do {
  975. status = readl(port->membase + USR1);
  976. } while (~status & USR1_TRDY);
  977. /* write */
  978. writel(c, port->membase + URTX0);
  979. /* flush */
  980. do {
  981. status = readl(port->membase + USR2);
  982. } while (~status & USR2_TXDC);
  983. /* restore control registers */
  984. imx_port_ucrs_restore(port, &old_ucr);
  985. }
  986. #endif
  987. static struct uart_ops imx_pops = {
  988. .tx_empty = imx_tx_empty,
  989. .set_mctrl = imx_set_mctrl,
  990. .get_mctrl = imx_get_mctrl,
  991. .stop_tx = imx_stop_tx,
  992. .start_tx = imx_start_tx,
  993. .stop_rx = imx_stop_rx,
  994. .enable_ms = imx_enable_ms,
  995. .break_ctl = imx_break_ctl,
  996. .startup = imx_startup,
  997. .shutdown = imx_shutdown,
  998. .set_termios = imx_set_termios,
  999. .type = imx_type,
  1000. .release_port = imx_release_port,
  1001. .request_port = imx_request_port,
  1002. .config_port = imx_config_port,
  1003. .verify_port = imx_verify_port,
  1004. #if defined(CONFIG_CONSOLE_POLL)
  1005. .poll_get_char = imx_poll_get_char,
  1006. .poll_put_char = imx_poll_put_char,
  1007. #endif
  1008. };
  1009. static struct imx_port *imx_ports[UART_NR];
  1010. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  1011. static void imx_console_putchar(struct uart_port *port, int ch)
  1012. {
  1013. struct imx_port *sport = (struct imx_port *)port;
  1014. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  1015. barrier();
  1016. writel(ch, sport->port.membase + URTX0);
  1017. }
  1018. /*
  1019. * Interrupts are disabled on entering
  1020. */
  1021. static void
  1022. imx_console_write(struct console *co, const char *s, unsigned int count)
  1023. {
  1024. struct imx_port *sport = imx_ports[co->index];
  1025. struct imx_port_ucrs old_ucr;
  1026. unsigned int ucr1;
  1027. /*
  1028. * First, save UCR1/2/3 and then disable interrupts
  1029. */
  1030. imx_port_ucrs_save(&sport->port, &old_ucr);
  1031. ucr1 = old_ucr.ucr1;
  1032. if (is_imx1_uart(sport))
  1033. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1034. ucr1 |= UCR1_UARTEN;
  1035. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1036. writel(ucr1, sport->port.membase + UCR1);
  1037. writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  1038. uart_console_write(&sport->port, s, count, imx_console_putchar);
  1039. /*
  1040. * Finally, wait for transmitter to become empty
  1041. * and restore UCR1/2/3
  1042. */
  1043. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  1044. imx_port_ucrs_restore(&sport->port, &old_ucr);
  1045. }
  1046. /*
  1047. * If the port was already initialised (eg, by a boot loader),
  1048. * try to determine the current setup.
  1049. */
  1050. static void __init
  1051. imx_console_get_options(struct imx_port *sport, int *baud,
  1052. int *parity, int *bits)
  1053. {
  1054. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  1055. /* ok, the port was enabled */
  1056. unsigned int ucr2, ubir,ubmr, uartclk;
  1057. unsigned int baud_raw;
  1058. unsigned int ucfr_rfdiv;
  1059. ucr2 = readl(sport->port.membase + UCR2);
  1060. *parity = 'n';
  1061. if (ucr2 & UCR2_PREN) {
  1062. if (ucr2 & UCR2_PROE)
  1063. *parity = 'o';
  1064. else
  1065. *parity = 'e';
  1066. }
  1067. if (ucr2 & UCR2_WS)
  1068. *bits = 8;
  1069. else
  1070. *bits = 7;
  1071. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  1072. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  1073. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  1074. if (ucfr_rfdiv == 6)
  1075. ucfr_rfdiv = 7;
  1076. else
  1077. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1078. uartclk = clk_get_rate(sport->clk_per);
  1079. uartclk /= ucfr_rfdiv;
  1080. { /*
  1081. * The next code provides exact computation of
  1082. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1083. * without need of float support or long long division,
  1084. * which would be required to prevent 32bit arithmetic overflow
  1085. */
  1086. unsigned int mul = ubir + 1;
  1087. unsigned int div = 16 * (ubmr + 1);
  1088. unsigned int rem = uartclk % div;
  1089. baud_raw = (uartclk / div) * mul;
  1090. baud_raw += (rem * mul + div / 2) / div;
  1091. *baud = (baud_raw + 50) / 100 * 100;
  1092. }
  1093. if(*baud != baud_raw)
  1094. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  1095. baud_raw, *baud);
  1096. }
  1097. }
  1098. static int __init
  1099. imx_console_setup(struct console *co, char *options)
  1100. {
  1101. struct imx_port *sport;
  1102. int baud = 9600;
  1103. int bits = 8;
  1104. int parity = 'n';
  1105. int flow = 'n';
  1106. /*
  1107. * Check whether an invalid uart number has been specified, and
  1108. * if so, search for the first available port that does have
  1109. * console support.
  1110. */
  1111. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1112. co->index = 0;
  1113. sport = imx_ports[co->index];
  1114. if(sport == NULL)
  1115. return -ENODEV;
  1116. if (options)
  1117. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1118. else
  1119. imx_console_get_options(sport, &baud, &parity, &bits);
  1120. imx_setup_ufcr(sport, 0);
  1121. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1122. }
  1123. static struct uart_driver imx_reg;
  1124. static struct console imx_console = {
  1125. .name = DEV_NAME,
  1126. .write = imx_console_write,
  1127. .device = uart_console_device,
  1128. .setup = imx_console_setup,
  1129. .flags = CON_PRINTBUFFER,
  1130. .index = -1,
  1131. .data = &imx_reg,
  1132. };
  1133. #define IMX_CONSOLE &imx_console
  1134. #else
  1135. #define IMX_CONSOLE NULL
  1136. #endif
  1137. static struct uart_driver imx_reg = {
  1138. .owner = THIS_MODULE,
  1139. .driver_name = DRIVER_NAME,
  1140. .dev_name = DEV_NAME,
  1141. .major = SERIAL_IMX_MAJOR,
  1142. .minor = MINOR_START,
  1143. .nr = ARRAY_SIZE(imx_ports),
  1144. .cons = IMX_CONSOLE,
  1145. };
  1146. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1147. {
  1148. struct imx_port *sport = platform_get_drvdata(dev);
  1149. unsigned int val;
  1150. /* enable wakeup from i.MX UART */
  1151. val = readl(sport->port.membase + UCR3);
  1152. val |= UCR3_AWAKEN;
  1153. writel(val, sport->port.membase + UCR3);
  1154. if (sport)
  1155. uart_suspend_port(&imx_reg, &sport->port);
  1156. return 0;
  1157. }
  1158. static int serial_imx_resume(struct platform_device *dev)
  1159. {
  1160. struct imx_port *sport = platform_get_drvdata(dev);
  1161. unsigned int val;
  1162. /* disable wakeup from i.MX UART */
  1163. val = readl(sport->port.membase + UCR3);
  1164. val &= ~UCR3_AWAKEN;
  1165. writel(val, sport->port.membase + UCR3);
  1166. if (sport)
  1167. uart_resume_port(&imx_reg, &sport->port);
  1168. return 0;
  1169. }
  1170. #ifdef CONFIG_OF
  1171. /*
  1172. * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
  1173. * could successfully get all information from dt or a negative errno.
  1174. */
  1175. static int serial_imx_probe_dt(struct imx_port *sport,
  1176. struct platform_device *pdev)
  1177. {
  1178. struct device_node *np = pdev->dev.of_node;
  1179. const struct of_device_id *of_id =
  1180. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1181. int ret;
  1182. if (!np)
  1183. /* no device tree device */
  1184. return 1;
  1185. ret = of_alias_get_id(np, "serial");
  1186. if (ret < 0) {
  1187. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1188. return ret;
  1189. }
  1190. sport->port.line = ret;
  1191. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1192. sport->have_rtscts = 1;
  1193. if (of_get_property(np, "fsl,irda-mode", NULL))
  1194. sport->use_irda = 1;
  1195. sport->devdata = of_id->data;
  1196. return 0;
  1197. }
  1198. #else
  1199. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1200. struct platform_device *pdev)
  1201. {
  1202. return 1;
  1203. }
  1204. #endif
  1205. static void serial_imx_probe_pdata(struct imx_port *sport,
  1206. struct platform_device *pdev)
  1207. {
  1208. struct imxuart_platform_data *pdata = pdev->dev.platform_data;
  1209. sport->port.line = pdev->id;
  1210. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1211. if (!pdata)
  1212. return;
  1213. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1214. sport->have_rtscts = 1;
  1215. if (pdata->flags & IMXUART_IRDA)
  1216. sport->use_irda = 1;
  1217. }
  1218. static int serial_imx_probe(struct platform_device *pdev)
  1219. {
  1220. struct imx_port *sport;
  1221. struct imxuart_platform_data *pdata;
  1222. void __iomem *base;
  1223. int ret = 0;
  1224. struct resource *res;
  1225. struct pinctrl *pinctrl;
  1226. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1227. if (!sport)
  1228. return -ENOMEM;
  1229. ret = serial_imx_probe_dt(sport, pdev);
  1230. if (ret > 0)
  1231. serial_imx_probe_pdata(sport, pdev);
  1232. else if (ret < 0)
  1233. goto free;
  1234. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1235. if (!res) {
  1236. ret = -ENODEV;
  1237. goto free;
  1238. }
  1239. base = ioremap(res->start, PAGE_SIZE);
  1240. if (!base) {
  1241. ret = -ENOMEM;
  1242. goto free;
  1243. }
  1244. sport->port.dev = &pdev->dev;
  1245. sport->port.mapbase = res->start;
  1246. sport->port.membase = base;
  1247. sport->port.type = PORT_IMX,
  1248. sport->port.iotype = UPIO_MEM;
  1249. sport->port.irq = platform_get_irq(pdev, 0);
  1250. sport->rxirq = platform_get_irq(pdev, 0);
  1251. sport->txirq = platform_get_irq(pdev, 1);
  1252. sport->rtsirq = platform_get_irq(pdev, 2);
  1253. sport->port.fifosize = 32;
  1254. sport->port.ops = &imx_pops;
  1255. sport->port.flags = UPF_BOOT_AUTOCONF;
  1256. init_timer(&sport->timer);
  1257. sport->timer.function = imx_timeout;
  1258. sport->timer.data = (unsigned long)sport;
  1259. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1260. if (IS_ERR(pinctrl)) {
  1261. ret = PTR_ERR(pinctrl);
  1262. goto unmap;
  1263. }
  1264. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1265. if (IS_ERR(sport->clk_ipg)) {
  1266. ret = PTR_ERR(sport->clk_ipg);
  1267. goto unmap;
  1268. }
  1269. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  1270. if (IS_ERR(sport->clk_per)) {
  1271. ret = PTR_ERR(sport->clk_per);
  1272. goto unmap;
  1273. }
  1274. clk_prepare_enable(sport->clk_per);
  1275. clk_prepare_enable(sport->clk_ipg);
  1276. sport->port.uartclk = clk_get_rate(sport->clk_per);
  1277. imx_ports[sport->port.line] = sport;
  1278. pdata = pdev->dev.platform_data;
  1279. if (pdata && pdata->init) {
  1280. ret = pdata->init(pdev);
  1281. if (ret)
  1282. goto clkput;
  1283. }
  1284. ret = uart_add_one_port(&imx_reg, &sport->port);
  1285. if (ret)
  1286. goto deinit;
  1287. platform_set_drvdata(pdev, &sport->port);
  1288. return 0;
  1289. deinit:
  1290. if (pdata && pdata->exit)
  1291. pdata->exit(pdev);
  1292. clkput:
  1293. clk_disable_unprepare(sport->clk_per);
  1294. clk_disable_unprepare(sport->clk_ipg);
  1295. unmap:
  1296. iounmap(sport->port.membase);
  1297. free:
  1298. kfree(sport);
  1299. return ret;
  1300. }
  1301. static int serial_imx_remove(struct platform_device *pdev)
  1302. {
  1303. struct imxuart_platform_data *pdata;
  1304. struct imx_port *sport = platform_get_drvdata(pdev);
  1305. pdata = pdev->dev.platform_data;
  1306. platform_set_drvdata(pdev, NULL);
  1307. uart_remove_one_port(&imx_reg, &sport->port);
  1308. clk_disable_unprepare(sport->clk_per);
  1309. clk_disable_unprepare(sport->clk_ipg);
  1310. if (pdata && pdata->exit)
  1311. pdata->exit(pdev);
  1312. iounmap(sport->port.membase);
  1313. kfree(sport);
  1314. return 0;
  1315. }
  1316. static struct platform_driver serial_imx_driver = {
  1317. .probe = serial_imx_probe,
  1318. .remove = serial_imx_remove,
  1319. .suspend = serial_imx_suspend,
  1320. .resume = serial_imx_resume,
  1321. .id_table = imx_uart_devtype,
  1322. .driver = {
  1323. .name = "imx-uart",
  1324. .owner = THIS_MODULE,
  1325. .of_match_table = imx_uart_dt_ids,
  1326. },
  1327. };
  1328. static int __init imx_serial_init(void)
  1329. {
  1330. int ret;
  1331. printk(KERN_INFO "Serial: IMX driver\n");
  1332. ret = uart_register_driver(&imx_reg);
  1333. if (ret)
  1334. return ret;
  1335. ret = platform_driver_register(&serial_imx_driver);
  1336. if (ret != 0)
  1337. uart_unregister_driver(&imx_reg);
  1338. return ret;
  1339. }
  1340. static void __exit imx_serial_exit(void)
  1341. {
  1342. platform_driver_unregister(&serial_imx_driver);
  1343. uart_unregister_driver(&imx_reg);
  1344. }
  1345. module_init(imx_serial_init);
  1346. module_exit(imx_serial_exit);
  1347. MODULE_AUTHOR("Sascha Hauer");
  1348. MODULE_DESCRIPTION("IMX generic serial port driver");
  1349. MODULE_LICENSE("GPL");
  1350. MODULE_ALIAS("platform:imx-uart");