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