imx.c 30 KB

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