From: Marek Vasut Date: Mon, 13 Nov 2017 17:53:42 +0100 Subject: [U-Boot] [PATCH v4 2/2] DW SPI: Get clock value from Device Tree In-Reply-To: <20171113094745.7506-3-Eugeniy.Paltsev@synopsys.com> References: <20171113094745.7506-1-Eugeniy.Paltsev@synopsys.com> <20171113094745.7506-3-Eugeniy.Paltsev@synopsys.com> Message-ID: List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de On 11/13/2017 10:47 AM, Eugeniy Paltsev wrote: > Add option to set spi controller clock frequency via device tree > using standard clock bindings. > > Define dw_spi_get_clk function as 'weak' as some targets > (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) fon't use standard clock API ^ don't ... detail > and implement dw_spi_get_clk their own way in their clock manager. > > Get rid of clock_manager.h include as we don't use > cm_get_spi_controller_clk_hz function anymore. (we use redefined > dw_spi_get_clk in SOCFPGA clock managers instead) > > Signed-off-by: Eugeniy Paltsev > --- > drivers/spi/designware_spi.c | 46 ++++++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 44 insertions(+), 2 deletions(-) > > diff --git a/drivers/spi/designware_spi.c b/drivers/spi/designware_spi.c > index 6cc4f51..5460ad7 100644 > --- a/drivers/spi/designware_spi.c > +++ b/drivers/spi/designware_spi.c > @@ -12,6 +12,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -19,7 +20,6 @@ > #include > #include > #include > -#include > > DECLARE_GLOBAL_DATA_PTR; > > @@ -95,6 +95,7 @@ struct dw_spi_priv { > void __iomem *regs; > unsigned int freq; /* Default frequency */ > unsigned int mode; > + unsigned long bus_clk_rate; > > struct gpio_desc cs_gpio; /* External chip-select gpio */ > > @@ -195,14 +196,55 @@ static void spi_hw_init(struct dw_spi_priv *priv) > debug("%s: fifo_len=%d\n", __func__, priv->fifo_len); > } > > +/* > + * We define dw_spi_get_clk function as 'weak' as some targets > + * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) fon't use standard clock API > + * and implement dw_spi_get_clk their own way in their clock manager. > + */ > +__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate) > +{ > +#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK) You can drop this ifdef IMO, this code will be overridden on SoCFPGA and all the other systems should use DT and clock framework. > + struct clk clk; > + int ret; > + > + ret = clk_get_by_index(bus, 0, &clk); > + if (ret) > + return -EINVAL; > + > + ret = clk_enable(&clk); > + if (ret && ret != -ENOSYS) > + return ret; > + > + *rate = clk_get_rate(&clk); > + if (!*rate) { > + clk_disable(&clk); > + return -EINVAL; > + } > + > + debug("%s: get spi controller clk via device tree: %lu Hz\n", > + __func__, *rate); > + > + clk_free(&clk); > + > + return 0; > +#else > + return -ENOSYS; > +#endif > +} > + > static int dw_spi_probe(struct udevice *bus) > { > struct dw_spi_platdata *plat = dev_get_platdata(bus); > struct dw_spi_priv *priv = dev_get_priv(bus); > + int ret; > > priv->regs = plat->regs; > priv->freq = plat->frequency; > > + ret = dw_spi_get_clk(bus, &priv->bus_clk_rate); > + if (ret) > + return ret; > + > /* Currently only bits_per_word == 8 supported */ > priv->bits_per_word = 8; > > @@ -411,7 +453,7 @@ static int dw_spi_set_speed(struct udevice *bus, uint speed) > spi_enable_chip(priv, 0); > > /* clk_div doesn't support odd number */ > - clk_div = cm_get_spi_controller_clk_hz() / speed; > + clk_div = priv->bus_clk_rate / speed; > clk_div = (clk_div + 1) & 0xfffe; > dw_write(priv, DW_SPI_BAUDR, clk_div); > > -- Best regards, Marek Vasut