imx.c 42 KB

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