Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 2 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 3 | * Designware SPI core controller driver (refer pxa2xx_spi.c) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2009, Intel Corporation. |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/dma-mapping.h> |
| 9 | #include <linux/interrupt.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 10 | #include <linux/module.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 11 | #include <linux/highmem.h> |
| 12 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 14 | #include <linux/spi/spi.h> |
Lars Povlsen | bac70b5 | 2020-08-24 22:30:05 +0200 | [diff] [blame] | 15 | #include <linux/of.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 16 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 17 | #include "spi-dw.h" |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 18 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 19 | #ifdef CONFIG_DEBUG_FS |
| 20 | #include <linux/debugfs.h> |
| 21 | #endif |
| 22 | |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 23 | /* Slave spi_device related */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 24 | struct chip_data { |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 25 | u32 cr0; |
Lars Povlsen | bac70b5 | 2020-08-24 22:30:05 +0200 | [diff] [blame] | 26 | u32 rx_sample_dly; /* RX sample delay */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | #ifdef CONFIG_DEBUG_FS |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 30 | |
Serge Semin | 8378449 | 2020-05-29 16:12:04 +0300 | [diff] [blame] | 31 | #define DW_SPI_DBGFS_REG(_name, _off) \ |
| 32 | { \ |
| 33 | .name = _name, \ |
| 34 | .offset = _off, \ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Serge Semin | 8378449 | 2020-05-29 16:12:04 +0300 | [diff] [blame] | 37 | static const struct debugfs_reg32 dw_spi_dbgfs_regs[] = { |
| 38 | DW_SPI_DBGFS_REG("CTRLR0", DW_SPI_CTRLR0), |
| 39 | DW_SPI_DBGFS_REG("CTRLR1", DW_SPI_CTRLR1), |
| 40 | DW_SPI_DBGFS_REG("SSIENR", DW_SPI_SSIENR), |
| 41 | DW_SPI_DBGFS_REG("SER", DW_SPI_SER), |
| 42 | DW_SPI_DBGFS_REG("BAUDR", DW_SPI_BAUDR), |
| 43 | DW_SPI_DBGFS_REG("TXFTLR", DW_SPI_TXFTLR), |
| 44 | DW_SPI_DBGFS_REG("RXFTLR", DW_SPI_RXFTLR), |
| 45 | DW_SPI_DBGFS_REG("TXFLR", DW_SPI_TXFLR), |
| 46 | DW_SPI_DBGFS_REG("RXFLR", DW_SPI_RXFLR), |
| 47 | DW_SPI_DBGFS_REG("SR", DW_SPI_SR), |
| 48 | DW_SPI_DBGFS_REG("IMR", DW_SPI_IMR), |
| 49 | DW_SPI_DBGFS_REG("ISR", DW_SPI_ISR), |
| 50 | DW_SPI_DBGFS_REG("DMACR", DW_SPI_DMACR), |
| 51 | DW_SPI_DBGFS_REG("DMATDLR", DW_SPI_DMATDLR), |
| 52 | DW_SPI_DBGFS_REG("DMARDLR", DW_SPI_DMARDLR), |
Lars Povlsen | bac70b5 | 2020-08-24 22:30:05 +0200 | [diff] [blame] | 53 | DW_SPI_DBGFS_REG("RX_SAMPLE_DLY", DW_SPI_RX_SAMPLE_DLY), |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 56 | static int dw_spi_debugfs_init(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 57 | { |
Phil Reid | e70002c80 | 2017-01-06 17:35:13 +0800 | [diff] [blame] | 58 | char name[32]; |
Phil Reid | 13288bd | 2016-12-22 17:18:12 +0800 | [diff] [blame] | 59 | |
Phil Reid | e70002c80 | 2017-01-06 17:35:13 +0800 | [diff] [blame] | 60 | snprintf(name, 32, "dw_spi%d", dws->master->bus_num); |
Phil Reid | 13288bd | 2016-12-22 17:18:12 +0800 | [diff] [blame] | 61 | dws->debugfs = debugfs_create_dir(name, NULL); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 62 | if (!dws->debugfs) |
| 63 | return -ENOMEM; |
| 64 | |
Serge Semin | 8378449 | 2020-05-29 16:12:04 +0300 | [diff] [blame] | 65 | dws->regset.regs = dw_spi_dbgfs_regs; |
| 66 | dws->regset.nregs = ARRAY_SIZE(dw_spi_dbgfs_regs); |
| 67 | dws->regset.base = dws->regs; |
| 68 | debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset); |
| 69 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 73 | static void dw_spi_debugfs_remove(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 74 | { |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 75 | debugfs_remove_recursive(dws->debugfs); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #else |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 79 | static inline int dw_spi_debugfs_init(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 80 | { |
George Shore | 20a588f | 2010-01-21 11:40:49 +0000 | [diff] [blame] | 81 | return 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 84 | static inline void dw_spi_debugfs_remove(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 85 | { |
| 86 | } |
| 87 | #endif /* CONFIG_DEBUG_FS */ |
| 88 | |
Alexandre Belloni | c79bdbb | 2018-07-27 21:53:54 +0200 | [diff] [blame] | 89 | void dw_spi_set_cs(struct spi_device *spi, bool enable) |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 90 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 91 | struct dw_spi *dws = spi_controller_get_devdata(spi->controller); |
Serge Semin | 9aea644 | 2020-05-15 13:47:43 +0300 | [diff] [blame] | 92 | bool cs_high = !!(spi->mode & SPI_CS_HIGH); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 93 | |
Serge Semin | 9aea644 | 2020-05-15 13:47:43 +0300 | [diff] [blame] | 94 | /* |
| 95 | * DW SPI controller demands any native CS being set in order to |
| 96 | * proceed with data transfer. So in order to activate the SPI |
| 97 | * communications we must set a corresponding bit in the Slave |
| 98 | * Enable register no matter whether the SPI core is configured to |
| 99 | * support active-high or active-low CS level. |
| 100 | */ |
| 101 | if (cs_high == enable) |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 102 | dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select)); |
Serge Semin | cc760f3 | 2020-09-20 14:28:53 +0300 | [diff] [blame] | 103 | else if (dws->caps & DW_SPI_CAP_CS_OVERRIDE) |
Talel Shenhar | f2d7047 | 2018-10-11 14:20:07 +0300 | [diff] [blame] | 104 | dw_writel(dws, DW_SPI_SER, 0); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 105 | } |
Alexandre Belloni | c79bdbb | 2018-07-27 21:53:54 +0200 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(dw_spi_set_cs); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 107 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 108 | /* Return the max entries we can fill into tx fifo */ |
| 109 | static inline u32 tx_max(struct dw_spi *dws) |
| 110 | { |
| 111 | u32 tx_left, tx_room, rxtx_gap; |
| 112 | |
| 113 | tx_left = (dws->tx_end - dws->tx) / dws->n_bytes; |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 114 | tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Another concern is about the tx/rx mismatch, we |
| 118 | * though to use (dws->fifo_len - rxflr - txflr) as |
| 119 | * one maximum value for tx, but it doesn't cover the |
| 120 | * data which is out of tx/rx fifo and inside the |
| 121 | * shift registers. So a control from sw point of |
| 122 | * view is taken. |
| 123 | */ |
| 124 | rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx)) |
| 125 | / dws->n_bytes; |
| 126 | |
| 127 | return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap)); |
| 128 | } |
| 129 | |
| 130 | /* Return the max entries we should read out of rx fifo */ |
| 131 | static inline u32 rx_max(struct dw_spi *dws) |
| 132 | { |
| 133 | u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes; |
| 134 | |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 135 | return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR)); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 138 | static void dw_writer(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 139 | { |
Serge Semin | 0b6bfad | 2020-09-20 14:28:48 +0300 | [diff] [blame] | 140 | u32 max = tx_max(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 141 | u16 txw = 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 142 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 143 | while (max--) { |
| 144 | /* Set the tx word if the transfer's original "tx" is not null */ |
| 145 | if (dws->tx_end - dws->len) { |
| 146 | if (dws->n_bytes == 1) |
| 147 | txw = *(u8 *)(dws->tx); |
| 148 | else |
| 149 | txw = *(u16 *)(dws->tx); |
| 150 | } |
Michael van der Westhuizen | c4fe57f | 2015-08-18 22:21:53 +0200 | [diff] [blame] | 151 | dw_write_io_reg(dws, DW_SPI_DR, txw); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 152 | dws->tx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 153 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 156 | static void dw_reader(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 157 | { |
Serge Semin | 0b6bfad | 2020-09-20 14:28:48 +0300 | [diff] [blame] | 158 | u32 max = rx_max(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 159 | u16 rxw; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 160 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 161 | while (max--) { |
Michael van der Westhuizen | c4fe57f | 2015-08-18 22:21:53 +0200 | [diff] [blame] | 162 | rxw = dw_read_io_reg(dws, DW_SPI_DR); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 163 | /* Care rx only if the transfer's original "rx" is not null */ |
| 164 | if (dws->rx_end - dws->len) { |
| 165 | if (dws->n_bytes == 1) |
| 166 | *(u8 *)(dws->rx) = rxw; |
| 167 | else |
| 168 | *(u16 *)(dws->rx) = rxw; |
| 169 | } |
| 170 | dws->rx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 171 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 174 | static void int_error_stop(struct dw_spi *dws, const char *msg) |
| 175 | { |
Andy Shevchenko | 45746e8 | 2015-03-02 14:58:55 +0200 | [diff] [blame] | 176 | spi_reset_chip(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 177 | |
| 178 | dev_err(&dws->master->dev, "%s\n", msg); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 179 | dws->master->cur_msg->status = -EIO; |
| 180 | spi_finalize_current_transfer(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 183 | static irqreturn_t interrupt_transfer(struct dw_spi *dws) |
| 184 | { |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 185 | u16 irq_status = dw_readl(dws, DW_SPI_ISR); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 186 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 187 | /* Error handling */ |
| 188 | if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) { |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 189 | dw_readl(dws, DW_SPI_ICR); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 190 | int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 191 | return IRQ_HANDLED; |
| 192 | } |
| 193 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 194 | dw_reader(dws); |
| 195 | if (dws->rx_end == dws->rx) { |
Serge Semin | a1d5aa6 | 2020-09-20 14:28:50 +0300 | [diff] [blame] | 196 | spi_mask_intr(dws, 0xff); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 197 | spi_finalize_current_transfer(dws->master); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 198 | return IRQ_HANDLED; |
| 199 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 200 | if (irq_status & SPI_INT_TXEI) { |
| 201 | spi_mask_intr(dws, SPI_INT_TXEI); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 202 | dw_writer(dws); |
| 203 | /* Enable TX irq always, it will be disabled when RX finished */ |
| 204 | spi_umask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 205 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 206 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 207 | return IRQ_HANDLED; |
| 208 | } |
| 209 | |
| 210 | static irqreturn_t dw_spi_irq(int irq, void *dev_id) |
| 211 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 212 | struct spi_controller *master = dev_id; |
| 213 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 214 | u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f; |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 215 | |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 216 | if (!irq_status) |
| 217 | return IRQ_NONE; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 218 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 219 | if (!master->cur_msg) { |
Serge Semin | a1d5aa6 | 2020-09-20 14:28:50 +0300 | [diff] [blame] | 220 | spi_mask_intr(dws, 0xff); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 221 | return IRQ_HANDLED; |
| 222 | } |
| 223 | |
| 224 | return dws->transfer_handler(dws); |
| 225 | } |
| 226 | |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 227 | static u32 dw_spi_prepare_cr0(struct dw_spi *dws, struct spi_device *spi) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 228 | { |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 229 | u32 cr0 = 0; |
Wan Ahmad Zainie | e539f43 | 2020-05-05 21:06:14 +0800 | [diff] [blame] | 230 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 231 | if (!(dws->caps & DW_SPI_CAP_DWC_SSI)) { |
| 232 | /* CTRLR0[ 5: 4] Frame Format */ |
| 233 | cr0 |= SSI_MOTO_SPI << SPI_FRF_OFFSET; |
Wan Ahmad Zainie | e539f43 | 2020-05-05 21:06:14 +0800 | [diff] [blame] | 234 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 235 | /* |
| 236 | * SPI mode (SCPOL|SCPH) |
| 237 | * CTRLR0[ 6] Serial Clock Phase |
| 238 | * CTRLR0[ 7] Serial Clock Polarity |
| 239 | */ |
| 240 | cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET; |
| 241 | cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET; |
Wan Ahmad Zainie | e539f43 | 2020-05-05 21:06:14 +0800 | [diff] [blame] | 242 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 243 | /* CTRLR0[11] Shift Register Loop */ |
| 244 | cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET; |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 245 | } else { |
| 246 | /* CTRLR0[ 7: 6] Frame Format */ |
| 247 | cr0 |= SSI_MOTO_SPI << DWC_SSI_CTRLR0_FRF_OFFSET; |
Wan Ahmad Zainie | e539f43 | 2020-05-05 21:06:14 +0800 | [diff] [blame] | 248 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 249 | /* |
| 250 | * SPI mode (SCPOL|SCPH) |
| 251 | * CTRLR0[ 8] Serial Clock Phase |
| 252 | * CTRLR0[ 9] Serial Clock Polarity |
| 253 | */ |
| 254 | cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << DWC_SSI_CTRLR0_SCPOL_OFFSET; |
| 255 | cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << DWC_SSI_CTRLR0_SCPH_OFFSET; |
Serge Semin | ffb7ca5 | 2020-09-20 14:28:54 +0300 | [diff] [blame] | 256 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 257 | /* CTRLR0[13] Shift Register Loop */ |
| 258 | cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << DWC_SSI_CTRLR0_SRL_OFFSET; |
| 259 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 260 | if (dws->caps & DW_SPI_CAP_KEEMBAY_MST) |
| 261 | cr0 |= DWC_SSI_CTRLR0_KEEMBAY_MST; |
| 262 | } |
| 263 | |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 264 | return cr0; |
| 265 | } |
| 266 | |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 267 | void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi, |
| 268 | struct dw_spi_cfg *cfg) |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 269 | { |
| 270 | struct chip_data *chip = spi_get_ctldata(spi); |
| 271 | u32 cr0 = chip->cr0; |
Serge Semin | c449ad7 | 2020-10-08 02:54:54 +0300 | [diff] [blame] | 272 | u32 speed_hz; |
| 273 | u16 clk_div; |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 274 | |
| 275 | /* CTRLR0[ 4/3: 0] Data Frame Size */ |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 276 | cr0 |= (cfg->dfs - 1); |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 277 | |
| 278 | if (!(dws->caps & DW_SPI_CAP_DWC_SSI)) |
| 279 | /* CTRLR0[ 9:8] Transfer Mode */ |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 280 | cr0 |= cfg->tmode << SPI_TMOD_OFFSET; |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 281 | else |
| 282 | /* CTRLR0[11:10] Transfer Mode */ |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 283 | cr0 |= cfg->tmode << DWC_SSI_CTRLR0_TMOD_OFFSET; |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 284 | |
Serge Semin | d6bbd11 | 2020-10-08 02:54:51 +0300 | [diff] [blame] | 285 | dw_writel(dws, DW_SPI_CTRLR0, cr0); |
Serge Semin | f76f314 | 2020-10-08 02:54:53 +0300 | [diff] [blame] | 286 | |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 287 | if (cfg->tmode == SPI_TMOD_EPROMREAD || cfg->tmode == SPI_TMOD_RO) |
| 288 | dw_writel(dws, DW_SPI_CTRLR1, cfg->ndf ? cfg->ndf - 1 : 0); |
| 289 | |
Serge Semin | c449ad7 | 2020-10-08 02:54:54 +0300 | [diff] [blame] | 290 | /* Note DW APB SSI clock divider doesn't support odd numbers */ |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 291 | clk_div = (DIV_ROUND_UP(dws->max_freq, cfg->freq) + 1) & 0xfffe; |
Serge Semin | c449ad7 | 2020-10-08 02:54:54 +0300 | [diff] [blame] | 292 | speed_hz = dws->max_freq / clk_div; |
| 293 | |
| 294 | if (dws->current_freq != speed_hz) { |
| 295 | spi_set_clk(dws, clk_div); |
| 296 | dws->current_freq = speed_hz; |
Serge Semin | f76f314 | 2020-10-08 02:54:53 +0300 | [diff] [blame] | 297 | } |
Serge Semin | 2613d2b | 2020-10-08 02:54:55 +0300 | [diff] [blame] | 298 | |
| 299 | /* Update RX sample delay if required */ |
| 300 | if (dws->cur_rx_sample_dly != chip->rx_sample_dly) { |
| 301 | dw_writel(dws, DW_SPI_RX_SAMPLE_DLY, chip->rx_sample_dly); |
| 302 | dws->cur_rx_sample_dly = chip->rx_sample_dly; |
| 303 | } |
Wan Ahmad Zainie | e539f43 | 2020-05-05 21:06:14 +0800 | [diff] [blame] | 304 | } |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 305 | EXPORT_SYMBOL_GPL(dw_spi_update_config); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 306 | |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 307 | static int dw_spi_transfer_one(struct spi_controller *master, |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 308 | struct spi_device *spi, struct spi_transfer *transfer) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 309 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 310 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 311 | struct dw_spi_cfg cfg = { |
| 312 | .tmode = SPI_TMOD_TR, |
| 313 | .dfs = transfer->bits_per_word, |
| 314 | .freq = transfer->speed_hz, |
| 315 | }; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 316 | u8 imask = 0; |
Andy Shevchenko | ea11370 | 2015-02-24 13:32:11 +0200 | [diff] [blame] | 317 | u16 txlevel = 0; |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 318 | int ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 319 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 320 | dws->dma_mapped = 0; |
Serge Semin | 8225c1c | 2020-09-20 14:28:47 +0300 | [diff] [blame] | 321 | dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 322 | dws->tx = (void *)transfer->tx_buf; |
| 323 | dws->tx_end = dws->tx + transfer->len; |
| 324 | dws->rx = transfer->rx_buf; |
| 325 | dws->rx_end = dws->rx + transfer->len; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 326 | dws->len = transfer->len; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 327 | |
Xinwei Kong | bfda044 | 2020-01-03 10:52:10 +0800 | [diff] [blame] | 328 | /* Ensure dw->rx and dw->rx_end are visible */ |
| 329 | smp_mb(); |
| 330 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 331 | spi_enable_chip(dws, 0); |
| 332 | |
Serge Semin | 3ff60c6 | 2020-10-08 02:54:56 +0300 | [diff] [blame^] | 333 | dw_spi_update_config(dws, spi, &cfg); |
Simon Goldschmidt | af060b3 | 2018-09-04 21:49:44 +0200 | [diff] [blame] | 334 | |
Serge Semin | c449ad7 | 2020-10-08 02:54:54 +0300 | [diff] [blame] | 335 | transfer->effective_speed_hz = dws->current_freq; |
Simon Goldschmidt | af060b3 | 2018-09-04 21:49:44 +0200 | [diff] [blame] | 336 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 337 | /* Check if current transfer is a DMA transaction */ |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 338 | if (master->can_dma && master->can_dma(master, spi, transfer)) |
| 339 | dws->dma_mapped = master->cur_msg_mapped; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 340 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 341 | /* For poll mode just disable all interrupts */ |
| 342 | spi_mask_intr(dws, 0xff); |
| 343 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 344 | /* |
| 345 | * Interrupt mode |
| 346 | * we only need set the TXEI IRQ, as TX/RX always happen syncronizely |
| 347 | */ |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 348 | if (dws->dma_mapped) { |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 349 | ret = dws->dma_ops->dma_setup(dws, transfer); |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 350 | if (ret < 0) { |
| 351 | spi_enable_chip(dws, 1); |
| 352 | return ret; |
| 353 | } |
Clement Leger | 33e8fd4 | 2020-04-16 13:09:16 +0200 | [diff] [blame] | 354 | } else { |
Andy Shevchenko | ea11370 | 2015-02-24 13:32:11 +0200 | [diff] [blame] | 355 | txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes); |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 356 | dw_writel(dws, DW_SPI_TXFTLR, txlevel); |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 357 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 358 | /* Set the interrupt mask */ |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 359 | imask |= SPI_INT_TXEI | SPI_INT_TXOI | |
| 360 | SPI_INT_RXUI | SPI_INT_RXOI; |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 361 | spi_umask_intr(dws, imask); |
| 362 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 363 | dws->transfer_handler = interrupt_transfer; |
| 364 | } |
| 365 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 366 | spi_enable_chip(dws, 1); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 367 | |
Serge Semin | f0410bb | 2020-05-29 16:11:51 +0300 | [diff] [blame] | 368 | if (dws->dma_mapped) |
| 369 | return dws->dma_ops->dma_transfer(dws, transfer); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 370 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 371 | return 1; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 374 | static void dw_spi_handle_err(struct spi_controller *master, |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 375 | struct spi_message *msg) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 376 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 377 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 378 | |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 379 | if (dws->dma_mapped) |
| 380 | dws->dma_ops->dma_stop(dws); |
| 381 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 382 | spi_reset_chip(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* This may be called twice for each spi dev */ |
| 386 | static int dw_spi_setup(struct spi_device *spi) |
| 387 | { |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 388 | struct dw_spi *dws = spi_controller_get_devdata(spi->controller); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 389 | struct chip_data *chip; |
| 390 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 391 | /* Only alloc on first setup */ |
| 392 | chip = spi_get_ctldata(spi); |
| 393 | if (!chip) { |
Lars Povlsen | bac70b5 | 2020-08-24 22:30:05 +0200 | [diff] [blame] | 394 | struct dw_spi *dws = spi_controller_get_devdata(spi->controller); |
| 395 | u32 rx_sample_dly_ns; |
| 396 | |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 397 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 398 | if (!chip) |
| 399 | return -ENOMEM; |
Baruch Siach | 43f627a | 2013-12-30 20:30:46 +0200 | [diff] [blame] | 400 | spi_set_ctldata(spi, chip); |
Lars Povlsen | bac70b5 | 2020-08-24 22:30:05 +0200 | [diff] [blame] | 401 | /* Get specific / default rx-sample-delay */ |
| 402 | if (device_property_read_u32(&spi->dev, |
| 403 | "rx-sample-delay-ns", |
| 404 | &rx_sample_dly_ns) != 0) |
| 405 | /* Use default controller value */ |
| 406 | rx_sample_dly_ns = dws->def_rx_sample_dly_ns; |
| 407 | chip->rx_sample_dly = DIV_ROUND_CLOSEST(rx_sample_dly_ns, |
| 408 | NSEC_PER_SEC / |
| 409 | dws->max_freq); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Serge Semin | a3577bd | 2020-10-08 02:54:52 +0300 | [diff] [blame] | 412 | /* |
| 413 | * Update CR0 data each time the setup callback is invoked since |
| 414 | * the device parameters could have been changed, for instance, by |
| 415 | * the MMC SPI driver or something else. |
| 416 | */ |
| 417 | chip->cr0 = dw_spi_prepare_cr0(dws, spi); |
| 418 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 422 | static void dw_spi_cleanup(struct spi_device *spi) |
| 423 | { |
| 424 | struct chip_data *chip = spi_get_ctldata(spi); |
| 425 | |
| 426 | kfree(chip); |
| 427 | spi_set_ctldata(spi, NULL); |
| 428 | } |
| 429 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 430 | /* Restart the controller, disable all interrupts, clean rx fifo */ |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 431 | static void spi_hw_init(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 432 | { |
Andy Shevchenko | 45746e8 | 2015-03-02 14:58:55 +0200 | [diff] [blame] | 433 | spi_reset_chip(dws); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * Try to detect the FIFO depth if not set by interface driver, |
| 437 | * the depth could be from 2 to 256 from HW spec |
| 438 | */ |
| 439 | if (!dws->fifo_len) { |
| 440 | u32 fifo; |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 441 | |
Andy Shevchenko | 9d239d3 | 2015-02-25 11:39:36 +0200 | [diff] [blame] | 442 | for (fifo = 1; fifo < 256; fifo++) { |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 443 | dw_writel(dws, DW_SPI_TXFTLR, fifo); |
| 444 | if (fifo != dw_readl(dws, DW_SPI_TXFTLR)) |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 445 | break; |
| 446 | } |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 447 | dw_writel(dws, DW_SPI_TXFTLR, 0); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 448 | |
Andy Shevchenko | 9d239d3 | 2015-02-25 11:39:36 +0200 | [diff] [blame] | 449 | dws->fifo_len = (fifo == 1) ? 0 : fifo; |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 450 | dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 451 | } |
Talel Shenhar | f2d7047 | 2018-10-11 14:20:07 +0300 | [diff] [blame] | 452 | |
| 453 | /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */ |
Serge Semin | cc760f3 | 2020-09-20 14:28:53 +0300 | [diff] [blame] | 454 | if (dws->caps & DW_SPI_CAP_CS_OVERRIDE) |
Talel Shenhar | f2d7047 | 2018-10-11 14:20:07 +0300 | [diff] [blame] | 455 | dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 458 | int dw_spi_add_host(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 459 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 460 | struct spi_controller *master; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 461 | int ret; |
| 462 | |
Aditya Pakki | 169f9ac | 2019-12-05 17:14:21 -0600 | [diff] [blame] | 463 | if (!dws) |
| 464 | return -EINVAL; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 465 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 466 | master = spi_alloc_master(dev, 0); |
| 467 | if (!master) |
| 468 | return -ENOMEM; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 469 | |
| 470 | dws->master = master; |
Andy Shevchenko | d7ef54c | 2015-10-27 17:48:16 +0200 | [diff] [blame] | 471 | dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 472 | |
Alexandre Belloni | 66b19d7 | 2018-07-17 16:23:10 +0200 | [diff] [blame] | 473 | spi_controller_set_devdata(master, dws); |
| 474 | |
Serge Semin | a1d5aa6 | 2020-09-20 14:28:50 +0300 | [diff] [blame] | 475 | /* Basic HW init */ |
| 476 | spi_hw_init(dev, dws); |
| 477 | |
Phil Reid | e70002c80 | 2017-01-06 17:35:13 +0800 | [diff] [blame] | 478 | ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev), |
| 479 | master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 480 | if (ret < 0) { |
Andy Shevchenko | 5f0966e | 2015-10-14 23:12:17 +0300 | [diff] [blame] | 481 | dev_err(dev, "can not get IRQ\n"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 482 | goto err_free_master; |
| 483 | } |
| 484 | |
Linus Walleij | 9400c41 | 2019-01-07 16:51:56 +0100 | [diff] [blame] | 485 | master->use_gpio_descriptors = true; |
Andy Shevchenko | c3ce15b | 2014-09-18 20:08:56 +0300 | [diff] [blame] | 486 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; |
Simon Goldschmidt | af060b3 | 2018-09-04 21:49:44 +0200 | [diff] [blame] | 487 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 488 | master->bus_num = dws->bus_num; |
| 489 | master->num_chipselect = dws->num_cs; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 490 | master->setup = dw_spi_setup; |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 491 | master->cleanup = dw_spi_cleanup; |
Serge Semin | f68fe8d | 2020-10-08 02:54:50 +0300 | [diff] [blame] | 492 | if (dws->set_cs) |
| 493 | master->set_cs = dws->set_cs; |
| 494 | else |
| 495 | master->set_cs = dw_spi_set_cs; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 496 | master->transfer_one = dw_spi_transfer_one; |
| 497 | master->handle_err = dw_spi_handle_err; |
Axel Lin | 765ee70 | 2014-02-20 21:37:56 +0800 | [diff] [blame] | 498 | master->max_speed_hz = dws->max_freq; |
Thor Thayer | 9c6de47 | 2014-10-08 13:51:34 -0500 | [diff] [blame] | 499 | master->dev.of_node = dev->of_node; |
Jay Fang | 32215a6 | 2018-12-03 11:15:50 +0800 | [diff] [blame] | 500 | master->dev.fwnode = dev->fwnode; |
Thor Thayer | 80b444e | 2016-10-10 09:25:25 -0500 | [diff] [blame] | 501 | master->flags = SPI_MASTER_GPIO_SS; |
Phil Edworthy | 1e69598 | 2019-09-18 09:04:35 +0100 | [diff] [blame] | 502 | master->auto_runtime_pm = true; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 503 | |
Lars Povlsen | bac70b5 | 2020-08-24 22:30:05 +0200 | [diff] [blame] | 504 | /* Get default rx sample delay */ |
| 505 | device_property_read_u32(dev, "rx-sample-delay-ns", |
| 506 | &dws->def_rx_sample_dly_ns); |
| 507 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 508 | if (dws->dma_ops && dws->dma_ops->dma_init) { |
Andy Shevchenko | 6370aba | 2020-05-06 18:30:24 +0300 | [diff] [blame] | 509 | ret = dws->dma_ops->dma_init(dev, dws); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 510 | if (ret) { |
Andy Shevchenko | 3dbb3b9 | 2015-01-07 16:56:54 +0200 | [diff] [blame] | 511 | dev_warn(dev, "DMA init failed\n"); |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 512 | } else { |
| 513 | master->can_dma = dws->dma_ops->can_dma; |
Serge Semin | 46164fd | 2020-05-29 16:11:57 +0300 | [diff] [blame] | 514 | master->flags |= SPI_CONTROLLER_MUST_TX; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Lukas Wunner | ca8b19d | 2020-05-25 14:25:01 +0200 | [diff] [blame] | 518 | ret = spi_register_controller(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 519 | if (ret) { |
| 520 | dev_err(&master->dev, "problem registering spi master\n"); |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 521 | goto err_dma_exit; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 524 | dw_spi_debugfs_init(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 525 | return 0; |
| 526 | |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 527 | err_dma_exit: |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 528 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 529 | dws->dma_ops->dma_exit(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 530 | spi_enable_chip(dws, 0); |
Andy Shevchenko | 02f2038 | 2015-10-20 12:11:40 +0300 | [diff] [blame] | 531 | free_irq(dws->irq, master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 532 | err_free_master: |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 533 | spi_controller_put(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 534 | return ret; |
| 535 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 536 | EXPORT_SYMBOL_GPL(dw_spi_add_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 537 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 538 | void dw_spi_remove_host(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 539 | { |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 540 | dw_spi_debugfs_remove(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 541 | |
Lukas Wunner | ca8b19d | 2020-05-25 14:25:01 +0200 | [diff] [blame] | 542 | spi_unregister_controller(dws->master); |
| 543 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 544 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 545 | dws->dma_ops->dma_exit(dws); |
Andy Shevchenko | 1cc3f14 | 2015-10-14 23:12:23 +0300 | [diff] [blame] | 546 | |
| 547 | spi_shutdown_chip(dws); |
Andy Shevchenko | 02f2038 | 2015-10-20 12:11:40 +0300 | [diff] [blame] | 548 | |
| 549 | free_irq(dws->irq, dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 550 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 551 | EXPORT_SYMBOL_GPL(dw_spi_remove_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 552 | |
| 553 | int dw_spi_suspend_host(struct dw_spi *dws) |
| 554 | { |
Andy Shevchenko | 1cc3f14 | 2015-10-14 23:12:23 +0300 | [diff] [blame] | 555 | int ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 556 | |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 557 | ret = spi_controller_suspend(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 558 | if (ret) |
| 559 | return ret; |
Andy Shevchenko | 1cc3f14 | 2015-10-14 23:12:23 +0300 | [diff] [blame] | 560 | |
| 561 | spi_shutdown_chip(dws); |
| 562 | return 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 563 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 564 | EXPORT_SYMBOL_GPL(dw_spi_suspend_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 565 | |
| 566 | int dw_spi_resume_host(struct dw_spi *dws) |
| 567 | { |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 568 | spi_hw_init(&dws->master->dev, dws); |
Geert Uytterhoeven | 7c5d8a2 | 2018-09-05 10:51:57 +0200 | [diff] [blame] | 569 | return spi_controller_resume(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 570 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 571 | EXPORT_SYMBOL_GPL(dw_spi_resume_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 572 | |
| 573 | MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>"); |
| 574 | MODULE_DESCRIPTION("Driver for DesignWare SPI controller core"); |
| 575 | MODULE_LICENSE("GPL v2"); |