mxs-auart.c 45 KB

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