imx.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. * Copyright (C) 2009 emlix GmbH
  12. * Author: Fabian Godehardt (added IrDA support for iMX)
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * [29-Mar-2005] Mike Lee
  29. * Added hardware handshake
  30. */
  31. #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  32. #define SUPPORT_SYSRQ
  33. #endif
  34. #include <linux/module.h>
  35. #include <linux/ioport.h>
  36. #include <linux/init.h>
  37. #include <linux/console.h>
  38. #include <linux/sysrq.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/serial_core.h>
  43. #include <linux/serial.h>
  44. #include <linux/clk.h>
  45. #include <linux/delay.h>
  46. #include <linux/rational.h>
  47. #include <asm/io.h>
  48. #include <asm/irq.h>
  49. #include <mach/hardware.h>
  50. #include <mach/imx-uart.h>
  51. /* Register definitions */
  52. #define URXD0 0x0 /* Receiver Register */
  53. #define URTX0 0x40 /* Transmitter Register */
  54. #define UCR1 0x80 /* Control Register 1 */
  55. #define UCR2 0x84 /* Control Register 2 */
  56. #define UCR3 0x88 /* Control Register 3 */
  57. #define UCR4 0x8c /* Control Register 4 */
  58. #define UFCR 0x90 /* FIFO Control Register */
  59. #define USR1 0x94 /* Status Register 1 */
  60. #define USR2 0x98 /* Status Register 2 */
  61. #define UESC 0x9c /* Escape Character Register */
  62. #define UTIM 0xa0 /* Escape Timer Register */
  63. #define UBIR 0xa4 /* BRM Incremental Register */
  64. #define UBMR 0xa8 /* BRM Modulator Register */
  65. #define UBRC 0xac /* Baud Rate Count Register */
  66. #if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2
  67. #define ONEMS 0xb0 /* One Millisecond register */
  68. #define UTS 0xb4 /* UART Test Register */
  69. #endif
  70. #if defined(CONFIG_ARCH_IMX) || defined(CONFIG_ARCH_MX1)
  71. #define BIPR1 0xb0 /* Incremental Preset Register 1 */
  72. #define BIPR2 0xb4 /* Incremental Preset Register 2 */
  73. #define BIPR3 0xb8 /* Incremental Preset Register 3 */
  74. #define BIPR4 0xbc /* Incremental Preset Register 4 */
  75. #define BMPR1 0xc0 /* BRM Modulator Register 1 */
  76. #define BMPR2 0xc4 /* BRM Modulator Register 2 */
  77. #define BMPR3 0xc8 /* BRM Modulator Register 3 */
  78. #define BMPR4 0xcc /* BRM Modulator Register 4 */
  79. #define UTS 0xd0 /* UART Test Register */
  80. #endif
  81. /* UART Control Register Bit Fields.*/
  82. #define URXD_CHARRDY (1<<15)
  83. #define URXD_ERR (1<<14)
  84. #define URXD_OVRRUN (1<<13)
  85. #define URXD_FRMERR (1<<12)
  86. #define URXD_BRK (1<<11)
  87. #define URXD_PRERR (1<<10)
  88. #define UCR1_ADEN (1<<15) /* Auto dectect interrupt */
  89. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  90. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  91. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  92. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  93. #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
  94. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  95. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  96. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  97. #define UCR1_SNDBRK (1<<4) /* Send break */
  98. #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
  99. #if defined(CONFIG_ARCH_IMX) || defined(CONFIG_ARCH_MX1)
  100. #define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */
  101. #endif
  102. #if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2
  103. #define UCR1_UARTCLKEN (0) /* not present on mx2/mx3 */
  104. #endif
  105. #define UCR1_DOZE (1<<1) /* Doze */
  106. #define UCR1_UARTEN (1<<0) /* UART enabled */
  107. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  108. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  109. #define UCR2_CTSC (1<<13) /* CTS pin control */
  110. #define UCR2_CTS (1<<12) /* Clear to send */
  111. #define UCR2_ESCEN (1<<11) /* Escape enable */
  112. #define UCR2_PREN (1<<8) /* Parity enable */
  113. #define UCR2_PROE (1<<7) /* Parity odd/even */
  114. #define UCR2_STPB (1<<6) /* Stop */
  115. #define UCR2_WS (1<<5) /* Word size */
  116. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  117. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  118. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  119. #define UCR2_SRST (1<<0) /* SW reset */
  120. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  121. #define UCR3_PARERREN (1<<12) /* Parity enable */
  122. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  123. #define UCR3_DSR (1<<10) /* Data set ready */
  124. #define UCR3_DCD (1<<9) /* Data carrier detect */
  125. #define UCR3_RI (1<<8) /* Ring indicator */
  126. #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
  127. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  128. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  129. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  130. #ifdef CONFIG_ARCH_IMX
  131. #define UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
  132. #define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
  133. #endif
  134. #if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3
  135. #define UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
  136. #endif
  137. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  138. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  139. #define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */
  140. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  141. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  142. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  143. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  144. #define UCR4_IRSC (1<<5) /* IR special case */
  145. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  146. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  147. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  148. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  149. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  150. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  151. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  152. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  153. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  154. #define USR1_RTSS (1<<14) /* RTS pin status */
  155. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  156. #define USR1_RTSD (1<<12) /* RTS delta */
  157. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  158. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  159. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  160. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  161. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  162. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  163. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  164. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  165. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  166. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  167. #define USR2_IDLE (1<<12) /* Idle condition */
  168. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  169. #define USR2_WAKE (1<<7) /* Wake */
  170. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  171. #define USR2_TXDC (1<<3) /* Transmitter complete */
  172. #define USR2_BRCD (1<<2) /* Break condition */
  173. #define USR2_ORE (1<<1) /* Overrun error */
  174. #define USR2_RDR (1<<0) /* Recv data ready */
  175. #define UTS_FRCPERR (1<<13) /* Force parity error */
  176. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  177. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  178. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  179. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  180. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  181. #define UTS_SOFTRST (1<<0) /* Software reset */
  182. /* We've been assigned a range on the "Low-density serial ports" major */
  183. #ifdef CONFIG_ARCH_IMX
  184. #define SERIAL_IMX_MAJOR 204
  185. #define MINOR_START 41
  186. #define DEV_NAME "ttySMX"
  187. #define MAX_INTERNAL_IRQ IMX_IRQS
  188. #endif
  189. #ifdef CONFIG_ARCH_MXC
  190. #define SERIAL_IMX_MAJOR 207
  191. #define MINOR_START 16
  192. #define DEV_NAME "ttymxc"
  193. #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
  194. #endif
  195. /*
  196. * This determines how often we check the modem status signals
  197. * for any change. They generally aren't connected to an IRQ
  198. * so we have to poll them. We also check immediately before
  199. * filling the TX fifo incase CTS has been dropped.
  200. */
  201. #define MCTRL_TIMEOUT (250*HZ/1000)
  202. #define DRIVER_NAME "IMX-uart"
  203. #define UART_NR 8
  204. struct imx_port {
  205. struct uart_port port;
  206. struct timer_list timer;
  207. unsigned int old_status;
  208. int txirq,rxirq,rtsirq;
  209. unsigned int have_rtscts:1;
  210. unsigned int use_irda:1;
  211. unsigned int irda_inv_rx:1;
  212. unsigned int irda_inv_tx:1;
  213. unsigned short trcv_delay; /* transceiver delay */
  214. struct clk *clk;
  215. };
  216. #ifdef CONFIG_IRDA
  217. #define USE_IRDA(sport) ((sport)->use_irda)
  218. #else
  219. #define USE_IRDA(sport) (0)
  220. #endif
  221. /*
  222. * Handle any change of modem status signal since we were last called.
  223. */
  224. static void imx_mctrl_check(struct imx_port *sport)
  225. {
  226. unsigned int status, changed;
  227. status = sport->port.ops->get_mctrl(&sport->port);
  228. changed = status ^ sport->old_status;
  229. if (changed == 0)
  230. return;
  231. sport->old_status = status;
  232. if (changed & TIOCM_RI)
  233. sport->port.icount.rng++;
  234. if (changed & TIOCM_DSR)
  235. sport->port.icount.dsr++;
  236. if (changed & TIOCM_CAR)
  237. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  238. if (changed & TIOCM_CTS)
  239. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  240. wake_up_interruptible(&sport->port.info->delta_msr_wait);
  241. }
  242. /*
  243. * This is our per-port timeout handler, for checking the
  244. * modem status signals.
  245. */
  246. static void imx_timeout(unsigned long data)
  247. {
  248. struct imx_port *sport = (struct imx_port *)data;
  249. unsigned long flags;
  250. if (sport->port.info) {
  251. spin_lock_irqsave(&sport->port.lock, flags);
  252. imx_mctrl_check(sport);
  253. spin_unlock_irqrestore(&sport->port.lock, flags);
  254. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  255. }
  256. }
  257. /*
  258. * interrupts disabled on entry
  259. */
  260. static void imx_stop_tx(struct uart_port *port)
  261. {
  262. struct imx_port *sport = (struct imx_port *)port;
  263. unsigned long temp;
  264. if (USE_IRDA(sport)) {
  265. /* half duplex - wait for end of transmission */
  266. int n = 256;
  267. while ((--n > 0) &&
  268. !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
  269. udelay(5);
  270. barrier();
  271. }
  272. /*
  273. * irda transceiver - wait a bit more to avoid
  274. * cutoff, hardware dependent
  275. */
  276. udelay(sport->trcv_delay);
  277. /*
  278. * half duplex - reactivate receive mode,
  279. * flush receive pipe echo crap
  280. */
  281. if (readl(sport->port.membase + USR2) & USR2_TXDC) {
  282. temp = readl(sport->port.membase + UCR1);
  283. temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
  284. writel(temp, sport->port.membase + UCR1);
  285. temp = readl(sport->port.membase + UCR4);
  286. temp &= ~(UCR4_TCEN);
  287. writel(temp, sport->port.membase + UCR4);
  288. while (readl(sport->port.membase + URXD0) &
  289. URXD_CHARRDY)
  290. barrier();
  291. temp = readl(sport->port.membase + UCR1);
  292. temp |= UCR1_RRDYEN;
  293. writel(temp, sport->port.membase + UCR1);
  294. temp = readl(sport->port.membase + UCR4);
  295. temp |= UCR4_DREN;
  296. writel(temp, sport->port.membase + UCR4);
  297. }
  298. return;
  299. }
  300. temp = readl(sport->port.membase + UCR1);
  301. writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
  302. }
  303. /*
  304. * interrupts disabled on entry
  305. */
  306. static void imx_stop_rx(struct uart_port *port)
  307. {
  308. struct imx_port *sport = (struct imx_port *)port;
  309. unsigned long temp;
  310. temp = readl(sport->port.membase + UCR2);
  311. writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
  312. }
  313. /*
  314. * Set the modem control timer to fire immediately.
  315. */
  316. static void imx_enable_ms(struct uart_port *port)
  317. {
  318. struct imx_port *sport = (struct imx_port *)port;
  319. mod_timer(&sport->timer, jiffies);
  320. }
  321. static inline void imx_transmit_buffer(struct imx_port *sport)
  322. {
  323. struct circ_buf *xmit = &sport->port.info->xmit;
  324. while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
  325. /* send xmit->buf[xmit->tail]
  326. * out the port here */
  327. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  328. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  329. sport->port.icount.tx++;
  330. if (uart_circ_empty(xmit))
  331. break;
  332. }
  333. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  334. uart_write_wakeup(&sport->port);
  335. if (uart_circ_empty(xmit))
  336. imx_stop_tx(&sport->port);
  337. }
  338. /*
  339. * interrupts disabled on entry
  340. */
  341. static void imx_start_tx(struct uart_port *port)
  342. {
  343. struct imx_port *sport = (struct imx_port *)port;
  344. unsigned long temp;
  345. if (USE_IRDA(sport)) {
  346. /* half duplex in IrDA mode; have to disable receive mode */
  347. temp = readl(sport->port.membase + UCR4);
  348. temp &= ~(UCR4_DREN);
  349. writel(temp, sport->port.membase + UCR4);
  350. temp = readl(sport->port.membase + UCR1);
  351. temp &= ~(UCR1_RRDYEN);
  352. writel(temp, sport->port.membase + UCR1);
  353. }
  354. temp = readl(sport->port.membase + UCR1);
  355. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  356. if (USE_IRDA(sport)) {
  357. temp = readl(sport->port.membase + UCR1);
  358. temp |= UCR1_TRDYEN;
  359. writel(temp, sport->port.membase + UCR1);
  360. temp = readl(sport->port.membase + UCR4);
  361. temp |= UCR4_TCEN;
  362. writel(temp, sport->port.membase + UCR4);
  363. }
  364. if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
  365. imx_transmit_buffer(sport);
  366. }
  367. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  368. {
  369. struct imx_port *sport = dev_id;
  370. unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
  371. unsigned long flags;
  372. spin_lock_irqsave(&sport->port.lock, flags);
  373. writel(USR1_RTSD, sport->port.membase + USR1);
  374. uart_handle_cts_change(&sport->port, !!val);
  375. wake_up_interruptible(&sport->port.info->delta_msr_wait);
  376. spin_unlock_irqrestore(&sport->port.lock, flags);
  377. return IRQ_HANDLED;
  378. }
  379. static irqreturn_t imx_txint(int irq, void *dev_id)
  380. {
  381. struct imx_port *sport = dev_id;
  382. struct circ_buf *xmit = &sport->port.info->xmit;
  383. unsigned long flags;
  384. spin_lock_irqsave(&sport->port.lock,flags);
  385. if (sport->port.x_char)
  386. {
  387. /* Send next char */
  388. writel(sport->port.x_char, sport->port.membase + URTX0);
  389. goto out;
  390. }
  391. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  392. imx_stop_tx(&sport->port);
  393. goto out;
  394. }
  395. imx_transmit_buffer(sport);
  396. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  397. uart_write_wakeup(&sport->port);
  398. out:
  399. spin_unlock_irqrestore(&sport->port.lock,flags);
  400. return IRQ_HANDLED;
  401. }
  402. static irqreturn_t imx_rxint(int irq, void *dev_id)
  403. {
  404. struct imx_port *sport = dev_id;
  405. unsigned int rx,flg,ignored = 0;
  406. struct tty_struct *tty = sport->port.info->port.tty;
  407. unsigned long flags, temp;
  408. spin_lock_irqsave(&sport->port.lock,flags);
  409. while (readl(sport->port.membase + USR2) & USR2_RDR) {
  410. flg = TTY_NORMAL;
  411. sport->port.icount.rx++;
  412. rx = readl(sport->port.membase + URXD0);
  413. temp = readl(sport->port.membase + USR2);
  414. if (temp & USR2_BRCD) {
  415. writel(temp | USR2_BRCD, sport->port.membase + USR2);
  416. if (uart_handle_break(&sport->port))
  417. continue;
  418. }
  419. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  420. continue;
  421. if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
  422. if (rx & URXD_PRERR)
  423. sport->port.icount.parity++;
  424. else if (rx & URXD_FRMERR)
  425. sport->port.icount.frame++;
  426. if (rx & URXD_OVRRUN)
  427. sport->port.icount.overrun++;
  428. if (rx & sport->port.ignore_status_mask) {
  429. if (++ignored > 100)
  430. goto out;
  431. continue;
  432. }
  433. rx &= sport->port.read_status_mask;
  434. if (rx & URXD_PRERR)
  435. flg = TTY_PARITY;
  436. else if (rx & URXD_FRMERR)
  437. flg = TTY_FRAME;
  438. if (rx & URXD_OVRRUN)
  439. flg = TTY_OVERRUN;
  440. #ifdef SUPPORT_SYSRQ
  441. sport->port.sysrq = 0;
  442. #endif
  443. }
  444. tty_insert_flip_char(tty, rx, flg);
  445. }
  446. out:
  447. spin_unlock_irqrestore(&sport->port.lock,flags);
  448. tty_flip_buffer_push(tty);
  449. return IRQ_HANDLED;
  450. }
  451. static irqreturn_t imx_int(int irq, void *dev_id)
  452. {
  453. struct imx_port *sport = dev_id;
  454. unsigned int sts;
  455. sts = readl(sport->port.membase + USR1);
  456. if (sts & USR1_RRDY)
  457. imx_rxint(irq, dev_id);
  458. if (sts & USR1_TRDY &&
  459. readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
  460. imx_txint(irq, dev_id);
  461. if (sts & USR1_RTSD)
  462. imx_rtsint(irq, dev_id);
  463. return IRQ_HANDLED;
  464. }
  465. /*
  466. * Return TIOCSER_TEMT when transmitter is not busy.
  467. */
  468. static unsigned int imx_tx_empty(struct uart_port *port)
  469. {
  470. struct imx_port *sport = (struct imx_port *)port;
  471. return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  472. }
  473. /*
  474. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  475. */
  476. static unsigned int imx_get_mctrl(struct uart_port *port)
  477. {
  478. struct imx_port *sport = (struct imx_port *)port;
  479. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  480. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  481. tmp |= TIOCM_CTS;
  482. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  483. tmp |= TIOCM_RTS;
  484. return tmp;
  485. }
  486. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  487. {
  488. struct imx_port *sport = (struct imx_port *)port;
  489. unsigned long temp;
  490. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  491. if (mctrl & TIOCM_RTS)
  492. temp |= UCR2_CTS;
  493. writel(temp, sport->port.membase + UCR2);
  494. }
  495. /*
  496. * Interrupts always disabled.
  497. */
  498. static void imx_break_ctl(struct uart_port *port, int break_state)
  499. {
  500. struct imx_port *sport = (struct imx_port *)port;
  501. unsigned long flags, temp;
  502. spin_lock_irqsave(&sport->port.lock, flags);
  503. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  504. if ( break_state != 0 )
  505. temp |= UCR1_SNDBRK;
  506. writel(temp, sport->port.membase + UCR1);
  507. spin_unlock_irqrestore(&sport->port.lock, flags);
  508. }
  509. #define TXTL 2 /* reset default */
  510. #define RXTL 1 /* reset default */
  511. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  512. {
  513. unsigned int val;
  514. unsigned int ufcr_rfdiv;
  515. /* set receiver / transmitter trigger level.
  516. * RFDIV is set such way to satisfy requested uartclk value
  517. */
  518. val = TXTL << 10 | RXTL;
  519. ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
  520. / sport->port.uartclk;
  521. if(!ufcr_rfdiv)
  522. ufcr_rfdiv = 1;
  523. val |= UFCR_RFDIV_REG(ufcr_rfdiv);
  524. writel(val, sport->port.membase + UFCR);
  525. return 0;
  526. }
  527. static int imx_startup(struct uart_port *port)
  528. {
  529. struct imx_port *sport = (struct imx_port *)port;
  530. int retval;
  531. unsigned long flags, temp;
  532. imx_setup_ufcr(sport, 0);
  533. /* disable the DREN bit (Data Ready interrupt enable) before
  534. * requesting IRQs
  535. */
  536. temp = readl(sport->port.membase + UCR4);
  537. if (USE_IRDA(sport))
  538. temp |= UCR4_IRSC;
  539. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  540. if (USE_IRDA(sport)) {
  541. /* reset fifo's and state machines */
  542. int i = 100;
  543. temp = readl(sport->port.membase + UCR2);
  544. temp &= ~UCR2_SRST;
  545. writel(temp, sport->port.membase + UCR2);
  546. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  547. (--i > 0)) {
  548. udelay(1);
  549. }
  550. }
  551. /*
  552. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  553. * chips only have one interrupt.
  554. */
  555. if (sport->txirq > 0) {
  556. retval = request_irq(sport->rxirq, imx_rxint, 0,
  557. DRIVER_NAME, sport);
  558. if (retval)
  559. goto error_out1;
  560. retval = request_irq(sport->txirq, imx_txint, 0,
  561. DRIVER_NAME, sport);
  562. if (retval)
  563. goto error_out2;
  564. /* do not use RTS IRQ on IrDA */
  565. if (!USE_IRDA(sport)) {
  566. retval = request_irq(sport->rtsirq, imx_rtsint,
  567. (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
  568. IRQF_TRIGGER_FALLING |
  569. IRQF_TRIGGER_RISING,
  570. DRIVER_NAME, sport);
  571. if (retval)
  572. goto error_out3;
  573. }
  574. } else {
  575. retval = request_irq(sport->port.irq, imx_int, 0,
  576. DRIVER_NAME, sport);
  577. if (retval) {
  578. free_irq(sport->port.irq, sport);
  579. goto error_out1;
  580. }
  581. }
  582. /*
  583. * Finally, clear and enable interrupts
  584. */
  585. writel(USR1_RTSD, sport->port.membase + USR1);
  586. temp = readl(sport->port.membase + UCR1);
  587. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  588. if (USE_IRDA(sport)) {
  589. temp |= UCR1_IREN;
  590. temp &= ~(UCR1_RTSDEN);
  591. }
  592. writel(temp, sport->port.membase + UCR1);
  593. temp = readl(sport->port.membase + UCR2);
  594. temp |= (UCR2_RXEN | UCR2_TXEN);
  595. writel(temp, sport->port.membase + UCR2);
  596. if (USE_IRDA(sport)) {
  597. /* clear RX-FIFO */
  598. int i = 64;
  599. while ((--i > 0) &&
  600. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  601. barrier();
  602. }
  603. }
  604. #if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3
  605. temp = readl(sport->port.membase + UCR3);
  606. temp |= UCR3_RXDMUXSEL;
  607. writel(temp, sport->port.membase + UCR3);
  608. #endif
  609. if (USE_IRDA(sport)) {
  610. temp = readl(sport->port.membase + UCR4);
  611. if (sport->irda_inv_rx)
  612. temp |= UCR4_INVR;
  613. else
  614. temp &= ~(UCR4_INVR);
  615. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  616. temp = readl(sport->port.membase + UCR3);
  617. if (sport->irda_inv_tx)
  618. temp |= UCR3_INVT;
  619. else
  620. temp &= ~(UCR3_INVT);
  621. writel(temp, sport->port.membase + UCR3);
  622. }
  623. /*
  624. * Enable modem status interrupts
  625. */
  626. spin_lock_irqsave(&sport->port.lock,flags);
  627. imx_enable_ms(&sport->port);
  628. spin_unlock_irqrestore(&sport->port.lock,flags);
  629. if (USE_IRDA(sport)) {
  630. struct imxuart_platform_data *pdata;
  631. pdata = sport->port.dev->platform_data;
  632. sport->irda_inv_rx = pdata->irda_inv_rx;
  633. sport->irda_inv_tx = pdata->irda_inv_tx;
  634. sport->trcv_delay = pdata->transceiver_delay;
  635. if (pdata->irda_enable)
  636. pdata->irda_enable(1);
  637. }
  638. return 0;
  639. error_out3:
  640. if (sport->txirq)
  641. free_irq(sport->txirq, sport);
  642. error_out2:
  643. if (sport->rxirq)
  644. free_irq(sport->rxirq, sport);
  645. error_out1:
  646. return retval;
  647. }
  648. static void imx_shutdown(struct uart_port *port)
  649. {
  650. struct imx_port *sport = (struct imx_port *)port;
  651. unsigned long temp;
  652. temp = readl(sport->port.membase + UCR2);
  653. temp &= ~(UCR2_TXEN);
  654. writel(temp, sport->port.membase + UCR2);
  655. if (USE_IRDA(sport)) {
  656. struct imxuart_platform_data *pdata;
  657. pdata = sport->port.dev->platform_data;
  658. if (pdata->irda_enable)
  659. pdata->irda_enable(0);
  660. }
  661. /*
  662. * Stop our timer.
  663. */
  664. del_timer_sync(&sport->timer);
  665. /*
  666. * Free the interrupts
  667. */
  668. if (sport->txirq > 0) {
  669. if (!USE_IRDA(sport))
  670. free_irq(sport->rtsirq, sport);
  671. free_irq(sport->txirq, sport);
  672. free_irq(sport->rxirq, sport);
  673. } else
  674. free_irq(sport->port.irq, sport);
  675. /*
  676. * Disable all interrupts, port and break condition.
  677. */
  678. temp = readl(sport->port.membase + UCR1);
  679. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  680. if (USE_IRDA(sport))
  681. temp &= ~(UCR1_IREN);
  682. writel(temp, sport->port.membase + UCR1);
  683. }
  684. static void
  685. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  686. struct ktermios *old)
  687. {
  688. struct imx_port *sport = (struct imx_port *)port;
  689. unsigned long flags;
  690. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  691. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  692. unsigned int div, ufcr;
  693. unsigned long num, denom;
  694. uint64_t tdiv64;
  695. /*
  696. * If we don't support modem control lines, don't allow
  697. * these to be set.
  698. */
  699. if (0) {
  700. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  701. termios->c_cflag |= CLOCAL;
  702. }
  703. /*
  704. * We only support CS7 and CS8.
  705. */
  706. while ((termios->c_cflag & CSIZE) != CS7 &&
  707. (termios->c_cflag & CSIZE) != CS8) {
  708. termios->c_cflag &= ~CSIZE;
  709. termios->c_cflag |= old_csize;
  710. old_csize = CS8;
  711. }
  712. if ((termios->c_cflag & CSIZE) == CS8)
  713. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  714. else
  715. ucr2 = UCR2_SRST | UCR2_IRTS;
  716. if (termios->c_cflag & CRTSCTS) {
  717. if( sport->have_rtscts ) {
  718. ucr2 &= ~UCR2_IRTS;
  719. ucr2 |= UCR2_CTSC;
  720. } else {
  721. termios->c_cflag &= ~CRTSCTS;
  722. }
  723. }
  724. if (termios->c_cflag & CSTOPB)
  725. ucr2 |= UCR2_STPB;
  726. if (termios->c_cflag & PARENB) {
  727. ucr2 |= UCR2_PREN;
  728. if (termios->c_cflag & PARODD)
  729. ucr2 |= UCR2_PROE;
  730. }
  731. /*
  732. * Ask the core to calculate the divisor for us.
  733. */
  734. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  735. quot = uart_get_divisor(port, baud);
  736. spin_lock_irqsave(&sport->port.lock, flags);
  737. sport->port.read_status_mask = 0;
  738. if (termios->c_iflag & INPCK)
  739. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  740. if (termios->c_iflag & (BRKINT | PARMRK))
  741. sport->port.read_status_mask |= URXD_BRK;
  742. /*
  743. * Characters to ignore
  744. */
  745. sport->port.ignore_status_mask = 0;
  746. if (termios->c_iflag & IGNPAR)
  747. sport->port.ignore_status_mask |= URXD_PRERR;
  748. if (termios->c_iflag & IGNBRK) {
  749. sport->port.ignore_status_mask |= URXD_BRK;
  750. /*
  751. * If we're ignoring parity and break indicators,
  752. * ignore overruns too (for real raw support).
  753. */
  754. if (termios->c_iflag & IGNPAR)
  755. sport->port.ignore_status_mask |= URXD_OVRRUN;
  756. }
  757. del_timer_sync(&sport->timer);
  758. /*
  759. * Update the per-port timeout.
  760. */
  761. uart_update_timeout(port, termios->c_cflag, baud);
  762. /*
  763. * disable interrupts and drain transmitter
  764. */
  765. old_ucr1 = readl(sport->port.membase + UCR1);
  766. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  767. sport->port.membase + UCR1);
  768. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  769. barrier();
  770. /* then, disable everything */
  771. old_txrxen = readl(sport->port.membase + UCR2);
  772. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  773. sport->port.membase + UCR2);
  774. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  775. if (USE_IRDA(sport)) {
  776. /*
  777. * use maximum available submodule frequency to
  778. * avoid missing short pulses due to low sampling rate
  779. */
  780. div = 1;
  781. } else {
  782. div = sport->port.uartclk / (baud * 16);
  783. if (div > 7)
  784. div = 7;
  785. if (!div)
  786. div = 1;
  787. }
  788. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  789. 1 << 16, 1 << 16, &num, &denom);
  790. tdiv64 = sport->port.uartclk;
  791. tdiv64 *= num;
  792. do_div(tdiv64, denom * 16 * div);
  793. tty_encode_baud_rate(sport->port.info->port.tty,
  794. (speed_t)tdiv64, (speed_t)tdiv64);
  795. num -= 1;
  796. denom -= 1;
  797. ufcr = readl(sport->port.membase + UFCR);
  798. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  799. writel(ufcr, sport->port.membase + UFCR);
  800. writel(num, sport->port.membase + UBIR);
  801. writel(denom, sport->port.membase + UBMR);
  802. #ifdef ONEMS
  803. writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
  804. #endif
  805. writel(old_ucr1, sport->port.membase + UCR1);
  806. /* set the parity, stop bits and data size */
  807. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  808. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  809. imx_enable_ms(&sport->port);
  810. spin_unlock_irqrestore(&sport->port.lock, flags);
  811. }
  812. static const char *imx_type(struct uart_port *port)
  813. {
  814. struct imx_port *sport = (struct imx_port *)port;
  815. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  816. }
  817. /*
  818. * Release the memory region(s) being used by 'port'.
  819. */
  820. static void imx_release_port(struct uart_port *port)
  821. {
  822. struct platform_device *pdev = to_platform_device(port->dev);
  823. struct resource *mmres;
  824. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  825. release_mem_region(mmres->start, mmres->end - mmres->start + 1);
  826. }
  827. /*
  828. * Request the memory region(s) being used by 'port'.
  829. */
  830. static int imx_request_port(struct uart_port *port)
  831. {
  832. struct platform_device *pdev = to_platform_device(port->dev);
  833. struct resource *mmres;
  834. void *ret;
  835. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  836. if (!mmres)
  837. return -ENODEV;
  838. ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
  839. "imx-uart");
  840. return ret ? 0 : -EBUSY;
  841. }
  842. /*
  843. * Configure/autoconfigure the port.
  844. */
  845. static void imx_config_port(struct uart_port *port, int flags)
  846. {
  847. struct imx_port *sport = (struct imx_port *)port;
  848. if (flags & UART_CONFIG_TYPE &&
  849. imx_request_port(&sport->port) == 0)
  850. sport->port.type = PORT_IMX;
  851. }
  852. /*
  853. * Verify the new serial_struct (for TIOCSSERIAL).
  854. * The only change we allow are to the flags and type, and
  855. * even then only between PORT_IMX and PORT_UNKNOWN
  856. */
  857. static int
  858. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  859. {
  860. struct imx_port *sport = (struct imx_port *)port;
  861. int ret = 0;
  862. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  863. ret = -EINVAL;
  864. if (sport->port.irq != ser->irq)
  865. ret = -EINVAL;
  866. if (ser->io_type != UPIO_MEM)
  867. ret = -EINVAL;
  868. if (sport->port.uartclk / 16 != ser->baud_base)
  869. ret = -EINVAL;
  870. if ((void *)sport->port.mapbase != ser->iomem_base)
  871. ret = -EINVAL;
  872. if (sport->port.iobase != ser->port)
  873. ret = -EINVAL;
  874. if (ser->hub6 != 0)
  875. ret = -EINVAL;
  876. return ret;
  877. }
  878. static struct uart_ops imx_pops = {
  879. .tx_empty = imx_tx_empty,
  880. .set_mctrl = imx_set_mctrl,
  881. .get_mctrl = imx_get_mctrl,
  882. .stop_tx = imx_stop_tx,
  883. .start_tx = imx_start_tx,
  884. .stop_rx = imx_stop_rx,
  885. .enable_ms = imx_enable_ms,
  886. .break_ctl = imx_break_ctl,
  887. .startup = imx_startup,
  888. .shutdown = imx_shutdown,
  889. .set_termios = imx_set_termios,
  890. .type = imx_type,
  891. .release_port = imx_release_port,
  892. .request_port = imx_request_port,
  893. .config_port = imx_config_port,
  894. .verify_port = imx_verify_port,
  895. };
  896. static struct imx_port *imx_ports[UART_NR];
  897. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  898. static void imx_console_putchar(struct uart_port *port, int ch)
  899. {
  900. struct imx_port *sport = (struct imx_port *)port;
  901. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  902. barrier();
  903. writel(ch, sport->port.membase + URTX0);
  904. }
  905. /*
  906. * Interrupts are disabled on entering
  907. */
  908. static void
  909. imx_console_write(struct console *co, const char *s, unsigned int count)
  910. {
  911. struct imx_port *sport = imx_ports[co->index];
  912. unsigned int old_ucr1, old_ucr2;
  913. /*
  914. * First, save UCR1/2 and then disable interrupts
  915. */
  916. old_ucr1 = readl(sport->port.membase + UCR1);
  917. old_ucr2 = readl(sport->port.membase + UCR2);
  918. writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
  919. ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  920. sport->port.membase + UCR1);
  921. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  922. uart_console_write(&sport->port, s, count, imx_console_putchar);
  923. /*
  924. * Finally, wait for transmitter to become empty
  925. * and restore UCR1/2
  926. */
  927. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  928. writel(old_ucr1, sport->port.membase + UCR1);
  929. writel(old_ucr2, sport->port.membase + UCR2);
  930. }
  931. /*
  932. * If the port was already initialised (eg, by a boot loader),
  933. * try to determine the current setup.
  934. */
  935. static void __init
  936. imx_console_get_options(struct imx_port *sport, int *baud,
  937. int *parity, int *bits)
  938. {
  939. if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
  940. /* ok, the port was enabled */
  941. unsigned int ucr2, ubir,ubmr, uartclk;
  942. unsigned int baud_raw;
  943. unsigned int ucfr_rfdiv;
  944. ucr2 = readl(sport->port.membase + UCR2);
  945. *parity = 'n';
  946. if (ucr2 & UCR2_PREN) {
  947. if (ucr2 & UCR2_PROE)
  948. *parity = 'o';
  949. else
  950. *parity = 'e';
  951. }
  952. if (ucr2 & UCR2_WS)
  953. *bits = 8;
  954. else
  955. *bits = 7;
  956. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  957. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  958. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  959. if (ucfr_rfdiv == 6)
  960. ucfr_rfdiv = 7;
  961. else
  962. ucfr_rfdiv = 6 - ucfr_rfdiv;
  963. uartclk = clk_get_rate(sport->clk);
  964. uartclk /= ucfr_rfdiv;
  965. { /*
  966. * The next code provides exact computation of
  967. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  968. * without need of float support or long long division,
  969. * which would be required to prevent 32bit arithmetic overflow
  970. */
  971. unsigned int mul = ubir + 1;
  972. unsigned int div = 16 * (ubmr + 1);
  973. unsigned int rem = uartclk % div;
  974. baud_raw = (uartclk / div) * mul;
  975. baud_raw += (rem * mul + div / 2) / div;
  976. *baud = (baud_raw + 50) / 100 * 100;
  977. }
  978. if(*baud != baud_raw)
  979. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  980. baud_raw, *baud);
  981. }
  982. }
  983. static int __init
  984. imx_console_setup(struct console *co, char *options)
  985. {
  986. struct imx_port *sport;
  987. int baud = 9600;
  988. int bits = 8;
  989. int parity = 'n';
  990. int flow = 'n';
  991. /*
  992. * Check whether an invalid uart number has been specified, and
  993. * if so, search for the first available port that does have
  994. * console support.
  995. */
  996. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  997. co->index = 0;
  998. sport = imx_ports[co->index];
  999. if(sport == NULL)
  1000. return -ENODEV;
  1001. if (options)
  1002. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1003. else
  1004. imx_console_get_options(sport, &baud, &parity, &bits);
  1005. imx_setup_ufcr(sport, 0);
  1006. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1007. }
  1008. static struct uart_driver imx_reg;
  1009. static struct console imx_console = {
  1010. .name = DEV_NAME,
  1011. .write = imx_console_write,
  1012. .device = uart_console_device,
  1013. .setup = imx_console_setup,
  1014. .flags = CON_PRINTBUFFER,
  1015. .index = -1,
  1016. .data = &imx_reg,
  1017. };
  1018. #define IMX_CONSOLE &imx_console
  1019. #else
  1020. #define IMX_CONSOLE NULL
  1021. #endif
  1022. static struct uart_driver imx_reg = {
  1023. .owner = THIS_MODULE,
  1024. .driver_name = DRIVER_NAME,
  1025. .dev_name = DEV_NAME,
  1026. .major = SERIAL_IMX_MAJOR,
  1027. .minor = MINOR_START,
  1028. .nr = ARRAY_SIZE(imx_ports),
  1029. .cons = IMX_CONSOLE,
  1030. };
  1031. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1032. {
  1033. struct imx_port *sport = platform_get_drvdata(dev);
  1034. if (sport)
  1035. uart_suspend_port(&imx_reg, &sport->port);
  1036. return 0;
  1037. }
  1038. static int serial_imx_resume(struct platform_device *dev)
  1039. {
  1040. struct imx_port *sport = platform_get_drvdata(dev);
  1041. if (sport)
  1042. uart_resume_port(&imx_reg, &sport->port);
  1043. return 0;
  1044. }
  1045. static int serial_imx_probe(struct platform_device *pdev)
  1046. {
  1047. struct imx_port *sport;
  1048. struct imxuart_platform_data *pdata;
  1049. void __iomem *base;
  1050. int ret = 0;
  1051. struct resource *res;
  1052. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1053. if (!sport)
  1054. return -ENOMEM;
  1055. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1056. if (!res) {
  1057. ret = -ENODEV;
  1058. goto free;
  1059. }
  1060. base = ioremap(res->start, PAGE_SIZE);
  1061. if (!base) {
  1062. ret = -ENOMEM;
  1063. goto free;
  1064. }
  1065. sport->port.dev = &pdev->dev;
  1066. sport->port.mapbase = res->start;
  1067. sport->port.membase = base;
  1068. sport->port.type = PORT_IMX,
  1069. sport->port.iotype = UPIO_MEM;
  1070. sport->port.irq = platform_get_irq(pdev, 0);
  1071. sport->rxirq = platform_get_irq(pdev, 0);
  1072. sport->txirq = platform_get_irq(pdev, 1);
  1073. sport->rtsirq = platform_get_irq(pdev, 2);
  1074. sport->port.fifosize = 32;
  1075. sport->port.ops = &imx_pops;
  1076. sport->port.flags = UPF_BOOT_AUTOCONF;
  1077. sport->port.line = pdev->id;
  1078. init_timer(&sport->timer);
  1079. sport->timer.function = imx_timeout;
  1080. sport->timer.data = (unsigned long)sport;
  1081. sport->clk = clk_get(&pdev->dev, "uart");
  1082. if (IS_ERR(sport->clk)) {
  1083. ret = PTR_ERR(sport->clk);
  1084. goto unmap;
  1085. }
  1086. clk_enable(sport->clk);
  1087. sport->port.uartclk = clk_get_rate(sport->clk);
  1088. imx_ports[pdev->id] = sport;
  1089. pdata = pdev->dev.platform_data;
  1090. if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  1091. sport->have_rtscts = 1;
  1092. #ifdef CONFIG_IRDA
  1093. if (pdata && (pdata->flags & IMXUART_IRDA))
  1094. sport->use_irda = 1;
  1095. #endif
  1096. if (pdata->init) {
  1097. ret = pdata->init(pdev);
  1098. if (ret)
  1099. goto clkput;
  1100. }
  1101. ret = uart_add_one_port(&imx_reg, &sport->port);
  1102. if (ret)
  1103. goto deinit;
  1104. platform_set_drvdata(pdev, &sport->port);
  1105. return 0;
  1106. deinit:
  1107. if (pdata->exit)
  1108. pdata->exit(pdev);
  1109. clkput:
  1110. clk_put(sport->clk);
  1111. clk_disable(sport->clk);
  1112. unmap:
  1113. iounmap(sport->port.membase);
  1114. free:
  1115. kfree(sport);
  1116. return ret;
  1117. }
  1118. static int serial_imx_remove(struct platform_device *pdev)
  1119. {
  1120. struct imxuart_platform_data *pdata;
  1121. struct imx_port *sport = platform_get_drvdata(pdev);
  1122. pdata = pdev->dev.platform_data;
  1123. platform_set_drvdata(pdev, NULL);
  1124. if (sport) {
  1125. uart_remove_one_port(&imx_reg, &sport->port);
  1126. clk_put(sport->clk);
  1127. }
  1128. clk_disable(sport->clk);
  1129. if (pdata->exit)
  1130. pdata->exit(pdev);
  1131. iounmap(sport->port.membase);
  1132. kfree(sport);
  1133. return 0;
  1134. }
  1135. static struct platform_driver serial_imx_driver = {
  1136. .probe = serial_imx_probe,
  1137. .remove = serial_imx_remove,
  1138. .suspend = serial_imx_suspend,
  1139. .resume = serial_imx_resume,
  1140. .driver = {
  1141. .name = "imx-uart",
  1142. .owner = THIS_MODULE,
  1143. },
  1144. };
  1145. static int __init imx_serial_init(void)
  1146. {
  1147. int ret;
  1148. printk(KERN_INFO "Serial: IMX driver\n");
  1149. ret = uart_register_driver(&imx_reg);
  1150. if (ret)
  1151. return ret;
  1152. ret = platform_driver_register(&serial_imx_driver);
  1153. if (ret != 0)
  1154. uart_unregister_driver(&imx_reg);
  1155. return 0;
  1156. }
  1157. static void __exit imx_serial_exit(void)
  1158. {
  1159. platform_driver_unregister(&serial_imx_driver);
  1160. uart_unregister_driver(&imx_reg);
  1161. }
  1162. module_init(imx_serial_init);
  1163. module_exit(imx_serial_exit);
  1164. MODULE_AUTHOR("Sascha Hauer");
  1165. MODULE_DESCRIPTION("IMX generic serial port driver");
  1166. MODULE_LICENSE("GPL");
  1167. MODULE_ALIAS("platform:imx-uart");