Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1 | /* |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 2 | * MPC8xxx 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. |
| 7 | * |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 8 | * CPM SPI and QE buffer descriptors mode support: |
| 9 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 10 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 11 | * |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 2 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | */ |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/kernel.h> |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame] | 21 | #include <linux/bug.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 22 | #include <linux/errno.h> |
| 23 | #include <linux/err.h> |
Anton Vorontsov | 9effb95 | 2009-06-18 16:49:05 -0700 | [diff] [blame] | 24 | #include <linux/io.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 25 | #include <linux/completion.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/irq.h> |
| 29 | #include <linux/device.h> |
| 30 | #include <linux/spi/spi.h> |
| 31 | #include <linux/spi/spi_bitbang.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/fsl_devices.h> |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 34 | #include <linux/dma-mapping.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/mutex.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 37 | #include <linux/of.h> |
| 38 | #include <linux/of_platform.h> |
| 39 | #include <linux/gpio.h> |
| 40 | #include <linux/of_gpio.h> |
| 41 | #include <linux/of_spi.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 42 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 43 | #include <sysdev/fsl_soc.h> |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 44 | #include <asm/cpm.h> |
| 45 | #include <asm/qe.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 46 | #include <asm/irq.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 47 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 48 | /* CPM1 and CPM2 are mutually exclusive. */ |
| 49 | #ifdef CONFIG_CPM1 |
| 50 | #include <asm/cpm1.h> |
| 51 | #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0) |
| 52 | #else |
| 53 | #include <asm/cpm2.h> |
| 54 | #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0) |
| 55 | #endif |
| 56 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 57 | /* SPI Controller registers */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 58 | struct mpc8xxx_spi_reg { |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 59 | u8 res1[0x20]; |
| 60 | __be32 mode; |
| 61 | __be32 event; |
| 62 | __be32 mask; |
| 63 | __be32 command; |
| 64 | __be32 transmit; |
| 65 | __be32 receive; |
| 66 | }; |
| 67 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 68 | /* SPI Parameter RAM */ |
| 69 | struct spi_pram { |
| 70 | __be16 rbase; /* Rx Buffer descriptor base address */ |
| 71 | __be16 tbase; /* Tx Buffer descriptor base address */ |
| 72 | u8 rfcr; /* Rx function code */ |
| 73 | u8 tfcr; /* Tx function code */ |
| 74 | __be16 mrblr; /* Max receive buffer length */ |
| 75 | __be32 rstate; /* Internal */ |
| 76 | __be32 rdp; /* Internal */ |
| 77 | __be16 rbptr; /* Internal */ |
| 78 | __be16 rbc; /* Internal */ |
| 79 | __be32 rxtmp; /* Internal */ |
| 80 | __be32 tstate; /* Internal */ |
| 81 | __be32 tdp; /* Internal */ |
| 82 | __be16 tbptr; /* Internal */ |
| 83 | __be16 tbc; /* Internal */ |
| 84 | __be32 txtmp; /* Internal */ |
| 85 | __be32 res; /* Tx temp. */ |
| 86 | __be16 rpbase; /* Relocation pointer (CPM1 only) */ |
| 87 | __be16 res1; /* Reserved */ |
| 88 | }; |
| 89 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 90 | /* SPI Controller mode register definitions */ |
Anton Vorontsov | 2a485d7 | 2007-07-31 00:38:45 -0700 | [diff] [blame] | 91 | #define SPMODE_LOOP (1 << 30) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 92 | #define SPMODE_CI_INACTIVEHIGH (1 << 29) |
| 93 | #define SPMODE_CP_BEGIN_EDGECLK (1 << 28) |
| 94 | #define SPMODE_DIV16 (1 << 27) |
| 95 | #define SPMODE_REV (1 << 26) |
| 96 | #define SPMODE_MS (1 << 25) |
| 97 | #define SPMODE_ENABLE (1 << 24) |
| 98 | #define SPMODE_LEN(x) ((x) << 20) |
| 99 | #define SPMODE_PM(x) ((x) << 16) |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 100 | #define SPMODE_OP (1 << 14) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 101 | #define SPMODE_CG(x) ((x) << 7) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Default for SPI Mode: |
| 105 | * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk |
| 106 | */ |
| 107 | #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \ |
| 108 | SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf)) |
| 109 | |
| 110 | /* SPIE register values */ |
| 111 | #define SPIE_NE 0x00000200 /* Not empty */ |
| 112 | #define SPIE_NF 0x00000100 /* Not full */ |
| 113 | |
| 114 | /* SPIM register values */ |
| 115 | #define SPIM_NE 0x00000200 /* Not empty */ |
| 116 | #define SPIM_NF 0x00000100 /* Not full */ |
| 117 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 118 | #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */ |
| 119 | #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */ |
| 120 | |
| 121 | /* SPCOM register values */ |
| 122 | #define SPCOM_STR (1 << 23) /* Start transmit */ |
| 123 | |
| 124 | #define SPI_PRAM_SIZE 0x100 |
| 125 | #define SPI_MRBLR ((unsigned int)PAGE_SIZE) |
| 126 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 127 | /* SPI Controller driver's private data. */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 128 | struct mpc8xxx_spi { |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 129 | struct device *dev; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 130 | struct mpc8xxx_spi_reg __iomem *base; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 131 | |
| 132 | /* rx & tx bufs from the spi_transfer */ |
| 133 | const void *tx; |
| 134 | void *rx; |
| 135 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 136 | int subblock; |
| 137 | struct spi_pram __iomem *pram; |
| 138 | struct cpm_buf_desc __iomem *tx_bd; |
| 139 | struct cpm_buf_desc __iomem *rx_bd; |
| 140 | |
| 141 | struct spi_transfer *xfer_in_progress; |
| 142 | |
| 143 | /* dma addresses for CPM transfers */ |
| 144 | dma_addr_t tx_dma; |
| 145 | dma_addr_t rx_dma; |
| 146 | bool map_tx_dma; |
| 147 | bool map_rx_dma; |
| 148 | |
| 149 | dma_addr_t dma_dummy_tx; |
| 150 | dma_addr_t dma_dummy_rx; |
| 151 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 152 | /* functions to deal with different sized buffers */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 153 | void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *); |
| 154 | u32(*get_tx) (struct mpc8xxx_spi *); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 155 | |
| 156 | unsigned int count; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 157 | unsigned int irq; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 158 | |
| 159 | unsigned nsecs; /* (clock cycle time)/2 */ |
| 160 | |
Anton Vorontsov | e24a4d1 | 2007-08-10 13:01:01 -0700 | [diff] [blame] | 161 | u32 spibrg; /* SPIBRG input clock */ |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 162 | u32 rx_shift; /* RX data reg shift when in qe mode */ |
| 163 | u32 tx_shift; /* TX data reg shift when in qe mode */ |
| 164 | |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 165 | unsigned int flags; |
| 166 | #define SPI_QE_CPU_MODE (1 << 0) /* QE CPU ("PIO") mode */ |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 167 | #define SPI_CPM_MODE (1 << 1) /* CPM/QE ("DMA") mode */ |
| 168 | #define SPI_CPM1 (1 << 2) /* SPI unit is in CPM1 block */ |
| 169 | #define SPI_CPM2 (1 << 3) /* SPI unit is in CPM2 block */ |
| 170 | #define SPI_QE (1 << 4) /* SPI unit is in QE block */ |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 171 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 172 | struct workqueue_struct *workqueue; |
| 173 | struct work_struct work; |
| 174 | |
| 175 | struct list_head queue; |
| 176 | spinlock_t lock; |
| 177 | |
| 178 | struct completion done; |
| 179 | }; |
| 180 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 181 | static void *mpc8xxx_dummy_rx; |
| 182 | static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock); |
| 183 | static int mpc8xxx_dummy_rx_refcnt; |
| 184 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 185 | struct spi_mpc8xxx_cs { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 186 | /* functions to deal with different sized buffers */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 187 | void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *); |
| 188 | u32 (*get_tx) (struct mpc8xxx_spi *); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 189 | u32 rx_shift; /* RX data reg shift when in qe mode */ |
| 190 | u32 tx_shift; /* TX data reg shift when in qe mode */ |
| 191 | u32 hw_mode; /* Holds HW mode register settings */ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 194 | static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 195 | { |
| 196 | out_be32(reg, val); |
| 197 | } |
| 198 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 199 | static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 200 | { |
| 201 | return in_be32(reg); |
| 202 | } |
| 203 | |
| 204 | #define MPC83XX_SPI_RX_BUF(type) \ |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 205 | static \ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 206 | void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 207 | { \ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 208 | type *rx = mpc8xxx_spi->rx; \ |
| 209 | *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \ |
| 210 | mpc8xxx_spi->rx = rx; \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | #define MPC83XX_SPI_TX_BUF(type) \ |
Anton Vorontsov | 34c8a20 | 2009-03-31 15:24:35 -0700 | [diff] [blame] | 214 | static \ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 215 | u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 216 | { \ |
| 217 | u32 data; \ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 218 | const type *tx = mpc8xxx_spi->tx; \ |
David Brownell | 4b1badf | 2006-12-29 16:48:39 -0800 | [diff] [blame] | 219 | if (!tx) \ |
| 220 | return 0; \ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 221 | data = *tx++ << mpc8xxx_spi->tx_shift; \ |
| 222 | mpc8xxx_spi->tx = tx; \ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 223 | return data; \ |
| 224 | } |
| 225 | |
| 226 | MPC83XX_SPI_RX_BUF(u8) |
| 227 | MPC83XX_SPI_RX_BUF(u16) |
| 228 | MPC83XX_SPI_RX_BUF(u32) |
| 229 | MPC83XX_SPI_TX_BUF(u8) |
| 230 | MPC83XX_SPI_TX_BUF(u16) |
| 231 | MPC83XX_SPI_TX_BUF(u32) |
| 232 | |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 233 | static void mpc8xxx_spi_change_mode(struct spi_device *spi) |
| 234 | { |
| 235 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master); |
| 236 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
| 237 | __be32 __iomem *mode = &mspi->base->mode; |
| 238 | unsigned long flags; |
| 239 | |
| 240 | if (cs->hw_mode == mpc8xxx_spi_read_reg(mode)) |
| 241 | return; |
| 242 | |
| 243 | /* Turn off IRQs locally to minimize time that SPI is disabled. */ |
| 244 | local_irq_save(flags); |
| 245 | |
| 246 | /* Turn off SPI unit prior changing mode */ |
| 247 | mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE); |
| 248 | mpc8xxx_spi_write_reg(mode, cs->hw_mode); |
| 249 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 250 | /* When in CPM mode, we need to reinit tx and rx. */ |
| 251 | if (mspi->flags & SPI_CPM_MODE) { |
| 252 | if (mspi->flags & SPI_QE) { |
| 253 | qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock, |
| 254 | QE_CR_PROTOCOL_UNSPECIFIED, 0); |
| 255 | } else { |
| 256 | cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX); |
| 257 | if (mspi->flags & SPI_CPM1) { |
| 258 | out_be16(&mspi->pram->rbptr, |
| 259 | in_be16(&mspi->pram->rbase)); |
| 260 | out_be16(&mspi->pram->tbptr, |
| 261 | in_be16(&mspi->pram->tbase)); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 266 | local_irq_restore(flags); |
| 267 | } |
| 268 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 269 | static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 270 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 271 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 272 | struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data; |
| 273 | bool pol = spi->mode & SPI_CS_HIGH; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 274 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 275 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 276 | if (value == BITBANG_CS_INACTIVE) { |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 277 | if (pdata->cs_control) |
| 278 | pdata->cs_control(spi, !pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (value == BITBANG_CS_ACTIVE) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 282 | mpc8xxx_spi->rx_shift = cs->rx_shift; |
| 283 | mpc8xxx_spi->tx_shift = cs->tx_shift; |
| 284 | mpc8xxx_spi->get_rx = cs->get_rx; |
| 285 | mpc8xxx_spi->get_tx = cs->get_tx; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 286 | |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 287 | mpc8xxx_spi_change_mode(spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 288 | |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 289 | if (pdata->cs_control) |
| 290 | pdata->cs_control(spi, pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | static |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 295 | int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 296 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 297 | struct mpc8xxx_spi *mpc8xxx_spi; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 298 | u8 bits_per_word, pm; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 299 | u32 hz; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 300 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 301 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 302 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 303 | |
| 304 | if (t) { |
| 305 | bits_per_word = t->bits_per_word; |
| 306 | hz = t->speed_hz; |
| 307 | } else { |
| 308 | bits_per_word = 0; |
| 309 | hz = 0; |
| 310 | } |
| 311 | |
| 312 | /* spi_transfer level calls that work per-word */ |
| 313 | if (!bits_per_word) |
| 314 | bits_per_word = spi->bits_per_word; |
| 315 | |
| 316 | /* Make sure its a bit width we support [4..16, 32] */ |
| 317 | if ((bits_per_word < 4) |
| 318 | || ((bits_per_word > 16) && (bits_per_word != 32))) |
| 319 | return -EINVAL; |
| 320 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 321 | if (!hz) |
| 322 | hz = spi->max_speed_hz; |
| 323 | |
| 324 | cs->rx_shift = 0; |
| 325 | cs->tx_shift = 0; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 326 | if (bits_per_word <= 8) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 327 | cs->get_rx = mpc8xxx_spi_rx_buf_u8; |
| 328 | cs->get_tx = mpc8xxx_spi_tx_buf_u8; |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 329 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 330 | cs->rx_shift = 16; |
| 331 | cs->tx_shift = 24; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 332 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 333 | } else if (bits_per_word <= 16) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 334 | cs->get_rx = mpc8xxx_spi_rx_buf_u16; |
| 335 | cs->get_tx = mpc8xxx_spi_tx_buf_u16; |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 336 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 337 | cs->rx_shift = 16; |
| 338 | cs->tx_shift = 16; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 339 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 340 | } else if (bits_per_word <= 32) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 341 | cs->get_rx = mpc8xxx_spi_rx_buf_u32; |
| 342 | cs->get_tx = mpc8xxx_spi_tx_buf_u32; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 343 | } else |
| 344 | return -EINVAL; |
| 345 | |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 346 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE && |
| 347 | spi->mode & SPI_LSB_FIRST) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 348 | cs->tx_shift = 0; |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 349 | if (bits_per_word <= 8) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 350 | cs->rx_shift = 8; |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 351 | else |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 352 | cs->rx_shift = 0; |
Anton Vorontsov | 35cc0b9 | 2007-07-31 00:38:42 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 355 | mpc8xxx_spi->rx_shift = cs->rx_shift; |
| 356 | mpc8xxx_spi->tx_shift = cs->tx_shift; |
| 357 | mpc8xxx_spi->get_rx = cs->get_rx; |
| 358 | mpc8xxx_spi->get_tx = cs->get_tx; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 359 | |
| 360 | if (bits_per_word == 32) |
| 361 | bits_per_word = 0; |
| 362 | else |
| 363 | bits_per_word = bits_per_word - 1; |
| 364 | |
Anton Vorontsov | 32421da | 2007-07-31 00:38:41 -0700 | [diff] [blame] | 365 | /* mask out bits we are going to set */ |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 366 | cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16 |
| 367 | | SPMODE_PM(0xF)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 368 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 369 | cs->hw_mode |= SPMODE_LEN(bits_per_word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 370 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 371 | if ((mpc8xxx_spi->spibrg / hz) > 64) { |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 372 | cs->hw_mode |= SPMODE_DIV16; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 373 | pm = mpc8xxx_spi->spibrg / (hz * 64); |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame] | 374 | |
| 375 | WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. " |
| 376 | "Will use %d Hz instead.\n", dev_name(&spi->dev), |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 377 | hz, mpc8xxx_spi->spibrg / 1024); |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame] | 378 | if (pm > 16) |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 379 | pm = 16; |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 380 | } else |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 381 | pm = mpc8xxx_spi->spibrg / (hz * 4); |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 382 | if (pm) |
| 383 | pm--; |
| 384 | |
| 385 | cs->hw_mode |= SPMODE_PM(pm); |
David Brownell | dccd573 | 2007-07-17 04:04:02 -0700 | [diff] [blame] | 386 | |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 387 | mpc8xxx_spi_change_mode(spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 391 | static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 392 | { |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 393 | struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd; |
| 394 | struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd; |
| 395 | unsigned int xfer_len = min(mspi->count, SPI_MRBLR); |
| 396 | unsigned int xfer_ofs; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 397 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 398 | xfer_ofs = mspi->xfer_in_progress->len - mspi->count; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 399 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 400 | out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs); |
| 401 | out_be16(&rx_bd->cbd_datlen, 0); |
| 402 | out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP); |
| 403 | |
| 404 | out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs); |
| 405 | out_be16(&tx_bd->cbd_datlen, xfer_len); |
| 406 | out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP | |
| 407 | BD_SC_LAST); |
| 408 | |
| 409 | /* start transfer */ |
| 410 | mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR); |
| 411 | } |
| 412 | |
| 413 | static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi, |
| 414 | struct spi_transfer *t, bool is_dma_mapped) |
| 415 | { |
| 416 | struct device *dev = mspi->dev; |
| 417 | |
| 418 | if (is_dma_mapped) { |
| 419 | mspi->map_tx_dma = 0; |
| 420 | mspi->map_rx_dma = 0; |
| 421 | } else { |
| 422 | mspi->map_tx_dma = 1; |
| 423 | mspi->map_rx_dma = 1; |
| 424 | } |
| 425 | |
| 426 | if (!t->tx_buf) { |
| 427 | mspi->tx_dma = mspi->dma_dummy_tx; |
| 428 | mspi->map_tx_dma = 0; |
| 429 | } |
| 430 | |
| 431 | if (!t->rx_buf) { |
| 432 | mspi->rx_dma = mspi->dma_dummy_rx; |
| 433 | mspi->map_rx_dma = 0; |
| 434 | } |
| 435 | |
| 436 | if (mspi->map_tx_dma) { |
| 437 | void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */ |
| 438 | |
| 439 | mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len, |
| 440 | DMA_TO_DEVICE); |
| 441 | if (dma_mapping_error(dev, mspi->tx_dma)) { |
| 442 | dev_err(dev, "unable to map tx dma\n"); |
| 443 | return -ENOMEM; |
| 444 | } |
| 445 | } else { |
| 446 | mspi->tx_dma = t->tx_dma; |
| 447 | } |
| 448 | |
| 449 | if (mspi->map_rx_dma) { |
| 450 | mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len, |
| 451 | DMA_FROM_DEVICE); |
| 452 | if (dma_mapping_error(dev, mspi->rx_dma)) { |
| 453 | dev_err(dev, "unable to map rx dma\n"); |
| 454 | goto err_rx_dma; |
| 455 | } |
| 456 | } else { |
| 457 | mspi->rx_dma = t->rx_dma; |
| 458 | } |
| 459 | |
| 460 | /* enable rx ints */ |
| 461 | mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB); |
| 462 | |
| 463 | mspi->xfer_in_progress = t; |
| 464 | mspi->count = t->len; |
| 465 | |
| 466 | /* start CPM transfers */ |
| 467 | mpc8xxx_spi_cpm_bufs_start(mspi); |
| 468 | |
| 469 | return 0; |
| 470 | |
| 471 | err_rx_dma: |
| 472 | if (mspi->map_tx_dma) |
| 473 | dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE); |
| 474 | return -ENOMEM; |
| 475 | } |
| 476 | |
| 477 | static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi) |
| 478 | { |
| 479 | struct device *dev = mspi->dev; |
| 480 | struct spi_transfer *t = mspi->xfer_in_progress; |
| 481 | |
| 482 | if (mspi->map_tx_dma) |
| 483 | dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE); |
| 484 | if (mspi->map_tx_dma) |
| 485 | dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE); |
| 486 | mspi->xfer_in_progress = NULL; |
| 487 | } |
| 488 | |
| 489 | static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi, |
| 490 | struct spi_transfer *t, unsigned int len) |
| 491 | { |
| 492 | u32 word; |
| 493 | |
| 494 | mspi->count = len; |
| 495 | |
| 496 | /* enable rx ints */ |
| 497 | mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE); |
| 498 | |
| 499 | /* transmit word */ |
| 500 | word = mspi->get_tx(mspi); |
| 501 | mpc8xxx_spi_write_reg(&mspi->base->transmit, word); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t, |
| 507 | bool is_dma_mapped) |
| 508 | { |
| 509 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
| 510 | unsigned int len = t->len; |
| 511 | u8 bits_per_word; |
| 512 | int ret; |
| 513 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 514 | bits_per_word = spi->bits_per_word; |
| 515 | if (t->bits_per_word) |
| 516 | bits_per_word = t->bits_per_word; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 517 | |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 518 | if (bits_per_word > 8) { |
| 519 | /* invalid length? */ |
| 520 | if (len & 1) |
| 521 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 522 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 523 | } |
| 524 | if (bits_per_word > 16) { |
| 525 | /* invalid length? */ |
| 526 | if (len & 1) |
| 527 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 528 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 529 | } |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 530 | |
| 531 | mpc8xxx_spi->tx = t->tx_buf; |
| 532 | mpc8xxx_spi->rx = t->rx_buf; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 533 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 534 | INIT_COMPLETION(mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 535 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 536 | if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
| 537 | ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped); |
| 538 | else |
| 539 | ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len); |
| 540 | if (ret) |
| 541 | return ret; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 542 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 543 | wait_for_completion(&mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 544 | |
| 545 | /* disable rx ints */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 546 | mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 547 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 548 | if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
| 549 | mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi); |
| 550 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 551 | return mpc8xxx_spi->count; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 554 | static void mpc8xxx_spi_do_one_msg(struct spi_message *m) |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 555 | { |
| 556 | struct spi_device *spi = m->spi; |
| 557 | struct spi_transfer *t; |
| 558 | unsigned int cs_change; |
| 559 | const int nsecs = 50; |
| 560 | int status; |
| 561 | |
| 562 | cs_change = 1; |
| 563 | status = 0; |
| 564 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 565 | if (t->bits_per_word || t->speed_hz) { |
| 566 | /* Don't allow changes if CS is active */ |
| 567 | status = -EINVAL; |
| 568 | |
| 569 | if (cs_change) |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 570 | status = mpc8xxx_spi_setup_transfer(spi, t); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 571 | if (status < 0) |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | if (cs_change) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 576 | mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 577 | ndelay(nsecs); |
| 578 | } |
| 579 | cs_change = t->cs_change; |
| 580 | if (t->len) |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 581 | status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 582 | if (status) { |
| 583 | status = -EMSGSIZE; |
| 584 | break; |
| 585 | } |
| 586 | m->actual_length += t->len; |
| 587 | |
| 588 | if (t->delay_usecs) |
| 589 | udelay(t->delay_usecs); |
| 590 | |
| 591 | if (cs_change) { |
| 592 | ndelay(nsecs); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 593 | mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 594 | ndelay(nsecs); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | m->status = status; |
| 599 | m->complete(m->context); |
| 600 | |
| 601 | if (status || !cs_change) { |
| 602 | ndelay(nsecs); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 603 | mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 606 | mpc8xxx_spi_setup_transfer(spi, NULL); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 609 | static void mpc8xxx_spi_work(struct work_struct *work) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 610 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 611 | struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi, |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 612 | work); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 613 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 614 | spin_lock_irq(&mpc8xxx_spi->lock); |
| 615 | while (!list_empty(&mpc8xxx_spi->queue)) { |
| 616 | struct spi_message *m = container_of(mpc8xxx_spi->queue.next, |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 617 | struct spi_message, queue); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 618 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 619 | list_del_init(&m->queue); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 620 | spin_unlock_irq(&mpc8xxx_spi->lock); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 621 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 622 | mpc8xxx_spi_do_one_msg(m); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 623 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 624 | spin_lock_irq(&mpc8xxx_spi->lock); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 625 | } |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 626 | spin_unlock_irq(&mpc8xxx_spi->lock); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 629 | static int mpc8xxx_spi_setup(struct spi_device *spi) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 630 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 631 | struct mpc8xxx_spi *mpc8xxx_spi; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 632 | int retval; |
| 633 | u32 hw_mode; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 634 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 635 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 636 | if (!spi->max_speed_hz) |
| 637 | return -EINVAL; |
| 638 | |
| 639 | if (!cs) { |
| 640 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
| 641 | if (!cs) |
| 642 | return -ENOMEM; |
| 643 | spi->controller_state = cs; |
| 644 | } |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 645 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 646 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 647 | hw_mode = cs->hw_mode; /* Save orginal settings */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 648 | cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 649 | /* mask out bits we are going to set */ |
| 650 | cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH |
| 651 | | SPMODE_REV | SPMODE_LOOP); |
| 652 | |
| 653 | if (spi->mode & SPI_CPHA) |
| 654 | cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK; |
| 655 | if (spi->mode & SPI_CPOL) |
| 656 | cs->hw_mode |= SPMODE_CI_INACTIVEHIGH; |
| 657 | if (!(spi->mode & SPI_LSB_FIRST)) |
| 658 | cs->hw_mode |= SPMODE_REV; |
| 659 | if (spi->mode & SPI_LOOP) |
| 660 | cs->hw_mode |= SPMODE_LOOP; |
| 661 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 662 | retval = mpc8xxx_spi_setup_transfer(spi, NULL); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 663 | if (retval < 0) { |
| 664 | cs->hw_mode = hw_mode; /* Restore settings */ |
| 665 | return retval; |
| 666 | } |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 667 | return 0; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 670 | static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 671 | { |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 672 | u16 len; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 673 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 674 | dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__, |
| 675 | in_be16(&mspi->rx_bd->cbd_datlen), mspi->count); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 676 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 677 | len = in_be16(&mspi->rx_bd->cbd_datlen); |
| 678 | if (len > mspi->count) { |
| 679 | WARN_ON(1); |
| 680 | len = mspi->count; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Clear the events */ |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 684 | mpc8xxx_spi_write_reg(&mspi->base->event, events); |
| 685 | |
| 686 | mspi->count -= len; |
| 687 | if (mspi->count) |
| 688 | mpc8xxx_spi_cpm_bufs_start(mspi); |
| 689 | else |
| 690 | complete(&mspi->done); |
| 691 | } |
| 692 | |
| 693 | static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
| 694 | { |
| 695 | /* We need handle RX first */ |
| 696 | if (events & SPIE_NE) { |
| 697 | u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive); |
| 698 | |
| 699 | if (mspi->rx) |
| 700 | mspi->get_rx(rx_data, mspi); |
| 701 | } |
| 702 | |
| 703 | if ((events & SPIE_NF) == 0) |
| 704 | /* spin until TX is done */ |
| 705 | while (((events = |
| 706 | mpc8xxx_spi_read_reg(&mspi->base->event)) & |
| 707 | SPIE_NF) == 0) |
| 708 | cpu_relax(); |
| 709 | |
| 710 | /* Clear the events */ |
| 711 | mpc8xxx_spi_write_reg(&mspi->base->event, events); |
| 712 | |
| 713 | mspi->count -= 1; |
| 714 | if (mspi->count) { |
| 715 | u32 word = mspi->get_tx(mspi); |
| 716 | |
| 717 | mpc8xxx_spi_write_reg(&mspi->base->transmit, word); |
| 718 | } else { |
| 719 | complete(&mspi->done); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data) |
| 724 | { |
| 725 | struct mpc8xxx_spi *mspi = context_data; |
| 726 | irqreturn_t ret = IRQ_NONE; |
| 727 | u32 events; |
| 728 | |
| 729 | /* Get interrupt events(tx/rx) */ |
| 730 | events = mpc8xxx_spi_read_reg(&mspi->base->event); |
| 731 | if (events) |
| 732 | ret = IRQ_HANDLED; |
| 733 | |
| 734 | dev_dbg(mspi->dev, "%s: events %x\n", __func__, events); |
| 735 | |
| 736 | if (mspi->flags & SPI_CPM_MODE) |
| 737 | mpc8xxx_spi_cpm_irq(mspi, events); |
| 738 | else |
| 739 | mpc8xxx_spi_cpu_irq(mspi, events); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 740 | |
| 741 | return ret; |
| 742 | } |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 743 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 744 | static int mpc8xxx_spi_transfer(struct spi_device *spi, |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 745 | struct spi_message *m) |
| 746 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 747 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 748 | unsigned long flags; |
| 749 | |
| 750 | m->actual_length = 0; |
| 751 | m->status = -EINPROGRESS; |
| 752 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 753 | spin_lock_irqsave(&mpc8xxx_spi->lock, flags); |
| 754 | list_add_tail(&m->queue, &mpc8xxx_spi->queue); |
| 755 | queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work); |
| 756 | spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 762 | static void mpc8xxx_spi_cleanup(struct spi_device *spi) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 763 | { |
| 764 | kfree(spi->controller_state); |
| 765 | } |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 766 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 767 | static void *mpc8xxx_spi_alloc_dummy_rx(void) |
| 768 | { |
| 769 | mutex_lock(&mpc8xxx_dummy_rx_lock); |
| 770 | |
| 771 | if (!mpc8xxx_dummy_rx) |
| 772 | mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL); |
| 773 | if (mpc8xxx_dummy_rx) |
| 774 | mpc8xxx_dummy_rx_refcnt++; |
| 775 | |
| 776 | mutex_unlock(&mpc8xxx_dummy_rx_lock); |
| 777 | |
| 778 | return mpc8xxx_dummy_rx; |
| 779 | } |
| 780 | |
| 781 | static void mpc8xxx_spi_free_dummy_rx(void) |
| 782 | { |
| 783 | mutex_lock(&mpc8xxx_dummy_rx_lock); |
| 784 | |
| 785 | switch (mpc8xxx_dummy_rx_refcnt) { |
| 786 | case 0: |
| 787 | WARN_ON(1); |
| 788 | break; |
| 789 | case 1: |
| 790 | kfree(mpc8xxx_dummy_rx); |
| 791 | mpc8xxx_dummy_rx = NULL; |
| 792 | /* fall through */ |
| 793 | default: |
| 794 | mpc8xxx_dummy_rx_refcnt--; |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | mutex_unlock(&mpc8xxx_dummy_rx_lock); |
| 799 | } |
| 800 | |
| 801 | static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi) |
| 802 | { |
| 803 | struct device *dev = mspi->dev; |
| 804 | struct device_node *np = dev_archdata_get_node(&dev->archdata); |
| 805 | const u32 *iprop; |
| 806 | int size; |
| 807 | unsigned long spi_base_ofs; |
| 808 | unsigned long pram_ofs = -ENOMEM; |
| 809 | |
| 810 | /* Can't use of_address_to_resource(), QE muram isn't at 0. */ |
| 811 | iprop = of_get_property(np, "reg", &size); |
| 812 | |
| 813 | /* QE with a fixed pram location? */ |
| 814 | if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4) |
| 815 | return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE); |
| 816 | |
| 817 | /* QE but with a dynamic pram location? */ |
| 818 | if (mspi->flags & SPI_QE) { |
| 819 | pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64); |
| 820 | qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock, |
| 821 | QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs); |
| 822 | return pram_ofs; |
| 823 | } |
| 824 | |
| 825 | /* CPM1 and CPM2 pram must be at a fixed addr. */ |
| 826 | if (!iprop || size != sizeof(*iprop) * 4) |
| 827 | return -ENOMEM; |
| 828 | |
| 829 | spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2); |
| 830 | if (IS_ERR_VALUE(spi_base_ofs)) |
| 831 | return -ENOMEM; |
| 832 | |
| 833 | if (mspi->flags & SPI_CPM2) { |
| 834 | pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64); |
| 835 | if (!IS_ERR_VALUE(pram_ofs)) { |
| 836 | u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs); |
| 837 | |
| 838 | out_be16(spi_base, pram_ofs); |
| 839 | } |
| 840 | } else { |
| 841 | struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs); |
| 842 | u16 rpbase = in_be16(&pram->rpbase); |
| 843 | |
| 844 | /* Microcode relocation patch applied? */ |
| 845 | if (rpbase) |
| 846 | pram_ofs = rpbase; |
| 847 | else |
| 848 | return spi_base_ofs; |
| 849 | } |
| 850 | |
| 851 | cpm_muram_free(spi_base_ofs); |
| 852 | return pram_ofs; |
| 853 | } |
| 854 | |
| 855 | static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi) |
| 856 | { |
| 857 | struct device *dev = mspi->dev; |
| 858 | struct device_node *np = dev_archdata_get_node(&dev->archdata); |
| 859 | const u32 *iprop; |
| 860 | int size; |
| 861 | unsigned long pram_ofs; |
| 862 | unsigned long bds_ofs; |
| 863 | |
| 864 | if (!(mspi->flags & SPI_CPM_MODE)) |
| 865 | return 0; |
| 866 | |
| 867 | if (!mpc8xxx_spi_alloc_dummy_rx()) |
| 868 | return -ENOMEM; |
| 869 | |
| 870 | if (mspi->flags & SPI_QE) { |
| 871 | iprop = of_get_property(np, "cell-index", &size); |
| 872 | if (iprop && size == sizeof(*iprop)) |
| 873 | mspi->subblock = *iprop; |
| 874 | |
| 875 | switch (mspi->subblock) { |
| 876 | default: |
| 877 | dev_warn(dev, "cell-index unspecified, assuming SPI1"); |
| 878 | /* fall through */ |
| 879 | case 0: |
| 880 | mspi->subblock = QE_CR_SUBBLOCK_SPI1; |
| 881 | break; |
| 882 | case 1: |
| 883 | mspi->subblock = QE_CR_SUBBLOCK_SPI2; |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi); |
| 889 | if (IS_ERR_VALUE(pram_ofs)) { |
| 890 | dev_err(dev, "can't allocate spi parameter ram\n"); |
| 891 | goto err_pram; |
| 892 | } |
| 893 | |
| 894 | bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) + |
| 895 | sizeof(*mspi->rx_bd), 8); |
| 896 | if (IS_ERR_VALUE(bds_ofs)) { |
| 897 | dev_err(dev, "can't allocate bds\n"); |
| 898 | goto err_bds; |
| 899 | } |
| 900 | |
| 901 | mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE, |
| 902 | DMA_TO_DEVICE); |
| 903 | if (dma_mapping_error(dev, mspi->dma_dummy_tx)) { |
| 904 | dev_err(dev, "unable to map dummy tx buffer\n"); |
| 905 | goto err_dummy_tx; |
| 906 | } |
| 907 | |
| 908 | mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR, |
| 909 | DMA_FROM_DEVICE); |
| 910 | if (dma_mapping_error(dev, mspi->dma_dummy_rx)) { |
| 911 | dev_err(dev, "unable to map dummy rx buffer\n"); |
| 912 | goto err_dummy_rx; |
| 913 | } |
| 914 | |
| 915 | mspi->pram = cpm_muram_addr(pram_ofs); |
| 916 | |
| 917 | mspi->tx_bd = cpm_muram_addr(bds_ofs); |
| 918 | mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd)); |
| 919 | |
| 920 | /* Initialize parameter ram. */ |
| 921 | out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd)); |
| 922 | out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd)); |
| 923 | out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL); |
| 924 | out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL); |
| 925 | out_be16(&mspi->pram->mrblr, SPI_MRBLR); |
| 926 | out_be32(&mspi->pram->rstate, 0); |
| 927 | out_be32(&mspi->pram->rdp, 0); |
| 928 | out_be16(&mspi->pram->rbptr, 0); |
| 929 | out_be16(&mspi->pram->rbc, 0); |
| 930 | out_be32(&mspi->pram->rxtmp, 0); |
| 931 | out_be32(&mspi->pram->tstate, 0); |
| 932 | out_be32(&mspi->pram->tdp, 0); |
| 933 | out_be16(&mspi->pram->tbptr, 0); |
| 934 | out_be16(&mspi->pram->tbc, 0); |
| 935 | out_be32(&mspi->pram->txtmp, 0); |
| 936 | |
| 937 | return 0; |
| 938 | |
| 939 | err_dummy_rx: |
| 940 | dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE); |
| 941 | err_dummy_tx: |
| 942 | cpm_muram_free(bds_ofs); |
| 943 | err_bds: |
| 944 | cpm_muram_free(pram_ofs); |
| 945 | err_pram: |
| 946 | mpc8xxx_spi_free_dummy_rx(); |
| 947 | return -ENOMEM; |
| 948 | } |
| 949 | |
| 950 | static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi) |
| 951 | { |
| 952 | struct device *dev = mspi->dev; |
| 953 | |
| 954 | dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE); |
| 955 | dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE); |
| 956 | cpm_muram_free(cpm_muram_offset(mspi->tx_bd)); |
| 957 | cpm_muram_free(cpm_muram_offset(mspi->pram)); |
| 958 | mpc8xxx_spi_free_dummy_rx(); |
| 959 | } |
| 960 | |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 961 | static const char *mpc8xxx_spi_strmode(unsigned int flags) |
| 962 | { |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 963 | if (flags & SPI_QE_CPU_MODE) { |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 964 | return "QE CPU"; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 965 | } else if (flags & SPI_CPM_MODE) { |
| 966 | if (flags & SPI_QE) |
| 967 | return "QE"; |
| 968 | else if (flags & SPI_CPM2) |
| 969 | return "CPM2"; |
| 970 | else |
| 971 | return "CPM1"; |
| 972 | } |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 973 | return "CPU"; |
| 974 | } |
| 975 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 976 | static struct spi_master * __devinit |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 977 | mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 978 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 979 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 980 | struct spi_master *master; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 981 | struct mpc8xxx_spi *mpc8xxx_spi; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 982 | u32 regval; |
| 983 | int ret = 0; |
| 984 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 985 | master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 986 | if (master == NULL) { |
| 987 | ret = -ENOMEM; |
| 988 | goto err; |
| 989 | } |
| 990 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 991 | dev_set_drvdata(dev, master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 992 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 993 | /* the spi->mode bits understood by this driver: */ |
| 994 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
| 995 | | SPI_LSB_FIRST | SPI_LOOP; |
| 996 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 997 | master->setup = mpc8xxx_spi_setup; |
| 998 | master->transfer = mpc8xxx_spi_transfer; |
| 999 | master->cleanup = mpc8xxx_spi_cleanup; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 1000 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1001 | mpc8xxx_spi = spi_master_get_devdata(master); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 1002 | mpc8xxx_spi->dev = dev; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1003 | mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8; |
| 1004 | mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8; |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 1005 | mpc8xxx_spi->flags = pdata->flags; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1006 | mpc8xxx_spi->spibrg = pdata->sysclk; |
Anton Vorontsov | e24a4d1 | 2007-08-10 13:01:01 -0700 | [diff] [blame] | 1007 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 1008 | ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi); |
| 1009 | if (ret) |
| 1010 | goto err_cpm_init; |
| 1011 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1012 | mpc8xxx_spi->rx_shift = 0; |
| 1013 | mpc8xxx_spi->tx_shift = 0; |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 1014 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1015 | mpc8xxx_spi->rx_shift = 16; |
| 1016 | mpc8xxx_spi->tx_shift = 24; |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1019 | init_completion(&mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1020 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1021 | mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1); |
| 1022 | if (mpc8xxx_spi->base == NULL) { |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1023 | ret = -ENOMEM; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 1024 | goto err_ioremap; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1027 | mpc8xxx_spi->irq = irq; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1028 | |
| 1029 | /* Register for SPI Interrupt */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1030 | ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq, |
| 1031 | 0, "mpc8xxx_spi", mpc8xxx_spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1032 | |
| 1033 | if (ret != 0) |
| 1034 | goto unmap_io; |
| 1035 | |
| 1036 | master->bus_num = pdata->bus_num; |
| 1037 | master->num_chipselect = pdata->max_chipselect; |
| 1038 | |
| 1039 | /* SPI controller initializations */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1040 | mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0); |
| 1041 | mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0); |
| 1042 | mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0); |
| 1043 | mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1044 | |
| 1045 | /* Enable SPI interface */ |
| 1046 | regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 1047 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 1048 | regval |= SPMODE_OP; |
| 1049 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1050 | mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval); |
| 1051 | spin_lock_init(&mpc8xxx_spi->lock); |
| 1052 | init_completion(&mpc8xxx_spi->done); |
| 1053 | INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work); |
| 1054 | INIT_LIST_HEAD(&mpc8xxx_spi->queue); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1055 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1056 | mpc8xxx_spi->workqueue = create_singlethread_workqueue( |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1057 | dev_name(master->dev.parent)); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1058 | if (mpc8xxx_spi->workqueue == NULL) { |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 1059 | ret = -EBUSY; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1060 | goto free_irq; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | ret = spi_register_master(master); |
| 1064 | if (ret < 0) |
| 1065 | goto unreg_master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1066 | |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 1067 | dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base, |
| 1068 | mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1069 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1070 | return master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1071 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 1072 | unreg_master: |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1073 | destroy_workqueue(mpc8xxx_spi->workqueue); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1074 | free_irq: |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1075 | free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1076 | unmap_io: |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1077 | iounmap(mpc8xxx_spi->base); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 1078 | err_ioremap: |
| 1079 | mpc8xxx_spi_cpm_free(mpc8xxx_spi); |
| 1080 | err_cpm_init: |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1081 | spi_master_put(master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1082 | err: |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1083 | return ERR_PTR(ret); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1086 | static int __devexit mpc8xxx_spi_remove(struct device *dev) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1087 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1088 | struct mpc8xxx_spi *mpc8xxx_spi; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1089 | struct spi_master *master; |
| 1090 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1091 | master = dev_get_drvdata(dev); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1092 | mpc8xxx_spi = spi_master_get_devdata(master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1093 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1094 | flush_workqueue(mpc8xxx_spi->workqueue); |
| 1095 | destroy_workqueue(mpc8xxx_spi->workqueue); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 1096 | spi_unregister_master(master); |
| 1097 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1098 | free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); |
| 1099 | iounmap(mpc8xxx_spi->base); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 1100 | mpc8xxx_spi_cpm_free(mpc8xxx_spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1105 | struct mpc8xxx_spi_probe_info { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1106 | struct fsl_spi_platform_data pdata; |
| 1107 | int *gpios; |
| 1108 | bool *alow_flags; |
| 1109 | }; |
| 1110 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1111 | static struct mpc8xxx_spi_probe_info * |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1112 | to_of_pinfo(struct fsl_spi_platform_data *pdata) |
| 1113 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1114 | return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1117 | static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1118 | { |
| 1119 | struct device *dev = spi->dev.parent; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1120 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1121 | u16 cs = spi->chip_select; |
| 1122 | int gpio = pinfo->gpios[cs]; |
| 1123 | bool alow = pinfo->alow_flags[cs]; |
| 1124 | |
| 1125 | gpio_set_value(gpio, on ^ alow); |
| 1126 | } |
| 1127 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1128 | static int of_mpc8xxx_spi_get_chipselects(struct device *dev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1129 | { |
| 1130 | struct device_node *np = dev_archdata_get_node(&dev->archdata); |
| 1131 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1132 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1133 | unsigned int ngpios; |
| 1134 | int i = 0; |
| 1135 | int ret; |
| 1136 | |
| 1137 | ngpios = of_gpio_count(np); |
| 1138 | if (!ngpios) { |
| 1139 | /* |
| 1140 | * SPI w/o chip-select line. One SPI device is still permitted |
| 1141 | * though. |
| 1142 | */ |
| 1143 | pdata->max_chipselect = 1; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 1147 | pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1148 | if (!pinfo->gpios) |
| 1149 | return -ENOMEM; |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 1150 | memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios)); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1151 | |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 1152 | pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags), |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1153 | GFP_KERNEL); |
| 1154 | if (!pinfo->alow_flags) { |
| 1155 | ret = -ENOMEM; |
| 1156 | goto err_alloc_flags; |
| 1157 | } |
| 1158 | |
| 1159 | for (; i < ngpios; i++) { |
| 1160 | int gpio; |
| 1161 | enum of_gpio_flags flags; |
| 1162 | |
| 1163 | gpio = of_get_gpio_flags(np, i, &flags); |
| 1164 | if (!gpio_is_valid(gpio)) { |
| 1165 | dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); |
Anton Vorontsov | 783058f | 2009-10-12 20:49:22 +0400 | [diff] [blame] | 1166 | ret = gpio; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1167 | goto err_loop; |
| 1168 | } |
| 1169 | |
| 1170 | ret = gpio_request(gpio, dev_name(dev)); |
| 1171 | if (ret) { |
| 1172 | dev_err(dev, "can't request gpio #%d: %d\n", i, ret); |
| 1173 | goto err_loop; |
| 1174 | } |
| 1175 | |
| 1176 | pinfo->gpios[i] = gpio; |
| 1177 | pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW; |
| 1178 | |
| 1179 | ret = gpio_direction_output(pinfo->gpios[i], |
| 1180 | pinfo->alow_flags[i]); |
| 1181 | if (ret) { |
| 1182 | dev_err(dev, "can't set output direction for gpio " |
| 1183 | "#%d: %d\n", i, ret); |
| 1184 | goto err_loop; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | pdata->max_chipselect = ngpios; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1189 | pdata->cs_control = mpc8xxx_spi_cs_control; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1190 | |
| 1191 | return 0; |
| 1192 | |
| 1193 | err_loop: |
| 1194 | while (i >= 0) { |
| 1195 | if (gpio_is_valid(pinfo->gpios[i])) |
| 1196 | gpio_free(pinfo->gpios[i]); |
| 1197 | i--; |
| 1198 | } |
| 1199 | |
| 1200 | kfree(pinfo->alow_flags); |
| 1201 | pinfo->alow_flags = NULL; |
| 1202 | err_alloc_flags: |
| 1203 | kfree(pinfo->gpios); |
| 1204 | pinfo->gpios = NULL; |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1208 | static int of_mpc8xxx_spi_free_chipselects(struct device *dev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1209 | { |
| 1210 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1211 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1212 | int i; |
| 1213 | |
| 1214 | if (!pinfo->gpios) |
| 1215 | return 0; |
| 1216 | |
| 1217 | for (i = 0; i < pdata->max_chipselect; i++) { |
| 1218 | if (gpio_is_valid(pinfo->gpios[i])) |
| 1219 | gpio_free(pinfo->gpios[i]); |
| 1220 | } |
| 1221 | |
| 1222 | kfree(pinfo->gpios); |
| 1223 | kfree(pinfo->alow_flags); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1227 | static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev, |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1228 | const struct of_device_id *ofid) |
| 1229 | { |
| 1230 | struct device *dev = &ofdev->dev; |
| 1231 | struct device_node *np = ofdev->node; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1232 | struct mpc8xxx_spi_probe_info *pinfo; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1233 | struct fsl_spi_platform_data *pdata; |
| 1234 | struct spi_master *master; |
| 1235 | struct resource mem; |
| 1236 | struct resource irq; |
| 1237 | const void *prop; |
| 1238 | int ret = -ENOMEM; |
| 1239 | |
| 1240 | pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL); |
| 1241 | if (!pinfo) |
| 1242 | return -ENOMEM; |
| 1243 | |
| 1244 | pdata = &pinfo->pdata; |
| 1245 | dev->platform_data = pdata; |
| 1246 | |
| 1247 | /* Allocate bus num dynamically. */ |
| 1248 | pdata->bus_num = -1; |
| 1249 | |
| 1250 | /* SPI controller is either clocked from QE or SoC clock. */ |
| 1251 | pdata->sysclk = get_brgfreq(); |
| 1252 | if (pdata->sysclk == -1) { |
| 1253 | pdata->sysclk = fsl_get_sys_freq(); |
| 1254 | if (pdata->sysclk == -1) { |
| 1255 | ret = -ENODEV; |
| 1256 | goto err_clk; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | prop = of_get_property(np, "mode", NULL); |
| 1261 | if (prop && !strcmp(prop, "cpu-qe")) |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 1262 | pdata->flags = SPI_QE_CPU_MODE; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame^] | 1263 | else if (prop && !strcmp(prop, "qe")) |
| 1264 | pdata->flags = SPI_CPM_MODE | SPI_QE; |
| 1265 | else if (of_device_is_compatible(np, "fsl,cpm2-spi")) |
| 1266 | pdata->flags = SPI_CPM_MODE | SPI_CPM2; |
| 1267 | else if (of_device_is_compatible(np, "fsl,cpm1-spi")) |
| 1268 | pdata->flags = SPI_CPM_MODE | SPI_CPM1; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1269 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1270 | ret = of_mpc8xxx_spi_get_chipselects(dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1271 | if (ret) |
| 1272 | goto err; |
| 1273 | |
| 1274 | ret = of_address_to_resource(np, 0, &mem); |
| 1275 | if (ret) |
| 1276 | goto err; |
| 1277 | |
| 1278 | ret = of_irq_to_resource(np, 0, &irq); |
| 1279 | if (!ret) { |
| 1280 | ret = -EINVAL; |
| 1281 | goto err; |
| 1282 | } |
| 1283 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1284 | master = mpc8xxx_spi_probe(dev, &mem, irq.start); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1285 | if (IS_ERR(master)) { |
| 1286 | ret = PTR_ERR(master); |
| 1287 | goto err; |
| 1288 | } |
| 1289 | |
| 1290 | of_register_spi_devices(master, np); |
| 1291 | |
| 1292 | return 0; |
| 1293 | |
| 1294 | err: |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1295 | of_mpc8xxx_spi_free_chipselects(dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1296 | err_clk: |
| 1297 | kfree(pinfo); |
| 1298 | return ret; |
| 1299 | } |
| 1300 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1301 | static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1302 | { |
| 1303 | int ret; |
| 1304 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1305 | ret = mpc8xxx_spi_remove(&ofdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1306 | if (ret) |
| 1307 | return ret; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1308 | of_mpc8xxx_spi_free_chipselects(&ofdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1312 | static const struct of_device_id of_mpc8xxx_spi_match[] = { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1313 | { .compatible = "fsl,spi" }, |
| 1314 | {}, |
| 1315 | }; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1316 | MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1317 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1318 | static struct of_platform_driver of_mpc8xxx_spi_driver = { |
| 1319 | .name = "mpc8xxx_spi", |
| 1320 | .match_table = of_mpc8xxx_spi_match, |
| 1321 | .probe = of_mpc8xxx_spi_probe, |
| 1322 | .remove = __devexit_p(of_mpc8xxx_spi_remove), |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1323 | }; |
| 1324 | |
| 1325 | #ifdef CONFIG_MPC832x_RDB |
| 1326 | /* |
| 1327 | * XXX XXX XXX |
| 1328 | * This is "legacy" platform driver, was used by the MPC8323E-RDB boards |
| 1329 | * only. The driver should go away soon, since newer MPC8323E-RDB's device |
| 1330 | * tree can work with OpenFirmware driver. But for now we support old trees |
| 1331 | * as well. |
| 1332 | */ |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1333 | static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1334 | { |
| 1335 | struct resource *mem; |
| 1336 | unsigned int irq; |
| 1337 | struct spi_master *master; |
| 1338 | |
| 1339 | if (!pdev->dev.platform_data) |
| 1340 | return -EINVAL; |
| 1341 | |
| 1342 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1343 | if (!mem) |
| 1344 | return -EINVAL; |
| 1345 | |
| 1346 | irq = platform_get_irq(pdev, 0); |
| 1347 | if (!irq) |
| 1348 | return -EINVAL; |
| 1349 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1350 | master = mpc8xxx_spi_probe(&pdev->dev, mem, irq); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1351 | if (IS_ERR(master)) |
| 1352 | return PTR_ERR(master); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1356 | static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1357 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1358 | return mpc8xxx_spi_remove(&pdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1361 | MODULE_ALIAS("platform:mpc8xxx_spi"); |
| 1362 | static struct platform_driver mpc8xxx_spi_driver = { |
| 1363 | .probe = plat_mpc8xxx_spi_probe, |
| 1364 | .remove = __exit_p(plat_mpc8xxx_spi_remove), |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1365 | .driver = { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1366 | .name = "mpc8xxx_spi", |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1367 | .owner = THIS_MODULE, |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1368 | }, |
| 1369 | }; |
| 1370 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1371 | static bool legacy_driver_failed; |
| 1372 | |
| 1373 | static void __init legacy_driver_register(void) |
| 1374 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1375 | legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static void __exit legacy_driver_unregister(void) |
| 1379 | { |
| 1380 | if (legacy_driver_failed) |
| 1381 | return; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1382 | platform_driver_unregister(&mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1383 | } |
| 1384 | #else |
| 1385 | static void __init legacy_driver_register(void) {} |
| 1386 | static void __exit legacy_driver_unregister(void) {} |
| 1387 | #endif /* CONFIG_MPC832x_RDB */ |
| 1388 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1389 | static int __init mpc8xxx_spi_init(void) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1390 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1391 | legacy_driver_register(); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1392 | return of_register_platform_driver(&of_mpc8xxx_spi_driver); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1395 | static void __exit mpc8xxx_spi_exit(void) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1396 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1397 | of_unregister_platform_driver(&of_mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 1398 | legacy_driver_unregister(); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1401 | module_init(mpc8xxx_spi_init); |
| 1402 | module_exit(mpc8xxx_spi_exit); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1403 | |
| 1404 | MODULE_AUTHOR("Kumar Gala"); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 1405 | MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver"); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1406 | MODULE_LICENSE("GPL"); |