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