imx.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. unsigned int div, num, denom, ufcr;
  498. /*
  499. * If we don't support modem control lines, don't allow
  500. * these to be set.
  501. */
  502. if (0) {
  503. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  504. termios->c_cflag |= CLOCAL;
  505. }
  506. /*
  507. * We only support CS7 and CS8.
  508. */
  509. while ((termios->c_cflag & CSIZE) != CS7 &&
  510. (termios->c_cflag & CSIZE) != CS8) {
  511. termios->c_cflag &= ~CSIZE;
  512. termios->c_cflag |= old_csize;
  513. old_csize = CS8;
  514. }
  515. if ((termios->c_cflag & CSIZE) == CS8)
  516. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  517. else
  518. ucr2 = UCR2_SRST | UCR2_IRTS;
  519. if (termios->c_cflag & CRTSCTS) {
  520. if( sport->have_rtscts ) {
  521. ucr2 &= ~UCR2_IRTS;
  522. ucr2 |= UCR2_CTSC;
  523. } else {
  524. termios->c_cflag &= ~CRTSCTS;
  525. }
  526. }
  527. if (termios->c_cflag & CSTOPB)
  528. ucr2 |= UCR2_STPB;
  529. if (termios->c_cflag & PARENB) {
  530. ucr2 |= UCR2_PREN;
  531. if (termios->c_cflag & PARODD)
  532. ucr2 |= UCR2_PROE;
  533. }
  534. /*
  535. * Ask the core to calculate the divisor for us.
  536. */
  537. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  538. quot = uart_get_divisor(port, baud);
  539. spin_lock_irqsave(&sport->port.lock, flags);
  540. sport->port.read_status_mask = 0;
  541. if (termios->c_iflag & INPCK)
  542. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  543. if (termios->c_iflag & (BRKINT | PARMRK))
  544. sport->port.read_status_mask |= URXD_BRK;
  545. /*
  546. * Characters to ignore
  547. */
  548. sport->port.ignore_status_mask = 0;
  549. if (termios->c_iflag & IGNPAR)
  550. sport->port.ignore_status_mask |= URXD_PRERR;
  551. if (termios->c_iflag & IGNBRK) {
  552. sport->port.ignore_status_mask |= URXD_BRK;
  553. /*
  554. * If we're ignoring parity and break indicators,
  555. * ignore overruns too (for real raw support).
  556. */
  557. if (termios->c_iflag & IGNPAR)
  558. sport->port.ignore_status_mask |= URXD_OVRRUN;
  559. }
  560. del_timer_sync(&sport->timer);
  561. /*
  562. * Update the per-port timeout.
  563. */
  564. uart_update_timeout(port, termios->c_cflag, baud);
  565. /*
  566. * disable interrupts and drain transmitter
  567. */
  568. old_ucr1 = readl(sport->port.membase + UCR1);
  569. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  570. sport->port.membase + UCR1);
  571. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  572. barrier();
  573. /* then, disable everything */
  574. old_txrxen = readl(sport->port.membase + UCR2);
  575. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  576. sport->port.membase + UCR2);
  577. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  578. div = sport->port.uartclk / (baud * 16);
  579. if (div > 7)
  580. div = 7;
  581. if (!div)
  582. div = 1;
  583. num = baud;
  584. denom = port->uartclk / div / 16;
  585. /* shift num and denom right until they fit into 16 bits */
  586. while (num > 0x10000 || denom > 0x10000) {
  587. num >>= 1;
  588. denom >>= 1;
  589. }
  590. if (num > 0)
  591. num -= 1;
  592. if (denom > 0)
  593. denom -= 1;
  594. writel(num, sport->port.membase + UBIR);
  595. writel(denom, sport->port.membase + UBMR);
  596. if (div == 7)
  597. div = 6; /* 6 in RFDIV means divide by 7 */
  598. else
  599. div = 6 - div;
  600. ufcr = readl(sport->port.membase + UFCR);
  601. ufcr = (ufcr & (~UFCR_RFDIV)) |
  602. (div << 7);
  603. writel(ufcr, sport->port.membase + UFCR);
  604. #ifdef ONEMS
  605. writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
  606. #endif
  607. writel(old_ucr1, sport->port.membase + UCR1);
  608. /* set the parity, stop bits and data size */
  609. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  610. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  611. imx_enable_ms(&sport->port);
  612. spin_unlock_irqrestore(&sport->port.lock, flags);
  613. }
  614. static const char *imx_type(struct uart_port *port)
  615. {
  616. struct imx_port *sport = (struct imx_port *)port;
  617. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  618. }
  619. /*
  620. * Release the memory region(s) being used by 'port'.
  621. */
  622. static void imx_release_port(struct uart_port *port)
  623. {
  624. struct platform_device *pdev = to_platform_device(port->dev);
  625. struct resource *mmres;
  626. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  627. release_mem_region(mmres->start, mmres->end - mmres->start + 1);
  628. }
  629. /*
  630. * Request the memory region(s) being used by 'port'.
  631. */
  632. static int imx_request_port(struct uart_port *port)
  633. {
  634. struct platform_device *pdev = to_platform_device(port->dev);
  635. struct resource *mmres;
  636. void *ret;
  637. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  638. if (!mmres)
  639. return -ENODEV;
  640. ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
  641. "imx-uart");
  642. return ret ? 0 : -EBUSY;
  643. }
  644. /*
  645. * Configure/autoconfigure the port.
  646. */
  647. static void imx_config_port(struct uart_port *port, int flags)
  648. {
  649. struct imx_port *sport = (struct imx_port *)port;
  650. if (flags & UART_CONFIG_TYPE &&
  651. imx_request_port(&sport->port) == 0)
  652. sport->port.type = PORT_IMX;
  653. }
  654. /*
  655. * Verify the new serial_struct (for TIOCSSERIAL).
  656. * The only change we allow are to the flags and type, and
  657. * even then only between PORT_IMX and PORT_UNKNOWN
  658. */
  659. static int
  660. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  661. {
  662. struct imx_port *sport = (struct imx_port *)port;
  663. int ret = 0;
  664. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  665. ret = -EINVAL;
  666. if (sport->port.irq != ser->irq)
  667. ret = -EINVAL;
  668. if (ser->io_type != UPIO_MEM)
  669. ret = -EINVAL;
  670. if (sport->port.uartclk / 16 != ser->baud_base)
  671. ret = -EINVAL;
  672. if ((void *)sport->port.mapbase != ser->iomem_base)
  673. ret = -EINVAL;
  674. if (sport->port.iobase != ser->port)
  675. ret = -EINVAL;
  676. if (ser->hub6 != 0)
  677. ret = -EINVAL;
  678. return ret;
  679. }
  680. static struct uart_ops imx_pops = {
  681. .tx_empty = imx_tx_empty,
  682. .set_mctrl = imx_set_mctrl,
  683. .get_mctrl = imx_get_mctrl,
  684. .stop_tx = imx_stop_tx,
  685. .start_tx = imx_start_tx,
  686. .stop_rx = imx_stop_rx,
  687. .enable_ms = imx_enable_ms,
  688. .break_ctl = imx_break_ctl,
  689. .startup = imx_startup,
  690. .shutdown = imx_shutdown,
  691. .set_termios = imx_set_termios,
  692. .type = imx_type,
  693. .release_port = imx_release_port,
  694. .request_port = imx_request_port,
  695. .config_port = imx_config_port,
  696. .verify_port = imx_verify_port,
  697. };
  698. static struct imx_port imx_ports[] = {
  699. {
  700. .txirq = UART1_MINT_TX,
  701. .rxirq = UART1_MINT_RX,
  702. .rtsirq = UART1_MINT_RTS,
  703. .port = {
  704. .type = PORT_IMX,
  705. .iotype = UPIO_MEM,
  706. .membase = (void *)IMX_UART1_BASE,
  707. .mapbase = 0x00206000,
  708. .irq = UART1_MINT_RX,
  709. .fifosize = 32,
  710. .flags = UPF_BOOT_AUTOCONF,
  711. .ops = &imx_pops,
  712. .line = 0,
  713. },
  714. }, {
  715. .txirq = UART2_MINT_TX,
  716. .rxirq = UART2_MINT_RX,
  717. .rtsirq = UART2_MINT_RTS,
  718. .port = {
  719. .type = PORT_IMX,
  720. .iotype = UPIO_MEM,
  721. .membase = (void *)IMX_UART2_BASE,
  722. .mapbase = 0x00207000,
  723. .irq = UART2_MINT_RX,
  724. .fifosize = 32,
  725. .flags = UPF_BOOT_AUTOCONF,
  726. .ops = &imx_pops,
  727. .line = 1,
  728. },
  729. }
  730. };
  731. /*
  732. * Setup the IMX serial ports.
  733. * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
  734. * Which serial port this ends up being depends on the machine you're
  735. * running this kernel on. I'm not convinced that this is a good idea,
  736. * but that's the way it traditionally works.
  737. *
  738. */
  739. static void __init imx_init_ports(void)
  740. {
  741. static int first = 1;
  742. int i;
  743. if (!first)
  744. return;
  745. first = 0;
  746. for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
  747. init_timer(&imx_ports[i].timer);
  748. imx_ports[i].timer.function = imx_timeout;
  749. imx_ports[i].timer.data = (unsigned long)&imx_ports[i];
  750. imx_ports[i].port.uartclk = imx_get_perclk1();
  751. }
  752. }
  753. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  754. static void imx_console_putchar(struct uart_port *port, int ch)
  755. {
  756. struct imx_port *sport = (struct imx_port *)port;
  757. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  758. barrier();
  759. writel(ch, sport->port.membase + URTX0);
  760. }
  761. /*
  762. * Interrupts are disabled on entering
  763. */
  764. static void
  765. imx_console_write(struct console *co, const char *s, unsigned int count)
  766. {
  767. struct imx_port *sport = &imx_ports[co->index];
  768. unsigned int old_ucr1, old_ucr2;
  769. /*
  770. * First, save UCR1/2 and then disable interrupts
  771. */
  772. old_ucr1 = readl(sport->port.membase + UCR1);
  773. old_ucr2 = readl(sport->port.membase + UCR2);
  774. writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
  775. ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  776. sport->port.membase + UCR1);
  777. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  778. uart_console_write(&sport->port, s, count, imx_console_putchar);
  779. /*
  780. * Finally, wait for transmitter to become empty
  781. * and restore UCR1/2
  782. */
  783. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  784. writel(old_ucr1, sport->port.membase + UCR1);
  785. writel(old_ucr2, sport->port.membase + UCR2);
  786. }
  787. /*
  788. * If the port was already initialised (eg, by a boot loader),
  789. * try to determine the current setup.
  790. */
  791. static void __init
  792. imx_console_get_options(struct imx_port *sport, int *baud,
  793. int *parity, int *bits)
  794. {
  795. if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
  796. /* ok, the port was enabled */
  797. unsigned int ucr2, ubir,ubmr, uartclk;
  798. unsigned int baud_raw;
  799. unsigned int ucfr_rfdiv;
  800. ucr2 = readl(sport->port.membase + UCR2);
  801. *parity = 'n';
  802. if (ucr2 & UCR2_PREN) {
  803. if (ucr2 & UCR2_PROE)
  804. *parity = 'o';
  805. else
  806. *parity = 'e';
  807. }
  808. if (ucr2 & UCR2_WS)
  809. *bits = 8;
  810. else
  811. *bits = 7;
  812. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  813. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  814. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  815. if (ucfr_rfdiv == 6)
  816. ucfr_rfdiv = 7;
  817. else
  818. ucfr_rfdiv = 6 - ucfr_rfdiv;
  819. uartclk = imx_get_perclk1();
  820. uartclk /= ucfr_rfdiv;
  821. { /*
  822. * The next code provides exact computation of
  823. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  824. * without need of float support or long long division,
  825. * which would be required to prevent 32bit arithmetic overflow
  826. */
  827. unsigned int mul = ubir + 1;
  828. unsigned int div = 16 * (ubmr + 1);
  829. unsigned int rem = uartclk % div;
  830. baud_raw = (uartclk / div) * mul;
  831. baud_raw += (rem * mul + div / 2) / div;
  832. *baud = (baud_raw + 50) / 100 * 100;
  833. }
  834. if(*baud != baud_raw)
  835. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  836. baud_raw, *baud);
  837. }
  838. }
  839. static int __init
  840. imx_console_setup(struct console *co, char *options)
  841. {
  842. struct imx_port *sport;
  843. int baud = 9600;
  844. int bits = 8;
  845. int parity = 'n';
  846. int flow = 'n';
  847. /*
  848. * Check whether an invalid uart number has been specified, and
  849. * if so, search for the first available port that does have
  850. * console support.
  851. */
  852. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  853. co->index = 0;
  854. sport = &imx_ports[co->index];
  855. if (options)
  856. uart_parse_options(options, &baud, &parity, &bits, &flow);
  857. else
  858. imx_console_get_options(sport, &baud, &parity, &bits);
  859. imx_setup_ufcr(sport, 0);
  860. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  861. }
  862. static struct uart_driver imx_reg;
  863. static struct console imx_console = {
  864. .name = "ttySMX",
  865. .write = imx_console_write,
  866. .device = uart_console_device,
  867. .setup = imx_console_setup,
  868. .flags = CON_PRINTBUFFER,
  869. .index = -1,
  870. .data = &imx_reg,
  871. };
  872. static int __init imx_rs_console_init(void)
  873. {
  874. imx_init_ports();
  875. register_console(&imx_console);
  876. return 0;
  877. }
  878. console_initcall(imx_rs_console_init);
  879. #define IMX_CONSOLE &imx_console
  880. #else
  881. #define IMX_CONSOLE NULL
  882. #endif
  883. static struct uart_driver imx_reg = {
  884. .owner = THIS_MODULE,
  885. .driver_name = DRIVER_NAME,
  886. .dev_name = "ttySMX",
  887. .major = SERIAL_IMX_MAJOR,
  888. .minor = MINOR_START,
  889. .nr = ARRAY_SIZE(imx_ports),
  890. .cons = IMX_CONSOLE,
  891. };
  892. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  893. {
  894. struct imx_port *sport = platform_get_drvdata(dev);
  895. if (sport)
  896. uart_suspend_port(&imx_reg, &sport->port);
  897. return 0;
  898. }
  899. static int serial_imx_resume(struct platform_device *dev)
  900. {
  901. struct imx_port *sport = platform_get_drvdata(dev);
  902. if (sport)
  903. uart_resume_port(&imx_reg, &sport->port);
  904. return 0;
  905. }
  906. static int serial_imx_probe(struct platform_device *dev)
  907. {
  908. struct imxuart_platform_data *pdata;
  909. imx_ports[dev->id].port.dev = &dev->dev;
  910. pdata = (struct imxuart_platform_data *)dev->dev.platform_data;
  911. if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  912. imx_ports[dev->id].have_rtscts = 1;
  913. uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
  914. platform_set_drvdata(dev, &imx_ports[dev->id]);
  915. return 0;
  916. }
  917. static int serial_imx_remove(struct platform_device *dev)
  918. {
  919. struct imx_port *sport = platform_get_drvdata(dev);
  920. platform_set_drvdata(dev, NULL);
  921. if (sport)
  922. uart_remove_one_port(&imx_reg, &sport->port);
  923. return 0;
  924. }
  925. static struct platform_driver serial_imx_driver = {
  926. .probe = serial_imx_probe,
  927. .remove = serial_imx_remove,
  928. .suspend = serial_imx_suspend,
  929. .resume = serial_imx_resume,
  930. .driver = {
  931. .name = "imx-uart",
  932. .owner = THIS_MODULE,
  933. },
  934. };
  935. static int __init imx_serial_init(void)
  936. {
  937. int ret;
  938. printk(KERN_INFO "Serial: IMX driver\n");
  939. imx_init_ports();
  940. ret = uart_register_driver(&imx_reg);
  941. if (ret)
  942. return ret;
  943. ret = platform_driver_register(&serial_imx_driver);
  944. if (ret != 0)
  945. uart_unregister_driver(&imx_reg);
  946. return 0;
  947. }
  948. static void __exit imx_serial_exit(void)
  949. {
  950. platform_driver_unregister(&serial_imx_driver);
  951. uart_unregister_driver(&imx_reg);
  952. }
  953. module_init(imx_serial_init);
  954. module_exit(imx_serial_exit);
  955. MODULE_AUTHOR("Sascha Hauer");
  956. MODULE_DESCRIPTION("IMX generic serial port driver");
  957. MODULE_LICENSE("GPL");
  958. MODULE_ALIAS("platform:imx-uart");