imx.c 36 KB

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