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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/dma-mapping.h> |
| 21 | #include <linux/interrupt.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 22 | #include <linux/module.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 23 | #include <linux/highmem.h> |
| 24 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 26 | #include <linux/spi/spi.h> |
| 27 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 28 | #include "spi-dw.h" |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 29 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 30 | #ifdef CONFIG_DEBUG_FS |
| 31 | #include <linux/debugfs.h> |
| 32 | #endif |
| 33 | |
| 34 | #define START_STATE ((void *)0) |
| 35 | #define RUNNING_STATE ((void *)1) |
| 36 | #define DONE_STATE ((void *)2) |
| 37 | #define ERROR_STATE ((void *)-1) |
| 38 | |
| 39 | #define QUEUE_RUNNING 0 |
| 40 | #define QUEUE_STOPPED 1 |
| 41 | |
| 42 | #define MRST_SPI_DEASSERT 0 |
| 43 | #define MRST_SPI_ASSERT 1 |
| 44 | |
| 45 | /* Slave spi_dev related */ |
| 46 | struct chip_data { |
| 47 | u16 cr0; |
| 48 | u8 cs; /* chip select pin */ |
| 49 | u8 n_bytes; /* current is a 1/2/4 byte op */ |
| 50 | u8 tmode; /* TR/TO/RO/EEPROM */ |
| 51 | u8 type; /* SPI/SSP/MicroWire */ |
| 52 | |
| 53 | u8 poll_mode; /* 1 means use poll mode */ |
| 54 | |
| 55 | u32 dma_width; |
| 56 | u32 rx_threshold; |
| 57 | u32 tx_threshold; |
| 58 | u8 enable_dma; |
| 59 | u8 bits_per_word; |
| 60 | u16 clk_div; /* baud rate divider */ |
| 61 | u32 speed_hz; /* baud rate */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 62 | void (*cs_control)(u32 command); |
| 63 | }; |
| 64 | |
| 65 | #ifdef CONFIG_DEBUG_FS |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 66 | #define SPI_REGS_BUFSIZE 1024 |
| 67 | static ssize_t spi_show_regs(struct file *file, char __user *user_buf, |
| 68 | size_t count, loff_t *ppos) |
| 69 | { |
| 70 | struct dw_spi *dws; |
| 71 | char *buf; |
| 72 | u32 len = 0; |
| 73 | ssize_t ret; |
| 74 | |
| 75 | dws = file->private_data; |
| 76 | |
| 77 | buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL); |
| 78 | if (!buf) |
| 79 | return 0; |
| 80 | |
| 81 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
| 82 | "MRST SPI0 registers:\n"); |
| 83 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
| 84 | "=================================\n"); |
| 85 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 86 | "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0)); |
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 | "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1)); |
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 | "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR)); |
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 | "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER)); |
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 | "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR)); |
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 | "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR)); |
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 | "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 99 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 100 | "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 101 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 102 | "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 103 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 104 | "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 105 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 106 | "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 107 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 108 | "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 109 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 110 | "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 111 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 112 | "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 113 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 114 | "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 115 | len += snprintf(buf + len, SPI_REGS_BUFSIZE - len, |
| 116 | "=================================\n"); |
| 117 | |
| 118 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 119 | kfree(buf); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static const struct file_operations mrst_spi_regs_ops = { |
| 124 | .owner = THIS_MODULE, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 125 | .open = simple_open, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 126 | .read = spi_show_regs, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 127 | .llseek = default_llseek, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static int mrst_spi_debugfs_init(struct dw_spi *dws) |
| 131 | { |
| 132 | dws->debugfs = debugfs_create_dir("mrst_spi", NULL); |
| 133 | if (!dws->debugfs) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | debugfs_create_file("registers", S_IFREG | S_IRUGO, |
| 137 | dws->debugfs, (void *)dws, &mrst_spi_regs_ops); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static void mrst_spi_debugfs_remove(struct dw_spi *dws) |
| 142 | { |
| 143 | if (dws->debugfs) |
| 144 | debugfs_remove_recursive(dws->debugfs); |
| 145 | } |
| 146 | |
| 147 | #else |
| 148 | static inline int mrst_spi_debugfs_init(struct dw_spi *dws) |
| 149 | { |
George Shore | 20a588f | 2010-01-21 11:40:49 +0000 | [diff] [blame] | 150 | return 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static inline void mrst_spi_debugfs_remove(struct dw_spi *dws) |
| 154 | { |
| 155 | } |
| 156 | #endif /* CONFIG_DEBUG_FS */ |
| 157 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 158 | /* Return the max entries we can fill into tx fifo */ |
| 159 | static inline u32 tx_max(struct dw_spi *dws) |
| 160 | { |
| 161 | u32 tx_left, tx_room, rxtx_gap; |
| 162 | |
| 163 | tx_left = (dws->tx_end - dws->tx) / dws->n_bytes; |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 164 | tx_room = dws->fifo_len - dw_readw(dws, DW_SPI_TXFLR); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Another concern is about the tx/rx mismatch, we |
| 168 | * though to use (dws->fifo_len - rxflr - txflr) as |
| 169 | * one maximum value for tx, but it doesn't cover the |
| 170 | * data which is out of tx/rx fifo and inside the |
| 171 | * shift registers. So a control from sw point of |
| 172 | * view is taken. |
| 173 | */ |
| 174 | rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx)) |
| 175 | / dws->n_bytes; |
| 176 | |
| 177 | return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap)); |
| 178 | } |
| 179 | |
| 180 | /* Return the max entries we should read out of rx fifo */ |
| 181 | static inline u32 rx_max(struct dw_spi *dws) |
| 182 | { |
| 183 | u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes; |
| 184 | |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 185 | return min(rx_left, (u32)dw_readw(dws, DW_SPI_RXFLR)); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 188 | static void dw_writer(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 189 | { |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 190 | u32 max = tx_max(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 191 | u16 txw = 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 192 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 193 | while (max--) { |
| 194 | /* Set the tx word if the transfer's original "tx" is not null */ |
| 195 | if (dws->tx_end - dws->len) { |
| 196 | if (dws->n_bytes == 1) |
| 197 | txw = *(u8 *)(dws->tx); |
| 198 | else |
| 199 | txw = *(u16 *)(dws->tx); |
| 200 | } |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 201 | dw_writew(dws, DW_SPI_DR, txw); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 202 | dws->tx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 203 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 206 | static void dw_reader(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 207 | { |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 208 | u32 max = rx_max(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 209 | u16 rxw; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 210 | |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 211 | while (max--) { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 212 | rxw = dw_readw(dws, DW_SPI_DR); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 213 | /* Care rx only if the transfer's original "rx" is not null */ |
| 214 | if (dws->rx_end - dws->len) { |
| 215 | if (dws->n_bytes == 1) |
| 216 | *(u8 *)(dws->rx) = rxw; |
| 217 | else |
| 218 | *(u16 *)(dws->rx) = rxw; |
| 219 | } |
| 220 | dws->rx += dws->n_bytes; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 221 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void *next_transfer(struct dw_spi *dws) |
| 225 | { |
| 226 | struct spi_message *msg = dws->cur_msg; |
| 227 | struct spi_transfer *trans = dws->cur_transfer; |
| 228 | |
| 229 | /* Move to next transfer */ |
| 230 | if (trans->transfer_list.next != &msg->transfers) { |
| 231 | dws->cur_transfer = |
| 232 | list_entry(trans->transfer_list.next, |
| 233 | struct spi_transfer, |
| 234 | transfer_list); |
| 235 | return RUNNING_STATE; |
| 236 | } else |
| 237 | return DONE_STATE; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Note: first step is the protocol driver prepares |
| 242 | * a dma-capable memory, and this func just need translate |
| 243 | * the virt addr to physical |
| 244 | */ |
| 245 | static int map_dma_buffers(struct dw_spi *dws) |
| 246 | { |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 247 | if (!dws->cur_msg->is_dma_mapped |
| 248 | || !dws->dma_inited |
| 249 | || !dws->cur_chip->enable_dma |
| 250 | || !dws->dma_ops) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 251 | return 0; |
| 252 | |
| 253 | if (dws->cur_transfer->tx_dma) |
| 254 | dws->tx_dma = dws->cur_transfer->tx_dma; |
| 255 | |
| 256 | if (dws->cur_transfer->rx_dma) |
| 257 | dws->rx_dma = dws->cur_transfer->rx_dma; |
| 258 | |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | /* Caller already set message->status; dma and pio irqs are blocked */ |
| 263 | static void giveback(struct dw_spi *dws) |
| 264 | { |
| 265 | struct spi_transfer *last_transfer; |
| 266 | unsigned long flags; |
| 267 | struct spi_message *msg; |
| 268 | |
| 269 | spin_lock_irqsave(&dws->lock, flags); |
| 270 | msg = dws->cur_msg; |
| 271 | dws->cur_msg = NULL; |
| 272 | dws->cur_transfer = NULL; |
| 273 | dws->prev_chip = dws->cur_chip; |
| 274 | dws->cur_chip = NULL; |
| 275 | dws->dma_mapped = 0; |
| 276 | queue_work(dws->workqueue, &dws->pump_messages); |
| 277 | spin_unlock_irqrestore(&dws->lock, flags); |
| 278 | |
| 279 | last_transfer = list_entry(msg->transfers.prev, |
| 280 | struct spi_transfer, |
| 281 | transfer_list); |
| 282 | |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 283 | if (!last_transfer->cs_change && dws->cs_control) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 284 | dws->cs_control(MRST_SPI_DEASSERT); |
| 285 | |
| 286 | msg->state = NULL; |
| 287 | if (msg->complete) |
| 288 | msg->complete(msg->context); |
| 289 | } |
| 290 | |
| 291 | static void int_error_stop(struct dw_spi *dws, const char *msg) |
| 292 | { |
Alek Du | 8a33a37 | 2011-03-30 23:09:53 +0800 | [diff] [blame] | 293 | /* Stop the hw */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 294 | spi_enable_chip(dws, 0); |
| 295 | |
| 296 | dev_err(&dws->master->dev, "%s\n", msg); |
| 297 | dws->cur_msg->state = ERROR_STATE; |
| 298 | tasklet_schedule(&dws->pump_transfers); |
| 299 | } |
| 300 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 301 | void dw_spi_xfer_done(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 302 | { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 303 | /* Update total byte transferred return count actual bytes read */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 304 | dws->cur_msg->actual_length += dws->len; |
| 305 | |
| 306 | /* Move to next transfer */ |
| 307 | dws->cur_msg->state = next_transfer(dws); |
| 308 | |
| 309 | /* Handle end of message */ |
| 310 | if (dws->cur_msg->state == DONE_STATE) { |
| 311 | dws->cur_msg->status = 0; |
| 312 | giveback(dws); |
| 313 | } else |
| 314 | tasklet_schedule(&dws->pump_transfers); |
| 315 | } |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(dw_spi_xfer_done); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 317 | |
| 318 | static irqreturn_t interrupt_transfer(struct dw_spi *dws) |
| 319 | { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 320 | u16 irq_status = dw_readw(dws, DW_SPI_ISR); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 321 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 322 | /* Error handling */ |
| 323 | 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] | 324 | dw_readw(dws, DW_SPI_TXOICR); |
| 325 | dw_readw(dws, DW_SPI_RXOICR); |
| 326 | dw_readw(dws, DW_SPI_RXUICR); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 327 | int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 328 | return IRQ_HANDLED; |
| 329 | } |
| 330 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 331 | dw_reader(dws); |
| 332 | if (dws->rx_end == dws->rx) { |
| 333 | spi_mask_intr(dws, SPI_INT_TXEI); |
| 334 | dw_spi_xfer_done(dws); |
| 335 | return IRQ_HANDLED; |
| 336 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 337 | if (irq_status & SPI_INT_TXEI) { |
| 338 | spi_mask_intr(dws, SPI_INT_TXEI); |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 339 | dw_writer(dws); |
| 340 | /* Enable TX irq always, it will be disabled when RX finished */ |
| 341 | spi_umask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 342 | } |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 343 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 344 | return IRQ_HANDLED; |
| 345 | } |
| 346 | |
| 347 | static irqreturn_t dw_spi_irq(int irq, void *dev_id) |
| 348 | { |
| 349 | struct dw_spi *dws = dev_id; |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 350 | u16 irq_status = dw_readw(dws, DW_SPI_ISR) & 0x3f; |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 351 | |
Yong Wang | cbcc062 | 2010-09-07 15:27:27 +0800 | [diff] [blame] | 352 | if (!irq_status) |
| 353 | return IRQ_NONE; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 354 | |
| 355 | if (!dws->cur_msg) { |
| 356 | spi_mask_intr(dws, SPI_INT_TXEI); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 357 | return IRQ_HANDLED; |
| 358 | } |
| 359 | |
| 360 | return dws->transfer_handler(dws); |
| 361 | } |
| 362 | |
| 363 | /* Must be called inside pump_transfers() */ |
| 364 | static void poll_transfer(struct dw_spi *dws) |
| 365 | { |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 366 | do { |
| 367 | dw_writer(dws); |
Feng Tang | de6efe0 | 2011-03-30 23:09:52 +0800 | [diff] [blame] | 368 | dw_reader(dws); |
Alek Du | 2ff271b | 2011-03-30 23:09:54 +0800 | [diff] [blame] | 369 | cpu_relax(); |
| 370 | } while (dws->rx_end > dws->rx); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 371 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 372 | dw_spi_xfer_done(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void pump_transfers(unsigned long data) |
| 376 | { |
| 377 | struct dw_spi *dws = (struct dw_spi *)data; |
| 378 | struct spi_message *message = NULL; |
| 379 | struct spi_transfer *transfer = NULL; |
| 380 | struct spi_transfer *previous = NULL; |
| 381 | struct spi_device *spi = NULL; |
| 382 | struct chip_data *chip = NULL; |
| 383 | u8 bits = 0; |
| 384 | u8 imask = 0; |
| 385 | u8 cs_change = 0; |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 386 | u16 txint_level = 0; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 387 | u16 clk_div = 0; |
| 388 | u32 speed = 0; |
| 389 | u32 cr0 = 0; |
| 390 | |
| 391 | /* Get current state information */ |
| 392 | message = dws->cur_msg; |
| 393 | transfer = dws->cur_transfer; |
| 394 | chip = dws->cur_chip; |
| 395 | spi = message->spi; |
| 396 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 397 | if (unlikely(!chip->clk_div)) |
| 398 | chip->clk_div = dws->max_freq / chip->speed_hz; |
| 399 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 400 | if (message->state == ERROR_STATE) { |
| 401 | message->status = -EIO; |
| 402 | goto early_exit; |
| 403 | } |
| 404 | |
| 405 | /* Handle end of message */ |
| 406 | if (message->state == DONE_STATE) { |
| 407 | message->status = 0; |
| 408 | goto early_exit; |
| 409 | } |
| 410 | |
| 411 | /* Delay if requested at end of transfer*/ |
| 412 | if (message->state == RUNNING_STATE) { |
| 413 | previous = list_entry(transfer->transfer_list.prev, |
| 414 | struct spi_transfer, |
| 415 | transfer_list); |
| 416 | if (previous->delay_usecs) |
| 417 | udelay(previous->delay_usecs); |
| 418 | } |
| 419 | |
| 420 | dws->n_bytes = chip->n_bytes; |
| 421 | dws->dma_width = chip->dma_width; |
| 422 | dws->cs_control = chip->cs_control; |
| 423 | |
| 424 | dws->rx_dma = transfer->rx_dma; |
| 425 | dws->tx_dma = transfer->tx_dma; |
| 426 | dws->tx = (void *)transfer->tx_buf; |
| 427 | dws->tx_end = dws->tx + transfer->len; |
| 428 | dws->rx = transfer->rx_buf; |
| 429 | dws->rx_end = dws->rx + transfer->len; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 430 | dws->len = dws->cur_transfer->len; |
| 431 | if (chip != dws->prev_chip) |
| 432 | cs_change = 1; |
| 433 | |
| 434 | cr0 = chip->cr0; |
| 435 | |
| 436 | /* Handle per transfer options for bpw and speed */ |
| 437 | if (transfer->speed_hz) { |
| 438 | speed = chip->speed_hz; |
| 439 | |
| 440 | if (transfer->speed_hz != speed) { |
| 441 | speed = transfer->speed_hz; |
| 442 | if (speed > dws->max_freq) { |
| 443 | printk(KERN_ERR "MRST SPI0: unsupported" |
| 444 | "freq: %dHz\n", speed); |
| 445 | message->status = -EIO; |
| 446 | goto early_exit; |
| 447 | } |
| 448 | |
| 449 | /* clk_div doesn't support odd number */ |
| 450 | clk_div = dws->max_freq / speed; |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 451 | clk_div = (clk_div + 1) & 0xfffe; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 452 | |
| 453 | chip->speed_hz = speed; |
| 454 | chip->clk_div = clk_div; |
| 455 | } |
| 456 | } |
| 457 | if (transfer->bits_per_word) { |
| 458 | bits = transfer->bits_per_word; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 459 | dws->n_bytes = dws->dma_width = bits >> 3; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 460 | cr0 = (bits - 1) |
| 461 | | (chip->type << SPI_FRF_OFFSET) |
| 462 | | (spi->mode << SPI_MODE_OFFSET) |
| 463 | | (chip->tmode << SPI_TMOD_OFFSET); |
| 464 | } |
| 465 | message->state = RUNNING_STATE; |
| 466 | |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 467 | /* |
| 468 | * Adjust transfer mode if necessary. Requires platform dependent |
| 469 | * chipselect mechanism. |
| 470 | */ |
| 471 | if (dws->cs_control) { |
| 472 | if (dws->rx && dws->tx) |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 473 | chip->tmode = SPI_TMOD_TR; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 474 | else if (dws->rx) |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 475 | chip->tmode = SPI_TMOD_RO; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 476 | else |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 477 | chip->tmode = SPI_TMOD_TO; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 478 | |
Feng Tang | e3e55ff | 2010-09-07 15:52:06 +0800 | [diff] [blame] | 479 | cr0 &= ~SPI_TMOD_MASK; |
George Shore | 052dc7c | 2010-01-21 11:40:52 +0000 | [diff] [blame] | 480 | cr0 |= (chip->tmode << SPI_TMOD_OFFSET); |
| 481 | } |
| 482 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 483 | /* Check if current transfer is a DMA transaction */ |
| 484 | dws->dma_mapped = map_dma_buffers(dws); |
| 485 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 486 | /* |
| 487 | * Interrupt mode |
| 488 | * we only need set the TXEI IRQ, as TX/RX always happen syncronizely |
| 489 | */ |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 490 | if (!dws->dma_mapped && !chip->poll_mode) { |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 491 | int templen = dws->len / dws->n_bytes; |
| 492 | txint_level = dws->fifo_len / 2; |
| 493 | txint_level = (templen > txint_level) ? txint_level : templen; |
| 494 | |
Alek Du | 3b8a4dd | 2011-03-30 23:09:55 +0800 | [diff] [blame] | 495 | imask |= SPI_INT_TXEI | SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 496 | dws->transfer_handler = interrupt_transfer; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Reprogram registers only if |
| 501 | * 1. chip select changes |
| 502 | * 2. clk_div is changed |
| 503 | * 3. control value changes |
| 504 | */ |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 505 | if (dw_readw(dws, DW_SPI_CTRL0) != cr0 || cs_change || clk_div || imask) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 506 | spi_enable_chip(dws, 0); |
| 507 | |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 508 | if (dw_readw(dws, DW_SPI_CTRL0) != cr0) |
| 509 | dw_writew(dws, DW_SPI_CTRL0, cr0); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 510 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 511 | spi_set_clk(dws, clk_div ? clk_div : chip->clk_div); |
| 512 | spi_chip_sel(dws, spi->chip_select); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 513 | |
Justin P. Mattock | 2f263d9 | 2010-12-30 15:07:51 -0800 | [diff] [blame] | 514 | /* Set the interrupt mask, for poll mode just disable all int */ |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 515 | spi_mask_intr(dws, 0xff); |
| 516 | if (imask) |
| 517 | spi_umask_intr(dws, imask); |
| 518 | if (txint_level) |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 519 | dw_writew(dws, DW_SPI_TXFLTR, txint_level); |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 520 | |
| 521 | spi_enable_chip(dws, 1); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 522 | if (cs_change) |
| 523 | dws->prev_chip = chip; |
| 524 | } |
| 525 | |
| 526 | if (dws->dma_mapped) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 527 | dws->dma_ops->dma_transfer(dws, cs_change); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 528 | |
| 529 | if (chip->poll_mode) |
| 530 | poll_transfer(dws); |
| 531 | |
| 532 | return; |
| 533 | |
| 534 | early_exit: |
| 535 | giveback(dws); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | static void pump_messages(struct work_struct *work) |
| 540 | { |
| 541 | struct dw_spi *dws = |
| 542 | container_of(work, struct dw_spi, pump_messages); |
| 543 | unsigned long flags; |
| 544 | |
| 545 | /* Lock queue and check for queue work */ |
| 546 | spin_lock_irqsave(&dws->lock, flags); |
| 547 | if (list_empty(&dws->queue) || dws->run == QUEUE_STOPPED) { |
| 548 | dws->busy = 0; |
| 549 | spin_unlock_irqrestore(&dws->lock, flags); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | /* Make sure we are not already running a message */ |
| 554 | if (dws->cur_msg) { |
| 555 | spin_unlock_irqrestore(&dws->lock, flags); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | /* Extract head of queue */ |
| 560 | dws->cur_msg = list_entry(dws->queue.next, struct spi_message, queue); |
| 561 | list_del_init(&dws->cur_msg->queue); |
| 562 | |
| 563 | /* Initial message state*/ |
| 564 | dws->cur_msg->state = START_STATE; |
| 565 | dws->cur_transfer = list_entry(dws->cur_msg->transfers.next, |
| 566 | struct spi_transfer, |
| 567 | transfer_list); |
| 568 | dws->cur_chip = spi_get_ctldata(dws->cur_msg->spi); |
| 569 | |
| 570 | /* Mark as busy and launch transfers */ |
| 571 | tasklet_schedule(&dws->pump_transfers); |
| 572 | |
| 573 | dws->busy = 1; |
| 574 | spin_unlock_irqrestore(&dws->lock, flags); |
| 575 | } |
| 576 | |
| 577 | /* spi_device use this to queue in their spi_msg */ |
| 578 | static int dw_spi_transfer(struct spi_device *spi, struct spi_message *msg) |
| 579 | { |
| 580 | struct dw_spi *dws = spi_master_get_devdata(spi->master); |
| 581 | unsigned long flags; |
| 582 | |
| 583 | spin_lock_irqsave(&dws->lock, flags); |
| 584 | |
| 585 | if (dws->run == QUEUE_STOPPED) { |
| 586 | spin_unlock_irqrestore(&dws->lock, flags); |
| 587 | return -ESHUTDOWN; |
| 588 | } |
| 589 | |
| 590 | msg->actual_length = 0; |
| 591 | msg->status = -EINPROGRESS; |
| 592 | msg->state = START_STATE; |
| 593 | |
| 594 | list_add_tail(&msg->queue, &dws->queue); |
| 595 | |
| 596 | if (dws->run == QUEUE_RUNNING && !dws->busy) { |
| 597 | |
| 598 | if (dws->cur_transfer || dws->cur_msg) |
| 599 | queue_work(dws->workqueue, |
| 600 | &dws->pump_messages); |
| 601 | else { |
| 602 | /* If no other data transaction in air, just go */ |
| 603 | spin_unlock_irqrestore(&dws->lock, flags); |
| 604 | pump_messages(&dws->pump_messages); |
| 605 | return 0; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | spin_unlock_irqrestore(&dws->lock, flags); |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* This may be called twice for each spi dev */ |
| 614 | static int dw_spi_setup(struct spi_device *spi) |
| 615 | { |
| 616 | struct dw_spi_chip *chip_info = NULL; |
| 617 | struct chip_data *chip; |
| 618 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 619 | /* Only alloc on first setup */ |
| 620 | chip = spi_get_ctldata(spi); |
| 621 | if (!chip) { |
| 622 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
| 623 | if (!chip) |
| 624 | return -ENOMEM; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Protocol drivers may change the chip settings, so... |
| 629 | * if chip_info exists, use it |
| 630 | */ |
| 631 | chip_info = spi->controller_data; |
| 632 | |
| 633 | /* chip_info doesn't always exist */ |
| 634 | if (chip_info) { |
| 635 | if (chip_info->cs_control) |
| 636 | chip->cs_control = chip_info->cs_control; |
| 637 | |
| 638 | chip->poll_mode = chip_info->poll_mode; |
| 639 | chip->type = chip_info->type; |
| 640 | |
| 641 | chip->rx_threshold = 0; |
| 642 | chip->tx_threshold = 0; |
| 643 | |
| 644 | chip->enable_dma = chip_info->enable_dma; |
| 645 | } |
| 646 | |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 647 | if (spi->bits_per_word == 8) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 648 | chip->n_bytes = 1; |
| 649 | chip->dma_width = 1; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 650 | } else if (spi->bits_per_word == 16) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 651 | chip->n_bytes = 2; |
| 652 | chip->dma_width = 2; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 653 | } |
| 654 | chip->bits_per_word = spi->bits_per_word; |
| 655 | |
Feng Tang | 552e450 | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 656 | if (!spi->max_speed_hz) { |
| 657 | dev_err(&spi->dev, "No max speed HZ parameter\n"); |
| 658 | return -EINVAL; |
| 659 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 660 | chip->speed_hz = spi->max_speed_hz; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 661 | |
| 662 | chip->tmode = 0; /* Tx & Rx */ |
| 663 | /* Default SPI mode is SCPOL = 0, SCPH = 0 */ |
| 664 | chip->cr0 = (chip->bits_per_word - 1) |
| 665 | | (chip->type << SPI_FRF_OFFSET) |
| 666 | | (spi->mode << SPI_MODE_OFFSET) |
| 667 | | (chip->tmode << SPI_TMOD_OFFSET); |
| 668 | |
| 669 | spi_set_ctldata(spi, chip); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static void dw_spi_cleanup(struct spi_device *spi) |
| 674 | { |
| 675 | struct chip_data *chip = spi_get_ctldata(spi); |
| 676 | kfree(chip); |
| 677 | } |
| 678 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 679 | static int init_queue(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 680 | { |
| 681 | INIT_LIST_HEAD(&dws->queue); |
| 682 | spin_lock_init(&dws->lock); |
| 683 | |
| 684 | dws->run = QUEUE_STOPPED; |
| 685 | dws->busy = 0; |
| 686 | |
| 687 | tasklet_init(&dws->pump_transfers, |
| 688 | pump_transfers, (unsigned long)dws); |
| 689 | |
| 690 | INIT_WORK(&dws->pump_messages, pump_messages); |
| 691 | dws->workqueue = create_singlethread_workqueue( |
| 692 | dev_name(dws->master->dev.parent)); |
| 693 | if (dws->workqueue == NULL) |
| 694 | return -EBUSY; |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int start_queue(struct dw_spi *dws) |
| 700 | { |
| 701 | unsigned long flags; |
| 702 | |
| 703 | spin_lock_irqsave(&dws->lock, flags); |
| 704 | |
| 705 | if (dws->run == QUEUE_RUNNING || dws->busy) { |
| 706 | spin_unlock_irqrestore(&dws->lock, flags); |
| 707 | return -EBUSY; |
| 708 | } |
| 709 | |
| 710 | dws->run = QUEUE_RUNNING; |
| 711 | dws->cur_msg = NULL; |
| 712 | dws->cur_transfer = NULL; |
| 713 | dws->cur_chip = NULL; |
| 714 | dws->prev_chip = NULL; |
| 715 | spin_unlock_irqrestore(&dws->lock, flags); |
| 716 | |
| 717 | queue_work(dws->workqueue, &dws->pump_messages); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int stop_queue(struct dw_spi *dws) |
| 723 | { |
| 724 | unsigned long flags; |
| 725 | unsigned limit = 50; |
| 726 | int status = 0; |
| 727 | |
| 728 | spin_lock_irqsave(&dws->lock, flags); |
| 729 | dws->run = QUEUE_STOPPED; |
Vasily Khoruzhick | 850a28e | 2011-04-06 17:49:15 +0300 | [diff] [blame] | 730 | while ((!list_empty(&dws->queue) || dws->busy) && limit--) { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 731 | spin_unlock_irqrestore(&dws->lock, flags); |
| 732 | msleep(10); |
| 733 | spin_lock_irqsave(&dws->lock, flags); |
| 734 | } |
| 735 | |
| 736 | if (!list_empty(&dws->queue) || dws->busy) |
| 737 | status = -EBUSY; |
| 738 | spin_unlock_irqrestore(&dws->lock, flags); |
| 739 | |
| 740 | return status; |
| 741 | } |
| 742 | |
| 743 | static int destroy_queue(struct dw_spi *dws) |
| 744 | { |
| 745 | int status; |
| 746 | |
| 747 | status = stop_queue(dws); |
| 748 | if (status != 0) |
| 749 | return status; |
| 750 | destroy_workqueue(dws->workqueue); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* Restart the controller, disable all interrupts, clean rx fifo */ |
| 755 | static void spi_hw_init(struct dw_spi *dws) |
| 756 | { |
| 757 | spi_enable_chip(dws, 0); |
| 758 | spi_mask_intr(dws, 0xff); |
| 759 | spi_enable_chip(dws, 1); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * Try to detect the FIFO depth if not set by interface driver, |
| 763 | * the depth could be from 2 to 256 from HW spec |
| 764 | */ |
| 765 | if (!dws->fifo_len) { |
| 766 | u32 fifo; |
| 767 | for (fifo = 2; fifo <= 257; fifo++) { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 768 | dw_writew(dws, DW_SPI_TXFLTR, fifo); |
| 769 | if (fifo != dw_readw(dws, DW_SPI_TXFLTR)) |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 770 | break; |
| 771 | } |
| 772 | |
| 773 | dws->fifo_len = (fifo == 257) ? 0 : fifo; |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 774 | dw_writew(dws, DW_SPI_TXFLTR, 0); |
Feng Tang | c587b6f | 2010-01-21 10:41:10 +0800 | [diff] [blame] | 775 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame^] | 778 | int dw_spi_add_host(struct device *dev, struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 779 | { |
| 780 | struct spi_master *master; |
| 781 | int ret; |
| 782 | |
| 783 | BUG_ON(dws == NULL); |
| 784 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame^] | 785 | master = spi_alloc_master(dev, 0); |
| 786 | if (!master) |
| 787 | return -ENOMEM; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 788 | |
| 789 | dws->master = master; |
| 790 | dws->type = SSI_MOTO_SPI; |
| 791 | dws->prev_chip = NULL; |
| 792 | dws->dma_inited = 0; |
| 793 | dws->dma_addr = (dma_addr_t)(dws->paddr + 0x60); |
Liu, ShuoX | 40bfff8 | 2011-07-08 14:24:31 +0800 | [diff] [blame] | 794 | snprintf(dws->name, sizeof(dws->name), "dw_spi%d", |
| 795 | dws->bus_num); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 796 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame^] | 797 | ret = devm_request_irq(dev, dws->irq, dw_spi_irq, IRQF_SHARED, |
Liu, ShuoX | 40bfff8 | 2011-07-08 14:24:31 +0800 | [diff] [blame] | 798 | dws->name, dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 799 | if (ret < 0) { |
| 800 | dev_err(&master->dev, "can not get IRQ\n"); |
| 801 | goto err_free_master; |
| 802 | } |
| 803 | |
| 804 | master->mode_bits = SPI_CPOL | SPI_CPHA; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 805 | 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] | 806 | master->bus_num = dws->bus_num; |
| 807 | master->num_chipselect = dws->num_cs; |
| 808 | master->cleanup = dw_spi_cleanup; |
| 809 | master->setup = dw_spi_setup; |
| 810 | master->transfer = dw_spi_transfer; |
| 811 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 812 | /* Basic HW init */ |
| 813 | spi_hw_init(dws); |
| 814 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 815 | if (dws->dma_ops && dws->dma_ops->dma_init) { |
| 816 | ret = dws->dma_ops->dma_init(dws); |
| 817 | if (ret) { |
| 818 | dev_warn(&master->dev, "DMA init failed\n"); |
| 819 | dws->dma_inited = 0; |
| 820 | } |
| 821 | } |
| 822 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 823 | /* Initial and start queue */ |
| 824 | ret = init_queue(dws); |
| 825 | if (ret) { |
| 826 | dev_err(&master->dev, "problem initializing queue\n"); |
| 827 | goto err_diable_hw; |
| 828 | } |
| 829 | ret = start_queue(dws); |
| 830 | if (ret) { |
| 831 | dev_err(&master->dev, "problem starting queue\n"); |
| 832 | goto err_diable_hw; |
| 833 | } |
| 834 | |
| 835 | spi_master_set_devdata(master, dws); |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame^] | 836 | ret = devm_spi_register_master(dev, master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 837 | if (ret) { |
| 838 | dev_err(&master->dev, "problem registering spi master\n"); |
| 839 | goto err_queue_alloc; |
| 840 | } |
| 841 | |
| 842 | mrst_spi_debugfs_init(dws); |
| 843 | return 0; |
| 844 | |
| 845 | err_queue_alloc: |
| 846 | destroy_queue(dws); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 847 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 848 | dws->dma_ops->dma_exit(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 849 | err_diable_hw: |
| 850 | spi_enable_chip(dws, 0); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 851 | err_free_master: |
| 852 | spi_master_put(master); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 853 | return ret; |
| 854 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 855 | EXPORT_SYMBOL_GPL(dw_spi_add_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 856 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 857 | void dw_spi_remove_host(struct dw_spi *dws) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 858 | { |
| 859 | int status = 0; |
| 860 | |
| 861 | if (!dws) |
| 862 | return; |
| 863 | mrst_spi_debugfs_remove(dws); |
| 864 | |
| 865 | /* Remove the queue */ |
| 866 | status = destroy_queue(dws); |
| 867 | if (status != 0) |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 868 | dev_err(&dws->master->dev, |
| 869 | "dw_spi_remove: workqueue will not complete, message memory not freed\n"); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 870 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 871 | if (dws->dma_ops && dws->dma_ops->dma_exit) |
| 872 | dws->dma_ops->dma_exit(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 873 | spi_enable_chip(dws, 0); |
| 874 | /* Disable clk */ |
| 875 | spi_set_clk(dws, 0); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 876 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 877 | EXPORT_SYMBOL_GPL(dw_spi_remove_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 878 | |
| 879 | int dw_spi_suspend_host(struct dw_spi *dws) |
| 880 | { |
| 881 | int ret = 0; |
| 882 | |
| 883 | ret = stop_queue(dws); |
| 884 | if (ret) |
| 885 | return ret; |
| 886 | spi_enable_chip(dws, 0); |
| 887 | spi_set_clk(dws, 0); |
| 888 | return ret; |
| 889 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 890 | EXPORT_SYMBOL_GPL(dw_spi_suspend_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 891 | |
| 892 | int dw_spi_resume_host(struct dw_spi *dws) |
| 893 | { |
| 894 | int ret; |
| 895 | |
| 896 | spi_hw_init(dws); |
| 897 | ret = start_queue(dws); |
| 898 | if (ret) |
| 899 | dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret); |
| 900 | return ret; |
| 901 | } |
Feng Tang | 79290a2 | 2010-12-24 13:59:10 +0800 | [diff] [blame] | 902 | EXPORT_SYMBOL_GPL(dw_spi_resume_host); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 903 | |
| 904 | MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>"); |
| 905 | MODULE_DESCRIPTION("Driver for DesignWare SPI controller core"); |
| 906 | MODULE_LICENSE("GPL v2"); |