imx.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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. /*
  695. * If we don't support modem control lines, don't allow
  696. * these to be set.
  697. */
  698. if (0) {
  699. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  700. termios->c_cflag |= CLOCAL;
  701. }
  702. /*
  703. * We only support CS7 and CS8.
  704. */
  705. while ((termios->c_cflag & CSIZE) != CS7 &&
  706. (termios->c_cflag & CSIZE) != CS8) {
  707. termios->c_cflag &= ~CSIZE;
  708. termios->c_cflag |= old_csize;
  709. old_csize = CS8;
  710. }
  711. if ((termios->c_cflag & CSIZE) == CS8)
  712. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  713. else
  714. ucr2 = UCR2_SRST | UCR2_IRTS;
  715. if (termios->c_cflag & CRTSCTS) {
  716. if( sport->have_rtscts ) {
  717. ucr2 &= ~UCR2_IRTS;
  718. ucr2 |= UCR2_CTSC;
  719. } else {
  720. termios->c_cflag &= ~CRTSCTS;
  721. }
  722. }
  723. if (termios->c_cflag & CSTOPB)
  724. ucr2 |= UCR2_STPB;
  725. if (termios->c_cflag & PARENB) {
  726. ucr2 |= UCR2_PREN;
  727. if (termios->c_cflag & PARODD)
  728. ucr2 |= UCR2_PROE;
  729. }
  730. /*
  731. * Ask the core to calculate the divisor for us.
  732. */
  733. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  734. quot = uart_get_divisor(port, baud);
  735. spin_lock_irqsave(&sport->port.lock, flags);
  736. sport->port.read_status_mask = 0;
  737. if (termios->c_iflag & INPCK)
  738. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  739. if (termios->c_iflag & (BRKINT | PARMRK))
  740. sport->port.read_status_mask |= URXD_BRK;
  741. /*
  742. * Characters to ignore
  743. */
  744. sport->port.ignore_status_mask = 0;
  745. if (termios->c_iflag & IGNPAR)
  746. sport->port.ignore_status_mask |= URXD_PRERR;
  747. if (termios->c_iflag & IGNBRK) {
  748. sport->port.ignore_status_mask |= URXD_BRK;
  749. /*
  750. * If we're ignoring parity and break indicators,
  751. * ignore overruns too (for real raw support).
  752. */
  753. if (termios->c_iflag & IGNPAR)
  754. sport->port.ignore_status_mask |= URXD_OVRRUN;
  755. }
  756. del_timer_sync(&sport->timer);
  757. /*
  758. * Update the per-port timeout.
  759. */
  760. uart_update_timeout(port, termios->c_cflag, baud);
  761. /*
  762. * disable interrupts and drain transmitter
  763. */
  764. old_ucr1 = readl(sport->port.membase + UCR1);
  765. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  766. sport->port.membase + UCR1);
  767. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  768. barrier();
  769. /* then, disable everything */
  770. old_txrxen = readl(sport->port.membase + UCR2);
  771. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  772. sport->port.membase + UCR2);
  773. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  774. if (USE_IRDA(sport)) {
  775. /*
  776. * use maximum available submodule frequency to
  777. * avoid missing short pulses due to low sampling rate
  778. */
  779. div = 1;
  780. } else {
  781. div = sport->port.uartclk / (baud * 16);
  782. if (div > 7)
  783. div = 7;
  784. if (!div)
  785. div = 1;
  786. }
  787. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  788. 1 << 16, 1 << 16, &num, &denom);
  789. num -= 1;
  790. denom -= 1;
  791. ufcr = readl(sport->port.membase + UFCR);
  792. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  793. writel(ufcr, sport->port.membase + UFCR);
  794. writel(num, sport->port.membase + UBIR);
  795. writel(denom, sport->port.membase + UBMR);
  796. #ifdef ONEMS
  797. writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
  798. #endif
  799. writel(old_ucr1, sport->port.membase + UCR1);
  800. /* set the parity, stop bits and data size */
  801. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  802. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  803. imx_enable_ms(&sport->port);
  804. spin_unlock_irqrestore(&sport->port.lock, flags);
  805. }
  806. static const char *imx_type(struct uart_port *port)
  807. {
  808. struct imx_port *sport = (struct imx_port *)port;
  809. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  810. }
  811. /*
  812. * Release the memory region(s) being used by 'port'.
  813. */
  814. static void imx_release_port(struct uart_port *port)
  815. {
  816. struct platform_device *pdev = to_platform_device(port->dev);
  817. struct resource *mmres;
  818. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  819. release_mem_region(mmres->start, mmres->end - mmres->start + 1);
  820. }
  821. /*
  822. * Request the memory region(s) being used by 'port'.
  823. */
  824. static int imx_request_port(struct uart_port *port)
  825. {
  826. struct platform_device *pdev = to_platform_device(port->dev);
  827. struct resource *mmres;
  828. void *ret;
  829. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  830. if (!mmres)
  831. return -ENODEV;
  832. ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
  833. "imx-uart");
  834. return ret ? 0 : -EBUSY;
  835. }
  836. /*
  837. * Configure/autoconfigure the port.
  838. */
  839. static void imx_config_port(struct uart_port *port, int flags)
  840. {
  841. struct imx_port *sport = (struct imx_port *)port;
  842. if (flags & UART_CONFIG_TYPE &&
  843. imx_request_port(&sport->port) == 0)
  844. sport->port.type = PORT_IMX;
  845. }
  846. /*
  847. * Verify the new serial_struct (for TIOCSSERIAL).
  848. * The only change we allow are to the flags and type, and
  849. * even then only between PORT_IMX and PORT_UNKNOWN
  850. */
  851. static int
  852. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  853. {
  854. struct imx_port *sport = (struct imx_port *)port;
  855. int ret = 0;
  856. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  857. ret = -EINVAL;
  858. if (sport->port.irq != ser->irq)
  859. ret = -EINVAL;
  860. if (ser->io_type != UPIO_MEM)
  861. ret = -EINVAL;
  862. if (sport->port.uartclk / 16 != ser->baud_base)
  863. ret = -EINVAL;
  864. if ((void *)sport->port.mapbase != ser->iomem_base)
  865. ret = -EINVAL;
  866. if (sport->port.iobase != ser->port)
  867. ret = -EINVAL;
  868. if (ser->hub6 != 0)
  869. ret = -EINVAL;
  870. return ret;
  871. }
  872. static struct uart_ops imx_pops = {
  873. .tx_empty = imx_tx_empty,
  874. .set_mctrl = imx_set_mctrl,
  875. .get_mctrl = imx_get_mctrl,
  876. .stop_tx = imx_stop_tx,
  877. .start_tx = imx_start_tx,
  878. .stop_rx = imx_stop_rx,
  879. .enable_ms = imx_enable_ms,
  880. .break_ctl = imx_break_ctl,
  881. .startup = imx_startup,
  882. .shutdown = imx_shutdown,
  883. .set_termios = imx_set_termios,
  884. .type = imx_type,
  885. .release_port = imx_release_port,
  886. .request_port = imx_request_port,
  887. .config_port = imx_config_port,
  888. .verify_port = imx_verify_port,
  889. };
  890. static struct imx_port *imx_ports[UART_NR];
  891. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  892. static void imx_console_putchar(struct uart_port *port, int ch)
  893. {
  894. struct imx_port *sport = (struct imx_port *)port;
  895. while (readl(sport->port.membase + UTS) & UTS_TXFULL)
  896. barrier();
  897. writel(ch, sport->port.membase + URTX0);
  898. }
  899. /*
  900. * Interrupts are disabled on entering
  901. */
  902. static void
  903. imx_console_write(struct console *co, const char *s, unsigned int count)
  904. {
  905. struct imx_port *sport = imx_ports[co->index];
  906. unsigned int old_ucr1, old_ucr2;
  907. /*
  908. * First, save UCR1/2 and then disable interrupts
  909. */
  910. old_ucr1 = readl(sport->port.membase + UCR1);
  911. old_ucr2 = readl(sport->port.membase + UCR2);
  912. writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
  913. ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  914. sport->port.membase + UCR1);
  915. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  916. uart_console_write(&sport->port, s, count, imx_console_putchar);
  917. /*
  918. * Finally, wait for transmitter to become empty
  919. * and restore UCR1/2
  920. */
  921. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  922. writel(old_ucr1, sport->port.membase + UCR1);
  923. writel(old_ucr2, sport->port.membase + UCR2);
  924. }
  925. /*
  926. * If the port was already initialised (eg, by a boot loader),
  927. * try to determine the current setup.
  928. */
  929. static void __init
  930. imx_console_get_options(struct imx_port *sport, int *baud,
  931. int *parity, int *bits)
  932. {
  933. if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
  934. /* ok, the port was enabled */
  935. unsigned int ucr2, ubir,ubmr, uartclk;
  936. unsigned int baud_raw;
  937. unsigned int ucfr_rfdiv;
  938. ucr2 = readl(sport->port.membase + UCR2);
  939. *parity = 'n';
  940. if (ucr2 & UCR2_PREN) {
  941. if (ucr2 & UCR2_PROE)
  942. *parity = 'o';
  943. else
  944. *parity = 'e';
  945. }
  946. if (ucr2 & UCR2_WS)
  947. *bits = 8;
  948. else
  949. *bits = 7;
  950. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  951. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  952. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  953. if (ucfr_rfdiv == 6)
  954. ucfr_rfdiv = 7;
  955. else
  956. ucfr_rfdiv = 6 - ucfr_rfdiv;
  957. uartclk = clk_get_rate(sport->clk);
  958. uartclk /= ucfr_rfdiv;
  959. { /*
  960. * The next code provides exact computation of
  961. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  962. * without need of float support or long long division,
  963. * which would be required to prevent 32bit arithmetic overflow
  964. */
  965. unsigned int mul = ubir + 1;
  966. unsigned int div = 16 * (ubmr + 1);
  967. unsigned int rem = uartclk % div;
  968. baud_raw = (uartclk / div) * mul;
  969. baud_raw += (rem * mul + div / 2) / div;
  970. *baud = (baud_raw + 50) / 100 * 100;
  971. }
  972. if(*baud != baud_raw)
  973. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  974. baud_raw, *baud);
  975. }
  976. }
  977. static int __init
  978. imx_console_setup(struct console *co, char *options)
  979. {
  980. struct imx_port *sport;
  981. int baud = 9600;
  982. int bits = 8;
  983. int parity = 'n';
  984. int flow = 'n';
  985. /*
  986. * Check whether an invalid uart number has been specified, and
  987. * if so, search for the first available port that does have
  988. * console support.
  989. */
  990. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  991. co->index = 0;
  992. sport = imx_ports[co->index];
  993. if(sport == NULL)
  994. return -ENODEV;
  995. if (options)
  996. uart_parse_options(options, &baud, &parity, &bits, &flow);
  997. else
  998. imx_console_get_options(sport, &baud, &parity, &bits);
  999. imx_setup_ufcr(sport, 0);
  1000. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1001. }
  1002. static struct uart_driver imx_reg;
  1003. static struct console imx_console = {
  1004. .name = DEV_NAME,
  1005. .write = imx_console_write,
  1006. .device = uart_console_device,
  1007. .setup = imx_console_setup,
  1008. .flags = CON_PRINTBUFFER,
  1009. .index = -1,
  1010. .data = &imx_reg,
  1011. };
  1012. #define IMX_CONSOLE &imx_console
  1013. #else
  1014. #define IMX_CONSOLE NULL
  1015. #endif
  1016. static struct uart_driver imx_reg = {
  1017. .owner = THIS_MODULE,
  1018. .driver_name = DRIVER_NAME,
  1019. .dev_name = DEV_NAME,
  1020. .major = SERIAL_IMX_MAJOR,
  1021. .minor = MINOR_START,
  1022. .nr = ARRAY_SIZE(imx_ports),
  1023. .cons = IMX_CONSOLE,
  1024. };
  1025. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1026. {
  1027. struct imx_port *sport = platform_get_drvdata(dev);
  1028. if (sport)
  1029. uart_suspend_port(&imx_reg, &sport->port);
  1030. return 0;
  1031. }
  1032. static int serial_imx_resume(struct platform_device *dev)
  1033. {
  1034. struct imx_port *sport = platform_get_drvdata(dev);
  1035. if (sport)
  1036. uart_resume_port(&imx_reg, &sport->port);
  1037. return 0;
  1038. }
  1039. static int serial_imx_probe(struct platform_device *pdev)
  1040. {
  1041. struct imx_port *sport;
  1042. struct imxuart_platform_data *pdata;
  1043. void __iomem *base;
  1044. int ret = 0;
  1045. struct resource *res;
  1046. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1047. if (!sport)
  1048. return -ENOMEM;
  1049. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1050. if (!res) {
  1051. ret = -ENODEV;
  1052. goto free;
  1053. }
  1054. base = ioremap(res->start, PAGE_SIZE);
  1055. if (!base) {
  1056. ret = -ENOMEM;
  1057. goto free;
  1058. }
  1059. sport->port.dev = &pdev->dev;
  1060. sport->port.mapbase = res->start;
  1061. sport->port.membase = base;
  1062. sport->port.type = PORT_IMX,
  1063. sport->port.iotype = UPIO_MEM;
  1064. sport->port.irq = platform_get_irq(pdev, 0);
  1065. sport->rxirq = platform_get_irq(pdev, 0);
  1066. sport->txirq = platform_get_irq(pdev, 1);
  1067. sport->rtsirq = platform_get_irq(pdev, 2);
  1068. sport->port.fifosize = 32;
  1069. sport->port.ops = &imx_pops;
  1070. sport->port.flags = UPF_BOOT_AUTOCONF;
  1071. sport->port.line = pdev->id;
  1072. init_timer(&sport->timer);
  1073. sport->timer.function = imx_timeout;
  1074. sport->timer.data = (unsigned long)sport;
  1075. sport->clk = clk_get(&pdev->dev, "uart");
  1076. if (IS_ERR(sport->clk)) {
  1077. ret = PTR_ERR(sport->clk);
  1078. goto unmap;
  1079. }
  1080. clk_enable(sport->clk);
  1081. sport->port.uartclk = clk_get_rate(sport->clk);
  1082. imx_ports[pdev->id] = sport;
  1083. pdata = pdev->dev.platform_data;
  1084. if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  1085. sport->have_rtscts = 1;
  1086. #ifdef CONFIG_IRDA
  1087. if (pdata && (pdata->flags & IMXUART_IRDA))
  1088. sport->use_irda = 1;
  1089. #endif
  1090. if (pdata->init) {
  1091. ret = pdata->init(pdev);
  1092. if (ret)
  1093. goto clkput;
  1094. }
  1095. ret = uart_add_one_port(&imx_reg, &sport->port);
  1096. if (ret)
  1097. goto deinit;
  1098. platform_set_drvdata(pdev, &sport->port);
  1099. return 0;
  1100. deinit:
  1101. if (pdata->exit)
  1102. pdata->exit(pdev);
  1103. clkput:
  1104. clk_put(sport->clk);
  1105. clk_disable(sport->clk);
  1106. unmap:
  1107. iounmap(sport->port.membase);
  1108. free:
  1109. kfree(sport);
  1110. return ret;
  1111. }
  1112. static int serial_imx_remove(struct platform_device *pdev)
  1113. {
  1114. struct imxuart_platform_data *pdata;
  1115. struct imx_port *sport = platform_get_drvdata(pdev);
  1116. pdata = pdev->dev.platform_data;
  1117. platform_set_drvdata(pdev, NULL);
  1118. if (sport) {
  1119. uart_remove_one_port(&imx_reg, &sport->port);
  1120. clk_put(sport->clk);
  1121. }
  1122. clk_disable(sport->clk);
  1123. if (pdata->exit)
  1124. pdata->exit(pdev);
  1125. iounmap(sport->port.membase);
  1126. kfree(sport);
  1127. return 0;
  1128. }
  1129. static struct platform_driver serial_imx_driver = {
  1130. .probe = serial_imx_probe,
  1131. .remove = serial_imx_remove,
  1132. .suspend = serial_imx_suspend,
  1133. .resume = serial_imx_resume,
  1134. .driver = {
  1135. .name = "imx-uart",
  1136. .owner = THIS_MODULE,
  1137. },
  1138. };
  1139. static int __init imx_serial_init(void)
  1140. {
  1141. int ret;
  1142. printk(KERN_INFO "Serial: IMX driver\n");
  1143. ret = uart_register_driver(&imx_reg);
  1144. if (ret)
  1145. return ret;
  1146. ret = platform_driver_register(&serial_imx_driver);
  1147. if (ret != 0)
  1148. uart_unregister_driver(&imx_reg);
  1149. return 0;
  1150. }
  1151. static void __exit imx_serial_exit(void)
  1152. {
  1153. platform_driver_unregister(&serial_imx_driver);
  1154. uart_unregister_driver(&imx_reg);
  1155. }
  1156. module_init(imx_serial_init);
  1157. module_exit(imx_serial_exit);
  1158. MODULE_AUTHOR("Sascha Hauer");
  1159. MODULE_DESCRIPTION("IMX generic serial port driver");
  1160. MODULE_LICENSE("GPL");
  1161. MODULE_ALIAS("platform:imx-uart");