imx.c 42 KB

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