imx.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * linux/drivers/serial/imx.c
  3. *
  4. * Driver for Motorola IMX serial ports
  5. *
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * Author: Sascha Hauer <sascha@saschahauer.de>
  9. * Copyright (C) 2004 Pengutronix
  10. *
  11. * Copyright (C) 2009 emlix GmbH
  12. * Author: Fabian Godehardt (added IrDA support for iMX)
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * [29-Mar-2005] Mike Lee
  29. * Added hardware handshake
  30. */
  31. #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  32. #define SUPPORT_SYSRQ
  33. #endif
  34. #include <linux/module.h>
  35. #include <linux/ioport.h>
  36. #include <linux/init.h>
  37. #include <linux/console.h>
  38. #include <linux/sysrq.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/serial_core.h>
  43. #include <linux/serial.h>
  44. #include <linux/clk.h>
  45. #include <linux/delay.h>
  46. #include <linux/rational.h>
  47. #include <linux/slab.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <mach/hardware.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 MX2_ONEMS 0xb0 /* One Millisecond register */
  68. #define UTS (cpu_is_mx1() ? 0xd0 : 0xb4) /* UART Test Register */
  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 dectect 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 MX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, 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 MX1_UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
  114. #define MX1_UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
  115. #define MX2_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
  116. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  117. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  118. #define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */
  119. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  120. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  121. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  122. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  123. #define UCR4_IRSC (1<<5) /* IR special case */
  124. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  125. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  126. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  127. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  128. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  129. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  130. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  131. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  132. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  133. #define USR1_RTSS (1<<14) /* RTS pin status */
  134. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  135. #define USR1_RTSD (1<<12) /* RTS delta */
  136. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  137. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  138. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  139. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  140. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  141. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  142. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  143. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  144. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  145. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  146. #define USR2_IDLE (1<<12) /* Idle condition */
  147. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  148. #define USR2_WAKE (1<<7) /* Wake */
  149. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  150. #define USR2_TXDC (1<<3) /* Transmitter complete */
  151. #define USR2_BRCD (1<<2) /* Break condition */
  152. #define USR2_ORE (1<<1) /* Overrun error */
  153. #define USR2_RDR (1<<0) /* Recv data ready */
  154. #define UTS_FRCPERR (1<<13) /* Force parity error */
  155. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  156. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  157. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  158. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  159. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  160. #define UTS_SOFTRST (1<<0) /* Software reset */
  161. /* We've been assigned a range on the "Low-density serial ports" major */
  162. #define SERIAL_IMX_MAJOR 207
  163. #define MINOR_START 16
  164. #define DEV_NAME "ttymxc"
  165. #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
  166. /*
  167. * This determines how often we check the modem status signals
  168. * for any change. They generally aren't connected to an IRQ
  169. * so we have to poll them. We also check immediately before
  170. * filling the TX fifo incase CTS has been dropped.
  171. */
  172. #define MCTRL_TIMEOUT (250*HZ/1000)
  173. #define DRIVER_NAME "IMX-uart"
  174. #define UART_NR 8
  175. struct imx_port {
  176. struct uart_port port;
  177. struct timer_list timer;
  178. unsigned int old_status;
  179. int txirq,rxirq,rtsirq;
  180. unsigned int have_rtscts:1;
  181. unsigned int use_irda:1;
  182. unsigned int irda_inv_rx:1;
  183. unsigned int irda_inv_tx:1;
  184. unsigned short trcv_delay; /* transceiver delay */
  185. struct clk *clk;
  186. };
  187. #ifdef CONFIG_IRDA
  188. #define USE_IRDA(sport) ((sport)->use_irda)
  189. #else
  190. #define USE_IRDA(sport) (0)
  191. #endif
  192. /*
  193. * Handle any change of modem status signal since we were last called.
  194. */
  195. static void imx_mctrl_check(struct imx_port *sport)
  196. {
  197. unsigned int status, changed;
  198. status = sport->port.ops->get_mctrl(&sport->port);
  199. changed = status ^ sport->old_status;
  200. if (changed == 0)
  201. return;
  202. sport->old_status = status;
  203. if (changed & TIOCM_RI)
  204. sport->port.icount.rng++;
  205. if (changed & TIOCM_DSR)
  206. sport->port.icount.dsr++;
  207. if (changed & TIOCM_CAR)
  208. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  209. if (changed & TIOCM_CTS)
  210. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  211. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  212. }
  213. /*
  214. * This is our per-port timeout handler, for checking the
  215. * modem status signals.
  216. */
  217. static void imx_timeout(unsigned long data)
  218. {
  219. struct imx_port *sport = (struct imx_port *)data;
  220. unsigned long flags;
  221. if (sport->port.state) {
  222. spin_lock_irqsave(&sport->port.lock, flags);
  223. imx_mctrl_check(sport);
  224. spin_unlock_irqrestore(&sport->port.lock, flags);
  225. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  226. }
  227. }
  228. /*
  229. * interrupts disabled on entry
  230. */
  231. static void imx_stop_tx(struct uart_port *port)
  232. {
  233. struct imx_port *sport = (struct imx_port *)port;
  234. unsigned long temp;
  235. if (USE_IRDA(sport)) {
  236. /* half duplex - wait for end of transmission */
  237. int n = 256;
  238. while ((--n > 0) &&
  239. !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
  240. udelay(5);
  241. barrier();
  242. }
  243. /*
  244. * irda transceiver - wait a bit more to avoid
  245. * cutoff, hardware dependent
  246. */
  247. udelay(sport->trcv_delay);
  248. /*
  249. * half duplex - reactivate receive mode,
  250. * flush receive pipe echo crap
  251. */
  252. if (readl(sport->port.membase + USR2) & USR2_TXDC) {
  253. temp = readl(sport->port.membase + UCR1);
  254. temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
  255. writel(temp, sport->port.membase + UCR1);
  256. temp = readl(sport->port.membase + UCR4);
  257. temp &= ~(UCR4_TCEN);
  258. writel(temp, sport->port.membase + UCR4);
  259. while (readl(sport->port.membase + URXD0) &
  260. URXD_CHARRDY)
  261. barrier();
  262. temp = readl(sport->port.membase + UCR1);
  263. temp |= UCR1_RRDYEN;
  264. writel(temp, sport->port.membase + UCR1);
  265. temp = readl(sport->port.membase + UCR4);
  266. temp |= UCR4_DREN;
  267. writel(temp, sport->port.membase + UCR4);
  268. }
  269. return;
  270. }
  271. temp = readl(sport->port.membase + UCR1);
  272. writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
  273. }
  274. /*
  275. * interrupts disabled on entry
  276. */
  277. static void imx_stop_rx(struct uart_port *port)
  278. {
  279. struct imx_port *sport = (struct imx_port *)port;
  280. unsigned long temp;
  281. temp = readl(sport->port.membase + UCR2);
  282. writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
  283. }
  284. /*
  285. * Set the modem control timer to fire immediately.
  286. */
  287. static void imx_enable_ms(struct uart_port *port)
  288. {
  289. struct imx_port *sport = (struct imx_port *)port;
  290. mod_timer(&sport->timer, jiffies);
  291. }
  292. static inline void imx_transmit_buffer(struct imx_port *sport)
  293. {
  294. struct circ_buf *xmit = &sport->port.state->xmit;
  295. while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
  296. /* send xmit->buf[xmit->tail]
  297. * out the port here */
  298. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  299. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  300. sport->port.icount.tx++;
  301. if (uart_circ_empty(xmit))
  302. break;
  303. }
  304. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  305. uart_write_wakeup(&sport->port);
  306. if (uart_circ_empty(xmit))
  307. imx_stop_tx(&sport->port);
  308. }
  309. /*
  310. * interrupts disabled on entry
  311. */
  312. static void imx_start_tx(struct uart_port *port)
  313. {
  314. struct imx_port *sport = (struct imx_port *)port;
  315. unsigned long temp;
  316. if (USE_IRDA(sport)) {
  317. /* half duplex in IrDA mode; have to disable receive mode */
  318. temp = readl(sport->port.membase + UCR4);
  319. temp &= ~(UCR4_DREN);
  320. writel(temp, sport->port.membase + UCR4);
  321. temp = readl(sport->port.membase + UCR1);
  322. temp &= ~(UCR1_RRDYEN);
  323. writel(temp, sport->port.membase + UCR1);
  324. }
  325. temp = readl(sport->port.membase + UCR1);
  326. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  327. if (USE_IRDA(sport)) {
  328. temp = readl(sport->port.membase + UCR1);
  329. temp |= UCR1_TRDYEN;
  330. writel(temp, sport->port.membase + UCR1);
  331. temp = readl(sport->port.membase + UCR4);
  332. temp |= UCR4_TCEN;
  333. writel(temp, sport->port.membase + UCR4);
  334. }
  335. if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
  336. imx_transmit_buffer(sport);
  337. }
  338. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  339. {
  340. struct imx_port *sport = dev_id;
  341. unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
  342. unsigned long flags;
  343. spin_lock_irqsave(&sport->port.lock, flags);
  344. writel(USR1_RTSD, sport->port.membase + USR1);
  345. uart_handle_cts_change(&sport->port, !!val);
  346. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  347. spin_unlock_irqrestore(&sport->port.lock, flags);
  348. return IRQ_HANDLED;
  349. }
  350. static irqreturn_t imx_txint(int irq, void *dev_id)
  351. {
  352. struct imx_port *sport = dev_id;
  353. struct circ_buf *xmit = &sport->port.state->xmit;
  354. unsigned long flags;
  355. spin_lock_irqsave(&sport->port.lock,flags);
  356. if (sport->port.x_char)
  357. {
  358. /* Send next char */
  359. writel(sport->port.x_char, sport->port.membase + URTX0);
  360. goto out;
  361. }
  362. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  363. imx_stop_tx(&sport->port);
  364. goto out;
  365. }
  366. imx_transmit_buffer(sport);
  367. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  368. uart_write_wakeup(&sport->port);
  369. out:
  370. spin_unlock_irqrestore(&sport->port.lock,flags);
  371. return IRQ_HANDLED;
  372. }
  373. static irqreturn_t imx_rxint(int irq, void *dev_id)
  374. {
  375. struct imx_port *sport = dev_id;
  376. unsigned int rx,flg,ignored = 0;
  377. struct tty_struct *tty = sport->port.state->port.tty;
  378. unsigned long flags, temp;
  379. spin_lock_irqsave(&sport->port.lock,flags);
  380. while (readl(sport->port.membase + USR2) & USR2_RDR) {
  381. flg = TTY_NORMAL;
  382. sport->port.icount.rx++;
  383. rx = readl(sport->port.membase + URXD0);
  384. temp = readl(sport->port.membase + USR2);
  385. if (temp & USR2_BRCD) {
  386. writel(USR2_BRCD, sport->port.membase + USR2);
  387. if (uart_handle_break(&sport->port))
  388. continue;
  389. }
  390. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  391. continue;
  392. if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
  393. if (rx & URXD_PRERR)
  394. sport->port.icount.parity++;
  395. else if (rx & URXD_FRMERR)
  396. sport->port.icount.frame++;
  397. if (rx & URXD_OVRRUN)
  398. sport->port.icount.overrun++;
  399. if (rx & sport->port.ignore_status_mask) {
  400. if (++ignored > 100)
  401. goto out;
  402. continue;
  403. }
  404. rx &= sport->port.read_status_mask;
  405. if (rx & URXD_PRERR)
  406. flg = TTY_PARITY;
  407. else if (rx & URXD_FRMERR)
  408. flg = TTY_FRAME;
  409. if (rx & URXD_OVRRUN)
  410. flg = TTY_OVERRUN;
  411. #ifdef SUPPORT_SYSRQ
  412. sport->port.sysrq = 0;
  413. #endif
  414. }
  415. tty_insert_flip_char(tty, rx, flg);
  416. }
  417. out:
  418. spin_unlock_irqrestore(&sport->port.lock,flags);
  419. tty_flip_buffer_push(tty);
  420. return IRQ_HANDLED;
  421. }
  422. static irqreturn_t imx_int(int irq, void *dev_id)
  423. {
  424. struct imx_port *sport = dev_id;
  425. unsigned int sts;
  426. sts = readl(sport->port.membase + USR1);
  427. if (sts & USR1_RRDY)
  428. imx_rxint(irq, dev_id);
  429. if (sts & USR1_TRDY &&
  430. readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
  431. imx_txint(irq, dev_id);
  432. if (sts & USR1_RTSD)
  433. imx_rtsint(irq, dev_id);
  434. return IRQ_HANDLED;
  435. }
  436. /*
  437. * Return TIOCSER_TEMT when transmitter is not busy.
  438. */
  439. static unsigned int imx_tx_empty(struct uart_port *port)
  440. {
  441. struct imx_port *sport = (struct imx_port *)port;
  442. return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  443. }
  444. /*
  445. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  446. */
  447. static unsigned int imx_get_mctrl(struct uart_port *port)
  448. {
  449. struct imx_port *sport = (struct imx_port *)port;
  450. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  451. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  452. tmp |= TIOCM_CTS;
  453. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  454. tmp |= TIOCM_RTS;
  455. return tmp;
  456. }
  457. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  458. {
  459. struct imx_port *sport = (struct imx_port *)port;
  460. unsigned long temp;
  461. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  462. if (mctrl & TIOCM_RTS)
  463. temp |= UCR2_CTS;
  464. writel(temp, sport->port.membase + UCR2);
  465. }
  466. /*
  467. * Interrupts always disabled.
  468. */
  469. static void imx_break_ctl(struct uart_port *port, int break_state)
  470. {
  471. struct imx_port *sport = (struct imx_port *)port;
  472. unsigned long flags, temp;
  473. spin_lock_irqsave(&sport->port.lock, flags);
  474. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  475. if ( break_state != 0 )
  476. temp |= UCR1_SNDBRK;
  477. writel(temp, sport->port.membase + UCR1);
  478. spin_unlock_irqrestore(&sport->port.lock, flags);
  479. }
  480. #define TXTL 2 /* reset default */
  481. #define RXTL 1 /* reset default */
  482. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  483. {
  484. unsigned int val;
  485. unsigned int ufcr_rfdiv;
  486. /* set receiver / transmitter trigger level.
  487. * RFDIV is set such way to satisfy requested uartclk value
  488. */
  489. val = TXTL << 10 | RXTL;
  490. ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
  491. / sport->port.uartclk;
  492. if(!ufcr_rfdiv)
  493. ufcr_rfdiv = 1;
  494. val |= UFCR_RFDIV_REG(ufcr_rfdiv);
  495. writel(val, sport->port.membase + UFCR);
  496. return 0;
  497. }
  498. static int imx_startup(struct uart_port *port)
  499. {
  500. struct imx_port *sport = (struct imx_port *)port;
  501. int retval;
  502. unsigned long flags, temp;
  503. imx_setup_ufcr(sport, 0);
  504. /* disable the DREN bit (Data Ready interrupt enable) before
  505. * requesting IRQs
  506. */
  507. temp = readl(sport->port.membase + UCR4);
  508. if (USE_IRDA(sport))
  509. temp |= UCR4_IRSC;
  510. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  511. if (USE_IRDA(sport)) {
  512. /* reset fifo's and state machines */
  513. int i = 100;
  514. temp = readl(sport->port.membase + UCR2);
  515. temp &= ~UCR2_SRST;
  516. writel(temp, sport->port.membase + UCR2);
  517. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  518. (--i > 0)) {
  519. udelay(1);
  520. }
  521. }
  522. /*
  523. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  524. * chips only have one interrupt.
  525. */
  526. if (sport->txirq > 0) {
  527. retval = request_irq(sport->rxirq, imx_rxint, 0,
  528. DRIVER_NAME, sport);
  529. if (retval)
  530. goto error_out1;
  531. retval = request_irq(sport->txirq, imx_txint, 0,
  532. DRIVER_NAME, sport);
  533. if (retval)
  534. goto error_out2;
  535. /* do not use RTS IRQ on IrDA */
  536. if (!USE_IRDA(sport)) {
  537. retval = request_irq(sport->rtsirq, imx_rtsint,
  538. (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
  539. IRQF_TRIGGER_FALLING |
  540. IRQF_TRIGGER_RISING,
  541. DRIVER_NAME, sport);
  542. if (retval)
  543. goto error_out3;
  544. }
  545. } else {
  546. retval = request_irq(sport->port.irq, imx_int, 0,
  547. DRIVER_NAME, sport);
  548. if (retval) {
  549. free_irq(sport->port.irq, sport);
  550. goto error_out1;
  551. }
  552. }
  553. /*
  554. * Finally, clear and enable interrupts
  555. */
  556. writel(USR1_RTSD, sport->port.membase + USR1);
  557. temp = readl(sport->port.membase + UCR1);
  558. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  559. if (USE_IRDA(sport)) {
  560. temp |= UCR1_IREN;
  561. temp &= ~(UCR1_RTSDEN);
  562. }
  563. writel(temp, sport->port.membase + UCR1);
  564. temp = readl(sport->port.membase + UCR2);
  565. temp |= (UCR2_RXEN | UCR2_TXEN);
  566. writel(temp, sport->port.membase + UCR2);
  567. if (USE_IRDA(sport)) {
  568. /* clear RX-FIFO */
  569. int i = 64;
  570. while ((--i > 0) &&
  571. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  572. barrier();
  573. }
  574. }
  575. if (!cpu_is_mx1()) {
  576. temp = readl(sport->port.membase + UCR3);
  577. temp |= MX2_UCR3_RXDMUXSEL;
  578. writel(temp, sport->port.membase + UCR3);
  579. }
  580. if (USE_IRDA(sport)) {
  581. temp = readl(sport->port.membase + UCR4);
  582. if (sport->irda_inv_rx)
  583. temp |= UCR4_INVR;
  584. else
  585. temp &= ~(UCR4_INVR);
  586. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  587. temp = readl(sport->port.membase + UCR3);
  588. if (sport->irda_inv_tx)
  589. temp |= UCR3_INVT;
  590. else
  591. temp &= ~(UCR3_INVT);
  592. writel(temp, sport->port.membase + UCR3);
  593. }
  594. /*
  595. * Enable modem status interrupts
  596. */
  597. spin_lock_irqsave(&sport->port.lock,flags);
  598. imx_enable_ms(&sport->port);
  599. spin_unlock_irqrestore(&sport->port.lock,flags);
  600. if (USE_IRDA(sport)) {
  601. struct imxuart_platform_data *pdata;
  602. pdata = sport->port.dev->platform_data;
  603. sport->irda_inv_rx = pdata->irda_inv_rx;
  604. sport->irda_inv_tx = pdata->irda_inv_tx;
  605. sport->trcv_delay = pdata->transceiver_delay;
  606. if (pdata->irda_enable)
  607. pdata->irda_enable(1);
  608. }
  609. return 0;
  610. error_out3:
  611. if (sport->txirq)
  612. free_irq(sport->txirq, sport);
  613. error_out2:
  614. if (sport->rxirq)
  615. free_irq(sport->rxirq, sport);
  616. error_out1:
  617. return retval;
  618. }
  619. static void imx_shutdown(struct uart_port *port)
  620. {
  621. struct imx_port *sport = (struct imx_port *)port;
  622. unsigned long temp;
  623. temp = readl(sport->port.membase + UCR2);
  624. temp &= ~(UCR2_TXEN);
  625. writel(temp, sport->port.membase + UCR2);
  626. if (USE_IRDA(sport)) {
  627. struct imxuart_platform_data *pdata;
  628. pdata = sport->port.dev->platform_data;
  629. if (pdata->irda_enable)
  630. pdata->irda_enable(0);
  631. }
  632. /*
  633. * Stop our timer.
  634. */
  635. del_timer_sync(&sport->timer);
  636. /*
  637. * Free the interrupts
  638. */
  639. if (sport->txirq > 0) {
  640. if (!USE_IRDA(sport))
  641. free_irq(sport->rtsirq, sport);
  642. free_irq(sport->txirq, sport);
  643. free_irq(sport->rxirq, sport);
  644. } else
  645. free_irq(sport->port.irq, sport);
  646. /*
  647. * Disable all interrupts, port and break condition.
  648. */
  649. temp = readl(sport->port.membase + UCR1);
  650. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  651. if (USE_IRDA(sport))
  652. temp &= ~(UCR1_IREN);
  653. writel(temp, sport->port.membase + UCR1);
  654. }
  655. static void
  656. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  657. struct ktermios *old)
  658. {
  659. struct imx_port *sport = (struct imx_port *)port;
  660. unsigned long flags;
  661. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  662. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  663. unsigned int div, ufcr;
  664. unsigned long num, denom;
  665. uint64_t tdiv64;
  666. /*
  667. * If we don't support modem control lines, don't allow
  668. * these to be set.
  669. */
  670. if (0) {
  671. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  672. termios->c_cflag |= CLOCAL;
  673. }
  674. /*
  675. * We only support CS7 and CS8.
  676. */
  677. while ((termios->c_cflag & CSIZE) != CS7 &&
  678. (termios->c_cflag & CSIZE) != CS8) {
  679. termios->c_cflag &= ~CSIZE;
  680. termios->c_cflag |= old_csize;
  681. old_csize = CS8;
  682. }
  683. if ((termios->c_cflag & CSIZE) == CS8)
  684. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  685. else
  686. ucr2 = UCR2_SRST | UCR2_IRTS;
  687. if (termios->c_cflag & CRTSCTS) {
  688. if( sport->have_rtscts ) {
  689. ucr2 &= ~UCR2_IRTS;
  690. ucr2 |= UCR2_CTSC;
  691. } else {
  692. termios->c_cflag &= ~CRTSCTS;
  693. }
  694. }
  695. if (termios->c_cflag & CSTOPB)
  696. ucr2 |= UCR2_STPB;
  697. if (termios->c_cflag & PARENB) {
  698. ucr2 |= UCR2_PREN;
  699. if (termios->c_cflag & PARODD)
  700. ucr2 |= UCR2_PROE;
  701. }
  702. /*
  703. * Ask the core to calculate the divisor for us.
  704. */
  705. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  706. quot = uart_get_divisor(port, baud);
  707. spin_lock_irqsave(&sport->port.lock, flags);
  708. sport->port.read_status_mask = 0;
  709. if (termios->c_iflag & INPCK)
  710. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  711. if (termios->c_iflag & (BRKINT | PARMRK))
  712. sport->port.read_status_mask |= URXD_BRK;
  713. /*
  714. * Characters to ignore
  715. */
  716. sport->port.ignore_status_mask = 0;
  717. if (termios->c_iflag & IGNPAR)
  718. sport->port.ignore_status_mask |= URXD_PRERR;
  719. if (termios->c_iflag & IGNBRK) {
  720. sport->port.ignore_status_mask |= URXD_BRK;
  721. /*
  722. * If we're ignoring parity and break indicators,
  723. * ignore overruns too (for real raw support).
  724. */
  725. if (termios->c_iflag & IGNPAR)
  726. sport->port.ignore_status_mask |= URXD_OVRRUN;
  727. }
  728. del_timer_sync(&sport->timer);
  729. /*
  730. * Update the per-port timeout.
  731. */
  732. uart_update_timeout(port, termios->c_cflag, baud);
  733. /*
  734. * disable interrupts and drain transmitter
  735. */
  736. old_ucr1 = readl(sport->port.membase + UCR1);
  737. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  738. sport->port.membase + UCR1);
  739. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  740. barrier();
  741. /* then, disable everything */
  742. old_txrxen = readl(sport->port.membase + UCR2);
  743. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  744. sport->port.membase + UCR2);
  745. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  746. if (USE_IRDA(sport)) {
  747. /*
  748. * use maximum available submodule frequency to
  749. * avoid missing short pulses due to low sampling rate
  750. */
  751. div = 1;
  752. } else {
  753. div = sport->port.uartclk / (baud * 16);
  754. if (div > 7)
  755. div = 7;
  756. if (!div)
  757. div = 1;
  758. }
  759. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  760. 1 << 16, 1 << 16, &num, &denom);
  761. if (port->state && port->state->port.tty) {
  762. tdiv64 = sport->port.uartclk;
  763. tdiv64 *= num;
  764. do_div(tdiv64, denom * 16 * div);
  765. tty_encode_baud_rate(sport->port.state->port.tty,
  766. (speed_t)tdiv64, (speed_t)tdiv64);
  767. }
  768. num -= 1;
  769. denom -= 1;
  770. ufcr = readl(sport->port.membase + UFCR);
  771. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  772. writel(ufcr, sport->port.membase + UFCR);
  773. writel(num, sport->port.membase + UBIR);
  774. writel(denom, sport->port.membase + UBMR);
  775. if (!cpu_is_mx1())
  776. writel(sport->port.uartclk / div / 1000,
  777. sport->port.membase + MX2_ONEMS);
  778. writel(old_ucr1, sport->port.membase + UCR1);
  779. /* set the parity, stop bits and data size */
  780. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  781. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  782. imx_enable_ms(&sport->port);
  783. spin_unlock_irqrestore(&sport->port.lock, flags);
  784. }
  785. static const char *imx_type(struct uart_port *port)
  786. {
  787. struct imx_port *sport = (struct imx_port *)port;
  788. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  789. }
  790. /*
  791. * Release the memory region(s) being used by 'port'.
  792. */
  793. static void imx_release_port(struct uart_port *port)
  794. {
  795. struct platform_device *pdev = to_platform_device(port->dev);
  796. struct resource *mmres;
  797. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  798. release_mem_region(mmres->start, mmres->end - mmres->start + 1);
  799. }
  800. /*
  801. * Request the memory region(s) being used by 'port'.
  802. */
  803. static int imx_request_port(struct uart_port *port)
  804. {
  805. struct platform_device *pdev = to_platform_device(port->dev);
  806. struct resource *mmres;
  807. void *ret;
  808. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  809. if (!mmres)
  810. return -ENODEV;
  811. ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
  812. "imx-uart");
  813. return ret ? 0 : -EBUSY;
  814. }
  815. /*
  816. * Configure/autoconfigure the port.
  817. */
  818. static void imx_config_port(struct uart_port *port, int flags)
  819. {
  820. struct imx_port *sport = (struct imx_port *)port;
  821. if (flags & UART_CONFIG_TYPE &&
  822. imx_request_port(&sport->port) == 0)
  823. sport->port.type = PORT_IMX;
  824. }
  825. /*
  826. * Verify the new serial_struct (for TIOCSSERIAL).
  827. * The only change we allow are to the flags and type, and
  828. * even then only between PORT_IMX and PORT_UNKNOWN
  829. */
  830. static int
  831. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  832. {
  833. struct imx_port *sport = (struct imx_port *)port;
  834. int ret = 0;
  835. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  836. ret = -EINVAL;
  837. if (sport->port.irq != ser->irq)
  838. ret = -EINVAL;
  839. if (ser->io_type != UPIO_MEM)
  840. ret = -EINVAL;
  841. if (sport->port.uartclk / 16 != ser->baud_base)
  842. ret = -EINVAL;
  843. if ((void *)sport->port.mapbase != ser->iomem_base)
  844. ret = -EINVAL;
  845. if (sport->port.iobase != ser->port)
  846. ret = -EINVAL;
  847. if (ser->hub6 != 0)
  848. ret = -EINVAL;
  849. return ret;
  850. }
  851. static struct uart_ops imx_pops = {
  852. .tx_empty = imx_tx_empty,
  853. .set_mctrl = imx_set_mctrl,
  854. .get_mctrl = imx_get_mctrl,
  855. .stop_tx = imx_stop_tx,
  856. .start_tx = imx_start_tx,
  857. .stop_rx = imx_stop_rx,
  858. .enable_ms = imx_enable_ms,
  859. .break_ctl = imx_break_ctl,
  860. .startup = imx_startup,
  861. .shutdown = imx_shutdown,
  862. .set_termios = imx_set_termios,
  863. .type = imx_type,
  864. .release_port = imx_release_port,
  865. .request_port = imx_request_port,
  866. .config_port = imx_config_port,
  867. .verify_port = imx_verify_port,
  868. };
  869. static struct imx_port *imx_ports[UART_NR];
  870. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  871. static void imx_console_putchar(struct uart_port *port, int ch)
  872. {
  873. struct imx_port *sport = (struct imx_port *)port;
  874. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  875. barrier();
  876. writel(ch, sport->port.membase + URTX0);
  877. }
  878. /*
  879. * Interrupts are disabled on entering
  880. */
  881. static void
  882. imx_console_write(struct console *co, const char *s, unsigned int count)
  883. {
  884. struct imx_port *sport = imx_ports[co->index];
  885. unsigned int old_ucr1, old_ucr2, ucr1;
  886. /*
  887. * First, save UCR1/2 and then disable interrupts
  888. */
  889. ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
  890. old_ucr2 = readl(sport->port.membase + UCR2);
  891. if (cpu_is_mx1())
  892. ucr1 |= MX1_UCR1_UARTCLKEN;
  893. ucr1 |= UCR1_UARTEN;
  894. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  895. writel(ucr1, sport->port.membase + UCR1);
  896. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  897. uart_console_write(&sport->port, s, count, imx_console_putchar);
  898. /*
  899. * Finally, wait for transmitter to become empty
  900. * and restore UCR1/2
  901. */
  902. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  903. writel(old_ucr1, sport->port.membase + UCR1);
  904. writel(old_ucr2, sport->port.membase + UCR2);
  905. }
  906. /*
  907. * If the port was already initialised (eg, by a boot loader),
  908. * try to determine the current setup.
  909. */
  910. static void __init
  911. imx_console_get_options(struct imx_port *sport, int *baud,
  912. int *parity, int *bits)
  913. {
  914. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  915. /* ok, the port was enabled */
  916. unsigned int ucr2, ubir,ubmr, uartclk;
  917. unsigned int baud_raw;
  918. unsigned int ucfr_rfdiv;
  919. ucr2 = readl(sport->port.membase + UCR2);
  920. *parity = 'n';
  921. if (ucr2 & UCR2_PREN) {
  922. if (ucr2 & UCR2_PROE)
  923. *parity = 'o';
  924. else
  925. *parity = 'e';
  926. }
  927. if (ucr2 & UCR2_WS)
  928. *bits = 8;
  929. else
  930. *bits = 7;
  931. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  932. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  933. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  934. if (ucfr_rfdiv == 6)
  935. ucfr_rfdiv = 7;
  936. else
  937. ucfr_rfdiv = 6 - ucfr_rfdiv;
  938. uartclk = clk_get_rate(sport->clk);
  939. uartclk /= ucfr_rfdiv;
  940. { /*
  941. * The next code provides exact computation of
  942. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  943. * without need of float support or long long division,
  944. * which would be required to prevent 32bit arithmetic overflow
  945. */
  946. unsigned int mul = ubir + 1;
  947. unsigned int div = 16 * (ubmr + 1);
  948. unsigned int rem = uartclk % div;
  949. baud_raw = (uartclk / div) * mul;
  950. baud_raw += (rem * mul + div / 2) / div;
  951. *baud = (baud_raw + 50) / 100 * 100;
  952. }
  953. if(*baud != baud_raw)
  954. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  955. baud_raw, *baud);
  956. }
  957. }
  958. static int __init
  959. imx_console_setup(struct console *co, char *options)
  960. {
  961. struct imx_port *sport;
  962. int baud = 9600;
  963. int bits = 8;
  964. int parity = 'n';
  965. int flow = 'n';
  966. /*
  967. * Check whether an invalid uart number has been specified, and
  968. * if so, search for the first available port that does have
  969. * console support.
  970. */
  971. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  972. co->index = 0;
  973. sport = imx_ports[co->index];
  974. if(sport == NULL)
  975. return -ENODEV;
  976. if (options)
  977. uart_parse_options(options, &baud, &parity, &bits, &flow);
  978. else
  979. imx_console_get_options(sport, &baud, &parity, &bits);
  980. imx_setup_ufcr(sport, 0);
  981. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  982. }
  983. static struct uart_driver imx_reg;
  984. static struct console imx_console = {
  985. .name = DEV_NAME,
  986. .write = imx_console_write,
  987. .device = uart_console_device,
  988. .setup = imx_console_setup,
  989. .flags = CON_PRINTBUFFER,
  990. .index = -1,
  991. .data = &imx_reg,
  992. };
  993. #define IMX_CONSOLE &imx_console
  994. #else
  995. #define IMX_CONSOLE NULL
  996. #endif
  997. static struct uart_driver imx_reg = {
  998. .owner = THIS_MODULE,
  999. .driver_name = DRIVER_NAME,
  1000. .dev_name = DEV_NAME,
  1001. .major = SERIAL_IMX_MAJOR,
  1002. .minor = MINOR_START,
  1003. .nr = ARRAY_SIZE(imx_ports),
  1004. .cons = IMX_CONSOLE,
  1005. };
  1006. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1007. {
  1008. struct imx_port *sport = platform_get_drvdata(dev);
  1009. if (sport)
  1010. uart_suspend_port(&imx_reg, &sport->port);
  1011. return 0;
  1012. }
  1013. static int serial_imx_resume(struct platform_device *dev)
  1014. {
  1015. struct imx_port *sport = platform_get_drvdata(dev);
  1016. if (sport)
  1017. uart_resume_port(&imx_reg, &sport->port);
  1018. return 0;
  1019. }
  1020. static int serial_imx_probe(struct platform_device *pdev)
  1021. {
  1022. struct imx_port *sport;
  1023. struct imxuart_platform_data *pdata;
  1024. void __iomem *base;
  1025. int ret = 0;
  1026. struct resource *res;
  1027. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1028. if (!sport)
  1029. return -ENOMEM;
  1030. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1031. if (!res) {
  1032. ret = -ENODEV;
  1033. goto free;
  1034. }
  1035. base = ioremap(res->start, PAGE_SIZE);
  1036. if (!base) {
  1037. ret = -ENOMEM;
  1038. goto free;
  1039. }
  1040. sport->port.dev = &pdev->dev;
  1041. sport->port.mapbase = res->start;
  1042. sport->port.membase = base;
  1043. sport->port.type = PORT_IMX,
  1044. sport->port.iotype = UPIO_MEM;
  1045. sport->port.irq = platform_get_irq(pdev, 0);
  1046. sport->rxirq = platform_get_irq(pdev, 0);
  1047. sport->txirq = platform_get_irq(pdev, 1);
  1048. sport->rtsirq = platform_get_irq(pdev, 2);
  1049. sport->port.fifosize = 32;
  1050. sport->port.ops = &imx_pops;
  1051. sport->port.flags = UPF_BOOT_AUTOCONF;
  1052. sport->port.line = pdev->id;
  1053. init_timer(&sport->timer);
  1054. sport->timer.function = imx_timeout;
  1055. sport->timer.data = (unsigned long)sport;
  1056. sport->clk = clk_get(&pdev->dev, "uart");
  1057. if (IS_ERR(sport->clk)) {
  1058. ret = PTR_ERR(sport->clk);
  1059. goto unmap;
  1060. }
  1061. clk_enable(sport->clk);
  1062. sport->port.uartclk = clk_get_rate(sport->clk);
  1063. imx_ports[pdev->id] = sport;
  1064. pdata = pdev->dev.platform_data;
  1065. if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  1066. sport->have_rtscts = 1;
  1067. #ifdef CONFIG_IRDA
  1068. if (pdata && (pdata->flags & IMXUART_IRDA))
  1069. sport->use_irda = 1;
  1070. #endif
  1071. if (pdata && pdata->init) {
  1072. ret = pdata->init(pdev);
  1073. if (ret)
  1074. goto clkput;
  1075. }
  1076. ret = uart_add_one_port(&imx_reg, &sport->port);
  1077. if (ret)
  1078. goto deinit;
  1079. platform_set_drvdata(pdev, &sport->port);
  1080. return 0;
  1081. deinit:
  1082. if (pdata && pdata->exit)
  1083. pdata->exit(pdev);
  1084. clkput:
  1085. clk_put(sport->clk);
  1086. clk_disable(sport->clk);
  1087. unmap:
  1088. iounmap(sport->port.membase);
  1089. free:
  1090. kfree(sport);
  1091. return ret;
  1092. }
  1093. static int serial_imx_remove(struct platform_device *pdev)
  1094. {
  1095. struct imxuart_platform_data *pdata;
  1096. struct imx_port *sport = platform_get_drvdata(pdev);
  1097. pdata = pdev->dev.platform_data;
  1098. platform_set_drvdata(pdev, NULL);
  1099. if (sport) {
  1100. uart_remove_one_port(&imx_reg, &sport->port);
  1101. clk_put(sport->clk);
  1102. }
  1103. clk_disable(sport->clk);
  1104. if (pdata && pdata->exit)
  1105. pdata->exit(pdev);
  1106. iounmap(sport->port.membase);
  1107. kfree(sport);
  1108. return 0;
  1109. }
  1110. static struct platform_driver serial_imx_driver = {
  1111. .probe = serial_imx_probe,
  1112. .remove = serial_imx_remove,
  1113. .suspend = serial_imx_suspend,
  1114. .resume = serial_imx_resume,
  1115. .driver = {
  1116. .name = "imx-uart",
  1117. .owner = THIS_MODULE,
  1118. },
  1119. };
  1120. static int __init imx_serial_init(void)
  1121. {
  1122. int ret;
  1123. printk(KERN_INFO "Serial: IMX driver\n");
  1124. ret = uart_register_driver(&imx_reg);
  1125. if (ret)
  1126. return ret;
  1127. ret = platform_driver_register(&serial_imx_driver);
  1128. if (ret != 0)
  1129. uart_unregister_driver(&imx_reg);
  1130. return 0;
  1131. }
  1132. static void __exit imx_serial_exit(void)
  1133. {
  1134. platform_driver_unregister(&serial_imx_driver);
  1135. uart_unregister_driver(&imx_reg);
  1136. }
  1137. module_init(imx_serial_init);
  1138. module_exit(imx_serial_exit);
  1139. MODULE_AUTHOR("Sascha Hauer");
  1140. MODULE_DESCRIPTION("IMX generic serial port driver");
  1141. MODULE_LICENSE("GPL");
  1142. MODULE_ALIAS("platform:imx-uart");