blob: e7ffd01669f51f0abb882cba2a73b11972eaefe0 [file] [log] [blame]
Maxime Coquelin48a60922015-06-10 21:19:36 +02001/*
2 * Copyright (C) Maxime Coquelin 2015
Alexandre TORGUEada86182016-09-15 18:42:33 +02003 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * Gerald Baeza <gerald.baeza@st.com>
Maxime Coquelin48a60922015-06-10 21:19:36 +02005 * License terms: GNU General Public License (GPL), version 2
6 *
7 * Inspired by st-asc.c from STMicroelectronics (c)
8 */
9
Maxime Coquelin6b596a82015-06-16 11:12:19 +020010#if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Maxime Coquelin48a60922015-06-10 21:19:36 +020011#define SUPPORT_SYSRQ
12#endif
13
14#include <linux/module.h>
15#include <linux/serial.h>
16#include <linux/console.h>
17#include <linux/sysrq.h>
18#include <linux/platform_device.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/delay.h>
24#include <linux/spinlock.h>
25#include <linux/pm_runtime.h>
26#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/serial_core.h>
29#include <linux/clk.h>
30
31#define DRIVER_NAME "stm32-usart"
32
Alexandre TORGUEada86182016-09-15 18:42:33 +020033struct stm32_usart_offsets {
34 u8 cr1;
35 u8 cr2;
36 u8 cr3;
37 u8 brr;
38 u8 gtpr;
39 u8 rtor;
40 u8 rqr;
41 u8 isr;
42 u8 icr;
43 u8 rdr;
44 u8 tdr;
45};
Maxime Coquelin48a60922015-06-10 21:19:36 +020046
Alexandre TORGUEada86182016-09-15 18:42:33 +020047struct stm32_usart_config {
48 u8 uart_enable_bit; /* USART_CR1_UE */
49 bool has_7bits_data;
50};
51
52struct stm32_usart_info {
53 struct stm32_usart_offsets ofs;
54 struct stm32_usart_config cfg;
55};
56
57#define UNDEF_REG ~0
58
59/* Register offsets */
60struct stm32_usart_info stm32f4_info = {
61 .ofs = {
62 .isr = 0x00,
63 .rdr = 0x04,
64 .tdr = 0x04,
65 .brr = 0x08,
66 .cr1 = 0x0c,
67 .cr2 = 0x10,
68 .cr3 = 0x14,
69 .gtpr = 0x18,
70 .rtor = UNDEF_REG,
71 .rqr = UNDEF_REG,
72 .icr = UNDEF_REG,
73 },
74 .cfg = {
75 .uart_enable_bit = 13,
76 .has_7bits_data = false,
77 }
78};
79
80struct stm32_usart_info stm32f7_info = {
81 .ofs = {
82 .cr1 = 0x00,
83 .cr2 = 0x04,
84 .cr3 = 0x08,
85 .brr = 0x0c,
86 .gtpr = 0x10,
87 .rtor = 0x14,
88 .rqr = 0x18,
89 .isr = 0x1c,
90 .icr = 0x20,
91 .rdr = 0x24,
92 .tdr = 0x28,
93 },
94 .cfg = {
95 .uart_enable_bit = 0,
96 .has_7bits_data = true,
97 }
98};
99
100/* USART_SR (F4) / USART_ISR (F7) */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200101#define USART_SR_PE BIT(0)
102#define USART_SR_FE BIT(1)
103#define USART_SR_NF BIT(2)
104#define USART_SR_ORE BIT(3)
105#define USART_SR_IDLE BIT(4)
106#define USART_SR_RXNE BIT(5)
107#define USART_SR_TC BIT(6)
108#define USART_SR_TXE BIT(7)
109#define USART_SR_LBD BIT(8)
Alexandre TORGUEada86182016-09-15 18:42:33 +0200110#define USART_SR_CTSIF BIT(9)
111#define USART_SR_CTS BIT(10) /* F7 */
112#define USART_SR_RTOF BIT(11) /* F7 */
113#define USART_SR_EOBF BIT(12) /* F7 */
114#define USART_SR_ABRE BIT(14) /* F7 */
115#define USART_SR_ABRF BIT(15) /* F7 */
116#define USART_SR_BUSY BIT(16) /* F7 */
117#define USART_SR_CMF BIT(17) /* F7 */
118#define USART_SR_SBKF BIT(18) /* F7 */
119#define USART_SR_TEACK BIT(21) /* F7 */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200120#define USART_SR_ERR_MASK (USART_SR_LBD | USART_SR_ORE | \
121 USART_SR_FE | USART_SR_PE)
122/* Dummy bits */
123#define USART_SR_DUMMY_RX BIT(16)
124
125/* USART_DR */
126#define USART_DR_MASK GENMASK(8, 0)
127
128/* USART_BRR */
129#define USART_BRR_DIV_F_MASK GENMASK(3, 0)
130#define USART_BRR_DIV_M_MASK GENMASK(15, 4)
131#define USART_BRR_DIV_M_SHIFT 4
132
133/* USART_CR1 */
134#define USART_CR1_SBK BIT(0)
Alexandre TORGUEada86182016-09-15 18:42:33 +0200135#define USART_CR1_RWU BIT(1) /* F4 */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200136#define USART_CR1_RE BIT(2)
137#define USART_CR1_TE BIT(3)
138#define USART_CR1_IDLEIE BIT(4)
139#define USART_CR1_RXNEIE BIT(5)
140#define USART_CR1_TCIE BIT(6)
141#define USART_CR1_TXEIE BIT(7)
142#define USART_CR1_PEIE BIT(8)
143#define USART_CR1_PS BIT(9)
144#define USART_CR1_PCE BIT(10)
145#define USART_CR1_WAKE BIT(11)
146#define USART_CR1_M BIT(12)
Alexandre TORGUEada86182016-09-15 18:42:33 +0200147#define USART_CR1_M0 BIT(12) /* F7 */
148#define USART_CR1_MME BIT(13) /* F7 */
149#define USART_CR1_CMIE BIT(14) /* F7 */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200150#define USART_CR1_OVER8 BIT(15)
Alexandre TORGUEada86182016-09-15 18:42:33 +0200151#define USART_CR1_DEDT_MASK GENMASK(20, 16) /* F7 */
152#define USART_CR1_DEAT_MASK GENMASK(25, 21) /* F7 */
153#define USART_CR1_RTOIE BIT(26) /* F7 */
154#define USART_CR1_EOBIE BIT(27) /* F7 */
155#define USART_CR1_M1 BIT(28) /* F7 */
156#define USART_CR1_IE_MASK (GENMASK(8, 4) | BIT(14) | BIT(26) | BIT(27))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200157
158/* USART_CR2 */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200159#define USART_CR2_ADD_MASK GENMASK(3, 0) /* F4 */
160#define USART_CR2_ADDM7 BIT(4) /* F7 */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200161#define USART_CR2_LBDL BIT(5)
162#define USART_CR2_LBDIE BIT(6)
163#define USART_CR2_LBCL BIT(8)
164#define USART_CR2_CPHA BIT(9)
165#define USART_CR2_CPOL BIT(10)
166#define USART_CR2_CLKEN BIT(11)
167#define USART_CR2_STOP_2B BIT(13)
168#define USART_CR2_STOP_MASK GENMASK(13, 12)
169#define USART_CR2_LINEN BIT(14)
Alexandre TORGUEada86182016-09-15 18:42:33 +0200170#define USART_CR2_SWAP BIT(15) /* F7 */
171#define USART_CR2_RXINV BIT(16) /* F7 */
172#define USART_CR2_TXINV BIT(17) /* F7 */
173#define USART_CR2_DATAINV BIT(18) /* F7 */
174#define USART_CR2_MSBFIRST BIT(19) /* F7 */
175#define USART_CR2_ABREN BIT(20) /* F7 */
176#define USART_CR2_ABRMOD_MASK GENMASK(22, 21) /* F7 */
177#define USART_CR2_RTOEN BIT(23) /* F7 */
178#define USART_CR2_ADD_F7_MASK GENMASK(31, 24) /* F7 */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200179
180/* USART_CR3 */
181#define USART_CR3_EIE BIT(0)
182#define USART_CR3_IREN BIT(1)
183#define USART_CR3_IRLP BIT(2)
184#define USART_CR3_HDSEL BIT(3)
185#define USART_CR3_NACK BIT(4)
186#define USART_CR3_SCEN BIT(5)
187#define USART_CR3_DMAR BIT(6)
188#define USART_CR3_DMAT BIT(7)
189#define USART_CR3_RTSE BIT(8)
190#define USART_CR3_CTSE BIT(9)
191#define USART_CR3_CTSIE BIT(10)
192#define USART_CR3_ONEBIT BIT(11)
Alexandre TORGUEada86182016-09-15 18:42:33 +0200193#define USART_CR3_OVRDIS BIT(12) /* F7 */
194#define USART_CR3_DDRE BIT(13) /* F7 */
195#define USART_CR3_DEM BIT(14) /* F7 */
196#define USART_CR3_DEP BIT(15) /* F7 */
197#define USART_CR3_SCARCNT_MASK GENMASK(19, 17) /* F7 */
Maxime Coquelin48a60922015-06-10 21:19:36 +0200198
199/* USART_GTPR */
200#define USART_GTPR_PSC_MASK GENMASK(7, 0)
201#define USART_GTPR_GT_MASK GENMASK(15, 8)
202
Alexandre TORGUEada86182016-09-15 18:42:33 +0200203/* USART_RTOR */
204#define USART_RTOR_RTO_MASK GENMASK(23, 0) /* F7 */
205#define USART_RTOR_BLEN_MASK GENMASK(31, 24) /* F7 */
206
207/* USART_RQR */
208#define USART_RQR_ABRRQ BIT(0) /* F7 */
209#define USART_RQR_SBKRQ BIT(1) /* F7 */
210#define USART_RQR_MMRQ BIT(2) /* F7 */
211#define USART_RQR_RXFRQ BIT(3) /* F7 */
212#define USART_RQR_TXFRQ BIT(4) /* F7 */
213
214/* USART_ICR */
215#define USART_ICR_PECF BIT(0) /* F7 */
216#define USART_ICR_FFECF BIT(1) /* F7 */
217#define USART_ICR_NCF BIT(2) /* F7 */
218#define USART_ICR_ORECF BIT(3) /* F7 */
219#define USART_ICR_IDLECF BIT(4) /* F7 */
220#define USART_ICR_TCCF BIT(6) /* F7 */
221#define USART_ICR_LBDCF BIT(8) /* F7 */
222#define USART_ICR_CTSCF BIT(9) /* F7 */
223#define USART_ICR_RTOCF BIT(11) /* F7 */
224#define USART_ICR_EOBCF BIT(12) /* F7 */
225#define USART_ICR_CMCF BIT(17) /* F7 */
226
Maxime Coquelin48a60922015-06-10 21:19:36 +0200227#define STM32_SERIAL_NAME "ttyS"
228#define STM32_MAX_PORTS 6
229
230struct stm32_port {
231 struct uart_port port;
232 struct clk *clk;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200233 struct stm32_usart_info *info;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200234 bool hw_flow_control;
235};
236
237static struct stm32_port stm32_ports[STM32_MAX_PORTS];
238static struct uart_driver stm32_usart_driver;
239
240static void stm32_stop_tx(struct uart_port *port);
241
242static inline struct stm32_port *to_stm32_port(struct uart_port *port)
243{
244 return container_of(port, struct stm32_port, port);
245}
246
247static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
248{
249 u32 val;
250
251 val = readl_relaxed(port->membase + reg);
252 val |= bits;
253 writel_relaxed(val, port->membase + reg);
254}
255
256static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
257{
258 u32 val;
259
260 val = readl_relaxed(port->membase + reg);
261 val &= ~bits;
262 writel_relaxed(val, port->membase + reg);
263}
264
265static void stm32_receive_chars(struct uart_port *port)
266{
267 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200268 struct stm32_port *stm32_port = to_stm32_port(port);
269 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200270 unsigned long c;
271 u32 sr;
272 char flag;
273
274 if (port->irq_wake)
275 pm_wakeup_event(tport->tty->dev, 0);
276
Alexandre TORGUEada86182016-09-15 18:42:33 +0200277 while ((sr = readl_relaxed(port->membase + ofs->isr)) & USART_SR_RXNE) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200278 sr |= USART_SR_DUMMY_RX;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200279 c = readl_relaxed(port->membase + ofs->rdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200280 flag = TTY_NORMAL;
281 port->icount.rx++;
282
283 if (sr & USART_SR_ERR_MASK) {
284 if (sr & USART_SR_LBD) {
285 port->icount.brk++;
286 if (uart_handle_break(port))
287 continue;
288 } else if (sr & USART_SR_ORE) {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200289 if (ofs->icr != UNDEF_REG)
290 writel_relaxed(USART_ICR_ORECF,
291 port->membase +
292 ofs->icr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200293 port->icount.overrun++;
294 } else if (sr & USART_SR_PE) {
295 port->icount.parity++;
296 } else if (sr & USART_SR_FE) {
297 port->icount.frame++;
298 }
299
300 sr &= port->read_status_mask;
301
302 if (sr & USART_SR_LBD)
303 flag = TTY_BREAK;
304 else if (sr & USART_SR_PE)
305 flag = TTY_PARITY;
306 else if (sr & USART_SR_FE)
307 flag = TTY_FRAME;
308 }
309
310 if (uart_handle_sysrq_char(port, c))
311 continue;
312 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
313 }
314
315 spin_unlock(&port->lock);
316 tty_flip_buffer_push(tport);
317 spin_lock(&port->lock);
318}
319
320static void stm32_transmit_chars(struct uart_port *port)
321{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200322 struct stm32_port *stm32_port = to_stm32_port(port);
323 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200324 struct circ_buf *xmit = &port->state->xmit;
325
326 if (port->x_char) {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200327 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200328 port->x_char = 0;
329 port->icount.tx++;
330 return;
331 }
332
333 if (uart_tx_stopped(port)) {
334 stm32_stop_tx(port);
335 return;
336 }
337
338 if (uart_circ_empty(xmit)) {
339 stm32_stop_tx(port);
340 return;
341 }
342
Alexandre TORGUEada86182016-09-15 18:42:33 +0200343 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200344 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
345 port->icount.tx++;
346
347 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
348 uart_write_wakeup(port);
349
350 if (uart_circ_empty(xmit))
351 stm32_stop_tx(port);
352}
353
354static irqreturn_t stm32_interrupt(int irq, void *ptr)
355{
356 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200357 struct stm32_port *stm32_port = to_stm32_port(port);
358 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200359 u32 sr;
360
361 spin_lock(&port->lock);
362
Alexandre TORGUEada86182016-09-15 18:42:33 +0200363 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200364
365 if (sr & USART_SR_RXNE)
366 stm32_receive_chars(port);
367
368 if (sr & USART_SR_TXE)
369 stm32_transmit_chars(port);
370
371 spin_unlock(&port->lock);
372
373 return IRQ_HANDLED;
374}
375
376static unsigned int stm32_tx_empty(struct uart_port *port)
377{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200378 struct stm32_port *stm32_port = to_stm32_port(port);
379 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
380
381 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200382}
383
384static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
385{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200386 struct stm32_port *stm32_port = to_stm32_port(port);
387 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
388
Maxime Coquelin48a60922015-06-10 21:19:36 +0200389 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Alexandre TORGUEada86182016-09-15 18:42:33 +0200390 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200391 else
Alexandre TORGUEada86182016-09-15 18:42:33 +0200392 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200393}
394
395static unsigned int stm32_get_mctrl(struct uart_port *port)
396{
397 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
398 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
399}
400
401/* Transmit stop */
402static void stm32_stop_tx(struct uart_port *port)
403{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200404 struct stm32_port *stm32_port = to_stm32_port(port);
405 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
406
407 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200408}
409
410/* There are probably characters waiting to be transmitted. */
411static void stm32_start_tx(struct uart_port *port)
412{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200413 struct stm32_port *stm32_port = to_stm32_port(port);
414 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200415 struct circ_buf *xmit = &port->state->xmit;
416
417 if (uart_circ_empty(xmit))
418 return;
419
Alexandre TORGUEada86182016-09-15 18:42:33 +0200420 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE | USART_CR1_TE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200421}
422
423/* Throttle the remote when input buffer is about to overflow. */
424static void stm32_throttle(struct uart_port *port)
425{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200426 struct stm32_port *stm32_port = to_stm32_port(port);
427 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200428 unsigned long flags;
429
430 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200431 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200432 spin_unlock_irqrestore(&port->lock, flags);
433}
434
435/* Unthrottle the remote, the input buffer can now accept data. */
436static void stm32_unthrottle(struct uart_port *port)
437{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200438 struct stm32_port *stm32_port = to_stm32_port(port);
439 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200440 unsigned long flags;
441
442 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200443 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200444 spin_unlock_irqrestore(&port->lock, flags);
445}
446
447/* Receive stop */
448static void stm32_stop_rx(struct uart_port *port)
449{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200450 struct stm32_port *stm32_port = to_stm32_port(port);
451 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
452
453 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200454}
455
456/* Handle breaks - ignored by us */
457static void stm32_break_ctl(struct uart_port *port, int break_state)
458{
459}
460
461static int stm32_startup(struct uart_port *port)
462{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200463 struct stm32_port *stm32_port = to_stm32_port(port);
464 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200465 const char *name = to_platform_device(port->dev)->name;
466 u32 val;
467 int ret;
468
Sudeep Holla616ea8d2015-09-21 16:47:06 +0100469 ret = request_irq(port->irq, stm32_interrupt, 0, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200470 if (ret)
471 return ret;
472
473 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200474 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200475
476 return 0;
477}
478
479static void stm32_shutdown(struct uart_port *port)
480{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200481 struct stm32_port *stm32_port = to_stm32_port(port);
482 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200483 u32 val;
484
485 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200486 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200487
488 free_irq(port->irq, port);
489}
490
491static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
492 struct ktermios *old)
493{
494 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200495 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
496 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200497 unsigned int baud;
498 u32 usartdiv, mantissa, fraction, oversampling;
499 tcflag_t cflag = termios->c_cflag;
500 u32 cr1, cr2, cr3;
501 unsigned long flags;
502
503 if (!stm32_port->hw_flow_control)
504 cflag &= ~CRTSCTS;
505
506 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
507
508 spin_lock_irqsave(&port->lock, flags);
509
510 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200511 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200512
Alexandre TORGUEada86182016-09-15 18:42:33 +0200513 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
514 cr1 |= BIT(cfg->uart_enable_bit);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200515 cr2 = 0;
516 cr3 = 0;
517
518 if (cflag & CSTOPB)
519 cr2 |= USART_CR2_STOP_2B;
520
521 if (cflag & PARENB) {
522 cr1 |= USART_CR1_PCE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200523 if ((cflag & CSIZE) == CS8) {
524 if (cfg->has_7bits_data)
525 cr1 |= USART_CR1_M0;
526 else
527 cr1 |= USART_CR1_M;
528 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200529 }
530
531 if (cflag & PARODD)
532 cr1 |= USART_CR1_PS;
533
534 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
535 if (cflag & CRTSCTS) {
536 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
537 cr3 |= USART_CR3_CTSE;
538 }
539
540 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
541
542 /*
543 * The USART supports 16 or 8 times oversampling.
544 * By default we prefer 16 times oversampling, so that the receiver
545 * has a better tolerance to clock deviations.
546 * 8 times oversampling is only used to achieve higher speeds.
547 */
548 if (usartdiv < 16) {
549 oversampling = 8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200550 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200551 } else {
552 oversampling = 16;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200553 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200554 }
555
556 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
557 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200558 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200559
560 uart_update_timeout(port, cflag, baud);
561
562 port->read_status_mask = USART_SR_ORE;
563 if (termios->c_iflag & INPCK)
564 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
565 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
566 port->read_status_mask |= USART_SR_LBD;
567
568 /* Characters to ignore */
569 port->ignore_status_mask = 0;
570 if (termios->c_iflag & IGNPAR)
571 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
572 if (termios->c_iflag & IGNBRK) {
573 port->ignore_status_mask |= USART_SR_LBD;
574 /*
575 * If we're ignoring parity and break indicators,
576 * ignore overruns too (for real raw support).
577 */
578 if (termios->c_iflag & IGNPAR)
579 port->ignore_status_mask |= USART_SR_ORE;
580 }
581
582 /* Ignore all characters if CREAD is not set */
583 if ((termios->c_cflag & CREAD) == 0)
584 port->ignore_status_mask |= USART_SR_DUMMY_RX;
585
Alexandre TORGUEada86182016-09-15 18:42:33 +0200586 writel_relaxed(cr3, port->membase + ofs->cr3);
587 writel_relaxed(cr2, port->membase + ofs->cr2);
588 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200589
590 spin_unlock_irqrestore(&port->lock, flags);
591}
592
593static const char *stm32_type(struct uart_port *port)
594{
595 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
596}
597
598static void stm32_release_port(struct uart_port *port)
599{
600}
601
602static int stm32_request_port(struct uart_port *port)
603{
604 return 0;
605}
606
607static void stm32_config_port(struct uart_port *port, int flags)
608{
609 if (flags & UART_CONFIG_TYPE)
610 port->type = PORT_STM32;
611}
612
613static int
614stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
615{
616 /* No user changeable parameters */
617 return -EINVAL;
618}
619
620static void stm32_pm(struct uart_port *port, unsigned int state,
621 unsigned int oldstate)
622{
623 struct stm32_port *stm32port = container_of(port,
624 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200625 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
626 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200627 unsigned long flags = 0;
628
629 switch (state) {
630 case UART_PM_STATE_ON:
631 clk_prepare_enable(stm32port->clk);
632 break;
633 case UART_PM_STATE_OFF:
634 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200635 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200636 spin_unlock_irqrestore(&port->lock, flags);
637 clk_disable_unprepare(stm32port->clk);
638 break;
639 }
640}
641
642static const struct uart_ops stm32_uart_ops = {
643 .tx_empty = stm32_tx_empty,
644 .set_mctrl = stm32_set_mctrl,
645 .get_mctrl = stm32_get_mctrl,
646 .stop_tx = stm32_stop_tx,
647 .start_tx = stm32_start_tx,
648 .throttle = stm32_throttle,
649 .unthrottle = stm32_unthrottle,
650 .stop_rx = stm32_stop_rx,
651 .break_ctl = stm32_break_ctl,
652 .startup = stm32_startup,
653 .shutdown = stm32_shutdown,
654 .set_termios = stm32_set_termios,
655 .pm = stm32_pm,
656 .type = stm32_type,
657 .release_port = stm32_release_port,
658 .request_port = stm32_request_port,
659 .config_port = stm32_config_port,
660 .verify_port = stm32_verify_port,
661};
662
663static int stm32_init_port(struct stm32_port *stm32port,
664 struct platform_device *pdev)
665{
666 struct uart_port *port = &stm32port->port;
667 struct resource *res;
668 int ret;
669
670 port->iotype = UPIO_MEM;
671 port->flags = UPF_BOOT_AUTOCONF;
672 port->ops = &stm32_uart_ops;
673 port->dev = &pdev->dev;
674 port->irq = platform_get_irq(pdev, 0);
675
676 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
677 port->membase = devm_ioremap_resource(&pdev->dev, res);
678 if (IS_ERR(port->membase))
679 return PTR_ERR(port->membase);
680 port->mapbase = res->start;
681
682 spin_lock_init(&port->lock);
683
684 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
685 if (IS_ERR(stm32port->clk))
686 return PTR_ERR(stm32port->clk);
687
688 /* Ensure that clk rate is correct by enabling the clk */
689 ret = clk_prepare_enable(stm32port->clk);
690 if (ret)
691 return ret;
692
693 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
694 if (!stm32port->port.uartclk)
695 ret = -EINVAL;
696
697 clk_disable_unprepare(stm32port->clk);
698
699 return ret;
700}
701
702static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
703{
704 struct device_node *np = pdev->dev.of_node;
705 int id;
706
707 if (!np)
708 return NULL;
709
710 id = of_alias_get_id(np, "serial");
711 if (id < 0)
712 id = 0;
713
714 if (WARN_ON(id >= STM32_MAX_PORTS))
715 return NULL;
716
717 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
718 "auto-flow-control");
719 stm32_ports[id].port.line = id;
720 return &stm32_ports[id];
721}
722
723#ifdef CONFIG_OF
724static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200725 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
726 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
727 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
728 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200729 {},
730};
731
732MODULE_DEVICE_TABLE(of, stm32_match);
733#endif
734
735static int stm32_serial_probe(struct platform_device *pdev)
736{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200737 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200738 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200739 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200740
741 stm32port = stm32_of_get_stm32_port(pdev);
742 if (!stm32port)
743 return -ENODEV;
744
Alexandre TORGUEada86182016-09-15 18:42:33 +0200745 match = of_match_device(stm32_match, &pdev->dev);
746 if (match && match->data)
747 stm32port->info = (struct stm32_usart_info *)match->data;
748 else
749 return -EINVAL;
750
Maxime Coquelin48a60922015-06-10 21:19:36 +0200751 ret = stm32_init_port(stm32port, pdev);
752 if (ret)
753 return ret;
754
755 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
756 if (ret)
757 return ret;
758
759 platform_set_drvdata(pdev, &stm32port->port);
760
761 return 0;
762}
763
764static int stm32_serial_remove(struct platform_device *pdev)
765{
766 struct uart_port *port = platform_get_drvdata(pdev);
767
768 return uart_remove_one_port(&stm32_usart_driver, port);
769}
770
771
772#ifdef CONFIG_SERIAL_STM32_CONSOLE
773static void stm32_console_putchar(struct uart_port *port, int ch)
774{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200775 struct stm32_port *stm32_port = to_stm32_port(port);
776 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
777
778 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200779 cpu_relax();
780
Alexandre TORGUEada86182016-09-15 18:42:33 +0200781 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200782}
783
784static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
785{
786 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200787 struct stm32_port *stm32_port = to_stm32_port(port);
788 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200789 unsigned long flags;
790 u32 old_cr1, new_cr1;
791 int locked = 1;
792
793 local_irq_save(flags);
794 if (port->sysrq)
795 locked = 0;
796 else if (oops_in_progress)
797 locked = spin_trylock(&port->lock);
798 else
799 spin_lock(&port->lock);
800
801 /* Save and disable interrupts */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200802 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200803 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200804 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200805
806 uart_console_write(port, s, cnt, stm32_console_putchar);
807
808 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200809 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200810
811 if (locked)
812 spin_unlock(&port->lock);
813 local_irq_restore(flags);
814}
815
816static int stm32_console_setup(struct console *co, char *options)
817{
818 struct stm32_port *stm32port;
819 int baud = 9600;
820 int bits = 8;
821 int parity = 'n';
822 int flow = 'n';
823
824 if (co->index >= STM32_MAX_PORTS)
825 return -ENODEV;
826
827 stm32port = &stm32_ports[co->index];
828
829 /*
830 * This driver does not support early console initialization
831 * (use ARM early printk support instead), so we only expect
832 * this to be called during the uart port registration when the
833 * driver gets probed and the port should be mapped at that point.
834 */
835 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
836 return -ENXIO;
837
838 if (options)
839 uart_parse_options(options, &baud, &parity, &bits, &flow);
840
841 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
842}
843
844static struct console stm32_console = {
845 .name = STM32_SERIAL_NAME,
846 .device = uart_console_device,
847 .write = stm32_console_write,
848 .setup = stm32_console_setup,
849 .flags = CON_PRINTBUFFER,
850 .index = -1,
851 .data = &stm32_usart_driver,
852};
853
854#define STM32_SERIAL_CONSOLE (&stm32_console)
855
856#else
857#define STM32_SERIAL_CONSOLE NULL
858#endif /* CONFIG_SERIAL_STM32_CONSOLE */
859
860static struct uart_driver stm32_usart_driver = {
861 .driver_name = DRIVER_NAME,
862 .dev_name = STM32_SERIAL_NAME,
863 .major = 0,
864 .minor = 0,
865 .nr = STM32_MAX_PORTS,
866 .cons = STM32_SERIAL_CONSOLE,
867};
868
869static struct platform_driver stm32_serial_driver = {
870 .probe = stm32_serial_probe,
871 .remove = stm32_serial_remove,
872 .driver = {
873 .name = DRIVER_NAME,
874 .of_match_table = of_match_ptr(stm32_match),
875 },
876};
877
878static int __init usart_init(void)
879{
880 static char banner[] __initdata = "STM32 USART driver initialized";
881 int ret;
882
883 pr_info("%s\n", banner);
884
885 ret = uart_register_driver(&stm32_usart_driver);
886 if (ret)
887 return ret;
888
889 ret = platform_driver_register(&stm32_serial_driver);
890 if (ret)
891 uart_unregister_driver(&stm32_usart_driver);
892
893 return ret;
894}
895
896static void __exit usart_exit(void)
897{
898 platform_driver_unregister(&stm32_serial_driver);
899 uart_unregister_driver(&stm32_usart_driver);
900}
901
902module_init(usart_init);
903module_exit(usart_exit);
904
905MODULE_ALIAS("platform:" DRIVER_NAME);
906MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
907MODULE_LICENSE("GPL v2");