imx.c 30 KB

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