imx.c 38 KB

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