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> |
| 15 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 16 | #include "spi-dw.h" |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 17 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 18 | #ifdef CONFIG_DEBUG_FS |
| 19 | #include <linux/debugfs.h> |
| 20 | #endif |
| 21 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 22 | /* Slave spi_dev related */ |
| 23 | struct chip_data { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 24 | u8 tmode; /* TR/TO/RO/EEPROM */ |
| 25 | u8 type; /* SPI/SSP/MicroWire */ |
| 26 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 27 | u16 clk_div; /* baud rate divider */ |
| 28 | u32 speed_hz; /* baud rate */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | #ifdef CONFIG_DEBUG_FS |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 32 | #define SPI_REGS_BUFSIZE 1024 |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 33 | static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf, |
| 34 | size_t count, loff_t *ppos) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 35 | { |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 36 | struct dw_spi *dws = file->private_data; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 37 | char *buf; |
| 38 | u32 len = 0; |
| 39 | ssize_t ret; |
| 40 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 41 | buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL); |
| 42 | if (!buf) |
| 43 | return 0; |
| 44 | |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 45 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 46 | "%s registers:\n", dev_name(&dws->master->dev)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 47 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 48 | "=================================\n"); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 49 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 50 | "CTRLR0: \t0x%08x\n", dw_readl(dws, DW_SPI_CTRLR0)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 51 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 52 | "CTRLR1: \t0x%08x\n", dw_readl(dws, DW_SPI_CTRLR1)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 53 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 54 | "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 55 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 56 | "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 57 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 58 | "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 59 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 60 | "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFTLR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 61 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 62 | "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFTLR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 63 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 64 | "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 65 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 66 | "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 67 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 68 | "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 69 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 70 | "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 71 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 72 | "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 73 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 74 | "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 75 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 76 | "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 77 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 78 | "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR)); |
Silvio Cesare | d1d6bd7 | 2019-01-12 16:28:44 +0100 | [diff] [blame] | 79 | len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 80 | "=================================\n"); |
| 81 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 82 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 83 | kfree(buf); |
| 84 | return ret; |
| 85 | } |
| 86 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 87 | static const struct file_operations dw_spi_regs_ops = { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 88 | .owner = THIS_MODULE, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 89 | .open = simple_open, |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 90 | .read = dw_spi_show_regs, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 91 | .llseek = default_llseek, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 94 | static int dw_spi_debugfs_init(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 95 | { |
Phil Reid | e70002c80 | 2017-01-06 17:35:13 +0800 | [diff] [blame] | 96 | char name[32]; |
Phil Reid | 13288bd | 2016-12-22 17:18:12 +0800 | [diff] [blame] | 97 | |
Phil Reid | e70002c80 | 2017-01-06 17:35:13 +0800 | [diff] [blame] | 98 | snprintf(name, 32, "dw_spi%d", dws->master->bus_num); |
Phil Reid | 13288bd | 2016-12-22 17:18:12 +0800 | [diff] [blame] | 99 | dws->debugfs = debugfs_create_dir(name, NULL); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 100 | if (!dws->debugfs) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | debugfs_create_file("registers", S_IFREG | S_IRUGO, |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 104 | dws->debugfs, (void *)dws, &dw_spi_regs_ops); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 108 | static void dw_spi_debugfs_remove(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 109 | { |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 110 | debugfs_remove_recursive(dws->debugfs); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | #else |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 114 | static inline int dw_spi_debugfs_init(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 115 | { |
George Shore | 20a588f | 2010-01-21 11:40:49 +0000 | [diff] [blame] | 116 | return 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 119 | static inline void dw_spi_debugfs_remove(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | #endif /* CONFIG_DEBUG_FS */ |
| 123 | |
Alexandre Belloni | c79bdbb | 2018-07-27 21:53:54 +0200 | [diff] [blame] | 124 | void dw_spi_set_cs(struct spi_device *spi, bool enable) |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 125 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 126 | struct dw_spi *dws = spi_controller_get_devdata(spi->controller); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 127 | |
Charles Keepax | ada9e3f | 2019-11-27 15:39:36 +0000 | [diff] [blame] | 128 | if (!enable) |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 129 | dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select)); |
Talel Shenhar | f2d7047 | 2018-10-11 14:20:07 +0300 | [diff] [blame] | 130 | else if (dws->cs_override) |
| 131 | dw_writel(dws, DW_SPI_SER, 0); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 132 | } |
Alexandre Belloni | c79bdbb | 2018-07-27 21:53:54 +0200 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(dw_spi_set_cs); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 134 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 135 | /* Return the max entries we can fill into tx fifo */ |
| 136 | static inline u32 tx_max(struct dw_spi *dws) |
| 137 | { |
| 138 | u32 tx_left, tx_room, rxtx_gap; |
| 139 | |
| 140 | tx_left = (dws->tx_end - dws->tx) / dws->n_bytes; |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 141 | tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * Another concern is about the tx/rx mismatch, we |
| 145 | * though to use (dws->fifo_len - rxflr - txflr) as |
| 146 | * one maximum value for tx, but it doesn't cover the |
| 147 | * data which is out of tx/rx fifo and inside the |
| 148 | * shift registers. So a control from sw point of |
| 149 | * view is taken. |
| 150 | */ |
| 151 | rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx)) |
| 152 | / dws->n_bytes; |
| 153 | |
| 154 | return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap)); |
| 155 | } |
| 156 | |
| 157 | /* Return the max entries we should read out of rx fifo */ |
| 158 | static inline u32 rx_max(struct dw_spi *dws) |
| 159 | { |
| 160 | u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes; |
| 161 | |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 162 | return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR)); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 165 | static void dw_writer(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 166 | { |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 167 | u32 max; |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 168 | u16 txw = 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 169 | |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 170 | spin_lock(&dws->buf_lock); |
| 171 | max = tx_max(dws); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 172 | while (max--) { |
| 173 | /* Set the tx word if the transfer's original "tx" is not null */ |
| 174 | if (dws->tx_end - dws->len) { |
| 175 | if (dws->n_bytes == 1) |
| 176 | txw = *(u8 *)(dws->tx); |
| 177 | else |
| 178 | txw = *(u16 *)(dws->tx); |
| 179 | } |
Michael van der Westhuizen | c4fe57f | 2015-08-18 22:21:53 +0200 | [diff] [blame] | 180 | dw_write_io_reg(dws, DW_SPI_DR, txw); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 181 | dws->tx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 182 | } |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 183 | spin_unlock(&dws->buf_lock); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 186 | static void dw_reader(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 187 | { |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 188 | u32 max; |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 189 | u16 rxw; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 190 | |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 191 | spin_lock(&dws->buf_lock); |
| 192 | max = rx_max(dws); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 193 | while (max--) { |
Michael van der Westhuizen | c4fe57f | 2015-08-18 22:21:53 +0200 | [diff] [blame] | 194 | rxw = dw_read_io_reg(dws, DW_SPI_DR); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 195 | /* Care rx only if the transfer's original "rx" is not null */ |
| 196 | if (dws->rx_end - dws->len) { |
| 197 | if (dws->n_bytes == 1) |
| 198 | *(u8 *)(dws->rx) = rxw; |
| 199 | else |
| 200 | *(u16 *)(dws->rx) = rxw; |
| 201 | } |
| 202 | dws->rx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 203 | } |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 204 | spin_unlock(&dws->buf_lock); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 207 | static void int_error_stop(struct dw_spi *dws, const char *msg) |
| 208 | { |
Andy Shevchenko | 45746e8 | 2015-03-02 14:58:55 +0200 | [diff] [blame] | 209 | spi_reset_chip(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 210 | |
| 211 | dev_err(&dws->master->dev, "%s\n", msg); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 212 | dws->master->cur_msg->status = -EIO; |
| 213 | spi_finalize_current_transfer(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 216 | static irqreturn_t interrupt_transfer(struct dw_spi *dws) |
| 217 | { |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 218 | u16 irq_status = dw_readl(dws, DW_SPI_ISR); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 219 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 220 | /* Error handling */ |
| 221 | if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) { |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 222 | dw_readl(dws, DW_SPI_ICR); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 223 | int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 224 | return IRQ_HANDLED; |
| 225 | } |
| 226 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 227 | dw_reader(dws); |
| 228 | if (dws->rx_end == dws->rx) { |
| 229 | spi_mask_intr(dws, SPI_INT_TXEI); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 230 | spi_finalize_current_transfer(dws->master); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 231 | return IRQ_HANDLED; |
| 232 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 233 | if (irq_status & SPI_INT_TXEI) { |
| 234 | spi_mask_intr(dws, SPI_INT_TXEI); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 235 | dw_writer(dws); |
| 236 | /* Enable TX irq always, it will be disabled when RX finished */ |
| 237 | spi_umask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 238 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 239 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 240 | return IRQ_HANDLED; |
| 241 | } |
| 242 | |
| 243 | static irqreturn_t dw_spi_irq(int irq, void *dev_id) |
| 244 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 245 | struct spi_controller *master = dev_id; |
| 246 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 247 | u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f; |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 248 | |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 249 | if (!irq_status) |
| 250 | return IRQ_NONE; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 251 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 252 | if (!master->cur_msg) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 253 | spi_mask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 254 | return IRQ_HANDLED; |
| 255 | } |
| 256 | |
| 257 | return dws->transfer_handler(dws); |
| 258 | } |
| 259 | |
Wan Ahmad Zainie | c4eadee | 2020-05-05 21:06:13 +0800 | [diff] [blame] | 260 | /* Configure CTRLR0 for DW_apb_ssi */ |
| 261 | u32 dw_spi_update_cr0(struct spi_controller *master, struct spi_device *spi, |
| 262 | struct spi_transfer *transfer) |
| 263 | { |
| 264 | struct dw_spi *dws = spi_controller_get_devdata(master); |
| 265 | struct chip_data *chip = spi_get_ctldata(spi); |
| 266 | u32 cr0; |
| 267 | |
| 268 | /* Default SPI mode is SCPOL = 0, SCPH = 0 */ |
| 269 | cr0 = (transfer->bits_per_word - 1) |
| 270 | | (chip->type << SPI_FRF_OFFSET) |
| 271 | | ((((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET) | |
| 272 | (((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET) | |
| 273 | (((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET)) |
| 274 | | (chip->tmode << SPI_TMOD_OFFSET); |
| 275 | |
| 276 | return cr0; |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(dw_spi_update_cr0); |
| 279 | |
Wan Ahmad Zainie | e539f43 | 2020-05-05 21:06:14 +0800 | [diff] [blame^] | 280 | /* Configure CTRLR0 for DWC_ssi */ |
| 281 | u32 dw_spi_update_cr0_v1_01a(struct spi_controller *master, |
| 282 | struct spi_device *spi, |
| 283 | struct spi_transfer *transfer) |
| 284 | { |
| 285 | struct dw_spi *dws = spi_controller_get_devdata(master); |
| 286 | struct chip_data *chip = spi_get_ctldata(spi); |
| 287 | u32 cr0; |
| 288 | |
| 289 | /* CTRLR0[ 4: 0] Data Frame Size */ |
| 290 | cr0 = (transfer->bits_per_word - 1); |
| 291 | |
| 292 | /* CTRLR0[ 7: 6] Frame Format */ |
| 293 | cr0 |= chip->type << DWC_SSI_CTRLR0_FRF_OFFSET; |
| 294 | |
| 295 | /* |
| 296 | * SPI mode (SCPOL|SCPH) |
| 297 | * CTRLR0[ 8] Serial Clock Phase |
| 298 | * CTRLR0[ 9] Serial Clock Polarity |
| 299 | */ |
| 300 | cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << DWC_SSI_CTRLR0_SCPOL_OFFSET; |
| 301 | cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << DWC_SSI_CTRLR0_SCPH_OFFSET; |
| 302 | |
| 303 | /* CTRLR0[11:10] Transfer Mode */ |
| 304 | cr0 |= chip->tmode << DWC_SSI_CTRLR0_TMOD_OFFSET; |
| 305 | |
| 306 | /* CTRLR0[13] Shift Register Loop */ |
| 307 | cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << DWC_SSI_CTRLR0_SRL_OFFSET; |
| 308 | |
| 309 | return cr0; |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(dw_spi_update_cr0_v1_01a); |
| 312 | |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 313 | static int dw_spi_transfer_one(struct spi_controller *master, |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 314 | struct spi_device *spi, struct spi_transfer *transfer) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 315 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 316 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 317 | struct chip_data *chip = spi_get_ctldata(spi); |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 318 | unsigned long flags; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 319 | u8 imask = 0; |
Andy Shevchenko | ea11370 | 2015-02-24 13:32:11 +0200 | [diff] [blame] | 320 | u16 txlevel = 0; |
Andy Shevchenko | 4adb1f8 | 2015-10-14 23:12:18 +0300 | [diff] [blame] | 321 | u32 cr0; |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 322 | int ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 323 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 324 | dws->dma_mapped = 0; |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 325 | spin_lock_irqsave(&dws->buf_lock, flags); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 326 | dws->tx = (void *)transfer->tx_buf; |
| 327 | dws->tx_end = dws->tx + transfer->len; |
| 328 | dws->rx = transfer->rx_buf; |
| 329 | dws->rx_end = dws->rx + transfer->len; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 330 | dws->len = transfer->len; |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 331 | spin_unlock_irqrestore(&dws->buf_lock, flags); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 332 | |
Xinwei Kong | bfda044 | 2020-01-03 10:52:10 +0800 | [diff] [blame] | 333 | /* Ensure dw->rx and dw->rx_end are visible */ |
| 334 | smp_mb(); |
| 335 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 336 | spi_enable_chip(dws, 0); |
| 337 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 338 | /* Handle per transfer options for bpw and speed */ |
Matthias Seidel | 13b1030 | 2016-09-04 02:04:49 +0200 | [diff] [blame] | 339 | if (transfer->speed_hz != dws->current_freq) { |
| 340 | if (transfer->speed_hz != chip->speed_hz) { |
| 341 | /* clk_div doesn't support odd number */ |
Matthias Seidel | 3aef463 | 2016-09-07 17:45:30 +0200 | [diff] [blame] | 342 | 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] | 343 | chip->speed_hz = transfer->speed_hz; |
| 344 | } |
| 345 | dws->current_freq = transfer->speed_hz; |
Jarkko Nikula | 0ed3699 | 2015-09-15 16:26:23 +0300 | [diff] [blame] | 346 | spi_set_clk(dws, chip->clk_div); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 347 | } |
Simon Goldschmidt | af060b3 | 2018-09-04 21:49:44 +0200 | [diff] [blame] | 348 | |
| 349 | dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE); |
| 350 | dws->dma_width = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE); |
| 351 | |
Wan Ahmad Zainie | c4eadee | 2020-05-05 21:06:13 +0800 | [diff] [blame] | 352 | cr0 = dws->update_cr0(master, spi, transfer); |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 353 | dw_writel(dws, DW_SPI_CTRLR0, cr0); |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 354 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 355 | /* Check if current transfer is a DMA transaction */ |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 356 | if (master->can_dma && master->can_dma(master, spi, transfer)) |
| 357 | dws->dma_mapped = master->cur_msg_mapped; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 358 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 359 | /* For poll mode just disable all interrupts */ |
| 360 | spi_mask_intr(dws, 0xff); |
| 361 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 362 | /* |
| 363 | * Interrupt mode |
| 364 | * we only need set the TXEI IRQ, as TX/RX always happen syncronizely |
| 365 | */ |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 366 | if (dws->dma_mapped) { |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 367 | ret = dws->dma_ops->dma_setup(dws, transfer); |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 368 | if (ret < 0) { |
| 369 | spi_enable_chip(dws, 1); |
| 370 | return ret; |
| 371 | } |
Clement Leger | 33e8fd4 | 2020-04-16 13:09:16 +0200 | [diff] [blame] | 372 | } else { |
Andy Shevchenko | ea11370 | 2015-02-24 13:32:11 +0200 | [diff] [blame] | 373 | 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] | 374 | dw_writel(dws, DW_SPI_TXFTLR, txlevel); |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 375 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 376 | /* Set the interrupt mask */ |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 377 | imask |= SPI_INT_TXEI | SPI_INT_TXOI | |
| 378 | SPI_INT_RXUI | SPI_INT_RXOI; |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 379 | spi_umask_intr(dws, imask); |
| 380 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 381 | dws->transfer_handler = interrupt_transfer; |
| 382 | } |
| 383 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 384 | spi_enable_chip(dws, 1); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 385 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 386 | if (dws->dma_mapped) { |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 387 | ret = dws->dma_ops->dma_transfer(dws, transfer); |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 388 | if (ret < 0) |
| 389 | return ret; |
| 390 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 391 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 392 | return 1; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 395 | static void dw_spi_handle_err(struct spi_controller *master, |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 396 | struct spi_message *msg) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 397 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 398 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 399 | |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 400 | if (dws->dma_mapped) |
| 401 | dws->dma_ops->dma_stop(dws); |
| 402 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 403 | spi_reset_chip(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* This may be called twice for each spi dev */ |
| 407 | static int dw_spi_setup(struct spi_device *spi) |
| 408 | { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 409 | struct chip_data *chip; |
| 410 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 411 | /* Only alloc on first setup */ |
| 412 | chip = spi_get_ctldata(spi); |
| 413 | if (!chip) { |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 414 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 415 | if (!chip) |
| 416 | return -ENOMEM; |
Baruch Siach | 43f627a | 2013-12-30 20:30:46 +0200 | [diff] [blame] | 417 | spi_set_ctldata(spi, chip); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Jisheng Zhang | 6096828 | 2015-12-23 19:05:39 +0800 | [diff] [blame] | 420 | chip->tmode = SPI_TMOD_TR; |
Andy Shevchenko | c3ce15b | 2014-09-18 20:08:56 +0300 | [diff] [blame] | 421 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 425 | static void dw_spi_cleanup(struct spi_device *spi) |
| 426 | { |
| 427 | struct chip_data *chip = spi_get_ctldata(spi); |
| 428 | |
| 429 | kfree(chip); |
| 430 | spi_set_ctldata(spi, NULL); |
| 431 | } |
| 432 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 433 | /* Restart the controller, disable all interrupts, clean rx fifo */ |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 434 | static void spi_hw_init(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 435 | { |
Andy Shevchenko | 45746e8 | 2015-03-02 14:58:55 +0200 | [diff] [blame] | 436 | spi_reset_chip(dws); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * Try to detect the FIFO depth if not set by interface driver, |
| 440 | * the depth could be from 2 to 256 from HW spec |
| 441 | */ |
| 442 | if (!dws->fifo_len) { |
| 443 | u32 fifo; |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 444 | |
Andy Shevchenko | 9d239d3 | 2015-02-25 11:39:36 +0200 | [diff] [blame] | 445 | for (fifo = 1; fifo < 256; fifo++) { |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 446 | dw_writel(dws, DW_SPI_TXFTLR, fifo); |
| 447 | if (fifo != dw_readl(dws, DW_SPI_TXFTLR)) |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 448 | break; |
| 449 | } |
Wan Ahmad Zainie | 299cb65 | 2020-05-05 21:06:12 +0800 | [diff] [blame] | 450 | dw_writel(dws, DW_SPI_TXFTLR, 0); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 451 | |
Andy Shevchenko | 9d239d3 | 2015-02-25 11:39:36 +0200 | [diff] [blame] | 452 | dws->fifo_len = (fifo == 1) ? 0 : fifo; |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 453 | dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 454 | } |
Talel Shenhar | f2d7047 | 2018-10-11 14:20:07 +0300 | [diff] [blame] | 455 | |
| 456 | /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */ |
| 457 | if (dws->cs_override) |
| 458 | dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 461 | int dw_spi_add_host(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 462 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 463 | struct spi_controller *master; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 464 | int ret; |
| 465 | |
Aditya Pakki | 169f9ac | 2019-12-05 17:14:21 -0600 | [diff] [blame] | 466 | if (!dws) |
| 467 | return -EINVAL; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 468 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 469 | master = spi_alloc_master(dev, 0); |
| 470 | if (!master) |
| 471 | return -ENOMEM; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 472 | |
| 473 | dws->master = master; |
| 474 | dws->type = SSI_MOTO_SPI; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 475 | dws->dma_inited = 0; |
Andy Shevchenko | d7ef54c | 2015-10-27 17:48:16 +0200 | [diff] [blame] | 476 | dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR); |
wuxu.wu | 19b6139 | 2020-01-01 11:39:41 +0800 | [diff] [blame] | 477 | spin_lock_init(&dws->buf_lock); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 478 | |
Alexandre Belloni | 66b19d7 | 2018-07-17 16:23:10 +0200 | [diff] [blame] | 479 | spi_controller_set_devdata(master, dws); |
| 480 | |
Phil Reid | e70002c80 | 2017-01-06 17:35:13 +0800 | [diff] [blame] | 481 | ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev), |
| 482 | master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 483 | if (ret < 0) { |
Andy Shevchenko | 5f0966e | 2015-10-14 23:12:17 +0300 | [diff] [blame] | 484 | dev_err(dev, "can not get IRQ\n"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 485 | goto err_free_master; |
| 486 | } |
| 487 | |
Linus Walleij | 9400c41 | 2019-01-07 16:51:56 +0100 | [diff] [blame] | 488 | master->use_gpio_descriptors = true; |
Andy Shevchenko | c3ce15b | 2014-09-18 20:08:56 +0300 | [diff] [blame] | 489 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; |
Simon Goldschmidt | af060b3 | 2018-09-04 21:49:44 +0200 | [diff] [blame] | 490 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 491 | master->bus_num = dws->bus_num; |
| 492 | master->num_chipselect = dws->num_cs; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 493 | master->setup = dw_spi_setup; |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 494 | master->cleanup = dw_spi_cleanup; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 495 | master->set_cs = dw_spi_set_cs; |
| 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 | |
Alexandre Belloni | 62dbbae | 2018-07-17 16:23:11 +0200 | [diff] [blame] | 504 | if (dws->set_cs) |
| 505 | master->set_cs = dws->set_cs; |
| 506 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 507 | /* Basic HW init */ |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 508 | spi_hw_init(dev, dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 509 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 510 | if (dws->dma_ops && dws->dma_ops->dma_init) { |
| 511 | ret = dws->dma_ops->dma_init(dws); |
| 512 | if (ret) { |
Andy Shevchenko | 3dbb3b9 | 2015-01-07 16:56:54 +0200 | [diff] [blame] | 513 | dev_warn(dev, "DMA init failed\n"); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 514 | dws->dma_inited = 0; |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 515 | } else { |
| 516 | master->can_dma = dws->dma_ops->can_dma; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 520 | ret = devm_spi_register_controller(dev, master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 521 | if (ret) { |
| 522 | dev_err(&master->dev, "problem registering spi master\n"); |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 523 | goto err_dma_exit; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 526 | dw_spi_debugfs_init(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 527 | return 0; |
| 528 | |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 529 | err_dma_exit: |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 530 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 531 | dws->dma_ops->dma_exit(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 532 | spi_enable_chip(dws, 0); |
Andy Shevchenko | 02f2038 | 2015-10-20 12:11:40 +0300 | [diff] [blame] | 533 | free_irq(dws->irq, master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 534 | err_free_master: |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 535 | spi_controller_put(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 536 | return ret; |
| 537 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 538 | EXPORT_SYMBOL_GPL(dw_spi_add_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 539 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 540 | void dw_spi_remove_host(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 541 | { |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 542 | dw_spi_debugfs_remove(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 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"); |