Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * Designware SPI core controller driver (refer pxa2xx_spi.c) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2009, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/dma-mapping.h> |
| 17 | #include <linux/interrupt.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 19 | #include <linux/highmem.h> |
| 20 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 22 | #include <linux/spi/spi.h> |
Baruch Siach | d9c73bb | 2014-01-31 12:07:47 +0200 | [diff] [blame] | 23 | #include <linux/gpio.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 24 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 25 | #include "spi-dw.h" |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 26 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 27 | #ifdef CONFIG_DEBUG_FS |
| 28 | #include <linux/debugfs.h> |
| 29 | #endif |
| 30 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 31 | /* Slave spi_dev related */ |
| 32 | struct chip_data { |
| 33 | u16 cr0; |
| 34 | u8 cs; /* chip select pin */ |
| 35 | u8 n_bytes; /* current is a 1/2/4 byte op */ |
| 36 | u8 tmode; /* TR/TO/RO/EEPROM */ |
| 37 | u8 type; /* SPI/SSP/MicroWire */ |
| 38 | |
| 39 | u8 poll_mode; /* 1 means use poll mode */ |
| 40 | |
| 41 | u32 dma_width; |
| 42 | u32 rx_threshold; |
| 43 | u32 tx_threshold; |
| 44 | u8 enable_dma; |
| 45 | u8 bits_per_word; |
| 46 | u16 clk_div; /* baud rate divider */ |
| 47 | u32 speed_hz; /* baud rate */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 48 | void (*cs_control)(u32 command); |
| 49 | }; |
| 50 | |
| 51 | #ifdef CONFIG_DEBUG_FS |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 52 | #define SPI_REGS_BUFSIZE 1024 |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 53 | static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf, |
| 54 | size_t count, loff_t *ppos) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 55 | { |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 56 | struct dw_spi *dws = file->private_data; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 57 | char *buf; |
| 58 | u32 len = 0; |
| 59 | ssize_t ret; |
| 60 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 61 | buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL); |
| 62 | if (!buf) |
| 63 | return 0; |
| 64 | |
| 65 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 66 | "%s registers:\n", dev_name(&dws->master->dev)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 67 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
| 68 | "=================================\n"); |
| 69 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 70 | "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 71 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 72 | "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 73 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 74 | "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 75 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 76 | "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 77 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 78 | "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 79 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 80 | "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 81 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 82 | "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 83 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 84 | "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 85 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 86 | "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 87 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 88 | "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 89 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 90 | "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 91 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 92 | "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 93 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 94 | "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 95 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 96 | "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 97 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 98 | "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 99 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
| 100 | "=================================\n"); |
| 101 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 102 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 103 | kfree(buf); |
| 104 | return ret; |
| 105 | } |
| 106 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 107 | static const struct file_operations dw_spi_regs_ops = { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 108 | .owner = THIS_MODULE, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 109 | .open = simple_open, |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 110 | .read = dw_spi_show_regs, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 111 | .llseek = default_llseek, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 114 | static int dw_spi_debugfs_init(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 115 | { |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 116 | dws->debugfs = debugfs_create_dir("dw_spi", NULL); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 117 | if (!dws->debugfs) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | debugfs_create_file("registers", S_IFREG | S_IRUGO, |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 121 | dws->debugfs, (void *)dws, &dw_spi_regs_ops); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 125 | static void dw_spi_debugfs_remove(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 126 | { |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 127 | debugfs_remove_recursive(dws->debugfs); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #else |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 131 | static inline int dw_spi_debugfs_init(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 132 | { |
George Shore | 20a588f | 2010-01-21 11:40:49 +0000 | [diff] [blame] | 133 | return 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 136 | static inline void dw_spi_debugfs_remove(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 137 | { |
| 138 | } |
| 139 | #endif /* CONFIG_DEBUG_FS */ |
| 140 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 141 | static void dw_spi_set_cs(struct spi_device *spi, bool enable) |
| 142 | { |
| 143 | struct dw_spi *dws = spi_master_get_devdata(spi->master); |
| 144 | struct chip_data *chip = spi_get_ctldata(spi); |
| 145 | |
| 146 | /* Chip select logic is inverted from spi_set_cs() */ |
| 147 | if (chip->cs_control) |
| 148 | chip->cs_control(!enable); |
| 149 | |
| 150 | if (!enable) |
| 151 | dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select)); |
| 152 | } |
| 153 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 154 | /* Return the max entries we can fill into tx fifo */ |
| 155 | static inline u32 tx_max(struct dw_spi *dws) |
| 156 | { |
| 157 | u32 tx_left, tx_room, rxtx_gap; |
| 158 | |
| 159 | tx_left = (dws->tx_end - dws->tx) / dws->n_bytes; |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 160 | tx_room = dws->fifo_len - dw_readw(dws, DW_SPI_TXFLR); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * Another concern is about the tx/rx mismatch, we |
| 164 | * though to use (dws->fifo_len - rxflr - txflr) as |
| 165 | * one maximum value for tx, but it doesn't cover the |
| 166 | * data which is out of tx/rx fifo and inside the |
| 167 | * shift registers. So a control from sw point of |
| 168 | * view is taken. |
| 169 | */ |
| 170 | rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx)) |
| 171 | / dws->n_bytes; |
| 172 | |
| 173 | return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap)); |
| 174 | } |
| 175 | |
| 176 | /* Return the max entries we should read out of rx fifo */ |
| 177 | static inline u32 rx_max(struct dw_spi *dws) |
| 178 | { |
| 179 | u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes; |
| 180 | |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 181 | return min_t(u32, rx_left, dw_readw(dws, DW_SPI_RXFLR)); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 184 | static void dw_writer(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 185 | { |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 186 | u32 max = tx_max(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 187 | u16 txw = 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 188 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 189 | while (max--) { |
| 190 | /* Set the tx word if the transfer's original "tx" is not null */ |
| 191 | if (dws->tx_end - dws->len) { |
| 192 | if (dws->n_bytes == 1) |
| 193 | txw = *(u8 *)(dws->tx); |
| 194 | else |
| 195 | txw = *(u16 *)(dws->tx); |
| 196 | } |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 197 | dw_writew(dws, DW_SPI_DR, txw); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 198 | dws->tx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 199 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 202 | static void dw_reader(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 203 | { |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 204 | u32 max = rx_max(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 205 | u16 rxw; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 206 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 207 | while (max--) { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 208 | rxw = dw_readw(dws, DW_SPI_DR); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 209 | /* Care rx only if the transfer's original "rx" is not null */ |
| 210 | if (dws->rx_end - dws->len) { |
| 211 | if (dws->n_bytes == 1) |
| 212 | *(u8 *)(dws->rx) = rxw; |
| 213 | else |
| 214 | *(u16 *)(dws->rx) = rxw; |
| 215 | } |
| 216 | dws->rx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 217 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 220 | /* |
| 221 | * Note: first step is the protocol driver prepares |
| 222 | * a dma-capable memory, and this func just need translate |
| 223 | * the virt addr to physical |
| 224 | */ |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 225 | static int map_dma_buffers(struct spi_master *master, |
| 226 | struct spi_device *spi, struct spi_transfer *transfer) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 227 | { |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 228 | struct dw_spi *dws = spi_master_get_devdata(master); |
| 229 | struct chip_data *chip = spi_get_ctldata(spi); |
| 230 | |
| 231 | if (!master->cur_msg->is_dma_mapped |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 232 | || !dws->dma_inited |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 233 | || !chip->enable_dma |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 234 | || !dws->dma_ops) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 235 | return 0; |
| 236 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 237 | if (transfer->tx_dma) |
| 238 | dws->tx_dma = transfer->tx_dma; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 239 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 240 | if (transfer->rx_dma) |
| 241 | dws->rx_dma = transfer->rx_dma; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 242 | |
| 243 | return 1; |
| 244 | } |
| 245 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 246 | static void int_error_stop(struct dw_spi *dws, const char *msg) |
| 247 | { |
Andy Shevchenko | 45746e8 | 2015-03-02 14:58:55 +0200 | [diff] [blame] | 248 | spi_reset_chip(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 249 | |
| 250 | dev_err(&dws->master->dev, "%s\n", msg); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 251 | dws->master->cur_msg->status = -EIO; |
| 252 | spi_finalize_current_transfer(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 255 | static irqreturn_t interrupt_transfer(struct dw_spi *dws) |
| 256 | { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 257 | u16 irq_status = dw_readw(dws, DW_SPI_ISR); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 258 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 259 | /* Error handling */ |
| 260 | if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 261 | dw_readw(dws, DW_SPI_TXOICR); |
| 262 | dw_readw(dws, DW_SPI_RXOICR); |
| 263 | dw_readw(dws, DW_SPI_RXUICR); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 264 | int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 265 | return IRQ_HANDLED; |
| 266 | } |
| 267 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 268 | dw_reader(dws); |
| 269 | if (dws->rx_end == dws->rx) { |
| 270 | spi_mask_intr(dws, SPI_INT_TXEI); |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 271 | spi_finalize_current_transfer(dws->master); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 272 | return IRQ_HANDLED; |
| 273 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 274 | if (irq_status & SPI_INT_TXEI) { |
| 275 | spi_mask_intr(dws, SPI_INT_TXEI); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 276 | dw_writer(dws); |
| 277 | /* Enable TX irq always, it will be disabled when RX finished */ |
| 278 | spi_umask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 279 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 280 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
| 284 | static irqreturn_t dw_spi_irq(int irq, void *dev_id) |
| 285 | { |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 286 | struct spi_master *master = dev_id; |
| 287 | struct dw_spi *dws = spi_master_get_devdata(master); |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 288 | u16 irq_status = dw_readw(dws, DW_SPI_ISR) & 0x3f; |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 289 | |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 290 | if (!irq_status) |
| 291 | return IRQ_NONE; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 292 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 293 | if (!master->cur_msg) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 294 | spi_mask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 295 | return IRQ_HANDLED; |
| 296 | } |
| 297 | |
| 298 | return dws->transfer_handler(dws); |
| 299 | } |
| 300 | |
| 301 | /* Must be called inside pump_transfers() */ |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 302 | static int poll_transfer(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 303 | { |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 304 | do { |
| 305 | dw_writer(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 306 | dw_reader(dws); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 307 | cpu_relax(); |
| 308 | } while (dws->rx_end > dws->rx); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 309 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 310 | return 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 313 | static int dw_spi_transfer_one(struct spi_master *master, |
| 314 | struct spi_device *spi, struct spi_transfer *transfer) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 315 | { |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 316 | struct dw_spi *dws = spi_master_get_devdata(master); |
| 317 | struct chip_data *chip = spi_get_ctldata(spi); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 318 | u8 imask = 0; |
Andy Shevchenko | ea11370 | 2015-02-24 13:32:11 +0200 | [diff] [blame] | 319 | u16 txlevel = 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 320 | u16 clk_div = 0; |
| 321 | u32 speed = 0; |
| 322 | u32 cr0 = 0; |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame^] | 323 | int ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 324 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 325 | dws->n_bytes = chip->n_bytes; |
| 326 | dws->dma_width = chip->dma_width; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 327 | |
| 328 | dws->rx_dma = transfer->rx_dma; |
| 329 | dws->tx_dma = transfer->tx_dma; |
| 330 | dws->tx = (void *)transfer->tx_buf; |
| 331 | dws->tx_end = dws->tx + transfer->len; |
| 332 | dws->rx = transfer->rx_buf; |
| 333 | dws->rx_end = dws->rx + transfer->len; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 334 | dws->len = transfer->len; |
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 | spi_enable_chip(dws, 0); |
| 337 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 338 | cr0 = chip->cr0; |
| 339 | |
| 340 | /* Handle per transfer options for bpw and speed */ |
| 341 | if (transfer->speed_hz) { |
| 342 | speed = chip->speed_hz; |
| 343 | |
Andy Shevchenko | 341c7dc | 2015-02-24 13:32:12 +0200 | [diff] [blame] | 344 | if ((transfer->speed_hz != speed) || !chip->clk_div) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 345 | speed = transfer->speed_hz; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 346 | |
| 347 | /* clk_div doesn't support odd number */ |
Andy Shevchenko | 341c7dc | 2015-02-24 13:32:12 +0200 | [diff] [blame] | 348 | clk_div = (dws->max_freq / speed + 1) & 0xfffe; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 349 | |
| 350 | chip->speed_hz = speed; |
| 351 | chip->clk_div = clk_div; |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 352 | |
| 353 | spi_set_clk(dws, chip->clk_div); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | if (transfer->bits_per_word) { |
Andy Shevchenko | e31abce | 2015-03-09 16:48:45 +0200 | [diff] [blame] | 357 | if (transfer->bits_per_word == 8) { |
| 358 | dws->n_bytes = 1; |
| 359 | dws->dma_width = 1; |
| 360 | } else if (transfer->bits_per_word == 16) { |
| 361 | dws->n_bytes = 2; |
| 362 | dws->dma_width = 2; |
| 363 | } |
| 364 | cr0 = (transfer->bits_per_word - 1) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 365 | | (chip->type << SPI_FRF_OFFSET) |
| 366 | | (spi->mode << SPI_MODE_OFFSET) |
| 367 | | (chip->tmode << SPI_TMOD_OFFSET); |
| 368 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 369 | |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 370 | /* |
| 371 | * Adjust transfer mode if necessary. Requires platform dependent |
| 372 | * chipselect mechanism. |
| 373 | */ |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 374 | if (chip->cs_control) { |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 375 | if (dws->rx && dws->tx) |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 376 | chip->tmode = SPI_TMOD_TR; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 377 | else if (dws->rx) |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 378 | chip->tmode = SPI_TMOD_RO; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 379 | else |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 380 | chip->tmode = SPI_TMOD_TO; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 381 | |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 382 | cr0 &= ~SPI_TMOD_MASK; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 383 | cr0 |= (chip->tmode << SPI_TMOD_OFFSET); |
| 384 | } |
| 385 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 386 | dw_writew(dws, DW_SPI_CTRL0, cr0); |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 387 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 388 | /* Check if current transfer is a DMA transaction */ |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 389 | dws->dma_mapped = map_dma_buffers(master, spi, transfer); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 390 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 391 | /* For poll mode just disable all interrupts */ |
| 392 | spi_mask_intr(dws, 0xff); |
| 393 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 394 | /* |
| 395 | * Interrupt mode |
| 396 | * we only need set the TXEI IRQ, as TX/RX always happen syncronizely |
| 397 | */ |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame^] | 398 | if (dws->dma_mapped) { |
| 399 | ret = dws->dma_ops->dma_setup(dws); |
| 400 | if (ret < 0) { |
| 401 | spi_enable_chip(dws, 1); |
| 402 | return ret; |
| 403 | } |
| 404 | } else if (!chip->poll_mode) { |
Andy Shevchenko | ea11370 | 2015-02-24 13:32:11 +0200 | [diff] [blame] | 405 | txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes); |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 406 | dw_writew(dws, DW_SPI_TXFLTR, txlevel); |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 407 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 408 | /* Set the interrupt mask */ |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 409 | imask |= SPI_INT_TXEI | SPI_INT_TXOI | |
| 410 | SPI_INT_RXUI | SPI_INT_RXOI; |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 411 | spi_umask_intr(dws, imask); |
| 412 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 413 | dws->transfer_handler = interrupt_transfer; |
| 414 | } |
| 415 | |
Andy Shevchenko | 0b2e891 | 2015-03-02 14:58:56 +0200 | [diff] [blame] | 416 | spi_enable_chip(dws, 1); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 417 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame^] | 418 | if (dws->dma_mapped) { |
| 419 | ret = dws->dma_ops->dma_transfer(dws); |
| 420 | if (ret < 0) |
| 421 | return ret; |
| 422 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 423 | |
| 424 | if (chip->poll_mode) |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 425 | return poll_transfer(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 426 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 427 | return 1; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 430 | static void dw_spi_handle_err(struct spi_master *master, |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 431 | struct spi_message *msg) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 432 | { |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 433 | struct dw_spi *dws = spi_master_get_devdata(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 434 | |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 435 | spi_reset_chip(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* This may be called twice for each spi dev */ |
| 439 | static int dw_spi_setup(struct spi_device *spi) |
| 440 | { |
| 441 | struct dw_spi_chip *chip_info = NULL; |
| 442 | struct chip_data *chip; |
Baruch Siach | d9c73bb | 2014-01-31 12:07:47 +0200 | [diff] [blame] | 443 | int ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 444 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 445 | /* Only alloc on first setup */ |
| 446 | chip = spi_get_ctldata(spi); |
| 447 | if (!chip) { |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 448 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 449 | if (!chip) |
| 450 | return -ENOMEM; |
Baruch Siach | 43f627a | 2013-12-30 20:30:46 +0200 | [diff] [blame] | 451 | spi_set_ctldata(spi, chip); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Protocol drivers may change the chip settings, so... |
| 456 | * if chip_info exists, use it |
| 457 | */ |
| 458 | chip_info = spi->controller_data; |
| 459 | |
| 460 | /* chip_info doesn't always exist */ |
| 461 | if (chip_info) { |
| 462 | if (chip_info->cs_control) |
| 463 | chip->cs_control = chip_info->cs_control; |
| 464 | |
| 465 | chip->poll_mode = chip_info->poll_mode; |
| 466 | chip->type = chip_info->type; |
| 467 | |
| 468 | chip->rx_threshold = 0; |
| 469 | chip->tx_threshold = 0; |
| 470 | |
| 471 | chip->enable_dma = chip_info->enable_dma; |
| 472 | } |
| 473 | |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 474 | if (spi->bits_per_word == 8) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 475 | chip->n_bytes = 1; |
| 476 | chip->dma_width = 1; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 477 | } else if (spi->bits_per_word == 16) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 478 | chip->n_bytes = 2; |
| 479 | chip->dma_width = 2; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 480 | } |
| 481 | chip->bits_per_word = spi->bits_per_word; |
| 482 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 483 | if (!spi->max_speed_hz) { |
| 484 | dev_err(&spi->dev, "No max speed HZ parameter\n"); |
| 485 | return -EINVAL; |
| 486 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 487 | |
| 488 | chip->tmode = 0; /* Tx & Rx */ |
| 489 | /* Default SPI mode is SCPOL = 0, SCPH = 0 */ |
| 490 | chip->cr0 = (chip->bits_per_word - 1) |
| 491 | | (chip->type << SPI_FRF_OFFSET) |
| 492 | | (spi->mode << SPI_MODE_OFFSET) |
| 493 | | (chip->tmode << SPI_TMOD_OFFSET); |
| 494 | |
Andy Shevchenko | c3ce15b | 2014-09-18 20:08:56 +0300 | [diff] [blame] | 495 | if (spi->mode & SPI_LOOP) |
| 496 | chip->cr0 |= 1 << SPI_SRL_OFFSET; |
| 497 | |
Baruch Siach | d9c73bb | 2014-01-31 12:07:47 +0200 | [diff] [blame] | 498 | if (gpio_is_valid(spi->cs_gpio)) { |
| 499 | ret = gpio_direction_output(spi->cs_gpio, |
| 500 | !(spi->mode & SPI_CS_HIGH)); |
| 501 | if (ret) |
| 502 | return ret; |
| 503 | } |
| 504 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 508 | static void dw_spi_cleanup(struct spi_device *spi) |
| 509 | { |
| 510 | struct chip_data *chip = spi_get_ctldata(spi); |
| 511 | |
| 512 | kfree(chip); |
| 513 | spi_set_ctldata(spi, NULL); |
| 514 | } |
| 515 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 516 | /* Restart the controller, disable all interrupts, clean rx fifo */ |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 517 | static void spi_hw_init(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 518 | { |
Andy Shevchenko | 45746e8 | 2015-03-02 14:58:55 +0200 | [diff] [blame] | 519 | spi_reset_chip(dws); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * Try to detect the FIFO depth if not set by interface driver, |
| 523 | * the depth could be from 2 to 256 from HW spec |
| 524 | */ |
| 525 | if (!dws->fifo_len) { |
| 526 | u32 fifo; |
Jingoo Han | fadcace | 2014-09-02 11:49:24 +0900 | [diff] [blame] | 527 | |
Andy Shevchenko | 9d239d3 | 2015-02-25 11:39:36 +0200 | [diff] [blame] | 528 | for (fifo = 1; fifo < 256; fifo++) { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 529 | dw_writew(dws, DW_SPI_TXFLTR, fifo); |
| 530 | if (fifo != dw_readw(dws, DW_SPI_TXFLTR)) |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 531 | break; |
| 532 | } |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 533 | dw_writew(dws, DW_SPI_TXFLTR, 0); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 534 | |
Andy Shevchenko | 9d239d3 | 2015-02-25 11:39:36 +0200 | [diff] [blame] | 535 | dws->fifo_len = (fifo == 1) ? 0 : fifo; |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 536 | dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 537 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 540 | int dw_spi_add_host(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 541 | { |
| 542 | struct spi_master *master; |
| 543 | int ret; |
| 544 | |
| 545 | BUG_ON(dws == NULL); |
| 546 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 547 | master = spi_alloc_master(dev, 0); |
| 548 | if (!master) |
| 549 | return -ENOMEM; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 550 | |
| 551 | dws->master = master; |
| 552 | dws->type = SSI_MOTO_SPI; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 553 | dws->dma_inited = 0; |
| 554 | dws->dma_addr = (dma_addr_t)(dws->paddr + 0x60); |
Andy Shevchenko | c3c6e23 | 2014-09-18 20:08:57 +0300 | [diff] [blame] | 555 | snprintf(dws->name, sizeof(dws->name), "dw_spi%d", dws->bus_num); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 556 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 557 | ret = devm_request_irq(dev, dws->irq, dw_spi_irq, IRQF_SHARED, |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 558 | dws->name, master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 559 | if (ret < 0) { |
| 560 | dev_err(&master->dev, "can not get IRQ\n"); |
| 561 | goto err_free_master; |
| 562 | } |
| 563 | |
Andy Shevchenko | c3ce15b | 2014-09-18 20:08:56 +0300 | [diff] [blame] | 564 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 565 | master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 566 | master->bus_num = dws->bus_num; |
| 567 | master->num_chipselect = dws->num_cs; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 568 | master->setup = dw_spi_setup; |
Axel Lin | a97c883 | 2014-08-31 12:47:06 +0800 | [diff] [blame] | 569 | master->cleanup = dw_spi_cleanup; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 570 | master->set_cs = dw_spi_set_cs; |
| 571 | master->transfer_one = dw_spi_transfer_one; |
| 572 | master->handle_err = dw_spi_handle_err; |
Axel Lin | 765ee70 | 2014-02-20 21:37:56 +0800 | [diff] [blame] | 573 | master->max_speed_hz = dws->max_freq; |
Thor Thayer | 9c6de47 | 2014-10-08 13:51:34 -0500 | [diff] [blame] | 574 | master->dev.of_node = dev->of_node; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 575 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 576 | /* Basic HW init */ |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 577 | spi_hw_init(dev, dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 578 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 579 | if (dws->dma_ops && dws->dma_ops->dma_init) { |
| 580 | ret = dws->dma_ops->dma_init(dws); |
| 581 | if (ret) { |
Andy Shevchenko | 3dbb3b9 | 2015-01-07 16:56:54 +0200 | [diff] [blame] | 582 | dev_warn(dev, "DMA init failed\n"); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 583 | dws->dma_inited = 0; |
| 584 | } |
| 585 | } |
| 586 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 587 | spi_master_set_devdata(master, dws); |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 588 | ret = devm_spi_register_master(dev, master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 589 | if (ret) { |
| 590 | dev_err(&master->dev, "problem registering spi master\n"); |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 591 | goto err_dma_exit; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 594 | dw_spi_debugfs_init(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 595 | return 0; |
| 596 | |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 597 | err_dma_exit: |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 598 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 599 | dws->dma_ops->dma_exit(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 600 | spi_enable_chip(dws, 0); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 601 | err_free_master: |
| 602 | spi_master_put(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 603 | return ret; |
| 604 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 605 | EXPORT_SYMBOL_GPL(dw_spi_add_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 606 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 607 | void dw_spi_remove_host(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 608 | { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 609 | if (!dws) |
| 610 | return; |
Andy Shevchenko | 53288fe | 2014-09-12 15:11:56 +0300 | [diff] [blame] | 611 | dw_spi_debugfs_remove(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 612 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 613 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 614 | dws->dma_ops->dma_exit(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 615 | spi_enable_chip(dws, 0); |
| 616 | /* Disable clk */ |
| 617 | spi_set_clk(dws, 0); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 618 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 619 | EXPORT_SYMBOL_GPL(dw_spi_remove_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 620 | |
| 621 | int dw_spi_suspend_host(struct dw_spi *dws) |
| 622 | { |
| 623 | int ret = 0; |
| 624 | |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 625 | ret = spi_master_suspend(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 626 | if (ret) |
| 627 | return ret; |
| 628 | spi_enable_chip(dws, 0); |
| 629 | spi_set_clk(dws, 0); |
| 630 | return ret; |
| 631 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 632 | EXPORT_SYMBOL_GPL(dw_spi_suspend_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 633 | |
| 634 | int dw_spi_resume_host(struct dw_spi *dws) |
| 635 | { |
| 636 | int ret; |
| 637 | |
Andy Shevchenko | 30b4b70 | 2015-01-07 16:56:55 +0200 | [diff] [blame] | 638 | spi_hw_init(&dws->master->dev, dws); |
Baruch Siach | ec37e8e | 2014-01-31 12:07:44 +0200 | [diff] [blame] | 639 | ret = spi_master_resume(dws->master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 640 | if (ret) |
| 641 | dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret); |
| 642 | return ret; |
| 643 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 644 | EXPORT_SYMBOL_GPL(dw_spi_resume_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 645 | |
| 646 | MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>"); |
| 647 | MODULE_DESCRIPTION("Driver for DesignWare SPI controller core"); |
| 648 | MODULE_LICENSE("GPL v2"); |