···148148 inexpensive battery powered microcontroller evaluation board.149149 This same cable can be used to flash new firmware.150150151151+config SPI_CADENCE152152+ tristate "Cadence SPI controller"153153+ depends on SPI_MASTER154154+ help155155+ This selects the Cadence SPI controller master driver156156+ used by Xilinx Zynq.157157+151158config SPI_CLPS711X152159 tristate "CLPS711X host SPI controller"153160 depends on ARCH_CLPS711X || COMPILE_TEST
···11+/*22+ * Cadence SPI controller driver (master mode only)33+ *44+ * Copyright (C) 2008 - 2014 Xilinx, Inc.55+ *66+ * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)77+ *88+ * This program is free software; you can redistribute it and/or modify it under99+ * the terms of the GNU General Public License version 2 as published by the1010+ * Free Software Foundation; either version 2 of the License, or (at your1111+ * option) any later version.1212+ */1313+1414+#include <linux/clk.h>1515+#include <linux/delay.h>1616+#include <linux/interrupt.h>1717+#include <linux/io.h>1818+#include <linux/module.h>1919+#include <linux/of_irq.h>2020+#include <linux/of_address.h>2121+#include <linux/platform_device.h>2222+#include <linux/spi/spi.h>2323+2424+/* Name of this driver */2525+#define CDNS_SPI_NAME "cdns-spi"2626+2727+/* Register offset definitions */2828+#define CDNS_SPI_CR_OFFSET 0x00 /* Configuration Register, RW */2929+#define CDNS_SPI_ISR_OFFSET 0x04 /* Interrupt Status Register, RO */3030+#define CDNS_SPI_IER_OFFSET 0x08 /* Interrupt Enable Register, WO */3131+#define CDNS_SPI_IDR_OFFSET 0x0c /* Interrupt Disable Register, WO */3232+#define CDNS_SPI_IMR_OFFSET 0x10 /* Interrupt Enabled Mask Register, RO */3333+#define CDNS_SPI_ER_OFFSET 0x14 /* Enable/Disable Register, RW */3434+#define CDNS_SPI_DR_OFFSET 0x18 /* Delay Register, RW */3535+#define CDNS_SPI_TXD_OFFSET 0x1C /* Data Transmit Register, WO */3636+#define CDNS_SPI_RXD_OFFSET 0x20 /* Data Receive Register, RO */3737+#define CDNS_SPI_SICR_OFFSET 0x24 /* Slave Idle Count Register, RW */3838+#define CDNS_SPI_THLD_OFFSET 0x28 /* Transmit FIFO Watermark Register,RW */3939+4040+/*4141+ * SPI Configuration Register bit Masks4242+ *4343+ * This register contains various control bits that affect the operation4444+ * of the SPI controller4545+ */4646+#define CDNS_SPI_CR_MANSTRT_MASK 0x00010000 /* Manual TX Start */4747+#define CDNS_SPI_CR_CPHA_MASK 0x00000004 /* Clock Phase Control */4848+#define CDNS_SPI_CR_CPOL_MASK 0x00000002 /* Clock Polarity Control */4949+#define CDNS_SPI_CR_SSCTRL_MASK 0x00003C00 /* Slave Select Mask */5050+#define CDNS_SPI_CR_BAUD_DIV_MASK 0x00000038 /* Baud Rate Divisor Mask */5151+#define CDNS_SPI_CR_MSTREN_MASK 0x00000001 /* Master Enable Mask */5252+#define CDNS_SPI_CR_MANSTRTEN_MASK 0x00008000 /* Manual TX Enable Mask */5353+#define CDNS_SPI_CR_SSFORCE_MASK 0x00004000 /* Manual SS Enable Mask */5454+#define CDNS_SPI_CR_BAUD_DIV_4_MASK 0x00000008 /* Default Baud Div Mask */5555+#define CDNS_SPI_CR_DEFAULT_MASK (CDNS_SPI_CR_MSTREN_MASK | \5656+ CDNS_SPI_CR_SSCTRL_MASK | \5757+ CDNS_SPI_CR_SSFORCE_MASK | \5858+ CDNS_SPI_CR_BAUD_DIV_4_MASK)5959+6060+/*6161+ * SPI Configuration Register - Baud rate and slave select6262+ *6363+ * These are the values used in the calculation of baud rate divisor and6464+ * setting the slave select.6565+ */6666+6767+#define CDNS_SPI_BAUD_DIV_MAX 7 /* Baud rate divisor maximum */6868+#define CDNS_SPI_BAUD_DIV_MIN 1 /* Baud rate divisor minimum */6969+#define CDNS_SPI_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift in CR */7070+#define CDNS_SPI_SS_SHIFT 10 /* Slave Select field shift in CR */7171+#define CDNS_SPI_SS0 0x1 /* Slave Select zero */7272+7373+/*7474+ * SPI Interrupt Registers bit Masks7575+ *7676+ * All the four interrupt registers (Status/Mask/Enable/Disable) have the same7777+ * bit definitions.7878+ */7979+#define CDNS_SPI_IXR_TXOW_MASK 0x00000004 /* SPI TX FIFO Overwater */8080+#define CDNS_SPI_IXR_MODF_MASK 0x00000002 /* SPI Mode Fault */8181+#define CDNS_SPI_IXR_RXNEMTY_MASK 0x00000010 /* SPI RX FIFO Not Empty */8282+#define CDNS_SPI_IXR_DEFAULT_MASK (CDNS_SPI_IXR_TXOW_MASK | \8383+ CDNS_SPI_IXR_MODF_MASK)8484+#define CDNS_SPI_IXR_TXFULL_MASK 0x00000008 /* SPI TX Full */8585+#define CDNS_SPI_IXR_ALL_MASK 0x0000007F /* SPI all interrupts */8686+8787+/*8888+ * SPI Enable Register bit Masks8989+ *9090+ * This register is used to enable or disable the SPI controller9191+ */9292+#define CDNS_SPI_ER_ENABLE_MASK 0x00000001 /* SPI Enable Bit Mask */9393+#define CDNS_SPI_ER_DISABLE_MASK 0x0 /* SPI Disable Bit Mask */9494+9595+/* SPI FIFO depth in bytes */9696+#define CDNS_SPI_FIFO_DEPTH 1289797+9898+/* Default number of chip select lines */9999+#define CDNS_SPI_DEFAULT_NUM_CS 4100100+101101+/**102102+ * struct cdns_spi - This definition defines spi driver instance103103+ * @regs: Virtual address of the SPI controller registers104104+ * @ref_clk: Pointer to the peripheral clock105105+ * @pclk: Pointer to the APB clock106106+ * @speed_hz: Current SPI bus clock speed in Hz107107+ * @txbuf: Pointer to the TX buffer108108+ * @rxbuf: Pointer to the RX buffer109109+ * @tx_bytes: Number of bytes left to transfer110110+ * @rx_bytes: Number of bytes requested111111+ * @dev_busy: Device busy flag112112+ * @is_decoded_cs: Flag for decoder property set or not113113+ */114114+struct cdns_spi {115115+ void __iomem *regs;116116+ struct clk *ref_clk;117117+ struct clk *pclk;118118+ u32 speed_hz;119119+ const u8 *txbuf;120120+ u8 *rxbuf;121121+ int tx_bytes;122122+ int rx_bytes;123123+ u8 dev_busy;124124+ u32 is_decoded_cs;125125+};126126+127127+/* Macros for the SPI controller read/write */128128+static inline u32 cdns_spi_read(struct cdns_spi *xspi, u32 offset)129129+{130130+ return readl_relaxed(xspi->regs + offset);131131+}132132+133133+static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val)134134+{135135+ writel_relaxed(val, xspi->regs + offset);136136+}137137+138138+/**139139+ * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller140140+ * @xspi: Pointer to the cdns_spi structure141141+ *142142+ * On reset the SPI controller is configured to be in master mode, baud rate143143+ * divisor is set to 4, threshold value for TX FIFO not full interrupt is set144144+ * to 1 and size of the word to be transferred as 8 bit.145145+ * This function initializes the SPI controller to disable and clear all the146146+ * interrupts, enable manual slave select and manual start, deselect all the147147+ * chip select lines, and enable the SPI controller.148148+ */149149+static void cdns_spi_init_hw(struct cdns_spi *xspi)150150+{151151+ cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,152152+ CDNS_SPI_ER_DISABLE_MASK);153153+ cdns_spi_write(xspi, CDNS_SPI_IDR_OFFSET,154154+ CDNS_SPI_IXR_ALL_MASK);155155+156156+ /* Clear the RX FIFO */157157+ while (cdns_spi_read(xspi, CDNS_SPI_ISR_OFFSET) &158158+ CDNS_SPI_IXR_RXNEMTY_MASK)159159+ cdns_spi_read(xspi, CDNS_SPI_RXD_OFFSET);160160+161161+ cdns_spi_write(xspi, CDNS_SPI_ISR_OFFSET,162162+ CDNS_SPI_IXR_ALL_MASK);163163+ cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET,164164+ CDNS_SPI_CR_DEFAULT_MASK);165165+ cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,166166+ CDNS_SPI_ER_ENABLE_MASK);167167+}168168+169169+/**170170+ * cdns_spi_chipselect - Select or deselect the chip select line171171+ * @spi: Pointer to the spi_device structure172172+ * @is_on: Select(0) or deselect (1) the chip select line173173+ */174174+static void cdns_spi_chipselect(struct spi_device *spi, bool is_high)175175+{176176+ struct cdns_spi *xspi = spi_master_get_devdata(spi->master);177177+ u32 ctrl_reg;178178+179179+ ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR_OFFSET);180180+181181+ if (is_high) {182182+ /* Deselect the slave */183183+ ctrl_reg |= CDNS_SPI_CR_SSCTRL_MASK;184184+ } else {185185+ /* Select the slave */186186+ ctrl_reg &= ~CDNS_SPI_CR_SSCTRL_MASK;187187+ if (!(xspi->is_decoded_cs))188188+ ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) <<189189+ CDNS_SPI_SS_SHIFT) &190190+ CDNS_SPI_CR_SSCTRL_MASK;191191+ else192192+ ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) &193193+ CDNS_SPI_CR_SSCTRL_MASK;194194+ }195195+196196+ cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET, ctrl_reg);197197+}198198+199199+/**200200+ * cdns_spi_config_clock_mode - Sets clock polarity and phase201201+ * @spi: Pointer to the spi_device structure202202+ *203203+ * Sets the requested clock polarity and phase.204204+ */205205+static void cdns_spi_config_clock_mode(struct spi_device *spi)206206+{207207+ struct cdns_spi *xspi = spi_master_get_devdata(spi->master);208208+ u32 ctrl_reg;209209+210210+ ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR_OFFSET);211211+212212+ /* Set the SPI clock phase and clock polarity */213213+ ctrl_reg &= ~(CDNS_SPI_CR_CPHA_MASK | CDNS_SPI_CR_CPOL_MASK);214214+ if (spi->mode & SPI_CPHA)215215+ ctrl_reg |= CDNS_SPI_CR_CPHA_MASK;216216+ if (spi->mode & SPI_CPOL)217217+ ctrl_reg |= CDNS_SPI_CR_CPOL_MASK;218218+219219+ cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET, ctrl_reg);220220+}221221+222222+/**223223+ * cdns_spi_config_clock_freq - Sets clock frequency224224+ * @spi: Pointer to the spi_device structure225225+ * @transfer: Pointer to the spi_transfer structure which provides226226+ * information about next transfer setup parameters227227+ *228228+ * Sets the requested clock frequency.229229+ * Note: If the requested frequency is not an exact match with what can be230230+ * obtained using the prescalar value the driver sets the clock frequency which231231+ * is lower than the requested frequency (maximum lower) for the transfer. If232232+ * the requested frequency is higher or lower than that is supported by the SPI233233+ * controller the driver will set the highest or lowest frequency supported by234234+ * controller.235235+ */236236+static void cdns_spi_config_clock_freq(struct spi_device *spi,237237+ struct spi_transfer *transfer)238238+{239239+ struct cdns_spi *xspi = spi_master_get_devdata(spi->master);240240+ u32 ctrl_reg, baud_rate_val;241241+ unsigned long frequency;242242+243243+ frequency = clk_get_rate(xspi->ref_clk);244244+245245+ ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR_OFFSET);246246+247247+ /* Set the clock frequency */248248+ if (xspi->speed_hz != transfer->speed_hz) {249249+ /* first valid value is 1 */250250+ baud_rate_val = CDNS_SPI_BAUD_DIV_MIN;251251+ while ((baud_rate_val < CDNS_SPI_BAUD_DIV_MAX) &&252252+ (frequency / (2 << baud_rate_val)) > transfer->speed_hz)253253+ baud_rate_val++;254254+255255+ ctrl_reg &= ~CDNS_SPI_CR_BAUD_DIV_MASK;256256+ ctrl_reg |= baud_rate_val << CDNS_SPI_BAUD_DIV_SHIFT;257257+258258+ xspi->speed_hz = frequency / (2 << baud_rate_val);259259+ }260260+ cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET, ctrl_reg);261261+}262262+263263+/**264264+ * cdns_spi_setup_transfer - Configure SPI controller for specified transfer265265+ * @spi: Pointer to the spi_device structure266266+ * @transfer: Pointer to the spi_transfer structure which provides267267+ * information about next transfer setup parameters268268+ *269269+ * Sets the operational mode of SPI controller for the next SPI transfer and270270+ * sets the requested clock frequency.271271+ *272272+ * Return: Always 0273273+ */274274+static int cdns_spi_setup_transfer(struct spi_device *spi,275275+ struct spi_transfer *transfer)276276+{277277+ struct cdns_spi *xspi = spi_master_get_devdata(spi->master);278278+279279+ cdns_spi_config_clock_freq(spi, transfer);280280+281281+ dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n",282282+ __func__, spi->mode, spi->bits_per_word,283283+ xspi->speed_hz);284284+285285+ return 0;286286+}287287+288288+/**289289+ * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible290290+ * @xspi: Pointer to the cdns_spi structure291291+ */292292+static void cdns_spi_fill_tx_fifo(struct cdns_spi *xspi)293293+{294294+ unsigned long trans_cnt = 0;295295+296296+ while ((trans_cnt < CDNS_SPI_FIFO_DEPTH) &&297297+ (xspi->tx_bytes > 0)) {298298+ if (xspi->txbuf)299299+ cdns_spi_write(xspi, CDNS_SPI_TXD_OFFSET,300300+ *xspi->txbuf++);301301+ else302302+ cdns_spi_write(xspi, CDNS_SPI_TXD_OFFSET, 0);303303+304304+ xspi->tx_bytes--;305305+ trans_cnt++;306306+ }307307+}308308+309309+/**310310+ * cdns_spi_irq - Interrupt service routine of the SPI controller311311+ * @irq: IRQ number312312+ * @dev_id: Pointer to the xspi structure313313+ *314314+ * This function handles TX empty and Mode Fault interrupts only.315315+ * On TX empty interrupt this function reads the received data from RX FIFO and316316+ * fills the TX FIFO if there is any data remaining to be transferred.317317+ * On Mode Fault interrupt this function indicates that transfer is completed,318318+ * the SPI subsystem will identify the error as the remaining bytes to be319319+ * transferred is non-zero.320320+ *321321+ * Return: IRQ_HANDLED when handled; IRQ_NONE otherwise.322322+ */323323+static irqreturn_t cdns_spi_irq(int irq, void *dev_id)324324+{325325+ struct spi_master *master = dev_id;326326+ struct cdns_spi *xspi = spi_master_get_devdata(master);327327+ u32 intr_status, status;328328+329329+ status = IRQ_NONE;330330+ intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR_OFFSET);331331+ cdns_spi_write(xspi, CDNS_SPI_ISR_OFFSET, intr_status);332332+333333+ if (intr_status & CDNS_SPI_IXR_MODF_MASK) {334334+ /* Indicate that transfer is completed, the SPI subsystem will335335+ * identify the error as the remaining bytes to be336336+ * transferred is non-zero337337+ */338338+ cdns_spi_write(xspi, CDNS_SPI_IDR_OFFSET,339339+ CDNS_SPI_IXR_DEFAULT_MASK);340340+ spi_finalize_current_transfer(master);341341+ status = IRQ_HANDLED;342342+ } else if (intr_status & CDNS_SPI_IXR_TXOW_MASK) {343343+ unsigned long trans_cnt;344344+345345+ trans_cnt = xspi->rx_bytes - xspi->tx_bytes;346346+347347+ /* Read out the data from the RX FIFO */348348+ while (trans_cnt) {349349+ u8 data;350350+351351+ data = cdns_spi_read(xspi, CDNS_SPI_RXD_OFFSET);352352+ if (xspi->rxbuf)353353+ *xspi->rxbuf++ = data;354354+355355+ xspi->rx_bytes--;356356+ trans_cnt--;357357+ }358358+359359+ if (xspi->tx_bytes) {360360+ /* There is more data to send */361361+ cdns_spi_fill_tx_fifo(xspi);362362+ } else {363363+ /* Transfer is completed */364364+ cdns_spi_write(xspi, CDNS_SPI_IDR_OFFSET,365365+ CDNS_SPI_IXR_DEFAULT_MASK);366366+ spi_finalize_current_transfer(master);367367+ }368368+ status = IRQ_HANDLED;369369+ }370370+371371+ return status;372372+}373373+374374+/**375375+ * cdns_transfer_one - Initiates the SPI transfer376376+ * @master: Pointer to spi_master structure377377+ * @spi: Pointer to the spi_device structure378378+ * @transfer: Pointer to the spi_transfer structure which provides379379+ * information about next transfer parameters380380+ *381381+ * This function fills the TX FIFO, starts the SPI transfer and382382+ * returns a positive transfer count so that core will wait for completion.383383+ *384384+ * Return: Number of bytes transferred in the last transfer385385+ */386386+static int cdns_transfer_one(struct spi_master *master,387387+ struct spi_device *spi,388388+ struct spi_transfer *transfer)389389+{390390+ struct cdns_spi *xspi = spi_master_get_devdata(master);391391+392392+ xspi->txbuf = transfer->tx_buf;393393+ xspi->rxbuf = transfer->rx_buf;394394+ xspi->tx_bytes = transfer->len;395395+ xspi->rx_bytes = transfer->len;396396+397397+ cdns_spi_setup_transfer(spi, transfer);398398+399399+ cdns_spi_fill_tx_fifo(xspi);400400+401401+ cdns_spi_write(xspi, CDNS_SPI_IER_OFFSET,402402+ CDNS_SPI_IXR_DEFAULT_MASK);403403+ return transfer->len;404404+}405405+406406+/**407407+ * cdns_prepare_transfer_hardware - Prepares hardware for transfer.408408+ * @master: Pointer to the spi_master structure which provides409409+ * information about the controller.410410+ *411411+ * This function enables SPI master controller.412412+ *413413+ * Return: 0 always414414+ */415415+static int cdns_prepare_transfer_hardware(struct spi_master *master)416416+{417417+ struct cdns_spi *xspi = spi_master_get_devdata(master);418418+419419+ cdns_spi_config_clock_mode(master->cur_msg->spi);420420+421421+ cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,422422+ CDNS_SPI_ER_ENABLE_MASK);423423+424424+ return 0;425425+}426426+427427+/**428428+ * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer429429+ * @master: Pointer to the spi_master structure which provides430430+ * information about the controller.431431+ *432432+ * This function disables the SPI master controller.433433+ *434434+ * Return: 0 always435435+ */436436+static int cdns_unprepare_transfer_hardware(struct spi_master *master)437437+{438438+ struct cdns_spi *xspi = spi_master_get_devdata(master);439439+440440+ cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,441441+ CDNS_SPI_ER_DISABLE_MASK);442442+443443+ return 0;444444+}445445+446446+/**447447+ * cdns_spi_probe - Probe method for the SPI driver448448+ * @pdev: Pointer to the platform_device structure449449+ *450450+ * This function initializes the driver data structures and the hardware.451451+ *452452+ * Return: 0 on success and error value on error453453+ */454454+static int cdns_spi_probe(struct platform_device *pdev)455455+{456456+ int ret = 0, irq;457457+ struct spi_master *master;458458+ struct cdns_spi *xspi;459459+ struct resource *res;460460+ u32 num_cs;461461+462462+ master = spi_alloc_master(&pdev->dev, sizeof(*xspi));463463+ if (master == NULL)464464+ return -ENOMEM;465465+466466+ xspi = spi_master_get_devdata(master);467467+ master->dev.of_node = pdev->dev.of_node;468468+ platform_set_drvdata(pdev, master);469469+470470+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);471471+ xspi->regs = devm_ioremap_resource(&pdev->dev, res);472472+ if (IS_ERR(xspi->regs)) {473473+ ret = PTR_ERR(xspi->regs);474474+ goto remove_master;475475+ }476476+477477+ xspi->pclk = devm_clk_get(&pdev->dev, "pclk");478478+ if (IS_ERR(xspi->pclk)) {479479+ dev_err(&pdev->dev, "pclk clock not found.\n");480480+ ret = PTR_ERR(xspi->pclk);481481+ goto remove_master;482482+ }483483+484484+ xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");485485+ if (IS_ERR(xspi->ref_clk)) {486486+ dev_err(&pdev->dev, "ref_clk clock not found.\n");487487+ ret = PTR_ERR(xspi->ref_clk);488488+ goto remove_master;489489+ }490490+491491+ ret = clk_prepare_enable(xspi->pclk);492492+ if (ret) {493493+ dev_err(&pdev->dev, "Unable to enable APB clock.\n");494494+ goto remove_master;495495+ }496496+497497+ ret = clk_prepare_enable(xspi->ref_clk);498498+ if (ret) {499499+ dev_err(&pdev->dev, "Unable to enable device clock.\n");500500+ goto clk_dis_apb;501501+ }502502+503503+ /* SPI controller initializations */504504+ cdns_spi_init_hw(xspi);505505+506506+ irq = platform_get_irq(pdev, 0);507507+ if (irq <= 0) {508508+ ret = -ENXIO;509509+ dev_err(&pdev->dev, "irq number is invalid\n");510510+ goto remove_master;511511+ }512512+513513+ ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq,514514+ 0, pdev->name, master);515515+ if (ret != 0) {516516+ ret = -ENXIO;517517+ dev_err(&pdev->dev, "request_irq failed\n");518518+ goto remove_master;519519+ }520520+521521+ ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);522522+523523+ if (ret < 0)524524+ master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS;525525+ else526526+ master->num_chipselect = num_cs;527527+528528+ ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs",529529+ &xspi->is_decoded_cs);530530+531531+ if (ret < 0)532532+ xspi->is_decoded_cs = 0;533533+534534+ master->prepare_transfer_hardware = cdns_prepare_transfer_hardware;535535+ master->transfer_one = cdns_transfer_one;536536+ master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware;537537+ master->set_cs = cdns_spi_chipselect;538538+ master->mode_bits = SPI_CPOL | SPI_CPHA;539539+540540+ /* Set to default valid value */541541+ master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4;542542+ xspi->speed_hz = master->max_speed_hz;543543+544544+ master->bits_per_word_mask = SPI_BPW_MASK(8);545545+546546+ ret = spi_register_master(master);547547+ if (ret) {548548+ dev_err(&pdev->dev, "spi_register_master failed\n");549549+ goto clk_dis_all;550550+ }551551+552552+ return ret;553553+554554+clk_dis_all:555555+ clk_disable_unprepare(xspi->ref_clk);556556+clk_dis_apb:557557+ clk_disable_unprepare(xspi->pclk);558558+remove_master:559559+ spi_master_put(master);560560+ return ret;561561+}562562+563563+/**564564+ * cdns_spi_remove - Remove method for the SPI driver565565+ * @pdev: Pointer to the platform_device structure566566+ *567567+ * This function is called if a device is physically removed from the system or568568+ * if the driver module is being unloaded. It frees all resources allocated to569569+ * the device.570570+ *571571+ * Return: 0 on success and error value on error572572+ */573573+static int cdns_spi_remove(struct platform_device *pdev)574574+{575575+ struct spi_master *master = platform_get_drvdata(pdev);576576+ struct cdns_spi *xspi = spi_master_get_devdata(master);577577+578578+ cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,579579+ CDNS_SPI_ER_DISABLE_MASK);580580+581581+ clk_disable_unprepare(xspi->ref_clk);582582+ clk_disable_unprepare(xspi->pclk);583583+584584+ spi_unregister_master(master);585585+586586+ return 0;587587+}588588+589589+/**590590+ * cdns_spi_suspend - Suspend method for the SPI driver591591+ * @dev: Address of the platform_device structure592592+ *593593+ * This function disables the SPI controller and594594+ * changes the driver state to "suspend"595595+ *596596+ * Return: Always 0597597+ */598598+static int __maybe_unused cdns_spi_suspend(struct device *dev)599599+{600600+ struct platform_device *pdev = container_of(dev,601601+ struct platform_device, dev);602602+ struct spi_master *master = platform_get_drvdata(pdev);603603+ struct cdns_spi *xspi = spi_master_get_devdata(master);604604+605605+ spi_master_suspend(master);606606+607607+ clk_disable_unprepare(xspi->ref_clk);608608+609609+ clk_disable_unprepare(xspi->pclk);610610+611611+ return 0;612612+}613613+614614+/**615615+ * cdns_spi_resume - Resume method for the SPI driver616616+ * @dev: Address of the platform_device structure617617+ *618618+ * This function changes the driver state to "ready"619619+ *620620+ * Return: 0 on success and error value on error621621+ */622622+static int __maybe_unused cdns_spi_resume(struct device *dev)623623+{624624+ struct platform_device *pdev = container_of(dev,625625+ struct platform_device, dev);626626+ struct spi_master *master = platform_get_drvdata(pdev);627627+ struct cdns_spi *xspi = spi_master_get_devdata(master);628628+ int ret = 0;629629+630630+ ret = clk_prepare_enable(xspi->pclk);631631+ if (ret) {632632+ dev_err(dev, "Cannot enable APB clock.\n");633633+ return ret;634634+ }635635+636636+ ret = clk_prepare_enable(xspi->ref_clk);637637+ if (ret) {638638+ dev_err(dev, "Cannot enable device clock.\n");639639+ clk_disable(xspi->pclk);640640+ return ret;641641+ }642642+ spi_master_resume(master);643643+644644+ return 0;645645+}646646+647647+static SIMPLE_DEV_PM_OPS(cdns_spi_dev_pm_ops, cdns_spi_suspend,648648+ cdns_spi_resume);649649+650650+static struct of_device_id cdns_spi_of_match[] = {651651+ { .compatible = "xlnx,zynq-spi-r1p6" },652652+ { .compatible = "cdns,spi-r1p6" },653653+ { /* end of table */ }654654+};655655+MODULE_DEVICE_TABLE(of, cdns_spi_of_match);656656+657657+/* cdns_spi_driver - This structure defines the SPI subsystem platform driver */658658+static struct platform_driver cdns_spi_driver = {659659+ .probe = cdns_spi_probe,660660+ .remove = cdns_spi_remove,661661+ .driver = {662662+ .name = CDNS_SPI_NAME,663663+ .owner = THIS_MODULE,664664+ .of_match_table = cdns_spi_of_match,665665+ .pm = &cdns_spi_dev_pm_ops,666666+ },667667+};668668+669669+module_platform_driver(cdns_spi_driver);670670+671671+MODULE_AUTHOR("Xilinx, Inc.");672672+MODULE_DESCRIPTION("Cadence SPI driver");673673+MODULE_LICENSE("GPL");