imx.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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 <linux/pinctrl/consumer.h>
  49. #include <linux/io.h>
  50. #include <asm/irq.h>
  51. #include <linux/platform_data/serial-imx.h>
  52. /* Register definitions */
  53. #define URXD0 0x0 /* Receiver Register */
  54. #define URTX0 0x40 /* Transmitter Register */
  55. #define UCR1 0x80 /* Control Register 1 */
  56. #define UCR2 0x84 /* Control Register 2 */
  57. #define UCR3 0x88 /* Control Register 3 */
  58. #define UCR4 0x8c /* Control Register 4 */
  59. #define UFCR 0x90 /* FIFO Control Register */
  60. #define USR1 0x94 /* Status Register 1 */
  61. #define USR2 0x98 /* Status Register 2 */
  62. #define UESC 0x9c /* Escape Character Register */
  63. #define UTIM 0xa0 /* Escape Timer Register */
  64. #define UBIR 0xa4 /* BRM Incremental Register */
  65. #define UBMR 0xa8 /* BRM Modulator Register */
  66. #define UBRC 0xac /* Baud Rate Count Register */
  67. #define IMX21_ONEMS 0xb0 /* One Millisecond register */
  68. #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
  69. #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
  70. /* UART Control Register Bit Fields.*/
  71. #define URXD_CHARRDY (1<<15)
  72. #define URXD_ERR (1<<14)
  73. #define URXD_OVRRUN (1<<13)
  74. #define URXD_FRMERR (1<<12)
  75. #define URXD_BRK (1<<11)
  76. #define URXD_PRERR (1<<10)
  77. #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
  78. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  79. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  80. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  81. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  82. #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
  83. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  84. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  85. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  86. #define UCR1_SNDBRK (1<<4) /* Send break */
  87. #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
  88. #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
  89. #define UCR1_DOZE (1<<1) /* Doze */
  90. #define UCR1_UARTEN (1<<0) /* UART enabled */
  91. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  92. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  93. #define UCR2_CTSC (1<<13) /* CTS pin control */
  94. #define UCR2_CTS (1<<12) /* Clear to send */
  95. #define UCR2_ESCEN (1<<11) /* Escape enable */
  96. #define UCR2_PREN (1<<8) /* Parity enable */
  97. #define UCR2_PROE (1<<7) /* Parity odd/even */
  98. #define UCR2_STPB (1<<6) /* Stop */
  99. #define UCR2_WS (1<<5) /* Word size */
  100. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  101. #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
  102. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  103. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  104. #define UCR2_SRST (1<<0) /* SW reset */
  105. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  106. #define UCR3_PARERREN (1<<12) /* Parity enable */
  107. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  108. #define UCR3_DSR (1<<10) /* Data set ready */
  109. #define UCR3_DCD (1<<9) /* Data carrier detect */
  110. #define UCR3_RI (1<<8) /* Ring indicator */
  111. #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
  112. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  113. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  114. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  115. #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
  116. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  117. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  118. #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
  119. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  120. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  121. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  122. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  123. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  124. #define UCR4_IRSC (1<<5) /* IR special case */
  125. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  126. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  127. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  128. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  129. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  130. #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
  131. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  132. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  133. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  134. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  135. #define USR1_RTSS (1<<14) /* RTS pin status */
  136. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  137. #define USR1_RTSD (1<<12) /* RTS delta */
  138. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  139. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  140. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  141. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  142. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  143. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  144. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  145. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  146. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  147. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  148. #define USR2_IDLE (1<<12) /* Idle condition */
  149. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  150. #define USR2_WAKE (1<<7) /* Wake */
  151. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  152. #define USR2_TXDC (1<<3) /* Transmitter complete */
  153. #define USR2_BRCD (1<<2) /* Break condition */
  154. #define USR2_ORE (1<<1) /* Overrun error */
  155. #define USR2_RDR (1<<0) /* Recv data ready */
  156. #define UTS_FRCPERR (1<<13) /* Force parity error */
  157. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  158. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  159. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  160. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  161. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  162. #define UTS_SOFTRST (1<<0) /* Software reset */
  163. /* We've been assigned a range on the "Low-density serial ports" major */
  164. #define SERIAL_IMX_MAJOR 207
  165. #define MINOR_START 16
  166. #define DEV_NAME "ttymxc"
  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_ipg;
  197. struct clk *clk_per;
  198. const struct imx_uart_data *devdata;
  199. };
  200. struct imx_port_ucrs {
  201. unsigned int ucr1;
  202. unsigned int ucr2;
  203. unsigned int ucr3;
  204. };
  205. #ifdef CONFIG_IRDA
  206. #define USE_IRDA(sport) ((sport)->use_irda)
  207. #else
  208. #define USE_IRDA(sport) (0)
  209. #endif
  210. static struct imx_uart_data imx_uart_devdata[] = {
  211. [IMX1_UART] = {
  212. .uts_reg = IMX1_UTS,
  213. .devtype = IMX1_UART,
  214. },
  215. [IMX21_UART] = {
  216. .uts_reg = IMX21_UTS,
  217. .devtype = IMX21_UART,
  218. },
  219. };
  220. static struct platform_device_id imx_uart_devtype[] = {
  221. {
  222. .name = "imx1-uart",
  223. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX1_UART],
  224. }, {
  225. .name = "imx21-uart",
  226. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
  227. }, {
  228. /* sentinel */
  229. }
  230. };
  231. MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
  232. static struct of_device_id imx_uart_dt_ids[] = {
  233. { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
  234. { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
  235. { /* sentinel */ }
  236. };
  237. MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
  238. static inline unsigned uts_reg(struct imx_port *sport)
  239. {
  240. return sport->devdata->uts_reg;
  241. }
  242. static inline int is_imx1_uart(struct imx_port *sport)
  243. {
  244. return sport->devdata->devtype == IMX1_UART;
  245. }
  246. static inline int is_imx21_uart(struct imx_port *sport)
  247. {
  248. return sport->devdata->devtype == IMX21_UART;
  249. }
  250. /*
  251. * Save and restore functions for UCR1, UCR2 and UCR3 registers
  252. */
  253. static void imx_port_ucrs_save(struct uart_port *port,
  254. struct imx_port_ucrs *ucr)
  255. {
  256. /* save control registers */
  257. ucr->ucr1 = readl(port->membase + UCR1);
  258. ucr->ucr2 = readl(port->membase + UCR2);
  259. ucr->ucr3 = readl(port->membase + UCR3);
  260. }
  261. static void imx_port_ucrs_restore(struct uart_port *port,
  262. struct imx_port_ucrs *ucr)
  263. {
  264. /* restore control registers */
  265. writel(ucr->ucr1, port->membase + UCR1);
  266. writel(ucr->ucr2, port->membase + UCR2);
  267. writel(ucr->ucr3, port->membase + UCR3);
  268. }
  269. /*
  270. * Handle any change of modem status signal since we were last called.
  271. */
  272. static void imx_mctrl_check(struct imx_port *sport)
  273. {
  274. unsigned int status, changed;
  275. status = sport->port.ops->get_mctrl(&sport->port);
  276. changed = status ^ sport->old_status;
  277. if (changed == 0)
  278. return;
  279. sport->old_status = status;
  280. if (changed & TIOCM_RI)
  281. sport->port.icount.rng++;
  282. if (changed & TIOCM_DSR)
  283. sport->port.icount.dsr++;
  284. if (changed & TIOCM_CAR)
  285. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  286. if (changed & TIOCM_CTS)
  287. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  288. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  289. }
  290. /*
  291. * This is our per-port timeout handler, for checking the
  292. * modem status signals.
  293. */
  294. static void imx_timeout(unsigned long data)
  295. {
  296. struct imx_port *sport = (struct imx_port *)data;
  297. unsigned long flags;
  298. if (sport->port.state) {
  299. spin_lock_irqsave(&sport->port.lock, flags);
  300. imx_mctrl_check(sport);
  301. spin_unlock_irqrestore(&sport->port.lock, flags);
  302. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  303. }
  304. }
  305. /*
  306. * interrupts disabled on entry
  307. */
  308. static void imx_stop_tx(struct uart_port *port)
  309. {
  310. struct imx_port *sport = (struct imx_port *)port;
  311. unsigned long temp;
  312. if (USE_IRDA(sport)) {
  313. /* half duplex - wait for end of transmission */
  314. int n = 256;
  315. while ((--n > 0) &&
  316. !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
  317. udelay(5);
  318. barrier();
  319. }
  320. /*
  321. * irda transceiver - wait a bit more to avoid
  322. * cutoff, hardware dependent
  323. */
  324. udelay(sport->trcv_delay);
  325. /*
  326. * half duplex - reactivate receive mode,
  327. * flush receive pipe echo crap
  328. */
  329. if (readl(sport->port.membase + USR2) & USR2_TXDC) {
  330. temp = readl(sport->port.membase + UCR1);
  331. temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
  332. writel(temp, sport->port.membase + UCR1);
  333. temp = readl(sport->port.membase + UCR4);
  334. temp &= ~(UCR4_TCEN);
  335. writel(temp, sport->port.membase + UCR4);
  336. while (readl(sport->port.membase + URXD0) &
  337. URXD_CHARRDY)
  338. barrier();
  339. temp = readl(sport->port.membase + UCR1);
  340. temp |= UCR1_RRDYEN;
  341. writel(temp, sport->port.membase + UCR1);
  342. temp = readl(sport->port.membase + UCR4);
  343. temp |= UCR4_DREN;
  344. writel(temp, sport->port.membase + UCR4);
  345. }
  346. return;
  347. }
  348. temp = readl(sport->port.membase + UCR1);
  349. writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
  350. }
  351. /*
  352. * interrupts disabled on entry
  353. */
  354. static void imx_stop_rx(struct uart_port *port)
  355. {
  356. struct imx_port *sport = (struct imx_port *)port;
  357. unsigned long temp;
  358. temp = readl(sport->port.membase + UCR2);
  359. writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
  360. }
  361. /*
  362. * Set the modem control timer to fire immediately.
  363. */
  364. static void imx_enable_ms(struct uart_port *port)
  365. {
  366. struct imx_port *sport = (struct imx_port *)port;
  367. mod_timer(&sport->timer, jiffies);
  368. }
  369. static inline void imx_transmit_buffer(struct imx_port *sport)
  370. {
  371. struct circ_buf *xmit = &sport->port.state->xmit;
  372. while (!uart_circ_empty(xmit) &&
  373. !(readl(sport->port.membase + uts_reg(sport))
  374. & UTS_TXFULL)) {
  375. /* send xmit->buf[xmit->tail]
  376. * out the port here */
  377. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  378. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  379. sport->port.icount.tx++;
  380. }
  381. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  382. uart_write_wakeup(&sport->port);
  383. if (uart_circ_empty(xmit))
  384. imx_stop_tx(&sport->port);
  385. }
  386. /*
  387. * interrupts disabled on entry
  388. */
  389. static void imx_start_tx(struct uart_port *port)
  390. {
  391. struct imx_port *sport = (struct imx_port *)port;
  392. unsigned long temp;
  393. if (USE_IRDA(sport)) {
  394. /* half duplex in IrDA mode; have to disable receive mode */
  395. temp = readl(sport->port.membase + UCR4);
  396. temp &= ~(UCR4_DREN);
  397. writel(temp, sport->port.membase + UCR4);
  398. temp = readl(sport->port.membase + UCR1);
  399. temp &= ~(UCR1_RRDYEN);
  400. writel(temp, sport->port.membase + UCR1);
  401. }
  402. temp = readl(sport->port.membase + UCR1);
  403. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  404. if (USE_IRDA(sport)) {
  405. temp = readl(sport->port.membase + UCR1);
  406. temp |= UCR1_TRDYEN;
  407. writel(temp, sport->port.membase + UCR1);
  408. temp = readl(sport->port.membase + UCR4);
  409. temp |= UCR4_TCEN;
  410. writel(temp, sport->port.membase + UCR4);
  411. }
  412. if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY)
  413. imx_transmit_buffer(sport);
  414. }
  415. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  416. {
  417. struct imx_port *sport = dev_id;
  418. unsigned int val;
  419. unsigned long flags;
  420. spin_lock_irqsave(&sport->port.lock, flags);
  421. writel(USR1_RTSD, sport->port.membase + USR1);
  422. val = readl(sport->port.membase + USR1) & USR1_RTSS;
  423. uart_handle_cts_change(&sport->port, !!val);
  424. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  425. spin_unlock_irqrestore(&sport->port.lock, flags);
  426. return IRQ_HANDLED;
  427. }
  428. static irqreturn_t imx_txint(int irq, void *dev_id)
  429. {
  430. struct imx_port *sport = dev_id;
  431. struct circ_buf *xmit = &sport->port.state->xmit;
  432. unsigned long flags;
  433. spin_lock_irqsave(&sport->port.lock, flags);
  434. if (sport->port.x_char) {
  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_port *port = &sport->port.state->port;
  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(port, rx, flg);
  497. }
  498. out:
  499. spin_unlock_irqrestore(&sport->port.lock, flags);
  500. tty_flip_buffer_push(port);
  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, 0,
  619. DRIVER_NAME, sport);
  620. if (retval)
  621. goto error_out3;
  622. }
  623. } else {
  624. retval = request_irq(sport->port.irq, imx_int, 0,
  625. DRIVER_NAME, sport);
  626. if (retval) {
  627. free_irq(sport->port.irq, sport);
  628. goto error_out1;
  629. }
  630. }
  631. spin_lock_irqsave(&sport->port.lock, flags);
  632. /*
  633. * Finally, clear and enable interrupts
  634. */
  635. writel(USR1_RTSD, sport->port.membase + USR1);
  636. temp = readl(sport->port.membase + UCR1);
  637. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  638. if (USE_IRDA(sport)) {
  639. temp |= UCR1_IREN;
  640. temp &= ~(UCR1_RTSDEN);
  641. }
  642. writel(temp, sport->port.membase + UCR1);
  643. temp = readl(sport->port.membase + UCR2);
  644. temp |= (UCR2_RXEN | UCR2_TXEN);
  645. writel(temp, sport->port.membase + UCR2);
  646. if (USE_IRDA(sport)) {
  647. /* clear RX-FIFO */
  648. int i = 64;
  649. while ((--i > 0) &&
  650. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  651. barrier();
  652. }
  653. }
  654. if (is_imx21_uart(sport)) {
  655. temp = readl(sport->port.membase + UCR3);
  656. temp |= IMX21_UCR3_RXDMUXSEL;
  657. writel(temp, sport->port.membase + UCR3);
  658. }
  659. if (USE_IRDA(sport)) {
  660. temp = readl(sport->port.membase + UCR4);
  661. if (sport->irda_inv_rx)
  662. temp |= UCR4_INVR;
  663. else
  664. temp &= ~(UCR4_INVR);
  665. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  666. temp = readl(sport->port.membase + UCR3);
  667. if (sport->irda_inv_tx)
  668. temp |= UCR3_INVT;
  669. else
  670. temp &= ~(UCR3_INVT);
  671. writel(temp, sport->port.membase + UCR3);
  672. }
  673. /*
  674. * Enable modem status interrupts
  675. */
  676. imx_enable_ms(&sport->port);
  677. spin_unlock_irqrestore(&sport->port.lock, flags);
  678. if (USE_IRDA(sport)) {
  679. struct imxuart_platform_data *pdata;
  680. pdata = sport->port.dev->platform_data;
  681. sport->irda_inv_rx = pdata->irda_inv_rx;
  682. sport->irda_inv_tx = pdata->irda_inv_tx;
  683. sport->trcv_delay = pdata->transceiver_delay;
  684. if (pdata->irda_enable)
  685. pdata->irda_enable(1);
  686. }
  687. return 0;
  688. error_out3:
  689. if (sport->txirq)
  690. free_irq(sport->txirq, sport);
  691. error_out2:
  692. if (sport->rxirq)
  693. free_irq(sport->rxirq, sport);
  694. error_out1:
  695. return retval;
  696. }
  697. static void imx_shutdown(struct uart_port *port)
  698. {
  699. struct imx_port *sport = (struct imx_port *)port;
  700. unsigned long temp;
  701. unsigned long flags;
  702. spin_lock_irqsave(&sport->port.lock, flags);
  703. temp = readl(sport->port.membase + UCR2);
  704. temp &= ~(UCR2_TXEN);
  705. writel(temp, sport->port.membase + UCR2);
  706. spin_unlock_irqrestore(&sport->port.lock, flags);
  707. if (USE_IRDA(sport)) {
  708. struct imxuart_platform_data *pdata;
  709. pdata = sport->port.dev->platform_data;
  710. if (pdata->irda_enable)
  711. pdata->irda_enable(0);
  712. }
  713. /*
  714. * Stop our timer.
  715. */
  716. del_timer_sync(&sport->timer);
  717. /*
  718. * Free the interrupts
  719. */
  720. if (sport->txirq > 0) {
  721. if (!USE_IRDA(sport))
  722. free_irq(sport->rtsirq, sport);
  723. free_irq(sport->txirq, sport);
  724. free_irq(sport->rxirq, sport);
  725. } else
  726. free_irq(sport->port.irq, sport);
  727. /*
  728. * Disable all interrupts, port and break condition.
  729. */
  730. spin_lock_irqsave(&sport->port.lock, flags);
  731. temp = readl(sport->port.membase + UCR1);
  732. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  733. if (USE_IRDA(sport))
  734. temp &= ~(UCR1_IREN);
  735. writel(temp, sport->port.membase + UCR1);
  736. spin_unlock_irqrestore(&sport->port.lock, flags);
  737. }
  738. static void
  739. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  740. struct ktermios *old)
  741. {
  742. struct imx_port *sport = (struct imx_port *)port;
  743. unsigned long flags;
  744. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  745. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  746. unsigned int div, ufcr;
  747. unsigned long num, denom;
  748. uint64_t tdiv64;
  749. /*
  750. * If we don't support modem control lines, don't allow
  751. * these to be set.
  752. */
  753. if (0) {
  754. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  755. termios->c_cflag |= CLOCAL;
  756. }
  757. /*
  758. * We only support CS7 and CS8.
  759. */
  760. while ((termios->c_cflag & CSIZE) != CS7 &&
  761. (termios->c_cflag & CSIZE) != CS8) {
  762. termios->c_cflag &= ~CSIZE;
  763. termios->c_cflag |= old_csize;
  764. old_csize = CS8;
  765. }
  766. if ((termios->c_cflag & CSIZE) == CS8)
  767. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  768. else
  769. ucr2 = UCR2_SRST | UCR2_IRTS;
  770. if (termios->c_cflag & CRTSCTS) {
  771. if (sport->have_rtscts) {
  772. ucr2 &= ~UCR2_IRTS;
  773. ucr2 |= UCR2_CTSC;
  774. } else {
  775. termios->c_cflag &= ~CRTSCTS;
  776. }
  777. }
  778. if (termios->c_cflag & CSTOPB)
  779. ucr2 |= UCR2_STPB;
  780. if (termios->c_cflag & PARENB) {
  781. ucr2 |= UCR2_PREN;
  782. if (termios->c_cflag & PARODD)
  783. ucr2 |= UCR2_PROE;
  784. }
  785. del_timer_sync(&sport->timer);
  786. /*
  787. * Ask the core to calculate the divisor for us.
  788. */
  789. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  790. quot = uart_get_divisor(port, baud);
  791. spin_lock_irqsave(&sport->port.lock, flags);
  792. sport->port.read_status_mask = 0;
  793. if (termios->c_iflag & INPCK)
  794. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  795. if (termios->c_iflag & (BRKINT | PARMRK))
  796. sport->port.read_status_mask |= URXD_BRK;
  797. /*
  798. * Characters to ignore
  799. */
  800. sport->port.ignore_status_mask = 0;
  801. if (termios->c_iflag & IGNPAR)
  802. sport->port.ignore_status_mask |= URXD_PRERR;
  803. if (termios->c_iflag & IGNBRK) {
  804. sport->port.ignore_status_mask |= URXD_BRK;
  805. /*
  806. * If we're ignoring parity and break indicators,
  807. * ignore overruns too (for real raw support).
  808. */
  809. if (termios->c_iflag & IGNPAR)
  810. sport->port.ignore_status_mask |= URXD_OVRRUN;
  811. }
  812. /*
  813. * Update the per-port timeout.
  814. */
  815. uart_update_timeout(port, termios->c_cflag, baud);
  816. /*
  817. * disable interrupts and drain transmitter
  818. */
  819. old_ucr1 = readl(sport->port.membase + UCR1);
  820. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  821. sport->port.membase + UCR1);
  822. while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
  823. barrier();
  824. /* then, disable everything */
  825. old_txrxen = readl(sport->port.membase + UCR2);
  826. writel(old_txrxen & ~(UCR2_TXEN | UCR2_RXEN),
  827. sport->port.membase + UCR2);
  828. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  829. if (USE_IRDA(sport)) {
  830. /*
  831. * use maximum available submodule frequency to
  832. * avoid missing short pulses due to low sampling rate
  833. */
  834. div = 1;
  835. } else {
  836. div = sport->port.uartclk / (baud * 16);
  837. if (div > 7)
  838. div = 7;
  839. if (!div)
  840. div = 1;
  841. }
  842. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  843. 1 << 16, 1 << 16, &num, &denom);
  844. tdiv64 = sport->port.uartclk;
  845. tdiv64 *= num;
  846. do_div(tdiv64, denom * 16 * div);
  847. tty_termios_encode_baud_rate(termios,
  848. (speed_t)tdiv64, (speed_t)tdiv64);
  849. num -= 1;
  850. denom -= 1;
  851. ufcr = readl(sport->port.membase + UFCR);
  852. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  853. writel(ufcr, sport->port.membase + UFCR);
  854. writel(num, sport->port.membase + UBIR);
  855. writel(denom, sport->port.membase + UBMR);
  856. if (is_imx21_uart(sport))
  857. writel(sport->port.uartclk / div / 1000,
  858. sport->port.membase + IMX21_ONEMS);
  859. writel(old_ucr1, sport->port.membase + UCR1);
  860. /* set the parity, stop bits and data size */
  861. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  862. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  863. imx_enable_ms(&sport->port);
  864. spin_unlock_irqrestore(&sport->port.lock, flags);
  865. }
  866. static const char *imx_type(struct uart_port *port)
  867. {
  868. struct imx_port *sport = (struct imx_port *)port;
  869. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  870. }
  871. /*
  872. * Release the memory region(s) being used by 'port'.
  873. */
  874. static void imx_release_port(struct uart_port *port)
  875. {
  876. struct platform_device *pdev = to_platform_device(port->dev);
  877. struct resource *mmres;
  878. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  879. release_mem_region(mmres->start, resource_size(mmres));
  880. }
  881. /*
  882. * Request the memory region(s) being used by 'port'.
  883. */
  884. static int imx_request_port(struct uart_port *port)
  885. {
  886. struct platform_device *pdev = to_platform_device(port->dev);
  887. struct resource *mmres;
  888. void *ret;
  889. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  890. if (!mmres)
  891. return -ENODEV;
  892. ret = request_mem_region(mmres->start, resource_size(mmres), "imx-uart");
  893. return ret ? 0 : -EBUSY;
  894. }
  895. /*
  896. * Configure/autoconfigure the port.
  897. */
  898. static void imx_config_port(struct uart_port *port, int flags)
  899. {
  900. struct imx_port *sport = (struct imx_port *)port;
  901. if (flags & UART_CONFIG_TYPE &&
  902. imx_request_port(&sport->port) == 0)
  903. sport->port.type = PORT_IMX;
  904. }
  905. /*
  906. * Verify the new serial_struct (for TIOCSSERIAL).
  907. * The only change we allow are to the flags and type, and
  908. * even then only between PORT_IMX and PORT_UNKNOWN
  909. */
  910. static int
  911. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  912. {
  913. struct imx_port *sport = (struct imx_port *)port;
  914. int ret = 0;
  915. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  916. ret = -EINVAL;
  917. if (sport->port.irq != ser->irq)
  918. ret = -EINVAL;
  919. if (ser->io_type != UPIO_MEM)
  920. ret = -EINVAL;
  921. if (sport->port.uartclk / 16 != ser->baud_base)
  922. ret = -EINVAL;
  923. if ((void *)sport->port.mapbase != ser->iomem_base)
  924. ret = -EINVAL;
  925. if (sport->port.iobase != ser->port)
  926. ret = -EINVAL;
  927. if (ser->hub6 != 0)
  928. ret = -EINVAL;
  929. return ret;
  930. }
  931. #if defined(CONFIG_CONSOLE_POLL)
  932. static int imx_poll_get_char(struct uart_port *port)
  933. {
  934. struct imx_port_ucrs old_ucr;
  935. unsigned int status;
  936. unsigned char c;
  937. /* save control registers */
  938. imx_port_ucrs_save(port, &old_ucr);
  939. /* disable interrupts */
  940. writel(UCR1_UARTEN, port->membase + UCR1);
  941. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  942. port->membase + UCR2);
  943. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  944. port->membase + UCR3);
  945. /* poll */
  946. do {
  947. status = readl(port->membase + USR2);
  948. } while (~status & USR2_RDR);
  949. /* read */
  950. c = readl(port->membase + URXD0);
  951. /* restore control registers */
  952. imx_port_ucrs_restore(port, &old_ucr);
  953. return c;
  954. }
  955. static void imx_poll_put_char(struct uart_port *port, unsigned char c)
  956. {
  957. struct imx_port_ucrs old_ucr;
  958. unsigned int status;
  959. /* save control registers */
  960. imx_port_ucrs_save(port, &old_ucr);
  961. /* disable interrupts */
  962. writel(UCR1_UARTEN, port->membase + UCR1);
  963. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  964. port->membase + UCR2);
  965. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  966. port->membase + UCR3);
  967. /* drain */
  968. do {
  969. status = readl(port->membase + USR1);
  970. } while (~status & USR1_TRDY);
  971. /* write */
  972. writel(c, port->membase + URTX0);
  973. /* flush */
  974. do {
  975. status = readl(port->membase + USR2);
  976. } while (~status & USR2_TXDC);
  977. /* restore control registers */
  978. imx_port_ucrs_restore(port, &old_ucr);
  979. }
  980. #endif
  981. static struct uart_ops imx_pops = {
  982. .tx_empty = imx_tx_empty,
  983. .set_mctrl = imx_set_mctrl,
  984. .get_mctrl = imx_get_mctrl,
  985. .stop_tx = imx_stop_tx,
  986. .start_tx = imx_start_tx,
  987. .stop_rx = imx_stop_rx,
  988. .enable_ms = imx_enable_ms,
  989. .break_ctl = imx_break_ctl,
  990. .startup = imx_startup,
  991. .shutdown = imx_shutdown,
  992. .set_termios = imx_set_termios,
  993. .type = imx_type,
  994. .release_port = imx_release_port,
  995. .request_port = imx_request_port,
  996. .config_port = imx_config_port,
  997. .verify_port = imx_verify_port,
  998. #if defined(CONFIG_CONSOLE_POLL)
  999. .poll_get_char = imx_poll_get_char,
  1000. .poll_put_char = imx_poll_put_char,
  1001. #endif
  1002. };
  1003. static struct imx_port *imx_ports[UART_NR];
  1004. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  1005. static void imx_console_putchar(struct uart_port *port, int ch)
  1006. {
  1007. struct imx_port *sport = (struct imx_port *)port;
  1008. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  1009. barrier();
  1010. writel(ch, sport->port.membase + URTX0);
  1011. }
  1012. /*
  1013. * Interrupts are disabled on entering
  1014. */
  1015. static void
  1016. imx_console_write(struct console *co, const char *s, unsigned int count)
  1017. {
  1018. struct imx_port *sport = imx_ports[co->index];
  1019. struct imx_port_ucrs old_ucr;
  1020. unsigned int ucr1;
  1021. unsigned long flags = 0;
  1022. int locked = 1;
  1023. if (sport->port.sysrq)
  1024. locked = 0;
  1025. else if (oops_in_progress)
  1026. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1027. else
  1028. spin_lock_irqsave(&sport->port.lock, flags);
  1029. /*
  1030. * First, save UCR1/2/3 and then disable interrupts
  1031. */
  1032. imx_port_ucrs_save(&sport->port, &old_ucr);
  1033. ucr1 = old_ucr.ucr1;
  1034. if (is_imx1_uart(sport))
  1035. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1036. ucr1 |= UCR1_UARTEN;
  1037. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1038. writel(ucr1, sport->port.membase + UCR1);
  1039. writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  1040. uart_console_write(&sport->port, s, count, imx_console_putchar);
  1041. /*
  1042. * Finally, wait for transmitter to become empty
  1043. * and restore UCR1/2/3
  1044. */
  1045. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  1046. imx_port_ucrs_restore(&sport->port, &old_ucr);
  1047. if (locked)
  1048. spin_unlock_irqrestore(&sport->port.lock, flags);
  1049. }
  1050. /*
  1051. * If the port was already initialised (eg, by a boot loader),
  1052. * try to determine the current setup.
  1053. */
  1054. static void __init
  1055. imx_console_get_options(struct imx_port *sport, int *baud,
  1056. int *parity, int *bits)
  1057. {
  1058. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  1059. /* ok, the port was enabled */
  1060. unsigned int ucr2, ubir, ubmr, uartclk;
  1061. unsigned int baud_raw;
  1062. unsigned int ucfr_rfdiv;
  1063. ucr2 = readl(sport->port.membase + UCR2);
  1064. *parity = 'n';
  1065. if (ucr2 & UCR2_PREN) {
  1066. if (ucr2 & UCR2_PROE)
  1067. *parity = 'o';
  1068. else
  1069. *parity = 'e';
  1070. }
  1071. if (ucr2 & UCR2_WS)
  1072. *bits = 8;
  1073. else
  1074. *bits = 7;
  1075. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  1076. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  1077. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  1078. if (ucfr_rfdiv == 6)
  1079. ucfr_rfdiv = 7;
  1080. else
  1081. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1082. uartclk = clk_get_rate(sport->clk_per);
  1083. uartclk /= ucfr_rfdiv;
  1084. { /*
  1085. * The next code provides exact computation of
  1086. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1087. * without need of float support or long long division,
  1088. * which would be required to prevent 32bit arithmetic overflow
  1089. */
  1090. unsigned int mul = ubir + 1;
  1091. unsigned int div = 16 * (ubmr + 1);
  1092. unsigned int rem = uartclk % div;
  1093. baud_raw = (uartclk / div) * mul;
  1094. baud_raw += (rem * mul + div / 2) / div;
  1095. *baud = (baud_raw + 50) / 100 * 100;
  1096. }
  1097. if (*baud != baud_raw)
  1098. pr_info("Console IMX rounded baud rate from %d to %d\n",
  1099. baud_raw, *baud);
  1100. }
  1101. }
  1102. static int __init
  1103. imx_console_setup(struct console *co, char *options)
  1104. {
  1105. struct imx_port *sport;
  1106. int baud = 9600;
  1107. int bits = 8;
  1108. int parity = 'n';
  1109. int flow = 'n';
  1110. /*
  1111. * Check whether an invalid uart number has been specified, and
  1112. * if so, search for the first available port that does have
  1113. * console support.
  1114. */
  1115. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1116. co->index = 0;
  1117. sport = imx_ports[co->index];
  1118. if (sport == NULL)
  1119. return -ENODEV;
  1120. if (options)
  1121. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1122. else
  1123. imx_console_get_options(sport, &baud, &parity, &bits);
  1124. imx_setup_ufcr(sport, 0);
  1125. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1126. }
  1127. static struct uart_driver imx_reg;
  1128. static struct console imx_console = {
  1129. .name = DEV_NAME,
  1130. .write = imx_console_write,
  1131. .device = uart_console_device,
  1132. .setup = imx_console_setup,
  1133. .flags = CON_PRINTBUFFER,
  1134. .index = -1,
  1135. .data = &imx_reg,
  1136. };
  1137. #define IMX_CONSOLE &imx_console
  1138. #else
  1139. #define IMX_CONSOLE NULL
  1140. #endif
  1141. static struct uart_driver imx_reg = {
  1142. .owner = THIS_MODULE,
  1143. .driver_name = DRIVER_NAME,
  1144. .dev_name = DEV_NAME,
  1145. .major = SERIAL_IMX_MAJOR,
  1146. .minor = MINOR_START,
  1147. .nr = ARRAY_SIZE(imx_ports),
  1148. .cons = IMX_CONSOLE,
  1149. };
  1150. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1151. {
  1152. struct imx_port *sport = platform_get_drvdata(dev);
  1153. unsigned int val;
  1154. /* enable wakeup from i.MX UART */
  1155. val = readl(sport->port.membase + UCR3);
  1156. val |= UCR3_AWAKEN;
  1157. writel(val, sport->port.membase + UCR3);
  1158. uart_suspend_port(&imx_reg, &sport->port);
  1159. return 0;
  1160. }
  1161. static int serial_imx_resume(struct platform_device *dev)
  1162. {
  1163. struct imx_port *sport = platform_get_drvdata(dev);
  1164. unsigned int val;
  1165. /* disable wakeup from i.MX UART */
  1166. val = readl(sport->port.membase + UCR3);
  1167. val &= ~UCR3_AWAKEN;
  1168. writel(val, sport->port.membase + UCR3);
  1169. uart_resume_port(&imx_reg, &sport->port);
  1170. return 0;
  1171. }
  1172. #ifdef CONFIG_OF
  1173. /*
  1174. * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
  1175. * could successfully get all information from dt or a negative errno.
  1176. */
  1177. static int serial_imx_probe_dt(struct imx_port *sport,
  1178. struct platform_device *pdev)
  1179. {
  1180. struct device_node *np = pdev->dev.of_node;
  1181. const struct of_device_id *of_id =
  1182. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1183. int ret;
  1184. if (!np)
  1185. /* no device tree device */
  1186. return 1;
  1187. ret = of_alias_get_id(np, "serial");
  1188. if (ret < 0) {
  1189. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1190. return ret;
  1191. }
  1192. sport->port.line = ret;
  1193. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1194. sport->have_rtscts = 1;
  1195. if (of_get_property(np, "fsl,irda-mode", NULL))
  1196. sport->use_irda = 1;
  1197. sport->devdata = of_id->data;
  1198. return 0;
  1199. }
  1200. #else
  1201. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1202. struct platform_device *pdev)
  1203. {
  1204. return 1;
  1205. }
  1206. #endif
  1207. static void serial_imx_probe_pdata(struct imx_port *sport,
  1208. struct platform_device *pdev)
  1209. {
  1210. struct imxuart_platform_data *pdata = pdev->dev.platform_data;
  1211. sport->port.line = pdev->id;
  1212. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1213. if (!pdata)
  1214. return;
  1215. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1216. sport->have_rtscts = 1;
  1217. if (pdata->flags & IMXUART_IRDA)
  1218. sport->use_irda = 1;
  1219. }
  1220. static int serial_imx_probe(struct platform_device *pdev)
  1221. {
  1222. struct imx_port *sport;
  1223. struct imxuart_platform_data *pdata;
  1224. void __iomem *base;
  1225. int ret = 0;
  1226. struct resource *res;
  1227. struct pinctrl *pinctrl;
  1228. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1229. if (!sport)
  1230. return -ENOMEM;
  1231. ret = serial_imx_probe_dt(sport, pdev);
  1232. if (ret > 0)
  1233. serial_imx_probe_pdata(sport, pdev);
  1234. else if (ret < 0)
  1235. return ret;
  1236. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1237. if (!res)
  1238. return -ENODEV;
  1239. base = devm_ioremap(&pdev->dev, res->start, PAGE_SIZE);
  1240. if (!base)
  1241. return -ENOMEM;
  1242. sport->port.dev = &pdev->dev;
  1243. sport->port.mapbase = res->start;
  1244. sport->port.membase = base;
  1245. sport->port.type = PORT_IMX,
  1246. sport->port.iotype = UPIO_MEM;
  1247. sport->port.irq = platform_get_irq(pdev, 0);
  1248. sport->rxirq = platform_get_irq(pdev, 0);
  1249. sport->txirq = platform_get_irq(pdev, 1);
  1250. sport->rtsirq = platform_get_irq(pdev, 2);
  1251. sport->port.fifosize = 32;
  1252. sport->port.ops = &imx_pops;
  1253. sport->port.flags = UPF_BOOT_AUTOCONF;
  1254. init_timer(&sport->timer);
  1255. sport->timer.function = imx_timeout;
  1256. sport->timer.data = (unsigned long)sport;
  1257. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1258. if (IS_ERR(pinctrl)) {
  1259. ret = PTR_ERR(pinctrl);
  1260. dev_err(&pdev->dev, "failed to get default pinctrl: %d\n", ret);
  1261. return ret;
  1262. }
  1263. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1264. if (IS_ERR(sport->clk_ipg)) {
  1265. ret = PTR_ERR(sport->clk_ipg);
  1266. dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
  1267. return ret;
  1268. }
  1269. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  1270. if (IS_ERR(sport->clk_per)) {
  1271. ret = PTR_ERR(sport->clk_per);
  1272. dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
  1273. return ret;
  1274. }
  1275. clk_prepare_enable(sport->clk_per);
  1276. clk_prepare_enable(sport->clk_ipg);
  1277. sport->port.uartclk = clk_get_rate(sport->clk_per);
  1278. imx_ports[sport->port.line] = sport;
  1279. pdata = pdev->dev.platform_data;
  1280. if (pdata && pdata->init) {
  1281. ret = pdata->init(pdev);
  1282. if (ret)
  1283. goto clkput;
  1284. }
  1285. ret = uart_add_one_port(&imx_reg, &sport->port);
  1286. if (ret)
  1287. goto deinit;
  1288. platform_set_drvdata(pdev, sport);
  1289. return 0;
  1290. deinit:
  1291. if (pdata && pdata->exit)
  1292. pdata->exit(pdev);
  1293. clkput:
  1294. clk_disable_unprepare(sport->clk_per);
  1295. clk_disable_unprepare(sport->clk_ipg);
  1296. return ret;
  1297. }
  1298. static int serial_imx_remove(struct platform_device *pdev)
  1299. {
  1300. struct imxuart_platform_data *pdata;
  1301. struct imx_port *sport = platform_get_drvdata(pdev);
  1302. pdata = pdev->dev.platform_data;
  1303. platform_set_drvdata(pdev, NULL);
  1304. uart_remove_one_port(&imx_reg, &sport->port);
  1305. clk_disable_unprepare(sport->clk_per);
  1306. clk_disable_unprepare(sport->clk_ipg);
  1307. if (pdata && pdata->exit)
  1308. pdata->exit(pdev);
  1309. return 0;
  1310. }
  1311. static struct platform_driver serial_imx_driver = {
  1312. .probe = serial_imx_probe,
  1313. .remove = serial_imx_remove,
  1314. .suspend = serial_imx_suspend,
  1315. .resume = serial_imx_resume,
  1316. .id_table = imx_uart_devtype,
  1317. .driver = {
  1318. .name = "imx-uart",
  1319. .owner = THIS_MODULE,
  1320. .of_match_table = imx_uart_dt_ids,
  1321. },
  1322. };
  1323. static int __init imx_serial_init(void)
  1324. {
  1325. int ret;
  1326. pr_info("Serial: IMX driver\n");
  1327. ret = uart_register_driver(&imx_reg);
  1328. if (ret)
  1329. return ret;
  1330. ret = platform_driver_register(&serial_imx_driver);
  1331. if (ret != 0)
  1332. uart_unregister_driver(&imx_reg);
  1333. return ret;
  1334. }
  1335. static void __exit imx_serial_exit(void)
  1336. {
  1337. platform_driver_unregister(&serial_imx_driver);
  1338. uart_unregister_driver(&imx_reg);
  1339. }
  1340. module_init(imx_serial_init);
  1341. module_exit(imx_serial_exit);
  1342. MODULE_AUTHOR("Sascha Hauer");
  1343. MODULE_DESCRIPTION("IMX generic serial port driver");
  1344. MODULE_LICENSE("GPL");
  1345. MODULE_ALIAS("platform:imx-uart");