mxs-auart.c 46 KB

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