imx.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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_SHF 10 /* CTS trigger level shift */
  119. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  120. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  121. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  122. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  123. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  124. #define UCR4_IRSC (1<<5) /* IR special case */
  125. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  126. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  127. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  128. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  129. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  130. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  131. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  132. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  133. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  134. #define USR1_RTSS (1<<14) /* RTS pin status */
  135. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  136. #define USR1_RTSD (1<<12) /* RTS delta */
  137. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  138. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  139. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  140. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  141. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  142. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  143. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  144. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  145. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  146. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  147. #define USR2_IDLE (1<<12) /* Idle condition */
  148. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  149. #define USR2_WAKE (1<<7) /* Wake */
  150. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  151. #define USR2_TXDC (1<<3) /* Transmitter complete */
  152. #define USR2_BRCD (1<<2) /* Break condition */
  153. #define USR2_ORE (1<<1) /* Overrun error */
  154. #define USR2_RDR (1<<0) /* Recv data ready */
  155. #define UTS_FRCPERR (1<<13) /* Force parity error */
  156. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  157. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  158. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  159. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  160. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  161. #define UTS_SOFTRST (1<<0) /* Software reset */
  162. /* We've been assigned a range on the "Low-density serial ports" major */
  163. #define SERIAL_IMX_MAJOR 207
  164. #define MINOR_START 16
  165. #define DEV_NAME "ttymxc"
  166. #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
  167. /*
  168. * This determines how often we check the modem status signals
  169. * for any change. They generally aren't connected to an IRQ
  170. * so we have to poll them. We also check immediately before
  171. * filling the TX fifo incase CTS has been dropped.
  172. */
  173. #define MCTRL_TIMEOUT (250*HZ/1000)
  174. #define DRIVER_NAME "IMX-uart"
  175. #define UART_NR 8
  176. struct imx_port {
  177. struct uart_port port;
  178. struct timer_list timer;
  179. unsigned int old_status;
  180. int txirq,rxirq,rtsirq;
  181. unsigned int have_rtscts:1;
  182. unsigned int use_irda:1;
  183. unsigned int irda_inv_rx:1;
  184. unsigned int irda_inv_tx:1;
  185. unsigned short trcv_delay; /* transceiver delay */
  186. struct clk *clk;
  187. };
  188. #ifdef CONFIG_IRDA
  189. #define USE_IRDA(sport) ((sport)->use_irda)
  190. #else
  191. #define USE_IRDA(sport) (0)
  192. #endif
  193. /*
  194. * Handle any change of modem status signal since we were last called.
  195. */
  196. static void imx_mctrl_check(struct imx_port *sport)
  197. {
  198. unsigned int status, changed;
  199. status = sport->port.ops->get_mctrl(&sport->port);
  200. changed = status ^ sport->old_status;
  201. if (changed == 0)
  202. return;
  203. sport->old_status = status;
  204. if (changed & TIOCM_RI)
  205. sport->port.icount.rng++;
  206. if (changed & TIOCM_DSR)
  207. sport->port.icount.dsr++;
  208. if (changed & TIOCM_CAR)
  209. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  210. if (changed & TIOCM_CTS)
  211. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  212. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  213. }
  214. /*
  215. * This is our per-port timeout handler, for checking the
  216. * modem status signals.
  217. */
  218. static void imx_timeout(unsigned long data)
  219. {
  220. struct imx_port *sport = (struct imx_port *)data;
  221. unsigned long flags;
  222. if (sport->port.state) {
  223. spin_lock_irqsave(&sport->port.lock, flags);
  224. imx_mctrl_check(sport);
  225. spin_unlock_irqrestore(&sport->port.lock, flags);
  226. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  227. }
  228. }
  229. /*
  230. * interrupts disabled on entry
  231. */
  232. static void imx_stop_tx(struct uart_port *port)
  233. {
  234. struct imx_port *sport = (struct imx_port *)port;
  235. unsigned long temp;
  236. if (USE_IRDA(sport)) {
  237. /* half duplex - wait for end of transmission */
  238. int n = 256;
  239. while ((--n > 0) &&
  240. !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
  241. udelay(5);
  242. barrier();
  243. }
  244. /*
  245. * irda transceiver - wait a bit more to avoid
  246. * cutoff, hardware dependent
  247. */
  248. udelay(sport->trcv_delay);
  249. /*
  250. * half duplex - reactivate receive mode,
  251. * flush receive pipe echo crap
  252. */
  253. if (readl(sport->port.membase + USR2) & USR2_TXDC) {
  254. temp = readl(sport->port.membase + UCR1);
  255. temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
  256. writel(temp, sport->port.membase + UCR1);
  257. temp = readl(sport->port.membase + UCR4);
  258. temp &= ~(UCR4_TCEN);
  259. writel(temp, sport->port.membase + UCR4);
  260. while (readl(sport->port.membase + URXD0) &
  261. URXD_CHARRDY)
  262. barrier();
  263. temp = readl(sport->port.membase + UCR1);
  264. temp |= UCR1_RRDYEN;
  265. writel(temp, sport->port.membase + UCR1);
  266. temp = readl(sport->port.membase + UCR4);
  267. temp |= UCR4_DREN;
  268. writel(temp, sport->port.membase + UCR4);
  269. }
  270. return;
  271. }
  272. temp = readl(sport->port.membase + UCR1);
  273. writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
  274. }
  275. /*
  276. * interrupts disabled on entry
  277. */
  278. static void imx_stop_rx(struct uart_port *port)
  279. {
  280. struct imx_port *sport = (struct imx_port *)port;
  281. unsigned long temp;
  282. temp = readl(sport->port.membase + UCR2);
  283. writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
  284. }
  285. /*
  286. * Set the modem control timer to fire immediately.
  287. */
  288. static void imx_enable_ms(struct uart_port *port)
  289. {
  290. struct imx_port *sport = (struct imx_port *)port;
  291. mod_timer(&sport->timer, jiffies);
  292. }
  293. static inline void imx_transmit_buffer(struct imx_port *sport)
  294. {
  295. struct circ_buf *xmit = &sport->port.state->xmit;
  296. while (!uart_circ_empty(xmit) &&
  297. !(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
  298. /* send xmit->buf[xmit->tail]
  299. * out the port here */
  300. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  301. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  302. sport->port.icount.tx++;
  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. /* half the RX buffer size */
  499. #define CTSTL 16
  500. static int imx_startup(struct uart_port *port)
  501. {
  502. struct imx_port *sport = (struct imx_port *)port;
  503. int retval;
  504. unsigned long flags, temp;
  505. imx_setup_ufcr(sport, 0);
  506. /* disable the DREN bit (Data Ready interrupt enable) before
  507. * requesting IRQs
  508. */
  509. temp = readl(sport->port.membase + UCR4);
  510. if (USE_IRDA(sport))
  511. temp |= UCR4_IRSC;
  512. /* set the trigger level for CTS */
  513. temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
  514. temp |= CTSTL<< UCR4_CTSTL_SHF;
  515. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  516. if (USE_IRDA(sport)) {
  517. /* reset fifo's and state machines */
  518. int i = 100;
  519. temp = readl(sport->port.membase + UCR2);
  520. temp &= ~UCR2_SRST;
  521. writel(temp, sport->port.membase + UCR2);
  522. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  523. (--i > 0)) {
  524. udelay(1);
  525. }
  526. }
  527. /*
  528. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  529. * chips only have one interrupt.
  530. */
  531. if (sport->txirq > 0) {
  532. retval = request_irq(sport->rxirq, imx_rxint, 0,
  533. DRIVER_NAME, sport);
  534. if (retval)
  535. goto error_out1;
  536. retval = request_irq(sport->txirq, imx_txint, 0,
  537. DRIVER_NAME, sport);
  538. if (retval)
  539. goto error_out2;
  540. /* do not use RTS IRQ on IrDA */
  541. if (!USE_IRDA(sport)) {
  542. retval = request_irq(sport->rtsirq, imx_rtsint,
  543. (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
  544. IRQF_TRIGGER_FALLING |
  545. IRQF_TRIGGER_RISING,
  546. DRIVER_NAME, sport);
  547. if (retval)
  548. goto error_out3;
  549. }
  550. } else {
  551. retval = request_irq(sport->port.irq, imx_int, 0,
  552. DRIVER_NAME, sport);
  553. if (retval) {
  554. free_irq(sport->port.irq, sport);
  555. goto error_out1;
  556. }
  557. }
  558. /*
  559. * Finally, clear and enable interrupts
  560. */
  561. writel(USR1_RTSD, sport->port.membase + USR1);
  562. temp = readl(sport->port.membase + UCR1);
  563. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  564. if (USE_IRDA(sport)) {
  565. temp |= UCR1_IREN;
  566. temp &= ~(UCR1_RTSDEN);
  567. }
  568. writel(temp, sport->port.membase + UCR1);
  569. temp = readl(sport->port.membase + UCR2);
  570. temp |= (UCR2_RXEN | UCR2_TXEN);
  571. writel(temp, sport->port.membase + UCR2);
  572. if (USE_IRDA(sport)) {
  573. /* clear RX-FIFO */
  574. int i = 64;
  575. while ((--i > 0) &&
  576. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  577. barrier();
  578. }
  579. }
  580. if (!cpu_is_mx1()) {
  581. temp = readl(sport->port.membase + UCR3);
  582. temp |= MX2_UCR3_RXDMUXSEL;
  583. writel(temp, sport->port.membase + UCR3);
  584. }
  585. if (USE_IRDA(sport)) {
  586. temp = readl(sport->port.membase + UCR4);
  587. if (sport->irda_inv_rx)
  588. temp |= UCR4_INVR;
  589. else
  590. temp &= ~(UCR4_INVR);
  591. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  592. temp = readl(sport->port.membase + UCR3);
  593. if (sport->irda_inv_tx)
  594. temp |= UCR3_INVT;
  595. else
  596. temp &= ~(UCR3_INVT);
  597. writel(temp, sport->port.membase + UCR3);
  598. }
  599. /*
  600. * Enable modem status interrupts
  601. */
  602. spin_lock_irqsave(&sport->port.lock,flags);
  603. imx_enable_ms(&sport->port);
  604. spin_unlock_irqrestore(&sport->port.lock,flags);
  605. if (USE_IRDA(sport)) {
  606. struct imxuart_platform_data *pdata;
  607. pdata = sport->port.dev->platform_data;
  608. sport->irda_inv_rx = pdata->irda_inv_rx;
  609. sport->irda_inv_tx = pdata->irda_inv_tx;
  610. sport->trcv_delay = pdata->transceiver_delay;
  611. if (pdata->irda_enable)
  612. pdata->irda_enable(1);
  613. }
  614. return 0;
  615. error_out3:
  616. if (sport->txirq)
  617. free_irq(sport->txirq, sport);
  618. error_out2:
  619. if (sport->rxirq)
  620. free_irq(sport->rxirq, sport);
  621. error_out1:
  622. return retval;
  623. }
  624. static void imx_shutdown(struct uart_port *port)
  625. {
  626. struct imx_port *sport = (struct imx_port *)port;
  627. unsigned long temp;
  628. temp = readl(sport->port.membase + UCR2);
  629. temp &= ~(UCR2_TXEN);
  630. writel(temp, sport->port.membase + UCR2);
  631. if (USE_IRDA(sport)) {
  632. struct imxuart_platform_data *pdata;
  633. pdata = sport->port.dev->platform_data;
  634. if (pdata->irda_enable)
  635. pdata->irda_enable(0);
  636. }
  637. /*
  638. * Stop our timer.
  639. */
  640. del_timer_sync(&sport->timer);
  641. /*
  642. * Free the interrupts
  643. */
  644. if (sport->txirq > 0) {
  645. if (!USE_IRDA(sport))
  646. free_irq(sport->rtsirq, sport);
  647. free_irq(sport->txirq, sport);
  648. free_irq(sport->rxirq, sport);
  649. } else
  650. free_irq(sport->port.irq, sport);
  651. /*
  652. * Disable all interrupts, port and break condition.
  653. */
  654. temp = readl(sport->port.membase + UCR1);
  655. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  656. if (USE_IRDA(sport))
  657. temp &= ~(UCR1_IREN);
  658. writel(temp, sport->port.membase + UCR1);
  659. }
  660. static void
  661. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  662. struct ktermios *old)
  663. {
  664. struct imx_port *sport = (struct imx_port *)port;
  665. unsigned long flags;
  666. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  667. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  668. unsigned int div, ufcr;
  669. unsigned long num, denom;
  670. uint64_t tdiv64;
  671. /*
  672. * If we don't support modem control lines, don't allow
  673. * these to be set.
  674. */
  675. if (0) {
  676. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  677. termios->c_cflag |= CLOCAL;
  678. }
  679. /*
  680. * We only support CS7 and CS8.
  681. */
  682. while ((termios->c_cflag & CSIZE) != CS7 &&
  683. (termios->c_cflag & CSIZE) != CS8) {
  684. termios->c_cflag &= ~CSIZE;
  685. termios->c_cflag |= old_csize;
  686. old_csize = CS8;
  687. }
  688. if ((termios->c_cflag & CSIZE) == CS8)
  689. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  690. else
  691. ucr2 = UCR2_SRST | UCR2_IRTS;
  692. if (termios->c_cflag & CRTSCTS) {
  693. if( sport->have_rtscts ) {
  694. ucr2 &= ~UCR2_IRTS;
  695. ucr2 |= UCR2_CTSC;
  696. } else {
  697. termios->c_cflag &= ~CRTSCTS;
  698. }
  699. }
  700. if (termios->c_cflag & CSTOPB)
  701. ucr2 |= UCR2_STPB;
  702. if (termios->c_cflag & PARENB) {
  703. ucr2 |= UCR2_PREN;
  704. if (termios->c_cflag & PARODD)
  705. ucr2 |= UCR2_PROE;
  706. }
  707. /*
  708. * Ask the core to calculate the divisor for us.
  709. */
  710. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  711. quot = uart_get_divisor(port, baud);
  712. spin_lock_irqsave(&sport->port.lock, flags);
  713. sport->port.read_status_mask = 0;
  714. if (termios->c_iflag & INPCK)
  715. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  716. if (termios->c_iflag & (BRKINT | PARMRK))
  717. sport->port.read_status_mask |= URXD_BRK;
  718. /*
  719. * Characters to ignore
  720. */
  721. sport->port.ignore_status_mask = 0;
  722. if (termios->c_iflag & IGNPAR)
  723. sport->port.ignore_status_mask |= URXD_PRERR;
  724. if (termios->c_iflag & IGNBRK) {
  725. sport->port.ignore_status_mask |= URXD_BRK;
  726. /*
  727. * If we're ignoring parity and break indicators,
  728. * ignore overruns too (for real raw support).
  729. */
  730. if (termios->c_iflag & IGNPAR)
  731. sport->port.ignore_status_mask |= URXD_OVRRUN;
  732. }
  733. del_timer_sync(&sport->timer);
  734. /*
  735. * Update the per-port timeout.
  736. */
  737. uart_update_timeout(port, termios->c_cflag, baud);
  738. /*
  739. * disable interrupts and drain transmitter
  740. */
  741. old_ucr1 = readl(sport->port.membase + UCR1);
  742. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  743. sport->port.membase + UCR1);
  744. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  745. barrier();
  746. /* then, disable everything */
  747. old_txrxen = readl(sport->port.membase + UCR2);
  748. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  749. sport->port.membase + UCR2);
  750. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  751. if (USE_IRDA(sport)) {
  752. /*
  753. * use maximum available submodule frequency to
  754. * avoid missing short pulses due to low sampling rate
  755. */
  756. div = 1;
  757. } else {
  758. div = sport->port.uartclk / (baud * 16);
  759. if (div > 7)
  760. div = 7;
  761. if (!div)
  762. div = 1;
  763. }
  764. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  765. 1 << 16, 1 << 16, &num, &denom);
  766. tdiv64 = sport->port.uartclk;
  767. tdiv64 *= num;
  768. do_div(tdiv64, denom * 16 * div);
  769. tty_termios_encode_baud_rate(termios,
  770. (speed_t)tdiv64, (speed_t)tdiv64);
  771. num -= 1;
  772. denom -= 1;
  773. ufcr = readl(sport->port.membase + UFCR);
  774. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  775. writel(ufcr, sport->port.membase + UFCR);
  776. writel(num, sport->port.membase + UBIR);
  777. writel(denom, sport->port.membase + UBMR);
  778. if (!cpu_is_mx1())
  779. writel(sport->port.uartclk / div / 1000,
  780. sport->port.membase + MX2_ONEMS);
  781. writel(old_ucr1, sport->port.membase + UCR1);
  782. /* set the parity, stop bits and data size */
  783. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  784. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  785. imx_enable_ms(&sport->port);
  786. spin_unlock_irqrestore(&sport->port.lock, flags);
  787. }
  788. static const char *imx_type(struct uart_port *port)
  789. {
  790. struct imx_port *sport = (struct imx_port *)port;
  791. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  792. }
  793. /*
  794. * Release the memory region(s) being used by 'port'.
  795. */
  796. static void imx_release_port(struct uart_port *port)
  797. {
  798. struct platform_device *pdev = to_platform_device(port->dev);
  799. struct resource *mmres;
  800. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  801. release_mem_region(mmres->start, mmres->end - mmres->start + 1);
  802. }
  803. /*
  804. * Request the memory region(s) being used by 'port'.
  805. */
  806. static int imx_request_port(struct uart_port *port)
  807. {
  808. struct platform_device *pdev = to_platform_device(port->dev);
  809. struct resource *mmres;
  810. void *ret;
  811. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  812. if (!mmres)
  813. return -ENODEV;
  814. ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
  815. "imx-uart");
  816. return ret ? 0 : -EBUSY;
  817. }
  818. /*
  819. * Configure/autoconfigure the port.
  820. */
  821. static void imx_config_port(struct uart_port *port, int flags)
  822. {
  823. struct imx_port *sport = (struct imx_port *)port;
  824. if (flags & UART_CONFIG_TYPE &&
  825. imx_request_port(&sport->port) == 0)
  826. sport->port.type = PORT_IMX;
  827. }
  828. /*
  829. * Verify the new serial_struct (for TIOCSSERIAL).
  830. * The only change we allow are to the flags and type, and
  831. * even then only between PORT_IMX and PORT_UNKNOWN
  832. */
  833. static int
  834. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  835. {
  836. struct imx_port *sport = (struct imx_port *)port;
  837. int ret = 0;
  838. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  839. ret = -EINVAL;
  840. if (sport->port.irq != ser->irq)
  841. ret = -EINVAL;
  842. if (ser->io_type != UPIO_MEM)
  843. ret = -EINVAL;
  844. if (sport->port.uartclk / 16 != ser->baud_base)
  845. ret = -EINVAL;
  846. if ((void *)sport->port.mapbase != ser->iomem_base)
  847. ret = -EINVAL;
  848. if (sport->port.iobase != ser->port)
  849. ret = -EINVAL;
  850. if (ser->hub6 != 0)
  851. ret = -EINVAL;
  852. return ret;
  853. }
  854. static struct uart_ops imx_pops = {
  855. .tx_empty = imx_tx_empty,
  856. .set_mctrl = imx_set_mctrl,
  857. .get_mctrl = imx_get_mctrl,
  858. .stop_tx = imx_stop_tx,
  859. .start_tx = imx_start_tx,
  860. .stop_rx = imx_stop_rx,
  861. .enable_ms = imx_enable_ms,
  862. .break_ctl = imx_break_ctl,
  863. .startup = imx_startup,
  864. .shutdown = imx_shutdown,
  865. .set_termios = imx_set_termios,
  866. .type = imx_type,
  867. .release_port = imx_release_port,
  868. .request_port = imx_request_port,
  869. .config_port = imx_config_port,
  870. .verify_port = imx_verify_port,
  871. };
  872. static struct imx_port *imx_ports[UART_NR];
  873. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  874. static void imx_console_putchar(struct uart_port *port, int ch)
  875. {
  876. struct imx_port *sport = (struct imx_port *)port;
  877. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  878. barrier();
  879. writel(ch, sport->port.membase + URTX0);
  880. }
  881. /*
  882. * Interrupts are disabled on entering
  883. */
  884. static void
  885. imx_console_write(struct console *co, const char *s, unsigned int count)
  886. {
  887. struct imx_port *sport = imx_ports[co->index];
  888. unsigned int old_ucr1, old_ucr2, ucr1;
  889. /*
  890. * First, save UCR1/2 and then disable interrupts
  891. */
  892. ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
  893. old_ucr2 = readl(sport->port.membase + UCR2);
  894. if (cpu_is_mx1())
  895. ucr1 |= MX1_UCR1_UARTCLKEN;
  896. ucr1 |= UCR1_UARTEN;
  897. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  898. writel(ucr1, sport->port.membase + UCR1);
  899. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  900. uart_console_write(&sport->port, s, count, imx_console_putchar);
  901. /*
  902. * Finally, wait for transmitter to become empty
  903. * and restore UCR1/2
  904. */
  905. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  906. writel(old_ucr1, sport->port.membase + UCR1);
  907. writel(old_ucr2, sport->port.membase + UCR2);
  908. }
  909. /*
  910. * If the port was already initialised (eg, by a boot loader),
  911. * try to determine the current setup.
  912. */
  913. static void __init
  914. imx_console_get_options(struct imx_port *sport, int *baud,
  915. int *parity, int *bits)
  916. {
  917. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  918. /* ok, the port was enabled */
  919. unsigned int ucr2, ubir,ubmr, uartclk;
  920. unsigned int baud_raw;
  921. unsigned int ucfr_rfdiv;
  922. ucr2 = readl(sport->port.membase + UCR2);
  923. *parity = 'n';
  924. if (ucr2 & UCR2_PREN) {
  925. if (ucr2 & UCR2_PROE)
  926. *parity = 'o';
  927. else
  928. *parity = 'e';
  929. }
  930. if (ucr2 & UCR2_WS)
  931. *bits = 8;
  932. else
  933. *bits = 7;
  934. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  935. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  936. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  937. if (ucfr_rfdiv == 6)
  938. ucfr_rfdiv = 7;
  939. else
  940. ucfr_rfdiv = 6 - ucfr_rfdiv;
  941. uartclk = clk_get_rate(sport->clk);
  942. uartclk /= ucfr_rfdiv;
  943. { /*
  944. * The next code provides exact computation of
  945. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  946. * without need of float support or long long division,
  947. * which would be required to prevent 32bit arithmetic overflow
  948. */
  949. unsigned int mul = ubir + 1;
  950. unsigned int div = 16 * (ubmr + 1);
  951. unsigned int rem = uartclk % div;
  952. baud_raw = (uartclk / div) * mul;
  953. baud_raw += (rem * mul + div / 2) / div;
  954. *baud = (baud_raw + 50) / 100 * 100;
  955. }
  956. if(*baud != baud_raw)
  957. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  958. baud_raw, *baud);
  959. }
  960. }
  961. static int __init
  962. imx_console_setup(struct console *co, char *options)
  963. {
  964. struct imx_port *sport;
  965. int baud = 9600;
  966. int bits = 8;
  967. int parity = 'n';
  968. int flow = 'n';
  969. /*
  970. * Check whether an invalid uart number has been specified, and
  971. * if so, search for the first available port that does have
  972. * console support.
  973. */
  974. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  975. co->index = 0;
  976. sport = imx_ports[co->index];
  977. if(sport == NULL)
  978. return -ENODEV;
  979. if (options)
  980. uart_parse_options(options, &baud, &parity, &bits, &flow);
  981. else
  982. imx_console_get_options(sport, &baud, &parity, &bits);
  983. imx_setup_ufcr(sport, 0);
  984. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  985. }
  986. static struct uart_driver imx_reg;
  987. static struct console imx_console = {
  988. .name = DEV_NAME,
  989. .write = imx_console_write,
  990. .device = uart_console_device,
  991. .setup = imx_console_setup,
  992. .flags = CON_PRINTBUFFER,
  993. .index = -1,
  994. .data = &imx_reg,
  995. };
  996. #define IMX_CONSOLE &imx_console
  997. #else
  998. #define IMX_CONSOLE NULL
  999. #endif
  1000. static struct uart_driver imx_reg = {
  1001. .owner = THIS_MODULE,
  1002. .driver_name = DRIVER_NAME,
  1003. .dev_name = DEV_NAME,
  1004. .major = SERIAL_IMX_MAJOR,
  1005. .minor = MINOR_START,
  1006. .nr = ARRAY_SIZE(imx_ports),
  1007. .cons = IMX_CONSOLE,
  1008. };
  1009. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1010. {
  1011. struct imx_port *sport = platform_get_drvdata(dev);
  1012. if (sport)
  1013. uart_suspend_port(&imx_reg, &sport->port);
  1014. return 0;
  1015. }
  1016. static int serial_imx_resume(struct platform_device *dev)
  1017. {
  1018. struct imx_port *sport = platform_get_drvdata(dev);
  1019. if (sport)
  1020. uart_resume_port(&imx_reg, &sport->port);
  1021. return 0;
  1022. }
  1023. static int serial_imx_probe(struct platform_device *pdev)
  1024. {
  1025. struct imx_port *sport;
  1026. struct imxuart_platform_data *pdata;
  1027. void __iomem *base;
  1028. int ret = 0;
  1029. struct resource *res;
  1030. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1031. if (!sport)
  1032. return -ENOMEM;
  1033. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1034. if (!res) {
  1035. ret = -ENODEV;
  1036. goto free;
  1037. }
  1038. base = ioremap(res->start, PAGE_SIZE);
  1039. if (!base) {
  1040. ret = -ENOMEM;
  1041. goto free;
  1042. }
  1043. sport->port.dev = &pdev->dev;
  1044. sport->port.mapbase = res->start;
  1045. sport->port.membase = base;
  1046. sport->port.type = PORT_IMX,
  1047. sport->port.iotype = UPIO_MEM;
  1048. sport->port.irq = platform_get_irq(pdev, 0);
  1049. sport->rxirq = platform_get_irq(pdev, 0);
  1050. sport->txirq = platform_get_irq(pdev, 1);
  1051. sport->rtsirq = platform_get_irq(pdev, 2);
  1052. sport->port.fifosize = 32;
  1053. sport->port.ops = &imx_pops;
  1054. sport->port.flags = UPF_BOOT_AUTOCONF;
  1055. sport->port.line = pdev->id;
  1056. init_timer(&sport->timer);
  1057. sport->timer.function = imx_timeout;
  1058. sport->timer.data = (unsigned long)sport;
  1059. sport->clk = clk_get(&pdev->dev, "uart");
  1060. if (IS_ERR(sport->clk)) {
  1061. ret = PTR_ERR(sport->clk);
  1062. goto unmap;
  1063. }
  1064. clk_enable(sport->clk);
  1065. sport->port.uartclk = clk_get_rate(sport->clk);
  1066. imx_ports[pdev->id] = sport;
  1067. pdata = pdev->dev.platform_data;
  1068. if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  1069. sport->have_rtscts = 1;
  1070. #ifdef CONFIG_IRDA
  1071. if (pdata && (pdata->flags & IMXUART_IRDA))
  1072. sport->use_irda = 1;
  1073. #endif
  1074. if (pdata && pdata->init) {
  1075. ret = pdata->init(pdev);
  1076. if (ret)
  1077. goto clkput;
  1078. }
  1079. ret = uart_add_one_port(&imx_reg, &sport->port);
  1080. if (ret)
  1081. goto deinit;
  1082. platform_set_drvdata(pdev, &sport->port);
  1083. return 0;
  1084. deinit:
  1085. if (pdata && pdata->exit)
  1086. pdata->exit(pdev);
  1087. clkput:
  1088. clk_put(sport->clk);
  1089. clk_disable(sport->clk);
  1090. unmap:
  1091. iounmap(sport->port.membase);
  1092. free:
  1093. kfree(sport);
  1094. return ret;
  1095. }
  1096. static int serial_imx_remove(struct platform_device *pdev)
  1097. {
  1098. struct imxuart_platform_data *pdata;
  1099. struct imx_port *sport = platform_get_drvdata(pdev);
  1100. pdata = pdev->dev.platform_data;
  1101. platform_set_drvdata(pdev, NULL);
  1102. if (sport) {
  1103. uart_remove_one_port(&imx_reg, &sport->port);
  1104. clk_put(sport->clk);
  1105. }
  1106. clk_disable(sport->clk);
  1107. if (pdata && pdata->exit)
  1108. pdata->exit(pdev);
  1109. iounmap(sport->port.membase);
  1110. kfree(sport);
  1111. return 0;
  1112. }
  1113. static struct platform_driver serial_imx_driver = {
  1114. .probe = serial_imx_probe,
  1115. .remove = serial_imx_remove,
  1116. .suspend = serial_imx_suspend,
  1117. .resume = serial_imx_resume,
  1118. .driver = {
  1119. .name = "imx-uart",
  1120. .owner = THIS_MODULE,
  1121. },
  1122. };
  1123. static int __init imx_serial_init(void)
  1124. {
  1125. int ret;
  1126. printk(KERN_INFO "Serial: IMX driver\n");
  1127. ret = uart_register_driver(&imx_reg);
  1128. if (ret)
  1129. return ret;
  1130. ret = platform_driver_register(&serial_imx_driver);
  1131. if (ret != 0)
  1132. uart_unregister_driver(&imx_reg);
  1133. return 0;
  1134. }
  1135. static void __exit imx_serial_exit(void)
  1136. {
  1137. platform_driver_unregister(&serial_imx_driver);
  1138. uart_unregister_driver(&imx_reg);
  1139. }
  1140. module_init(imx_serial_init);
  1141. module_exit(imx_serial_exit);
  1142. MODULE_AUTHOR("Sascha Hauer");
  1143. MODULE_DESCRIPTION("IMX generic serial port driver");
  1144. MODULE_LICENSE("GPL");
  1145. MODULE_ALIAS("platform:imx-uart");