mxs-auart.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  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 = 0;
  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 = DIV_ROUND_CLOSEST(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 (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. return -ENXIO;
  1406. s->port.mapbase = r->start;
  1407. s->port.membase = ioremap(r->start, resource_size(r));
  1408. s->port.ops = &mxs_auart_ops;
  1409. s->port.iotype = UPIO_MEM;
  1410. s->port.fifosize = MXS_AUART_FIFO_SIZE;
  1411. s->port.uartclk = clk_get_rate(s->clk);
  1412. s->port.type = PORT_IMX;
  1413. mxs_init_regs(s);
  1414. s->mctrl_prev = 0;
  1415. irq = platform_get_irq(pdev, 0);
  1416. if (irq < 0)
  1417. return irq;
  1418. s->port.irq = irq;
  1419. ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
  1420. dev_name(&pdev->dev), s);
  1421. if (ret)
  1422. return ret;
  1423. platform_set_drvdata(pdev, s);
  1424. ret = mxs_auart_init_gpios(s, &pdev->dev);
  1425. if (ret) {
  1426. dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
  1427. return ret;
  1428. }
  1429. /*
  1430. * Get the GPIO lines IRQ
  1431. */
  1432. ret = mxs_auart_request_gpio_irq(s);
  1433. if (ret)
  1434. return ret;
  1435. auart_port[s->port.line] = s;
  1436. mxs_auart_reset_deassert(s);
  1437. ret = uart_add_one_port(&auart_driver, &s->port);
  1438. if (ret)
  1439. goto out_free_gpio_irq;
  1440. /* ASM9260 don't have version reg */
  1441. if (is_asm9260_auart(s)) {
  1442. dev_info(&pdev->dev, "Found APPUART ASM9260\n");
  1443. } else {
  1444. version = mxs_read(s, REG_VERSION);
  1445. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  1446. (version >> 24) & 0xff,
  1447. (version >> 16) & 0xff, version & 0xffff);
  1448. }
  1449. return 0;
  1450. out_free_gpio_irq:
  1451. mxs_auart_free_gpio_irq(s);
  1452. auart_port[pdev->id] = NULL;
  1453. return ret;
  1454. }
  1455. static int mxs_auart_remove(struct platform_device *pdev)
  1456. {
  1457. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  1458. uart_remove_one_port(&auart_driver, &s->port);
  1459. auart_port[pdev->id] = NULL;
  1460. mxs_auart_free_gpio_irq(s);
  1461. return 0;
  1462. }
  1463. static struct platform_driver mxs_auart_driver = {
  1464. .probe = mxs_auart_probe,
  1465. .remove = mxs_auart_remove,
  1466. .driver = {
  1467. .name = "mxs-auart",
  1468. .of_match_table = mxs_auart_dt_ids,
  1469. },
  1470. };
  1471. static int __init mxs_auart_init(void)
  1472. {
  1473. int r;
  1474. r = uart_register_driver(&auart_driver);
  1475. if (r)
  1476. goto out;
  1477. r = platform_driver_register(&mxs_auart_driver);
  1478. if (r)
  1479. goto out_err;
  1480. return 0;
  1481. out_err:
  1482. uart_unregister_driver(&auart_driver);
  1483. out:
  1484. return r;
  1485. }
  1486. static void __exit mxs_auart_exit(void)
  1487. {
  1488. platform_driver_unregister(&mxs_auart_driver);
  1489. uart_unregister_driver(&auart_driver);
  1490. }
  1491. module_init(mxs_auart_init);
  1492. module_exit(mxs_auart_exit);
  1493. MODULE_LICENSE("GPL");
  1494. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  1495. MODULE_ALIAS("platform:mxs-auart");