Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * MPC512x PSC in SPI mode driver. |
| 3 | * |
| 4 | * Copyright (C) 2007,2008 Freescale Semiconductor Inc. |
| 5 | * Original port from 52xx driver: |
| 6 | * Hongjun Chen <hong-jun.chen@freescale.com> |
| 7 | * |
| 8 | * Fork of mpc52xx_psc_spi.c: |
| 9 | * Copyright (C) 2006 TOPTICA Photonics AG., Dragos Carp |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/interrupt.h> |
Grant Likely | 22ae782 | 2010-07-29 11:49:01 -0600 | [diff] [blame] | 22 | #include <linux/of_address.h> |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 23 | #include <linux/of_platform.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/completion.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/clk.h> |
| 29 | #include <linux/spi/spi.h> |
| 30 | #include <linux/fsl_devices.h> |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 31 | #include <linux/gpio.h> |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 32 | #include <asm/mpc52xx_psc.h> |
| 33 | |
| 34 | struct mpc512x_psc_spi { |
| 35 | void (*cs_control)(struct spi_device *spi, bool on); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 36 | |
| 37 | /* driver internal data */ |
| 38 | struct mpc52xx_psc __iomem *psc; |
| 39 | struct mpc512x_psc_fifo __iomem *fifo; |
| 40 | unsigned int irq; |
| 41 | u8 bits_per_word; |
| 42 | u8 busy; |
| 43 | u32 mclk; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 44 | |
| 45 | struct workqueue_struct *workqueue; |
| 46 | struct work_struct work; |
| 47 | |
| 48 | struct list_head queue; |
| 49 | spinlock_t lock; /* Message queue lock */ |
| 50 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 51 | struct completion txisrdone; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /* controller state */ |
| 55 | struct mpc512x_psc_spi_cs { |
| 56 | int bits_per_word; |
| 57 | int speed_hz; |
| 58 | }; |
| 59 | |
| 60 | /* set clock freq, clock ramp, bits per work |
| 61 | * if t is NULL then reset the values to the default values |
| 62 | */ |
| 63 | static int mpc512x_psc_spi_transfer_setup(struct spi_device *spi, |
| 64 | struct spi_transfer *t) |
| 65 | { |
| 66 | struct mpc512x_psc_spi_cs *cs = spi->controller_state; |
| 67 | |
| 68 | cs->speed_hz = (t && t->speed_hz) |
| 69 | ? t->speed_hz : spi->max_speed_hz; |
| 70 | cs->bits_per_word = (t && t->bits_per_word) |
| 71 | ? t->bits_per_word : spi->bits_per_word; |
| 72 | cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static void mpc512x_psc_spi_activate_cs(struct spi_device *spi) |
| 77 | { |
| 78 | struct mpc512x_psc_spi_cs *cs = spi->controller_state; |
| 79 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 80 | struct mpc52xx_psc __iomem *psc = mps->psc; |
| 81 | u32 sicr; |
| 82 | u32 ccr; |
| 83 | u16 bclkdiv; |
| 84 | |
| 85 | sicr = in_be32(&psc->sicr); |
| 86 | |
| 87 | /* Set clock phase and polarity */ |
| 88 | if (spi->mode & SPI_CPHA) |
| 89 | sicr |= 0x00001000; |
| 90 | else |
| 91 | sicr &= ~0x00001000; |
| 92 | |
| 93 | if (spi->mode & SPI_CPOL) |
| 94 | sicr |= 0x00002000; |
| 95 | else |
| 96 | sicr &= ~0x00002000; |
| 97 | |
| 98 | if (spi->mode & SPI_LSB_FIRST) |
| 99 | sicr |= 0x10000000; |
| 100 | else |
| 101 | sicr &= ~0x10000000; |
| 102 | out_be32(&psc->sicr, sicr); |
| 103 | |
| 104 | ccr = in_be32(&psc->ccr); |
| 105 | ccr &= 0xFF000000; |
| 106 | if (cs->speed_hz) |
| 107 | bclkdiv = (mps->mclk / cs->speed_hz) - 1; |
| 108 | else |
| 109 | bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */ |
| 110 | |
| 111 | ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8)); |
| 112 | out_be32(&psc->ccr, ccr); |
| 113 | mps->bits_per_word = cs->bits_per_word; |
| 114 | |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 115 | if (mps->cs_control && gpio_is_valid(spi->cs_gpio)) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 116 | mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0); |
| 117 | } |
| 118 | |
| 119 | static void mpc512x_psc_spi_deactivate_cs(struct spi_device *spi) |
| 120 | { |
| 121 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 122 | |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 123 | if (mps->cs_control && gpio_is_valid(spi->cs_gpio)) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 124 | mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | /* extract and scale size field in txsz or rxsz */ |
| 129 | #define MPC512x_PSC_FIFO_SZ(sz) ((sz & 0x7ff) << 2); |
| 130 | |
| 131 | #define EOFBYTE 1 |
| 132 | |
| 133 | static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi, |
| 134 | struct spi_transfer *t) |
| 135 | { |
| 136 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 137 | struct mpc52xx_psc __iomem *psc = mps->psc; |
| 138 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 139 | size_t tx_len = t->len; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 140 | u8 *tx_buf = (u8 *)t->tx_buf; |
| 141 | u8 *rx_buf = (u8 *)t->rx_buf; |
| 142 | |
| 143 | if (!tx_buf && !rx_buf && t->len) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | /* Zero MR2 */ |
| 147 | in_8(&psc->mode); |
| 148 | out_8(&psc->mode, 0x0); |
| 149 | |
Anatolij Gustschin | c3e2aa8 | 2013-04-01 17:31:19 +0200 | [diff] [blame] | 150 | /* enable transmiter/receiver */ |
| 151 | out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE); |
| 152 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 153 | while (tx_len) { |
| 154 | size_t txcount; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 155 | int i; |
| 156 | u8 data; |
| 157 | size_t fifosz; |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 158 | size_t rxcount; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * The number of bytes that can be sent at a time |
| 162 | * depends on the fifo size. |
| 163 | */ |
| 164 | fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz)); |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 165 | txcount = min(fifosz, tx_len); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 166 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 167 | for (i = txcount; i > 0; i--) { |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 168 | data = tx_buf ? *tx_buf++ : 0; |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 169 | if (tx_len == EOFBYTE && t->cs_change) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 170 | setbits32(&fifo->txcmd, MPC512x_PSC_FIFO_EOF); |
| 171 | out_8(&fifo->txdata_8, data); |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 172 | tx_len--; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 175 | INIT_COMPLETION(mps->txisrdone); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 176 | |
| 177 | /* interrupt on tx fifo empty */ |
| 178 | out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY); |
| 179 | out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY); |
| 180 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 181 | wait_for_completion(&mps->txisrdone); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 182 | |
| 183 | mdelay(1); |
| 184 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 185 | /* rx fifo should have txcount bytes in it */ |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 186 | rxcount = in_be32(&fifo->rxcnt); |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 187 | if (rxcount != txcount) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 188 | mdelay(1); |
| 189 | |
| 190 | rxcount = in_be32(&fifo->rxcnt); |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 191 | if (rxcount != txcount) { |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 192 | dev_warn(&spi->dev, "expected %d bytes in rx fifo " |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 193 | "but got %d\n", txcount, rxcount); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 196 | rxcount = min(rxcount, txcount); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 197 | for (i = rxcount; i > 0; i--) { |
| 198 | data = in_8(&fifo->rxdata_8); |
| 199 | if (rx_buf) |
| 200 | *rx_buf++ = data; |
| 201 | } |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 202 | while (in_be32(&fifo->rxcnt)) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 203 | in_8(&fifo->rxdata_8); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 204 | } |
| 205 | /* disable transmiter/receiver and fifo interrupt */ |
| 206 | out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); |
| 207 | out_be32(&fifo->tximr, 0); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static void mpc512x_psc_spi_work(struct work_struct *work) |
| 212 | { |
| 213 | struct mpc512x_psc_spi *mps = container_of(work, |
| 214 | struct mpc512x_psc_spi, |
| 215 | work); |
| 216 | |
| 217 | spin_lock_irq(&mps->lock); |
| 218 | mps->busy = 1; |
| 219 | while (!list_empty(&mps->queue)) { |
| 220 | struct spi_message *m; |
| 221 | struct spi_device *spi; |
| 222 | struct spi_transfer *t = NULL; |
| 223 | unsigned cs_change; |
| 224 | int status; |
| 225 | |
| 226 | m = container_of(mps->queue.next, struct spi_message, queue); |
| 227 | list_del_init(&m->queue); |
| 228 | spin_unlock_irq(&mps->lock); |
| 229 | |
| 230 | spi = m->spi; |
| 231 | cs_change = 1; |
| 232 | status = 0; |
| 233 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 234 | if (t->bits_per_word || t->speed_hz) { |
| 235 | status = mpc512x_psc_spi_transfer_setup(spi, t); |
| 236 | if (status < 0) |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | if (cs_change) |
| 241 | mpc512x_psc_spi_activate_cs(spi); |
| 242 | cs_change = t->cs_change; |
| 243 | |
| 244 | status = mpc512x_psc_spi_transfer_rxtx(spi, t); |
| 245 | if (status) |
| 246 | break; |
| 247 | m->actual_length += t->len; |
| 248 | |
| 249 | if (t->delay_usecs) |
| 250 | udelay(t->delay_usecs); |
| 251 | |
| 252 | if (cs_change) |
| 253 | mpc512x_psc_spi_deactivate_cs(spi); |
| 254 | } |
| 255 | |
| 256 | m->status = status; |
| 257 | m->complete(m->context); |
| 258 | |
| 259 | if (status || !cs_change) |
| 260 | mpc512x_psc_spi_deactivate_cs(spi); |
| 261 | |
| 262 | mpc512x_psc_spi_transfer_setup(spi, NULL); |
| 263 | |
| 264 | spin_lock_irq(&mps->lock); |
| 265 | } |
| 266 | mps->busy = 0; |
| 267 | spin_unlock_irq(&mps->lock); |
| 268 | } |
| 269 | |
| 270 | static int mpc512x_psc_spi_setup(struct spi_device *spi) |
| 271 | { |
| 272 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 273 | struct mpc512x_psc_spi_cs *cs = spi->controller_state; |
| 274 | unsigned long flags; |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 275 | int ret; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 276 | |
| 277 | if (spi->bits_per_word % 8) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | if (!cs) { |
| 281 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
| 282 | if (!cs) |
| 283 | return -ENOMEM; |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 284 | |
| 285 | if (gpio_is_valid(spi->cs_gpio)) { |
| 286 | ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev)); |
| 287 | if (ret) { |
| 288 | dev_err(&spi->dev, "can't get CS gpio: %d\n", |
| 289 | ret); |
| 290 | kfree(cs); |
| 291 | return ret; |
| 292 | } |
| 293 | gpio_direction_output(spi->cs_gpio, |
| 294 | spi->mode & SPI_CS_HIGH ? 0 : 1); |
| 295 | } |
| 296 | |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 297 | spi->controller_state = cs; |
| 298 | } |
| 299 | |
| 300 | cs->bits_per_word = spi->bits_per_word; |
| 301 | cs->speed_hz = spi->max_speed_hz; |
| 302 | |
| 303 | spin_lock_irqsave(&mps->lock, flags); |
| 304 | if (!mps->busy) |
| 305 | mpc512x_psc_spi_deactivate_cs(spi); |
| 306 | spin_unlock_irqrestore(&mps->lock, flags); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int mpc512x_psc_spi_transfer(struct spi_device *spi, |
| 312 | struct spi_message *m) |
| 313 | { |
| 314 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 315 | unsigned long flags; |
| 316 | |
| 317 | m->actual_length = 0; |
| 318 | m->status = -EINPROGRESS; |
| 319 | |
| 320 | spin_lock_irqsave(&mps->lock, flags); |
| 321 | list_add_tail(&m->queue, &mps->queue); |
| 322 | queue_work(mps->workqueue, &mps->work); |
| 323 | spin_unlock_irqrestore(&mps->lock, flags); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static void mpc512x_psc_spi_cleanup(struct spi_device *spi) |
| 329 | { |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 330 | if (gpio_is_valid(spi->cs_gpio)) |
| 331 | gpio_free(spi->cs_gpio); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 332 | kfree(spi->controller_state); |
| 333 | } |
| 334 | |
| 335 | static int mpc512x_psc_spi_port_config(struct spi_master *master, |
| 336 | struct mpc512x_psc_spi *mps) |
| 337 | { |
| 338 | struct mpc52xx_psc __iomem *psc = mps->psc; |
| 339 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; |
| 340 | struct clk *spiclk; |
| 341 | int ret = 0; |
| 342 | char name[32]; |
| 343 | u32 sicr; |
| 344 | u32 ccr; |
| 345 | u16 bclkdiv; |
| 346 | |
| 347 | sprintf(name, "psc%d_mclk", master->bus_num); |
| 348 | spiclk = clk_get(&master->dev, name); |
| 349 | clk_enable(spiclk); |
| 350 | mps->mclk = clk_get_rate(spiclk); |
| 351 | clk_put(spiclk); |
| 352 | |
| 353 | /* Reset the PSC into a known state */ |
| 354 | out_8(&psc->command, MPC52xx_PSC_RST_RX); |
| 355 | out_8(&psc->command, MPC52xx_PSC_RST_TX); |
| 356 | out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); |
| 357 | |
| 358 | /* Disable psc interrupts all useful interrupts are in fifo */ |
| 359 | out_be16(&psc->isr_imr.imr, 0); |
| 360 | |
| 361 | /* Disable fifo interrupts, will be enabled later */ |
| 362 | out_be32(&fifo->tximr, 0); |
| 363 | out_be32(&fifo->rximr, 0); |
| 364 | |
| 365 | /* Setup fifo slice address and size */ |
| 366 | /*out_be32(&fifo->txsz, 0x0fe00004);*/ |
| 367 | /*out_be32(&fifo->rxsz, 0x0ff00004);*/ |
| 368 | |
| 369 | sicr = 0x01000000 | /* SIM = 0001 -- 8 bit */ |
| 370 | 0x00800000 | /* GenClk = 1 -- internal clk */ |
| 371 | 0x00008000 | /* SPI = 1 */ |
| 372 | 0x00004000 | /* MSTR = 1 -- SPI master */ |
| 373 | 0x00000800; /* UseEOF = 1 -- SS low until EOF */ |
| 374 | |
| 375 | out_be32(&psc->sicr, sicr); |
| 376 | |
| 377 | ccr = in_be32(&psc->ccr); |
| 378 | ccr &= 0xFF000000; |
| 379 | bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */ |
| 380 | ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8)); |
| 381 | out_be32(&psc->ccr, ccr); |
| 382 | |
| 383 | /* Set 2ms DTL delay */ |
| 384 | out_8(&psc->ctur, 0x00); |
| 385 | out_8(&psc->ctlr, 0x82); |
| 386 | |
| 387 | /* we don't use the alarms */ |
| 388 | out_be32(&fifo->rxalarm, 0xfff); |
| 389 | out_be32(&fifo->txalarm, 0); |
| 390 | |
| 391 | /* Enable FIFO slices for Rx/Tx */ |
| 392 | out_be32(&fifo->rxcmd, |
| 393 | MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA); |
| 394 | out_be32(&fifo->txcmd, |
| 395 | MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA); |
| 396 | |
| 397 | mps->bits_per_word = 8; |
| 398 | |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id) |
| 403 | { |
| 404 | struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id; |
| 405 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; |
| 406 | |
| 407 | /* clear interrupt and wake up the work queue */ |
| 408 | if (in_be32(&fifo->txisr) & |
| 409 | in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) { |
| 410 | out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY); |
| 411 | out_be32(&fifo->tximr, 0); |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 412 | complete(&mps->txisrdone); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 413 | return IRQ_HANDLED; |
| 414 | } |
| 415 | return IRQ_NONE; |
| 416 | } |
| 417 | |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 418 | static void mpc512x_spi_cs_control(struct spi_device *spi, bool onoff) |
| 419 | { |
| 420 | gpio_set_value(spi->cs_gpio, onoff); |
| 421 | } |
| 422 | |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 423 | /* bus_num is used only for the case dev->platform_data == NULL */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 424 | static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr, |
Anatolij Gustschin | cf40f08 | 2010-07-05 12:17:51 +0200 | [diff] [blame] | 425 | u32 size, unsigned int irq, |
| 426 | s16 bus_num) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 427 | { |
| 428 | struct fsl_spi_platform_data *pdata = dev->platform_data; |
| 429 | struct mpc512x_psc_spi *mps; |
| 430 | struct spi_master *master; |
| 431 | int ret; |
| 432 | void *tempp; |
| 433 | |
| 434 | master = spi_alloc_master(dev, sizeof *mps); |
| 435 | if (master == NULL) |
| 436 | return -ENOMEM; |
| 437 | |
| 438 | dev_set_drvdata(dev, master); |
| 439 | mps = spi_master_get_devdata(master); |
| 440 | mps->irq = irq; |
| 441 | |
| 442 | if (pdata == NULL) { |
Anatolij Gustschin | 86e9874 | 2013-04-01 17:29:21 +0200 | [diff] [blame] | 443 | mps->cs_control = mpc512x_spi_cs_control; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 444 | master->bus_num = bus_num; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 445 | } else { |
| 446 | mps->cs_control = pdata->cs_control; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 447 | master->bus_num = pdata->bus_num; |
| 448 | master->num_chipselect = pdata->max_chipselect; |
| 449 | } |
| 450 | |
Anatolij Gustschin | c88dd34 | 2013-01-14 21:27:00 +0100 | [diff] [blame] | 451 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 452 | master->setup = mpc512x_psc_spi_setup; |
| 453 | master->transfer = mpc512x_psc_spi_transfer; |
| 454 | master->cleanup = mpc512x_psc_spi_cleanup; |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 455 | master->dev.of_node = dev->of_node; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 456 | |
| 457 | tempp = ioremap(regaddr, size); |
| 458 | if (!tempp) { |
| 459 | dev_err(dev, "could not ioremap I/O port range\n"); |
| 460 | ret = -EFAULT; |
| 461 | goto free_master; |
| 462 | } |
| 463 | mps->psc = tempp; |
| 464 | mps->fifo = |
| 465 | (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc)); |
| 466 | |
| 467 | ret = request_irq(mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED, |
| 468 | "mpc512x-psc-spi", mps); |
| 469 | if (ret) |
| 470 | goto free_master; |
| 471 | |
| 472 | ret = mpc512x_psc_spi_port_config(master, mps); |
| 473 | if (ret < 0) |
| 474 | goto free_irq; |
| 475 | |
| 476 | spin_lock_init(&mps->lock); |
Gerhard Sittig | c36e93a | 2013-06-03 14:03:49 +0200 | [diff] [blame^] | 477 | init_completion(&mps->txisrdone); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 478 | INIT_WORK(&mps->work, mpc512x_psc_spi_work); |
| 479 | INIT_LIST_HEAD(&mps->queue); |
| 480 | |
| 481 | mps->workqueue = |
| 482 | create_singlethread_workqueue(dev_name(master->dev.parent)); |
| 483 | if (mps->workqueue == NULL) { |
| 484 | ret = -EBUSY; |
| 485 | goto free_irq; |
| 486 | } |
| 487 | |
| 488 | ret = spi_register_master(master); |
| 489 | if (ret < 0) |
| 490 | goto unreg_master; |
| 491 | |
| 492 | return ret; |
| 493 | |
| 494 | unreg_master: |
| 495 | destroy_workqueue(mps->workqueue); |
| 496 | free_irq: |
| 497 | free_irq(mps->irq, mps); |
| 498 | free_master: |
| 499 | if (mps->psc) |
| 500 | iounmap(mps->psc); |
| 501 | spi_master_put(master); |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 506 | static int mpc512x_psc_spi_do_remove(struct device *dev) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 507 | { |
Guenter Roeck | 2187921 | 2012-08-18 09:29:24 -0700 | [diff] [blame] | 508 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 509 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(master); |
| 510 | |
| 511 | flush_workqueue(mps->workqueue); |
| 512 | destroy_workqueue(mps->workqueue); |
| 513 | spi_unregister_master(master); |
| 514 | free_irq(mps->irq, mps); |
| 515 | if (mps->psc) |
| 516 | iounmap(mps->psc); |
Guenter Roeck | 2187921 | 2012-08-18 09:29:24 -0700 | [diff] [blame] | 517 | spi_master_put(master); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 522 | static int mpc512x_psc_spi_of_probe(struct platform_device *op) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 523 | { |
| 524 | const u32 *regaddr_p; |
| 525 | u64 regaddr64, size64; |
| 526 | s16 id = -1; |
| 527 | |
Anatolij Gustschin | ef7f2e8 | 2010-05-31 18:34:54 +0200 | [diff] [blame] | 528 | regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 529 | if (!regaddr_p) { |
| 530 | dev_err(&op->dev, "Invalid PSC address\n"); |
| 531 | return -EINVAL; |
| 532 | } |
Anatolij Gustschin | ef7f2e8 | 2010-05-31 18:34:54 +0200 | [diff] [blame] | 533 | regaddr64 = of_translate_address(op->dev.of_node, regaddr_p); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 534 | |
| 535 | /* get PSC id (0..11, used by port_config) */ |
Anatolij Gustschin | 9d15a3b | 2013-01-11 01:05:48 +0100 | [diff] [blame] | 536 | id = of_alias_get_id(op->dev.of_node, "spi"); |
| 537 | if (id < 0) { |
| 538 | dev_err(&op->dev, "no alias id for %s\n", |
| 539 | op->dev.of_node->full_name); |
| 540 | return id; |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64, |
Anatolij Gustschin | ef7f2e8 | 2010-05-31 18:34:54 +0200 | [diff] [blame] | 544 | irq_of_parse_and_map(op->dev.of_node, 0), id); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 547 | static int mpc512x_psc_spi_of_remove(struct platform_device *op) |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 548 | { |
| 549 | return mpc512x_psc_spi_do_remove(&op->dev); |
| 550 | } |
| 551 | |
| 552 | static struct of_device_id mpc512x_psc_spi_of_match[] = { |
| 553 | { .compatible = "fsl,mpc5121-psc-spi", }, |
| 554 | {}, |
| 555 | }; |
| 556 | |
| 557 | MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match); |
| 558 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 559 | static struct platform_driver mpc512x_psc_spi_of_driver = { |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 560 | .probe = mpc512x_psc_spi_of_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 561 | .remove = mpc512x_psc_spi_of_remove, |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 562 | .driver = { |
| 563 | .name = "mpc512x-psc-spi", |
| 564 | .owner = THIS_MODULE, |
Anatolij Gustschin | ef7f2e8 | 2010-05-31 18:34:54 +0200 | [diff] [blame] | 565 | .of_match_table = mpc512x_psc_spi_of_match, |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 566 | }, |
| 567 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 568 | module_platform_driver(mpc512x_psc_spi_of_driver); |
Anatolij Gustschin | 6e27388f1b | 2010-04-30 13:21:27 +0000 | [diff] [blame] | 569 | |
| 570 | MODULE_AUTHOR("John Rigby"); |
| 571 | MODULE_DESCRIPTION("MPC512x PSC SPI Driver"); |
| 572 | MODULE_LICENSE("GPL"); |