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 |
| 6 | * Juha Yrjölä <juha.yrjola@nokia.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/dma-mapping.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/err.h> |
| 33 | #include <linux/clk.h> |
| 34 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 36 | |
| 37 | #include <linux/spi/spi.h> |
| 38 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 39 | #include <plat/dma.h> |
| 40 | #include <plat/clock.h> |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 41 | #include <plat/mcspi.h> |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 42 | |
| 43 | #define OMAP2_MCSPI_MAX_FREQ 48000000 |
| 44 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 45 | /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */ |
| 46 | #define OMAP2_MCSPI_MAX_CTRL 4 |
| 47 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 48 | #define OMAP2_MCSPI_REVISION 0x00 |
| 49 | #define OMAP2_MCSPI_SYSCONFIG 0x10 |
| 50 | #define OMAP2_MCSPI_SYSSTATUS 0x14 |
| 51 | #define OMAP2_MCSPI_IRQSTATUS 0x18 |
| 52 | #define OMAP2_MCSPI_IRQENABLE 0x1c |
| 53 | #define OMAP2_MCSPI_WAKEUPENABLE 0x20 |
| 54 | #define OMAP2_MCSPI_SYST 0x24 |
| 55 | #define OMAP2_MCSPI_MODULCTRL 0x28 |
| 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: */ |
| 65 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 66 | #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4) |
| 67 | #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2) |
| 68 | #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0) |
| 69 | #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1) |
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_SYSSTATUS_RESETDONE BIT(0) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 72 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 73 | #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0) |
| 74 | #define OMAP2_MCSPI_MODULCTRL_MS BIT(2) |
| 75 | #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 76 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 77 | #define OMAP2_MCSPI_CHCONF_PHA BIT(0) |
| 78 | #define OMAP2_MCSPI_CHCONF_POL BIT(1) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 79 | #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2) |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 80 | #define OMAP2_MCSPI_CHCONF_EPOL BIT(6) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 81 | #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7) |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 82 | #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12) |
| 83 | #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 84 | #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12) |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 85 | #define OMAP2_MCSPI_CHCONF_DMAW BIT(14) |
| 86 | #define OMAP2_MCSPI_CHCONF_DMAR BIT(15) |
| 87 | #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16) |
| 88 | #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17) |
| 89 | #define OMAP2_MCSPI_CHCONF_IS BIT(18) |
| 90 | #define OMAP2_MCSPI_CHCONF_TURBO BIT(19) |
| 91 | #define OMAP2_MCSPI_CHCONF_FORCE BIT(20) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 92 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 93 | #define OMAP2_MCSPI_CHSTAT_RXS BIT(0) |
| 94 | #define OMAP2_MCSPI_CHSTAT_TXS BIT(1) |
| 95 | #define OMAP2_MCSPI_CHSTAT_EOT BIT(2) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 96 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 97 | #define OMAP2_MCSPI_CHCTRL_EN BIT(0) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 98 | |
Jouni Hogander | 7a8fa72 | 2009-09-22 16:45:58 -0700 | [diff] [blame] | 99 | #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0) |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 100 | |
| 101 | /* We have 2 DMA channels per CS, one for RX and one for TX */ |
| 102 | struct omap2_mcspi_dma { |
| 103 | int dma_tx_channel; |
| 104 | int dma_rx_channel; |
| 105 | |
| 106 | int dma_tx_sync_dev; |
| 107 | int dma_rx_sync_dev; |
| 108 | |
| 109 | struct completion dma_tx_completion; |
| 110 | struct completion dma_rx_completion; |
| 111 | }; |
| 112 | |
| 113 | /* use PIO for small transfers, avoiding DMA setup/teardown overhead and |
| 114 | * cache operations; better heuristics consider wordsize and bitrate. |
| 115 | */ |
Roman Tereshonkov | 8b66c13 | 2010-04-12 09:07:54 +0000 | [diff] [blame] | 116 | #define DMA_MIN_BYTES 160 |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | struct omap2_mcspi { |
| 120 | struct work_struct work; |
| 121 | /* lock protects queue and registers */ |
| 122 | spinlock_t lock; |
| 123 | struct list_head msg_queue; |
| 124 | struct spi_master *master; |
| 125 | struct clk *ick; |
| 126 | struct clk *fck; |
| 127 | /* Virtual base address of the controller */ |
| 128 | void __iomem *base; |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 129 | unsigned long phys; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 130 | /* SPI1 has 4 channels, while SPI2 has 2 */ |
| 131 | struct omap2_mcspi_dma *dma_channels; |
| 132 | }; |
| 133 | |
| 134 | struct omap2_mcspi_cs { |
| 135 | void __iomem *base; |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 136 | unsigned long phys; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 137 | int word_len; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 138 | struct list_head node; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 139 | /* Context save and restore shadow register */ |
| 140 | u32 chconf0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 143 | /* used for context save and restore, structure members to be updated whenever |
| 144 | * corresponding registers are modified. |
| 145 | */ |
| 146 | struct omap2_mcspi_regs { |
| 147 | u32 sysconfig; |
| 148 | u32 modulctrl; |
| 149 | u32 wakeupenable; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 150 | struct list_head cs; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL]; |
| 154 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 155 | static struct workqueue_struct *omap2_mcspi_wq; |
| 156 | |
| 157 | #define MOD_REG_BIT(val, mask, set) do { \ |
| 158 | if (set) \ |
| 159 | val |= mask; \ |
| 160 | else \ |
| 161 | val &= ~mask; \ |
| 162 | } while (0) |
| 163 | |
| 164 | static inline void mcspi_write_reg(struct spi_master *master, |
| 165 | int idx, u32 val) |
| 166 | { |
| 167 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
| 168 | |
| 169 | __raw_writel(val, mcspi->base + idx); |
| 170 | } |
| 171 | |
| 172 | static inline u32 mcspi_read_reg(struct spi_master *master, int idx) |
| 173 | { |
| 174 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
| 175 | |
| 176 | return __raw_readl(mcspi->base + idx); |
| 177 | } |
| 178 | |
| 179 | static inline void mcspi_write_cs_reg(const struct spi_device *spi, |
| 180 | int idx, u32 val) |
| 181 | { |
| 182 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 183 | |
| 184 | __raw_writel(val, cs->base + idx); |
| 185 | } |
| 186 | |
| 187 | static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx) |
| 188 | { |
| 189 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 190 | |
| 191 | return __raw_readl(cs->base + idx); |
| 192 | } |
| 193 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 194 | static inline u32 mcspi_cached_chconf0(const struct spi_device *spi) |
| 195 | { |
| 196 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 197 | |
| 198 | return cs->chconf0; |
| 199 | } |
| 200 | |
| 201 | static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val) |
| 202 | { |
| 203 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 204 | |
| 205 | cs->chconf0 = val; |
| 206 | mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val); |
Roman Tereshonkov | a330ce2 | 2010-03-15 09:06:28 +0000 | [diff] [blame] | 207 | mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 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 | |
| 222 | MOD_REG_BIT(l, rw, enable); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 223 | mcspi_write_chconf0(spi, l); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable) |
| 227 | { |
| 228 | u32 l; |
| 229 | |
| 230 | l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0; |
| 231 | mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 232 | /* Flash post-writes */ |
| 233 | mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active) |
| 237 | { |
| 238 | u32 l; |
| 239 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 240 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 241 | MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 242 | mcspi_write_chconf0(spi, l); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void omap2_mcspi_set_master_mode(struct spi_master *master) |
| 246 | { |
| 247 | u32 l; |
| 248 | |
| 249 | /* setup when switching from (reset default) slave mode |
| 250 | * to single-channel master mode |
| 251 | */ |
| 252 | l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL); |
| 253 | MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0); |
| 254 | MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0); |
| 255 | MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1); |
| 256 | mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 257 | |
| 258 | omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l; |
| 259 | } |
| 260 | |
| 261 | static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi) |
| 262 | { |
| 263 | struct spi_master *spi_cntrl; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 264 | struct omap2_mcspi_cs *cs; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 265 | spi_cntrl = mcspi->master; |
| 266 | |
| 267 | /* McSPI: context restore */ |
| 268 | mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, |
| 269 | omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl); |
| 270 | |
| 271 | mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG, |
| 272 | omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig); |
| 273 | |
| 274 | mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, |
| 275 | omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable); |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 276 | |
| 277 | list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs, |
| 278 | node) |
| 279 | __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 280 | } |
| 281 | static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi) |
| 282 | { |
| 283 | clk_disable(mcspi->ick); |
| 284 | clk_disable(mcspi->fck); |
| 285 | } |
| 286 | |
| 287 | static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi) |
| 288 | { |
| 289 | if (clk_enable(mcspi->ick)) |
| 290 | return -ENODEV; |
| 291 | if (clk_enable(mcspi->fck)) |
| 292 | return -ENODEV; |
| 293 | |
| 294 | omap2_mcspi_restore_ctx(mcspi); |
| 295 | |
| 296 | return 0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 299 | static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit) |
| 300 | { |
| 301 | unsigned long timeout; |
| 302 | |
| 303 | timeout = jiffies + msecs_to_jiffies(1000); |
| 304 | while (!(__raw_readl(reg) & bit)) { |
| 305 | if (time_after(jiffies, timeout)) |
| 306 | return -1; |
| 307 | cpu_relax(); |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 312 | static unsigned |
| 313 | omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer) |
| 314 | { |
| 315 | struct omap2_mcspi *mcspi; |
| 316 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 317 | struct omap2_mcspi_dma *mcspi_dma; |
| 318 | unsigned int count, c; |
| 319 | unsigned long base, tx_reg, rx_reg; |
| 320 | int word_len, data_type, element_count; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 321 | int elements; |
| 322 | u32 l; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 323 | u8 * rx; |
| 324 | const u8 * tx; |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 325 | void __iomem *chstat_reg; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 326 | |
| 327 | mcspi = spi_master_get_devdata(spi->master); |
| 328 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 329 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 330 | |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 331 | chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0; |
| 332 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 333 | count = xfer->len; |
| 334 | c = count; |
| 335 | word_len = cs->word_len; |
| 336 | |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 337 | base = cs->phys; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 338 | tx_reg = base + OMAP2_MCSPI_TX0; |
| 339 | rx_reg = base + OMAP2_MCSPI_RX0; |
| 340 | rx = xfer->rx_buf; |
| 341 | tx = xfer->tx_buf; |
| 342 | |
| 343 | if (word_len <= 8) { |
| 344 | data_type = OMAP_DMA_DATA_TYPE_S8; |
| 345 | element_count = count; |
| 346 | } else if (word_len <= 16) { |
| 347 | data_type = OMAP_DMA_DATA_TYPE_S16; |
| 348 | element_count = count >> 1; |
| 349 | } else /* word_len <= 32 */ { |
| 350 | data_type = OMAP_DMA_DATA_TYPE_S32; |
| 351 | element_count = count >> 2; |
| 352 | } |
| 353 | |
| 354 | if (tx != NULL) { |
| 355 | omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel, |
| 356 | data_type, element_count, 1, |
| 357 | OMAP_DMA_SYNC_ELEMENT, |
| 358 | mcspi_dma->dma_tx_sync_dev, 0); |
| 359 | |
| 360 | omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0, |
| 361 | OMAP_DMA_AMODE_CONSTANT, |
| 362 | tx_reg, 0, 0); |
| 363 | |
| 364 | omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0, |
| 365 | OMAP_DMA_AMODE_POST_INC, |
| 366 | xfer->tx_dma, 0, 0); |
| 367 | } |
| 368 | |
| 369 | if (rx != NULL) { |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 370 | elements = element_count - 1; |
| 371 | if (l & OMAP2_MCSPI_CHCONF_TURBO) |
| 372 | elements--; |
| 373 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 374 | omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel, |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 375 | data_type, elements, 1, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 376 | OMAP_DMA_SYNC_ELEMENT, |
| 377 | mcspi_dma->dma_rx_sync_dev, 1); |
| 378 | |
| 379 | omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0, |
| 380 | OMAP_DMA_AMODE_CONSTANT, |
| 381 | rx_reg, 0, 0); |
| 382 | |
| 383 | omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0, |
| 384 | OMAP_DMA_AMODE_POST_INC, |
| 385 | xfer->rx_dma, 0, 0); |
| 386 | } |
| 387 | |
| 388 | if (tx != NULL) { |
| 389 | omap_start_dma(mcspi_dma->dma_tx_channel); |
| 390 | omap2_mcspi_set_dma_req(spi, 0, 1); |
| 391 | } |
| 392 | |
| 393 | if (rx != NULL) { |
| 394 | omap_start_dma(mcspi_dma->dma_rx_channel); |
| 395 | omap2_mcspi_set_dma_req(spi, 1, 1); |
| 396 | } |
| 397 | |
| 398 | if (tx != NULL) { |
| 399 | wait_for_completion(&mcspi_dma->dma_tx_completion); |
Russell King - ARM Linux | 07fe035 | 2011-01-07 15:49:20 +0000 | [diff] [blame] | 400 | dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE); |
Ilkka Koskinen | 2764c50 | 2010-10-19 17:07:31 +0300 | [diff] [blame] | 401 | |
| 402 | /* for TX_ONLY mode, be sure all words have shifted out */ |
| 403 | if (rx == NULL) { |
| 404 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 405 | OMAP2_MCSPI_CHSTAT_TXS) < 0) |
| 406 | dev_err(&spi->dev, "TXS timed out\n"); |
| 407 | else if (mcspi_wait_for_reg_bit(chstat_reg, |
| 408 | OMAP2_MCSPI_CHSTAT_EOT) < 0) |
| 409 | dev_err(&spi->dev, "EOT timed out\n"); |
| 410 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (rx != NULL) { |
| 414 | wait_for_completion(&mcspi_dma->dma_rx_completion); |
Russell King - ARM Linux | 07fe035 | 2011-01-07 15:49:20 +0000 | [diff] [blame] | 415 | dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE); |
Eero Nurkkala | 57c5c28d | 2009-07-29 15:02:12 -0700 | [diff] [blame] | 416 | omap2_mcspi_set_enable(spi, 0); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 417 | |
| 418 | if (l & OMAP2_MCSPI_CHCONF_TURBO) { |
| 419 | |
| 420 | if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0) |
| 421 | & OMAP2_MCSPI_CHSTAT_RXS)) { |
| 422 | u32 w; |
| 423 | |
| 424 | w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0); |
| 425 | if (word_len <= 8) |
| 426 | ((u8 *)xfer->rx_buf)[elements++] = w; |
| 427 | else if (word_len <= 16) |
| 428 | ((u16 *)xfer->rx_buf)[elements++] = w; |
| 429 | else /* word_len <= 32 */ |
| 430 | ((u32 *)xfer->rx_buf)[elements++] = w; |
| 431 | } else { |
| 432 | dev_err(&spi->dev, |
| 433 | "DMA RX penultimate word empty"); |
| 434 | count -= (word_len <= 8) ? 2 : |
| 435 | (word_len <= 16) ? 4 : |
| 436 | /* word_len <= 32 */ 8; |
| 437 | omap2_mcspi_set_enable(spi, 1); |
| 438 | return count; |
| 439 | } |
| 440 | } |
| 441 | |
Eero Nurkkala | 57c5c28d | 2009-07-29 15:02:12 -0700 | [diff] [blame] | 442 | if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0) |
| 443 | & OMAP2_MCSPI_CHSTAT_RXS)) { |
| 444 | u32 w; |
| 445 | |
| 446 | w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0); |
| 447 | if (word_len <= 8) |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 448 | ((u8 *)xfer->rx_buf)[elements] = w; |
Eero Nurkkala | 57c5c28d | 2009-07-29 15:02:12 -0700 | [diff] [blame] | 449 | else if (word_len <= 16) |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 450 | ((u16 *)xfer->rx_buf)[elements] = w; |
Eero Nurkkala | 57c5c28d | 2009-07-29 15:02:12 -0700 | [diff] [blame] | 451 | else /* word_len <= 32 */ |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 452 | ((u32 *)xfer->rx_buf)[elements] = w; |
Eero Nurkkala | 57c5c28d | 2009-07-29 15:02:12 -0700 | [diff] [blame] | 453 | } else { |
| 454 | dev_err(&spi->dev, "DMA RX last word empty"); |
| 455 | count -= (word_len <= 8) ? 1 : |
| 456 | (word_len <= 16) ? 2 : |
| 457 | /* word_len <= 32 */ 4; |
| 458 | } |
| 459 | omap2_mcspi_set_enable(spi, 1); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 460 | } |
| 461 | return count; |
| 462 | } |
| 463 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 464 | static unsigned |
| 465 | omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer) |
| 466 | { |
| 467 | struct omap2_mcspi *mcspi; |
| 468 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 469 | unsigned int count, c; |
| 470 | u32 l; |
| 471 | void __iomem *base = cs->base; |
| 472 | void __iomem *tx_reg; |
| 473 | void __iomem *rx_reg; |
| 474 | void __iomem *chstat_reg; |
| 475 | int word_len; |
| 476 | |
| 477 | mcspi = spi_master_get_devdata(spi->master); |
| 478 | count = xfer->len; |
| 479 | c = count; |
| 480 | word_len = cs->word_len; |
| 481 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 482 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 483 | |
| 484 | /* We store the pre-calculated register addresses on stack to speed |
| 485 | * up the transfer loop. */ |
| 486 | tx_reg = base + OMAP2_MCSPI_TX0; |
| 487 | rx_reg = base + OMAP2_MCSPI_RX0; |
| 488 | chstat_reg = base + OMAP2_MCSPI_CHSTAT0; |
| 489 | |
Michael Jones | adef658 | 2011-02-25 16:55:11 +0100 | [diff] [blame^] | 490 | if (c < (word_len>>3)) |
| 491 | return 0; |
| 492 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 493 | if (word_len <= 8) { |
| 494 | u8 *rx; |
| 495 | const u8 *tx; |
| 496 | |
| 497 | rx = xfer->rx_buf; |
| 498 | tx = xfer->tx_buf; |
| 499 | |
| 500 | do { |
Kalle Valo | feed9ba | 2008-01-24 14:00:40 -0800 | [diff] [blame] | 501 | c -= 1; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 502 | if (tx != NULL) { |
| 503 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 504 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 505 | dev_err(&spi->dev, "TXS timed out\n"); |
| 506 | goto out; |
| 507 | } |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 508 | dev_vdbg(&spi->dev, "write-%d %02x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 509 | word_len, *tx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 510 | __raw_writel(*tx++, tx_reg); |
| 511 | } |
| 512 | if (rx != NULL) { |
| 513 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 514 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 515 | dev_err(&spi->dev, "RXS timed out\n"); |
| 516 | goto out; |
| 517 | } |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 518 | |
| 519 | if (c == 1 && tx == NULL && |
| 520 | (l & OMAP2_MCSPI_CHCONF_TURBO)) { |
| 521 | omap2_mcspi_set_enable(spi, 0); |
| 522 | *rx++ = __raw_readl(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 523 | dev_vdbg(&spi->dev, "read-%d %02x\n", |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 524 | word_len, *(rx - 1)); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 525 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 526 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 527 | dev_err(&spi->dev, |
| 528 | "RXS timed out\n"); |
| 529 | goto out; |
| 530 | } |
| 531 | c = 0; |
| 532 | } else if (c == 0 && tx == NULL) { |
| 533 | omap2_mcspi_set_enable(spi, 0); |
| 534 | } |
| 535 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 536 | *rx++ = __raw_readl(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 537 | dev_vdbg(&spi->dev, "read-%d %02x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 538 | word_len, *(rx - 1)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 539 | } |
Michael Jones | adef658 | 2011-02-25 16:55:11 +0100 | [diff] [blame^] | 540 | } while (c > (word_len>>3)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 541 | } else if (word_len <= 16) { |
| 542 | u16 *rx; |
| 543 | const u16 *tx; |
| 544 | |
| 545 | rx = xfer->rx_buf; |
| 546 | tx = xfer->tx_buf; |
| 547 | do { |
Kalle Valo | feed9ba | 2008-01-24 14:00:40 -0800 | [diff] [blame] | 548 | c -= 2; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 549 | if (tx != NULL) { |
| 550 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 551 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 552 | dev_err(&spi->dev, "TXS timed out\n"); |
| 553 | goto out; |
| 554 | } |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 555 | dev_vdbg(&spi->dev, "write-%d %04x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 556 | word_len, *tx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 557 | __raw_writel(*tx++, tx_reg); |
| 558 | } |
| 559 | if (rx != NULL) { |
| 560 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 561 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 562 | dev_err(&spi->dev, "RXS timed out\n"); |
| 563 | goto out; |
| 564 | } |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 565 | |
| 566 | if (c == 2 && tx == NULL && |
| 567 | (l & OMAP2_MCSPI_CHCONF_TURBO)) { |
| 568 | omap2_mcspi_set_enable(spi, 0); |
| 569 | *rx++ = __raw_readl(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 570 | dev_vdbg(&spi->dev, "read-%d %04x\n", |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 571 | word_len, *(rx - 1)); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 572 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 573 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 574 | dev_err(&spi->dev, |
| 575 | "RXS timed out\n"); |
| 576 | goto out; |
| 577 | } |
| 578 | c = 0; |
| 579 | } else if (c == 0 && tx == NULL) { |
| 580 | omap2_mcspi_set_enable(spi, 0); |
| 581 | } |
| 582 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 583 | *rx++ = __raw_readl(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 584 | dev_vdbg(&spi->dev, "read-%d %04x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 585 | word_len, *(rx - 1)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 586 | } |
Michael Jones | adef658 | 2011-02-25 16:55:11 +0100 | [diff] [blame^] | 587 | } while (c > (word_len>>3)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 588 | } else if (word_len <= 32) { |
| 589 | u32 *rx; |
| 590 | const u32 *tx; |
| 591 | |
| 592 | rx = xfer->rx_buf; |
| 593 | tx = xfer->tx_buf; |
| 594 | do { |
Kalle Valo | feed9ba | 2008-01-24 14:00:40 -0800 | [diff] [blame] | 595 | c -= 4; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 596 | if (tx != NULL) { |
| 597 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 598 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 599 | dev_err(&spi->dev, "TXS timed out\n"); |
| 600 | goto out; |
| 601 | } |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 602 | dev_vdbg(&spi->dev, "write-%d %08x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 603 | word_len, *tx); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 604 | __raw_writel(*tx++, tx_reg); |
| 605 | } |
| 606 | if (rx != NULL) { |
| 607 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 608 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 609 | dev_err(&spi->dev, "RXS timed out\n"); |
| 610 | goto out; |
| 611 | } |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 612 | |
| 613 | if (c == 4 && tx == NULL && |
| 614 | (l & OMAP2_MCSPI_CHCONF_TURBO)) { |
| 615 | omap2_mcspi_set_enable(spi, 0); |
| 616 | *rx++ = __raw_readl(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 617 | dev_vdbg(&spi->dev, "read-%d %08x\n", |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 618 | word_len, *(rx - 1)); |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 619 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 620 | OMAP2_MCSPI_CHSTAT_RXS) < 0) { |
| 621 | dev_err(&spi->dev, |
| 622 | "RXS timed out\n"); |
| 623 | goto out; |
| 624 | } |
| 625 | c = 0; |
| 626 | } else if (c == 0 && tx == NULL) { |
| 627 | omap2_mcspi_set_enable(spi, 0); |
| 628 | } |
| 629 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 630 | *rx++ = __raw_readl(rx_reg); |
Felipe Balbi | 079a176 | 2010-09-29 17:31:29 +0900 | [diff] [blame] | 631 | dev_vdbg(&spi->dev, "read-%d %08x\n", |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 632 | word_len, *(rx - 1)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 633 | } |
Michael Jones | adef658 | 2011-02-25 16:55:11 +0100 | [diff] [blame^] | 634 | } while (c > (word_len>>3)); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* for TX_ONLY mode, be sure all words have shifted out */ |
| 638 | if (xfer->rx_buf == NULL) { |
| 639 | if (mcspi_wait_for_reg_bit(chstat_reg, |
| 640 | OMAP2_MCSPI_CHSTAT_TXS) < 0) { |
| 641 | dev_err(&spi->dev, "TXS timed out\n"); |
| 642 | } else if (mcspi_wait_for_reg_bit(chstat_reg, |
| 643 | OMAP2_MCSPI_CHSTAT_EOT) < 0) |
| 644 | dev_err(&spi->dev, "EOT timed out\n"); |
Jason Wang | e1993ed | 2010-10-19 18:03:27 +0800 | [diff] [blame] | 645 | |
| 646 | /* disable chan to purge rx datas received in TX_ONLY transfer, |
| 647 | * otherwise these rx datas will affect the direct following |
| 648 | * RX_ONLY transfer. |
| 649 | */ |
| 650 | omap2_mcspi_set_enable(spi, 0); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 651 | } |
| 652 | out: |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 653 | omap2_mcspi_set_enable(spi, 1); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 654 | return count - c; |
| 655 | } |
| 656 | |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 657 | static u32 omap2_mcspi_calc_divisor(u32 speed_hz) |
| 658 | { |
| 659 | u32 div; |
| 660 | |
| 661 | for (div = 0; div < 15; div++) |
| 662 | if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div)) |
| 663 | return div; |
| 664 | |
| 665 | return 15; |
| 666 | } |
| 667 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 668 | /* called only when no transfer is active to this device */ |
| 669 | static int omap2_mcspi_setup_transfer(struct spi_device *spi, |
| 670 | struct spi_transfer *t) |
| 671 | { |
| 672 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 673 | struct omap2_mcspi *mcspi; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 674 | struct spi_master *spi_cntrl; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 675 | u32 l = 0, div = 0; |
| 676 | u8 word_len = spi->bits_per_word; |
Scott Ellis | 9bd4517 | 2010-03-10 14:23:13 -0700 | [diff] [blame] | 677 | u32 speed_hz = spi->max_speed_hz; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 678 | |
| 679 | mcspi = spi_master_get_devdata(spi->master); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 680 | spi_cntrl = mcspi->master; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 681 | |
| 682 | if (t != NULL && t->bits_per_word) |
| 683 | word_len = t->bits_per_word; |
| 684 | |
| 685 | cs->word_len = word_len; |
| 686 | |
Scott Ellis | 9bd4517 | 2010-03-10 14:23:13 -0700 | [diff] [blame] | 687 | if (t && t->speed_hz) |
| 688 | speed_hz = t->speed_hz; |
| 689 | |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 690 | speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ); |
| 691 | div = omap2_mcspi_calc_divisor(speed_hz); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 692 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 693 | l = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 694 | |
| 695 | /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS |
| 696 | * REVISIT: this controller could support SPI_3WIRE mode. |
| 697 | */ |
| 698 | l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1); |
| 699 | l |= OMAP2_MCSPI_CHCONF_DPE0; |
| 700 | |
| 701 | /* wordlength */ |
| 702 | l &= ~OMAP2_MCSPI_CHCONF_WL_MASK; |
| 703 | l |= (word_len - 1) << 7; |
| 704 | |
| 705 | /* set chipselect polarity; manage with FORCE */ |
| 706 | if (!(spi->mode & SPI_CS_HIGH)) |
| 707 | l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */ |
| 708 | else |
| 709 | l &= ~OMAP2_MCSPI_CHCONF_EPOL; |
| 710 | |
| 711 | /* set clock divisor */ |
| 712 | l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK; |
| 713 | l |= div << 2; |
| 714 | |
| 715 | /* set SPI mode 0..3 */ |
| 716 | if (spi->mode & SPI_CPOL) |
| 717 | l |= OMAP2_MCSPI_CHCONF_POL; |
| 718 | else |
| 719 | l &= ~OMAP2_MCSPI_CHCONF_POL; |
| 720 | if (spi->mode & SPI_CPHA) |
| 721 | l |= OMAP2_MCSPI_CHCONF_PHA; |
| 722 | else |
| 723 | l &= ~OMAP2_MCSPI_CHCONF_PHA; |
| 724 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 725 | mcspi_write_chconf0(spi, l); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 726 | |
| 727 | dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n", |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 728 | OMAP2_MCSPI_MAX_FREQ >> div, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 729 | (spi->mode & SPI_CPHA) ? "trailing" : "leading", |
| 730 | (spi->mode & SPI_CPOL) ? "inverted" : "normal"); |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data) |
| 736 | { |
| 737 | struct spi_device *spi = data; |
| 738 | struct omap2_mcspi *mcspi; |
| 739 | struct omap2_mcspi_dma *mcspi_dma; |
| 740 | |
| 741 | mcspi = spi_master_get_devdata(spi->master); |
| 742 | mcspi_dma = &(mcspi->dma_channels[spi->chip_select]); |
| 743 | |
| 744 | complete(&mcspi_dma->dma_rx_completion); |
| 745 | |
| 746 | /* We must disable the DMA RX request */ |
| 747 | omap2_mcspi_set_dma_req(spi, 1, 0); |
| 748 | } |
| 749 | |
| 750 | static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data) |
| 751 | { |
| 752 | struct spi_device *spi = data; |
| 753 | struct omap2_mcspi *mcspi; |
| 754 | struct omap2_mcspi_dma *mcspi_dma; |
| 755 | |
| 756 | mcspi = spi_master_get_devdata(spi->master); |
| 757 | mcspi_dma = &(mcspi->dma_channels[spi->chip_select]); |
| 758 | |
| 759 | complete(&mcspi_dma->dma_tx_completion); |
| 760 | |
| 761 | /* We must disable the DMA TX request */ |
| 762 | omap2_mcspi_set_dma_req(spi, 0, 0); |
| 763 | } |
| 764 | |
| 765 | static int omap2_mcspi_request_dma(struct spi_device *spi) |
| 766 | { |
| 767 | struct spi_master *master = spi->master; |
| 768 | struct omap2_mcspi *mcspi; |
| 769 | struct omap2_mcspi_dma *mcspi_dma; |
| 770 | |
| 771 | mcspi = spi_master_get_devdata(master); |
| 772 | mcspi_dma = mcspi->dma_channels + spi->chip_select; |
| 773 | |
| 774 | if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX", |
| 775 | omap2_mcspi_dma_rx_callback, spi, |
| 776 | &mcspi_dma->dma_rx_channel)) { |
| 777 | dev_err(&spi->dev, "no RX DMA channel for McSPI\n"); |
| 778 | return -EAGAIN; |
| 779 | } |
| 780 | |
| 781 | if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX", |
| 782 | omap2_mcspi_dma_tx_callback, spi, |
| 783 | &mcspi_dma->dma_tx_channel)) { |
| 784 | omap_free_dma(mcspi_dma->dma_rx_channel); |
| 785 | mcspi_dma->dma_rx_channel = -1; |
| 786 | dev_err(&spi->dev, "no TX DMA channel for McSPI\n"); |
| 787 | return -EAGAIN; |
| 788 | } |
| 789 | |
| 790 | init_completion(&mcspi_dma->dma_rx_completion); |
| 791 | init_completion(&mcspi_dma->dma_tx_completion); |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 796 | static int omap2_mcspi_setup(struct spi_device *spi) |
| 797 | { |
| 798 | int ret; |
| 799 | struct omap2_mcspi *mcspi; |
| 800 | struct omap2_mcspi_dma *mcspi_dma; |
| 801 | struct omap2_mcspi_cs *cs = spi->controller_state; |
| 802 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 803 | if (spi->bits_per_word < 4 || spi->bits_per_word > 32) { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 804 | dev_dbg(&spi->dev, "setup: unsupported %d bit words\n", |
| 805 | spi->bits_per_word); |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | |
| 809 | mcspi = spi_master_get_devdata(spi->master); |
| 810 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 811 | |
| 812 | if (!cs) { |
| 813 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
| 814 | if (!cs) |
| 815 | return -ENOMEM; |
| 816 | cs->base = mcspi->base + spi->chip_select * 0x14; |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 817 | cs->phys = mcspi->phys + spi->chip_select * 0x14; |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 818 | cs->chconf0 = 0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 819 | spi->controller_state = cs; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 820 | /* Link this to context save list */ |
| 821 | list_add_tail(&cs->node, |
| 822 | &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | if (mcspi_dma->dma_rx_channel == -1 |
| 826 | || mcspi_dma->dma_tx_channel == -1) { |
| 827 | ret = omap2_mcspi_request_dma(spi); |
| 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | } |
| 831 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 832 | if (omap2_mcspi_enable_clocks(mcspi)) |
| 833 | return -ENODEV; |
| 834 | |
Kyungmin Park | 86eeb6f | 2007-10-16 01:27:45 -0700 | [diff] [blame] | 835 | ret = omap2_mcspi_setup_transfer(spi, NULL); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 836 | omap2_mcspi_disable_clocks(mcspi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static void omap2_mcspi_cleanup(struct spi_device *spi) |
| 842 | { |
| 843 | struct omap2_mcspi *mcspi; |
| 844 | struct omap2_mcspi_dma *mcspi_dma; |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 845 | struct omap2_mcspi_cs *cs; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 846 | |
| 847 | mcspi = spi_master_get_devdata(spi->master); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 848 | |
Scott Ellis | 5e77494 | 2010-03-10 14:22:45 -0700 | [diff] [blame] | 849 | if (spi->controller_state) { |
| 850 | /* Unlink controller state from context save list */ |
| 851 | cs = spi->controller_state; |
| 852 | list_del(&cs->node); |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 853 | |
Scott Ellis | 5e77494 | 2010-03-10 14:22:45 -0700 | [diff] [blame] | 854 | kfree(spi->controller_state); |
| 855 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 856 | |
Scott Ellis | 99f1a43 | 2010-05-24 14:20:27 +0000 | [diff] [blame] | 857 | if (spi->chip_select < spi->master->num_chipselect) { |
| 858 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
| 859 | |
| 860 | if (mcspi_dma->dma_rx_channel != -1) { |
| 861 | omap_free_dma(mcspi_dma->dma_rx_channel); |
| 862 | mcspi_dma->dma_rx_channel = -1; |
| 863 | } |
| 864 | if (mcspi_dma->dma_tx_channel != -1) { |
| 865 | omap_free_dma(mcspi_dma->dma_tx_channel); |
| 866 | mcspi_dma->dma_tx_channel = -1; |
| 867 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | static void omap2_mcspi_work(struct work_struct *work) |
| 872 | { |
| 873 | struct omap2_mcspi *mcspi; |
| 874 | |
| 875 | mcspi = container_of(work, struct omap2_mcspi, work); |
| 876 | spin_lock_irq(&mcspi->lock); |
| 877 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 878 | if (omap2_mcspi_enable_clocks(mcspi)) |
| 879 | goto out; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 880 | |
| 881 | /* We only enable one channel at a time -- the one whose message is |
| 882 | * at the head of the queue -- although this controller would gladly |
| 883 | * arbitrate among multiple channels. This corresponds to "single |
| 884 | * channel" master mode. As a side effect, we need to manage the |
| 885 | * chipselect with the FORCE bit ... CS != channel enable. |
| 886 | */ |
| 887 | while (!list_empty(&mcspi->msg_queue)) { |
| 888 | struct spi_message *m; |
| 889 | struct spi_device *spi; |
| 890 | struct spi_transfer *t = NULL; |
| 891 | int cs_active = 0; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 892 | struct omap2_mcspi_cs *cs; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 893 | struct omap2_mcspi_device_config *cd; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 894 | int par_override = 0; |
| 895 | int status = 0; |
| 896 | u32 chconf; |
| 897 | |
| 898 | m = container_of(mcspi->msg_queue.next, struct spi_message, |
| 899 | queue); |
| 900 | |
| 901 | list_del_init(&m->queue); |
| 902 | spin_unlock_irq(&mcspi->lock); |
| 903 | |
| 904 | spi = m->spi; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 905 | cs = spi->controller_state; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 906 | cd = spi->controller_data; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 907 | |
| 908 | omap2_mcspi_set_enable(spi, 1); |
| 909 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 910 | if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) { |
| 911 | status = -EINVAL; |
| 912 | break; |
| 913 | } |
| 914 | if (par_override || t->speed_hz || t->bits_per_word) { |
| 915 | par_override = 1; |
| 916 | status = omap2_mcspi_setup_transfer(spi, t); |
| 917 | if (status < 0) |
| 918 | break; |
| 919 | if (!t->speed_hz && !t->bits_per_word) |
| 920 | par_override = 0; |
| 921 | } |
| 922 | |
| 923 | if (!cs_active) { |
| 924 | omap2_mcspi_force_cs(spi, 1); |
| 925 | cs_active = 1; |
| 926 | } |
| 927 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 928 | chconf = mcspi_cached_chconf0(spi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 929 | chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 930 | chconf &= ~OMAP2_MCSPI_CHCONF_TURBO; |
| 931 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 932 | if (t->tx_buf == NULL) |
| 933 | chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY; |
| 934 | else if (t->rx_buf == NULL) |
| 935 | chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY; |
Roman Tereshonkov | 4743a0f | 2010-04-13 10:41:51 +0000 | [diff] [blame] | 936 | |
| 937 | if (cd && cd->turbo_mode && t->tx_buf == NULL) { |
| 938 | /* Turbo mode is for more than one word */ |
| 939 | if (t->len > ((cs->word_len + 7) >> 3)) |
| 940 | chconf |= OMAP2_MCSPI_CHCONF_TURBO; |
| 941 | } |
| 942 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 943 | mcspi_write_chconf0(spi, chconf); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 944 | |
| 945 | if (t->len) { |
| 946 | unsigned count; |
| 947 | |
| 948 | /* RX_ONLY mode needs dummy data in TX reg */ |
| 949 | if (t->tx_buf == NULL) |
| 950 | __raw_writel(0, cs->base |
| 951 | + OMAP2_MCSPI_TX0); |
| 952 | |
| 953 | if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES) |
| 954 | count = omap2_mcspi_txrx_dma(spi, t); |
| 955 | else |
| 956 | count = omap2_mcspi_txrx_pio(spi, t); |
| 957 | m->actual_length += count; |
| 958 | |
| 959 | if (count != t->len) { |
| 960 | status = -EIO; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | if (t->delay_usecs) |
| 966 | udelay(t->delay_usecs); |
| 967 | |
| 968 | /* ignore the "leave it on after last xfer" hint */ |
| 969 | if (t->cs_change) { |
| 970 | omap2_mcspi_force_cs(spi, 0); |
| 971 | cs_active = 0; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | /* Restore defaults if they were overriden */ |
| 976 | if (par_override) { |
| 977 | par_override = 0; |
| 978 | status = omap2_mcspi_setup_transfer(spi, NULL); |
| 979 | } |
| 980 | |
| 981 | if (cs_active) |
| 982 | omap2_mcspi_force_cs(spi, 0); |
| 983 | |
| 984 | omap2_mcspi_set_enable(spi, 0); |
| 985 | |
| 986 | m->status = status; |
| 987 | m->complete(m->context); |
| 988 | |
| 989 | spin_lock_irq(&mcspi->lock); |
| 990 | } |
| 991 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 992 | omap2_mcspi_disable_clocks(mcspi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 993 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 994 | out: |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 995 | spin_unlock_irq(&mcspi->lock); |
| 996 | } |
| 997 | |
| 998 | static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m) |
| 999 | { |
| 1000 | struct omap2_mcspi *mcspi; |
| 1001 | unsigned long flags; |
| 1002 | struct spi_transfer *t; |
| 1003 | |
| 1004 | m->actual_length = 0; |
| 1005 | m->status = 0; |
| 1006 | |
| 1007 | /* reject invalid messages and transfers */ |
| 1008 | if (list_empty(&m->transfers) || !m->complete) |
| 1009 | return -EINVAL; |
| 1010 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 1011 | const void *tx_buf = t->tx_buf; |
| 1012 | void *rx_buf = t->rx_buf; |
| 1013 | unsigned len = t->len; |
| 1014 | |
| 1015 | if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ |
| 1016 | || (len && !(rx_buf || tx_buf)) |
| 1017 | || (t->bits_per_word && |
| 1018 | ( t->bits_per_word < 4 |
| 1019 | || t->bits_per_word > 32))) { |
| 1020 | dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n", |
| 1021 | t->speed_hz, |
| 1022 | len, |
| 1023 | tx_buf ? "tx" : "", |
| 1024 | rx_buf ? "rx" : "", |
| 1025 | t->bits_per_word); |
| 1026 | return -EINVAL; |
| 1027 | } |
Hannu Heikkinen | 57d9c10 | 2011-02-24 21:31:33 +0200 | [diff] [blame] | 1028 | if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) { |
| 1029 | dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n", |
| 1030 | t->speed_hz, |
| 1031 | OMAP2_MCSPI_MAX_FREQ >> 15); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1032 | return -EINVAL; |
| 1033 | } |
| 1034 | |
| 1035 | if (m->is_dma_mapped || len < DMA_MIN_BYTES) |
| 1036 | continue; |
| 1037 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1038 | if (tx_buf != NULL) { |
| 1039 | t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf, |
| 1040 | len, DMA_TO_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 1041 | if (dma_mapping_error(&spi->dev, t->tx_dma)) { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1042 | dev_dbg(&spi->dev, "dma %cX %d bytes error\n", |
| 1043 | 'T', len); |
| 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | } |
| 1047 | if (rx_buf != NULL) { |
| 1048 | t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len, |
| 1049 | DMA_FROM_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 1050 | if (dma_mapping_error(&spi->dev, t->rx_dma)) { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1051 | dev_dbg(&spi->dev, "dma %cX %d bytes error\n", |
| 1052 | 'R', len); |
| 1053 | if (tx_buf != NULL) |
Russell King - ARM Linux | 07fe035 | 2011-01-07 15:49:20 +0000 | [diff] [blame] | 1054 | dma_unmap_single(&spi->dev, t->tx_dma, |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1055 | len, DMA_TO_DEVICE); |
| 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | mcspi = spi_master_get_devdata(spi->master); |
| 1062 | |
| 1063 | spin_lock_irqsave(&mcspi->lock, flags); |
| 1064 | list_add_tail(&m->queue, &mcspi->msg_queue); |
| 1065 | queue_work(omap2_mcspi_wq, &mcspi->work); |
| 1066 | spin_unlock_irqrestore(&mcspi->lock, flags); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi) |
| 1072 | { |
| 1073 | struct spi_master *master = mcspi->master; |
| 1074 | u32 tmp; |
| 1075 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 1076 | if (omap2_mcspi_enable_clocks(mcspi)) |
| 1077 | return -1; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1078 | |
| 1079 | mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, |
| 1080 | OMAP2_MCSPI_SYSCONFIG_SOFTRESET); |
| 1081 | do { |
| 1082 | tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS); |
| 1083 | } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE)); |
| 1084 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 1085 | tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE | |
| 1086 | OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP | |
| 1087 | OMAP2_MCSPI_SYSCONFIG_SMARTIDLE; |
| 1088 | mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp); |
| 1089 | omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp; |
Jouni Hogander | ddb2219 | 2009-07-29 15:02:11 -0700 | [diff] [blame] | 1090 | |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 1091 | tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN; |
| 1092 | mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp); |
| 1093 | omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1094 | |
| 1095 | omap2_mcspi_set_master_mode(master); |
Hemanth V | a41ae1a | 2009-09-22 16:46:16 -0700 | [diff] [blame] | 1096 | omap2_mcspi_disable_clocks(mcspi); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | static u8 __initdata spi1_rxdma_id [] = { |
| 1101 | OMAP24XX_DMA_SPI1_RX0, |
| 1102 | OMAP24XX_DMA_SPI1_RX1, |
| 1103 | OMAP24XX_DMA_SPI1_RX2, |
| 1104 | OMAP24XX_DMA_SPI1_RX3, |
| 1105 | }; |
| 1106 | |
| 1107 | static u8 __initdata spi1_txdma_id [] = { |
| 1108 | OMAP24XX_DMA_SPI1_TX0, |
| 1109 | OMAP24XX_DMA_SPI1_TX1, |
| 1110 | OMAP24XX_DMA_SPI1_TX2, |
| 1111 | OMAP24XX_DMA_SPI1_TX3, |
| 1112 | }; |
| 1113 | |
| 1114 | static u8 __initdata spi2_rxdma_id[] = { |
| 1115 | OMAP24XX_DMA_SPI2_RX0, |
| 1116 | OMAP24XX_DMA_SPI2_RX1, |
| 1117 | }; |
| 1118 | |
| 1119 | static u8 __initdata spi2_txdma_id[] = { |
| 1120 | OMAP24XX_DMA_SPI2_TX0, |
| 1121 | OMAP24XX_DMA_SPI2_TX1, |
| 1122 | }; |
| 1123 | |
Tony Lindgren | a8eb7ca | 2010-02-12 12:26:48 -0800 | [diff] [blame] | 1124 | #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \ |
Syed Rafiuddin | 7869c0b | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 1125 | || defined(CONFIG_ARCH_OMAP4) |
Girish | ccc7bae | 2008-02-06 01:38:16 -0800 | [diff] [blame] | 1126 | static u8 __initdata spi3_rxdma_id[] = { |
| 1127 | OMAP24XX_DMA_SPI3_RX0, |
| 1128 | OMAP24XX_DMA_SPI3_RX1, |
| 1129 | }; |
| 1130 | |
| 1131 | static u8 __initdata spi3_txdma_id[] = { |
| 1132 | OMAP24XX_DMA_SPI3_TX0, |
| 1133 | OMAP24XX_DMA_SPI3_TX1, |
| 1134 | }; |
| 1135 | #endif |
| 1136 | |
Syed Rafiuddin | 7869c0b | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 1137 | #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4) |
Girish | ccc7bae | 2008-02-06 01:38:16 -0800 | [diff] [blame] | 1138 | static u8 __initdata spi4_rxdma_id[] = { |
| 1139 | OMAP34XX_DMA_SPI4_RX0, |
| 1140 | }; |
| 1141 | |
| 1142 | static u8 __initdata spi4_txdma_id[] = { |
| 1143 | OMAP34XX_DMA_SPI4_TX0, |
| 1144 | }; |
| 1145 | #endif |
| 1146 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1147 | static int __init omap2_mcspi_probe(struct platform_device *pdev) |
| 1148 | { |
| 1149 | struct spi_master *master; |
| 1150 | struct omap2_mcspi *mcspi; |
| 1151 | struct resource *r; |
| 1152 | int status = 0, i; |
| 1153 | const u8 *rxdma_id, *txdma_id; |
| 1154 | unsigned num_chipselect; |
| 1155 | |
| 1156 | switch (pdev->id) { |
| 1157 | case 1: |
| 1158 | rxdma_id = spi1_rxdma_id; |
| 1159 | txdma_id = spi1_txdma_id; |
| 1160 | num_chipselect = 4; |
| 1161 | break; |
| 1162 | case 2: |
| 1163 | rxdma_id = spi2_rxdma_id; |
| 1164 | txdma_id = spi2_txdma_id; |
| 1165 | num_chipselect = 2; |
| 1166 | break; |
Syed Rafiuddin | 7869c0b | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 1167 | #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \ |
| 1168 | || defined(CONFIG_ARCH_OMAP4) |
Girish | ccc7bae | 2008-02-06 01:38:16 -0800 | [diff] [blame] | 1169 | case 3: |
| 1170 | rxdma_id = spi3_rxdma_id; |
| 1171 | txdma_id = spi3_txdma_id; |
| 1172 | num_chipselect = 2; |
| 1173 | break; |
| 1174 | #endif |
Syed Rafiuddin | 7869c0b | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 1175 | #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4) |
Girish | ccc7bae | 2008-02-06 01:38:16 -0800 | [diff] [blame] | 1176 | case 4: |
| 1177 | rxdma_id = spi4_rxdma_id; |
| 1178 | txdma_id = spi4_txdma_id; |
| 1179 | num_chipselect = 1; |
| 1180 | break; |
| 1181 | #endif |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1182 | default: |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | |
| 1186 | master = spi_alloc_master(&pdev->dev, sizeof *mcspi); |
| 1187 | if (master == NULL) { |
| 1188 | dev_dbg(&pdev->dev, "master allocation failed\n"); |
| 1189 | return -ENOMEM; |
| 1190 | } |
| 1191 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1192 | /* the spi->mode bits understood by this driver: */ |
| 1193 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 1194 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1195 | if (pdev->id != -1) |
| 1196 | master->bus_num = pdev->id; |
| 1197 | |
| 1198 | master->setup = omap2_mcspi_setup; |
| 1199 | master->transfer = omap2_mcspi_transfer; |
| 1200 | master->cleanup = omap2_mcspi_cleanup; |
| 1201 | master->num_chipselect = num_chipselect; |
| 1202 | |
| 1203 | dev_set_drvdata(&pdev->dev, master); |
| 1204 | |
| 1205 | mcspi = spi_master_get_devdata(master); |
| 1206 | mcspi->master = master; |
| 1207 | |
| 1208 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1209 | if (r == NULL) { |
| 1210 | status = -ENODEV; |
| 1211 | goto err1; |
| 1212 | } |
| 1213 | if (!request_mem_region(r->start, (r->end - r->start) + 1, |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1214 | dev_name(&pdev->dev))) { |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1215 | status = -EBUSY; |
| 1216 | goto err1; |
| 1217 | } |
| 1218 | |
Russell King | e5480b73 | 2008-09-01 21:51:50 +0100 | [diff] [blame] | 1219 | mcspi->phys = r->start; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 1220 | mcspi->base = ioremap(r->start, r->end - r->start + 1); |
| 1221 | if (!mcspi->base) { |
| 1222 | dev_dbg(&pdev->dev, "can't ioremap MCSPI\n"); |
| 1223 | status = -ENOMEM; |
| 1224 | goto err1aa; |
| 1225 | } |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1226 | |
| 1227 | INIT_WORK(&mcspi->work, omap2_mcspi_work); |
| 1228 | |
| 1229 | spin_lock_init(&mcspi->lock); |
| 1230 | INIT_LIST_HEAD(&mcspi->msg_queue); |
Tero Kristo | 89c0537 | 2009-09-22 16:46:17 -0700 | [diff] [blame] | 1231 | INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1232 | |
Russell King | 1b5715e | 2009-01-19 20:49:37 +0000 | [diff] [blame] | 1233 | mcspi->ick = clk_get(&pdev->dev, "ick"); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1234 | if (IS_ERR(mcspi->ick)) { |
| 1235 | dev_dbg(&pdev->dev, "can't get mcspi_ick\n"); |
| 1236 | status = PTR_ERR(mcspi->ick); |
| 1237 | goto err1a; |
| 1238 | } |
Russell King | 1b5715e | 2009-01-19 20:49:37 +0000 | [diff] [blame] | 1239 | mcspi->fck = clk_get(&pdev->dev, "fck"); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1240 | if (IS_ERR(mcspi->fck)) { |
| 1241 | dev_dbg(&pdev->dev, "can't get mcspi_fck\n"); |
| 1242 | status = PTR_ERR(mcspi->fck); |
| 1243 | goto err2; |
| 1244 | } |
| 1245 | |
| 1246 | mcspi->dma_channels = kcalloc(master->num_chipselect, |
| 1247 | sizeof(struct omap2_mcspi_dma), |
| 1248 | GFP_KERNEL); |
| 1249 | |
| 1250 | if (mcspi->dma_channels == NULL) |
| 1251 | goto err3; |
| 1252 | |
| 1253 | for (i = 0; i < num_chipselect; i++) { |
| 1254 | mcspi->dma_channels[i].dma_rx_channel = -1; |
| 1255 | mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i]; |
| 1256 | mcspi->dma_channels[i].dma_tx_channel = -1; |
| 1257 | mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i]; |
| 1258 | } |
| 1259 | |
| 1260 | if (omap2_mcspi_reset(mcspi) < 0) |
| 1261 | goto err4; |
| 1262 | |
| 1263 | status = spi_register_master(master); |
| 1264 | if (status < 0) |
| 1265 | goto err4; |
| 1266 | |
| 1267 | return status; |
| 1268 | |
| 1269 | err4: |
| 1270 | kfree(mcspi->dma_channels); |
| 1271 | err3: |
| 1272 | clk_put(mcspi->fck); |
| 1273 | err2: |
| 1274 | clk_put(mcspi->ick); |
| 1275 | err1a: |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 1276 | iounmap(mcspi->base); |
| 1277 | err1aa: |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1278 | release_mem_region(r->start, (r->end - r->start) + 1); |
| 1279 | err1: |
| 1280 | spi_master_put(master); |
| 1281 | return status; |
| 1282 | } |
| 1283 | |
| 1284 | static int __exit omap2_mcspi_remove(struct platform_device *pdev) |
| 1285 | { |
| 1286 | struct spi_master *master; |
| 1287 | struct omap2_mcspi *mcspi; |
| 1288 | struct omap2_mcspi_dma *dma_channels; |
| 1289 | struct resource *r; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 1290 | void __iomem *base; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1291 | |
| 1292 | master = dev_get_drvdata(&pdev->dev); |
| 1293 | mcspi = spi_master_get_devdata(master); |
| 1294 | dma_channels = mcspi->dma_channels; |
| 1295 | |
| 1296 | clk_put(mcspi->fck); |
| 1297 | clk_put(mcspi->ick); |
| 1298 | |
| 1299 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1300 | release_mem_region(r->start, (r->end - r->start) + 1); |
| 1301 | |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 1302 | base = mcspi->base; |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1303 | spi_unregister_master(master); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 1304 | iounmap(base); |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1305 | kfree(dma_channels); |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1310 | /* work with hotplug and coldplug */ |
| 1311 | MODULE_ALIAS("platform:omap2_mcspi"); |
| 1312 | |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1313 | #ifdef CONFIG_SUSPEND |
| 1314 | /* |
| 1315 | * When SPI wake up from off-mode, CS is in activate state. If it was in |
| 1316 | * unactive state when driver was suspend, then force it to unactive state at |
| 1317 | * wake up. |
| 1318 | */ |
| 1319 | static int omap2_mcspi_resume(struct device *dev) |
| 1320 | { |
| 1321 | struct spi_master *master = dev_get_drvdata(dev); |
| 1322 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
| 1323 | struct omap2_mcspi_cs *cs; |
| 1324 | |
| 1325 | omap2_mcspi_enable_clocks(mcspi); |
| 1326 | list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs, |
| 1327 | node) { |
| 1328 | if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) { |
| 1329 | |
| 1330 | /* |
| 1331 | * We need to toggle CS state for OMAP take this |
| 1332 | * change in account. |
| 1333 | */ |
| 1334 | MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1); |
| 1335 | __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0); |
| 1336 | MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0); |
| 1337 | __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0); |
| 1338 | } |
| 1339 | } |
| 1340 | omap2_mcspi_disable_clocks(mcspi); |
| 1341 | return 0; |
| 1342 | } |
| 1343 | #else |
| 1344 | #define omap2_mcspi_resume NULL |
| 1345 | #endif |
| 1346 | |
| 1347 | static const struct dev_pm_ops omap2_mcspi_pm_ops = { |
| 1348 | .resume = omap2_mcspi_resume, |
| 1349 | }; |
| 1350 | |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1351 | static struct platform_driver omap2_mcspi_driver = { |
| 1352 | .driver = { |
| 1353 | .name = "omap2_mcspi", |
| 1354 | .owner = THIS_MODULE, |
Gregory CLEMENT | 42ce7fd | 2010-12-29 11:52:53 +0100 | [diff] [blame] | 1355 | .pm = &omap2_mcspi_pm_ops |
Samuel Ortiz | ccdc7bf | 2007-07-17 04:04:13 -0700 | [diff] [blame] | 1356 | }, |
| 1357 | .remove = __exit_p(omap2_mcspi_remove), |
| 1358 | }; |
| 1359 | |
| 1360 | |
| 1361 | static int __init omap2_mcspi_init(void) |
| 1362 | { |
| 1363 | omap2_mcspi_wq = create_singlethread_workqueue( |
| 1364 | omap2_mcspi_driver.driver.name); |
| 1365 | if (omap2_mcspi_wq == NULL) |
| 1366 | return -1; |
| 1367 | return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe); |
| 1368 | } |
| 1369 | subsys_initcall(omap2_mcspi_init); |
| 1370 | |
| 1371 | static void __exit omap2_mcspi_exit(void) |
| 1372 | { |
| 1373 | platform_driver_unregister(&omap2_mcspi_driver); |
| 1374 | |
| 1375 | destroy_workqueue(omap2_mcspi_wq); |
| 1376 | } |
| 1377 | module_exit(omap2_mcspi_exit); |
| 1378 | |
| 1379 | MODULE_LICENSE("GPL"); |