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