imx.c 39 KB

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