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