Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1 | /* |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 2 | * Freescale SPI controller driver. |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 3 | * |
| 4 | * Maintainer: Kumar Gala |
| 5 | * |
| 6 | * Copyright (C) 2006 Polycom, Inc. |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 7 | * Copyright 2010 Freescale Semiconductor, Inc. |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 8 | * |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 9 | * CPM SPI and QE buffer descriptors mode support: |
| 10 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 11 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 12 | * |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License as published by the |
| 15 | * Free Software Foundation; either version 2 of the License, or (at your |
| 16 | * option) any later version. |
| 17 | */ |
| 18 | #include <linux/module.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | #include <linux/kernel.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/irq.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 24 | #include <linux/spi/spi.h> |
| 25 | #include <linux/spi/spi_bitbang.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/fsl_devices.h> |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/mutex.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_platform.h> |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 33 | #include <linux/of_address.h> |
| 34 | #include <linux/of_irq.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 35 | #include <linux/gpio.h> |
| 36 | #include <linux/of_gpio.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 37 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 38 | #include "spi-fsl-lib.h" |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 39 | #include "spi-fsl-cpm.h" |
| 40 | #include "spi-fsl-spi.h" |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 41 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 42 | static void fsl_spi_change_mode(struct spi_device *spi) |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 43 | { |
| 44 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master); |
| 45 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 46 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
| 47 | __be32 __iomem *mode = ®_base->mode; |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 48 | unsigned long flags; |
| 49 | |
| 50 | if (cs->hw_mode == mpc8xxx_spi_read_reg(mode)) |
| 51 | return; |
| 52 | |
| 53 | /* Turn off IRQs locally to minimize time that SPI is disabled. */ |
| 54 | local_irq_save(flags); |
| 55 | |
| 56 | /* Turn off SPI unit prior changing mode */ |
| 57 | mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE); |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 58 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 59 | /* When in CPM mode, we need to reinit tx and rx. */ |
| 60 | if (mspi->flags & SPI_CPM_MODE) { |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 61 | fsl_spi_cpm_reinit_txrx(mspi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 62 | } |
Joakim Tjernlund | f9218c2 | 2010-05-22 02:18:02 -0600 | [diff] [blame] | 63 | mpc8xxx_spi_write_reg(mode, cs->hw_mode); |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 64 | local_irq_restore(flags); |
| 65 | } |
| 66 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 67 | static void fsl_spi_chipselect(struct spi_device *spi, int value) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 68 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 69 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Kenth Eriksson | 5039a86 | 2012-03-30 17:05:30 +0200 | [diff] [blame] | 70 | struct fsl_spi_platform_data *pdata; |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 71 | bool pol = spi->mode & SPI_CS_HIGH; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 72 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 73 | |
Kenth Eriksson | 5039a86 | 2012-03-30 17:05:30 +0200 | [diff] [blame] | 74 | pdata = spi->dev.parent->parent->platform_data; |
| 75 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 76 | if (value == BITBANG_CS_INACTIVE) { |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 77 | if (pdata->cs_control) |
| 78 | pdata->cs_control(spi, !pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (value == BITBANG_CS_ACTIVE) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 82 | mpc8xxx_spi->rx_shift = cs->rx_shift; |
| 83 | mpc8xxx_spi->tx_shift = cs->tx_shift; |
| 84 | mpc8xxx_spi->get_rx = cs->get_rx; |
| 85 | mpc8xxx_spi->get_tx = cs->get_tx; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 86 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 87 | fsl_spi_change_mode(spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 88 | |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 89 | if (pdata->cs_control) |
| 90 | pdata->cs_control(spi, pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 94 | static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs, |
| 95 | struct spi_device *spi, |
| 96 | struct mpc8xxx_spi *mpc8xxx_spi, |
| 97 | int bits_per_word) |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 98 | { |
| 99 | cs->rx_shift = 0; |
| 100 | cs->tx_shift = 0; |
| 101 | if (bits_per_word <= 8) { |
| 102 | cs->get_rx = mpc8xxx_spi_rx_buf_u8; |
| 103 | cs->get_tx = mpc8xxx_spi_tx_buf_u8; |
| 104 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { |
| 105 | cs->rx_shift = 16; |
| 106 | cs->tx_shift = 24; |
| 107 | } |
| 108 | } else if (bits_per_word <= 16) { |
| 109 | cs->get_rx = mpc8xxx_spi_rx_buf_u16; |
| 110 | cs->get_tx = mpc8xxx_spi_tx_buf_u16; |
| 111 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { |
| 112 | cs->rx_shift = 16; |
| 113 | cs->tx_shift = 16; |
| 114 | } |
| 115 | } else if (bits_per_word <= 32) { |
| 116 | cs->get_rx = mpc8xxx_spi_rx_buf_u32; |
| 117 | cs->get_tx = mpc8xxx_spi_tx_buf_u32; |
| 118 | } else |
| 119 | return -EINVAL; |
| 120 | |
| 121 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE && |
| 122 | spi->mode & SPI_LSB_FIRST) { |
| 123 | cs->tx_shift = 0; |
| 124 | if (bits_per_word <= 8) |
| 125 | cs->rx_shift = 8; |
| 126 | else |
| 127 | cs->rx_shift = 0; |
| 128 | } |
| 129 | mpc8xxx_spi->rx_shift = cs->rx_shift; |
| 130 | mpc8xxx_spi->tx_shift = cs->tx_shift; |
| 131 | mpc8xxx_spi->get_rx = cs->get_rx; |
| 132 | mpc8xxx_spi->get_tx = cs->get_tx; |
| 133 | |
| 134 | return bits_per_word; |
| 135 | } |
| 136 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 137 | static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs, |
| 138 | struct spi_device *spi, |
| 139 | int bits_per_word) |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 140 | { |
| 141 | /* QE uses Little Endian for words > 8 |
| 142 | * so transform all words > 8 into 8 bits |
| 143 | * Unfortnatly that doesn't work for LSB so |
| 144 | * reject these for now */ |
| 145 | /* Note: 32 bits word, LSB works iff |
| 146 | * tfcr/rfcr is set to CPMFCR_GBL */ |
| 147 | if (spi->mode & SPI_LSB_FIRST && |
| 148 | bits_per_word > 8) |
| 149 | return -EINVAL; |
| 150 | if (bits_per_word > 8) |
| 151 | return 8; /* pretend its 8 bits */ |
| 152 | return bits_per_word; |
| 153 | } |
| 154 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 155 | static int fsl_spi_setup_transfer(struct spi_device *spi, |
| 156 | struct spi_transfer *t) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 157 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 158 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 159 | int bits_per_word = 0; |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 160 | u8 pm; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 161 | u32 hz = 0; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 162 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 163 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 164 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 165 | |
| 166 | if (t) { |
| 167 | bits_per_word = t->bits_per_word; |
| 168 | hz = t->speed_hz; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* spi_transfer level calls that work per-word */ |
| 172 | if (!bits_per_word) |
| 173 | bits_per_word = spi->bits_per_word; |
| 174 | |
| 175 | /* Make sure its a bit width we support [4..16, 32] */ |
| 176 | if ((bits_per_word < 4) |
| 177 | || ((bits_per_word > 16) && (bits_per_word != 32))) |
| 178 | return -EINVAL; |
| 179 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 180 | if (!hz) |
| 181 | hz = spi->max_speed_hz; |
| 182 | |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 183 | if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) |
| 184 | bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi, |
| 185 | mpc8xxx_spi, |
| 186 | bits_per_word); |
| 187 | else if (mpc8xxx_spi->flags & SPI_QE) |
| 188 | bits_per_word = mspi_apply_qe_mode_quirks(cs, spi, |
| 189 | bits_per_word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 190 | |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 191 | if (bits_per_word < 0) |
| 192 | return bits_per_word; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 193 | |
| 194 | if (bits_per_word == 32) |
| 195 | bits_per_word = 0; |
| 196 | else |
| 197 | bits_per_word = bits_per_word - 1; |
| 198 | |
Anton Vorontsov | 32421da | 2007-07-31 00:38:41 -0700 | [diff] [blame] | 199 | /* mask out bits we are going to set */ |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 200 | cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16 |
| 201 | | SPMODE_PM(0xF)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 202 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 203 | cs->hw_mode |= SPMODE_LEN(bits_per_word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 204 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 205 | if ((mpc8xxx_spi->spibrg / hz) > 64) { |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 206 | cs->hw_mode |= SPMODE_DIV16; |
Ernst Schwab | 4f4517c | 2010-02-16 21:02:57 -0700 | [diff] [blame] | 207 | pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1; |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame] | 208 | |
| 209 | WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. " |
| 210 | "Will use %d Hz instead.\n", dev_name(&spi->dev), |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 211 | hz, mpc8xxx_spi->spibrg / 1024); |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame] | 212 | if (pm > 16) |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 213 | pm = 16; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 214 | } else { |
Ernst Schwab | 4f4517c | 2010-02-16 21:02:57 -0700 | [diff] [blame] | 215 | pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 216 | } |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 217 | if (pm) |
| 218 | pm--; |
| 219 | |
| 220 | cs->hw_mode |= SPMODE_PM(pm); |
David Brownell | dccd573 | 2007-07-17 04:04:02 -0700 | [diff] [blame] | 221 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 222 | fsl_spi_change_mode(spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 226 | static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi, |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 227 | struct spi_transfer *t, unsigned int len) |
| 228 | { |
| 229 | u32 word; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 230 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 231 | |
| 232 | mspi->count = len; |
| 233 | |
| 234 | /* enable rx ints */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 235 | mpc8xxx_spi_write_reg(®_base->mask, SPIM_NE); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 236 | |
| 237 | /* transmit word */ |
| 238 | word = mspi->get_tx(mspi); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 239 | mpc8xxx_spi_write_reg(®_base->transmit, word); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 244 | static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t, |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 245 | bool is_dma_mapped) |
| 246 | { |
| 247 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 248 | struct fsl_spi_reg *reg_base; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 249 | unsigned int len = t->len; |
| 250 | u8 bits_per_word; |
| 251 | int ret; |
| 252 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 253 | reg_base = mpc8xxx_spi->reg_base; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 254 | bits_per_word = spi->bits_per_word; |
| 255 | if (t->bits_per_word) |
| 256 | bits_per_word = t->bits_per_word; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 257 | |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 258 | if (bits_per_word > 8) { |
| 259 | /* invalid length? */ |
| 260 | if (len & 1) |
| 261 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 262 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 263 | } |
| 264 | if (bits_per_word > 16) { |
| 265 | /* invalid length? */ |
| 266 | if (len & 1) |
| 267 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 268 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 269 | } |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 270 | |
| 271 | mpc8xxx_spi->tx = t->tx_buf; |
| 272 | mpc8xxx_spi->rx = t->rx_buf; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 273 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 274 | INIT_COMPLETION(mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 275 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 276 | if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 277 | ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 278 | else |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 279 | ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 280 | if (ret) |
| 281 | return ret; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 282 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 283 | wait_for_completion(&mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 284 | |
| 285 | /* disable rx ints */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 286 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 287 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 288 | if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 289 | fsl_spi_cpm_bufs_complete(mpc8xxx_spi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 290 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 291 | return mpc8xxx_spi->count; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 294 | static void fsl_spi_do_one_msg(struct spi_message *m) |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 295 | { |
| 296 | struct spi_device *spi = m->spi; |
| 297 | struct spi_transfer *t; |
| 298 | unsigned int cs_change; |
| 299 | const int nsecs = 50; |
| 300 | int status; |
| 301 | |
| 302 | cs_change = 1; |
| 303 | status = 0; |
| 304 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 305 | if (t->bits_per_word || t->speed_hz) { |
| 306 | /* Don't allow changes if CS is active */ |
| 307 | status = -EINVAL; |
| 308 | |
| 309 | if (cs_change) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 310 | status = fsl_spi_setup_transfer(spi, t); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 311 | if (status < 0) |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | if (cs_change) { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 316 | fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 317 | ndelay(nsecs); |
| 318 | } |
| 319 | cs_change = t->cs_change; |
| 320 | if (t->len) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 321 | status = fsl_spi_bufs(spi, t, m->is_dma_mapped); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 322 | if (status) { |
| 323 | status = -EMSGSIZE; |
| 324 | break; |
| 325 | } |
| 326 | m->actual_length += t->len; |
| 327 | |
| 328 | if (t->delay_usecs) |
| 329 | udelay(t->delay_usecs); |
| 330 | |
| 331 | if (cs_change) { |
| 332 | ndelay(nsecs); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 333 | fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 334 | ndelay(nsecs); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | m->status = status; |
| 339 | m->complete(m->context); |
| 340 | |
| 341 | if (status || !cs_change) { |
| 342 | ndelay(nsecs); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 343 | fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 346 | fsl_spi_setup_transfer(spi, NULL); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 349 | static int fsl_spi_setup(struct spi_device *spi) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 350 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 351 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 352 | struct fsl_spi_reg *reg_base; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 353 | int retval; |
| 354 | u32 hw_mode; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 355 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 356 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 357 | if (!spi->max_speed_hz) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | if (!cs) { |
| 361 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
| 362 | if (!cs) |
| 363 | return -ENOMEM; |
| 364 | spi->controller_state = cs; |
| 365 | } |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 366 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 367 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 368 | reg_base = mpc8xxx_spi->reg_base; |
| 369 | |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 370 | hw_mode = cs->hw_mode; /* Save original settings */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 371 | cs->hw_mode = mpc8xxx_spi_read_reg(®_base->mode); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 372 | /* mask out bits we are going to set */ |
| 373 | cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH |
| 374 | | SPMODE_REV | SPMODE_LOOP); |
| 375 | |
| 376 | if (spi->mode & SPI_CPHA) |
| 377 | cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK; |
| 378 | if (spi->mode & SPI_CPOL) |
| 379 | cs->hw_mode |= SPMODE_CI_INACTIVEHIGH; |
| 380 | if (!(spi->mode & SPI_LSB_FIRST)) |
| 381 | cs->hw_mode |= SPMODE_REV; |
| 382 | if (spi->mode & SPI_LOOP) |
| 383 | cs->hw_mode |= SPMODE_LOOP; |
| 384 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 385 | retval = fsl_spi_setup_transfer(spi, NULL); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 386 | if (retval < 0) { |
| 387 | cs->hw_mode = hw_mode; /* Restore settings */ |
| 388 | return retval; |
| 389 | } |
Andreas Larsson | f482cd0 | 2013-02-15 16:52:22 +0100 | [diff] [blame^] | 390 | |
| 391 | /* Initialize chipselect - might be active for SPI_CS_HIGH mode */ |
| 392 | fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
| 393 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 394 | return 0; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 397 | static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 398 | { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 399 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
| 400 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 401 | /* We need handle RX first */ |
| 402 | if (events & SPIE_NE) { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 403 | u32 rx_data = mpc8xxx_spi_read_reg(®_base->receive); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 404 | |
| 405 | if (mspi->rx) |
| 406 | mspi->get_rx(rx_data, mspi); |
| 407 | } |
| 408 | |
| 409 | if ((events & SPIE_NF) == 0) |
| 410 | /* spin until TX is done */ |
| 411 | while (((events = |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 412 | mpc8xxx_spi_read_reg(®_base->event)) & |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 413 | SPIE_NF) == 0) |
| 414 | cpu_relax(); |
| 415 | |
| 416 | /* Clear the events */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 417 | mpc8xxx_spi_write_reg(®_base->event, events); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 418 | |
| 419 | mspi->count -= 1; |
| 420 | if (mspi->count) { |
| 421 | u32 word = mspi->get_tx(mspi); |
| 422 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 423 | mpc8xxx_spi_write_reg(®_base->transmit, word); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 424 | } else { |
| 425 | complete(&mspi->done); |
| 426 | } |
| 427 | } |
| 428 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 429 | static irqreturn_t fsl_spi_irq(s32 irq, void *context_data) |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 430 | { |
| 431 | struct mpc8xxx_spi *mspi = context_data; |
| 432 | irqreturn_t ret = IRQ_NONE; |
| 433 | u32 events; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 434 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 435 | |
| 436 | /* Get interrupt events(tx/rx) */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 437 | events = mpc8xxx_spi_read_reg(®_base->event); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 438 | if (events) |
| 439 | ret = IRQ_HANDLED; |
| 440 | |
| 441 | dev_dbg(mspi->dev, "%s: events %x\n", __func__, events); |
| 442 | |
| 443 | if (mspi->flags & SPI_CPM_MODE) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 444 | fsl_spi_cpm_irq(mspi, events); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 445 | else |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 446 | fsl_spi_cpu_irq(mspi, events); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 447 | |
| 448 | return ret; |
| 449 | } |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 450 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 451 | static void fsl_spi_remove(struct mpc8xxx_spi *mspi) |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 452 | { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 453 | iounmap(mspi->reg_base); |
| 454 | fsl_spi_cpm_free(mspi); |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 455 | } |
| 456 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 457 | static struct spi_master * fsl_spi_probe(struct device *dev, |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 458 | struct resource *mem, unsigned int irq) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 459 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 460 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 461 | struct spi_master *master; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 462 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 463 | struct fsl_spi_reg *reg_base; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 464 | u32 regval; |
| 465 | int ret = 0; |
| 466 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 467 | master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 468 | if (master == NULL) { |
| 469 | ret = -ENOMEM; |
| 470 | goto err; |
| 471 | } |
| 472 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 473 | dev_set_drvdata(dev, master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 474 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 475 | ret = mpc8xxx_spi_probe(dev, mem, irq); |
| 476 | if (ret) |
| 477 | goto err_probe; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 478 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 479 | master->setup = fsl_spi_setup; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 480 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 481 | mpc8xxx_spi = spi_master_get_devdata(master); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 482 | mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg; |
| 483 | mpc8xxx_spi->spi_remove = fsl_spi_remove; |
Anton Vorontsov | e24a4d1 | 2007-08-10 13:01:01 -0700 | [diff] [blame] | 484 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 485 | |
| 486 | ret = fsl_spi_cpm_init(mpc8xxx_spi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 487 | if (ret) |
| 488 | goto err_cpm_init; |
| 489 | |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 490 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 491 | mpc8xxx_spi->rx_shift = 16; |
| 492 | mpc8xxx_spi->tx_shift = 24; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 495 | mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem)); |
| 496 | if (mpc8xxx_spi->reg_base == NULL) { |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 497 | ret = -ENOMEM; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 498 | goto err_ioremap; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 501 | /* Register for SPI Interrupt */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 502 | ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq, |
| 503 | 0, "fsl_spi", mpc8xxx_spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 504 | |
| 505 | if (ret != 0) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 506 | goto free_irq; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 507 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 508 | reg_base = mpc8xxx_spi->reg_base; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 509 | |
| 510 | /* SPI controller initializations */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 511 | mpc8xxx_spi_write_reg(®_base->mode, 0); |
| 512 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
| 513 | mpc8xxx_spi_write_reg(®_base->command, 0); |
| 514 | mpc8xxx_spi_write_reg(®_base->event, 0xffffffff); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 515 | |
| 516 | /* Enable SPI interface */ |
| 517 | regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 518 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 519 | regval |= SPMODE_OP; |
| 520 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 521 | mpc8xxx_spi_write_reg(®_base->mode, regval); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 522 | |
| 523 | ret = spi_register_master(master); |
| 524 | if (ret < 0) |
| 525 | goto unreg_master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 526 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 527 | dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base, |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 528 | mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 529 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 530 | return master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 531 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 532 | unreg_master: |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 533 | free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 534 | free_irq: |
| 535 | iounmap(mpc8xxx_spi->reg_base); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 536 | err_ioremap: |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 537 | fsl_spi_cpm_free(mpc8xxx_spi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 538 | err_cpm_init: |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 539 | err_probe: |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 540 | spi_master_put(master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 541 | err: |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 542 | return ERR_PTR(ret); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 545 | static void fsl_spi_cs_control(struct spi_device *spi, bool on) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 546 | { |
Herton Ronaldo Krzesinski | 067aa48 | 2012-05-11 15:29:50 -0700 | [diff] [blame] | 547 | struct device *dev = spi->dev.parent->parent; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 548 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 549 | u16 cs = spi->chip_select; |
| 550 | int gpio = pinfo->gpios[cs]; |
| 551 | bool alow = pinfo->alow_flags[cs]; |
| 552 | |
| 553 | gpio_set_value(gpio, on ^ alow); |
| 554 | } |
| 555 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 556 | static int of_fsl_spi_get_chipselects(struct device *dev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 557 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 558 | struct device_node *np = dev->of_node; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 559 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 560 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 561 | int ngpios; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 562 | int i = 0; |
| 563 | int ret; |
| 564 | |
| 565 | ngpios = of_gpio_count(np); |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 566 | if (ngpios <= 0) { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 567 | /* |
| 568 | * SPI w/o chip-select line. One SPI device is still permitted |
| 569 | * though. |
| 570 | */ |
| 571 | pdata->max_chipselect = 1; |
| 572 | return 0; |
| 573 | } |
| 574 | |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 575 | pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 576 | if (!pinfo->gpios) |
| 577 | return -ENOMEM; |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 578 | memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios)); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 579 | |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 580 | pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags), |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 581 | GFP_KERNEL); |
| 582 | if (!pinfo->alow_flags) { |
| 583 | ret = -ENOMEM; |
| 584 | goto err_alloc_flags; |
| 585 | } |
| 586 | |
| 587 | for (; i < ngpios; i++) { |
| 588 | int gpio; |
| 589 | enum of_gpio_flags flags; |
| 590 | |
| 591 | gpio = of_get_gpio_flags(np, i, &flags); |
| 592 | if (!gpio_is_valid(gpio)) { |
| 593 | dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); |
Anton Vorontsov | 783058f | 2009-10-12 20:49:22 +0400 | [diff] [blame] | 594 | ret = gpio; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 595 | goto err_loop; |
| 596 | } |
| 597 | |
| 598 | ret = gpio_request(gpio, dev_name(dev)); |
| 599 | if (ret) { |
| 600 | dev_err(dev, "can't request gpio #%d: %d\n", i, ret); |
| 601 | goto err_loop; |
| 602 | } |
| 603 | |
| 604 | pinfo->gpios[i] = gpio; |
| 605 | pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW; |
| 606 | |
| 607 | ret = gpio_direction_output(pinfo->gpios[i], |
| 608 | pinfo->alow_flags[i]); |
| 609 | if (ret) { |
| 610 | dev_err(dev, "can't set output direction for gpio " |
| 611 | "#%d: %d\n", i, ret); |
| 612 | goto err_loop; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | pdata->max_chipselect = ngpios; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 617 | pdata->cs_control = fsl_spi_cs_control; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 618 | |
| 619 | return 0; |
| 620 | |
| 621 | err_loop: |
| 622 | while (i >= 0) { |
| 623 | if (gpio_is_valid(pinfo->gpios[i])) |
| 624 | gpio_free(pinfo->gpios[i]); |
| 625 | i--; |
| 626 | } |
| 627 | |
| 628 | kfree(pinfo->alow_flags); |
| 629 | pinfo->alow_flags = NULL; |
| 630 | err_alloc_flags: |
| 631 | kfree(pinfo->gpios); |
| 632 | pinfo->gpios = NULL; |
| 633 | return ret; |
| 634 | } |
| 635 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 636 | static int of_fsl_spi_free_chipselects(struct device *dev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 637 | { |
| 638 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 639 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 640 | int i; |
| 641 | |
| 642 | if (!pinfo->gpios) |
| 643 | return 0; |
| 644 | |
| 645 | for (i = 0; i < pdata->max_chipselect; i++) { |
| 646 | if (gpio_is_valid(pinfo->gpios[i])) |
| 647 | gpio_free(pinfo->gpios[i]); |
| 648 | } |
| 649 | |
| 650 | kfree(pinfo->gpios); |
| 651 | kfree(pinfo->alow_flags); |
| 652 | return 0; |
| 653 | } |
| 654 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 655 | static int of_fsl_spi_probe(struct platform_device *ofdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 656 | { |
| 657 | struct device *dev = &ofdev->dev; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 658 | struct device_node *np = ofdev->dev.of_node; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 659 | struct spi_master *master; |
| 660 | struct resource mem; |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 661 | int irq; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 662 | int ret = -ENOMEM; |
| 663 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 664 | ret = of_mpc8xxx_spi_probe(ofdev); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 665 | if (ret) |
| 666 | return ret; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 667 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 668 | ret = of_fsl_spi_get_chipselects(dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 669 | if (ret) |
| 670 | goto err; |
| 671 | |
| 672 | ret = of_address_to_resource(np, 0, &mem); |
| 673 | if (ret) |
| 674 | goto err; |
| 675 | |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 676 | irq = irq_of_parse_and_map(np, 0); |
| 677 | if (!irq) { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 678 | ret = -EINVAL; |
| 679 | goto err; |
| 680 | } |
| 681 | |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 682 | master = fsl_spi_probe(dev, &mem, irq); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 683 | if (IS_ERR(master)) { |
| 684 | ret = PTR_ERR(master); |
| 685 | goto err; |
| 686 | } |
| 687 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 688 | return 0; |
| 689 | |
| 690 | err: |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 691 | of_fsl_spi_free_chipselects(dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 692 | return ret; |
| 693 | } |
| 694 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 695 | static int of_fsl_spi_remove(struct platform_device *ofdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 696 | { |
| 697 | int ret; |
| 698 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 699 | ret = mpc8xxx_spi_remove(&ofdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 700 | if (ret) |
| 701 | return ret; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 702 | of_fsl_spi_free_chipselects(&ofdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 706 | static const struct of_device_id of_fsl_spi_match[] = { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 707 | { .compatible = "fsl,spi" }, |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 708 | {} |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 709 | }; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 710 | MODULE_DEVICE_TABLE(of, of_fsl_spi_match); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 711 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 712 | static struct platform_driver of_fsl_spi_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 713 | .driver = { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 714 | .name = "fsl_spi", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 715 | .owner = THIS_MODULE, |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 716 | .of_match_table = of_fsl_spi_match, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 717 | }, |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 718 | .probe = of_fsl_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 719 | .remove = of_fsl_spi_remove, |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | #ifdef CONFIG_MPC832x_RDB |
| 723 | /* |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 724 | * XXX XXX XXX |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 725 | * This is "legacy" platform driver, was used by the MPC8323E-RDB boards |
| 726 | * only. The driver should go away soon, since newer MPC8323E-RDB's device |
| 727 | * tree can work with OpenFirmware driver. But for now we support old trees |
| 728 | * as well. |
| 729 | */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 730 | static int plat_mpc8xxx_spi_probe(struct platform_device *pdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 731 | { |
| 732 | struct resource *mem; |
Uwe Kleine-König | e9a172f | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 733 | int irq; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 734 | struct spi_master *master; |
| 735 | |
| 736 | if (!pdev->dev.platform_data) |
| 737 | return -EINVAL; |
| 738 | |
| 739 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 740 | if (!mem) |
| 741 | return -EINVAL; |
| 742 | |
| 743 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | e9a172f | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 744 | if (irq <= 0) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 745 | return -EINVAL; |
| 746 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 747 | master = fsl_spi_probe(&pdev->dev, mem, irq); |
Alexandru Gheorghiu | e4d4378 | 2013-03-14 11:07:31 +0200 | [diff] [blame] | 748 | return PTR_RET(master); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 751 | static int plat_mpc8xxx_spi_remove(struct platform_device *pdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 752 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 753 | return mpc8xxx_spi_remove(&pdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 756 | MODULE_ALIAS("platform:mpc8xxx_spi"); |
| 757 | static struct platform_driver mpc8xxx_spi_driver = { |
| 758 | .probe = plat_mpc8xxx_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 759 | .remove = plat_mpc8xxx_spi_remove, |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 760 | .driver = { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 761 | .name = "mpc8xxx_spi", |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 762 | .owner = THIS_MODULE, |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 763 | }, |
| 764 | }; |
| 765 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 766 | static bool legacy_driver_failed; |
| 767 | |
| 768 | static void __init legacy_driver_register(void) |
| 769 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 770 | legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void __exit legacy_driver_unregister(void) |
| 774 | { |
| 775 | if (legacy_driver_failed) |
| 776 | return; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 777 | platform_driver_unregister(&mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 778 | } |
| 779 | #else |
| 780 | static void __init legacy_driver_register(void) {} |
| 781 | static void __exit legacy_driver_unregister(void) {} |
| 782 | #endif /* CONFIG_MPC832x_RDB */ |
| 783 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 784 | static int __init fsl_spi_init(void) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 785 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 786 | legacy_driver_register(); |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 787 | return platform_driver_register(&of_fsl_spi_driver); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 788 | } |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 789 | module_init(fsl_spi_init); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 790 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 791 | static void __exit fsl_spi_exit(void) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 792 | { |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 793 | platform_driver_unregister(&of_fsl_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 794 | legacy_driver_unregister(); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 795 | } |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 796 | module_exit(fsl_spi_exit); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 797 | |
| 798 | MODULE_AUTHOR("Kumar Gala"); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 799 | MODULE_DESCRIPTION("Simple Freescale SPI Driver"); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 800 | MODULE_LICENSE("GPL"); |