imx.c 38 KB

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