···1818- gpios : specifies the gpio pins to be used for chipselects.1919 The gpios will be referred to as reg = <index> in the SPI child nodes.2020 If unspecified, a single SPI device without a chip select can be used.2121+- fsl,spisel_boot : for the MPC8306 and MPC8309, specifies that the2222+ SPISEL_BOOT signal is used as chip select for a slave device. Use2323+ reg = <number of gpios> in the corresponding child node, i.e. 0 if2424+ the gpios property is not present.21252226Example:2327 spi@4c0 {
···2323Recommended properties:2424- spi-max-frequency: Definition as per2525 Documentation/devicetree/bindings/spi/spi-bus.txt2626+Optional properties:2727+- nvidia,tx-clk-tap-delay: Delays the clock going out to the external device2828+ with this tap value. This property is used to tune the outgoing data from2929+ Tegra SPI master with respect to outgoing Tegra SPI master clock.3030+ Tap values vary based on the platform design trace lengths from Tegra SPI3131+ to corresponding slave devices. Valid tap values are from 0 thru 63.3232+- nvidia,rx-clk-tap-delay: Delays the clock coming in from the external device3333+ with this tap value. This property is used to adjust the Tegra SPI master3434+ clock with respect to the data from the SPI slave device.3535+ Tap values vary based on the platform design trace lengths from Tegra SPI3636+ to corresponding slave devices. Valid tap values are from 0 thru 63.3737+2638Example:27392840spi@7000d600 {···5038 reset-names = "spi";5139 dmas = <&apbdma 16>, <&apbdma 16>;5240 dma-names = "rx", "tx";4141+ <spi-client>@<bus_num> {4242+ ...4343+ ...4444+ nvidia,rx-clk-tap-delay = <0>;4545+ nvidia,tx-clk-tap-delay = <16>;4646+ ...4747+ };4848+5349};
···88- interrupts : One interrupt, used by the controller.99- #address-cells : <1>, as required by generic SPI binding.1010- #size-cells : <0>, also as required by generic SPI binding.1111+- clocks : phandles for the clocks, see the description of clock-names below.1212+ The phandle for the "ssi_clk" is required. The phandle for the "pclk" clock1313+ is optional. If a single clock is specified but no clock-name, it is the1414+ "ssi_clk" clock. If both clocks are listed, the "ssi_clk" must be first.11151216Optional properties:1313-- cs-gpios : Specifies the gpio pis to be used for chipselects.1717+- clock-names : Contains the names of the clocks:1818+ "ssi_clk", for the core clock used to generate the external SPI clock.1919+ "pclk", the interface clock, required for register access.2020+- cs-gpios : Specifies the gpio pins to be used for chipselects.1421- num-cs : The number of chipselects. If omitted, this will default to 4.1522- reg-io-width : The I/O register width (in bytes) implemented by this1623 device. Supported values are 2 or 4 (the default).···3225 interrupts = <0 154 4>;3326 #address-cells = <1>;3427 #size-cells = <0>;2828+ clocks = <&spi_m_clk>;3529 num-cs = <2>;3630 cs-gpios = <&gpio0 13 0>,3731 <&gpio0 14 0>;
···77- reg : address and length of the lpspi master registers88- interrupt-parent : core interrupt controller99- interrupts : lpspi interrupt1010-- clocks : lpspi clock specifier1010+- clocks : lpspi clock specifier. Its number and order need to correspond to the1111+ value in clock-names.1212+- clock-names : Corresponding to per clock and ipg clock in "clocks"1313+ respectively. In i.MX7ULP, it only has per clk, so use CLK_DUMMY1414+ to fill the "ipg" blank.1115- spi-slave : spi slave mode support. In slave mode, add this attribute without1216 value. In master mode, remove it.1317···2218 reg = <0x40290000 0x10000>;2319 interrupt-parent = <&intc>;2420 interrupts = <GIC_SPI 28 IRQ_TYPE_LEVEL_HIGH>;2525- clocks = <&clks IMX7ULP_CLK_LPSPI2>;2121+ clocks = <&clks IMX7ULP_CLK_LPSPI2>,2222+ <&clks IMX7ULP_CLK_DUMMY>;2323+ clock-names = "per", "ipg";2624 spi-slave;2725};
···1010 - mediatek,mt8135-spi: for mt8135 platforms1111 - mediatek,mt8173-spi: for mt8173 platforms1212 - mediatek,mt8183-spi: for mt8183 platforms1313+ - "mediatek,mt8516-spi", "mediatek,mt2712-spi": for mt8516 platforms13141415- #address-cells: should be 1.1516
···11+Binding for MTK SPI controller (MT7621 MIPS)22+33+Required properties:44+- compatible: Should be one of the following:55+ - "ralink,mt7621-spi": for mt7621/mt7628/mt7688 platforms66+- #address-cells: should be 1.77+- #size-cells: should be 0.88+- reg: Address and length of the register set for the device99+- resets: phandle to the reset controller asserting this device in1010+ reset1111+ See ../reset/reset.txt for details.1212+1313+Optional properties:1414+- cs-gpios: see spi-bus.txt.1515+1616+Example:1717+1818+- SoC Specific Portion:1919+spi0: spi@b00 {2020+ compatible = "ralink,mt7621-spi";2121+ reg = <0xb00 0x100>;2222+ #address-cells = <1>;2323+ #size-cells = <0>;2424+ resets = <&rstctrl 18>;2525+ reset-names = "spi";2626+};
···11+Xilinx Zynq QSPI controller Device Tree Bindings22+-------------------------------------------------------------------33+44+Required properties:55+- compatible : Should be "xlnx,zynq-qspi-1.0".66+- reg : Physical base address and size of QSPI registers map.77+- interrupts : Property with a value describing the interrupt88+ number.99+- clock-names : List of input clock names - "ref_clk", "pclk"1010+ (See clock bindings for details).1111+- clocks : Clock phandles (see clock bindings for details).1212+1313+Optional properties:1414+- num-cs : Number of chip selects used.1515+1616+Example:1717+ qspi: spi@e000d000 {1818+ compatible = "xlnx,zynq-qspi-1.0";1919+ reg = <0xe000d000 0x1000>;2020+ interrupt-parent = <&intc>;2121+ interrupts = <0 19 4>;2222+ clock-names = "ref_clk", "pclk";2323+ clocks = <&clkc 10>, <&clkc 43>;2424+ num-cs = <1>;2525+ };
+6
Documentation/spi/spi-summary
···572572 0: transfer is finished573573 1: transfer is still in progress574574575575+ master->set_cs_timing(struct spi_device *spi, u8 setup_clk_cycles,576576+ u8 hold_clk_cycles, u8 inactive_clk_cycles)577577+ This method allows SPI client drivers to request SPI master controller578578+ for configuring device specific CS setup, hold and inactive timing579579+ requirements.580580+575581 DEPRECATED METHODS576582577583 master->transfer(struct spi_device *spi, struct spi_message *message)
···426426 say Y or M here.If you are not sure, say N.427427 SPI drivers for Mediatek MT65XX and MT81XX series ARM SoCs.428428429429+config SPI_MT7621430430+ tristate "MediaTek MT7621 SPI Controller"431431+ depends on RALINK || COMPILE_TEST432432+ help433433+ This selects a driver for the MediaTek MT7621 SPI Controller.434434+429435config SPI_NPCM_PSPI430436 tristate "Nuvoton NPCM PSPI Controller"431437 depends on ARCH_NPCM || COMPILE_TEST···848842 16 bit words in SPI mode 0, automatically asserting CS on transfer849843 start and deasserting on end.850844845845+config SPI_ZYNQ_QSPI846846+ tristate "Xilinx Zynq QSPI controller"847847+ depends on ARCH_ZYNQ || COMPILE_TEST848848+ help849849+ This enables support for the Zynq Quad SPI controller850850+ in master mode.851851+ This controller only supports SPI memory interface.852852+851853config SPI_ZYNQMP_GQSPI852854 tristate "Xilinx ZynqMP GQSPI controller"853853- depends on SPI_MASTER && HAS_DMA855855+ depends on (SPI_MASTER && HAS_DMA) || COMPILE_TEST854856 help855857 Enables Xilinx GQSPI controller driver for Zynq UltraScale+ MPSoC.856858
···335335 return 1;336336}337337338338-/*339339- * DMA support340340- *341341- * this implementation has currently a few issues in so far as it does342342- * not work arrount limitations of the HW.343343- *344344- * the main one being that DMA transfers are limited to 16 bit345345- * (so 0 to 65535 bytes) by the SPI HW due to BCM2835_SPI_DLEN346346- *347347- * there may be a few more border-cases we may need to address as well348348- * but unfortunately this would mean splitting up the scatter-gather349349- * list making it slightly unpractical...350350- */351351-352338/**353339 * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA354340 * @master: SPI master···616630 if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)617631 return false;618632619619- /* BCM2835_SPI_DLEN has defined a max transfer size as620620- * 16 bit, so max is 65535621621- * we can revisit this by using an alternative transfer622622- * method - ideally this would get done without any more623623- * interaction...624624- */625625- if (tfr->len > 65535) {626626- dev_warn_once(&spi->dev,627627- "transfer size of %d too big for dma-transfer\n",628628- tfr->len);629629- return false;630630- }631631-632633 /* return OK */633634 return true;634635}···680707681708 /* all went well, so set can_dma */682709 master->can_dma = bcm2835_spi_can_dma;683683- master->max_dma_len = 65535; /* limitation by BCM2835_SPI_DLEN */684710 /* need to do TX AND RX DMA, so we need dummy buffers */685711 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;686712···816844 struct spi_device *spi = msg->spi;817845 struct bcm2835_spi *bs = spi_master_get_devdata(master);818846 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);847847+ int ret;848848+849849+ /*850850+ * DMA transfers are limited to 16 bit (0 to 65535 bytes) by the SPI HW851851+ * due to DLEN. Split up transfers (32-bit FIFO aligned) if the limit is852852+ * exceeded.853853+ */854854+ ret = spi_split_transfers_maxsize(master, msg, 65532,855855+ GFP_KERNEL | GFP_DMA);856856+ if (ret)857857+ return ret;819858820859 cs &= ~(BCM2835_SPI_CS_CPOL | BCM2835_SPI_CS_CPHA);821860
+148-57
drivers/spi/spi-bcm2835aux.c
···21212222#include <linux/clk.h>2323#include <linux/completion.h>2424+#include <linux/debugfs.h>2425#include <linux/delay.h>2526#include <linux/err.h>2627#include <linux/interrupt.h>···3635#include <linux/regmap.h>3736#include <linux/spi/spi.h>3837#include <linux/spinlock.h>3838+3939+/* define polling limits */4040+static unsigned int polling_limit_us = 30;4141+module_param(polling_limit_us, uint, 0664);4242+MODULE_PARM_DESC(polling_limit_us,4343+ "time in us to run a transfer in polling mode - if zero no polling is used\n");39444045/*4146 * spi register defines···9588#define BCM2835_AUX_SPI_STAT_BUSY 0x000000409689#define BCM2835_AUX_SPI_STAT_BITCOUNT 0x0000003F97909898-/* timeout values */9999-#define BCM2835_AUX_SPI_POLLING_LIMIT_US 30100100-#define BCM2835_AUX_SPI_POLLING_JIFFIES 2101101-10291struct bcm2835aux_spi {10392 void __iomem *regs;10493 struct clk *clk;···105102 int tx_len;106103 int rx_len;107104 int pending;105105+106106+ u64 count_transfer_polling;107107+ u64 count_transfer_irq;108108+ u64 count_transfer_irq_after_poll;109109+110110+ struct dentry *debugfs_dir;108111};112112+113113+#if defined(CONFIG_DEBUG_FS)114114+static void bcm2835aux_debugfs_create(struct bcm2835aux_spi *bs,115115+ const char *dname)116116+{117117+ char name[64];118118+ struct dentry *dir;119119+120120+ /* get full name */121121+ snprintf(name, sizeof(name), "spi-bcm2835aux-%s", dname);122122+123123+ /* the base directory */124124+ dir = debugfs_create_dir(name, NULL);125125+ bs->debugfs_dir = dir;126126+127127+ /* the counters */128128+ debugfs_create_u64("count_transfer_polling", 0444, dir,129129+ &bs->count_transfer_polling);130130+ debugfs_create_u64("count_transfer_irq", 0444, dir,131131+ &bs->count_transfer_irq);132132+ debugfs_create_u64("count_transfer_irq_after_poll", 0444, dir,133133+ &bs->count_transfer_irq_after_poll);134134+}135135+136136+static void bcm2835aux_debugfs_remove(struct bcm2835aux_spi *bs)137137+{138138+ debugfs_remove_recursive(bs->debugfs_dir);139139+ bs->debugfs_dir = NULL;140140+}141141+#else142142+static void bcm2835aux_debugfs_create(struct bcm2835aux_spi *bs,143143+ const char *dname)144144+{145145+}146146+147147+static void bcm2835aux_debugfs_remove(struct bcm2835aux_spi *bs)148148+{149149+}150150+#endif /* CONFIG_DEBUG_FS */109151110152static inline u32 bcm2835aux_rd(struct bcm2835aux_spi *bs, unsigned reg)111153{···171123 data = bcm2835aux_rd(bs, BCM2835_AUX_SPI_IO);172124 if (bs->rx_buf) {173125 switch (count) {174174- case 4:175175- *bs->rx_buf++ = (data >> 24) & 0xff;176176- /* fallthrough */177126 case 3:178127 *bs->rx_buf++ = (data >> 16) & 0xff;179128 /* fallthrough */···223178 BCM2835_AUX_SPI_CNTL0_CLEARFIFO);224179}225180226226-static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)181181+static void bcm2835aux_spi_transfer_helper(struct bcm2835aux_spi *bs)227182{228228- struct spi_master *master = dev_id;229229- struct bcm2835aux_spi *bs = spi_master_get_devdata(master);230230- irqreturn_t ret = IRQ_NONE;231231-232232- /* IRQ may be shared, so return if our interrupts are disabled */233233- if (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_CNTL1) &234234- (BCM2835_AUX_SPI_CNTL1_TXEMPTY | BCM2835_AUX_SPI_CNTL1_IDLE)))235235- return ret;183183+ u32 stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);236184237185 /* check if we have data to read */238238- while (bs->rx_len &&239239- (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &240240- BCM2835_AUX_SPI_STAT_RX_EMPTY))) {186186+ for (; bs->rx_len && (stat & BCM2835_AUX_SPI_STAT_RX_LVL);187187+ stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT))241188 bcm2835aux_rd_fifo(bs);242242- ret = IRQ_HANDLED;243243- }244189245190 /* check if we have data to write */246191 while (bs->tx_len &&···238203 (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &239204 BCM2835_AUX_SPI_STAT_TX_FULL))) {240205 bcm2835aux_wr_fifo(bs);241241- ret = IRQ_HANDLED;242206 }207207+}243208244244- /* and check if we have reached "done" */245245- while (bs->rx_len &&246246- (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &247247- BCM2835_AUX_SPI_STAT_BUSY))) {248248- bcm2835aux_rd_fifo(bs);249249- ret = IRQ_HANDLED;250250- }209209+static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)210210+{211211+ struct spi_master *master = dev_id;212212+ struct bcm2835aux_spi *bs = spi_master_get_devdata(master);213213+214214+ /* IRQ may be shared, so return if our interrupts are disabled */215215+ if (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_CNTL1) &216216+ (BCM2835_AUX_SPI_CNTL1_TXEMPTY | BCM2835_AUX_SPI_CNTL1_IDLE)))217217+ return IRQ_NONE;218218+219219+ /* do common fifo handling */220220+ bcm2835aux_spi_transfer_helper(bs);251221252222 if (!bs->tx_len) {253223 /* disable tx fifo empty interrupt */···266226 complete(&master->xfer_completion);267227 }268228269269- /* and return */270270- return ret;229229+ return IRQ_HANDLED;271230}272231273232static int __bcm2835aux_spi_transfer_one_irq(struct spi_master *master,···290251{291252 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);292253254254+ /* update statistics */255255+ bs->count_transfer_irq++;256256+293257 /* fill in registers and fifos before enabling interrupts */294258 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);295259 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);···315273{316274 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);317275 unsigned long timeout;318318- u32 stat;276276+277277+ /* update statistics */278278+ bs->count_transfer_polling++;319279320280 /* configure spi */321281 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);322282 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);323283324324- /* set the timeout */325325- timeout = jiffies + BCM2835_AUX_SPI_POLLING_JIFFIES;284284+ /* set the timeout to at least 2 jiffies */285285+ timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;326286327287 /* loop until finished the transfer */328288 while (bs->rx_len) {329329- /* read status */330330- stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);331289332332- /* fill in tx fifo with remaining data */333333- if ((bs->tx_len) && (!(stat & BCM2835_AUX_SPI_STAT_TX_FULL))) {334334- bcm2835aux_wr_fifo(bs);335335- continue;336336- }337337-338338- /* read data from fifo for both cases */339339- if (!(stat & BCM2835_AUX_SPI_STAT_RX_EMPTY)) {340340- bcm2835aux_rd_fifo(bs);341341- continue;342342- }343343- if (!(stat & BCM2835_AUX_SPI_STAT_BUSY)) {344344- bcm2835aux_rd_fifo(bs);345345- continue;346346- }290290+ /* do common fifo handling */291291+ bcm2835aux_spi_transfer_helper(bs);347292348293 /* there is still data pending to read check the timeout */349294 if (bs->rx_len && time_after(jiffies, timeout)) {···339310 jiffies - timeout,340311 bs->tx_len, bs->rx_len);341312 /* forward to interrupt handler */313313+ bs->count_transfer_irq_after_poll++;342314 return __bcm2835aux_spi_transfer_one_irq(master,343315 spi, tfr);344316 }···354324 struct spi_transfer *tfr)355325{356326 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);357357- unsigned long spi_hz, clk_hz, speed;358358- unsigned long spi_used_hz;327327+ unsigned long spi_hz, clk_hz, speed, spi_used_hz;328328+ unsigned long hz_per_byte, byte_limit;359329360330 /* calculate the registers to handle361331 *···399369 * of Hz per byte per polling limit. E.g., we can transfer 1 byte in400370 * 30 µs per 300,000 Hz of bus clock.401371 */402402-#define HZ_PER_BYTE ((9 * 1000000) / BCM2835_AUX_SPI_POLLING_LIMIT_US)372372+ hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;373373+ byte_limit = hz_per_byte ? spi_used_hz / hz_per_byte : 1;374374+403375 /* run in polling mode for short transfers */404404- if (tfr->len < spi_used_hz / HZ_PER_BYTE)376376+ if (tfr->len < byte_limit)405377 return bcm2835aux_spi_transfer_one_poll(master, spi, tfr);406378407379 /* run in interrupt mode for all others */408380 return bcm2835aux_spi_transfer_one_irq(master, spi, tfr);409409-#undef HZ_PER_BYTE410381}411382412383static int bcm2835aux_spi_prepare_message(struct spi_master *master,···452421 bcm2835aux_spi_reset_hw(bs);453422}454423424424+static int bcm2835aux_spi_setup(struct spi_device *spi)425425+{426426+ int ret;427427+428428+ /* sanity check for native cs */429429+ if (spi->mode & SPI_NO_CS)430430+ return 0;431431+ if (gpio_is_valid(spi->cs_gpio)) {432432+ /* with gpio-cs set the GPIO to the correct level433433+ * and as output (in case the dt has the gpio not configured434434+ * as output but native cs)435435+ */436436+ ret = gpio_direction_output(spi->cs_gpio,437437+ (spi->mode & SPI_CS_HIGH) ? 0 : 1);438438+ if (ret)439439+ dev_err(&spi->dev,440440+ "could not set gpio %i as output: %i\n",441441+ spi->cs_gpio, ret);442442+443443+ return ret;444444+ }445445+446446+ /* for dt-backwards compatibility: only support native on CS0447447+ * known things not supported with broken native CS:448448+ * * multiple chip-selects: cs0-cs2 are all449449+ * simultaniously asserted whenever there is a transfer450450+ * this even includes SPI_NO_CS451451+ * * SPI_CS_HIGH: cs are always asserted low452452+ * * cs_change: cs is deasserted after each spi_transfer453453+ * * cs_delay_usec: cs is always deasserted one SCK cycle454454+ * after the last transfer455455+ * probably more...456456+ */457457+ dev_warn(&spi->dev,458458+ "Native CS is not supported - please configure cs-gpio in device-tree\n");459459+460460+ if (spi->chip_select == 0)461461+ return 0;462462+463463+ dev_warn(&spi->dev, "Native CS is not working for cs > 0\n");464464+465465+ return -EINVAL;466466+}467467+455468static int bcm2835aux_spi_probe(struct platform_device *pdev)456469{457470 struct spi_master *master;···513438 platform_set_drvdata(pdev, master);514439 master->mode_bits = (SPI_CPOL | SPI_CS_HIGH | SPI_NO_CS);515440 master->bits_per_word_mask = SPI_BPW_MASK(8);516516- master->num_chipselect = -1;441441+ /* even though the driver never officially supported native CS442442+ * allow a single native CS for legacy DT support purposes when443443+ * no cs-gpio is configured.444444+ * Known limitations for native cs are:445445+ * * multiple chip-selects: cs0-cs2 are all simultaniously asserted446446+ * whenever there is a transfer - this even includes SPI_NO_CS447447+ * * SPI_CS_HIGH: is ignores - cs are always asserted low448448+ * * cs_change: cs is deasserted after each spi_transfer449449+ * * cs_delay_usec: cs is always deasserted one SCK cycle after450450+ * a spi_transfer451451+ */452452+ master->num_chipselect = 1;453453+ master->setup = bcm2835aux_spi_setup;517454 master->transfer_one = bcm2835aux_spi_transfer_one;518455 master->handle_err = bcm2835aux_spi_handle_err;519456 master->prepare_message = bcm2835aux_spi_prepare_message;···589502 goto out_clk_disable;590503 }591504505505+ bcm2835aux_debugfs_create(bs, dev_name(&pdev->dev));506506+592507 return 0;593508594509out_clk_disable:···604515{605516 struct spi_master *master = platform_get_drvdata(pdev);606517 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);518518+519519+ bcm2835aux_debugfs_remove(bs);607520608521 bcm2835aux_spi_reset_hw(bs);609522
+39-27
drivers/spi/spi-bitbang.c
···335335336336/*----------------------------------------------------------------------*/337337338338+int spi_bitbang_init(struct spi_bitbang *bitbang)339339+{340340+ struct spi_master *master = bitbang->master;341341+342342+ if (!master || !bitbang->chipselect)343343+ return -EINVAL;344344+345345+ mutex_init(&bitbang->lock);346346+347347+ if (!master->mode_bits)348348+ master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;349349+350350+ if (master->transfer || master->transfer_one_message)351351+ return -EINVAL;352352+353353+ master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;354354+ master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;355355+ master->transfer_one = spi_bitbang_transfer_one;356356+ master->set_cs = spi_bitbang_set_cs;357357+358358+ if (!bitbang->txrx_bufs) {359359+ bitbang->use_dma = 0;360360+ bitbang->txrx_bufs = spi_bitbang_bufs;361361+ if (!master->setup) {362362+ if (!bitbang->setup_transfer)363363+ bitbang->setup_transfer =364364+ spi_bitbang_setup_transfer;365365+ master->setup = spi_bitbang_setup;366366+ master->cleanup = spi_bitbang_cleanup;367367+ }368368+ }369369+370370+ return 0;371371+}372372+EXPORT_SYMBOL_GPL(spi_bitbang_init);373373+338374/**339375 * spi_bitbang_start - start up a polled/bitbanging SPI master driver340376 * @bitbang: driver handle···404368 struct spi_master *master = bitbang->master;405369 int ret;406370407407- if (!master || !bitbang->chipselect)408408- return -EINVAL;409409-410410- mutex_init(&bitbang->lock);411411-412412- if (!master->mode_bits)413413- master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;414414-415415- if (master->transfer || master->transfer_one_message)416416- return -EINVAL;417417-418418- master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;419419- master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;420420- master->transfer_one = spi_bitbang_transfer_one;421421- master->set_cs = spi_bitbang_set_cs;422422-423423- if (!bitbang->txrx_bufs) {424424- bitbang->use_dma = 0;425425- bitbang->txrx_bufs = spi_bitbang_bufs;426426- if (!master->setup) {427427- if (!bitbang->setup_transfer)428428- bitbang->setup_transfer =429429- spi_bitbang_setup_transfer;430430- master->setup = spi_bitbang_setup;431431- master->cleanup = spi_bitbang_cleanup;432432- }433433- }371371+ ret = spi_bitbang_init(bitbang);372372+ if (ret)373373+ return ret;434374435375 /* driver may get busy before register() returns, especially436376 * if someone registered boardinfo for devices
···2828#include <linux/platform_device.h>2929#include <linux/sched.h>3030#include <linux/scatterlist.h>3131-#include <linux/gpio.h>3231#include <linux/spi/spi.h>33323433#include <linux/platform_data/dma-ep93xx.h>···675676 if (!master)676677 return -ENOMEM;677678679679+ master->use_gpio_descriptors = true;678680 master->prepare_transfer_hardware = ep93xx_spi_prepare_hardware;679681 master->unprepare_transfer_hardware = ep93xx_spi_unprepare_hardware;680682 master->prepare_message = ep93xx_spi_prepare_message;···683683 master->bus_num = pdev->id;684684 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;685685 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);686686-687687- master->num_chipselect = info->num_chipselect;688688- master->cs_gpios = devm_kcalloc(&master->dev,689689- master->num_chipselect, sizeof(int),690690- GFP_KERNEL);691691- if (!master->cs_gpios) {692692- error = -ENOMEM;693693- goto fail_release_master;694694- }695695-696696- for (i = 0; i < master->num_chipselect; i++) {697697- master->cs_gpios[i] = info->chipselect[i];698698-699699- if (!gpio_is_valid(master->cs_gpios[i]))700700- continue;701701-702702- error = devm_gpio_request_one(&pdev->dev, master->cs_gpios[i],703703- GPIOF_OUT_INIT_HIGH,704704- "ep93xx-spi");705705- if (error) {706706- dev_err(&pdev->dev, "could not request cs gpio %d\n",707707- master->cs_gpios[i]);708708- goto fail_release_master;709709- }710710- }686686+ /*687687+ * The SPI core will count the number of GPIO descriptors to figure688688+ * out the number of chip selects available on the platform.689689+ */690690+ master->num_chipselect = 0;711691712692 platform_set_drvdata(pdev, master);713693
+2
drivers/spi/spi-fsl-lib.h
···95959696struct mpc8xxx_spi_probe_info {9797 struct fsl_spi_platform_data pdata;9898+ int ngpios;9899 int *gpios;99100 bool *alow_flags;101101+ __be32 __iomem *immr_spi_cs;100102};101103102104extern u32 mpc8xxx_spi_tx_buf_u8(struct mpc8xxx_spi *mpc8xxx_spi);
+506-63
drivers/spi/spi-fsl-lpspi.c
···88#include <linux/clk.h>99#include <linux/completion.h>1010#include <linux/delay.h>1111+#include <linux/dmaengine.h>1212+#include <linux/dma-mapping.h>1113#include <linux/err.h>1414+#include <linux/gpio.h>1215#include <linux/interrupt.h>1316#include <linux/io.h>1417#include <linux/irq.h>···1916#include <linux/module.h>2017#include <linux/of.h>2118#include <linux/of_device.h>1919+#include <linux/of_gpio.h>2020+#include <linux/pinctrl/consumer.h>2221#include <linux/platform_device.h>2222+#include <linux/platform_data/dma-imx.h>2323+#include <linux/platform_data/spi-imx.h>2424+#include <linux/pm_runtime.h>2325#include <linux/slab.h>2426#include <linux/spi/spi.h>2527#include <linux/spi/spi_bitbang.h>2628#include <linux/types.h>27292830#define DRIVER_NAME "fsl_lpspi"3131+3232+#define FSL_LPSPI_RPM_TIMEOUT 50 /* 50ms */3333+3434+/* The maximum bytes that edma can transfer once.*/3535+#define FSL_LPSPI_MAX_EDMA_BYTES ((1 << 15) - 1)29363037/* i.MX7ULP LPSPI registers */3138#define IMX7ULP_VERID 0x0···7057#define IER_FCIE BIT(9)7158#define IER_RDIE BIT(1)7259#define IER_TDIE BIT(0)6060+#define DER_RDDE BIT(1)6161+#define DER_TDDE BIT(0)7362#define CFGR1_PCSCFG BIT(27)7463#define CFGR1_PINCFG (BIT(24)|BIT(25))7564#define CFGR1_PCSPOL BIT(8)···9984struct fsl_lpspi_data {10085 struct device *dev;10186 void __iomem *base;102102- struct clk *clk;8787+ unsigned long base_phys;8888+ struct clk *clk_ipg;8989+ struct clk *clk_per;10390 bool is_slave;9191+ bool is_first_byte;1049210593 void *rx_buf;10694 const void *tx_buf;···119101 struct completion xfer_done;120102121103 bool slave_aborted;104104+105105+ /* DMA */106106+ bool usedma;107107+ struct completion dma_rx_completion;108108+ struct completion dma_tx_completion;109109+110110+ int chipselect[0];122111};123112124113static const struct of_device_id fsl_lpspi_dt_ids[] = {···172147 writel(enable, fsl_lpspi->base + IMX7ULP_IER);173148}174149150150+static int fsl_lpspi_bytes_per_word(const int bpw)151151+{152152+ return DIV_ROUND_UP(bpw, BITS_PER_BYTE);153153+}154154+155155+static bool fsl_lpspi_can_dma(struct spi_controller *controller,156156+ struct spi_device *spi,157157+ struct spi_transfer *transfer)158158+{159159+ unsigned int bytes_per_word;160160+161161+ if (!controller->dma_rx)162162+ return false;163163+164164+ bytes_per_word = fsl_lpspi_bytes_per_word(transfer->bits_per_word);165165+166166+ switch (bytes_per_word)167167+ {168168+ case 1:169169+ case 2:170170+ case 4:171171+ break;172172+ default:173173+ return false;174174+ }175175+176176+ return true;177177+}178178+175179static int lpspi_prepare_xfer_hardware(struct spi_controller *controller)176180{177181 struct fsl_lpspi_data *fsl_lpspi =178182 spi_controller_get_devdata(controller);183183+ int ret;179184180180- return clk_prepare_enable(fsl_lpspi->clk);185185+ ret = pm_runtime_get_sync(fsl_lpspi->dev);186186+ if (ret < 0) {187187+ dev_err(fsl_lpspi->dev, "failed to enable clock\n");188188+ return ret;189189+ }190190+191191+ return 0;181192}182193183194static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller)···221160 struct fsl_lpspi_data *fsl_lpspi =222161 spi_controller_get_devdata(controller);223162224224- clk_disable_unprepare(fsl_lpspi->clk);163163+ pm_runtime_mark_last_busy(fsl_lpspi->dev);164164+ pm_runtime_put_autosuspend(fsl_lpspi->dev);165165+166166+ return 0;167167+}168168+169169+static int fsl_lpspi_prepare_message(struct spi_controller *controller,170170+ struct spi_message *msg)171171+{172172+ struct fsl_lpspi_data *fsl_lpspi =173173+ spi_controller_get_devdata(controller);174174+ struct spi_device *spi = msg->spi;175175+ int gpio = fsl_lpspi->chipselect[spi->chip_select];176176+177177+ if (gpio_is_valid(gpio))178178+ gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);225179226180 return 0;227181}···273197 fsl_lpspi->rx(fsl_lpspi);274198}275199276276-static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi,277277- bool is_first_xfer)200200+static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi)278201{279202 u32 temp = 0;280203···288213 * For the first transfer, clear TCR_CONTC to assert SS.289214 * For subsequent transfer, set TCR_CONTC to keep SS asserted.290215 */291291- temp |= TCR_CONT;292292- if (is_first_xfer)293293- temp &= ~TCR_CONTC;294294- else295295- temp |= TCR_CONTC;216216+ if (!fsl_lpspi->usedma) {217217+ temp |= TCR_CONT;218218+ if (fsl_lpspi->is_first_byte)219219+ temp &= ~TCR_CONTC;220220+ else221221+ temp |= TCR_CONTC;222222+ }296223 }297224 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);298225···305228{306229 u32 temp;307230308308- temp = fsl_lpspi->watermark >> 1 | (fsl_lpspi->watermark >> 1) << 16;231231+ if (!fsl_lpspi->usedma)232232+ temp = fsl_lpspi->watermark >> 1 |233233+ (fsl_lpspi->watermark >> 1) << 16;234234+ else235235+ temp = fsl_lpspi->watermark >> 1;309236310237 writel(temp, fsl_lpspi->base + IMX7ULP_FCR);311238···322241 unsigned int perclk_rate, scldiv;323242 u8 prescale;324243325325- perclk_rate = clk_get_rate(fsl_lpspi->clk);244244+ perclk_rate = clk_get_rate(fsl_lpspi->clk_per);245245+246246+ if (config.speed_hz > perclk_rate / 2) {247247+ dev_err(fsl_lpspi->dev,248248+ "per-clk should be at least two times of transfer speed");249249+ return -EINVAL;250250+ }251251+326252 for (prescale = 0; prescale < 8; prescale++) {327253 scldiv = perclk_rate /328254 (clkdivs[prescale] * config.speed_hz) - 2;···345257 writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),346258 fsl_lpspi->base + IMX7ULP_CCR);347259348348- dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n",260260+ dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale=%d, scldiv=%d\n",349261 perclk_rate, config.speed_hz, prescale, scldiv);262262+263263+ return 0;264264+}265265+266266+static int fsl_lpspi_dma_configure(struct spi_controller *controller)267267+{268268+ int ret;269269+ enum dma_slave_buswidth buswidth;270270+ struct dma_slave_config rx = {}, tx = {};271271+ struct fsl_lpspi_data *fsl_lpspi =272272+ spi_controller_get_devdata(controller);273273+274274+ switch (fsl_lpspi_bytes_per_word(fsl_lpspi->config.bpw)) {275275+ case 4:276276+ buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;277277+ break;278278+ case 2:279279+ buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;280280+ break;281281+ case 1:282282+ buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;283283+ break;284284+ default:285285+ return -EINVAL;286286+ }287287+288288+ tx.direction = DMA_MEM_TO_DEV;289289+ tx.dst_addr = fsl_lpspi->base_phys + IMX7ULP_TDR;290290+ tx.dst_addr_width = buswidth;291291+ tx.dst_maxburst = 1;292292+ ret = dmaengine_slave_config(controller->dma_tx, &tx);293293+ if (ret) {294294+ dev_err(fsl_lpspi->dev, "TX dma configuration failed with %d\n",295295+ ret);296296+ return ret;297297+ }298298+299299+ rx.direction = DMA_DEV_TO_MEM;300300+ rx.src_addr = fsl_lpspi->base_phys + IMX7ULP_RDR;301301+ rx.src_addr_width = buswidth;302302+ rx.src_maxburst = 1;303303+ ret = dmaengine_slave_config(controller->dma_rx, &rx);304304+ if (ret) {305305+ dev_err(fsl_lpspi->dev, "RX dma configuration failed with %d\n",306306+ ret);307307+ return ret;308308+ }350309351310 return 0;352311}···423288 temp |= CR_RRF | CR_RTF | CR_MEN;424289 writel(temp, fsl_lpspi->base + IMX7ULP_CR);425290291291+ temp = 0;292292+ if (fsl_lpspi->usedma)293293+ temp = DER_TDDE | DER_RDDE;294294+ writel(temp, fsl_lpspi->base + IMX7ULP_DER);295295+426296 return 0;427297}428298429429-static void fsl_lpspi_setup_transfer(struct spi_device *spi,299299+static int fsl_lpspi_setup_transfer(struct spi_controller *controller,300300+ struct spi_device *spi,430301 struct spi_transfer *t)431302{432303 struct fsl_lpspi_data *fsl_lpspi =433304 spi_controller_get_devdata(spi->controller);434305306306+ if (t == NULL)307307+ return -EINVAL;308308+435309 fsl_lpspi->config.mode = spi->mode;436436- fsl_lpspi->config.bpw = t ? t->bits_per_word : spi->bits_per_word;437437- fsl_lpspi->config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;310310+ fsl_lpspi->config.bpw = t->bits_per_word;311311+ fsl_lpspi->config.speed_hz = t->speed_hz;438312 fsl_lpspi->config.chip_select = spi->chip_select;439313440314 if (!fsl_lpspi->config.speed_hz)···468324 else469325 fsl_lpspi->watermark = fsl_lpspi->txfifosize;470326471471- fsl_lpspi_config(fsl_lpspi);327327+ if (fsl_lpspi_can_dma(controller, spi, t))328328+ fsl_lpspi->usedma = 1;329329+ else330330+ fsl_lpspi->usedma = 0;331331+332332+ return fsl_lpspi_config(fsl_lpspi);472333}473334474335static int fsl_lpspi_slave_abort(struct spi_controller *controller)···482333 spi_controller_get_devdata(controller);483334484335 fsl_lpspi->slave_aborted = true;485485- complete(&fsl_lpspi->xfer_done);336336+ if (!fsl_lpspi->usedma)337337+ complete(&fsl_lpspi->xfer_done);338338+ else {339339+ complete(&fsl_lpspi->dma_tx_completion);340340+ complete(&fsl_lpspi->dma_rx_completion);341341+ }342342+486343 return 0;487344}488345···517362{518363 u32 temp;519364520520- /* Disable all interrupt */521521- fsl_lpspi_intctrl(fsl_lpspi, 0);365365+ if (!fsl_lpspi->usedma) {366366+ /* Disable all interrupt */367367+ fsl_lpspi_intctrl(fsl_lpspi, 0);368368+ }522369523370 /* W1C for all flags in SR */524371 temp = 0x3F << 8;···533376 return 0;534377}535378536536-static int fsl_lpspi_transfer_one(struct spi_controller *controller,537537- struct spi_device *spi,379379+static void fsl_lpspi_dma_rx_callback(void *cookie)380380+{381381+ struct fsl_lpspi_data *fsl_lpspi = (struct fsl_lpspi_data *)cookie;382382+383383+ complete(&fsl_lpspi->dma_rx_completion);384384+}385385+386386+static void fsl_lpspi_dma_tx_callback(void *cookie)387387+{388388+ struct fsl_lpspi_data *fsl_lpspi = (struct fsl_lpspi_data *)cookie;389389+390390+ complete(&fsl_lpspi->dma_tx_completion);391391+}392392+393393+static int fsl_lpspi_calculate_timeout(struct fsl_lpspi_data *fsl_lpspi,394394+ int size)395395+{396396+ unsigned long timeout = 0;397397+398398+ /* Time with actual data transfer and CS change delay related to HW */399399+ timeout = (8 + 4) * size / fsl_lpspi->config.speed_hz;400400+401401+ /* Add extra second for scheduler related activities */402402+ timeout += 1;403403+404404+ /* Double calculated timeout */405405+ return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);406406+}407407+408408+static int fsl_lpspi_dma_transfer(struct spi_controller *controller,409409+ struct fsl_lpspi_data *fsl_lpspi,410410+ struct spi_transfer *transfer)411411+{412412+ struct dma_async_tx_descriptor *desc_tx, *desc_rx;413413+ unsigned long transfer_timeout;414414+ unsigned long timeout;415415+ struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;416416+ int ret;417417+418418+ ret = fsl_lpspi_dma_configure(controller);419419+ if (ret)420420+ return ret;421421+422422+ desc_rx = dmaengine_prep_slave_sg(controller->dma_rx,423423+ rx->sgl, rx->nents, DMA_DEV_TO_MEM,424424+ DMA_PREP_INTERRUPT | DMA_CTRL_ACK);425425+ if (!desc_rx)426426+ return -EINVAL;427427+428428+ desc_rx->callback = fsl_lpspi_dma_rx_callback;429429+ desc_rx->callback_param = (void *)fsl_lpspi;430430+ dmaengine_submit(desc_rx);431431+ reinit_completion(&fsl_lpspi->dma_rx_completion);432432+ dma_async_issue_pending(controller->dma_rx);433433+434434+ desc_tx = dmaengine_prep_slave_sg(controller->dma_tx,435435+ tx->sgl, tx->nents, DMA_MEM_TO_DEV,436436+ DMA_PREP_INTERRUPT | DMA_CTRL_ACK);437437+ if (!desc_tx) {438438+ dmaengine_terminate_all(controller->dma_tx);439439+ return -EINVAL;440440+ }441441+442442+ desc_tx->callback = fsl_lpspi_dma_tx_callback;443443+ desc_tx->callback_param = (void *)fsl_lpspi;444444+ dmaengine_submit(desc_tx);445445+ reinit_completion(&fsl_lpspi->dma_tx_completion);446446+ dma_async_issue_pending(controller->dma_tx);447447+448448+ fsl_lpspi->slave_aborted = false;449449+450450+ if (!fsl_lpspi->is_slave) {451451+ transfer_timeout = fsl_lpspi_calculate_timeout(fsl_lpspi,452452+ transfer->len);453453+454454+ /* Wait eDMA to finish the data transfer.*/455455+ timeout = wait_for_completion_timeout(&fsl_lpspi->dma_tx_completion,456456+ transfer_timeout);457457+ if (!timeout) {458458+ dev_err(fsl_lpspi->dev, "I/O Error in DMA TX\n");459459+ dmaengine_terminate_all(controller->dma_tx);460460+ dmaengine_terminate_all(controller->dma_rx);461461+ fsl_lpspi_reset(fsl_lpspi);462462+ return -ETIMEDOUT;463463+ }464464+465465+ timeout = wait_for_completion_timeout(&fsl_lpspi->dma_rx_completion,466466+ transfer_timeout);467467+ if (!timeout) {468468+ dev_err(fsl_lpspi->dev, "I/O Error in DMA RX\n");469469+ dmaengine_terminate_all(controller->dma_tx);470470+ dmaengine_terminate_all(controller->dma_rx);471471+ fsl_lpspi_reset(fsl_lpspi);472472+ return -ETIMEDOUT;473473+ }474474+ } else {475475+ if (wait_for_completion_interruptible(&fsl_lpspi->dma_tx_completion) ||476476+ fsl_lpspi->slave_aborted) {477477+ dev_dbg(fsl_lpspi->dev,478478+ "I/O Error in DMA TX interrupted\n");479479+ dmaengine_terminate_all(controller->dma_tx);480480+ dmaengine_terminate_all(controller->dma_rx);481481+ fsl_lpspi_reset(fsl_lpspi);482482+ return -EINTR;483483+ }484484+485485+ if (wait_for_completion_interruptible(&fsl_lpspi->dma_rx_completion) ||486486+ fsl_lpspi->slave_aborted) {487487+ dev_dbg(fsl_lpspi->dev,488488+ "I/O Error in DMA RX interrupted\n");489489+ dmaengine_terminate_all(controller->dma_tx);490490+ dmaengine_terminate_all(controller->dma_rx);491491+ fsl_lpspi_reset(fsl_lpspi);492492+ return -EINTR;493493+ }494494+ }495495+496496+ fsl_lpspi_reset(fsl_lpspi);497497+498498+ return 0;499499+}500500+501501+static void fsl_lpspi_dma_exit(struct spi_controller *controller)502502+{503503+ if (controller->dma_rx) {504504+ dma_release_channel(controller->dma_rx);505505+ controller->dma_rx = NULL;506506+ }507507+508508+ if (controller->dma_tx) {509509+ dma_release_channel(controller->dma_tx);510510+ controller->dma_tx = NULL;511511+ }512512+}513513+514514+static int fsl_lpspi_dma_init(struct device *dev,515515+ struct fsl_lpspi_data *fsl_lpspi,516516+ struct spi_controller *controller)517517+{518518+ int ret;519519+520520+ /* Prepare for TX DMA: */521521+ controller->dma_tx = dma_request_slave_channel_reason(dev, "tx");522522+ if (IS_ERR(controller->dma_tx)) {523523+ ret = PTR_ERR(controller->dma_tx);524524+ dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);525525+ controller->dma_tx = NULL;526526+ goto err;527527+ }528528+529529+ /* Prepare for RX DMA: */530530+ controller->dma_rx = dma_request_slave_channel_reason(dev, "rx");531531+ if (IS_ERR(controller->dma_rx)) {532532+ ret = PTR_ERR(controller->dma_rx);533533+ dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);534534+ controller->dma_rx = NULL;535535+ goto err;536536+ }537537+538538+ init_completion(&fsl_lpspi->dma_rx_completion);539539+ init_completion(&fsl_lpspi->dma_tx_completion);540540+ controller->can_dma = fsl_lpspi_can_dma;541541+ controller->max_dma_len = FSL_LPSPI_MAX_EDMA_BYTES;542542+543543+ return 0;544544+err:545545+ fsl_lpspi_dma_exit(controller);546546+ return ret;547547+}548548+549549+static int fsl_lpspi_pio_transfer(struct spi_controller *controller,538550 struct spi_transfer *t)539551{540552 struct fsl_lpspi_data *fsl_lpspi =···728402 return 0;729403}730404731731-static int fsl_lpspi_transfer_one_msg(struct spi_controller *controller,732732- struct spi_message *msg)405405+static int fsl_lpspi_transfer_one(struct spi_controller *controller,406406+ struct spi_device *spi,407407+ struct spi_transfer *t)733408{734409 struct fsl_lpspi_data *fsl_lpspi =735735- spi_controller_get_devdata(controller);736736- struct spi_device *spi = msg->spi;737737- struct spi_transfer *xfer;738738- bool is_first_xfer = true;739739- int ret = 0;410410+ spi_controller_get_devdata(controller);411411+ int ret;740412741741- msg->status = 0;742742- msg->actual_length = 0;413413+ fsl_lpspi->is_first_byte = true;414414+ ret = fsl_lpspi_setup_transfer(controller, spi, t);415415+ if (ret < 0)416416+ return ret;743417744744- list_for_each_entry(xfer, &msg->transfers, transfer_list) {745745- fsl_lpspi_setup_transfer(spi, xfer);746746- fsl_lpspi_set_cmd(fsl_lpspi, is_first_xfer);418418+ fsl_lpspi_set_cmd(fsl_lpspi);419419+ fsl_lpspi->is_first_byte = false;747420748748- is_first_xfer = false;421421+ if (fsl_lpspi->usedma)422422+ ret = fsl_lpspi_dma_transfer(controller, fsl_lpspi, t);423423+ else424424+ ret = fsl_lpspi_pio_transfer(controller, t);425425+ if (ret < 0)426426+ return ret;749427750750- ret = fsl_lpspi_transfer_one(controller, spi, xfer);751751- if (ret < 0)752752- goto complete;753753-754754- msg->actual_length += xfer->len;755755- }756756-757757-complete:758758- msg->status = ret;759759- spi_finalize_current_message(controller);760760-761761- return ret;428428+ return 0;762429}763430764431static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id)···786467 return IRQ_NONE;787468}788469470470+#ifdef CONFIG_PM471471+static int fsl_lpspi_runtime_resume(struct device *dev)472472+{473473+ struct spi_controller *controller = dev_get_drvdata(dev);474474+ struct fsl_lpspi_data *fsl_lpspi;475475+ int ret;476476+477477+ fsl_lpspi = spi_controller_get_devdata(controller);478478+479479+ ret = clk_prepare_enable(fsl_lpspi->clk_per);480480+ if (ret)481481+ return ret;482482+483483+ ret = clk_prepare_enable(fsl_lpspi->clk_ipg);484484+ if (ret) {485485+ clk_disable_unprepare(fsl_lpspi->clk_per);486486+ return ret;487487+ }488488+489489+ return 0;490490+}491491+492492+static int fsl_lpspi_runtime_suspend(struct device *dev)493493+{494494+ struct spi_controller *controller = dev_get_drvdata(dev);495495+ struct fsl_lpspi_data *fsl_lpspi;496496+497497+ fsl_lpspi = spi_controller_get_devdata(controller);498498+499499+ clk_disable_unprepare(fsl_lpspi->clk_per);500500+ clk_disable_unprepare(fsl_lpspi->clk_ipg);501501+502502+ return 0;503503+}504504+#endif505505+506506+static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)507507+{508508+ struct device *dev = fsl_lpspi->dev;509509+510510+ pm_runtime_enable(dev);511511+ pm_runtime_set_autosuspend_delay(dev, FSL_LPSPI_RPM_TIMEOUT);512512+ pm_runtime_use_autosuspend(dev);513513+514514+ return 0;515515+}516516+789517static int fsl_lpspi_probe(struct platform_device *pdev)790518{519519+ struct device_node *np = pdev->dev.of_node;791520 struct fsl_lpspi_data *fsl_lpspi;792521 struct spi_controller *controller;522522+ struct spi_imx_master *lpspi_platform_info =523523+ dev_get_platdata(&pdev->dev);793524 struct resource *res;794794- int ret, irq;525525+ int i, ret, irq;795526 u32 temp;527527+ bool is_slave;796528797797- if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave"))529529+ is_slave = of_property_read_bool((&pdev->dev)->of_node, "spi-slave");530530+ if (is_slave)798531 controller = spi_alloc_slave(&pdev->dev,799532 sizeof(struct fsl_lpspi_data));800533 else···858487859488 platform_set_drvdata(pdev, controller);860489861861- controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);862862- controller->bus_num = pdev->id;863863-864490 fsl_lpspi = spi_controller_get_devdata(controller);865491 fsl_lpspi->dev = &pdev->dev;866866- fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node,867867- "spi-slave");492492+ fsl_lpspi->is_slave = is_slave;868493869869- controller->transfer_one_message = fsl_lpspi_transfer_one_msg;494494+ if (!fsl_lpspi->is_slave) {495495+ for (i = 0; i < controller->num_chipselect; i++) {496496+ int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);497497+498498+ if (!gpio_is_valid(cs_gpio) && lpspi_platform_info)499499+ cs_gpio = lpspi_platform_info->chipselect[i];500500+501501+ fsl_lpspi->chipselect[i] = cs_gpio;502502+ if (!gpio_is_valid(cs_gpio))503503+ continue;504504+505505+ ret = devm_gpio_request(&pdev->dev,506506+ fsl_lpspi->chipselect[i],507507+ DRIVER_NAME);508508+ if (ret) {509509+ dev_err(&pdev->dev, "can't get cs gpios\n");510510+ goto out_controller_put;511511+ }512512+ }513513+ controller->cs_gpios = fsl_lpspi->chipselect;514514+ controller->prepare_message = fsl_lpspi_prepare_message;515515+ }516516+517517+ controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);518518+ controller->transfer_one = fsl_lpspi_transfer_one;870519 controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;871520 controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;872521 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;···903512 ret = PTR_ERR(fsl_lpspi->base);904513 goto out_controller_put;905514 }515515+ fsl_lpspi->base_phys = res->start;906516907517 irq = platform_get_irq(pdev, 0);908518 if (irq < 0) {···918526 goto out_controller_put;919527 }920528921921- fsl_lpspi->clk = devm_clk_get(&pdev->dev, "ipg");922922- if (IS_ERR(fsl_lpspi->clk)) {923923- ret = PTR_ERR(fsl_lpspi->clk);529529+ fsl_lpspi->clk_per = devm_clk_get(&pdev->dev, "per");530530+ if (IS_ERR(fsl_lpspi->clk_per)) {531531+ ret = PTR_ERR(fsl_lpspi->clk_per);924532 goto out_controller_put;925533 }926534927927- ret = clk_prepare_enable(fsl_lpspi->clk);928928- if (ret) {929929- dev_err(&pdev->dev, "can't enable lpspi clock, ret=%d\n", ret);535535+ fsl_lpspi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");536536+ if (IS_ERR(fsl_lpspi->clk_ipg)) {537537+ ret = PTR_ERR(fsl_lpspi->clk_ipg);930538 goto out_controller_put;539539+ }540540+541541+ /* enable the clock */542542+ ret = fsl_lpspi_init_rpm(fsl_lpspi);543543+ if (ret)544544+ goto out_controller_put;545545+546546+ ret = pm_runtime_get_sync(fsl_lpspi->dev);547547+ if (ret < 0) {548548+ dev_err(fsl_lpspi->dev, "failed to enable clock\n");549549+ return ret;931550 }932551933552 temp = readl(fsl_lpspi->base + IMX7ULP_PARAM);934553 fsl_lpspi->txfifosize = 1 << (temp & 0x0f);935554 fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f);936555937937- clk_disable_unprepare(fsl_lpspi->clk);556556+ ret = fsl_lpspi_dma_init(&pdev->dev, fsl_lpspi, controller);557557+ if (ret == -EPROBE_DEFER)558558+ goto out_controller_put;559559+560560+ if (ret < 0)561561+ dev_err(&pdev->dev, "dma setup error %d, use pio\n", ret);938562939563 ret = devm_spi_register_controller(&pdev->dev, controller);940564 if (ret < 0) {···972564 struct fsl_lpspi_data *fsl_lpspi =973565 spi_controller_get_devdata(controller);974566975975- clk_disable_unprepare(fsl_lpspi->clk);567567+ pm_runtime_disable(fsl_lpspi->dev);568568+569569+ spi_master_put(controller);976570977571 return 0;978572}573573+574574+#ifdef CONFIG_PM_SLEEP575575+static int fsl_lpspi_suspend(struct device *dev)576576+{577577+ int ret;578578+579579+ pinctrl_pm_select_sleep_state(dev);580580+ ret = pm_runtime_force_suspend(dev);581581+ return ret;582582+}583583+584584+static int fsl_lpspi_resume(struct device *dev)585585+{586586+ int ret;587587+588588+ ret = pm_runtime_force_resume(dev);589589+ if (ret) {590590+ dev_err(dev, "Error in resume: %d\n", ret);591591+ return ret;592592+ }593593+594594+ pinctrl_pm_select_default_state(dev);595595+596596+ return 0;597597+}598598+#endif /* CONFIG_PM_SLEEP */599599+600600+static const struct dev_pm_ops fsl_lpspi_pm_ops = {601601+ SET_RUNTIME_PM_OPS(fsl_lpspi_runtime_suspend,602602+ fsl_lpspi_runtime_resume, NULL)603603+ SET_SYSTEM_SLEEP_PM_OPS(fsl_lpspi_suspend, fsl_lpspi_resume)604604+};979605980606static struct platform_driver fsl_lpspi_driver = {981607 .driver = {982608 .name = DRIVER_NAME,983609 .of_match_table = fsl_lpspi_dt_ids,610610+ .pm = &fsl_lpspi_pm_ops,984611 },985612 .probe = fsl_lpspi_probe,986613 .remove = fsl_lpspi_remove,
+1-1
drivers/spi/spi-fsl-qspi.c
···882882883883 ctlr->dev.of_node = np;884884885885- ret = spi_register_controller(ctlr);885885+ ret = devm_spi_register_controller(dev, ctlr);886886 if (ret)887887 goto err_destroy_mutex;888888
+64-17
drivers/spi/spi-fsl-spi.c
···3939#include <linux/spi/spi_bitbang.h>4040#include <linux/types.h>41414242+#ifdef CONFIG_FSL_SOC4343+#include <sysdev/fsl_soc.h>4444+#endif4545+4646+/* Specific to the MPC8306/MPC8309 */4747+#define IMMR_SPI_CS_OFFSET 0x14c4848+#define SPI_BOOT_SEL_BIT 0x800000004949+4250#include "spi-fsl-lib.h"4351#include "spi-fsl-cpm.h"4452#include "spi-fsl-spi.h"···363355static int fsl_spi_do_one_msg(struct spi_master *master,364356 struct spi_message *m)365357{358358+ struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);366359 struct spi_device *spi = m->spi;367360 struct spi_transfer *t, *first;368361 unsigned int cs_change;369362 const int nsecs = 50;370370- int status;363363+ int status, last_bpw;364364+365365+ /*366366+ * In CPU mode, optimize large byte transfers to use larger367367+ * bits_per_word values to reduce number of interrupts taken.368368+ */369369+ if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) {370370+ list_for_each_entry(t, &m->transfers, transfer_list) {371371+ if (t->len < 256 || t->bits_per_word != 8)372372+ continue;373373+ if ((t->len & 3) == 0)374374+ t->bits_per_word = 32;375375+ else if ((t->len & 1) == 0)376376+ t->bits_per_word = 16;377377+ }378378+ }371379372380 /* Don't allow changes if CS is active */373373- first = list_first_entry(&m->transfers, struct spi_transfer,374374- transfer_list);381381+ cs_change = 1;375382 list_for_each_entry(t, &m->transfers, transfer_list) {376376- if ((first->bits_per_word != t->bits_per_word) ||377377- (first->speed_hz != t->speed_hz)) {383383+ if (cs_change)384384+ first = t;385385+ cs_change = t->cs_change;386386+ if (first->speed_hz != t->speed_hz) {378387 dev_err(&spi->dev,379379- "bits_per_word/speed_hz should be same for the same SPI transfer\n");388388+ "speed_hz cannot change while CS is active\n");380389 return -EINVAL;381390 }382391 }383392393393+ last_bpw = -1;384394 cs_change = 1;385395 status = -EINVAL;386396 list_for_each_entry(t, &m->transfers, transfer_list) {387387- if (t->bits_per_word || t->speed_hz) {388388- if (cs_change)389389- status = fsl_spi_setup_transfer(spi, t);390390- if (status < 0)391391- break;392392- }397397+ if (cs_change || last_bpw != t->bits_per_word)398398+ status = fsl_spi_setup_transfer(spi, t);399399+ if (status < 0)400400+ break;401401+ last_bpw = t->bits_per_word;393402394403 if (cs_change) {395404 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);···726701 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);727702 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);728703 u16 cs = spi->chip_select;729729- int gpio = pinfo->gpios[cs];730730- bool alow = pinfo->alow_flags[cs];731704732732- gpio_set_value(gpio, on ^ alow);705705+ if (cs < pinfo->ngpios) {706706+ int gpio = pinfo->gpios[cs];707707+ bool alow = pinfo->alow_flags[cs];708708+709709+ gpio_set_value(gpio, on ^ alow);710710+ } else {711711+ if (WARN_ON_ONCE(cs > pinfo->ngpios || !pinfo->immr_spi_cs))712712+ return;713713+ iowrite32be(on ? SPI_BOOT_SEL_BIT : 0, pinfo->immr_spi_cs);714714+ }733715}734716735717static int of_fsl_spi_get_chipselects(struct device *dev)···744712 struct device_node *np = dev->of_node;745713 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);746714 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);715715+ bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) &&716716+ of_property_read_bool(np, "fsl,spisel_boot");747717 int ngpios;748718 int i = 0;749719 int ret;750720751721 ngpios = of_gpio_count(np);752752- if (ngpios <= 0) {722722+ ngpios = max(ngpios, 0);723723+ if (ngpios == 0 && !spisel_boot) {753724 /*754725 * SPI w/o chip-select line. One SPI device is still permitted755726 * though.···761726 return 0;762727 }763728729729+ pinfo->ngpios = ngpios;764730 pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),765731 GFP_KERNEL);766732 if (!pinfo->gpios)···805769 }806770 }807771808808- pdata->max_chipselect = ngpios;772772+#if IS_ENABLED(CONFIG_FSL_SOC)773773+ if (spisel_boot) {774774+ pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4);775775+ if (!pinfo->immr_spi_cs) {776776+ ret = -ENOMEM;777777+ i = ngpios - 1;778778+ goto err_loop;779779+ }780780+ }781781+#endif782782+783783+ pdata->max_chipselect = ngpios + spisel_boot;809784 pdata->cs_control = fsl_spi_cs_control;810785811786 return 0;
+99-135
drivers/spi/spi-gpio.c
···41414242struct spi_gpio {4343 struct spi_bitbang bitbang;4444- struct spi_gpio_platform_data pdata;4545- struct platform_device *pdev;4644 struct gpio_desc *sck;4745 struct gpio_desc *miso;4846 struct gpio_desc *mosi;4947 struct gpio_desc **cs_gpios;5050- bool has_cs;5148};52495350/*----------------------------------------------------------------------*/···9093 bang = spi_master_get_devdata(spi->master);9194 spi_gpio = container_of(bang, struct spi_gpio, bitbang);9295 return spi_gpio;9393-}9494-9595-static inline struct spi_gpio_platform_data *__pure9696-spi_to_pdata(const struct spi_device *spi)9797-{9898- return &spi_to_spi_gpio(spi)->pdata;9996}1009710198/* These helpers are in turn called by the bitbang inlines */···214223 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);215224216225 /* Drive chip select line, if we have one */217217- if (spi_gpio->has_cs) {226226+ if (spi_gpio->cs_gpios) {218227 struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];219228220229 /* SPI chip selects are normally active-low */···232241 * The CS GPIOs have already been233242 * initialized from the descriptor lookup.234243 */235235- cs = spi_gpio->cs_gpios[spi->chip_select];236236- if (!spi->controller_state && cs)237237- status = gpiod_direction_output(cs,238238- !(spi->mode & SPI_CS_HIGH));244244+ if (spi_gpio->cs_gpios) {245245+ cs = spi_gpio->cs_gpios[spi->chip_select];246246+ if (!spi->controller_state && cs)247247+ status = gpiod_direction_output(cs,248248+ !(spi->mode & SPI_CS_HIGH));249249+ }239250240251 if (!status)241252 status = spi_bitbang_setup(spi);···288295 * floating signals. (A weak pulldown would save power too, but many289296 * drivers expect to see all-ones data as the no slave "response".)290297 */291291-static int spi_gpio_request(struct device *dev,292292- struct spi_gpio *spi_gpio,293293- unsigned int num_chipselects,294294- u16 *mflags)298298+static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)295299{296296- int i;297297-298300 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);299301 if (IS_ERR(spi_gpio->mosi))300302 return PTR_ERR(spi_gpio->mosi);301301- if (!spi_gpio->mosi)302302- /* HW configuration without MOSI pin */303303- *mflags |= SPI_MASTER_NO_TX;304303305304 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);306305 if (IS_ERR(spi_gpio->miso))307306 return PTR_ERR(spi_gpio->miso);308308- /*309309- * No setting SPI_MASTER_NO_RX here - if there is only a MOSI310310- * pin connected the host can still do RX by changing the311311- * direction of the line.312312- */313307314308 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);315309 if (IS_ERR(spi_gpio->sck))316310 return PTR_ERR(spi_gpio->sck);317317-318318- for (i = 0; i < num_chipselects; i++) {319319- spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs",320320- i, GPIOD_OUT_HIGH);321321- if (IS_ERR(spi_gpio->cs_gpios[i]))322322- return PTR_ERR(spi_gpio->cs_gpios[i]);323323- }324311325312 return 0;326313}···312339};313340MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);314341315315-static int spi_gpio_probe_dt(struct platform_device *pdev)342342+static int spi_gpio_probe_dt(struct platform_device *pdev,343343+ struct spi_master *master)316344{317317- int ret;318318- u32 tmp;319319- struct spi_gpio_platform_data *pdata;320320- struct device_node *np = pdev->dev.of_node;321321- const struct of_device_id *of_id =322322- of_match_device(spi_gpio_dt_ids, &pdev->dev);345345+ master->dev.of_node = pdev->dev.of_node;346346+ master->use_gpio_descriptors = true;323347324324- if (!of_id)325325- return 0;326326-327327- pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);328328- if (!pdata)329329- return -ENOMEM;330330-331331-332332- ret = of_property_read_u32(np, "num-chipselects", &tmp);333333- if (ret < 0) {334334- dev_err(&pdev->dev, "num-chipselects property not found\n");335335- goto error_free;336336- }337337-338338- pdata->num_chipselect = tmp;339339- pdev->dev.platform_data = pdata;340340-341341- return 1;342342-343343-error_free:344344- devm_kfree(&pdev->dev, pdata);345345- return ret;348348+ return 0;346349}347350#else348348-static inline int spi_gpio_probe_dt(struct platform_device *pdev)351351+static inline int spi_gpio_probe_dt(struct platform_device *pdev,352352+ struct spi_master *master)349353{350354 return 0;351355}352356#endif357357+358358+static int spi_gpio_probe_pdata(struct platform_device *pdev,359359+ struct spi_master *master)360360+{361361+ struct device *dev = &pdev->dev;362362+ struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);363363+ struct spi_gpio *spi_gpio = spi_master_get_devdata(master);364364+ int i;365365+366366+#ifdef GENERIC_BITBANG367367+ if (!pdata || !pdata->num_chipselect)368368+ return -ENODEV;369369+#endif370370+ /*371371+ * The master needs to think there is a chipselect even if not372372+ * connected373373+ */374374+ master->num_chipselect = pdata->num_chipselect ?: 1;375375+376376+ spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,377377+ sizeof(*spi_gpio->cs_gpios),378378+ GFP_KERNEL);379379+ if (!spi_gpio->cs_gpios)380380+ return -ENOMEM;381381+382382+ for (i = 0; i < master->num_chipselect; i++) {383383+ spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,384384+ GPIOD_OUT_HIGH);385385+ if (IS_ERR(spi_gpio->cs_gpios[i]))386386+ return PTR_ERR(spi_gpio->cs_gpios[i]);387387+ }388388+389389+ return 0;390390+}391391+392392+static void spi_gpio_put(void *data)393393+{394394+ spi_master_put(data);395395+}353396354397static int spi_gpio_probe(struct platform_device *pdev)355398{356399 int status;357400 struct spi_master *master;358401 struct spi_gpio *spi_gpio;359359- struct spi_gpio_platform_data *pdata;360360- u16 master_flags = 0;361361- bool use_of = 0;402402+ struct device *dev = &pdev->dev;403403+ struct spi_bitbang *bb;404404+ const struct of_device_id *of_id;362405363363- status = spi_gpio_probe_dt(pdev);364364- if (status < 0)365365- return status;366366- if (status > 0)367367- use_of = 1;406406+ of_id = of_match_device(spi_gpio_dt_ids, &pdev->dev);368407369369- pdata = dev_get_platdata(&pdev->dev);370370-#ifdef GENERIC_BITBANG371371- if (!pdata || (!use_of && !pdata->num_chipselect))372372- return -ENODEV;373373-#endif374374-375375- master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio));408408+ master = spi_alloc_master(dev, sizeof(*spi_gpio));376409 if (!master)377410 return -ENOMEM;378411412412+ status = devm_add_action_or_reset(&pdev->dev, spi_gpio_put, master);413413+ if (status)414414+ return status;415415+416416+ if (of_id)417417+ status = spi_gpio_probe_dt(pdev, master);418418+ else419419+ status = spi_gpio_probe_pdata(pdev, master);420420+421421+ if (status)422422+ return status;423423+379424 spi_gpio = spi_master_get_devdata(master);380425381381- spi_gpio->cs_gpios = devm_kcalloc(&pdev->dev,382382- pdata->num_chipselect,383383- sizeof(*spi_gpio->cs_gpios),384384- GFP_KERNEL);385385- if (!spi_gpio->cs_gpios)386386- return -ENOMEM;387387-388388- platform_set_drvdata(pdev, spi_gpio);389389-390390- /* Determine if we have chip selects connected */391391- spi_gpio->has_cs = !!pdata->num_chipselect;392392-393393- spi_gpio->pdev = pdev;394394- if (pdata)395395- spi_gpio->pdata = *pdata;396396-397397- status = spi_gpio_request(&pdev->dev, spi_gpio,398398- pdata->num_chipselect, &master_flags);426426+ status = spi_gpio_request(dev, spi_gpio);399427 if (status)400428 return status;401429402430 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);403431 master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |404432 SPI_CS_HIGH;405405- master->flags = master_flags;433433+ if (!spi_gpio->mosi) {434434+ /* HW configuration without MOSI pin435435+ *436436+ * No setting SPI_MASTER_NO_RX here - if there is only437437+ * a MOSI pin connected the host can still do RX by438438+ * changing the direction of the line.439439+ */440440+ master->flags = SPI_MASTER_NO_TX;441441+ }442442+406443 master->bus_num = pdev->id;407407- /* The master needs to think there is a chipselect even if not connected */408408- master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1;409444 master->setup = spi_gpio_setup;410445 master->cleanup = spi_gpio_cleanup;411411-#ifdef CONFIG_OF412412- master->dev.of_node = pdev->dev.of_node;413413-#endif414446415415- spi_gpio->bitbang.master = master;416416- spi_gpio->bitbang.chipselect = spi_gpio_chipselect;417417- spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction;447447+ bb = &spi_gpio->bitbang;448448+ bb->master = master;449449+ bb->chipselect = spi_gpio_chipselect;450450+ bb->set_line_direction = spi_gpio_set_direction;418451419419- if ((master_flags & SPI_MASTER_NO_TX) == 0) {420420- spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;421421- spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;422422- spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;423423- spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;452452+ if (master->flags & SPI_MASTER_NO_TX) {453453+ bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;454454+ bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;455455+ bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;456456+ bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;424457 } else {425425- spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;426426- spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;427427- spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;428428- spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;458458+ bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;459459+ bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;460460+ bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;461461+ bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;429462 }430430- spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;463463+ bb->setup_transfer = spi_bitbang_setup_transfer;431464432432- status = spi_bitbang_start(&spi_gpio->bitbang);465465+ status = spi_bitbang_init(&spi_gpio->bitbang);433466 if (status)434434- spi_master_put(master);467467+ return status;435468436436- return status;437437-}438438-439439-static int spi_gpio_remove(struct platform_device *pdev)440440-{441441- struct spi_gpio *spi_gpio;442442-443443- spi_gpio = platform_get_drvdata(pdev);444444-445445- /* stop() unregisters child devices too */446446- spi_bitbang_stop(&spi_gpio->bitbang);447447-448448- spi_master_put(spi_gpio->bitbang.master);449449-450450- return 0;469469+ return devm_spi_register_master(&pdev->dev, spi_master_get(master));451470}452471453472MODULE_ALIAS("platform:" DRIVER_NAME);···450485 .of_match_table = of_match_ptr(spi_gpio_dt_ids),451486 },452487 .probe = spi_gpio_probe,453453- .remove = spi_gpio_remove,454488};455489module_platform_driver(spi_gpio_driver);456490
+7
drivers/spi/spi-imx.c
···28282929#define DRIVER_NAME "spi_imx"30303131+static bool use_dma = true;3232+module_param(use_dma, bool, 0644);3333+MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)");3434+3135#define MXC_CSPIRXDATA 0x003236#define MXC_CSPITXDATA 0x043337#define MXC_CSPICTRL 0x08···222218 struct spi_transfer *transfer)223219{224220 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);221221+222222+ if (!use_dma)223223+ return false;225224226225 if (!master->dma_rx)227226 return false;
+4-4
drivers/spi/spi-mem.c
···135135 return -ENOTSUPP;136136}137137138138-static bool spi_mem_default_supports_op(struct spi_mem *mem,139139- const struct spi_mem_op *op)138138+bool spi_mem_default_supports_op(struct spi_mem *mem,139139+ const struct spi_mem_op *op)140140{141141 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))142142 return false;···622622EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);623623624624/**625625- * spi_mem_dirmap_dirmap_read() - Read data through a direct mapping625625+ * spi_mem_dirmap_read() - Read data through a direct mapping626626 * @desc: direct mapping descriptor627627 * @offs: offset to start reading from. Note that this is not an absolute628628 * offset, but the offset within the direct mapping which already has···668668EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);669669670670/**671671- * spi_mem_dirmap_dirmap_write() - Write data through a direct mapping671671+ * spi_mem_dirmap_write() - Write data through a direct mapping672672 * @desc: direct mapping descriptor673673 * @offs: offset to start writing from. Note that this is not an absolute674674 * offset, but the offset within the direct mapping which already has
···470470 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)471471 goto out;472472 count--;473473+ if (xfer->word_delay_usecs)474474+ udelay(xfer->word_delay_usecs);473475 } while (count);474476 } else if (word_len == 16) {475477 const u16 *tx = xfer->tx_buf;···481479 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)482480 goto out;483481 count -= 2;482482+ if (xfer->word_delay_usecs)483483+ udelay(xfer->word_delay_usecs);484484 } while (count);485485 }486486
+3-1
drivers/spi/spi-pxa2xx-dma.c
···239239 u32 *threshold)240240{241241 struct pxa2xx_spi_chip *chip_info = spi->controller_data;242242+ struct driver_data *drv_data = spi_controller_get_devdata(spi->controller);243243+ u32 dma_burst_size = drv_data->controller_info->dma_burst_size;242244243245 /*244246 * If the DMA burst size is given in chip_info we use that,245247 * otherwise we use the default. Also we use the default FIFO246248 * thresholds for now.247249 */248248- *burst_code = chip_info ? chip_info->dma_burst_size : 1;250250+ *burst_code = chip_info ? chip_info->dma_burst_size : dma_burst_size;249251 *threshold = SSCR1_RxTresh(RX_THRESH_DFLT)250252 | SSCR1_TxTresh(TX_THRESH_DFLT);251253
···11+// SPDX-License-Identifier: GPL-2.0+22+/*33+ * Copyright (C) 2019 Xilinx, Inc.44+ *55+ * Author: Naga Sureshkumar Relli <nagasure@xilinx.com>66+ */77+88+#include <linux/clk.h>99+#include <linux/delay.h>1010+#include <linux/gpio.h>1111+#include <linux/interrupt.h>1212+#include <linux/io.h>1313+#include <linux/module.h>1414+#include <linux/of_irq.h>1515+#include <linux/of_address.h>1616+#include <linux/platform_device.h>1717+#include <linux/spi/spi.h>1818+#include <linux/workqueue.h>1919+#include <linux/spi/spi-mem.h>2020+2121+/* Register offset definitions */2222+#define ZYNQ_QSPI_CONFIG_OFFSET 0x00 /* Configuration Register, RW */2323+#define ZYNQ_QSPI_STATUS_OFFSET 0x04 /* Interrupt Status Register, RO */2424+#define ZYNQ_QSPI_IEN_OFFSET 0x08 /* Interrupt Enable Register, WO */2525+#define ZYNQ_QSPI_IDIS_OFFSET 0x0C /* Interrupt Disable Reg, WO */2626+#define ZYNQ_QSPI_IMASK_OFFSET 0x10 /* Interrupt Enabled Mask Reg,RO */2727+#define ZYNQ_QSPI_ENABLE_OFFSET 0x14 /* Enable/Disable Register, RW */2828+#define ZYNQ_QSPI_DELAY_OFFSET 0x18 /* Delay Register, RW */2929+#define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst, WO */3030+#define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst, WO */3131+#define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst, WO */3232+#define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst, WO */3333+#define ZYNQ_QSPI_RXD_OFFSET 0x20 /* Data Receive Register, RO */3434+#define ZYNQ_QSPI_SIC_OFFSET 0x24 /* Slave Idle Count Register, RW */3535+#define ZYNQ_QSPI_TX_THRESH_OFFSET 0x28 /* TX FIFO Watermark Reg, RW */3636+#define ZYNQ_QSPI_RX_THRESH_OFFSET 0x2C /* RX FIFO Watermark Reg, RW */3737+#define ZYNQ_QSPI_GPIO_OFFSET 0x30 /* GPIO Register, RW */3838+#define ZYNQ_QSPI_LINEAR_CFG_OFFSET 0xA0 /* Linear Adapter Config Ref, RW */3939+#define ZYNQ_QSPI_MOD_ID_OFFSET 0xFC /* Module ID Register, RO */4040+4141+/*4242+ * QSPI Configuration Register bit Masks4343+ *4444+ * This register contains various control bits that effect the operation4545+ * of the QSPI controller4646+ */4747+#define ZYNQ_QSPI_CONFIG_IFMODE_MASK BIT(31) /* Flash Memory Interface */4848+#define ZYNQ_QSPI_CONFIG_MANSRT_MASK BIT(16) /* Manual TX Start */4949+#define ZYNQ_QSPI_CONFIG_MANSRTEN_MASK BIT(15) /* Enable Manual TX Mode */5050+#define ZYNQ_QSPI_CONFIG_SSFORCE_MASK BIT(14) /* Manual Chip Select */5151+#define ZYNQ_QSPI_CONFIG_BDRATE_MASK GENMASK(5, 3) /* Baud Rate Mask */5252+#define ZYNQ_QSPI_CONFIG_CPHA_MASK BIT(2) /* Clock Phase Control */5353+#define ZYNQ_QSPI_CONFIG_CPOL_MASK BIT(1) /* Clock Polarity Control */5454+#define ZYNQ_QSPI_CONFIG_SSCTRL_MASK BIT(10) /* Slave Select Mask */5555+#define ZYNQ_QSPI_CONFIG_FWIDTH_MASK GENMASK(7, 6) /* FIFO width */5656+#define ZYNQ_QSPI_CONFIG_MSTREN_MASK BIT(0) /* Master Mode */5757+5858+/*5959+ * QSPI Configuration Register - Baud rate and slave select6060+ *6161+ * These are the values used in the calculation of baud rate divisor and6262+ * setting the slave select.6363+ */6464+#define ZYNQ_QSPI_BAUD_DIV_MAX GENMASK(2, 0) /* Baud rate maximum */6565+#define ZYNQ_QSPI_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift in CR */6666+#define ZYNQ_QSPI_SS_SHIFT 10 /* Slave Select field shift in CR */6767+6868+/*6969+ * QSPI Interrupt Registers bit Masks7070+ *7171+ * All the four interrupt registers (Status/Mask/Enable/Disable) have the same7272+ * bit definitions.7373+ */7474+#define ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK BIT(0) /* QSPI RX FIFO Overflow */7575+#define ZYNQ_QSPI_IXR_TXNFULL_MASK BIT(2) /* QSPI TX FIFO Overflow */7676+#define ZYNQ_QSPI_IXR_TXFULL_MASK BIT(3) /* QSPI TX FIFO is full */7777+#define ZYNQ_QSPI_IXR_RXNEMTY_MASK BIT(4) /* QSPI RX FIFO Not Empty */7878+#define ZYNQ_QSPI_IXR_RXF_FULL_MASK BIT(5) /* QSPI RX FIFO is full */7979+#define ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK BIT(6) /* QSPI TX FIFO Underflow */8080+#define ZYNQ_QSPI_IXR_ALL_MASK (ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK | \8181+ ZYNQ_QSPI_IXR_TXNFULL_MASK | \8282+ ZYNQ_QSPI_IXR_TXFULL_MASK | \8383+ ZYNQ_QSPI_IXR_RXNEMTY_MASK | \8484+ ZYNQ_QSPI_IXR_RXF_FULL_MASK | \8585+ ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK)8686+#define ZYNQ_QSPI_IXR_RXTX_MASK (ZYNQ_QSPI_IXR_TXNFULL_MASK | \8787+ ZYNQ_QSPI_IXR_RXNEMTY_MASK)8888+8989+/*9090+ * QSPI Enable Register bit Masks9191+ *9292+ * This register is used to enable or disable the QSPI controller9393+ */9494+#define ZYNQ_QSPI_ENABLE_ENABLE_MASK BIT(0) /* QSPI Enable Bit Mask */9595+9696+/*9797+ * QSPI Linear Configuration Register9898+ *9999+ * It is named Linear Configuration but it controls other modes when not in100100+ * linear mode also.101101+ */102102+#define ZYNQ_QSPI_LCFG_TWO_MEM_MASK BIT(30) /* LQSPI Two memories Mask */103103+#define ZYNQ_QSPI_LCFG_SEP_BUS_MASK BIT(29) /* LQSPI Separate bus Mask */104104+#define ZYNQ_QSPI_LCFG_U_PAGE_MASK BIT(28) /* LQSPI Upper Page Mask */105105+106106+#define ZYNQ_QSPI_LCFG_DUMMY_SHIFT 8107107+108108+#define ZYNQ_QSPI_FAST_READ_QOUT_CODE 0x6B /* read instruction code */109109+#define ZYNQ_QSPI_FIFO_DEPTH 63 /* FIFO depth in words */110110+#define ZYNQ_QSPI_RX_THRESHOLD 32 /* Rx FIFO threshold level */111111+#define ZYNQ_QSPI_TX_THRESHOLD 1 /* Tx FIFO threshold level */112112+113113+/*114114+ * The modebits configurable by the driver to make the SPI support different115115+ * data formats116116+ */117117+#define ZYNQ_QSPI_MODEBITS (SPI_CPOL | SPI_CPHA)118118+119119+/* Default number of chip selects */120120+#define ZYNQ_QSPI_DEFAULT_NUM_CS 1121121+122122+/**123123+ * struct zynq_qspi - Defines qspi driver instance124124+ * @regs: Virtual address of the QSPI controller registers125125+ * @refclk: Pointer to the peripheral clock126126+ * @pclk: Pointer to the APB clock127127+ * @irq: IRQ number128128+ * @txbuf: Pointer to the TX buffer129129+ * @rxbuf: Pointer to the RX buffer130130+ * @tx_bytes: Number of bytes left to transfer131131+ * @rx_bytes: Number of bytes left to receive132132+ * @data_completion: completion structure133133+ */134134+struct zynq_qspi {135135+ struct device *dev;136136+ void __iomem *regs;137137+ struct clk *refclk;138138+ struct clk *pclk;139139+ int irq;140140+ u8 *txbuf;141141+ u8 *rxbuf;142142+ int tx_bytes;143143+ int rx_bytes;144144+ struct completion data_completion;145145+};146146+147147+/*148148+ * Inline functions for the QSPI controller read/write149149+ */150150+static inline u32 zynq_qspi_read(struct zynq_qspi *xqspi, u32 offset)151151+{152152+ return readl_relaxed(xqspi->regs + offset);153153+}154154+155155+static inline void zynq_qspi_write(struct zynq_qspi *xqspi, u32 offset,156156+ u32 val)157157+{158158+ writel_relaxed(val, xqspi->regs + offset);159159+}160160+161161+/**162162+ * zynq_qspi_init_hw - Initialize the hardware163163+ * @xqspi: Pointer to the zynq_qspi structure164164+ *165165+ * The default settings of the QSPI controller's configurable parameters on166166+ * reset are167167+ * - Master mode168168+ * - Baud rate divisor is set to 2169169+ * - Tx threshold set to 1l Rx threshold set to 32170170+ * - Flash memory interface mode enabled171171+ * - Size of the word to be transferred as 8 bit172172+ * This function performs the following actions173173+ * - Disable and clear all the interrupts174174+ * - Enable manual slave select175175+ * - Enable manual start176176+ * - Deselect all the chip select lines177177+ * - Set the size of the word to be transferred as 32 bit178178+ * - Set the little endian mode of TX FIFO and179179+ * - Enable the QSPI controller180180+ */181181+static void zynq_qspi_init_hw(struct zynq_qspi *xqspi)182182+{183183+ u32 config_reg;184184+185185+ zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0);186186+ zynq_qspi_write(xqspi, ZYNQ_QSPI_IDIS_OFFSET, ZYNQ_QSPI_IXR_ALL_MASK);187187+188188+ /* Disable linear mode as the boot loader may have used it */189189+ zynq_qspi_write(xqspi, ZYNQ_QSPI_LINEAR_CFG_OFFSET, 0);190190+191191+ /* Clear the RX FIFO */192192+ while (zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET) &193193+ ZYNQ_QSPI_IXR_RXNEMTY_MASK)194194+ zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET);195195+196196+ zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, ZYNQ_QSPI_IXR_ALL_MASK);197197+ config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET);198198+ config_reg &= ~(ZYNQ_QSPI_CONFIG_MSTREN_MASK |199199+ ZYNQ_QSPI_CONFIG_CPOL_MASK |200200+ ZYNQ_QSPI_CONFIG_CPHA_MASK |201201+ ZYNQ_QSPI_CONFIG_BDRATE_MASK |202202+ ZYNQ_QSPI_CONFIG_SSFORCE_MASK |203203+ ZYNQ_QSPI_CONFIG_MANSRTEN_MASK |204204+ ZYNQ_QSPI_CONFIG_MANSRT_MASK);205205+ config_reg |= (ZYNQ_QSPI_CONFIG_MSTREN_MASK |206206+ ZYNQ_QSPI_CONFIG_SSFORCE_MASK |207207+ ZYNQ_QSPI_CONFIG_FWIDTH_MASK |208208+ ZYNQ_QSPI_CONFIG_IFMODE_MASK);209209+ zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg);210210+211211+ zynq_qspi_write(xqspi, ZYNQ_QSPI_RX_THRESH_OFFSET,212212+ ZYNQ_QSPI_RX_THRESHOLD);213213+ zynq_qspi_write(xqspi, ZYNQ_QSPI_TX_THRESH_OFFSET,214214+ ZYNQ_QSPI_TX_THRESHOLD);215215+216216+ zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET,217217+ ZYNQ_QSPI_ENABLE_ENABLE_MASK);218218+}219219+220220+static bool zynq_qspi_supports_op(struct spi_mem *mem,221221+ const struct spi_mem_op *op)222222+{223223+ if (!spi_mem_default_supports_op(mem, op))224224+ return false;225225+226226+ /*227227+ * The number of address bytes should be equal to or less than 3 bytes.228228+ */229229+ if (op->addr.nbytes > 3)230230+ return false;231231+232232+ return true;233233+}234234+235235+/**236236+ * zynq_qspi_rxfifo_op - Read 1..4 bytes from RxFIFO to RX buffer237237+ * @xqspi: Pointer to the zynq_qspi structure238238+ * @size: Number of bytes to be read (1..4)239239+ */240240+static void zynq_qspi_rxfifo_op(struct zynq_qspi *xqspi, unsigned int size)241241+{242242+ u32 data;243243+244244+ data = zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET);245245+246246+ if (xqspi->rxbuf) {247247+ memcpy(xqspi->rxbuf, ((u8 *)&data) + 4 - size, size);248248+ xqspi->rxbuf += size;249249+ }250250+251251+ xqspi->rx_bytes -= size;252252+ if (xqspi->rx_bytes < 0)253253+ xqspi->rx_bytes = 0;254254+}255255+256256+/**257257+ * zynq_qspi_txfifo_op - Write 1..4 bytes from TX buffer to TxFIFO258258+ * @xqspi: Pointer to the zynq_qspi structure259259+ * @size: Number of bytes to be written (1..4)260260+ */261261+static void zynq_qspi_txfifo_op(struct zynq_qspi *xqspi, unsigned int size)262262+{263263+ static const unsigned int offset[4] = {264264+ ZYNQ_QSPI_TXD_00_01_OFFSET, ZYNQ_QSPI_TXD_00_10_OFFSET,265265+ ZYNQ_QSPI_TXD_00_11_OFFSET, ZYNQ_QSPI_TXD_00_00_OFFSET };266266+ u32 data;267267+268268+ if (xqspi->txbuf) {269269+ data = 0xffffffff;270270+ memcpy(&data, xqspi->txbuf, size);271271+ xqspi->txbuf += size;272272+ } else {273273+ data = 0;274274+ }275275+276276+ xqspi->tx_bytes -= size;277277+ zynq_qspi_write(xqspi, offset[size - 1], data);278278+}279279+280280+/**281281+ * zynq_qspi_chipselect - Select or deselect the chip select line282282+ * @spi: Pointer to the spi_device structure283283+ * @assert: 1 for select or 0 for deselect the chip select line284284+ */285285+static void zynq_qspi_chipselect(struct spi_device *spi, bool assert)286286+{287287+ struct spi_controller *ctrl = spi->master;288288+ struct zynq_qspi *xqspi = spi_controller_get_devdata(ctrl);289289+ u32 config_reg;290290+291291+ config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET);292292+ if (assert) {293293+ /* Select the slave */294294+ config_reg &= ~ZYNQ_QSPI_CONFIG_SSCTRL_MASK;295295+ config_reg |= (((~(BIT(spi->chip_select))) <<296296+ ZYNQ_QSPI_SS_SHIFT) &297297+ ZYNQ_QSPI_CONFIG_SSCTRL_MASK);298298+ } else {299299+ config_reg |= ZYNQ_QSPI_CONFIG_SSCTRL_MASK;300300+ }301301+302302+ zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg);303303+}304304+305305+/**306306+ * zynq_qspi_config_op - Configure QSPI controller for specified transfer307307+ * @xqspi: Pointer to the zynq_qspi structure308308+ * @qspi: Pointer to the spi_device structure309309+ *310310+ * Sets the operational mode of QSPI controller for the next QSPI transfer and311311+ * sets the requested clock frequency.312312+ *313313+ * Return: 0 on success and -EINVAL on invalid input parameter314314+ *315315+ * Note: If the requested frequency is not an exact match with what can be316316+ * obtained using the prescalar value, the driver sets the clock frequency which317317+ * is lower than the requested frequency (maximum lower) for the transfer. If318318+ * the requested frequency is higher or lower than that is supported by the QSPI319319+ * controller the driver will set the highest or lowest frequency supported by320320+ * controller.321321+ */322322+static int zynq_qspi_config_op(struct zynq_qspi *xqspi, struct spi_device *spi)323323+{324324+ u32 config_reg, baud_rate_val = 0;325325+326326+ /*327327+ * Set the clock frequency328328+ * The baud rate divisor is not a direct mapping to the value written329329+ * into the configuration register (config_reg[5:3])330330+ * i.e. 000 - divide by 2331331+ * 001 - divide by 4332332+ * ----------------333333+ * 111 - divide by 256334334+ */335335+ while ((baud_rate_val < ZYNQ_QSPI_BAUD_DIV_MAX) &&336336+ (clk_get_rate(xqspi->refclk) / (2 << baud_rate_val)) >337337+ spi->max_speed_hz)338338+ baud_rate_val++;339339+340340+ config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET);341341+342342+ /* Set the QSPI clock phase and clock polarity */343343+ config_reg &= (~ZYNQ_QSPI_CONFIG_CPHA_MASK) &344344+ (~ZYNQ_QSPI_CONFIG_CPOL_MASK);345345+ if (spi->mode & SPI_CPHA)346346+ config_reg |= ZYNQ_QSPI_CONFIG_CPHA_MASK;347347+ if (spi->mode & SPI_CPOL)348348+ config_reg |= ZYNQ_QSPI_CONFIG_CPOL_MASK;349349+350350+ config_reg &= ~ZYNQ_QSPI_CONFIG_BDRATE_MASK;351351+ config_reg |= (baud_rate_val << ZYNQ_QSPI_BAUD_DIV_SHIFT);352352+ zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg);353353+354354+ return 0;355355+}356356+357357+/**358358+ * zynq_qspi_setup - Configure the QSPI controller359359+ * @spi: Pointer to the spi_device structure360360+ *361361+ * Sets the operational mode of QSPI controller for the next QSPI transfer, baud362362+ * rate and divisor value to setup the requested qspi clock.363363+ *364364+ * Return: 0 on success and error value on failure365365+ */366366+static int zynq_qspi_setup_op(struct spi_device *spi)367367+{368368+ struct spi_controller *ctrl = spi->master;369369+ struct zynq_qspi *qspi = spi_controller_get_devdata(ctrl);370370+371371+ if (ctrl->busy)372372+ return -EBUSY;373373+374374+ clk_enable(qspi->refclk);375375+ clk_enable(qspi->pclk);376376+ zynq_qspi_write(qspi, ZYNQ_QSPI_ENABLE_OFFSET,377377+ ZYNQ_QSPI_ENABLE_ENABLE_MASK);378378+379379+ return 0;380380+}381381+382382+/**383383+ * zynq_qspi_write_op - Fills the TX FIFO with as many bytes as possible384384+ * @xqspi: Pointer to the zynq_qspi structure385385+ * @txcount: Maximum number of words to write386386+ * @txempty: Indicates that TxFIFO is empty387387+ */388388+static void zynq_qspi_write_op(struct zynq_qspi *xqspi, int txcount,389389+ bool txempty)390390+{391391+ int count, len, k;392392+393393+ len = xqspi->tx_bytes;394394+ if (len && len < 4) {395395+ /*396396+ * We must empty the TxFIFO between accesses to TXD0,397397+ * TXD1, TXD2, TXD3.398398+ */399399+ if (txempty)400400+ zynq_qspi_txfifo_op(xqspi, len);401401+402402+ return;403403+ }404404+405405+ count = len / 4;406406+ if (count > txcount)407407+ count = txcount;408408+409409+ if (xqspi->txbuf) {410410+ iowrite32_rep(xqspi->regs + ZYNQ_QSPI_TXD_00_00_OFFSET,411411+ xqspi->txbuf, count);412412+ xqspi->txbuf += count * 4;413413+ } else {414414+ for (k = 0; k < count; k++)415415+ writel_relaxed(0, xqspi->regs +416416+ ZYNQ_QSPI_TXD_00_00_OFFSET);417417+ }418418+419419+ xqspi->tx_bytes -= count * 4;420420+}421421+422422+/**423423+ * zynq_qspi_read_op - Drains the RX FIFO by as many bytes as possible424424+ * @xqspi: Pointer to the zynq_qspi structure425425+ * @rxcount: Maximum number of words to read426426+ */427427+static void zynq_qspi_read_op(struct zynq_qspi *xqspi, int rxcount)428428+{429429+ int count, len, k;430430+431431+ len = xqspi->rx_bytes - xqspi->tx_bytes;432432+ count = len / 4;433433+ if (count > rxcount)434434+ count = rxcount;435435+ if (xqspi->rxbuf) {436436+ ioread32_rep(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET,437437+ xqspi->rxbuf, count);438438+ xqspi->rxbuf += count * 4;439439+ } else {440440+ for (k = 0; k < count; k++)441441+ readl_relaxed(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET);442442+ }443443+ xqspi->rx_bytes -= count * 4;444444+ len -= count * 4;445445+446446+ if (len && len < 4 && count < rxcount)447447+ zynq_qspi_rxfifo_op(xqspi, len);448448+}449449+450450+/**451451+ * zynq_qspi_irq - Interrupt service routine of the QSPI controller452452+ * @irq: IRQ number453453+ * @dev_id: Pointer to the xqspi structure454454+ *455455+ * This function handles TX empty only.456456+ * On TX empty interrupt this function reads the received data from RX FIFO and457457+ * fills the TX FIFO if there is any data remaining to be transferred.458458+ *459459+ * Return: IRQ_HANDLED when interrupt is handled; IRQ_NONE otherwise.460460+ */461461+static irqreturn_t zynq_qspi_irq(int irq, void *dev_id)462462+{463463+ u32 intr_status;464464+ bool txempty;465465+ struct zynq_qspi *xqspi = (struct zynq_qspi *)dev_id;466466+467467+ intr_status = zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET);468468+ zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, intr_status);469469+470470+ if ((intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK) ||471471+ (intr_status & ZYNQ_QSPI_IXR_RXNEMTY_MASK)) {472472+ /*473473+ * This bit is set when Tx FIFO has < THRESHOLD entries.474474+ * We have the THRESHOLD value set to 1,475475+ * so this bit indicates Tx FIFO is empty.476476+ */477477+ txempty = !!(intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK);478478+ /* Read out the data from the RX FIFO */479479+ zynq_qspi_read_op(xqspi, ZYNQ_QSPI_RX_THRESHOLD);480480+ if (xqspi->tx_bytes) {481481+ /* There is more data to send */482482+ zynq_qspi_write_op(xqspi, ZYNQ_QSPI_RX_THRESHOLD,483483+ txempty);484484+ } else {485485+ /*486486+ * If transfer and receive is completed then only send487487+ * complete signal.488488+ */489489+ if (!xqspi->rx_bytes) {490490+ zynq_qspi_write(xqspi,491491+ ZYNQ_QSPI_IDIS_OFFSET,492492+ ZYNQ_QSPI_IXR_RXTX_MASK);493493+ complete(&xqspi->data_completion);494494+ }495495+ }496496+ return IRQ_HANDLED;497497+ }498498+499499+ return IRQ_NONE;500500+}501501+502502+/**503503+ * zynq_qspi_exec_mem_op() - Initiates the QSPI transfer504504+ * @mem: the SPI memory505505+ * @op: the memory operation to execute506506+ *507507+ * Executes a memory operation.508508+ *509509+ * This function first selects the chip and starts the memory operation.510510+ *511511+ * Return: 0 in case of success, a negative error code otherwise.512512+ */513513+static int zynq_qspi_exec_mem_op(struct spi_mem *mem,514514+ const struct spi_mem_op *op)515515+{516516+ struct zynq_qspi *xqspi = spi_controller_get_devdata(mem->spi->master);517517+ int err = 0, i;518518+ u8 *tmpbuf;519519+520520+ dev_dbg(xqspi->dev, "cmd:%#x mode:%d.%d.%d.%d\n",521521+ op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,522522+ op->dummy.buswidth, op->data.buswidth);523523+524524+ zynq_qspi_chipselect(mem->spi, true);525525+ zynq_qspi_config_op(xqspi, mem->spi);526526+527527+ if (op->cmd.opcode) {528528+ reinit_completion(&xqspi->data_completion);529529+ xqspi->txbuf = (u8 *)&op->cmd.opcode;530530+ xqspi->rxbuf = NULL;531531+ xqspi->tx_bytes = sizeof(op->cmd.opcode);532532+ xqspi->rx_bytes = sizeof(op->cmd.opcode);533533+ zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true);534534+ zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET,535535+ ZYNQ_QSPI_IXR_RXTX_MASK);536536+ if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion,537537+ msecs_to_jiffies(1000)))538538+ err = -ETIMEDOUT;539539+ }540540+541541+ if (op->addr.nbytes) {542542+ for (i = 0; i < op->addr.nbytes; i++) {543543+ xqspi->txbuf[i] = op->addr.val >>544544+ (8 * (op->addr.nbytes - i - 1));545545+ }546546+547547+ reinit_completion(&xqspi->data_completion);548548+ xqspi->rxbuf = NULL;549549+ xqspi->tx_bytes = op->addr.nbytes;550550+ xqspi->rx_bytes = op->addr.nbytes;551551+ zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true);552552+ zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET,553553+ ZYNQ_QSPI_IXR_RXTX_MASK);554554+ if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion,555555+ msecs_to_jiffies(1000)))556556+ err = -ETIMEDOUT;557557+ }558558+559559+ if (op->dummy.nbytes) {560560+ tmpbuf = kzalloc(op->dummy.nbytes, GFP_KERNEL);561561+ memset(tmpbuf, 0xff, op->dummy.nbytes);562562+ reinit_completion(&xqspi->data_completion);563563+ xqspi->txbuf = tmpbuf;564564+ xqspi->rxbuf = NULL;565565+ xqspi->tx_bytes = op->dummy.nbytes;566566+ xqspi->rx_bytes = op->dummy.nbytes;567567+ zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true);568568+ zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET,569569+ ZYNQ_QSPI_IXR_RXTX_MASK);570570+ if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion,571571+ msecs_to_jiffies(1000)))572572+ err = -ETIMEDOUT;573573+574574+ kfree(tmpbuf);575575+ }576576+577577+ if (op->data.nbytes) {578578+ reinit_completion(&xqspi->data_completion);579579+ if (op->data.dir == SPI_MEM_DATA_OUT) {580580+ xqspi->txbuf = (u8 *)op->data.buf.out;581581+ xqspi->tx_bytes = op->data.nbytes;582582+ xqspi->rxbuf = NULL;583583+ xqspi->rx_bytes = op->data.nbytes;584584+ } else {585585+ xqspi->txbuf = NULL;586586+ xqspi->rxbuf = (u8 *)op->data.buf.in;587587+ xqspi->rx_bytes = op->data.nbytes;588588+ xqspi->tx_bytes = op->data.nbytes;589589+ }590590+591591+ zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true);592592+ zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET,593593+ ZYNQ_QSPI_IXR_RXTX_MASK);594594+ if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion,595595+ msecs_to_jiffies(1000)))596596+ err = -ETIMEDOUT;597597+ }598598+ zynq_qspi_chipselect(mem->spi, false);599599+600600+ return err;601601+}602602+603603+static const struct spi_controller_mem_ops zynq_qspi_mem_ops = {604604+ .supports_op = zynq_qspi_supports_op,605605+ .exec_op = zynq_qspi_exec_mem_op,606606+};607607+608608+/**609609+ * zynq_qspi_probe - Probe method for the QSPI driver610610+ * @pdev: Pointer to the platform_device structure611611+ *612612+ * This function initializes the driver data structures and the hardware.613613+ *614614+ * Return: 0 on success and error value on failure615615+ */616616+static int zynq_qspi_probe(struct platform_device *pdev)617617+{618618+ int ret = 0;619619+ struct spi_controller *ctlr;620620+ struct device *dev = &pdev->dev;621621+ struct device_node *np = dev->of_node;622622+ struct zynq_qspi *xqspi;623623+ struct resource *res;624624+ u32 num_cs;625625+626626+ ctlr = spi_alloc_master(&pdev->dev, sizeof(*xqspi));627627+ if (!ctlr)628628+ return -ENOMEM;629629+630630+ xqspi = spi_controller_get_devdata(ctlr);631631+ xqspi->dev = dev;632632+ platform_set_drvdata(pdev, xqspi);633633+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);634634+ xqspi->regs = devm_ioremap_resource(&pdev->dev, res);635635+ if (IS_ERR(xqspi->regs)) {636636+ ret = PTR_ERR(xqspi->regs);637637+ goto remove_master;638638+ }639639+640640+ xqspi->pclk = devm_clk_get(&pdev->dev, "pclk");641641+ if (IS_ERR(xqspi->pclk)) {642642+ dev_err(&pdev->dev, "pclk clock not found.\n");643643+ ret = PTR_ERR(xqspi->pclk);644644+ goto remove_master;645645+ }646646+647647+ init_completion(&xqspi->data_completion);648648+649649+ xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk");650650+ if (IS_ERR(xqspi->refclk)) {651651+ dev_err(&pdev->dev, "ref_clk clock not found.\n");652652+ ret = PTR_ERR(xqspi->refclk);653653+ goto remove_master;654654+ }655655+656656+ ret = clk_prepare_enable(xqspi->pclk);657657+ if (ret) {658658+ dev_err(&pdev->dev, "Unable to enable APB clock.\n");659659+ goto remove_master;660660+ }661661+662662+ ret = clk_prepare_enable(xqspi->refclk);663663+ if (ret) {664664+ dev_err(&pdev->dev, "Unable to enable device clock.\n");665665+ goto clk_dis_pclk;666666+ }667667+668668+ /* QSPI controller initializations */669669+ zynq_qspi_init_hw(xqspi);670670+671671+ xqspi->irq = platform_get_irq(pdev, 0);672672+ if (xqspi->irq <= 0) {673673+ ret = -ENXIO;674674+ dev_err(&pdev->dev, "irq resource not found\n");675675+ goto remove_master;676676+ }677677+ ret = devm_request_irq(&pdev->dev, xqspi->irq, zynq_qspi_irq,678678+ 0, pdev->name, xqspi);679679+ if (ret != 0) {680680+ ret = -ENXIO;681681+ dev_err(&pdev->dev, "request_irq failed\n");682682+ goto remove_master;683683+ }684684+685685+ ret = of_property_read_u32(np, "num-cs",686686+ &num_cs);687687+ if (ret < 0)688688+ ctlr->num_chipselect = ZYNQ_QSPI_DEFAULT_NUM_CS;689689+ else690690+ ctlr->num_chipselect = num_cs;691691+692692+ ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD |693693+ SPI_TX_DUAL | SPI_TX_QUAD;694694+ ctlr->mem_ops = &zynq_qspi_mem_ops;695695+ ctlr->setup = zynq_qspi_setup_op;696696+ ctlr->max_speed_hz = clk_get_rate(xqspi->refclk) / 2;697697+ ctlr->dev.of_node = np;698698+ ret = spi_register_controller(ctlr);699699+ if (ret) {700700+ dev_err(&pdev->dev, "spi_register_master failed\n");701701+ goto clk_dis_all;702702+ }703703+704704+ return ret;705705+706706+clk_dis_all:707707+ clk_disable_unprepare(xqspi->refclk);708708+clk_dis_pclk:709709+ clk_disable_unprepare(xqspi->pclk);710710+remove_master:711711+ spi_controller_put(ctlr);712712+713713+ return ret;714714+}715715+716716+/**717717+ * zynq_qspi_remove - Remove method for the QSPI driver718718+ * @pdev: Pointer to the platform_device structure719719+ *720720+ * This function is called if a device is physically removed from the system or721721+ * if the driver module is being unloaded. It frees all resources allocated to722722+ * the device.723723+ *724724+ * Return: 0 on success and error value on failure725725+ */726726+static int zynq_qspi_remove(struct platform_device *pdev)727727+{728728+ struct zynq_qspi *xqspi = platform_get_drvdata(pdev);729729+730730+ zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0);731731+732732+ clk_disable_unprepare(xqspi->refclk);733733+ clk_disable_unprepare(xqspi->pclk);734734+735735+ return 0;736736+}737737+738738+static const struct of_device_id zynq_qspi_of_match[] = {739739+ { .compatible = "xlnx,zynq-qspi-1.0", },740740+ { /* end of table */ }741741+};742742+743743+MODULE_DEVICE_TABLE(of, zynq_qspi_of_match);744744+745745+/*746746+ * zynq_qspi_driver - This structure defines the QSPI platform driver747747+ */748748+static struct platform_driver zynq_qspi_driver = {749749+ .probe = zynq_qspi_probe,750750+ .remove = zynq_qspi_remove,751751+ .driver = {752752+ .name = "zynq-qspi",753753+ .of_match_table = zynq_qspi_of_match,754754+ },755755+};756756+757757+module_platform_driver(zynq_qspi_driver);758758+759759+MODULE_AUTHOR("Xilinx, Inc.");760760+MODULE_DESCRIPTION("Xilinx Zynq QSPI driver");761761+MODULE_LICENSE("GPL");
+44-28
drivers/spi/spi.c
···36363737#define CREATE_TRACE_POINTS3838#include <trace/events/spi.h>3939+EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);4040+EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);39414042#include "internals.h"4143···11811179 if (msg->status && ctlr->handle_err)11821180 ctlr->handle_err(ctlr, msg);1183118111841184- spi_res_release(ctlr, msg);11851185-11861182 spi_finalize_current_message(ctlr);11831183+11841184+ spi_res_release(ctlr, msg);1187118511881186 return ret;11891187}···22672265{22682266 struct device *dev = ctlr->dev.parent;22692267 struct boardinfo *bi;22702270- int status = -ENODEV;22682268+ int status;22712269 int id, first_dynamic;2272227022732271 if (!dev)···22802278 status = spi_controller_check_ops(ctlr);22812279 if (status)22822280 return status;22832283-22842284- if (!spi_controller_is_slave(ctlr)) {22852285- if (ctlr->use_gpio_descriptors) {22862286- status = spi_get_gpio_descs(ctlr);22872287- if (status)22882288- return status;22892289- /*22902290- * A controller using GPIO descriptors always22912291- * supports SPI_CS_HIGH if need be.22922292- */22932293- ctlr->mode_bits |= SPI_CS_HIGH;22942294- } else {22952295- /* Legacy code path for GPIOs from DT */22962296- status = of_spi_register_master(ctlr);22972297- if (status)22982298- return status;22992299- }23002300- }2301228123022282 /* even if it's just one always-selected device, there must23032283 * be at least one chipselect···23372353 * registration fails if the bus ID is in use.23382354 */23392355 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);23562356+23572357+ if (!spi_controller_is_slave(ctlr)) {23582358+ if (ctlr->use_gpio_descriptors) {23592359+ status = spi_get_gpio_descs(ctlr);23602360+ if (status)23612361+ return status;23622362+ /*23632363+ * A controller using GPIO descriptors always23642364+ * supports SPI_CS_HIGH if need be.23652365+ */23662366+ ctlr->mode_bits |= SPI_CS_HIGH;23672367+ } else {23682368+ /* Legacy code path for GPIOs from DT */23692369+ status = of_spi_register_master(ctlr);23702370+ if (status)23712371+ return status;23722372+ }23732373+ }23742374+23402375 status = device_add(&ctlr->dev);23412376 if (status < 0) {23422377 /* free bus id */···27882785 size_t offset;27892786 size_t count, i;2790278727912791- /* warn once about this fact that we are splitting a transfer */27922792- dev_warn_once(&msg->spi->dev,27932793- "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",27942794- xfer->len, maxsize);27952795-27962788 /* calculate how many we have to replace */27972789 count = DIV_ROUND_UP(xfer->len, maxsize);27982790···29452947 * so it is ignored here.29462948 */29472949 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);29502950+ /* nothing prevents from working with active-high CS in case if it29512951+ * is driven by GPIO.29522952+ */29532953+ if (gpio_is_valid(spi->cs_gpio))29542954+ bad_bits &= ~SPI_CS_HIGH;29482955 ugly_bits = bad_bits &29492956 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |29502957 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);···29942991 return status;29952992}29962993EXPORT_SYMBOL_GPL(spi_setup);29942994+29952995+/**29962996+ * spi_set_cs_timing - configure CS setup, hold, and inactive delays29972997+ * @spi: the device that requires specific CS timing configuration29982998+ * @setup: CS setup time in terms of clock count29992999+ * @hold: CS hold time in terms of clock count30003000+ * @inactive_dly: CS inactive delay between transfers in terms of clock count30013001+ */30023002+void spi_set_cs_timing(struct spi_device *spi, u8 setup, u8 hold,30033003+ u8 inactive_dly)30043004+{30053005+ if (spi->controller->set_cs_timing)30063006+ spi->controller->set_cs_timing(spi, setup, hold, inactive_dly);30073007+}30083008+EXPORT_SYMBOL_GPL(spi_set_cs_timing);2997300929983010static int __spi_validate(struct spi_device *spi, struct spi_message *message)29993011{···3084306630853067 if (!xfer->speed_hz)30863068 xfer->speed_hz = spi->max_speed_hz;30873087- if (!xfer->speed_hz)30883088- xfer->speed_hz = ctlr->max_speed_hz;3089306930903070 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)30913071 xfer->speed_hz = ctlr->max_speed_hz;
···11-config SPI_MT762122- tristate "MediaTek MT7621 SPI Controller"33- depends on RALINK44- help55- This selects a driver for the MediaTek MT7621 SPI Controller.66-
···11// SPDX-License-Identifier: GPL-2.022-/*33- * spi-mt7621.c -- MediaTek MT7621 SPI controller driver44- *55- * Copyright (C) 2011 Sergiy <piratfm@gmail.com>66- * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>77- * Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>88- *99- * Some parts are based on spi-orion.c:1010- * Author: Shadi Ammouri <shadi@marvell.com>1111- * Copyright (C) 2007-2008 Marvell Ltd.1212- */22+//33+// spi-mt7621.c -- MediaTek MT7621 SPI controller driver44+//55+// Copyright (C) 2011 Sergiy <piratfm@gmail.com>66+// Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>77+// Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>88+//99+// Some parts are based on spi-orion.c:1010+// Author: Shadi Ammouri <shadi@marvell.com>1111+// Copyright (C) 2007-2008 Marvell Ltd.13121413#include <linux/clk.h>1514#include <linux/delay.h>···5152#define MT7621_LSB_FIRST BIT(3)52535354struct mt7621_spi {5454- struct spi_master *master;5555+ struct spi_controller *master;5556 void __iomem *base;5657 unsigned int sys_freq;5758 unsigned int speed;···63646465static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)6566{6666- return spi_master_get_devdata(spi->master);6767+ return spi_controller_get_devdata(spi->master);6768}68696970static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)···7677 iowrite32(val, rs->base + reg);7778}78797979-static void mt7621_spi_reset(struct mt7621_spi *rs)8080+static void mt7621_spi_set_cs(struct spi_device *spi, int enable)8081{8181- u32 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);8282+ struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);8383+ int cs = spi->chip_select;8484+ u32 polar = 0;8585+ u32 master;82868387 /*8488 * Select SPI device 7, enable "more buffer mode" and disable8589 * full-duplex (only half-duplex really works on this chip8690 * reliably)8791 */9292+ master = mt7621_spi_read(rs, MT7621_SPI_MASTER);8893 master |= MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE;8994 master &= ~MASTER_FULL_DUPLEX;9090-9195 mt7621_spi_write(rs, MT7621_SPI_MASTER, master);9696+9297 rs->pending_write = 0;9393-}94989595-static void mt7621_spi_set_cs(struct spi_device *spi, int enable)9696-{9797- struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);9898- int cs = spi->chip_select;9999- u32 polar = 0;100100-101101- mt7621_spi_reset(rs);10299 if (enable)103100 polar = BIT(cs);104101 mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);···158163static void mt7621_spi_read_half_duplex(struct mt7621_spi *rs,159164 int rx_len, u8 *buf)160165{166166+ int tx_len;167167+161168 /*162169 * Combine with any pending write, and perform one or more half-duplex163170 * transactions reading 'len' bytes. Data to be written is already in164171 * MT7621_SPI_DATA.165172 */166166- int tx_len = rs->pending_write;167167-173173+ tx_len = rs->pending_write;168174 rs->pending_write = 0;169175170176 while (rx_len || tx_len) {···205209static void mt7621_spi_write_half_duplex(struct mt7621_spi *rs,206210 int tx_len, const u8 *buf)207211{208208- int val = 0;209212 int len = rs->pending_write;213213+ int val = 0;210214211215 if (len & 3) {212216 val = mt7621_spi_read(rs, MT7621_SPI_OPCODE + (len & ~3));···234238 }235239 tx_len -= 1;236240 }241241+237242 if (len & 3) {238243 if (len < 4) {239244 val = swab32(val);···242245 }243246 mt7621_spi_write(rs, MT7621_SPI_OPCODE + (len & ~3), val);244247 }248248+245249 rs->pending_write = len;246250}247251248248-static int mt7621_spi_transfer_one_message(struct spi_master *master,252252+static int mt7621_spi_transfer_one_message(struct spi_controller *master,249253 struct spi_message *m)250254{251251- struct mt7621_spi *rs = spi_master_get_devdata(master);255255+ struct mt7621_spi *rs = spi_controller_get_devdata(master);252256 struct spi_device *spi = m->spi;253257 unsigned int speed = spi->max_speed_hz;254258 struct spi_transfer *t = NULL;···266268 goto msg_done;267269 }268270271271+ /* Assert CS */269272 mt7621_spi_set_cs(spi, 1);273273+270274 m->actual_length = 0;271275 list_for_each_entry(t, &m->transfers, transfer_list) {272276 if ((t->rx_buf) && (t->tx_buf)) {273273- /* This controller will shift some extra data out277277+ /*278278+ * This controller will shift some extra data out274279 * of spi_opcode if (mosi_bit_cnt > 0) &&275280 * (cmd_bit_cnt == 0). So the claimed full-duplex276281 * support is broken since we have no way to read···288287 }289288 m->actual_length += t->len;290289 }291291- mt7621_spi_flush(rs);292290291291+ /* Flush data and deassert CS */292292+ mt7621_spi_flush(rs);293293 mt7621_spi_set_cs(spi, 0);294294295295msg_done:···305303 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);306304307305 if ((spi->max_speed_hz == 0) ||308308- (spi->max_speed_hz > (rs->sys_freq / 2)))306306+ (spi->max_speed_hz > (rs->sys_freq / 2)))309307 spi->max_speed_hz = (rs->sys_freq / 2);310308311309 if (spi->max_speed_hz < (rs->sys_freq / 4097)) {···326324static int mt7621_spi_probe(struct platform_device *pdev)327325{328326 const struct of_device_id *match;329329- struct spi_master *master;327327+ struct spi_controller *master;330328 struct mt7621_spi *rs;331329 void __iomem *base;332330 struct resource *r;···363361 }364362365363 master->mode_bits = SPI_LSB_FIRST;366366-364364+ master->flags = SPI_CONTROLLER_HALF_DUPLEX;367365 master->setup = mt7621_spi_setup;368366 master->transfer_one_message = mt7621_spi_transfer_one_message;369367 master->bits_per_word_mask = SPI_BPW_MASK(8);···372370373371 dev_set_drvdata(&pdev->dev, master);374372375375- rs = spi_master_get_devdata(master);373373+ rs = spi_controller_get_devdata(master);376374 rs->base = base;377375 rs->clk = clk;378376 rs->master = master;···387385 return ret;388386 }389387390390- mt7621_spi_reset(rs);391391-392392- return spi_register_master(master);388388+ return devm_spi_register_controller(&pdev->dev, master);393389}394390395391static int mt7621_spi_remove(struct platform_device *pdev)396392{397397- struct spi_master *master;393393+ struct spi_controller *master;398394 struct mt7621_spi *rs;399395400396 master = dev_get_drvdata(&pdev->dev);401401- rs = spi_master_get_devdata(master);397397+ rs = spi_controller_get_devdata(master);402398403403- clk_disable(rs->clk);404404- spi_unregister_master(master);399399+ clk_disable_unprepare(rs->clk);405400406401 return 0;407402}
-4
include/linux/platform_data/spi-ep93xx.h
···6677/**88 * struct ep93xx_spi_info - EP93xx specific SPI descriptor99- * @chipselect: array of gpio numbers to use as chip selects1010- * @num_chipselect: ARRAY_SIZE(chipselect)119 * @use_dma: use DMA for the transfers1210 */1311struct ep93xx_spi_info {1414- int *chipselect;1515- int num_chipselect;1612 bool use_dma;1713};1814
···143143 u32 max_speed_hz;144144 u8 chip_select;145145 u8 bits_per_word;146146- u16 mode;146146+ u32 mode;147147#define SPI_CPHA 0x01 /* clock phase */148148#define SPI_CPOL 0x02 /* clock polarity */149149#define SPI_MODE_0 (0|0) /* (original MicroWire) */···330330 * must fail if an unrecognized or unsupported mode is requested.331331 * It's always safe to call this unless transfers are pending on332332 * the device whose settings are being modified.333333+ * @set_cs_timing: optional hook for SPI devices to request SPI master334334+ * controller for configuring specific CS setup time, hold time and inactive335335+ * delay interms of clock counts333336 * @transfer: adds a message to the controller's transfer queue.334337 * @cleanup: frees controller-specific state335338 * @can_dma: determine whether this controller supports DMA···366363 * @unprepare_transfer_hardware: there are currently no more messages on the367364 * queue so the subsystem notifies the driver that it may relax the368365 * hardware by issuing this call366366+ *369367 * @set_cs: set the logic level of the chip select line. May be called370368 * from interrupt context.371369 * @prepare_message: set up the controller to transfer a single message,···443439 u16 dma_alignment;444440445441 /* spi_device.mode flags understood by this controller driver */446446- u16 mode_bits;442442+ u32 mode_bits;447443448444 /* bitmask of supported bits_per_word for transfers */449445 u32 bits_per_word_mask;450446#define SPI_BPW_MASK(bits) BIT((bits) - 1)451451-#define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))452452-#define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1))447447+#define SPI_BPW_RANGE_MASK(min, max) GENMASK((max) - 1, (min) - 1)453448454449 /* limits on transfer speed */455450 u32 min_speed_hz;···491488 * which could break those transfers.492489 */493490 int (*setup)(struct spi_device *spi);491491+492492+ /*493493+ * set_cs_timing() method is for SPI controllers that supports494494+ * configuring CS timing.495495+ *496496+ * This hook allows SPI client drivers to request SPI controllers497497+ * to configure specific CS timing through spi_set_cs_timing() after498498+ * spi_setup().499499+ */500500+ void (*set_cs_timing)(struct spi_device *spi, u8 setup_clk_cycles,501501+ u8 hold_clk_cycles, u8 inactive_clk_cycles);494502495503 /* bidirectional bulk transfers496504 *···12911277 /* mode becomes spi_device.mode, and is essential for chips12921278 * where the default of SPI_CS_HIGH = 0 is wrong.12931279 */12941294- u16 mode;12801280+ u32 mode;1295128112961282 /* ... may need additional spi_device chip config data here.12971283 * avoid stuff protocol drivers can set; but include stuff
+1
include/linux/spi/spi_bitbang.h
···44444545/* start or stop queue processing */4646extern int spi_bitbang_start(struct spi_bitbang *spi);4747+extern int spi_bitbang_init(struct spi_bitbang *spi);4748extern void spi_bitbang_stop(struct spi_bitbang *spi);48494950#endif /* __SPI_BITBANG_H */
···6666 * @delay_usecs: If nonzero, how long to delay after the last bit transfer6767 * before optionally deselecting the device before the next transfer.6868 * @cs_change: True to deselect device before starting the next transfer.6969+ * @word_delay_usecs: If nonzero, how long to wait between words within one7070+ * transfer. This property needs explicit support in the SPI controller,7171+ * otherwise it is silently ignored.6972 *7073 * This structure is mapped directly to the kernel spi_transfer structure;7174 * the fields have the same meanings, except of course that the pointers···103100 __u8 cs_change;104101 __u8 tx_nbits;105102 __u8 rx_nbits;106106- __u16 pad;103103+ __u8 word_delay_usecs;104104+ __u8 pad;107105108106 /* If the contents of 'struct spi_ioc_transfer' ever change109107 * incompatibly, then the ioctl number (currently 0) must change;