spi: octeontx2: Add spi driver for OcteonTX2 SOC
cover spi: octeontx2: Add spi driver for OcteonTX2 SOC
Add driver for spi controller on Marvell OcteonTX2 SOC. This driver supports 1KB data buffer and 4-bit bus width.
It also supports ACPI and reads tx(rx)-bus-width which is used to set the SPI mode - DUAL, QUAD, OCTAL.
Piyush Malgujar (1):
spi: octeontx2: Add ACPI support
Suneel Garapati (1):
spi: octeontx2: Add support for octeontx2 spi controller
drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/spi-octeontx2.c | 467 ++++++++++++++++++++++++++++++++++++ drivers/spi/spi-octeontx2.h | 152 ++++++++++++ 4 files changed, 628 insertions(+) create mode 100644 drivers/spi/spi-octeontx2.c create mode 100644 drivers/spi/spi-octeontx2.h
Signature · 1 line
2.17.1
PATCH 1/2 spi: octeontx2: Add support for octeontx2 spi controller
From: Suneel Garapati <[email protected]>
Enable use of spi controller on Marvell OcteonTX2 SoC's. This supports for 1KB data buffer and 4-bit bus width.
Also, support for using fixed time base (TBI Clk) or system clock (SCLK) as reference clock for SPI bus is provided using module param tbi_clk_en with default value to 1 (TBI clk).
--- drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/spi-octeontx2.c | 378 ++++++++++++++++++++++++++++++++++++ drivers/spi/spi-octeontx2.h | 152 +++++++++++++++ 4 files changed, 539 insertions(+) create mode 100644 drivers/spi/spi-octeontx2.c create mode 100644 drivers/spi/spi-octeontx2.h
drivers/spi/Kconfig +8 -0
@@ -694,6 +694,14 @@ config SPI_OCTEON SPI host driver for the hardware found on some Cavium OCTEON SOCs. +config SPI_OCTEONTX2 + tristate "Marvell OcteonTX2 SPI controller" + depends on PCI && 64BIT && (ARM64 || COMPILE_TEST) + help + This driver supports the OcteonTX2 SPI controller in master + mode. It supports single, dual and quad mode transfers. + This controller hardware is found on Marvell OcteonTX2 SoCs. + config SPI_OMAP_UWIRE tristate "OMAP1 MicroWire" depends on ARCH_OMAP1 || (ARM && COMPILE_TEST)
drivers/spi/Makefile +1 -0
@@ -91,6 +91,7 @@ obj-$(CONFIG_SPI_NXP_FLEXSPI) += spi-nxp-fspi.o obj-$(CONFIG_SPI_OC_TINY) += spi-oc-tiny.o spi-octeon-objs := spi-cavium.o spi-cavium-octeon.o obj-$(CONFIG_SPI_OCTEON) += spi-octeon.o +obj-$(CONFIG_SPI_OCTEONTX2) += spi-octeontx2.o obj-$(CONFIG_SPI_OMAP_UWIRE) += spi-omap-uwire.o obj-$(CONFIG_SPI_OMAP24XX) += spi-omap2-mcspi.o obj-$(CONFIG_SPI_TI_QSPI) += spi-ti-qspi.o
drivers/spi/spi-octeontx2.c new file +378 -0
378 lines not on this page · raw message
drivers/spi/spi-octeontx2.h new file +152 -0
152 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 1/2] spi: octeontx2: Add support for octeontx2 spi controller
Hi Suneel,
[...]
Quoted 1 line
+static int
tbi_clk_en= 1;
bool?
drivers/spi/spi-octeontx2.c line 24
Quoted 3 lines
drivers/spi/spi-octeontx2.c new file +3
-0+module_param (tbi_clk_en, uint,0644 ); +MODULE_PARM_DESC (tbi_clk_en, +"Use Fixed Time Base 100MHz Reference Clock (0=Disable, 1=Enable [default])" );
can we avoid using module parameters? You can have these defined in device tree and you can also make sysfs interfaces, as well.
Quoted 4 lines
drivers/spi/spi-octeontx2.c new file +4
-0+static int cfg_mode_delay =30 ; +module_param (cfg_mode_delay, uint,0644 ); +MODULE_PARM_DESC (cfg_mode_delay, +"Delay in micro-seconds for mode change in MPI CFG register (30 [default])" );
drivers/spi/spi-octeontx2.c line 39
Quoted 10 lines
drivers/spi/spi-octeontx2.c new file +10
-0+ +static void octeontx2_spi_wait_ready (struct octeontx2_spi *p) +{ +union mpix_sts mpi_sts; +unsigned int loops =0 ; + + mpi_sts.u64 =0 ; + do { + if (loops++) + __delay(500);
mmhhh... why have you chosen __delay() ?
Quoted 2 lines
drivers/spi/spi-octeontx2.c new file +2
-0+ mpi_sts.u64 =readq (p->register_base +OCTEONTX2_SPI_STS (p)); + }while (mpi_sts.s.busy);
[...]
drivers/spi/spi-octeontx2.c line 113
Quoted 5 lines
drivers/spi/spi-octeontx2.c new file +5
-0+if (mpi_cfg.u64 != p->last_cfg) { + p->last_cfg = mpi_cfg.u64; +writeq (mpi_cfg.u64, p->register_base +OCTEONTX2_SPI_CFG (p)); + mpi_cfg.u64 =readq (p->register_base +OCTEONTX2_SPI_CFG (p)); +udelay (cfg_mode_delay);/* allow CS change to settle */
before "udelaying" anything that the user gives you, I would check what cfg_mode_delay is.
Andi
Mark Brown
maintainerRe: [PATCH 1/2] spi: octeontx2: Add support for octeontx2 spi controller
In reply to Piyush Malgujardrivers/spi/spi-octeontx2.c line 29
Quoted 9 lines · Piyush Malgujar
drivers/spi/spi-octeontx2.c new file +9
-0+static int tbi_clk_en =1 ; +module_param (tbi_clk_en, uint,0644 ); +MODULE_PARM_DESC (tbi_clk_en, +"Use Fixed Time Base 100MHz Reference Clock (0=Disable, 1=Enable [default])" ); + +static int cfg_mode_delay =30 ; +module_param (cfg_mode_delay, uint,0644 ); +MODULE_PARM_DESC (cfg_mode_delay, +"Delay in micro-seconds for mode change in MPI CFG register (30 [default])" );
These don't look like they should be configurable at runtime, either DT or (especially in the second case) just have the driver do the right thing.
drivers/spi/spi-octeontx2.c line 41
Quoted 11 lines
drivers/spi/spi-octeontx2.c new file +11
-0+static void octeontx2_spi_wait_ready (struct octeontx2_spi *p) +{ +union mpix_sts mpi_sts; +unsigned int loops =0 ; + + mpi_sts.u64 =0 ; +do { +if (loops++) +__delay (500 ); + mpi_sts.u64 =readq (p->register_base +OCTEONTX2_SPI_STS (p)); + }while (mpi_sts.s.busy);
Might want a cpu_relax() in there.
drivers/spi/spi-octeontx2.c line 114
Quoted 9 lines
drivers/spi/spi-octeontx2.c new file +9
-0+ p->cs_enax |= (0xFull <<12 ); + mpi_cfg.u64 |= p->cs_enax; + +if (mpi_cfg.u64 != p->last_cfg) { + p->last_cfg = mpi_cfg.u64; +writeq (mpi_cfg.u64, p->register_base +OCTEONTX2_SPI_CFG (p)); + mpi_cfg.u64 =readq (p->register_base +OCTEONTX2_SPI_CFG (p)); +udelay (cfg_mode_delay);/* allow CS change to settle */ + }
It looks like the hardware has independent control of chip select so should just implement a set_cs() operation. That seems to be the only thing stopping the driver using transfer_one() instead of transfer_one_message() so it should do that too and save more code.
drivers/spi/spi-octeontx2.c line 291
Quoted 9 lines
drivers/spi/spi-octeontx2.c new file +9
-0+ p->clk =devm_clk_get (dev,NULL ); +if (IS_ERR (p->clk)) { + p->clk =devm_clk_get (dev,"sclk" ); + p->sys_freq =0 ; + }else { + ret =clk_prepare_enable (p->clk); +if (!ret) + p->sys_freq =clk_get_rate (p->clk); + }
We don't need to enable sclk or care about errors? That seems buggy. The driver should just rely on the clock API here.
drivers/spi/spi-octeontx2.c line 296
Quoted 4 lines
drivers/spi/spi-octeontx2.c new file +4
-0+if (!p->sys_freq) + p->sys_freq =SYS_FREQ_DEFAULT ; +if (tbi_clk_en) + p->sys_freq =TBI_FREQ ;
We never seem to do anything to actually configure this in the hardware.
PATCH 2/2 spi: octeontx2: Add ACPI support
In reply to Piyush MalgujarEnable ACPI support for SPI controller on Marvell Octeontx2 SOC. This supports reading the tx(rx)-bus-width from ACPI table which is used to set the SPI mode - DUAL, QUAD, OCTAL.
--- drivers/spi/spi-octeontx2.c | 117 +++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 14 deletions(-)
drivers/spi/spi-octeontx2.c +103 -14
@@ -5,11 +5,16 @@* Copyright (C) 2018 Marvell International Ltd. */ +#include <linux/acpi.h> #include <linux/delay.h> #include <linux/module.h> #include <linux/of.h> #include <linux/pci.h> +#include <linux/property.h> #include <linux/spi/spi.h> +#ifdef CONFIG_ACPI +#include <linux/spi/spi-mem.h> +#endif #include "spi-octeontx2.h"
@@ -227,6 +232,73 @@ int octeontx2_spi_transfer_one_message(struct spi_master *master,return status; } +#ifdef CONFIG_ACPI + +static bool octeontx2_spi_supports_op (struct spi_mem *mem, +const struct spi_mem_op *op) +{ +struct spi_device *spi = mem->spi; +const union acpi_object *obj; +struct acpi_device *adev; + + adev =ACPI_COMPANION (&spi->dev); + +if (!acpi_dev_get_property (adev,"spi-tx-bus-width" ,ACPI_TYPE_INTEGER , + &obj)) { +switch (obj->integer.value) { +case 1 : +break ; +case 2 : + spi->mode |=SPI_TX_DUAL ; +break ; +case 4 : + spi->mode |=SPI_TX_QUAD ; +break ; +case 8 : + spi->mode |=SPI_TX_OCTAL ; +break ; +default : +dev_warn (&spi->dev, +"spi-tx-bus-width %lld not supported\n" , + obj->integer.value); +break ; + } + } + +if (!acpi_dev_get_property (adev,"spi-rx-bus-width" ,ACPI_TYPE_INTEGER , + &obj)) { +switch (obj->integer.value) { +case 1 : +break ; +case 2 : + spi->mode |=SPI_RX_DUAL ; +break ; +case 4 : + spi->mode |=SPI_RX_QUAD ; +break ; +case 8 : + spi->mode |=SPI_RX_OCTAL ; +break ; +default : +dev_warn (&spi->dev, +"spi-rx-bus-width %lld not supported\n" , + obj->integer.value); +break ; + } + } + +if (!spi_mem_default_supports_op (mem, op)) +return false; + +return true; +} + +static const struct spi_controller_mem_ops octeontx2_spi_mem_ops = { + .supports_op = octeontx2_spi_supports_op, +}; + +#endif +static int octeontx2_spi_probe (struct pci_dev *pdev,const struct pci_device_id *ent) {
@@ -235,6 +307,7 @@ static int octeontx2_spi_probe(struct pci_dev *pdev,struct octeontx2_spi *p;union mpix_sts mpi_sts;int ret = -ENOENT ; +bool has_acpi;/* may need to hunt for devtree entry */ if (!pdev->dev.of_node) {
@@ -248,6 +321,8 @@ static int octeontx2_spi_probe(struct pci_dev *pdev,of_node_put (np); } + has_acpi =has_acpi_companion (dev); + master =spi_alloc_master (dev,sizeof (struct octeontx2_spi ));if (!master) { ret = -ENOMEM ;
@@ -280,20 +355,26 @@ static int octeontx2_spi_probe(struct pci_dev *pdev, mpi_sts.u64 =readq (p->register_base +OCTEONTX2_SPI_STS (p)); p->rcvd_present = mpi_sts.u64 &0x4 ? true : false; - p->clk =devm_clk_get (dev,NULL ); -if (IS_ERR (p->clk)) { - p->clk =devm_clk_get (dev,"sclk" ); - p->sys_freq =0 ; +if (!has_acpi) { + p->clk =devm_clk_get (dev,NULL ); +if (IS_ERR (p->clk)) { + p->clk =devm_clk_get (dev,"sclk" ); + p->sys_freq =0 ; + }else { + ret =clk_prepare_enable (p->clk); +if (!ret) + p->sys_freq =clk_get_rate (p->clk); + } + +if (!p->sys_freq) + p->sys_freq =SYS_FREQ_DEFAULT ; +if (tbi_clk_en) + p->sys_freq =TBI_FREQ ; }else { - ret =clk_prepare_enable (p->clk); -if (!ret) - p->sys_freq =clk_get_rate (p->clk); +device_property_read_u32 (dev,"sclk" , &p->sys_freq); +if (!p->sys_freq) + p->sys_freq =TBI_FREQ ; } - -if (!p->sys_freq) - p->sys_freq =SYS_FREQ_DEFAULT ; -if (tbi_clk_en) - p->sys_freq =TBI_FREQ ;dev_info (dev,"Reference clock is %u\n" , p->sys_freq); master->num_chipselect =4 ;
@@ -305,6 +386,10 @@ static int octeontx2_spi_probe(struct pci_dev *pdev, master->bits_per_word_mask =SPI_BPW_MASK (8 ); master->max_speed_hz =OCTEONTX2_SPI_MAX_CLOCK_HZ ; master->dev.of_node = pdev->dev.of_node; + master->dev.fwnode = pdev->dev.fwnode; +#ifdef CONFIG_ACPI + master->mem_ops = &octeontx2_spi_mem_ops; +#endif pci_set_drvdata (pdev, master);
@@ -315,7 +400,8 @@ static int octeontx2_spi_probe(struct pci_dev *pdev,return 0 ; error_disable: -clk_disable_unprepare (p->clk); +if (!has_acpi) +clk_disable_unprepare (p->clk); error_put:spi_master_put (master); error:
@@ -327,10 +413,13 @@ static void octeontx2_spi_remove(struct pci_dev *pdev)struct spi_master *master =pci_get_drvdata (pdev);struct octeontx2_spi *p; +bool has_acpi =has_acpi_companion (&pdev->dev); + p =spi_master_get_devdata (master);/* Put everything in a known state. */ if (p) { -clk_disable_unprepare (p->clk); +if (!has_acpi) +clk_disable_unprepare (p->clk);writeq (0 , p->register_base +OCTEONTX2_SPI_CFG (p)); }
Signature · 1 line
2.17.1