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