mxs-auart.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
  821. UART_GPIO_RTS))
  822. #define CTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
  823. UART_GPIO_CTS))
  824. static void mxs_auart_settermios(struct uart_port *u,
  825. struct ktermios *termios,
  826. struct ktermios *old)
  827. {
  828. struct mxs_auart_port *s = to_auart_port(u);
  829. u32 bm, ctrl, ctrl2, div;
  830. unsigned int cflag, baud, baud_min, baud_max;
  831. cflag = termios->c_cflag;
  832. ctrl = AUART_LINECTRL_FEN;
  833. ctrl2 = mxs_read(s, REG_CTRL2);
  834. /* byte size */
  835. switch (cflag & CSIZE) {
  836. case CS5:
  837. bm = 0;
  838. break;
  839. case CS6:
  840. bm = 1;
  841. break;
  842. case CS7:
  843. bm = 2;
  844. break;
  845. case CS8:
  846. bm = 3;
  847. break;
  848. default:
  849. return;
  850. }
  851. ctrl |= AUART_LINECTRL_WLEN(bm);
  852. /* parity */
  853. if (cflag & PARENB) {
  854. ctrl |= AUART_LINECTRL_PEN;
  855. if ((cflag & PARODD) == 0)
  856. ctrl |= AUART_LINECTRL_EPS;
  857. if (cflag & CMSPAR)
  858. ctrl |= AUART_LINECTRL_SPS;
  859. }
  860. u->read_status_mask = AUART_STAT_OERR;
  861. if (termios->c_iflag & INPCK)
  862. u->read_status_mask |= AUART_STAT_PERR;
  863. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  864. u->read_status_mask |= AUART_STAT_BERR;
  865. /*
  866. * Characters to ignore
  867. */
  868. u->ignore_status_mask = 0;
  869. if (termios->c_iflag & IGNPAR)
  870. u->ignore_status_mask |= AUART_STAT_PERR;
  871. if (termios->c_iflag & IGNBRK) {
  872. u->ignore_status_mask |= AUART_STAT_BERR;
  873. /*
  874. * If we're ignoring parity and break indicators,
  875. * ignore overruns too (for real raw support).
  876. */
  877. if (termios->c_iflag & IGNPAR)
  878. u->ignore_status_mask |= AUART_STAT_OERR;
  879. }
  880. /*
  881. * ignore all characters if CREAD is not set
  882. */
  883. if (cflag & CREAD)
  884. ctrl2 |= AUART_CTRL2_RXE;
  885. else
  886. ctrl2 &= ~AUART_CTRL2_RXE;
  887. /* figure out the stop bits requested */
  888. if (cflag & CSTOPB)
  889. ctrl |= AUART_LINECTRL_STP2;
  890. /* figure out the hardware flow control settings */
  891. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  892. if (cflag & CRTSCTS) {
  893. /*
  894. * The DMA has a bug(see errata:2836) in mx23.
  895. * So we can not implement the DMA for auart in mx23,
  896. * we can only implement the DMA support for auart
  897. * in mx28.
  898. */
  899. if (is_imx28_auart(s)
  900. && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
  901. if (!mxs_auart_dma_init(s))
  902. /* enable DMA tranfer */
  903. ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
  904. | AUART_CTRL2_DMAONERR;
  905. }
  906. /* Even if RTS is GPIO line RTSEN can be enabled because
  907. * the pinctrl configuration decides about RTS pin function */
  908. ctrl2 |= AUART_CTRL2_RTSEN;
  909. if (CTS_AT_AUART())
  910. ctrl2 |= AUART_CTRL2_CTSEN;
  911. }
  912. /* set baud rate */
  913. if (is_asm9260_auart(s)) {
  914. baud = uart_get_baud_rate(u, termios, old,
  915. u->uartclk * 4 / 0x3FFFFF,
  916. u->uartclk / 16);
  917. div = u->uartclk * 4 / baud;
  918. } else {
  919. baud_min = DIV_ROUND_UP(u->uartclk * 32,
  920. AUART_LINECTRL_BAUD_DIV_MAX);
  921. baud_max = u->uartclk * 32 / AUART_LINECTRL_BAUD_DIV_MIN;
  922. baud = uart_get_baud_rate(u, termios, old, baud_min, baud_max);
  923. div = DIV_ROUND_CLOSEST(u->uartclk * 32, baud);
  924. }
  925. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  926. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  927. mxs_write(ctrl, s, REG_LINECTRL);
  928. mxs_write(ctrl2, s, REG_CTRL2);
  929. uart_update_timeout(u, termios->c_cflag, baud);
  930. /* prepare for the DMA RX. */
  931. if (auart_dma_enabled(s) &&
  932. !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
  933. if (!mxs_auart_dma_prep_rx(s)) {
  934. /* Disable the normal RX interrupt. */
  935. mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
  936. s, REG_INTR);
  937. } else {
  938. mxs_auart_dma_exit(s);
  939. dev_err(s->dev, "We can not start up the DMA.\n");
  940. }
  941. }
  942. /* CTS flow-control and modem-status interrupts */
  943. if (UART_ENABLE_MS(u, termios->c_cflag))
  944. mxs_auart_enable_ms(u);
  945. else
  946. mxs_auart_disable_ms(u);
  947. }
  948. static void mxs_auart_set_ldisc(struct uart_port *port,
  949. struct ktermios *termios)
  950. {
  951. if (termios->c_line == N_PPS) {
  952. port->flags |= UPF_HARDPPS_CD;
  953. mxs_auart_enable_ms(port);
  954. } else {
  955. port->flags &= ~UPF_HARDPPS_CD;
  956. }
  957. }
  958. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  959. {
  960. u32 istat;
  961. struct mxs_auart_port *s = context;
  962. u32 mctrl_temp = s->mctrl_prev;
  963. u32 stat = mxs_read(s, REG_STAT);
  964. istat = mxs_read(s, REG_INTR);
  965. /* ack irq */
  966. mxs_clr(istat & (AUART_INTR_RTIS | AUART_INTR_TXIS | AUART_INTR_RXIS
  967. | AUART_INTR_CTSMIS), s, REG_INTR);
  968. /*
  969. * Dealing with GPIO interrupt
  970. */
  971. if (irq == s->gpio_irq[UART_GPIO_CTS] ||
  972. irq == s->gpio_irq[UART_GPIO_DCD] ||
  973. irq == s->gpio_irq[UART_GPIO_DSR] ||
  974. irq == s->gpio_irq[UART_GPIO_RI])
  975. mxs_auart_modem_status(s,
  976. mctrl_gpio_get(s->gpios, &mctrl_temp));
  977. if (istat & AUART_INTR_CTSMIS) {
  978. if (CTS_AT_AUART() && s->ms_irq_enabled)
  979. uart_handle_cts_change(&s->port,
  980. stat & AUART_STAT_CTS);
  981. mxs_clr(AUART_INTR_CTSMIS, s, REG_INTR);
  982. istat &= ~AUART_INTR_CTSMIS;
  983. }
  984. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  985. if (!auart_dma_enabled(s))
  986. mxs_auart_rx_chars(s);
  987. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  988. }
  989. if (istat & AUART_INTR_TXIS) {
  990. mxs_auart_tx_chars(s);
  991. istat &= ~AUART_INTR_TXIS;
  992. }
  993. return IRQ_HANDLED;
  994. }
  995. static void mxs_auart_reset_deassert(struct mxs_auart_port *s)
  996. {
  997. int i;
  998. unsigned int reg;
  999. mxs_clr(AUART_CTRL0_SFTRST, s, REG_CTRL0);
  1000. for (i = 0; i < 10000; i++) {
  1001. reg = mxs_read(s, REG_CTRL0);
  1002. if (!(reg & AUART_CTRL0_SFTRST))
  1003. break;
  1004. udelay(3);
  1005. }
  1006. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1007. }
  1008. static void mxs_auart_reset_assert(struct mxs_auart_port *s)
  1009. {
  1010. int i;
  1011. u32 reg;
  1012. reg = mxs_read(s, REG_CTRL0);
  1013. /* if already in reset state, keep it untouched */
  1014. if (reg & AUART_CTRL0_SFTRST)
  1015. return;
  1016. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1017. mxs_set(AUART_CTRL0_SFTRST, s, REG_CTRL0);
  1018. for (i = 0; i < 1000; i++) {
  1019. reg = mxs_read(s, REG_CTRL0);
  1020. /* reset is finished when the clock is gated */
  1021. if (reg & AUART_CTRL0_CLKGATE)
  1022. return;
  1023. udelay(10);
  1024. }
  1025. dev_err(s->dev, "Failed to reset the unit.");
  1026. }
  1027. static int mxs_auart_startup(struct uart_port *u)
  1028. {
  1029. int ret;
  1030. struct mxs_auart_port *s = to_auart_port(u);
  1031. ret = clk_prepare_enable(s->clk);
  1032. if (ret)
  1033. return ret;
  1034. if (uart_console(u)) {
  1035. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1036. } else {
  1037. /* reset the unit to a well known state */
  1038. mxs_auart_reset_assert(s);
  1039. mxs_auart_reset_deassert(s);
  1040. }
  1041. mxs_set(AUART_CTRL2_UARTEN, s, REG_CTRL2);
  1042. mxs_write(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  1043. s, REG_INTR);
  1044. /* Reset FIFO size (it could have changed if DMA was enabled) */
  1045. u->fifosize = MXS_AUART_FIFO_SIZE;
  1046. /*
  1047. * Enable fifo so all four bytes of a DMA word are written to
  1048. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  1049. */
  1050. mxs_set(AUART_LINECTRL_FEN, s, REG_LINECTRL);
  1051. /* get initial status of modem lines */
  1052. mctrl_gpio_get(s->gpios, &s->mctrl_prev);
  1053. s->ms_irq_enabled = false;
  1054. return 0;
  1055. }
  1056. static void mxs_auart_shutdown(struct uart_port *u)
  1057. {
  1058. struct mxs_auart_port *s = to_auart_port(u);
  1059. mxs_auart_disable_ms(u);
  1060. if (auart_dma_enabled(s))
  1061. mxs_auart_dma_exit(s);
  1062. if (uart_console(u)) {
  1063. mxs_clr(AUART_CTRL2_UARTEN, s, REG_CTRL2);
  1064. mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN |
  1065. AUART_INTR_CTSMIEN, s, REG_INTR);
  1066. mxs_set(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1067. } else {
  1068. mxs_auart_reset_assert(s);
  1069. }
  1070. clk_disable_unprepare(s->clk);
  1071. }
  1072. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  1073. {
  1074. struct mxs_auart_port *s = to_auart_port(u);
  1075. if ((mxs_read(s, REG_STAT) &
  1076. (AUART_STAT_TXFE | AUART_STAT_BUSY)) == AUART_STAT_TXFE)
  1077. return TIOCSER_TEMT;
  1078. return 0;
  1079. }
  1080. static void mxs_auart_start_tx(struct uart_port *u)
  1081. {
  1082. struct mxs_auart_port *s = to_auart_port(u);
  1083. /* enable transmitter */
  1084. mxs_set(AUART_CTRL2_TXE, s, REG_CTRL2);
  1085. mxs_auart_tx_chars(s);
  1086. }
  1087. static void mxs_auart_stop_tx(struct uart_port *u)
  1088. {
  1089. struct mxs_auart_port *s = to_auart_port(u);
  1090. mxs_clr(AUART_CTRL2_TXE, s, REG_CTRL2);
  1091. }
  1092. static void mxs_auart_stop_rx(struct uart_port *u)
  1093. {
  1094. struct mxs_auart_port *s = to_auart_port(u);
  1095. mxs_clr(AUART_CTRL2_RXE, s, REG_CTRL2);
  1096. }
  1097. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  1098. {
  1099. struct mxs_auart_port *s = to_auart_port(u);
  1100. if (ctl)
  1101. mxs_set(AUART_LINECTRL_BRK, s, REG_LINECTRL);
  1102. else
  1103. mxs_clr(AUART_LINECTRL_BRK, s, REG_LINECTRL);
  1104. }
  1105. static const struct uart_ops mxs_auart_ops = {
  1106. .tx_empty = mxs_auart_tx_empty,
  1107. .start_tx = mxs_auart_start_tx,
  1108. .stop_tx = mxs_auart_stop_tx,
  1109. .stop_rx = mxs_auart_stop_rx,
  1110. .enable_ms = mxs_auart_enable_ms,
  1111. .break_ctl = mxs_auart_break_ctl,
  1112. .set_mctrl = mxs_auart_set_mctrl,
  1113. .get_mctrl = mxs_auart_get_mctrl,
  1114. .startup = mxs_auart_startup,
  1115. .shutdown = mxs_auart_shutdown,
  1116. .set_termios = mxs_auart_settermios,
  1117. .set_ldisc = mxs_auart_set_ldisc,
  1118. .type = mxs_auart_type,
  1119. .release_port = mxs_auart_release_port,
  1120. .request_port = mxs_auart_request_port,
  1121. .config_port = mxs_auart_config_port,
  1122. .verify_port = mxs_auart_verify_port,
  1123. };
  1124. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  1125. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  1126. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  1127. {
  1128. struct mxs_auart_port *s = to_auart_port(port);
  1129. unsigned int to = 1000;
  1130. while (mxs_read(s, REG_STAT) & AUART_STAT_TXFF) {
  1131. if (!to--)
  1132. break;
  1133. udelay(1);
  1134. }
  1135. mxs_write(ch, s, REG_DATA);
  1136. }
  1137. static void
  1138. auart_console_write(struct console *co, const char *str, unsigned int count)
  1139. {
  1140. struct mxs_auart_port *s;
  1141. struct uart_port *port;
  1142. unsigned int old_ctrl0, old_ctrl2;
  1143. unsigned int to = 20000;
  1144. if (co->index >= MXS_AUART_PORTS || co->index < 0)
  1145. return;
  1146. s = auart_port[co->index];
  1147. port = &s->port;
  1148. clk_enable(s->clk);
  1149. /* First save the CR then disable the interrupts */
  1150. old_ctrl2 = mxs_read(s, REG_CTRL2);
  1151. old_ctrl0 = mxs_read(s, REG_CTRL0);
  1152. mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
  1153. mxs_set(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE, s, REG_CTRL2);
  1154. uart_console_write(port, str, count, mxs_auart_console_putchar);
  1155. /* Finally, wait for transmitter to become empty ... */
  1156. while (mxs_read(s, REG_STAT) & AUART_STAT_BUSY) {
  1157. udelay(1);
  1158. if (!to--)
  1159. break;
  1160. }
  1161. /*
  1162. * ... and restore the TCR if we waited long enough for the transmitter
  1163. * to be idle. This might keep the transmitter enabled although it is
  1164. * unused, but that is better than to disable it while it is still
  1165. * transmitting.
  1166. */
  1167. if (!(mxs_read(s, REG_STAT) & AUART_STAT_BUSY)) {
  1168. mxs_write(old_ctrl0, s, REG_CTRL0);
  1169. mxs_write(old_ctrl2, s, REG_CTRL2);
  1170. }
  1171. clk_disable(s->clk);
  1172. }
  1173. static void __init
  1174. auart_console_get_options(struct mxs_auart_port *s, int *baud,
  1175. int *parity, int *bits)
  1176. {
  1177. struct uart_port *port = &s->port;
  1178. unsigned int lcr_h, quot;
  1179. if (!(mxs_read(s, REG_CTRL2) & AUART_CTRL2_UARTEN))
  1180. return;
  1181. lcr_h = mxs_read(s, REG_LINECTRL);
  1182. *parity = 'n';
  1183. if (lcr_h & AUART_LINECTRL_PEN) {
  1184. if (lcr_h & AUART_LINECTRL_EPS)
  1185. *parity = 'e';
  1186. else
  1187. *parity = 'o';
  1188. }
  1189. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  1190. *bits = 7;
  1191. else
  1192. *bits = 8;
  1193. quot = ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVINT_MASK))
  1194. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  1195. quot |= ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  1196. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  1197. if (quot == 0)
  1198. quot = 1;
  1199. *baud = (port->uartclk << 2) / quot;
  1200. }
  1201. static int __init
  1202. auart_console_setup(struct console *co, char *options)
  1203. {
  1204. struct mxs_auart_port *s;
  1205. int baud = 9600;
  1206. int bits = 8;
  1207. int parity = 'n';
  1208. int flow = 'n';
  1209. int ret;
  1210. /*
  1211. * Check whether an invalid uart number has been specified, and
  1212. * if so, search for the first available port that does have
  1213. * console support.
  1214. */
  1215. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  1216. co->index = 0;
  1217. s = auart_port[co->index];
  1218. if (!s)
  1219. return -ENODEV;
  1220. ret = clk_prepare_enable(s->clk);
  1221. if (ret)
  1222. return ret;
  1223. if (options)
  1224. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1225. else
  1226. auart_console_get_options(s, &baud, &parity, &bits);
  1227. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  1228. clk_disable_unprepare(s->clk);
  1229. return ret;
  1230. }
  1231. static struct console auart_console = {
  1232. .name = "ttyAPP",
  1233. .write = auart_console_write,
  1234. .device = uart_console_device,
  1235. .setup = auart_console_setup,
  1236. .flags = CON_PRINTBUFFER,
  1237. .index = -1,
  1238. .data = &auart_driver,
  1239. };
  1240. #endif
  1241. static struct uart_driver auart_driver = {
  1242. .owner = THIS_MODULE,
  1243. .driver_name = "ttyAPP",
  1244. .dev_name = "ttyAPP",
  1245. .major = 0,
  1246. .minor = 0,
  1247. .nr = MXS_AUART_PORTS,
  1248. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  1249. .cons = &auart_console,
  1250. #endif
  1251. };
  1252. static void mxs_init_regs(struct mxs_auart_port *s)
  1253. {
  1254. if (is_asm9260_auart(s))
  1255. s->vendor = &vendor_alphascale_asm9260;
  1256. else
  1257. s->vendor = &vendor_freescale_stmp37xx;
  1258. }
  1259. static int mxs_get_clks(struct mxs_auart_port *s,
  1260. struct platform_device *pdev)
  1261. {
  1262. int err;
  1263. if (!is_asm9260_auart(s)) {
  1264. s->clk = devm_clk_get(&pdev->dev, NULL);
  1265. return PTR_ERR_OR_ZERO(s->clk);
  1266. }
  1267. s->clk = devm_clk_get(s->dev, "mod");
  1268. if (IS_ERR(s->clk)) {
  1269. dev_err(s->dev, "Failed to get \"mod\" clk\n");
  1270. return PTR_ERR(s->clk);
  1271. }
  1272. s->clk_ahb = devm_clk_get(s->dev, "ahb");
  1273. if (IS_ERR(s->clk_ahb)) {
  1274. dev_err(s->dev, "Failed to get \"ahb\" clk\n");
  1275. return PTR_ERR(s->clk_ahb);
  1276. }
  1277. err = clk_prepare_enable(s->clk_ahb);
  1278. if (err) {
  1279. dev_err(s->dev, "Failed to enable ahb_clk!\n");
  1280. return err;
  1281. }
  1282. err = clk_set_rate(s->clk, clk_get_rate(s->clk_ahb));
  1283. if (err) {
  1284. dev_err(s->dev, "Failed to set rate!\n");
  1285. goto disable_clk_ahb;
  1286. }
  1287. err = clk_prepare_enable(s->clk);
  1288. if (err) {
  1289. dev_err(s->dev, "Failed to enable clk!\n");
  1290. goto disable_clk_ahb;
  1291. }
  1292. return 0;
  1293. disable_clk_ahb:
  1294. clk_disable_unprepare(s->clk_ahb);
  1295. return err;
  1296. }
  1297. /*
  1298. * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
  1299. * could successfully get all information from dt or a negative errno.
  1300. */
  1301. static int serial_mxs_probe_dt(struct mxs_auart_port *s,
  1302. struct platform_device *pdev)
  1303. {
  1304. struct device_node *np = pdev->dev.of_node;
  1305. int ret;
  1306. if (!np)
  1307. /* no device tree device */
  1308. return 1;
  1309. ret = of_alias_get_id(np, "serial");
  1310. if (ret < 0) {
  1311. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  1312. return ret;
  1313. }
  1314. s->port.line = ret;
  1315. if (of_get_property(np, "uart-has-rtscts", NULL) ||
  1316. of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
  1317. set_bit(MXS_AUART_RTSCTS, &s->flags);
  1318. return 0;
  1319. }
  1320. static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
  1321. {
  1322. enum mctrl_gpio_idx i;
  1323. struct gpio_desc *gpiod;
  1324. s->gpios = mctrl_gpio_init_noauto(dev, 0);
  1325. if (IS_ERR(s->gpios))
  1326. return PTR_ERR(s->gpios);
  1327. /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
  1328. if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
  1329. if (test_bit(MXS_AUART_RTSCTS, &s->flags))
  1330. dev_warn(dev,
  1331. "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
  1332. clear_bit(MXS_AUART_RTSCTS, &s->flags);
  1333. }
  1334. for (i = 0; i < UART_GPIO_MAX; i++) {
  1335. gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
  1336. if (gpiod && (gpiod_get_direction(gpiod) == 1))
  1337. s->gpio_irq[i] = gpiod_to_irq(gpiod);
  1338. else
  1339. s->gpio_irq[i] = -EINVAL;
  1340. }
  1341. return 0;
  1342. }
  1343. static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
  1344. {
  1345. enum mctrl_gpio_idx i;
  1346. for (i = 0; i < UART_GPIO_MAX; i++)
  1347. if (s->gpio_irq[i] >= 0)
  1348. free_irq(s->gpio_irq[i], s);
  1349. }
  1350. static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
  1351. {
  1352. int *irq = s->gpio_irq;
  1353. enum mctrl_gpio_idx i;
  1354. int err = 0;
  1355. for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
  1356. if (irq[i] < 0)
  1357. continue;
  1358. irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
  1359. err = request_irq(irq[i], mxs_auart_irq_handle,
  1360. IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
  1361. if (err)
  1362. dev_err(s->dev, "%s - Can't get %d irq\n",
  1363. __func__, irq[i]);
  1364. }
  1365. /*
  1366. * If something went wrong, rollback.
  1367. * Be careful: i may be unsigned.
  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 (s->port.line >= ARRAY_SIZE(auart_port)) {
  1393. dev_err(&pdev->dev, "serial%d out of range\n", s->port.line);
  1394. return -EINVAL;
  1395. }
  1396. if (of_id) {
  1397. pdev->id_entry = of_id->data;
  1398. s->devtype = pdev->id_entry->driver_data;
  1399. }
  1400. ret = mxs_get_clks(s, pdev);
  1401. if (ret)
  1402. return ret;
  1403. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1404. if (!r) {
  1405. ret = -ENXIO;
  1406. goto out_disable_clks;
  1407. }
  1408. s->port.mapbase = r->start;
  1409. s->port.membase = ioremap(r->start, resource_size(r));
  1410. if (!s->port.membase) {
  1411. ret = -ENOMEM;
  1412. goto out_disable_clks;
  1413. }
  1414. s->port.ops = &mxs_auart_ops;
  1415. s->port.iotype = UPIO_MEM;
  1416. s->port.fifosize = MXS_AUART_FIFO_SIZE;
  1417. s->port.uartclk = clk_get_rate(s->clk);
  1418. s->port.type = PORT_IMX;
  1419. mxs_init_regs(s);
  1420. s->mctrl_prev = 0;
  1421. irq = platform_get_irq(pdev, 0);
  1422. if (irq < 0) {
  1423. ret = irq;
  1424. goto out_iounmap;
  1425. }
  1426. s->port.irq = irq;
  1427. ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
  1428. dev_name(&pdev->dev), s);
  1429. if (ret)
  1430. goto out_iounmap;
  1431. platform_set_drvdata(pdev, s);
  1432. ret = mxs_auart_init_gpios(s, &pdev->dev);
  1433. if (ret) {
  1434. dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
  1435. goto out_iounmap;
  1436. }
  1437. /*
  1438. * Get the GPIO lines IRQ
  1439. */
  1440. ret = mxs_auart_request_gpio_irq(s);
  1441. if (ret)
  1442. goto out_iounmap;
  1443. auart_port[s->port.line] = s;
  1444. mxs_auart_reset_deassert(s);
  1445. ret = uart_add_one_port(&auart_driver, &s->port);
  1446. if (ret)
  1447. goto out_free_qpio_irq;
  1448. /* ASM9260 don't have version reg */
  1449. if (is_asm9260_auart(s)) {
  1450. dev_info(&pdev->dev, "Found APPUART ASM9260\n");
  1451. } else {
  1452. version = mxs_read(s, REG_VERSION);
  1453. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  1454. (version >> 24) & 0xff,
  1455. (version >> 16) & 0xff, version & 0xffff);
  1456. }
  1457. return 0;
  1458. out_free_qpio_irq:
  1459. mxs_auart_free_gpio_irq(s);
  1460. auart_port[pdev->id] = NULL;
  1461. out_iounmap:
  1462. iounmap(s->port.membase);
  1463. out_disable_clks:
  1464. if (is_asm9260_auart(s)) {
  1465. clk_disable_unprepare(s->clk);
  1466. clk_disable_unprepare(s->clk_ahb);
  1467. }
  1468. return ret;
  1469. }
  1470. static int mxs_auart_remove(struct platform_device *pdev)
  1471. {
  1472. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  1473. uart_remove_one_port(&auart_driver, &s->port);
  1474. auart_port[pdev->id] = NULL;
  1475. mxs_auart_free_gpio_irq(s);
  1476. iounmap(s->port.membase);
  1477. if (is_asm9260_auart(s)) {
  1478. clk_disable_unprepare(s->clk);
  1479. clk_disable_unprepare(s->clk_ahb);
  1480. }
  1481. return 0;
  1482. }
  1483. static struct platform_driver mxs_auart_driver = {
  1484. .probe = mxs_auart_probe,
  1485. .remove = mxs_auart_remove,
  1486. .driver = {
  1487. .name = "mxs-auart",
  1488. .of_match_table = mxs_auart_dt_ids,
  1489. },
  1490. };
  1491. static int __init mxs_auart_init(void)
  1492. {
  1493. int r;
  1494. r = uart_register_driver(&auart_driver);
  1495. if (r)
  1496. goto out;
  1497. r = platform_driver_register(&mxs_auart_driver);
  1498. if (r)
  1499. goto out_err;
  1500. return 0;
  1501. out_err:
  1502. uart_unregister_driver(&auart_driver);
  1503. out:
  1504. return r;
  1505. }
  1506. static void __exit mxs_auart_exit(void)
  1507. {
  1508. platform_driver_unregister(&mxs_auart_driver);
  1509. uart_unregister_driver(&auart_driver);
  1510. }
  1511. module_init(mxs_auart_init);
  1512. module_exit(mxs_auart_exit);
  1513. MODULE_LICENSE("GPL");
  1514. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  1515. MODULE_ALIAS("platform:mxs-auart");