devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Max Filippov <jcmvbkbc@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	 linux-serial <linux-serial@vger.kernel.org>,
	devicetree@vger.kernel.org,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Jiri Slaby <jirislaby@kernel.org>,
	Rob Herring <robh+dt@kernel.org>,
	 Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	 Conor Dooley <conor+dt@kernel.org>
Subject: Re: [PATCH 2/4] drivers/tty/serial: add driver for the ESP32 UART
Date: Mon, 18 Sep 2023 12:07:46 +0300 (EEST)	[thread overview]
Message-ID: <8c524546-a6ae-ac68-bac3-f1fcc2e2f732@linux.intel.com> (raw)
In-Reply-To: <CAMo8BfJs+ToGNUCtMamU10EqpMR4PNDmJ8+4ya0jZtqTFwCEOQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 6259 bytes --]

On Fri, 15 Sep 2023, Max Filippov wrote:

> On Thu, Sep 14, 2023 at 6:07 AM Ilpo Järvinen
> <ilpo.jarvinen@linux.intel.com> wrote:
> >
> > On Wed, 13 Sep 2023, Max Filippov wrote:
> >
> > > Add driver for the UART controllers of the Espressif ESP32 and ESP32S3
> > > SoCs. Hardware specification is available at the following URLs:
> > >
> > >   https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf
> > >   (Chapter 13 UART Controller)
> > >   https://www.espressif.com/sites/default/files/documentation/esp32-s3_technical_reference_manual_en.pdf
> > >   (Chapter 26 UART Controller)
> > >
> > > Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
> > > ---
> > >  drivers/tty/serial/Kconfig       |  13 +
> > >  drivers/tty/serial/Makefile      |   1 +
> > >  drivers/tty/serial/esp32_uart.c  | 766 +++++++++++++++++++++++++++++++
> > >  include/uapi/linux/serial_core.h |   3 +
> > >  4 files changed, 783 insertions(+)
> > >  create mode 100644 drivers/tty/serial/esp32_uart.c
> > >
> > > diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
> > > index bdc568a4ab66..d9ca6b268f01 100644
> > > --- a/drivers/tty/serial/Kconfig
> > > +++ b/drivers/tty/serial/Kconfig
> > > @@ -1578,6 +1578,19 @@ config SERIAL_NUVOTON_MA35D1_CONSOLE
> > >         but you can alter that using a kernel command line option such as
> > >         "console=ttyNVTx".
> > >
> > > +config SERIAL_ESP32
> > > +     tristate "Espressif ESP32 UART support"
> > > +     depends on XTENSA_PLATFORM_ESP32 || (COMPILE_TEST && OF)
> > > +     select SERIAL_CORE
> > > +     select SERIAL_CORE_CONSOLE
> > > +     select SERIAL_EARLYCON
> > > +     help
> > > +       Driver for the UART controllers of the Espressif ESP32xx SoCs.
> > > +       When earlycon option is enabled the following kernel command line
> > > +       snippets may be used:
> > > +         earlycon=esp32s3uart,mmio32,0x60000000,115200n8,40000000
> > > +         earlycon=esp32uart,mmio32,0x3ff40000,115200n8
> > > +
> > >  endmenu
> > >
> > >  config SERIAL_MCTRL_GPIO
> > > diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
> > > index 138abbc89738..7b73137df7f3 100644
> > > --- a/drivers/tty/serial/Makefile
> > > +++ b/drivers/tty/serial/Makefile
> > > @@ -88,6 +88,7 @@ obj-$(CONFIG_SERIAL_MILBEAUT_USIO) += milbeaut_usio.o
> > >  obj-$(CONFIG_SERIAL_SIFIVE)  += sifive.o
> > >  obj-$(CONFIG_SERIAL_LITEUART) += liteuart.o
> > >  obj-$(CONFIG_SERIAL_SUNPLUS) += sunplus-uart.o
> > > +obj-$(CONFIG_SERIAL_ESP32)   += esp32_uart.o
> > >
> > >  # GPIOLIB helpers for modem control lines
> > >  obj-$(CONFIG_SERIAL_MCTRL_GPIO)      += serial_mctrl_gpio.o
> > > diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
> > > new file mode 100644
> > > index 000000000000..05ec0fce3a62
> > > --- /dev/null
> > > +++ b/drivers/tty/serial/esp32_uart.c

