mxs-auart.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Application UART driver for:
  4. * Freescale STMP37XX/STMP378X
  5. * Alphascale ASM9260
  6. *
  7. * Author: dmitry pervushin <[email protected]>
  8. *
  9. * Copyright 2014 Oleksij Rempel <[email protected]>
  10. * Provide Alphascale ASM9260 support.
  11. * Copyright 2008-2010 Freescale Semiconductor, Inc.
  12. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/console.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/wait.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_driver.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/serial.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/device.h>
  29. #include <linux/clk.h>
  30. #include <linux/delay.h>
  31. #include <linux/io.h>
  32. #include <linux/of_device.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/dmaengine.h>
  35. #include <linux/gpio/consumer.h>
  36. #include <linux/err.h>
  37. #include <linux/irq.h>
  38. #include "serial_mctrl_gpio.h"
  39. #define MXS_AUART_PORTS 5
  40. #define MXS_AUART_FIFO_SIZE 16
  41. #define SET_REG 0x4
  42. #define CLR_REG 0x8
  43. #define TOG_REG 0xc
  44. #define AUART_CTRL0 0x00000000
  45. #define AUART_CTRL1 0x00000010
  46. #define AUART_CTRL2 0x00000020
  47. #define AUART_LINECTRL 0x00000030
  48. #define AUART_LINECTRL2 0x00000040
  49. #define AUART_INTR 0x00000050
  50. #define AUART_DATA 0x00000060
  51. #define AUART_STAT 0x00000070
  52. #define AUART_DEBUG 0x00000080
  53. #define AUART_VERSION 0x00000090
  54. #define AUART_AUTOBAUD 0x000000a0
  55. #define AUART_CTRL0_SFTRST (1 << 31)
  56. #define AUART_CTRL0_CLKGATE (1 << 30)
  57. #define AUART_CTRL0_RXTO_ENABLE (1 << 27)
  58. #define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
  59. #define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
  60. #define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
  61. #define AUART_CTRL2_DMAONERR (1 << 26)
  62. #define AUART_CTRL2_TXDMAE (1 << 25)
  63. #define AUART_CTRL2_RXDMAE (1 << 24)
  64. #define AUART_CTRL2_CTSEN (1 << 15)
  65. #define AUART_CTRL2_RTSEN (1 << 14)
  66. #define AUART_CTRL2_RTS (1 << 11)
  67. #define AUART_CTRL2_RXE (1 << 9)
  68. #define AUART_CTRL2_TXE (1 << 8)
  69. #define AUART_CTRL2_UARTEN (1 << 0)
  70. #define AUART_LINECTRL_BAUD_DIV_MAX 0x003fffc0
  71. #define AUART_LINECTRL_BAUD_DIV_MIN 0x000000ec
  72. #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
  73. #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
  74. #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
  75. #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
  76. #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
  77. #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
  78. #define AUART_LINECTRL_SPS (1 << 7)
  79. #define AUART_LINECTRL_WLEN_MASK 0x00000060
  80. #define AUART_LINECTRL_WLEN(v) ((((v) - 5) & 0x3) << 5)
  81. #define AUART_LINECTRL_FEN (1 << 4)
  82. #define AUART_LINECTRL_STP2 (1 << 3)
  83. #define AUART_LINECTRL_EPS (1 << 2)
  84. #define AUART_LINECTRL_PEN (1 << 1)
  85. #define AUART_LINECTRL_BRK (1 << 0)
  86. #define AUART_INTR_RTIEN (1 << 22)
  87. #define AUART_INTR_TXIEN (1 << 21)
  88. #define AUART_INTR_RXIEN (1 << 20)
  89. #define AUART_INTR_CTSMIEN (1 << 17)
  90. #define AUART_INTR_RTIS (1 << 6)
  91. #define AUART_INTR_TXIS (1 << 5)
  92. #define AUART_INTR_RXIS (1 << 4)
  93. #define AUART_INTR_CTSMIS (1 << 1)
  94. #define AUART_STAT_BUSY (1 << 29)
  95. #define AUART_STAT_CTS (1 << 28)
  96. #define AUART_STAT_TXFE (1 << 27)
  97. #define AUART_STAT_TXFF (1 << 25)
  98. #define AUART_STAT_RXFE (1 << 24)
  99. #define AUART_STAT_OERR (1 << 19)
  100. #define AUART_STAT_BERR (1 << 18)
  101. #define AUART_STAT_PERR (1 << 17)
  102. #define AUART_STAT_FERR (1 << 16)
  103. #define AUART_STAT_RXCOUNT_MASK 0xffff
  104. /*
  105. * Start of Alphascale asm9260 defines
  106. * This list contains only differences of existing bits
  107. * between imx2x and asm9260
  108. */
  109. #define ASM9260_HW_CTRL0 0x0000
  110. /*
  111. * RW. Tell the UART to execute the RX DMA Command. The
  112. * UART will clear this bit at the end of receive execution.
  113. */
  114. #define ASM9260_BM_CTRL0_RXDMA_RUN BIT(28)
  115. /* RW. 0 use FIFO for status register; 1 use DMA */
  116. #define ASM9260_BM_CTRL0_RXTO_SOURCE_STATUS BIT(25)
  117. /*
  118. * RW. RX TIMEOUT Enable. Valid for FIFO and DMA.
  119. * Warning: If this bit is set to 0, the RX timeout will not affect receive DMA
  120. * operation. If this bit is set to 1, a receive timeout will cause the receive
  121. * DMA logic to terminate by filling the remaining DMA bytes with garbage data.
  122. */
  123. #define ASM9260_BM_CTRL0_RXTO_ENABLE BIT(24)
  124. /*
  125. * RW. Receive Timeout Counter Value: number of 8-bit-time to wait before
  126. * asserting timeout on the RX input. If the RXFIFO is not empty and the RX
  127. * input is idle, then the watchdog counter will decrement each bit-time. Note
  128. * 7-bit-time is added to the programmed value, so a value of zero will set
  129. * the counter to 7-bit-time, a value of 0x1 gives 15-bit-time and so on. Also
  130. * note that the counter is reloaded at the end of each frame, so if the frame
  131. * is 10 bits long and the timeout counter value is zero, then timeout will
  132. * occur (when FIFO is not empty) even if the RX input is not idle. The default
  133. * value is 0x3 (31 bit-time).
  134. */
  135. #define ASM9260_BM_CTRL0_RXTO_MASK (0xff << 16)
  136. /* TIMEOUT = (100*7+1)*(1/BAUD) */
  137. #define ASM9260_BM_CTRL0_DEFAULT_RXTIMEOUT (20 << 16)
  138. /* TX ctrl register */
  139. #define ASM9260_HW_CTRL1 0x0010
  140. /*
  141. * RW. Tell the UART to execute the TX DMA Command. The
  142. * UART will clear this bit at the end of transmit execution.
  143. */
  144. #define ASM9260_BM_CTRL1_TXDMA_RUN BIT(28)
  145. #define ASM9260_HW_CTRL2 0x0020
  146. /*
  147. * RW. Receive Interrupt FIFO Level Select.
  148. * The trigger points for the receive interrupt are as follows:
  149. * ONE_EIGHTHS = 0x0 Trigger on FIFO full to at least 2 of 16 entries.
  150. * ONE_QUARTER = 0x1 Trigger on FIFO full to at least 4 of 16 entries.
  151. * ONE_HALF = 0x2 Trigger on FIFO full to at least 8 of 16 entries.
  152. * THREE_QUARTERS = 0x3 Trigger on FIFO full to at least 12 of 16 entries.
  153. * SEVEN_EIGHTHS = 0x4 Trigger on FIFO full to at least 14 of 16 entries.
  154. */
  155. #define ASM9260_BM_CTRL2_RXIFLSEL (7 << 20)
  156. #define ASM9260_BM_CTRL2_DEFAULT_RXIFLSEL (3 << 20)
  157. /* RW. Same as RXIFLSEL */
  158. #define ASM9260_BM_CTRL2_TXIFLSEL (7 << 16)
  159. #define ASM9260_BM_CTRL2_DEFAULT_TXIFLSEL (2 << 16)
  160. /* RW. Set DTR. When this bit is 1, the output is 0. */
  161. #define ASM9260_BM_CTRL2_DTR BIT(10)
  162. /* RW. Loop Back Enable */
  163. #define ASM9260_BM_CTRL2_LBE BIT(7)
  164. #define ASM9260_BM_CTRL2_PORT_ENABLE BIT(0)
  165. #define ASM9260_HW_LINECTRL 0x0030
  166. /*
  167. * RW. Stick Parity Select. When bits 1, 2, and 7 of this register are set, the
  168. * parity bit is transmitted and checked as a 0. When bits 1 and 7 are set,
  169. * and bit 2 is 0, the parity bit is transmitted and checked as a 1. When this
  170. * bit is cleared stick parity is disabled.
  171. */
  172. #define ASM9260_BM_LCTRL_SPS BIT(7)
  173. /* RW. Word length */
  174. #define ASM9260_BM_LCTRL_WLEN (3 << 5)
  175. #define ASM9260_BM_LCTRL_CHRL_5 (0 << 5)
  176. #define ASM9260_BM_LCTRL_CHRL_6 (1 << 5)
  177. #define ASM9260_BM_LCTRL_CHRL_7 (2 << 5)
  178. #define ASM9260_BM_LCTRL_CHRL_8 (3 << 5)
  179. /*
  180. * Interrupt register.
  181. * contains the interrupt enables and the interrupt status bits
  182. */
  183. #define ASM9260_HW_INTR 0x0040
  184. /* Tx FIFO EMPTY Raw Interrupt enable */
  185. #define ASM9260_BM_INTR_TFEIEN BIT(27)
  186. /* Overrun Error Interrupt Enable. */
  187. #define ASM9260_BM_INTR_OEIEN BIT(26)
  188. /* Break Error Interrupt Enable. */
  189. #define ASM9260_BM_INTR_BEIEN BIT(25)
  190. /* Parity Error Interrupt Enable. */
  191. #define ASM9260_BM_INTR_PEIEN BIT(24)
  192. /* Framing Error Interrupt Enable. */
  193. #define ASM9260_BM_INTR_FEIEN BIT(23)
  194. /* nUARTDSR Modem Interrupt Enable. */
  195. #define ASM9260_BM_INTR_DSRMIEN BIT(19)
  196. /* nUARTDCD Modem Interrupt Enable. */
  197. #define ASM9260_BM_INTR_DCDMIEN BIT(18)
  198. /* nUARTRI Modem Interrupt Enable. */
  199. #define ASM9260_BM_INTR_RIMIEN BIT(16)
  200. /* Auto-Boud Timeout */
  201. #define ASM9260_BM_INTR_ABTO BIT(13)
  202. #define ASM9260_BM_INTR_ABEO BIT(12)
  203. /* Tx FIFO EMPTY Raw Interrupt state */
  204. #define ASM9260_BM_INTR_TFEIS BIT(11)
  205. /* Overrun Error */
  206. #define ASM9260_BM_INTR_OEIS BIT(10)
  207. /* Break Error */
  208. #define ASM9260_BM_INTR_BEIS BIT(9)
  209. /* Parity Error */
  210. #define ASM9260_BM_INTR_PEIS BIT(8)
  211. /* Framing Error */
  212. #define ASM9260_BM_INTR_FEIS BIT(7)
  213. #define ASM9260_BM_INTR_DSRMIS BIT(3)
  214. #define ASM9260_BM_INTR_DCDMIS BIT(2)
  215. #define ASM9260_BM_INTR_RIMIS BIT(0)
  216. /*
  217. * RW. In DMA mode, up to 4 Received/Transmit characters can be accessed at a
  218. * time. In PIO mode, only one character can be accessed at a time. The status
  219. * register contains the receive data flags and valid bits.
  220. */
  221. #define ASM9260_HW_DATA 0x0050
  222. #define ASM9260_HW_STAT 0x0060
  223. /* RO. If 1, UARTAPP is present in this product. */
  224. #define ASM9260_BM_STAT_PRESENT BIT(31)
  225. /* RO. If 1, HISPEED is present in this product. */
  226. #define ASM9260_BM_STAT_HISPEED BIT(30)
  227. /* RO. Receive FIFO Full. */
  228. #define ASM9260_BM_STAT_RXFULL BIT(26)
  229. /* RO. The UART Debug Register contains the state of the DMA signals. */
  230. #define ASM9260_HW_DEBUG 0x0070
  231. /* DMA Command Run Status */
  232. #define ASM9260_BM_DEBUG_TXDMARUN BIT(5)
  233. #define ASM9260_BM_DEBUG_RXDMARUN BIT(4)
  234. /* DMA Command End Status */
  235. #define ASM9260_BM_DEBUG_TXCMDEND BIT(3)
  236. #define ASM9260_BM_DEBUG_RXCMDEND BIT(2)
  237. /* DMA Request Status */
  238. #define ASM9260_BM_DEBUG_TXDMARQ BIT(1)
  239. #define ASM9260_BM_DEBUG_RXDMARQ BIT(0)
  240. #define ASM9260_HW_ILPR 0x0080
  241. #define ASM9260_HW_RS485CTRL 0x0090
  242. /*
  243. * RW. This bit reverses the polarity of the direction control signal on the RTS
  244. * (or DTR) pin.
  245. * If 0, The direction control pin will be driven to logic ‘0’ when the
  246. * transmitter has data to be sent. It will be driven to logic ‘1’ after the
  247. * last bit of data has been transmitted.
  248. */
  249. #define ASM9260_BM_RS485CTRL_ONIV BIT(5)
  250. /* RW. Enable Auto Direction Control. */
  251. #define ASM9260_BM_RS485CTRL_DIR_CTRL BIT(4)
  252. /*
  253. * RW. If 0 and DIR_CTRL = 1, pin RTS is used for direction control.
  254. * If 1 and DIR_CTRL = 1, pin DTR is used for direction control.
  255. */
  256. #define ASM9260_BM_RS485CTRL_PINSEL BIT(3)
  257. /* RW. Enable Auto Address Detect (AAD). */
  258. #define ASM9260_BM_RS485CTRL_AADEN BIT(2)
  259. /* RW. Disable receiver. */
  260. #define ASM9260_BM_RS485CTRL_RXDIS BIT(1)
  261. /* RW. Enable RS-485/EIA-485 Normal Multidrop Mode (NMM) */
  262. #define ASM9260_BM_RS485CTRL_RS485EN BIT(0)
  263. #define ASM9260_HW_RS485ADRMATCH 0x00a0
  264. /* Contains the address match value. */
  265. #define ASM9260_BM_RS485ADRMATCH_MASK (0xff << 0)
  266. #define ASM9260_HW_RS485DLY 0x00b0
  267. /*
  268. * RW. Contains the direction control (RTS or DTR) delay value. This delay time
  269. * is in periods of the baud clock.
  270. */
  271. #define ASM9260_BM_RS485DLY_MASK (0xff << 0)
  272. #define ASM9260_HW_AUTOBAUD 0x00c0
  273. /* WO. Auto-baud time-out interrupt clear bit. */
  274. #define ASM9260_BM_AUTOBAUD_TO_INT_CLR BIT(9)
  275. /* WO. End of auto-baud interrupt clear bit. */
  276. #define ASM9260_BM_AUTOBAUD_EO_INT_CLR BIT(8)
  277. /* Restart in case of timeout (counter restarts at next UART Rx falling edge) */
  278. #define ASM9260_BM_AUTOBAUD_AUTORESTART BIT(2)
  279. /* Auto-baud mode select bit. 0 - Mode 0, 1 - Mode 1. */
  280. #define ASM9260_BM_AUTOBAUD_MODE BIT(1)
  281. /*
  282. * Auto-baud start (auto-baud is running). Auto-baud run bit. This bit is
  283. * automatically cleared after auto-baud completion.
  284. */
  285. #define ASM9260_BM_AUTOBAUD_START BIT(0)
  286. #define ASM9260_HW_CTRL3 0x00d0
  287. #define ASM9260_BM_CTRL3_OUTCLK_DIV_MASK (0xffff << 16)
  288. /*
  289. * RW. Provide clk over OUTCLK pin. In case of asm9260 it can be configured on
  290. * pins 137 and 144.
  291. */
  292. #define ASM9260_BM_CTRL3_MASTERMODE BIT(6)
  293. /* RW. Baud Rate Mode: 1 - Enable sync mode. 0 - async mode. */
  294. #define ASM9260_BM_CTRL3_SYNCMODE BIT(4)
  295. /* RW. 1 - MSB bit send frist; 0 - LSB bit frist. */
  296. #define ASM9260_BM_CTRL3_MSBF BIT(2)
  297. /* RW. 1 - sample rate = 8 x Baudrate; 0 - sample rate = 16 x Baudrate. */
  298. #define ASM9260_BM_CTRL3_BAUD8 BIT(1)
  299. /* RW. 1 - Set word length to 9bit. 0 - use ASM9260_BM_LCTRL_WLEN */
  300. #define ASM9260_BM_CTRL3_9BIT BIT(0)
  301. #define ASM9260_HW_ISO7816_CTRL 0x00e0
  302. /* RW. Enable High Speed mode. */
  303. #define ASM9260_BM_ISO7816CTRL_HS BIT(12)
  304. /* Disable Successive Receive NACK */
  305. #define ASM9260_BM_ISO7816CTRL_DS_NACK BIT(8)
  306. #define ASM9260_BM_ISO7816CTRL_MAX_ITER_MASK (0xff << 4)
  307. /* Receive NACK Inhibit */
  308. #define ASM9260_BM_ISO7816CTRL_INACK BIT(3)
  309. #define ASM9260_BM_ISO7816CTRL_NEG_DATA BIT(2)
  310. /* RW. 1 - ISO7816 mode; 0 - USART mode */
  311. #define ASM9260_BM_ISO7816CTRL_ENABLE BIT(0)
  312. #define ASM9260_HW_ISO7816_ERRCNT 0x00f0
  313. /* Parity error counter. Will be cleared after reading */
  314. #define ASM9260_BM_ISO7816_NB_ERRORS_MASK (0xff << 0)
  315. #define ASM9260_HW_ISO7816_STATUS 0x0100
  316. /* Max number of Repetitions Reached */
  317. #define ASM9260_BM_ISO7816_STAT_ITERATION BIT(0)
  318. /* End of Alphascale asm9260 defines */
  319. static struct uart_driver auart_driver;
  320. enum mxs_auart_type {
  321. IMX23_AUART,
  322. IMX28_AUART,
  323. ASM9260_AUART,
  324. };
  325. struct vendor_data {
  326. const u16 *reg_offset;
  327. };
  328. enum {
  329. REG_CTRL0,
  330. REG_CTRL1,
  331. REG_CTRL2,
  332. REG_LINECTRL,
  333. REG_LINECTRL2,
  334. REG_INTR,
  335. REG_DATA,
  336. REG_STAT,
  337. REG_DEBUG,
  338. REG_VERSION,
  339. REG_AUTOBAUD,
  340. /* The size of the array - must be last */
  341. REG_ARRAY_SIZE,
  342. };
  343. static const u16 mxs_asm9260_offsets[REG_ARRAY_SIZE] = {
  344. [REG_CTRL0] = ASM9260_HW_CTRL0,
  345. [REG_CTRL1] = ASM9260_HW_CTRL1,
  346. [REG_CTRL2] = ASM9260_HW_CTRL2,
  347. [REG_LINECTRL] = ASM9260_HW_LINECTRL,
  348. [REG_INTR] = ASM9260_HW_INTR,
  349. [REG_DATA] = ASM9260_HW_DATA,
  350. [REG_STAT] = ASM9260_HW_STAT,
  351. [REG_DEBUG] = ASM9260_HW_DEBUG,
  352. [REG_AUTOBAUD] = ASM9260_HW_AUTOBAUD,
  353. };
  354. static const u16 mxs_stmp37xx_offsets[REG_ARRAY_SIZE] = {
  355. [REG_CTRL0] = AUART_CTRL0,
  356. [REG_CTRL1] = AUART_CTRL1,
  357. [REG_CTRL2] = AUART_CTRL2,
  358. [REG_LINECTRL] = AUART_LINECTRL,
  359. [REG_LINECTRL2] = AUART_LINECTRL2,
  360. [REG_INTR] = AUART_INTR,
  361. [REG_DATA] = AUART_DATA,
  362. [REG_STAT] = AUART_STAT,
  363. [REG_DEBUG] = AUART_DEBUG,
  364. [REG_VERSION] = AUART_VERSION,
  365. [REG_AUTOBAUD] = AUART_AUTOBAUD,
  366. };
  367. static const struct vendor_data vendor_alphascale_asm9260 = {
  368. .reg_offset = mxs_asm9260_offsets,
  369. };
  370. static const struct vendor_data vendor_freescale_stmp37xx = {
  371. .reg_offset = mxs_stmp37xx_offsets,
  372. };
  373. struct mxs_auart_port {
  374. struct uart_port port;
  375. #define MXS_AUART_DMA_ENABLED 0x2
  376. #define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
  377. #define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
  378. #define MXS_AUART_RTSCTS 4 /* bit 4 */
  379. unsigned long flags;
  380. unsigned int mctrl_prev;
  381. enum mxs_auart_type devtype;
  382. const struct vendor_data *vendor;
  383. struct clk *clk;
  384. struct clk *clk_ahb;
  385. struct device *dev;
  386. /* for DMA */
  387. struct scatterlist tx_sgl;
  388. struct dma_chan *tx_dma_chan;
  389. void *tx_dma_buf;
  390. struct scatterlist rx_sgl;
  391. struct dma_chan *rx_dma_chan;
  392. void *rx_dma_buf;
  393. struct mctrl_gpios *gpios;
  394. int gpio_irq[UART_GPIO_MAX];
  395. bool ms_irq_enabled;
  396. };
  397. static const struct of_device_id mxs_auart_dt_ids[] = {
  398. {
  399. .compatible = "fsl,imx28-auart",
  400. .data = (const void *)IMX28_AUART
  401. }, {
  402. .compatible = "fsl,imx23-auart",
  403. .data = (const void *)IMX23_AUART
  404. }, {
  405. .compatible = "alphascale,asm9260-auart",
  406. .data = (const void *)ASM9260_AUART
  407. }, { /* sentinel */ }
  408. };
  409. MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
  410. static inline int is_imx28_auart(struct mxs_auart_port *s)
  411. {
  412. return s->devtype == IMX28_AUART;
  413. }
  414. static inline int is_asm9260_auart(struct mxs_auart_port *s)
  415. {
  416. return s->devtype == ASM9260_AUART;
  417. }
  418. static inline bool auart_dma_enabled(struct mxs_auart_port *s)
  419. {
  420. return s->flags & MXS_AUART_DMA_ENABLED;
  421. }
  422. static unsigned int mxs_reg_to_offset(const struct mxs_auart_port *uap,
  423. unsigned int reg)
  424. {
  425. return uap->vendor->reg_offset[reg];
  426. }
  427. static unsigned int mxs_read(const struct mxs_auart_port *uap,
  428. unsigned int reg)
  429. {
  430. void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
  431. return readl_relaxed(addr);
  432. }
  433. static void mxs_write(unsigned int val, struct mxs_auart_port *uap,
  434. unsigned int reg)
  435. {
  436. void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
  437. writel_relaxed(val, addr);
  438. }
  439. static void mxs_set(unsigned int val, struct mxs_auart_port *uap,
  440. unsigned int reg)
  441. {
  442. void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
  443. writel_relaxed(val, addr + SET_REG);
  444. }
  445. static void mxs_clr(unsigned int val, struct mxs_auart_port *uap,
  446. unsigned int reg)
  447. {
  448. void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
  449. writel_relaxed(val, addr + CLR_REG);
  450. }
  451. static void mxs_auart_stop_tx(struct uart_port *u);
  452. #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
  453. static void mxs_auart_tx_chars(struct mxs_auart_port *s);
  454. static void dma_tx_callback(void *param)
  455. {
  456. struct mxs_auart_port *s = param;
  457. struct circ_buf *xmit = &s->port.state->xmit;
  458. dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
  459. /* clear the bit used to serialize the DMA tx. */
  460. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  461. smp_mb__after_atomic();
  462. /* wake up the possible processes. */
  463. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  464. uart_write_wakeup(&s->port);
  465. mxs_auart_tx_chars(s);
  466. }
  467. static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
  468. {
  469. struct dma_async_tx_descriptor *desc;
  470. struct scatterlist *sgl = &s->tx_sgl;
  471. struct dma_chan *channel = s->tx_dma_chan;
  472. u32 pio;
  473. /* [1] : send PIO. Note, the first pio word is CTRL1. */
  474. pio = AUART_CTRL1_XFER_COUNT(size);
  475. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
  476. 1, DMA_TRANS_NONE, 0);
  477. if (!desc) {
  478. dev_err(s->dev, "step 1 error\n");
  479. return -EINVAL;
  480. }
  481. /* [2] : set DMA buffer. */
  482. sg_init_one(sgl, s->tx_dma_buf, size);
  483. dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
  484. desc = dmaengine_prep_slave_sg(channel, sgl,
  485. 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  486. if (!desc) {
  487. dev_err(s->dev, "step 2 error\n");
  488. return -EINVAL;
  489. }
  490. /* [3] : submit the DMA */
  491. desc->callback = dma_tx_callback;
  492. desc->callback_param = s;
  493. dmaengine_submit(desc);
  494. dma_async_issue_pending(channel);
  495. return 0;
  496. }
  497. static void mxs_auart_tx_chars(struct mxs_auart_port *s)
  498. {
  499. struct circ_buf *xmit = &s->port.state->xmit;
  500. if (auart_dma_enabled(s)) {
  501. u32 i = 0;
  502. int size;
  503. void *buffer = s->tx_dma_buf;
  504. if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
  505. return;
  506. while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  507. size = min_t(u32, UART_XMIT_SIZE - i,
  508. CIRC_CNT_TO_END(xmit->head,
  509. xmit->tail,
  510. UART_XMIT_SIZE));
  511. memcpy(buffer + i, xmit->buf + xmit->tail, size);
  512. xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
  513. i += size;
  514. if (i >= UART_XMIT_SIZE)
  515. break;
  516. }
  517. if (uart_tx_stopped(&s->port))
  518. mxs_auart_stop_tx(&s->port);
  519. if (i) {
  520. mxs_auart_dma_tx(s, i);
  521. } else {
  522. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  523. smp_mb__after_atomic();
  524. }
  525. return;
  526. }
  527. while (!(mxs_read(s, REG_STAT) & AUART_STAT_TXFF)) {
  528. if (s->port.x_char) {
  529. s->port.icount.tx++;
  530. mxs_write(s->port.x_char, s, REG_DATA);
  531. s->port.x_char = 0;
  532. continue;
  533. }
  534. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  535. s->port.icount.tx++;
  536. mxs_write(xmit->buf[xmit->tail], s, REG_DATA);
  537. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  538. } else
  539. break;
  540. }
  541. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  542. uart_write_wakeup(&s->port);
  543. if (uart_circ_empty(&(s->port.state->xmit)))
  544. mxs_clr(AUART_INTR_TXIEN, s, REG_INTR);
  545. else
  546. mxs_set(AUART_INTR_TXIEN, s, REG_INTR);
  547. if (uart_tx_stopped(&s->port))
  548. mxs_auart_stop_tx(&s->port);
  549. }
  550. static void mxs_auart_rx_char(struct mxs_auart_port *s)
  551. {
  552. int flag;
  553. u32 stat;
  554. u8 c;
  555. c = mxs_read(s, REG_DATA);
  556. stat = mxs_read(s, REG_STAT);
  557. flag = TTY_NORMAL;
  558. s->port.icount.rx++;
  559. if (stat & AUART_STAT_BERR) {
  560. s->port.icount.brk++;
  561. if (uart_handle_break(&s->port))
  562. goto out;
  563. } else if (stat & AUART_STAT_PERR) {
  564. s->port.icount.parity++;
  565. } else if (stat & AUART_STAT_FERR) {
  566. s->port.icount.frame++;
  567. }
  568. /*
  569. * Mask off conditions which should be ingored.
  570. */
  571. stat &= s->port.read_status_mask;
  572. if (stat & AUART_STAT_BERR) {
  573. flag = TTY_BREAK;
  574. } else if (stat & AUART_STAT_PERR)
  575. flag = TTY_PARITY;
  576. else if (stat & AUART_STAT_FERR)
  577. flag = TTY_FRAME;
  578. if (stat & AUART_STAT_OERR)
  579. s->port.icount.overrun++;
  580. if (uart_handle_sysrq_char(&s->port, c))
  581. goto out;
  582. uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
  583. out:
  584. mxs_write(stat, s, REG_STAT);
  585. }
  586. static void mxs_auart_rx_chars(struct mxs_auart_port *s)
  587. {
  588. u32 stat = 0;
  589. for (;;) {
  590. stat = mxs_read(s, REG_STAT);
  591. if (stat & AUART_STAT_RXFE)
  592. break;
  593. mxs_auart_rx_char(s);
  594. }
  595. mxs_write(stat, s, REG_STAT);
  596. tty_flip_buffer_push(&s->port.state->port);
  597. }
  598. static int mxs_auart_request_port(struct uart_port *u)
  599. {
  600. return 0;
  601. }
  602. static int mxs_auart_verify_port(struct uart_port *u,
  603. struct serial_struct *ser)
  604. {
  605. if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
  606. return -EINVAL;
  607. return 0;
  608. }
  609. static void mxs_auart_config_port(struct uart_port *u, int flags)
  610. {
  611. }
  612. static const char *mxs_auart_type(struct uart_port *u)
  613. {
  614. struct mxs_auart_port *s = to_auart_port(u);
  615. return dev_name(s->dev);
  616. }
  617. static void mxs_auart_release_port(struct uart_port *u)
  618. {
  619. }
  620. static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
  621. {
  622. struct mxs_auart_port *s = to_auart_port(u);
  623. u32 ctrl = mxs_read(s, REG_CTRL2);
  624. ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
  625. if (mctrl & TIOCM_RTS) {
  626. if (uart_cts_enabled(u))
  627. ctrl |= AUART_CTRL2_RTSEN;
  628. else
  629. ctrl |= AUART_CTRL2_RTS;
  630. }
  631. mxs_write(ctrl, s, REG_CTRL2);
  632. mctrl_gpio_set(s->gpios, mctrl);
  633. }
  634. #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
  635. static u32 mxs_auart_modem_status(struct mxs_auart_port *s, u32 mctrl)
  636. {
  637. u32 mctrl_diff;
  638. mctrl_diff = mctrl ^ s->mctrl_prev;
  639. s->mctrl_prev = mctrl;
  640. if (mctrl_diff & MCTRL_ANY_DELTA && s->ms_irq_enabled &&
  641. s->port.state != NULL) {
  642. if (mctrl_diff & TIOCM_RI)
  643. s->port.icount.rng++;
  644. if (mctrl_diff & TIOCM_DSR)
  645. s->port.icount.dsr++;
  646. if (mctrl_diff & TIOCM_CD)
  647. uart_handle_dcd_change(&s->port, mctrl & TIOCM_CD);
  648. if (mctrl_diff & TIOCM_CTS)
  649. uart_handle_cts_change(&s->port, mctrl & TIOCM_CTS);
  650. wake_up_interruptible(&s->port.state->port.delta_msr_wait);
  651. }
  652. return mctrl;
  653. }
  654. static u32 mxs_auart_get_mctrl(struct uart_port *u)
  655. {
  656. struct mxs_auart_port *s = to_auart_port(u);
  657. u32 stat = mxs_read(s, REG_STAT);
  658. u32 mctrl = 0;
  659. if (stat & AUART_STAT_CTS)
  660. mctrl |= TIOCM_CTS;
  661. return mctrl_gpio_get(s->gpios, &mctrl);
  662. }
  663. /*
  664. * Enable modem status interrupts
  665. */
  666. static void mxs_auart_enable_ms(struct uart_port *port)
  667. {
  668. struct mxs_auart_port *s = to_auart_port(port);
  669. /*
  670. * Interrupt should not be enabled twice
  671. */
  672. if (s->ms_irq_enabled)
  673. return;
  674. s->ms_irq_enabled = true;
  675. if (s->gpio_irq[UART_GPIO_CTS] >= 0)
  676. enable_irq(s->gpio_irq[UART_GPIO_CTS]);
  677. /* TODO: enable AUART_INTR_CTSMIEN otherwise */
  678. if (s->gpio_irq[UART_GPIO_DSR] >= 0)
  679. enable_irq(s->gpio_irq[UART_GPIO_DSR]);
  680. if (s->gpio_irq[UART_GPIO_RI] >= 0)
  681. enable_irq(s->gpio_irq[UART_GPIO_RI]);
  682. if (s->gpio_irq[UART_GPIO_DCD] >= 0)
  683. enable_irq(s->gpio_irq[UART_GPIO_DCD]);
  684. }
  685. /*
  686. * Disable modem status interrupts
  687. */
  688. static void mxs_auart_disable_ms(struct uart_port *port)
  689. {
  690. struct mxs_auart_port *s = to_auart_port(port);
  691. /*
  692. * Interrupt should not be disabled twice
  693. */
  694. if (!s->ms_irq_enabled)
  695. return;
  696. s->ms_irq_enabled = false;
  697. if (s->gpio_irq[UART_GPIO_CTS] >= 0)
  698. disable_irq(s->gpio_irq[UART_GPIO_CTS]);
  699. /* TODO: disable AUART_INTR_CTSMIEN otherwise */
  700. if (s->gpio_irq[UART_GPIO_DSR] >= 0)
  701. disable_irq(s->gpio_irq[UART_GPIO_DSR]);
  702. if (s->gpio_irq[UART_GPIO_RI] >= 0)
  703. disable_irq(s->gpio_irq[UART_GPIO_RI]);
  704. if (s->gpio_irq[UART_GPIO_DCD] >= 0)
  705. disable_irq(s->gpio_irq[UART_GPIO_DCD]);
  706. }
  707. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
  708. static void dma_rx_callback(void *arg)
  709. {
  710. struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
  711. struct tty_port *port = &s->port.state->port;
  712. int count;
  713. u32 stat;
  714. dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
  715. stat = mxs_read(s, REG_STAT);
  716. stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
  717. AUART_STAT_PERR | AUART_STAT_FERR);
  718. count = stat & AUART_STAT_RXCOUNT_MASK;
  719. tty_insert_flip_string(port, s->rx_dma_buf, count);
  720. mxs_write(stat, s, REG_STAT);
  721. tty_flip_buffer_push(port);
  722. /* start the next DMA for RX. */
  723. mxs_auart_dma_prep_rx(s);
  724. }
  725. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
  726. {
  727. struct dma_async_tx_descriptor *desc;
  728. struct scatterlist *sgl = &s->rx_sgl;
  729. struct dma_chan *channel = s->rx_dma_chan;
  730. u32 pio[1];
  731. /* [1] : send PIO */
  732. pio[0] = AUART_CTRL0_RXTO_ENABLE
  733. | AUART_CTRL0_RXTIMEOUT(0x80)
  734. | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
  735. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  736. 1, DMA_TRANS_NONE, 0);
  737. if (!desc) {
  738. dev_err(s->dev, "step 1 error\n");
  739. return -EINVAL;
  740. }
  741. /* [2] : send DMA request */
  742. sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
  743. dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
  744. desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
  745. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  746. if (!desc) {
  747. dev_err(s->dev, "step 2 error\n");
  748. return -1;
  749. }
  750. /* [3] : submit the DMA, but do not issue it. */
  751. desc->callback = dma_rx_callback;
  752. desc->callback_param = s;
  753. dmaengine_submit(desc);
  754. dma_async_issue_pending(channel);
  755. return 0;
  756. }
  757. static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
  758. {
  759. if (s->tx_dma_chan) {
  760. dma_release_channel(s->tx_dma_chan);
  761. s->tx_dma_chan = NULL;
  762. }
  763. if (s->rx_dma_chan) {
  764. dma_release_channel(s->rx_dma_chan);
  765. s->rx_dma_chan = NULL;
  766. }
  767. kfree(s->tx_dma_buf);
  768. kfree(s->rx_dma_buf);
  769. s->tx_dma_buf = NULL;
  770. s->rx_dma_buf = NULL;
  771. }
  772. static void mxs_auart_dma_exit(struct mxs_auart_port *s)
  773. {
  774. mxs_clr(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
  775. s, REG_CTRL2);
  776. mxs_auart_dma_exit_channel(s);
  777. s->flags &= ~MXS_AUART_DMA_ENABLED;
  778. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  779. clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
  780. }
  781. static int mxs_auart_dma_init(struct mxs_auart_port *s)
  782. {
  783. if (auart_dma_enabled(s))
  784. return 0;
  785. /* init for RX */
  786. s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
  787. if (!s->rx_dma_chan)
  788. goto err_out;
  789. s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  790. if (!s->rx_dma_buf)
  791. goto err_out;
  792. /* init for TX */
  793. s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
  794. if (!s->tx_dma_chan)
  795. goto err_out;
  796. s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  797. if (!s->tx_dma_buf)
  798. goto err_out;
  799. /* set the flags */
  800. s->flags |= MXS_AUART_DMA_ENABLED;
  801. dev_dbg(s->dev, "enabled the DMA support.");
  802. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  803. s->port.fifosize = UART_XMIT_SIZE;
  804. return 0;
  805. err_out:
  806. mxs_auart_dma_exit_channel(s);
  807. return -EINVAL;
  808. }
  809. #define RTS_AT_AUART() !mctrl_gpio_to_gpiod(s->gpios, UART_GPIO_RTS)
  810. #define CTS_AT_AUART() !mctrl_gpio_to_gpiod(s->gpios, UART_GPIO_CTS)
  811. static void mxs_auart_settermios(struct uart_port *u,
  812. struct ktermios *termios,
  813. const struct ktermios *old)
  814. {
  815. struct mxs_auart_port *s = to_auart_port(u);
  816. u32 ctrl, ctrl2, div;
  817. unsigned int cflag, baud, baud_min, baud_max;
  818. cflag = termios->c_cflag;
  819. ctrl = AUART_LINECTRL_FEN;
  820. ctrl2 = mxs_read(s, REG_CTRL2);
  821. ctrl |= AUART_LINECTRL_WLEN(tty_get_char_size(cflag));
  822. /* parity */
  823. if (cflag & PARENB) {
  824. ctrl |= AUART_LINECTRL_PEN;
  825. if ((cflag & PARODD) == 0)
  826. ctrl |= AUART_LINECTRL_EPS;
  827. if (cflag & CMSPAR)
  828. ctrl |= AUART_LINECTRL_SPS;
  829. }
  830. u->read_status_mask = AUART_STAT_OERR;
  831. if (termios->c_iflag & INPCK)
  832. u->read_status_mask |= AUART_STAT_PERR;
  833. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  834. u->read_status_mask |= AUART_STAT_BERR;
  835. /*
  836. * Characters to ignore
  837. */
  838. u->ignore_status_mask = 0;
  839. if (termios->c_iflag & IGNPAR)
  840. u->ignore_status_mask |= AUART_STAT_PERR;
  841. if (termios->c_iflag & IGNBRK) {
  842. u->ignore_status_mask |= AUART_STAT_BERR;
  843. /*
  844. * If we're ignoring parity and break indicators,
  845. * ignore overruns too (for real raw support).
  846. */
  847. if (termios->c_iflag & IGNPAR)
  848. u->ignore_status_mask |= AUART_STAT_OERR;
  849. }
  850. /*
  851. * ignore all characters if CREAD is not set
  852. */
  853. if (cflag & CREAD)
  854. ctrl2 |= AUART_CTRL2_RXE;
  855. else
  856. ctrl2 &= ~AUART_CTRL2_RXE;
  857. /* figure out the stop bits requested */
  858. if (cflag & CSTOPB)
  859. ctrl |= AUART_LINECTRL_STP2;
  860. /* figure out the hardware flow control settings */
  861. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  862. if (cflag & CRTSCTS) {
  863. /*
  864. * The DMA has a bug(see errata:2836) in mx23.
  865. * So we can not implement the DMA for auart in mx23,
  866. * we can only implement the DMA support for auart
  867. * in mx28.
  868. */
  869. if (is_imx28_auart(s)
  870. && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
  871. if (!mxs_auart_dma_init(s))
  872. /* enable DMA tranfer */
  873. ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
  874. | AUART_CTRL2_DMAONERR;
  875. }
  876. /* Even if RTS is GPIO line RTSEN can be enabled because
  877. * the pinctrl configuration decides about RTS pin function */
  878. ctrl2 |= AUART_CTRL2_RTSEN;
  879. if (CTS_AT_AUART())
  880. ctrl2 |= AUART_CTRL2_CTSEN;
  881. }
  882. /* set baud rate */
  883. if (is_asm9260_auart(s)) {
  884. baud = uart_get_baud_rate(u, termios, old,
  885. u->uartclk * 4 / 0x3FFFFF,
  886. u->uartclk / 16);
  887. div = u->uartclk * 4 / baud;
  888. } else {
  889. baud_min = DIV_ROUND_UP(u->uartclk * 32,
  890. AUART_LINECTRL_BAUD_DIV_MAX);
  891. baud_max = u->uartclk * 32 / AUART_LINECTRL_BAUD_DIV_MIN;
  892. baud = uart_get_baud_rate(u, termios, old, baud_min, baud_max);
  893. div = DIV_ROUND_CLOSEST(u->uartclk * 32, baud);
  894. }
  895. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  896. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  897. mxs_write(ctrl, s, REG_LINECTRL);
  898. mxs_write(ctrl2, s, REG_CTRL2);
  899. uart_update_timeout(u, termios->c_cflag, baud);
  900. /* prepare for the DMA RX. */
  901. if (auart_dma_enabled(s) &&
  902. !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
  903. if (!mxs_auart_dma_prep_rx(s)) {
  904. /* Disable the normal RX interrupt. */
  905. mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
  906. s, REG_INTR);
  907. } else {
  908. mxs_auart_dma_exit(s);
  909. dev_err(s->dev, "We can not start up the DMA.\n");
  910. }
  911. }
  912. /* CTS flow-control and modem-status interrupts */
  913. if (UART_ENABLE_MS(u, termios->c_cflag))
  914. mxs_auart_enable_ms(u);
  915. else
  916. mxs_auart_disable_ms(u);
  917. }
  918. static void mxs_auart_set_ldisc(struct uart_port *port,
  919. struct ktermios *termios)
  920. {
  921. if (termios->c_line == N_PPS) {
  922. port->flags |= UPF_HARDPPS_CD;
  923. mxs_auart_enable_ms(port);
  924. } else {
  925. port->flags &= ~UPF_HARDPPS_CD;
  926. }
  927. }
  928. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  929. {
  930. u32 istat;
  931. struct mxs_auart_port *s = context;
  932. u32 mctrl_temp = s->mctrl_prev;
  933. u32 stat = mxs_read(s, REG_STAT);
  934. istat = mxs_read(s, REG_INTR);
  935. /* ack irq */
  936. mxs_clr(istat & (AUART_INTR_RTIS | AUART_INTR_TXIS | AUART_INTR_RXIS
  937. | AUART_INTR_CTSMIS), s, REG_INTR);
  938. /*
  939. * Dealing with GPIO interrupt
  940. */
  941. if (irq == s->gpio_irq[UART_GPIO_CTS] ||
  942. irq == s->gpio_irq[UART_GPIO_DCD] ||
  943. irq == s->gpio_irq[UART_GPIO_DSR] ||
  944. irq == s->gpio_irq[UART_GPIO_RI])
  945. mxs_auart_modem_status(s,
  946. mctrl_gpio_get(s->gpios, &mctrl_temp));
  947. if (istat & AUART_INTR_CTSMIS) {
  948. if (CTS_AT_AUART() && s->ms_irq_enabled)
  949. uart_handle_cts_change(&s->port,
  950. stat & AUART_STAT_CTS);
  951. mxs_clr(AUART_INTR_CTSMIS, s, REG_INTR);
  952. istat &= ~AUART_INTR_CTSMIS;
  953. }
  954. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  955. if (!auart_dma_enabled(s))
  956. mxs_auart_rx_chars(s);
  957. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  958. }
  959. if (istat & AUART_INTR_TXIS) {
  960. mxs_auart_tx_chars(s);
  961. istat &= ~AUART_INTR_TXIS;
  962. }
  963. return IRQ_HANDLED;
  964. }
  965. static void mxs_auart_reset_deassert(struct mxs_auart_port *s)
  966. {
  967. int i;
  968. unsigned int reg;
  969. mxs_clr(AUART_CTRL0_SFTRST, s, REG_CTRL0);
  970. for (i = 0; i < 10000; i++) {
  971. reg = mxs_read(s, REG_CTRL0);
  972. if (!(reg & AUART_CTRL0_SFTRST))
  973. break;
  974. udelay(3);
  975. }
  976. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  977. }
  978. static void mxs_auart_reset_assert(struct mxs_auart_port *s)
  979. {
  980. int i;
  981. u32 reg;
  982. reg = mxs_read(s, REG_CTRL0);
  983. /* if already in reset state, keep it untouched */
  984. if (reg & AUART_CTRL0_SFTRST)
  985. return;
  986. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  987. mxs_set(AUART_CTRL0_SFTRST, s, REG_CTRL0);
  988. for (i = 0; i < 1000; i++) {
  989. reg = mxs_read(s, REG_CTRL0);
  990. /* reset is finished when the clock is gated */
  991. if (reg & AUART_CTRL0_CLKGATE)
  992. return;
  993. udelay(10);
  994. }
  995. dev_err(s->dev, "Failed to reset the unit.");
  996. }
  997. static int mxs_auart_startup(struct uart_port *u)
  998. {
  999. int ret;
  1000. struct mxs_auart_port *s = to_auart_port(u);
  1001. ret = clk_prepare_enable(s->clk);
  1002. if (ret)
  1003. return ret;
  1004. if (uart_console(u)) {
  1005. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1006. } else {
  1007. /* reset the unit to a well known state */
  1008. mxs_auart_reset_assert(s);
  1009. mxs_auart_reset_deassert(s);
  1010. }
  1011. mxs_set(AUART_CTRL2_UARTEN, s, REG_CTRL2);
  1012. mxs_write(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  1013. s, REG_INTR);
  1014. /* Reset FIFO size (it could have changed if DMA was enabled) */
  1015. u->fifosize = MXS_AUART_FIFO_SIZE;
  1016. /*
  1017. * Enable fifo so all four bytes of a DMA word are written to
  1018. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  1019. */
  1020. mxs_set(AUART_LINECTRL_FEN, s, REG_LINECTRL);
  1021. /* get initial status of modem lines */
  1022. mctrl_gpio_get(s->gpios, &s->mctrl_prev);
  1023. s->ms_irq_enabled = false;
  1024. return 0;
  1025. }
  1026. static void mxs_auart_shutdown(struct uart_port *u)
  1027. {
  1028. struct mxs_auart_port *s = to_auart_port(u);
  1029. mxs_auart_disable_ms(u);
  1030. if (auart_dma_enabled(s))
  1031. mxs_auart_dma_exit(s);
  1032. if (uart_console(u)) {
  1033. mxs_clr(AUART_CTRL2_UARTEN, s, REG_CTRL2);
  1034. mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN |
  1035. AUART_INTR_CTSMIEN, s, REG_INTR);
  1036. mxs_set(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1037. } else {
  1038. mxs_auart_reset_assert(s);
  1039. }
  1040. clk_disable_unprepare(s->clk);
  1041. }
  1042. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  1043. {
  1044. struct mxs_auart_port *s = to_auart_port(u);
  1045. if ((mxs_read(s, REG_STAT) &
  1046. (AUART_STAT_TXFE | AUART_STAT_BUSY)) == AUART_STAT_TXFE)
  1047. return TIOCSER_TEMT;
  1048. return 0;
  1049. }
  1050. static void mxs_auart_start_tx(struct uart_port *u)
  1051. {
  1052. struct mxs_auart_port *s = to_auart_port(u);
  1053. /* enable transmitter */
  1054. mxs_set(AUART_CTRL2_TXE, s, REG_CTRL2);
  1055. mxs_auart_tx_chars(s);
  1056. }
  1057. static void mxs_auart_stop_tx(struct uart_port *u)
  1058. {
  1059. struct mxs_auart_port *s = to_auart_port(u);
  1060. mxs_clr(AUART_CTRL2_TXE, s, REG_CTRL2);
  1061. }
  1062. static void mxs_auart_stop_rx(struct uart_port *u)
  1063. {
  1064. struct mxs_auart_port *s = to_auart_port(u);
  1065. mxs_clr(AUART_CTRL2_RXE, s, REG_CTRL2);
  1066. }
  1067. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  1068. {
  1069. struct mxs_auart_port *s = to_auart_port(u);
  1070. if (ctl)
  1071. mxs_set(AUART_LINECTRL_BRK, s, REG_LINECTRL);
  1072. else
  1073. mxs_clr(AUART_LINECTRL_BRK, s, REG_LINECTRL);
  1074. }
  1075. static const struct uart_ops mxs_auart_ops = {
  1076. .tx_empty = mxs_auart_tx_empty,
  1077. .start_tx = mxs_auart_start_tx,
  1078. .stop_tx = mxs_auart_stop_tx,
  1079. .stop_rx = mxs_auart_stop_rx,
  1080. .enable_ms = mxs_auart_enable_ms,
  1081. .break_ctl = mxs_auart_break_ctl,
  1082. .set_mctrl = mxs_auart_set_mctrl,
  1083. .get_mctrl = mxs_auart_get_mctrl,
  1084. .startup = mxs_auart_startup,
  1085. .shutdown = mxs_auart_shutdown,
  1086. .set_termios = mxs_auart_settermios,
  1087. .set_ldisc = mxs_auart_set_ldisc,
  1088. .type = mxs_auart_type,
  1089. .release_port = mxs_auart_release_port,
  1090. .request_port = mxs_auart_request_port,
  1091. .config_port = mxs_auart_config_port,
  1092. .verify_port = mxs_auart_verify_port,
  1093. };
  1094. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  1095. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  1096. static void mxs_auart_console_putchar(struct uart_port *port, unsigned char ch)
  1097. {
  1098. struct mxs_auart_port *s = to_auart_port(port);
  1099. unsigned int to = 1000;
  1100. while (mxs_read(s, REG_STAT) & AUART_STAT_TXFF) {
  1101. if (!to--)
  1102. break;
  1103. udelay(1);
  1104. }
  1105. mxs_write(ch, s, REG_DATA);
  1106. }
  1107. static void
  1108. auart_console_write(struct console *co, const char *str, unsigned int count)
  1109. {
  1110. struct mxs_auart_port *s;
  1111. struct uart_port *port;
  1112. unsigned int old_ctrl0, old_ctrl2;
  1113. unsigned int to = 20000;
  1114. if (co->index >= MXS_AUART_PORTS || co->index < 0)
  1115. return;
  1116. s = auart_port[co->index];
  1117. port = &s->port;
  1118. clk_enable(s->clk);
  1119. /* First save the CR then disable the interrupts */
  1120. old_ctrl2 = mxs_read(s, REG_CTRL2);
  1121. old_ctrl0 = mxs_read(s, REG_CTRL0);
  1122. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1123. mxs_set(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE, s, REG_CTRL2);
  1124. uart_console_write(port, str, count, mxs_auart_console_putchar);
  1125. /* Finally, wait for transmitter to become empty ... */
  1126. while (mxs_read(s, REG_STAT) & AUART_STAT_BUSY) {
  1127. udelay(1);
  1128. if (!to--)
  1129. break;
  1130. }
  1131. /*
  1132. * ... and restore the TCR if we waited long enough for the transmitter
  1133. * to be idle. This might keep the transmitter enabled although it is
  1134. * unused, but that is better than to disable it while it is still
  1135. * transmitting.
  1136. */
  1137. if (!(mxs_read(s, REG_STAT) & AUART_STAT_BUSY)) {
  1138. mxs_write(old_ctrl0, s, REG_CTRL0);
  1139. mxs_write(old_ctrl2, s, REG_CTRL2);
  1140. }
  1141. clk_disable(s->clk);
  1142. }
  1143. static void __init
  1144. auart_console_get_options(struct mxs_auart_port *s, int *baud,
  1145. int *parity, int *bits)
  1146. {
  1147. struct uart_port *port = &s->port;
  1148. unsigned int lcr_h, quot;
  1149. if (!(mxs_read(s, REG_CTRL2) & AUART_CTRL2_UARTEN))
  1150. return;
  1151. lcr_h = mxs_read(s, REG_LINECTRL);
  1152. *parity = 'n';
  1153. if (lcr_h & AUART_LINECTRL_PEN) {
  1154. if (lcr_h & AUART_LINECTRL_EPS)
  1155. *parity = 'e';
  1156. else
  1157. *parity = 'o';
  1158. }
  1159. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(7))
  1160. *bits = 7;
  1161. else
  1162. *bits = 8;
  1163. quot = ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVINT_MASK))
  1164. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  1165. quot |= ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  1166. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  1167. if (quot == 0)
  1168. quot = 1;
  1169. *baud = (port->uartclk << 2) / quot;
  1170. }
  1171. static int __init
  1172. auart_console_setup(struct console *co, char *options)
  1173. {
  1174. struct mxs_auart_port *s;
  1175. int baud = 9600;
  1176. int bits = 8;
  1177. int parity = 'n';
  1178. int flow = 'n';
  1179. int ret;
  1180. /*
  1181. * Check whether an invalid uart number has been specified, and
  1182. * if so, search for the first available port that does have
  1183. * console support.
  1184. */
  1185. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  1186. co->index = 0;
  1187. s = auart_port[co->index];
  1188. if (!s)
  1189. return -ENODEV;
  1190. ret = clk_prepare_enable(s->clk);
  1191. if (ret)
  1192. return ret;
  1193. if (options)
  1194. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1195. else
  1196. auart_console_get_options(s, &baud, &parity, &bits);
  1197. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  1198. clk_disable_unprepare(s->clk);
  1199. return ret;
  1200. }
  1201. static struct console auart_console = {
  1202. .name = "ttyAPP",
  1203. .write = auart_console_write,
  1204. .device = uart_console_device,
  1205. .setup = auart_console_setup,
  1206. .flags = CON_PRINTBUFFER,
  1207. .index = -1,
  1208. .data = &auart_driver,
  1209. };
  1210. #endif
  1211. static struct uart_driver auart_driver = {
  1212. .owner = THIS_MODULE,
  1213. .driver_name = "ttyAPP",
  1214. .dev_name = "ttyAPP",
  1215. .major = 0,
  1216. .minor = 0,
  1217. .nr = MXS_AUART_PORTS,
  1218. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  1219. .cons = &auart_console,
  1220. #endif
  1221. };
  1222. static void mxs_init_regs(struct mxs_auart_port *s)
  1223. {
  1224. if (is_asm9260_auart(s))
  1225. s->vendor = &vendor_alphascale_asm9260;
  1226. else
  1227. s->vendor = &vendor_freescale_stmp37xx;
  1228. }
  1229. static int mxs_get_clks(struct mxs_auart_port *s,
  1230. struct platform_device *pdev)
  1231. {
  1232. int err;
  1233. if (!is_asm9260_auart(s)) {
  1234. s->clk = devm_clk_get(&pdev->dev, NULL);
  1235. return PTR_ERR_OR_ZERO(s->clk);
  1236. }
  1237. s->clk = devm_clk_get(s->dev, "mod");
  1238. if (IS_ERR(s->clk)) {
  1239. dev_err(s->dev, "Failed to get \"mod\" clk\n");
  1240. return PTR_ERR(s->clk);
  1241. }
  1242. s->clk_ahb = devm_clk_get(s->dev, "ahb");
  1243. if (IS_ERR(s->clk_ahb)) {
  1244. dev_err(s->dev, "Failed to get \"ahb\" clk\n");
  1245. return PTR_ERR(s->clk_ahb);
  1246. }
  1247. err = clk_prepare_enable(s->clk_ahb);
  1248. if (err) {
  1249. dev_err(s->dev, "Failed to enable ahb_clk!\n");
  1250. return err;
  1251. }
  1252. err = clk_set_rate(s->clk, clk_get_rate(s->clk_ahb));
  1253. if (err) {
  1254. dev_err(s->dev, "Failed to set rate!\n");
  1255. goto disable_clk_ahb;
  1256. }
  1257. err = clk_prepare_enable(s->clk);
  1258. if (err) {
  1259. dev_err(s->dev, "Failed to enable clk!\n");
  1260. goto disable_clk_ahb;
  1261. }
  1262. return 0;
  1263. disable_clk_ahb:
  1264. clk_disable_unprepare(s->clk_ahb);
  1265. return err;
  1266. }
  1267. static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
  1268. {
  1269. enum mctrl_gpio_idx i;
  1270. struct gpio_desc *gpiod;
  1271. s->gpios = mctrl_gpio_init_noauto(dev, 0);
  1272. if (IS_ERR(s->gpios))
  1273. return PTR_ERR(s->gpios);
  1274. /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
  1275. if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
  1276. if (test_bit(MXS_AUART_RTSCTS, &s->flags))
  1277. dev_warn(dev,
  1278. "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
  1279. clear_bit(MXS_AUART_RTSCTS, &s->flags);
  1280. }
  1281. for (i = 0; i < UART_GPIO_MAX; i++) {
  1282. gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
  1283. if (gpiod && (gpiod_get_direction(gpiod) == 1))
  1284. s->gpio_irq[i] = gpiod_to_irq(gpiod);
  1285. else
  1286. s->gpio_irq[i] = -EINVAL;
  1287. }
  1288. return 0;
  1289. }
  1290. static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
  1291. {
  1292. enum mctrl_gpio_idx i;
  1293. for (i = 0; i < UART_GPIO_MAX; i++)
  1294. if (s->gpio_irq[i] >= 0)
  1295. free_irq(s->gpio_irq[i], s);
  1296. }
  1297. static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
  1298. {
  1299. int *irq = s->gpio_irq;
  1300. enum mctrl_gpio_idx i;
  1301. int err = 0;
  1302. for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
  1303. if (irq[i] < 0)
  1304. continue;
  1305. irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
  1306. err = request_irq(irq[i], mxs_auart_irq_handle,
  1307. IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
  1308. if (err)
  1309. dev_err(s->dev, "%s - Can't get %d irq\n",
  1310. __func__, irq[i]);
  1311. }
  1312. /*
  1313. * If something went wrong, rollback.
  1314. * Be careful: i may be unsigned.
  1315. */
  1316. while (err && (i-- > 0))
  1317. if (irq[i] >= 0)
  1318. free_irq(irq[i], s);
  1319. return err;
  1320. }
  1321. static int mxs_auart_probe(struct platform_device *pdev)
  1322. {
  1323. struct device_node *np = pdev->dev.of_node;
  1324. struct mxs_auart_port *s;
  1325. u32 version;
  1326. int ret, irq;
  1327. struct resource *r;
  1328. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  1329. if (!s)
  1330. return -ENOMEM;
  1331. s->port.dev = &pdev->dev;
  1332. s->dev = &pdev->dev;
  1333. ret = of_alias_get_id(np, "serial");
  1334. if (ret < 0) {
  1335. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  1336. return ret;
  1337. }
  1338. s->port.line = ret;
  1339. if (of_get_property(np, "uart-has-rtscts", NULL) ||
  1340. of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
  1341. set_bit(MXS_AUART_RTSCTS, &s->flags);
  1342. if (s->port.line >= ARRAY_SIZE(auart_port)) {
  1343. dev_err(&pdev->dev, "serial%d out of range\n", s->port.line);
  1344. return -EINVAL;
  1345. }
  1346. s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
  1347. ret = mxs_get_clks(s, pdev);
  1348. if (ret)
  1349. return ret;
  1350. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1351. if (!r) {
  1352. ret = -ENXIO;
  1353. goto out_disable_clks;
  1354. }
  1355. s->port.mapbase = r->start;
  1356. s->port.membase = ioremap(r->start, resource_size(r));
  1357. if (!s->port.membase) {
  1358. ret = -ENOMEM;
  1359. goto out_disable_clks;
  1360. }
  1361. s->port.ops = &mxs_auart_ops;
  1362. s->port.iotype = UPIO_MEM;
  1363. s->port.fifosize = MXS_AUART_FIFO_SIZE;
  1364. s->port.uartclk = clk_get_rate(s->clk);
  1365. s->port.type = PORT_IMX;
  1366. s->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_MXS_AUART_CONSOLE);
  1367. mxs_init_regs(s);
  1368. s->mctrl_prev = 0;
  1369. irq = platform_get_irq(pdev, 0);
  1370. if (irq < 0) {
  1371. ret = irq;
  1372. goto out_iounmap;
  1373. }
  1374. s->port.irq = irq;
  1375. ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
  1376. dev_name(&pdev->dev), s);
  1377. if (ret)
  1378. goto out_iounmap;
  1379. platform_set_drvdata(pdev, s);
  1380. ret = mxs_auart_init_gpios(s, &pdev->dev);
  1381. if (ret) {
  1382. dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
  1383. goto out_iounmap;
  1384. }
  1385. /*
  1386. * Get the GPIO lines IRQ
  1387. */
  1388. ret = mxs_auart_request_gpio_irq(s);
  1389. if (ret)
  1390. goto out_iounmap;
  1391. auart_port[s->port.line] = s;
  1392. mxs_auart_reset_deassert(s);
  1393. ret = uart_add_one_port(&auart_driver, &s->port);
  1394. if (ret)
  1395. goto out_free_qpio_irq;
  1396. /* ASM9260 don't have version reg */
  1397. if (is_asm9260_auart(s)) {
  1398. dev_info(&pdev->dev, "Found APPUART ASM9260\n");
  1399. } else {
  1400. version = mxs_read(s, REG_VERSION);
  1401. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  1402. (version >> 24) & 0xff,
  1403. (version >> 16) & 0xff, version & 0xffff);
  1404. }
  1405. return 0;
  1406. out_free_qpio_irq:
  1407. mxs_auart_free_gpio_irq(s);
  1408. auart_port[pdev->id] = NULL;
  1409. out_iounmap:
  1410. iounmap(s->port.membase);
  1411. out_disable_clks:
  1412. if (is_asm9260_auart(s)) {
  1413. clk_disable_unprepare(s->clk);
  1414. clk_disable_unprepare(s->clk_ahb);
  1415. }
  1416. return ret;
  1417. }
  1418. static int mxs_auart_remove(struct platform_device *pdev)
  1419. {
  1420. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  1421. uart_remove_one_port(&auart_driver, &s->port);
  1422. auart_port[pdev->id] = NULL;
  1423. mxs_auart_free_gpio_irq(s);
  1424. iounmap(s->port.membase);
  1425. if (is_asm9260_auart(s)) {
  1426. clk_disable_unprepare(s->clk);
  1427. clk_disable_unprepare(s->clk_ahb);
  1428. }
  1429. return 0;
  1430. }
  1431. static struct platform_driver mxs_auart_driver = {
  1432. .probe = mxs_auart_probe,
  1433. .remove = mxs_auart_remove,
  1434. .driver = {
  1435. .name = "mxs-auart",
  1436. .of_match_table = mxs_auart_dt_ids,
  1437. },
  1438. };
  1439. static int __init mxs_auart_init(void)
  1440. {
  1441. int r;
  1442. r = uart_register_driver(&auart_driver);
  1443. if (r)
  1444. goto out;
  1445. r = platform_driver_register(&mxs_auart_driver);
  1446. if (r)
  1447. goto out_err;
  1448. return 0;
  1449. out_err:
  1450. uart_unregister_driver(&auart_driver);
  1451. out:
  1452. return r;
  1453. }
  1454. static void __exit mxs_auart_exit(void)
  1455. {
  1456. platform_driver_unregister(&mxs_auart_driver);
  1457. uart_unregister_driver(&auart_driver);
  1458. }
  1459. module_init(mxs_auart_init);
  1460. module_exit(mxs_auart_exit);
  1461. MODULE_LICENSE("GPL");
  1462. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  1463. MODULE_ALIAS("platform:mxs-auart");