stm32-usart.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Maxime Coquelin 2015
  4. * Copyright (C) STMicroelectronics SA 2017
  5. * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
  6. * Gerald Baeza <gerald_baeza@yahoo.fr>
  7. */
  8. #define DRIVER_NAME "stm32-usart"
  9. struct stm32_usart_offsets {
  10. u8 cr1;
  11. u8 cr2;
  12. u8 cr3;
  13. u8 brr;
  14. u8 gtpr;
  15. u8 rtor;
  16. u8 rqr;
  17. u8 isr;
  18. u8 icr;
  19. u8 rdr;
  20. u8 tdr;
  21. };
  22. struct stm32_usart_config {
  23. u8 uart_enable_bit; /* USART_CR1_UE */
  24. bool has_7bits_data;
  25. bool has_wakeup;
  26. bool has_fifo;
  27. int fifosize;
  28. };
  29. struct stm32_usart_info {
  30. struct stm32_usart_offsets ofs;
  31. struct stm32_usart_config cfg;
  32. };
  33. #define UNDEF_REG 0xff
  34. /* Register offsets */
  35. struct stm32_usart_info stm32f4_info = {
  36. .ofs = {
  37. .isr = 0x00,
  38. .rdr = 0x04,
  39. .tdr = 0x04,
  40. .brr = 0x08,
  41. .cr1 = 0x0c,
  42. .cr2 = 0x10,
  43. .cr3 = 0x14,
  44. .gtpr = 0x18,
  45. .rtor = UNDEF_REG,
  46. .rqr = UNDEF_REG,
  47. .icr = UNDEF_REG,
  48. },
  49. .cfg = {
  50. .uart_enable_bit = 13,
  51. .has_7bits_data = false,
  52. .fifosize = 1,
  53. }
  54. };
  55. struct stm32_usart_info stm32f7_info = {
  56. .ofs = {
  57. .cr1 = 0x00,
  58. .cr2 = 0x04,
  59. .cr3 = 0x08,
  60. .brr = 0x0c,
  61. .gtpr = 0x10,
  62. .rtor = 0x14,
  63. .rqr = 0x18,
  64. .isr = 0x1c,
  65. .icr = 0x20,
  66. .rdr = 0x24,
  67. .tdr = 0x28,
  68. },
  69. .cfg = {
  70. .uart_enable_bit = 0,
  71. .has_7bits_data = true,
  72. .fifosize = 1,
  73. }
  74. };
  75. struct stm32_usart_info stm32h7_info = {
  76. .ofs = {
  77. .cr1 = 0x00,
  78. .cr2 = 0x04,
  79. .cr3 = 0x08,
  80. .brr = 0x0c,
  81. .gtpr = 0x10,
  82. .rtor = 0x14,
  83. .rqr = 0x18,
  84. .isr = 0x1c,
  85. .icr = 0x20,
  86. .rdr = 0x24,
  87. .tdr = 0x28,
  88. },
  89. .cfg = {
  90. .uart_enable_bit = 0,
  91. .has_7bits_data = true,
  92. .has_wakeup = true,
  93. .has_fifo = true,
  94. .fifosize = 16,
  95. }
  96. };
  97. /* USART_SR (F4) / USART_ISR (F7) */
  98. #define USART_SR_PE BIT(0)
  99. #define USART_SR_FE BIT(1)
  100. #define USART_SR_NF BIT(2)
  101. #define USART_SR_ORE BIT(3)
  102. #define USART_SR_IDLE BIT(4)
  103. #define USART_SR_RXNE BIT(5)
  104. #define USART_SR_TC BIT(6)
  105. #define USART_SR_TXE BIT(7)
  106. #define USART_SR_CTSIF BIT(9)
  107. #define USART_SR_CTS BIT(10) /* F7 */
  108. #define USART_SR_RTOF BIT(11) /* F7 */
  109. #define USART_SR_EOBF BIT(12) /* F7 */
  110. #define USART_SR_ABRE BIT(14) /* F7 */
  111. #define USART_SR_ABRF BIT(15) /* F7 */
  112. #define USART_SR_BUSY BIT(16) /* F7 */
  113. #define USART_SR_CMF BIT(17) /* F7 */
  114. #define USART_SR_SBKF BIT(18) /* F7 */
  115. #define USART_SR_WUF BIT(20) /* H7 */
  116. #define USART_SR_TEACK BIT(21) /* F7 */
  117. #define USART_SR_ERR_MASK (USART_SR_ORE | USART_SR_FE | USART_SR_PE)
  118. /* Dummy bits */
  119. #define USART_SR_DUMMY_RX BIT(16)
  120. /* USART_DR */
  121. #define USART_DR_MASK GENMASK(8, 0)
  122. /* USART_BRR */
  123. #define USART_BRR_DIV_F_MASK GENMASK(3, 0)
  124. #define USART_BRR_DIV_M_MASK GENMASK(15, 4)
  125. #define USART_BRR_DIV_M_SHIFT 4
  126. #define USART_BRR_04_R_SHIFT 1
  127. /* USART_CR1 */
  128. #define USART_CR1_SBK BIT(0)
  129. #define USART_CR1_RWU BIT(1) /* F4 */
  130. #define USART_CR1_UESM BIT(1) /* H7 */
  131. #define USART_CR1_RE BIT(2)
  132. #define USART_CR1_TE BIT(3)
  133. #define USART_CR1_IDLEIE BIT(4)
  134. #define USART_CR1_RXNEIE BIT(5)
  135. #define USART_CR1_TCIE BIT(6)
  136. #define USART_CR1_TXEIE BIT(7)
  137. #define USART_CR1_PEIE BIT(8)
  138. #define USART_CR1_PS BIT(9)
  139. #define USART_CR1_PCE BIT(10)
  140. #define USART_CR1_WAKE BIT(11)
  141. #define USART_CR1_M0 BIT(12) /* F7 (CR1_M for F4) */
  142. #define USART_CR1_MME BIT(13) /* F7 */
  143. #define USART_CR1_CMIE BIT(14) /* F7 */
  144. #define USART_CR1_OVER8 BIT(15)
  145. #define USART_CR1_DEDT_MASK GENMASK(20, 16) /* F7 */
  146. #define USART_CR1_DEAT_MASK GENMASK(25, 21) /* F7 */
  147. #define USART_CR1_RTOIE BIT(26) /* F7 */
  148. #define USART_CR1_EOBIE BIT(27) /* F7 */
  149. #define USART_CR1_M1 BIT(28) /* F7 */
  150. #define USART_CR1_IE_MASK (GENMASK(8, 4) | BIT(14) | BIT(26) | BIT(27))
  151. #define USART_CR1_FIFOEN BIT(29) /* H7 */
  152. #define USART_CR1_DEAT_SHIFT 21
  153. #define USART_CR1_DEDT_SHIFT 16
  154. /* USART_CR2 */
  155. #define USART_CR2_ADD_MASK GENMASK(3, 0) /* F4 */
  156. #define USART_CR2_ADDM7 BIT(4) /* F7 */
  157. #define USART_CR2_LBCL BIT(8)
  158. #define USART_CR2_CPHA BIT(9)
  159. #define USART_CR2_CPOL BIT(10)
  160. #define USART_CR2_CLKEN BIT(11)
  161. #define USART_CR2_STOP_2B BIT(13)
  162. #define USART_CR2_STOP_MASK GENMASK(13, 12)
  163. #define USART_CR2_LINEN BIT(14)
  164. #define USART_CR2_SWAP BIT(15) /* F7 */
  165. #define USART_CR2_RXINV BIT(16) /* F7 */
  166. #define USART_CR2_TXINV BIT(17) /* F7 */
  167. #define USART_CR2_DATAINV BIT(18) /* F7 */
  168. #define USART_CR2_MSBFIRST BIT(19) /* F7 */
  169. #define USART_CR2_ABREN BIT(20) /* F7 */
  170. #define USART_CR2_ABRMOD_MASK GENMASK(22, 21) /* F7 */
  171. #define USART_CR2_RTOEN BIT(23) /* F7 */
  172. #define USART_CR2_ADD_F7_MASK GENMASK(31, 24) /* F7 */
  173. /* USART_CR3 */
  174. #define USART_CR3_EIE BIT(0)
  175. #define USART_CR3_IREN BIT(1)
  176. #define USART_CR3_IRLP BIT(2)
  177. #define USART_CR3_HDSEL BIT(3)
  178. #define USART_CR3_NACK BIT(4)
  179. #define USART_CR3_SCEN BIT(5)
  180. #define USART_CR3_DMAR BIT(6)
  181. #define USART_CR3_DMAT BIT(7)
  182. #define USART_CR3_RTSE BIT(8)
  183. #define USART_CR3_CTSE BIT(9)
  184. #define USART_CR3_CTSIE BIT(10)
  185. #define USART_CR3_ONEBIT BIT(11)
  186. #define USART_CR3_OVRDIS BIT(12) /* F7 */
  187. #define USART_CR3_DDRE BIT(13) /* F7 */
  188. #define USART_CR3_DEM BIT(14) /* F7 */
  189. #define USART_CR3_DEP BIT(15) /* F7 */
  190. #define USART_CR3_SCARCNT_MASK GENMASK(19, 17) /* F7 */
  191. #define USART_CR3_WUS_MASK GENMASK(21, 20) /* H7 */
  192. #define USART_CR3_WUS_START_BIT BIT(21) /* H7 */
  193. #define USART_CR3_WUFIE BIT(22) /* H7 */
  194. #define USART_CR3_TXFTIE BIT(23) /* H7 */
  195. #define USART_CR3_TCBGTIE BIT(24) /* H7 */
  196. #define USART_CR3_RXFTCFG_MASK GENMASK(27, 25) /* H7 */
  197. #define USART_CR3_RXFTCFG_SHIFT 25 /* H7 */
  198. #define USART_CR3_RXFTIE BIT(28) /* H7 */
  199. #define USART_CR3_TXFTCFG_MASK GENMASK(31, 29) /* H7 */
  200. #define USART_CR3_TXFTCFG_SHIFT 29 /* H7 */
  201. /* TX FIFO threashold set to half of its depth */
  202. #define USART_CR3_TXFTCFG_HALF 0x2
  203. /* RX FIFO threashold set to half of its depth */
  204. #define USART_CR3_RXFTCFG_HALF 0x2
  205. /* USART_GTPR */
  206. #define USART_GTPR_PSC_MASK GENMASK(7, 0)
  207. #define USART_GTPR_GT_MASK GENMASK(15, 8)
  208. /* USART_RTOR */
  209. #define USART_RTOR_RTO_MASK GENMASK(23, 0) /* F7 */
  210. #define USART_RTOR_BLEN_MASK GENMASK(31, 24) /* F7 */
  211. /* USART_RQR */
  212. #define USART_RQR_ABRRQ BIT(0) /* F7 */
  213. #define USART_RQR_SBKRQ BIT(1) /* F7 */
  214. #define USART_RQR_MMRQ BIT(2) /* F7 */
  215. #define USART_RQR_RXFRQ BIT(3) /* F7 */
  216. #define USART_RQR_TXFRQ BIT(4) /* F7 */
  217. /* USART_ICR */
  218. #define USART_ICR_PECF BIT(0) /* F7 */
  219. #define USART_ICR_FECF BIT(1) /* F7 */
  220. #define USART_ICR_ORECF BIT(3) /* F7 */
  221. #define USART_ICR_IDLECF BIT(4) /* F7 */
  222. #define USART_ICR_TCCF BIT(6) /* F7 */
  223. #define USART_ICR_CTSCF BIT(9) /* F7 */
  224. #define USART_ICR_RTOCF BIT(11) /* F7 */
  225. #define USART_ICR_EOBCF BIT(12) /* F7 */
  226. #define USART_ICR_CMCF BIT(17) /* F7 */
  227. #define USART_ICR_WUCF BIT(20) /* H7 */
  228. #define STM32_SERIAL_NAME "ttySTM"
  229. #define STM32_MAX_PORTS 8
  230. #define RX_BUF_L 200 /* dma rx buffer length */
  231. #define RX_BUF_P RX_BUF_L /* dma rx buffer period */
  232. #define TX_BUF_L 200 /* dma tx buffer length */
  233. struct stm32_port {
  234. struct uart_port port;
  235. struct clk *clk;
  236. struct stm32_usart_info *info;
  237. struct dma_chan *rx_ch; /* dma rx channel */
  238. dma_addr_t rx_dma_buf; /* dma rx buffer bus address */
  239. unsigned char *rx_buf; /* dma rx buffer cpu address */
  240. struct dma_chan *tx_ch; /* dma tx channel */
  241. dma_addr_t tx_dma_buf; /* dma tx buffer bus address */
  242. unsigned char *tx_buf; /* dma tx buffer cpu address */
  243. u32 cr1_irq; /* USART_CR1_RXNEIE or RTOIE */
  244. u32 cr3_irq; /* USART_CR3_RXFTIE */
  245. int last_res;
  246. bool tx_dma_busy; /* dma tx busy */
  247. bool hw_flow_control;
  248. bool fifoen;
  249. int wakeirq;
  250. int rdr_mask; /* receive data register mask */
  251. };
  252. static struct stm32_port stm32_ports[STM32_MAX_PORTS];
  253. static struct uart_driver stm32_usart_driver;