#define DEBUG #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DRIVER_NAME "ark_dw_spi" /* Register offsets */ #define DW_SPI_CTRL0 0x00 #define DW_SPI_CTRL1 0x04 #define DW_SPI_SSIENR 0x08 #define DW_SPI_MWCR 0x0c #define DW_SPI_SER 0x10 #define DW_SPI_BAUDR 0x14 #define DW_SPI_TXFLTR 0x18 #define DW_SPI_RXFLTR 0x1c #define DW_SPI_TXFLR 0x20 #define DW_SPI_RXFLR 0x24 #define DW_SPI_SR 0x28 #define DW_SPI_IMR 0x2c #define DW_SPI_ISR 0x30 #define DW_SPI_RISR 0x34 #define DW_SPI_TXOICR 0x38 #define DW_SPI_RXOICR 0x3c #define DW_SPI_RXUICR 0x40 #define DW_SPI_MSTICR 0x44 #define DW_SPI_ICR 0x48 #define DW_SPI_DMACR 0x4c #define DW_SPI_DMATDLR 0x50 #define DW_SPI_DMARDLR 0x54 #define DW_SPI_IDR 0x58 #define DW_SPI_VERSION 0x5c #define DW_SPI_DR 0x60 /* Bit fields in CTRLR0 */ #define SPI_DFS_OFFSET 16 #define SPI_FRF_OFFSET 4 #define SPI_FRF_SPI 0x0 #define SPI_FRF_SSP 0x1 #define SPI_FRF_MICROWIRE 0x2 #define SPI_FRF_RESV 0x3 #define SPI_MODE_OFFSET 6 #define SPI_SCPH_OFFSET 6 #define SPI_SCOL_OFFSET 7 #define SPI_TMOD_OFFSET 8 #define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET) #define SPI_TMOD_TR 0x0 /* xmit & recv */ #define SPI_TMOD_TO 0x1 /* xmit only */ #define SPI_TMOD_RO 0x2 /* recv only */ #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */ #define SPI_SLVOE_OFFSET 10 #define SPI_SRL_OFFSET 11 #define SPI_CFS_OFFSET 12 /* Bit fields in SR, 7 bits */ #define SR_MASK 0x7f /* cover 7 bits */ #define SR_BUSY (1 << 0) #define SR_TF_NOT_FULL (1 << 1) #define SR_TF_EMPT (1 << 2) #define SR_RF_NOT_EMPT (1 << 3) #define SR_RF_FULL (1 << 4) #define SR_TX_ERR (1 << 5) #define SR_DCOL (1 << 6) /* Bit fields in ISR, IMR, RISR, 7 bits */ #define SPI_INT_TXEI (1 << 0) #define SPI_INT_TXOI (1 << 1) #define SPI_INT_RXUI (1 << 2) #define SPI_INT_RXOI (1 << 3) #define SPI_INT_RXFI (1 << 4) #define SPI_INT_MSTI (1 << 5) /* Bit fields in DMACR */ #define SPI_DMA_RDMAE (1 << 0) #define SPI_DMA_TDMAE (1 << 1) /* TX RX interrupt level threshold, max can be 256 */ #define SPI_INT_THRESHOLD 32 enum dw_ssi_type { SSI_MOTO_SPI = 0, SSI_TI_SSP, SSI_NS_MICROWIRE, }; struct dw_spi; struct dw_spi_dma_ops { int (*dma_init)(struct dw_spi *dws); void (*dma_exit)(struct dw_spi *dws); int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer); bool (*can_dma)(struct spi_master *master, struct spi_device *spi, struct spi_transfer *xfer); int (*dma_transfer)(struct dw_spi *dws, struct spi_transfer *xfer); void (*dma_stop)(struct dw_spi *dws); }; struct dw_spi { struct spi_master *master; enum dw_ssi_type type; void __iomem *regs; struct clk *clk; unsigned long paddr; int irq; u32 fifo_len; /* depth of the FIFO buffer */ u32 max_freq; /* max bus freq supported */ u32 reg_io_width; /* DR I/O width in bytes */ u16 bus_num; u16 num_cs; /* supported slave numbers */ /* Current message transfer state info */ size_t len; void *tx; void *tx_end; void *rx; void *rx_end; int dma_mapped; u8 n_bytes; /* current is a 1/2 bytes op */ u32 dma_width; irqreturn_t (*transfer_handler)(struct dw_spi *dws); u32 current_freq; /* frequency in hz */ /* DMA info */ int dma_inited; struct dma_chan *txchan; struct dma_chan *rxchan; unsigned long dma_chan_busy; dma_addr_t dma_addr; /* phy address of the Data register */ const struct dw_spi_dma_ops *dma_ops; void *dma_tx; void *dma_rx; /* Bus interface info */ void *priv; #ifdef CONFIG_DEBUG_FS struct dentry *debugfs; #endif }; /* Slave spi_dev related */ struct chip_data { u8 cs; /* chip select pin */ u8 tmode; /* TR/TO/RO/EEPROM */ u8 type; /* SPI/SSP/MicroWire */ u8 poll_mode; /* 1 means use poll mode */ u8 enable_dma; u16 clk_div; /* baud rate divider */ u32 speed_hz; /* baud rate */ void (*cs_control)(u32 command); }; /* * Each SPI slave device to work with dw_api controller should * has such a structure claiming its working mode (poll or PIO/DMA), * which can be save in the "controller_data" member of the * struct spi_device. */ struct dw_spi_chip { u8 poll_mode; /* 1 for controller polling mode */ u8 type; /* SPI/SSP/MicroWire */ void (*cs_control)(u32 command); }; static inline u32 dw_readl(struct dw_spi *dws, u32 offset) { return __raw_readl(dws->regs + offset); } static inline u16 dw_readw(struct dw_spi *dws, u32 offset) { return __raw_readw(dws->regs + offset); } static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val) { __raw_writel(val, dws->regs + offset); } static inline void dw_writew(struct dw_spi *dws, u32 offset, u16 val) { __raw_writew(val, dws->regs + offset); } static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset) { switch (dws->reg_io_width) { case 2: return dw_readw(dws, offset); case 4: default: return dw_readl(dws, offset); } } static inline void dw_write_io_reg(struct dw_spi *dws, u32 offset, u32 val) { switch (dws->reg_io_width) { case 2: dw_writew(dws, offset, val); break; case 4: default: dw_writel(dws, offset, val); break; } } static inline void spi_enable_chip(struct dw_spi *dws, int enable) { dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0)); } static inline void spi_set_clk(struct dw_spi *dws, u16 div) { dw_writel(dws, DW_SPI_BAUDR, div); } /* Disable IRQ bits */ static inline void spi_mask_intr(struct dw_spi *dws, u32 mask) { u32 new_mask; new_mask = dw_readl(dws, DW_SPI_IMR) & ~mask; dw_writel(dws, DW_SPI_IMR, new_mask); } /* Enable IRQ bits */ static inline void spi_umask_intr(struct dw_spi *dws, u32 mask) { u32 new_mask; new_mask = dw_readl(dws, DW_SPI_IMR) | mask; dw_writel(dws, DW_SPI_IMR, new_mask); } /* * This does disable the SPI controller, interrupts, and re-enable the * controller back. Transmit and receive FIFO buffers are cleared when the * device is disabled. */ static inline void spi_reset_chip(struct dw_spi *dws) { spi_enable_chip(dws, 0); spi_mask_intr(dws, 0xff); spi_enable_chip(dws, 1); } static inline void spi_shutdown_chip(struct dw_spi *dws) { spi_enable_chip(dws, 0); spi_set_clk(dws, 0); } static void dw_spi_set_cs(struct spi_device *spi, bool enable) { struct dw_spi *dws = spi_master_get_devdata(spi->master); struct chip_data *chip = spi_get_ctldata(spi); /* Chip select logic is inverted from spi_set_cs() */ if (chip && chip->cs_control) chip->cs_control(!enable); if (!enable) dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select)); } /* Return the max entries we can fill into tx fifo */ static inline u32 tx_max(struct dw_spi *dws) { u32 tx_left, tx_room, rxtx_gap; tx_left = (dws->tx_end - dws->tx) / dws->n_bytes; tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR); /* * Another concern is about the tx/rx mismatch, we * though to use (dws->fifo_len - rxflr - txflr) as * one maximum value for tx, but it doesn't cover the * data which is out of tx/rx fifo and inside the * shift registers. So a control from sw point of * view is taken. */ rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx)) / dws->n_bytes; return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap)); } /* Return the max entries we should read out of rx fifo */ static inline u32 rx_max(struct dw_spi *dws) { u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes; return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR)); } static void dw_writer(struct dw_spi *dws) { u32 max = tx_max(dws); u32 txw = 0; while (max--) { /* Set the tx word if the transfer's original "tx" is not null */ if (dws->tx_end - dws->len) { if (dws->n_bytes == 1) txw = *(u8 *)(dws->tx); else if (dws->n_bytes == 2) txw = *(u16 *)(dws->tx); else txw = *(u32 *)(dws->tx); } dw_write_io_reg(dws, DW_SPI_DR, txw); dws->tx += dws->n_bytes; } } static void dw_reader(struct dw_spi *dws) { u32 max = rx_max(dws); u32 rxw; while (max--) { rxw = dw_read_io_reg(dws, DW_SPI_DR); /* Care rx only if the transfer's original "rx" is not null */ if (dws->rx_end - dws->len) { if (dws->n_bytes == 1) *(u8 *)(dws->rx) = rxw; else if (dws->n_bytes == 2) *(u16 *)(dws->rx) = rxw; else *(u32 *)(dws->rx) = rxw; } dws->rx += dws->n_bytes; } } static void int_error_stop(struct dw_spi *dws, const char *msg) { spi_reset_chip(dws); dev_err(&dws->master->dev, "%s\n", msg); dws->master->cur_msg->status = -EIO; spi_finalize_current_transfer(dws->master); } static irqreturn_t interrupt_transfer(struct dw_spi *dws) { u16 irq_status = dw_readl(dws, DW_SPI_ISR); /* Error handling */ if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) { dw_readl(dws, DW_SPI_ICR); int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun"); return IRQ_HANDLED; } dw_reader(dws); if (dws->rx_end == dws->rx) { spi_mask_intr(dws, SPI_INT_TXEI); spi_finalize_current_transfer(dws->master); return IRQ_HANDLED; } if (irq_status & SPI_INT_TXEI) { spi_mask_intr(dws, SPI_INT_TXEI); dw_writer(dws); /* Enable TX irq always, it will be disabled when RX finished */ spi_umask_intr(dws, SPI_INT_TXEI); } return IRQ_HANDLED; } static irqreturn_t dw_spi_irq(int irq, void *dev_id) { struct spi_master *master = dev_id; struct dw_spi *dws = spi_master_get_devdata(master); u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f; if (!irq_status) return IRQ_NONE; if (!master->cur_msg) { spi_mask_intr(dws, SPI_INT_TXEI); return IRQ_HANDLED; } return dws->transfer_handler(dws); } /* Must be called inside pump_transfers() */ static int poll_transfer(struct dw_spi *dws) { do { dw_writer(dws); dw_reader(dws); cpu_relax(); } while (dws->rx_end > dws->rx); return 0; } static int dw_spi_transfer_one(struct spi_master *master, struct spi_device *spi, struct spi_transfer *transfer) { struct dw_spi *dws = spi_master_get_devdata(master); struct chip_data *chip = spi_get_ctldata(spi); u8 imask = 0; u16 txlevel = 0; u32 cr0; u32 bits_per_word = 0; int ret; dws->dma_mapped = 0; dws->tx = (void *)transfer->tx_buf; dws->tx_end = dws->tx + transfer->len; dws->rx = transfer->rx_buf; dws->rx_end = dws->rx + transfer->len; dws->len = transfer->len; spi_enable_chip(dws, 0); /* Handle per transfer options for bpw and speed */ if (transfer->speed_hz != dws->current_freq) { if (transfer->speed_hz != chip->speed_hz) { /* clk_div doesn't support odd number */ chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe; chip->speed_hz = transfer->speed_hz; } dws->current_freq = transfer->speed_hz; spi_set_clk(dws, chip->clk_div); } if (transfer->len & 1) bits_per_word = 8; else if (transfer->len & 3) bits_per_word = 16; else bits_per_word = 32; //printk("len=%d, bits_per_word=%d.\n", transfer->len, bits_per_word); if (bits_per_word == 8) { dws->n_bytes = 1; dws->dma_width = 1; } else if (bits_per_word == 16) { dws->n_bytes = 2; dws->dma_width = 2; } else if (bits_per_word == 32) { dws->n_bytes = 4; dws->dma_width = 4; } else { return -EINVAL; } /* Default SPI mode is SCPOL = 0, SCPH = 0 */ cr0 = ((bits_per_word - 1) << SPI_DFS_OFFSET) | (chip->type << SPI_FRF_OFFSET) | (spi->mode << SPI_MODE_OFFSET) | (chip->tmode << SPI_TMOD_OFFSET); /* * Adjust transfer mode if necessary. Requires platform dependent * chipselect mechanism. */ if (chip->cs_control) { if (dws->rx && dws->tx) chip->tmode = SPI_TMOD_TR; else if (dws->rx) chip->tmode = SPI_TMOD_RO; else chip->tmode = SPI_TMOD_TO; cr0 &= ~SPI_TMOD_MASK; cr0 |= (chip->tmode << SPI_TMOD_OFFSET); } dw_writel(dws, DW_SPI_CTRL0, cr0); /* Check if current transfer is a DMA transaction */ if (master->can_dma && master->can_dma(master, spi, transfer)) dws->dma_mapped = master->cur_msg_mapped; /* For poll mode just disable all interrupts */ spi_mask_intr(dws, 0xff); /* * Interrupt mode * we only need set the TXEI IRQ, as TX/RX always happen syncronizely */ if (dws->dma_mapped) { ret = dws->dma_ops->dma_setup(dws, transfer); if (ret < 0) { spi_enable_chip(dws, 1); return ret; } } else if (!chip->poll_mode) { txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes); dw_writel(dws, DW_SPI_TXFLTR, txlevel); /* Set the interrupt mask */ imask |= SPI_INT_TXEI | SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI; spi_umask_intr(dws, imask); dws->transfer_handler = interrupt_transfer; } spi_enable_chip(dws, 1); if (dws->dma_mapped) { ret = dws->dma_ops->dma_transfer(dws, transfer); if (ret < 0) return ret; } if (chip->poll_mode) { return poll_transfer(dws); } return 1; } static void dw_spi_handle_err(struct spi_master *master, struct spi_message *msg) { struct dw_spi *dws = spi_master_get_devdata(master); if (dws->dma_mapped) dws->dma_ops->dma_stop(dws); spi_reset_chip(dws); } /* This may be called twice for each spi dev */ static int dw_spi_setup(struct spi_device *spi) { struct dw_spi_chip *chip_info = NULL; struct chip_data *chip; int ret; /* Only alloc on first setup */ chip = spi_get_ctldata(spi); if (!chip) { chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); if (!chip) return -ENOMEM; spi_set_ctldata(spi, chip); } /* * Protocol drivers may change the chip settings, so... * if chip_info exists, use it */ chip_info = spi->controller_data; /* chip_info doesn't always exist */ if (chip_info) { if (chip_info->cs_control) chip->cs_control = chip_info->cs_control; chip->poll_mode = chip_info->poll_mode; chip->type = chip_info->type; } chip->tmode = SPI_TMOD_TR; if (gpio_is_valid(spi->cs_gpio)) { ret = gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); if (ret) return ret; } return 0; } static void dw_spi_cleanup(struct spi_device *spi) { struct chip_data *chip = spi_get_ctldata(spi); kfree(chip); spi_set_ctldata(spi, NULL); } /* Restart the controller, disable all interrupts, clean rx fifo */ static void spi_hw_init(struct device *dev, struct dw_spi *dws) { spi_reset_chip(dws); /* * Try to detect the FIFO depth if not set by interface driver, * the depth could be from 2 to 256 from HW spec */ if (!dws->fifo_len) { u32 fifo; for (fifo = 1; fifo < 256; fifo++) { dw_writel(dws, DW_SPI_TXFLTR, fifo); if (fifo != dw_readl(dws, DW_SPI_TXFLTR)) break; } dw_writel(dws, DW_SPI_TXFLTR, 0); dws->fifo_len = (fifo == 1) ? 0 : fifo; dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len); } } static int dw_spi_add_host(struct device *dev, struct dw_spi *dws) { struct spi_master *master; int ret; BUG_ON(dws == NULL); master = spi_alloc_master(dev, 0); if (!master) return -ENOMEM; dws->master = master; dws->type = SSI_MOTO_SPI; dws->dma_inited = 0; dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR); ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev), master); if (ret < 0) { dev_err(dev, "can not get IRQ\n"); goto err_free_master; } master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH | SPI_NO_CS; master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); master->bus_num = dws->bus_num; master->num_chipselect = dws->num_cs; master->setup = dw_spi_setup; master->cleanup = dw_spi_cleanup; master->set_cs = dw_spi_set_cs; master->transfer_one = dw_spi_transfer_one; master->handle_err = dw_spi_handle_err; master->max_speed_hz = dws->max_freq; master->dev.of_node = dev->of_node; master->flags = SPI_MASTER_GPIO_SS; /* Basic HW init */ spi_hw_init(dev, dws); if (dws->dma_ops && dws->dma_ops->dma_init) { ret = dws->dma_ops->dma_init(dws); if (ret) { dev_warn(dev, "DMA init failed\n"); dws->dma_inited = 0; } else { master->can_dma = dws->dma_ops->can_dma; } } spi_master_set_devdata(master, dws); ret = devm_spi_register_master(dev, master); if (ret) { dev_err(&master->dev, "problem registering spi master\n"); goto err_dma_exit; } return 0; err_dma_exit: if (dws->dma_ops && dws->dma_ops->dma_exit) dws->dma_ops->dma_exit(dws); spi_enable_chip(dws, 0); free_irq(dws->irq, master); err_free_master: spi_master_put(master); return ret; } static void dw_spi_remove_host(struct dw_spi *dws) { if (dws->dma_ops && dws->dma_ops->dma_exit) dws->dma_ops->dma_exit(dws); spi_shutdown_chip(dws); free_irq(dws->irq, dws->master); } /* static int dw_spi_suspend_host(struct dw_spi *dws) { int ret; ret = spi_master_suspend(dws->master); if (ret) return ret; spi_shutdown_chip(dws); return 0; } static int dw_spi_resume_host(struct dw_spi *dws) { int ret; spi_hw_init(&dws->master->dev, dws); ret = spi_master_resume(dws->master); if (ret) dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret); return ret; } */ static int ark_dw_spi_probe(struct platform_device *pdev) { struct dw_spi *dws; struct resource *mem; int ret; int num_cs; dws = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi), GFP_KERNEL); if (!dws) return -ENOMEM; /* Get basic io resource and map it */ mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); dws->regs = devm_ioremap_resource(&pdev->dev, mem); if (IS_ERR(dws->regs)) { dev_err(&pdev->dev, "SPI region map failed\n"); return PTR_ERR(dws->regs); } dws->irq = platform_get_irq(pdev, 0); if (dws->irq < 0) { dev_err(&pdev->dev, "no irq resource?\n"); return dws->irq; /* -ENXIO */ } dws->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(dws->clk)) return PTR_ERR(dws->clk); ret = clk_prepare_enable(dws->clk); if (ret) return ret; dws->bus_num = pdev->id; dws->max_freq = clk_get_rate(dws->clk); device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width); num_cs = 4; device_property_read_u32(&pdev->dev, "num-cs", &num_cs); dws->num_cs = num_cs; if (pdev->dev.of_node) { int i; for (i = 0; i < dws->num_cs; i++) { int cs_gpio = of_get_named_gpio(pdev->dev.of_node, "cs-gpios", i); if (cs_gpio == -EPROBE_DEFER) { ret = cs_gpio; goto out; } if (gpio_is_valid(cs_gpio)) { ret = devm_gpio_request(&pdev->dev, cs_gpio, dev_name(&pdev->dev)); if (ret) goto out; } } } ret = dw_spi_add_host(&pdev->dev, dws); if (ret) goto out; platform_set_drvdata(pdev, dws); return 0; out: clk_disable_unprepare(dws->clk); return ret; } static int ark_dw_spi_remove(struct platform_device *pdev) { struct dw_spi *dws = platform_get_drvdata(pdev); dw_spi_remove_host(dws); clk_disable_unprepare(dws->clk); return 0; } static const struct of_device_id ark_dw_spi_of_match[] = { { .compatible = "arkmicro,ark-dw-ssi", }, { /* end of table */} }; MODULE_DEVICE_TABLE(of, ark_dw_spi_of_match); static struct platform_driver ark_dw_spi_driver = { .probe = ark_dw_spi_probe, .remove = ark_dw_spi_remove, .driver = { .name = DRIVER_NAME, .of_match_table = ark_dw_spi_of_match, }, }; module_platform_driver(ark_dw_spi_driver); MODULE_AUTHOR("Sim"); MODULE_DESCRIPTION("Arkmicro new dw spi controller driver"); MODULE_LICENSE("GPL v2");