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