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