imx.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. * Driver for Motorola IMX serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Author: Sascha Hauer <sascha@saschahauer.de>
  7. * Copyright (C) 2004 Pengutronix
  8. *
  9. * Copyright (C) 2009 emlix GmbH
  10. * Author: Fabian Godehardt (added IrDA support for iMX)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * [29-Mar-2005] Mike Lee
  27. * Added hardware handshake
  28. */
  29. #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  30. #define SUPPORT_SYSRQ
  31. #endif
  32. #include <linux/module.h>
  33. #include <linux/ioport.h>
  34. #include <linux/init.h>
  35. #include <linux/console.h>
  36. #include <linux/sysrq.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/tty.h>
  39. #include <linux/tty_flip.h>
  40. #include <linux/serial_core.h>
  41. #include <linux/serial.h>
  42. #include <linux/clk.h>
  43. #include <linux/delay.h>
  44. #include <linux/rational.h>
  45. #include <linux/slab.h>
  46. #include <asm/io.h>
  47. #include <asm/irq.h>
  48. #include <mach/hardware.h>
  49. #include <mach/imx-uart.h>
  50. /* Register definitions */
  51. #define URXD0 0x0 /* Receiver Register */
  52. #define URTX0 0x40 /* Transmitter Register */
  53. #define UCR1 0x80 /* Control Register 1 */
  54. #define UCR2 0x84 /* Control Register 2 */
  55. #define UCR3 0x88 /* Control Register 3 */
  56. #define UCR4 0x8c /* Control Register 4 */
  57. #define UFCR 0x90 /* FIFO Control Register */
  58. #define USR1 0x94 /* Status Register 1 */
  59. #define USR2 0x98 /* Status Register 2 */
  60. #define UESC 0x9c /* Escape Character Register */
  61. #define UTIM 0xa0 /* Escape Timer Register */
  62. #define UBIR 0xa4 /* BRM Incremental Register */
  63. #define UBMR 0xa8 /* BRM Modulator Register */
  64. #define UBRC 0xac /* Baud Rate Count Register */
  65. #define MX2_ONEMS 0xb0 /* One Millisecond register */
  66. #define UTS (cpu_is_mx1() ? 0xd0 : 0xb4) /* UART Test Register */
  67. /* UART Control Register Bit Fields.*/
  68. #define URXD_CHARRDY (1<<15)
  69. #define URXD_ERR (1<<14)
  70. #define URXD_OVRRUN (1<<13)
  71. #define URXD_FRMERR (1<<12)
  72. #define URXD_BRK (1<<11)
  73. #define URXD_PRERR (1<<10)
  74. #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
  75. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  76. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  77. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  78. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  79. #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
  80. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  81. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  82. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  83. #define UCR1_SNDBRK (1<<4) /* Send break */
  84. #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
  85. #define MX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, mx1 only */
  86. #define UCR1_DOZE (1<<1) /* Doze */
  87. #define UCR1_UARTEN (1<<0) /* UART enabled */
  88. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  89. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  90. #define UCR2_CTSC (1<<13) /* CTS pin control */
  91. #define UCR2_CTS (1<<12) /* Clear to send */
  92. #define UCR2_ESCEN (1<<11) /* Escape enable */
  93. #define UCR2_PREN (1<<8) /* Parity enable */
  94. #define UCR2_PROE (1<<7) /* Parity odd/even */
  95. #define UCR2_STPB (1<<6) /* Stop */
  96. #define UCR2_WS (1<<5) /* Word size */
  97. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  98. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  99. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  100. #define UCR2_SRST (1<<0) /* SW reset */
  101. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  102. #define UCR3_PARERREN (1<<12) /* Parity enable */
  103. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  104. #define UCR3_DSR (1<<10) /* Data set ready */
  105. #define UCR3_DCD (1<<9) /* Data carrier detect */
  106. #define UCR3_RI (1<<8) /* Ring indicator */
  107. #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
  108. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  109. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  110. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  111. #define MX1_UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
  112. #define MX1_UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
  113. #define MX2_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
  114. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  115. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  116. #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
  117. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  118. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  119. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  120. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  121. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  122. #define UCR4_IRSC (1<<5) /* IR special case */
  123. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  124. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  125. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  126. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  127. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  128. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  129. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  130. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  131. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  132. #define USR1_RTSS (1<<14) /* RTS pin status */
  133. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  134. #define USR1_RTSD (1<<12) /* RTS delta */
  135. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  136. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  137. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  138. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  139. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  140. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  141. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  142. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  143. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  144. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  145. #define USR2_IDLE (1<<12) /* Idle condition */
  146. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  147. #define USR2_WAKE (1<<7) /* Wake */
  148. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  149. #define USR2_TXDC (1<<3) /* Transmitter complete */
  150. #define USR2_BRCD (1<<2) /* Break condition */
  151. #define USR2_ORE (1<<1) /* Overrun error */
  152. #define USR2_RDR (1<<0) /* Recv data ready */
  153. #define UTS_FRCPERR (1<<13) /* Force parity error */
  154. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  155. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  156. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  157. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  158. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  159. #define UTS_SOFTRST (1<<0) /* Software reset */
  160. /* We've been assigned a range on the "Low-density serial ports" major */
  161. #define SERIAL_IMX_MAJOR 207
  162. #define MINOR_START 16
  163. #define DEV_NAME "ttymxc"
  164. #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
  165. /*
  166. * This determines how often we check the modem status signals
  167. * for any change. They generally aren't connected to an IRQ
  168. * so we have to poll them. We also check immediately before
  169. * filling the TX fifo incase CTS has been dropped.
  170. */
  171. #define MCTRL_TIMEOUT (250*HZ/1000)
  172. #define DRIVER_NAME "IMX-uart"
  173. #define UART_NR 8
  174. 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 (!uart_circ_empty(xmit) &&
  295. !(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
  296. /* send xmit->buf[xmit->tail]
  297. * out the port here */
  298. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  299. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  300. sport->port.icount.tx++;
  301. }
  302. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  303. uart_write_wakeup(&sport->port);
  304. if (uart_circ_empty(xmit))
  305. imx_stop_tx(&sport->port);
  306. }
  307. /*
  308. * interrupts disabled on entry
  309. */
  310. static void imx_start_tx(struct uart_port *port)
  311. {
  312. struct imx_port *sport = (struct imx_port *)port;
  313. unsigned long temp;
  314. if (USE_IRDA(sport)) {
  315. /* half duplex in IrDA mode; have to disable receive mode */
  316. temp = readl(sport->port.membase + UCR4);
  317. temp &= ~(UCR4_DREN);
  318. writel(temp, sport->port.membase + UCR4);
  319. temp = readl(sport->port.membase + UCR1);
  320. temp &= ~(UCR1_RRDYEN);
  321. writel(temp, sport->port.membase + UCR1);
  322. }
  323. temp = readl(sport->port.membase + UCR1);
  324. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  325. if (USE_IRDA(sport)) {
  326. temp = readl(sport->port.membase + UCR1);
  327. temp |= UCR1_TRDYEN;
  328. writel(temp, sport->port.membase + UCR1);
  329. temp = readl(sport->port.membase + UCR4);
  330. temp |= UCR4_TCEN;
  331. writel(temp, sport->port.membase + UCR4);
  332. }
  333. if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
  334. imx_transmit_buffer(sport);
  335. }
  336. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  337. {
  338. struct imx_port *sport = dev_id;
  339. unsigned int val;
  340. unsigned long flags;
  341. spin_lock_irqsave(&sport->port.lock, flags);
  342. writel(USR1_RTSD, sport->port.membase + USR1);
  343. val = readl(sport->port.membase + USR1) & USR1_RTSS;
  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(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. /* half the RX buffer size */
  498. #define CTSTL 16
  499. static int imx_startup(struct uart_port *port)
  500. {
  501. struct imx_port *sport = (struct imx_port *)port;
  502. int retval;
  503. unsigned long flags, temp;
  504. imx_setup_ufcr(sport, 0);
  505. /* disable the DREN bit (Data Ready interrupt enable) before
  506. * requesting IRQs
  507. */
  508. temp = readl(sport->port.membase + UCR4);
  509. if (USE_IRDA(sport))
  510. temp |= UCR4_IRSC;
  511. /* set the trigger level for CTS */
  512. temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
  513. temp |= CTSTL<< UCR4_CTSTL_SHF;
  514. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  515. if (USE_IRDA(sport)) {
  516. /* reset fifo's and state machines */
  517. int i = 100;
  518. temp = readl(sport->port.membase + UCR2);
  519. temp &= ~UCR2_SRST;
  520. writel(temp, sport->port.membase + UCR2);
  521. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  522. (--i > 0)) {
  523. udelay(1);
  524. }
  525. }
  526. /*
  527. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  528. * chips only have one interrupt.
  529. */
  530. if (sport->txirq > 0) {
  531. retval = request_irq(sport->rxirq, imx_rxint, 0,
  532. DRIVER_NAME, sport);
  533. if (retval)
  534. goto error_out1;
  535. retval = request_irq(sport->txirq, imx_txint, 0,
  536. DRIVER_NAME, sport);
  537. if (retval)
  538. goto error_out2;
  539. /* do not use RTS IRQ on IrDA */
  540. if (!USE_IRDA(sport)) {
  541. retval = request_irq(sport->rtsirq, imx_rtsint,
  542. (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
  543. IRQF_TRIGGER_FALLING |
  544. IRQF_TRIGGER_RISING,
  545. DRIVER_NAME, sport);
  546. if (retval)
  547. goto error_out3;
  548. }
  549. } else {
  550. retval = request_irq(sport->port.irq, imx_int, 0,
  551. DRIVER_NAME, sport);
  552. if (retval) {
  553. free_irq(sport->port.irq, sport);
  554. goto error_out1;
  555. }
  556. }
  557. /*
  558. * Finally, clear and enable interrupts
  559. */
  560. writel(USR1_RTSD, sport->port.membase + USR1);
  561. temp = readl(sport->port.membase + UCR1);
  562. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  563. if (USE_IRDA(sport)) {
  564. temp |= UCR1_IREN;
  565. temp &= ~(UCR1_RTSDEN);
  566. }
  567. writel(temp, sport->port.membase + UCR1);
  568. temp = readl(sport->port.membase + UCR2);
  569. temp |= (UCR2_RXEN | UCR2_TXEN);
  570. writel(temp, sport->port.membase + UCR2);
  571. if (USE_IRDA(sport)) {
  572. /* clear RX-FIFO */
  573. int i = 64;
  574. while ((--i > 0) &&
  575. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  576. barrier();
  577. }
  578. }
  579. if (!cpu_is_mx1()) {
  580. temp = readl(sport->port.membase + UCR3);
  581. temp |= MX2_UCR3_RXDMUXSEL;
  582. writel(temp, sport->port.membase + UCR3);
  583. }
  584. if (USE_IRDA(sport)) {
  585. temp = readl(sport->port.membase + UCR4);
  586. if (sport->irda_inv_rx)
  587. temp |= UCR4_INVR;
  588. else
  589. temp &= ~(UCR4_INVR);
  590. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  591. temp = readl(sport->port.membase + UCR3);
  592. if (sport->irda_inv_tx)
  593. temp |= UCR3_INVT;
  594. else
  595. temp &= ~(UCR3_INVT);
  596. writel(temp, sport->port.membase + UCR3);
  597. }
  598. /*
  599. * Enable modem status interrupts
  600. */
  601. spin_lock_irqsave(&sport->port.lock,flags);
  602. imx_enable_ms(&sport->port);
  603. spin_unlock_irqrestore(&sport->port.lock,flags);
  604. if (USE_IRDA(sport)) {
  605. struct imxuart_platform_data *pdata;
  606. pdata = sport->port.dev->platform_data;
  607. sport->irda_inv_rx = pdata->irda_inv_rx;
  608. sport->irda_inv_tx = pdata->irda_inv_tx;
  609. sport->trcv_delay = pdata->transceiver_delay;
  610. if (pdata->irda_enable)
  611. pdata->irda_enable(1);
  612. }
  613. return 0;
  614. error_out3:
  615. if (sport->txirq)
  616. free_irq(sport->txirq, sport);
  617. error_out2:
  618. if (sport->rxirq)
  619. free_irq(sport->rxirq, sport);
  620. error_out1:
  621. return retval;
  622. }
  623. static void imx_shutdown(struct uart_port *port)
  624. {
  625. struct imx_port *sport = (struct imx_port *)port;
  626. unsigned long temp;
  627. temp = readl(sport->port.membase + UCR2);
  628. temp &= ~(UCR2_TXEN);
  629. writel(temp, sport->port.membase + UCR2);
  630. if (USE_IRDA(sport)) {
  631. struct imxuart_platform_data *pdata;
  632. pdata = sport->port.dev->platform_data;
  633. if (pdata->irda_enable)
  634. pdata->irda_enable(0);
  635. }
  636. /*
  637. * Stop our timer.
  638. */
  639. del_timer_sync(&sport->timer);
  640. /*
  641. * Free the interrupts
  642. */
  643. if (sport->txirq > 0) {
  644. if (!USE_IRDA(sport))
  645. free_irq(sport->rtsirq, sport);
  646. free_irq(sport->txirq, sport);
  647. free_irq(sport->rxirq, sport);
  648. } else
  649. free_irq(sport->port.irq, sport);
  650. /*
  651. * Disable all interrupts, port and break condition.
  652. */
  653. temp = readl(sport->port.membase + UCR1);
  654. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  655. if (USE_IRDA(sport))
  656. temp &= ~(UCR1_IREN);
  657. writel(temp, sport->port.membase + UCR1);
  658. }
  659. static void
  660. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  661. struct ktermios *old)
  662. {
  663. struct imx_port *sport = (struct imx_port *)port;
  664. unsigned long flags;
  665. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  666. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  667. unsigned int div, ufcr;
  668. unsigned long num, denom;
  669. uint64_t tdiv64;
  670. /*
  671. * If we don't support modem control lines, don't allow
  672. * these to be set.
  673. */
  674. if (0) {
  675. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  676. termios->c_cflag |= CLOCAL;
  677. }
  678. /*
  679. * We only support CS7 and CS8.
  680. */
  681. while ((termios->c_cflag & CSIZE) != CS7 &&
  682. (termios->c_cflag & CSIZE) != CS8) {
  683. termios->c_cflag &= ~CSIZE;
  684. termios->c_cflag |= old_csize;
  685. old_csize = CS8;
  686. }
  687. if ((termios->c_cflag & CSIZE) == CS8)
  688. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  689. else
  690. ucr2 = UCR2_SRST | UCR2_IRTS;
  691. if (termios->c_cflag & CRTSCTS) {
  692. if( sport->have_rtscts ) {
  693. ucr2 &= ~UCR2_IRTS;
  694. ucr2 |= UCR2_CTSC;
  695. } else {
  696. termios->c_cflag &= ~CRTSCTS;
  697. }
  698. }
  699. if (termios->c_cflag & CSTOPB)
  700. ucr2 |= UCR2_STPB;
  701. if (termios->c_cflag & PARENB) {
  702. ucr2 |= UCR2_PREN;
  703. if (termios->c_cflag & PARODD)
  704. ucr2 |= UCR2_PROE;
  705. }
  706. /*
  707. * Ask the core to calculate the divisor for us.
  708. */
  709. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  710. quot = uart_get_divisor(port, baud);
  711. spin_lock_irqsave(&sport->port.lock, flags);
  712. sport->port.read_status_mask = 0;
  713. if (termios->c_iflag & INPCK)
  714. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  715. if (termios->c_iflag & (BRKINT | PARMRK))
  716. sport->port.read_status_mask |= URXD_BRK;
  717. /*
  718. * Characters to ignore
  719. */
  720. sport->port.ignore_status_mask = 0;
  721. if (termios->c_iflag & IGNPAR)
  722. sport->port.ignore_status_mask |= URXD_PRERR;
  723. if (termios->c_iflag & IGNBRK) {
  724. sport->port.ignore_status_mask |= URXD_BRK;
  725. /*
  726. * If we're ignoring parity and break indicators,
  727. * ignore overruns too (for real raw support).
  728. */
  729. if (termios->c_iflag & IGNPAR)
  730. sport->port.ignore_status_mask |= URXD_OVRRUN;
  731. }
  732. del_timer_sync(&sport->timer);
  733. /*
  734. * Update the per-port timeout.
  735. */
  736. uart_update_timeout(port, termios->c_cflag, baud);
  737. /*
  738. * disable interrupts and drain transmitter
  739. */
  740. old_ucr1 = readl(sport->port.membase + UCR1);
  741. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  742. sport->port.membase + UCR1);
  743. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  744. barrier();
  745. /* then, disable everything */
  746. old_txrxen = readl(sport->port.membase + UCR2);
  747. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  748. sport->port.membase + UCR2);
  749. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  750. if (USE_IRDA(sport)) {
  751. /*
  752. * use maximum available submodule frequency to
  753. * avoid missing short pulses due to low sampling rate
  754. */
  755. div = 1;
  756. } else {
  757. div = sport->port.uartclk / (baud * 16);
  758. if (div > 7)
  759. div = 7;
  760. if (!div)
  761. div = 1;
  762. }
  763. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  764. 1 << 16, 1 << 16, &num, &denom);
  765. tdiv64 = sport->port.uartclk;
  766. tdiv64 *= num;
  767. do_div(tdiv64, denom * 16 * div);
  768. tty_termios_encode_baud_rate(termios,
  769. (speed_t)tdiv64, (speed_t)tdiv64);
  770. num -= 1;
  771. denom -= 1;
  772. ufcr = readl(sport->port.membase + UFCR);
  773. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  774. writel(ufcr, sport->port.membase + UFCR);
  775. writel(num, sport->port.membase + UBIR);
  776. writel(denom, sport->port.membase + UBMR);
  777. if (!cpu_is_mx1())
  778. writel(sport->port.uartclk / div / 1000,
  779. sport->port.membase + MX2_ONEMS);
  780. writel(old_ucr1, sport->port.membase + UCR1);
  781. /* set the parity, stop bits and data size */
  782. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  783. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  784. imx_enable_ms(&sport->port);
  785. spin_unlock_irqrestore(&sport->port.lock, flags);
  786. }
  787. static const char *imx_type(struct uart_port *port)
  788. {
  789. struct imx_port *sport = (struct imx_port *)port;
  790. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  791. }
  792. /*
  793. * Release the memory region(s) being used by 'port'.
  794. */
  795. static void imx_release_port(struct uart_port *port)
  796. {
  797. struct platform_device *pdev = to_platform_device(port->dev);
  798. struct resource *mmres;
  799. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  800. release_mem_region(mmres->start, mmres->end - mmres->start + 1);
  801. }
  802. /*
  803. * Request the memory region(s) being used by 'port'.
  804. */
  805. static int imx_request_port(struct uart_port *port)
  806. {
  807. struct platform_device *pdev = to_platform_device(port->dev);
  808. struct resource *mmres;
  809. void *ret;
  810. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  811. if (!mmres)
  812. return -ENODEV;
  813. ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
  814. "imx-uart");
  815. return ret ? 0 : -EBUSY;
  816. }
  817. /*
  818. * Configure/autoconfigure the port.
  819. */
  820. static void imx_config_port(struct uart_port *port, int flags)
  821. {
  822. struct imx_port *sport = (struct imx_port *)port;
  823. if (flags & UART_CONFIG_TYPE &&
  824. imx_request_port(&sport->port) == 0)
  825. sport->port.type = PORT_IMX;
  826. }
  827. /*
  828. * Verify the new serial_struct (for TIOCSSERIAL).
  829. * The only change we allow are to the flags and type, and
  830. * even then only between PORT_IMX and PORT_UNKNOWN
  831. */
  832. static int
  833. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  834. {
  835. struct imx_port *sport = (struct imx_port *)port;
  836. int ret = 0;
  837. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  838. ret = -EINVAL;
  839. if (sport->port.irq != ser->irq)
  840. ret = -EINVAL;
  841. if (ser->io_type != UPIO_MEM)
  842. ret = -EINVAL;
  843. if (sport->port.uartclk / 16 != ser->baud_base)
  844. ret = -EINVAL;
  845. if ((void *)sport->port.mapbase != ser->iomem_base)
  846. ret = -EINVAL;
  847. if (sport->port.iobase != ser->port)
  848. ret = -EINVAL;
  849. if (ser->hub6 != 0)
  850. ret = -EINVAL;
  851. return ret;
  852. }
  853. static struct uart_ops imx_pops = {
  854. .tx_empty = imx_tx_empty,
  855. .set_mctrl = imx_set_mctrl,
  856. .get_mctrl = imx_get_mctrl,
  857. .stop_tx = imx_stop_tx,
  858. .start_tx = imx_start_tx,
  859. .stop_rx = imx_stop_rx,
  860. .enable_ms = imx_enable_ms,
  861. .break_ctl = imx_break_ctl,
  862. .startup = imx_startup,
  863. .shutdown = imx_shutdown,
  864. .set_termios = imx_set_termios,
  865. .type = imx_type,
  866. .release_port = imx_release_port,
  867. .request_port = imx_request_port,
  868. .config_port = imx_config_port,
  869. .verify_port = imx_verify_port,
  870. };
  871. static struct imx_port *imx_ports[UART_NR];
  872. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  873. static void imx_console_putchar(struct uart_port *port, int ch)
  874. {
  875. struct imx_port *sport = (struct imx_port *)port;
  876. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  877. barrier();
  878. writel(ch, sport->port.membase + URTX0);
  879. }
  880. /*
  881. * Interrupts are disabled on entering
  882. */
  883. static void
  884. imx_console_write(struct console *co, const char *s, unsigned int count)
  885. {
  886. struct imx_port *sport = imx_ports[co->index];
  887. unsigned int old_ucr1, old_ucr2, ucr1;
  888. /*
  889. * First, save UCR1/2 and then disable interrupts
  890. */
  891. ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
  892. old_ucr2 = readl(sport->port.membase + UCR2);
  893. if (cpu_is_mx1())
  894. ucr1 |= MX1_UCR1_UARTCLKEN;
  895. ucr1 |= UCR1_UARTEN;
  896. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  897. writel(ucr1, sport->port.membase + UCR1);
  898. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  899. uart_console_write(&sport->port, s, count, imx_console_putchar);
  900. /*
  901. * Finally, wait for transmitter to become empty
  902. * and restore UCR1/2
  903. */
  904. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  905. writel(old_ucr1, sport->port.membase + UCR1);
  906. writel(old_ucr2, sport->port.membase + UCR2);
  907. }
  908. /*
  909. * If the port was already initialised (eg, by a boot loader),
  910. * try to determine the current setup.
  911. */
  912. static void __init
  913. imx_console_get_options(struct imx_port *sport, int *baud,
  914. int *parity, int *bits)
  915. {
  916. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  917. /* ok, the port was enabled */
  918. unsigned int ucr2, ubir,ubmr, uartclk;
  919. unsigned int baud_raw;
  920. unsigned int ucfr_rfdiv;
  921. ucr2 = readl(sport->port.membase + UCR2);
  922. *parity = 'n';
  923. if (ucr2 & UCR2_PREN) {
  924. if (ucr2 & UCR2_PROE)
  925. *parity = 'o';
  926. else
  927. *parity = 'e';
  928. }
  929. if (ucr2 & UCR2_WS)
  930. *bits = 8;
  931. else
  932. *bits = 7;
  933. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  934. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  935. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  936. if (ucfr_rfdiv == 6)
  937. ucfr_rfdiv = 7;
  938. else
  939. ucfr_rfdiv = 6 - ucfr_rfdiv;
  940. uartclk = clk_get_rate(sport->clk);
  941. uartclk /= ucfr_rfdiv;
  942. { /*
  943. * The next code provides exact computation of
  944. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  945. * without need of float support or long long division,
  946. * which would be required to prevent 32bit arithmetic overflow
  947. */
  948. unsigned int mul = ubir + 1;
  949. unsigned int div = 16 * (ubmr + 1);
  950. unsigned int rem = uartclk % div;
  951. baud_raw = (uartclk / div) * mul;
  952. baud_raw += (rem * mul + div / 2) / div;
  953. *baud = (baud_raw + 50) / 100 * 100;
  954. }
  955. if(*baud != baud_raw)
  956. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  957. baud_raw, *baud);
  958. }
  959. }
  960. static int __init
  961. imx_console_setup(struct console *co, char *options)
  962. {
  963. struct imx_port *sport;
  964. int baud = 9600;
  965. int bits = 8;
  966. int parity = 'n';
  967. int flow = 'n';
  968. /*
  969. * Check whether an invalid uart number has been specified, and
  970. * if so, search for the first available port that does have
  971. * console support.
  972. */
  973. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  974. co->index = 0;
  975. sport = imx_ports[co->index];
  976. if(sport == NULL)
  977. return -ENODEV;
  978. if (options)
  979. uart_parse_options(options, &baud, &parity, &bits, &flow);
  980. else
  981. imx_console_get_options(sport, &baud, &parity, &bits);
  982. imx_setup_ufcr(sport, 0);
  983. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  984. }
  985. static struct uart_driver imx_reg;
  986. static struct console imx_console = {
  987. .name = DEV_NAME,
  988. .write = imx_console_write,
  989. .device = uart_console_device,
  990. .setup = imx_console_setup,
  991. .flags = CON_PRINTBUFFER,
  992. .index = -1,
  993. .data = &imx_reg,
  994. };
  995. #define IMX_CONSOLE &imx_console
  996. #else
  997. #define IMX_CONSOLE NULL
  998. #endif
  999. static struct uart_driver imx_reg = {
  1000. .owner = THIS_MODULE,
  1001. .driver_name = DRIVER_NAME,
  1002. .dev_name = DEV_NAME,
  1003. .major = SERIAL_IMX_MAJOR,
  1004. .minor = MINOR_START,
  1005. .nr = ARRAY_SIZE(imx_ports),
  1006. .cons = IMX_CONSOLE,
  1007. };
  1008. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1009. {
  1010. struct imx_port *sport = platform_get_drvdata(dev);
  1011. if (sport)
  1012. uart_suspend_port(&imx_reg, &sport->port);
  1013. return 0;
  1014. }
  1015. static int serial_imx_resume(struct platform_device *dev)
  1016. {
  1017. struct imx_port *sport = platform_get_drvdata(dev);
  1018. if (sport)
  1019. uart_resume_port(&imx_reg, &sport->port);
  1020. return 0;
  1021. }
  1022. static int serial_imx_probe(struct platform_device *pdev)
  1023. {
  1024. struct imx_port *sport;
  1025. struct imxuart_platform_data *pdata;
  1026. void __iomem *base;
  1027. int ret = 0;
  1028. struct resource *res;
  1029. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1030. if (!sport)
  1031. return -ENOMEM;
  1032. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1033. if (!res) {
  1034. ret = -ENODEV;
  1035. goto free;
  1036. }
  1037. base = ioremap(res->start, PAGE_SIZE);
  1038. if (!base) {
  1039. ret = -ENOMEM;
  1040. goto free;
  1041. }
  1042. sport->port.dev = &pdev->dev;
  1043. sport->port.mapbase = res->start;
  1044. sport->port.membase = base;
  1045. sport->port.type = PORT_IMX,
  1046. sport->port.iotype = UPIO_MEM;
  1047. sport->port.irq = platform_get_irq(pdev, 0);
  1048. sport->rxirq = platform_get_irq(pdev, 0);
  1049. sport->txirq = platform_get_irq(pdev, 1);
  1050. sport->rtsirq = platform_get_irq(pdev, 2);
  1051. sport->port.fifosize = 32;
  1052. sport->port.ops = &imx_pops;
  1053. sport->port.flags = UPF_BOOT_AUTOCONF;
  1054. sport->port.line = pdev->id;
  1055. init_timer(&sport->timer);
  1056. sport->timer.function = imx_timeout;
  1057. sport->timer.data = (unsigned long)sport;
  1058. sport->clk = clk_get(&pdev->dev, "uart");
  1059. if (IS_ERR(sport->clk)) {
  1060. ret = PTR_ERR(sport->clk);
  1061. goto unmap;
  1062. }
  1063. clk_enable(sport->clk);
  1064. sport->port.uartclk = clk_get_rate(sport->clk);
  1065. imx_ports[pdev->id] = sport;
  1066. pdata = pdev->dev.platform_data;
  1067. if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  1068. sport->have_rtscts = 1;
  1069. #ifdef CONFIG_IRDA
  1070. if (pdata && (pdata->flags & IMXUART_IRDA))
  1071. sport->use_irda = 1;
  1072. #endif
  1073. if (pdata && pdata->init) {
  1074. ret = pdata->init(pdev);
  1075. if (ret)
  1076. goto clkput;
  1077. }
  1078. ret = uart_add_one_port(&imx_reg, &sport->port);
  1079. if (ret)
  1080. goto deinit;
  1081. platform_set_drvdata(pdev, &sport->port);
  1082. return 0;
  1083. deinit:
  1084. if (pdata && pdata->exit)
  1085. pdata->exit(pdev);
  1086. clkput:
  1087. clk_put(sport->clk);
  1088. clk_disable(sport->clk);
  1089. unmap:
  1090. iounmap(sport->port.membase);
  1091. free:
  1092. kfree(sport);
  1093. return ret;
  1094. }
  1095. static int serial_imx_remove(struct platform_device *pdev)
  1096. {
  1097. struct imxuart_platform_data *pdata;
  1098. struct imx_port *sport = platform_get_drvdata(pdev);
  1099. pdata = pdev->dev.platform_data;
  1100. platform_set_drvdata(pdev, NULL);
  1101. if (sport) {
  1102. uart_remove_one_port(&imx_reg, &sport->port);
  1103. clk_put(sport->clk);
  1104. }
  1105. clk_disable(sport->clk);
  1106. if (pdata && pdata->exit)
  1107. pdata->exit(pdev);
  1108. iounmap(sport->port.membase);
  1109. kfree(sport);
  1110. return 0;
  1111. }
  1112. static struct platform_driver serial_imx_driver = {
  1113. .probe = serial_imx_probe,
  1114. .remove = serial_imx_remove,
  1115. .suspend = serial_imx_suspend,
  1116. .resume = serial_imx_resume,
  1117. .driver = {
  1118. .name = "imx-uart",
  1119. .owner = THIS_MODULE,
  1120. },
  1121. };
  1122. static int __init imx_serial_init(void)
  1123. {
  1124. int ret;
  1125. printk(KERN_INFO "Serial: IMX driver\n");
  1126. ret = uart_register_driver(&imx_reg);
  1127. if (ret)
  1128. return ret;
  1129. ret = platform_driver_register(&serial_imx_driver);
  1130. if (ret != 0)
  1131. uart_unregister_driver(&imx_reg);
  1132. return 0;
  1133. }
  1134. static void __exit imx_serial_exit(void)
  1135. {
  1136. platform_driver_unregister(&serial_imx_driver);
  1137. uart_unregister_driver(&imx_reg);
  1138. }
  1139. module_init(imx_serial_init);
  1140. module_exit(imx_serial_exit);
  1141. MODULE_AUTHOR("Sascha Hauer");
  1142. MODULE_DESCRIPTION("IMX generic serial port driver");
  1143. MODULE_LICENSE("GPL");
  1144. MODULE_ALIAS("platform:imx-uart");