imx.c 41 KB

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