1111#include <linux/serial_core.h>
1212#include <linux/serial_sci.h>
1313#include <linux/tty_flip.h>
14+
15+ #include "serial_mctrl_gpio.h"
1416#include "rsci.h"
1517
1618MODULE_IMPORT_NS ("SH_SCI" );
@@ -59,6 +61,41 @@ MODULE_IMPORT_NS("SH_SCI");
5961#define CCR1_CTSPEN BIT(1) /* CTS External Pin Enable */
6062#define CCR1_CTSE BIT(0) /* CTS Enable */
6163
64+ /* CCR2 (Common Control Register 2) */
65+ #define CCR2_INIT 0xFF000004
66+ #define CCR2_CKS_TCLK (0) /* TCLK clock */
67+ #define CCR2_CKS_TCLK_DIV4 BIT(20) /* TCLK/4 clock */
68+ #define CCR2_CKS_TCLK_DIV16 BIT(21) /* TCLK16 clock */
69+ #define CCR2_CKS_TCLK_DIV64 (BIT(21) | BIT(20)) /* TCLK/64 clock */
70+ #define CCR2_BRME BIT(16) /* Bitrate Modulation Enable */
71+ #define CCR2_ABCSE BIT(6) /* Asynchronous Mode Extended Base Clock Select */
72+ #define CCR2_ABCS BIT(5) /* Asynchronous Mode Base Clock Select */
73+ #define CCR2_BGDM BIT(4) /* Baud Rate Generator Double-Speed Mode Select */
74+
75+ /* CCR3 (Common Control Register 3) */
76+ #define CCR3_INIT 0x1203
77+ #define CCR3_BLK BIT(29) /* Block Transfer Mode */
78+ #define CCR3_GM BIT(28) /* GSM Mode */
79+ #define CCR3_CKE1 BIT(25) /* Clock Enable 1 */
80+ #define CCR3_CKE0 BIT(24) /* Clock Enable 0 */
81+ #define CCR3_DEN BIT(21) /* Driver Enabled */
82+ #define CCR3_FM BIT(20) /* FIFO Mode Select */
83+ #define CCR3_MP BIT(19) /* Multi-Processor Mode */
84+ #define CCR3_MOD_ASYNC 0 /* Asynchronous mode (Multi-processor mode) */
85+ #define CCR3_MOD_IRDA BIT(16) /* Smart card interface mode */
86+ #define CCR3_MOD_CLK_SYNC BIT(17) /* Clock synchronous mode */
87+ #define CCR3_MOD_SPI (BIT(17) | BIT(16)) /* Simple SPI mode */
88+ #define CCR3_MOD_I2C BIT(18) /* Simple I2C mode */
89+ #define CCR3_RXDESEL BIT(15) /* Asynchronous Start Bit Edge Detection Select */
90+ #define CCR3_STP BIT(14) /* Stop bit Length */
91+ #define CCR3_SINV BIT(13) /* Transmitted/Received Data Invert */
92+ #define CCR3_LSBF BIT(12) /* LSB First select */
93+ #define CCR3_CHR1 BIT(9) /* Character Length */
94+ #define CCR3_CHR0 BIT(8) /* Character Length */
95+ #define CCR3_BPEN BIT(7) /* Synchronizer Bypass Enable */
96+ #define CCR3_CPOL BIT(1) /* Clock Polarity Select */
97+ #define CCR3_CPHA BIT(0) /* Clock Phase Select */
98+
6299/* FCR (FIFO Control Register) */
63100#define FCR_RFRST BIT(23) /* Receive FIFO Data Register Reset */
64101#define FCR_TFRST BIT(15) /* Transmit FIFO Data Register Reset */
@@ -138,6 +175,29 @@ static void rsci_start_rx(struct uart_port *port)
138175 rsci_serial_out (port , CCR0 , ctrl );
139176}
140177
178+ static void rsci_enable_ms (struct uart_port * port )
179+ {
180+ mctrl_gpio_enable_ms (to_sci_port (port )-> gpios );
181+ }
182+
183+ static void rsci_init_pins (struct uart_port * port , unsigned int cflag )
184+ {
185+ struct sci_port * s = to_sci_port (port );
186+
187+ /* Use port-specific handler if provided */
188+ if (s -> cfg -> ops && s -> cfg -> ops -> init_pins ) {
189+ s -> cfg -> ops -> init_pins (port , cflag );
190+ return ;
191+ }
192+
193+ if (!s -> has_rtscts )
194+ return ;
195+
196+ if (s -> autorts )
197+ rsci_serial_out (port , CCR1 , rsci_serial_in (port , CCR1 ) |
198+ CCR1_CTSE | CCR1_CTSPEN );
199+ }
200+
141201static int rsci_scif_set_rtrg (struct uart_port * port , int rx_trig )
142202{
143203 u32 fcr = rsci_serial_in (port , FCR );
@@ -157,18 +217,119 @@ static int rsci_scif_set_rtrg(struct uart_port *port, int rx_trig)
157217static void rsci_set_termios (struct uart_port * port , struct ktermios * termios ,
158218 const struct ktermios * old )
159219{
220+ unsigned int ccr2_val = CCR2_INIT , ccr3_val = CCR3_INIT ;
221+ unsigned int ccr0_val = 0 , ccr1_val = 0 , ccr4_val = 0 ;
222+ unsigned int brr1 = 255 , cks1 = 0 , srr1 = 15 ;
160223 struct sci_port * s = to_sci_port (port );
224+ unsigned int brr = 255 , cks = 0 ;
225+ int min_err = INT_MAX , err ;
226+ unsigned long max_freq = 0 ;
227+ unsigned int baud , i ;
161228 unsigned long flags ;
229+ unsigned int ctrl ;
230+ int best_clk = -1 ;
231+
232+ if ((termios -> c_cflag & CSIZE ) == CS7 ) {
233+ ccr3_val |= CCR3_CHR0 ;
234+ } else {
235+ termios -> c_cflag &= ~CSIZE ;
236+ termios -> c_cflag |= CS8 ;
237+ }
238+
239+ if (termios -> c_cflag & PARENB )
240+ ccr1_val |= CCR1_PE ;
241+
242+ if (termios -> c_cflag & PARODD )
243+ ccr1_val |= (CCR1_PE | CCR1_PM );
244+
245+ if (termios -> c_cflag & CSTOPB )
246+ ccr3_val |= CCR3_STP ;
247+
248+ /* Enable noise filter function */
249+ ccr1_val |= CCR1_NFEN ;
250+
251+ /*
252+ * earlyprintk comes here early on with port->uartclk set to zero.
253+ * the clock framework is not up and running at this point so here
254+ * we assume that 115200 is the maximum baud rate. please note that
255+ * the baud rate is not programmed during earlyprintk - it is assumed
256+ * that the previous boot loader has enabled required clocks and
257+ * setup the baud rate generator hardware for us already.
258+ */
259+ if (!port -> uartclk ) {
260+ max_freq = 115200 ;
261+ } else {
262+ for (i = 0 ; i < SCI_NUM_CLKS ; i ++ )
263+ max_freq = max (max_freq , s -> clk_rates [i ]);
264+
265+ max_freq /= min_sr (s );
266+ }
267+
268+ baud = uart_get_baud_rate (port , termios , old , 0 , max_freq );
269+ if (!baud )
270+ goto done ;
271+
272+ /* Divided Functional Clock using standard Bit Rate Register */
273+ err = sci_scbrr_calc (s , baud , & brr1 , & srr1 , & cks1 );
274+ if (abs (err ) < abs (min_err )) {
275+ best_clk = SCI_FCK ;
276+ ccr0_val = 0 ;
277+ min_err = err ;
278+ brr = brr1 ;
279+ cks = cks1 ;
280+ }
281+
282+ done :
283+ if (best_clk >= 0 )
284+ dev_dbg (port -> dev , "Using clk %pC for %u%+d bps\n" ,
285+ s -> clks [best_clk ], baud , min_err );
162286
163287 sci_port_enable (s );
164288 uart_port_lock_irqsave (port , & flags );
165289
166- /* For now, only RX enabling is supported */
167- if (termios -> c_cflag & CREAD )
290+ uart_update_timeout (port , termios -> c_cflag , baud );
291+
292+ rsci_serial_out (port , CCR0 , ccr0_val );
293+
294+ ccr3_val |= CCR3_FM ;
295+ rsci_serial_out (port , CCR3 , ccr3_val );
296+
297+ ccr2_val |= (cks << 20 ) | (brr << 8 );
298+ rsci_serial_out (port , CCR2 , ccr2_val );
299+
300+ rsci_serial_out (port , CCR1 , ccr1_val );
301+ rsci_serial_out (port , CCR4 , ccr4_val );
302+
303+ ctrl = rsci_serial_in (port , FCR );
304+ ctrl |= (FCR_RFRST | FCR_TFRST );
305+ rsci_serial_out (port , FCR , ctrl );
306+
307+ if (s -> rx_trigger > 1 )
308+ rsci_scif_set_rtrg (port , s -> rx_trigger );
309+
310+ port -> status &= ~UPSTAT_AUTOCTS ;
311+ s -> autorts = false;
312+
313+ if ((port -> flags & UPF_HARD_FLOW ) && (termios -> c_cflag & CRTSCTS )) {
314+ port -> status |= UPSTAT_AUTOCTS ;
315+ s -> autorts = true;
316+ }
317+
318+ rsci_init_pins (port , termios -> c_cflag );
319+ rsci_serial_out (port , CFCLR , CFCLR_CLRFLAG );
320+ rsci_serial_out (port , FFCLR , FFCLR_DRC );
321+
322+ ccr0_val |= CCR0_RE ;
323+ rsci_serial_out (port , CCR0 , ccr0_val );
324+
325+ if ((termios -> c_cflag & CREAD ) != 0 )
168326 rsci_start_rx (port );
169327
170328 uart_port_unlock_irqrestore (port , flags );
171329 sci_port_disable (s );
330+
331+ if (UART_ENABLE_MS (port , termios -> c_cflag ))
332+ rsci_enable_ms (port );
172333}
173334
174335static int rsci_txfill (struct uart_port * port )
@@ -193,13 +354,34 @@ static unsigned int rsci_tx_empty(struct uart_port *port)
193354
194355static void rsci_set_mctrl (struct uart_port * port , unsigned int mctrl )
195356{
196- /* Not supported yet */
357+ if (mctrl & TIOCM_LOOP ) {
358+ /* Standard loopback mode */
359+ rsci_serial_out (port , CCR1 , rsci_serial_in (port , CCR1 ) | CCR1_SPLP );
360+ }
197361}
198362
199363static unsigned int rsci_get_mctrl (struct uart_port * port )
200364{
201- /* Not supported yet */
202- return 0 ;
365+ struct sci_port * s = to_sci_port (port );
366+ struct mctrl_gpios * gpios = s -> gpios ;
367+ unsigned int mctrl = 0 ;
368+
369+ mctrl_gpio_get (gpios , & mctrl );
370+
371+ /*
372+ * CTS/RTS is handled in hardware when supported, while nothing
373+ * else is wired up.
374+ */
375+ if (!mctrl_gpio_to_gpiod (gpios , UART_GPIO_CTS ))
376+ mctrl |= TIOCM_CTS ;
377+
378+ if (!mctrl_gpio_to_gpiod (gpios , UART_GPIO_DSR ))
379+ mctrl |= TIOCM_DSR ;
380+
381+ if (!mctrl_gpio_to_gpiod (gpios , UART_GPIO_DCD ))
382+ mctrl |= TIOCM_CAR ;
383+
384+ return mctrl ;
203385}
204386
205387static void rsci_clear_CFC (struct uart_port * port , unsigned int mask )
@@ -329,7 +511,8 @@ static void rsci_receive_chars(struct uart_port *port)
329511 continue ;
330512 }
331513
332- /* Store data and status.
514+ /*
515+ * Store data and status.
333516 * Non FIFO mode is not supported
334517 */
335518 if (rdat & RDR_FFER ) {
@@ -363,6 +546,28 @@ static void rsci_receive_chars(struct uart_port *port)
363546 }
364547}
365548
549+ static void rsci_break_ctl (struct uart_port * port , int break_state )
550+ {
551+ unsigned short ccr0_val , ccr1_val ;
552+ unsigned long flags ;
553+
554+ uart_port_lock_irqsave (port , & flags );
555+ ccr1_val = rsci_serial_in (port , CCR1 );
556+ ccr0_val = rsci_serial_in (port , CCR0 );
557+
558+ if (break_state == -1 ) {
559+ ccr1_val = (ccr1_val | CCR1_SPB2IO ) & ~CCR1_SPB2DT ;
560+ ccr0_val &= ~CCR0_TE ;
561+ } else {
562+ ccr1_val = (ccr1_val | CCR1_SPB2DT ) & ~CCR1_SPB2IO ;
563+ ccr0_val |= CCR0_TE ;
564+ }
565+
566+ rsci_serial_out (port , CCR1 , ccr1_val );
567+ rsci_serial_out (port , CCR0 , ccr0_val );
568+ uart_port_unlock_irqrestore (port , flags );
569+ }
570+
366571static void rsci_poll_put_char (struct uart_port * port , unsigned char c )
367572{
368573 u32 status ;
@@ -384,12 +589,21 @@ static void rsci_poll_put_char(struct uart_port *port, unsigned char c)
384589static void rsci_prepare_console_write (struct uart_port * port , u32 ctrl )
385590{
386591 struct sci_port * s = to_sci_port (port );
387- u32 ctrl_temp =
388- s -> params -> param_bits -> rxtx_enable | CCR0_TIE |
389- s -> hscif_tot ;
592+ u32 ctrl_temp = s -> params -> param_bits -> rxtx_enable ;
593+
594+ if (s -> type == RSCI_PORT_SCIF16 )
595+ ctrl_temp |= CCR0_TIE | s -> hscif_tot ;
596+
390597 rsci_serial_out (port , CCR0 , ctrl_temp );
391598}
392599
600+ static void rsci_finish_console_write (struct uart_port * port , u32 ctrl )
601+ {
602+ /* First set TE = 0 and then restore the CCR0 value */
603+ rsci_serial_out (port , CCR0 , ctrl & ~CCR0_TE );
604+ rsci_serial_out (port , CCR0 , ctrl );
605+ }
606+
393607static const char * rsci_type (struct uart_port * port )
394608{
395609 return "rsci" ;
@@ -419,6 +633,17 @@ static const struct sci_port_params_bits rsci_port_param_bits = {
419633 .poll_sent_bits = CSR_TDRE | CSR_TEND ,
420634};
421635
636+ static const struct sci_port_params rsci_rzg3e_port_params = {
637+ .fifosize = 32 ,
638+ .overrun_reg = CSR ,
639+ .overrun_mask = CSR_ORER ,
640+ .sampling_rate_mask = SCI_SR (32 ),
641+ .error_mask = RSCI_DEFAULT_ERROR_MASK ,
642+ .error_clear = RSCI_ERROR_CLEAR ,
643+ .param_bits = & rsci_port_param_bits ,
644+ .common_regs = & rsci_common_regs ,
645+ };
646+
422647static const struct sci_port_params rsci_rzt2h_port_params = {
423648 .fifosize = 16 ,
424649 .overrun_reg = CSR ,
@@ -437,6 +662,8 @@ static const struct uart_ops rsci_uart_ops = {
437662 .start_tx = rsci_start_tx ,
438663 .stop_tx = rsci_stop_tx ,
439664 .stop_rx = rsci_stop_rx ,
665+ .enable_ms = rsci_enable_ms ,
666+ .break_ctl = rsci_break_ctl ,
440667 .startup = sci_startup ,
441668 .shutdown = sci_shutdown ,
442669 .set_termios = rsci_set_termios ,
@@ -456,11 +683,19 @@ static const struct sci_port_ops rsci_port_ops = {
456683 .receive_chars = rsci_receive_chars ,
457684 .poll_put_char = rsci_poll_put_char ,
458685 .prepare_console_write = rsci_prepare_console_write ,
686+ .finish_console_write = rsci_finish_console_write ,
459687 .suspend_regs_size = rsci_suspend_regs_size ,
460688 .set_rtrg = rsci_scif_set_rtrg ,
461689 .shutdown_complete = rsci_shutdown_complete ,
462690};
463691
692+ struct sci_of_data of_rsci_rzg3e_data = {
693+ .type = RSCI_PORT_SCIF32 ,
694+ .ops = & rsci_port_ops ,
695+ .uart_ops = & rsci_uart_ops ,
696+ .params = & rsci_rzg3e_port_params ,
697+ };
698+
464699struct sci_of_data of_rsci_rzt2h_data = {
465700 .type = RSCI_PORT_SCIF16 ,
466701 .ops = & rsci_port_ops ,
@@ -470,12 +705,19 @@ struct sci_of_data of_rsci_rzt2h_data = {
470705
471706#ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
472707
708+ static int __init rsci_rzg3e_early_console_setup (struct earlycon_device * device ,
709+ const char * opt )
710+ {
711+ return scix_early_console_setup (device , & of_rsci_rzg3e_data );
712+ }
713+
473714static int __init rsci_rzt2h_early_console_setup (struct earlycon_device * device ,
474715 const char * opt )
475716{
476717 return scix_early_console_setup (device , & of_rsci_rzt2h_data );
477718}
478719
720+ OF_EARLYCON_DECLARE (rsci , "renesas,r9a09g047-rsci" , rsci_rzg3e_early_console_setup );
479721OF_EARLYCON_DECLARE (rsci , "renesas,r9a09g077-rsci" , rsci_rzt2h_early_console_setup );
480722
481723#endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
0 commit comments