mxs-auart.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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 <dimka@embeddedalley.com>
  8. *
  9. * Copyright 2014 Oleksij Rempel <linux@rempel-privat.de>
  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.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 tty_port *tport = &s->port.state->port;
  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 (kfifo_len(&tport->xmit_fifo) < 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 tty_port *tport = &s->port.state->port;
  500. bool pending;
  501. u8 ch;
  502. if (auart_dma_enabled(s)) {
  503. u32 i = 0;
  504. void *buffer = s->tx_dma_buf;
  505. if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
  506. return;
  507. if (uart_tx_stopped(&s->port))
  508. mxs_auart_stop_tx(&s->port);
  509. else
  510. i = kfifo_out(&tport->xmit_fifo, buffer,
  511. UART_XMIT_SIZE);
  512. if (i) {
  513. mxs_auart_dma_tx(s, i);
  514. } else {
  515. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  516. smp_mb__after_atomic();
  517. }
  518. return;
  519. }
  520. pending = uart_port_tx_flags(&s->port, ch, UART_TX_NOSTOP,
  521. !(mxs_read(s, REG_STAT) & AUART_STAT_TXFF),
  522. mxs_write(ch, s, REG_DATA));
  523. if (pending)
  524. mxs_set(AUART_INTR_TXIEN, s, REG_INTR);
  525. else
  526. mxs_clr(AUART_INTR_TXIEN, s, REG_INTR);
  527. if (uart_tx_stopped(&s->port))
  528. mxs_auart_stop_tx(&s->port);
  529. }
  530. static void mxs_auart_rx_char(struct mxs_auart_port *s)
  531. {
  532. u32 stat;
  533. u8 c, flag;
  534. c = mxs_read(s, REG_DATA);
  535. stat = mxs_read(s, REG_STAT);
  536. flag = TTY_NORMAL;
  537. s->port.icount.rx++;
  538. if (stat & AUART_STAT_BERR) {
  539. s->port.icount.brk++;
  540. if (uart_handle_break(&s->port))
  541. goto out;
  542. } else if (stat & AUART_STAT_PERR) {
  543. s->port.icount.parity++;
  544. } else if (stat & AUART_STAT_FERR) {
  545. s->port.icount.frame++;
  546. }
  547. /*
  548. * Mask off conditions which should be ingored.
  549. */
  550. stat &= s->port.read_status_mask;
  551. if (stat & AUART_STAT_BERR) {
  552. flag = TTY_BREAK;
  553. } else if (stat & AUART_STAT_PERR)
  554. flag = TTY_PARITY;
  555. else if (stat & AUART_STAT_FERR)
  556. flag = TTY_FRAME;
  557. if (stat & AUART_STAT_OERR)
  558. s->port.icount.overrun++;
  559. if (uart_handle_sysrq_char(&s->port, c))
  560. goto out;
  561. uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
  562. out:
  563. mxs_write(stat, s, REG_STAT);
  564. }
  565. static void mxs_auart_rx_chars(struct mxs_auart_port *s)
  566. {
  567. u32 stat = 0;
  568. for (;;) {
  569. stat = mxs_read(s, REG_STAT);
  570. if (stat & AUART_STAT_RXFE)
  571. break;
  572. mxs_auart_rx_char(s);
  573. }
  574. mxs_write(stat, s, REG_STAT);
  575. tty_flip_buffer_push(&s->port.state->port);
  576. }
  577. static int mxs_auart_request_port(struct uart_port *u)
  578. {
  579. return 0;
  580. }
  581. static int mxs_auart_verify_port(struct uart_port *u,
  582. struct serial_struct *ser)
  583. {
  584. if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
  585. return -EINVAL;
  586. return 0;
  587. }
  588. static void mxs_auart_config_port(struct uart_port *u, int flags)
  589. {
  590. }
  591. static const char *mxs_auart_type(struct uart_port *u)
  592. {
  593. struct mxs_auart_port *s = to_auart_port(u);
  594. return dev_name(s->dev);
  595. }
  596. static void mxs_auart_release_port(struct uart_port *u)
  597. {
  598. }
  599. static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
  600. {
  601. struct mxs_auart_port *s = to_auart_port(u);
  602. u32 ctrl = mxs_read(s, REG_CTRL2);
  603. ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
  604. if (mctrl & TIOCM_RTS) {
  605. if (uart_cts_enabled(u))
  606. ctrl |= AUART_CTRL2_RTSEN;
  607. else
  608. ctrl |= AUART_CTRL2_RTS;
  609. }
  610. mxs_write(ctrl, s, REG_CTRL2);
  611. mctrl_gpio_set(s->gpios, mctrl);
  612. }
  613. #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
  614. static u32 mxs_auart_modem_status(struct mxs_auart_port *s, u32 mctrl)
  615. {
  616. u32 mctrl_diff;
  617. mctrl_diff = mctrl ^ s->mctrl_prev;
  618. s->mctrl_prev = mctrl;
  619. if (mctrl_diff & MCTRL_ANY_DELTA && s->ms_irq_enabled &&
  620. s->port.state != NULL) {
  621. if (mctrl_diff & TIOCM_RI)
  622. s->port.icount.rng++;
  623. if (mctrl_diff & TIOCM_DSR)
  624. s->port.icount.dsr++;
  625. if (mctrl_diff & TIOCM_CD)
  626. uart_handle_dcd_change(&s->port, mctrl & TIOCM_CD);
  627. if (mctrl_diff & TIOCM_CTS)
  628. uart_handle_cts_change(&s->port, mctrl & TIOCM_CTS);
  629. wake_up_interruptible(&s->port.state->port.delta_msr_wait);
  630. }
  631. return mctrl;
  632. }
  633. static u32 mxs_auart_get_mctrl(struct uart_port *u)
  634. {
  635. struct mxs_auart_port *s = to_auart_port(u);
  636. u32 stat = mxs_read(s, REG_STAT);
  637. u32 mctrl = 0;
  638. if (stat & AUART_STAT_CTS)
  639. mctrl |= TIOCM_CTS;
  640. return mctrl_gpio_get(s->gpios, &mctrl);
  641. }
  642. /*
  643. * Enable modem status interrupts
  644. */
  645. static void mxs_auart_enable_ms(struct uart_port *port)
  646. {
  647. struct mxs_auart_port *s = to_auart_port(port);
  648. /*
  649. * Interrupt should not be enabled twice
  650. */
  651. if (s->ms_irq_enabled)
  652. return;
  653. s->ms_irq_enabled = true;
  654. if (s->gpio_irq[UART_GPIO_CTS] >= 0)
  655. enable_irq(s->gpio_irq[UART_GPIO_CTS]);
  656. /* TODO: enable AUART_INTR_CTSMIEN otherwise */
  657. if (s->gpio_irq[UART_GPIO_DSR] >= 0)
  658. enable_irq(s->gpio_irq[UART_GPIO_DSR]);
  659. if (s->gpio_irq[UART_GPIO_RI] >= 0)
  660. enable_irq(s->gpio_irq[UART_GPIO_RI]);
  661. if (s->gpio_irq[UART_GPIO_DCD] >= 0)
  662. enable_irq(s->gpio_irq[UART_GPIO_DCD]);
  663. }
  664. /*
  665. * Disable modem status interrupts
  666. */
  667. static void mxs_auart_disable_ms(struct uart_port *port)
  668. {
  669. struct mxs_auart_port *s = to_auart_port(port);
  670. /*
  671. * Interrupt should not be disabled twice
  672. */
  673. if (!s->ms_irq_enabled)
  674. return;
  675. s->ms_irq_enabled = false;
  676. if (s->gpio_irq[UART_GPIO_CTS] >= 0)
  677. disable_irq(s->gpio_irq[UART_GPIO_CTS]);
  678. /* TODO: disable AUART_INTR_CTSMIEN otherwise */
  679. if (s->gpio_irq[UART_GPIO_DSR] >= 0)
  680. disable_irq(s->gpio_irq[UART_GPIO_DSR]);
  681. if (s->gpio_irq[UART_GPIO_RI] >= 0)
  682. disable_irq(s->gpio_irq[UART_GPIO_RI]);
  683. if (s->gpio_irq[UART_GPIO_DCD] >= 0)
  684. disable_irq(s->gpio_irq[UART_GPIO_DCD]);
  685. }
  686. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
  687. static void dma_rx_callback(void *arg)
  688. {
  689. struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
  690. struct tty_port *port = &s->port.state->port;
  691. int count;
  692. u32 stat;
  693. dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
  694. stat = mxs_read(s, REG_STAT);
  695. stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
  696. AUART_STAT_PERR | AUART_STAT_FERR);
  697. count = stat & AUART_STAT_RXCOUNT_MASK;
  698. tty_insert_flip_string(port, s->rx_dma_buf, count);
  699. mxs_write(stat, s, REG_STAT);
  700. tty_flip_buffer_push(port);
  701. /* start the next DMA for RX. */
  702. mxs_auart_dma_prep_rx(s);
  703. }
  704. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
  705. {
  706. struct dma_async_tx_descriptor *desc;
  707. struct scatterlist *sgl = &s->rx_sgl;
  708. struct dma_chan *channel = s->rx_dma_chan;
  709. u32 pio[1];
  710. /* [1] : send PIO */
  711. pio[0] = AUART_CTRL0_RXTO_ENABLE
  712. | AUART_CTRL0_RXTIMEOUT(0x80)
  713. | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
  714. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  715. 1, DMA_TRANS_NONE, 0);
  716. if (!desc) {
  717. dev_err(s->dev, "step 1 error\n");
  718. return -EINVAL;
  719. }
  720. /* [2] : send DMA request */
  721. sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
  722. dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
  723. desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
  724. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  725. if (!desc) {
  726. dev_err(s->dev, "step 2 error\n");
  727. return -1;
  728. }
  729. /* [3] : submit the DMA, but do not issue it. */
  730. desc->callback = dma_rx_callback;
  731. desc->callback_param = s;
  732. dmaengine_submit(desc);
  733. dma_async_issue_pending(channel);
  734. return 0;
  735. }
  736. static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
  737. {
  738. if (s->tx_dma_chan) {
  739. dma_release_channel(s->tx_dma_chan);
  740. s->tx_dma_chan = NULL;
  741. }
  742. if (s->rx_dma_chan) {
  743. dma_release_channel(s->rx_dma_chan);
  744. s->rx_dma_chan = NULL;
  745. }
  746. kfree(s->tx_dma_buf);
  747. kfree(s->rx_dma_buf);
  748. s->tx_dma_buf = NULL;
  749. s->rx_dma_buf = NULL;
  750. }
  751. static void mxs_auart_dma_exit(struct mxs_auart_port *s)
  752. {
  753. mxs_clr(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
  754. s, REG_CTRL2);
  755. mxs_auart_dma_exit_channel(s);
  756. s->flags &= ~MXS_AUART_DMA_ENABLED;
  757. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  758. clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
  759. }
  760. static int mxs_auart_dma_init(struct mxs_auart_port *s)
  761. {
  762. struct dma_chan *chan;
  763. if (auart_dma_enabled(s))
  764. return 0;
  765. /* init for RX */
  766. chan = dma_request_chan(s->dev, "rx");
  767. if (IS_ERR(chan))
  768. goto err_out;
  769. s->rx_dma_chan = chan;
  770. s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  771. if (!s->rx_dma_buf)
  772. goto err_out;
  773. /* init for TX */
  774. chan = dma_request_chan(s->dev, "tx");
  775. if (IS_ERR(chan))
  776. goto err_out;
  777. s->tx_dma_chan = chan;
  778. s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  779. if (!s->tx_dma_buf)
  780. goto err_out;
  781. /* set the flags */
  782. s->flags |= MXS_AUART_DMA_ENABLED;
  783. dev_dbg(s->dev, "enabled the DMA support.");
  784. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  785. s->port.fifosize = UART_XMIT_SIZE;
  786. return 0;
  787. err_out:
  788. mxs_auart_dma_exit_channel(s);
  789. return -EINVAL;
  790. }
  791. #define RTS_AT_AUART() !mctrl_gpio_to_gpiod(s->gpios, UART_GPIO_RTS)
  792. #define CTS_AT_AUART() !mctrl_gpio_to_gpiod(s->gpios, UART_GPIO_CTS)
  793. static void mxs_auart_settermios(struct uart_port *u,
  794. struct ktermios *termios,
  795. const struct ktermios *old)
  796. {
  797. struct mxs_auart_port *s = to_auart_port(u);
  798. u32 ctrl, ctrl2, div;
  799. unsigned int cflag, baud, baud_min, baud_max;
  800. cflag = termios->c_cflag;
  801. ctrl = AUART_LINECTRL_FEN;
  802. ctrl2 = mxs_read(s, REG_CTRL2);
  803. ctrl |= AUART_LINECTRL_WLEN(tty_get_char_size(cflag));
  804. /* parity */
  805. if (cflag & PARENB) {
  806. ctrl |= AUART_LINECTRL_PEN;
  807. if ((cflag & PARODD) == 0)
  808. ctrl |= AUART_LINECTRL_EPS;
  809. if (cflag & CMSPAR)
  810. ctrl |= AUART_LINECTRL_SPS;
  811. }
  812. u->read_status_mask = AUART_STAT_OERR;
  813. if (termios->c_iflag & INPCK)
  814. u->read_status_mask |= AUART_STAT_PERR;
  815. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  816. u->read_status_mask |= AUART_STAT_BERR;
  817. /*
  818. * Characters to ignore
  819. */
  820. u->ignore_status_mask = 0;
  821. if (termios->c_iflag & IGNPAR)
  822. u->ignore_status_mask |= AUART_STAT_PERR;
  823. if (termios->c_iflag & IGNBRK) {
  824. u->ignore_status_mask |= AUART_STAT_BERR;
  825. /*
  826. * If we're ignoring parity and break indicators,
  827. * ignore overruns too (for real raw support).
  828. */
  829. if (termios->c_iflag & IGNPAR)
  830. u->ignore_status_mask |= AUART_STAT_OERR;
  831. }
  832. /*
  833. * ignore all characters if CREAD is not set
  834. */
  835. if (cflag & CREAD)
  836. ctrl2 |= AUART_CTRL2_RXE;
  837. else
  838. ctrl2 &= ~AUART_CTRL2_RXE;
  839. /* figure out the stop bits requested */
  840. if (cflag & CSTOPB)
  841. ctrl |= AUART_LINECTRL_STP2;
  842. /* figure out the hardware flow control settings */
  843. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  844. if (cflag & CRTSCTS) {
  845. /*
  846. * The DMA has a bug(see errata:2836) in mx23.
  847. * So we can not implement the DMA for auart in mx23,
  848. * we can only implement the DMA support for auart
  849. * in mx28.
  850. */
  851. if (is_imx28_auart(s)
  852. && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
  853. if (!mxs_auart_dma_init(s))
  854. /* enable DMA tranfer */
  855. ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
  856. | AUART_CTRL2_DMAONERR;
  857. }
  858. /* Even if RTS is GPIO line RTSEN can be enabled because
  859. * the pinctrl configuration decides about RTS pin function */
  860. ctrl2 |= AUART_CTRL2_RTSEN;
  861. if (CTS_AT_AUART())
  862. ctrl2 |= AUART_CTRL2_CTSEN;
  863. }
  864. /* set baud rate */
  865. if (is_asm9260_auart(s)) {
  866. baud = uart_get_baud_rate(u, termios, old,
  867. u->uartclk * 4 / 0x3FFFFF,
  868. u->uartclk / 16);
  869. div = u->uartclk * 4 / baud;
  870. } else {
  871. baud_min = DIV_ROUND_UP(u->uartclk * 32,
  872. AUART_LINECTRL_BAUD_DIV_MAX);
  873. baud_max = u->uartclk * 32 / AUART_LINECTRL_BAUD_DIV_MIN;
  874. baud = uart_get_baud_rate(u, termios, old, baud_min, baud_max);
  875. div = DIV_ROUND_CLOSEST(u->uartclk * 32, baud);
  876. }
  877. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  878. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  879. mxs_write(ctrl, s, REG_LINECTRL);
  880. mxs_write(ctrl2, s, REG_CTRL2);
  881. uart_update_timeout(u, termios->c_cflag, baud);
  882. /* prepare for the DMA RX. */
  883. if (auart_dma_enabled(s) &&
  884. !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
  885. if (!mxs_auart_dma_prep_rx(s)) {
  886. /* Disable the normal RX interrupt. */
  887. mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
  888. s, REG_INTR);
  889. } else {
  890. mxs_auart_dma_exit(s);
  891. dev_err(s->dev, "We can not start up the DMA.\n");
  892. }
  893. }
  894. /* CTS flow-control and modem-status interrupts */
  895. if (UART_ENABLE_MS(u, termios->c_cflag))
  896. mxs_auart_enable_ms(u);
  897. else
  898. mxs_auart_disable_ms(u);
  899. }
  900. static void mxs_auart_set_ldisc(struct uart_port *port,
  901. struct ktermios *termios)
  902. {
  903. if (termios->c_line == N_PPS) {
  904. port->flags |= UPF_HARDPPS_CD;
  905. mxs_auart_enable_ms(port);
  906. } else {
  907. port->flags &= ~UPF_HARDPPS_CD;
  908. }
  909. }
  910. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  911. {
  912. u32 istat, stat;
  913. struct mxs_auart_port *s = context;
  914. u32 mctrl_temp = s->mctrl_prev;
  915. uart_port_lock(&s->port);
  916. stat = mxs_read(s, REG_STAT);
  917. istat = mxs_read(s, REG_INTR);
  918. /* ack irq */
  919. mxs_clr(istat & (AUART_INTR_RTIS | AUART_INTR_TXIS | AUART_INTR_RXIS
  920. | AUART_INTR_CTSMIS), s, REG_INTR);
  921. /*
  922. * Dealing with GPIO interrupt
  923. */
  924. if (irq == s->gpio_irq[UART_GPIO_CTS] ||
  925. irq == s->gpio_irq[UART_GPIO_DCD] ||
  926. irq == s->gpio_irq[UART_GPIO_DSR] ||
  927. irq == s->gpio_irq[UART_GPIO_RI])
  928. mxs_auart_modem_status(s,
  929. mctrl_gpio_get(s->gpios, &mctrl_temp));
  930. if (istat & AUART_INTR_CTSMIS) {
  931. if (CTS_AT_AUART() && s->ms_irq_enabled)
  932. uart_handle_cts_change(&s->port,
  933. stat & AUART_STAT_CTS);
  934. mxs_clr(AUART_INTR_CTSMIS, s, REG_INTR);
  935. istat &= ~AUART_INTR_CTSMIS;
  936. }
  937. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  938. if (!auart_dma_enabled(s))
  939. mxs_auart_rx_chars(s);
  940. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  941. }
  942. if (istat & AUART_INTR_TXIS) {
  943. mxs_auart_tx_chars(s);
  944. istat &= ~AUART_INTR_TXIS;
  945. }
  946. uart_port_unlock(&s->port);
  947. return IRQ_HANDLED;
  948. }
  949. static void mxs_auart_reset_deassert(struct mxs_auart_port *s)
  950. {
  951. int i;
  952. unsigned int reg;
  953. mxs_clr(AUART_CTRL0_SFTRST, s, REG_CTRL0);
  954. for (i = 0; i < 10000; i++) {
  955. reg = mxs_read(s, REG_CTRL0);
  956. if (!(reg & AUART_CTRL0_SFTRST))
  957. break;
  958. udelay(3);
  959. }
  960. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  961. }
  962. static void mxs_auart_reset_assert(struct mxs_auart_port *s)
  963. {
  964. int i;
  965. u32 reg;
  966. reg = mxs_read(s, REG_CTRL0);
  967. /* if already in reset state, keep it untouched */
  968. if (reg & AUART_CTRL0_SFTRST)
  969. return;
  970. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  971. mxs_set(AUART_CTRL0_SFTRST, s, REG_CTRL0);
  972. for (i = 0; i < 1000; i++) {
  973. reg = mxs_read(s, REG_CTRL0);
  974. /* reset is finished when the clock is gated */
  975. if (reg & AUART_CTRL0_CLKGATE)
  976. return;
  977. udelay(10);
  978. }
  979. dev_err(s->dev, "Failed to reset the unit.");
  980. }
  981. static int mxs_auart_startup(struct uart_port *u)
  982. {
  983. int ret;
  984. struct mxs_auart_port *s = to_auart_port(u);
  985. ret = clk_prepare_enable(s->clk);
  986. if (ret)
  987. return ret;
  988. if (uart_console(u)) {
  989. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  990. } else {
  991. /* reset the unit to a well known state */
  992. mxs_auart_reset_assert(s);
  993. mxs_auart_reset_deassert(s);
  994. }
  995. mxs_set(AUART_CTRL2_UARTEN, s, REG_CTRL2);
  996. mxs_write(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  997. s, REG_INTR);
  998. /* Reset FIFO size (it could have changed if DMA was enabled) */
  999. u->fifosize = MXS_AUART_FIFO_SIZE;
  1000. /*
  1001. * Enable fifo so all four bytes of a DMA word are written to
  1002. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  1003. */
  1004. mxs_set(AUART_LINECTRL_FEN, s, REG_LINECTRL);
  1005. /* get initial status of modem lines */
  1006. mctrl_gpio_get(s->gpios, &s->mctrl_prev);
  1007. s->ms_irq_enabled = false;
  1008. return 0;
  1009. }
  1010. static void mxs_auart_shutdown(struct uart_port *u)
  1011. {
  1012. struct mxs_auart_port *s = to_auart_port(u);
  1013. mxs_auart_disable_ms(u);
  1014. if (auart_dma_enabled(s))
  1015. mxs_auart_dma_exit(s);
  1016. if (uart_console(u)) {
  1017. mxs_clr(AUART_CTRL2_UARTEN, s, REG_CTRL2);
  1018. mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN |
  1019. AUART_INTR_CTSMIEN, s, REG_INTR);
  1020. mxs_set(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1021. } else {
  1022. mxs_auart_reset_assert(s);
  1023. }
  1024. clk_disable_unprepare(s->clk);
  1025. }
  1026. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  1027. {
  1028. struct mxs_auart_port *s = to_auart_port(u);
  1029. if ((mxs_read(s, REG_STAT) &
  1030. (AUART_STAT_TXFE | AUART_STAT_BUSY)) == AUART_STAT_TXFE)
  1031. return TIOCSER_TEMT;
  1032. return 0;
  1033. }
  1034. static void mxs_auart_start_tx(struct uart_port *u)
  1035. {
  1036. struct mxs_auart_port *s = to_auart_port(u);
  1037. /* enable transmitter */
  1038. mxs_set(AUART_CTRL2_TXE, s, REG_CTRL2);
  1039. mxs_auart_tx_chars(s);
  1040. }
  1041. static void mxs_auart_stop_tx(struct uart_port *u)
  1042. {
  1043. struct mxs_auart_port *s = to_auart_port(u);
  1044. mxs_clr(AUART_CTRL2_TXE, s, REG_CTRL2);
  1045. }
  1046. static void mxs_auart_stop_rx(struct uart_port *u)
  1047. {
  1048. struct mxs_auart_port *s = to_auart_port(u);
  1049. mxs_clr(AUART_CTRL2_RXE, s, REG_CTRL2);
  1050. }
  1051. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  1052. {
  1053. struct mxs_auart_port *s = to_auart_port(u);
  1054. if (ctl)
  1055. mxs_set(AUART_LINECTRL_BRK, s, REG_LINECTRL);
  1056. else
  1057. mxs_clr(AUART_LINECTRL_BRK, s, REG_LINECTRL);
  1058. }
  1059. static const struct uart_ops mxs_auart_ops = {
  1060. .tx_empty = mxs_auart_tx_empty,
  1061. .start_tx = mxs_auart_start_tx,
  1062. .stop_tx = mxs_auart_stop_tx,
  1063. .stop_rx = mxs_auart_stop_rx,
  1064. .enable_ms = mxs_auart_enable_ms,
  1065. .break_ctl = mxs_auart_break_ctl,
  1066. .set_mctrl = mxs_auart_set_mctrl,
  1067. .get_mctrl = mxs_auart_get_mctrl,
  1068. .startup = mxs_auart_startup,
  1069. .shutdown = mxs_auart_shutdown,
  1070. .set_termios = mxs_auart_settermios,
  1071. .set_ldisc = mxs_auart_set_ldisc,
  1072. .type = mxs_auart_type,
  1073. .release_port = mxs_auart_release_port,
  1074. .request_port = mxs_auart_request_port,
  1075. .config_port = mxs_auart_config_port,
  1076. .verify_port = mxs_auart_verify_port,
  1077. };
  1078. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  1079. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  1080. static void mxs_auart_console_putchar(struct uart_port *port, unsigned char ch)
  1081. {
  1082. struct mxs_auart_port *s = to_auart_port(port);
  1083. unsigned int to = 1000;
  1084. while (mxs_read(s, REG_STAT) & AUART_STAT_TXFF) {
  1085. if (!to--)
  1086. break;
  1087. udelay(1);
  1088. }
  1089. mxs_write(ch, s, REG_DATA);
  1090. }
  1091. static void
  1092. auart_console_write(struct console *co, const char *str, unsigned int count)
  1093. {
  1094. struct mxs_auart_port *s;
  1095. struct uart_port *port;
  1096. unsigned int old_ctrl0, old_ctrl2;
  1097. unsigned int to = 20000;
  1098. if (co->index >= MXS_AUART_PORTS || co->index < 0)
  1099. return;
  1100. s = auart_port[co->index];
  1101. port = &s->port;
  1102. clk_enable(s->clk);
  1103. /* First save the CR then disable the interrupts */
  1104. old_ctrl2 = mxs_read(s, REG_CTRL2);
  1105. old_ctrl0 = mxs_read(s, REG_CTRL0);
  1106. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1107. mxs_set(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE, s, REG_CTRL2);
  1108. uart_console_write(port, str, count, mxs_auart_console_putchar);
  1109. /* Finally, wait for transmitter to become empty ... */
  1110. while (mxs_read(s, REG_STAT) & AUART_STAT_BUSY) {
  1111. udelay(1);
  1112. if (!to--)
  1113. break;
  1114. }
  1115. /*
  1116. * ... and restore the TCR if we waited long enough for the transmitter
  1117. * to be idle. This might keep the transmitter enabled although it is
  1118. * unused, but that is better than to disable it while it is still
  1119. * transmitting.
  1120. */
  1121. if (!(mxs_read(s, REG_STAT) & AUART_STAT_BUSY)) {
  1122. mxs_write(old_ctrl0, s, REG_CTRL0);
  1123. mxs_write(old_ctrl2, s, REG_CTRL2);
  1124. }
  1125. clk_disable(s->clk);
  1126. }
  1127. static void __init
  1128. auart_console_get_options(struct mxs_auart_port *s, int *baud,
  1129. int *parity, int *bits)
  1130. {
  1131. struct uart_port *port = &s->port;
  1132. unsigned int lcr_h, quot;
  1133. if (!(mxs_read(s, REG_CTRL2) & AUART_CTRL2_UARTEN))
  1134. return;
  1135. lcr_h = mxs_read(s, REG_LINECTRL);
  1136. *parity = 'n';
  1137. if (lcr_h & AUART_LINECTRL_PEN) {
  1138. if (lcr_h & AUART_LINECTRL_EPS)
  1139. *parity = 'e';
  1140. else
  1141. *parity = 'o';
  1142. }
  1143. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(7))
  1144. *bits = 7;
  1145. else
  1146. *bits = 8;
  1147. quot = ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVINT_MASK))
  1148. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  1149. quot |= ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  1150. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  1151. if (quot == 0)
  1152. quot = 1;
  1153. *baud = (port->uartclk << 2) / quot;
  1154. }
  1155. static int __init
  1156. auart_console_setup(struct console *co, char *options)
  1157. {
  1158. struct mxs_auart_port *s;
  1159. int baud = 9600;
  1160. int bits = 8;
  1161. int parity = 'n';
  1162. int flow = 'n';
  1163. int ret;
  1164. /*
  1165. * Check whether an invalid uart number has been specified, and
  1166. * if so, search for the first available port that does have
  1167. * console support.
  1168. */
  1169. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  1170. co->index = 0;
  1171. s = auart_port[co->index];
  1172. if (!s)
  1173. return -ENODEV;
  1174. ret = clk_prepare_enable(s->clk);
  1175. if (ret)
  1176. return ret;
  1177. if (options)
  1178. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1179. else
  1180. auart_console_get_options(s, &baud, &parity, &bits);
  1181. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  1182. clk_disable_unprepare(s->clk);
  1183. return ret;
  1184. }
  1185. static struct console auart_console = {
  1186. .name = "ttyAPP",
  1187. .write = auart_console_write,
  1188. .device = uart_console_device,
  1189. .setup = auart_console_setup,
  1190. .flags = CON_PRINTBUFFER,
  1191. .index = -1,
  1192. .data = &auart_driver,
  1193. };
  1194. #endif
  1195. static struct uart_driver auart_driver = {
  1196. .owner = THIS_MODULE,
  1197. .driver_name = "ttyAPP",
  1198. .dev_name = "ttyAPP",
  1199. .major = 0,
  1200. .minor = 0,
  1201. .nr = MXS_AUART_PORTS,
  1202. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  1203. .cons = &auart_console,
  1204. #endif
  1205. };
  1206. static void mxs_init_regs(struct mxs_auart_port *s)
  1207. {
  1208. if (is_asm9260_auart(s))
  1209. s->vendor = &vendor_alphascale_asm9260;
  1210. else
  1211. s->vendor = &vendor_freescale_stmp37xx;
  1212. }
  1213. static int mxs_get_clks(struct mxs_auart_port *s,
  1214. struct platform_device *pdev)
  1215. {
  1216. int err;
  1217. if (!is_asm9260_auart(s)) {
  1218. s->clk = devm_clk_get(&pdev->dev, NULL);
  1219. return PTR_ERR_OR_ZERO(s->clk);
  1220. }
  1221. s->clk = devm_clk_get(s->dev, "mod");
  1222. if (IS_ERR(s->clk)) {
  1223. dev_err(s->dev, "Failed to get \"mod\" clk\n");
  1224. return PTR_ERR(s->clk);
  1225. }
  1226. s->clk_ahb = devm_clk_get(s->dev, "ahb");
  1227. if (IS_ERR(s->clk_ahb)) {
  1228. dev_err(s->dev, "Failed to get \"ahb\" clk\n");
  1229. return PTR_ERR(s->clk_ahb);
  1230. }
  1231. err = clk_prepare_enable(s->clk_ahb);
  1232. if (err) {
  1233. dev_err(s->dev, "Failed to enable ahb_clk!\n");
  1234. return err;
  1235. }
  1236. err = clk_set_rate(s->clk, clk_get_rate(s->clk_ahb));
  1237. if (err) {
  1238. dev_err(s->dev, "Failed to set rate!\n");
  1239. goto disable_clk_ahb;
  1240. }
  1241. err = clk_prepare_enable(s->clk);
  1242. if (err) {
  1243. dev_err(s->dev, "Failed to enable clk!\n");
  1244. goto disable_clk_ahb;
  1245. }
  1246. return 0;
  1247. disable_clk_ahb:
  1248. clk_disable_unprepare(s->clk_ahb);
  1249. return err;
  1250. }
  1251. static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
  1252. {
  1253. enum mctrl_gpio_idx i;
  1254. struct gpio_desc *gpiod;
  1255. s->gpios = mctrl_gpio_init_noauto(dev, 0);
  1256. if (IS_ERR(s->gpios))
  1257. return PTR_ERR(s->gpios);
  1258. /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
  1259. if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
  1260. if (test_bit(MXS_AUART_RTSCTS, &s->flags))
  1261. dev_warn(dev,
  1262. "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
  1263. clear_bit(MXS_AUART_RTSCTS, &s->flags);
  1264. }
  1265. for (i = 0; i < UART_GPIO_MAX; i++) {
  1266. gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
  1267. if (gpiod && (gpiod_get_direction(gpiod) == 1))
  1268. s->gpio_irq[i] = gpiod_to_irq(gpiod);
  1269. else
  1270. s->gpio_irq[i] = -EINVAL;
  1271. }
  1272. return 0;
  1273. }
  1274. static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
  1275. {
  1276. enum mctrl_gpio_idx i;
  1277. for (i = 0; i < UART_GPIO_MAX; i++)
  1278. if (s->gpio_irq[i] >= 0)
  1279. free_irq(s->gpio_irq[i], s);
  1280. }
  1281. static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
  1282. {
  1283. int *irq = s->gpio_irq;
  1284. enum mctrl_gpio_idx i;
  1285. int err = 0;
  1286. for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
  1287. if (irq[i] < 0)
  1288. continue;
  1289. irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
  1290. err = request_irq(irq[i], mxs_auart_irq_handle,
  1291. IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
  1292. if (err)
  1293. dev_err(s->dev, "%s - Can't get %d irq\n",
  1294. __func__, irq[i]);
  1295. }
  1296. /*
  1297. * If something went wrong, rollback.
  1298. * Be careful: i may be unsigned.
  1299. */
  1300. while (err && (i-- > 0))
  1301. if (irq[i] >= 0)
  1302. free_irq(irq[i], s);
  1303. return err;
  1304. }
  1305. static int mxs_auart_probe(struct platform_device *pdev)
  1306. {
  1307. struct device_node *np = pdev->dev.of_node;
  1308. struct mxs_auart_port *s;
  1309. u32 version;
  1310. int ret, irq;
  1311. struct resource *r;
  1312. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  1313. if (!s)
  1314. return -ENOMEM;
  1315. s->port.dev = &pdev->dev;
  1316. s->dev = &pdev->dev;
  1317. ret = of_alias_get_id(np, "serial");
  1318. if (ret < 0) {
  1319. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  1320. return ret;
  1321. }
  1322. s->port.line = ret;
  1323. if (of_property_read_bool(np, "uart-has-rtscts") ||
  1324. of_property_read_bool(np, "fsl,uart-has-rtscts") /* deprecated */)
  1325. set_bit(MXS_AUART_RTSCTS, &s->flags);
  1326. if (s->port.line >= ARRAY_SIZE(auart_port)) {
  1327. dev_err(&pdev->dev, "serial%d out of range\n", s->port.line);
  1328. return -EINVAL;
  1329. }
  1330. s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
  1331. ret = mxs_get_clks(s, pdev);
  1332. if (ret)
  1333. return ret;
  1334. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1335. if (!r) {
  1336. ret = -ENXIO;
  1337. goto out_disable_clks;
  1338. }
  1339. s->port.mapbase = r->start;
  1340. s->port.membase = ioremap(r->start, resource_size(r));
  1341. if (!s->port.membase) {
  1342. ret = -ENOMEM;
  1343. goto out_disable_clks;
  1344. }
  1345. s->port.ops = &mxs_auart_ops;
  1346. s->port.iotype = UPIO_MEM;
  1347. s->port.fifosize = MXS_AUART_FIFO_SIZE;
  1348. s->port.uartclk = clk_get_rate(s->clk);
  1349. s->port.type = PORT_IMX;
  1350. s->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_MXS_AUART_CONSOLE);
  1351. mxs_init_regs(s);
  1352. s->mctrl_prev = 0;
  1353. irq = platform_get_irq(pdev, 0);
  1354. if (irq < 0) {
  1355. ret = irq;
  1356. goto out_iounmap;
  1357. }
  1358. s->port.irq = irq;
  1359. ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
  1360. dev_name(&pdev->dev), s);
  1361. if (ret)
  1362. goto out_iounmap;
  1363. platform_set_drvdata(pdev, s);
  1364. ret = mxs_auart_init_gpios(s, &pdev->dev);
  1365. if (ret) {
  1366. dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
  1367. goto out_iounmap;
  1368. }
  1369. /*
  1370. * Get the GPIO lines IRQ
  1371. */
  1372. ret = mxs_auart_request_gpio_irq(s);
  1373. if (ret)
  1374. goto out_iounmap;
  1375. auart_port[s->port.line] = s;
  1376. mxs_auart_reset_deassert(s);
  1377. ret = uart_add_one_port(&auart_driver, &s->port);
  1378. if (ret)
  1379. goto out_free_qpio_irq;
  1380. /* ASM9260 don't have version reg */
  1381. if (is_asm9260_auart(s)) {
  1382. dev_info(&pdev->dev, "Found APPUART ASM9260\n");
  1383. } else {
  1384. version = mxs_read(s, REG_VERSION);
  1385. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  1386. (version >> 24) & 0xff,
  1387. (version >> 16) & 0xff, version & 0xffff);
  1388. }
  1389. return 0;
  1390. out_free_qpio_irq:
  1391. mxs_auart_free_gpio_irq(s);
  1392. auart_port[pdev->id] = NULL;
  1393. out_iounmap:
  1394. iounmap(s->port.membase);
  1395. out_disable_clks:
  1396. if (is_asm9260_auart(s)) {
  1397. clk_disable_unprepare(s->clk);
  1398. clk_disable_unprepare(s->clk_ahb);
  1399. }
  1400. return ret;
  1401. }
  1402. static void mxs_auart_remove(struct platform_device *pdev)
  1403. {
  1404. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  1405. uart_remove_one_port(&auart_driver, &s->port);
  1406. auart_port[pdev->id] = NULL;
  1407. mxs_auart_free_gpio_irq(s);
  1408. iounmap(s->port.membase);
  1409. if (is_asm9260_auart(s)) {
  1410. clk_disable_unprepare(s->clk);
  1411. clk_disable_unprepare(s->clk_ahb);
  1412. }
  1413. }
  1414. static struct platform_driver mxs_auart_driver = {
  1415. .probe = mxs_auart_probe,
  1416. .remove = mxs_auart_remove,
  1417. .driver = {
  1418. .name = "mxs-auart",
  1419. .of_match_table = mxs_auart_dt_ids,
  1420. },
  1421. };
  1422. static int __init mxs_auart_init(void)
  1423. {
  1424. int r;
  1425. r = uart_register_driver(&auart_driver);
  1426. if (r)
  1427. goto out;
  1428. r = platform_driver_register(&mxs_auart_driver);
  1429. if (r)
  1430. goto out_err;
  1431. return 0;
  1432. out_err:
  1433. uart_unregister_driver(&auart_driver);
  1434. out:
  1435. return r;
  1436. }
  1437. static void __exit mxs_auart_exit(void)
  1438. {
  1439. platform_driver_unregister(&mxs_auart_driver);
  1440. uart_unregister_driver(&auart_driver);
  1441. }
  1442. module_init(mxs_auart_init);
  1443. module_exit(mxs_auart_exit);
  1444. MODULE_LICENSE("GPL");
  1445. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  1446. MODULE_ALIAS("platform:mxs-auart");