imx.c 30 KB

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