mxs-auart.c 45 KB

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