imx.c 41 KB

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