Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 1 | /* |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 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> |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 24 | #include <linux/delay.h> |
| 25 | #include <linux/clk.h> |
| 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/platform_device.h> |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 29 | #include <linux/spi/spi.h> |
| 30 | |
| 31 | #include <mach/dma.h> |
Jassi Brar | e6b873c | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 32 | #include <plat/s3c64xx-spi.h> |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 33 | |
| 34 | /* Registers and bit-fields */ |
| 35 | |
| 36 | #define S3C64XX_SPI_CH_CFG 0x00 |
| 37 | #define S3C64XX_SPI_CLK_CFG 0x04 |
| 38 | #define S3C64XX_SPI_MODE_CFG 0x08 |
| 39 | #define S3C64XX_SPI_SLAVE_SEL 0x0C |
| 40 | #define S3C64XX_SPI_INT_EN 0x10 |
| 41 | #define S3C64XX_SPI_STATUS 0x14 |
| 42 | #define S3C64XX_SPI_TX_DATA 0x18 |
| 43 | #define S3C64XX_SPI_RX_DATA 0x1C |
| 44 | #define S3C64XX_SPI_PACKET_CNT 0x20 |
| 45 | #define S3C64XX_SPI_PENDING_CLR 0x24 |
| 46 | #define S3C64XX_SPI_SWAP_CFG 0x28 |
| 47 | #define S3C64XX_SPI_FB_CLK 0x2C |
| 48 | |
| 49 | #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */ |
| 50 | #define S3C64XX_SPI_CH_SW_RST (1<<5) |
| 51 | #define S3C64XX_SPI_CH_SLAVE (1<<4) |
| 52 | #define S3C64XX_SPI_CPOL_L (1<<3) |
| 53 | #define S3C64XX_SPI_CPHA_B (1<<2) |
| 54 | #define S3C64XX_SPI_CH_RXCH_ON (1<<1) |
| 55 | #define S3C64XX_SPI_CH_TXCH_ON (1<<0) |
| 56 | |
| 57 | #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9) |
| 58 | #define S3C64XX_SPI_CLKSEL_SRCSHFT 9 |
| 59 | #define S3C64XX_SPI_ENCLK_ENABLE (1<<8) |
| 60 | #define S3C64XX_SPI_PSR_MASK 0xff |
| 61 | |
| 62 | #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29) |
| 63 | #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29) |
| 64 | #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29) |
| 65 | #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29) |
| 66 | #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17) |
| 67 | #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17) |
| 68 | #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17) |
| 69 | #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17) |
| 70 | #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2) |
| 71 | #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1) |
| 72 | #define S3C64XX_SPI_MODE_4BURST (1<<0) |
| 73 | |
| 74 | #define S3C64XX_SPI_SLAVE_AUTO (1<<1) |
| 75 | #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0) |
| 76 | |
| 77 | #define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL) |
| 78 | |
| 79 | #define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \ |
| 80 | (c)->regs + S3C64XX_SPI_SLAVE_SEL) |
| 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 S3C64XX_SPI_ST_TRLCNTZ(v, i) ((((v) >> (i)->rx_lvl_offset) & \ |
| 117 | (((i)->fifo_lvl_mask + 1))) \ |
| 118 | ? 1 : 0) |
| 119 | |
Padmavathi Venna | 3075741 | 2011-07-05 17:14:02 +0900 | [diff] [blame] | 120 | #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & (1 << (i)->tx_st_done)) ? 1 : 0) |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 121 | #define TX_FIFO_LVL(v, i) (((v) >> 6) & (i)->fifo_lvl_mask) |
| 122 | #define RX_FIFO_LVL(v, i) (((v) >> (i)->rx_lvl_offset) & (i)->fifo_lvl_mask) |
| 123 | |
| 124 | #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff |
| 125 | #define S3C64XX_SPI_TRAILCNT_OFF 19 |
| 126 | |
| 127 | #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT |
| 128 | |
| 129 | #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) |
| 130 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 131 | #define RXBUSY (1<<2) |
| 132 | #define TXBUSY (1<<3) |
| 133 | |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 134 | struct s3c64xx_spi_dma_data { |
| 135 | unsigned ch; |
| 136 | enum dma_data_direction direction; |
| 137 | enum dma_ch dmach; |
| 138 | }; |
| 139 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 140 | /** |
| 141 | * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver. |
| 142 | * @clk: Pointer to the spi clock. |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 143 | * @src_clk: Pointer to the clock used to generate SPI signals. |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 144 | * @master: Pointer to the SPI Protocol master. |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 145 | * @cntrlr_info: Platform specific data for the controller this driver manages. |
| 146 | * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint. |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 147 | * @queue: To log SPI xfer requests. |
| 148 | * @lock: Controller specific lock. |
| 149 | * @state: Set of FLAGS to indicate status. |
| 150 | * @rx_dmach: Controller's DMA channel for Rx. |
| 151 | * @tx_dmach: Controller's DMA channel for Tx. |
| 152 | * @sfr_start: BUS address of SPI controller regs. |
| 153 | * @regs: Pointer to ioremap'ed controller registers. |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 154 | * @irq: interrupt |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 155 | * @xfer_completion: To indicate completion of xfer task. |
| 156 | * @cur_mode: Stores the active configuration of the controller. |
| 157 | * @cur_bpw: Stores the active bits per word settings. |
| 158 | * @cur_speed: Stores the active xfer clock speed. |
| 159 | */ |
| 160 | struct s3c64xx_spi_driver_data { |
| 161 | void __iomem *regs; |
| 162 | struct clk *clk; |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 163 | struct clk *src_clk; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 164 | struct platform_device *pdev; |
| 165 | struct spi_master *master; |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 166 | struct s3c64xx_spi_info *cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 167 | struct spi_device *tgl_spi; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 168 | struct list_head queue; |
| 169 | spinlock_t lock; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 170 | unsigned long sfr_start; |
| 171 | struct completion xfer_completion; |
| 172 | unsigned state; |
| 173 | unsigned cur_mode, cur_bpw; |
| 174 | unsigned cur_speed; |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 175 | struct s3c64xx_spi_dma_data rx_dma; |
| 176 | struct s3c64xx_spi_dma_data tx_dma; |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 177 | struct samsung_dma_ops *ops; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | static struct s3c2410_dma_client s3c64xx_spi_dma_client = { |
| 181 | .name = "samsung-spi-dma", |
| 182 | }; |
| 183 | |
| 184 | static void flush_fifo(struct s3c64xx_spi_driver_data *sdd) |
| 185 | { |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 186 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 187 | void __iomem *regs = sdd->regs; |
| 188 | unsigned long loops; |
| 189 | u32 val; |
| 190 | |
| 191 | writel(0, regs + S3C64XX_SPI_PACKET_CNT); |
| 192 | |
| 193 | val = readl(regs + S3C64XX_SPI_CH_CFG); |
| 194 | val |= S3C64XX_SPI_CH_SW_RST; |
| 195 | val &= ~S3C64XX_SPI_CH_HS_EN; |
| 196 | writel(val, regs + S3C64XX_SPI_CH_CFG); |
| 197 | |
| 198 | /* Flush TxFIFO*/ |
| 199 | loops = msecs_to_loops(1); |
| 200 | do { |
| 201 | val = readl(regs + S3C64XX_SPI_STATUS); |
| 202 | } while (TX_FIFO_LVL(val, sci) && loops--); |
| 203 | |
Mark Brown | be7852a | 2010-08-23 17:40:56 +0100 | [diff] [blame] | 204 | if (loops == 0) |
| 205 | dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n"); |
| 206 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 207 | /* Flush RxFIFO*/ |
| 208 | loops = msecs_to_loops(1); |
| 209 | do { |
| 210 | val = readl(regs + S3C64XX_SPI_STATUS); |
| 211 | if (RX_FIFO_LVL(val, sci)) |
| 212 | readl(regs + S3C64XX_SPI_RX_DATA); |
| 213 | else |
| 214 | break; |
| 215 | } while (loops--); |
| 216 | |
Mark Brown | be7852a | 2010-08-23 17:40:56 +0100 | [diff] [blame] | 217 | if (loops == 0) |
| 218 | dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n"); |
| 219 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 220 | val = readl(regs + S3C64XX_SPI_CH_CFG); |
| 221 | val &= ~S3C64XX_SPI_CH_SW_RST; |
| 222 | writel(val, regs + S3C64XX_SPI_CH_CFG); |
| 223 | |
| 224 | val = readl(regs + S3C64XX_SPI_MODE_CFG); |
| 225 | val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON); |
| 226 | writel(val, regs + S3C64XX_SPI_MODE_CFG); |
| 227 | |
| 228 | val = readl(regs + S3C64XX_SPI_CH_CFG); |
| 229 | val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON); |
| 230 | writel(val, regs + S3C64XX_SPI_CH_CFG); |
| 231 | } |
| 232 | |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 233 | static void s3c64xx_spi_dmacb(void *data) |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 234 | { |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 235 | struct s3c64xx_spi_driver_data *sdd; |
| 236 | struct s3c64xx_spi_dma_data *dma = data; |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 237 | unsigned long flags; |
| 238 | |
Kyoungil Kim | 054ebcc | 2012-03-10 09:48:46 +0900 | [diff] [blame] | 239 | if (dma->direction == DMA_DEV_TO_MEM) |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 240 | sdd = container_of(data, |
| 241 | struct s3c64xx_spi_driver_data, rx_dma); |
| 242 | else |
| 243 | sdd = container_of(data, |
| 244 | struct s3c64xx_spi_driver_data, tx_dma); |
| 245 | |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 246 | spin_lock_irqsave(&sdd->lock, flags); |
| 247 | |
Kyoungil Kim | 054ebcc | 2012-03-10 09:48:46 +0900 | [diff] [blame] | 248 | if (dma->direction == DMA_DEV_TO_MEM) { |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 249 | sdd->state &= ~RXBUSY; |
| 250 | if (!(sdd->state & TXBUSY)) |
| 251 | complete(&sdd->xfer_completion); |
| 252 | } else { |
| 253 | sdd->state &= ~TXBUSY; |
| 254 | if (!(sdd->state & RXBUSY)) |
| 255 | complete(&sdd->xfer_completion); |
| 256 | } |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 257 | |
| 258 | spin_unlock_irqrestore(&sdd->lock, flags); |
| 259 | } |
| 260 | |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 261 | static void prepare_dma(struct s3c64xx_spi_dma_data *dma, |
| 262 | unsigned len, dma_addr_t buf) |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 263 | { |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 264 | struct s3c64xx_spi_driver_data *sdd; |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 265 | struct samsung_dma_prep info; |
| 266 | struct samsung_dma_config config; |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 267 | |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 268 | if (dma->direction == DMA_DEV_TO_MEM) { |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 269 | sdd = container_of((void *)dma, |
| 270 | struct s3c64xx_spi_driver_data, rx_dma); |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 271 | config.direction = sdd->rx_dma.direction; |
| 272 | config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA; |
| 273 | config.width = sdd->cur_bpw / 8; |
| 274 | sdd->ops->config(sdd->rx_dma.ch, &config); |
| 275 | } else { |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 276 | sdd = container_of((void *)dma, |
| 277 | struct s3c64xx_spi_driver_data, tx_dma); |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 278 | config.direction = sdd->tx_dma.direction; |
| 279 | config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA; |
| 280 | config.width = sdd->cur_bpw / 8; |
| 281 | sdd->ops->config(sdd->tx_dma.ch, &config); |
| 282 | } |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 283 | |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 284 | info.cap = DMA_SLAVE; |
| 285 | info.len = len; |
| 286 | info.fp = s3c64xx_spi_dmacb; |
| 287 | info.fp_param = dma; |
| 288 | info.direction = dma->direction; |
| 289 | info.buf = buf; |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 290 | |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 291 | sdd->ops->prepare(dma->ch, &info); |
| 292 | sdd->ops->trigger(dma->ch); |
| 293 | } |
| 294 | |
| 295 | static int acquire_dma(struct s3c64xx_spi_driver_data *sdd) |
| 296 | { |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 297 | struct samsung_dma_req req; |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 298 | |
| 299 | sdd->ops = samsung_dma_get_ops(); |
| 300 | |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 301 | req.cap = DMA_SLAVE; |
| 302 | req.client = &s3c64xx_spi_dma_client; |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 303 | |
Boojin Kim | 4969c32 | 2012-06-19 13:27:03 +0900 | [diff] [blame^] | 304 | sdd->rx_dma.ch = sdd->ops->request(sdd->rx_dma.dmach, &req); |
| 305 | sdd->tx_dma.ch = sdd->ops->request(sdd->tx_dma.dmach, &req); |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 306 | |
| 307 | return 1; |
Boojin Kim | 39d3e80 | 2011-09-02 09:44:41 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 310 | static void enable_datapath(struct s3c64xx_spi_driver_data *sdd, |
| 311 | struct spi_device *spi, |
| 312 | struct spi_transfer *xfer, int dma_mode) |
| 313 | { |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 314 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 315 | void __iomem *regs = sdd->regs; |
| 316 | u32 modecfg, chcfg; |
| 317 | |
| 318 | modecfg = readl(regs + S3C64XX_SPI_MODE_CFG); |
| 319 | modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON); |
| 320 | |
| 321 | chcfg = readl(regs + S3C64XX_SPI_CH_CFG); |
| 322 | chcfg &= ~S3C64XX_SPI_CH_TXCH_ON; |
| 323 | |
| 324 | if (dma_mode) { |
| 325 | chcfg &= ~S3C64XX_SPI_CH_RXCH_ON; |
| 326 | } else { |
| 327 | /* Always shift in data in FIFO, even if xfer is Tx only, |
| 328 | * this helps setting PCKT_CNT value for generating clocks |
| 329 | * as exactly needed. |
| 330 | */ |
| 331 | chcfg |= S3C64XX_SPI_CH_RXCH_ON; |
| 332 | writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff) |
| 333 | | S3C64XX_SPI_PACKET_CNT_EN, |
| 334 | regs + S3C64XX_SPI_PACKET_CNT); |
| 335 | } |
| 336 | |
| 337 | if (xfer->tx_buf != NULL) { |
| 338 | sdd->state |= TXBUSY; |
| 339 | chcfg |= S3C64XX_SPI_CH_TXCH_ON; |
| 340 | if (dma_mode) { |
| 341 | modecfg |= S3C64XX_SPI_MODE_TXDMA_ON; |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 342 | prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 343 | } else { |
Jassi Brar | 0c92ecf | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 344 | switch (sdd->cur_bpw) { |
| 345 | case 32: |
| 346 | iowrite32_rep(regs + S3C64XX_SPI_TX_DATA, |
| 347 | xfer->tx_buf, xfer->len / 4); |
| 348 | break; |
| 349 | case 16: |
| 350 | iowrite16_rep(regs + S3C64XX_SPI_TX_DATA, |
| 351 | xfer->tx_buf, xfer->len / 2); |
| 352 | break; |
| 353 | default: |
| 354 | iowrite8_rep(regs + S3C64XX_SPI_TX_DATA, |
| 355 | xfer->tx_buf, xfer->len); |
| 356 | break; |
| 357 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | if (xfer->rx_buf != NULL) { |
| 362 | sdd->state |= RXBUSY; |
| 363 | |
| 364 | if (sci->high_speed && sdd->cur_speed >= 30000000UL |
| 365 | && !(sdd->cur_mode & SPI_CPHA)) |
| 366 | chcfg |= S3C64XX_SPI_CH_HS_EN; |
| 367 | |
| 368 | if (dma_mode) { |
| 369 | modecfg |= S3C64XX_SPI_MODE_RXDMA_ON; |
| 370 | chcfg |= S3C64XX_SPI_CH_RXCH_ON; |
| 371 | writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff) |
| 372 | | S3C64XX_SPI_PACKET_CNT_EN, |
| 373 | regs + S3C64XX_SPI_PACKET_CNT); |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 374 | prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | writel(modecfg, regs + S3C64XX_SPI_MODE_CFG); |
| 379 | writel(chcfg, regs + S3C64XX_SPI_CH_CFG); |
| 380 | } |
| 381 | |
| 382 | static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd, |
| 383 | struct spi_device *spi) |
| 384 | { |
| 385 | struct s3c64xx_spi_csinfo *cs; |
| 386 | |
| 387 | if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */ |
| 388 | if (sdd->tgl_spi != spi) { /* if last mssg on diff device */ |
| 389 | /* Deselect the last toggled device */ |
| 390 | cs = sdd->tgl_spi->controller_data; |
Jassi Brar | fa0fcde | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 391 | cs->set_level(cs->line, |
| 392 | spi->mode & SPI_CS_HIGH ? 0 : 1); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 393 | } |
| 394 | sdd->tgl_spi = NULL; |
| 395 | } |
| 396 | |
| 397 | cs = spi->controller_data; |
Jassi Brar | fa0fcde | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 398 | cs->set_level(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd, |
| 402 | struct spi_transfer *xfer, int dma_mode) |
| 403 | { |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 404 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 405 | void __iomem *regs = sdd->regs; |
| 406 | unsigned long val; |
| 407 | int ms; |
| 408 | |
| 409 | /* millisecs to xfer 'len' bytes @ 'cur_speed' */ |
| 410 | ms = xfer->len * 8 * 1000 / sdd->cur_speed; |
Mark Brown | 9d8f86b | 2010-09-07 16:37:52 +0100 | [diff] [blame] | 411 | ms += 10; /* some tolerance */ |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 412 | |
| 413 | if (dma_mode) { |
| 414 | val = msecs_to_jiffies(ms) + 10; |
| 415 | val = wait_for_completion_timeout(&sdd->xfer_completion, val); |
| 416 | } else { |
Jassi Brar | c3f139b | 2010-09-03 10:36:46 +0900 | [diff] [blame] | 417 | u32 status; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 418 | val = msecs_to_loops(ms); |
| 419 | do { |
Jassi Brar | c3f139b | 2010-09-03 10:36:46 +0900 | [diff] [blame] | 420 | status = readl(regs + S3C64XX_SPI_STATUS); |
| 421 | } while (RX_FIFO_LVL(status, sci) < xfer->len && --val); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (!val) |
| 425 | return -EIO; |
| 426 | |
| 427 | if (dma_mode) { |
| 428 | u32 status; |
| 429 | |
| 430 | /* |
| 431 | * DmaTx returns after simply writing data in the FIFO, |
| 432 | * w/o waiting for real transmission on the bus to finish. |
| 433 | * DmaRx returns only after Dma read data from FIFO which |
| 434 | * needs bus transmission to finish, so we don't worry if |
| 435 | * Xfer involved Rx(with or without Tx). |
| 436 | */ |
| 437 | if (xfer->rx_buf == NULL) { |
| 438 | val = msecs_to_loops(10); |
| 439 | status = readl(regs + S3C64XX_SPI_STATUS); |
| 440 | while ((TX_FIFO_LVL(status, sci) |
| 441 | || !S3C64XX_SPI_ST_TX_DONE(status, sci)) |
| 442 | && --val) { |
| 443 | cpu_relax(); |
| 444 | status = readl(regs + S3C64XX_SPI_STATUS); |
| 445 | } |
| 446 | |
| 447 | if (!val) |
| 448 | return -EIO; |
| 449 | } |
| 450 | } else { |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 451 | /* If it was only Tx */ |
| 452 | if (xfer->rx_buf == NULL) { |
| 453 | sdd->state &= ~TXBUSY; |
| 454 | return 0; |
| 455 | } |
| 456 | |
Jassi Brar | 0c92ecf | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 457 | switch (sdd->cur_bpw) { |
| 458 | case 32: |
| 459 | ioread32_rep(regs + S3C64XX_SPI_RX_DATA, |
| 460 | xfer->rx_buf, xfer->len / 4); |
| 461 | break; |
| 462 | case 16: |
| 463 | ioread16_rep(regs + S3C64XX_SPI_RX_DATA, |
| 464 | xfer->rx_buf, xfer->len / 2); |
| 465 | break; |
| 466 | default: |
| 467 | ioread8_rep(regs + S3C64XX_SPI_RX_DATA, |
| 468 | xfer->rx_buf, xfer->len); |
| 469 | break; |
| 470 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 471 | sdd->state &= ~RXBUSY; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd, |
| 478 | struct spi_device *spi) |
| 479 | { |
| 480 | struct s3c64xx_spi_csinfo *cs = spi->controller_data; |
| 481 | |
| 482 | if (sdd->tgl_spi == spi) |
| 483 | sdd->tgl_spi = NULL; |
| 484 | |
Jassi Brar | fa0fcde | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 485 | cs->set_level(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd) |
| 489 | { |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 490 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 491 | void __iomem *regs = sdd->regs; |
| 492 | u32 val; |
| 493 | |
| 494 | /* Disable Clock */ |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 495 | if (sci->clk_from_cmu) { |
| 496 | clk_disable(sdd->src_clk); |
| 497 | } else { |
| 498 | val = readl(regs + S3C64XX_SPI_CLK_CFG); |
| 499 | val &= ~S3C64XX_SPI_ENCLK_ENABLE; |
| 500 | writel(val, regs + S3C64XX_SPI_CLK_CFG); |
| 501 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 502 | |
| 503 | /* Set Polarity and Phase */ |
| 504 | val = readl(regs + S3C64XX_SPI_CH_CFG); |
| 505 | val &= ~(S3C64XX_SPI_CH_SLAVE | |
| 506 | S3C64XX_SPI_CPOL_L | |
| 507 | S3C64XX_SPI_CPHA_B); |
| 508 | |
| 509 | if (sdd->cur_mode & SPI_CPOL) |
| 510 | val |= S3C64XX_SPI_CPOL_L; |
| 511 | |
| 512 | if (sdd->cur_mode & SPI_CPHA) |
| 513 | val |= S3C64XX_SPI_CPHA_B; |
| 514 | |
| 515 | writel(val, regs + S3C64XX_SPI_CH_CFG); |
| 516 | |
| 517 | /* Set Channel & DMA Mode */ |
| 518 | val = readl(regs + S3C64XX_SPI_MODE_CFG); |
| 519 | val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK |
| 520 | | S3C64XX_SPI_MODE_CH_TSZ_MASK); |
| 521 | |
| 522 | switch (sdd->cur_bpw) { |
| 523 | case 32: |
| 524 | val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD; |
Jassi Brar | 0c92ecf | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 525 | val |= S3C64XX_SPI_MODE_CH_TSZ_WORD; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 526 | break; |
| 527 | case 16: |
| 528 | val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD; |
Jassi Brar | 0c92ecf | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 529 | val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 530 | break; |
| 531 | default: |
| 532 | val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE; |
Jassi Brar | 0c92ecf | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 533 | val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 534 | break; |
| 535 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 536 | |
| 537 | writel(val, regs + S3C64XX_SPI_MODE_CFG); |
| 538 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 539 | if (sci->clk_from_cmu) { |
| 540 | /* Configure Clock */ |
| 541 | /* There is half-multiplier before the SPI */ |
| 542 | clk_set_rate(sdd->src_clk, sdd->cur_speed * 2); |
| 543 | /* Enable Clock */ |
| 544 | clk_enable(sdd->src_clk); |
| 545 | } else { |
| 546 | /* Configure Clock */ |
| 547 | val = readl(regs + S3C64XX_SPI_CLK_CFG); |
| 548 | val &= ~S3C64XX_SPI_PSR_MASK; |
| 549 | val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1) |
| 550 | & S3C64XX_SPI_PSR_MASK); |
| 551 | writel(val, regs + S3C64XX_SPI_CLK_CFG); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 552 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 553 | /* Enable Clock */ |
| 554 | val = readl(regs + S3C64XX_SPI_CLK_CFG); |
| 555 | val |= S3C64XX_SPI_ENCLK_ENABLE; |
| 556 | writel(val, regs + S3C64XX_SPI_CLK_CFG); |
| 557 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 560 | #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32) |
| 561 | |
| 562 | static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd, |
| 563 | struct spi_message *msg) |
| 564 | { |
Jassi Brar | e02ddd4 | 2010-09-29 17:31:31 +0900 | [diff] [blame] | 565 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 566 | struct device *dev = &sdd->pdev->dev; |
| 567 | struct spi_transfer *xfer; |
| 568 | |
| 569 | if (msg->is_dma_mapped) |
| 570 | return 0; |
| 571 | |
| 572 | /* First mark all xfer unmapped */ |
| 573 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 574 | xfer->rx_dma = XFER_DMAADDR_INVALID; |
| 575 | xfer->tx_dma = XFER_DMAADDR_INVALID; |
| 576 | } |
| 577 | |
| 578 | /* Map until end or first fail */ |
| 579 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 580 | |
Jassi Brar | e02ddd4 | 2010-09-29 17:31:31 +0900 | [diff] [blame] | 581 | if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1)) |
| 582 | continue; |
| 583 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 584 | if (xfer->tx_buf != NULL) { |
Jassi Brar | 251ee47 | 2010-09-03 10:36:26 +0900 | [diff] [blame] | 585 | xfer->tx_dma = dma_map_single(dev, |
| 586 | (void *)xfer->tx_buf, xfer->len, |
| 587 | DMA_TO_DEVICE); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 588 | if (dma_mapping_error(dev, xfer->tx_dma)) { |
| 589 | dev_err(dev, "dma_map_single Tx failed\n"); |
| 590 | xfer->tx_dma = XFER_DMAADDR_INVALID; |
| 591 | return -ENOMEM; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (xfer->rx_buf != NULL) { |
| 596 | xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, |
| 597 | xfer->len, DMA_FROM_DEVICE); |
| 598 | if (dma_mapping_error(dev, xfer->rx_dma)) { |
| 599 | dev_err(dev, "dma_map_single Rx failed\n"); |
| 600 | dma_unmap_single(dev, xfer->tx_dma, |
| 601 | xfer->len, DMA_TO_DEVICE); |
| 602 | xfer->tx_dma = XFER_DMAADDR_INVALID; |
| 603 | xfer->rx_dma = XFER_DMAADDR_INVALID; |
| 604 | return -ENOMEM; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd, |
| 613 | struct spi_message *msg) |
| 614 | { |
Jassi Brar | e02ddd4 | 2010-09-29 17:31:31 +0900 | [diff] [blame] | 615 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 616 | struct device *dev = &sdd->pdev->dev; |
| 617 | struct spi_transfer *xfer; |
| 618 | |
| 619 | if (msg->is_dma_mapped) |
| 620 | return; |
| 621 | |
| 622 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 623 | |
Jassi Brar | e02ddd4 | 2010-09-29 17:31:31 +0900 | [diff] [blame] | 624 | if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1)) |
| 625 | continue; |
| 626 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 627 | if (xfer->rx_buf != NULL |
| 628 | && xfer->rx_dma != XFER_DMAADDR_INVALID) |
| 629 | dma_unmap_single(dev, xfer->rx_dma, |
| 630 | xfer->len, DMA_FROM_DEVICE); |
| 631 | |
| 632 | if (xfer->tx_buf != NULL |
| 633 | && xfer->tx_dma != XFER_DMAADDR_INVALID) |
| 634 | dma_unmap_single(dev, xfer->tx_dma, |
| 635 | xfer->len, DMA_TO_DEVICE); |
| 636 | } |
| 637 | } |
| 638 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 639 | static int s3c64xx_spi_transfer_one_message(struct spi_master *master, |
| 640 | struct spi_message *msg) |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 641 | { |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 642 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master); |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 643 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 644 | struct spi_device *spi = msg->spi; |
| 645 | struct s3c64xx_spi_csinfo *cs = spi->controller_data; |
| 646 | struct spi_transfer *xfer; |
| 647 | int status = 0, cs_toggle = 0; |
| 648 | u32 speed; |
| 649 | u8 bpw; |
| 650 | |
| 651 | /* If Master's(controller) state differs from that needed by Slave */ |
| 652 | if (sdd->cur_speed != spi->max_speed_hz |
| 653 | || sdd->cur_mode != spi->mode |
| 654 | || sdd->cur_bpw != spi->bits_per_word) { |
| 655 | sdd->cur_bpw = spi->bits_per_word; |
| 656 | sdd->cur_speed = spi->max_speed_hz; |
| 657 | sdd->cur_mode = spi->mode; |
| 658 | s3c64xx_spi_config(sdd); |
| 659 | } |
| 660 | |
| 661 | /* Map all the transfers if needed */ |
| 662 | if (s3c64xx_spi_map_mssg(sdd, msg)) { |
| 663 | dev_err(&spi->dev, |
| 664 | "Xfer: Unable to map message buffers!\n"); |
| 665 | status = -ENOMEM; |
| 666 | goto out; |
| 667 | } |
| 668 | |
| 669 | /* Configure feedback delay */ |
| 670 | writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK); |
| 671 | |
| 672 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 673 | |
| 674 | unsigned long flags; |
| 675 | int use_dma; |
| 676 | |
| 677 | INIT_COMPLETION(sdd->xfer_completion); |
| 678 | |
| 679 | /* Only BPW and Speed may change across transfers */ |
| 680 | bpw = xfer->bits_per_word ? : spi->bits_per_word; |
| 681 | speed = xfer->speed_hz ? : spi->max_speed_hz; |
| 682 | |
Jassi Brar | 0c92ecf | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 683 | if (xfer->len % (bpw / 8)) { |
| 684 | dev_err(&spi->dev, |
| 685 | "Xfer length(%u) not a multiple of word size(%u)\n", |
| 686 | xfer->len, bpw / 8); |
| 687 | status = -EIO; |
| 688 | goto out; |
| 689 | } |
| 690 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 691 | if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) { |
| 692 | sdd->cur_bpw = bpw; |
| 693 | sdd->cur_speed = speed; |
| 694 | s3c64xx_spi_config(sdd); |
| 695 | } |
| 696 | |
| 697 | /* Polling method for xfers not bigger than FIFO capacity */ |
| 698 | if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1)) |
| 699 | use_dma = 0; |
| 700 | else |
| 701 | use_dma = 1; |
| 702 | |
| 703 | spin_lock_irqsave(&sdd->lock, flags); |
| 704 | |
| 705 | /* Pending only which is to be done */ |
| 706 | sdd->state &= ~RXBUSY; |
| 707 | sdd->state &= ~TXBUSY; |
| 708 | |
| 709 | enable_datapath(sdd, spi, xfer, use_dma); |
| 710 | |
| 711 | /* Slave Select */ |
| 712 | enable_cs(sdd, spi); |
| 713 | |
| 714 | /* Start the signals */ |
| 715 | S3C64XX_SPI_ACT(sdd); |
| 716 | |
| 717 | spin_unlock_irqrestore(&sdd->lock, flags); |
| 718 | |
| 719 | status = wait_for_xfer(sdd, xfer, use_dma); |
| 720 | |
| 721 | /* Quiese the signals */ |
| 722 | S3C64XX_SPI_DEACT(sdd); |
| 723 | |
| 724 | if (status) { |
Joe Perches | 8a349d4 | 2010-02-02 07:22:13 +0000 | [diff] [blame] | 725 | dev_err(&spi->dev, "I/O Error: " |
| 726 | "rx-%d tx-%d res:rx-%c tx-%c len-%d\n", |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 727 | xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0, |
| 728 | (sdd->state & RXBUSY) ? 'f' : 'p', |
| 729 | (sdd->state & TXBUSY) ? 'f' : 'p', |
| 730 | xfer->len); |
| 731 | |
| 732 | if (use_dma) { |
| 733 | if (xfer->tx_buf != NULL |
| 734 | && (sdd->state & TXBUSY)) |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 735 | sdd->ops->stop(sdd->tx_dma.ch); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 736 | if (xfer->rx_buf != NULL |
| 737 | && (sdd->state & RXBUSY)) |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 738 | sdd->ops->stop(sdd->rx_dma.ch); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | goto out; |
| 742 | } |
| 743 | |
| 744 | if (xfer->delay_usecs) |
| 745 | udelay(xfer->delay_usecs); |
| 746 | |
| 747 | if (xfer->cs_change) { |
| 748 | /* Hint that the next mssg is gonna be |
| 749 | for the same device */ |
| 750 | if (list_is_last(&xfer->transfer_list, |
| 751 | &msg->transfers)) |
| 752 | cs_toggle = 1; |
| 753 | else |
| 754 | disable_cs(sdd, spi); |
| 755 | } |
| 756 | |
| 757 | msg->actual_length += xfer->len; |
| 758 | |
| 759 | flush_fifo(sdd); |
| 760 | } |
| 761 | |
| 762 | out: |
| 763 | if (!cs_toggle || status) |
| 764 | disable_cs(sdd, spi); |
| 765 | else |
| 766 | sdd->tgl_spi = spi; |
| 767 | |
| 768 | s3c64xx_spi_unmap_mssg(sdd, msg); |
| 769 | |
| 770 | msg->status = status; |
| 771 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 772 | spi_finalize_current_message(master); |
| 773 | |
| 774 | return 0; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 777 | static int s3c64xx_spi_prepare_transfer(struct spi_master *spi) |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 778 | { |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 779 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 780 | |
| 781 | /* Acquire DMA channels */ |
| 782 | while (!acquire_dma(sdd)) |
| 783 | msleep(10); |
| 784 | |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 785 | pm_runtime_get_sync(&sdd->pdev->dev); |
| 786 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 787 | return 0; |
| 788 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 789 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 790 | static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi) |
| 791 | { |
| 792 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 793 | |
| 794 | /* Free DMA channels */ |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 795 | sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client); |
| 796 | sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client); |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 797 | |
| 798 | pm_runtime_put(&sdd->pdev->dev); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * Here we only check the validity of requested configuration |
| 805 | * and save the configuration in a local data-structure. |
| 806 | * The controller is actually configured only just before we |
| 807 | * get a message to transfer. |
| 808 | */ |
| 809 | static int s3c64xx_spi_setup(struct spi_device *spi) |
| 810 | { |
| 811 | struct s3c64xx_spi_csinfo *cs = spi->controller_data; |
| 812 | struct s3c64xx_spi_driver_data *sdd; |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 813 | struct s3c64xx_spi_info *sci; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 814 | struct spi_message *msg; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 815 | unsigned long flags; |
| 816 | int err = 0; |
| 817 | |
| 818 | if (cs == NULL || cs->set_level == NULL) { |
| 819 | dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select); |
| 820 | return -ENODEV; |
| 821 | } |
| 822 | |
| 823 | sdd = spi_master_get_devdata(spi->master); |
| 824 | sci = sdd->cntrlr_info; |
| 825 | |
| 826 | spin_lock_irqsave(&sdd->lock, flags); |
| 827 | |
| 828 | list_for_each_entry(msg, &sdd->queue, queue) { |
| 829 | /* Is some mssg is already queued for this device */ |
| 830 | if (msg->spi == spi) { |
| 831 | dev_err(&spi->dev, |
| 832 | "setup: attempt while mssg in queue!\n"); |
| 833 | spin_unlock_irqrestore(&sdd->lock, flags); |
| 834 | return -EBUSY; |
| 835 | } |
| 836 | } |
| 837 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 838 | spin_unlock_irqrestore(&sdd->lock, flags); |
| 839 | |
| 840 | if (spi->bits_per_word != 8 |
| 841 | && spi->bits_per_word != 16 |
| 842 | && spi->bits_per_word != 32) { |
| 843 | dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n", |
| 844 | spi->bits_per_word); |
| 845 | err = -EINVAL; |
| 846 | goto setup_exit; |
| 847 | } |
| 848 | |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 849 | pm_runtime_get_sync(&sdd->pdev->dev); |
| 850 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 851 | /* Check if we can provide the requested rate */ |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 852 | if (!sci->clk_from_cmu) { |
| 853 | u32 psr, speed; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 854 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 855 | /* Max possible */ |
| 856 | speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 857 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 858 | if (spi->max_speed_hz > speed) |
| 859 | spi->max_speed_hz = speed; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 860 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 861 | psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1; |
| 862 | psr &= S3C64XX_SPI_PSR_MASK; |
| 863 | if (psr == S3C64XX_SPI_PSR_MASK) |
| 864 | psr--; |
| 865 | |
| 866 | speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1); |
| 867 | if (spi->max_speed_hz < speed) { |
| 868 | if (psr+1 < S3C64XX_SPI_PSR_MASK) { |
| 869 | psr++; |
| 870 | } else { |
| 871 | err = -EINVAL; |
| 872 | goto setup_exit; |
| 873 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 874 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 875 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 876 | speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1); |
| 877 | if (spi->max_speed_hz >= speed) |
| 878 | spi->max_speed_hz = speed; |
| 879 | else |
| 880 | err = -EINVAL; |
| 881 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 882 | |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 883 | pm_runtime_put(&sdd->pdev->dev); |
| 884 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 885 | setup_exit: |
| 886 | |
| 887 | /* setup() returns with device de-selected */ |
| 888 | disable_cs(sdd, spi); |
| 889 | |
| 890 | return err; |
| 891 | } |
| 892 | |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 893 | static irqreturn_t s3c64xx_spi_irq(int irq, void *data) |
| 894 | { |
| 895 | struct s3c64xx_spi_driver_data *sdd = data; |
| 896 | struct spi_master *spi = sdd->master; |
| 897 | unsigned int val; |
| 898 | |
| 899 | val = readl(sdd->regs + S3C64XX_SPI_PENDING_CLR); |
| 900 | |
| 901 | val &= S3C64XX_SPI_PND_RX_OVERRUN_CLR | |
| 902 | S3C64XX_SPI_PND_RX_UNDERRUN_CLR | |
| 903 | S3C64XX_SPI_PND_TX_OVERRUN_CLR | |
| 904 | S3C64XX_SPI_PND_TX_UNDERRUN_CLR; |
| 905 | |
| 906 | writel(val, sdd->regs + S3C64XX_SPI_PENDING_CLR); |
| 907 | |
| 908 | if (val & S3C64XX_SPI_PND_RX_OVERRUN_CLR) |
| 909 | dev_err(&spi->dev, "RX overrun\n"); |
| 910 | if (val & S3C64XX_SPI_PND_RX_UNDERRUN_CLR) |
| 911 | dev_err(&spi->dev, "RX underrun\n"); |
| 912 | if (val & S3C64XX_SPI_PND_TX_OVERRUN_CLR) |
| 913 | dev_err(&spi->dev, "TX overrun\n"); |
| 914 | if (val & S3C64XX_SPI_PND_TX_UNDERRUN_CLR) |
| 915 | dev_err(&spi->dev, "TX underrun\n"); |
| 916 | |
| 917 | return IRQ_HANDLED; |
| 918 | } |
| 919 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 920 | static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel) |
| 921 | { |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 922 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 923 | void __iomem *regs = sdd->regs; |
| 924 | unsigned int val; |
| 925 | |
| 926 | sdd->cur_speed = 0; |
| 927 | |
| 928 | S3C64XX_SPI_DEACT(sdd); |
| 929 | |
| 930 | /* Disable Interrupts - we use Polling if not DMA mode */ |
| 931 | writel(0, regs + S3C64XX_SPI_INT_EN); |
| 932 | |
Jassi Brar | b42a81c | 2010-09-29 17:31:33 +0900 | [diff] [blame] | 933 | if (!sci->clk_from_cmu) |
| 934 | writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT, |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 935 | regs + S3C64XX_SPI_CLK_CFG); |
| 936 | writel(0, regs + S3C64XX_SPI_MODE_CFG); |
| 937 | writel(0, regs + S3C64XX_SPI_PACKET_CNT); |
| 938 | |
| 939 | /* Clear any irq pending bits */ |
| 940 | writel(readl(regs + S3C64XX_SPI_PENDING_CLR), |
| 941 | regs + S3C64XX_SPI_PENDING_CLR); |
| 942 | |
| 943 | writel(0, regs + S3C64XX_SPI_SWAP_CFG); |
| 944 | |
| 945 | val = readl(regs + S3C64XX_SPI_MODE_CFG); |
| 946 | val &= ~S3C64XX_SPI_MODE_4BURST; |
| 947 | val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF); |
| 948 | val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF); |
| 949 | writel(val, regs + S3C64XX_SPI_MODE_CFG); |
| 950 | |
| 951 | flush_fifo(sdd); |
| 952 | } |
| 953 | |
| 954 | static int __init s3c64xx_spi_probe(struct platform_device *pdev) |
| 955 | { |
| 956 | struct resource *mem_res, *dmatx_res, *dmarx_res; |
| 957 | struct s3c64xx_spi_driver_data *sdd; |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 958 | struct s3c64xx_spi_info *sci; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 959 | struct spi_master *master; |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 960 | int ret, irq; |
Padmavathi Venna | a24d850 | 2011-11-02 20:04:19 +0900 | [diff] [blame] | 961 | char clk_name[16]; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 962 | |
| 963 | if (pdev->id < 0) { |
| 964 | dev_err(&pdev->dev, |
| 965 | "Invalid platform device id-%d\n", pdev->id); |
| 966 | return -ENODEV; |
| 967 | } |
| 968 | |
| 969 | if (pdev->dev.platform_data == NULL) { |
| 970 | dev_err(&pdev->dev, "platform_data missing!\n"); |
| 971 | return -ENODEV; |
| 972 | } |
| 973 | |
Mark Brown | cc0fc0b | 2010-09-01 08:55:22 -0600 | [diff] [blame] | 974 | sci = pdev->dev.platform_data; |
Mark Brown | cc0fc0b | 2010-09-01 08:55:22 -0600 | [diff] [blame] | 975 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 976 | /* Check for availability of necessary resource */ |
| 977 | |
| 978 | dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 979 | if (dmatx_res == NULL) { |
| 980 | dev_err(&pdev->dev, "Unable to get SPI-Tx dma resource\n"); |
| 981 | return -ENXIO; |
| 982 | } |
| 983 | |
| 984 | dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 985 | if (dmarx_res == NULL) { |
| 986 | dev_err(&pdev->dev, "Unable to get SPI-Rx dma resource\n"); |
| 987 | return -ENXIO; |
| 988 | } |
| 989 | |
| 990 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 991 | if (mem_res == NULL) { |
| 992 | dev_err(&pdev->dev, "Unable to get SPI MEM resource\n"); |
| 993 | return -ENXIO; |
| 994 | } |
| 995 | |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 996 | irq = platform_get_irq(pdev, 0); |
| 997 | if (irq < 0) { |
| 998 | dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq); |
| 999 | return irq; |
| 1000 | } |
| 1001 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1002 | master = spi_alloc_master(&pdev->dev, |
| 1003 | sizeof(struct s3c64xx_spi_driver_data)); |
| 1004 | if (master == NULL) { |
| 1005 | dev_err(&pdev->dev, "Unable to allocate SPI Master\n"); |
| 1006 | return -ENOMEM; |
| 1007 | } |
| 1008 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1009 | platform_set_drvdata(pdev, master); |
| 1010 | |
| 1011 | sdd = spi_master_get_devdata(master); |
| 1012 | sdd->master = master; |
| 1013 | sdd->cntrlr_info = sci; |
| 1014 | sdd->pdev = pdev; |
| 1015 | sdd->sfr_start = mem_res->start; |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 1016 | sdd->tx_dma.dmach = dmatx_res->start; |
Kyoungil Kim | 054ebcc | 2012-03-10 09:48:46 +0900 | [diff] [blame] | 1017 | sdd->tx_dma.direction = DMA_MEM_TO_DEV; |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 1018 | sdd->rx_dma.dmach = dmarx_res->start; |
Kyoungil Kim | 054ebcc | 2012-03-10 09:48:46 +0900 | [diff] [blame] | 1019 | sdd->rx_dma.direction = DMA_DEV_TO_MEM; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1020 | |
| 1021 | sdd->cur_bpw = 8; |
| 1022 | |
| 1023 | master->bus_num = pdev->id; |
| 1024 | master->setup = s3c64xx_spi_setup; |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 1025 | master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer; |
| 1026 | master->transfer_one_message = s3c64xx_spi_transfer_one_message; |
| 1027 | master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1028 | master->num_chipselect = sci->num_cs; |
| 1029 | master->dma_alignment = 8; |
| 1030 | /* the spi->mode bits understood by this driver: */ |
| 1031 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 1032 | |
| 1033 | if (request_mem_region(mem_res->start, |
| 1034 | resource_size(mem_res), pdev->name) == NULL) { |
| 1035 | dev_err(&pdev->dev, "Req mem region failed\n"); |
| 1036 | ret = -ENXIO; |
| 1037 | goto err0; |
| 1038 | } |
| 1039 | |
| 1040 | sdd->regs = ioremap(mem_res->start, resource_size(mem_res)); |
| 1041 | if (sdd->regs == NULL) { |
| 1042 | dev_err(&pdev->dev, "Unable to remap IO\n"); |
| 1043 | ret = -ENXIO; |
| 1044 | goto err1; |
| 1045 | } |
| 1046 | |
| 1047 | if (sci->cfg_gpio == NULL || sci->cfg_gpio(pdev)) { |
| 1048 | dev_err(&pdev->dev, "Unable to config gpio\n"); |
| 1049 | ret = -EBUSY; |
| 1050 | goto err2; |
| 1051 | } |
| 1052 | |
| 1053 | /* Setup clocks */ |
| 1054 | sdd->clk = clk_get(&pdev->dev, "spi"); |
| 1055 | if (IS_ERR(sdd->clk)) { |
| 1056 | dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n"); |
| 1057 | ret = PTR_ERR(sdd->clk); |
| 1058 | goto err3; |
| 1059 | } |
| 1060 | |
| 1061 | if (clk_enable(sdd->clk)) { |
| 1062 | dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n"); |
| 1063 | ret = -EBUSY; |
| 1064 | goto err4; |
| 1065 | } |
| 1066 | |
Padmavathi Venna | a24d850 | 2011-11-02 20:04:19 +0900 | [diff] [blame] | 1067 | sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr); |
| 1068 | sdd->src_clk = clk_get(&pdev->dev, clk_name); |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1069 | if (IS_ERR(sdd->src_clk)) { |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1070 | dev_err(&pdev->dev, |
Padmavathi Venna | a24d850 | 2011-11-02 20:04:19 +0900 | [diff] [blame] | 1071 | "Unable to acquire clock '%s'\n", clk_name); |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1072 | ret = PTR_ERR(sdd->src_clk); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1073 | goto err5; |
| 1074 | } |
| 1075 | |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1076 | if (clk_enable(sdd->src_clk)) { |
Padmavathi Venna | a24d850 | 2011-11-02 20:04:19 +0900 | [diff] [blame] | 1077 | dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1078 | ret = -EBUSY; |
| 1079 | goto err6; |
| 1080 | } |
| 1081 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1082 | /* Setup Deufult Mode */ |
| 1083 | s3c64xx_spi_hwinit(sdd, pdev->id); |
| 1084 | |
| 1085 | spin_lock_init(&sdd->lock); |
| 1086 | init_completion(&sdd->xfer_completion); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1087 | INIT_LIST_HEAD(&sdd->queue); |
| 1088 | |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 1089 | ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd); |
| 1090 | if (ret != 0) { |
| 1091 | dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n", |
| 1092 | irq, ret); |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 1093 | goto err7; |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN | |
| 1097 | S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN, |
| 1098 | sdd->regs + S3C64XX_SPI_INT_EN); |
| 1099 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1100 | if (spi_register_master(master)) { |
| 1101 | dev_err(&pdev->dev, "cannot register SPI master\n"); |
| 1102 | ret = -EBUSY; |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 1103 | goto err8; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Joe Perches | 8a349d4 | 2010-02-02 07:22:13 +0000 | [diff] [blame] | 1106 | dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d " |
| 1107 | "with %d Slaves attached\n", |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1108 | pdev->id, master->num_chipselect); |
Joe Perches | 8a349d4 | 2010-02-02 07:22:13 +0000 | [diff] [blame] | 1109 | dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n", |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1110 | mem_res->end, mem_res->start, |
Boojin Kim | 82ab8cd | 2011-09-02 09:44:42 +0900 | [diff] [blame] | 1111 | sdd->rx_dma.dmach, sdd->tx_dma.dmach); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1112 | |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 1113 | pm_runtime_enable(&pdev->dev); |
| 1114 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1115 | return 0; |
| 1116 | |
| 1117 | err8: |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 1118 | free_irq(irq, sdd); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1119 | err7: |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1120 | clk_disable(sdd->src_clk); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1121 | err6: |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1122 | clk_put(sdd->src_clk); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1123 | err5: |
| 1124 | clk_disable(sdd->clk); |
| 1125 | err4: |
| 1126 | clk_put(sdd->clk); |
| 1127 | err3: |
| 1128 | err2: |
| 1129 | iounmap((void *) sdd->regs); |
| 1130 | err1: |
| 1131 | release_mem_region(mem_res->start, resource_size(mem_res)); |
| 1132 | err0: |
| 1133 | platform_set_drvdata(pdev, NULL); |
| 1134 | spi_master_put(master); |
| 1135 | |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | static int s3c64xx_spi_remove(struct platform_device *pdev) |
| 1140 | { |
| 1141 | struct spi_master *master = spi_master_get(platform_get_drvdata(pdev)); |
| 1142 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1143 | struct resource *mem_res; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1144 | |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 1145 | pm_runtime_disable(&pdev->dev); |
| 1146 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1147 | spi_unregister_master(master); |
| 1148 | |
Mark Brown | c257312 | 2011-11-10 10:57:32 +0000 | [diff] [blame] | 1149 | writel(0, sdd->regs + S3C64XX_SPI_INT_EN); |
| 1150 | |
| 1151 | free_irq(platform_get_irq(pdev, 0), sdd); |
| 1152 | |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1153 | clk_disable(sdd->src_clk); |
| 1154 | clk_put(sdd->src_clk); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1155 | |
| 1156 | clk_disable(sdd->clk); |
| 1157 | clk_put(sdd->clk); |
| 1158 | |
| 1159 | iounmap((void *) sdd->regs); |
| 1160 | |
| 1161 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jassi Brar | ef6c680 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1162 | if (mem_res != NULL) |
| 1163 | release_mem_region(mem_res->start, resource_size(mem_res)); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1164 | |
| 1165 | platform_set_drvdata(pdev, NULL); |
| 1166 | spi_master_put(master); |
| 1167 | |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | #ifdef CONFIG_PM |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1172 | static int s3c64xx_spi_suspend(struct device *dev) |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1173 | { |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1174 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1175 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1176 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 1177 | spi_master_suspend(master); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* Disable the clock */ |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1180 | clk_disable(sdd->src_clk); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1181 | clk_disable(sdd->clk); |
| 1182 | |
| 1183 | sdd->cur_speed = 0; /* Output Clock is stopped */ |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1188 | static int s3c64xx_spi_resume(struct device *dev) |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1189 | { |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1190 | struct platform_device *pdev = to_platform_device(dev); |
| 1191 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1192 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master); |
Jassi Brar | ad7de72 | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1193 | struct s3c64xx_spi_info *sci = sdd->cntrlr_info; |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1194 | |
| 1195 | sci->cfg_gpio(pdev); |
| 1196 | |
| 1197 | /* Enable the clock */ |
Jassi Brar | b0d5d6e | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 1198 | clk_enable(sdd->src_clk); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1199 | clk_enable(sdd->clk); |
| 1200 | |
| 1201 | s3c64xx_spi_hwinit(sdd, pdev->id); |
| 1202 | |
Mark Brown | ad2a99a | 2012-02-15 14:48:32 -0800 | [diff] [blame] | 1203 | spi_master_resume(master); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1204 | |
| 1205 | return 0; |
| 1206 | } |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1207 | #endif /* CONFIG_PM */ |
| 1208 | |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 1209 | #ifdef CONFIG_PM_RUNTIME |
| 1210 | static int s3c64xx_spi_runtime_suspend(struct device *dev) |
| 1211 | { |
| 1212 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); |
| 1213 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master); |
| 1214 | |
| 1215 | clk_disable(sdd->clk); |
| 1216 | clk_disable(sdd->src_clk); |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static int s3c64xx_spi_runtime_resume(struct device *dev) |
| 1222 | { |
| 1223 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); |
| 1224 | struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master); |
| 1225 | |
| 1226 | clk_enable(sdd->src_clk); |
| 1227 | clk_enable(sdd->clk); |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | #endif /* CONFIG_PM_RUNTIME */ |
| 1232 | |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1233 | static const struct dev_pm_ops s3c64xx_spi_pm = { |
| 1234 | SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume) |
Mark Brown | b97b662 | 2011-12-04 00:58:06 +0000 | [diff] [blame] | 1235 | SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend, |
| 1236 | s3c64xx_spi_runtime_resume, NULL) |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1237 | }; |
| 1238 | |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1239 | static struct platform_driver s3c64xx_spi_driver = { |
| 1240 | .driver = { |
| 1241 | .name = "s3c64xx-spi", |
| 1242 | .owner = THIS_MODULE, |
Mark Brown | e25d0bf | 2011-12-04 00:36:18 +0000 | [diff] [blame] | 1243 | .pm = &s3c64xx_spi_pm, |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1244 | }, |
| 1245 | .remove = s3c64xx_spi_remove, |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1246 | }; |
| 1247 | MODULE_ALIAS("platform:s3c64xx-spi"); |
| 1248 | |
| 1249 | static int __init s3c64xx_spi_init(void) |
| 1250 | { |
| 1251 | return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe); |
| 1252 | } |
Mark Brown | d2a787f | 2010-09-07 11:29:17 +0100 | [diff] [blame] | 1253 | subsys_initcall(s3c64xx_spi_init); |
Jassi Brar | 230d42d | 2009-11-30 07:39:42 +0000 | [diff] [blame] | 1254 | |
| 1255 | static void __exit s3c64xx_spi_exit(void) |
| 1256 | { |
| 1257 | platform_driver_unregister(&s3c64xx_spi_driver); |
| 1258 | } |
| 1259 | module_exit(s3c64xx_spi_exit); |
| 1260 | |
| 1261 | MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>"); |
| 1262 | MODULE_DESCRIPTION("S3C64XX SPI Controller Driver"); |
| 1263 | MODULE_LICENSE("GPL"); |