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