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