imx.c 35 KB

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