imx.c 42 KB

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