imx.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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. if (sts & USR1_AWAKE)
  490. writel(USR1_AWAKE, sport->port.membase + USR1);
  491. return IRQ_HANDLED;
  492. }
  493. /*
  494. * Return TIOCSER_TEMT when transmitter is not busy.
  495. */
  496. static unsigned int imx_tx_empty(struct uart_port *port)
  497. {
  498. struct imx_port *sport = (struct imx_port *)port;
  499. return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  500. }
  501. /*
  502. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  503. */
  504. static unsigned int imx_get_mctrl(struct uart_port *port)
  505. {
  506. struct imx_port *sport = (struct imx_port *)port;
  507. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  508. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  509. tmp |= TIOCM_CTS;
  510. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  511. tmp |= TIOCM_RTS;
  512. return tmp;
  513. }
  514. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  515. {
  516. struct imx_port *sport = (struct imx_port *)port;
  517. unsigned long temp;
  518. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  519. if (mctrl & TIOCM_RTS)
  520. temp |= UCR2_CTS;
  521. writel(temp, sport->port.membase + UCR2);
  522. }
  523. /*
  524. * Interrupts always disabled.
  525. */
  526. static void imx_break_ctl(struct uart_port *port, int break_state)
  527. {
  528. struct imx_port *sport = (struct imx_port *)port;
  529. unsigned long flags, temp;
  530. spin_lock_irqsave(&sport->port.lock, flags);
  531. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  532. if ( break_state != 0 )
  533. temp |= UCR1_SNDBRK;
  534. writel(temp, sport->port.membase + UCR1);
  535. spin_unlock_irqrestore(&sport->port.lock, flags);
  536. }
  537. #define TXTL 2 /* reset default */
  538. #define RXTL 1 /* reset default */
  539. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  540. {
  541. unsigned int val;
  542. unsigned int ufcr_rfdiv;
  543. /* set receiver / transmitter trigger level.
  544. * RFDIV is set such way to satisfy requested uartclk value
  545. */
  546. val = TXTL << 10 | RXTL;
  547. ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
  548. / sport->port.uartclk;
  549. if(!ufcr_rfdiv)
  550. ufcr_rfdiv = 1;
  551. val |= UFCR_RFDIV_REG(ufcr_rfdiv);
  552. writel(val, sport->port.membase + UFCR);
  553. return 0;
  554. }
  555. /* half the RX buffer size */
  556. #define CTSTL 16
  557. static int imx_startup(struct uart_port *port)
  558. {
  559. struct imx_port *sport = (struct imx_port *)port;
  560. int retval;
  561. unsigned long flags, temp;
  562. imx_setup_ufcr(sport, 0);
  563. /* disable the DREN bit (Data Ready interrupt enable) before
  564. * requesting IRQs
  565. */
  566. temp = readl(sport->port.membase + UCR4);
  567. if (USE_IRDA(sport))
  568. temp |= UCR4_IRSC;
  569. /* set the trigger level for CTS */
  570. temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
  571. temp |= CTSTL<< UCR4_CTSTL_SHF;
  572. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  573. if (USE_IRDA(sport)) {
  574. /* reset fifo's and state machines */
  575. int i = 100;
  576. temp = readl(sport->port.membase + UCR2);
  577. temp &= ~UCR2_SRST;
  578. writel(temp, sport->port.membase + UCR2);
  579. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  580. (--i > 0)) {
  581. udelay(1);
  582. }
  583. }
  584. /*
  585. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  586. * chips only have one interrupt.
  587. */
  588. if (sport->txirq > 0) {
  589. retval = request_irq(sport->rxirq, imx_rxint, 0,
  590. DRIVER_NAME, sport);
  591. if (retval)
  592. goto error_out1;
  593. retval = request_irq(sport->txirq, imx_txint, 0,
  594. DRIVER_NAME, sport);
  595. if (retval)
  596. goto error_out2;
  597. /* do not use RTS IRQ on IrDA */
  598. if (!USE_IRDA(sport)) {
  599. retval = request_irq(sport->rtsirq, imx_rtsint,
  600. (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
  601. IRQF_TRIGGER_FALLING |
  602. IRQF_TRIGGER_RISING,
  603. DRIVER_NAME, sport);
  604. if (retval)
  605. goto error_out3;
  606. }
  607. } else {
  608. retval = request_irq(sport->port.irq, imx_int, 0,
  609. DRIVER_NAME, sport);
  610. if (retval) {
  611. free_irq(sport->port.irq, sport);
  612. goto error_out1;
  613. }
  614. }
  615. /*
  616. * Finally, clear and enable interrupts
  617. */
  618. writel(USR1_RTSD, sport->port.membase + USR1);
  619. temp = readl(sport->port.membase + UCR1);
  620. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  621. if (USE_IRDA(sport)) {
  622. temp |= UCR1_IREN;
  623. temp &= ~(UCR1_RTSDEN);
  624. }
  625. writel(temp, sport->port.membase + UCR1);
  626. temp = readl(sport->port.membase + UCR2);
  627. temp |= (UCR2_RXEN | UCR2_TXEN);
  628. writel(temp, sport->port.membase + UCR2);
  629. if (USE_IRDA(sport)) {
  630. /* clear RX-FIFO */
  631. int i = 64;
  632. while ((--i > 0) &&
  633. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  634. barrier();
  635. }
  636. }
  637. if (is_imx21_uart(sport)) {
  638. temp = readl(sport->port.membase + UCR3);
  639. temp |= IMX21_UCR3_RXDMUXSEL;
  640. writel(temp, sport->port.membase + UCR3);
  641. }
  642. if (USE_IRDA(sport)) {
  643. temp = readl(sport->port.membase + UCR4);
  644. if (sport->irda_inv_rx)
  645. temp |= UCR4_INVR;
  646. else
  647. temp &= ~(UCR4_INVR);
  648. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  649. temp = readl(sport->port.membase + UCR3);
  650. if (sport->irda_inv_tx)
  651. temp |= UCR3_INVT;
  652. else
  653. temp &= ~(UCR3_INVT);
  654. writel(temp, sport->port.membase + UCR3);
  655. }
  656. /*
  657. * Enable modem status interrupts
  658. */
  659. spin_lock_irqsave(&sport->port.lock,flags);
  660. imx_enable_ms(&sport->port);
  661. spin_unlock_irqrestore(&sport->port.lock,flags);
  662. if (USE_IRDA(sport)) {
  663. struct imxuart_platform_data *pdata;
  664. pdata = sport->port.dev->platform_data;
  665. sport->irda_inv_rx = pdata->irda_inv_rx;
  666. sport->irda_inv_tx = pdata->irda_inv_tx;
  667. sport->trcv_delay = pdata->transceiver_delay;
  668. if (pdata->irda_enable)
  669. pdata->irda_enable(1);
  670. }
  671. return 0;
  672. error_out3:
  673. if (sport->txirq)
  674. free_irq(sport->txirq, sport);
  675. error_out2:
  676. if (sport->rxirq)
  677. free_irq(sport->rxirq, sport);
  678. error_out1:
  679. return retval;
  680. }
  681. static void imx_shutdown(struct uart_port *port)
  682. {
  683. struct imx_port *sport = (struct imx_port *)port;
  684. unsigned long temp;
  685. temp = readl(sport->port.membase + UCR2);
  686. temp &= ~(UCR2_TXEN);
  687. writel(temp, sport->port.membase + UCR2);
  688. if (USE_IRDA(sport)) {
  689. struct imxuart_platform_data *pdata;
  690. pdata = sport->port.dev->platform_data;
  691. if (pdata->irda_enable)
  692. pdata->irda_enable(0);
  693. }
  694. /*
  695. * Stop our timer.
  696. */
  697. del_timer_sync(&sport->timer);
  698. /*
  699. * Free the interrupts
  700. */
  701. if (sport->txirq > 0) {
  702. if (!USE_IRDA(sport))
  703. free_irq(sport->rtsirq, sport);
  704. free_irq(sport->txirq, sport);
  705. free_irq(sport->rxirq, sport);
  706. } else
  707. free_irq(sport->port.irq, sport);
  708. /*
  709. * Disable all interrupts, port and break condition.
  710. */
  711. temp = readl(sport->port.membase + UCR1);
  712. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  713. if (USE_IRDA(sport))
  714. temp &= ~(UCR1_IREN);
  715. writel(temp, sport->port.membase + UCR1);
  716. }
  717. static void
  718. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  719. struct ktermios *old)
  720. {
  721. struct imx_port *sport = (struct imx_port *)port;
  722. unsigned long flags;
  723. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  724. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  725. unsigned int div, ufcr;
  726. unsigned long num, denom;
  727. uint64_t tdiv64;
  728. /*
  729. * If we don't support modem control lines, don't allow
  730. * these to be set.
  731. */
  732. if (0) {
  733. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  734. termios->c_cflag |= CLOCAL;
  735. }
  736. /*
  737. * We only support CS7 and CS8.
  738. */
  739. while ((termios->c_cflag & CSIZE) != CS7 &&
  740. (termios->c_cflag & CSIZE) != CS8) {
  741. termios->c_cflag &= ~CSIZE;
  742. termios->c_cflag |= old_csize;
  743. old_csize = CS8;
  744. }
  745. if ((termios->c_cflag & CSIZE) == CS8)
  746. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  747. else
  748. ucr2 = UCR2_SRST | UCR2_IRTS;
  749. if (termios->c_cflag & CRTSCTS) {
  750. if( sport->have_rtscts ) {
  751. ucr2 &= ~UCR2_IRTS;
  752. ucr2 |= UCR2_CTSC;
  753. } else {
  754. termios->c_cflag &= ~CRTSCTS;
  755. }
  756. }
  757. if (termios->c_cflag & CSTOPB)
  758. ucr2 |= UCR2_STPB;
  759. if (termios->c_cflag & PARENB) {
  760. ucr2 |= UCR2_PREN;
  761. if (termios->c_cflag & PARODD)
  762. ucr2 |= UCR2_PROE;
  763. }
  764. /*
  765. * Ask the core to calculate the divisor for us.
  766. */
  767. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  768. quot = uart_get_divisor(port, baud);
  769. spin_lock_irqsave(&sport->port.lock, flags);
  770. sport->port.read_status_mask = 0;
  771. if (termios->c_iflag & INPCK)
  772. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  773. if (termios->c_iflag & (BRKINT | PARMRK))
  774. sport->port.read_status_mask |= URXD_BRK;
  775. /*
  776. * Characters to ignore
  777. */
  778. sport->port.ignore_status_mask = 0;
  779. if (termios->c_iflag & IGNPAR)
  780. sport->port.ignore_status_mask |= URXD_PRERR;
  781. if (termios->c_iflag & IGNBRK) {
  782. sport->port.ignore_status_mask |= URXD_BRK;
  783. /*
  784. * If we're ignoring parity and break indicators,
  785. * ignore overruns too (for real raw support).
  786. */
  787. if (termios->c_iflag & IGNPAR)
  788. sport->port.ignore_status_mask |= URXD_OVRRUN;
  789. }
  790. del_timer_sync(&sport->timer);
  791. /*
  792. * Update the per-port timeout.
  793. */
  794. uart_update_timeout(port, termios->c_cflag, baud);
  795. /*
  796. * disable interrupts and drain transmitter
  797. */
  798. old_ucr1 = readl(sport->port.membase + UCR1);
  799. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  800. sport->port.membase + UCR1);
  801. while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
  802. barrier();
  803. /* then, disable everything */
  804. old_txrxen = readl(sport->port.membase + UCR2);
  805. writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
  806. sport->port.membase + UCR2);
  807. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  808. if (USE_IRDA(sport)) {
  809. /*
  810. * use maximum available submodule frequency to
  811. * avoid missing short pulses due to low sampling rate
  812. */
  813. div = 1;
  814. } else {
  815. div = sport->port.uartclk / (baud * 16);
  816. if (div > 7)
  817. div = 7;
  818. if (!div)
  819. div = 1;
  820. }
  821. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  822. 1 << 16, 1 << 16, &num, &denom);
  823. tdiv64 = sport->port.uartclk;
  824. tdiv64 *= num;
  825. do_div(tdiv64, denom * 16 * div);
  826. tty_termios_encode_baud_rate(termios,
  827. (speed_t)tdiv64, (speed_t)tdiv64);
  828. num -= 1;
  829. denom -= 1;
  830. ufcr = readl(sport->port.membase + UFCR);
  831. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  832. writel(ufcr, sport->port.membase + UFCR);
  833. writel(num, sport->port.membase + UBIR);
  834. writel(denom, sport->port.membase + UBMR);
  835. if (is_imx21_uart(sport))
  836. writel(sport->port.uartclk / div / 1000,
  837. sport->port.membase + IMX21_ONEMS);
  838. writel(old_ucr1, sport->port.membase + UCR1);
  839. /* set the parity, stop bits and data size */
  840. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  841. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  842. imx_enable_ms(&sport->port);
  843. spin_unlock_irqrestore(&sport->port.lock, flags);
  844. }
  845. static const char *imx_type(struct uart_port *port)
  846. {
  847. struct imx_port *sport = (struct imx_port *)port;
  848. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  849. }
  850. /*
  851. * Release the memory region(s) being used by 'port'.
  852. */
  853. static void imx_release_port(struct uart_port *port)
  854. {
  855. struct platform_device *pdev = to_platform_device(port->dev);
  856. struct resource *mmres;
  857. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  858. release_mem_region(mmres->start, resource_size(mmres));
  859. }
  860. /*
  861. * Request the memory region(s) being used by 'port'.
  862. */
  863. static int imx_request_port(struct uart_port *port)
  864. {
  865. struct platform_device *pdev = to_platform_device(port->dev);
  866. struct resource *mmres;
  867. void *ret;
  868. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  869. if (!mmres)
  870. return -ENODEV;
  871. ret = request_mem_region(mmres->start, resource_size(mmres), "imx-uart");
  872. return ret ? 0 : -EBUSY;
  873. }
  874. /*
  875. * Configure/autoconfigure the port.
  876. */
  877. static void imx_config_port(struct uart_port *port, int flags)
  878. {
  879. struct imx_port *sport = (struct imx_port *)port;
  880. if (flags & UART_CONFIG_TYPE &&
  881. imx_request_port(&sport->port) == 0)
  882. sport->port.type = PORT_IMX;
  883. }
  884. /*
  885. * Verify the new serial_struct (for TIOCSSERIAL).
  886. * The only change we allow are to the flags and type, and
  887. * even then only between PORT_IMX and PORT_UNKNOWN
  888. */
  889. static int
  890. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  891. {
  892. struct imx_port *sport = (struct imx_port *)port;
  893. int ret = 0;
  894. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  895. ret = -EINVAL;
  896. if (sport->port.irq != ser->irq)
  897. ret = -EINVAL;
  898. if (ser->io_type != UPIO_MEM)
  899. ret = -EINVAL;
  900. if (sport->port.uartclk / 16 != ser->baud_base)
  901. ret = -EINVAL;
  902. if ((void *)sport->port.mapbase != ser->iomem_base)
  903. ret = -EINVAL;
  904. if (sport->port.iobase != ser->port)
  905. ret = -EINVAL;
  906. if (ser->hub6 != 0)
  907. ret = -EINVAL;
  908. return ret;
  909. }
  910. static struct uart_ops imx_pops = {
  911. .tx_empty = imx_tx_empty,
  912. .set_mctrl = imx_set_mctrl,
  913. .get_mctrl = imx_get_mctrl,
  914. .stop_tx = imx_stop_tx,
  915. .start_tx = imx_start_tx,
  916. .stop_rx = imx_stop_rx,
  917. .enable_ms = imx_enable_ms,
  918. .break_ctl = imx_break_ctl,
  919. .startup = imx_startup,
  920. .shutdown = imx_shutdown,
  921. .set_termios = imx_set_termios,
  922. .type = imx_type,
  923. .release_port = imx_release_port,
  924. .request_port = imx_request_port,
  925. .config_port = imx_config_port,
  926. .verify_port = imx_verify_port,
  927. };
  928. static struct imx_port *imx_ports[UART_NR];
  929. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  930. static void imx_console_putchar(struct uart_port *port, int ch)
  931. {
  932. struct imx_port *sport = (struct imx_port *)port;
  933. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  934. barrier();
  935. writel(ch, sport->port.membase + URTX0);
  936. }
  937. /*
  938. * Interrupts are disabled on entering
  939. */
  940. static void
  941. imx_console_write(struct console *co, const char *s, unsigned int count)
  942. {
  943. struct imx_port *sport = imx_ports[co->index];
  944. unsigned int old_ucr1, old_ucr2, ucr1;
  945. /*
  946. * First, save UCR1/2 and then disable interrupts
  947. */
  948. ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
  949. old_ucr2 = readl(sport->port.membase + UCR2);
  950. if (is_imx1_uart(sport))
  951. ucr1 |= IMX1_UCR1_UARTCLKEN;
  952. ucr1 |= UCR1_UARTEN;
  953. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  954. writel(ucr1, sport->port.membase + UCR1);
  955. writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  956. uart_console_write(&sport->port, s, count, imx_console_putchar);
  957. /*
  958. * Finally, wait for transmitter to become empty
  959. * and restore UCR1/2
  960. */
  961. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  962. writel(old_ucr1, sport->port.membase + UCR1);
  963. writel(old_ucr2, sport->port.membase + UCR2);
  964. }
  965. /*
  966. * If the port was already initialised (eg, by a boot loader),
  967. * try to determine the current setup.
  968. */
  969. static void __init
  970. imx_console_get_options(struct imx_port *sport, int *baud,
  971. int *parity, int *bits)
  972. {
  973. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  974. /* ok, the port was enabled */
  975. unsigned int ucr2, ubir,ubmr, uartclk;
  976. unsigned int baud_raw;
  977. unsigned int ucfr_rfdiv;
  978. ucr2 = readl(sport->port.membase + UCR2);
  979. *parity = 'n';
  980. if (ucr2 & UCR2_PREN) {
  981. if (ucr2 & UCR2_PROE)
  982. *parity = 'o';
  983. else
  984. *parity = 'e';
  985. }
  986. if (ucr2 & UCR2_WS)
  987. *bits = 8;
  988. else
  989. *bits = 7;
  990. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  991. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  992. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  993. if (ucfr_rfdiv == 6)
  994. ucfr_rfdiv = 7;
  995. else
  996. ucfr_rfdiv = 6 - ucfr_rfdiv;
  997. uartclk = clk_get_rate(sport->clk);
  998. uartclk /= ucfr_rfdiv;
  999. { /*
  1000. * The next code provides exact computation of
  1001. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1002. * without need of float support or long long division,
  1003. * which would be required to prevent 32bit arithmetic overflow
  1004. */
  1005. unsigned int mul = ubir + 1;
  1006. unsigned int div = 16 * (ubmr + 1);
  1007. unsigned int rem = uartclk % div;
  1008. baud_raw = (uartclk / div) * mul;
  1009. baud_raw += (rem * mul + div / 2) / div;
  1010. *baud = (baud_raw + 50) / 100 * 100;
  1011. }
  1012. if(*baud != baud_raw)
  1013. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  1014. baud_raw, *baud);
  1015. }
  1016. }
  1017. static int __init
  1018. imx_console_setup(struct console *co, char *options)
  1019. {
  1020. struct imx_port *sport;
  1021. int baud = 9600;
  1022. int bits = 8;
  1023. int parity = 'n';
  1024. int flow = 'n';
  1025. /*
  1026. * Check whether an invalid uart number has been specified, and
  1027. * if so, search for the first available port that does have
  1028. * console support.
  1029. */
  1030. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1031. co->index = 0;
  1032. sport = imx_ports[co->index];
  1033. if(sport == NULL)
  1034. return -ENODEV;
  1035. if (options)
  1036. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1037. else
  1038. imx_console_get_options(sport, &baud, &parity, &bits);
  1039. imx_setup_ufcr(sport, 0);
  1040. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1041. }
  1042. static struct uart_driver imx_reg;
  1043. static struct console imx_console = {
  1044. .name = DEV_NAME,
  1045. .write = imx_console_write,
  1046. .device = uart_console_device,
  1047. .setup = imx_console_setup,
  1048. .flags = CON_PRINTBUFFER,
  1049. .index = -1,
  1050. .data = &imx_reg,
  1051. };
  1052. #define IMX_CONSOLE &imx_console
  1053. #else
  1054. #define IMX_CONSOLE NULL
  1055. #endif
  1056. static struct uart_driver imx_reg = {
  1057. .owner = THIS_MODULE,
  1058. .driver_name = DRIVER_NAME,
  1059. .dev_name = DEV_NAME,
  1060. .major = SERIAL_IMX_MAJOR,
  1061. .minor = MINOR_START,
  1062. .nr = ARRAY_SIZE(imx_ports),
  1063. .cons = IMX_CONSOLE,
  1064. };
  1065. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1066. {
  1067. struct imx_port *sport = platform_get_drvdata(dev);
  1068. unsigned int val;
  1069. /* enable wakeup from i.MX UART */
  1070. val = readl(sport->port.membase + UCR3);
  1071. val |= UCR3_AWAKEN;
  1072. writel(val, sport->port.membase + UCR3);
  1073. if (sport)
  1074. uart_suspend_port(&imx_reg, &sport->port);
  1075. return 0;
  1076. }
  1077. static int serial_imx_resume(struct platform_device *dev)
  1078. {
  1079. struct imx_port *sport = platform_get_drvdata(dev);
  1080. unsigned int val;
  1081. /* disable wakeup from i.MX UART */
  1082. val = readl(sport->port.membase + UCR3);
  1083. val &= ~UCR3_AWAKEN;
  1084. writel(val, sport->port.membase + UCR3);
  1085. if (sport)
  1086. uart_resume_port(&imx_reg, &sport->port);
  1087. return 0;
  1088. }
  1089. #ifdef CONFIG_OF
  1090. static int serial_imx_probe_dt(struct imx_port *sport,
  1091. struct platform_device *pdev)
  1092. {
  1093. struct device_node *np = pdev->dev.of_node;
  1094. const struct of_device_id *of_id =
  1095. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1096. int ret;
  1097. if (!np)
  1098. return -ENODEV;
  1099. ret = of_alias_get_id(np, "serial");
  1100. if (ret < 0) {
  1101. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1102. return ret;
  1103. }
  1104. sport->port.line = ret;
  1105. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1106. sport->have_rtscts = 1;
  1107. if (of_get_property(np, "fsl,irda-mode", NULL))
  1108. sport->use_irda = 1;
  1109. sport->devdata = of_id->data;
  1110. return 0;
  1111. }
  1112. #else
  1113. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1114. struct platform_device *pdev)
  1115. {
  1116. return -ENODEV;
  1117. }
  1118. #endif
  1119. static void serial_imx_probe_pdata(struct imx_port *sport,
  1120. struct platform_device *pdev)
  1121. {
  1122. struct imxuart_platform_data *pdata = pdev->dev.platform_data;
  1123. sport->port.line = pdev->id;
  1124. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1125. if (!pdata)
  1126. return;
  1127. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1128. sport->have_rtscts = 1;
  1129. if (pdata->flags & IMXUART_IRDA)
  1130. sport->use_irda = 1;
  1131. }
  1132. static int serial_imx_probe(struct platform_device *pdev)
  1133. {
  1134. struct imx_port *sport;
  1135. struct imxuart_platform_data *pdata;
  1136. void __iomem *base;
  1137. int ret = 0;
  1138. struct resource *res;
  1139. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  1140. if (!sport)
  1141. return -ENOMEM;
  1142. ret = serial_imx_probe_dt(sport, pdev);
  1143. if (ret == -ENODEV)
  1144. serial_imx_probe_pdata(sport, pdev);
  1145. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1146. if (!res) {
  1147. ret = -ENODEV;
  1148. goto free;
  1149. }
  1150. base = ioremap(res->start, PAGE_SIZE);
  1151. if (!base) {
  1152. ret = -ENOMEM;
  1153. goto free;
  1154. }
  1155. sport->port.dev = &pdev->dev;
  1156. sport->port.mapbase = res->start;
  1157. sport->port.membase = base;
  1158. sport->port.type = PORT_IMX,
  1159. sport->port.iotype = UPIO_MEM;
  1160. sport->port.irq = platform_get_irq(pdev, 0);
  1161. sport->rxirq = platform_get_irq(pdev, 0);
  1162. sport->txirq = platform_get_irq(pdev, 1);
  1163. sport->rtsirq = platform_get_irq(pdev, 2);
  1164. sport->port.fifosize = 32;
  1165. sport->port.ops = &imx_pops;
  1166. sport->port.flags = UPF_BOOT_AUTOCONF;
  1167. init_timer(&sport->timer);
  1168. sport->timer.function = imx_timeout;
  1169. sport->timer.data = (unsigned long)sport;
  1170. sport->clk = clk_get(&pdev->dev, "uart");
  1171. if (IS_ERR(sport->clk)) {
  1172. ret = PTR_ERR(sport->clk);
  1173. goto unmap;
  1174. }
  1175. clk_enable(sport->clk);
  1176. sport->port.uartclk = clk_get_rate(sport->clk);
  1177. imx_ports[sport->port.line] = sport;
  1178. pdata = pdev->dev.platform_data;
  1179. if (pdata && pdata->init) {
  1180. ret = pdata->init(pdev);
  1181. if (ret)
  1182. goto clkput;
  1183. }
  1184. ret = uart_add_one_port(&imx_reg, &sport->port);
  1185. if (ret)
  1186. goto deinit;
  1187. platform_set_drvdata(pdev, &sport->port);
  1188. return 0;
  1189. deinit:
  1190. if (pdata && pdata->exit)
  1191. pdata->exit(pdev);
  1192. clkput:
  1193. clk_put(sport->clk);
  1194. clk_disable(sport->clk);
  1195. unmap:
  1196. iounmap(sport->port.membase);
  1197. free:
  1198. kfree(sport);
  1199. return ret;
  1200. }
  1201. static int serial_imx_remove(struct platform_device *pdev)
  1202. {
  1203. struct imxuart_platform_data *pdata;
  1204. struct imx_port *sport = platform_get_drvdata(pdev);
  1205. pdata = pdev->dev.platform_data;
  1206. platform_set_drvdata(pdev, NULL);
  1207. if (sport) {
  1208. uart_remove_one_port(&imx_reg, &sport->port);
  1209. clk_put(sport->clk);
  1210. }
  1211. clk_disable(sport->clk);
  1212. if (pdata && pdata->exit)
  1213. pdata->exit(pdev);
  1214. iounmap(sport->port.membase);
  1215. kfree(sport);
  1216. return 0;
  1217. }
  1218. static struct platform_driver serial_imx_driver = {
  1219. .probe = serial_imx_probe,
  1220. .remove = serial_imx_remove,
  1221. .suspend = serial_imx_suspend,
  1222. .resume = serial_imx_resume,
  1223. .id_table = imx_uart_devtype,
  1224. .driver = {
  1225. .name = "imx-uart",
  1226. .owner = THIS_MODULE,
  1227. .of_match_table = imx_uart_dt_ids,
  1228. },
  1229. };
  1230. static int __init imx_serial_init(void)
  1231. {
  1232. int ret;
  1233. printk(KERN_INFO "Serial: IMX driver\n");
  1234. ret = uart_register_driver(&imx_reg);
  1235. if (ret)
  1236. return ret;
  1237. ret = platform_driver_register(&serial_imx_driver);
  1238. if (ret != 0)
  1239. uart_unregister_driver(&imx_reg);
  1240. return ret;
  1241. }
  1242. static void __exit imx_serial_exit(void)
  1243. {
  1244. platform_driver_unregister(&serial_imx_driver);
  1245. uart_unregister_driver(&imx_reg);
  1246. }
  1247. module_init(imx_serial_init);
  1248. module_exit(imx_serial_exit);
  1249. MODULE_AUTHOR("Sascha Hauer");
  1250. MODULE_DESCRIPTION("IMX generic serial port driver");
  1251. MODULE_LICENSE("GPL");
  1252. MODULE_ALIAS("platform:imx-uart");