> > > +static u32 esp32_uart_tx_fifo_cnt(struct uart_port *port)
> > > +{
> > > +     return (esp32_uart_read(port, UART_STATUS_REG) &
> > > +             port_variant(port)->txfifo_cnt_mask) >> UART_TXFIFO_CNT_SHIFT;
> > > +}
> > > +
> > > +static u32 esp32_uart_rx_fifo_cnt(struct uart_port *port)
> > > +{
> > > +     return (esp32_uart_read(port, UART_STATUS_REG) &
> > > +             port_variant(port)->rxfifo_cnt_mask) >> UART_RXFIFO_CNT_SHIFT;
> >
> > FIELD_GET().
> 
> I see how FIELD_GET can be used in other places, but here
> port_variant(port)->rxfifo_cnt_mask is not a runtime constant and
> FIELD_GET does not work with non-constant field definitions. Any
> suggestions?

Ah, I sorry. I probably moved around while composing the reply and it 
seems I returned to add the comment to a wrong line.

I don't have a good suggestion for this one besides breking it into 
multiple lines for better readability.

> > Use more lines (and a local variable) instead of splitting one line. It
> > will be more readable that way.
> 
> Ok.

> > > +     u32 frag = (port->uartclk * 16) / baud - div * 16;
> > > +
> > > +     if (div <= port_variant(port)->clkdiv_mask) {
> > > +             esp32_uart_write(port, UART_CLKDIV_REG,
> > > +                              div | (frag << UART_CLKDIV_FRAG_SHIFT));
> >
> > FIELD_PREP().
> 
> Ok.
> 
> > Also div be encapsulated into FIELD_PREP here even if it's
> > shift is 0.
> 
> This field's mask, port_variant(port)->clkdiv_mask, is, again, not a runtime
> constant. Any suggestion for this?

So you're saying there are two different sized fields. I suppose it can be
left as is then.

> > > +                     port->uartclk / port_variant(port)->clkdiv_mask);
> > > +             esp32_uart_write(port, UART_CLKDIV_REG,
> > > +                              port_variant(port)->clkdiv_mask |
> > > +                              UART_CLKDIV_FRAG_MASK);
> >
> > I think you want to make the meaning more obvious here by using
> > FIELD_MAX(UART_CLKDIV_FRAG_MASK);
> 
> No. I think FIELD_MAX makes it less obvious, because to work properly
> it must be not just
>   FIELD_MAX(UART_CLKDIV_FRAG),
> but
>   FIELD_PREP(UART_CLKDIV_FRAG, FIELD_MAX(UART_CLKDIV_FRAG)).

Maybe add FIELD_PREP_MAX() in a preparatory patch to bitfield.h as this 
looks something that could be of use beyond this driver.

> > > +     esp32_uart_write(port, UART_CONF0_REG, conf0);
> > > +     esp32_uart_write(port, UART_CONF1_REG, conf1);
> > > +     spin_unlock_irqrestore(&port->lock, flags);
> > > +
> > > +     /*
> > > +      * esp32s3 may not support 9600, passing its minimal baud rate
> > > +      * as the min argument would trigger a WARN inside uart_get_baud_rate
> > > +      */
> > > +     baud = uart_get_baud_rate(port, termios, old,
> > > +                               0, port->uartclk / 16);
> >
> > This looks questionable solution to the problem mentioned in the comment.
> > What happens when user asks for baudrates below the minimum supported one?
> 
> I'll change it to refuse to change to unsupported baud rate and set default
> to 115200.
>
> > You might need to do something touching the core code to handle the case
> > where 9600 is not possible fallback.
> 
> I'd rather make a fallback to 115200, as it's a much more reasonable default.

I'll have to see the code before commenting on these.

-- 
 i.

  reply	other threads:[~2023-09-18  9:09 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 21:14 [PATCH 0/4] serial: add drivers for the ESP32xx serial devices Max Filippov
2023-09-13 21:14 ` [PATCH 1/4] dt-bindings: serial: document esp32-uart bindings Max Filippov
2023-09-14  5:54   ` Krzysztof Kozlowski
2023-09-14 20:00     ` Max Filippov
2023-09-14  5:55   ` Krzysztof Kozlowski
2023-09-14 14:48     ` Conor Dooley
2023-09-14 20:02       ` Max Filippov
2023-09-13 21:14 ` [PATCH 2/4] drivers/tty/serial: add driver for the ESP32 UART Max Filippov
2023-09-14  7:06   ` Jiri Slaby
2023-09-15  9:04     ` Max Filippov
2023-09-14 13:07   ` Ilpo Järvinen
2023-09-15 22:33     ` Max Filippov
2023-09-18  9:07       ` Ilpo Järvinen [this message]
2023-09-13 21:14 ` [PATCH 3/4] dt-bindings: serial: document esp32s3-acm bindings Max Filippov
2023-09-14  5:57   ` Krzysztof Kozlowski
2023-09-14 20:47     ` Max Filippov
2023-09-15  6:50       ` Krzysztof Kozlowski
2023-09-13 21:14 ` [PATCH 4/4] drivers/tty/serial: add ESP32S3 ACM device driver Max Filippov
2023-09-14  7:16   ` Jiri Slaby
2023-09-15 23:54     ` Max Filippov
2023-09-14 13:14   ` Ilpo Järvinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8c524546-a6ae-ac68-bac3-f1fcc2e2f732@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jcmvbkbc@gmail.com \
    --cc=jirislaby@kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).