imx.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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 <linux/dma-mapping.h>
  50. #include <asm/irq.h>
  51. #include <linux/platform_data/serial-imx.h>
  52. #include <linux/platform_data/dma-imx.h>
  53. /* Register definitions */
  54. #define URXD0 0x0 /* Receiver Register */
  55. #define URTX0 0x40 /* Transmitter Register */
  56. #define UCR1 0x80 /* Control Register 1 */
  57. #define UCR2 0x84 /* Control Register 2 */
  58. #define UCR3 0x88 /* Control Register 3 */
  59. #define UCR4 0x8c /* Control Register 4 */
  60. #define UFCR 0x90 /* FIFO Control Register */
  61. #define USR1 0x94 /* Status Register 1 */
  62. #define USR2 0x98 /* Status Register 2 */
  63. #define UESC 0x9c /* Escape Character Register */
  64. #define UTIM 0xa0 /* Escape Timer Register */
  65. #define UBIR 0xa4 /* BRM Incremental Register */
  66. #define UBMR 0xa8 /* BRM Modulator Register */
  67. #define UBRC 0xac /* Baud Rate Count Register */
  68. #define IMX21_ONEMS 0xb0 /* One Millisecond register */
  69. #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
  70. #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
  71. /* UART Control Register Bit Fields.*/
  72. #define URXD_CHARRDY (1<<15)
  73. #define URXD_ERR (1<<14)
  74. #define URXD_OVRRUN (1<<13)
  75. #define URXD_FRMERR (1<<12)
  76. #define URXD_BRK (1<<11)
  77. #define URXD_PRERR (1<<10)
  78. #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
  79. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  80. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  81. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  82. #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
  83. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  84. #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
  85. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  86. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  87. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  88. #define UCR1_SNDBRK (1<<4) /* Send break */
  89. #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
  90. #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
  91. #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
  92. #define UCR1_DOZE (1<<1) /* Doze */
  93. #define UCR1_UARTEN (1<<0) /* UART enabled */
  94. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  95. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  96. #define UCR2_CTSC (1<<13) /* CTS pin control */
  97. #define UCR2_CTS (1<<12) /* Clear to send */
  98. #define UCR2_ESCEN (1<<11) /* Escape enable */
  99. #define UCR2_PREN (1<<8) /* Parity enable */
  100. #define UCR2_PROE (1<<7) /* Parity odd/even */
  101. #define UCR2_STPB (1<<6) /* Stop */
  102. #define UCR2_WS (1<<5) /* Word size */
  103. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  104. #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
  105. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  106. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  107. #define UCR2_SRST (1<<0) /* SW reset */
  108. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  109. #define UCR3_PARERREN (1<<12) /* Parity enable */
  110. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  111. #define UCR3_DSR (1<<10) /* Data set ready */
  112. #define UCR3_DCD (1<<9) /* Data carrier detect */
  113. #define UCR3_RI (1<<8) /* Ring indicator */
  114. #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
  115. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  116. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  117. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  118. #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
  119. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  120. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  121. #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
  122. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  123. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  124. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  125. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  126. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  127. #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
  128. #define UCR4_IRSC (1<<5) /* IR special case */
  129. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  130. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  131. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  132. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  133. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  134. #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
  135. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  136. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  137. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  138. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  139. #define USR1_RTSS (1<<14) /* RTS pin status */
  140. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  141. #define USR1_RTSD (1<<12) /* RTS delta */
  142. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  143. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  144. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  145. #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
  146. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  147. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  148. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  149. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  150. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  151. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  152. #define USR2_IDLE (1<<12) /* Idle condition */
  153. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  154. #define USR2_WAKE (1<<7) /* Wake */
  155. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  156. #define USR2_TXDC (1<<3) /* Transmitter complete */
  157. #define USR2_BRCD (1<<2) /* Break condition */
  158. #define USR2_ORE (1<<1) /* Overrun error */
  159. #define USR2_RDR (1<<0) /* Recv data ready */
  160. #define UTS_FRCPERR (1<<13) /* Force parity error */
  161. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  162. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  163. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  164. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  165. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  166. #define UTS_SOFTRST (1<<0) /* Software reset */
  167. /* We've been assigned a range on the "Low-density serial ports" major */
  168. #define SERIAL_IMX_MAJOR 207
  169. #define MINOR_START 16
  170. #define DEV_NAME "ttymxc"
  171. /*
  172. * This determines how often we check the modem status signals
  173. * for any change. They generally aren't connected to an IRQ
  174. * so we have to poll them. We also check immediately before
  175. * filling the TX fifo incase CTS has been dropped.
  176. */
  177. #define MCTRL_TIMEOUT (250*HZ/1000)
  178. #define DRIVER_NAME "IMX-uart"
  179. #define UART_NR 8
  180. /* i.mx21 type uart runs on all i.mx except i.mx1 */
  181. enum imx_uart_type {
  182. IMX1_UART,
  183. IMX21_UART,
  184. IMX6Q_UART,
  185. };
  186. /* device type dependent stuff */
  187. struct imx_uart_data {
  188. unsigned uts_reg;
  189. enum imx_uart_type devtype;
  190. };
  191. struct imx_port {
  192. struct uart_port port;
  193. struct timer_list timer;
  194. unsigned int old_status;
  195. int txirq, rxirq, rtsirq;
  196. unsigned int have_rtscts:1;
  197. unsigned int dte_mode:1;
  198. unsigned int use_irda:1;
  199. unsigned int irda_inv_rx:1;
  200. unsigned int irda_inv_tx:1;
  201. unsigned short trcv_delay; /* transceiver delay */
  202. struct clk *clk_ipg;
  203. struct clk *clk_per;
  204. const struct imx_uart_data *devdata;
  205. /* DMA fields */
  206. unsigned int dma_is_inited:1;
  207. unsigned int dma_is_enabled:1;
  208. unsigned int dma_is_rxing:1;
  209. unsigned int dma_is_txing:1;
  210. struct dma_chan *dma_chan_rx, *dma_chan_tx;
  211. struct scatterlist rx_sgl, tx_sgl[2];
  212. void *rx_buf;
  213. unsigned int rx_bytes, tx_bytes;
  214. struct work_struct tsk_dma_rx, tsk_dma_tx;
  215. unsigned int dma_tx_nents;
  216. wait_queue_head_t dma_wait;
  217. };
  218. struct imx_port_ucrs {
  219. unsigned int ucr1;
  220. unsigned int ucr2;
  221. unsigned int ucr3;
  222. };
  223. #ifdef CONFIG_IRDA
  224. #define USE_IRDA(sport) ((sport)->use_irda)
  225. #else
  226. #define USE_IRDA(sport) (0)
  227. #endif
  228. static struct imx_uart_data imx_uart_devdata[] = {
  229. [IMX1_UART] = {
  230. .uts_reg = IMX1_UTS,
  231. .devtype = IMX1_UART,
  232. },
  233. [IMX21_UART] = {
  234. .uts_reg = IMX21_UTS,
  235. .devtype = IMX21_UART,
  236. },
  237. [IMX6Q_UART] = {
  238. .uts_reg = IMX21_UTS,
  239. .devtype = IMX6Q_UART,
  240. },
  241. };
  242. static struct platform_device_id imx_uart_devtype[] = {
  243. {
  244. .name = "imx1-uart",
  245. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX1_UART],
  246. }, {
  247. .name = "imx21-uart",
  248. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
  249. }, {
  250. .name = "imx6q-uart",
  251. .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX6Q_UART],
  252. }, {
  253. /* sentinel */
  254. }
  255. };
  256. MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
  257. static struct of_device_id imx_uart_dt_ids[] = {
  258. { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
  259. { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
  260. { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
  261. { /* sentinel */ }
  262. };
  263. MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
  264. static inline unsigned uts_reg(struct imx_port *sport)
  265. {
  266. return sport->devdata->uts_reg;
  267. }
  268. static inline int is_imx1_uart(struct imx_port *sport)
  269. {
  270. return sport->devdata->devtype == IMX1_UART;
  271. }
  272. static inline int is_imx21_uart(struct imx_port *sport)
  273. {
  274. return sport->devdata->devtype == IMX21_UART;
  275. }
  276. static inline int is_imx6q_uart(struct imx_port *sport)
  277. {
  278. return sport->devdata->devtype == IMX6Q_UART;
  279. }
  280. /*
  281. * Save and restore functions for UCR1, UCR2 and UCR3 registers
  282. */
  283. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_IMX_CONSOLE)
  284. static void imx_port_ucrs_save(struct uart_port *port,
  285. struct imx_port_ucrs *ucr)
  286. {
  287. /* save control registers */
  288. ucr->ucr1 = readl(port->membase + UCR1);
  289. ucr->ucr2 = readl(port->membase + UCR2);
  290. ucr->ucr3 = readl(port->membase + UCR3);
  291. }
  292. static void imx_port_ucrs_restore(struct uart_port *port,
  293. struct imx_port_ucrs *ucr)
  294. {
  295. /* restore control registers */
  296. writel(ucr->ucr1, port->membase + UCR1);
  297. writel(ucr->ucr2, port->membase + UCR2);
  298. writel(ucr->ucr3, port->membase + UCR3);
  299. }
  300. #endif
  301. /*
  302. * Handle any change of modem status signal since we were last called.
  303. */
  304. static void imx_mctrl_check(struct imx_port *sport)
  305. {
  306. unsigned int status, changed;
  307. status = sport->port.ops->get_mctrl(&sport->port);
  308. changed = status ^ sport->old_status;
  309. if (changed == 0)
  310. return;
  311. sport->old_status = status;
  312. if (changed & TIOCM_RI)
  313. sport->port.icount.rng++;
  314. if (changed & TIOCM_DSR)
  315. sport->port.icount.dsr++;
  316. if (changed & TIOCM_CAR)
  317. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  318. if (changed & TIOCM_CTS)
  319. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  320. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  321. }
  322. /*
  323. * This is our per-port timeout handler, for checking the
  324. * modem status signals.
  325. */
  326. static void imx_timeout(unsigned long data)
  327. {
  328. struct imx_port *sport = (struct imx_port *)data;
  329. unsigned long flags;
  330. if (sport->port.state) {
  331. spin_lock_irqsave(&sport->port.lock, flags);
  332. imx_mctrl_check(sport);
  333. spin_unlock_irqrestore(&sport->port.lock, flags);
  334. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  335. }
  336. }
  337. /*
  338. * interrupts disabled on entry
  339. */
  340. static void imx_stop_tx(struct uart_port *port)
  341. {
  342. struct imx_port *sport = (struct imx_port *)port;
  343. unsigned long temp;
  344. if (USE_IRDA(sport)) {
  345. /* half duplex - wait for end of transmission */
  346. int n = 256;
  347. while ((--n > 0) &&
  348. !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
  349. udelay(5);
  350. barrier();
  351. }
  352. /*
  353. * irda transceiver - wait a bit more to avoid
  354. * cutoff, hardware dependent
  355. */
  356. udelay(sport->trcv_delay);
  357. /*
  358. * half duplex - reactivate receive mode,
  359. * flush receive pipe echo crap
  360. */
  361. if (readl(sport->port.membase + USR2) & USR2_TXDC) {
  362. temp = readl(sport->port.membase + UCR1);
  363. temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
  364. writel(temp, sport->port.membase + UCR1);
  365. temp = readl(sport->port.membase + UCR4);
  366. temp &= ~(UCR4_TCEN);
  367. writel(temp, sport->port.membase + UCR4);
  368. while (readl(sport->port.membase + URXD0) &
  369. URXD_CHARRDY)
  370. barrier();
  371. temp = readl(sport->port.membase + UCR1);
  372. temp |= UCR1_RRDYEN;
  373. writel(temp, sport->port.membase + UCR1);
  374. temp = readl(sport->port.membase + UCR4);
  375. temp |= UCR4_DREN;
  376. writel(temp, sport->port.membase + UCR4);
  377. }
  378. return;
  379. }
  380. /*
  381. * We are maybe in the SMP context, so if the DMA TX thread is running
  382. * on other cpu, we have to wait for it to finish.
  383. */
  384. if (sport->dma_is_enabled && sport->dma_is_txing)
  385. return;
  386. temp = readl(sport->port.membase + UCR1);
  387. writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
  388. }
  389. /*
  390. * interrupts disabled on entry
  391. */
  392. static void imx_stop_rx(struct uart_port *port)
  393. {
  394. struct imx_port *sport = (struct imx_port *)port;
  395. unsigned long temp;
  396. /*
  397. * We are maybe in the SMP context, so if the DMA TX thread is running
  398. * on other cpu, we have to wait for it to finish.
  399. */
  400. if (sport->dma_is_enabled && sport->dma_is_rxing)
  401. return;
  402. temp = readl(sport->port.membase + UCR2);
  403. writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
  404. }
  405. /*
  406. * Set the modem control timer to fire immediately.
  407. */
  408. static void imx_enable_ms(struct uart_port *port)
  409. {
  410. struct imx_port *sport = (struct imx_port *)port;
  411. mod_timer(&sport->timer, jiffies);
  412. }
  413. static inline void imx_transmit_buffer(struct imx_port *sport)
  414. {
  415. struct circ_buf *xmit = &sport->port.state->xmit;
  416. while (!uart_circ_empty(xmit) &&
  417. !(readl(sport->port.membase + uts_reg(sport))
  418. & UTS_TXFULL)) {
  419. /* send xmit->buf[xmit->tail]
  420. * out the port here */
  421. writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
  422. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  423. sport->port.icount.tx++;
  424. }
  425. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  426. uart_write_wakeup(&sport->port);
  427. if (uart_circ_empty(xmit))
  428. imx_stop_tx(&sport->port);
  429. }
  430. static void dma_tx_callback(void *data)
  431. {
  432. struct imx_port *sport = data;
  433. struct scatterlist *sgl = &sport->tx_sgl[0];
  434. struct circ_buf *xmit = &sport->port.state->xmit;
  435. unsigned long flags;
  436. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  437. sport->dma_is_txing = 0;
  438. /* update the stat */
  439. spin_lock_irqsave(&sport->port.lock, flags);
  440. xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
  441. sport->port.icount.tx += sport->tx_bytes;
  442. spin_unlock_irqrestore(&sport->port.lock, flags);
  443. dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
  444. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  445. uart_write_wakeup(&sport->port);
  446. if (waitqueue_active(&sport->dma_wait)) {
  447. wake_up(&sport->dma_wait);
  448. dev_dbg(sport->port.dev, "exit in %s.\n", __func__);
  449. return;
  450. }
  451. schedule_work(&sport->tsk_dma_tx);
  452. }
  453. static void dma_tx_work(struct work_struct *w)
  454. {
  455. struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_tx);
  456. struct circ_buf *xmit = &sport->port.state->xmit;
  457. struct scatterlist *sgl = sport->tx_sgl;
  458. struct dma_async_tx_descriptor *desc;
  459. struct dma_chan *chan = sport->dma_chan_tx;
  460. struct device *dev = sport->port.dev;
  461. enum dma_status status;
  462. unsigned long flags;
  463. int ret;
  464. status = chan->device->device_tx_status(chan, (dma_cookie_t)0, NULL);
  465. if (DMA_IN_PROGRESS == status)
  466. return;
  467. spin_lock_irqsave(&sport->port.lock, flags);
  468. sport->tx_bytes = uart_circ_chars_pending(xmit);
  469. if (sport->tx_bytes == 0) {
  470. spin_unlock_irqrestore(&sport->port.lock, flags);
  471. return;
  472. }
  473. if (xmit->tail > xmit->head) {
  474. sport->dma_tx_nents = 2;
  475. sg_init_table(sgl, 2);
  476. sg_set_buf(sgl, xmit->buf + xmit->tail,
  477. UART_XMIT_SIZE - xmit->tail);
  478. sg_set_buf(sgl + 1, xmit->buf, xmit->head);
  479. } else {
  480. sport->dma_tx_nents = 1;
  481. sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
  482. }
  483. spin_unlock_irqrestore(&sport->port.lock, flags);
  484. ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  485. if (ret == 0) {
  486. dev_err(dev, "DMA mapping error for TX.\n");
  487. return;
  488. }
  489. desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
  490. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  491. if (!desc) {
  492. dev_err(dev, "We cannot prepare for the TX slave dma!\n");
  493. return;
  494. }
  495. desc->callback = dma_tx_callback;
  496. desc->callback_param = sport;
  497. dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
  498. uart_circ_chars_pending(xmit));
  499. /* fire it */
  500. sport->dma_is_txing = 1;
  501. dmaengine_submit(desc);
  502. dma_async_issue_pending(chan);
  503. return;
  504. }
  505. /*
  506. * interrupts disabled on entry
  507. */
  508. static void imx_start_tx(struct uart_port *port)
  509. {
  510. struct imx_port *sport = (struct imx_port *)port;
  511. unsigned long temp;
  512. if (USE_IRDA(sport)) {
  513. /* half duplex in IrDA mode; have to disable receive mode */
  514. temp = readl(sport->port.membase + UCR4);
  515. temp &= ~(UCR4_DREN);
  516. writel(temp, sport->port.membase + UCR4);
  517. temp = readl(sport->port.membase + UCR1);
  518. temp &= ~(UCR1_RRDYEN);
  519. writel(temp, sport->port.membase + UCR1);
  520. }
  521. /* Clear any pending ORE flag before enabling interrupt */
  522. temp = readl(sport->port.membase + USR2);
  523. writel(temp | USR2_ORE, sport->port.membase + USR2);
  524. temp = readl(sport->port.membase + UCR4);
  525. temp |= UCR4_OREN;
  526. writel(temp, sport->port.membase + UCR4);
  527. if (!sport->dma_is_enabled) {
  528. temp = readl(sport->port.membase + UCR1);
  529. writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
  530. }
  531. if (USE_IRDA(sport)) {
  532. temp = readl(sport->port.membase + UCR1);
  533. temp |= UCR1_TRDYEN;
  534. writel(temp, sport->port.membase + UCR1);
  535. temp = readl(sport->port.membase + UCR4);
  536. temp |= UCR4_TCEN;
  537. writel(temp, sport->port.membase + UCR4);
  538. }
  539. if (sport->dma_is_enabled) {
  540. /*
  541. * We may in the interrupt context, so arise a work_struct to
  542. * do the real job.
  543. */
  544. schedule_work(&sport->tsk_dma_tx);
  545. return;
  546. }
  547. if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY)
  548. imx_transmit_buffer(sport);
  549. }
  550. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  551. {
  552. struct imx_port *sport = dev_id;
  553. unsigned int val;
  554. unsigned long flags;
  555. spin_lock_irqsave(&sport->port.lock, flags);
  556. writel(USR1_RTSD, sport->port.membase + USR1);
  557. val = readl(sport->port.membase + USR1) & USR1_RTSS;
  558. uart_handle_cts_change(&sport->port, !!val);
  559. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  560. spin_unlock_irqrestore(&sport->port.lock, flags);
  561. return IRQ_HANDLED;
  562. }
  563. static irqreturn_t imx_txint(int irq, void *dev_id)
  564. {
  565. struct imx_port *sport = dev_id;
  566. struct circ_buf *xmit = &sport->port.state->xmit;
  567. unsigned long flags;
  568. spin_lock_irqsave(&sport->port.lock, flags);
  569. if (sport->port.x_char) {
  570. /* Send next char */
  571. writel(sport->port.x_char, sport->port.membase + URTX0);
  572. goto out;
  573. }
  574. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  575. imx_stop_tx(&sport->port);
  576. goto out;
  577. }
  578. imx_transmit_buffer(sport);
  579. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  580. uart_write_wakeup(&sport->port);
  581. out:
  582. spin_unlock_irqrestore(&sport->port.lock, flags);
  583. return IRQ_HANDLED;
  584. }
  585. static irqreturn_t imx_rxint(int irq, void *dev_id)
  586. {
  587. struct imx_port *sport = dev_id;
  588. unsigned int rx, flg, ignored = 0;
  589. struct tty_port *port = &sport->port.state->port;
  590. unsigned long flags, temp;
  591. spin_lock_irqsave(&sport->port.lock, flags);
  592. while (readl(sport->port.membase + USR2) & USR2_RDR) {
  593. flg = TTY_NORMAL;
  594. sport->port.icount.rx++;
  595. rx = readl(sport->port.membase + URXD0);
  596. temp = readl(sport->port.membase + USR2);
  597. if (temp & USR2_BRCD) {
  598. writel(USR2_BRCD, sport->port.membase + USR2);
  599. if (uart_handle_break(&sport->port))
  600. continue;
  601. }
  602. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  603. continue;
  604. if (unlikely(rx & URXD_ERR)) {
  605. if (rx & URXD_BRK)
  606. sport->port.icount.brk++;
  607. else if (rx & URXD_PRERR)
  608. sport->port.icount.parity++;
  609. else if (rx & URXD_FRMERR)
  610. sport->port.icount.frame++;
  611. if (rx & URXD_OVRRUN)
  612. sport->port.icount.overrun++;
  613. if (rx & sport->port.ignore_status_mask) {
  614. if (++ignored > 100)
  615. goto out;
  616. continue;
  617. }
  618. rx &= sport->port.read_status_mask;
  619. if (rx & URXD_BRK)
  620. flg = TTY_BREAK;
  621. else if (rx & URXD_PRERR)
  622. flg = TTY_PARITY;
  623. else if (rx & URXD_FRMERR)
  624. flg = TTY_FRAME;
  625. if (rx & URXD_OVRRUN)
  626. flg = TTY_OVERRUN;
  627. #ifdef SUPPORT_SYSRQ
  628. sport->port.sysrq = 0;
  629. #endif
  630. }
  631. tty_insert_flip_char(port, rx, flg);
  632. }
  633. out:
  634. spin_unlock_irqrestore(&sport->port.lock, flags);
  635. tty_flip_buffer_push(port);
  636. return IRQ_HANDLED;
  637. }
  638. /*
  639. * If the RXFIFO is filled with some data, and then we
  640. * arise a DMA operation to receive them.
  641. */
  642. static void imx_dma_rxint(struct imx_port *sport)
  643. {
  644. unsigned long temp;
  645. temp = readl(sport->port.membase + USR2);
  646. if ((temp & USR2_RDR) && !sport->dma_is_rxing) {
  647. sport->dma_is_rxing = 1;
  648. /* disable the `Recerver Ready Interrrupt` */
  649. temp = readl(sport->port.membase + UCR1);
  650. temp &= ~(UCR1_RRDYEN);
  651. writel(temp, sport->port.membase + UCR1);
  652. /* tell the DMA to receive the data. */
  653. schedule_work(&sport->tsk_dma_rx);
  654. }
  655. }
  656. static irqreturn_t imx_int(int irq, void *dev_id)
  657. {
  658. struct imx_port *sport = dev_id;
  659. unsigned int sts;
  660. unsigned int sts2;
  661. sts = readl(sport->port.membase + USR1);
  662. if (sts & USR1_RRDY) {
  663. if (sport->dma_is_enabled)
  664. imx_dma_rxint(sport);
  665. else
  666. imx_rxint(irq, dev_id);
  667. }
  668. if (sts & USR1_TRDY &&
  669. readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
  670. imx_txint(irq, dev_id);
  671. if (sts & USR1_RTSD)
  672. imx_rtsint(irq, dev_id);
  673. if (sts & USR1_AWAKE)
  674. writel(USR1_AWAKE, sport->port.membase + USR1);
  675. sts2 = readl(sport->port.membase + USR2);
  676. if (sts2 & USR2_ORE) {
  677. dev_err(sport->port.dev, "Rx FIFO overrun\n");
  678. sport->port.icount.overrun++;
  679. writel(sts2 | USR2_ORE, sport->port.membase + USR2);
  680. }
  681. return IRQ_HANDLED;
  682. }
  683. /*
  684. * Return TIOCSER_TEMT when transmitter is not busy.
  685. */
  686. static unsigned int imx_tx_empty(struct uart_port *port)
  687. {
  688. struct imx_port *sport = (struct imx_port *)port;
  689. unsigned int ret;
  690. ret = (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  691. /* If the TX DMA is working, return 0. */
  692. if (sport->dma_is_enabled && sport->dma_is_txing)
  693. ret = 0;
  694. return ret;
  695. }
  696. /*
  697. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  698. */
  699. static unsigned int imx_get_mctrl(struct uart_port *port)
  700. {
  701. struct imx_port *sport = (struct imx_port *)port;
  702. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  703. if (readl(sport->port.membase + USR1) & USR1_RTSS)
  704. tmp |= TIOCM_CTS;
  705. if (readl(sport->port.membase + UCR2) & UCR2_CTS)
  706. tmp |= TIOCM_RTS;
  707. return tmp;
  708. }
  709. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  710. {
  711. struct imx_port *sport = (struct imx_port *)port;
  712. unsigned long temp;
  713. temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
  714. if (mctrl & TIOCM_RTS)
  715. if (!sport->dma_is_enabled)
  716. temp |= UCR2_CTS;
  717. writel(temp, sport->port.membase + UCR2);
  718. }
  719. /*
  720. * Interrupts always disabled.
  721. */
  722. static void imx_break_ctl(struct uart_port *port, int break_state)
  723. {
  724. struct imx_port *sport = (struct imx_port *)port;
  725. unsigned long flags, temp;
  726. spin_lock_irqsave(&sport->port.lock, flags);
  727. temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
  728. if (break_state != 0)
  729. temp |= UCR1_SNDBRK;
  730. writel(temp, sport->port.membase + UCR1);
  731. spin_unlock_irqrestore(&sport->port.lock, flags);
  732. }
  733. #define TXTL 2 /* reset default */
  734. #define RXTL 1 /* reset default */
  735. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  736. {
  737. unsigned int val;
  738. /* set receiver / transmitter trigger level */
  739. val = readl(sport->port.membase + UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
  740. val |= TXTL << UFCR_TXTL_SHF | RXTL;
  741. writel(val, sport->port.membase + UFCR);
  742. return 0;
  743. }
  744. #define RX_BUF_SIZE (PAGE_SIZE)
  745. static int start_rx_dma(struct imx_port *sport);
  746. static void dma_rx_work(struct work_struct *w)
  747. {
  748. struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_rx);
  749. struct tty_port *port = &sport->port.state->port;
  750. if (sport->rx_bytes) {
  751. tty_insert_flip_string(port, sport->rx_buf, sport->rx_bytes);
  752. tty_flip_buffer_push(port);
  753. sport->rx_bytes = 0;
  754. }
  755. if (sport->dma_is_rxing)
  756. start_rx_dma(sport);
  757. }
  758. static void imx_rx_dma_done(struct imx_port *sport)
  759. {
  760. unsigned long temp;
  761. /* Enable this interrupt when the RXFIFO is empty. */
  762. temp = readl(sport->port.membase + UCR1);
  763. temp |= UCR1_RRDYEN;
  764. writel(temp, sport->port.membase + UCR1);
  765. sport->dma_is_rxing = 0;
  766. /* Is the shutdown waiting for us? */
  767. if (waitqueue_active(&sport->dma_wait))
  768. wake_up(&sport->dma_wait);
  769. }
  770. /*
  771. * There are three kinds of RX DMA interrupts(such as in the MX6Q):
  772. * [1] the RX DMA buffer is full.
  773. * [2] the Aging timer expires(wait for 8 bytes long)
  774. * [3] the Idle Condition Detect(enabled the UCR4_IDDMAEN).
  775. *
  776. * The [2] is trigger when a character was been sitting in the FIFO
  777. * meanwhile [3] can wait for 32 bytes long when the RX line is
  778. * on IDLE state and RxFIFO is empty.
  779. */
  780. static void dma_rx_callback(void *data)
  781. {
  782. struct imx_port *sport = data;
  783. struct dma_chan *chan = sport->dma_chan_rx;
  784. struct scatterlist *sgl = &sport->rx_sgl;
  785. struct dma_tx_state state;
  786. enum dma_status status;
  787. unsigned int count;
  788. /* unmap it first */
  789. dma_unmap_sg(sport->port.dev, sgl, 1, DMA_FROM_DEVICE);
  790. status = chan->device->device_tx_status(chan, (dma_cookie_t)0, &state);
  791. count = RX_BUF_SIZE - state.residue;
  792. dev_dbg(sport->port.dev, "We get %d bytes.\n", count);
  793. if (count) {
  794. sport->rx_bytes = count;
  795. schedule_work(&sport->tsk_dma_rx);
  796. } else
  797. imx_rx_dma_done(sport);
  798. }
  799. static int start_rx_dma(struct imx_port *sport)
  800. {
  801. struct scatterlist *sgl = &sport->rx_sgl;
  802. struct dma_chan *chan = sport->dma_chan_rx;
  803. struct device *dev = sport->port.dev;
  804. struct dma_async_tx_descriptor *desc;
  805. int ret;
  806. sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
  807. ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
  808. if (ret == 0) {
  809. dev_err(dev, "DMA mapping error for RX.\n");
  810. return -EINVAL;
  811. }
  812. desc = dmaengine_prep_slave_sg(chan, sgl, 1, DMA_DEV_TO_MEM,
  813. DMA_PREP_INTERRUPT);
  814. if (!desc) {
  815. dev_err(dev, "We cannot prepare for the RX slave dma!\n");
  816. return -EINVAL;
  817. }
  818. desc->callback = dma_rx_callback;
  819. desc->callback_param = sport;
  820. dev_dbg(dev, "RX: prepare for the DMA.\n");
  821. dmaengine_submit(desc);
  822. dma_async_issue_pending(chan);
  823. return 0;
  824. }
  825. static void imx_uart_dma_exit(struct imx_port *sport)
  826. {
  827. if (sport->dma_chan_rx) {
  828. dma_release_channel(sport->dma_chan_rx);
  829. sport->dma_chan_rx = NULL;
  830. kfree(sport->rx_buf);
  831. sport->rx_buf = NULL;
  832. }
  833. if (sport->dma_chan_tx) {
  834. dma_release_channel(sport->dma_chan_tx);
  835. sport->dma_chan_tx = NULL;
  836. }
  837. sport->dma_is_inited = 0;
  838. }
  839. static int imx_uart_dma_init(struct imx_port *sport)
  840. {
  841. struct dma_slave_config slave_config = {};
  842. struct device *dev = sport->port.dev;
  843. int ret;
  844. /* Prepare for RX : */
  845. sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
  846. if (!sport->dma_chan_rx) {
  847. dev_dbg(dev, "cannot get the DMA channel.\n");
  848. ret = -EINVAL;
  849. goto err;
  850. }
  851. slave_config.direction = DMA_DEV_TO_MEM;
  852. slave_config.src_addr = sport->port.mapbase + URXD0;
  853. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  854. slave_config.src_maxburst = RXTL;
  855. ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
  856. if (ret) {
  857. dev_err(dev, "error in RX dma configuration.\n");
  858. goto err;
  859. }
  860. sport->rx_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  861. if (!sport->rx_buf) {
  862. dev_err(dev, "cannot alloc DMA buffer.\n");
  863. ret = -ENOMEM;
  864. goto err;
  865. }
  866. sport->rx_bytes = 0;
  867. /* Prepare for TX : */
  868. sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
  869. if (!sport->dma_chan_tx) {
  870. dev_err(dev, "cannot get the TX DMA channel!\n");
  871. ret = -EINVAL;
  872. goto err;
  873. }
  874. slave_config.direction = DMA_MEM_TO_DEV;
  875. slave_config.dst_addr = sport->port.mapbase + URTX0;
  876. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  877. slave_config.dst_maxburst = TXTL;
  878. ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
  879. if (ret) {
  880. dev_err(dev, "error in TX dma configuration.");
  881. goto err;
  882. }
  883. sport->dma_is_inited = 1;
  884. return 0;
  885. err:
  886. imx_uart_dma_exit(sport);
  887. return ret;
  888. }
  889. static void imx_enable_dma(struct imx_port *sport)
  890. {
  891. unsigned long temp;
  892. struct tty_port *port = &sport->port.state->port;
  893. port->low_latency = 1;
  894. INIT_WORK(&sport->tsk_dma_tx, dma_tx_work);
  895. INIT_WORK(&sport->tsk_dma_rx, dma_rx_work);
  896. init_waitqueue_head(&sport->dma_wait);
  897. /* set UCR1 */
  898. temp = readl(sport->port.membase + UCR1);
  899. temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN |
  900. /* wait for 32 idle frames for IDDMA interrupt */
  901. UCR1_ICD_REG(3);
  902. writel(temp, sport->port.membase + UCR1);
  903. /* set UCR4 */
  904. temp = readl(sport->port.membase + UCR4);
  905. temp |= UCR4_IDDMAEN;
  906. writel(temp, sport->port.membase + UCR4);
  907. sport->dma_is_enabled = 1;
  908. }
  909. static void imx_disable_dma(struct imx_port *sport)
  910. {
  911. unsigned long temp;
  912. struct tty_port *port = &sport->port.state->port;
  913. /* clear UCR1 */
  914. temp = readl(sport->port.membase + UCR1);
  915. temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
  916. writel(temp, sport->port.membase + UCR1);
  917. /* clear UCR2 */
  918. temp = readl(sport->port.membase + UCR2);
  919. temp &= ~(UCR2_CTSC | UCR2_CTS);
  920. writel(temp, sport->port.membase + UCR2);
  921. /* clear UCR4 */
  922. temp = readl(sport->port.membase + UCR4);
  923. temp &= ~UCR4_IDDMAEN;
  924. writel(temp, sport->port.membase + UCR4);
  925. sport->dma_is_enabled = 0;
  926. port->low_latency = 0;
  927. }
  928. /* half the RX buffer size */
  929. #define CTSTL 16
  930. static int imx_startup(struct uart_port *port)
  931. {
  932. struct imx_port *sport = (struct imx_port *)port;
  933. int retval;
  934. unsigned long flags, temp;
  935. retval = clk_prepare_enable(sport->clk_per);
  936. if (retval)
  937. goto error_out1;
  938. retval = clk_prepare_enable(sport->clk_ipg);
  939. if (retval) {
  940. clk_disable_unprepare(sport->clk_per);
  941. goto error_out1;
  942. }
  943. imx_setup_ufcr(sport, 0);
  944. /* disable the DREN bit (Data Ready interrupt enable) before
  945. * requesting IRQs
  946. */
  947. temp = readl(sport->port.membase + UCR4);
  948. if (USE_IRDA(sport))
  949. temp |= UCR4_IRSC;
  950. /* set the trigger level for CTS */
  951. temp &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
  952. temp |= CTSTL << UCR4_CTSTL_SHF;
  953. writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
  954. if (USE_IRDA(sport)) {
  955. /* reset fifo's and state machines */
  956. int i = 100;
  957. temp = readl(sport->port.membase + UCR2);
  958. temp &= ~UCR2_SRST;
  959. writel(temp, sport->port.membase + UCR2);
  960. while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
  961. (--i > 0)) {
  962. udelay(1);
  963. }
  964. }
  965. /*
  966. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  967. * chips only have one interrupt.
  968. */
  969. if (sport->txirq > 0) {
  970. retval = request_irq(sport->rxirq, imx_rxint, 0,
  971. DRIVER_NAME, sport);
  972. if (retval)
  973. goto error_out1;
  974. retval = request_irq(sport->txirq, imx_txint, 0,
  975. DRIVER_NAME, sport);
  976. if (retval)
  977. goto error_out2;
  978. /* do not use RTS IRQ on IrDA */
  979. if (!USE_IRDA(sport)) {
  980. retval = request_irq(sport->rtsirq, imx_rtsint, 0,
  981. DRIVER_NAME, sport);
  982. if (retval)
  983. goto error_out3;
  984. }
  985. } else {
  986. retval = request_irq(sport->port.irq, imx_int, 0,
  987. DRIVER_NAME, sport);
  988. if (retval) {
  989. free_irq(sport->port.irq, sport);
  990. goto error_out1;
  991. }
  992. }
  993. spin_lock_irqsave(&sport->port.lock, flags);
  994. /*
  995. * Finally, clear and enable interrupts
  996. */
  997. writel(USR1_RTSD, sport->port.membase + USR1);
  998. temp = readl(sport->port.membase + UCR1);
  999. temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
  1000. if (USE_IRDA(sport)) {
  1001. temp |= UCR1_IREN;
  1002. temp &= ~(UCR1_RTSDEN);
  1003. }
  1004. writel(temp, sport->port.membase + UCR1);
  1005. temp = readl(sport->port.membase + UCR2);
  1006. temp |= (UCR2_RXEN | UCR2_TXEN);
  1007. if (!sport->have_rtscts)
  1008. temp |= UCR2_IRTS;
  1009. writel(temp, sport->port.membase + UCR2);
  1010. if (USE_IRDA(sport)) {
  1011. /* clear RX-FIFO */
  1012. int i = 64;
  1013. while ((--i > 0) &&
  1014. (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
  1015. barrier();
  1016. }
  1017. }
  1018. if (!is_imx1_uart(sport)) {
  1019. temp = readl(sport->port.membase + UCR3);
  1020. temp |= IMX21_UCR3_RXDMUXSEL;
  1021. writel(temp, sport->port.membase + UCR3);
  1022. }
  1023. if (USE_IRDA(sport)) {
  1024. temp = readl(sport->port.membase + UCR4);
  1025. if (sport->irda_inv_rx)
  1026. temp |= UCR4_INVR;
  1027. else
  1028. temp &= ~(UCR4_INVR);
  1029. writel(temp | UCR4_DREN, sport->port.membase + UCR4);
  1030. temp = readl(sport->port.membase + UCR3);
  1031. if (sport->irda_inv_tx)
  1032. temp |= UCR3_INVT;
  1033. else
  1034. temp &= ~(UCR3_INVT);
  1035. writel(temp, sport->port.membase + UCR3);
  1036. }
  1037. /*
  1038. * Enable modem status interrupts
  1039. */
  1040. imx_enable_ms(&sport->port);
  1041. spin_unlock_irqrestore(&sport->port.lock, flags);
  1042. if (USE_IRDA(sport)) {
  1043. struct imxuart_platform_data *pdata;
  1044. pdata = dev_get_platdata(sport->port.dev);
  1045. sport->irda_inv_rx = pdata->irda_inv_rx;
  1046. sport->irda_inv_tx = pdata->irda_inv_tx;
  1047. sport->trcv_delay = pdata->transceiver_delay;
  1048. if (pdata->irda_enable)
  1049. pdata->irda_enable(1);
  1050. }
  1051. return 0;
  1052. error_out3:
  1053. if (sport->txirq)
  1054. free_irq(sport->txirq, sport);
  1055. error_out2:
  1056. if (sport->rxirq)
  1057. free_irq(sport->rxirq, sport);
  1058. error_out1:
  1059. return retval;
  1060. }
  1061. static void imx_shutdown(struct uart_port *port)
  1062. {
  1063. struct imx_port *sport = (struct imx_port *)port;
  1064. unsigned long temp;
  1065. unsigned long flags;
  1066. if (sport->dma_is_enabled) {
  1067. /* We have to wait for the DMA to finish. */
  1068. wait_event(sport->dma_wait,
  1069. !sport->dma_is_rxing && !sport->dma_is_txing);
  1070. imx_stop_rx(port);
  1071. imx_disable_dma(sport);
  1072. imx_uart_dma_exit(sport);
  1073. }
  1074. spin_lock_irqsave(&sport->port.lock, flags);
  1075. temp = readl(sport->port.membase + UCR2);
  1076. temp &= ~(UCR2_TXEN);
  1077. writel(temp, sport->port.membase + UCR2);
  1078. spin_unlock_irqrestore(&sport->port.lock, flags);
  1079. if (USE_IRDA(sport)) {
  1080. struct imxuart_platform_data *pdata;
  1081. pdata = dev_get_platdata(sport->port.dev);
  1082. if (pdata->irda_enable)
  1083. pdata->irda_enable(0);
  1084. }
  1085. /*
  1086. * Stop our timer.
  1087. */
  1088. del_timer_sync(&sport->timer);
  1089. /*
  1090. * Free the interrupts
  1091. */
  1092. if (sport->txirq > 0) {
  1093. if (!USE_IRDA(sport))
  1094. free_irq(sport->rtsirq, sport);
  1095. free_irq(sport->txirq, sport);
  1096. free_irq(sport->rxirq, sport);
  1097. } else
  1098. free_irq(sport->port.irq, sport);
  1099. /*
  1100. * Disable all interrupts, port and break condition.
  1101. */
  1102. spin_lock_irqsave(&sport->port.lock, flags);
  1103. temp = readl(sport->port.membase + UCR1);
  1104. temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  1105. if (USE_IRDA(sport))
  1106. temp &= ~(UCR1_IREN);
  1107. writel(temp, sport->port.membase + UCR1);
  1108. spin_unlock_irqrestore(&sport->port.lock, flags);
  1109. clk_disable_unprepare(sport->clk_per);
  1110. clk_disable_unprepare(sport->clk_ipg);
  1111. }
  1112. static void imx_flush_buffer(struct uart_port *port)
  1113. {
  1114. struct imx_port *sport = (struct imx_port *)port;
  1115. if (sport->dma_is_enabled) {
  1116. sport->tx_bytes = 0;
  1117. dmaengine_terminate_all(sport->dma_chan_tx);
  1118. }
  1119. }
  1120. static void
  1121. imx_set_termios(struct uart_port *port, struct ktermios *termios,
  1122. struct ktermios *old)
  1123. {
  1124. struct imx_port *sport = (struct imx_port *)port;
  1125. unsigned long flags;
  1126. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  1127. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1128. unsigned int div, ufcr;
  1129. unsigned long num, denom;
  1130. uint64_t tdiv64;
  1131. /*
  1132. * If we don't support modem control lines, don't allow
  1133. * these to be set.
  1134. */
  1135. if (0) {
  1136. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  1137. termios->c_cflag |= CLOCAL;
  1138. }
  1139. /*
  1140. * We only support CS7 and CS8.
  1141. */
  1142. while ((termios->c_cflag & CSIZE) != CS7 &&
  1143. (termios->c_cflag & CSIZE) != CS8) {
  1144. termios->c_cflag &= ~CSIZE;
  1145. termios->c_cflag |= old_csize;
  1146. old_csize = CS8;
  1147. }
  1148. if ((termios->c_cflag & CSIZE) == CS8)
  1149. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  1150. else
  1151. ucr2 = UCR2_SRST | UCR2_IRTS;
  1152. if (termios->c_cflag & CRTSCTS) {
  1153. if (sport->have_rtscts) {
  1154. ucr2 &= ~UCR2_IRTS;
  1155. ucr2 |= UCR2_CTSC;
  1156. /* Can we enable the DMA support? */
  1157. if (is_imx6q_uart(sport) && !uart_console(port)
  1158. && !sport->dma_is_inited)
  1159. imx_uart_dma_init(sport);
  1160. } else {
  1161. termios->c_cflag &= ~CRTSCTS;
  1162. }
  1163. }
  1164. if (termios->c_cflag & CSTOPB)
  1165. ucr2 |= UCR2_STPB;
  1166. if (termios->c_cflag & PARENB) {
  1167. ucr2 |= UCR2_PREN;
  1168. if (termios->c_cflag & PARODD)
  1169. ucr2 |= UCR2_PROE;
  1170. }
  1171. del_timer_sync(&sport->timer);
  1172. /*
  1173. * Ask the core to calculate the divisor for us.
  1174. */
  1175. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1176. quot = uart_get_divisor(port, baud);
  1177. spin_lock_irqsave(&sport->port.lock, flags);
  1178. sport->port.read_status_mask = 0;
  1179. if (termios->c_iflag & INPCK)
  1180. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  1181. if (termios->c_iflag & (BRKINT | PARMRK))
  1182. sport->port.read_status_mask |= URXD_BRK;
  1183. /*
  1184. * Characters to ignore
  1185. */
  1186. sport->port.ignore_status_mask = 0;
  1187. if (termios->c_iflag & IGNPAR)
  1188. sport->port.ignore_status_mask |= URXD_PRERR;
  1189. if (termios->c_iflag & IGNBRK) {
  1190. sport->port.ignore_status_mask |= URXD_BRK;
  1191. /*
  1192. * If we're ignoring parity and break indicators,
  1193. * ignore overruns too (for real raw support).
  1194. */
  1195. if (termios->c_iflag & IGNPAR)
  1196. sport->port.ignore_status_mask |= URXD_OVRRUN;
  1197. }
  1198. /*
  1199. * Update the per-port timeout.
  1200. */
  1201. uart_update_timeout(port, termios->c_cflag, baud);
  1202. /*
  1203. * disable interrupts and drain transmitter
  1204. */
  1205. old_ucr1 = readl(sport->port.membase + UCR1);
  1206. writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
  1207. sport->port.membase + UCR1);
  1208. while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
  1209. barrier();
  1210. /* then, disable everything */
  1211. old_txrxen = readl(sport->port.membase + UCR2);
  1212. writel(old_txrxen & ~(UCR2_TXEN | UCR2_RXEN),
  1213. sport->port.membase + UCR2);
  1214. old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
  1215. if (USE_IRDA(sport)) {
  1216. /*
  1217. * use maximum available submodule frequency to
  1218. * avoid missing short pulses due to low sampling rate
  1219. */
  1220. div = 1;
  1221. } else {
  1222. /* custom-baudrate handling */
  1223. div = sport->port.uartclk / (baud * 16);
  1224. if (baud == 38400 && quot != div)
  1225. baud = sport->port.uartclk / (quot * 16);
  1226. div = sport->port.uartclk / (baud * 16);
  1227. if (div > 7)
  1228. div = 7;
  1229. if (!div)
  1230. div = 1;
  1231. }
  1232. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  1233. 1 << 16, 1 << 16, &num, &denom);
  1234. tdiv64 = sport->port.uartclk;
  1235. tdiv64 *= num;
  1236. do_div(tdiv64, denom * 16 * div);
  1237. tty_termios_encode_baud_rate(termios,
  1238. (speed_t)tdiv64, (speed_t)tdiv64);
  1239. num -= 1;
  1240. denom -= 1;
  1241. ufcr = readl(sport->port.membase + UFCR);
  1242. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  1243. if (sport->dte_mode)
  1244. ufcr |= UFCR_DCEDTE;
  1245. writel(ufcr, sport->port.membase + UFCR);
  1246. writel(num, sport->port.membase + UBIR);
  1247. writel(denom, sport->port.membase + UBMR);
  1248. if (!is_imx1_uart(sport))
  1249. writel(sport->port.uartclk / div / 1000,
  1250. sport->port.membase + IMX21_ONEMS);
  1251. writel(old_ucr1, sport->port.membase + UCR1);
  1252. /* set the parity, stop bits and data size */
  1253. writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
  1254. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  1255. imx_enable_ms(&sport->port);
  1256. if (sport->dma_is_inited && !sport->dma_is_enabled)
  1257. imx_enable_dma(sport);
  1258. spin_unlock_irqrestore(&sport->port.lock, flags);
  1259. }
  1260. static const char *imx_type(struct uart_port *port)
  1261. {
  1262. struct imx_port *sport = (struct imx_port *)port;
  1263. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  1264. }
  1265. /*
  1266. * Release the memory region(s) being used by 'port'.
  1267. */
  1268. static void imx_release_port(struct uart_port *port)
  1269. {
  1270. struct platform_device *pdev = to_platform_device(port->dev);
  1271. struct resource *mmres;
  1272. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1273. release_mem_region(mmres->start, resource_size(mmres));
  1274. }
  1275. /*
  1276. * Request the memory region(s) being used by 'port'.
  1277. */
  1278. static int imx_request_port(struct uart_port *port)
  1279. {
  1280. struct platform_device *pdev = to_platform_device(port->dev);
  1281. struct resource *mmres;
  1282. void *ret;
  1283. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1284. if (!mmres)
  1285. return -ENODEV;
  1286. ret = request_mem_region(mmres->start, resource_size(mmres), "imx-uart");
  1287. return ret ? 0 : -EBUSY;
  1288. }
  1289. /*
  1290. * Configure/autoconfigure the port.
  1291. */
  1292. static void imx_config_port(struct uart_port *port, int flags)
  1293. {
  1294. struct imx_port *sport = (struct imx_port *)port;
  1295. if (flags & UART_CONFIG_TYPE &&
  1296. imx_request_port(&sport->port) == 0)
  1297. sport->port.type = PORT_IMX;
  1298. }
  1299. /*
  1300. * Verify the new serial_struct (for TIOCSSERIAL).
  1301. * The only change we allow are to the flags and type, and
  1302. * even then only between PORT_IMX and PORT_UNKNOWN
  1303. */
  1304. static int
  1305. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  1306. {
  1307. struct imx_port *sport = (struct imx_port *)port;
  1308. int ret = 0;
  1309. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  1310. ret = -EINVAL;
  1311. if (sport->port.irq != ser->irq)
  1312. ret = -EINVAL;
  1313. if (ser->io_type != UPIO_MEM)
  1314. ret = -EINVAL;
  1315. if (sport->port.uartclk / 16 != ser->baud_base)
  1316. ret = -EINVAL;
  1317. if (sport->port.mapbase != (unsigned long)ser->iomem_base)
  1318. ret = -EINVAL;
  1319. if (sport->port.iobase != ser->port)
  1320. ret = -EINVAL;
  1321. if (ser->hub6 != 0)
  1322. ret = -EINVAL;
  1323. return ret;
  1324. }
  1325. #if defined(CONFIG_CONSOLE_POLL)
  1326. static int imx_poll_get_char(struct uart_port *port)
  1327. {
  1328. struct imx_port_ucrs old_ucr;
  1329. unsigned int status;
  1330. unsigned char c;
  1331. /* save control registers */
  1332. imx_port_ucrs_save(port, &old_ucr);
  1333. /* disable interrupts */
  1334. writel(UCR1_UARTEN, port->membase + UCR1);
  1335. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  1336. port->membase + UCR2);
  1337. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  1338. port->membase + UCR3);
  1339. /* poll */
  1340. do {
  1341. status = readl(port->membase + USR2);
  1342. } while (~status & USR2_RDR);
  1343. /* read */
  1344. c = readl(port->membase + URXD0);
  1345. /* restore control registers */
  1346. imx_port_ucrs_restore(port, &old_ucr);
  1347. return c;
  1348. }
  1349. static void imx_poll_put_char(struct uart_port *port, unsigned char c)
  1350. {
  1351. struct imx_port_ucrs old_ucr;
  1352. unsigned int status;
  1353. /* save control registers */
  1354. imx_port_ucrs_save(port, &old_ucr);
  1355. /* disable interrupts */
  1356. writel(UCR1_UARTEN, port->membase + UCR1);
  1357. writel(old_ucr.ucr2 & ~(UCR2_ATEN | UCR2_RTSEN | UCR2_ESCI),
  1358. port->membase + UCR2);
  1359. writel(old_ucr.ucr3 & ~(UCR3_DCD | UCR3_RI | UCR3_DTREN),
  1360. port->membase + UCR3);
  1361. /* drain */
  1362. do {
  1363. status = readl(port->membase + USR1);
  1364. } while (~status & USR1_TRDY);
  1365. /* write */
  1366. writel(c, port->membase + URTX0);
  1367. /* flush */
  1368. do {
  1369. status = readl(port->membase + USR2);
  1370. } while (~status & USR2_TXDC);
  1371. /* restore control registers */
  1372. imx_port_ucrs_restore(port, &old_ucr);
  1373. }
  1374. #endif
  1375. static struct uart_ops imx_pops = {
  1376. .tx_empty = imx_tx_empty,
  1377. .set_mctrl = imx_set_mctrl,
  1378. .get_mctrl = imx_get_mctrl,
  1379. .stop_tx = imx_stop_tx,
  1380. .start_tx = imx_start_tx,
  1381. .stop_rx = imx_stop_rx,
  1382. .enable_ms = imx_enable_ms,
  1383. .break_ctl = imx_break_ctl,
  1384. .startup = imx_startup,
  1385. .shutdown = imx_shutdown,
  1386. .flush_buffer = imx_flush_buffer,
  1387. .set_termios = imx_set_termios,
  1388. .type = imx_type,
  1389. .release_port = imx_release_port,
  1390. .request_port = imx_request_port,
  1391. .config_port = imx_config_port,
  1392. .verify_port = imx_verify_port,
  1393. #if defined(CONFIG_CONSOLE_POLL)
  1394. .poll_get_char = imx_poll_get_char,
  1395. .poll_put_char = imx_poll_put_char,
  1396. #endif
  1397. };
  1398. static struct imx_port *imx_ports[UART_NR];
  1399. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  1400. static void imx_console_putchar(struct uart_port *port, int ch)
  1401. {
  1402. struct imx_port *sport = (struct imx_port *)port;
  1403. while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
  1404. barrier();
  1405. writel(ch, sport->port.membase + URTX0);
  1406. }
  1407. /*
  1408. * Interrupts are disabled on entering
  1409. */
  1410. static void
  1411. imx_console_write(struct console *co, const char *s, unsigned int count)
  1412. {
  1413. struct imx_port *sport = imx_ports[co->index];
  1414. struct imx_port_ucrs old_ucr;
  1415. unsigned int ucr1;
  1416. unsigned long flags = 0;
  1417. int locked = 1;
  1418. int retval;
  1419. retval = clk_enable(sport->clk_per);
  1420. if (retval)
  1421. return;
  1422. retval = clk_enable(sport->clk_ipg);
  1423. if (retval) {
  1424. clk_disable(sport->clk_per);
  1425. return;
  1426. }
  1427. if (sport->port.sysrq)
  1428. locked = 0;
  1429. else if (oops_in_progress)
  1430. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1431. else
  1432. spin_lock_irqsave(&sport->port.lock, flags);
  1433. /*
  1434. * First, save UCR1/2/3 and then disable interrupts
  1435. */
  1436. imx_port_ucrs_save(&sport->port, &old_ucr);
  1437. ucr1 = old_ucr.ucr1;
  1438. if (is_imx1_uart(sport))
  1439. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1440. ucr1 |= UCR1_UARTEN;
  1441. ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1442. writel(ucr1, sport->port.membase + UCR1);
  1443. writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
  1444. uart_console_write(&sport->port, s, count, imx_console_putchar);
  1445. /*
  1446. * Finally, wait for transmitter to become empty
  1447. * and restore UCR1/2/3
  1448. */
  1449. while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
  1450. imx_port_ucrs_restore(&sport->port, &old_ucr);
  1451. if (locked)
  1452. spin_unlock_irqrestore(&sport->port.lock, flags);
  1453. clk_disable(sport->clk_ipg);
  1454. clk_disable(sport->clk_per);
  1455. }
  1456. /*
  1457. * If the port was already initialised (eg, by a boot loader),
  1458. * try to determine the current setup.
  1459. */
  1460. static void __init
  1461. imx_console_get_options(struct imx_port *sport, int *baud,
  1462. int *parity, int *bits)
  1463. {
  1464. if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
  1465. /* ok, the port was enabled */
  1466. unsigned int ucr2, ubir, ubmr, uartclk;
  1467. unsigned int baud_raw;
  1468. unsigned int ucfr_rfdiv;
  1469. ucr2 = readl(sport->port.membase + UCR2);
  1470. *parity = 'n';
  1471. if (ucr2 & UCR2_PREN) {
  1472. if (ucr2 & UCR2_PROE)
  1473. *parity = 'o';
  1474. else
  1475. *parity = 'e';
  1476. }
  1477. if (ucr2 & UCR2_WS)
  1478. *bits = 8;
  1479. else
  1480. *bits = 7;
  1481. ubir = readl(sport->port.membase + UBIR) & 0xffff;
  1482. ubmr = readl(sport->port.membase + UBMR) & 0xffff;
  1483. ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
  1484. if (ucfr_rfdiv == 6)
  1485. ucfr_rfdiv = 7;
  1486. else
  1487. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1488. uartclk = clk_get_rate(sport->clk_per);
  1489. uartclk /= ucfr_rfdiv;
  1490. { /*
  1491. * The next code provides exact computation of
  1492. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1493. * without need of float support or long long division,
  1494. * which would be required to prevent 32bit arithmetic overflow
  1495. */
  1496. unsigned int mul = ubir + 1;
  1497. unsigned int div = 16 * (ubmr + 1);
  1498. unsigned int rem = uartclk % div;
  1499. baud_raw = (uartclk / div) * mul;
  1500. baud_raw += (rem * mul + div / 2) / div;
  1501. *baud = (baud_raw + 50) / 100 * 100;
  1502. }
  1503. if (*baud != baud_raw)
  1504. pr_info("Console IMX rounded baud rate from %d to %d\n",
  1505. baud_raw, *baud);
  1506. }
  1507. }
  1508. static int __init
  1509. imx_console_setup(struct console *co, char *options)
  1510. {
  1511. struct imx_port *sport;
  1512. int baud = 9600;
  1513. int bits = 8;
  1514. int parity = 'n';
  1515. int flow = 'n';
  1516. int retval;
  1517. /*
  1518. * Check whether an invalid uart number has been specified, and
  1519. * if so, search for the first available port that does have
  1520. * console support.
  1521. */
  1522. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  1523. co->index = 0;
  1524. sport = imx_ports[co->index];
  1525. if (sport == NULL)
  1526. return -ENODEV;
  1527. /* For setting the registers, we only need to enable the ipg clock. */
  1528. retval = clk_prepare_enable(sport->clk_ipg);
  1529. if (retval)
  1530. goto error_console;
  1531. if (options)
  1532. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1533. else
  1534. imx_console_get_options(sport, &baud, &parity, &bits);
  1535. imx_setup_ufcr(sport, 0);
  1536. retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1537. clk_disable(sport->clk_ipg);
  1538. if (retval) {
  1539. clk_unprepare(sport->clk_ipg);
  1540. goto error_console;
  1541. }
  1542. retval = clk_prepare(sport->clk_per);
  1543. if (retval)
  1544. clk_disable_unprepare(sport->clk_ipg);
  1545. error_console:
  1546. return retval;
  1547. }
  1548. static struct uart_driver imx_reg;
  1549. static struct console imx_console = {
  1550. .name = DEV_NAME,
  1551. .write = imx_console_write,
  1552. .device = uart_console_device,
  1553. .setup = imx_console_setup,
  1554. .flags = CON_PRINTBUFFER,
  1555. .index = -1,
  1556. .data = &imx_reg,
  1557. };
  1558. #define IMX_CONSOLE &imx_console
  1559. #else
  1560. #define IMX_CONSOLE NULL
  1561. #endif
  1562. static struct uart_driver imx_reg = {
  1563. .owner = THIS_MODULE,
  1564. .driver_name = DRIVER_NAME,
  1565. .dev_name = DEV_NAME,
  1566. .major = SERIAL_IMX_MAJOR,
  1567. .minor = MINOR_START,
  1568. .nr = ARRAY_SIZE(imx_ports),
  1569. .cons = IMX_CONSOLE,
  1570. };
  1571. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  1572. {
  1573. struct imx_port *sport = platform_get_drvdata(dev);
  1574. unsigned int val;
  1575. /* enable wakeup from i.MX UART */
  1576. val = readl(sport->port.membase + UCR3);
  1577. val |= UCR3_AWAKEN;
  1578. writel(val, sport->port.membase + UCR3);
  1579. uart_suspend_port(&imx_reg, &sport->port);
  1580. return 0;
  1581. }
  1582. static int serial_imx_resume(struct platform_device *dev)
  1583. {
  1584. struct imx_port *sport = platform_get_drvdata(dev);
  1585. unsigned int val;
  1586. /* disable wakeup from i.MX UART */
  1587. val = readl(sport->port.membase + UCR3);
  1588. val &= ~UCR3_AWAKEN;
  1589. writel(val, sport->port.membase + UCR3);
  1590. uart_resume_port(&imx_reg, &sport->port);
  1591. return 0;
  1592. }
  1593. #ifdef CONFIG_OF
  1594. /*
  1595. * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
  1596. * could successfully get all information from dt or a negative errno.
  1597. */
  1598. static int serial_imx_probe_dt(struct imx_port *sport,
  1599. struct platform_device *pdev)
  1600. {
  1601. struct device_node *np = pdev->dev.of_node;
  1602. const struct of_device_id *of_id =
  1603. of_match_device(imx_uart_dt_ids, &pdev->dev);
  1604. int ret;
  1605. if (!np)
  1606. /* no device tree device */
  1607. return 1;
  1608. ret = of_alias_get_id(np, "serial");
  1609. if (ret < 0) {
  1610. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1611. return ret;
  1612. }
  1613. sport->port.line = ret;
  1614. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  1615. sport->have_rtscts = 1;
  1616. if (of_get_property(np, "fsl,irda-mode", NULL))
  1617. sport->use_irda = 1;
  1618. if (of_get_property(np, "fsl,dte-mode", NULL))
  1619. sport->dte_mode = 1;
  1620. sport->devdata = of_id->data;
  1621. if (of_device_is_stdout_path(np))
  1622. add_preferred_console(imx_reg.cons->name, sport->port.line,
  1623. NULL);
  1624. return 0;
  1625. }
  1626. #else
  1627. static inline int serial_imx_probe_dt(struct imx_port *sport,
  1628. struct platform_device *pdev)
  1629. {
  1630. return 1;
  1631. }
  1632. #endif
  1633. static void serial_imx_probe_pdata(struct imx_port *sport,
  1634. struct platform_device *pdev)
  1635. {
  1636. struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1637. sport->port.line = pdev->id;
  1638. sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
  1639. if (!pdata)
  1640. return;
  1641. if (pdata->flags & IMXUART_HAVE_RTSCTS)
  1642. sport->have_rtscts = 1;
  1643. if (pdata->flags & IMXUART_IRDA)
  1644. sport->use_irda = 1;
  1645. }
  1646. static int serial_imx_probe(struct platform_device *pdev)
  1647. {
  1648. struct imx_port *sport;
  1649. struct imxuart_platform_data *pdata;
  1650. void __iomem *base;
  1651. int ret = 0;
  1652. struct resource *res;
  1653. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1654. if (!sport)
  1655. return -ENOMEM;
  1656. ret = serial_imx_probe_dt(sport, pdev);
  1657. if (ret > 0)
  1658. serial_imx_probe_pdata(sport, pdev);
  1659. else if (ret < 0)
  1660. return ret;
  1661. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1662. if (!res)
  1663. return -ENODEV;
  1664. base = devm_ioremap(&pdev->dev, res->start, PAGE_SIZE);
  1665. if (!base)
  1666. return -ENOMEM;
  1667. sport->port.dev = &pdev->dev;
  1668. sport->port.mapbase = res->start;
  1669. sport->port.membase = base;
  1670. sport->port.type = PORT_IMX,
  1671. sport->port.iotype = UPIO_MEM;
  1672. sport->port.irq = platform_get_irq(pdev, 0);
  1673. sport->rxirq = platform_get_irq(pdev, 0);
  1674. sport->txirq = platform_get_irq(pdev, 1);
  1675. sport->rtsirq = platform_get_irq(pdev, 2);
  1676. sport->port.fifosize = 32;
  1677. sport->port.ops = &imx_pops;
  1678. sport->port.flags = UPF_BOOT_AUTOCONF;
  1679. init_timer(&sport->timer);
  1680. sport->timer.function = imx_timeout;
  1681. sport->timer.data = (unsigned long)sport;
  1682. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1683. if (IS_ERR(sport->clk_ipg)) {
  1684. ret = PTR_ERR(sport->clk_ipg);
  1685. dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
  1686. return ret;
  1687. }
  1688. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  1689. if (IS_ERR(sport->clk_per)) {
  1690. ret = PTR_ERR(sport->clk_per);
  1691. dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
  1692. return ret;
  1693. }
  1694. sport->port.uartclk = clk_get_rate(sport->clk_per);
  1695. imx_ports[sport->port.line] = sport;
  1696. pdata = dev_get_platdata(&pdev->dev);
  1697. if (pdata && pdata->init) {
  1698. ret = pdata->init(pdev);
  1699. if (ret)
  1700. return ret;
  1701. }
  1702. ret = uart_add_one_port(&imx_reg, &sport->port);
  1703. if (ret)
  1704. goto deinit;
  1705. platform_set_drvdata(pdev, sport);
  1706. return 0;
  1707. deinit:
  1708. if (pdata && pdata->exit)
  1709. pdata->exit(pdev);
  1710. return ret;
  1711. }
  1712. static int serial_imx_remove(struct platform_device *pdev)
  1713. {
  1714. struct imxuart_platform_data *pdata;
  1715. struct imx_port *sport = platform_get_drvdata(pdev);
  1716. pdata = dev_get_platdata(&pdev->dev);
  1717. uart_remove_one_port(&imx_reg, &sport->port);
  1718. if (pdata && pdata->exit)
  1719. pdata->exit(pdev);
  1720. return 0;
  1721. }
  1722. static struct platform_driver serial_imx_driver = {
  1723. .probe = serial_imx_probe,
  1724. .remove = serial_imx_remove,
  1725. .suspend = serial_imx_suspend,
  1726. .resume = serial_imx_resume,
  1727. .id_table = imx_uart_devtype,
  1728. .driver = {
  1729. .name = "imx-uart",
  1730. .owner = THIS_MODULE,
  1731. .of_match_table = imx_uart_dt_ids,
  1732. },
  1733. };
  1734. static int __init imx_serial_init(void)
  1735. {
  1736. int ret;
  1737. pr_info("Serial: IMX driver\n");
  1738. ret = uart_register_driver(&imx_reg);
  1739. if (ret)
  1740. return ret;
  1741. ret = platform_driver_register(&serial_imx_driver);
  1742. if (ret != 0)
  1743. uart_unregister_driver(&imx_reg);
  1744. return ret;
  1745. }
  1746. static void __exit imx_serial_exit(void)
  1747. {
  1748. platform_driver_unregister(&serial_imx_driver);
  1749. uart_unregister_driver(&imx_reg);
  1750. }
  1751. module_init(imx_serial_init);
  1752. module_exit(imx_serial_exit);
  1753. MODULE_AUTHOR("Sascha Hauer");
  1754. MODULE_DESCRIPTION("IMX generic serial port driver");
  1755. MODULE_LICENSE("GPL");
  1756. MODULE_ALIAS("platform:imx-uart");