imx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. rx = readl(sport->port.membase + URXD0);
  315. spin_lock_irqsave(&sport->port.lock,flags);
  316. do {
  317. flg = TTY_NORMAL;
  318. sport->port.icount.rx++;
  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. goto ignore_char;
  324. }
  325. if (uart_handle_sysrq_char
  326. (&sport->port, (unsigned char)rx))
  327. goto ignore_char;
  328. if( rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) )
  329. goto handle_error;
  330. error_return:
  331. tty_insert_flip_char(tty, rx, flg);
  332. ignore_char:
  333. rx = readl(sport->port.membase + URXD0);
  334. } while(rx & URXD_CHARRDY);
  335. out:
  336. spin_unlock_irqrestore(&sport->port.lock,flags);
  337. tty_flip_buffer_push(tty);
  338. return IRQ_HANDLED;
  339. handle_error:
  340. if (rx & URXD_PRERR)
  341. sport->port.icount.parity++;
  342. else if (rx & URXD_FRMERR)
  343. sport->port.icount.frame++;
  344. if (rx & URXD_OVRRUN)
  345. sport->port.icount.overrun++;
  346. if (rx & sport->port.ignore_status_mask) {
  347. if (++ignored > 100)
  348. goto out;
  349. goto ignore_char;
  350. }
  351. rx &= sport->port.read_status_mask;
  352. if (rx & URXD_PRERR)
  353. flg = TTY_PARITY;
  354. else if (rx & URXD_FRMERR)
  355. flg = TTY_FRAME;
  356. if (rx & URXD_OVRRUN)
  357. flg = TTY_OVERRUN;
  358. #ifdef SUPPORT_SYSRQ
  359. sport->port.sysrq = 0;
  360. #endif
  361. goto error_return;
  362. }
  363. /*
  364. * Return TIOCSER_TEMT when transmitter is not busy.
  365. */
  366. static unsigned int imx_tx_empty(struct uart_port *port)
  367. {
  368. struct imx_port *sport = (struct imx_port *)port;
  369. return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  370. }
  371. /*
  372. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  373. */
  374. static unsigned int imx_get_mctrl(struct uart_port *port)
  375. {
  376. struct imx_port *sport = (struct imx_port *)port;
  377. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  378. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  379. tmp |= TIOCM_CTS;
  380. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  381. tmp |= TIOCM_RTS;
  382. return tmp;
  383. }
  384. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  385. {
  386. struct imx_port *sport = (struct imx_port *)port;
  387. unsigned long temp;
  388. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  389. if (mctrl & TIOCM_RTS)
  390. temp |= UCR2_CTS;
  391. writel(temp, sport->port.membase + UCR2);
  392. }
  393. /*
  394. * Interrupts always disabled.
  395. */
  396. static void imx_break_ctl(struct uart_port *port, int break_state)
  397. {
  398. struct imx_port *sport = (struct imx_port *)port;
  399. unsigned long flags, temp;
  400. spin_lock_irqsave(&sport->port.lock, flags);
  401. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  402. if ( break_state != 0 )
  403. temp |= UCR1_SNDBRK;
  404. writel(temp, sport->port.membase + UCR1);
  405. spin_unlock_irqrestore(&sport->port.lock, flags);
  406. }
  407. #define TXTL 2 /* reset default */
  408. #define RXTL 1 /* reset default */
  409. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  410. {
  411. unsigned int val;
  412. unsigned int ufcr_rfdiv;
  413. /* set receiver / transmitter trigger level.
  414. * RFDIV is set such way to satisfy requested uartclk value
  415. */
  416. val = TXTL << 10 | RXTL;
  417. ufcr_rfdiv = (imx_get_perclk1() + sport->port.uartclk / 2) / sport->port.uartclk;
  418. if(!ufcr_rfdiv)
  419. ufcr_rfdiv = 1;
  420. if(ufcr_rfdiv >= 7)
  421. ufcr_rfdiv = 6;
  422. else
  423. ufcr_rfdiv = 6 - ufcr_rfdiv;
  424. val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
  425. writel(val, sport->port.membase + UFCR);
  426. return 0;
  427. }
  428. static int imx_startup(struct uart_port *port)
  429. {
  430. struct imx_port *sport = (struct imx_port *)port;
  431. int retval;
  432. unsigned long flags, temp;
  433. imx_setup_ufcr(sport, 0);
  434. /* disable the DREN bit (Data Ready interrupt enable) before
  435. * requesting IRQs
  436. */
  437. temp = readl(sport->port.membase + UCR4);
  438. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  439. /*
  440. * Allocate the IRQ
  441. */
  442. retval = request_irq(sport->rxirq, imx_rxint, 0,
  443. DRIVER_NAME, sport);
  444. if (retval) goto error_out1;
  445. retval = request_irq(sport->txirq, imx_txint, 0,
  446. DRIVER_NAME, sport);
  447. if (retval) goto error_out2;
  448. retval = request_irq(sport->rtsirq, imx_rtsint,
  449. (sport->rtsirq < IMX_IRQS) ? 0 :
  450. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  451. DRIVER_NAME, sport);
  452. if (retval) goto error_out3;
  453. /*
  454. * Finally, clear and enable interrupts
  455. */
  456. writel(USR1_RTSD, sport->port.membase + USR1);
  457. temp = readl(sport->port.membase + UCR1);
  458. temp |= (UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  459. writel(temp, sport->port.membase + UCR1);
  460. temp = readl(sport->port.membase + UCR2);
  461. temp |= (UCR2_RXEN | UCR2_TXEN);
  462. writel(temp, sport->port.membase + UCR2);
  463. /*
  464. * Enable modem status interrupts
  465. */
  466. spin_lock_irqsave(&sport->port.lock,flags);
  467. imx_enable_ms(&sport->port);
  468. spin_unlock_irqrestore(&sport->port.lock,flags);
  469. return 0;
  470. error_out3:
  471. free_irq(sport->txirq, sport);
  472. error_out2:
  473. free_irq(sport->rxirq, sport);
  474. error_out1:
  475. return retval;
  476. }
  477. static void imx_shutdown(struct uart_port *port)
  478. {
  479. struct imx_port *sport = (struct imx_port *)port;
  480. unsigned long temp;
  481. /*
  482. * Stop our timer.
  483. */
  484. del_timer_sync(&sport->timer);
  485. /*
  486. * Free the interrupts
  487. */
  488. free_irq(sport->rtsirq, sport);
  489. free_irq(sport->txirq, sport);
  490. free_irq(sport->rxirq, sport);
  491. /*
  492. * Disable all interrupts, port and break condition.
  493. */
  494. temp = readl(sport->port.membase + UCR1);
  495. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  496. writel(temp, sport->port.membase + UCR1);
  497. }
  498. static void
  499. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  500. struct ktermios *old)
  501. {
  502. struct imx_port *sport = (struct imx_port *)port;
  503. unsigned long flags;
  504. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  505. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  506. /*
  507. * If we don't support modem control lines, don't allow
  508. * these to be set.
  509. */
  510. if (0) {
  511. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  512. termios->c_cflag |= CLOCAL;
  513. }
  514. /*
  515. * We only support CS7 and CS8.
  516. */
  517. while ((termios->c_cflag & CSIZE) != CS7 &&
  518. (termios->c_cflag & CSIZE) != CS8) {
  519. termios->c_cflag &= ~CSIZE;
  520. termios->c_cflag |= old_csize;
  521. old_csize = CS8;
  522. }
  523. if ((termios->c_cflag & CSIZE) == CS8)
  524. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  525. else
  526. ucr2 = UCR2_SRST | UCR2_IRTS;
  527. if (termios->c_cflag & CRTSCTS) {
  528. if( sport->have_rtscts ) {
  529. ucr2 &= ~UCR2_IRTS;
  530. ucr2 |= UCR2_CTSC;
  531. } else {
  532. termios->c_cflag &= ~CRTSCTS;
  533. }
  534. }
  535. if (termios->c_cflag & CSTOPB)
  536. ucr2 |= UCR2_STPB;
  537. if (termios->c_cflag & PARENB) {
  538. ucr2 |= UCR2_PREN;
  539. if (termios->c_cflag & PARODD)
  540. ucr2 |= UCR2_PROE;
  541. }
  542. /*
  543. * Ask the core to calculate the divisor for us.
  544. */
  545. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  546. quot = uart_get_divisor(port, baud);
  547. spin_lock_irqsave(&sport->port.lock, flags);
  548. sport->port.read_status_mask = 0;
  549. if (termios->c_iflag & INPCK)
  550. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  551. if (termios->c_iflag & (BRKINT | PARMRK))
  552. sport->port.read_status_mask |= URXD_BRK;
  553. /*
  554. * Characters to ignore
  555. */
  556. sport->port.ignore_status_mask = 0;
  557. if (termios->c_iflag & IGNPAR)
  558. sport->port.ignore_status_mask |= URXD_PRERR;
  559. if (termios->c_iflag & IGNBRK) {
  560. sport->port.ignore_status_mask |= URXD_BRK;
  561. /*
  562. * If we're ignoring parity and break indicators,
  563. * ignore overruns too (for real raw support).
  564. */
  565. if (termios->c_iflag & IGNPAR)
  566. sport->port.ignore_status_mask |= URXD_OVRRUN;
  567. }
  568. del_timer_sync(&sport->timer);
  569. /*
  570. * Update the per-port timeout.
  571. */
  572. uart_update_timeout(port, termios->c_cflag, baud);
  573. /*
  574. * disable interrupts and drain transmitter
  575. */
  576. old_ucr1 = readl(sport->port.membase + UCR1);
  577. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  578. sport->port.membase + UCR1);
  579. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  580. barrier();
  581. /* then, disable everything */
  582. old_txrxen = readl(sport->port.membase + UCR2);
  583. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  584. sport->port.membase + UCR2);
  585. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  586. /* set the baud rate. We assume uartclk = 16 MHz
  587. *
  588. * baud * 16 UBIR - 1
  589. * --------- = --------
  590. * uartclk UBMR - 1
  591. */
  592. writel((baud / 100) - 1, sport->port.membase + UBIR);
  593. writel(10000 - 1, sport->port.membase + UBMR);
  594. writel(old_ucr1, sport->port.membase + UCR1);
  595. /* set the parity, stop bits and data size */
  596. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  597. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  598. imx_enable_ms(&sport->port);
  599. spin_unlock_irqrestore(&sport->port.lock, flags);
  600. }
  601. static const char *imx_type(struct uart_port *port)
  602. {
  603. struct imx_port *sport = (struct imx_port *)port;
  604. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  605. }
  606. /*
  607. * Release the memory region(s) being used by 'port'.
  608. */
  609. static void imx_release_port(struct uart_port *port)
  610. {
  611. struct imx_port *sport = (struct imx_port *)port;
  612. release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
  613. }
  614. /*
  615. * Request the memory region(s) being used by 'port'.
  616. */
  617. static int imx_request_port(struct uart_port *port)
  618. {
  619. struct imx_port *sport = (struct imx_port *)port;
  620. return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
  621. "imx-uart") != NULL ? 0 : -EBUSY;
  622. }
  623. /*
  624. * Configure/autoconfigure the port.
  625. */
  626. static void imx_config_port(struct uart_port *port, int flags)
  627. {
  628. struct imx_port *sport = (struct imx_port *)port;
  629. if (flags & UART_CONFIG_TYPE &&
  630. imx_request_port(&sport->port) == 0)
  631. sport->port.type = PORT_IMX;
  632. }
  633. /*
  634. * Verify the new serial_struct (for TIOCSSERIAL).
  635. * The only change we allow are to the flags and type, and
  636. * even then only between PORT_IMX and PORT_UNKNOWN
  637. */
  638. static int
  639. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  640. {
  641. struct imx_port *sport = (struct imx_port *)port;
  642. int ret = 0;
  643. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  644. ret = -EINVAL;
  645. if (sport->port.irq != ser->irq)
  646. ret = -EINVAL;
  647. if (ser->io_type != UPIO_MEM)
  648. ret = -EINVAL;
  649. if (sport->port.uartclk / 16 != ser->baud_base)
  650. ret = -EINVAL;
  651. if ((void *)sport->port.mapbase != ser->iomem_base)
  652. ret = -EINVAL;
  653. if (sport->port.iobase != ser->port)
  654. ret = -EINVAL;
  655. if (ser->hub6 != 0)
  656. ret = -EINVAL;
  657. return ret;
  658. }
  659. static struct uart_ops imx_pops = {
  660. .tx_empty = imx_tx_empty,
  661. .set_mctrl = imx_set_mctrl,
  662. .get_mctrl = imx_get_mctrl,
  663. .stop_tx = imx_stop_tx,
  664. .start_tx = imx_start_tx,
  665. .stop_rx = imx_stop_rx,
  666. .enable_ms = imx_enable_ms,
  667. .break_ctl = imx_break_ctl,
  668. .startup = imx_startup,
  669. .shutdown = imx_shutdown,
  670. .set_termios = imx_set_termios,
  671. .type = imx_type,
  672. .release_port = imx_release_port,
  673. .request_port = imx_request_port,
  674. .config_port = imx_config_port,
  675. .verify_port = imx_verify_port,
  676. };
  677. static struct imx_port imx_ports[] = {
  678. {
  679. .txirq = UART1_MINT_TX,
  680. .rxirq = UART1_MINT_RX,
  681. .rtsirq = UART1_MINT_RTS,
  682. .port = {
  683. .type = PORT_IMX,
  684. .iotype = UPIO_MEM,
  685. .membase = (void *)IMX_UART1_BASE,
  686. .mapbase = IMX_UART1_BASE, /* FIXME */
  687. .irq = UART1_MINT_RX,
  688. .uartclk = 16000000,
  689. .fifosize = 32,
  690. .flags = UPF_BOOT_AUTOCONF,
  691. .ops = &imx_pops,
  692. .line = 0,
  693. },
  694. }, {
  695. .txirq = UART2_MINT_TX,
  696. .rxirq = UART2_MINT_RX,
  697. .rtsirq = UART2_MINT_RTS,
  698. .port = {
  699. .type = PORT_IMX,
  700. .iotype = UPIO_MEM,
  701. .membase = (void *)IMX_UART2_BASE,
  702. .mapbase = IMX_UART2_BASE, /* FIXME */
  703. .irq = UART2_MINT_RX,
  704. .uartclk = 16000000,
  705. .fifosize = 32,
  706. .flags = UPF_BOOT_AUTOCONF,
  707. .ops = &imx_pops,
  708. .line = 1,
  709. },
  710. }
  711. };
  712. /*
  713. * Setup the IMX serial ports.
  714. * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
  715. * Which serial port this ends up being depends on the machine you're
  716. * running this kernel on. I'm not convinced that this is a good idea,
  717. * but that's the way it traditionally works.
  718. *
  719. */
  720. static void __init imx_init_ports(void)
  721. {
  722. static int first = 1;
  723. int i;
  724. if (!first)
  725. return;
  726. first = 0;
  727. for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
  728. init_timer(&imx_ports[i].timer);
  729. imx_ports[i].timer.function = imx_timeout;
  730. imx_ports[i].timer.data = (unsigned long)&imx_ports[i];
  731. }
  732. }
  733. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  734. static void imx_console_putchar(struct uart_port *port, int ch)
  735. {
  736. struct imx_port *sport = (struct imx_port *)port;
  737. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  738. barrier();
  739. writel(ch, sport->port.membase + URTX0);
  740. }
  741. /*
  742. * Interrupts are disabled on entering
  743. */
  744. static void
  745. imx_console_write(struct console *co, const char *s, unsigned int count)
  746. {
  747. struct imx_port *sport = &imx_ports[co->index];
  748. unsigned int old_ucr1, old_ucr2;
  749. /*
  750. * First, save UCR1/2 and then disable interrupts
  751. */
  752. old_ucr1 = readl(sport->port.membase + UCR1);
  753. old_ucr2 = readl(sport->port.membase + UCR2);
  754. writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
  755. ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  756. sport->port.membase + UCR1);
  757. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  758. uart_console_write(&sport->port, s, count, imx_console_putchar);
  759. /*
  760. * Finally, wait for transmitter to become empty
  761. * and restore UCR1/2
  762. */
  763. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  764. writel(old_ucr1, sport->port.membase + UCR1);
  765. writel(old_ucr2, sport->port.membase + UCR2);
  766. }
  767. /*
  768. * If the port was already initialised (eg, by a boot loader),
  769. * try to determine the current setup.
  770. */
  771. static void __init
  772. imx_console_get_options(struct imx_port *sport, int *baud,
  773. int *parity, int *bits)
  774. {
  775. if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
  776. /* ok, the port was enabled */
  777. unsigned int ucr2, ubir,ubmr, uartclk;
  778. unsigned int baud_raw;
  779. unsigned int ucfr_rfdiv;
  780. ucr2 = readl(sport->port.membase + UCR2);
  781. *parity = 'n';
  782. if (ucr2 & UCR2_PREN) {
  783. if (ucr2 & UCR2_PROE)
  784. *parity = 'o';
  785. else
  786. *parity = 'e';
  787. }
  788. if (ucr2 & UCR2_WS)
  789. *bits = 8;
  790. else
  791. *bits = 7;
  792. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  793. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  794. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  795. if (ucfr_rfdiv == 6)
  796. ucfr_rfdiv = 7;
  797. else
  798. ucfr_rfdiv = 6 - ucfr_rfdiv;
  799. uartclk = imx_get_perclk1();
  800. uartclk /= ucfr_rfdiv;
  801. { /*
  802. * The next code provides exact computation of
  803. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  804. * without need of float support or long long division,
  805. * which would be required to prevent 32bit arithmetic overflow
  806. */
  807. unsigned int mul = ubir + 1;
  808. unsigned int div = 16 * (ubmr + 1);
  809. unsigned int rem = uartclk % div;
  810. baud_raw = (uartclk / div) * mul;
  811. baud_raw += (rem * mul + div / 2) / div;
  812. *baud = (baud_raw + 50) / 100 * 100;
  813. }
  814. if(*baud != baud_raw)
  815. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  816. baud_raw, *baud);
  817. }
  818. }
  819. static int __init
  820. imx_console_setup(struct console *co, char *options)
  821. {
  822. struct imx_port *sport;
  823. int baud = 9600;
  824. int bits = 8;
  825. int parity = 'n';
  826. int flow = 'n';
  827. /*
  828. * Check whether an invalid uart number has been specified, and
  829. * if so, search for the first available port that does have
  830. * console support.
  831. */
  832. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  833. co->index = 0;
  834. sport = &imx_ports[co->index];
  835. if (options)
  836. uart_parse_options(options, &baud, &parity, &bits, &flow);
  837. else
  838. imx_console_get_options(sport, &baud, &parity, &bits);
  839. imx_setup_ufcr(sport, 0);
  840. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  841. }
  842. static struct uart_driver imx_reg;
  843. static struct console imx_console = {
  844. .name = "ttySMX",
  845. .write = imx_console_write,
  846. .device = uart_console_device,
  847. .setup = imx_console_setup,
  848. .flags = CON_PRINTBUFFER,
  849. .index = -1,
  850. .data = &imx_reg,
  851. };
  852. static int __init imx_rs_console_init(void)
  853. {
  854. imx_init_ports();
  855. register_console(&imx_console);
  856. return 0;
  857. }
  858. console_initcall(imx_rs_console_init);
  859. #define IMX_CONSOLE &imx_console
  860. #else
  861. #define IMX_CONSOLE NULL
  862. #endif
  863. static struct uart_driver imx_reg = {
  864. .owner = THIS_MODULE,
  865. .driver_name = DRIVER_NAME,
  866. .dev_name = "ttySMX",
  867. .major = SERIAL_IMX_MAJOR,
  868. .minor = MINOR_START,
  869. .nr = ARRAY_SIZE(imx_ports),
  870. .cons = IMX_CONSOLE,
  871. };
  872. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  873. {
  874. struct imx_port *sport = platform_get_drvdata(dev);
  875. if (sport)
  876. uart_suspend_port(&imx_reg, &sport->port);
  877. return 0;
  878. }
  879. static int serial_imx_resume(struct platform_device *dev)
  880. {
  881. struct imx_port *sport = platform_get_drvdata(dev);
  882. if (sport)
  883. uart_resume_port(&imx_reg, &sport->port);
  884. return 0;
  885. }
  886. static int serial_imx_probe(struct platform_device *dev)
  887. {
  888. struct imxuart_platform_data *pdata;
  889. imx_ports[dev->id].port.dev = &dev->dev;
  890. pdata = (struct imxuart_platform_data *)dev->dev.platform_data;
  891. if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  892. imx_ports[dev->id].have_rtscts = 1;
  893. uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
  894. platform_set_drvdata(dev, &imx_ports[dev->id]);
  895. return 0;
  896. }
  897. static int serial_imx_remove(struct platform_device *dev)
  898. {
  899. struct imx_port *sport = platform_get_drvdata(dev);
  900. platform_set_drvdata(dev, NULL);
  901. if (sport)
  902. uart_remove_one_port(&imx_reg, &sport->port);
  903. return 0;
  904. }
  905. static struct platform_driver serial_imx_driver = {
  906. .probe = serial_imx_probe,
  907. .remove = serial_imx_remove,
  908. .suspend = serial_imx_suspend,
  909. .resume = serial_imx_resume,
  910. .driver = {
  911. .name = "imx-uart",
  912. },
  913. };
  914. static int __init imx_serial_init(void)
  915. {
  916. int ret;
  917. printk(KERN_INFO "Serial: IMX driver\n");
  918. imx_init_ports();
  919. ret = uart_register_driver(&imx_reg);
  920. if (ret)
  921. return ret;
  922. ret = platform_driver_register(&serial_imx_driver);
  923. if (ret != 0)
  924. uart_unregister_driver(&imx_reg);
  925. return 0;
  926. }
  927. static void __exit imx_serial_exit(void)
  928. {
  929. platform_driver_unregister(&serial_imx_driver);
  930. uart_unregister_driver(&imx_reg);
  931. }
  932. module_init(imx_serial_init);
  933. module_exit(imx_serial_exit);
  934. MODULE_AUTHOR("Sascha Hauer");
  935. MODULE_DESCRIPTION("IMX generic serial port driver");
  936. MODULE_LICENSE("GPL");