Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MPC83xx SPI controller driver. |
| 3 | * |
| 4 | * Maintainer: Kumar Gala |
| 5 | * |
| 6 | * Copyright (C) 2006 Polycom, Inc. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame^] | 17 | #include <linux/bug.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
| 19 | #include <linux/err.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 20 | #include <linux/completion.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/spi/spi.h> |
| 26 | #include <linux/spi/spi_bitbang.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/fsl_devices.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_platform.h> |
| 31 | #include <linux/gpio.h> |
| 32 | #include <linux/of_gpio.h> |
| 33 | #include <linux/of_spi.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 34 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 35 | #include <sysdev/fsl_soc.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 36 | #include <asm/irq.h> |
| 37 | #include <asm/io.h> |
| 38 | |
| 39 | /* SPI Controller registers */ |
| 40 | struct mpc83xx_spi_reg { |
| 41 | u8 res1[0x20]; |
| 42 | __be32 mode; |
| 43 | __be32 event; |
| 44 | __be32 mask; |
| 45 | __be32 command; |
| 46 | __be32 transmit; |
| 47 | __be32 receive; |
| 48 | }; |
| 49 | |
| 50 | /* SPI Controller mode register definitions */ |
Anton Vorontsov | 2a485d7 | 2007-07-31 00:38:45 -0700 | [diff] [blame] | 51 | #define SPMODE_LOOP (1 << 30) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 52 | #define SPMODE_CI_INACTIVEHIGH (1 << 29) |
| 53 | #define SPMODE_CP_BEGIN_EDGECLK (1 << 28) |
| 54 | #define SPMODE_DIV16 (1 << 27) |
| 55 | #define SPMODE_REV (1 << 26) |
| 56 | #define SPMODE_MS (1 << 25) |
| 57 | #define SPMODE_ENABLE (1 << 24) |
| 58 | #define SPMODE_LEN(x) ((x) << 20) |
| 59 | #define SPMODE_PM(x) ((x) << 16) |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 60 | #define SPMODE_OP (1 << 14) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 61 | #define SPMODE_CG(x) ((x) << 7) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Default for SPI Mode: |
| 65 | * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk |
| 66 | */ |
| 67 | #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \ |
| 68 | SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf)) |
| 69 | |
| 70 | /* SPIE register values */ |
| 71 | #define SPIE_NE 0x00000200 /* Not empty */ |
| 72 | #define SPIE_NF 0x00000100 /* Not full */ |
| 73 | |
| 74 | /* SPIM register values */ |
| 75 | #define SPIM_NE 0x00000200 /* Not empty */ |
| 76 | #define SPIM_NF 0x00000100 /* Not full */ |
| 77 | |
| 78 | /* SPI Controller driver's private data. */ |
| 79 | struct mpc83xx_spi { |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 80 | struct mpc83xx_spi_reg __iomem *base; |
| 81 | |
| 82 | /* rx & tx bufs from the spi_transfer */ |
| 83 | const void *tx; |
| 84 | void *rx; |
| 85 | |
| 86 | /* functions to deal with different sized buffers */ |
| 87 | void (*get_rx) (u32 rx_data, struct mpc83xx_spi *); |
| 88 | u32(*get_tx) (struct mpc83xx_spi *); |
| 89 | |
| 90 | unsigned int count; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 91 | unsigned int irq; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 92 | |
| 93 | unsigned nsecs; /* (clock cycle time)/2 */ |
| 94 | |
Anton Vorontsov | e24a4d1 | 2007-08-10 13:01:01 -0700 | [diff] [blame] | 95 | u32 spibrg; /* SPIBRG input clock */ |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 96 | u32 rx_shift; /* RX data reg shift when in qe mode */ |
| 97 | u32 tx_shift; /* TX data reg shift when in qe mode */ |
| 98 | |
| 99 | bool qe_mode; |
| 100 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 101 | u8 busy; |
| 102 | |
| 103 | struct workqueue_struct *workqueue; |
| 104 | struct work_struct work; |
| 105 | |
| 106 | struct list_head queue; |
| 107 | spinlock_t lock; |
| 108 | |
| 109 | struct completion done; |
| 110 | }; |
| 111 | |
| 112 | struct spi_mpc83xx_cs { |
| 113 | /* functions to deal with different sized buffers */ |
| 114 | void (*get_rx) (u32 rx_data, struct mpc83xx_spi *); |
| 115 | u32 (*get_tx) (struct mpc83xx_spi *); |
| 116 | u32 rx_shift; /* RX data reg shift when in qe mode */ |
| 117 | u32 tx_shift; /* TX data reg shift when in qe mode */ |
| 118 | u32 hw_mode; /* Holds HW mode register settings */ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val) |
| 122 | { |
| 123 | out_be32(reg, val); |
| 124 | } |
| 125 | |
| 126 | static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg) |
| 127 | { |
| 128 | return in_be32(reg); |
| 129 | } |
| 130 | |
| 131 | #define MPC83XX_SPI_RX_BUF(type) \ |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 132 | static \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 133 | void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \ |
| 134 | { \ |
| 135 | type * rx = mpc83xx_spi->rx; \ |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 136 | *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 137 | mpc83xx_spi->rx = rx; \ |
| 138 | } |
| 139 | |
| 140 | #define MPC83XX_SPI_TX_BUF(type) \ |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 141 | static \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 142 | u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \ |
| 143 | { \ |
| 144 | u32 data; \ |
| 145 | const type * tx = mpc83xx_spi->tx; \ |
David Brownell | 4b1badf | 2006-12-29 16:48:39 -0800 | [diff] [blame] | 146 | if (!tx) \ |
| 147 | return 0; \ |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 148 | data = *tx++ << mpc83xx_spi->tx_shift; \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 149 | mpc83xx_spi->tx = tx; \ |
| 150 | return data; \ |
| 151 | } |
| 152 | |
| 153 | MPC83XX_SPI_RX_BUF(u8) |
| 154 | MPC83XX_SPI_RX_BUF(u16) |
| 155 | MPC83XX_SPI_RX_BUF(u32) |
| 156 | MPC83XX_SPI_TX_BUF(u8) |
| 157 | MPC83XX_SPI_TX_BUF(u16) |
| 158 | MPC83XX_SPI_TX_BUF(u32) |
| 159 | |
| 160 | static void mpc83xx_spi_chipselect(struct spi_device *spi, int value) |
| 161 | { |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 162 | struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master); |
| 163 | struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data; |
| 164 | bool pol = spi->mode & SPI_CS_HIGH; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 165 | struct spi_mpc83xx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 166 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 167 | if (value == BITBANG_CS_INACTIVE) { |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 168 | if (pdata->cs_control) |
| 169 | pdata->cs_control(spi, !pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if (value == BITBANG_CS_ACTIVE) { |
| 173 | u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode); |
Anton Vorontsov | a44648b | 2007-08-10 13:01:02 -0700 | [diff] [blame] | 174 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 175 | mpc83xx_spi->rx_shift = cs->rx_shift; |
| 176 | mpc83xx_spi->tx_shift = cs->tx_shift; |
| 177 | mpc83xx_spi->get_rx = cs->get_rx; |
| 178 | mpc83xx_spi->get_tx = cs->get_tx; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 179 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 180 | if (cs->hw_mode != regval) { |
| 181 | unsigned long flags; |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 182 | __be32 __iomem *mode = &mpc83xx_spi->base->mode; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 183 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 184 | regval = cs->hw_mode; |
| 185 | /* Turn off IRQs locally to minimize time that |
| 186 | * SPI is disabled |
| 187 | */ |
| 188 | local_irq_save(flags); |
| 189 | /* Turn off SPI unit prior changing mode */ |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 190 | mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE); |
| 191 | mpc83xx_spi_write_reg(mode, regval); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 192 | local_irq_restore(flags); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 193 | } |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 194 | if (pdata->cs_control) |
| 195 | pdata->cs_control(spi, pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | static |
| 200 | int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t) |
| 201 | { |
| 202 | struct mpc83xx_spi *mpc83xx_spi; |
| 203 | u32 regval; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 204 | u8 bits_per_word, pm; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 205 | u32 hz; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 206 | struct spi_mpc83xx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 207 | |
| 208 | mpc83xx_spi = spi_master_get_devdata(spi->master); |
| 209 | |
| 210 | if (t) { |
| 211 | bits_per_word = t->bits_per_word; |
| 212 | hz = t->speed_hz; |
| 213 | } else { |
| 214 | bits_per_word = 0; |
| 215 | hz = 0; |
| 216 | } |
| 217 | |
| 218 | /* spi_transfer level calls that work per-word */ |
| 219 | if (!bits_per_word) |
| 220 | bits_per_word = spi->bits_per_word; |
| 221 | |
| 222 | /* Make sure its a bit width we support [4..16, 32] */ |
| 223 | if ((bits_per_word < 4) |
| 224 | || ((bits_per_word > 16) && (bits_per_word != 32))) |
| 225 | return -EINVAL; |
| 226 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 227 | if (!hz) |
| 228 | hz = spi->max_speed_hz; |
| 229 | |
| 230 | cs->rx_shift = 0; |
| 231 | cs->tx_shift = 0; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 232 | if (bits_per_word <= 8) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 233 | cs->get_rx = mpc83xx_spi_rx_buf_u8; |
| 234 | cs->get_tx = mpc83xx_spi_tx_buf_u8; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 235 | if (mpc83xx_spi->qe_mode) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 236 | cs->rx_shift = 16; |
| 237 | cs->tx_shift = 24; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 238 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 239 | } else if (bits_per_word <= 16) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 240 | cs->get_rx = mpc83xx_spi_rx_buf_u16; |
| 241 | cs->get_tx = mpc83xx_spi_tx_buf_u16; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 242 | if (mpc83xx_spi->qe_mode) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 243 | cs->rx_shift = 16; |
| 244 | cs->tx_shift = 16; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 245 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 246 | } else if (bits_per_word <= 32) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 247 | cs->get_rx = mpc83xx_spi_rx_buf_u32; |
| 248 | cs->get_tx = mpc83xx_spi_tx_buf_u32; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 249 | } else |
| 250 | return -EINVAL; |
| 251 | |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 252 | if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 253 | cs->tx_shift = 0; |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 254 | if (bits_per_word <= 8) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 255 | cs->rx_shift = 8; |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 256 | else |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 257 | cs->rx_shift = 0; |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 260 | mpc83xx_spi->rx_shift = cs->rx_shift; |
| 261 | mpc83xx_spi->tx_shift = cs->tx_shift; |
| 262 | mpc83xx_spi->get_rx = cs->get_rx; |
| 263 | mpc83xx_spi->get_tx = cs->get_tx; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 264 | |
| 265 | if (bits_per_word == 32) |
| 266 | bits_per_word = 0; |
| 267 | else |
| 268 | bits_per_word = bits_per_word - 1; |
| 269 | |
Anton Vorontsov | 32421da | 2007-07-31 00:38:41 -0700 | [diff] [blame] | 270 | /* mask out bits we are going to set */ |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 271 | cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16 |
| 272 | | SPMODE_PM(0xF)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 273 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 274 | cs->hw_mode |= SPMODE_LEN(bits_per_word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 275 | |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 276 | if ((mpc83xx_spi->spibrg / hz) > 64) { |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 277 | cs->hw_mode |= SPMODE_DIV16; |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 278 | pm = mpc83xx_spi->spibrg / (hz * 64); |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame^] | 279 | |
| 280 | WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. " |
| 281 | "Will use %d Hz instead.\n", dev_name(&spi->dev), |
| 282 | hz, mpc83xx_spi->spibrg / 1024); |
| 283 | if (pm > 16) |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 284 | pm = 16; |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 285 | } else |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 286 | pm = mpc83xx_spi->spibrg / (hz * 4); |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 287 | if (pm) |
| 288 | pm--; |
| 289 | |
| 290 | cs->hw_mode |= SPMODE_PM(pm); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 291 | regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode); |
| 292 | if (cs->hw_mode != regval) { |
| 293 | unsigned long flags; |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 294 | __be32 __iomem *mode = &mpc83xx_spi->base->mode; |
David Brownell | dccd573 | 2007-07-17 04:04:02 -0700 | [diff] [blame] | 295 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 296 | regval = cs->hw_mode; |
| 297 | /* Turn off IRQs locally to minimize time |
| 298 | * that SPI is disabled |
| 299 | */ |
| 300 | local_irq_save(flags); |
| 301 | /* Turn off SPI unit prior changing mode */ |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 302 | mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE); |
| 303 | mpc83xx_spi_write_reg(mode, regval); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 304 | local_irq_restore(flags); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 305 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t) |
| 310 | { |
| 311 | struct mpc83xx_spi *mpc83xx_spi; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 312 | u32 word, len, bits_per_word; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 313 | |
| 314 | mpc83xx_spi = spi_master_get_devdata(spi->master); |
| 315 | |
| 316 | mpc83xx_spi->tx = t->tx_buf; |
| 317 | mpc83xx_spi->rx = t->rx_buf; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 318 | bits_per_word = spi->bits_per_word; |
| 319 | if (t->bits_per_word) |
| 320 | bits_per_word = t->bits_per_word; |
| 321 | len = t->len; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 322 | if (bits_per_word > 8) { |
| 323 | /* invalid length? */ |
| 324 | if (len & 1) |
| 325 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 326 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 327 | } |
| 328 | if (bits_per_word > 16) { |
| 329 | /* invalid length? */ |
| 330 | if (len & 1) |
| 331 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 332 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 333 | } |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 334 | mpc83xx_spi->count = len; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 335 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 336 | INIT_COMPLETION(mpc83xx_spi->done); |
| 337 | |
| 338 | /* enable rx ints */ |
| 339 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE); |
| 340 | |
| 341 | /* transmit word */ |
| 342 | word = mpc83xx_spi->get_tx(mpc83xx_spi); |
| 343 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word); |
| 344 | |
| 345 | wait_for_completion(&mpc83xx_spi->done); |
| 346 | |
| 347 | /* disable rx ints */ |
| 348 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0); |
| 349 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 350 | return mpc83xx_spi->count; |
| 351 | } |
| 352 | |
| 353 | static void mpc83xx_spi_work(struct work_struct *work) |
| 354 | { |
| 355 | struct mpc83xx_spi *mpc83xx_spi = |
| 356 | container_of(work, struct mpc83xx_spi, work); |
| 357 | |
| 358 | spin_lock_irq(&mpc83xx_spi->lock); |
| 359 | mpc83xx_spi->busy = 1; |
| 360 | while (!list_empty(&mpc83xx_spi->queue)) { |
| 361 | struct spi_message *m; |
| 362 | struct spi_device *spi; |
| 363 | struct spi_transfer *t = NULL; |
| 364 | unsigned cs_change; |
| 365 | int status, nsecs = 50; |
| 366 | |
| 367 | m = container_of(mpc83xx_spi->queue.next, |
| 368 | struct spi_message, queue); |
| 369 | list_del_init(&m->queue); |
| 370 | spin_unlock_irq(&mpc83xx_spi->lock); |
| 371 | |
| 372 | spi = m->spi; |
| 373 | cs_change = 1; |
| 374 | status = 0; |
| 375 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 376 | if (t->bits_per_word || t->speed_hz) { |
| 377 | /* Don't allow changes if CS is active */ |
| 378 | status = -EINVAL; |
| 379 | |
| 380 | if (cs_change) |
| 381 | status = mpc83xx_spi_setup_transfer(spi, t); |
| 382 | if (status < 0) |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | if (cs_change) |
| 387 | mpc83xx_spi_chipselect(spi, BITBANG_CS_ACTIVE); |
| 388 | cs_change = t->cs_change; |
| 389 | if (t->len) |
| 390 | status = mpc83xx_spi_bufs(spi, t); |
| 391 | if (status) { |
| 392 | status = -EMSGSIZE; |
| 393 | break; |
| 394 | } |
| 395 | m->actual_length += t->len; |
| 396 | |
| 397 | if (t->delay_usecs) |
| 398 | udelay(t->delay_usecs); |
| 399 | |
| 400 | if (cs_change) { |
| 401 | ndelay(nsecs); |
| 402 | mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
| 403 | ndelay(nsecs); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | m->status = status; |
| 408 | m->complete(m->context); |
| 409 | |
| 410 | if (status || !cs_change) { |
| 411 | ndelay(nsecs); |
| 412 | mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
| 413 | } |
| 414 | |
| 415 | mpc83xx_spi_setup_transfer(spi, NULL); |
| 416 | |
| 417 | spin_lock_irq(&mpc83xx_spi->lock); |
| 418 | } |
| 419 | mpc83xx_spi->busy = 0; |
| 420 | spin_unlock_irq(&mpc83xx_spi->lock); |
| 421 | } |
| 422 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 423 | static int mpc83xx_spi_setup(struct spi_device *spi) |
| 424 | { |
| 425 | struct mpc83xx_spi *mpc83xx_spi; |
| 426 | int retval; |
| 427 | u32 hw_mode; |
| 428 | struct spi_mpc83xx_cs *cs = spi->controller_state; |
| 429 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 430 | if (!spi->max_speed_hz) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | if (!cs) { |
| 434 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
| 435 | if (!cs) |
| 436 | return -ENOMEM; |
| 437 | spi->controller_state = cs; |
| 438 | } |
| 439 | mpc83xx_spi = spi_master_get_devdata(spi->master); |
| 440 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 441 | hw_mode = cs->hw_mode; /* Save orginal settings */ |
| 442 | cs->hw_mode = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode); |
| 443 | /* mask out bits we are going to set */ |
| 444 | cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH |
| 445 | | SPMODE_REV | SPMODE_LOOP); |
| 446 | |
| 447 | if (spi->mode & SPI_CPHA) |
| 448 | cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK; |
| 449 | if (spi->mode & SPI_CPOL) |
| 450 | cs->hw_mode |= SPMODE_CI_INACTIVEHIGH; |
| 451 | if (!(spi->mode & SPI_LSB_FIRST)) |
| 452 | cs->hw_mode |= SPMODE_REV; |
| 453 | if (spi->mode & SPI_LOOP) |
| 454 | cs->hw_mode |= SPMODE_LOOP; |
| 455 | |
| 456 | retval = mpc83xx_spi_setup_transfer(spi, NULL); |
| 457 | if (retval < 0) { |
| 458 | cs->hw_mode = hw_mode; /* Restore settings */ |
| 459 | return retval; |
| 460 | } |
| 461 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 462 | #if 0 /* Don't think this is needed */ |
| 463 | /* NOTE we _need_ to call chipselect() early, ideally with adapter |
| 464 | * setup, unless the hardware defaults cooperate to avoid confusion |
| 465 | * between normal (active low) and inverted chipselects. |
| 466 | */ |
| 467 | |
| 468 | /* deselect chip (low or high) */ |
| 469 | spin_lock(&mpc83xx_spi->lock); |
| 470 | if (!mpc83xx_spi->busy) |
| 471 | mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
| 472 | spin_unlock(&mpc83xx_spi->lock); |
| 473 | #endif |
| 474 | return 0; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 477 | static irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 478 | { |
| 479 | struct mpc83xx_spi *mpc83xx_spi = context_data; |
| 480 | u32 event; |
| 481 | irqreturn_t ret = IRQ_NONE; |
| 482 | |
| 483 | /* Get interrupt events(tx/rx) */ |
| 484 | event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event); |
| 485 | |
| 486 | /* We need handle RX first */ |
| 487 | if (event & SPIE_NE) { |
| 488 | u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive); |
| 489 | |
| 490 | if (mpc83xx_spi->rx) |
| 491 | mpc83xx_spi->get_rx(rx_data, mpc83xx_spi); |
| 492 | |
| 493 | ret = IRQ_HANDLED; |
| 494 | } |
| 495 | |
| 496 | if ((event & SPIE_NF) == 0) |
| 497 | /* spin until TX is done */ |
| 498 | while (((event = |
| 499 | mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) & |
| 500 | SPIE_NF) == 0) |
| 501 | cpu_relax(); |
| 502 | |
| 503 | mpc83xx_spi->count -= 1; |
| 504 | if (mpc83xx_spi->count) { |
Jan Andersson | 65e213c | 2007-09-11 15:23:30 -0700 | [diff] [blame] | 505 | u32 word = mpc83xx_spi->get_tx(mpc83xx_spi); |
| 506 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 507 | } else { |
| 508 | complete(&mpc83xx_spi->done); |
| 509 | } |
| 510 | |
| 511 | /* Clear the events */ |
| 512 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event); |
| 513 | |
| 514 | return ret; |
| 515 | } |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 516 | static int mpc83xx_spi_transfer(struct spi_device *spi, |
| 517 | struct spi_message *m) |
| 518 | { |
| 519 | struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master); |
| 520 | unsigned long flags; |
| 521 | |
| 522 | m->actual_length = 0; |
| 523 | m->status = -EINPROGRESS; |
| 524 | |
| 525 | spin_lock_irqsave(&mpc83xx_spi->lock, flags); |
| 526 | list_add_tail(&m->queue, &mpc83xx_spi->queue); |
| 527 | queue_work(mpc83xx_spi->workqueue, &mpc83xx_spi->work); |
| 528 | spin_unlock_irqrestore(&mpc83xx_spi->lock, flags); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | static void mpc83xx_spi_cleanup(struct spi_device *spi) |
| 535 | { |
| 536 | kfree(spi->controller_state); |
| 537 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 538 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 539 | static struct spi_master * __devinit |
| 540 | mpc83xx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 541 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 542 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 543 | struct spi_master *master; |
| 544 | struct mpc83xx_spi *mpc83xx_spi; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 545 | u32 regval; |
| 546 | int ret = 0; |
| 547 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 548 | master = spi_alloc_master(dev, sizeof(struct mpc83xx_spi)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 549 | if (master == NULL) { |
| 550 | ret = -ENOMEM; |
| 551 | goto err; |
| 552 | } |
| 553 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 554 | dev_set_drvdata(dev, master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 555 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 556 | /* the spi->mode bits understood by this driver: */ |
| 557 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
| 558 | | SPI_LSB_FIRST | SPI_LOOP; |
| 559 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 560 | master->setup = mpc83xx_spi_setup; |
| 561 | master->transfer = mpc83xx_spi_transfer; |
| 562 | master->cleanup = mpc83xx_spi_cleanup; |
| 563 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 564 | mpc83xx_spi = spi_master_get_devdata(master); |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 565 | mpc83xx_spi->qe_mode = pdata->qe_mode; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 566 | mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8; |
| 567 | mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8; |
Anton Vorontsov | 59a0ea5 | 2008-01-24 18:40:03 +0300 | [diff] [blame] | 568 | mpc83xx_spi->spibrg = pdata->sysclk; |
Anton Vorontsov | e24a4d1 | 2007-08-10 13:01:01 -0700 | [diff] [blame] | 569 | |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 570 | mpc83xx_spi->rx_shift = 0; |
| 571 | mpc83xx_spi->tx_shift = 0; |
| 572 | if (mpc83xx_spi->qe_mode) { |
| 573 | mpc83xx_spi->rx_shift = 16; |
| 574 | mpc83xx_spi->tx_shift = 24; |
| 575 | } |
| 576 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 577 | init_completion(&mpc83xx_spi->done); |
| 578 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 579 | mpc83xx_spi->base = ioremap(mem->start, mem->end - mem->start + 1); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 580 | if (mpc83xx_spi->base == NULL) { |
| 581 | ret = -ENOMEM; |
| 582 | goto put_master; |
| 583 | } |
| 584 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 585 | mpc83xx_spi->irq = irq; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 586 | |
| 587 | /* Register for SPI Interrupt */ |
| 588 | ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq, |
| 589 | 0, "mpc83xx_spi", mpc83xx_spi); |
| 590 | |
| 591 | if (ret != 0) |
| 592 | goto unmap_io; |
| 593 | |
| 594 | master->bus_num = pdata->bus_num; |
| 595 | master->num_chipselect = pdata->max_chipselect; |
| 596 | |
| 597 | /* SPI controller initializations */ |
| 598 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0); |
| 599 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0); |
| 600 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0); |
| 601 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff); |
| 602 | |
| 603 | /* Enable SPI interface */ |
| 604 | regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 605 | if (pdata->qe_mode) |
| 606 | regval |= SPMODE_OP; |
| 607 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 608 | mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 609 | spin_lock_init(&mpc83xx_spi->lock); |
| 610 | init_completion(&mpc83xx_spi->done); |
| 611 | INIT_WORK(&mpc83xx_spi->work, mpc83xx_spi_work); |
| 612 | INIT_LIST_HEAD(&mpc83xx_spi->queue); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 613 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 614 | mpc83xx_spi->workqueue = create_singlethread_workqueue( |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 615 | dev_name(master->dev.parent)); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 616 | if (mpc83xx_spi->workqueue == NULL) { |
| 617 | ret = -EBUSY; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 618 | goto free_irq; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | ret = spi_register_master(master); |
| 622 | if (ret < 0) |
| 623 | goto unreg_master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 624 | |
| 625 | printk(KERN_INFO |
| 626 | "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n", |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 627 | dev_name(dev), mpc83xx_spi->base, mpc83xx_spi->irq); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 628 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 629 | return master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 630 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 631 | unreg_master: |
| 632 | destroy_workqueue(mpc83xx_spi->workqueue); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 633 | free_irq: |
| 634 | free_irq(mpc83xx_spi->irq, mpc83xx_spi); |
| 635 | unmap_io: |
| 636 | iounmap(mpc83xx_spi->base); |
| 637 | put_master: |
| 638 | spi_master_put(master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 639 | err: |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 640 | return ERR_PTR(ret); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 643 | static int __devexit mpc83xx_spi_remove(struct device *dev) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 644 | { |
| 645 | struct mpc83xx_spi *mpc83xx_spi; |
| 646 | struct spi_master *master; |
| 647 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 648 | master = dev_get_drvdata(dev); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 649 | mpc83xx_spi = spi_master_get_devdata(master); |
| 650 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 651 | flush_workqueue(mpc83xx_spi->workqueue); |
| 652 | destroy_workqueue(mpc83xx_spi->workqueue); |
| 653 | spi_unregister_master(master); |
| 654 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 655 | free_irq(mpc83xx_spi->irq, mpc83xx_spi); |
| 656 | iounmap(mpc83xx_spi->base); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 661 | struct mpc83xx_spi_probe_info { |
| 662 | struct fsl_spi_platform_data pdata; |
| 663 | int *gpios; |
| 664 | bool *alow_flags; |
| 665 | }; |
| 666 | |
| 667 | static struct mpc83xx_spi_probe_info * |
| 668 | to_of_pinfo(struct fsl_spi_platform_data *pdata) |
| 669 | { |
| 670 | return container_of(pdata, struct mpc83xx_spi_probe_info, pdata); |
| 671 | } |
| 672 | |
| 673 | static void mpc83xx_spi_cs_control(struct spi_device *spi, bool on) |
| 674 | { |
| 675 | struct device *dev = spi->dev.parent; |
| 676 | struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data); |
| 677 | u16 cs = spi->chip_select; |
| 678 | int gpio = pinfo->gpios[cs]; |
| 679 | bool alow = pinfo->alow_flags[cs]; |
| 680 | |
| 681 | gpio_set_value(gpio, on ^ alow); |
| 682 | } |
| 683 | |
| 684 | static int of_mpc83xx_spi_get_chipselects(struct device *dev) |
| 685 | { |
| 686 | struct device_node *np = dev_archdata_get_node(&dev->archdata); |
| 687 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
| 688 | struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
| 689 | unsigned int ngpios; |
| 690 | int i = 0; |
| 691 | int ret; |
| 692 | |
| 693 | ngpios = of_gpio_count(np); |
| 694 | if (!ngpios) { |
| 695 | /* |
| 696 | * SPI w/o chip-select line. One SPI device is still permitted |
| 697 | * though. |
| 698 | */ |
| 699 | pdata->max_chipselect = 1; |
| 700 | return 0; |
| 701 | } |
| 702 | |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 703 | pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 704 | if (!pinfo->gpios) |
| 705 | return -ENOMEM; |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 706 | memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios)); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 707 | |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 708 | pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags), |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 709 | GFP_KERNEL); |
| 710 | if (!pinfo->alow_flags) { |
| 711 | ret = -ENOMEM; |
| 712 | goto err_alloc_flags; |
| 713 | } |
| 714 | |
| 715 | for (; i < ngpios; i++) { |
| 716 | int gpio; |
| 717 | enum of_gpio_flags flags; |
| 718 | |
| 719 | gpio = of_get_gpio_flags(np, i, &flags); |
| 720 | if (!gpio_is_valid(gpio)) { |
| 721 | dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); |
| 722 | goto err_loop; |
| 723 | } |
| 724 | |
| 725 | ret = gpio_request(gpio, dev_name(dev)); |
| 726 | if (ret) { |
| 727 | dev_err(dev, "can't request gpio #%d: %d\n", i, ret); |
| 728 | goto err_loop; |
| 729 | } |
| 730 | |
| 731 | pinfo->gpios[i] = gpio; |
| 732 | pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW; |
| 733 | |
| 734 | ret = gpio_direction_output(pinfo->gpios[i], |
| 735 | pinfo->alow_flags[i]); |
| 736 | if (ret) { |
| 737 | dev_err(dev, "can't set output direction for gpio " |
| 738 | "#%d: %d\n", i, ret); |
| 739 | goto err_loop; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | pdata->max_chipselect = ngpios; |
| 744 | pdata->cs_control = mpc83xx_spi_cs_control; |
| 745 | |
| 746 | return 0; |
| 747 | |
| 748 | err_loop: |
| 749 | while (i >= 0) { |
| 750 | if (gpio_is_valid(pinfo->gpios[i])) |
| 751 | gpio_free(pinfo->gpios[i]); |
| 752 | i--; |
| 753 | } |
| 754 | |
| 755 | kfree(pinfo->alow_flags); |
| 756 | pinfo->alow_flags = NULL; |
| 757 | err_alloc_flags: |
| 758 | kfree(pinfo->gpios); |
| 759 | pinfo->gpios = NULL; |
| 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | static int of_mpc83xx_spi_free_chipselects(struct device *dev) |
| 764 | { |
| 765 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
| 766 | struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
| 767 | int i; |
| 768 | |
| 769 | if (!pinfo->gpios) |
| 770 | return 0; |
| 771 | |
| 772 | for (i = 0; i < pdata->max_chipselect; i++) { |
| 773 | if (gpio_is_valid(pinfo->gpios[i])) |
| 774 | gpio_free(pinfo->gpios[i]); |
| 775 | } |
| 776 | |
| 777 | kfree(pinfo->gpios); |
| 778 | kfree(pinfo->alow_flags); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int __devinit of_mpc83xx_spi_probe(struct of_device *ofdev, |
| 783 | const struct of_device_id *ofid) |
| 784 | { |
| 785 | struct device *dev = &ofdev->dev; |
| 786 | struct device_node *np = ofdev->node; |
| 787 | struct mpc83xx_spi_probe_info *pinfo; |
| 788 | struct fsl_spi_platform_data *pdata; |
| 789 | struct spi_master *master; |
| 790 | struct resource mem; |
| 791 | struct resource irq; |
| 792 | const void *prop; |
| 793 | int ret = -ENOMEM; |
| 794 | |
| 795 | pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL); |
| 796 | if (!pinfo) |
| 797 | return -ENOMEM; |
| 798 | |
| 799 | pdata = &pinfo->pdata; |
| 800 | dev->platform_data = pdata; |
| 801 | |
| 802 | /* Allocate bus num dynamically. */ |
| 803 | pdata->bus_num = -1; |
| 804 | |
| 805 | /* SPI controller is either clocked from QE or SoC clock. */ |
| 806 | pdata->sysclk = get_brgfreq(); |
| 807 | if (pdata->sysclk == -1) { |
| 808 | pdata->sysclk = fsl_get_sys_freq(); |
| 809 | if (pdata->sysclk == -1) { |
| 810 | ret = -ENODEV; |
| 811 | goto err_clk; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | prop = of_get_property(np, "mode", NULL); |
| 816 | if (prop && !strcmp(prop, "cpu-qe")) |
| 817 | pdata->qe_mode = 1; |
| 818 | |
| 819 | ret = of_mpc83xx_spi_get_chipselects(dev); |
| 820 | if (ret) |
| 821 | goto err; |
| 822 | |
| 823 | ret = of_address_to_resource(np, 0, &mem); |
| 824 | if (ret) |
| 825 | goto err; |
| 826 | |
| 827 | ret = of_irq_to_resource(np, 0, &irq); |
| 828 | if (!ret) { |
| 829 | ret = -EINVAL; |
| 830 | goto err; |
| 831 | } |
| 832 | |
| 833 | master = mpc83xx_spi_probe(dev, &mem, irq.start); |
| 834 | if (IS_ERR(master)) { |
| 835 | ret = PTR_ERR(master); |
| 836 | goto err; |
| 837 | } |
| 838 | |
| 839 | of_register_spi_devices(master, np); |
| 840 | |
| 841 | return 0; |
| 842 | |
| 843 | err: |
| 844 | of_mpc83xx_spi_free_chipselects(dev); |
| 845 | err_clk: |
| 846 | kfree(pinfo); |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | static int __devexit of_mpc83xx_spi_remove(struct of_device *ofdev) |
| 851 | { |
| 852 | int ret; |
| 853 | |
| 854 | ret = mpc83xx_spi_remove(&ofdev->dev); |
| 855 | if (ret) |
| 856 | return ret; |
| 857 | of_mpc83xx_spi_free_chipselects(&ofdev->dev); |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | static const struct of_device_id of_mpc83xx_spi_match[] = { |
| 862 | { .compatible = "fsl,spi" }, |
| 863 | {}, |
| 864 | }; |
| 865 | MODULE_DEVICE_TABLE(of, of_mpc83xx_spi_match); |
| 866 | |
| 867 | static struct of_platform_driver of_mpc83xx_spi_driver = { |
| 868 | .name = "mpc83xx_spi", |
| 869 | .match_table = of_mpc83xx_spi_match, |
| 870 | .probe = of_mpc83xx_spi_probe, |
| 871 | .remove = __devexit_p(of_mpc83xx_spi_remove), |
| 872 | }; |
| 873 | |
| 874 | #ifdef CONFIG_MPC832x_RDB |
| 875 | /* |
| 876 | * XXX XXX XXX |
| 877 | * This is "legacy" platform driver, was used by the MPC8323E-RDB boards |
| 878 | * only. The driver should go away soon, since newer MPC8323E-RDB's device |
| 879 | * tree can work with OpenFirmware driver. But for now we support old trees |
| 880 | * as well. |
| 881 | */ |
| 882 | static int __devinit plat_mpc83xx_spi_probe(struct platform_device *pdev) |
| 883 | { |
| 884 | struct resource *mem; |
| 885 | unsigned int irq; |
| 886 | struct spi_master *master; |
| 887 | |
| 888 | if (!pdev->dev.platform_data) |
| 889 | return -EINVAL; |
| 890 | |
| 891 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 892 | if (!mem) |
| 893 | return -EINVAL; |
| 894 | |
| 895 | irq = platform_get_irq(pdev, 0); |
| 896 | if (!irq) |
| 897 | return -EINVAL; |
| 898 | |
| 899 | master = mpc83xx_spi_probe(&pdev->dev, mem, irq); |
| 900 | if (IS_ERR(master)) |
| 901 | return PTR_ERR(master); |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static int __devexit plat_mpc83xx_spi_remove(struct platform_device *pdev) |
| 906 | { |
| 907 | return mpc83xx_spi_remove(&pdev->dev); |
| 908 | } |
| 909 | |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 910 | MODULE_ALIAS("platform:mpc83xx_spi"); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 911 | static struct platform_driver mpc83xx_spi_driver = { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 912 | .probe = plat_mpc83xx_spi_probe, |
| 913 | .remove = __exit_p(plat_mpc83xx_spi_remove), |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 914 | .driver = { |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 915 | .name = "mpc83xx_spi", |
| 916 | .owner = THIS_MODULE, |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 917 | }, |
| 918 | }; |
| 919 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 920 | static bool legacy_driver_failed; |
| 921 | |
| 922 | static void __init legacy_driver_register(void) |
| 923 | { |
| 924 | legacy_driver_failed = platform_driver_register(&mpc83xx_spi_driver); |
| 925 | } |
| 926 | |
| 927 | static void __exit legacy_driver_unregister(void) |
| 928 | { |
| 929 | if (legacy_driver_failed) |
| 930 | return; |
| 931 | platform_driver_unregister(&mpc83xx_spi_driver); |
| 932 | } |
| 933 | #else |
| 934 | static void __init legacy_driver_register(void) {} |
| 935 | static void __exit legacy_driver_unregister(void) {} |
| 936 | #endif /* CONFIG_MPC832x_RDB */ |
| 937 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 938 | static int __init mpc83xx_spi_init(void) |
| 939 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 940 | legacy_driver_register(); |
| 941 | return of_register_platform_driver(&of_mpc83xx_spi_driver); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static void __exit mpc83xx_spi_exit(void) |
| 945 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 946 | of_unregister_platform_driver(&of_mpc83xx_spi_driver); |
| 947 | legacy_driver_unregister(); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | module_init(mpc83xx_spi_init); |
| 951 | module_exit(mpc83xx_spi_exit); |
| 952 | |
| 953 | MODULE_AUTHOR("Kumar Gala"); |
| 954 | MODULE_DESCRIPTION("Simple MPC83xx SPI Driver"); |
| 955 | MODULE_LICENSE("GPL"); |