Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OMAP2 McSPI controller driver |
| 3 | * |
| 4 | * Copyright (C) 2005, 2006 Nokia Corporation |
| 5 | * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and |
Charulatha V | 1a5d819 | 2011-02-02 17:52:14 +0530 | [diff] [blame] | 6 | * Juha Yrj�l� <juha.yrjola@nokia.com> |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/dma-mapping.h> |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 31 | #include <linux/dmaengine.h> |
| 32 | #include <linux/omap-dma.h> |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/clk.h> |
| 36 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 38 | #include <linux/pm_runtime.h> |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 39 | #include <linux/of.h> |
| 40 | #include <linux/of_device.h> |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 41 | #include <linux/gcd.h> |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 42 | |
| 43 | #include <linux/spi/spi.h> |
| 44 | |
Arnd Bergmann | 2203747 | 2012-08-24 15:21:06 +0200 | [diff] [blame] | 45 | #include <linux/platform_data/spi-omap2-mcspi.h> |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 46 | |
| 47 | #define OMAP2_MCSPI_MAX_FREQ 48000000 |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 48 | #define OMAP2_MCSPI_MAX_FIFODEPTH 64 |
| 49 | #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF |
Shubhrajyoti D | 27b5284 | 2012-03-26 17:04:22 +0530 | [diff] [blame] | 50 | #define SPI_AUTOSUSPEND_TIMEOUT 2000 |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 51 | |
| 52 | #define OMAP2_MCSPI_REVISION 0x00 |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 53 | #define OMAP2_MCSPI_SYSSTATUS 0x14 |
| 54 | #define OMAP2_MCSPI_IRQSTATUS 0x18 |
| 55 | #define OMAP2_MCSPI_IRQENABLE 0x1c |
| 56 | #define OMAP2_MCSPI_WAKEUPENABLE 0x20 |
| 57 | #define OMAP2_MCSPI_SYST 0x24 |
| 58 | #define OMAP2_MCSPI_MODULCTRL 0x28 |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 59 | #define OMAP2_MCSPI_XFERLEVEL 0x7c |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 60 | |
| 61 | /* per-channel banks, 0x14 bytes each, first is: */ |
| 62 | #define OMAP2_MCSPI_CHCONF0 0x2c |
| 63 | #define OMAP2_MCSPI_CHSTAT0 0x30 |
| 64 | #define OMAP2_MCSPI_CHCTRL0 0x34 |
| 65 | #define OMAP2_MCSPI_TX0 0x38 |
| 66 | #define OMAP2_MCSPI_RX0 0x3c |
| 67 | |
| 68 | /* per-register bitmasks: */ |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 69 | #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 70 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 71 | #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0) |
| 72 | #define OMAP2_MCSPI_MODULCTRL_MS BIT(2) |
| 73 | #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 74 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 75 | #define OMAP2_MCSPI_CHCONF_PHA BIT(0) |
| 76 | #define OMAP2_MCSPI_CHCONF_POL BIT(1) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 77 | #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2) |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 78 | #define OMAP2_MCSPI_CHCONF_EPOL BIT(6) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 79 | #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7) |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 80 | #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12) |
| 81 | #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 82 | #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12) |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 83 | #define OMAP2_MCSPI_CHCONF_DMAW BIT(14) |
| 84 | #define OMAP2_MCSPI_CHCONF_DMAR BIT(15) |
| 85 | #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16) |
| 86 | #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17) |
| 87 | #define OMAP2_MCSPI_CHCONF_IS BIT(18) |
| 88 | #define OMAP2_MCSPI_CHCONF_TURBO BIT(19) |
| 89 | #define OMAP2_MCSPI_CHCONF_FORCE BIT(20) |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 90 | #define OMAP2_MCSPI_CHCONF_FFET BIT(27) |
| 91 | #define OMAP2_MCSPI_CHCONF_FFER BIT(28) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 92 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 93 | #define OMAP2_MCSPI_CHSTAT_RXS BIT(0) |
| 94 | #define OMAP2_MCSPI_CHSTAT_TXS BIT(1) |
| 95 | #define OMAP2_MCSPI_CHSTAT_EOT BIT(2) |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 96 | #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 97 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 98 | #define OMAP2_MCSPI_CHCTRL_EN BIT(0) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 99 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 100 | #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 101 | |
| 102 | /* We have 2 DMA channels per CS, one for RX and one for TX */ |
| 103 | struct omap2_mcspi_dma { |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 104 | struct dma_chan *dma_tx; |
| 105 | struct dma_chan *dma_rx; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 106 | |
| 107 | int dma_tx_sync_dev; |
| 108 | int dma_rx_sync_dev; |
| 109 | |
| 110 | struct completion dma_tx_completion; |
| 111 | struct completion dma_rx_completion; |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 112 | |
| 113 | char dma_rx_ch_name[14]; |
| 114 | char dma_tx_ch_name[14]; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* use PIO for small transfers, avoiding DMA setup/teardown overhead and |
| 118 | * cache operations; better heuristics consider wordsize and bitrate. |
| 119 | */ |
Roman Tereshonkov | 8b66c13 | 2010-04-12 09:07:54 +0000 | [diff] [blame] | 120 | #define DMA_MIN_BYTES 160 |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 121 | |
| 122 | |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 123 | /* |
| 124 | * Used for context save and restore, structure members to be updated whenever |
| 125 | * corresponding registers are modified. |
| 126 | */ |
| 127 | struct omap2_mcspi_regs { |
| 128 | u32 modulctrl; |
| 129 | u32 wakeupenable; |
| 130 | struct list_head cs; |
| 131 | }; |
| 132 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 133 | struct omap2_mcspi { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 134 | struct spi_master *master; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 135 | /* Virtual base address of the controller */ |
| 136 | void __iomem *base; |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 137 | unsigned long phys; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 138 | /* SPI1 has 4 channels, while SPI2 has 2 */ |
| 139 | struct omap2_mcspi_dma *dma_channels; |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 140 | struct device *dev; |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 141 | struct omap2_mcspi_regs ctx; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 142 | int fifo_depth; |
Daniel Mack | 0384e90 | 2012-10-07 18:19:44 +0200 | [diff] [blame] | 143 | unsigned int pin_dir:1; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | struct omap2_mcspi_cs { |
| 147 | void __iomem *base; |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 148 | unsigned long phys; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 149 | int word_len; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 150 | struct list_head node; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 151 | /* Context save and restore shadow register */ |
| 152 | u32 chconf0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 155 | static inline void mcspi_write_reg(struct spi_master *master, |
| 156 | int idx, u32 val) |
| 157 | { |
| 158 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
| 159 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 160 | writel_relaxed(val, mcspi->base + idx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static inline u32 mcspi_read_reg(struct spi_master *master, int idx) |
| 164 | { |
| 165 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
| 166 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 167 | return readl_relaxed(mcspi->base + idx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static inline void mcspi_write_cs_reg(const struct spi_device *spi, |
| 171 | int idx, u32 val) |
| 172 | { |
| 173 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 174 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 175 | writel_relaxed(val, cs->base + idx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx) |
| 179 | { |
| 180 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 181 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 182 | return readl_relaxed(cs->base + idx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 185 | static inline u32 mcspi_cached_chconf0(const struct spi_device *spi) |
| 186 | { |
| 187 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 188 | |
| 189 | return cs->chconf0; |
| 190 | } |
| 191 | |
| 192 | static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val) |
| 193 | { |
| 194 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 195 | |
| 196 | cs->chconf0 = val; |
| 197 | mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val); |
Roman Tereshonkov | a330ce2 | 2010-03-15 09:06:28 +0000 | [diff] [blame] | 198 | mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Illia Smyrnov | 56cd5c1 | 2013-06-14 19:12:07 +0300 | [diff] [blame] | 201 | static inline int mcspi_bytes_per_word(int word_len) |
| 202 | { |
| 203 | if (word_len <= 8) |
| 204 | return 1; |
| 205 | else if (word_len <= 16) |
| 206 | return 2; |
| 207 | else /* word_len <= 32 */ |
| 208 | return 4; |
| 209 | } |
| 210 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 211 | static void omap2_mcspi_set_dma_req(const struct spi_device *spi, |
| 212 | int is_read, int enable) |
| 213 | { |
| 214 | u32 l, rw; |
| 215 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 216 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 217 | |
| 218 | if (is_read) /* 1 is read, 0 write */ |
| 219 | rw = OMAP2_MCSPI_CHCONF_DMAR; |
| 220 | else |
| 221 | rw = OMAP2_MCSPI_CHCONF_DMAW; |
| 222 | |
Shubhrajyoti D | af4e944 | 2012-08-22 11:35:13 +0530 | [diff] [blame] | 223 | if (enable) |
| 224 | l |= rw; |
| 225 | else |
| 226 | l &= ~rw; |
| 227 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 228 | mcspi_write_chconf0(spi, l); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable) |
| 232 | { |
| 233 | u32 l; |
| 234 | |
| 235 | l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0; |
| 236 | mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 237 | /* Flash post-writes */ |
| 238 | mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active) |
| 242 | { |
| 243 | u32 l; |
| 244 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 245 | l = mcspi_cached_chconf0(spi); |
Shubhrajyoti D | af4e944 | 2012-08-22 11:35:13 +0530 | [diff] [blame] | 246 | if (cs_active) |
| 247 | l |= OMAP2_MCSPI_CHCONF_FORCE; |
| 248 | else |
| 249 | l &= ~OMAP2_MCSPI_CHCONF_FORCE; |
| 250 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 251 | mcspi_write_chconf0(spi, l); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void omap2_mcspi_set_master_mode(struct spi_master *master) |
| 255 | { |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 256 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
| 257 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 258 | u32 l; |
| 259 | |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 260 | /* |
| 261 | * Setup when switching from (reset default) slave mode |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 262 | * to single-channel master mode |
| 263 | */ |
| 264 | l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL); |
Shubhrajyoti D | af4e944 | 2012-08-22 11:35:13 +0530 | [diff] [blame] | 265 | l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS); |
| 266 | l |= OMAP2_MCSPI_MODULCTRL_SINGLE; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 267 | mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 268 | |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 269 | ctx->modulctrl = l; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 272 | static void omap2_mcspi_set_fifo(const struct spi_device *spi, |
| 273 | struct spi_transfer *t, int enable) |
| 274 | { |
| 275 | struct spi_master *master = spi->master; |
| 276 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 277 | struct omap2_mcspi *mcspi; |
| 278 | unsigned int wcnt; |
Illia Smyrnov | 5db542e | 2013-10-09 15:05:08 +0300 | [diff] [blame] | 279 | int max_fifo_depth, fifo_depth, bytes_per_word; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 280 | u32 chconf, xferlevel; |
| 281 | |
| 282 | mcspi = spi_master_get_devdata(master); |
| 283 | |
| 284 | chconf = mcspi_cached_chconf0(spi); |
| 285 | if (enable) { |
| 286 | bytes_per_word = mcspi_bytes_per_word(cs->word_len); |
| 287 | if (t->len % bytes_per_word != 0) |
| 288 | goto disable_fifo; |
| 289 | |
Illia Smyrnov | 5db542e | 2013-10-09 15:05:08 +0300 | [diff] [blame] | 290 | if (t->rx_buf != NULL && t->tx_buf != NULL) |
| 291 | max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2; |
| 292 | else |
| 293 | max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH; |
| 294 | |
| 295 | fifo_depth = gcd(t->len, max_fifo_depth); |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 296 | if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0) |
| 297 | goto disable_fifo; |
| 298 | |
| 299 | wcnt = t->len / bytes_per_word; |
| 300 | if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT) |
| 301 | goto disable_fifo; |
| 302 | |
| 303 | xferlevel = wcnt << 16; |
| 304 | if (t->rx_buf != NULL) { |
| 305 | chconf |= OMAP2_MCSPI_CHCONF_FFER; |
| 306 | xferlevel |= (fifo_depth - 1) << 8; |
Illia Smyrnov | 5db542e | 2013-10-09 15:05:08 +0300 | [diff] [blame] | 307 | } |
| 308 | if (t->tx_buf != NULL) { |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 309 | chconf |= OMAP2_MCSPI_CHCONF_FFET; |
| 310 | xferlevel |= fifo_depth - 1; |
| 311 | } |
| 312 | |
| 313 | mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel); |
| 314 | mcspi_write_chconf0(spi, chconf); |
| 315 | mcspi->fifo_depth = fifo_depth; |
| 316 | |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | disable_fifo: |
| 321 | if (t->rx_buf != NULL) |
| 322 | chconf &= ~OMAP2_MCSPI_CHCONF_FFER; |
| 323 | else |
| 324 | chconf &= ~OMAP2_MCSPI_CHCONF_FFET; |
| 325 | |
| 326 | mcspi_write_chconf0(spi, chconf); |
| 327 | mcspi->fifo_depth = 0; |
| 328 | } |
| 329 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 330 | static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi) |
| 331 | { |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 332 | struct spi_master *spi_cntrl = mcspi->master; |
| 333 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
| 334 | struct omap2_mcspi_cs *cs; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 335 | |
| 336 | /* McSPI: context restore */ |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 337 | mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl); |
| 338 | mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 339 | |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 340 | list_for_each_entry(cs, &ctx->cs, node) |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 341 | writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 342 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 343 | |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 344 | static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit) |
| 345 | { |
| 346 | unsigned long timeout; |
| 347 | |
| 348 | timeout = jiffies + msecs_to_jiffies(1000); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 349 | while (!(readl_relaxed(reg) & bit)) { |
Sebastian Andrzej Siewior | ff23fa3 | 2013-03-21 13:22:48 +0100 | [diff] [blame] | 350 | if (time_after(jiffies, timeout)) { |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 351 | if (!(readl_relaxed(reg) & bit)) |
Sebastian Andrzej Siewior | ff23fa3 | 2013-03-21 13:22:48 +0100 | [diff] [blame] | 352 | return -ETIMEDOUT; |
| 353 | else |
| 354 | return 0; |
| 355 | } |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 356 | cpu_relax(); |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 361 | static void omap2_mcspi_rx_callback(void *data) |
| 362 | { |
| 363 | struct spi_device *spi = data; |
| 364 | struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master); |
| 365 | struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 366 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 367 | /* We must disable the DMA RX request */ |
| 368 | omap2_mcspi_set_dma_req(spi, 1, 0); |
Felipe Balbi | 830379e | 2012-12-12 10:45:59 +0200 | [diff] [blame] | 369 | |
| 370 | complete(&mcspi_dma->dma_rx_completion); |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static void omap2_mcspi_tx_callback(void *data) |
| 374 | { |
| 375 | struct spi_device *spi = data; |
| 376 | struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master); |
| 377 | struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 378 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 379 | /* We must disable the DMA TX request */ |
| 380 | omap2_mcspi_set_dma_req(spi, 0, 0); |
Felipe Balbi | 830379e | 2012-12-12 10:45:59 +0200 | [diff] [blame] | 381 | |
| 382 | complete(&mcspi_dma->dma_tx_completion); |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 385 | static void omap2_mcspi_tx_dma(struct spi_device *spi, |
| 386 | struct spi_transfer *xfer, |
| 387 | struct dma_slave_config cfg) |
| 388 | { |
| 389 | struct omap2_mcspi *mcspi; |
| 390 | struct omap2_mcspi_dma *mcspi_dma; |
| 391 | unsigned int count; |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 392 | |
| 393 | mcspi = spi_master_get_devdata(spi->master); |
| 394 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 395 | count = xfer->len; |
| 396 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 397 | if (mcspi_dma->dma_tx) { |
| 398 | struct dma_async_tx_descriptor *tx; |
| 399 | struct scatterlist sg; |
| 400 | |
| 401 | dmaengine_slave_config(mcspi_dma->dma_tx, &cfg); |
| 402 | |
| 403 | sg_init_table(&sg, 1); |
| 404 | sg_dma_address(&sg) = xfer->tx_dma; |
| 405 | sg_dma_len(&sg) = xfer->len; |
| 406 | |
| 407 | tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1, |
| 408 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 409 | if (tx) { |
| 410 | tx->callback = omap2_mcspi_tx_callback; |
| 411 | tx->callback_param = spi; |
| 412 | dmaengine_submit(tx); |
| 413 | } else { |
| 414 | /* FIXME: fall back to PIO? */ |
| 415 | } |
| 416 | } |
| 417 | dma_async_issue_pending(mcspi_dma->dma_tx); |
| 418 | omap2_mcspi_set_dma_req(spi, 0, 1); |
| 419 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static unsigned |
| 423 | omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer, |
| 424 | struct dma_slave_config cfg, |
| 425 | unsigned es) |
| 426 | { |
| 427 | struct omap2_mcspi *mcspi; |
| 428 | struct omap2_mcspi_dma *mcspi_dma; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 429 | unsigned int count, dma_count; |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 430 | u32 l; |
| 431 | int elements = 0; |
| 432 | int word_len, element_count; |
| 433 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 434 | mcspi = spi_master_get_devdata(spi->master); |
| 435 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 436 | count = xfer->len; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 437 | dma_count = xfer->len; |
| 438 | |
| 439 | if (mcspi->fifo_depth == 0) |
| 440 | dma_count -= es; |
| 441 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 442 | word_len = cs->word_len; |
| 443 | l = mcspi_cached_chconf0(spi); |
| 444 | |
| 445 | if (word_len <= 8) |
| 446 | element_count = count; |
| 447 | else if (word_len <= 16) |
| 448 | element_count = count >> 1; |
| 449 | else /* word_len <= 32 */ |
| 450 | element_count = count >> 2; |
| 451 | |
| 452 | if (mcspi_dma->dma_rx) { |
| 453 | struct dma_async_tx_descriptor *tx; |
| 454 | struct scatterlist sg; |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 455 | |
| 456 | dmaengine_slave_config(mcspi_dma->dma_rx, &cfg); |
| 457 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 458 | if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0) |
| 459 | dma_count -= es; |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 460 | |
| 461 | sg_init_table(&sg, 1); |
| 462 | sg_dma_address(&sg) = xfer->rx_dma; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 463 | sg_dma_len(&sg) = dma_count; |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 464 | |
| 465 | tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1, |
| 466 | DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | |
| 467 | DMA_CTRL_ACK); |
| 468 | if (tx) { |
| 469 | tx->callback = omap2_mcspi_rx_callback; |
| 470 | tx->callback_param = spi; |
| 471 | dmaengine_submit(tx); |
| 472 | } else { |
| 473 | /* FIXME: fall back to PIO? */ |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | dma_async_issue_pending(mcspi_dma->dma_rx); |
| 478 | omap2_mcspi_set_dma_req(spi, 1, 1); |
| 479 | |
| 480 | wait_for_completion(&mcspi_dma->dma_rx_completion); |
| 481 | dma_unmap_single(mcspi->dev, xfer->rx_dma, count, |
| 482 | DMA_FROM_DEVICE); |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 483 | |
| 484 | if (mcspi->fifo_depth > 0) |
| 485 | return count; |
| 486 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 487 | omap2_mcspi_set_enable(spi, 0); |
| 488 | |
| 489 | elements = element_count - 1; |
| 490 | |
| 491 | if (l & OMAP2_MCSPI_CHCONF_TURBO) { |
| 492 | elements--; |
| 493 | |
| 494 | if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0) |
| 495 | & OMAP2_MCSPI_CHSTAT_RXS)) { |
| 496 | u32 w; |
| 497 | |
| 498 | w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0); |
| 499 | if (word_len <= 8) |
| 500 | ((u8 *)xfer->rx_buf)[elements++] = w; |
| 501 | else if (word_len <= 16) |
| 502 | ((u16 *)xfer->rx_buf)[elements++] = w; |
| 503 | else /* word_len <= 32 */ |
| 504 | ((u32 *)xfer->rx_buf)[elements++] = w; |
| 505 | } else { |
Illia Smyrnov | 56cd5c1 | 2013-06-14 19:12:07 +0300 | [diff] [blame] | 506 | int bytes_per_word = mcspi_bytes_per_word(word_len); |
Jarkko Nikula | a1829d2 | 2013-10-11 13:53:59 +0300 | [diff] [blame] | 507 | dev_err(&spi->dev, "DMA RX penultimate word empty\n"); |
Illia Smyrnov | 56cd5c1 | 2013-06-14 19:12:07 +0300 | [diff] [blame] | 508 | count -= (bytes_per_word << 1); |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 509 | omap2_mcspi_set_enable(spi, 1); |
| 510 | return count; |
| 511 | } |
| 512 | } |
| 513 | if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0) |
| 514 | & OMAP2_MCSPI_CHSTAT_RXS)) { |
| 515 | u32 w; |
| 516 | |
| 517 | w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0); |
| 518 | if (word_len <= 8) |
| 519 | ((u8 *)xfer->rx_buf)[elements] = w; |
| 520 | else if (word_len <= 16) |
| 521 | ((u16 *)xfer->rx_buf)[elements] = w; |
| 522 | else /* word_len <= 32 */ |
| 523 | ((u32 *)xfer->rx_buf)[elements] = w; |
| 524 | } else { |
Jarkko Nikula | a1829d2 | 2013-10-11 13:53:59 +0300 | [diff] [blame] | 525 | dev_err(&spi->dev, "DMA RX last word empty\n"); |
Illia Smyrnov | 56cd5c1 | 2013-06-14 19:12:07 +0300 | [diff] [blame] | 526 | count -= mcspi_bytes_per_word(word_len); |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 527 | } |
| 528 | omap2_mcspi_set_enable(spi, 1); |
| 529 | return count; |
| 530 | } |
| 531 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 532 | static unsigned |
| 533 | omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer) |
| 534 | { |
| 535 | struct omap2_mcspi *mcspi; |
| 536 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 537 | struct omap2_mcspi_dma *mcspi_dma; |
Russell King | 8c7494a | 2012-04-23 13:56:25 +0100 | [diff] [blame] | 538 | unsigned int count; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 539 | u32 l; |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 540 | u8 *rx; |
| 541 | const u8 *tx; |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 542 | struct dma_slave_config cfg; |
| 543 | enum dma_slave_buswidth width; |
| 544 | unsigned es; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 545 | u32 burst; |
Shubhrajyoti D | e47a682 | 2012-11-06 14:30:19 +0530 | [diff] [blame] | 546 | void __iomem *chstat_reg; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 547 | void __iomem *irqstat_reg; |
| 548 | int wait_res; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 549 | |
| 550 | mcspi = spi_master_get_devdata(spi->master); |
| 551 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 552 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 553 | |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 554 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 555 | if (cs->word_len <= 8) { |
| 556 | width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 557 | es = 1; |
| 558 | } else if (cs->word_len <= 16) { |
| 559 | width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 560 | es = 2; |
| 561 | } else { |
| 562 | width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 563 | es = 4; |
| 564 | } |
| 565 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 566 | count = xfer->len; |
| 567 | burst = 1; |
| 568 | |
| 569 | if (mcspi->fifo_depth > 0) { |
| 570 | if (count > mcspi->fifo_depth) |
| 571 | burst = mcspi->fifo_depth / es; |
| 572 | else |
| 573 | burst = count / es; |
| 574 | } |
| 575 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 576 | memset(&cfg, 0, sizeof(cfg)); |
| 577 | cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0; |
| 578 | cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0; |
| 579 | cfg.src_addr_width = width; |
| 580 | cfg.dst_addr_width = width; |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 581 | cfg.src_maxburst = burst; |
| 582 | cfg.dst_maxburst = burst; |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 583 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 584 | rx = xfer->rx_buf; |
| 585 | tx = xfer->tx_buf; |
| 586 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 587 | if (tx != NULL) |
| 588 | omap2_mcspi_tx_dma(spi, xfer, cfg); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 589 | |
Shubhrajyoti D | d7b4394 | 2012-09-11 12:13:20 +0530 | [diff] [blame] | 590 | if (rx != NULL) |
Shubhrajyoti D | e47a682 | 2012-11-06 14:30:19 +0530 | [diff] [blame] | 591 | count = omap2_mcspi_rx_dma(spi, xfer, cfg, es); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 592 | |
Shubhrajyoti D | e47a682 | 2012-11-06 14:30:19 +0530 | [diff] [blame] | 593 | if (tx != NULL) { |
Shubhrajyoti D | e47a682 | 2012-11-06 14:30:19 +0530 | [diff] [blame] | 594 | wait_for_completion(&mcspi_dma->dma_tx_completion); |
| 595 | dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len, |
| 596 | DMA_TO_DEVICE); |
| 597 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 598 | if (mcspi->fifo_depth > 0) { |
| 599 | irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS; |
| 600 | |
| 601 | if (mcspi_wait_for_reg_bit(irqstat_reg, |
| 602 | OMAP2_MCSPI_IRQSTATUS_EOW) < 0) |
| 603 | dev_err(&spi->dev, "EOW timed out\n"); |
| 604 | |
| 605 | mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS, |
| 606 | OMAP2_MCSPI_IRQSTATUS_EOW); |
| 607 | } |
| 608 | |
Shubhrajyoti D | e47a682 | 2012-11-06 14:30:19 +0530 | [diff] [blame] | 609 | /* for TX_ONLY mode, be sure all words have shifted out */ |
| 610 | if (rx == NULL) { |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 611 | chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0; |
| 612 | if (mcspi->fifo_depth > 0) { |
| 613 | wait_res = mcspi_wait_for_reg_bit(chstat_reg, |
| 614 | OMAP2_MCSPI_CHSTAT_TXFFE); |
| 615 | if (wait_res < 0) |
| 616 | dev_err(&spi->dev, "TXFFE timed out\n"); |
| 617 | } else { |
| 618 | wait_res = mcspi_wait_for_reg_bit(chstat_reg, |
| 619 | OMAP2_MCSPI_CHSTAT_TXS); |
| 620 | if (wait_res < 0) |
| 621 | dev_err(&spi->dev, "TXS timed out\n"); |
| 622 | } |
| 623 | if (wait_res >= 0 && |
| 624 | (mcspi_wait_for_reg_bit(chstat_reg, |
| 625 | OMAP2_MCSPI_CHSTAT_EOT) < 0)) |
Shubhrajyoti D | e47a682 | 2012-11-06 14:30:19 +0530 | [diff] [blame] | 626 | dev_err(&spi->dev, "EOT timed out\n"); |
| 627 | } |
| 628 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 629 | return count; |
| 630 | } |
| 631 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 632 | static unsigned |
| 633 | omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer) |
| 634 | { |
| 635 | struct omap2_mcspi *mcspi; |
| 636 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 637 | unsigned int count, c; |
| 638 | u32 l; |
| 639 | void __iomem *base = cs->base; |
| 640 | void __iomem *tx_reg; |
| 641 | void __iomem *rx_reg; |
| 642 | void __iomem *chstat_reg; |
| 643 | int word_len; |
| 644 | |
| 645 | mcspi = spi_master_get_devdata(spi->master); |
| 646 | count = xfer->len; |
| 647 | c = count; |
| 648 | word_len = cs->word_len; |
| 649 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 650 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 651 | |
| 652 | /* We store the pre-calculated register addresses on stack to speed |
| 653 | * up the transfer loop. */ |
| 654 | tx_reg = base + OMAP2_MCSPI_TX0; |
| 655 | rx_reg = base + OMAP2_MCSPI_RX0; |
| 656 | chstat_reg = base + OMAP2_MCSPI_CHSTAT0; |
| 657 | |
Michael Jones | adef658 | 2011-02-25 16:55:11 +0100 | [diff] [blame] | 658 | if (c < (word_len>>3)) |
| 659 | return 0; |
| 660 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 661 | if (word_len <= 8) { |
| 662 | u8 *rx; |
| 663 | const u8 *tx; |
| 664 | |
| 665 | rx = xfer->rx_buf; |
| 666 | tx = xfer->tx_buf; |
| 667 | |
| 668 | do { |
Kalle Valo | feed9ba | 2008-01-24 14:00:40 -0800 | [diff] [blame] | 669 | c -= 1; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 670 | if (tx != NULL) { |
| 671 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 672 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 673 | dev_err(&spi->dev, "TXS timed out\n"); |
| 674 | goto out; |
| 675 | } |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 676 | dev_vdbg(&spi->dev, "write-%d %02x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 677 | word_len, *tx); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 678 | writel_relaxed(*tx++, tx_reg); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 679 | } |
| 680 | if (rx != NULL) { |
| 681 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 682 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 683 | dev_err(&spi->dev, "RXS timed out\n"); |
| 684 | goto out; |
| 685 | } |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 686 | |
| 687 | if (c == 1 && tx == NULL && |
| 688 | (l & OMAP2_MCSPI_CHCONF_TURBO)) { |
| 689 | omap2_mcspi_set_enable(spi, 0); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 690 | *rx++ = readl_relaxed(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 691 | dev_vdbg(&spi->dev, "read-%d %02x\n", |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 692 | word_len, *(rx - 1)); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 693 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 694 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 695 | dev_err(&spi->dev, |
| 696 | "RXS timed out\n"); |
| 697 | goto out; |
| 698 | } |
| 699 | c = 0; |
| 700 | } else if (c == 0 && tx == NULL) { |
| 701 | omap2_mcspi_set_enable(spi, 0); |
| 702 | } |
| 703 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 704 | *rx++ = readl_relaxed(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 705 | dev_vdbg(&spi->dev, "read-%d %02x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 706 | word_len, *(rx - 1)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 707 | } |
Jarkko Nikula | 95c5c3a | 2011-03-21 16:27:30 +0200 | [diff] [blame] | 708 | } while (c); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 709 | } else if (word_len <= 16) { |
| 710 | u16 *rx; |
| 711 | const u16 *tx; |
| 712 | |
| 713 | rx = xfer->rx_buf; |
| 714 | tx = xfer->tx_buf; |
| 715 | do { |
Kalle Valo | feed9ba | 2008-01-24 14:00:40 -0800 | [diff] [blame] | 716 | c -= 2; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 717 | if (tx != NULL) { |
| 718 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 719 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 720 | dev_err(&spi->dev, "TXS timed out\n"); |
| 721 | goto out; |
| 722 | } |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 723 | dev_vdbg(&spi->dev, "write-%d %04x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 724 | word_len, *tx); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 725 | writel_relaxed(*tx++, tx_reg); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 726 | } |
| 727 | if (rx != NULL) { |
| 728 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 729 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 730 | dev_err(&spi->dev, "RXS timed out\n"); |
| 731 | goto out; |
| 732 | } |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 733 | |
| 734 | if (c == 2 && tx == NULL && |
| 735 | (l & OMAP2_MCSPI_CHCONF_TURBO)) { |
| 736 | omap2_mcspi_set_enable(spi, 0); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 737 | *rx++ = readl_relaxed(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 738 | dev_vdbg(&spi->dev, "read-%d %04x\n", |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 739 | word_len, *(rx - 1)); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 740 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 741 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 742 | dev_err(&spi->dev, |
| 743 | "RXS timed out\n"); |
| 744 | goto out; |
| 745 | } |
| 746 | c = 0; |
| 747 | } else if (c == 0 && tx == NULL) { |
| 748 | omap2_mcspi_set_enable(spi, 0); |
| 749 | } |
| 750 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 751 | *rx++ = readl_relaxed(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 752 | dev_vdbg(&spi->dev, "read-%d %04x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 753 | word_len, *(rx - 1)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 754 | } |
Jarkko Nikula | 95c5c3a | 2011-03-21 16:27:30 +0200 | [diff] [blame] | 755 | } while (c >= 2); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 756 | } else if (word_len <= 32) { |
| 757 | u32 *rx; |
| 758 | const u32 *tx; |
| 759 | |
| 760 | rx = xfer->rx_buf; |
| 761 | tx = xfer->tx_buf; |
| 762 | do { |
Kalle Valo | feed9ba | 2008-01-24 14:00:40 -0800 | [diff] [blame] | 763 | c -= 4; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 764 | if (tx != NULL) { |
| 765 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 766 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 767 | dev_err(&spi->dev, "TXS timed out\n"); |
| 768 | goto out; |
| 769 | } |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 770 | dev_vdbg(&spi->dev, "write-%d %08x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 771 | word_len, *tx); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 772 | writel_relaxed(*tx++, tx_reg); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 773 | } |
| 774 | if (rx != NULL) { |
| 775 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 776 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 777 | dev_err(&spi->dev, "RXS timed out\n"); |
| 778 | goto out; |
| 779 | } |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 780 | |
| 781 | if (c == 4 && tx == NULL && |
| 782 | (l & OMAP2_MCSPI_CHCONF_TURBO)) { |
| 783 | omap2_mcspi_set_enable(spi, 0); |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 784 | *rx++ = readl_relaxed(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 785 | dev_vdbg(&spi->dev, "read-%d %08x\n", |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 786 | word_len, *(rx - 1)); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 787 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 788 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 789 | dev_err(&spi->dev, |
| 790 | "RXS timed out\n"); |
| 791 | goto out; |
| 792 | } |
| 793 | c = 0; |
| 794 | } else if (c == 0 && tx == NULL) { |
| 795 | omap2_mcspi_set_enable(spi, 0); |
| 796 | } |
| 797 | |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 798 | *rx++ = readl_relaxed(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 799 | dev_vdbg(&spi->dev, "read-%d %08x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 800 | word_len, *(rx - 1)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 801 | } |
Jarkko Nikula | 95c5c3a | 2011-03-21 16:27:30 +0200 | [diff] [blame] | 802 | } while (c >= 4); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* for TX_ONLY mode, be sure all words have shifted out */ |
| 806 | if (xfer->rx_buf == NULL) { |
| 807 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 808 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 809 | dev_err(&spi->dev, "TXS timed out\n"); |
| 810 | } else if (mcspi_wait_for_reg_bit(chstat_reg, |
| 811 | OMAP2_MCSPI_CHSTAT_EOT) < 0) |
| 812 | dev_err(&spi->dev, "EOT timed out\n"); |
Jason Wang | e1993ed | 2010-10-19 18:03:27 +0800 | [diff] [blame] | 813 | |
| 814 | /* disable chan to purge rx datas received in TX_ONLY transfer, |
| 815 | * otherwise these rx datas will affect the direct following |
| 816 | * RX_ONLY transfer. |
| 817 | */ |
| 818 | omap2_mcspi_set_enable(spi, 0); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 819 | } |
| 820 | out: |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 821 | omap2_mcspi_set_enable(spi, 1); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 822 | return count - c; |
| 823 | } |
| 824 | |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 825 | static u32 omap2_mcspi_calc_divisor(u32 speed_hz) |
| 826 | { |
| 827 | u32 div; |
| 828 | |
| 829 | for (div = 0; div < 15; div++) |
| 830 | if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div)) |
| 831 | return div; |
| 832 | |
| 833 | return 15; |
| 834 | } |
| 835 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 836 | /* called only when no transfer is active to this device */ |
| 837 | static int omap2_mcspi_setup_transfer(struct spi_device *spi, |
| 838 | struct spi_transfer *t) |
| 839 | { |
| 840 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 841 | struct omap2_mcspi *mcspi; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 842 | struct spi_master *spi_cntrl; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 843 | u32 l = 0, div = 0; |
| 844 | u8 word_len = spi->bits_per_word; |
Scott Ellis | 9bd4517 | 2010-03-10 14:23:13 -0700 | [diff] [blame] | 845 | u32 speed_hz = spi->max_speed_hz; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 846 | |
| 847 | mcspi = spi_master_get_devdata(spi->master); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 848 | spi_cntrl = mcspi->master; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 849 | |
| 850 | if (t != NULL && t->bits_per_word) |
| 851 | word_len = t->bits_per_word; |
| 852 | |
| 853 | cs->word_len = word_len; |
| 854 | |
Scott Ellis | 9bd4517 | 2010-03-10 14:23:13 -0700 | [diff] [blame] | 855 | if (t && t->speed_hz) |
| 856 | speed_hz = t->speed_hz; |
| 857 | |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 858 | speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ); |
| 859 | div = omap2_mcspi_calc_divisor(speed_hz); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 860 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 861 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 862 | |
| 863 | /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS |
| 864 | * REVISIT: this controller could support SPI_3WIRE mode. |
| 865 | */ |
Daniel Mack | 2cd4517 | 2012-11-14 11:14:26 +0800 | [diff] [blame] | 866 | if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) { |
Daniel Mack | 0384e90 | 2012-10-07 18:19:44 +0200 | [diff] [blame] | 867 | l &= ~OMAP2_MCSPI_CHCONF_IS; |
| 868 | l &= ~OMAP2_MCSPI_CHCONF_DPE1; |
| 869 | l |= OMAP2_MCSPI_CHCONF_DPE0; |
| 870 | } else { |
| 871 | l |= OMAP2_MCSPI_CHCONF_IS; |
| 872 | l |= OMAP2_MCSPI_CHCONF_DPE1; |
| 873 | l &= ~OMAP2_MCSPI_CHCONF_DPE0; |
| 874 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 875 | |
| 876 | /* wordlength */ |
| 877 | l &= ~OMAP2_MCSPI_CHCONF_WL_MASK; |
| 878 | l |= (word_len - 1) << 7; |
| 879 | |
| 880 | /* set chipselect polarity; manage with FORCE */ |
| 881 | if (!(spi->mode & SPI_CS_HIGH)) |
| 882 | l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */ |
| 883 | else |
| 884 | l &= ~OMAP2_MCSPI_CHCONF_EPOL; |
| 885 | |
| 886 | /* set clock divisor */ |
| 887 | l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK; |
| 888 | l |= div << 2; |
| 889 | |
| 890 | /* set SPI mode 0..3 */ |
| 891 | if (spi->mode & SPI_CPOL) |
| 892 | l |= OMAP2_MCSPI_CHCONF_POL; |
| 893 | else |
| 894 | l &= ~OMAP2_MCSPI_CHCONF_POL; |
| 895 | if (spi->mode & SPI_CPHA) |
| 896 | l |= OMAP2_MCSPI_CHCONF_PHA; |
| 897 | else |
| 898 | l &= ~OMAP2_MCSPI_CHCONF_PHA; |
| 899 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 900 | mcspi_write_chconf0(spi, l); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 901 | |
| 902 | dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n", |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 903 | OMAP2_MCSPI_MAX_FREQ >> div, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 904 | (spi->mode & SPI_CPHA) ? "trailing" : "leading", |
| 905 | (spi->mode & SPI_CPOL) ? "inverted" : "normal"); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 910 | /* |
| 911 | * Note that we currently allow DMA only if we get a channel |
| 912 | * for both rx and tx. Otherwise we'll do PIO for both rx and tx. |
| 913 | */ |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 914 | static int omap2_mcspi_request_dma(struct spi_device *spi) |
| 915 | { |
| 916 | struct spi_master *master = spi->master; |
| 917 | struct omap2_mcspi *mcspi; |
| 918 | struct omap2_mcspi_dma *mcspi_dma; |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 919 | dma_cap_mask_t mask; |
| 920 | unsigned sig; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 921 | |
| 922 | mcspi = spi_master_get_devdata(master); |
| 923 | mcspi_dma = mcspi->dma_channels + spi->chip_select; |
| 924 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 925 | init_completion(&mcspi_dma->dma_rx_completion); |
| 926 | init_completion(&mcspi_dma->dma_tx_completion); |
| 927 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 928 | dma_cap_zero(mask); |
| 929 | dma_cap_set(DMA_SLAVE, mask); |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 930 | sig = mcspi_dma->dma_rx_sync_dev; |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 931 | |
| 932 | mcspi_dma->dma_rx = |
| 933 | dma_request_slave_channel_compat(mask, omap_dma_filter_fn, |
| 934 | &sig, &master->dev, |
| 935 | mcspi_dma->dma_rx_ch_name); |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 936 | if (!mcspi_dma->dma_rx) |
| 937 | goto no_dma; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 938 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 939 | sig = mcspi_dma->dma_tx_sync_dev; |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 940 | mcspi_dma->dma_tx = |
| 941 | dma_request_slave_channel_compat(mask, omap_dma_filter_fn, |
| 942 | &sig, &master->dev, |
| 943 | mcspi_dma->dma_tx_ch_name); |
| 944 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 945 | if (!mcspi_dma->dma_tx) { |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 946 | dma_release_channel(mcspi_dma->dma_rx); |
| 947 | mcspi_dma->dma_rx = NULL; |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 948 | goto no_dma; |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 949 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 950 | |
| 951 | return 0; |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 952 | |
| 953 | no_dma: |
| 954 | dev_warn(&spi->dev, "not using DMA for McSPI\n"); |
| 955 | return -EAGAIN; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 958 | static int omap2_mcspi_setup(struct spi_device *spi) |
| 959 | { |
| 960 | int ret; |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 961 | struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master); |
| 962 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 963 | struct omap2_mcspi_dma *mcspi_dma; |
| 964 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 965 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 966 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 967 | |
| 968 | if (!cs) { |
Russell King | 10aa5a3 | 2012-06-18 11:27:04 +0100 | [diff] [blame] | 969 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 970 | if (!cs) |
| 971 | return -ENOMEM; |
| 972 | cs->base = mcspi->base + spi->chip_select * 0x14; |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 973 | cs->phys = mcspi->phys + spi->chip_select * 0x14; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 974 | cs->chconf0 = 0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 975 | spi->controller_state = cs; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 976 | /* Link this to context save list */ |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 977 | list_add_tail(&cs->node, &ctx->cs); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Russell King | 8c7494a | 2012-04-23 13:56:25 +0100 | [diff] [blame] | 980 | if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 981 | ret = omap2_mcspi_request_dma(spi); |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 982 | if (ret < 0 && ret != -EAGAIN) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 983 | return ret; |
| 984 | } |
| 985 | |
Shubhrajyoti D | 034d3dc | 2012-08-22 11:35:12 +0530 | [diff] [blame] | 986 | ret = pm_runtime_get_sync(mcspi->dev); |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 987 | if (ret < 0) |
| 988 | return ret; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 989 | |
Kyungmin Park | 86eeb6f | 2007-10-16 01:27:45 -0700 | [diff] [blame] | 990 | ret = omap2_mcspi_setup_transfer(spi, NULL); |
Shubhrajyoti D | 034d3dc | 2012-08-22 11:35:12 +0530 | [diff] [blame] | 991 | pm_runtime_mark_last_busy(mcspi->dev); |
| 992 | pm_runtime_put_autosuspend(mcspi->dev); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 993 | |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | static void omap2_mcspi_cleanup(struct spi_device *spi) |
| 998 | { |
| 999 | struct omap2_mcspi *mcspi; |
| 1000 | struct omap2_mcspi_dma *mcspi_dma; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 1001 | struct omap2_mcspi_cs *cs; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1002 | |
| 1003 | mcspi = spi_master_get_devdata(spi->master); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1004 | |
Scott Ellis | 5e77494 | 2010-03-10 14:22:45 -0700 | [diff] [blame] | 1005 | if (spi->controller_state) { |
| 1006 | /* Unlink controller state from context save list */ |
| 1007 | cs = spi->controller_state; |
| 1008 | list_del(&cs->node); |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 1009 | |
Russell King | 10aa5a3 | 2012-06-18 11:27:04 +0100 | [diff] [blame] | 1010 | kfree(cs); |
Scott Ellis | 5e77494 | 2010-03-10 14:22:45 -0700 | [diff] [blame] | 1011 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1012 | |
Scott Ellis | 99f1a43 | 2010-05-24 14:20:27 +0000 | [diff] [blame] | 1013 | if (spi->chip_select < spi->master->num_chipselect) { |
| 1014 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 1015 | |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 1016 | if (mcspi_dma->dma_rx) { |
| 1017 | dma_release_channel(mcspi_dma->dma_rx); |
| 1018 | mcspi_dma->dma_rx = NULL; |
Scott Ellis | 99f1a43 | 2010-05-24 14:20:27 +0000 | [diff] [blame] | 1019 | } |
Russell King | 53741ed | 2012-04-23 13:51:48 +0100 | [diff] [blame] | 1020 | if (mcspi_dma->dma_tx) { |
| 1021 | dma_release_channel(mcspi_dma->dma_tx); |
| 1022 | mcspi_dma->dma_tx = NULL; |
Scott Ellis | 99f1a43 | 2010-05-24 14:20:27 +0000 | [diff] [blame] | 1023 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1027 | static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1028 | { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1029 | |
| 1030 | /* We only enable one channel at a time -- the one whose message is |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1031 | * -- although this controller would gladly |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1032 | * arbitrate among multiple channels. This corresponds to "single |
| 1033 | * channel" master mode. As a side effect, we need to manage the |
| 1034 | * chipselect with the FORCE bit ... CS != channel enable. |
| 1035 | */ |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1036 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1037 | struct spi_device *spi; |
| 1038 | struct spi_transfer *t = NULL; |
Matthias Brugger | 5cbc7ca | 2013-01-24 13:40:41 +0100 | [diff] [blame] | 1039 | struct spi_master *master; |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1040 | struct omap2_mcspi_dma *mcspi_dma; |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1041 | int cs_active = 0; |
| 1042 | struct omap2_mcspi_cs *cs; |
| 1043 | struct omap2_mcspi_device_config *cd; |
| 1044 | int par_override = 0; |
| 1045 | int status = 0; |
| 1046 | u32 chconf; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1047 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1048 | spi = m->spi; |
Matthias Brugger | 5cbc7ca | 2013-01-24 13:40:41 +0100 | [diff] [blame] | 1049 | master = spi->master; |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1050 | mcspi_dma = mcspi->dma_channels + spi->chip_select; |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1051 | cs = spi->controller_state; |
| 1052 | cd = spi->controller_data; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1053 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 1054 | omap2_mcspi_set_enable(spi, 0); |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1055 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 1056 | if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) { |
| 1057 | status = -EINVAL; |
| 1058 | break; |
| 1059 | } |
| 1060 | if (par_override || t->speed_hz || t->bits_per_word) { |
| 1061 | par_override = 1; |
| 1062 | status = omap2_mcspi_setup_transfer(spi, t); |
| 1063 | if (status < 0) |
| 1064 | break; |
| 1065 | if (!t->speed_hz && !t->bits_per_word) |
| 1066 | par_override = 0; |
| 1067 | } |
Matthias Brugger | 5cbc7ca | 2013-01-24 13:40:41 +0100 | [diff] [blame] | 1068 | if (cd && cd->cs_per_word) { |
| 1069 | chconf = mcspi->ctx.modulctrl; |
| 1070 | chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE; |
| 1071 | mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf); |
| 1072 | mcspi->ctx.modulctrl = |
| 1073 | mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL); |
| 1074 | } |
| 1075 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1076 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1077 | if (!cs_active) { |
| 1078 | omap2_mcspi_force_cs(spi, 1); |
| 1079 | cs_active = 1; |
| 1080 | } |
| 1081 | |
| 1082 | chconf = mcspi_cached_chconf0(spi); |
| 1083 | chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK; |
| 1084 | chconf &= ~OMAP2_MCSPI_CHCONF_TURBO; |
| 1085 | |
| 1086 | if (t->tx_buf == NULL) |
| 1087 | chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY; |
| 1088 | else if (t->rx_buf == NULL) |
| 1089 | chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY; |
| 1090 | |
| 1091 | if (cd && cd->turbo_mode && t->tx_buf == NULL) { |
| 1092 | /* Turbo mode is for more than one word */ |
| 1093 | if (t->len > ((cs->word_len + 7) >> 3)) |
| 1094 | chconf |= OMAP2_MCSPI_CHCONF_TURBO; |
| 1095 | } |
| 1096 | |
| 1097 | mcspi_write_chconf0(spi, chconf); |
| 1098 | |
| 1099 | if (t->len) { |
| 1100 | unsigned count; |
| 1101 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 1102 | if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) && |
| 1103 | (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)) |
| 1104 | omap2_mcspi_set_fifo(spi, t, 1); |
| 1105 | |
| 1106 | omap2_mcspi_set_enable(spi, 1); |
| 1107 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1108 | /* RX_ONLY mode needs dummy data in TX reg */ |
| 1109 | if (t->tx_buf == NULL) |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 1110 | writel_relaxed(0, cs->base |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1111 | + OMAP2_MCSPI_TX0); |
| 1112 | |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1113 | if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) && |
| 1114 | (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)) |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1115 | count = omap2_mcspi_txrx_dma(spi, t); |
| 1116 | else |
| 1117 | count = omap2_mcspi_txrx_pio(spi, t); |
| 1118 | m->actual_length += count; |
| 1119 | |
| 1120 | if (count != t->len) { |
| 1121 | status = -EIO; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1122 | break; |
| 1123 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1126 | if (t->delay_usecs) |
| 1127 | udelay(t->delay_usecs); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1128 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1129 | /* ignore the "leave it on after last xfer" hint */ |
| 1130 | if (t->cs_change) { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1131 | omap2_mcspi_force_cs(spi, 0); |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1132 | cs_active = 0; |
| 1133 | } |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 1134 | |
| 1135 | omap2_mcspi_set_enable(spi, 0); |
| 1136 | |
| 1137 | if (mcspi->fifo_depth > 0) |
| 1138 | omap2_mcspi_set_fifo(spi, t, 0); |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1139 | } |
| 1140 | /* Restore defaults if they were overriden */ |
| 1141 | if (par_override) { |
| 1142 | par_override = 0; |
| 1143 | status = omap2_mcspi_setup_transfer(spi, NULL); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1146 | if (cs_active) |
| 1147 | omap2_mcspi_force_cs(spi, 0); |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1148 | |
Matthias Brugger | 5cbc7ca | 2013-01-24 13:40:41 +0100 | [diff] [blame] | 1149 | if (cd && cd->cs_per_word) { |
| 1150 | chconf = mcspi->ctx.modulctrl; |
| 1151 | chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE; |
| 1152 | mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf); |
| 1153 | mcspi->ctx.modulctrl = |
| 1154 | mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL); |
| 1155 | } |
| 1156 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1157 | omap2_mcspi_set_enable(spi, 0); |
| 1158 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 1159 | if (mcspi->fifo_depth > 0 && t) |
| 1160 | omap2_mcspi_set_fifo(spi, t, 0); |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1161 | |
Illia Smyrnov | d33f473 | 2013-06-17 16:31:06 +0300 | [diff] [blame] | 1162 | m->status = status; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1165 | static int omap2_mcspi_transfer_one_message(struct spi_master *master, |
Matthias Brugger | 18dd619 | 2013-01-24 13:28:58 +0100 | [diff] [blame] | 1166 | struct spi_message *m) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1167 | { |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1168 | struct spi_device *spi; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1169 | struct omap2_mcspi *mcspi; |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1170 | struct omap2_mcspi_dma *mcspi_dma; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1171 | struct spi_transfer *t; |
| 1172 | |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1173 | spi = m->spi; |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1174 | mcspi = spi_master_get_devdata(master); |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1175 | mcspi_dma = mcspi->dma_channels + spi->chip_select; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1176 | m->actual_length = 0; |
| 1177 | m->status = 0; |
| 1178 | |
| 1179 | /* reject invalid messages and transfers */ |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1180 | if (list_empty(&m->transfers)) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1181 | return -EINVAL; |
| 1182 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 1183 | const void *tx_buf = t->tx_buf; |
| 1184 | void *rx_buf = t->rx_buf; |
| 1185 | unsigned len = t->len; |
| 1186 | |
| 1187 | if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1188 | || (len && !(rx_buf || tx_buf))) { |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1189 | dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1190 | t->speed_hz, |
| 1191 | len, |
| 1192 | tx_buf ? "tx" : "", |
| 1193 | rx_buf ? "rx" : "", |
| 1194 | t->bits_per_word); |
| 1195 | return -EINVAL; |
| 1196 | } |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 1197 | if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) { |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1198 | dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n", |
Matthias Brugger | 18dd619 | 2013-01-24 13:28:58 +0100 | [diff] [blame] | 1199 | t->speed_hz, |
| 1200 | OMAP2_MCSPI_MAX_FREQ >> 15); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1201 | return -EINVAL; |
| 1202 | } |
| 1203 | |
| 1204 | if (m->is_dma_mapped || len < DMA_MIN_BYTES) |
| 1205 | continue; |
| 1206 | |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1207 | if (mcspi_dma->dma_tx && tx_buf != NULL) { |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1208 | t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1209 | len, DMA_TO_DEVICE); |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1210 | if (dma_mapping_error(mcspi->dev, t->tx_dma)) { |
| 1211 | dev_dbg(mcspi->dev, "dma %cX %d bytes error\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1212 | 'T', len); |
| 1213 | return -EINVAL; |
| 1214 | } |
| 1215 | } |
Tony Lindgren | ddc5cdf | 2013-04-12 17:25:07 -0700 | [diff] [blame] | 1216 | if (mcspi_dma->dma_rx && rx_buf != NULL) { |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1217 | t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1218 | DMA_FROM_DEVICE); |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1219 | if (dma_mapping_error(mcspi->dev, t->rx_dma)) { |
| 1220 | dev_dbg(mcspi->dev, "dma %cX %d bytes error\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1221 | 'R', len); |
| 1222 | if (tx_buf != NULL) |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1223 | dma_unmap_single(mcspi->dev, t->tx_dma, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1224 | len, DMA_TO_DEVICE); |
| 1225 | return -EINVAL; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1230 | omap2_mcspi_work(mcspi, m); |
| 1231 | spi_finalize_current_message(master); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1232 | return 0; |
| 1233 | } |
| 1234 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1235 | static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1236 | { |
| 1237 | struct spi_master *master = mcspi->master; |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 1238 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 1239 | int ret = 0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1240 | |
Shubhrajyoti D | 034d3dc | 2012-08-22 11:35:12 +0530 | [diff] [blame] | 1241 | ret = pm_runtime_get_sync(mcspi->dev); |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1242 | if (ret < 0) |
| 1243 | return ret; |
Jouni Hogander | ddb2219 | 2009-07-29 15:02:11 -0700 | [diff] [blame] | 1244 | |
Shubhrajyoti D | 39f8052 | 2012-03-29 22:11:07 +0530 | [diff] [blame] | 1245 | mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, |
Matthias Brugger | 18dd619 | 2013-01-24 13:28:58 +0100 | [diff] [blame] | 1246 | OMAP2_MCSPI_WAKEUPENABLE_WKEN); |
Shubhrajyoti D | 39f8052 | 2012-03-29 22:11:07 +0530 | [diff] [blame] | 1247 | ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1248 | |
| 1249 | omap2_mcspi_set_master_mode(master); |
Shubhrajyoti D | 034d3dc | 2012-08-22 11:35:12 +0530 | [diff] [blame] | 1250 | pm_runtime_mark_last_busy(mcspi->dev); |
| 1251 | pm_runtime_put_autosuspend(mcspi->dev); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1255 | static int omap_mcspi_runtime_resume(struct device *dev) |
| 1256 | { |
| 1257 | struct omap2_mcspi *mcspi; |
| 1258 | struct spi_master *master; |
| 1259 | |
| 1260 | master = dev_get_drvdata(dev); |
| 1261 | mcspi = spi_master_get_devdata(master); |
| 1262 | omap2_mcspi_restore_ctx(mcspi); |
| 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1267 | static struct omap2_mcspi_platform_config omap2_pdata = { |
| 1268 | .regs_offset = 0, |
| 1269 | }; |
| 1270 | |
| 1271 | static struct omap2_mcspi_platform_config omap4_pdata = { |
| 1272 | .regs_offset = OMAP4_MCSPI_REG_OFFSET, |
| 1273 | }; |
| 1274 | |
| 1275 | static const struct of_device_id omap_mcspi_of_match[] = { |
| 1276 | { |
| 1277 | .compatible = "ti,omap2-mcspi", |
| 1278 | .data = &omap2_pdata, |
| 1279 | }, |
| 1280 | { |
| 1281 | .compatible = "ti,omap4-mcspi", |
| 1282 | .data = &omap4_pdata, |
| 1283 | }, |
| 1284 | { }, |
| 1285 | }; |
| 1286 | MODULE_DEVICE_TABLE(of, omap_mcspi_of_match); |
Girish | ccc7bae | 2008-02-06 01:38:16 -0800 | [diff] [blame] | 1287 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1288 | static int omap2_mcspi_probe(struct platform_device *pdev) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1289 | { |
| 1290 | struct spi_master *master; |
Uwe Kleine-König | 83a01e7 | 2012-05-21 21:57:39 +0200 | [diff] [blame] | 1291 | const struct omap2_mcspi_platform_config *pdata; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1292 | struct omap2_mcspi *mcspi; |
| 1293 | struct resource *r; |
| 1294 | int status = 0, i; |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1295 | u32 regs_offset = 0; |
| 1296 | static int bus_num = 1; |
| 1297 | struct device_node *node = pdev->dev.of_node; |
| 1298 | const struct of_device_id *match; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1299 | |
| 1300 | master = spi_alloc_master(&pdev->dev, sizeof *mcspi); |
| 1301 | if (master == NULL) { |
| 1302 | dev_dbg(&pdev->dev, "master allocation failed\n"); |
| 1303 | return -ENOMEM; |
| 1304 | } |
| 1305 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1306 | /* the spi->mode bits understood by this driver: */ |
| 1307 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1308 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1309 | master->setup = omap2_mcspi_setup; |
Mark Brown | f0278a1 | 2013-07-28 15:34:37 +0100 | [diff] [blame] | 1310 | master->auto_runtime_pm = true; |
Shubhrajyoti D | 5fda88f | 2012-05-10 18:27:45 +0530 | [diff] [blame] | 1311 | master->transfer_one_message = omap2_mcspi_transfer_one_message; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1312 | master->cleanup = omap2_mcspi_cleanup; |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1313 | master->dev.of_node = node; |
| 1314 | |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 1315 | platform_set_drvdata(pdev, master); |
Daniel Mack | 0384e90 | 2012-10-07 18:19:44 +0200 | [diff] [blame] | 1316 | |
| 1317 | mcspi = spi_master_get_devdata(master); |
| 1318 | mcspi->master = master; |
| 1319 | |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1320 | match = of_match_device(omap_mcspi_of_match, &pdev->dev); |
| 1321 | if (match) { |
| 1322 | u32 num_cs = 1; /* default number of chipselect */ |
| 1323 | pdata = match->data; |
| 1324 | |
| 1325 | of_property_read_u32(node, "ti,spi-num-cs", &num_cs); |
| 1326 | master->num_chipselect = num_cs; |
| 1327 | master->bus_num = bus_num++; |
Daniel Mack | 2cd4517 | 2012-11-14 11:14:26 +0800 | [diff] [blame] | 1328 | if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL)) |
| 1329 | mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN; |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1330 | } else { |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 1331 | pdata = dev_get_platdata(&pdev->dev); |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1332 | master->num_chipselect = pdata->num_cs; |
| 1333 | if (pdev->id != -1) |
| 1334 | master->bus_num = pdev->id; |
Daniel Mack | 0384e90 | 2012-10-07 18:19:44 +0200 | [diff] [blame] | 1335 | mcspi->pin_dir = pdata->pin_dir; |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1336 | } |
| 1337 | regs_offset = pdata->regs_offset; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1338 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1339 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1340 | if (r == NULL) { |
| 1341 | status = -ENODEV; |
Shubhrajyoti D | 39f1b56 | 2011-10-28 17:14:19 +0530 | [diff] [blame] | 1342 | goto free_master; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1343 | } |
Shubhrajyoti D | 1458d16 | 2011-10-24 15:54:24 +0530 | [diff] [blame] | 1344 | |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1345 | r->start += regs_offset; |
| 1346 | r->end += regs_offset; |
Shubhrajyoti D | 1458d16 | 2011-10-24 15:54:24 +0530 | [diff] [blame] | 1347 | mcspi->phys = r->start; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1348 | |
Thierry Reding | b0ee560 | 2013-01-21 11:09:18 +0100 | [diff] [blame] | 1349 | mcspi->base = devm_ioremap_resource(&pdev->dev, r); |
| 1350 | if (IS_ERR(mcspi->base)) { |
| 1351 | status = PTR_ERR(mcspi->base); |
Shubhrajyoti D | 1a77b12 | 2012-03-17 12:44:01 +0530 | [diff] [blame] | 1352 | goto free_master; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 1353 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1354 | |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1355 | mcspi->dev = &pdev->dev; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1356 | |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 1357 | INIT_LIST_HEAD(&mcspi->ctx.cs); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1358 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1359 | mcspi->dma_channels = kcalloc(master->num_chipselect, |
| 1360 | sizeof(struct omap2_mcspi_dma), |
| 1361 | GFP_KERNEL); |
| 1362 | |
| 1363 | if (mcspi->dma_channels == NULL) |
Shubhrajyoti D | 1a77b12 | 2012-03-17 12:44:01 +0530 | [diff] [blame] | 1364 | goto free_master; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1365 | |
Charulatha V | 1a5d819 | 2011-02-02 17:52:14 +0530 | [diff] [blame] | 1366 | for (i = 0; i < master->num_chipselect; i++) { |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 1367 | char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name; |
| 1368 | char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name; |
Charulatha V | 1a5d819 | 2011-02-02 17:52:14 +0530 | [diff] [blame] | 1369 | struct resource *dma_res; |
| 1370 | |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 1371 | sprintf(dma_rx_ch_name, "rx%d", i); |
| 1372 | if (!pdev->dev.of_node) { |
| 1373 | dma_res = |
| 1374 | platform_get_resource_byname(pdev, |
| 1375 | IORESOURCE_DMA, |
| 1376 | dma_rx_ch_name); |
| 1377 | if (!dma_res) { |
| 1378 | dev_dbg(&pdev->dev, |
| 1379 | "cannot get DMA RX channel\n"); |
| 1380 | status = -ENODEV; |
| 1381 | break; |
| 1382 | } |
Charulatha V | 1a5d819 | 2011-02-02 17:52:14 +0530 | [diff] [blame] | 1383 | |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 1384 | mcspi->dma_channels[i].dma_rx_sync_dev = |
| 1385 | dma_res->start; |
Charulatha V | 1a5d819 | 2011-02-02 17:52:14 +0530 | [diff] [blame] | 1386 | } |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 1387 | sprintf(dma_tx_ch_name, "tx%d", i); |
| 1388 | if (!pdev->dev.of_node) { |
| 1389 | dma_res = |
| 1390 | platform_get_resource_byname(pdev, |
| 1391 | IORESOURCE_DMA, |
| 1392 | dma_tx_ch_name); |
| 1393 | if (!dma_res) { |
| 1394 | dev_dbg(&pdev->dev, |
| 1395 | "cannot get DMA TX channel\n"); |
| 1396 | status = -ENODEV; |
| 1397 | break; |
| 1398 | } |
Charulatha V | 1a5d819 | 2011-02-02 17:52:14 +0530 | [diff] [blame] | 1399 | |
Matt Porter | 74f3aaa | 2013-06-22 23:07:38 +0530 | [diff] [blame] | 1400 | mcspi->dma_channels[i].dma_tx_sync_dev = |
| 1401 | dma_res->start; |
| 1402 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Shubhrajyoti D | 39f1b56 | 2011-10-28 17:14:19 +0530 | [diff] [blame] | 1405 | if (status < 0) |
| 1406 | goto dma_chnl_free; |
| 1407 | |
Shubhrajyoti D | 27b5284 | 2012-03-26 17:04:22 +0530 | [diff] [blame] | 1408 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1409 | pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1410 | pm_runtime_enable(&pdev->dev); |
| 1411 | |
Wei Yongjun | 142e07b | 2013-04-18 11:14:59 +0800 | [diff] [blame] | 1412 | status = omap2_mcspi_master_setup(mcspi); |
| 1413 | if (status < 0) |
Shubhrajyoti D | 39f1b56 | 2011-10-28 17:14:19 +0530 | [diff] [blame] | 1414 | goto disable_pm; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1415 | |
Jingoo Han | b95e02b | 2013-09-24 13:40:29 +0900 | [diff] [blame] | 1416 | status = devm_spi_register_master(&pdev->dev, master); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1417 | if (status < 0) |
Shubhrajyoti D | 37a2d84 | 2012-08-02 16:41:25 +0530 | [diff] [blame] | 1418 | goto disable_pm; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1419 | |
| 1420 | return status; |
| 1421 | |
Shubhrajyoti D | 39f1b56 | 2011-10-28 17:14:19 +0530 | [diff] [blame] | 1422 | disable_pm: |
Shubhrajyoti D | 751c925 | 2011-10-28 17:14:18 +0530 | [diff] [blame] | 1423 | pm_runtime_disable(&pdev->dev); |
Shubhrajyoti D | 39f1b56 | 2011-10-28 17:14:19 +0530 | [diff] [blame] | 1424 | dma_chnl_free: |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1425 | kfree(mcspi->dma_channels); |
Shubhrajyoti D | 39f1b56 | 2011-10-28 17:14:19 +0530 | [diff] [blame] | 1426 | free_master: |
Shubhrajyoti D | 37a2d84 | 2012-08-02 16:41:25 +0530 | [diff] [blame] | 1427 | spi_master_put(master); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1428 | return status; |
| 1429 | } |
| 1430 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1431 | static int omap2_mcspi_remove(struct platform_device *pdev) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1432 | { |
| 1433 | struct spi_master *master; |
| 1434 | struct omap2_mcspi *mcspi; |
| 1435 | struct omap2_mcspi_dma *dma_channels; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1436 | |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 1437 | master = platform_get_drvdata(pdev); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1438 | mcspi = spi_master_get_devdata(master); |
| 1439 | dma_channels = mcspi->dma_channels; |
| 1440 | |
Shubhrajyoti D | a93a202 | 2012-08-22 11:35:14 +0530 | [diff] [blame] | 1441 | pm_runtime_put_sync(mcspi->dev); |
Shubhrajyoti D | 751c925 | 2011-10-28 17:14:18 +0530 | [diff] [blame] | 1442 | pm_runtime_disable(&pdev->dev); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1443 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1444 | kfree(dma_channels); |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1449 | /* work with hotplug and coldplug */ |
| 1450 | MODULE_ALIAS("platform:omap2_mcspi"); |
| 1451 | |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1452 | #ifdef CONFIG_SUSPEND |
| 1453 | /* |
| 1454 | * When SPI wake up from off-mode, CS is in activate state. If it was in |
| 1455 | * unactive state when driver was suspend, then force it to unactive state at |
| 1456 | * wake up. |
| 1457 | */ |
| 1458 | static int omap2_mcspi_resume(struct device *dev) |
| 1459 | { |
| 1460 | struct spi_master *master = dev_get_drvdata(dev); |
| 1461 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 1462 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
| 1463 | struct omap2_mcspi_cs *cs; |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1464 | |
Shubhrajyoti D | 034d3dc | 2012-08-22 11:35:12 +0530 | [diff] [blame] | 1465 | pm_runtime_get_sync(mcspi->dev); |
Benoit Cousson | 1bd897f8 | 2012-03-26 15:32:33 +0530 | [diff] [blame] | 1466 | list_for_each_entry(cs, &ctx->cs, node) { |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1467 | if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) { |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1468 | /* |
| 1469 | * We need to toggle CS state for OMAP take this |
| 1470 | * change in account. |
| 1471 | */ |
Shubhrajyoti D | af4e944 | 2012-08-22 11:35:13 +0530 | [diff] [blame] | 1472 | cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE; |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 1473 | writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0); |
Shubhrajyoti D | af4e944 | 2012-08-22 11:35:13 +0530 | [diff] [blame] | 1474 | cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE; |
Victor Kamensky | 21b2ce5 | 2013-11-16 02:01:16 +0200 | [diff] [blame^] | 1475 | writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0); |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1476 | } |
| 1477 | } |
Shubhrajyoti D | 034d3dc | 2012-08-22 11:35:12 +0530 | [diff] [blame] | 1478 | pm_runtime_mark_last_busy(mcspi->dev); |
| 1479 | pm_runtime_put_autosuspend(mcspi->dev); |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | #else |
| 1483 | #define omap2_mcspi_resume NULL |
| 1484 | #endif |
| 1485 | |
| 1486 | static const struct dev_pm_ops omap2_mcspi_pm_ops = { |
| 1487 | .resume = omap2_mcspi_resume, |
Govindraj.R | 1f1a438 | 2011-02-02 17:52:15 +0530 | [diff] [blame] | 1488 | .runtime_resume = omap_mcspi_runtime_resume, |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1489 | }; |
| 1490 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1491 | static struct platform_driver omap2_mcspi_driver = { |
| 1492 | .driver = { |
| 1493 | .name = "omap2_mcspi", |
| 1494 | .owner = THIS_MODULE, |
Benoit Cousson | d5a8003 | 2012-02-15 18:37:34 +0100 | [diff] [blame] | 1495 | .pm = &omap2_mcspi_pm_ops, |
| 1496 | .of_match_table = omap_mcspi_of_match, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1497 | }, |
Felipe Balbi | 7d6b6d8 | 2012-03-14 11:18:30 +0200 | [diff] [blame] | 1498 | .probe = omap2_mcspi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1499 | .remove = omap2_mcspi_remove, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1500 | }; |
| 1501 | |
Felipe Balbi | 9fdca9d | 2012-03-14 11:18:31 +0200 | [diff] [blame] | 1502 | module_platform_driver(omap2_mcspi_driver); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1503 | MODULE_LICENSE("GPL"); |