Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 1 | /* |
Grant Likely | 57cc097 | 2008-05-14 16:05:29 -0700 | [diff] [blame] | 2 | * MPC52xx PSC in SPI mode driver. |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 3 | * |
| 4 | * Maintainer: Dragos Carp |
| 5 | * |
| 6 | * Copyright (C) 2006 TOPTICA Photonics AG. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
Anton Vorontsov | 7390284 | 2009-06-17 16:26:05 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/interrupt.h> |
Grant Likely | 22ae782 | 2010-07-29 11:49:01 -0600 | [diff] [blame] | 18 | #include <linux/of_address.h> |
Stephen Rothwell | 76ef7dd | 2008-05-23 16:35:47 +1000 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/completion.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/spi/spi.h> |
| 25 | #include <linux/fsl_devices.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/mpc52xx.h> |
| 29 | #include <asm/mpc52xx_psc.h> |
| 30 | |
| 31 | #define MCLK 20000000 /* PSC port MClk in hz */ |
| 32 | |
| 33 | struct mpc52xx_psc_spi { |
| 34 | /* fsl_spi_platform data */ |
Anton Vorontsov | 7390284 | 2009-06-17 16:26:05 -0700 | [diff] [blame] | 35 | void (*cs_control)(struct spi_device *spi, bool on); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 36 | u32 sysclk; |
| 37 | |
| 38 | /* driver internal data */ |
| 39 | struct mpc52xx_psc __iomem *psc; |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 40 | struct mpc52xx_psc_fifo __iomem *fifo; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 41 | unsigned int irq; |
| 42 | u8 bits_per_word; |
| 43 | u8 busy; |
| 44 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 45 | struct work_struct work; |
| 46 | |
| 47 | struct list_head queue; |
| 48 | spinlock_t lock; |
| 49 | |
| 50 | struct completion done; |
| 51 | }; |
| 52 | |
| 53 | /* controller state */ |
| 54 | struct mpc52xx_psc_spi_cs { |
| 55 | int bits_per_word; |
| 56 | int speed_hz; |
| 57 | }; |
| 58 | |
| 59 | /* set clock freq, clock ramp, bits per work |
| 60 | * if t is NULL then reset the values to the default values |
| 61 | */ |
| 62 | static int mpc52xx_psc_spi_transfer_setup(struct spi_device *spi, |
| 63 | struct spi_transfer *t) |
| 64 | { |
| 65 | struct mpc52xx_psc_spi_cs *cs = spi->controller_state; |
| 66 | |
| 67 | cs->speed_hz = (t && t->speed_hz) |
| 68 | ? t->speed_hz : spi->max_speed_hz; |
| 69 | cs->bits_per_word = (t && t->bits_per_word) |
| 70 | ? t->bits_per_word : spi->bits_per_word; |
| 71 | cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static void mpc52xx_psc_spi_activate_cs(struct spi_device *spi) |
| 76 | { |
| 77 | struct mpc52xx_psc_spi_cs *cs = spi->controller_state; |
| 78 | struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 79 | struct mpc52xx_psc __iomem *psc = mps->psc; |
| 80 | u32 sicr; |
| 81 | u16 ccr; |
| 82 | |
| 83 | sicr = in_be32(&psc->sicr); |
| 84 | |
| 85 | /* Set clock phase and polarity */ |
| 86 | if (spi->mode & SPI_CPHA) |
| 87 | sicr |= 0x00001000; |
| 88 | else |
| 89 | sicr &= ~0x00001000; |
| 90 | if (spi->mode & SPI_CPOL) |
| 91 | sicr |= 0x00002000; |
| 92 | else |
| 93 | sicr &= ~0x00002000; |
| 94 | |
| 95 | if (spi->mode & SPI_LSB_FIRST) |
| 96 | sicr |= 0x10000000; |
| 97 | else |
| 98 | sicr &= ~0x10000000; |
| 99 | out_be32(&psc->sicr, sicr); |
| 100 | |
| 101 | /* Set clock frequency and bits per word |
| 102 | * Because psc->ccr is defined as 16bit register instead of 32bit |
| 103 | * just set the lower byte of BitClkDiv |
| 104 | */ |
Grant Likely | a897ea1 | 2008-10-08 09:02:11 -0600 | [diff] [blame] | 105 | ccr = in_be16((u16 __iomem *)&psc->ccr); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 106 | ccr &= 0xFF00; |
| 107 | if (cs->speed_hz) |
| 108 | ccr |= (MCLK / cs->speed_hz - 1) & 0xFF; |
| 109 | else /* by default SPI Clk 1MHz */ |
| 110 | ccr |= (MCLK / 1000000 - 1) & 0xFF; |
Grant Likely | a897ea1 | 2008-10-08 09:02:11 -0600 | [diff] [blame] | 111 | out_be16((u16 __iomem *)&psc->ccr, ccr); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 112 | mps->bits_per_word = cs->bits_per_word; |
| 113 | |
Anton Vorontsov | 7390284 | 2009-06-17 16:26:05 -0700 | [diff] [blame] | 114 | if (mps->cs_control) |
| 115 | mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void mpc52xx_psc_spi_deactivate_cs(struct spi_device *spi) |
| 119 | { |
| 120 | struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 121 | |
Anton Vorontsov | 7390284 | 2009-06-17 16:26:05 -0700 | [diff] [blame] | 122 | if (mps->cs_control) |
| 123 | mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | #define MPC52xx_PSC_BUFSIZE (MPC52xx_PSC_RFNUM_MASK + 1) |
| 127 | /* wake up when 80% fifo full */ |
| 128 | #define MPC52xx_PSC_RFALARM (MPC52xx_PSC_BUFSIZE * 20 / 100) |
| 129 | |
| 130 | static int mpc52xx_psc_spi_transfer_rxtx(struct spi_device *spi, |
| 131 | struct spi_transfer *t) |
| 132 | { |
| 133 | struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 134 | struct mpc52xx_psc __iomem *psc = mps->psc; |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 135 | struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 136 | unsigned rb = 0; /* number of bytes receieved */ |
| 137 | unsigned sb = 0; /* number of bytes sent */ |
| 138 | unsigned char *rx_buf = (unsigned char *)t->rx_buf; |
| 139 | unsigned char *tx_buf = (unsigned char *)t->tx_buf; |
| 140 | unsigned rfalarm; |
| 141 | unsigned send_at_once = MPC52xx_PSC_BUFSIZE; |
| 142 | unsigned recv_at_once; |
Stefano Babic | b7d271d | 2008-12-01 13:13:53 -0800 | [diff] [blame] | 143 | int last_block = 0; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 144 | |
| 145 | if (!t->tx_buf && !t->rx_buf && t->len) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | /* enable transmiter/receiver */ |
| 149 | out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE); |
| 150 | while (rb < t->len) { |
| 151 | if (t->len - rb > MPC52xx_PSC_BUFSIZE) { |
| 152 | rfalarm = MPC52xx_PSC_RFALARM; |
Stefano Babic | b7d271d | 2008-12-01 13:13:53 -0800 | [diff] [blame] | 153 | last_block = 0; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 154 | } else { |
| 155 | send_at_once = t->len - sb; |
| 156 | rfalarm = MPC52xx_PSC_BUFSIZE - (t->len - rb); |
Stefano Babic | b7d271d | 2008-12-01 13:13:53 -0800 | [diff] [blame] | 157 | last_block = 1; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | dev_dbg(&spi->dev, "send %d bytes...\n", send_at_once); |
Luotao Fu | 9a7867e | 2008-07-28 15:46:32 -0700 | [diff] [blame] | 161 | for (; send_at_once; sb++, send_at_once--) { |
| 162 | /* set EOF flag before the last word is sent */ |
Stefano Babic | b7d271d | 2008-12-01 13:13:53 -0800 | [diff] [blame] | 163 | if (send_at_once == 1 && last_block) |
Luotao Fu | 9a7867e | 2008-07-28 15:46:32 -0700 | [diff] [blame] | 164 | out_8(&psc->ircr2, 0x01); |
| 165 | |
| 166 | if (tx_buf) |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 167 | out_8(&psc->mpc52xx_psc_buffer_8, tx_buf[sb]); |
Luotao Fu | 9a7867e | 2008-07-28 15:46:32 -0700 | [diff] [blame] | 168 | else |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 169 | out_8(&psc->mpc52xx_psc_buffer_8, 0); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 173 | /* enable interrupts and wait for wake up |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 174 | * if just one byte is expected the Rx FIFO genererates no |
| 175 | * FFULL interrupt, so activate the RxRDY interrupt |
| 176 | */ |
| 177 | out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1); |
| 178 | if (t->len - rb == 1) { |
| 179 | out_8(&psc->mode, 0); |
| 180 | } else { |
| 181 | out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL); |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 182 | out_be16(&fifo->rfalarm, rfalarm); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 183 | } |
| 184 | out_be16(&psc->mpc52xx_psc_imr, MPC52xx_PSC_IMR_RXRDY); |
| 185 | wait_for_completion(&mps->done); |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 186 | recv_at_once = in_be16(&fifo->rfnum); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 187 | dev_dbg(&spi->dev, "%d bytes received\n", recv_at_once); |
| 188 | |
| 189 | send_at_once = recv_at_once; |
| 190 | if (rx_buf) { |
| 191 | for (; recv_at_once; rb++, recv_at_once--) |
| 192 | rx_buf[rb] = in_8(&psc->mpc52xx_psc_buffer_8); |
| 193 | } else { |
| 194 | for (; recv_at_once; rb++, recv_at_once--) |
| 195 | in_8(&psc->mpc52xx_psc_buffer_8); |
| 196 | } |
| 197 | } |
| 198 | /* disable transmiter/receiver */ |
| 199 | out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static void mpc52xx_psc_spi_work(struct work_struct *work) |
| 205 | { |
| 206 | struct mpc52xx_psc_spi *mps = |
| 207 | container_of(work, struct mpc52xx_psc_spi, work); |
| 208 | |
| 209 | spin_lock_irq(&mps->lock); |
| 210 | mps->busy = 1; |
| 211 | while (!list_empty(&mps->queue)) { |
| 212 | struct spi_message *m; |
| 213 | struct spi_device *spi; |
| 214 | struct spi_transfer *t = NULL; |
| 215 | unsigned cs_change; |
| 216 | int status; |
| 217 | |
| 218 | m = container_of(mps->queue.next, struct spi_message, queue); |
| 219 | list_del_init(&m->queue); |
| 220 | spin_unlock_irq(&mps->lock); |
| 221 | |
| 222 | spi = m->spi; |
| 223 | cs_change = 1; |
| 224 | status = 0; |
| 225 | list_for_each_entry (t, &m->transfers, transfer_list) { |
| 226 | if (t->bits_per_word || t->speed_hz) { |
| 227 | status = mpc52xx_psc_spi_transfer_setup(spi, t); |
| 228 | if (status < 0) |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if (cs_change) |
| 233 | mpc52xx_psc_spi_activate_cs(spi); |
| 234 | cs_change = t->cs_change; |
| 235 | |
| 236 | status = mpc52xx_psc_spi_transfer_rxtx(spi, t); |
| 237 | if (status) |
| 238 | break; |
| 239 | m->actual_length += t->len; |
| 240 | |
| 241 | if (t->delay_usecs) |
| 242 | udelay(t->delay_usecs); |
| 243 | |
| 244 | if (cs_change) |
| 245 | mpc52xx_psc_spi_deactivate_cs(spi); |
| 246 | } |
| 247 | |
| 248 | m->status = status; |
Axel Lin | 0a6d387 | 2014-04-02 22:21:04 +0800 | [diff] [blame] | 249 | if (m->complete) |
| 250 | m->complete(m->context); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 251 | |
| 252 | if (status || !cs_change) |
| 253 | mpc52xx_psc_spi_deactivate_cs(spi); |
| 254 | |
| 255 | mpc52xx_psc_spi_transfer_setup(spi, NULL); |
| 256 | |
| 257 | spin_lock_irq(&mps->lock); |
| 258 | } |
| 259 | mps->busy = 0; |
| 260 | spin_unlock_irq(&mps->lock); |
| 261 | } |
| 262 | |
| 263 | static int mpc52xx_psc_spi_setup(struct spi_device *spi) |
| 264 | { |
| 265 | struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 266 | struct mpc52xx_psc_spi_cs *cs = spi->controller_state; |
| 267 | unsigned long flags; |
| 268 | |
| 269 | if (spi->bits_per_word%8) |
| 270 | return -EINVAL; |
| 271 | |
| 272 | if (!cs) { |
| 273 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
| 274 | if (!cs) |
| 275 | return -ENOMEM; |
| 276 | spi->controller_state = cs; |
| 277 | } |
| 278 | |
| 279 | cs->bits_per_word = spi->bits_per_word; |
| 280 | cs->speed_hz = spi->max_speed_hz; |
| 281 | |
| 282 | spin_lock_irqsave(&mps->lock, flags); |
| 283 | if (!mps->busy) |
| 284 | mpc52xx_psc_spi_deactivate_cs(spi); |
| 285 | spin_unlock_irqrestore(&mps->lock, flags); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int mpc52xx_psc_spi_transfer(struct spi_device *spi, |
| 291 | struct spi_message *m) |
| 292 | { |
| 293 | struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master); |
| 294 | unsigned long flags; |
| 295 | |
| 296 | m->actual_length = 0; |
| 297 | m->status = -EINPROGRESS; |
| 298 | |
| 299 | spin_lock_irqsave(&mps->lock, flags); |
| 300 | list_add_tail(&m->queue, &mps->queue); |
Bhaktipriya Shridhar | ac96b73 | 2016-07-02 14:27:06 +0530 | [diff] [blame] | 301 | schedule_work(&mps->work); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 302 | spin_unlock_irqrestore(&mps->lock, flags); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void mpc52xx_psc_spi_cleanup(struct spi_device *spi) |
| 308 | { |
| 309 | kfree(spi->controller_state); |
| 310 | } |
| 311 | |
| 312 | static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps) |
| 313 | { |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 314 | struct mpc52xx_psc __iomem *psc = mps->psc; |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 315 | struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 316 | u32 mclken_div; |
Wolfram Sang | f856cf0 | 2009-11-02 03:53:11 +0000 | [diff] [blame] | 317 | int ret; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 318 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 319 | /* default sysclk is 512MHz */ |
Grant Likely | 4fb4c55 | 2008-01-24 22:25:31 -0700 | [diff] [blame] | 320 | mclken_div = (mps->sysclk ? mps->sysclk : 512000000) / MCLK; |
Wolfram Sang | f856cf0 | 2009-11-02 03:53:11 +0000 | [diff] [blame] | 321 | ret = mpc52xx_set_psc_clkdiv(psc_id, mclken_div); |
| 322 | if (ret) |
| 323 | return ret; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 324 | |
| 325 | /* Reset the PSC into a known state */ |
| 326 | out_8(&psc->command, MPC52xx_PSC_RST_RX); |
| 327 | out_8(&psc->command, MPC52xx_PSC_RST_TX); |
| 328 | out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); |
| 329 | |
| 330 | /* Disable interrupts, interrupts are based on alarm level */ |
| 331 | out_be16(&psc->mpc52xx_psc_imr, 0); |
| 332 | out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1); |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 333 | out_8(&fifo->rfcntl, 0); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 334 | out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL); |
| 335 | |
| 336 | /* Configure 8bit codec mode as a SPI master and use EOF flags */ |
| 337 | /* SICR_SIM_CODEC8|SICR_GENCLK|SICR_SPI|SICR_MSTR|SICR_USEEOF */ |
| 338 | out_be32(&psc->sicr, 0x0180C800); |
Grant Likely | a897ea1 | 2008-10-08 09:02:11 -0600 | [diff] [blame] | 339 | out_be16((u16 __iomem *)&psc->ccr, 0x070F); /* default SPI Clk 1MHz */ |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 340 | |
| 341 | /* Set 2ms DTL delay */ |
| 342 | out_8(&psc->ctur, 0x00); |
| 343 | out_8(&psc->ctlr, 0x84); |
| 344 | |
| 345 | mps->bits_per_word = 8; |
| 346 | |
Wolfram Sang | f856cf0 | 2009-11-02 03:53:11 +0000 | [diff] [blame] | 347 | return 0; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static irqreturn_t mpc52xx_psc_spi_isr(int irq, void *dev_id) |
| 351 | { |
| 352 | struct mpc52xx_psc_spi *mps = (struct mpc52xx_psc_spi *)dev_id; |
| 353 | struct mpc52xx_psc __iomem *psc = mps->psc; |
| 354 | |
| 355 | /* disable interrupt and wake up the work queue */ |
| 356 | if (in_be16(&psc->mpc52xx_psc_isr) & MPC52xx_PSC_IMR_RXRDY) { |
| 357 | out_be16(&psc->mpc52xx_psc_imr, 0); |
| 358 | complete(&mps->done); |
| 359 | return IRQ_HANDLED; |
| 360 | } |
| 361 | return IRQ_NONE; |
| 362 | } |
| 363 | |
| 364 | /* bus_num is used only for the case dev->platform_data == NULL */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 365 | static int mpc52xx_psc_spi_do_probe(struct device *dev, u32 regaddr, |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 366 | u32 size, unsigned int irq, s16 bus_num) |
| 367 | { |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 368 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 369 | struct mpc52xx_psc_spi *mps; |
| 370 | struct spi_master *master; |
| 371 | int ret; |
| 372 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 373 | master = spi_alloc_master(dev, sizeof *mps); |
| 374 | if (master == NULL) |
| 375 | return -ENOMEM; |
| 376 | |
| 377 | dev_set_drvdata(dev, master); |
| 378 | mps = spi_master_get_devdata(master); |
| 379 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 380 | /* the spi->mode bits understood by this driver: */ |
| 381 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST; |
| 382 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 383 | mps->irq = irq; |
| 384 | if (pdata == NULL) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 385 | dev_warn(dev, |
| 386 | "probe called without platform data, no cs_control function will be called\n"); |
Anton Vorontsov | 7390284 | 2009-06-17 16:26:05 -0700 | [diff] [blame] | 387 | mps->cs_control = NULL; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 388 | mps->sysclk = 0; |
| 389 | master->bus_num = bus_num; |
| 390 | master->num_chipselect = 255; |
| 391 | } else { |
Anton Vorontsov | 7390284 | 2009-06-17 16:26:05 -0700 | [diff] [blame] | 392 | mps->cs_control = pdata->cs_control; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 393 | mps->sysclk = pdata->sysclk; |
| 394 | master->bus_num = pdata->bus_num; |
| 395 | master->num_chipselect = pdata->max_chipselect; |
| 396 | } |
| 397 | master->setup = mpc52xx_psc_spi_setup; |
| 398 | master->transfer = mpc52xx_psc_spi_transfer; |
| 399 | master->cleanup = mpc52xx_psc_spi_cleanup; |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 400 | master->dev.of_node = dev->of_node; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 401 | |
| 402 | mps->psc = ioremap(regaddr, size); |
| 403 | if (!mps->psc) { |
| 404 | dev_err(dev, "could not ioremap I/O port range\n"); |
| 405 | ret = -EFAULT; |
| 406 | goto free_master; |
| 407 | } |
Grant Likely | 4874cc1 | 2008-03-04 14:28:42 -0800 | [diff] [blame] | 408 | /* On the 5200, fifo regs are immediately ajacent to the psc regs */ |
| 409 | mps->fifo = ((void __iomem *)mps->psc) + sizeof(struct mpc52xx_psc); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 410 | |
| 411 | ret = request_irq(mps->irq, mpc52xx_psc_spi_isr, 0, "mpc52xx-psc-spi", |
| 412 | mps); |
| 413 | if (ret) |
| 414 | goto free_master; |
| 415 | |
| 416 | ret = mpc52xx_psc_spi_port_config(master->bus_num, mps); |
Wolfram Sang | f856cf0 | 2009-11-02 03:53:11 +0000 | [diff] [blame] | 417 | if (ret < 0) { |
| 418 | dev_err(dev, "can't configure PSC! Is it capable of SPI?\n"); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 419 | goto free_irq; |
Wolfram Sang | f856cf0 | 2009-11-02 03:53:11 +0000 | [diff] [blame] | 420 | } |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 421 | |
| 422 | spin_lock_init(&mps->lock); |
| 423 | init_completion(&mps->done); |
| 424 | INIT_WORK(&mps->work, mpc52xx_psc_spi_work); |
| 425 | INIT_LIST_HEAD(&mps->queue); |
| 426 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 427 | ret = spi_register_master(master); |
| 428 | if (ret < 0) |
Bhaktipriya Shridhar | ac96b73 | 2016-07-02 14:27:06 +0530 | [diff] [blame] | 429 | goto free_irq; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 430 | |
| 431 | return ret; |
| 432 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 433 | free_irq: |
| 434 | free_irq(mps->irq, mps); |
| 435 | free_master: |
| 436 | if (mps->psc) |
| 437 | iounmap(mps->psc); |
| 438 | spi_master_put(master); |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 443 | static int mpc52xx_psc_spi_of_probe(struct platform_device *op) |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 444 | { |
| 445 | const u32 *regaddr_p; |
| 446 | u64 regaddr64, size64; |
| 447 | s16 id = -1; |
| 448 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 449 | regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 450 | if (!regaddr_p) { |
Wolfram Sang | 5cc17d7 | 2009-11-03 23:49:20 +0000 | [diff] [blame] | 451 | dev_err(&op->dev, "Invalid PSC address\n"); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 452 | return -EINVAL; |
| 453 | } |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 454 | regaddr64 = of_translate_address(op->dev.of_node, regaddr_p); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 455 | |
Domen Puncer | 8888735 | 2007-05-23 13:57:32 -0700 | [diff] [blame] | 456 | /* get PSC id (1..6, used by port_config) */ |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 457 | if (op->dev.platform_data == NULL) { |
Domen Puncer | 8888735 | 2007-05-23 13:57:32 -0700 | [diff] [blame] | 458 | const u32 *psc_nump; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 459 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 460 | psc_nump = of_get_property(op->dev.of_node, "cell-index", NULL); |
Domen Puncer | 8888735 | 2007-05-23 13:57:32 -0700 | [diff] [blame] | 461 | if (!psc_nump || *psc_nump > 5) { |
Wolfram Sang | 5cc17d7 | 2009-11-03 23:49:20 +0000 | [diff] [blame] | 462 | dev_err(&op->dev, "Invalid cell-index property\n"); |
Domen Puncer | 8888735 | 2007-05-23 13:57:32 -0700 | [diff] [blame] | 463 | return -EINVAL; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 464 | } |
Domen Puncer | 8888735 | 2007-05-23 13:57:32 -0700 | [diff] [blame] | 465 | id = *psc_nump + 1; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 468 | return mpc52xx_psc_spi_do_probe(&op->dev, (u32)regaddr64, (u32)size64, |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 469 | irq_of_parse_and_map(op->dev.of_node, 0), id); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 472 | static int mpc52xx_psc_spi_of_remove(struct platform_device *op) |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 473 | { |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 474 | struct spi_master *master = spi_master_get(platform_get_drvdata(op)); |
Wolfram Sang | 5aa68b8 | 2010-12-17 15:44:00 +0100 | [diff] [blame] | 475 | struct mpc52xx_psc_spi *mps = spi_master_get_devdata(master); |
| 476 | |
Bhaktipriya Shridhar | ac96b73 | 2016-07-02 14:27:06 +0530 | [diff] [blame] | 477 | flush_work(&mps->work); |
Wolfram Sang | 5aa68b8 | 2010-12-17 15:44:00 +0100 | [diff] [blame] | 478 | spi_unregister_master(master); |
| 479 | free_irq(mps->irq, mps); |
| 480 | if (mps->psc) |
| 481 | iounmap(mps->psc); |
Guenter Roeck | c8c87c6 | 2012-08-18 09:29:23 -0700 | [diff] [blame] | 482 | spi_master_put(master); |
Wolfram Sang | 5aa68b8 | 2010-12-17 15:44:00 +0100 | [diff] [blame] | 483 | |
| 484 | return 0; |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Márton Németh | 631e61b | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 487 | static const struct of_device_id mpc52xx_psc_spi_of_match[] = { |
Grant Likely | 66ffbe4 | 2008-01-24 22:25:31 -0700 | [diff] [blame] | 488 | { .compatible = "fsl,mpc5200-psc-spi", }, |
| 489 | { .compatible = "mpc5200-psc-spi", }, /* old */ |
| 490 | {} |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | MODULE_DEVICE_TABLE(of, mpc52xx_psc_spi_of_match); |
| 494 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 495 | static struct platform_driver mpc52xx_psc_spi_of_driver = { |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 496 | .probe = mpc52xx_psc_spi_of_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 497 | .remove = mpc52xx_psc_spi_of_remove, |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 498 | .driver = { |
| 499 | .name = "mpc52xx-psc-spi", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 500 | .of_match_table = mpc52xx_psc_spi_of_match, |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 501 | }, |
| 502 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 503 | module_platform_driver(mpc52xx_psc_spi_of_driver); |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 504 | |
Dragos Carp | 00b8fd2 | 2007-05-10 22:22:52 -0700 | [diff] [blame] | 505 | MODULE_AUTHOR("Dragos Carp"); |
| 506 | MODULE_DESCRIPTION("MPC52xx PSC SPI Driver"); |
| 507 | MODULE_LICENSE("GPL"); |