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