linux/drivers/spi/spi-s3c64xx.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2009 Samsung Electronics Ltd.
   3 *      Jaswinder Singh <jassi.brar@samsung.com>
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2 of the License, or
   8 * (at your option) any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18 */
  19
  20#include <linux/init.h>
  21#include <linux/module.h>
  22#include <linux/workqueue.h>
  23#include <linux/interrupt.h>
  24#include <linux/delay.h>
  25#include <linux/clk.h>
  26#include <linux/dma-mapping.h>
  27#include <linux/platform_device.h>
  28#include <linux/pm_runtime.h>
  29#include <linux/spi/spi.h>
  30#include <linux/gpio.h>
  31#include <linux/of.h>
  32#include <linux/of_gpio.h>
  33
  34#include <mach/dma.h>
  35#include <linux/platform_data/spi-s3c64xx.h>
  36
  37#define MAX_SPI_PORTS           3
  38
  39/* Registers and bit-fields */
  40
  41#define S3C64XX_SPI_CH_CFG              0x00
  42#define S3C64XX_SPI_CLK_CFG             0x04
  43#define S3C64XX_SPI_MODE_CFG    0x08
  44#define S3C64XX_SPI_SLAVE_SEL   0x0C
  45#define S3C64XX_SPI_INT_EN              0x10
  46#define S3C64XX_SPI_STATUS              0x14
  47#define S3C64XX_SPI_TX_DATA             0x18
  48#define S3C64XX_SPI_RX_DATA             0x1C
  49#define S3C64XX_SPI_PACKET_CNT  0x20
  50#define S3C64XX_SPI_PENDING_CLR 0x24
  51#define S3C64XX_SPI_SWAP_CFG    0x28
  52#define S3C64XX_SPI_FB_CLK              0x2C
  53
  54#define S3C64XX_SPI_CH_HS_EN            (1<<6)  /* High Speed Enable */
  55#define S3C64XX_SPI_CH_SW_RST           (1<<5)
  56#define S3C64XX_SPI_CH_SLAVE            (1<<4)
  57#define S3C64XX_SPI_CPOL_L              (1<<3)
  58#define S3C64XX_SPI_CPHA_B              (1<<2)
  59#define S3C64XX_SPI_CH_RXCH_ON          (1<<1)
  60#define S3C64XX_SPI_CH_TXCH_ON          (1<<0)
  61
  62#define S3C64XX_SPI_CLKSEL_SRCMSK       (3<<9)
  63#define S3C64XX_SPI_CLKSEL_SRCSHFT      9
  64#define S3C64XX_SPI_ENCLK_ENABLE        (1<<8)
  65#define S3C64XX_SPI_PSR_MASK            0xff
  66
  67#define S3C64XX_SPI_MODE_CH_TSZ_BYTE            (0<<29)
  68#define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD        (1<<29)
  69#define S3C64XX_SPI_MODE_CH_TSZ_WORD            (2<<29)
  70#define S3C64XX_SPI_MODE_CH_TSZ_MASK            (3<<29)
  71#define S3C64XX_SPI_MODE_BUS_TSZ_BYTE           (0<<17)
  72#define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD       (1<<17)
  73#define S3C64XX_SPI_MODE_BUS_TSZ_WORD           (2<<17)
  74#define S3C64XX_SPI_MODE_BUS_TSZ_MASK           (3<<17)
  75#define S3C64XX_SPI_MODE_RXDMA_ON               (1<<2)
  76#define S3C64XX_SPI_MODE_TXDMA_ON               (1<<1)
  77#define S3C64XX_SPI_MODE_4BURST                 (1<<0)
  78
  79#define S3C64XX_SPI_SLAVE_AUTO                  (1<<1)
  80#define S3C64XX_SPI_SLAVE_SIG_INACT             (1<<0)
  81
  82#define S3C64XX_SPI_INT_TRAILING_EN             (1<<6)
  83#define S3C64XX_SPI_INT_RX_OVERRUN_EN           (1<<5)
  84#define S3C64XX_SPI_INT_RX_UNDERRUN_EN          (1<<4)
  85#define S3C64XX_SPI_INT_TX_OVERRUN_EN           (1<<3)
  86#define S3C64XX_SPI_INT_TX_UNDERRUN_EN          (1<<2)
  87#define S3C64XX_SPI_INT_RX_FIFORDY_EN           (1<<1)
  88#define S3C64XX_SPI_INT_TX_FIFORDY_EN           (1<<0)
  89
  90#define S3C64XX_SPI_ST_RX_OVERRUN_ERR           (1<<5)
  91#define S3C64XX_SPI_ST_RX_UNDERRUN_ERR  (1<<4)
  92#define S3C64XX_SPI_ST_TX_OVERRUN_ERR           (1<<3)
  93#define S3C64XX_SPI_ST_TX_UNDERRUN_ERR  (1<<2)
  94#define S3C64XX_SPI_ST_RX_FIFORDY               (1<<1)
  95#define S3C64XX_SPI_ST_TX_FIFORDY               (1<<0)
  96
  97#define S3C64XX_SPI_PACKET_CNT_EN               (1<<16)
  98
  99#define S3C64XX_SPI_PND_TX_UNDERRUN_CLR         (1<<4)
 100#define S3C64XX_SPI_PND_TX_OVERRUN_CLR          (1<<3)
 101#define S3C64XX_SPI_PND_RX_UNDERRUN_CLR         (1<<2)
 102#define S3C64XX_SPI_PND_RX_OVERRUN_CLR          (1<<1)
 103#define S3C64XX_SPI_PND_TRAILING_CLR            (1<<0)
 104
 105#define S3C64XX_SPI_SWAP_RX_HALF_WORD           (1<<7)
 106#define S3C64XX_SPI_SWAP_RX_BYTE                (1<<6)
 107#define S3C64XX_SPI_SWAP_RX_BIT                 (1<<5)
 108#define S3C64XX_SPI_SWAP_RX_EN                  (1<<4)
 109#define S3C64XX_SPI_SWAP_TX_HALF_WORD           (1<<3)
 110#define S3C64XX_SPI_SWAP_TX_BYTE                (1<<2)
 111#define S3C64XX_SPI_SWAP_TX_BIT                 (1<<1)
 112#define S3C64XX_SPI_SWAP_TX_EN                  (1<<0)
 113
 114#define S3C64XX_SPI_FBCLK_MSK           (3<<0)
 115
 116#define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
 117#define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
 118                                (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
 119#define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
 120#define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
 121                                        FIFO_LVL_MASK(i))
 122
 123#define S3C64XX_SPI_MAX_TRAILCNT        0x3ff
 124#define S3C64XX_SPI_TRAILCNT_OFF        19
 125
 126#define S3C64XX_SPI_TRAILCNT            S3C64XX_SPI_MAX_TRAILCNT
 127
 128#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
 129
 130#define RXBUSY    (1<<2)
 131#define TXBUSY    (1<<3)
 132
 133struct s3c64xx_spi_dma_data {
 134        unsigned                ch;
 135        enum dma_transfer_direction direction;
 136        enum dma_ch     dmach;
 137        struct property         *dma_prop;
 138};
 139
 140/**
 141 * struct s3c64xx_spi_info - SPI Controller hardware info
 142 * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
 143 * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
 144 * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
 145 * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
 146 * @clk_from_cmu: True, if the controller does not include a clock mux and
 147 *      prescaler unit.
 148 *
 149 * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
 150 * differ in some aspects such as the size of the fifo and spi bus clock
 151 * setup. Such differences are specified to the driver using this structure
 152 * which is provided as driver data to the driver.
 153 */
 154struct s3c64xx_spi_port_config {
 155        int     fifo_lvl_mask[MAX_SPI_PORTS];
 156        int     rx_lvl_offset;
 157        int     tx_st_done;
 158        bool    high_speed;
 159        bool    clk_from_cmu;
 160};
 161
 162/**
 163 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
 164 * @clk: Pointer to the spi clock.
 165 * @src_clk: Pointer to the clock used to generate SPI signals.
 166 * @master: Pointer to the SPI Protocol master.
 167 * @cntrlr_info: Platform specific data for the controller this driver manages.
 168 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
 169 * @queue: To log SPI xfer requests.
 170 * @lock: Controller specific lock.
 171 * @state: Set of FLAGS to indicate status.
 172 * @rx_dmach: Controller's DMA channel for Rx.
 173 * @tx_dmach: Controller's DMA channel for Tx.
 174 * @sfr_start: BUS address of SPI controller regs.
 175 * @regs: Pointer to ioremap'ed controller registers.
 176 * @irq: interrupt
 177 * @xfer_completion: To indicate completion of xfer task.
 178 * @cur_mode: Stores the active configuration of the controller.
 179 * @cur_bpw: Stores the active bits per word settings.
 180 * @cur_speed: Stores the active xfer clock speed.
 181 */
 182struct s3c64xx_spi_driver_data {
 183        void __iomem                    *regs;
 184        struct clk                      *clk;
 185        struct clk                      *src_clk;
 186        struct platform_device          *pdev;
 187        struct spi_master               *master;
 188        struct s3c64xx_spi_info  *cntrlr_info;
 189        struct spi_device               *tgl_spi;
 190        struct list_head                queue;
 191        spinlock_t                      lock;
 192        unsigned long                   sfr_start;
 193        struct completion               xfer_completion;
 194        unsigned                        state;
 195        unsigned                        cur_mode, cur_bpw;
 196        unsigned                        cur_speed;
 197        struct s3c64xx_spi_dma_data     rx_dma;
 198        struct s3c64xx_spi_dma_data     tx_dma;
 199        struct samsung_dma_ops          *ops;
 200        struct s3c64xx_spi_port_config  *port_conf;
 201        unsigned int                    port_id;
 202        unsigned long                   gpios[4];
 203};
 204
 205static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
 206        .name = "samsung-spi-dma",
 207};
 208
 209static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
 210{
 211        void __iomem *regs = sdd->regs;
 212        unsigned long loops;
 213        u32 val;
 214
 215        writel(0, regs + S3C64XX_SPI_PACKET_CNT);
 216
 217        val = readl(regs + S3C64XX_SPI_CH_CFG);
 218        val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
 219        writel(val, regs + S3C64XX_SPI_CH_CFG);
 220
 221        val = readl(regs + S3C64XX_SPI_CH_CFG);
 222        val |= S3C64XX_SPI_CH_SW_RST;
 223        val &= ~S3C64XX_SPI_CH_HS_EN;
 224        writel(val, regs + S3C64XX_SPI_CH_CFG);
 225
 226        /* Flush TxFIFO*/
 227        loops = msecs_to_loops(1);
 228        do {
 229                val = readl(regs + S3C64XX_SPI_STATUS);
 230        } while (TX_FIFO_LVL(val, sdd) && loops--);
 231
 232        if (loops == 0)
 233                dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
 234
 235        /* Flush RxFIFO*/
 236        loops = msecs_to_loops(1);
 237        do {
 238                val = readl(regs + S3C64XX_SPI_STATUS);
 239                if (RX_FIFO_LVL(val, sdd))
 240                        readl(regs + S3C64XX_SPI_RX_DATA);
 241                else
 242                        break;
 243        } while (loops--);
 244
 245        if (loops == 0)
 246                dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
 247
 248        val = readl(regs + S3C64XX_SPI_CH_CFG);
 249        val &= ~S3C64XX_SPI_CH_SW_RST;
 250        writel(val, regs + S3C64XX_SPI_CH_CFG);
 251
 252        val = readl(regs + S3C64XX_SPI_MODE_CFG);
 253        val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
 254        writel(val, regs + S3C64XX_SPI_MODE_CFG);
 255}
 256
 257static void s3c64xx_spi_dmacb(void *data)
 258{
 259        struct s3c64xx_spi_driver_data *sdd;
 260        struct s3c64xx_spi_dma_data *dma = data;
 261        unsigned long flags;
 262
 263        if (dma->direction == DMA_DEV_TO_MEM)
 264                sdd = container_of(data,
 265                        struct s3c64xx_spi_driver_data, rx_dma);
 266        else
 267                sdd = container_of(data,
 268                        struct s3c64xx_spi_driver_data, tx_dma);
 269
 270        spin_lock_irqsave(&sdd->lock, flags);
 271
 272        if (dma->direction == DMA_DEV_TO_MEM) {
 273                sdd->state &= ~RXBUSY;
 274                if (!(sdd->state & TXBUSY))
 275                        complete(&sdd->xfer_completion);
 276        } else {
 277                sdd->state &= ~TXBUSY;
 278                if (!(sdd->state & RXBUSY))
 279                        complete(&sdd->xfer_completion);
 280        }
 281
 282        spin_unlock_irqrestore(&sdd->lock, flags);
 283}
 284
 285static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
 286                                        unsigned len, dma_addr_t buf)
 287{
 288        struct s3c64xx_spi_driver_data *sdd;
 289        struct samsung_dma_prep info;
 290        struct samsung_dma_config config;
 291
 292        if (dma->direction == DMA_DEV_TO_MEM) {
 293                sdd = container_of((void *)dma,
 294                        struct s3c64xx_spi_driver_data, rx_dma);
 295                config.direction = sdd->rx_dma.direction;
 296                config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
 297                config.width = sdd->cur_bpw / 8;
 298                sdd->ops->config(sdd->rx_dma.ch, &config);
 299        } else {
 300                sdd = container_of((void *)dma,
 301                        struct s3c64xx_spi_driver_data, tx_dma);
 302                config.direction =  sdd->tx_dma.direction;
 303                config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
 304                config.width = sdd->cur_bpw / 8;
 305                sdd->ops->config(sdd->tx_dma.ch, &config);
 306        }
 307
 308        info.cap = DMA_SLAVE;
 309        info.len = len;
 310        info.fp = s3c64xx_spi_dmacb;
 311        info.fp_param = dma;
 312        info.direction = dma->direction;
 313        info.buf = buf;
 314
 315        sdd->ops->prepare(dma->ch, &info);
 316        sdd->ops->trigger(dma->ch);
 317}
 318
 319static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
 320{
 321        struct samsung_dma_req req;
 322
 323        sdd->ops = samsung_dma_get_ops();
 324
 325        req.cap = DMA_SLAVE;
 326        req.client = &s3c64xx_spi_dma_client;
 327
 328        req.dt_dmach_prop = sdd->rx_dma.dma_prop;
 329        sdd->rx_dma.ch = sdd->ops->request(sdd->rx_dma.dmach, &req);
 330        req.dt_dmach_prop = sdd->tx_dma.dma_prop;
 331        sdd->tx_dma.ch = sdd->ops->request(sdd->tx_dma.dmach, &req);
 332
 333        return 1;
 334}
 335
 336static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
 337                                struct spi_device *spi,
 338                                struct spi_transfer *xfer, int dma_mode)
 339{
 340        void __iomem *regs = sdd->regs;
 341        u32 modecfg, chcfg;
 342
 343        modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
 344        modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
 345
 346        chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
 347        chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
 348
 349        if (dma_mode) {
 350                chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
 351        } else {
 352                /* Always shift in data in FIFO, even if xfer is Tx only,
 353                 * this helps setting PCKT_CNT value for generating clocks
 354                 * as exactly needed.
 355                 */
 356                chcfg |= S3C64XX_SPI_CH_RXCH_ON;
 357                writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
 358                                        | S3C64XX_SPI_PACKET_CNT_EN,
 359                                        regs + S3C64XX_SPI_PACKET_CNT);
 360        }
 361
 362        if (xfer->tx_buf != NULL) {
 363                sdd->state |= TXBUSY;
 364                chcfg |= S3C64XX_SPI_CH_TXCH_ON;
 365                if (dma_mode) {
 366                        modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
 367                        prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
 368                } else {
 369                        switch (sdd->cur_bpw) {
 370                        case 32:
 371                                iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
 372                                        xfer->tx_buf, xfer->len / 4);
 373                                break;
 374                        case 16:
 375                                iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
 376                                        xfer->tx_buf, xfer->len / 2);
 377                                break;
 378                        default:
 379                                iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
 380                                        xfer->tx_buf, xfer->len);
 381                                break;
 382                        }
 383                }
 384        }
 385
 386        if (xfer->rx_buf != NULL) {
 387                sdd->state |= RXBUSY;
 388
 389                if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
 390                                        && !(sdd->cur_mode & SPI_CPHA))
 391                        chcfg |= S3C64XX_SPI_CH_HS_EN;
 392
 393                if (dma_mode) {
 394                        modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
 395                        chcfg |= S3C64XX_SPI_CH_RXCH_ON;
 396                        writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
 397                                        | S3C64XX_SPI_PACKET_CNT_EN,
 398                                        regs + S3C64XX_SPI_PACKET_CNT);
 399                        prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
 400                }
 401        }
 402
 403        writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
 404        writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
 405}
 406
 407static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
 408                                                struct spi_device *spi)
 409{
 410        struct s3c64xx_spi_csinfo *cs;
 411
 412        if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
 413                if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
 414                        /* Deselect the last toggled device */
 415                        cs = sdd->tgl_spi->controller_data;
 416                        gpio_set_value(cs->line,
 417                                spi->mode & SPI_CS_HIGH ? 0 : 1);
 418                }
 419                sdd->tgl_spi = NULL;
 420        }
 421
 422        cs = spi->controller_data;
 423        gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
 424}
 425
 426static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
 427                                struct spi_transfer *xfer, int dma_mode)
 428{
 429        void __iomem *regs = sdd->regs;
 430        unsigned long val;
 431        int ms;
 432
 433        /* millisecs to xfer 'len' bytes @ 'cur_speed' */
 434        ms = xfer->len * 8 * 1000 / sdd->cur_speed;
 435        ms += 10; /* some tolerance */
 436
 437        if (dma_mode) {
 438                val = msecs_to_jiffies(ms) + 10;
 439                val = wait_for_completion_timeout(&sdd->xfer_completion, val);
 440        } else {
 441                u32 status;
 442                val = msecs_to_loops(ms);
 443                do {
 444                        status = readl(regs + S3C64XX_SPI_STATUS);
 445                } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
 446        }
 447
 448        if (!val)
 449                return -EIO;
 450
 451        if (dma_mode) {
 452                u32 status;
 453
 454                /*
 455                 * DmaTx returns after simply writing data in the FIFO,
 456                 * w/o waiting for real transmission on the bus to finish.
 457                 * DmaRx returns only after Dma read data from FIFO which
 458                 * needs bus transmission to finish, so we don't worry if
 459                 * Xfer involved Rx(with or without Tx).
 460                 */
 461                if (xfer->rx_buf == NULL) {
 462                        val = msecs_to_loops(10);
 463                        status = readl(regs + S3C64XX_SPI_STATUS);
 464                        while ((TX_FIFO_LVL(status, sdd)
 465                                || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
 466                                        && --val) {
 467                                cpu_relax();
 468                                status = readl(regs + S3C64XX_SPI_STATUS);
 469                        }
 470
 471                        if (!val)
 472                                return -EIO;
 473                }
 474        } else {
 475                /* If it was only Tx */
 476                if (xfer->rx_buf == NULL) {
 477                        sdd->state &= ~TXBUSY;
 478                        return 0;
 479                }
 480
 481                switch (sdd->cur_bpw) {
 482                case 32:
 483                        ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
 484                                xfer->rx_buf, xfer->len / 4);
 485                        break;
 486                case 16:
 487                        ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
 488                                xfer->rx_buf, xfer->len / 2);
 489                        break;
 490                default:
 491                        ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
 492                                xfer->rx_buf, xfer->len);
 493                        break;
 494                }
 495                sdd->state &= ~RXBUSY;
 496        }
 497
 498        return 0;
 499}
 500
 501static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
 502                                                struct spi_device *spi)
 503{
 504        struct s3c64xx_spi_csinfo *cs = spi->controller_data;
 505
 506        if (sdd->tgl_spi == spi)
 507                sdd->tgl_spi = NULL;
 508
 509        gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
 510}
 511
 512static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
 513{
 514        void __iomem *regs = sdd->regs;
 515        u32 val;
 516
 517        /* Disable Clock */
 518        if (sdd->port_conf->clk_from_cmu) {
 519                clk_disable_unprepare(sdd->src_clk);
 520        } else {
 521                val = readl(regs + S3C64XX_SPI_CLK_CFG);
 522                val &= ~S3C64XX_SPI_ENCLK_ENABLE;
 523                writel(val, regs + S3C64XX_SPI_CLK_CFG);
 524        }
 525
 526        /* Set Polarity and Phase */
 527        val = readl(regs + S3C64XX_SPI_CH_CFG);
 528        val &= ~(S3C64XX_SPI_CH_SLAVE |
 529                        S3C64XX_SPI_CPOL_L |
 530                        S3C64XX_SPI_CPHA_B);
 531
 532        if (sdd->cur_mode & SPI_CPOL)
 533                val |= S3C64XX_SPI_CPOL_L;
 534
 535        if (sdd->cur_mode & SPI_CPHA)
 536                val |= S3C64XX_SPI_CPHA_B;
 537
 538        writel(val, regs + S3C64XX_SPI_CH_CFG);
 539
 540        /* Set Channel & DMA Mode */
 541        val = readl(regs + S3C64XX_SPI_MODE_CFG);
 542        val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
 543                        | S3C64XX_SPI_MODE_CH_TSZ_MASK);
 544
 545        switch (sdd->cur_bpw) {
 546        case 32:
 547                val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
 548                val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
 549                break;
 550        case 16:
 551                val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
 552                val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
 553                break;
 554        default:
 555                val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
 556                val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
 557                break;
 558        }
 559
 560        writel(val, regs + S3C64XX_SPI_MODE_CFG);
 561
 562        if (sdd->port_conf->clk_from_cmu) {
 563                /* Configure Clock */
 564                /* There is half-multiplier before the SPI */
 565                clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
 566                /* Enable Clock */
 567                clk_prepare_enable(sdd->src_clk);
 568        } else {
 569                /* Configure Clock */
 570                val = readl(regs + S3C64XX_SPI_CLK_CFG);
 571                val &= ~S3C64XX_SPI_PSR_MASK;
 572                val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
 573                                & S3C64XX_SPI_PSR_MASK);
 574                writel(val, regs + S3C64XX_SPI_CLK_CFG);
 575
 576                /* Enable Clock */
 577                val = readl(regs + S3C64XX_SPI_CLK_CFG);
 578                val |= S3C64XX_SPI_ENCLK_ENABLE;
 579                writel(val, regs + S3C64XX_SPI_CLK_CFG);
 580        }
 581}
 582
 583#define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
 584
 585static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
 586                                                struct spi_message *msg)
 587{
 588        struct device *dev = &sdd->pdev->dev;
 589        struct spi_transfer *xfer;
 590
 591        if (msg->is_dma_mapped)
 592                return 0;
 593
 594        /* First mark all xfer unmapped */
 595        list_for_each_entry(xfer, &msg->transfers, transfer_list) {
 596                xfer->rx_dma = XFER_DMAADDR_INVALID;
 597                xfer->tx_dma = XFER_DMAADDR_INVALID;
 598        }
 599
 600        /* Map until end or first fail */
 601        list_for_each_entry(xfer, &msg->transfers, transfer_list) {
 602
 603                if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
 604                        continue;
 605
 606                if (xfer->tx_buf != NULL) {
 607                        xfer->tx_dma = dma_map_single(dev,
 608                                        (void *)xfer->tx_buf, xfer->len,
 609                                        DMA_TO_DEVICE);
 610                        if (dma_mapping_error(dev, xfer->tx_dma)) {
 611                                dev_err(dev, "dma_map_single Tx failed\n");
 612                                xfer->tx_dma = XFER_DMAADDR_INVALID;
 613                                return -ENOMEM;
 614                        }
 615                }
 616
 617                if (xfer->rx_buf != NULL) {
 618                        xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
 619                                                xfer->len, DMA_FROM_DEVICE);
 620                        if (dma_mapping_error(dev, xfer->rx_dma)) {
 621                                dev_err(dev, "dma_map_single Rx failed\n");
 622                                dma_unmap_single(dev, xfer->tx_dma,
 623                                                xfer->len, DMA_TO_DEVICE);
 624                                xfer->tx_dma = XFER_DMAADDR_INVALID;
 625                                xfer->rx_dma = XFER_DMAADDR_INVALID;
 626                                return -ENOMEM;
 627                        }
 628                }
 629        }
 630
 631        return 0;
 632}
 633
 634static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
 635                                                struct spi_message *msg)
 636{
 637        struct device *dev = &sdd->pdev->dev;
 638        struct spi_transfer *xfer;
 639
 640        if (msg->is_dma_mapped)
 641                return;
 642
 643        list_for_each_entry(xfer, &msg->transfers, transfer_list) {
 644
 645                if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
 646                        continue;
 647
 648                if (xfer->rx_buf != NULL
 649                                && xfer->rx_dma != XFER_DMAADDR_INVALID)
 650                        dma_unmap_single(dev, xfer->rx_dma,
 651                                                xfer->len, DMA_FROM_DEVICE);
 652
 653                if (xfer->tx_buf != NULL
 654                                && xfer->tx_dma != XFER_DMAADDR_INVALID)
 655                        dma_unmap_single(dev, xfer->tx_dma,
 656                                                xfer->len, DMA_TO_DEVICE);
 657        }
 658}
 659
 660static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
 661                                            struct spi_message *msg)
 662{
 663        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
 664        struct spi_device *spi = msg->spi;
 665        struct s3c64xx_spi_csinfo *cs = spi->controller_data;
 666        struct spi_transfer *xfer;
 667        int status = 0, cs_toggle = 0;
 668        u32 speed;
 669        u8 bpw;
 670
 671        /* If Master's(controller) state differs from that needed by Slave */
 672        if (sdd->cur_speed != spi->max_speed_hz
 673                        || sdd->cur_mode != spi->mode
 674                        || sdd->cur_bpw != spi->bits_per_word) {
 675                sdd->cur_bpw = spi->bits_per_word;
 676                sdd->cur_speed = spi->max_speed_hz;
 677                sdd->cur_mode = spi->mode;
 678                s3c64xx_spi_config(sdd);
 679        }
 680
 681        /* Map all the transfers if needed */
 682        if (s3c64xx_spi_map_mssg(sdd, msg)) {
 683                dev_err(&spi->dev,
 684                        "Xfer: Unable to map message buffers!\n");
 685                status = -ENOMEM;
 686                goto out;
 687        }
 688
 689        /* Configure feedback delay */
 690        writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
 691
 692        list_for_each_entry(xfer, &msg->transfers, transfer_list) {
 693
 694                unsigned long flags;
 695                int use_dma;
 696
 697                INIT_COMPLETION(sdd->xfer_completion);
 698
 699                /* Only BPW and Speed may change across transfers */
 700                bpw = xfer->bits_per_word ? : spi->bits_per_word;
 701                speed = xfer->speed_hz ? : spi->max_speed_hz;
 702
 703                if (xfer->len % (bpw / 8)) {
 704                        dev_err(&spi->dev,
 705                                "Xfer length(%u) not a multiple of word size(%u)\n",
 706                                xfer->len, bpw / 8);
 707                        status = -EIO;
 708                        goto out;
 709                }
 710
 711                if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
 712                        sdd->cur_bpw = bpw;
 713                        sdd->cur_speed = speed;
 714                        s3c64xx_spi_config(sdd);
 715                }
 716
 717                /* Polling method for xfers not bigger than FIFO capacity */
 718                if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
 719                        use_dma = 0;
 720                else
 721                        use_dma = 1;
 722
 723                spin_lock_irqsave(&sdd->lock, flags);
 724
 725                /* Pending only which is to be done */
 726                sdd->state &= ~RXBUSY;
 727                sdd->state &= ~TXBUSY;
 728
 729                enable_datapath(sdd, spi, xfer, use_dma);
 730
 731                /* Slave Select */
 732                enable_cs(sdd, spi);
 733
 734                /* Start the signals */
 735                writel(0, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
 736
 737                spin_unlock_irqrestore(&sdd->lock, flags);
 738
 739                status = wait_for_xfer(sdd, xfer, use_dma);
 740
 741                /* Quiese the signals */
 742                writel(S3C64XX_SPI_SLAVE_SIG_INACT,
 743                       sdd->regs + S3C64XX_SPI_SLAVE_SEL);
 744
 745                if (status) {
 746                        dev_err(&spi->dev, "I/O Error: "
 747                                "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
 748                                xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
 749                                (sdd->state & RXBUSY) ? 'f' : 'p',
 750                                (sdd->state & TXBUSY) ? 'f' : 'p',
 751                                xfer->len);
 752
 753                        if (use_dma) {
 754                                if (xfer->tx_buf != NULL
 755                                                && (sdd->state & TXBUSY))
 756                                        sdd->ops->stop(sdd->tx_dma.ch);
 757                                if (xfer->rx_buf != NULL
 758                                                && (sdd->state & RXBUSY))
 759                                        sdd->ops->stop(sdd->rx_dma.ch);
 760                        }
 761
 762                        goto out;
 763                }
 764
 765                if (xfer->delay_usecs)
 766                        udelay(xfer->delay_usecs);
 767
 768                if (xfer->cs_change) {
 769                        /* Hint that the next mssg is gonna be
 770                           for the same device */
 771                        if (list_is_last(&xfer->transfer_list,
 772                                                &msg->transfers))
 773                                cs_toggle = 1;
 774                }
 775
 776                msg->actual_length += xfer->len;
 777
 778                flush_fifo(sdd);
 779        }
 780
 781out:
 782        if (!cs_toggle || status)
 783                disable_cs(sdd, spi);
 784        else
 785                sdd->tgl_spi = spi;
 786
 787        s3c64xx_spi_unmap_mssg(sdd, msg);
 788
 789        msg->status = status;
 790
 791        spi_finalize_current_message(master);
 792
 793        return 0;
 794}
 795
 796static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
 797{
 798        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
 799
 800        /* Acquire DMA channels */
 801        while (!acquire_dma(sdd))
 802                msleep(10);
 803
 804        pm_runtime_get_sync(&sdd->pdev->dev);
 805
 806        return 0;
 807}
 808
 809static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
 810{
 811        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
 812
 813        /* Free DMA channels */
 814        sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
 815        sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
 816
 817        pm_runtime_put(&sdd->pdev->dev);
 818
 819        return 0;
 820}
 821
 822static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
 823                                struct s3c64xx_spi_driver_data *sdd,
 824                                struct spi_device *spi)
 825{
 826        struct s3c64xx_spi_csinfo *cs;
 827        struct device_node *slave_np, *data_np = NULL;
 828        u32 fb_delay = 0;
 829
 830        slave_np = spi->dev.of_node;
 831        if (!slave_np) {
 832                dev_err(&spi->dev, "device node not found\n");
 833                return ERR_PTR(-EINVAL);
 834        }
 835
 836        data_np = of_get_child_by_name(slave_np, "controller-data");
 837        if (!data_np) {
 838                dev_err(&spi->dev, "child node 'controller-data' not found\n");
 839                return ERR_PTR(-EINVAL);
 840        }
 841
 842        cs = kzalloc(sizeof(*cs), GFP_KERNEL);
 843        if (!cs) {
 844                dev_err(&spi->dev, "could not allocate memory for controller"
 845                                        " data\n");
 846                of_node_put(data_np);
 847                return ERR_PTR(-ENOMEM);
 848        }
 849
 850        cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
 851        if (!gpio_is_valid(cs->line)) {
 852                dev_err(&spi->dev, "chip select gpio is not specified or "
 853                                        "invalid\n");
 854                kfree(cs);
 855                of_node_put(data_np);
 856                return ERR_PTR(-EINVAL);
 857        }
 858
 859        of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
 860        cs->fb_delay = fb_delay;
 861        of_node_put(data_np);
 862        return cs;
 863}
 864
 865/*
 866 * Here we only check the validity of requested configuration
 867 * and save the configuration in a local data-structure.
 868 * The controller is actually configured only just before we
 869 * get a message to transfer.
 870 */
 871static int s3c64xx_spi_setup(struct spi_device *spi)
 872{
 873        struct s3c64xx_spi_csinfo *cs = spi->controller_data;
 874        struct s3c64xx_spi_driver_data *sdd;
 875        struct s3c64xx_spi_info *sci;
 876        struct spi_message *msg;
 877        unsigned long flags;
 878        int err;
 879
 880        sdd = spi_master_get_devdata(spi->master);
 881        if (!cs && spi->dev.of_node) {
 882                cs = s3c64xx_get_slave_ctrldata(sdd, spi);
 883                spi->controller_data = cs;
 884        }
 885
 886        if (IS_ERR_OR_NULL(cs)) {
 887                dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
 888                return -ENODEV;
 889        }
 890
 891        if (!spi_get_ctldata(spi)) {
 892                err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
 893                                       dev_name(&spi->dev));
 894                if (err) {
 895                        dev_err(&spi->dev,
 896                                "Failed to get /CS gpio [%d]: %d\n",
 897                                cs->line, err);
 898                        goto err_gpio_req;
 899                }
 900                spi_set_ctldata(spi, cs);
 901        }
 902
 903        sci = sdd->cntrlr_info;
 904
 905        spin_lock_irqsave(&sdd->lock, flags);
 906
 907        list_for_each_entry(msg, &sdd->queue, queue) {
 908                /* Is some mssg is already queued for this device */
 909                if (msg->spi == spi) {
 910                        dev_err(&spi->dev,
 911                                "setup: attempt while mssg in queue!\n");
 912                        spin_unlock_irqrestore(&sdd->lock, flags);
 913                        err = -EBUSY;
 914                        goto err_msgq;
 915                }
 916        }
 917
 918        spin_unlock_irqrestore(&sdd->lock, flags);
 919
 920        if (spi->bits_per_word != 8
 921                        && spi->bits_per_word != 16
 922                        && spi->bits_per_word != 32) {
 923                dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n",
 924                                                        spi->bits_per_word);
 925                err = -EINVAL;
 926                goto setup_exit;
 927        }
 928
 929        pm_runtime_get_sync(&sdd->pdev->dev);
 930
 931        /* Check if we can provide the requested rate */
 932        if (!sdd->port_conf->clk_from_cmu) {
 933                u32 psr, speed;
 934
 935                /* Max possible */
 936                speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
 937
 938                if (spi->max_speed_hz > speed)
 939                        spi->max_speed_hz = speed;
 940
 941                psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
 942                psr &= S3C64XX_SPI_PSR_MASK;
 943                if (psr == S3C64XX_SPI_PSR_MASK)
 944                        psr--;
 945
 946                speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
 947                if (spi->max_speed_hz < speed) {
 948                        if (psr+1 < S3C64XX_SPI_PSR_MASK) {
 949                                psr++;
 950                        } else {
 951                                err = -EINVAL;
 952                                goto setup_exit;
 953                        }
 954                }
 955
 956                speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
 957                if (spi->max_speed_hz >= speed) {
 958                        spi->max_speed_hz = speed;
 959                } else {
 960                        err = -EINVAL;
 961                        goto setup_exit;
 962                }
 963        }
 964
 965        pm_runtime_put(&sdd->pdev->dev);
 966        disable_cs(sdd, spi);
 967        return 0;
 968
 969setup_exit:
 970        /* setup() returns with device de-selected */
 971        disable_cs(sdd, spi);
 972
 973err_msgq:
 974        gpio_free(cs->line);
 975        spi_set_ctldata(spi, NULL);
 976
 977err_gpio_req:
 978        if (spi->dev.of_node)
 979                kfree(cs);
 980
 981        return err;
 982}
 983
 984static void s3c64xx_spi_cleanup(struct spi_device *spi)
 985{
 986        struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
 987
 988        if (cs) {
 989                gpio_free(cs->line);
 990                if (spi->dev.of_node)
 991                        kfree(cs);
 992        }
 993        spi_set_ctldata(spi, NULL);
 994}
 995
 996static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
 997{
 998        struct s3c64xx_spi_driver_data *sdd = data;
 999        struct spi_master *spi = sdd->master;
1000        unsigned int val, clr = 0;
1001
1002        val = readl(sdd->regs + S3C64XX_SPI_STATUS);
1003
1004        if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
1005                clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
1006                dev_err(&spi->dev, "RX overrun\n");
1007        }
1008        if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
1009                clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
1010                dev_err(&spi->dev, "RX underrun\n");
1011        }
1012        if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
1013                clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
1014                dev_err(&spi->dev, "TX overrun\n");
1015        }
1016        if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
1017                clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1018                dev_err(&spi->dev, "TX underrun\n");
1019        }
1020
1021        /* Clear the pending irq by setting and then clearing it */
1022        writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1023        writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1024
1025        return IRQ_HANDLED;
1026}
1027
1028static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
1029{
1030        struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1031        void __iomem *regs = sdd->regs;
1032        unsigned int val;
1033
1034        sdd->cur_speed = 0;
1035
1036        writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
1037
1038        /* Disable Interrupts - we use Polling if not DMA mode */
1039        writel(0, regs + S3C64XX_SPI_INT_EN);
1040
1041        if (!sdd->port_conf->clk_from_cmu)
1042                writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
1043                                regs + S3C64XX_SPI_CLK_CFG);
1044        writel(0, regs + S3C64XX_SPI_MODE_CFG);
1045        writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1046
1047        /* Clear any irq pending bits, should set and clear the bits */
1048        val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1049                S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1050                S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1051                S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1052        writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1053        writel(0, regs + S3C64XX_SPI_PENDING_CLR);
1054
1055        writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1056
1057        val = readl(regs + S3C64XX_SPI_MODE_CFG);
1058        val &= ~S3C64XX_SPI_MODE_4BURST;
1059        val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1060        val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1061        writel(val, regs + S3C64XX_SPI_MODE_CFG);
1062
1063        flush_fifo(sdd);
1064}
1065
1066static int s3c64xx_spi_get_dmares(
1067                        struct s3c64xx_spi_driver_data *sdd, bool tx)
1068{
1069        struct platform_device *pdev = sdd->pdev;
1070        struct s3c64xx_spi_dma_data *dma_data;
1071        struct property *prop;
1072        struct resource *res;
1073        char prop_name[15], *chan_str;
1074
1075        if (tx) {
1076                dma_data = &sdd->tx_dma;
1077                dma_data->direction = DMA_MEM_TO_DEV;
1078                chan_str = "tx";
1079        } else {
1080                dma_data = &sdd->rx_dma;
1081                dma_data->direction = DMA_DEV_TO_MEM;
1082                chan_str = "rx";
1083        }
1084
1085        if (!sdd->pdev->dev.of_node) {
1086                res = platform_get_resource(pdev, IORESOURCE_DMA, tx ? 0 : 1);
1087                if (!res) {
1088                        dev_err(&pdev->dev, "Unable to get SPI-%s dma "
1089                                        "resource\n", chan_str);
1090                        return -ENXIO;
1091                }
1092                dma_data->dmach = res->start;
1093                return 0;
1094        }
1095
1096        sprintf(prop_name, "%s-dma-channel", chan_str);
1097        prop = of_find_property(pdev->dev.of_node, prop_name, NULL);
1098        if (!prop) {
1099                dev_err(&pdev->dev, "%s dma channel property not specified\n",
1100                                        chan_str);
1101                return -ENXIO;
1102        }
1103
1104        dma_data->dmach = DMACH_DT_PROP;
1105        dma_data->dma_prop = prop;
1106        return 0;
1107}
1108
1109#ifdef CONFIG_OF
1110static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
1111{
1112        struct device *dev = &sdd->pdev->dev;
1113        int idx, gpio, ret;
1114
1115        /* find gpios for mosi, miso and clock lines */
1116        for (idx = 0; idx < 3; idx++) {
1117                gpio = of_get_gpio(dev->of_node, idx);
1118                if (!gpio_is_valid(gpio)) {
1119                        dev_err(dev, "invalid gpio[%d]: %d\n", idx, gpio);
1120                        goto free_gpio;
1121                }
1122                sdd->gpios[idx] = gpio;
1123                ret = gpio_request(gpio, "spi-bus");
1124                if (ret) {
1125                        dev_err(dev, "gpio [%d] request failed: %d\n",
1126                                gpio, ret);
1127                        goto free_gpio;
1128                }
1129        }
1130        return 0;
1131
1132free_gpio:
1133        while (--idx >= 0)
1134                gpio_free(sdd->gpios[idx]);
1135        return -EINVAL;
1136}
1137
1138static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
1139{
1140        unsigned int idx;
1141        for (idx = 0; idx < 3; idx++)
1142                gpio_free(sdd->gpios[idx]);
1143}
1144
1145static struct s3c64xx_spi_info * s3c64xx_spi_parse_dt(
1146                                                struct device *dev)
1147{
1148        struct s3c64xx_spi_info *sci;
1149        u32 temp;
1150
1151        sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1152        if (!sci) {
1153                dev_err(dev, "memory allocation for spi_info failed\n");
1154                return ERR_PTR(-ENOMEM);
1155        }
1156
1157        if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1158                dev_warn(dev, "spi bus clock parent not specified, using "
1159                                "clock at index 0 as parent\n");
1160                sci->src_clk_nr = 0;
1161        } else {
1162                sci->src_clk_nr = temp;
1163        }
1164
1165        if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1166                dev_warn(dev, "number of chip select lines not specified, "
1167                                "assuming 1 chip select line\n");
1168                sci->num_cs = 1;
1169        } else {
1170                sci->num_cs = temp;
1171        }
1172
1173        return sci;
1174}
1175#else
1176static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1177{
1178        return dev->platform_data;
1179}
1180
1181static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
1182{
1183        return -EINVAL;
1184}
1185
1186static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
1187{
1188}
1189#endif
1190
1191static const struct of_device_id s3c64xx_spi_dt_match[];
1192
1193static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1194                                                struct platform_device *pdev)
1195{
1196#ifdef CONFIG_OF
1197        if (pdev->dev.of_node) {
1198                const struct of_device_id *match;
1199                match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
1200                return (struct s3c64xx_spi_port_config *)match->data;
1201        }
1202#endif
1203        return (struct s3c64xx_spi_port_config *)
1204                         platform_get_device_id(pdev)->driver_data;
1205}
1206
1207static int __init s3c64xx_spi_probe(struct platform_device *pdev)
1208{
1209        struct resource *mem_res;
1210        struct s3c64xx_spi_driver_data *sdd;
1211        struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
1212        struct spi_master *master;
1213        int ret, irq;
1214        char clk_name[16];
1215
1216        if (!sci && pdev->dev.of_node) {
1217                sci = s3c64xx_spi_parse_dt(&pdev->dev);
1218                if (IS_ERR(sci))
1219                        return PTR_ERR(sci);
1220        }
1221
1222        if (!sci) {
1223                dev_err(&pdev->dev, "platform_data missing!\n");
1224                return -ENODEV;
1225        }
1226
1227        mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1228        if (mem_res == NULL) {
1229                dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
1230                return -ENXIO;
1231        }
1232
1233        irq = platform_get_irq(pdev, 0);
1234        if (irq < 0) {
1235                dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
1236                return irq;
1237        }
1238
1239        master = spi_alloc_master(&pdev->dev,
1240                                sizeof(struct s3c64xx_spi_driver_data));
1241        if (master == NULL) {
1242                dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1243                return -ENOMEM;
1244        }
1245
1246        platform_set_drvdata(pdev, master);
1247
1248        sdd = spi_master_get_devdata(master);
1249        sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
1250        sdd->master = master;
1251        sdd->cntrlr_info = sci;
1252        sdd->pdev = pdev;
1253        sdd->sfr_start = mem_res->start;
1254        if (pdev->dev.of_node) {
1255                ret = of_alias_get_id(pdev->dev.of_node, "spi");
1256                if (ret < 0) {
1257                        dev_err(&pdev->dev, "failed to get alias id, "
1258                                                "errno %d\n", ret);
1259                        goto err0;
1260                }
1261                sdd->port_id = ret;
1262        } else {
1263                sdd->port_id = pdev->id;
1264        }
1265
1266        sdd->cur_bpw = 8;
1267
1268        ret = s3c64xx_spi_get_dmares(sdd, true);
1269        if (ret)
1270                goto err0;
1271
1272        ret = s3c64xx_spi_get_dmares(sdd, false);
1273        if (ret)
1274                goto err0;
1275
1276        master->dev.of_node = pdev->dev.of_node;
1277        master->bus_num = sdd->port_id;
1278        master->setup = s3c64xx_spi_setup;
1279        master->cleanup = s3c64xx_spi_cleanup;
1280        master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1281        master->transfer_one_message = s3c64xx_spi_transfer_one_message;
1282        master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
1283        master->num_chipselect = sci->num_cs;
1284        master->dma_alignment = 8;
1285        /* the spi->mode bits understood by this driver: */
1286        master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1287
1288        sdd->regs = devm_request_and_ioremap(&pdev->dev, mem_res);
1289        if (sdd->regs == NULL) {
1290                dev_err(&pdev->dev, "Unable to remap IO\n");
1291                ret = -ENXIO;
1292                goto err1;
1293        }
1294
1295        if (!sci->cfg_gpio && pdev->dev.of_node) {
1296                if (s3c64xx_spi_parse_dt_gpio(sdd))
1297                        return -EBUSY;
1298        } else if (sci->cfg_gpio == NULL || sci->cfg_gpio()) {
1299                dev_err(&pdev->dev, "Unable to config gpio\n");
1300                ret = -EBUSY;
1301                goto err2;
1302        }
1303
1304        /* Setup clocks */
1305        sdd->clk = clk_get(&pdev->dev, "spi");
1306        if (IS_ERR(sdd->clk)) {
1307                dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
1308                ret = PTR_ERR(sdd->clk);
1309                goto err3;
1310        }
1311
1312        if (clk_prepare_enable(sdd->clk)) {
1313                dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1314                ret = -EBUSY;
1315                goto err4;
1316        }
1317
1318        sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1319        sdd->src_clk = clk_get(&pdev->dev, clk_name);
1320        if (IS_ERR(sdd->src_clk)) {
1321                dev_err(&pdev->dev,
1322                        "Unable to acquire clock '%s'\n", clk_name);
1323                ret = PTR_ERR(sdd->src_clk);
1324                goto err5;
1325        }
1326
1327        if (clk_prepare_enable(sdd->src_clk)) {
1328                dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
1329                ret = -EBUSY;
1330                goto err6;
1331        }
1332
1333        /* Setup Deufult Mode */
1334        s3c64xx_spi_hwinit(sdd, sdd->port_id);
1335
1336        spin_lock_init(&sdd->lock);
1337        init_completion(&sdd->xfer_completion);
1338        INIT_LIST_HEAD(&sdd->queue);
1339
1340        ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
1341        if (ret != 0) {
1342                dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1343                        irq, ret);
1344                goto err7;
1345        }
1346
1347        writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1348               S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1349               sdd->regs + S3C64XX_SPI_INT_EN);
1350
1351        if (spi_register_master(master)) {
1352                dev_err(&pdev->dev, "cannot register SPI master\n");
1353                ret = -EBUSY;
1354                goto err8;
1355        }
1356
1357        dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
1358                                        "with %d Slaves attached\n",
1359                                        sdd->port_id, master->num_chipselect);
1360        dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
1361                                        mem_res->end, mem_res->start,
1362                                        sdd->rx_dma.dmach, sdd->tx_dma.dmach);
1363
1364        pm_runtime_enable(&pdev->dev);
1365
1366        return 0;
1367
1368err8:
1369        free_irq(irq, sdd);
1370err7:
1371        clk_disable_unprepare(sdd->src_clk);
1372err6:
1373        clk_put(sdd->src_clk);
1374err5:
1375        clk_disable_unprepare(sdd->clk);
1376err4:
1377        clk_put(sdd->clk);
1378err3:
1379        if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
1380                s3c64xx_spi_dt_gpio_free(sdd);
1381err2:
1382err1:
1383err0:
1384        platform_set_drvdata(pdev, NULL);
1385        spi_master_put(master);
1386
1387        return ret;
1388}
1389
1390static int s3c64xx_spi_remove(struct platform_device *pdev)
1391{
1392        struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1393        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1394
1395        pm_runtime_disable(&pdev->dev);
1396
1397        spi_unregister_master(master);
1398
1399        writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1400
1401        free_irq(platform_get_irq(pdev, 0), sdd);
1402
1403        clk_disable_unprepare(sdd->src_clk);
1404        clk_put(sdd->src_clk);
1405
1406        clk_disable_unprepare(sdd->clk);
1407        clk_put(sdd->clk);
1408
1409        if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
1410                s3c64xx_spi_dt_gpio_free(sdd);
1411
1412        platform_set_drvdata(pdev, NULL);
1413        spi_master_put(master);
1414
1415        return 0;
1416}
1417
1418#ifdef CONFIG_PM
1419static int s3c64xx_spi_suspend(struct device *dev)
1420{
1421        struct spi_master *master = dev_get_drvdata(dev);
1422        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1423
1424        spi_master_suspend(master);
1425
1426        /* Disable the clock */
1427        clk_disable_unprepare(sdd->src_clk);
1428        clk_disable_unprepare(sdd->clk);
1429
1430        if (!sdd->cntrlr_info->cfg_gpio && dev->of_node)
1431                s3c64xx_spi_dt_gpio_free(sdd);
1432
1433        sdd->cur_speed = 0; /* Output Clock is stopped */
1434
1435        return 0;
1436}
1437
1438static int s3c64xx_spi_resume(struct device *dev)
1439{
1440        struct spi_master *master = dev_get_drvdata(dev);
1441        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1442        struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1443
1444        if (!sci->cfg_gpio && dev->of_node)
1445                s3c64xx_spi_parse_dt_gpio(sdd);
1446        else
1447                sci->cfg_gpio();
1448
1449        /* Enable the clock */
1450        clk_prepare_enable(sdd->src_clk);
1451        clk_prepare_enable(sdd->clk);
1452
1453        s3c64xx_spi_hwinit(sdd, sdd->port_id);
1454
1455        spi_master_resume(master);
1456
1457        return 0;
1458}
1459#endif /* CONFIG_PM */
1460
1461#ifdef CONFIG_PM_RUNTIME
1462static int s3c64xx_spi_runtime_suspend(struct device *dev)
1463{
1464        struct spi_master *master = dev_get_drvdata(dev);
1465        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1466
1467        clk_disable_unprepare(sdd->clk);
1468        clk_disable_unprepare(sdd->src_clk);
1469
1470        return 0;
1471}
1472
1473static int s3c64xx_spi_runtime_resume(struct device *dev)
1474{
1475        struct spi_master *master = dev_get_drvdata(dev);
1476        struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1477
1478        clk_prepare_enable(sdd->src_clk);
1479        clk_prepare_enable(sdd->clk);
1480
1481        return 0;
1482}
1483#endif /* CONFIG_PM_RUNTIME */
1484
1485static const struct dev_pm_ops s3c64xx_spi_pm = {
1486        SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
1487        SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1488                           s3c64xx_spi_runtime_resume, NULL)
1489};
1490
1491static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1492        .fifo_lvl_mask  = { 0x7f },
1493        .rx_lvl_offset  = 13,
1494        .tx_st_done     = 21,
1495        .high_speed     = true,
1496};
1497
1498static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1499        .fifo_lvl_mask  = { 0x7f, 0x7F },
1500        .rx_lvl_offset  = 13,
1501        .tx_st_done     = 21,
1502};
1503
1504static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
1505        .fifo_lvl_mask  = { 0x1ff, 0x7F },
1506        .rx_lvl_offset  = 15,
1507        .tx_st_done     = 25,
1508};
1509
1510static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
1511        .fifo_lvl_mask  = { 0x7f, 0x7F },
1512        .rx_lvl_offset  = 13,
1513        .tx_st_done     = 21,
1514        .high_speed     = true,
1515};
1516
1517static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1518        .fifo_lvl_mask  = { 0x1ff, 0x7F },
1519        .rx_lvl_offset  = 15,
1520        .tx_st_done     = 25,
1521        .high_speed     = true,
1522};
1523
1524static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1525        .fifo_lvl_mask  = { 0x1ff, 0x7F, 0x7F },
1526        .rx_lvl_offset  = 15,
1527        .tx_st_done     = 25,
1528        .high_speed     = true,
1529        .clk_from_cmu   = true,
1530};
1531
1532static struct platform_device_id s3c64xx_spi_driver_ids[] = {
1533        {
1534                .name           = "s3c2443-spi",
1535                .driver_data    = (kernel_ulong_t)&s3c2443_spi_port_config,
1536        }, {
1537                .name           = "s3c6410-spi",
1538                .driver_data    = (kernel_ulong_t)&s3c6410_spi_port_config,
1539        }, {
1540                .name           = "s5p64x0-spi",
1541                .driver_data    = (kernel_ulong_t)&s5p64x0_spi_port_config,
1542        }, {
1543                .name           = "s5pc100-spi",
1544                .driver_data    = (kernel_ulong_t)&s5pc100_spi_port_config,
1545        }, {
1546                .name           = "s5pv210-spi",
1547                .driver_data    = (kernel_ulong_t)&s5pv210_spi_port_config,
1548        }, {
1549                .name           = "exynos4210-spi",
1550                .driver_data    = (kernel_ulong_t)&exynos4_spi_port_config,
1551        },
1552        { },
1553};
1554
1555#ifdef CONFIG_OF
1556static const struct of_device_id s3c64xx_spi_dt_match[] = {
1557        { .compatible = "samsung,exynos4210-spi",
1558                        .data = (void *)&exynos4_spi_port_config,
1559        },
1560        { },
1561};
1562MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
1563#endif /* CONFIG_OF */
1564
1565static struct platform_driver s3c64xx_spi_driver = {
1566        .driver = {
1567                .name   = "s3c64xx-spi",
1568                .owner = THIS_MODULE,
1569                .pm = &s3c64xx_spi_pm,
1570                .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
1571        },
1572        .remove = s3c64xx_spi_remove,
1573        .id_table = s3c64xx_spi_driver_ids,
1574};
1575MODULE_ALIAS("platform:s3c64xx-spi");
1576
1577static int __init s3c64xx_spi_init(void)
1578{
1579        return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
1580}
1581subsys_initcall(s3c64xx_spi_init);
1582
1583static void __exit s3c64xx_spi_exit(void)
1584{
1585        platform_driver_unregister(&s3c64xx_spi_driver);
1586}
1587module_exit(s3c64xx_spi_exit);
1588
1589MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1590MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1591MODULE_LICENSE("GPL");
1592
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.