imx.c 30 KB

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